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
2239 static cp_parameter_declarator
*cp_parser_parameter_declaration
2240 (cp_parser
*, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser
*, bool);
2243 static void cp_parser_function_body
2244 (cp_parser
*, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser
*, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser
*, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser
*, bool*);
2251 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2252 (cp_parser
*, bool *);
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser
*, bool);
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser
*, tree
);
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser
*, tree
);
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser
*, tree
);
2265 static tree finish_fully_implicit_template
2266 (cp_parser
*, tree
);
2267 static void abort_fully_implicit_template
2270 /* Classes [gram.class] */
2272 static tree cp_parser_class_name
2273 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2276 static tree cp_parser_class_head
2277 (cp_parser
*, bool *);
2278 static enum tag_types cp_parser_class_key
2280 static void cp_parser_type_parameter_key
2281 (cp_parser
* parser
);
2282 static void cp_parser_member_specification_opt
2284 static void cp_parser_member_declaration
2286 static tree cp_parser_pure_specifier
2288 static tree cp_parser_constant_initializer
2291 /* Derived classes [gram.class.derived] */
2293 static tree cp_parser_base_clause
2295 static tree cp_parser_base_specifier
2298 /* Special member functions [gram.special] */
2300 static tree cp_parser_conversion_function_id
2302 static tree cp_parser_conversion_type_id
2304 static cp_declarator
*cp_parser_conversion_declarator_opt
2306 static void cp_parser_ctor_initializer_opt
2308 static void cp_parser_mem_initializer_list
2310 static tree cp_parser_mem_initializer
2312 static tree cp_parser_mem_initializer_id
2315 /* Overloading [gram.over] */
2317 static cp_expr cp_parser_operator_function_id
2319 static cp_expr cp_parser_operator
2322 /* Templates [gram.temp] */
2324 static void cp_parser_template_declaration
2325 (cp_parser
*, bool);
2326 static tree cp_parser_template_parameter_list
2328 static tree cp_parser_template_parameter
2329 (cp_parser
*, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser
*, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser
*, bool, bool, enum tag_types
, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2336 static tree cp_parser_template_argument_list
2338 static tree cp_parser_template_argument
2340 static void cp_parser_explicit_instantiation
2342 static void cp_parser_explicit_specialization
2345 /* Exception handling [gram.exception] */
2347 static tree cp_parser_try_block
2349 static void cp_parser_function_try_block
2351 static void cp_parser_handler_seq
2353 static void cp_parser_handler
2355 static tree cp_parser_exception_declaration
2357 static tree cp_parser_throw_expression
2359 static tree cp_parser_exception_specification_opt
2361 static tree cp_parser_type_id_list
2364 /* GNU Extensions */
2366 static tree cp_parser_asm_specification_opt
2368 static tree cp_parser_asm_operand_list
2370 static tree cp_parser_asm_clobber_list
2372 static tree cp_parser_asm_label_list
2374 static bool cp_next_tokens_can_be_attribute_p
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 static bool cp_next_tokens_can_be_std_attribute_p
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser
*, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser
*, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser
*, size_t);
2386 static tree cp_parser_attributes_opt
2388 static tree cp_parser_gnu_attributes_opt
2390 static tree cp_parser_gnu_attribute_list
2392 static tree cp_parser_std_attribute
2393 (cp_parser
*, tree
);
2394 static tree cp_parser_std_attribute_spec
2396 static tree cp_parser_std_attribute_spec_seq
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser
*, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser
*, int *);
2402 static void cp_parser_label_declaration
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2409 static tree cp_parser_requires_clause_opt
2411 static tree cp_parser_requires_expression
2413 static tree cp_parser_requirement_parameter_list
2415 static tree cp_parser_requirement_body
2417 static tree cp_parser_requirement_list
2419 static tree cp_parser_requirement
2421 static tree cp_parser_simple_requirement
2423 static tree cp_parser_compound_requirement
2425 static tree cp_parser_type_requirement
2427 static tree cp_parser_nested_requirement
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser
*, cp_token
*);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser
*, enum rid
);
2436 static void cp_parser_function_transaction
2437 (cp_parser
*, enum rid
);
2438 static tree cp_parser_transaction_cancel
2441 enum pragma_context
{
2448 static bool cp_parser_pragma
2449 (cp_parser
*, enum pragma_context
, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2455 static tree cp_parser_objc_message_args
2457 static tree cp_parser_objc_message_expression
2459 static cp_expr cp_parser_objc_encode_expression
2461 static tree cp_parser_objc_defs_expression
2463 static tree cp_parser_objc_protocol_expression
2465 static tree cp_parser_objc_selector_expression
2467 static cp_expr cp_parser_objc_expression
2469 static bool cp_parser_objc_selector_p
2471 static tree cp_parser_objc_selector
2473 static tree cp_parser_objc_protocol_refs_opt
2475 static void cp_parser_objc_declaration
2476 (cp_parser
*, tree
);
2477 static tree cp_parser_objc_statement
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser
*, tree
*);
2481 static void cp_parser_objc_at_property_declaration
2483 static void cp_parser_objc_at_synthesize_declaration
2485 static void cp_parser_objc_at_dynamic_declaration
2487 static tree cp_parser_objc_struct_declaration
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser
*, tree
, location_t
);
2496 static tree cp_parser_maybe_treat_template_as_class
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser
*, cp_declarator
*, location_t
);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2502 static cp_expr cp_parser_simple_cast_expression
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser
*, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser
*, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser
*, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser
*, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec
<deferred_access_check
, va_gc
> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser
*, tree
);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2522 static tree cp_parser_save_nsdmi
2524 static tree cp_parser_enclosed_template_argument_list
2526 static void cp_parser_save_default_args
2527 (cp_parser
*, tree
);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser
*, tree
, tree
, tree
);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser
*, tree
);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser
*, tree
);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser
*, enum rid
);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser
*, enum rid
);
2540 static bool cp_parser_declares_only_class_p
2542 static void cp_parser_set_storage_class
2543 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq
*decl_specs
,
2548 cp_decl_spec ds
, cp_token
*);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq
*);
2551 static void cp_parser_required_error
2552 (cp_parser
*, required_token
, bool, location_t
);
2553 static cp_token
*cp_parser_require
2554 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2555 static cp_token
*cp_parser_require_keyword
2556 (cp_parser
*, enum rid
, required_token
);
2557 static bool cp_parser_token_starts_function_definition_p
2559 static bool cp_parser_next_token_starts_class_definition_p
2561 static bool cp_parser_next_token_ends_template_argument_p
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser
*, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2569 static void cp_parser_check_class_key
2570 (enum tag_types
, tree type
);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type
, location_t location
);
2573 static bool cp_parser_optional_template_keyword
2575 static void cp_parser_pre_parsed_nested_name_specifier
2577 static bool cp_parser_cache_group
2578 (cp_parser
*, enum cpp_ttype
, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser
*parser
, bool nsdmi
);
2581 static void cp_parser_parse_tentatively
2583 static void cp_parser_commit_to_tentative_parse
2585 static void cp_parser_commit_to_topmost_tentative_parse
2587 static void cp_parser_abort_tentative_parse
2589 static bool cp_parser_parse_definitely
2591 static inline bool cp_parser_parsing_tentatively
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 static void cp_parser_error
2596 (cp_parser
*, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2599 static bool cp_parser_simulate_error
2601 static bool cp_parser_check_type_definition
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator
*, tree
, location_t type_location
);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser
*, non_integral_constant
);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser
*, tree
, location_t
);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser
*, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2619 static void cp_parser_skip_to_end_of_block_or_statement
2621 static bool cp_parser_skip_to_closing_brace
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser
*, cp_token
*);
2627 static bool cp_parser_error_occurred
2629 static bool cp_parser_allow_gnu_extensions_p
2631 static bool cp_parser_is_pure_string_literal
2633 static bool cp_parser_is_string_literal
2635 static bool cp_parser_is_keyword
2636 (cp_token
*, enum rid
);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser
*, tree
, location_t location
);
2639 static cp_declarator
* cp_parser_make_indirect_declarator
2640 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2641 static bool cp_parser_compound_literal_p
2643 static bool cp_parser_array_designator_p
2645 static bool cp_parser_init_statement_p
2647 static bool cp_parser_skip_to_closing_square_bracket
2650 /* Concept-related syntactic transformations */
2652 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2653 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand
;
2662 ++c_inhibit_evaluation_warnings
;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings
;
2668 --cp_unevaluated_operand
;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2677 cp_parser_parsing_tentatively (cp_parser
* parser
)
2679 return parser
->context
->next
!= NULL
;
2682 /* Returns nonzero if TOKEN is a string literal. */
2685 cp_parser_is_pure_string_literal (cp_token
* token
)
2687 return (token
->type
== CPP_STRING
||
2688 token
->type
== CPP_STRING16
||
2689 token
->type
== CPP_STRING32
||
2690 token
->type
== CPP_WSTRING
||
2691 token
->type
== CPP_UTF8STRING
);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2698 cp_parser_is_string_literal (cp_token
* token
)
2700 return (cp_parser_is_pure_string_literal (token
) ||
2701 token
->type
== CPP_STRING_USERDEF
||
2702 token
->type
== CPP_STRING16_USERDEF
||
2703 token
->type
== CPP_STRING32_USERDEF
||
2704 token
->type
== CPP_WSTRING_USERDEF
||
2705 token
->type
== CPP_UTF8STRING_USERDEF
);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2713 return token
->keyword
== keyword
;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token
*token
)
2722 if (token
->type
!= CPP_PRAGMA
)
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2740 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2741 location_t
*out_loc
)
2743 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2744 if (token2
->type
!= tok1_kind
)
2746 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2747 if (token3
->type
!= tok1_kind
)
2749 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2750 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2753 /* It must be at the start of the line. */
2754 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2755 if (LOCATION_COLUMN (start_loc
) != 1)
2758 /* We have a conflict marker. Construct a location of the form:
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc
= get_finish (token4
->location
);
2763 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2772 get_matching_symbol (required_token token_desc
)
2779 case RT_CLOSE_BRACE
:
2781 case RT_CLOSE_PAREN
:
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc
)
2795 return CPP_SEMICOLON
;
2797 return CPP_OPEN_PAREN
;
2798 case RT_CLOSE_BRACE
:
2799 return CPP_CLOSE_BRACE
;
2801 return CPP_OPEN_BRACE
;
2802 case RT_CLOSE_SQUARE
:
2803 return CPP_CLOSE_SQUARE
;
2804 case RT_OPEN_SQUARE
:
2805 return CPP_OPEN_SQUARE
;
2810 case RT_CLOSE_PAREN
:
2811 return CPP_CLOSE_PAREN
;
2814 /* Use CPP_EOF as a "no completions possible" code. */
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2839 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2840 required_token missing_token_desc
,
2841 location_t matching_location
)
2843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token
);
2848 if (token
->type
== CPP_PRAGMA
)
2850 error_at (token
->location
,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser
, token
);
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token
->type
== CPP_LSHIFT
2858 || token
->type
== CPP_RSHIFT
2859 || token
->type
== CPP_EQ_EQ
)
2862 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2864 error_at (loc
, "version control conflict marker in file");
2865 expanded_location token_exploc
= expand_location (token
->location
);
2866 /* Consume tokens until the end of the source line. */
2869 cp_lexer_consume_token (parser
->lexer
);
2870 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2873 expanded_location next_exploc
= expand_location (next
->location
);
2874 if (next_exploc
.file
!= token_exploc
.file
)
2876 if (next_exploc
.line
!= token_exploc
.line
)
2883 gcc_rich_location
richloc (input_location
);
2885 bool added_matching_location
= false;
2887 if (missing_token_desc
!= RT_NONE
)
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser
->lexer
)->location
;
2895 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location
!= UNKNOWN_LOCATION
)
2901 added_matching_location
2902 = richloc
.add_location_if_nearby (matching_location
);
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid
,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2910 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2911 token
->u
.value
, token
->flags
, &richloc
);
2913 if (missing_token_desc
!= RT_NONE
)
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location
!= UNKNOWN_LOCATION
2918 && !added_matching_location
)
2919 inform (matching_location
, "to match this %qs",
2920 get_matching_symbol (missing_token_desc
));
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2931 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2933 if (!cp_parser_simulate_error (parser
))
2934 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2943 cp_parser_name_lookup_error (cp_parser
* parser
,
2946 name_lookup_error desired
,
2947 location_t location
)
2949 /* If name lookup completely failed, tell the user that NAME was not
2951 if (decl
== error_mark_node
)
2953 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2954 error_at (location
, "%<%E::%E%> has not been declared",
2955 parser
->scope
, name
);
2956 else if (parser
->scope
== global_namespace
)
2957 error_at (location
, "%<::%E%> has not been declared", name
);
2958 else if (parser
->object_scope
2959 && !CLASS_TYPE_P (parser
->object_scope
))
2960 error_at (location
, "request for member %qE in non-class type %qT",
2961 name
, parser
->object_scope
);
2962 else if (parser
->object_scope
)
2963 error_at (location
, "%<%T::%E%> has not been declared",
2964 parser
->object_scope
, name
);
2966 error_at (location
, "%qE has not been declared", name
);
2968 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2973 error_at (location
, "%<%E::%E%> is not a type",
2974 parser
->scope
, name
);
2977 error_at (location
, "%<%E::%E%> is not a class or namespace",
2978 parser
->scope
, name
);
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser
->scope
, name
);
2990 else if (parser
->scope
== global_namespace
)
2995 error_at (location
, "%<::%E%> is not a type", name
);
2998 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3002 "%<::%E%> is not a class, namespace, or enumeration",
3014 error_at (location
, "%qE is not a type", name
);
3017 error_at (location
, "%qE is not a class or namespace", name
);
3021 "%qE is not a class, namespace, or enumeration", name
);
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3034 cp_parser_simulate_error (cp_parser
* parser
)
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3038 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3049 cp_parser_check_type_definition (cp_parser
* parser
)
3051 /* If types are forbidden here, issue a message. */
3052 if (parser
->type_definition_forbidden_message
)
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser
->type_definition_forbidden_message
);
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3069 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3070 tree type
, location_t type_location
)
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3076 && (declarator
->kind
== cdk_pointer
3077 || declarator
->kind
== cdk_reference
3078 || declarator
->kind
== cdk_ptrmem
))
3079 declarator
= declarator
->declarator
;
3081 && declarator
->kind
== cdk_function
)
3083 error_at (type_location
,
3084 "new types may not be defined in a return type");
3085 inform (type_location
,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3098 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3100 enum tag_types tag_type
,
3101 location_t location
)
3103 cp_token_position start
= 0;
3105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3107 if (TREE_CODE (type
) == TYPE_DECL
)
3108 type
= TREE_TYPE (type
);
3109 if (TYPE_P (type
) && !template_placeholder_p (type
))
3110 error_at (location
, "%qT is not a template", type
);
3111 else if (identifier_p (type
))
3113 if (tag_type
!= none_type
)
3114 error_at (location
, "%qE is not a class template", type
);
3116 error_at (location
, "%qE is not a template", type
);
3119 error_at (location
, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3122 start
= cp_lexer_token_position (parser
->lexer
, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser
->lexer
);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser
);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3130 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3140 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3141 non_integral_constant thing
)
3143 parser
->non_integral_constant_expression_p
= true;
3144 if (parser
->integral_constant_expression_p
)
3146 if (!parser
->allow_non_integral_constant_expression_p
)
3148 const char *msg
= NULL
;
3152 pedwarn (input_location
, OPT_Wpedantic
,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3185 case NIC_ADDR_LABEL
:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3189 case NIC_OVERLOADED
:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3193 case NIC_ASSIGNMENT
:
3194 error ("an assignment cannot appear in a constant-expression");
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3200 case NIC_CONSTRUCTOR
:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3204 case NIC_TRANSACTION
:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3212 msg
= "__FUNCTION__";
3214 case NIC_PRETTY_FUNC
:
3215 msg
= "__PRETTY_FUNCTION__";
3235 case NIC_PREINCREMENT
:
3238 case NIC_PREDECREMENT
:
3251 error ("%qs cannot appear in a constant-expression", msg
);
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3264 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3265 location_t location
)
3267 tree decl
, ambiguous_decls
;
3268 cp_parser_commit_to_tentative_parse (parser
);
3269 /* Try to lookup the identifier. */
3270 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls
, location
);
3275 if (ambiguous_decls
)
3276 /* If the lookup was ambiguous, an error will already have
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl
))
3284 "invalid use of template-name %qE without an argument list",
3286 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3287 inform (location
, "class template argument deduction is only available "
3288 "with -std=c++17 or -std=gnu++17");
3289 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3291 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3292 error_at (location
, "invalid use of destructor %qD as a type", id
);
3293 else if (TREE_CODE (decl
) == TYPE_DECL
)
3294 /* Something like 'unsigned A a;' */
3295 error_at (location
, "invalid combination of multiple type-specifiers");
3296 else if (!parser
->scope
)
3298 /* Issue an error message. */
3300 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3301 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3304 gcc_rich_location
richloc (location
);
3305 richloc
.add_fixit_replace (hint
.suggestion ());
3307 "%qE does not name a type; did you mean %qs?",
3308 id
, hint
.suggestion ());
3311 error_at (location
, "%qE does not name a type", id
);
3312 /* If we're in a template class, it's possible that the user was
3313 referring to a type from a base class. For example:
3315 template <typename T> struct A { typedef T X; };
3316 template <typename T> struct B : public A<T> { X x; };
3318 The user should have said "typename A<T>::X". */
3319 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3320 inform (location
, "C++11 %<constexpr%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3323 inform (location
, "C++11 %<noexcept%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (cxx_dialect
< cxx11
3326 && TREE_CODE (id
) == IDENTIFIER_NODE
3327 && id_equal (id
, "thread_local"))
3328 inform (location
, "C++11 %<thread_local%> only available with "
3329 "-std=c++11 or -std=gnu++11");
3330 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3331 inform (location
, "%<concept%> only available with -fconcepts");
3332 else if (processing_template_decl
&& current_class_type
3333 && TYPE_BINFO (current_class_type
))
3337 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3341 tree base_type
= BINFO_TYPE (b
);
3342 if (CLASS_TYPE_P (base_type
)
3343 && dependent_type_p (base_type
))
3346 /* Go from a particular instantiation of the
3347 template (which will have an empty TYPE_FIELDs),
3348 to the main version. */
3349 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3350 for (field
= TYPE_FIELDS (base_type
);
3352 field
= DECL_CHAIN (field
))
3353 if (TREE_CODE (field
) == TYPE_DECL
3354 && DECL_NAME (field
) == id
)
3357 "(perhaps %<typename %T::%E%> was intended)",
3358 BINFO_TYPE (b
), id
);
3367 /* Here we diagnose qualified-ids where the scope is actually correct,
3368 but the identifier does not resolve to a valid type name. */
3369 else if (parser
->scope
!= error_mark_node
)
3371 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3374 error_at (location_of (id
),
3375 "%qE in namespace %qE does not name a template type",
3377 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3378 error_at (location_of (id
),
3379 "%qE in namespace %qE does not name a template type",
3380 TREE_OPERAND (id
, 0), parser
->scope
);
3382 error_at (location_of (id
),
3383 "%qE in namespace %qE does not name a type",
3386 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3387 else if (decl
== error_mark_node
)
3388 suggest_alternative_in_explicit_scope (location
, id
,
3391 else if (CLASS_TYPE_P (parser
->scope
)
3392 && constructor_name_p (id
, parser
->scope
))
3395 error_at (location
, "%<%T::%E%> names the constructor, not"
3396 " the type", parser
->scope
, id
);
3397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3398 error_at (location
, "and %qT has no template constructors",
3401 else if (TYPE_P (parser
->scope
)
3402 && dependent_scope_p (parser
->scope
))
3404 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3406 "need %<typename%> before %<%T::%D::%E%> because "
3407 "%<%T::%D%> is a dependent scope",
3408 TYPE_CONTEXT (parser
->scope
),
3409 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3411 TYPE_CONTEXT (parser
->scope
),
3412 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3414 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3415 "%qT is a dependent scope",
3416 parser
->scope
, id
, parser
->scope
);
3418 else if (TYPE_P (parser
->scope
))
3420 if (!COMPLETE_TYPE_P (parser
->scope
))
3421 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3423 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3424 error_at (location_of (id
),
3425 "%qE in %q#T does not name a template type",
3427 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3428 error_at (location_of (id
),
3429 "%qE in %q#T does not name a template type",
3430 TREE_OPERAND (id
, 0), parser
->scope
);
3432 error_at (location_of (id
),
3433 "%qE in %q#T does not name a type",
3436 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3443 /* Check for a common situation where a type-name should be present,
3444 but is not, and issue a sensible error message. Returns true if an
3445 invalid type-name was detected.
3447 The situation handled by this function are variable declarations of the
3448 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3449 Usually, `ID' should name a type, but if we got here it means that it
3450 does not. We try to emit the best possible error message depending on
3451 how exactly the id-expression looks like. */
3454 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3457 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3459 /* Avoid duplicate error about ambiguous lookup. */
3460 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3462 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3463 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3467 cp_parser_parse_tentatively (parser
);
3468 id
= cp_parser_id_expression (parser
,
3469 /*template_keyword_p=*/false,
3470 /*check_dependency_p=*/true,
3471 /*template_p=*/NULL
,
3472 /*declarator_p=*/false,
3473 /*optional_p=*/false);
3474 /* If the next token is a (, this is a function with no explicit return
3475 type, i.e. constructor, destructor or conversion op. */
3476 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3477 || TREE_CODE (id
) == TYPE_DECL
)
3479 cp_parser_abort_tentative_parse (parser
);
3482 if (!cp_parser_parse_definitely (parser
))
3485 /* Emit a diagnostic for the invalid type. */
3486 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3488 /* If we aren't in the middle of a declarator (i.e. in a
3489 parameter-declaration-clause), skip to the end of the declaration;
3490 there's no point in trying to process it. */
3491 if (!parser
->in_declarator_p
)
3492 cp_parser_skip_to_end_of_block_or_statement (parser
);
3496 /* Consume tokens up to, and including, the next non-nested closing `)'.
3497 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3498 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3499 found an unnested token of that type. */
3502 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3507 unsigned paren_depth
= 0;
3508 unsigned brace_depth
= 0;
3509 unsigned square_depth
= 0;
3510 unsigned condop_depth
= 0;
3512 if (recovering
&& or_ttype
== CPP_EOF
3513 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3518 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3520 /* Have we found what we're looking for before the closing paren? */
3521 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3522 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3525 switch (token
->type
)
3528 case CPP_PRAGMA_EOL
:
3529 /* If we've run out of tokens, then there is no closing `)'. */
3532 /* This is good for lambda expression capture-lists. */
3533 case CPP_OPEN_SQUARE
:
3536 case CPP_CLOSE_SQUARE
:
3537 if (!square_depth
--)
3542 /* This matches the processing in skip_to_end_of_statement. */
3547 case CPP_OPEN_BRACE
:
3550 case CPP_CLOSE_BRACE
:
3555 case CPP_OPEN_PAREN
:
3560 case CPP_CLOSE_PAREN
:
3561 if (!brace_depth
&& !paren_depth
--)
3564 cp_lexer_consume_token (parser
->lexer
);
3570 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3575 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3583 /* Consume the token. */
3584 cp_lexer_consume_token (parser
->lexer
);
3588 /* Consume tokens up to, and including, the next non-nested closing `)'.
3589 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3590 are doing error recovery. Returns -1 if OR_COMMA is true and we
3591 found an unnested token of that type. */
3594 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3599 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3600 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3601 ttype
, consume_paren
);
3604 /* Consume tokens until we reach the end of the current statement.
3605 Normally, that will be just before consuming a `;'. However, if a
3606 non-nested `}' comes first, then we stop before consuming that. */
3609 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3611 unsigned nesting_depth
= 0;
3613 /* Unwind generic function template scope if necessary. */
3614 if (parser
->fully_implicit_function_template_p
)
3615 abort_fully_implicit_template (parser
);
3619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3621 switch (token
->type
)
3624 case CPP_PRAGMA_EOL
:
3625 /* If we've run out of tokens, stop. */
3629 /* If the next token is a `;', we have reached the end of the
3635 case CPP_CLOSE_BRACE
:
3636 /* If this is a non-nested '}', stop before consuming it.
3637 That way, when confronted with something like:
3641 we stop before consuming the closing '}', even though we
3642 have not yet reached a `;'. */
3643 if (nesting_depth
== 0)
3646 /* If it is the closing '}' for a block that we have
3647 scanned, stop -- but only after consuming the token.
3653 we will stop after the body of the erroneously declared
3654 function, but before consuming the following `typedef'
3656 if (--nesting_depth
== 0)
3658 cp_lexer_consume_token (parser
->lexer
);
3663 case CPP_OPEN_BRACE
:
3671 /* Consume the token. */
3672 cp_lexer_consume_token (parser
->lexer
);
3676 /* This function is called at the end of a statement or declaration.
3677 If the next token is a semicolon, it is consumed; otherwise, error
3678 recovery is attempted. */
3681 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3683 /* Look for the trailing `;'. */
3684 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3686 /* If there is additional (erroneous) input, skip to the end of
3688 cp_parser_skip_to_end_of_statement (parser
);
3689 /* If the next token is now a `;', consume it. */
3690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3691 cp_lexer_consume_token (parser
->lexer
);
3695 /* Skip tokens until we have consumed an entire block, or until we
3696 have consumed a non-nested `;'. */
3699 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3701 int nesting_depth
= 0;
3703 /* Unwind generic function template scope if necessary. */
3704 if (parser
->fully_implicit_function_template_p
)
3705 abort_fully_implicit_template (parser
);
3707 while (nesting_depth
>= 0)
3709 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3711 switch (token
->type
)
3714 case CPP_PRAGMA_EOL
:
3715 /* If we've run out of tokens, stop. */
3719 /* Stop if this is an unnested ';'. */
3724 case CPP_CLOSE_BRACE
:
3725 /* Stop if this is an unnested '}', or closes the outermost
3728 if (nesting_depth
< 0)
3734 case CPP_OPEN_BRACE
:
3743 /* Consume the token. */
3744 cp_lexer_consume_token (parser
->lexer
);
3748 /* Skip tokens until a non-nested closing curly brace is the next
3749 token, or there are no more tokens. Return true in the first case,
3753 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3755 unsigned nesting_depth
= 0;
3759 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3761 switch (token
->type
)
3764 case CPP_PRAGMA_EOL
:
3765 /* If we've run out of tokens, stop. */
3768 case CPP_CLOSE_BRACE
:
3769 /* If the next token is a non-nested `}', then we have reached
3770 the end of the current block. */
3771 if (nesting_depth
-- == 0)
3775 case CPP_OPEN_BRACE
:
3776 /* If it the next token is a `{', then we are entering a new
3777 block. Consume the entire block. */
3785 /* Consume the token. */
3786 cp_lexer_consume_token (parser
->lexer
);
3790 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3791 parameter is the PRAGMA token, allowing us to purge the entire pragma
3795 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3799 parser
->lexer
->in_pragma
= false;
3802 token
= cp_lexer_consume_token (parser
->lexer
);
3803 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3805 /* Ensure that the pragma is not parsed again. */
3806 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3809 /* Require pragma end of line, resyncing with it as necessary. The
3810 arguments are as for cp_parser_skip_to_pragma_eol. */
3813 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3815 parser
->lexer
->in_pragma
= false;
3816 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3817 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3820 /* This is a simple wrapper around make_typename_type. When the id is
3821 an unresolved identifier node, we can provide a superior diagnostic
3822 using cp_parser_diagnose_invalid_type_name. */
3825 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3826 location_t id_location
)
3829 if (identifier_p (id
))
3831 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3832 /*complain=*/tf_none
);
3833 if (result
== error_mark_node
)
3834 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3837 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3840 /* This is a wrapper around the
3841 make_{pointer,ptrmem,reference}_declarator functions that decides
3842 which one to call based on the CODE and CLASS_TYPE arguments. The
3843 CODE argument should be one of the values returned by
3844 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3845 appertain to the pointer or reference. */
3847 static cp_declarator
*
3848 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3849 cp_cv_quals cv_qualifiers
,
3850 cp_declarator
*target
,
3853 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3854 return cp_error_declarator
;
3856 if (code
== INDIRECT_REF
)
3857 if (class_type
== NULL_TREE
)
3858 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3860 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3861 target
, attributes
);
3862 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3863 return make_reference_declarator (cv_qualifiers
, target
,
3865 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3866 return make_reference_declarator (cv_qualifiers
, target
,
3871 /* Create a new C++ parser. */
3874 cp_parser_new (void)
3880 /* cp_lexer_new_main is called before doing GC allocation because
3881 cp_lexer_new_main might load a PCH file. */
3882 lexer
= cp_lexer_new_main ();
3884 /* Initialize the binops_by_token so that we can get the tree
3885 directly from the token. */
3886 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3887 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3889 parser
= ggc_cleared_alloc
<cp_parser
> ();
3890 parser
->lexer
= lexer
;
3891 parser
->context
= cp_parser_context_new (NULL
);
3893 /* For now, we always accept GNU extensions. */
3894 parser
->allow_gnu_extensions_p
= 1;
3896 /* The `>' token is a greater-than operator, not the end of a
3898 parser
->greater_than_is_operator_p
= true;
3900 parser
->default_arg_ok_p
= true;
3902 /* We are not parsing a constant-expression. */
3903 parser
->integral_constant_expression_p
= false;
3904 parser
->allow_non_integral_constant_expression_p
= false;
3905 parser
->non_integral_constant_expression_p
= false;
3907 /* Local variable names are not forbidden. */
3908 parser
->local_variables_forbidden_p
= false;
3910 /* We are not processing an `extern "C"' declaration. */
3911 parser
->in_unbraced_linkage_specification_p
= false;
3913 /* We are not processing a declarator. */
3914 parser
->in_declarator_p
= false;
3916 /* We are not processing a template-argument-list. */
3917 parser
->in_template_argument_list_p
= false;
3919 /* We are not in an iteration statement. */
3920 parser
->in_statement
= 0;
3922 /* We are not in a switch statement. */
3923 parser
->in_switch_statement_p
= false;
3925 /* We are not parsing a type-id inside an expression. */
3926 parser
->in_type_id_in_expr_p
= false;
3928 /* Declarations aren't implicitly extern "C". */
3929 parser
->implicit_extern_c
= false;
3931 /* String literals should be translated to the execution character set. */
3932 parser
->translate_strings_p
= true;
3934 /* We are not parsing a function body. */
3935 parser
->in_function_body
= false;
3937 /* We can correct until told otherwise. */
3938 parser
->colon_corrects_to_scope_p
= true;
3940 /* The unparsed function queue is empty. */
3941 push_unparsed_function_queues (parser
);
3943 /* There are no classes being defined. */
3944 parser
->num_classes_being_defined
= 0;
3946 /* No template parameters apply. */
3947 parser
->num_template_parameter_lists
= 0;
3949 /* Special parsing data structures. */
3950 parser
->omp_declare_simd
= NULL
;
3951 parser
->oacc_routine
= NULL
;
3953 /* Not declaring an implicit function template. */
3954 parser
->auto_is_implicit_function_template_parm_p
= false;
3955 parser
->fully_implicit_function_template_p
= false;
3956 parser
->implicit_template_parms
= 0;
3957 parser
->implicit_template_scope
= 0;
3959 /* Allow constrained-type-specifiers. */
3960 parser
->prevent_constrained_type_specifiers
= 0;
3962 /* We haven't yet seen an 'extern "C"'. */
3963 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3968 /* Create a cp_lexer structure which will emit the tokens in CACHE
3969 and push it onto the parser's lexer stack. This is used for delayed
3970 parsing of in-class method bodies and default arguments, and should
3971 not be confused with tentative parsing. */
3973 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3975 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3976 lexer
->next
= parser
->lexer
;
3977 parser
->lexer
= lexer
;
3979 /* Move the current source position to that of the first token in the
3981 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3984 /* Pop the top lexer off the parser stack. This is never used for the
3985 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3987 cp_parser_pop_lexer (cp_parser
*parser
)
3989 cp_lexer
*lexer
= parser
->lexer
;
3990 parser
->lexer
= lexer
->next
;
3991 cp_lexer_destroy (lexer
);
3993 /* Put the current source position back where it was before this
3994 lexer was pushed. */
3995 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3998 /* Lexical conventions [gram.lex] */
4000 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4004 cp_parser_identifier (cp_parser
* parser
)
4008 /* Look for the identifier. */
4009 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4010 /* Return the value. */
4012 return cp_expr (token
->u
.value
, token
->location
);
4014 return error_mark_node
;
4017 /* Parse a sequence of adjacent string constants. Returns a
4018 TREE_STRING representing the combined, nul-terminated string
4019 constant. If TRANSLATE is true, translate the string to the
4020 execution character set. If WIDE_OK is true, a wide string is
4023 C++98 [lex.string] says that if a narrow string literal token is
4024 adjacent to a wide string literal token, the behavior is undefined.
4025 However, C99 6.4.5p4 says that this results in a wide string literal.
4026 We follow C99 here, for consistency with the C front end.
4028 This code is largely lifted from lex_string() in c-lex.c.
4030 FUTURE: ObjC++ will need to handle @-strings here. */
4032 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4033 bool lookup_udlit
= true)
4037 struct obstack str_ob
;
4038 cpp_string str
, istr
, *strs
;
4040 enum cpp_ttype type
, curr_type
;
4041 int have_suffix_p
= 0;
4043 tree suffix_id
= NULL_TREE
;
4044 bool curr_tok_is_userdef_p
= false;
4046 tok
= cp_lexer_peek_token (parser
->lexer
);
4047 if (!cp_parser_is_string_literal (tok
))
4049 cp_parser_error (parser
, "expected string-literal");
4050 return error_mark_node
;
4053 location_t loc
= tok
->location
;
4055 if (cpp_userdef_string_p (tok
->type
))
4057 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4058 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4059 curr_tok_is_userdef_p
= true;
4063 string_tree
= tok
->u
.value
;
4064 curr_type
= tok
->type
;
4068 /* Try to avoid the overhead of creating and destroying an obstack
4069 for the common case of just one string. */
4070 if (!cp_parser_is_string_literal
4071 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
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
);
4079 if (curr_tok_is_userdef_p
)
4081 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4083 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4086 curr_type
= tok
->type
;
4092 location_t last_tok_loc
= tok
->location
;
4093 gcc_obstack_init (&str_ob
);
4098 cp_lexer_consume_token (parser
->lexer
);
4100 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4101 str
.len
= TREE_STRING_LENGTH (string_tree
);
4103 if (curr_tok_is_userdef_p
)
4105 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4106 if (have_suffix_p
== 0)
4108 suffix_id
= curr_suffix_id
;
4111 else if (have_suffix_p
== 1
4112 && curr_suffix_id
!= suffix_id
)
4114 error ("inconsistent user-defined literal suffixes"
4115 " %qD and %qD in string literal",
4116 suffix_id
, curr_suffix_id
);
4119 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4122 curr_type
= tok
->type
;
4124 if (type
!= curr_type
)
4126 if (type
== CPP_STRING
)
4128 else if (curr_type
!= CPP_STRING
)
4130 rich_location
rich_loc (line_table
, tok
->location
);
4131 rich_loc
.add_range (last_tok_loc
, false);
4132 error_at (&rich_loc
,
4133 "unsupported non-standard concatenation "
4134 "of string literals");
4138 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4140 last_tok_loc
= tok
->location
;
4142 tok
= cp_lexer_peek_token (parser
->lexer
);
4143 if (cpp_userdef_string_p (tok
->type
))
4145 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4146 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4147 curr_tok_is_userdef_p
= true;
4151 string_tree
= tok
->u
.value
;
4152 curr_type
= tok
->type
;
4153 curr_tok_is_userdef_p
= false;
4156 while (cp_parser_is_string_literal (tok
));
4158 /* A string literal built by concatenation has its caret=start at
4159 the start of the initial string, and its finish at the finish of
4160 the final string literal. */
4161 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4163 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4166 if (type
!= CPP_STRING
&& !wide_ok
)
4168 cp_parser_error (parser
, "a wide string is invalid in this context");
4172 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4173 (parse_in
, strs
, count
, &istr
, type
))
4175 value
= build_string (istr
.len
, (const char *)istr
.text
);
4176 free (CONST_CAST (unsigned char *, istr
.text
));
4182 case CPP_UTF8STRING
:
4183 TREE_TYPE (value
) = char_array_type_node
;
4186 TREE_TYPE (value
) = char16_array_type_node
;
4189 TREE_TYPE (value
) = char32_array_type_node
;
4192 TREE_TYPE (value
) = wchar_array_type_node
;
4196 value
= fix_string_type (value
);
4200 tree literal
= build_userdef_literal (suffix_id
, value
,
4201 OT_NONE
, NULL_TREE
);
4203 value
= cp_parser_userdef_string_literal (literal
);
4209 /* cpp_interpret_string has issued an error. */
4210 value
= error_mark_node
;
4213 obstack_free (&str_ob
, 0);
4215 return cp_expr (value
, loc
);
4218 /* Look up a literal operator with the name and the exact arguments. */
4221 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4224 decl
= lookup_name (name
);
4225 if (!decl
|| !is_overloaded_fn (decl
))
4226 return error_mark_node
;
4228 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4233 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4234 if (parmtypes
!= NULL_TREE
)
4236 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4237 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4239 tree tparm
= TREE_VALUE (parmtypes
);
4240 tree targ
= TREE_TYPE ((*args
)[ix
]);
4241 bool ptr
= TYPE_PTR_P (tparm
);
4242 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4243 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4245 || !same_type_p (TREE_TYPE (tparm
),
4250 && ix
== vec_safe_length (args
)
4251 /* May be this should be sufficient_parms_p instead,
4252 depending on how exactly should user-defined literals
4253 work in presence of default arguments on the literal
4254 operator parameters. */
4255 && parmtypes
== void_list_node
)
4260 return error_mark_node
;
4263 /* Parse a user-defined char constant. Returns a call to a user-defined
4264 literal operator taking the character as an argument. */
4267 cp_parser_userdef_char_literal (cp_parser
*parser
)
4269 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4270 tree literal
= token
->u
.value
;
4271 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4272 tree value
= USERDEF_LITERAL_VALUE (literal
);
4273 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4276 /* Build up a call to the user-defined operator */
4277 /* Lookup the name we got back from the id-expression. */
4278 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4279 vec_safe_push (args
, value
);
4280 decl
= lookup_literal_operator (name
, args
);
4281 if (!decl
|| decl
== error_mark_node
)
4283 error ("unable to find character literal operator %qD with %qT argument",
4284 name
, TREE_TYPE (value
));
4285 release_tree_vector (args
);
4286 return error_mark_node
;
4288 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4289 release_tree_vector (args
);
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294 create a char... template parameter pack from a string node. */
4297 make_char_string_pack (tree value
)
4300 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4301 const char *str
= TREE_STRING_POINTER (value
);
4302 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4303 tree argvec
= make_tree_vec (1);
4305 /* Fill in CHARVEC with all of the parameters. */
4306 charvec
= make_tree_vec (len
);
4307 for (i
= 0; i
< len
; ++i
)
4309 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4310 cpp_string in
= { 3, s
};
4311 cpp_string out
= { 0, 0 };
4312 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4314 gcc_assert (out
.len
== 2);
4315 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4319 /* Build the argument packs. */
4320 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4322 TREE_VEC_ELT (argvec
, 0) = argpack
;
4327 /* A subroutine of cp_parser_userdef_numeric_literal to
4328 create a char... template parameter pack from a string node. */
4331 make_string_pack (tree value
)
4334 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4335 const unsigned char *str
4336 = (const unsigned char *) TREE_STRING_POINTER (value
);
4337 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4338 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4339 tree argvec
= make_tree_vec (2);
4341 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4342 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4344 /* First template parm is character type. */
4345 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4347 /* Fill in CHARVEC with all of the parameters. */
4348 charvec
= make_tree_vec (len
);
4349 for (int i
= 0; i
< len
; ++i
)
4350 TREE_VEC_ELT (charvec
, i
)
4351 = double_int_to_tree (str_char_type_node
,
4352 double_int::from_buffer (str
+ i
* sz
, sz
));
4354 /* Build the argument packs. */
4355 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4357 TREE_VEC_ELT (argvec
, 1) = argpack
;
4362 /* Parse a user-defined numeric constant. returns a call to a user-defined
4363 literal operator. */
4366 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4368 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4369 tree literal
= token
->u
.value
;
4370 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4371 tree value
= USERDEF_LITERAL_VALUE (literal
);
4372 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4373 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4374 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4376 vec
<tree
, va_gc
> *args
;
4378 /* Look for a literal operator taking the exact type of numeric argument
4379 as the literal value. */
4380 args
= make_tree_vector ();
4381 vec_safe_push (args
, value
);
4382 decl
= lookup_literal_operator (name
, args
);
4383 if (decl
&& decl
!= error_mark_node
)
4385 result
= finish_call_expr (decl
, &args
, false, true,
4386 tf_warning_or_error
);
4388 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4390 warning_at (token
->location
, OPT_Woverflow
,
4391 "integer literal exceeds range of %qT type",
4392 long_long_unsigned_type_node
);
4397 warning_at (token
->location
, OPT_Woverflow
,
4398 "floating literal exceeds range of %qT type",
4399 long_double_type_node
);
4400 else if (overflow
< 0)
4401 warning_at (token
->location
, OPT_Woverflow
,
4402 "floating literal truncated to zero");
4405 release_tree_vector (args
);
4408 release_tree_vector (args
);
4410 /* If the numeric argument didn't work, look for a raw literal
4411 operator taking a const char* argument consisting of the number
4412 in string format. */
4413 args
= make_tree_vector ();
4414 vec_safe_push (args
, num_string
);
4415 decl
= lookup_literal_operator (name
, args
);
4416 if (decl
&& decl
!= error_mark_node
)
4418 result
= finish_call_expr (decl
, &args
, false, true,
4419 tf_warning_or_error
);
4420 release_tree_vector (args
);
4423 release_tree_vector (args
);
4425 /* If the raw literal didn't work, look for a non-type template
4426 function with parameter pack char.... Call the function with
4427 template parameter characters representing the number. */
4428 args
= make_tree_vector ();
4429 decl
= lookup_literal_operator (name
, args
);
4430 if (decl
&& decl
!= error_mark_node
)
4432 tree tmpl_args
= make_char_string_pack (num_string
);
4433 if (tmpl_args
== NULL_TREE
)
4435 error ("failed to translate literal to execution character set %qT",
4437 return error_mark_node
;
4439 decl
= lookup_template_function (decl
, tmpl_args
);
4440 result
= finish_call_expr (decl
, &args
, false, true,
4441 tf_warning_or_error
);
4442 release_tree_vector (args
);
4446 release_tree_vector (args
);
4448 /* In C++14 the standard library defines complex number suffixes that
4449 conflict with GNU extensions. Prefer them if <complex> is #included. */
4450 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4451 bool i14
= (cxx_dialect
> cxx11
4452 && (id_equal (suffix_id
, "i")
4453 || id_equal (suffix_id
, "if")
4454 || id_equal (suffix_id
, "il")));
4455 diagnostic_t kind
= DK_ERROR
;
4460 tree cxlit
= lookup_qualified_name (std_node
,
4461 get_identifier ("complex_literals"),
4463 if (cxlit
== error_mark_node
)
4465 /* No <complex>, so pedwarn and use GNU semantics. */
4467 opt
= OPT_Wpedantic
;
4472 = emit_diagnostic (kind
, input_location
, opt
,
4473 "unable to find numeric literal operator %qD", name
);
4476 /* Don't inform either. */;
4479 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4480 "(from <complex>) to enable the C++14 user-defined literal "
4483 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4484 "GNU built-in suffix");
4487 inform (token
->location
, "use -fext-numeric-literals "
4488 "to enable more built-in suffixes");
4490 if (kind
== DK_ERROR
)
4491 value
= error_mark_node
;
4494 /* Use the built-in semantics. */
4496 if (id_equal (suffix_id
, "i"))
4498 if (TREE_CODE (value
) == INTEGER_CST
)
4499 type
= integer_type_node
;
4501 type
= double_type_node
;
4503 else if (id_equal (suffix_id
, "if"))
4504 type
= float_type_node
;
4505 else /* if (id_equal (suffix_id, "il")) */
4506 type
= long_double_type_node
;
4508 value
= build_complex (build_complex_type (type
),
4509 fold_convert (type
, integer_zero_node
),
4510 fold_convert (type
, value
));
4513 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4514 /* Avoid repeated diagnostics. */
4515 token
->u
.value
= value
;
4519 /* Parse a user-defined string constant. Returns a call to a user-defined
4520 literal operator taking a character pointer and the length of the string
4524 cp_parser_userdef_string_literal (tree literal
)
4526 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4527 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4528 tree value
= USERDEF_LITERAL_VALUE (literal
);
4529 int len
= TREE_STRING_LENGTH (value
)
4530 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4532 vec
<tree
, va_gc
> *args
;
4534 /* Build up a call to the user-defined operator. */
4535 /* Lookup the name we got back from the id-expression. */
4536 args
= make_tree_vector ();
4537 vec_safe_push (args
, value
);
4538 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4539 decl
= lookup_literal_operator (name
, args
);
4541 if (decl
&& decl
!= error_mark_node
)
4543 result
= finish_call_expr (decl
, &args
, false, true,
4544 tf_warning_or_error
);
4545 release_tree_vector (args
);
4548 release_tree_vector (args
);
4550 /* Look for a template function with typename parameter CharT
4551 and parameter pack CharT... Call the function with
4552 template parameter characters representing the string. */
4553 args
= make_tree_vector ();
4554 decl
= lookup_literal_operator (name
, args
);
4555 if (decl
&& decl
!= error_mark_node
)
4557 tree tmpl_args
= make_string_pack (value
);
4558 decl
= lookup_template_function (decl
, tmpl_args
);
4559 result
= finish_call_expr (decl
, &args
, false, true,
4560 tf_warning_or_error
);
4561 release_tree_vector (args
);
4564 release_tree_vector (args
);
4566 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4567 name
, TREE_TYPE (value
), size_type_node
);
4568 return error_mark_node
;
4572 /* Basic concepts [gram.basic] */
4574 /* Parse a translation-unit.
4577 declaration-seq [opt]
4579 Returns TRUE if all went well. */
4582 cp_parser_translation_unit (cp_parser
* parser
)
4584 /* The address of the first non-permanent object on the declarator
4586 static void *declarator_obstack_base
;
4590 /* Create the declarator obstack, if necessary. */
4591 if (!cp_error_declarator
)
4593 gcc_obstack_init (&declarator_obstack
);
4594 /* Create the error declarator. */
4595 cp_error_declarator
= make_declarator (cdk_error
);
4596 /* Create the empty parameter list. */
4597 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4599 /* Remember where the base of the declarator obstack lies. */
4600 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4603 cp_parser_declaration_seq_opt (parser
);
4605 /* If there are no tokens left then all went well. */
4606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4608 /* Get rid of the token array; we don't need it any more. */
4609 cp_lexer_destroy (parser
->lexer
);
4610 parser
->lexer
= NULL
;
4612 /* This file might have been a context that's implicitly extern
4613 "C". If so, pop the lang context. (Only relevant for PCH.) */
4614 if (parser
->implicit_extern_c
)
4616 pop_lang_context ();
4617 parser
->implicit_extern_c
= false;
4621 finish_translation_unit ();
4627 cp_parser_error (parser
, "expected declaration");
4631 /* Make sure the declarator obstack was fully cleaned up. */
4632 gcc_assert (obstack_next_free (&declarator_obstack
)
4633 == declarator_obstack_base
);
4635 /* All went well. */
4639 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4640 decltype context. */
4642 static inline tsubst_flags_t
4643 complain_flags (bool decltype_p
)
4645 tsubst_flags_t complain
= tf_warning_or_error
;
4647 complain
|= tf_decltype
;
4651 /* We're about to parse a collection of statements. If we're currently
4652 parsing tentatively, set up a firewall so that any nested
4653 cp_parser_commit_to_tentative_parse won't affect the current context. */
4655 static cp_token_position
4656 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4661 cp_parser_parse_tentatively (parser
);
4662 cp_parser_commit_to_topmost_tentative_parse (parser
);
4663 return cp_lexer_token_position (parser
->lexer
, false);
4666 /* We've finished parsing the collection of statements. Wrap up the
4667 firewall and replace the relevant tokens with the parsed form. */
4670 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4676 /* Finish the firewall level. */
4677 cp_parser_parse_definitely (parser
);
4678 /* And remember the result of the parse for when we try again. */
4679 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4680 token
->type
= CPP_PREPARSED_EXPR
;
4681 token
->u
.value
= expr
;
4682 token
->keyword
= RID_MAX
;
4683 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4686 /* Like the above functions, but let the user modify the tokens. Used by
4687 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4688 later parses, so it makes sense to localize the effects of
4689 cp_parser_commit_to_tentative_parse. */
4691 struct tentative_firewall
4696 tentative_firewall (cp_parser
*p
): parser(p
)
4698 /* If we're currently parsing tentatively, start a committed level as a
4699 firewall and then an inner tentative parse. */
4700 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4702 cp_parser_parse_tentatively (parser
);
4703 cp_parser_commit_to_topmost_tentative_parse (parser
);
4704 cp_parser_parse_tentatively (parser
);
4708 ~tentative_firewall()
4712 /* Finish the inner tentative parse and the firewall, propagating any
4713 uncommitted error state to the outer tentative parse. */
4714 bool err
= cp_parser_error_occurred (parser
);
4715 cp_parser_parse_definitely (parser
);
4716 cp_parser_parse_definitely (parser
);
4718 cp_parser_simulate_error (parser
);
4723 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4724 This class is for tracking such a matching pair of symbols.
4725 In particular, it tracks the location of the first token,
4726 so that if the second token is missing, we can highlight the
4727 location of the first token when notifying the user about the
4730 template <typename traits_t
>
4734 /* token_pair's ctor. */
4735 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4737 /* If the next token is the opening symbol for this pair, consume it and
4739 Otherwise, issue an error and return false.
4740 In either case, record the location of the opening token. */
4742 bool require_open (cp_parser
*parser
)
4744 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4745 return cp_parser_require (parser
, traits_t::open_token_type
,
4746 traits_t::required_token_open
);
4749 /* Consume the next token from PARSER, recording its location as
4750 that of the opening token within the pair. */
4752 cp_token
* consume_open (cp_parser
*parser
)
4754 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4755 gcc_assert (tok
->type
== traits_t::open_token_type
);
4756 m_open_loc
= tok
->location
;
4760 /* If the next token is the closing symbol for this pair, consume it
4762 Otherwise, issue an error, highlighting the location of the
4763 corresponding opening token, and return NULL. */
4765 cp_token
*require_close (cp_parser
*parser
) const
4767 return cp_parser_require (parser
, traits_t::close_token_type
,
4768 traits_t::required_token_close
,
4773 location_t m_open_loc
;
4776 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4778 struct matching_paren_traits
4780 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4781 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4782 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4783 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4786 /* "matching_parens" is a token_pair<T> class for tracking matching
4787 pairs of parentheses. */
4789 typedef token_pair
<matching_paren_traits
> matching_parens
;
4791 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4793 struct matching_brace_traits
4795 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4796 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4797 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4798 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4801 /* "matching_braces" is a token_pair<T> class for tracking matching
4804 typedef token_pair
<matching_brace_traits
> matching_braces
;
4807 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4808 enclosing parentheses. */
4811 cp_parser_statement_expr (cp_parser
*parser
)
4813 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4815 /* Consume the '('. */
4816 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4817 matching_parens parens
;
4818 parens
.consume_open (parser
);
4819 /* Start the statement-expression. */
4820 tree expr
= begin_stmt_expr ();
4821 /* Parse the compound-statement. */
4822 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4824 expr
= finish_stmt_expr (expr
, false);
4825 /* Consume the ')'. */
4826 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4827 if (!parens
.require_close (parser
))
4828 cp_parser_skip_to_end_of_statement (parser
);
4830 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4831 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4832 return cp_expr (expr
, combined_loc
);
4835 /* Expressions [gram.expr] */
4837 /* Parse a fold-operator.
4840 - * / % ^ & | = < > << >>
4841 = -= *= /= %= ^= &= |= <<= >>=
4842 == != <= >= && || , .* ->*
4844 This returns the tree code corresponding to the matched operator
4845 as an int. When the current token matches a compound assignment
4846 opertor, the resulting tree code is the negative value of the
4847 non-assignment operator. */
4850 cp_parser_fold_operator (cp_token
*token
)
4852 switch (token
->type
)
4854 case CPP_PLUS
: return PLUS_EXPR
;
4855 case CPP_MINUS
: return MINUS_EXPR
;
4856 case CPP_MULT
: return MULT_EXPR
;
4857 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4858 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4859 case CPP_XOR
: return BIT_XOR_EXPR
;
4860 case CPP_AND
: return BIT_AND_EXPR
;
4861 case CPP_OR
: return BIT_IOR_EXPR
;
4862 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4863 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4865 case CPP_EQ
: return -NOP_EXPR
;
4866 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4867 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4868 case CPP_MULT_EQ
: return -MULT_EXPR
;
4869 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4870 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4871 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4872 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4873 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4874 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4875 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4877 case CPP_EQ_EQ
: return EQ_EXPR
;
4878 case CPP_NOT_EQ
: return NE_EXPR
;
4879 case CPP_LESS
: return LT_EXPR
;
4880 case CPP_GREATER
: return GT_EXPR
;
4881 case CPP_LESS_EQ
: return LE_EXPR
;
4882 case CPP_GREATER_EQ
: return GE_EXPR
;
4884 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4885 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4887 case CPP_COMMA
: return COMPOUND_EXPR
;
4889 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4890 case CPP_DEREF_STAR
: return MEMBER_REF
;
4892 default: return ERROR_MARK
;
4896 /* Returns true if CODE indicates a binary expression, which is not allowed in
4897 the LHS of a fold-expression. More codes will need to be added to use this
4898 function in other contexts. */
4901 is_binary_op (tree_code code
)
4906 case POINTER_PLUS_EXPR
:
4909 case TRUNC_DIV_EXPR
:
4910 case TRUNC_MOD_EXPR
:
4926 case TRUTH_ANDIF_EXPR
:
4927 case TRUTH_ORIF_EXPR
:
4940 /* If the next token is a suitable fold operator, consume it and return as
4941 the function above. */
4944 cp_parser_fold_operator (cp_parser
*parser
)
4946 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4947 int code
= cp_parser_fold_operator (token
);
4948 if (code
!= ERROR_MARK
)
4949 cp_lexer_consume_token (parser
->lexer
);
4953 /* Parse a fold-expression.
4956 ( ... folding-operator cast-expression)
4957 ( cast-expression folding-operator ... )
4958 ( cast-expression folding operator ... folding-operator cast-expression)
4960 Note that the '(' and ')' are matched in primary expression. */
4963 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4970 cp_lexer_consume_token (parser
->lexer
);
4971 int op
= cp_parser_fold_operator (parser
);
4972 if (op
== ERROR_MARK
)
4974 cp_parser_error (parser
, "expected binary operator");
4975 return error_mark_node
;
4978 tree expr
= cp_parser_cast_expression (parser
, false, false,
4980 if (expr
== error_mark_node
)
4981 return error_mark_node
;
4982 return finish_left_unary_fold_expr (expr
, op
);
4985 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4986 int op
= cp_parser_fold_operator (parser
);
4987 if (op
== ERROR_MARK
)
4989 cp_parser_error (parser
, "expected binary operator");
4990 return error_mark_node
;
4993 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4995 cp_parser_error (parser
, "expected ...");
4996 return error_mark_node
;
4998 cp_lexer_consume_token (parser
->lexer
);
5000 /* The operands of a fold-expression are cast-expressions, so binary or
5001 conditional expressions are not allowed. We check this here to avoid
5002 tentative parsing. */
5003 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5004 /* OK, the expression was parenthesized. */;
5005 else if (is_binary_op (TREE_CODE (expr1
)))
5006 error_at (location_of (expr1
),
5007 "binary expression in operand of fold-expression");
5008 else if (TREE_CODE (expr1
) == COND_EXPR
5009 || (REFERENCE_REF_P (expr1
)
5010 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5011 error_at (location_of (expr1
),
5012 "conditional expression in operand of fold-expression");
5015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5016 return finish_right_unary_fold_expr (expr1
, op
);
5018 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5020 cp_parser_error (parser
, "mismatched operator in fold-expression");
5021 return error_mark_node
;
5023 cp_lexer_consume_token (parser
->lexer
);
5025 // Binary left or right fold.
5026 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5027 if (expr2
== error_mark_node
)
5028 return error_mark_node
;
5029 return finish_binary_fold_expr (expr1
, expr2
, op
);
5032 /* Parse a primary-expression.
5039 lambda-expression (C++11)
5044 ( compound-statement )
5045 __builtin_va_arg ( assignment-expression , type-id )
5046 __builtin_offsetof ( type-id , offsetof-expression )
5049 __has_nothrow_assign ( type-id )
5050 __has_nothrow_constructor ( type-id )
5051 __has_nothrow_copy ( type-id )
5052 __has_trivial_assign ( type-id )
5053 __has_trivial_constructor ( type-id )
5054 __has_trivial_copy ( type-id )
5055 __has_trivial_destructor ( type-id )
5056 __has_virtual_destructor ( type-id )
5057 __is_abstract ( type-id )
5058 __is_base_of ( type-id , type-id )
5059 __is_class ( type-id )
5060 __is_empty ( type-id )
5061 __is_enum ( type-id )
5062 __is_final ( type-id )
5063 __is_literal_type ( type-id )
5064 __is_pod ( type-id )
5065 __is_polymorphic ( type-id )
5066 __is_std_layout ( type-id )
5067 __is_trivial ( type-id )
5068 __is_union ( type-id )
5070 Objective-C++ Extension:
5078 ADDRESS_P is true iff this expression was immediately preceded by
5079 "&" and therefore might denote a pointer-to-member. CAST_P is true
5080 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5081 true iff this expression is a template argument.
5083 Returns a representation of the expression. Upon return, *IDK
5084 indicates what kind of id-expression (if any) was present. */
5087 cp_parser_primary_expression (cp_parser
*parser
,
5090 bool template_arg_p
,
5094 cp_token
*token
= NULL
;
5096 /* Assume the primary expression is not an id-expression. */
5097 *idk
= CP_ID_KIND_NONE
;
5099 /* Peek at the next token. */
5100 token
= cp_lexer_peek_token (parser
->lexer
);
5101 switch ((int) token
->type
)
5110 user-defined-literal */
5117 case CPP_PREPARSED_EXPR
:
5118 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5119 return cp_parser_userdef_numeric_literal (parser
);
5120 token
= cp_lexer_consume_token (parser
->lexer
);
5121 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5123 error_at (token
->location
,
5124 "fixed-point types not supported in C++");
5125 return error_mark_node
;
5127 /* Floating-point literals are only allowed in an integral
5128 constant expression if they are cast to an integral or
5129 enumeration type. */
5130 if (TREE_CODE (token
->u
.value
) == REAL_CST
5131 && parser
->integral_constant_expression_p
5134 /* CAST_P will be set even in invalid code like "int(2.7 +
5135 ...)". Therefore, we have to check that the next token
5136 is sure to end the cast. */
5139 cp_token
*next_token
;
5141 next_token
= cp_lexer_peek_token (parser
->lexer
);
5142 if (/* The comma at the end of an
5143 enumerator-definition. */
5144 next_token
->type
!= CPP_COMMA
5145 /* The curly brace at the end of an enum-specifier. */
5146 && next_token
->type
!= CPP_CLOSE_BRACE
5147 /* The end of a statement. */
5148 && next_token
->type
!= CPP_SEMICOLON
5149 /* The end of the cast-expression. */
5150 && next_token
->type
!= CPP_CLOSE_PAREN
5151 /* The end of an array bound. */
5152 && next_token
->type
!= CPP_CLOSE_SQUARE
5153 /* The closing ">" in a template-argument-list. */
5154 && (next_token
->type
!= CPP_GREATER
5155 || parser
->greater_than_is_operator_p
)
5156 /* C++0x only: A ">>" treated like two ">" tokens,
5157 in a template-argument-list. */
5158 && (next_token
->type
!= CPP_RSHIFT
5159 || (cxx_dialect
== cxx98
)
5160 || parser
->greater_than_is_operator_p
))
5164 /* If we are within a cast, then the constraint that the
5165 cast is to an integral or enumeration type will be
5166 checked at that point. If we are not within a cast, then
5167 this code is invalid. */
5169 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5171 return cp_expr (token
->u
.value
, token
->location
);
5173 case CPP_CHAR_USERDEF
:
5174 case CPP_CHAR16_USERDEF
:
5175 case CPP_CHAR32_USERDEF
:
5176 case CPP_WCHAR_USERDEF
:
5177 case CPP_UTF8CHAR_USERDEF
:
5178 return cp_parser_userdef_char_literal (parser
);
5184 case CPP_UTF8STRING
:
5185 case CPP_STRING_USERDEF
:
5186 case CPP_STRING16_USERDEF
:
5187 case CPP_STRING32_USERDEF
:
5188 case CPP_WSTRING_USERDEF
:
5189 case CPP_UTF8STRING_USERDEF
:
5190 /* ??? Should wide strings be allowed when parser->translate_strings_p
5191 is false (i.e. in attributes)? If not, we can kill the third
5192 argument to cp_parser_string_literal. */
5193 return cp_parser_string_literal (parser
,
5194 parser
->translate_strings_p
,
5197 case CPP_OPEN_PAREN
:
5198 /* If we see `( { ' then we are looking at the beginning of
5199 a GNU statement-expression. */
5200 if (cp_parser_allow_gnu_extensions_p (parser
)
5201 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5203 /* Statement-expressions are not allowed by the standard. */
5204 pedwarn (token
->location
, OPT_Wpedantic
,
5205 "ISO C++ forbids braced-groups within expressions");
5207 /* And they're not allowed outside of a function-body; you
5208 cannot, for example, write:
5210 int i = ({ int j = 3; j + 1; });
5212 at class or namespace scope. */
5213 if (!parser
->in_function_body
5214 || parser
->in_template_argument_list_p
)
5216 error_at (token
->location
,
5217 "statement-expressions are not allowed outside "
5218 "functions nor in template-argument lists");
5219 cp_parser_skip_to_end_of_block_or_statement (parser
);
5220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5221 cp_lexer_consume_token (parser
->lexer
);
5222 return error_mark_node
;
5225 return cp_parser_statement_expr (parser
);
5227 /* Otherwise it's a normal parenthesized expression. */
5230 bool saved_greater_than_is_operator_p
;
5232 location_t open_paren_loc
= token
->location
;
5234 /* Consume the `('. */
5235 matching_parens parens
;
5236 parens
.consume_open (parser
);
5237 /* Within a parenthesized expression, a `>' token is always
5238 the greater-than operator. */
5239 saved_greater_than_is_operator_p
5240 = parser
->greater_than_is_operator_p
;
5241 parser
->greater_than_is_operator_p
= true;
5243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5244 /* Left fold expression. */
5247 /* Parse the parenthesized expression. */
5248 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5250 token
= cp_lexer_peek_token (parser
->lexer
);
5251 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5253 expr
= cp_parser_fold_expression (parser
, expr
);
5254 if (expr
!= error_mark_node
5255 && cxx_dialect
< cxx17
5256 && !in_system_header_at (input_location
))
5257 pedwarn (input_location
, 0, "fold-expressions only available "
5258 "with -std=c++17 or -std=gnu++17");
5261 /* Let the front end know that this expression was
5262 enclosed in parentheses. This matters in case, for
5263 example, the expression is of the form `A::B', since
5264 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5266 expr
= finish_parenthesized_expr (expr
);
5268 /* DR 705: Wrapping an unqualified name in parentheses
5269 suppresses arg-dependent lookup. We want to pass back
5270 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5271 (c++/37862), but none of the others. */
5272 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5273 *idk
= CP_ID_KIND_NONE
;
5275 /* The `>' token might be the end of a template-id or
5276 template-parameter-list now. */
5277 parser
->greater_than_is_operator_p
5278 = saved_greater_than_is_operator_p
;
5280 /* Consume the `)'. */
5281 token
= cp_lexer_peek_token (parser
->lexer
);
5282 location_t close_paren_loc
= token
->location
;
5283 expr
.set_range (open_paren_loc
, close_paren_loc
);
5284 if (!parens
.require_close (parser
)
5285 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5286 cp_parser_skip_to_end_of_statement (parser
);
5291 case CPP_OPEN_SQUARE
:
5293 if (c_dialect_objc ())
5295 /* We might have an Objective-C++ message. */
5296 cp_parser_parse_tentatively (parser
);
5297 tree msg
= cp_parser_objc_message_expression (parser
);
5298 /* If that works out, we're done ... */
5299 if (cp_parser_parse_definitely (parser
))
5301 /* ... else, fall though to see if it's a lambda. */
5303 cp_expr lam
= cp_parser_lambda_expression (parser
);
5304 /* Don't warn about a failed tentative parse. */
5305 if (cp_parser_error_occurred (parser
))
5306 return error_mark_node
;
5307 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5311 case CPP_OBJC_STRING
:
5312 if (c_dialect_objc ())
5313 /* We have an Objective-C++ string literal. */
5314 return cp_parser_objc_expression (parser
);
5315 cp_parser_error (parser
, "expected primary-expression");
5316 return error_mark_node
;
5319 switch (token
->keyword
)
5321 /* These two are the boolean literals. */
5323 cp_lexer_consume_token (parser
->lexer
);
5324 return cp_expr (boolean_true_node
, token
->location
);
5326 cp_lexer_consume_token (parser
->lexer
);
5327 return cp_expr (boolean_false_node
, token
->location
);
5329 /* The `__null' literal. */
5331 cp_lexer_consume_token (parser
->lexer
);
5332 return cp_expr (null_node
, token
->location
);
5334 /* The `nullptr' literal. */
5336 cp_lexer_consume_token (parser
->lexer
);
5337 return cp_expr (nullptr_node
, token
->location
);
5339 /* Recognize the `this' keyword. */
5341 cp_lexer_consume_token (parser
->lexer
);
5342 if (parser
->local_variables_forbidden_p
)
5344 error_at (token
->location
,
5345 "%<this%> may not be used in this context");
5346 return error_mark_node
;
5348 /* Pointers cannot appear in constant-expressions. */
5349 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5350 return error_mark_node
;
5351 return cp_expr (finish_this_expr (), token
->location
);
5353 /* The `operator' keyword can be the beginning of an
5358 case RID_FUNCTION_NAME
:
5359 case RID_PRETTY_FUNCTION_NAME
:
5360 case RID_C99_FUNCTION_NAME
:
5362 non_integral_constant name
;
5364 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5365 __func__ are the names of variables -- but they are
5366 treated specially. Therefore, they are handled here,
5367 rather than relying on the generic id-expression logic
5368 below. Grammatically, these names are id-expressions.
5370 Consume the token. */
5371 token
= cp_lexer_consume_token (parser
->lexer
);
5373 switch (token
->keyword
)
5375 case RID_FUNCTION_NAME
:
5376 name
= NIC_FUNC_NAME
;
5378 case RID_PRETTY_FUNCTION_NAME
:
5379 name
= NIC_PRETTY_FUNC
;
5381 case RID_C99_FUNCTION_NAME
:
5382 name
= NIC_C99_FUNC
;
5388 if (cp_parser_non_integral_constant_expression (parser
, name
))
5389 return error_mark_node
;
5391 /* Look up the name. */
5392 return finish_fname (token
->u
.value
);
5399 source_location type_location
;
5400 location_t start_loc
5401 = cp_lexer_peek_token (parser
->lexer
)->location
;
5402 /* The `__builtin_va_arg' construct is used to handle
5403 `va_arg'. Consume the `__builtin_va_arg' token. */
5404 cp_lexer_consume_token (parser
->lexer
);
5405 /* Look for the opening `('. */
5406 matching_parens parens
;
5407 parens
.require_open (parser
);
5408 /* Now, parse the assignment-expression. */
5409 expression
= cp_parser_assignment_expression (parser
);
5410 /* Look for the `,'. */
5411 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5412 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5413 /* Parse the type-id. */
5415 type_id_in_expr_sentinel
s (parser
);
5416 type
= cp_parser_type_id (parser
);
5418 /* Look for the closing `)'. */
5419 location_t finish_loc
5420 = cp_lexer_peek_token (parser
->lexer
)->location
;
5421 parens
.require_close (parser
);
5422 /* Using `va_arg' in a constant-expression is not
5424 if (cp_parser_non_integral_constant_expression (parser
,
5426 return error_mark_node
;
5427 /* Construct a location of the form:
5428 __builtin_va_arg (v, int)
5429 ~~~~~~~~~~~~~~~~~~~~~^~~~
5430 with the caret at the type, ranging from the start of the
5431 "__builtin_va_arg" token to the close paren. */
5432 location_t combined_loc
5433 = make_location (type_location
, start_loc
, finish_loc
);
5434 return build_x_va_arg (combined_loc
, expression
, type
);
5438 return cp_parser_builtin_offsetof (parser
);
5440 case RID_HAS_NOTHROW_ASSIGN
:
5441 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5442 case RID_HAS_NOTHROW_COPY
:
5443 case RID_HAS_TRIVIAL_ASSIGN
:
5444 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5445 case RID_HAS_TRIVIAL_COPY
:
5446 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5447 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5448 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5449 case RID_IS_ABSTRACT
:
5450 case RID_IS_AGGREGATE
:
5451 case RID_IS_BASE_OF
:
5456 case RID_IS_LITERAL_TYPE
:
5458 case RID_IS_POLYMORPHIC
:
5459 case RID_IS_SAME_AS
:
5460 case RID_IS_STD_LAYOUT
:
5461 case RID_IS_TRIVIAL
:
5462 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5463 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5464 case RID_IS_TRIVIALLY_COPYABLE
:
5466 case RID_IS_ASSIGNABLE
:
5467 case RID_IS_CONSTRUCTIBLE
:
5468 return cp_parser_trait_expr (parser
, token
->keyword
);
5472 return cp_parser_requires_expression (parser
);
5474 /* Objective-C++ expressions. */
5476 case RID_AT_PROTOCOL
:
5477 case RID_AT_SELECTOR
:
5478 return cp_parser_objc_expression (parser
);
5481 if (parser
->in_function_body
5482 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5485 error_at (token
->location
,
5486 "a template declaration cannot appear at block scope");
5487 cp_parser_skip_to_end_of_block_or_statement (parser
);
5488 return error_mark_node
;
5492 cp_parser_error (parser
, "expected primary-expression");
5493 return error_mark_node
;
5496 /* An id-expression can start with either an identifier, a
5497 `::' as the beginning of a qualified-id, or the "operator"
5501 case CPP_TEMPLATE_ID
:
5502 case CPP_NESTED_NAME_SPECIFIER
:
5505 cp_expr id_expression
;
5507 const char *error_msg
;
5510 cp_token
*id_expr_token
;
5512 /* Parse the id-expression. */
5514 = cp_parser_id_expression (parser
,
5515 /*template_keyword_p=*/false,
5516 /*check_dependency_p=*/true,
5518 /*declarator_p=*/false,
5519 /*optional_p=*/false);
5520 if (id_expression
== error_mark_node
)
5521 return error_mark_node
;
5522 id_expr_token
= token
;
5523 token
= cp_lexer_peek_token (parser
->lexer
);
5524 done
= (token
->type
!= CPP_OPEN_SQUARE
5525 && token
->type
!= CPP_OPEN_PAREN
5526 && token
->type
!= CPP_DOT
5527 && token
->type
!= CPP_DEREF
5528 && token
->type
!= CPP_PLUS_PLUS
5529 && token
->type
!= CPP_MINUS_MINUS
);
5530 /* If we have a template-id, then no further lookup is
5531 required. If the template-id was for a template-class, we
5532 will sometimes have a TYPE_DECL at this point. */
5533 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5534 || TREE_CODE (id_expression
) == TYPE_DECL
)
5535 decl
= id_expression
;
5536 /* Look up the name. */
5539 tree ambiguous_decls
;
5541 /* If we already know that this lookup is ambiguous, then
5542 we've already issued an error message; there's no reason
5544 if (id_expr_token
->type
== CPP_NAME
5545 && id_expr_token
->error_reported
)
5547 cp_parser_simulate_error (parser
);
5548 return error_mark_node
;
5551 decl
= cp_parser_lookup_name (parser
, id_expression
,
5554 /*is_namespace=*/false,
5555 /*check_dependency=*/true,
5557 id_expr_token
->location
);
5558 /* If the lookup was ambiguous, an error will already have
5560 if (ambiguous_decls
)
5561 return error_mark_node
;
5563 /* In Objective-C++, we may have an Objective-C 2.0
5564 dot-syntax for classes here. */
5565 if (c_dialect_objc ()
5566 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5567 && TREE_CODE (decl
) == TYPE_DECL
5568 && objc_is_class_name (decl
))
5571 cp_lexer_consume_token (parser
->lexer
);
5572 component
= cp_parser_identifier (parser
);
5573 if (component
== error_mark_node
)
5574 return error_mark_node
;
5576 tree result
= objc_build_class_component_ref (id_expression
,
5578 /* Build a location of the form:
5581 with caret at the start of the component name (at
5582 input_location), ranging from the start of the id_expression
5583 to the end of the component name. */
5584 location_t combined_loc
5585 = make_location (input_location
, id_expression
.get_start (),
5586 get_finish (input_location
));
5587 protected_set_expr_location (result
, combined_loc
);
5591 /* In Objective-C++, an instance variable (ivar) may be preferred
5592 to whatever cp_parser_lookup_name() found.
5593 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5594 rest of c-family, we have to do a little extra work to preserve
5595 any location information in cp_expr "decl". Given that
5596 objc_lookup_ivar is implemented in "c-family" and "objc", we
5597 have a trip through the pure "tree" type, rather than cp_expr.
5598 Naively copying it back to "decl" would implicitly give the
5599 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5600 store an EXPR_LOCATION. Hence we only update "decl" (and
5601 hence its location_t) if we get back a different tree node. */
5602 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5604 if (decl_tree
!= decl
.get_value ())
5605 decl
= cp_expr (decl_tree
);
5607 /* If name lookup gives us a SCOPE_REF, then the
5608 qualifying scope was dependent. */
5609 if (TREE_CODE (decl
) == SCOPE_REF
)
5611 /* At this point, we do not know if DECL is a valid
5612 integral constant expression. We assume that it is
5613 in fact such an expression, so that code like:
5615 template <int N> struct A {
5619 is accepted. At template-instantiation time, we
5620 will check that B<N>::i is actually a constant. */
5623 /* Check to see if DECL is a local variable in a context
5624 where that is forbidden. */
5625 if (parser
->local_variables_forbidden_p
5626 && local_variable_p (decl
))
5628 error_at (id_expr_token
->location
,
5629 "local variable %qD may not appear in this context",
5631 return error_mark_node
;
5635 if (processing_template_decl
)
5636 if (tree fns
= maybe_get_fns (decl
))
5637 /* It's too difficult to mark ths in all the places where
5638 we know for sure we need to keep the lookup, so do it
5639 now. The cost is extra GC to recycle the lookups
5640 resolved at parse time. */
5643 decl
= (finish_id_expression
5644 (id_expression
, decl
, parser
->scope
,
5646 parser
->integral_constant_expression_p
,
5647 parser
->allow_non_integral_constant_expression_p
,
5648 &parser
->non_integral_constant_expression_p
,
5649 template_p
, done
, address_p
,
5652 id_expression
.get_location ()));
5654 cp_parser_error (parser
, error_msg
);
5655 decl
.set_location (id_expr_token
->location
);
5659 /* Anything else is an error. */
5661 cp_parser_error (parser
, "expected primary-expression");
5662 return error_mark_node
;
5666 static inline cp_expr
5667 cp_parser_primary_expression (cp_parser
*parser
,
5670 bool template_arg_p
,
5673 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5674 /*decltype*/false, idk
);
5677 /* Parse an id-expression.
5684 :: [opt] nested-name-specifier template [opt] unqualified-id
5686 :: operator-function-id
5689 Return a representation of the unqualified portion of the
5690 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5691 a `::' or nested-name-specifier.
5693 Often, if the id-expression was a qualified-id, the caller will
5694 want to make a SCOPE_REF to represent the qualified-id. This
5695 function does not do this in order to avoid wastefully creating
5696 SCOPE_REFs when they are not required.
5698 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5701 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5702 uninstantiated templates.
5704 If *TEMPLATE_P is non-NULL, it is set to true iff the
5705 `template' keyword is used to explicitly indicate that the entity
5706 named is a template.
5708 If DECLARATOR_P is true, the id-expression is appearing as part of
5709 a declarator, rather than as part of an expression. */
5712 cp_parser_id_expression (cp_parser
*parser
,
5713 bool template_keyword_p
,
5714 bool check_dependency_p
,
5719 bool global_scope_p
;
5720 bool nested_name_specifier_p
;
5722 /* Assume the `template' keyword was not used. */
5724 *template_p
= template_keyword_p
;
5726 /* Look for the optional `::' operator. */
5728 = (!template_keyword_p
5729 && (cp_parser_global_scope_opt (parser
,
5730 /*current_scope_valid_p=*/false)
5733 /* Look for the optional nested-name-specifier. */
5734 nested_name_specifier_p
5735 = (cp_parser_nested_name_specifier_opt (parser
,
5736 /*typename_keyword_p=*/false,
5743 /* If there is a nested-name-specifier, then we are looking at
5744 the first qualified-id production. */
5745 if (nested_name_specifier_p
)
5748 tree saved_object_scope
;
5749 tree saved_qualifying_scope
;
5750 cp_expr unqualified_id
;
5753 /* See if the next token is the `template' keyword. */
5755 template_p
= &is_template
;
5756 *template_p
= cp_parser_optional_template_keyword (parser
);
5757 /* Name lookup we do during the processing of the
5758 unqualified-id might obliterate SCOPE. */
5759 saved_scope
= parser
->scope
;
5760 saved_object_scope
= parser
->object_scope
;
5761 saved_qualifying_scope
= parser
->qualifying_scope
;
5762 /* Process the final unqualified-id. */
5763 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5766 /*optional_p=*/false);
5767 /* Restore the SAVED_SCOPE for our caller. */
5768 parser
->scope
= saved_scope
;
5769 parser
->object_scope
= saved_object_scope
;
5770 parser
->qualifying_scope
= saved_qualifying_scope
;
5772 return unqualified_id
;
5774 /* Otherwise, if we are in global scope, then we are looking at one
5775 of the other qualified-id productions. */
5776 else if (global_scope_p
)
5781 /* Peek at the next token. */
5782 token
= cp_lexer_peek_token (parser
->lexer
);
5784 /* If it's an identifier, and the next token is not a "<", then
5785 we can avoid the template-id case. This is an optimization
5786 for this common case. */
5787 if (token
->type
== CPP_NAME
5788 && !cp_parser_nth_token_starts_template_argument_list_p
5790 return cp_parser_identifier (parser
);
5792 cp_parser_parse_tentatively (parser
);
5793 /* Try a template-id. */
5794 id
= cp_parser_template_id (parser
,
5795 /*template_keyword_p=*/false,
5796 /*check_dependency_p=*/true,
5799 /* If that worked, we're done. */
5800 if (cp_parser_parse_definitely (parser
))
5803 /* Peek at the next token. (Changes in the token buffer may
5804 have invalidated the pointer obtained above.) */
5805 token
= cp_lexer_peek_token (parser
->lexer
);
5807 switch (token
->type
)
5810 return cp_parser_identifier (parser
);
5813 if (token
->keyword
== RID_OPERATOR
)
5814 return cp_parser_operator_function_id (parser
);
5818 cp_parser_error (parser
, "expected id-expression");
5819 return error_mark_node
;
5823 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5824 /*check_dependency_p=*/true,
5829 /* Parse an unqualified-id.
5833 operator-function-id
5834 conversion-function-id
5838 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5839 keyword, in a construct like `A::template ...'.
5841 Returns a representation of unqualified-id. For the `identifier'
5842 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5843 production a BIT_NOT_EXPR is returned; the operand of the
5844 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5845 other productions, see the documentation accompanying the
5846 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5847 names are looked up in uninstantiated templates. If DECLARATOR_P
5848 is true, the unqualified-id is appearing as part of a declarator,
5849 rather than as part of an expression. */
5852 cp_parser_unqualified_id (cp_parser
* parser
,
5853 bool template_keyword_p
,
5854 bool check_dependency_p
,
5860 /* Peek at the next token. */
5861 token
= cp_lexer_peek_token (parser
->lexer
);
5863 switch ((int) token
->type
)
5869 /* We don't know yet whether or not this will be a
5871 cp_parser_parse_tentatively (parser
);
5872 /* Try a template-id. */
5873 id
= cp_parser_template_id (parser
, template_keyword_p
,
5877 /* If it worked, we're done. */
5878 if (cp_parser_parse_definitely (parser
))
5880 /* Otherwise, it's an ordinary identifier. */
5881 return cp_parser_identifier (parser
);
5884 case CPP_TEMPLATE_ID
:
5885 return cp_parser_template_id (parser
, template_keyword_p
,
5893 tree qualifying_scope
;
5898 /* Consume the `~' token. */
5899 cp_lexer_consume_token (parser
->lexer
);
5900 /* Parse the class-name. The standard, as written, seems to
5903 template <typename T> struct S { ~S (); };
5904 template <typename T> S<T>::~S() {}
5906 is invalid, since `~' must be followed by a class-name, but
5907 `S<T>' is dependent, and so not known to be a class.
5908 That's not right; we need to look in uninstantiated
5909 templates. A further complication arises from:
5911 template <typename T> void f(T t) {
5915 Here, it is not possible to look up `T' in the scope of `T'
5916 itself. We must look in both the current scope, and the
5917 scope of the containing complete expression.
5919 Yet another issue is:
5928 The standard does not seem to say that the `S' in `~S'
5929 should refer to the type `S' and not the data member
5932 /* DR 244 says that we look up the name after the "~" in the
5933 same scope as we looked up the qualifying name. That idea
5934 isn't fully worked out; it's more complicated than that. */
5935 scope
= parser
->scope
;
5936 object_scope
= parser
->object_scope
;
5937 qualifying_scope
= parser
->qualifying_scope
;
5939 /* Check for invalid scopes. */
5940 if (scope
== error_mark_node
)
5942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5943 cp_lexer_consume_token (parser
->lexer
);
5944 return error_mark_node
;
5946 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5948 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5949 error_at (token
->location
,
5950 "scope %qT before %<~%> is not a class-name",
5952 cp_parser_simulate_error (parser
);
5953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5954 cp_lexer_consume_token (parser
->lexer
);
5955 return error_mark_node
;
5957 gcc_assert (!scope
|| TYPE_P (scope
));
5959 /* If the name is of the form "X::~X" it's OK even if X is a
5961 token
= cp_lexer_peek_token (parser
->lexer
);
5963 && token
->type
== CPP_NAME
5964 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5966 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5967 || (CLASS_TYPE_P (scope
)
5968 && constructor_name_p (token
->u
.value
, scope
))))
5970 cp_lexer_consume_token (parser
->lexer
);
5971 return build_nt (BIT_NOT_EXPR
, scope
);
5974 /* ~auto means the destructor of whatever the object is. */
5975 if (cp_parser_is_keyword (token
, RID_AUTO
))
5977 if (cxx_dialect
< cxx14
)
5978 pedwarn (input_location
, 0,
5979 "%<~auto%> only available with "
5980 "-std=c++14 or -std=gnu++14");
5981 cp_lexer_consume_token (parser
->lexer
);
5982 return build_nt (BIT_NOT_EXPR
, make_auto ());
5985 /* If there was an explicit qualification (S::~T), first look
5986 in the scope given by the qualification (i.e., S).
5988 Note: in the calls to cp_parser_class_name below we pass
5989 typename_type so that lookup finds the injected-class-name
5990 rather than the constructor. */
5992 type_decl
= NULL_TREE
;
5995 cp_parser_parse_tentatively (parser
);
5996 type_decl
= cp_parser_class_name (parser
,
5997 /*typename_keyword_p=*/false,
5998 /*template_keyword_p=*/false,
6000 /*check_dependency=*/false,
6001 /*class_head_p=*/false,
6003 if (cp_parser_parse_definitely (parser
))
6006 /* In "N::S::~S", look in "N" as well. */
6007 if (!done
&& scope
&& qualifying_scope
)
6009 cp_parser_parse_tentatively (parser
);
6010 parser
->scope
= qualifying_scope
;
6011 parser
->object_scope
= NULL_TREE
;
6012 parser
->qualifying_scope
= NULL_TREE
;
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (cp_parser_parse_definitely (parser
))
6024 /* In "p->S::~T", look in the scope given by "*p" as well. */
6025 else if (!done
&& object_scope
)
6027 cp_parser_parse_tentatively (parser
);
6028 parser
->scope
= object_scope
;
6029 parser
->object_scope
= NULL_TREE
;
6030 parser
->qualifying_scope
= NULL_TREE
;
6032 = cp_parser_class_name (parser
,
6033 /*typename_keyword_p=*/false,
6034 /*template_keyword_p=*/false,
6036 /*check_dependency=*/false,
6037 /*class_head_p=*/false,
6039 if (cp_parser_parse_definitely (parser
))
6042 /* Look in the surrounding context. */
6045 parser
->scope
= NULL_TREE
;
6046 parser
->object_scope
= NULL_TREE
;
6047 parser
->qualifying_scope
= NULL_TREE
;
6048 if (processing_template_decl
)
6049 cp_parser_parse_tentatively (parser
);
6051 = cp_parser_class_name (parser
,
6052 /*typename_keyword_p=*/false,
6053 /*template_keyword_p=*/false,
6055 /*check_dependency=*/false,
6056 /*class_head_p=*/false,
6058 if (processing_template_decl
6059 && ! cp_parser_parse_definitely (parser
))
6061 /* We couldn't find a type with this name. If we're parsing
6062 tentatively, fail and try something else. */
6063 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6065 cp_parser_simulate_error (parser
);
6066 return error_mark_node
;
6068 /* Otherwise, accept it and check for a match at instantiation
6070 type_decl
= cp_parser_identifier (parser
);
6071 if (type_decl
!= error_mark_node
)
6072 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6076 /* If an error occurred, assume that the name of the
6077 destructor is the same as the name of the qualifying
6078 class. That allows us to keep parsing after running
6079 into ill-formed destructor names. */
6080 if (type_decl
== error_mark_node
&& scope
)
6081 return build_nt (BIT_NOT_EXPR
, scope
);
6082 else if (type_decl
== error_mark_node
)
6083 return error_mark_node
;
6085 /* Check that destructor name and scope match. */
6086 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6088 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6089 error_at (token
->location
,
6090 "declaration of %<~%T%> as member of %qT",
6092 cp_parser_simulate_error (parser
);
6093 return error_mark_node
;
6098 A typedef-name that names a class shall not be used as the
6099 identifier in the declarator for a destructor declaration. */
6101 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6102 && !DECL_SELF_REFERENCE_P (type_decl
)
6103 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6104 error_at (token
->location
,
6105 "typedef-name %qD used as destructor declarator",
6108 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6112 if (token
->keyword
== RID_OPERATOR
)
6116 /* This could be a template-id, so we try that first. */
6117 cp_parser_parse_tentatively (parser
);
6118 /* Try a template-id. */
6119 id
= cp_parser_template_id (parser
, template_keyword_p
,
6120 /*check_dependency_p=*/true,
6123 /* If that worked, we're done. */
6124 if (cp_parser_parse_definitely (parser
))
6126 /* We still don't know whether we're looking at an
6127 operator-function-id or a conversion-function-id. */
6128 cp_parser_parse_tentatively (parser
);
6129 /* Try an operator-function-id. */
6130 id
= cp_parser_operator_function_id (parser
);
6131 /* If that didn't work, try a conversion-function-id. */
6132 if (!cp_parser_parse_definitely (parser
))
6133 id
= cp_parser_conversion_function_id (parser
);
6142 cp_parser_error (parser
, "expected unqualified-id");
6143 return error_mark_node
;
6147 /* Parse an (optional) nested-name-specifier.
6149 nested-name-specifier: [C++98]
6150 class-or-namespace-name :: nested-name-specifier [opt]
6151 class-or-namespace-name :: template nested-name-specifier [opt]
6153 nested-name-specifier: [C++0x]
6156 nested-name-specifier identifier ::
6157 nested-name-specifier template [opt] simple-template-id ::
6159 PARSER->SCOPE should be set appropriately before this function is
6160 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6161 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6164 Sets PARSER->SCOPE to the class (TYPE) or namespace
6165 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6166 it unchanged if there is no nested-name-specifier. Returns the new
6167 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6169 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6170 part of a declaration and/or decl-specifier. */
6173 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6174 bool typename_keyword_p
,
6175 bool check_dependency_p
,
6177 bool is_declaration
,
6178 bool template_keyword_p
/* = false */)
6180 bool success
= false;
6181 cp_token_position start
= 0;
6184 /* Remember where the nested-name-specifier starts. */
6185 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6187 start
= cp_lexer_token_position (parser
->lexer
, false);
6188 push_deferring_access_checks (dk_deferred
);
6195 tree saved_qualifying_scope
;
6197 /* Spot cases that cannot be the beginning of a
6198 nested-name-specifier. */
6199 token
= cp_lexer_peek_token (parser
->lexer
);
6201 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6202 the already parsed nested-name-specifier. */
6203 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6205 /* Grab the nested-name-specifier and continue the loop. */
6206 cp_parser_pre_parsed_nested_name_specifier (parser
);
6207 /* If we originally encountered this nested-name-specifier
6208 with IS_DECLARATION set to false, we will not have
6209 resolved TYPENAME_TYPEs, so we must do so here. */
6211 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6213 new_scope
= resolve_typename_type (parser
->scope
,
6214 /*only_current_p=*/false);
6215 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6216 parser
->scope
= new_scope
;
6222 /* Spot cases that cannot be the beginning of a
6223 nested-name-specifier. On the second and subsequent times
6224 through the loop, we look for the `template' keyword. */
6225 if (success
&& token
->keyword
== RID_TEMPLATE
)
6227 /* A template-id can start a nested-name-specifier. */
6228 else if (token
->type
== CPP_TEMPLATE_ID
)
6230 /* DR 743: decltype can be used in a nested-name-specifier. */
6231 else if (token_is_decltype (token
))
6235 /* If the next token is not an identifier, then it is
6236 definitely not a type-name or namespace-name. */
6237 if (token
->type
!= CPP_NAME
)
6239 /* If the following token is neither a `<' (to begin a
6240 template-id), nor a `::', then we are not looking at a
6241 nested-name-specifier. */
6242 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6244 if (token
->type
== CPP_COLON
6245 && parser
->colon_corrects_to_scope_p
6246 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6248 gcc_rich_location
richloc (token
->location
);
6249 richloc
.add_fixit_replace ("::");
6251 "found %<:%> in nested-name-specifier, "
6253 token
->type
= CPP_SCOPE
;
6256 if (token
->type
!= CPP_SCOPE
6257 && !cp_parser_nth_token_starts_template_argument_list_p
6262 /* The nested-name-specifier is optional, so we parse
6264 cp_parser_parse_tentatively (parser
);
6266 /* Look for the optional `template' keyword, if this isn't the
6267 first time through the loop. */
6269 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6271 /* Save the old scope since the name lookup we are about to do
6272 might destroy it. */
6273 old_scope
= parser
->scope
;
6274 saved_qualifying_scope
= parser
->qualifying_scope
;
6275 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6276 look up names in "X<T>::I" in order to determine that "Y" is
6277 a template. So, if we have a typename at this point, we make
6278 an effort to look through it. */
6280 && !typename_keyword_p
6282 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6283 parser
->scope
= resolve_typename_type (parser
->scope
,
6284 /*only_current_p=*/false);
6285 /* Parse the qualifying entity. */
6287 = cp_parser_qualifying_entity (parser
,
6293 /* Look for the `::' token. */
6294 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6296 /* If we found what we wanted, we keep going; otherwise, we're
6298 if (!cp_parser_parse_definitely (parser
))
6300 bool error_p
= false;
6302 /* Restore the OLD_SCOPE since it was valid before the
6303 failed attempt at finding the last
6304 class-or-namespace-name. */
6305 parser
->scope
= old_scope
;
6306 parser
->qualifying_scope
= saved_qualifying_scope
;
6308 /* If the next token is a decltype, and the one after that is a
6309 `::', then the decltype has failed to resolve to a class or
6310 enumeration type. Give this error even when parsing
6311 tentatively since it can't possibly be valid--and we're going
6312 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6313 won't get another chance.*/
6314 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6315 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6318 token
= cp_lexer_consume_token (parser
->lexer
);
6319 error_at (token
->location
, "decltype evaluates to %qT, "
6320 "which is not a class or enumeration type",
6321 token
->u
.tree_check_value
->value
);
6322 parser
->scope
= error_mark_node
;
6326 cp_lexer_consume_token (parser
->lexer
);
6329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6330 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6332 /* If we have a non-type template-id followed by ::, it can't
6333 possibly be valid. */
6334 token
= cp_lexer_peek_token (parser
->lexer
);
6335 tree tid
= token
->u
.tree_check_value
->value
;
6336 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6337 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6339 tree tmpl
= NULL_TREE
;
6340 if (is_overloaded_fn (tid
))
6342 tree fns
= get_fns (tid
);
6343 if (OVL_SINGLE_P (fns
))
6344 tmpl
= OVL_FIRST (fns
);
6345 error_at (token
->location
, "function template-id %qD "
6346 "in nested-name-specifier", tid
);
6350 /* Variable template. */
6351 tmpl
= TREE_OPERAND (tid
, 0);
6352 gcc_assert (variable_template_p (tmpl
));
6353 error_at (token
->location
, "variable template-id %qD "
6354 "in nested-name-specifier", tid
);
6357 inform (DECL_SOURCE_LOCATION (tmpl
),
6358 "%qD declared here", tmpl
);
6360 parser
->scope
= error_mark_node
;
6364 cp_lexer_consume_token (parser
->lexer
);
6365 cp_lexer_consume_token (parser
->lexer
);
6369 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6371 /* If the next token is an identifier, and the one after
6372 that is a `::', then any valid interpretation would have
6373 found a class-or-namespace-name. */
6374 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6375 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6377 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6380 token
= cp_lexer_consume_token (parser
->lexer
);
6383 if (!token
->error_reported
)
6386 tree ambiguous_decls
;
6388 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6390 /*is_template=*/false,
6391 /*is_namespace=*/false,
6392 /*check_dependency=*/true,
6395 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6396 error_at (token
->location
,
6397 "%qD used without template arguments",
6399 else if (ambiguous_decls
)
6401 // cp_parser_lookup_name has the same diagnostic,
6402 // thus make sure to emit it at most once.
6403 if (cp_parser_uncommitted_to_tentative_parse_p
6406 error_at (token
->location
,
6407 "reference to %qD is ambiguous",
6409 print_candidates (ambiguous_decls
);
6411 decl
= error_mark_node
;
6415 if (cxx_dialect
!= cxx98
)
6416 cp_parser_name_lookup_error
6417 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6420 cp_parser_name_lookup_error
6421 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6425 parser
->scope
= error_mark_node
;
6427 /* Treat this as a successful nested-name-specifier
6432 If the name found is not a class-name (clause
6433 _class_) or namespace-name (_namespace.def_), the
6434 program is ill-formed. */
6437 cp_lexer_consume_token (parser
->lexer
);
6441 /* We've found one valid nested-name-specifier. */
6443 /* Name lookup always gives us a DECL. */
6444 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6445 new_scope
= TREE_TYPE (new_scope
);
6446 /* Uses of "template" must be followed by actual templates. */
6447 if (template_keyword_p
6448 && !(CLASS_TYPE_P (new_scope
)
6449 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6451 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6452 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6453 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6454 == TEMPLATE_ID_EXPR
)))
6455 permerror (input_location
, TYPE_P (new_scope
)
6456 ? G_("%qT is not a template")
6457 : G_("%qD is not a template"),
6459 /* If it is a class scope, try to complete it; we are about to
6460 be looking up names inside the class. */
6461 if (TYPE_P (new_scope
)
6462 /* Since checking types for dependency can be expensive,
6463 avoid doing it if the type is already complete. */
6464 && !COMPLETE_TYPE_P (new_scope
)
6465 /* Do not try to complete dependent types. */
6466 && !dependent_type_p (new_scope
))
6468 new_scope
= complete_type (new_scope
);
6469 /* If it is a typedef to current class, use the current
6470 class instead, as the typedef won't have any names inside
6472 if (!COMPLETE_TYPE_P (new_scope
)
6473 && currently_open_class (new_scope
))
6474 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6476 /* Make sure we look in the right scope the next time through
6478 parser
->scope
= new_scope
;
6481 /* If parsing tentatively, replace the sequence of tokens that makes
6482 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6483 token. That way, should we re-parse the token stream, we will
6484 not have to repeat the effort required to do the parse, nor will
6485 we issue duplicate error messages. */
6486 if (success
&& start
)
6490 token
= cp_lexer_token_at (parser
->lexer
, start
);
6491 /* Reset the contents of the START token. */
6492 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6493 /* Retrieve any deferred checks. Do not pop this access checks yet
6494 so the memory will not be reclaimed during token replacing below. */
6495 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6496 token
->u
.tree_check_value
->value
= parser
->scope
;
6497 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6498 token
->u
.tree_check_value
->qualifying_scope
=
6499 parser
->qualifying_scope
;
6500 token
->keyword
= RID_MAX
;
6502 /* Purge all subsequent tokens. */
6503 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6507 pop_to_parent_deferring_access_checks ();
6509 return success
? parser
->scope
: NULL_TREE
;
6512 /* Parse a nested-name-specifier. See
6513 cp_parser_nested_name_specifier_opt for details. This function
6514 behaves identically, except that it will an issue an error if no
6515 nested-name-specifier is present. */
6518 cp_parser_nested_name_specifier (cp_parser
*parser
,
6519 bool typename_keyword_p
,
6520 bool check_dependency_p
,
6522 bool is_declaration
)
6526 /* Look for the nested-name-specifier. */
6527 scope
= cp_parser_nested_name_specifier_opt (parser
,
6532 /* If it was not present, issue an error message. */
6535 cp_parser_error (parser
, "expected nested-name-specifier");
6536 parser
->scope
= NULL_TREE
;
6542 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6543 this is either a class-name or a namespace-name (which corresponds
6544 to the class-or-namespace-name production in the grammar). For
6545 C++0x, it can also be a type-name that refers to an enumeration
6546 type or a simple-template-id.
6548 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6549 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6550 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6551 TYPE_P is TRUE iff the next name should be taken as a class-name,
6552 even the same name is declared to be another entity in the same
6555 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6556 specified by the class-or-namespace-name. If neither is found the
6557 ERROR_MARK_NODE is returned. */
6560 cp_parser_qualifying_entity (cp_parser
*parser
,
6561 bool typename_keyword_p
,
6562 bool template_keyword_p
,
6563 bool check_dependency_p
,
6565 bool is_declaration
)
6568 tree saved_qualifying_scope
;
6569 tree saved_object_scope
;
6572 bool successful_parse_p
;
6574 /* DR 743: decltype can appear in a nested-name-specifier. */
6575 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6577 scope
= cp_parser_decltype (parser
);
6578 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6579 && !MAYBE_CLASS_TYPE_P (scope
))
6581 cp_parser_simulate_error (parser
);
6582 return error_mark_node
;
6584 if (TYPE_NAME (scope
))
6585 scope
= TYPE_NAME (scope
);
6589 /* Before we try to parse the class-name, we must save away the
6590 current PARSER->SCOPE since cp_parser_class_name will destroy
6592 saved_scope
= parser
->scope
;
6593 saved_qualifying_scope
= parser
->qualifying_scope
;
6594 saved_object_scope
= parser
->object_scope
;
6595 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6596 there is no need to look for a namespace-name. */
6597 only_class_p
= template_keyword_p
6598 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6600 cp_parser_parse_tentatively (parser
);
6601 scope
= cp_parser_class_name (parser
,
6604 type_p
? class_type
: none_type
,
6606 /*class_head_p=*/false,
6608 /*enum_ok=*/cxx_dialect
> cxx98
);
6609 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6610 /* If that didn't work, try for a namespace-name. */
6611 if (!only_class_p
&& !successful_parse_p
)
6613 /* Restore the saved scope. */
6614 parser
->scope
= saved_scope
;
6615 parser
->qualifying_scope
= saved_qualifying_scope
;
6616 parser
->object_scope
= saved_object_scope
;
6617 /* If we are not looking at an identifier followed by the scope
6618 resolution operator, then this is not part of a
6619 nested-name-specifier. (Note that this function is only used
6620 to parse the components of a nested-name-specifier.) */
6621 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6622 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6623 return error_mark_node
;
6624 scope
= cp_parser_namespace_name (parser
);
6630 /* Return true if we are looking at a compound-literal, false otherwise. */
6633 cp_parser_compound_literal_p (cp_parser
*parser
)
6635 cp_lexer_save_tokens (parser
->lexer
);
6637 /* Skip tokens until the next token is a closing parenthesis.
6638 If we find the closing `)', and the next token is a `{', then
6639 we are looking at a compound-literal. */
6640 bool compound_literal_p
6641 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6642 /*consume_paren=*/true)
6643 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6645 /* Roll back the tokens we skipped. */
6646 cp_lexer_rollback_tokens (parser
->lexer
);
6648 return compound_literal_p
;
6651 /* Return true if EXPR is the integer constant zero or a complex constant
6652 of zero, without any folding, but ignoring location wrappers. */
6655 literal_integer_zerop (const_tree expr
)
6657 return (location_wrapper_p (expr
)
6658 && integer_zerop (TREE_OPERAND (expr
, 0)));
6661 /* Parse a postfix-expression.
6665 postfix-expression [ expression ]
6666 postfix-expression ( expression-list [opt] )
6667 simple-type-specifier ( expression-list [opt] )
6668 typename :: [opt] nested-name-specifier identifier
6669 ( expression-list [opt] )
6670 typename :: [opt] nested-name-specifier template [opt] template-id
6671 ( expression-list [opt] )
6672 postfix-expression . template [opt] id-expression
6673 postfix-expression -> template [opt] id-expression
6674 postfix-expression . pseudo-destructor-name
6675 postfix-expression -> pseudo-destructor-name
6676 postfix-expression ++
6677 postfix-expression --
6678 dynamic_cast < type-id > ( expression )
6679 static_cast < type-id > ( expression )
6680 reinterpret_cast < type-id > ( expression )
6681 const_cast < type-id > ( expression )
6682 typeid ( expression )
6688 ( type-id ) { initializer-list , [opt] }
6690 This extension is a GNU version of the C99 compound-literal
6691 construct. (The C99 grammar uses `type-name' instead of `type-id',
6692 but they are essentially the same concept.)
6694 If ADDRESS_P is true, the postfix expression is the operand of the
6695 `&' operator. CAST_P is true if this expression is the target of a
6698 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6699 class member access expressions [expr.ref].
6701 Returns a representation of the expression. */
6704 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6705 bool member_access_only_p
, bool decltype_p
,
6706 cp_id_kind
* pidk_return
)
6711 cp_id_kind idk
= CP_ID_KIND_NONE
;
6712 cp_expr postfix_expression
= NULL_TREE
;
6713 bool is_member_access
= false;
6715 /* Peek at the next token. */
6716 token
= cp_lexer_peek_token (parser
->lexer
);
6717 loc
= token
->location
;
6718 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6720 /* Some of the productions are determined by keywords. */
6721 keyword
= token
->keyword
;
6731 const char *saved_message
;
6732 bool saved_in_type_id_in_expr_p
;
6734 /* All of these can be handled in the same way from the point
6735 of view of parsing. Begin by consuming the token
6736 identifying the cast. */
6737 cp_lexer_consume_token (parser
->lexer
);
6739 /* New types cannot be defined in the cast. */
6740 saved_message
= parser
->type_definition_forbidden_message
;
6741 parser
->type_definition_forbidden_message
6742 = G_("types may not be defined in casts");
6744 /* Look for the opening `<'. */
6745 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6746 /* Parse the type to which we are casting. */
6747 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6748 parser
->in_type_id_in_expr_p
= true;
6749 type
= cp_parser_type_id (parser
);
6750 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6751 /* Look for the closing `>'. */
6752 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6753 /* Restore the old message. */
6754 parser
->type_definition_forbidden_message
= saved_message
;
6756 bool saved_greater_than_is_operator_p
6757 = parser
->greater_than_is_operator_p
;
6758 parser
->greater_than_is_operator_p
= true;
6760 /* And the expression which is being cast. */
6761 matching_parens parens
;
6762 parens
.require_open (parser
);
6763 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6764 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6766 location_t end_loc
= close_paren
?
6767 close_paren
->location
: UNKNOWN_LOCATION
;
6769 parser
->greater_than_is_operator_p
6770 = saved_greater_than_is_operator_p
;
6772 /* Only type conversions to integral or enumeration types
6773 can be used in constant-expressions. */
6774 if (!cast_valid_in_integral_constant_expression_p (type
)
6775 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6777 postfix_expression
= error_mark_node
;
6785 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6789 = build_static_cast (type
, expression
, tf_warning_or_error
);
6793 = build_reinterpret_cast (type
, expression
,
6794 tf_warning_or_error
);
6798 = build_const_cast (type
, expression
, tf_warning_or_error
);
6804 /* Construct a location e.g. :
6805 reinterpret_cast <int *> (expr)
6806 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6807 ranging from the start of the "*_cast" token to the final closing
6808 paren, with the caret at the start. */
6809 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6810 postfix_expression
.set_location (cp_cast_loc
);
6817 const char *saved_message
;
6818 bool saved_in_type_id_in_expr_p
;
6820 /* Consume the `typeid' token. */
6821 cp_lexer_consume_token (parser
->lexer
);
6822 /* Look for the `(' token. */
6823 matching_parens parens
;
6824 parens
.require_open (parser
);
6825 /* Types cannot be defined in a `typeid' expression. */
6826 saved_message
= parser
->type_definition_forbidden_message
;
6827 parser
->type_definition_forbidden_message
6828 = G_("types may not be defined in a %<typeid%> expression");
6829 /* We can't be sure yet whether we're looking at a type-id or an
6831 cp_parser_parse_tentatively (parser
);
6832 /* Try a type-id first. */
6833 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6834 parser
->in_type_id_in_expr_p
= true;
6835 type
= cp_parser_type_id (parser
);
6836 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6837 /* Look for the `)' token. Otherwise, we can't be sure that
6838 we're not looking at an expression: consider `typeid (int
6839 (3))', for example. */
6840 cp_token
*close_paren
= parens
.require_close (parser
);
6841 /* If all went well, simply lookup the type-id. */
6842 if (cp_parser_parse_definitely (parser
))
6843 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6844 /* Otherwise, fall back to the expression variant. */
6849 /* Look for an expression. */
6850 expression
= cp_parser_expression (parser
, & idk
);
6851 /* Compute its typeid. */
6852 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6853 /* Look for the `)' token. */
6854 close_paren
= parens
.require_close (parser
);
6856 /* Restore the saved message. */
6857 parser
->type_definition_forbidden_message
= saved_message
;
6858 /* `typeid' may not appear in an integral constant expression. */
6859 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6860 postfix_expression
= error_mark_node
;
6862 /* Construct a location e.g. :
6865 ranging from the start of the "typeid" token to the final closing
6866 paren, with the caret at the start. */
6869 location_t typeid_loc
6870 = make_location (start_loc
, start_loc
, close_paren
->location
);
6871 postfix_expression
.set_location (typeid_loc
);
6872 postfix_expression
.maybe_add_location_wrapper ();
6880 /* The syntax permitted here is the same permitted for an
6881 elaborated-type-specifier. */
6882 ++parser
->prevent_constrained_type_specifiers
;
6883 type
= cp_parser_elaborated_type_specifier (parser
,
6884 /*is_friend=*/false,
6885 /*is_declaration=*/false);
6886 --parser
->prevent_constrained_type_specifiers
;
6887 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6892 case RID_BUILTIN_SHUFFLE
:
6893 case RID_BUILTIN_LAUNDER
:
6895 vec
<tree
, va_gc
> *vec
;
6899 cp_lexer_consume_token (parser
->lexer
);
6900 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6901 /*cast_p=*/false, /*allow_expansion_p=*/true,
6902 /*non_constant_p=*/NULL
);
6905 postfix_expression
= error_mark_node
;
6909 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6915 if (vec
->length () == 1)
6917 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6920 error_at (loc
, "wrong number of arguments to "
6921 "%<__builtin_addressof%>");
6922 postfix_expression
= error_mark_node
;
6926 case RID_BUILTIN_LAUNDER
:
6927 if (vec
->length () == 1)
6928 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6929 tf_warning_or_error
);
6932 error_at (loc
, "wrong number of arguments to "
6933 "%<__builtin_launder%>");
6934 postfix_expression
= error_mark_node
;
6938 case RID_BUILTIN_SHUFFLE
:
6939 if (vec
->length () == 2)
6941 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6942 (*vec
)[1], tf_warning_or_error
);
6943 else if (vec
->length () == 3)
6945 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6946 (*vec
)[2], tf_warning_or_error
);
6949 error_at (loc
, "wrong number of arguments to "
6950 "%<__builtin_shuffle%>");
6951 postfix_expression
= error_mark_node
;
6965 /* If the next thing is a simple-type-specifier, we may be
6966 looking at a functional cast. We could also be looking at
6967 an id-expression. So, we try the functional cast, and if
6968 that doesn't work we fall back to the primary-expression. */
6969 cp_parser_parse_tentatively (parser
);
6970 /* Look for the simple-type-specifier. */
6971 ++parser
->prevent_constrained_type_specifiers
;
6972 type
= cp_parser_simple_type_specifier (parser
,
6973 /*decl_specs=*/NULL
,
6974 CP_PARSER_FLAGS_NONE
);
6975 --parser
->prevent_constrained_type_specifiers
;
6976 /* Parse the cast itself. */
6977 if (!cp_parser_error_occurred (parser
))
6979 = cp_parser_functional_cast (parser
, type
);
6980 /* If that worked, we're done. */
6981 if (cp_parser_parse_definitely (parser
))
6984 /* If the functional-cast didn't work out, try a
6985 compound-literal. */
6986 if (cp_parser_allow_gnu_extensions_p (parser
)
6987 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6989 cp_expr initializer
= NULL_TREE
;
6991 cp_parser_parse_tentatively (parser
);
6993 matching_parens parens
;
6994 parens
.consume_open (parser
);
6996 /* Avoid calling cp_parser_type_id pointlessly, see comment
6997 in cp_parser_cast_expression about c++/29234. */
6998 if (!cp_parser_compound_literal_p (parser
))
6999 cp_parser_simulate_error (parser
);
7002 /* Parse the type. */
7003 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7004 parser
->in_type_id_in_expr_p
= true;
7005 type
= cp_parser_type_id (parser
);
7006 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7007 parens
.require_close (parser
);
7010 /* If things aren't going well, there's no need to
7012 if (!cp_parser_error_occurred (parser
))
7014 bool non_constant_p
;
7015 /* Parse the brace-enclosed initializer list. */
7016 initializer
= cp_parser_braced_list (parser
,
7019 /* If that worked, we're definitely looking at a
7020 compound-literal expression. */
7021 if (cp_parser_parse_definitely (parser
))
7023 /* Warn the user that a compound literal is not
7024 allowed in standard C++. */
7025 pedwarn (input_location
, OPT_Wpedantic
,
7026 "ISO C++ forbids compound-literals");
7027 /* For simplicity, we disallow compound literals in
7028 constant-expressions. We could
7029 allow compound literals of integer type, whose
7030 initializer was a constant, in constant
7031 expressions. Permitting that usage, as a further
7032 extension, would not change the meaning of any
7033 currently accepted programs. (Of course, as
7034 compound literals are not part of ISO C++, the
7035 standard has nothing to say.) */
7036 if (cp_parser_non_integral_constant_expression (parser
,
7039 postfix_expression
= error_mark_node
;
7042 /* Form the representation of the compound-literal. */
7044 = finish_compound_literal (type
, initializer
,
7045 tf_warning_or_error
, fcl_c99
);
7046 postfix_expression
.set_location (initializer
.get_location ());
7051 /* It must be a primary-expression. */
7053 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7054 /*template_arg_p=*/false,
7061 /* Note that we don't need to worry about calling build_cplus_new on a
7062 class-valued CALL_EXPR in decltype when it isn't the end of the
7063 postfix-expression; unary_complex_lvalue will take care of that for
7066 /* Keep looping until the postfix-expression is complete. */
7069 if (idk
== CP_ID_KIND_UNQUALIFIED
7070 && identifier_p (postfix_expression
)
7071 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7072 /* It is not a Koenig lookup function call. */
7074 = unqualified_name_lookup_error (postfix_expression
);
7076 /* Peek at the next token. */
7077 token
= cp_lexer_peek_token (parser
->lexer
);
7079 switch (token
->type
)
7081 case CPP_OPEN_SQUARE
:
7082 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7084 cp_parser_error (parser
,
7085 "two consecutive %<[%> shall "
7086 "only introduce an attribute");
7087 return error_mark_node
;
7090 = cp_parser_postfix_open_square_expression (parser
,
7094 postfix_expression
.set_range (start_loc
,
7095 postfix_expression
.get_location ());
7097 idk
= CP_ID_KIND_NONE
;
7098 is_member_access
= false;
7101 case CPP_OPEN_PAREN
:
7102 /* postfix-expression ( expression-list [opt] ) */
7105 bool is_builtin_constant_p
;
7106 bool saved_integral_constant_expression_p
= false;
7107 bool saved_non_integral_constant_expression_p
= false;
7108 tsubst_flags_t complain
= complain_flags (decltype_p
);
7109 vec
<tree
, va_gc
> *args
;
7110 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7112 is_member_access
= false;
7114 is_builtin_constant_p
7115 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7116 if (is_builtin_constant_p
)
7118 /* The whole point of __builtin_constant_p is to allow
7119 non-constant expressions to appear as arguments. */
7120 saved_integral_constant_expression_p
7121 = parser
->integral_constant_expression_p
;
7122 saved_non_integral_constant_expression_p
7123 = parser
->non_integral_constant_expression_p
;
7124 parser
->integral_constant_expression_p
= false;
7126 args
= (cp_parser_parenthesized_expression_list
7128 /*cast_p=*/false, /*allow_expansion_p=*/true,
7129 /*non_constant_p=*/NULL
,
7130 /*close_paren_loc=*/&close_paren_loc
,
7131 /*wrap_locations_p=*/true));
7132 if (is_builtin_constant_p
)
7134 parser
->integral_constant_expression_p
7135 = saved_integral_constant_expression_p
;
7136 parser
->non_integral_constant_expression_p
7137 = saved_non_integral_constant_expression_p
;
7142 postfix_expression
= error_mark_node
;
7146 /* Function calls are not permitted in
7147 constant-expressions. */
7148 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7149 && cp_parser_non_integral_constant_expression (parser
,
7152 postfix_expression
= error_mark_node
;
7153 release_tree_vector (args
);
7158 if (idk
== CP_ID_KIND_UNQUALIFIED
7159 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7161 if (identifier_p (postfix_expression
))
7163 if (!args
->is_empty ())
7166 if (!any_type_dependent_arguments_p (args
))
7168 = perform_koenig_lookup (postfix_expression
, args
,
7173 = unqualified_fn_lookup_error (postfix_expression
);
7175 /* We do not perform argument-dependent lookup if
7176 normal lookup finds a non-function, in accordance
7177 with the expected resolution of DR 218. */
7178 else if (!args
->is_empty ()
7179 && is_overloaded_fn (postfix_expression
))
7181 tree fn
= get_first_fn (postfix_expression
);
7182 fn
= STRIP_TEMPLATE (fn
);
7184 /* Do not do argument dependent lookup if regular
7185 lookup finds a member function or a block-scope
7186 function declaration. [basic.lookup.argdep]/3 */
7187 if (!DECL_FUNCTION_MEMBER_P (fn
)
7188 && !DECL_LOCAL_FUNCTION_P (fn
))
7191 if (!any_type_dependent_arguments_p (args
))
7193 = perform_koenig_lookup (postfix_expression
, args
,
7199 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7201 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7202 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7204 if (processing_template_decl
7205 && (type_dependent_object_expression_p (instance
)
7206 || (!BASELINK_P (fn
)
7207 && TREE_CODE (fn
) != FIELD_DECL
)
7208 || type_dependent_expression_p (fn
)
7209 || any_type_dependent_arguments_p (args
)))
7211 maybe_generic_this_capture (instance
, fn
);
7213 = build_min_nt_call_vec (postfix_expression
, args
);
7214 release_tree_vector (args
);
7218 if (BASELINK_P (fn
))
7221 = (build_new_method_call
7222 (instance
, fn
, &args
, NULL_TREE
,
7223 (idk
== CP_ID_KIND_QUALIFIED
7224 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7231 = finish_call_expr (postfix_expression
, &args
,
7232 /*disallow_virtual=*/false,
7236 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7237 || TREE_CODE (postfix_expression
) == MEMBER_REF
7238 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7239 postfix_expression
= (build_offset_ref_call_from_tree
7240 (postfix_expression
, &args
,
7242 else if (idk
== CP_ID_KIND_QUALIFIED
)
7243 /* A call to a static class member, or a namespace-scope
7246 = finish_call_expr (postfix_expression
, &args
,
7247 /*disallow_virtual=*/true,
7251 /* All other function calls. */
7253 = finish_call_expr (postfix_expression
, &args
,
7254 /*disallow_virtual=*/false,
7258 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7260 location_t combined_loc
= make_location (token
->location
,
7263 postfix_expression
.set_location (combined_loc
);
7266 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7267 idk
= CP_ID_KIND_NONE
;
7269 release_tree_vector (args
);
7275 /* postfix-expression . template [opt] id-expression
7276 postfix-expression . pseudo-destructor-name
7277 postfix-expression -> template [opt] id-expression
7278 postfix-expression -> pseudo-destructor-name */
7280 /* Consume the `.' or `->' operator. */
7281 cp_lexer_consume_token (parser
->lexer
);
7284 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7288 is_member_access
= true;
7292 /* postfix-expression ++ */
7293 /* Consume the `++' token. */
7294 cp_lexer_consume_token (parser
->lexer
);
7295 /* Generate a representation for the complete expression. */
7297 = finish_increment_expr (postfix_expression
,
7298 POSTINCREMENT_EXPR
);
7299 /* Increments may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7301 postfix_expression
= error_mark_node
;
7302 idk
= CP_ID_KIND_NONE
;
7303 is_member_access
= false;
7306 case CPP_MINUS_MINUS
:
7307 /* postfix-expression -- */
7308 /* Consume the `--' token. */
7309 cp_lexer_consume_token (parser
->lexer
);
7310 /* Generate a representation for the complete expression. */
7312 = finish_increment_expr (postfix_expression
,
7313 POSTDECREMENT_EXPR
);
7314 /* Decrements may not appear in constant-expressions. */
7315 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7316 postfix_expression
= error_mark_node
;
7317 idk
= CP_ID_KIND_NONE
;
7318 is_member_access
= false;
7322 if (pidk_return
!= NULL
)
7323 * pidk_return
= idk
;
7324 if (member_access_only_p
)
7325 return is_member_access
7326 ? postfix_expression
7327 : cp_expr (error_mark_node
);
7329 return postfix_expression
;
7333 /* We should never get here. */
7335 return error_mark_node
;
7338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7339 by cp_parser_builtin_offsetof. We're looking for
7341 postfix-expression [ expression ]
7342 postfix-expression [ braced-init-list ] (C++11)
7344 FOR_OFFSETOF is set if we're being called in that context, which
7345 changes how we deal with integer constant expressions. */
7348 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7349 tree postfix_expression
,
7353 tree index
= NULL_TREE
;
7354 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7355 bool saved_greater_than_is_operator_p
;
7357 /* Consume the `[' token. */
7358 cp_lexer_consume_token (parser
->lexer
);
7360 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7361 parser
->greater_than_is_operator_p
= true;
7363 /* Parse the index expression. */
7364 /* ??? For offsetof, there is a question of what to allow here. If
7365 offsetof is not being used in an integral constant expression context,
7366 then we *could* get the right answer by computing the value at runtime.
7367 If we are in an integral constant expression context, then we might
7368 could accept any constant expression; hard to say without analysis.
7369 Rather than open the barn door too wide right away, allow only integer
7370 constant expressions here. */
7372 index
= cp_parser_constant_expression (parser
);
7375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7377 bool expr_nonconst_p
;
7378 cp_lexer_set_source_position (parser
->lexer
);
7379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7380 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7383 index
= cp_parser_expression (parser
);
7386 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7388 /* Look for the closing `]'. */
7389 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7391 /* Build the ARRAY_REF. */
7392 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7395 /* When not doing offsetof, array references are not permitted in
7396 constant-expressions. */
7398 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7399 postfix_expression
= error_mark_node
;
7401 return postfix_expression
;
7404 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7405 dereference of incomplete type, returns true if error_mark_node should
7406 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7407 and *DEPENDENT_P. */
7410 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7413 /* In a template, be permissive by treating an object expression
7414 of incomplete type as dependent (after a pedwarn). */
7415 diagnostic_t kind
= (processing_template_decl
7416 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7418 switch (TREE_CODE (*postfix_expression
))
7421 case REINTERPRET_CAST_EXPR
:
7422 case CONST_CAST_EXPR
:
7423 case STATIC_CAST_EXPR
:
7424 case DYNAMIC_CAST_EXPR
:
7425 case IMPLICIT_CONV_EXPR
:
7426 case VIEW_CONVERT_EXPR
:
7427 case NON_LVALUE_EXPR
:
7431 /* Don't emit any diagnostic for OVERLOADs. */
7435 /* Avoid clobbering e.g. DECLs. */
7436 if (!EXPR_P (*postfix_expression
))
7441 if (kind
== DK_IGNORED
)
7444 location_t exploc
= location_of (*postfix_expression
);
7445 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7446 if (!MAYBE_CLASS_TYPE_P (*scope
))
7448 if (kind
== DK_ERROR
)
7449 *scope
= *postfix_expression
= error_mark_node
;
7450 else if (processing_template_decl
)
7452 *dependent_p
= true;
7453 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7461 postfix-expression . template [opt] id-expression
7462 postfix-expression . pseudo-destructor-name
7463 postfix-expression -> template [opt] id-expression
7464 postfix-expression -> pseudo-destructor-name
7466 FOR_OFFSETOF is set if we're being called in that context. That sorta
7467 limits what of the above we'll actually accept, but nevermind.
7468 TOKEN_TYPE is the "." or "->" token, which will already have been
7469 removed from the stream. */
7472 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7473 enum cpp_ttype token_type
,
7474 cp_expr postfix_expression
,
7475 bool for_offsetof
, cp_id_kind
*idk
,
7476 location_t location
)
7480 bool pseudo_destructor_p
;
7481 tree scope
= NULL_TREE
;
7482 location_t start_loc
= postfix_expression
.get_start ();
7484 /* If this is a `->' operator, dereference the pointer. */
7485 if (token_type
== CPP_DEREF
)
7486 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7487 tf_warning_or_error
);
7488 /* Check to see whether or not the expression is type-dependent and
7489 not the current instantiation. */
7490 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7491 /* The identifier following the `->' or `.' is not qualified. */
7492 parser
->scope
= NULL_TREE
;
7493 parser
->qualifying_scope
= NULL_TREE
;
7494 parser
->object_scope
= NULL_TREE
;
7495 *idk
= CP_ID_KIND_NONE
;
7497 /* Enter the scope corresponding to the type of the object
7498 given by the POSTFIX_EXPRESSION. */
7501 scope
= TREE_TYPE (postfix_expression
);
7502 /* According to the standard, no expression should ever have
7503 reference type. Unfortunately, we do not currently match
7504 the standard in this respect in that our internal representation
7505 of an expression may have reference type even when the standard
7506 says it does not. Therefore, we have to manually obtain the
7507 underlying type here. */
7508 scope
= non_reference (scope
);
7509 /* The type of the POSTFIX_EXPRESSION must be complete. */
7510 /* Unlike the object expression in other contexts, *this is not
7511 required to be of complete type for purposes of class member
7512 access (5.2.5) outside the member function body. */
7513 if (postfix_expression
!= current_class_ref
7514 && scope
!= error_mark_node
7515 && !currently_open_class (scope
))
7517 scope
= complete_type (scope
);
7518 if (!COMPLETE_TYPE_P (scope
)
7519 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7521 return error_mark_node
;
7526 /* Let the name lookup machinery know that we are processing a
7527 class member access expression. */
7528 parser
->context
->object_type
= scope
;
7529 /* If something went wrong, we want to be able to discern that case,
7530 as opposed to the case where there was no SCOPE due to the type
7531 of expression being dependent. */
7533 scope
= error_mark_node
;
7534 /* If the SCOPE was erroneous, make the various semantic analysis
7535 functions exit quickly -- and without issuing additional error
7537 if (scope
== error_mark_node
)
7538 postfix_expression
= error_mark_node
;
7543 /* Tell cp_parser_lookup_name that there was an object, even though it's
7545 parser
->context
->object_type
= unknown_type_node
;
7547 /* Assume this expression is not a pseudo-destructor access. */
7548 pseudo_destructor_p
= false;
7550 /* If the SCOPE is a scalar type, then, if this is a valid program,
7551 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7552 is type dependent, it can be pseudo-destructor-name or something else.
7553 Try to parse it as pseudo-destructor-name first. */
7554 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7559 cp_parser_parse_tentatively (parser
);
7560 /* Parse the pseudo-destructor-name. */
7562 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7565 && (cp_parser_error_occurred (parser
)
7566 || !SCALAR_TYPE_P (type
)))
7567 cp_parser_abort_tentative_parse (parser
);
7568 else if (cp_parser_parse_definitely (parser
))
7570 pseudo_destructor_p
= true;
7572 = finish_pseudo_destructor_expr (postfix_expression
,
7577 if (!pseudo_destructor_p
)
7579 /* If the SCOPE is not a scalar type, we are looking at an
7580 ordinary class member access expression, rather than a
7581 pseudo-destructor-name. */
7583 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7584 /* Parse the id-expression. */
7585 name
= (cp_parser_id_expression
7587 cp_parser_optional_template_keyword (parser
),
7588 /*check_dependency_p=*/true,
7590 /*declarator_p=*/false,
7591 /*optional_p=*/false));
7592 /* In general, build a SCOPE_REF if the member name is qualified.
7593 However, if the name was not dependent and has already been
7594 resolved; there is no need to build the SCOPE_REF. For example;
7596 struct X { void f(); };
7597 template <typename T> void f(T* t) { t->X::f(); }
7599 Even though "t" is dependent, "X::f" is not and has been resolved
7600 to a BASELINK; there is no need to include scope information. */
7602 /* But we do need to remember that there was an explicit scope for
7603 virtual function calls. */
7605 *idk
= CP_ID_KIND_QUALIFIED
;
7607 /* If the name is a template-id that names a type, we will get a
7608 TYPE_DECL here. That is invalid code. */
7609 if (TREE_CODE (name
) == TYPE_DECL
)
7611 error_at (token
->location
, "invalid use of %qD", name
);
7612 postfix_expression
= error_mark_node
;
7616 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7618 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7620 error_at (token
->location
, "%<%D::%D%> is not a class member",
7621 parser
->scope
, name
);
7622 postfix_expression
= error_mark_node
;
7625 name
= build_qualified_name (/*type=*/NULL_TREE
,
7629 parser
->scope
= NULL_TREE
;
7630 parser
->qualifying_scope
= NULL_TREE
;
7631 parser
->object_scope
= NULL_TREE
;
7633 if (parser
->scope
&& name
&& BASELINK_P (name
))
7634 adjust_result_of_qualified_name_lookup
7635 (name
, parser
->scope
, scope
);
7637 = finish_class_member_access_expr (postfix_expression
, name
,
7639 tf_warning_or_error
);
7640 /* Build a location e.g.:
7643 where the caret is at the deref token, ranging from
7644 the start of postfix_expression to the end of the access expr. */
7646 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7647 location_t combined_loc
7648 = make_location (input_location
, start_loc
, end_loc
);
7649 protected_set_expr_location (postfix_expression
, combined_loc
);
7653 /* We no longer need to look up names in the scope of the object on
7654 the left-hand side of the `.' or `->' operator. */
7655 parser
->context
->object_type
= NULL_TREE
;
7657 /* Outside of offsetof, these operators may not appear in
7658 constant-expressions. */
7660 && (cp_parser_non_integral_constant_expression
7661 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7662 postfix_expression
= error_mark_node
;
7664 return postfix_expression
;
7667 /* Parse a parenthesized expression-list.
7670 assignment-expression
7671 expression-list, assignment-expression
7676 identifier, expression-list
7678 CAST_P is true if this expression is the target of a cast.
7680 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7683 WRAP_LOCATIONS_P is true if expressions within this list for which
7684 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7685 their source locations.
7687 Returns a vector of trees. Each element is a representation of an
7688 assignment-expression. NULL is returned if the ( and or ) are
7689 missing. An empty, but allocated, vector is returned on no
7690 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7691 if we are parsing an attribute list for an attribute that wants a
7692 plain identifier argument, normal_attr for an attribute that wants
7693 an expression, or non_attr if we aren't parsing an attribute list. If
7694 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7695 not all of the expressions in the list were constant.
7696 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7697 will be written to with the location of the closing parenthesis. If
7698 an error occurs, it may or may not be written to. */
7700 static vec
<tree
, va_gc
> *
7701 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7702 int is_attribute_list
,
7704 bool allow_expansion_p
,
7705 bool *non_constant_p
,
7706 location_t
*close_paren_loc
,
7707 bool wrap_locations_p
)
7709 vec
<tree
, va_gc
> *expression_list
;
7710 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7711 tree identifier
= NULL_TREE
;
7712 bool saved_greater_than_is_operator_p
;
7714 /* Assume all the expressions will be constant. */
7716 *non_constant_p
= false;
7718 matching_parens parens
;
7719 if (!parens
.require_open (parser
))
7722 expression_list
= make_tree_vector ();
7724 /* Within a parenthesized expression, a `>' token is always
7725 the greater-than operator. */
7726 saved_greater_than_is_operator_p
7727 = parser
->greater_than_is_operator_p
;
7728 parser
->greater_than_is_operator_p
= true;
7730 cp_expr
expr (NULL_TREE
);
7732 /* Consume expressions until there are no more. */
7733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7736 /* At the beginning of attribute lists, check to see if the
7737 next token is an identifier. */
7738 if (is_attribute_list
== id_attr
7739 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7743 /* Consume the identifier. */
7744 token
= cp_lexer_consume_token (parser
->lexer
);
7745 /* Save the identifier. */
7746 identifier
= token
->u
.value
;
7750 bool expr_non_constant_p
;
7752 /* Parse the next assignment-expression. */
7753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7755 /* A braced-init-list. */
7756 cp_lexer_set_source_position (parser
->lexer
);
7757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7758 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7759 if (non_constant_p
&& expr_non_constant_p
)
7760 *non_constant_p
= true;
7762 else if (non_constant_p
)
7764 expr
= (cp_parser_constant_expression
7765 (parser
, /*allow_non_constant_p=*/true,
7766 &expr_non_constant_p
));
7767 if (expr_non_constant_p
)
7768 *non_constant_p
= true;
7771 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7775 expr
= instantiate_non_dependent_expr (expr
);
7777 /* If we have an ellipsis, then this is an expression
7779 if (allow_expansion_p
7780 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7782 /* Consume the `...'. */
7783 cp_lexer_consume_token (parser
->lexer
);
7785 /* Build the argument pack. */
7786 expr
= make_pack_expansion (expr
);
7789 if (wrap_locations_p
)
7790 expr
.maybe_add_location_wrapper ();
7792 /* Add it to the list. We add error_mark_node
7793 expressions to the list, so that we can still tell if
7794 the correct form for a parenthesized expression-list
7795 is found. That gives better errors. */
7796 vec_safe_push (expression_list
, expr
.get_value ());
7798 if (expr
== error_mark_node
)
7802 /* After the first item, attribute lists look the same as
7803 expression lists. */
7804 is_attribute_list
= non_attr
;
7807 /* If the next token isn't a `,', then we are done. */
7808 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7811 /* Otherwise, consume the `,' and keep going. */
7812 cp_lexer_consume_token (parser
->lexer
);
7815 if (close_paren_loc
)
7816 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7818 if (!parens
.require_close (parser
))
7823 /* We try and resync to an unnested comma, as that will give the
7824 user better diagnostics. */
7825 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7826 /*recovering=*/true,
7828 /*consume_paren=*/true);
7833 parser
->greater_than_is_operator_p
7834 = saved_greater_than_is_operator_p
;
7839 parser
->greater_than_is_operator_p
7840 = saved_greater_than_is_operator_p
;
7843 vec_safe_insert (expression_list
, 0, identifier
);
7845 return expression_list
;
7848 /* Parse a pseudo-destructor-name.
7850 pseudo-destructor-name:
7851 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7852 :: [opt] nested-name-specifier template template-id :: ~ type-name
7853 :: [opt] nested-name-specifier [opt] ~ type-name
7855 If either of the first two productions is used, sets *SCOPE to the
7856 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7857 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7858 or ERROR_MARK_NODE if the parse fails. */
7861 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7866 bool nested_name_specifier_p
;
7869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7870 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7871 && !type_dependent_expression_p (object
))
7873 if (cxx_dialect
< cxx14
)
7874 pedwarn (input_location
, 0,
7875 "%<~auto%> only available with "
7876 "-std=c++14 or -std=gnu++14");
7877 cp_lexer_consume_token (parser
->lexer
);
7878 cp_lexer_consume_token (parser
->lexer
);
7880 *type
= TREE_TYPE (object
);
7884 /* Assume that things will not work out. */
7885 *type
= error_mark_node
;
7887 /* Look for the optional `::' operator. */
7888 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7889 /* Look for the optional nested-name-specifier. */
7890 nested_name_specifier_p
7891 = (cp_parser_nested_name_specifier_opt (parser
,
7892 /*typename_keyword_p=*/false,
7893 /*check_dependency_p=*/true,
7895 /*is_declaration=*/false)
7897 /* Now, if we saw a nested-name-specifier, we might be doing the
7898 second production. */
7899 if (nested_name_specifier_p
7900 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7902 /* Consume the `template' keyword. */
7903 cp_lexer_consume_token (parser
->lexer
);
7904 /* Parse the template-id. */
7905 cp_parser_template_id (parser
,
7906 /*template_keyword_p=*/true,
7907 /*check_dependency_p=*/false,
7909 /*is_declaration=*/true);
7910 /* Look for the `::' token. */
7911 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7913 /* If the next token is not a `~', then there might be some
7914 additional qualification. */
7915 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7917 /* At this point, we're looking for "type-name :: ~". The type-name
7918 must not be a class-name, since this is a pseudo-destructor. So,
7919 it must be either an enum-name, or a typedef-name -- both of which
7920 are just identifiers. So, we peek ahead to check that the "::"
7921 and "~" tokens are present; if they are not, then we can avoid
7922 calling type_name. */
7923 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7924 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7925 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7927 cp_parser_error (parser
, "non-scalar type");
7931 /* Look for the type-name. */
7932 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7933 if (*scope
== error_mark_node
)
7936 /* Look for the `::' token. */
7937 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7942 /* Look for the `~'. */
7943 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7945 /* Once we see the ~, this has to be a pseudo-destructor. */
7946 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7947 cp_parser_commit_to_topmost_tentative_parse (parser
);
7949 /* Look for the type-name again. We are not responsible for
7950 checking that it matches the first type-name. */
7951 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7954 /* Parse a unary-expression.
7960 unary-operator cast-expression
7961 sizeof unary-expression
7963 alignof ( type-id ) [C++0x]
7970 __extension__ cast-expression
7971 __alignof__ unary-expression
7972 __alignof__ ( type-id )
7973 alignof unary-expression [C++0x]
7974 __real__ cast-expression
7975 __imag__ cast-expression
7977 sizeof ( type-id ) { initializer-list , [opt] }
7978 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7979 __alignof__ ( type-id ) { initializer-list , [opt] }
7981 ADDRESS_P is true iff the unary-expression is appearing as the
7982 operand of the `&' operator. CAST_P is true if this expression is
7983 the target of a cast.
7985 Returns a representation of the expression. */
7988 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7989 bool address_p
, bool cast_p
, bool decltype_p
)
7992 enum tree_code unary_operator
;
7994 /* Peek at the next token. */
7995 token
= cp_lexer_peek_token (parser
->lexer
);
7996 /* Some keywords give away the kind of expression. */
7997 if (token
->type
== CPP_KEYWORD
)
7999 enum rid keyword
= token
->keyword
;
8008 location_t start_loc
= token
->location
;
8010 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8011 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8013 /* Consume the token. */
8014 cp_lexer_consume_token (parser
->lexer
);
8015 /* Parse the operand. */
8016 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8018 if (TYPE_P (operand
))
8019 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8023 /* ISO C++ defines alignof only with types, not with
8024 expressions. So pedwarn if alignof is used with a non-
8025 type expression. However, __alignof__ is ok. */
8027 pedwarn (token
->location
, OPT_Wpedantic
,
8028 "ISO C++ does not allow %<alignof%> "
8031 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8033 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8034 SIZEOF_EXPR with the original operand. */
8035 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8037 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8039 if (!processing_template_decl
&& TYPE_P (operand
))
8041 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8042 build1 (NOP_EXPR
, operand
,
8044 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8047 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8048 TREE_SIDE_EFFECTS (ret
) = 0;
8049 TREE_READONLY (ret
) = 1;
8053 /* Construct a location e.g. :
8056 with start == caret at the start of the "alignof"/"sizeof"
8057 token, with the endpoint at the final closing paren. */
8058 location_t finish_loc
8059 = cp_lexer_previous_token (parser
->lexer
)->location
;
8060 location_t compound_loc
8061 = make_location (start_loc
, start_loc
, finish_loc
);
8063 cp_expr
ret_expr (ret
);
8064 ret_expr
.set_location (compound_loc
);
8065 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8070 return cp_parser_new_expression (parser
);
8073 return cp_parser_delete_expression (parser
);
8077 /* The saved value of the PEDANTIC flag. */
8081 /* Save away the PEDANTIC flag. */
8082 cp_parser_extension_opt (parser
, &saved_pedantic
);
8083 /* Parse the cast-expression. */
8084 expr
= cp_parser_simple_cast_expression (parser
);
8085 /* Restore the PEDANTIC flag. */
8086 pedantic
= saved_pedantic
;
8096 /* Consume the `__real__' or `__imag__' token. */
8097 cp_lexer_consume_token (parser
->lexer
);
8098 /* Parse the cast-expression. */
8099 expression
= cp_parser_simple_cast_expression (parser
);
8100 /* Create the complete representation. */
8101 return build_x_unary_op (token
->location
,
8102 (keyword
== RID_REALPART
8103 ? REALPART_EXPR
: IMAGPART_EXPR
),
8105 tf_warning_or_error
);
8109 case RID_TRANSACTION_ATOMIC
:
8110 case RID_TRANSACTION_RELAXED
:
8111 return cp_parser_transaction_expression (parser
, keyword
);
8116 const char *saved_message
;
8117 bool saved_integral_constant_expression_p
;
8118 bool saved_non_integral_constant_expression_p
;
8119 bool saved_greater_than_is_operator_p
;
8121 location_t start_loc
= token
->location
;
8123 cp_lexer_consume_token (parser
->lexer
);
8124 matching_parens parens
;
8125 parens
.require_open (parser
);
8127 saved_message
= parser
->type_definition_forbidden_message
;
8128 parser
->type_definition_forbidden_message
8129 = G_("types may not be defined in %<noexcept%> expressions");
8131 saved_integral_constant_expression_p
8132 = parser
->integral_constant_expression_p
;
8133 saved_non_integral_constant_expression_p
8134 = parser
->non_integral_constant_expression_p
;
8135 parser
->integral_constant_expression_p
= false;
8137 saved_greater_than_is_operator_p
8138 = parser
->greater_than_is_operator_p
;
8139 parser
->greater_than_is_operator_p
= true;
8141 ++cp_unevaluated_operand
;
8142 ++c_inhibit_evaluation_warnings
;
8143 ++cp_noexcept_operand
;
8144 expr
= cp_parser_expression (parser
);
8145 --cp_noexcept_operand
;
8146 --c_inhibit_evaluation_warnings
;
8147 --cp_unevaluated_operand
;
8149 parser
->greater_than_is_operator_p
8150 = saved_greater_than_is_operator_p
;
8152 parser
->integral_constant_expression_p
8153 = saved_integral_constant_expression_p
;
8154 parser
->non_integral_constant_expression_p
8155 = saved_non_integral_constant_expression_p
;
8157 parser
->type_definition_forbidden_message
= saved_message
;
8159 location_t finish_loc
8160 = cp_lexer_peek_token (parser
->lexer
)->location
;
8161 parens
.require_close (parser
);
8163 /* Construct a location of the form:
8166 with start == caret, finishing at the close-paren. */
8167 location_t noexcept_loc
8168 = make_location (start_loc
, start_loc
, finish_loc
);
8170 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8179 /* Look for the `:: new' and `:: delete', which also signal the
8180 beginning of a new-expression, or delete-expression,
8181 respectively. If the next token is `::', then it might be one of
8183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8187 /* See if the token after the `::' is one of the keywords in
8188 which we're interested. */
8189 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8190 /* If it's `new', we have a new-expression. */
8191 if (keyword
== RID_NEW
)
8192 return cp_parser_new_expression (parser
);
8193 /* Similarly, for `delete'. */
8194 else if (keyword
== RID_DELETE
)
8195 return cp_parser_delete_expression (parser
);
8198 /* Look for a unary operator. */
8199 unary_operator
= cp_parser_unary_operator (token
);
8200 /* The `++' and `--' operators can be handled similarly, even though
8201 they are not technically unary-operators in the grammar. */
8202 if (unary_operator
== ERROR_MARK
)
8204 if (token
->type
== CPP_PLUS_PLUS
)
8205 unary_operator
= PREINCREMENT_EXPR
;
8206 else if (token
->type
== CPP_MINUS_MINUS
)
8207 unary_operator
= PREDECREMENT_EXPR
;
8208 /* Handle the GNU address-of-label extension. */
8209 else if (cp_parser_allow_gnu_extensions_p (parser
)
8210 && token
->type
== CPP_AND_AND
)
8214 location_t start_loc
= token
->location
;
8216 /* Consume the '&&' token. */
8217 cp_lexer_consume_token (parser
->lexer
);
8218 /* Look for the identifier. */
8219 location_t finish_loc
8220 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8221 identifier
= cp_parser_identifier (parser
);
8222 /* Construct a location of the form:
8225 with caret==start at the "&&", finish at the end of the label. */
8226 location_t combined_loc
8227 = make_location (start_loc
, start_loc
, finish_loc
);
8228 /* Create an expression representing the address. */
8229 expression
= finish_label_address_expr (identifier
, combined_loc
);
8230 if (cp_parser_non_integral_constant_expression (parser
,
8232 expression
= error_mark_node
;
8236 if (unary_operator
!= ERROR_MARK
)
8238 cp_expr cast_expression
;
8239 cp_expr expression
= error_mark_node
;
8240 non_integral_constant non_constant_p
= NIC_NONE
;
8241 location_t loc
= token
->location
;
8242 tsubst_flags_t complain
= complain_flags (decltype_p
);
8244 /* Consume the operator token. */
8245 token
= cp_lexer_consume_token (parser
->lexer
);
8246 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8248 /* Parse the cast-expression. */
8250 = cp_parser_cast_expression (parser
,
8251 unary_operator
== ADDR_EXPR
,
8257 OP_TOKEN CAST_EXPRESSION
8258 ^~~~~~~~~~~~~~~~~~~~~~~~~
8259 with start==caret at the operator token, and
8260 extending to the end of the cast_expression. */
8261 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8263 /* Now, build an appropriate representation. */
8264 switch (unary_operator
)
8267 non_constant_p
= NIC_STAR
;
8268 expression
= build_x_indirect_ref (loc
, cast_expression
,
8271 /* TODO: build_x_indirect_ref does not always honor the
8272 location, so ensure it is set. */
8273 expression
.set_location (loc
);
8277 non_constant_p
= NIC_ADDR
;
8280 expression
= build_x_unary_op (loc
, unary_operator
,
8283 /* TODO: build_x_unary_op does not always honor the location,
8284 so ensure it is set. */
8285 expression
.set_location (loc
);
8288 case PREINCREMENT_EXPR
:
8289 case PREDECREMENT_EXPR
:
8290 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8291 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8294 /* Immediately fold negation of a constant, unless the constant is 0
8295 (since -0 == 0) or it would overflow. */
8296 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8297 && CONSTANT_CLASS_P (cast_expression
)
8298 && !integer_zerop (cast_expression
)
8299 && !TREE_OVERFLOW (cast_expression
))
8301 tree folded
= fold_build1 (unary_operator
,
8302 TREE_TYPE (cast_expression
),
8304 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8306 expression
= cp_expr (folded
, loc
);
8311 case UNARY_PLUS_EXPR
:
8312 case TRUTH_NOT_EXPR
:
8313 expression
= finish_unary_op_expr (loc
, unary_operator
,
8314 cast_expression
, complain
);
8321 if (non_constant_p
!= NIC_NONE
8322 && cp_parser_non_integral_constant_expression (parser
,
8324 expression
= error_mark_node
;
8329 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8330 /*member_access_only_p=*/false,
8335 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8336 unary-operator, the corresponding tree code is returned. */
8338 static enum tree_code
8339 cp_parser_unary_operator (cp_token
* token
)
8341 switch (token
->type
)
8344 return INDIRECT_REF
;
8350 return UNARY_PLUS_EXPR
;
8356 return TRUTH_NOT_EXPR
;
8359 return BIT_NOT_EXPR
;
8366 /* Parse a new-expression.
8369 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8370 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8372 Returns a representation of the expression. */
8375 cp_parser_new_expression (cp_parser
* parser
)
8377 bool global_scope_p
;
8378 vec
<tree
, va_gc
> *placement
;
8380 vec
<tree
, va_gc
> *initializer
;
8381 tree nelts
= NULL_TREE
;
8384 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8386 /* Look for the optional `::' operator. */
8388 = (cp_parser_global_scope_opt (parser
,
8389 /*current_scope_valid_p=*/false)
8391 /* Look for the `new' operator. */
8392 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8393 /* There's no easy way to tell a new-placement from the
8394 `( type-id )' construct. */
8395 cp_parser_parse_tentatively (parser
);
8396 /* Look for a new-placement. */
8397 placement
= cp_parser_new_placement (parser
);
8398 /* If that didn't work out, there's no new-placement. */
8399 if (!cp_parser_parse_definitely (parser
))
8401 if (placement
!= NULL
)
8402 release_tree_vector (placement
);
8406 /* If the next token is a `(', then we have a parenthesized
8408 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8411 const char *saved_message
= parser
->type_definition_forbidden_message
;
8413 /* Consume the `('. */
8414 matching_parens parens
;
8415 parens
.consume_open (parser
);
8417 /* Parse the type-id. */
8418 parser
->type_definition_forbidden_message
8419 = G_("types may not be defined in a new-expression");
8421 type_id_in_expr_sentinel
s (parser
);
8422 type
= cp_parser_type_id (parser
);
8424 parser
->type_definition_forbidden_message
= saved_message
;
8426 /* Look for the closing `)'. */
8427 parens
.require_close (parser
);
8428 token
= cp_lexer_peek_token (parser
->lexer
);
8429 /* There should not be a direct-new-declarator in this production,
8430 but GCC used to allowed this, so we check and emit a sensible error
8431 message for this case. */
8432 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8434 error_at (token
->location
,
8435 "array bound forbidden after parenthesized type-id");
8436 inform (token
->location
,
8437 "try removing the parentheses around the type-id");
8438 cp_parser_direct_new_declarator (parser
);
8441 /* Otherwise, there must be a new-type-id. */
8443 type
= cp_parser_new_type_id (parser
, &nelts
);
8445 /* If the next token is a `(' or '{', then we have a new-initializer. */
8446 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8447 if (token
->type
== CPP_OPEN_PAREN
8448 || token
->type
== CPP_OPEN_BRACE
)
8449 initializer
= cp_parser_new_initializer (parser
);
8453 /* A new-expression may not appear in an integral constant
8455 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8456 ret
= error_mark_node
;
8457 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8458 of a new-type-id or type-id of a new-expression, the new-expression shall
8459 contain a new-initializer of the form ( assignment-expression )".
8460 Additionally, consistently with the spirit of DR 1467, we want to accept
8461 'new auto { 2 }' too. */
8462 else if ((ret
= type_uses_auto (type
))
8463 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8464 && (vec_safe_length (initializer
) != 1
8465 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8466 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8468 error_at (token
->location
,
8469 "initialization of new-expression for type %<auto%> "
8470 "requires exactly one element");
8471 ret
= error_mark_node
;
8475 /* Construct a location e.g.:
8478 with caret == start at the start of the "new" token, and the end
8479 at the end of the final token we consumed. */
8480 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8481 location_t end_loc
= get_finish (end_tok
->location
);
8482 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8484 /* Create a representation of the new-expression. */
8485 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8486 tf_warning_or_error
);
8487 protected_set_expr_location (ret
, combined_loc
);
8490 if (placement
!= NULL
)
8491 release_tree_vector (placement
);
8492 if (initializer
!= NULL
)
8493 release_tree_vector (initializer
);
8498 /* Parse a new-placement.
8503 Returns the same representation as for an expression-list. */
8505 static vec
<tree
, va_gc
> *
8506 cp_parser_new_placement (cp_parser
* parser
)
8508 vec
<tree
, va_gc
> *expression_list
;
8510 /* Parse the expression-list. */
8511 expression_list
= (cp_parser_parenthesized_expression_list
8512 (parser
, non_attr
, /*cast_p=*/false,
8513 /*allow_expansion_p=*/true,
8514 /*non_constant_p=*/NULL
));
8516 if (expression_list
&& expression_list
->is_empty ())
8517 error ("expected expression-list or type-id");
8519 return expression_list
;
8522 /* Parse a new-type-id.
8525 type-specifier-seq new-declarator [opt]
8527 Returns the TYPE allocated. If the new-type-id indicates an array
8528 type, *NELTS is set to the number of elements in the last array
8529 bound; the TYPE will not include the last array bound. */
8532 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8534 cp_decl_specifier_seq type_specifier_seq
;
8535 cp_declarator
*new_declarator
;
8536 cp_declarator
*declarator
;
8537 cp_declarator
*outer_declarator
;
8538 const char *saved_message
;
8540 /* The type-specifier sequence must not contain type definitions.
8541 (It cannot contain declarations of new types either, but if they
8542 are not definitions we will catch that because they are not
8544 saved_message
= parser
->type_definition_forbidden_message
;
8545 parser
->type_definition_forbidden_message
8546 = G_("types may not be defined in a new-type-id");
8547 /* Parse the type-specifier-seq. */
8548 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8549 /*is_trailing_return=*/false,
8550 &type_specifier_seq
);
8551 /* Restore the old message. */
8552 parser
->type_definition_forbidden_message
= saved_message
;
8554 if (type_specifier_seq
.type
== error_mark_node
)
8555 return error_mark_node
;
8557 /* Parse the new-declarator. */
8558 new_declarator
= cp_parser_new_declarator_opt (parser
);
8560 /* Determine the number of elements in the last array dimension, if
8563 /* Skip down to the last array dimension. */
8564 declarator
= new_declarator
;
8565 outer_declarator
= NULL
;
8566 while (declarator
&& (declarator
->kind
== cdk_pointer
8567 || declarator
->kind
== cdk_ptrmem
))
8569 outer_declarator
= declarator
;
8570 declarator
= declarator
->declarator
;
8573 && declarator
->kind
== cdk_array
8574 && declarator
->declarator
8575 && declarator
->declarator
->kind
== cdk_array
)
8577 outer_declarator
= declarator
;
8578 declarator
= declarator
->declarator
;
8581 if (declarator
&& declarator
->kind
== cdk_array
)
8583 *nelts
= declarator
->u
.array
.bounds
;
8584 if (*nelts
== error_mark_node
)
8585 *nelts
= integer_one_node
;
8587 if (outer_declarator
)
8588 outer_declarator
->declarator
= declarator
->declarator
;
8590 new_declarator
= NULL
;
8593 return groktypename (&type_specifier_seq
, new_declarator
, false);
8596 /* Parse an (optional) new-declarator.
8599 ptr-operator new-declarator [opt]
8600 direct-new-declarator
8602 Returns the declarator. */
8604 static cp_declarator
*
8605 cp_parser_new_declarator_opt (cp_parser
* parser
)
8607 enum tree_code code
;
8608 tree type
, std_attributes
= NULL_TREE
;
8609 cp_cv_quals cv_quals
;
8611 /* We don't know if there's a ptr-operator next, or not. */
8612 cp_parser_parse_tentatively (parser
);
8613 /* Look for a ptr-operator. */
8614 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8615 /* If that worked, look for more new-declarators. */
8616 if (cp_parser_parse_definitely (parser
))
8618 cp_declarator
*declarator
;
8620 /* Parse another optional declarator. */
8621 declarator
= cp_parser_new_declarator_opt (parser
);
8623 declarator
= cp_parser_make_indirect_declarator
8624 (code
, type
, cv_quals
, declarator
, std_attributes
);
8629 /* If the next token is a `[', there is a direct-new-declarator. */
8630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8631 return cp_parser_direct_new_declarator (parser
);
8636 /* Parse a direct-new-declarator.
8638 direct-new-declarator:
8640 direct-new-declarator [constant-expression]
8644 static cp_declarator
*
8645 cp_parser_direct_new_declarator (cp_parser
* parser
)
8647 cp_declarator
*declarator
= NULL
;
8654 /* Look for the opening `['. */
8655 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8657 token
= cp_lexer_peek_token (parser
->lexer
);
8658 expression
= cp_parser_expression (parser
);
8659 /* The standard requires that the expression have integral
8660 type. DR 74 adds enumeration types. We believe that the
8661 real intent is that these expressions be handled like the
8662 expression in a `switch' condition, which also allows
8663 classes with a single conversion to integral or
8664 enumeration type. */
8665 if (!processing_template_decl
)
8668 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8673 error_at (token
->location
,
8674 "expression in new-declarator must have integral "
8675 "or enumeration type");
8676 expression
= error_mark_node
;
8680 /* Look for the closing `]'. */
8681 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8683 /* Add this bound to the declarator. */
8684 declarator
= make_array_declarator (declarator
, expression
);
8686 /* If the next token is not a `[', then there are no more
8688 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8695 /* Parse a new-initializer.
8698 ( expression-list [opt] )
8701 Returns a representation of the expression-list. */
8703 static vec
<tree
, va_gc
> *
8704 cp_parser_new_initializer (cp_parser
* parser
)
8706 vec
<tree
, va_gc
> *expression_list
;
8708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8711 bool expr_non_constant_p
;
8712 cp_lexer_set_source_position (parser
->lexer
);
8713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8714 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8715 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8716 expression_list
= make_tree_vector_single (t
);
8719 expression_list
= (cp_parser_parenthesized_expression_list
8720 (parser
, non_attr
, /*cast_p=*/false,
8721 /*allow_expansion_p=*/true,
8722 /*non_constant_p=*/NULL
));
8724 return expression_list
;
8727 /* Parse a delete-expression.
8730 :: [opt] delete cast-expression
8731 :: [opt] delete [ ] cast-expression
8733 Returns a representation of the expression. */
8736 cp_parser_delete_expression (cp_parser
* parser
)
8738 bool global_scope_p
;
8742 /* Look for the optional `::' operator. */
8744 = (cp_parser_global_scope_opt (parser
,
8745 /*current_scope_valid_p=*/false)
8747 /* Look for the `delete' keyword. */
8748 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8749 /* See if the array syntax is in use. */
8750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8752 /* Consume the `[' token. */
8753 cp_lexer_consume_token (parser
->lexer
);
8754 /* Look for the `]' token. */
8755 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8756 /* Remember that this is the `[]' construct. */
8762 /* Parse the cast-expression. */
8763 expression
= cp_parser_simple_cast_expression (parser
);
8765 /* A delete-expression may not appear in an integral constant
8767 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8768 return error_mark_node
;
8770 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8771 tf_warning_or_error
);
8774 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8775 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8779 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8781 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8782 switch (token
->type
)
8788 case CPP_CLOSE_SQUARE
:
8789 case CPP_CLOSE_PAREN
:
8790 case CPP_CLOSE_BRACE
:
8791 case CPP_OPEN_BRACE
:
8795 case CPP_DEREF_STAR
:
8803 case CPP_GREATER_EQ
:
8824 case CPP_OPEN_PAREN
:
8825 /* In ((type ()) () the last () isn't a valid cast-expression,
8826 so the whole must be parsed as postfix-expression. */
8827 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8830 case CPP_OPEN_SQUARE
:
8831 /* '[' may start a primary-expression in obj-c++ and in C++11,
8832 as a lambda-expression, eg, '(void)[]{}'. */
8833 if (cxx_dialect
>= cxx11
)
8835 return c_dialect_objc ();
8838 case CPP_MINUS_MINUS
:
8839 /* '++' and '--' may or may not start a cast-expression:
8841 struct T { void operator++(int); };
8842 void f() { (T())++; }
8855 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8856 in the order: const_cast, static_cast, reinterpret_cast.
8858 Don't suggest dynamic_cast.
8860 Return the first legal cast kind found, or NULL otherwise. */
8863 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8867 /* Reuse the parser logic by attempting to build the various kinds of
8868 cast, with "complain" disabled.
8869 Identify the first such cast that is valid. */
8871 /* Don't attempt to run such logic within template processing. */
8872 if (processing_template_decl
)
8875 /* First try const_cast. */
8876 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8877 if (trial
!= error_mark_node
)
8878 return "const_cast";
8880 /* If that fails, try static_cast. */
8881 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8882 if (trial
!= error_mark_node
)
8883 return "static_cast";
8885 /* Finally, try reinterpret_cast. */
8886 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8887 if (trial
!= error_mark_node
)
8888 return "reinterpret_cast";
8890 /* No such cast possible. */
8894 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8895 suggesting how to convert a C-style cast of the form:
8899 to a C++-style cast.
8901 The primary range of RICHLOC is asssumed to be that of the original
8902 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8903 of the parens in the C-style cast. */
8906 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8907 location_t close_paren_loc
, tree orig_expr
,
8910 /* This function is non-trivial, so bail out now if the warning isn't
8911 going to be emitted. */
8912 if (!warn_old_style_cast
)
8915 /* Try to find a legal C++ cast, trying them in order:
8916 const_cast, static_cast, reinterpret_cast. */
8917 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8918 if (!cast_suggestion
)
8921 /* Replace the open paren with "CAST_SUGGESTION<". */
8923 pp_printf (&pp
, "%s<", cast_suggestion
);
8924 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8926 /* Replace the close paren with "> (". */
8927 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8929 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8930 rich_loc
->add_fixit_insert_after (")");
8934 /* Parse a cast-expression.
8938 ( type-id ) cast-expression
8940 ADDRESS_P is true iff the unary-expression is appearing as the
8941 operand of the `&' operator. CAST_P is true if this expression is
8942 the target of a cast.
8944 Returns a representation of the expression. */
8947 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8948 bool decltype_p
, cp_id_kind
* pidk
)
8950 /* If it's a `(', then we might be looking at a cast. */
8951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8953 tree type
= NULL_TREE
;
8954 cp_expr
expr (NULL_TREE
);
8955 int cast_expression
= 0;
8956 const char *saved_message
;
8958 /* There's no way to know yet whether or not this is a cast.
8959 For example, `(int (3))' is a unary-expression, while `(int)
8960 3' is a cast. So, we resort to parsing tentatively. */
8961 cp_parser_parse_tentatively (parser
);
8962 /* Types may not be defined in a cast. */
8963 saved_message
= parser
->type_definition_forbidden_message
;
8964 parser
->type_definition_forbidden_message
8965 = G_("types may not be defined in casts");
8966 /* Consume the `('. */
8967 matching_parens parens
;
8968 cp_token
*open_paren
= parens
.consume_open (parser
);
8969 location_t open_paren_loc
= open_paren
->location
;
8970 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8972 /* A very tricky bit is that `(struct S) { 3 }' is a
8973 compound-literal (which we permit in C++ as an extension).
8974 But, that construct is not a cast-expression -- it is a
8975 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8976 is legal; if the compound-literal were a cast-expression,
8977 you'd need an extra set of parentheses.) But, if we parse
8978 the type-id, and it happens to be a class-specifier, then we
8979 will commit to the parse at that point, because we cannot
8980 undo the action that is done when creating a new class. So,
8981 then we cannot back up and do a postfix-expression.
8983 Another tricky case is the following (c++/29234):
8985 struct S { void operator () (); };
8992 As a type-id we parse the parenthesized S()() as a function
8993 returning a function, groktypename complains and we cannot
8994 back up in this case either.
8996 Therefore, we scan ahead to the closing `)', and check to see
8997 if the tokens after the `)' can start a cast-expression. Otherwise
8998 we are dealing with an unary-expression, a postfix-expression
9001 Yet another tricky case, in C++11, is the following (c++/54891):
9005 The issue is that usually, besides the case of lambda-expressions,
9006 the parenthesized type-id cannot be followed by '[', and, eg, we
9007 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9008 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9009 we don't commit, we try a cast-expression, then an unary-expression.
9011 Save tokens so that we can put them back. */
9012 cp_lexer_save_tokens (parser
->lexer
);
9014 /* We may be looking at a cast-expression. */
9015 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9016 /*consume_paren=*/true))
9018 = cp_parser_tokens_start_cast_expression (parser
);
9020 /* Roll back the tokens we skipped. */
9021 cp_lexer_rollback_tokens (parser
->lexer
);
9022 /* If we aren't looking at a cast-expression, simulate an error so
9023 that the call to cp_parser_error_occurred below returns true. */
9024 if (!cast_expression
)
9025 cp_parser_simulate_error (parser
);
9028 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9029 parser
->in_type_id_in_expr_p
= true;
9030 /* Look for the type-id. */
9031 type
= cp_parser_type_id (parser
);
9032 /* Look for the closing `)'. */
9033 cp_token
*close_paren
= parens
.require_close (parser
);
9035 close_paren_loc
= close_paren
->location
;
9036 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9039 /* Restore the saved message. */
9040 parser
->type_definition_forbidden_message
= saved_message
;
9042 /* At this point this can only be either a cast or a
9043 parenthesized ctor such as `(T ())' that looks like a cast to
9044 function returning T. */
9045 if (!cp_parser_error_occurred (parser
))
9047 /* Only commit if the cast-expression doesn't start with
9048 '++', '--', or '[' in C++11. */
9049 if (cast_expression
> 0)
9050 cp_parser_commit_to_topmost_tentative_parse (parser
);
9052 expr
= cp_parser_cast_expression (parser
,
9053 /*address_p=*/false,
9055 /*decltype_p=*/false,
9058 if (cp_parser_parse_definitely (parser
))
9060 /* Warn about old-style casts, if so requested. */
9061 if (warn_old_style_cast
9062 && !in_system_header_at (input_location
)
9063 && !VOID_TYPE_P (type
)
9064 && current_lang_name
!= lang_name_c
)
9066 gcc_rich_location
rich_loc (input_location
);
9067 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9069 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9070 "use of old-style cast to %q#T", type
);
9073 /* Only type conversions to integral or enumeration types
9074 can be used in constant-expressions. */
9075 if (!cast_valid_in_integral_constant_expression_p (type
)
9076 && cp_parser_non_integral_constant_expression (parser
,
9078 return error_mark_node
;
9080 /* Perform the cast. */
9084 with start==caret at the open paren, extending to the
9086 location_t cast_loc
= make_location (open_paren_loc
,
9088 expr
.get_finish ());
9089 expr
= build_c_cast (cast_loc
, type
, expr
);
9094 cp_parser_abort_tentative_parse (parser
);
9097 /* If we get here, then it's not a cast, so it must be a
9098 unary-expression. */
9099 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9100 cast_p
, decltype_p
);
9103 /* Parse a binary expression of the general form:
9107 pm-expression .* cast-expression
9108 pm-expression ->* cast-expression
9110 multiplicative-expression:
9112 multiplicative-expression * pm-expression
9113 multiplicative-expression / pm-expression
9114 multiplicative-expression % pm-expression
9116 additive-expression:
9117 multiplicative-expression
9118 additive-expression + multiplicative-expression
9119 additive-expression - multiplicative-expression
9123 shift-expression << additive-expression
9124 shift-expression >> additive-expression
9126 relational-expression:
9128 relational-expression < shift-expression
9129 relational-expression > shift-expression
9130 relational-expression <= shift-expression
9131 relational-expression >= shift-expression
9135 relational-expression:
9136 relational-expression <? shift-expression
9137 relational-expression >? shift-expression
9139 equality-expression:
9140 relational-expression
9141 equality-expression == relational-expression
9142 equality-expression != relational-expression
9146 and-expression & equality-expression
9148 exclusive-or-expression:
9150 exclusive-or-expression ^ and-expression
9152 inclusive-or-expression:
9153 exclusive-or-expression
9154 inclusive-or-expression | exclusive-or-expression
9156 logical-and-expression:
9157 inclusive-or-expression
9158 logical-and-expression && inclusive-or-expression
9160 logical-or-expression:
9161 logical-and-expression
9162 logical-or-expression || logical-and-expression
9164 All these are implemented with a single function like:
9167 simple-cast-expression
9168 binary-expression <token> binary-expression
9170 CAST_P is true if this expression is the target of a cast.
9172 The binops_by_token map is used to get the tree codes for each <token> type.
9173 binary-expressions are associated according to a precedence table. */
9175 #define TOKEN_PRECEDENCE(token) \
9176 (((token->type == CPP_GREATER \
9177 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9178 && !parser->greater_than_is_operator_p) \
9179 ? PREC_NOT_OPERATOR \
9180 : binops_by_token[token->type].prec)
9183 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9184 bool no_toplevel_fold_p
,
9186 enum cp_parser_prec prec
,
9189 cp_parser_expression_stack stack
;
9190 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9191 cp_parser_expression_stack_entry current
;
9194 enum tree_code rhs_type
;
9195 enum cp_parser_prec new_prec
, lookahead_prec
;
9198 /* Parse the first expression. */
9199 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9200 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9201 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9202 cast_p
, decltype_p
, pidk
);
9203 current
.prec
= prec
;
9205 if (cp_parser_error_occurred (parser
))
9206 return error_mark_node
;
9210 /* Get an operator token. */
9211 token
= cp_lexer_peek_token (parser
->lexer
);
9213 if (warn_cxx11_compat
9214 && token
->type
== CPP_RSHIFT
9215 && !parser
->greater_than_is_operator_p
)
9217 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9218 "%<>>%> operator is treated"
9219 " as two right angle brackets in C++11"))
9220 inform (token
->location
,
9221 "suggest parentheses around %<>>%> expression");
9224 new_prec
= TOKEN_PRECEDENCE (token
);
9225 if (new_prec
!= PREC_NOT_OPERATOR
9226 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9227 /* This is a fold-expression; handle it later. */
9228 new_prec
= PREC_NOT_OPERATOR
;
9230 /* Popping an entry off the stack means we completed a subexpression:
9231 - either we found a token which is not an operator (`>' where it is not
9232 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9233 will happen repeatedly;
9234 - or, we found an operator which has lower priority. This is the case
9235 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9237 if (new_prec
<= current
.prec
)
9246 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9247 current
.loc
= token
->location
;
9249 /* We used the operator token. */
9250 cp_lexer_consume_token (parser
->lexer
);
9252 /* For "false && x" or "true || x", x will never be executed;
9253 disable warnings while evaluating it. */
9254 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9255 c_inhibit_evaluation_warnings
+=
9256 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9257 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9258 c_inhibit_evaluation_warnings
+=
9259 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9261 /* Extract another operand. It may be the RHS of this expression
9262 or the LHS of a new, higher priority expression. */
9263 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9264 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9265 rhs
= cp_parser_simple_cast_expression (parser
);
9267 /* Get another operator token. Look up its precedence to avoid
9268 building a useless (immediately popped) stack entry for common
9269 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9270 token
= cp_lexer_peek_token (parser
->lexer
);
9271 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9272 if (lookahead_prec
!= PREC_NOT_OPERATOR
9273 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9274 lookahead_prec
= PREC_NOT_OPERATOR
;
9275 if (lookahead_prec
> new_prec
)
9277 /* ... and prepare to parse the RHS of the new, higher priority
9278 expression. Since precedence levels on the stack are
9279 monotonically increasing, we do not have to care about
9284 current
.lhs_type
= rhs_type
;
9285 current
.prec
= new_prec
;
9286 new_prec
= lookahead_prec
;
9290 lookahead_prec
= new_prec
;
9291 /* If the stack is not empty, we have parsed into LHS the right side
9292 (`4' in the example above) of an expression we had suspended.
9293 We can use the information on the stack to recover the LHS (`3')
9294 from the stack together with the tree code (`MULT_EXPR'), and
9295 the precedence of the higher level subexpression
9296 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9297 which will be used to actually build the additive expression. */
9299 rhs_type
= current
.lhs_type
;
9304 /* Undo the disabling of warnings done above. */
9305 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9306 c_inhibit_evaluation_warnings
-=
9307 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9308 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9309 c_inhibit_evaluation_warnings
-=
9310 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9312 if (warn_logical_not_paren
9313 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9314 && current
.lhs_type
== TRUTH_NOT_EXPR
9315 /* Avoid warning for !!x == y. */
9316 && (TREE_CODE (current
.lhs
) != NE_EXPR
9317 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9318 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9319 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9320 /* Avoid warning for !b == y where b is boolean. */
9321 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9322 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9324 /* Avoid warning for !!b == y where b is boolean. */
9325 && (!DECL_P (current
.lhs
)
9326 || TREE_TYPE (current
.lhs
) == NULL_TREE
9327 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9328 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9329 current
.lhs
, maybe_constant_value (rhs
));
9333 location_t combined_loc
= make_location (current
.loc
,
9334 current
.lhs
.get_start (),
9337 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9338 ERROR_MARK for everything that is not a binary expression.
9339 This makes warn_about_parentheses miss some warnings that
9340 involve unary operators. For unary expressions we should
9341 pass the correct tree_code unless the unary expression was
9342 surrounded by parentheses.
9344 if (no_toplevel_fold_p
9345 && lookahead_prec
<= current
.prec
9348 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9349 current
.lhs
= error_mark_node
;
9353 = build_min (current
.tree_type
,
9354 TREE_CODE_CLASS (current
.tree_type
)
9356 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9357 current
.lhs
.get_value (), rhs
.get_value ());
9358 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9363 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9364 current
.lhs
, current
.lhs_type
,
9365 rhs
, rhs_type
, &overload
,
9366 complain_flags (decltype_p
));
9367 /* TODO: build_x_binary_op doesn't always honor the location. */
9368 current
.lhs
.set_location (combined_loc
);
9370 current
.lhs_type
= current
.tree_type
;
9372 /* If the binary operator required the use of an overloaded operator,
9373 then this expression cannot be an integral constant-expression.
9374 An overloaded operator can be used even if both operands are
9375 otherwise permissible in an integral constant-expression if at
9376 least one of the operands is of enumeration type. */
9379 && cp_parser_non_integral_constant_expression (parser
,
9381 return error_mark_node
;
9388 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9389 bool no_toplevel_fold_p
,
9390 enum cp_parser_prec prec
,
9393 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9394 /*decltype*/false, prec
, pidk
);
9397 /* Parse the `? expression : assignment-expression' part of a
9398 conditional-expression. The LOGICAL_OR_EXPR is the
9399 logical-or-expression that started the conditional-expression.
9400 Returns a representation of the entire conditional-expression.
9402 This routine is used by cp_parser_assignment_expression.
9404 ? expression : assignment-expression
9408 ? : assignment-expression */
9411 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9413 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9414 cp_expr assignment_expr
;
9415 struct cp_token
*token
;
9416 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9418 /* Consume the `?' token. */
9419 cp_lexer_consume_token (parser
->lexer
);
9420 token
= cp_lexer_peek_token (parser
->lexer
);
9421 if (cp_parser_allow_gnu_extensions_p (parser
)
9422 && token
->type
== CPP_COLON
)
9424 pedwarn (token
->location
, OPT_Wpedantic
,
9425 "ISO C++ does not allow ?: with omitted middle operand");
9426 /* Implicit true clause. */
9428 c_inhibit_evaluation_warnings
+=
9429 folded_logical_or_expr
== truthvalue_true_node
;
9430 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9434 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9435 parser
->colon_corrects_to_scope_p
= false;
9436 /* Parse the expression. */
9437 c_inhibit_evaluation_warnings
+=
9438 folded_logical_or_expr
== truthvalue_false_node
;
9439 expr
= cp_parser_expression (parser
);
9440 c_inhibit_evaluation_warnings
+=
9441 ((folded_logical_or_expr
== truthvalue_true_node
)
9442 - (folded_logical_or_expr
== truthvalue_false_node
));
9443 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9446 /* The next token should be a `:'. */
9447 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9448 /* Parse the assignment-expression. */
9449 assignment_expr
= cp_parser_assignment_expression (parser
);
9450 c_inhibit_evaluation_warnings
-=
9451 folded_logical_or_expr
== truthvalue_true_node
;
9454 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9455 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9456 with the caret at the "?", ranging from the start of
9457 the logical_or_expr to the end of the assignment_expr. */
9458 loc
= make_location (loc
,
9459 logical_or_expr
.get_start (),
9460 assignment_expr
.get_finish ());
9462 /* Build the conditional-expression. */
9463 return build_x_conditional_expr (loc
, logical_or_expr
,
9466 tf_warning_or_error
);
9469 /* Parse an assignment-expression.
9471 assignment-expression:
9472 conditional-expression
9473 logical-or-expression assignment-operator assignment_expression
9476 CAST_P is true if this expression is the target of a cast.
9477 DECLTYPE_P is true if this expression is the operand of decltype.
9479 Returns a representation for the expression. */
9482 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9483 bool cast_p
, bool decltype_p
)
9487 /* If the next token is the `throw' keyword, then we're looking at
9488 a throw-expression. */
9489 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9490 expr
= cp_parser_throw_expression (parser
);
9491 /* Otherwise, it must be that we are looking at a
9492 logical-or-expression. */
9495 /* Parse the binary expressions (logical-or-expression). */
9496 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9498 PREC_NOT_OPERATOR
, pidk
);
9499 /* If the next token is a `?' then we're actually looking at a
9500 conditional-expression. */
9501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9502 return cp_parser_question_colon_clause (parser
, expr
);
9505 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9507 /* If it's an assignment-operator, we're using the second
9509 enum tree_code assignment_operator
9510 = cp_parser_assignment_operator_opt (parser
);
9511 if (assignment_operator
!= ERROR_MARK
)
9513 bool non_constant_p
;
9515 /* Parse the right-hand side of the assignment. */
9516 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9519 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9520 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9522 /* An assignment may not appear in a
9523 constant-expression. */
9524 if (cp_parser_non_integral_constant_expression (parser
,
9526 return error_mark_node
;
9527 /* Build the assignment expression. Its default
9531 is the location of the '=' token as the
9532 caret, ranging from the start of the lhs to the
9534 loc
= make_location (loc
,
9537 expr
= build_x_modify_expr (loc
, expr
,
9538 assignment_operator
,
9540 complain_flags (decltype_p
));
9541 /* TODO: build_x_modify_expr doesn't honor the location,
9542 so we must set it here. */
9543 expr
.set_location (loc
);
9551 /* Parse an (optional) assignment-operator.
9553 assignment-operator: one of
9554 = *= /= %= += -= >>= <<= &= ^= |=
9558 assignment-operator: one of
9561 If the next token is an assignment operator, the corresponding tree
9562 code is returned, and the token is consumed. For example, for
9563 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9564 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9565 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9566 operator, ERROR_MARK is returned. */
9568 static enum tree_code
9569 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9574 /* Peek at the next token. */
9575 token
= cp_lexer_peek_token (parser
->lexer
);
9577 switch (token
->type
)
9588 op
= TRUNC_DIV_EXPR
;
9592 op
= TRUNC_MOD_EXPR
;
9624 /* Nothing else is an assignment operator. */
9628 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9629 if (op
!= ERROR_MARK
9630 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9633 /* If it was an assignment operator, consume it. */
9634 if (op
!= ERROR_MARK
)
9635 cp_lexer_consume_token (parser
->lexer
);
9640 /* Parse an expression.
9643 assignment-expression
9644 expression , assignment-expression
9646 CAST_P is true if this expression is the target of a cast.
9647 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9648 except possibly parenthesized or on the RHS of a comma (N3276).
9650 Returns a representation of the expression. */
9653 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9654 bool cast_p
, bool decltype_p
)
9656 cp_expr expression
= NULL_TREE
;
9657 location_t loc
= UNKNOWN_LOCATION
;
9661 cp_expr assignment_expression
;
9663 /* Parse the next assignment-expression. */
9664 assignment_expression
9665 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9667 /* We don't create a temporary for a call that is the immediate operand
9668 of decltype or on the RHS of a comma. But when we see a comma, we
9669 need to create a temporary for a call on the LHS. */
9670 if (decltype_p
&& !processing_template_decl
9671 && TREE_CODE (assignment_expression
) == CALL_EXPR
9672 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9673 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9674 assignment_expression
9675 = build_cplus_new (TREE_TYPE (assignment_expression
),
9676 assignment_expression
, tf_warning_or_error
);
9678 /* If this is the first assignment-expression, we can just
9681 expression
= assignment_expression
;
9684 /* Create a location with caret at the comma, ranging
9685 from the start of the LHS to the end of the RHS. */
9686 loc
= make_location (loc
,
9687 expression
.get_start (),
9688 assignment_expression
.get_finish ());
9689 expression
= build_x_compound_expr (loc
, expression
,
9690 assignment_expression
,
9691 complain_flags (decltype_p
));
9692 expression
.set_location (loc
);
9694 /* If the next token is not a comma, or we're in a fold-expression, then
9695 we are done with the expression. */
9696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9697 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9699 /* Consume the `,'. */
9700 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9701 cp_lexer_consume_token (parser
->lexer
);
9702 /* A comma operator cannot appear in a constant-expression. */
9703 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9704 expression
= error_mark_node
;
9710 /* Parse a constant-expression.
9712 constant-expression:
9713 conditional-expression
9715 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9716 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9717 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9718 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9719 only parse a conditional-expression, otherwise parse an
9720 assignment-expression. See below for rationale. */
9723 cp_parser_constant_expression (cp_parser
* parser
,
9724 bool allow_non_constant_p
,
9725 bool *non_constant_p
,
9728 bool saved_integral_constant_expression_p
;
9729 bool saved_allow_non_integral_constant_expression_p
;
9730 bool saved_non_integral_constant_expression_p
;
9733 /* It might seem that we could simply parse the
9734 conditional-expression, and then check to see if it were
9735 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9736 one that the compiler can figure out is constant, possibly after
9737 doing some simplifications or optimizations. The standard has a
9738 precise definition of constant-expression, and we must honor
9739 that, even though it is somewhat more restrictive.
9745 is not a legal declaration, because `(2, 3)' is not a
9746 constant-expression. The `,' operator is forbidden in a
9747 constant-expression. However, GCC's constant-folding machinery
9748 will fold this operation to an INTEGER_CST for `3'. */
9750 /* Save the old settings. */
9751 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9752 saved_allow_non_integral_constant_expression_p
9753 = parser
->allow_non_integral_constant_expression_p
;
9754 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9755 /* We are now parsing a constant-expression. */
9756 parser
->integral_constant_expression_p
= true;
9757 parser
->allow_non_integral_constant_expression_p
9758 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9759 parser
->non_integral_constant_expression_p
= false;
9760 /* Although the grammar says "conditional-expression", when not STRICT_P,
9761 we parse an "assignment-expression", which also permits
9762 "throw-expression" and the use of assignment operators. In the case
9763 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9764 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9765 actually essential that we look for an assignment-expression.
9766 For example, cp_parser_initializer_clauses uses this function to
9767 determine whether a particular assignment-expression is in fact
9771 /* Parse the binary expressions (logical-or-expression). */
9772 expression
= cp_parser_binary_expression (parser
, false, false, false,
9773 PREC_NOT_OPERATOR
, NULL
);
9774 /* If the next token is a `?' then we're actually looking at
9775 a conditional-expression; otherwise we're done. */
9776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9777 expression
= cp_parser_question_colon_clause (parser
, expression
);
9780 expression
= cp_parser_assignment_expression (parser
);
9781 /* Restore the old settings. */
9782 parser
->integral_constant_expression_p
9783 = saved_integral_constant_expression_p
;
9784 parser
->allow_non_integral_constant_expression_p
9785 = saved_allow_non_integral_constant_expression_p
;
9786 if (cxx_dialect
>= cxx11
)
9788 /* Require an rvalue constant expression here; that's what our
9789 callers expect. Reference constant expressions are handled
9790 separately in e.g. cp_parser_template_argument. */
9791 tree decay
= expression
;
9792 if (TREE_TYPE (expression
)
9793 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9794 decay
= build_address (expression
);
9795 bool is_const
= potential_rvalue_constant_expression (decay
);
9796 parser
->non_integral_constant_expression_p
= !is_const
;
9797 if (!is_const
&& !allow_non_constant_p
)
9798 require_potential_rvalue_constant_expression (decay
);
9800 if (allow_non_constant_p
)
9801 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9802 parser
->non_integral_constant_expression_p
9803 = saved_non_integral_constant_expression_p
;
9808 /* Parse __builtin_offsetof.
9810 offsetof-expression:
9811 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9813 offsetof-member-designator:
9815 | offsetof-member-designator "." id-expression
9816 | offsetof-member-designator "[" expression "]"
9817 | offsetof-member-designator "->" id-expression */
9820 cp_parser_builtin_offsetof (cp_parser
*parser
)
9822 int save_ice_p
, save_non_ice_p
;
9827 location_t finish_loc
;
9829 /* We're about to accept non-integral-constant things, but will
9830 definitely yield an integral constant expression. Save and
9831 restore these values around our local parsing. */
9832 save_ice_p
= parser
->integral_constant_expression_p
;
9833 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9835 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9837 /* Consume the "__builtin_offsetof" token. */
9838 cp_lexer_consume_token (parser
->lexer
);
9839 /* Consume the opening `('. */
9840 matching_parens parens
;
9841 parens
.require_open (parser
);
9842 /* Parse the type-id. */
9843 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9845 const char *saved_message
= parser
->type_definition_forbidden_message
;
9846 parser
->type_definition_forbidden_message
9847 = G_("types may not be defined within __builtin_offsetof");
9848 type
= cp_parser_type_id (parser
);
9849 parser
->type_definition_forbidden_message
= saved_message
;
9851 /* Look for the `,'. */
9852 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9853 token
= cp_lexer_peek_token (parser
->lexer
);
9855 /* Build the (type *)null that begins the traditional offsetof macro. */
9857 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9858 tf_warning_or_error
);
9860 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9861 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9862 true, &dummy
, token
->location
);
9865 token
= cp_lexer_peek_token (parser
->lexer
);
9866 switch (token
->type
)
9868 case CPP_OPEN_SQUARE
:
9869 /* offsetof-member-designator "[" expression "]" */
9870 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9875 /* offsetof-member-designator "->" identifier */
9876 expr
= grok_array_decl (token
->location
, expr
,
9877 integer_zero_node
, false);
9881 /* offsetof-member-designator "." identifier */
9882 cp_lexer_consume_token (parser
->lexer
);
9883 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9888 case CPP_CLOSE_PAREN
:
9889 /* Consume the ")" token. */
9890 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9891 cp_lexer_consume_token (parser
->lexer
);
9895 /* Error. We know the following require will fail, but
9896 that gives the proper error message. */
9897 parens
.require_close (parser
);
9898 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9899 expr
= error_mark_node
;
9905 /* Make a location of the form:
9906 __builtin_offsetof (struct s, f)
9907 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9908 with caret at the type-id, ranging from the start of the
9909 "_builtin_offsetof" token to the close paren. */
9910 loc
= make_location (loc
, start_loc
, finish_loc
);
9911 /* The result will be an INTEGER_CST, so we need to explicitly
9912 preserve the location. */
9913 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9916 parser
->integral_constant_expression_p
= save_ice_p
;
9917 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9919 expr
= expr
.maybe_add_location_wrapper ();
9923 /* Parse a trait expression.
9925 Returns a representation of the expression, the underlying type
9926 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9929 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9932 tree type1
, type2
= NULL_TREE
;
9933 bool binary
= false;
9934 bool variadic
= false;
9938 case RID_HAS_NOTHROW_ASSIGN
:
9939 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9941 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9942 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9944 case RID_HAS_NOTHROW_COPY
:
9945 kind
= CPTK_HAS_NOTHROW_COPY
;
9947 case RID_HAS_TRIVIAL_ASSIGN
:
9948 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9950 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9951 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9953 case RID_HAS_TRIVIAL_COPY
:
9954 kind
= CPTK_HAS_TRIVIAL_COPY
;
9956 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9957 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9959 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9960 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9962 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9963 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9965 case RID_IS_ABSTRACT
:
9966 kind
= CPTK_IS_ABSTRACT
;
9968 case RID_IS_AGGREGATE
:
9969 kind
= CPTK_IS_AGGREGATE
;
9971 case RID_IS_BASE_OF
:
9972 kind
= CPTK_IS_BASE_OF
;
9976 kind
= CPTK_IS_CLASS
;
9979 kind
= CPTK_IS_EMPTY
;
9982 kind
= CPTK_IS_ENUM
;
9985 kind
= CPTK_IS_FINAL
;
9987 case RID_IS_LITERAL_TYPE
:
9988 kind
= CPTK_IS_LITERAL_TYPE
;
9993 case RID_IS_POLYMORPHIC
:
9994 kind
= CPTK_IS_POLYMORPHIC
;
9996 case RID_IS_SAME_AS
:
9997 kind
= CPTK_IS_SAME_AS
;
10000 case RID_IS_STD_LAYOUT
:
10001 kind
= CPTK_IS_STD_LAYOUT
;
10003 case RID_IS_TRIVIAL
:
10004 kind
= CPTK_IS_TRIVIAL
;
10006 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10007 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10010 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10011 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10014 case RID_IS_TRIVIALLY_COPYABLE
:
10015 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10018 kind
= CPTK_IS_UNION
;
10020 case RID_UNDERLYING_TYPE
:
10021 kind
= CPTK_UNDERLYING_TYPE
;
10026 case RID_DIRECT_BASES
:
10027 kind
= CPTK_DIRECT_BASES
;
10029 case RID_IS_ASSIGNABLE
:
10030 kind
= CPTK_IS_ASSIGNABLE
;
10033 case RID_IS_CONSTRUCTIBLE
:
10034 kind
= CPTK_IS_CONSTRUCTIBLE
;
10038 gcc_unreachable ();
10041 /* Get location of initial token. */
10042 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10044 /* Consume the token. */
10045 cp_lexer_consume_token (parser
->lexer
);
10047 matching_parens parens
;
10048 parens
.require_open (parser
);
10051 type_id_in_expr_sentinel
s (parser
);
10052 type1
= cp_parser_type_id (parser
);
10055 if (type1
== error_mark_node
)
10056 return error_mark_node
;
10060 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10063 type_id_in_expr_sentinel
s (parser
);
10064 type2
= cp_parser_type_id (parser
);
10067 if (type2
== error_mark_node
)
10068 return error_mark_node
;
10072 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10074 cp_lexer_consume_token (parser
->lexer
);
10075 tree elt
= cp_parser_type_id (parser
);
10076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10078 cp_lexer_consume_token (parser
->lexer
);
10079 elt
= make_pack_expansion (elt
);
10081 if (elt
== error_mark_node
)
10082 return error_mark_node
;
10083 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10087 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10088 parens
.require_close (parser
);
10090 /* Construct a location of the form:
10091 __is_trivially_copyable(_Tp)
10092 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10093 with start == caret, finishing at the close-paren. */
10094 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10096 /* Complete the trait expression, which may mean either processing
10097 the trait expr now or saving it for template instantiation. */
10100 case CPTK_UNDERLYING_TYPE
:
10101 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10103 return cp_expr (finish_bases (type1
, false), trait_loc
);
10104 case CPTK_DIRECT_BASES
:
10105 return cp_expr (finish_bases (type1
, true), trait_loc
);
10107 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10111 /* Parse a lambda expression.
10114 lambda-introducer lambda-declarator [opt] compound-statement
10116 Returns a representation of the expression. */
10119 cp_parser_lambda_expression (cp_parser
* parser
)
10121 tree lambda_expr
= build_lambda_expr ();
10124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10125 cp_token_position start
= 0;
10127 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10129 if (cp_unevaluated_operand
)
10131 if (!token
->error_reported
)
10133 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10134 "lambda-expression in unevaluated context");
10135 token
->error_reported
= true;
10139 else if (parser
->in_template_argument_list_p
)
10141 if (!token
->error_reported
)
10143 error_at (token
->location
, "lambda-expression in template-argument");
10144 token
->error_reported
= true;
10149 /* We may be in the middle of deferred access check. Disable
10151 push_deferring_access_checks (dk_no_deferred
);
10153 cp_parser_lambda_introducer (parser
, lambda_expr
);
10155 type
= begin_lambda_type (lambda_expr
);
10156 if (type
== error_mark_node
)
10157 return error_mark_node
;
10159 record_lambda_scope (lambda_expr
);
10161 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10162 determine_visibility (TYPE_NAME (type
));
10164 /* Now that we've started the type, add the capture fields for any
10165 explicit captures. */
10166 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10169 /* Inside the class, surrounding template-parameter-lists do not apply. */
10170 unsigned int saved_num_template_parameter_lists
10171 = parser
->num_template_parameter_lists
;
10172 unsigned char in_statement
= parser
->in_statement
;
10173 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10174 bool fully_implicit_function_template_p
10175 = parser
->fully_implicit_function_template_p
;
10176 tree implicit_template_parms
= parser
->implicit_template_parms
;
10177 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10178 bool auto_is_implicit_function_template_parm_p
10179 = parser
->auto_is_implicit_function_template_parm_p
;
10181 parser
->num_template_parameter_lists
= 0;
10182 parser
->in_statement
= 0;
10183 parser
->in_switch_statement_p
= false;
10184 parser
->fully_implicit_function_template_p
= false;
10185 parser
->implicit_template_parms
= 0;
10186 parser
->implicit_template_scope
= 0;
10187 parser
->auto_is_implicit_function_template_parm_p
= false;
10189 /* By virtue of defining a local class, a lambda expression has access to
10190 the private variables of enclosing classes. */
10192 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10194 if (ok
&& cp_parser_error_occurred (parser
))
10199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10200 && cp_parser_start_tentative_firewall (parser
))
10202 cp_parser_lambda_body (parser
, lambda_expr
);
10204 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10206 if (cp_parser_skip_to_closing_brace (parser
))
10207 cp_lexer_consume_token (parser
->lexer
);
10210 /* The capture list was built up in reverse order; fix that now. */
10211 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10212 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10215 maybe_add_lambda_conv_op (type
);
10217 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10219 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10220 parser
->in_statement
= in_statement
;
10221 parser
->in_switch_statement_p
= in_switch_statement_p
;
10222 parser
->fully_implicit_function_template_p
10223 = fully_implicit_function_template_p
;
10224 parser
->implicit_template_parms
= implicit_template_parms
;
10225 parser
->implicit_template_scope
= implicit_template_scope
;
10226 parser
->auto_is_implicit_function_template_parm_p
10227 = auto_is_implicit_function_template_parm_p
;
10230 /* This field is only used during parsing of the lambda. */
10231 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10233 /* This lambda shouldn't have any proxies left at this point. */
10234 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10235 /* And now that we're done, push proxies for an enclosing lambda. */
10236 insert_pending_capture_proxies ();
10238 /* Update the lambda expression to a range. */
10239 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
10240 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10242 end_tok
->location
);
10245 lambda_expr
= build_lambda_object (lambda_expr
);
10247 lambda_expr
= error_mark_node
;
10249 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10251 pop_deferring_access_checks ();
10253 return lambda_expr
;
10256 /* Parse the beginning of a lambda expression.
10259 [ lambda-capture [opt] ]
10261 LAMBDA_EXPR is the current representation of the lambda expression. */
10264 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10266 /* Need commas after the first capture. */
10269 /* Eat the leading `['. */
10270 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10272 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10274 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10275 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10276 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10279 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10281 cp_lexer_consume_token (parser
->lexer
);
10285 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10287 cp_token
* capture_token
;
10289 tree capture_init_expr
;
10290 cp_id_kind idk
= CP_ID_KIND_NONE
;
10291 bool explicit_init_p
= false;
10293 enum capture_kind_type
10298 enum capture_kind_type capture_kind
= BY_COPY
;
10300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10302 error ("expected end of capture-list");
10309 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10311 /* Possibly capture `this'. */
10312 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10314 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10315 if (cxx_dialect
< cxx2a
10316 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10317 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10318 "with by-copy capture default");
10319 cp_lexer_consume_token (parser
->lexer
);
10320 add_capture (lambda_expr
,
10321 /*id=*/this_identifier
,
10322 /*initializer=*/finish_this_expr (),
10323 /*by_reference_p=*/true,
10328 /* Possibly capture `*this'. */
10329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10330 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10332 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10333 if (cxx_dialect
< cxx17
)
10334 pedwarn (loc
, 0, "%<*this%> capture only available with "
10335 "-std=c++17 or -std=gnu++17");
10336 cp_lexer_consume_token (parser
->lexer
);
10337 cp_lexer_consume_token (parser
->lexer
);
10338 add_capture (lambda_expr
,
10339 /*id=*/this_identifier
,
10340 /*initializer=*/finish_this_expr (),
10341 /*by_reference_p=*/false,
10346 /* Remember whether we want to capture as a reference or not. */
10347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10349 capture_kind
= BY_REFERENCE
;
10350 cp_lexer_consume_token (parser
->lexer
);
10353 /* Get the identifier. */
10354 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10355 capture_id
= cp_parser_identifier (parser
);
10357 if (capture_id
== error_mark_node
)
10358 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10359 delimiters, but I modified this to stop on unnested ']' as well. It
10360 was already changed to stop on unnested '}', so the
10361 "closing_parenthesis" name is no more misleading with my change. */
10363 cp_parser_skip_to_closing_parenthesis (parser
,
10364 /*recovering=*/true,
10366 /*consume_paren=*/true);
10370 /* Find the initializer for this capture. */
10371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10372 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10373 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10375 bool direct
, non_constant
;
10376 /* An explicit initializer exists. */
10377 if (cxx_dialect
< cxx14
)
10378 pedwarn (input_location
, 0,
10379 "lambda capture initializers "
10380 "only available with -std=c++14 or -std=gnu++14");
10381 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10382 &non_constant
, true);
10383 explicit_init_p
= true;
10384 if (capture_init_expr
== NULL_TREE
)
10386 error ("empty initializer for lambda init-capture");
10387 capture_init_expr
= error_mark_node
;
10392 const char* error_msg
;
10394 /* Turn the identifier into an id-expression. */
10396 = cp_parser_lookup_name_simple (parser
, capture_id
,
10397 capture_token
->location
);
10399 if (capture_init_expr
== error_mark_node
)
10401 unqualified_name_lookup_error (capture_id
);
10404 else if (!VAR_P (capture_init_expr
)
10405 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10407 error_at (capture_token
->location
,
10408 "capture of non-variable %qE",
10409 capture_init_expr
);
10410 if (DECL_P (capture_init_expr
))
10411 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10412 "%q#D declared here", capture_init_expr
);
10415 if (VAR_P (capture_init_expr
)
10416 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10418 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10419 "%qD with non-automatic storage duration",
10420 capture_init_expr
))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10422 "%q#D declared here", capture_init_expr
);
10427 = finish_id_expression
10432 /*integral_constant_expression_p=*/false,
10433 /*allow_non_integral_constant_expression_p=*/false,
10434 /*non_integral_constant_expression_p=*/NULL
,
10435 /*template_p=*/false,
10437 /*address_p=*/false,
10438 /*template_arg_p=*/false,
10440 capture_token
->location
);
10442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10444 cp_lexer_consume_token (parser
->lexer
);
10445 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10450 && !explicit_init_p
)
10452 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10453 && capture_kind
== BY_COPY
)
10454 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10455 "of %qD redundant with by-copy capture default",
10457 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10458 && capture_kind
== BY_REFERENCE
)
10459 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10460 "capture of %qD redundant with by-reference capture "
10461 "default", capture_id
);
10464 add_capture (lambda_expr
,
10467 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10470 /* If there is any qualification still in effect, clear it
10471 now; we will be starting fresh with the next capture. */
10472 parser
->scope
= NULL_TREE
;
10473 parser
->qualifying_scope
= NULL_TREE
;
10474 parser
->object_scope
= NULL_TREE
;
10477 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10480 /* Parse the (optional) middle of a lambda expression.
10483 < template-parameter-list [opt] >
10484 ( parameter-declaration-clause [opt] )
10485 attribute-specifier [opt]
10486 decl-specifier-seq [opt]
10487 exception-specification [opt]
10488 lambda-return-type-clause [opt]
10490 LAMBDA_EXPR is the current representation of the lambda expression. */
10493 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10495 /* 5.1.1.4 of the standard says:
10496 If a lambda-expression does not include a lambda-declarator, it is as if
10497 the lambda-declarator were ().
10498 This means an empty parameter list, no attributes, and no exception
10500 tree param_list
= void_list_node
;
10501 tree attributes
= NULL_TREE
;
10502 tree exception_spec
= NULL_TREE
;
10503 tree template_param_list
= NULL_TREE
;
10504 tree tx_qual
= NULL_TREE
;
10505 tree return_type
= NULL_TREE
;
10506 cp_decl_specifier_seq lambda_specs
;
10507 clear_decl_specs (&lambda_specs
);
10509 /* The template-parameter-list is optional, but must begin with
10510 an opening angle if present. */
10511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10513 if (cxx_dialect
< cxx14
)
10514 pedwarn (parser
->lexer
->next_token
->location
, 0,
10515 "lambda templates are only available with "
10516 "-std=c++14 or -std=gnu++14");
10517 else if (cxx_dialect
< cxx2a
)
10518 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10519 "lambda templates are only available with "
10520 "-std=c++2a or -std=gnu++2a");
10522 cp_lexer_consume_token (parser
->lexer
);
10524 template_param_list
= cp_parser_template_parameter_list (parser
);
10526 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10528 /* We just processed one more parameter list. */
10529 ++parser
->num_template_parameter_lists
;
10532 /* The parameter-declaration-clause is optional (unless
10533 template-parameter-list was given), but must begin with an
10534 opening parenthesis if present. */
10535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10537 matching_parens parens
;
10538 parens
.consume_open (parser
);
10540 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10542 /* Parse parameters. */
10543 param_list
= cp_parser_parameter_declaration_clause (parser
);
10545 /* Default arguments shall not be specified in the
10546 parameter-declaration-clause of a lambda-declarator. */
10547 if (cxx_dialect
< cxx14
)
10548 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10549 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10550 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10551 "default argument specified for lambda parameter");
10553 parens
.require_close (parser
);
10555 attributes
= cp_parser_attributes_opt (parser
);
10557 /* In the decl-specifier-seq of the lambda-declarator, each
10558 decl-specifier shall either be mutable or constexpr. */
10559 int declares_class_or_enum
;
10560 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10561 cp_parser_decl_specifier_seq (parser
,
10562 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10563 &lambda_specs
, &declares_class_or_enum
);
10564 if (lambda_specs
.storage_class
== sc_mutable
)
10566 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10567 if (lambda_specs
.conflicting_specifiers_p
)
10568 error_at (lambda_specs
.locations
[ds_storage_class
],
10569 "duplicate %<mutable%>");
10572 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10574 /* Parse optional exception specification. */
10575 exception_spec
= cp_parser_exception_specification_opt (parser
);
10577 /* Parse optional trailing return type. */
10578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10580 cp_lexer_consume_token (parser
->lexer
);
10581 return_type
= cp_parser_trailing_type_id (parser
);
10584 /* The function parameters must be in scope all the way until after the
10585 trailing-return-type in case of decltype. */
10586 pop_bindings_and_leave_scope ();
10588 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10589 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10591 /* Create the function call operator.
10593 Messing with declarators like this is no uglier than building up the
10594 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10597 cp_decl_specifier_seq return_type_specs
;
10598 cp_declarator
* declarator
;
10603 clear_decl_specs (&return_type_specs
);
10604 return_type_specs
.type
= make_auto ();
10606 if (lambda_specs
.locations
[ds_constexpr
])
10608 if (cxx_dialect
>= cxx17
)
10609 return_type_specs
.locations
[ds_constexpr
]
10610 = lambda_specs
.locations
[ds_constexpr
];
10612 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10613 "lambda only available with -std=c++17 or -std=gnu++17");
10616 p
= obstack_alloc (&declarator_obstack
, 0);
10618 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10620 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10621 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10622 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10623 VIRT_SPEC_UNSPECIFIED
,
10627 /*late_return_type=*/NULL_TREE
,
10628 /*requires_clause*/NULL_TREE
);
10629 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10631 declarator
->u
.function
.late_return_type
= return_type
;
10633 fco
= grokmethod (&return_type_specs
,
10636 if (fco
!= error_mark_node
)
10638 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10639 DECL_ARTIFICIAL (fco
) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10643 if (template_param_list
)
10645 fco
= finish_member_template_decl (fco
);
10646 finish_template_decl (template_param_list
);
10647 --parser
->num_template_parameter_lists
;
10649 else if (parser
->fully_implicit_function_template_p
)
10650 fco
= finish_fully_implicit_template (parser
, fco
);
10652 finish_member_declaration (fco
);
10654 obstack_free (&declarator_obstack
, p
);
10656 return (fco
!= error_mark_node
);
10660 /* Parse the body of a lambda expression, which is simply
10664 but which requires special handling.
10665 LAMBDA_EXPR is the current representation of the lambda expression. */
10668 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10670 bool nested
= (current_function_decl
!= NULL_TREE
);
10671 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10672 bool in_function_body
= parser
->in_function_body
;
10675 push_function_context ();
10677 /* Still increment function_depth so that we don't GC in the
10678 middle of an expression. */
10681 vec
<tree
> omp_privatization_save
;
10682 save_omp_privatization_clauses (omp_privatization_save
);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser
->local_variables_forbidden_p
= false;
10685 parser
->in_function_body
= true;
10688 local_specialization_stack
s (lss_copy
);
10689 tree fco
= lambda_function (lambda_expr
);
10690 tree body
= start_lambda_function (fco
, lambda_expr
);
10691 matching_braces braces
;
10693 if (braces
.require_open (parser
))
10695 tree compound_stmt
= begin_compound_stmt (0);
10697 /* Originally C++11 required us to peek for 'return expr'; and
10698 process it specially here to deduce the return type. N3638
10699 removed the need for that. */
10701 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10702 cp_parser_label_declaration (parser
);
10703 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10704 braces
.require_close (parser
);
10706 finish_compound_stmt (compound_stmt
);
10709 finish_lambda_function (body
);
10712 restore_omp_privatization_clauses (omp_privatization_save
);
10713 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10714 parser
->in_function_body
= in_function_body
;
10716 pop_function_context();
10721 /* Statements [gram.stmt.stmt] */
10723 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10726 add_debug_begin_stmt (location_t loc
)
10728 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10730 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10731 /* A concept is never expanded normally. */
10734 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10735 SET_EXPR_LOCATION (stmt
, loc
);
10739 /* Parse a statement.
10743 expression-statement
10745 selection-statement
10746 iteration-statement
10748 declaration-statement
10755 attribute-specifier-seq (opt) expression-statement
10756 attribute-specifier-seq (opt) compound-statement
10757 attribute-specifier-seq (opt) selection-statement
10758 attribute-specifier-seq (opt) iteration-statement
10759 attribute-specifier-seq (opt) jump-statement
10760 declaration-statement
10761 attribute-specifier-seq (opt) try-block
10764 expression-statement
10772 IN_COMPOUND is true when the statement is nested inside a
10773 cp_parser_compound_statement; this matters for certain pragmas.
10775 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10776 is a (possibly labeled) if statement which is not enclosed in braces
10777 and has an else clause. This is used to implement -Wparentheses.
10779 CHAIN is a vector of if-else-if conditions. */
10782 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10783 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10784 location_t
*loc_after_labels
)
10786 tree statement
, std_attrs
= NULL_TREE
;
10788 location_t statement_location
, attrs_location
;
10793 /* There is no statement yet. */
10794 statement
= NULL_TREE
;
10796 saved_token_sentinel
saved_tokens (parser
->lexer
);
10797 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10798 if (c_dialect_objc ())
10799 /* In obj-c++, seeing '[[' might be the either the beginning of
10800 c++11 attributes, or a nested objc-message-expression. So
10801 let's parse the c++11 attributes tentatively. */
10802 cp_parser_parse_tentatively (parser
);
10803 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10804 if (c_dialect_objc ())
10806 if (!cp_parser_parse_definitely (parser
))
10807 std_attrs
= NULL_TREE
;
10810 /* Peek at the next token. */
10811 token
= cp_lexer_peek_token (parser
->lexer
);
10812 /* Remember the location of the first token in the statement. */
10813 statement_location
= token
->location
;
10814 add_debug_begin_stmt (statement_location
);
10815 /* If this is a keyword, then that will often determine what kind of
10816 statement we have. */
10817 if (token
->type
== CPP_KEYWORD
)
10819 enum rid keyword
= token
->keyword
;
10825 /* Looks like a labeled-statement with a case label.
10826 Parse the label, and then use tail recursion to parse
10828 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10829 in_compound
= false;
10834 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10840 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10847 statement
= cp_parser_jump_statement (parser
);
10850 /* Objective-C++ exception-handling constructs. */
10853 case RID_AT_FINALLY
:
10854 case RID_AT_SYNCHRONIZED
:
10856 statement
= cp_parser_objc_statement (parser
);
10860 statement
= cp_parser_try_block (parser
);
10863 case RID_NAMESPACE
:
10864 /* This must be a namespace alias definition. */
10865 cp_parser_declaration_statement (parser
);
10868 case RID_TRANSACTION_ATOMIC
:
10869 case RID_TRANSACTION_RELAXED
:
10870 case RID_SYNCHRONIZED
:
10871 case RID_ATOMIC_NOEXCEPT
:
10872 case RID_ATOMIC_CANCEL
:
10873 statement
= cp_parser_transaction (parser
, token
);
10875 case RID_TRANSACTION_CANCEL
:
10876 statement
= cp_parser_transaction_cancel (parser
);
10880 /* It might be a keyword like `int' that can start a
10881 declaration-statement. */
10885 else if (token
->type
== CPP_NAME
)
10887 /* If the next token is a `:', then we are looking at a
10888 labeled-statement. */
10889 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10890 if (token
->type
== CPP_COLON
)
10892 /* Looks like a labeled-statement with an ordinary label.
10893 Parse the label, and then use tail recursion to parse
10896 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10897 in_compound
= false;
10901 /* Anything that starts with a `{' must be a compound-statement. */
10902 else if (token
->type
== CPP_OPEN_BRACE
)
10903 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10904 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10905 a statement all its own. */
10906 else if (token
->type
== CPP_PRAGMA
)
10908 /* Only certain OpenMP pragmas are attached to statements, and thus
10909 are considered statements themselves. All others are not. In
10910 the context of a compound, accept the pragma as a "statement" and
10911 return so that we can check for a close brace. Otherwise we
10912 require a real statement and must go back and read one. */
10914 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10915 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10919 else if (token
->type
== CPP_EOF
)
10921 cp_parser_error (parser
, "expected statement");
10925 /* Everything else must be a declaration-statement or an
10926 expression-statement. Try for the declaration-statement
10927 first, unless we are looking at a `;', in which case we know that
10928 we have an expression-statement. */
10931 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10933 if (std_attrs
!= NULL_TREE
)
10935 /* Attributes should be parsed as part of the the
10936 declaration, so let's un-parse them. */
10937 saved_tokens
.rollback();
10938 std_attrs
= NULL_TREE
;
10941 cp_parser_parse_tentatively (parser
);
10942 /* Try to parse the declaration-statement. */
10943 cp_parser_declaration_statement (parser
);
10944 /* If that worked, we're done. */
10945 if (cp_parser_parse_definitely (parser
))
10948 /* All preceding labels have been parsed at this point. */
10949 if (loc_after_labels
!= NULL
)
10950 *loc_after_labels
= statement_location
;
10952 /* Look for an expression-statement instead. */
10953 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10955 /* Handle [[fallthrough]];. */
10956 if (attribute_fallthrough_p (std_attrs
))
10958 /* The next token after the fallthrough attribute is ';'. */
10959 if (statement
== NULL_TREE
)
10961 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10962 statement
= build_call_expr_internal_loc (statement_location
,
10964 void_type_node
, 0);
10965 finish_expr_stmt (statement
);
10968 warning_at (statement_location
, OPT_Wattributes
,
10969 "%<fallthrough%> attribute not followed by %<;%>");
10970 std_attrs
= NULL_TREE
;
10974 /* Set the line number for the statement. */
10975 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10976 SET_EXPR_LOCATION (statement
, statement_location
);
10978 /* Allow "[[fallthrough]];", but warn otherwise. */
10979 if (std_attrs
!= NULL_TREE
)
10980 warning_at (attrs_location
,
10982 "attributes at the beginning of statement are ignored");
10985 /* Append ATTR to attribute list ATTRS. */
10988 attr_chainon (tree attrs
, tree attr
)
10990 if (attrs
== error_mark_node
)
10991 return error_mark_node
;
10992 if (attr
== error_mark_node
)
10993 return error_mark_node
;
10994 return chainon (attrs
, attr
);
10997 /* Parse the label for a labeled-statement, i.e.
11000 case constant-expression :
11004 case constant-expression ... constant-expression : statement
11006 When a label is parsed without errors, the label is added to the
11007 parse tree by the finish_* functions, so this function doesn't
11008 have to return the label. */
11011 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11014 tree label
= NULL_TREE
;
11015 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11017 /* The next token should be an identifier. */
11018 token
= cp_lexer_peek_token (parser
->lexer
);
11019 if (token
->type
!= CPP_NAME
11020 && token
->type
!= CPP_KEYWORD
)
11022 cp_parser_error (parser
, "expected labeled-statement");
11026 /* Remember whether this case or a user-defined label is allowed to fall
11028 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11030 parser
->colon_corrects_to_scope_p
= false;
11031 switch (token
->keyword
)
11035 tree expr
, expr_hi
;
11036 cp_token
*ellipsis
;
11038 /* Consume the `case' token. */
11039 cp_lexer_consume_token (parser
->lexer
);
11040 /* Parse the constant-expression. */
11041 expr
= cp_parser_constant_expression (parser
);
11042 if (check_for_bare_parameter_packs (expr
))
11043 expr
= error_mark_node
;
11045 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11046 if (ellipsis
->type
== CPP_ELLIPSIS
)
11048 /* Consume the `...' token. */
11049 cp_lexer_consume_token (parser
->lexer
);
11050 expr_hi
= cp_parser_constant_expression (parser
);
11051 if (check_for_bare_parameter_packs (expr_hi
))
11052 expr_hi
= error_mark_node
;
11054 /* We don't need to emit warnings here, as the common code
11055 will do this for us. */
11058 expr_hi
= NULL_TREE
;
11060 if (parser
->in_switch_statement_p
)
11062 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11063 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11064 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11067 error_at (token
->location
,
11068 "case label %qE not within a switch statement",
11074 /* Consume the `default' token. */
11075 cp_lexer_consume_token (parser
->lexer
);
11077 if (parser
->in_switch_statement_p
)
11079 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11080 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11081 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11084 error_at (token
->location
, "case label not within a switch statement");
11088 /* Anything else must be an ordinary label. */
11089 label
= finish_label_stmt (cp_parser_identifier (parser
));
11090 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11091 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11095 /* Require the `:' token. */
11096 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11098 /* An ordinary label may optionally be followed by attributes.
11099 However, this is only permitted if the attributes are then
11100 followed by a semicolon. This is because, for backward
11101 compatibility, when parsing
11102 lab: __attribute__ ((unused)) int i;
11103 we want the attribute to attach to "i", not "lab". */
11104 if (label
!= NULL_TREE
11105 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11108 cp_parser_parse_tentatively (parser
);
11109 attrs
= cp_parser_gnu_attributes_opt (parser
);
11110 if (attrs
== NULL_TREE
11111 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11112 cp_parser_abort_tentative_parse (parser
);
11113 else if (!cp_parser_parse_definitely (parser
))
11116 attributes
= attr_chainon (attributes
, attrs
);
11119 if (attributes
!= NULL_TREE
)
11120 cplus_decl_attributes (&label
, attributes
, 0);
11122 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11125 /* Parse an expression-statement.
11127 expression-statement:
11130 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11131 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11132 indicates whether this expression-statement is part of an
11133 expression statement. */
11136 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11138 tree statement
= NULL_TREE
;
11139 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11140 location_t loc
= token
->location
;
11142 /* There might be attribute fallthrough. */
11143 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11145 /* If the next token is a ';', then there is no expression
11147 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11149 statement
= cp_parser_expression (parser
);
11150 if (statement
== error_mark_node
11151 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11153 cp_parser_skip_to_end_of_block_or_statement (parser
);
11154 return error_mark_node
;
11158 /* Handle [[fallthrough]];. */
11159 if (attribute_fallthrough_p (attr
))
11161 /* The next token after the fallthrough attribute is ';'. */
11162 if (statement
== NULL_TREE
)
11163 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11164 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11165 void_type_node
, 0);
11167 warning_at (loc
, OPT_Wattributes
,
11168 "%<fallthrough%> attribute not followed by %<;%>");
11172 /* Allow "[[fallthrough]];", but warn otherwise. */
11173 if (attr
!= NULL_TREE
)
11174 warning_at (loc
, OPT_Wattributes
,
11175 "attributes at the beginning of statement are ignored");
11177 /* Give a helpful message for "A<T>::type t;" and the like. */
11178 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11179 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11181 if (TREE_CODE (statement
) == SCOPE_REF
)
11182 error_at (token
->location
, "need %<typename%> before %qE because "
11183 "%qT is a dependent scope",
11184 statement
, TREE_OPERAND (statement
, 0));
11185 else if (is_overloaded_fn (statement
)
11186 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11189 tree fn
= get_first_fn (statement
);
11190 error_at (token
->location
,
11191 "%<%T::%D%> names the constructor, not the type",
11192 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11196 /* Consume the final `;'. */
11197 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11199 if (in_statement_expr
11200 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11201 /* This is the final expression statement of a statement
11203 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11204 else if (statement
)
11205 statement
= finish_expr_stmt (statement
);
11210 /* Parse a compound-statement.
11212 compound-statement:
11213 { statement-seq [opt] }
11217 compound-statement:
11218 { label-declaration-seq [opt] statement-seq [opt] }
11220 label-declaration-seq:
11222 label-declaration-seq label-declaration
11224 Returns a tree representing the statement. */
11227 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11228 int bcs_flags
, bool function_body
)
11230 tree compound_stmt
;
11231 matching_braces braces
;
11233 /* Consume the `{'. */
11234 if (!braces
.require_open (parser
))
11235 return error_mark_node
;
11236 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11237 && !function_body
&& cxx_dialect
< cxx14
)
11238 pedwarn (input_location
, OPT_Wpedantic
,
11239 "compound-statement in %<constexpr%> function");
11240 /* Begin the compound-statement. */
11241 compound_stmt
= begin_compound_stmt (bcs_flags
);
11242 /* If the next keyword is `__label__' we have a label declaration. */
11243 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11244 cp_parser_label_declaration (parser
);
11245 /* Parse an (optional) statement-seq. */
11246 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11247 /* Finish the compound-statement. */
11248 finish_compound_stmt (compound_stmt
);
11249 /* Consume the `}'. */
11250 braces
.require_close (parser
);
11252 return compound_stmt
;
11255 /* Parse an (optional) statement-seq.
11259 statement-seq [opt] statement */
11262 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11264 /* Scan statements until there aren't any more. */
11267 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11269 /* If we are looking at a `}', then we have run out of
11270 statements; the same is true if we have reached the end
11271 of file, or have stumbled upon a stray '@end'. */
11272 if (token
->type
== CPP_CLOSE_BRACE
11273 || token
->type
== CPP_EOF
11274 || token
->type
== CPP_PRAGMA_EOL
11275 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11278 /* If we are in a compound statement and find 'else' then
11279 something went wrong. */
11280 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11282 if (parser
->in_statement
& IN_IF_STMT
)
11286 token
= cp_lexer_consume_token (parser
->lexer
);
11287 error_at (token
->location
, "%<else%> without a previous %<if%>");
11291 /* Parse the statement. */
11292 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11296 /* Return true if this is the C++20 version of range-based-for with
11300 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11304 /* Save tokens so that we can put them back. */
11305 cp_lexer_save_tokens (parser
->lexer
);
11307 /* There has to be an unnested ; followed by an unnested :. */
11308 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11309 /*recovering=*/false,
11311 /*consume_paren=*/false) != -1)
11314 /* We found the semicolon, eat it now. */
11315 cp_lexer_consume_token (parser
->lexer
);
11317 /* Now look for ':' that is not nested in () or {}. */
11318 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11319 /*recovering=*/false,
11321 /*consume_paren=*/false) == -1);
11324 /* Roll back the tokens we skipped. */
11325 cp_lexer_rollback_tokens (parser
->lexer
);
11330 /* Return true if we're looking at (init; cond), false otherwise. */
11333 cp_parser_init_statement_p (cp_parser
*parser
)
11335 /* Save tokens so that we can put them back. */
11336 cp_lexer_save_tokens (parser
->lexer
);
11338 /* Look for ';' that is not nested in () or {}. */
11339 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11340 /*recovering=*/false,
11342 /*consume_paren=*/false);
11344 /* Roll back the tokens we skipped. */
11345 cp_lexer_rollback_tokens (parser
->lexer
);
11350 /* Parse a selection-statement.
11352 selection-statement:
11353 if ( init-statement [opt] condition ) statement
11354 if ( init-statement [opt] condition ) statement else statement
11355 switch ( init-statement [opt] condition ) statement
11357 Returns the new IF_STMT or SWITCH_STMT.
11359 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11360 is a (possibly labeled) if statement which is not enclosed in
11361 braces and has an else clause. This is used to implement
11364 CHAIN is a vector of if-else-if conditions. This is used to implement
11365 -Wduplicated-cond. */
11368 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11373 token_indent_info guard_tinfo
;
11378 /* Peek at the next token. */
11379 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11380 guard_tinfo
= get_token_indent_info (token
);
11382 /* See what kind of keyword it is. */
11383 keyword
= token
->keyword
;
11393 if (keyword
== RID_IF
11394 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11398 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11399 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11400 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11401 "with -std=c++17 or -std=gnu++17");
11404 /* Look for the `('. */
11405 matching_parens parens
;
11406 if (!parens
.require_open (parser
))
11408 cp_parser_skip_to_end_of_statement (parser
);
11409 return error_mark_node
;
11412 /* Begin the selection-statement. */
11413 if (keyword
== RID_IF
)
11415 statement
= begin_if_stmt ();
11416 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11419 statement
= begin_switch_stmt ();
11421 /* Parse the optional init-statement. */
11422 if (cp_parser_init_statement_p (parser
))
11425 if (cxx_dialect
< cxx17
)
11426 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11427 "init-statement in selection statements only available "
11428 "with -std=c++17 or -std=gnu++17");
11429 cp_parser_init_statement (parser
, &decl
);
11432 /* Parse the condition. */
11433 condition
= cp_parser_condition (parser
);
11434 /* Look for the `)'. */
11435 if (!parens
.require_close (parser
))
11436 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11437 /*consume_paren=*/true);
11439 if (keyword
== RID_IF
)
11442 unsigned char in_statement
;
11444 /* Add the condition. */
11445 condition
= finish_if_stmt_cond (condition
, statement
);
11447 if (warn_duplicated_cond
)
11448 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11451 /* Parse the then-clause. */
11452 in_statement
= parser
->in_statement
;
11453 parser
->in_statement
|= IN_IF_STMT
;
11455 /* Outside a template, the non-selected branch of a constexpr
11456 if is a 'discarded statement', i.e. unevaluated. */
11457 bool was_discarded
= in_discarded_stmt
;
11458 bool discard_then
= (cx
&& !processing_template_decl
11459 && integer_zerop (condition
));
11462 in_discarded_stmt
= true;
11463 ++c_inhibit_evaluation_warnings
;
11466 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11469 parser
->in_statement
= in_statement
;
11471 finish_then_clause (statement
);
11475 THEN_CLAUSE (statement
) = NULL_TREE
;
11476 in_discarded_stmt
= was_discarded
;
11477 --c_inhibit_evaluation_warnings
;
11480 /* If the next token is `else', parse the else-clause. */
11481 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11484 bool discard_else
= (cx
&& !processing_template_decl
11485 && integer_nonzerop (condition
));
11488 in_discarded_stmt
= true;
11489 ++c_inhibit_evaluation_warnings
;
11493 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11494 /* Consume the `else' keyword. */
11495 cp_lexer_consume_token (parser
->lexer
);
11496 if (warn_duplicated_cond
)
11498 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11502 /* We've got "if (COND) else if (COND2)". Start
11503 the condition chain and add COND as the first
11505 chain
= new vec
<tree
> ();
11506 if (!CONSTANT_CLASS_P (condition
)
11507 && !TREE_SIDE_EFFECTS (condition
))
11509 /* Wrap it in a NOP_EXPR so that we can set the
11510 location of the condition. */
11511 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11513 SET_EXPR_LOCATION (e
, token
->location
);
11514 chain
->safe_push (e
);
11517 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11520 /* This is if-else without subsequent if. Zap the
11521 condition chain; we would have already warned at
11527 begin_else_clause (statement
);
11528 /* Parse the else-clause. */
11529 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11530 guard_tinfo
, chain
);
11532 finish_else_clause (statement
);
11534 /* If we are currently parsing a then-clause, then
11535 IF_P will not be NULL. We set it to true to
11536 indicate that this if statement has an else clause.
11537 This may trigger the Wparentheses warning below
11538 when we get back up to the parent if statement. */
11544 ELSE_CLAUSE (statement
) = NULL_TREE
;
11545 in_discarded_stmt
= was_discarded
;
11546 --c_inhibit_evaluation_warnings
;
11551 /* This if statement does not have an else clause. If
11552 NESTED_IF is true, then the then-clause has an if
11553 statement which does have an else clause. We warn
11554 about the potential ambiguity. */
11556 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11557 "suggest explicit braces to avoid ambiguous"
11559 if (warn_duplicated_cond
)
11561 /* We don't need the condition chain anymore. */
11567 /* Now we're all done with the if-statement. */
11568 finish_if_stmt (statement
);
11572 bool in_switch_statement_p
;
11573 unsigned char in_statement
;
11575 /* Add the condition. */
11576 finish_switch_cond (condition
, statement
);
11578 /* Parse the body of the switch-statement. */
11579 in_switch_statement_p
= parser
->in_switch_statement_p
;
11580 in_statement
= parser
->in_statement
;
11581 parser
->in_switch_statement_p
= true;
11582 parser
->in_statement
|= IN_SWITCH_STMT
;
11583 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11585 parser
->in_switch_statement_p
= in_switch_statement_p
;
11586 parser
->in_statement
= in_statement
;
11588 /* Now we're all done with the switch-statement. */
11589 finish_switch_stmt (statement
);
11597 cp_parser_error (parser
, "expected selection-statement");
11598 return error_mark_node
;
11602 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11603 If we have seen at least one decl-specifier, and the next token
11604 is not a parenthesis, then we must be looking at a declaration.
11605 (After "int (" we might be looking at a functional cast.) */
11608 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11609 bool any_specifiers_p
)
11611 if (any_specifiers_p
11612 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11613 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11614 && !cp_parser_error_occurred (parser
))
11615 cp_parser_commit_to_tentative_parse (parser
);
11618 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11619 The declarator shall not specify a function or an array. Returns
11620 TRUE if the declarator is valid, FALSE otherwise. */
11623 cp_parser_check_condition_declarator (cp_parser
* parser
,
11624 cp_declarator
*declarator
,
11627 if (declarator
== cp_error_declarator
11628 || function_declarator_p (declarator
)
11629 || declarator
->kind
== cdk_array
)
11631 if (declarator
== cp_error_declarator
)
11632 /* Already complained. */;
11633 else if (declarator
->kind
== cdk_array
)
11634 error_at (loc
, "condition declares an array");
11636 error_at (loc
, "condition declares a function");
11637 if (parser
->fully_implicit_function_template_p
)
11638 abort_fully_implicit_template (parser
);
11639 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11640 /*or_comma=*/false,
11641 /*consume_paren=*/false);
11648 /* Parse a condition.
11652 type-specifier-seq declarator = initializer-clause
11653 type-specifier-seq declarator braced-init-list
11658 type-specifier-seq declarator asm-specification [opt]
11659 attributes [opt] = assignment-expression
11661 Returns the expression that should be tested. */
11664 cp_parser_condition (cp_parser
* parser
)
11666 cp_decl_specifier_seq type_specifiers
;
11667 const char *saved_message
;
11668 int declares_class_or_enum
;
11670 /* Try the declaration first. */
11671 cp_parser_parse_tentatively (parser
);
11672 /* New types are not allowed in the type-specifier-seq for a
11674 saved_message
= parser
->type_definition_forbidden_message
;
11675 parser
->type_definition_forbidden_message
11676 = G_("types may not be defined in conditions");
11677 /* Parse the type-specifier-seq. */
11678 cp_parser_decl_specifier_seq (parser
,
11679 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11681 &declares_class_or_enum
);
11682 /* Restore the saved message. */
11683 parser
->type_definition_forbidden_message
= saved_message
;
11685 cp_parser_maybe_commit_to_declaration (parser
,
11686 type_specifiers
.any_specifiers_p
);
11688 /* If all is well, we might be looking at a declaration. */
11689 if (!cp_parser_error_occurred (parser
))
11692 tree asm_specification
;
11694 cp_declarator
*declarator
;
11695 tree initializer
= NULL_TREE
;
11696 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11698 /* Parse the declarator. */
11699 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11700 /*ctor_dtor_or_conv_p=*/NULL
,
11701 /*parenthesized_p=*/NULL
,
11702 /*member_p=*/false,
11703 /*friend_p=*/false);
11704 /* Parse the attributes. */
11705 attributes
= cp_parser_attributes_opt (parser
);
11706 /* Parse the asm-specification. */
11707 asm_specification
= cp_parser_asm_specification_opt (parser
);
11708 /* If the next token is not an `=' or '{', then we might still be
11709 looking at an expression. For example:
11713 looks like a decl-specifier-seq and a declarator -- but then
11714 there is no `=', so this is an expression. */
11715 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11716 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11717 cp_parser_simulate_error (parser
);
11719 /* If we did see an `=' or '{', then we are looking at a declaration
11721 if (cp_parser_parse_definitely (parser
))
11724 bool non_constant_p
;
11725 int flags
= LOOKUP_ONLYCONVERTING
;
11727 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11728 return error_mark_node
;
11730 /* Create the declaration. */
11731 decl
= start_decl (declarator
, &type_specifiers
,
11732 /*initialized_p=*/true,
11733 attributes
, /*prefix_attributes=*/NULL_TREE
,
11736 /* Parse the initializer. */
11737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11739 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11740 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11743 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11745 /* Consume the `='. */
11746 cp_lexer_consume_token (parser
->lexer
);
11747 initializer
= cp_parser_initializer_clause (parser
,
11752 cp_parser_error (parser
, "expected initializer");
11753 initializer
= error_mark_node
;
11755 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11756 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11758 /* Process the initializer. */
11759 cp_finish_decl (decl
,
11760 initializer
, !non_constant_p
,
11765 pop_scope (pushed_scope
);
11767 return convert_from_reference (decl
);
11770 /* If we didn't even get past the declarator successfully, we are
11771 definitely not looking at a declaration. */
11773 cp_parser_abort_tentative_parse (parser
);
11775 /* Otherwise, we are looking at an expression. */
11776 return cp_parser_expression (parser
);
11779 /* Parses a for-statement or range-for-statement until the closing ')',
11783 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11785 tree init
, scope
, decl
;
11788 /* Begin the for-statement. */
11789 scope
= begin_for_scope (&init
);
11791 /* Parse the initialization. */
11792 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11795 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11797 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11801 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11802 unsigned short unroll
)
11804 /* Normal for loop */
11805 tree condition
= NULL_TREE
;
11806 tree expression
= NULL_TREE
;
11809 stmt
= begin_for_stmt (scope
, init
);
11810 /* The init-statement has already been parsed in
11811 cp_parser_init_statement, so no work is needed here. */
11812 finish_init_stmt (stmt
);
11814 /* If there's a condition, process it. */
11815 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11816 condition
= cp_parser_condition (parser
);
11819 cp_parser_error (parser
, "missing loop condition in loop with "
11820 "%<GCC ivdep%> pragma");
11821 condition
= error_mark_node
;
11825 cp_parser_error (parser
, "missing loop condition in loop with "
11826 "%<GCC unroll%> pragma");
11827 condition
= error_mark_node
;
11829 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11830 /* Look for the `;'. */
11831 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11833 /* If there's an expression, process it. */
11834 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11835 expression
= cp_parser_expression (parser
);
11836 finish_for_expr (expression
, stmt
);
11841 /* Tries to parse a range-based for-statement:
11844 decl-specifier-seq declarator : expression
11846 The decl-specifier-seq declarator and the `:' are already parsed by
11847 cp_parser_init_statement. If processing_template_decl it returns a
11848 newly created RANGE_FOR_STMT; if not, it is converted to a
11849 regular FOR_STMT. */
11852 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11853 bool ivdep
, unsigned short unroll
)
11855 tree stmt
, range_expr
;
11856 auto_vec
<cxx_binding
*, 16> bindings
;
11857 auto_vec
<tree
, 16> names
;
11858 tree decomp_first_name
= NULL_TREE
;
11859 unsigned int decomp_cnt
= 0;
11861 /* Get the range declaration momentarily out of the way so that
11862 the range expression doesn't clash with it. */
11863 if (range_decl
!= error_mark_node
)
11865 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11867 tree v
= DECL_VALUE_EXPR (range_decl
);
11868 /* For decomposition declaration get all of the corresponding
11869 declarations out of the way. */
11870 if (TREE_CODE (v
) == ARRAY_REF
11871 && VAR_P (TREE_OPERAND (v
, 0))
11872 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11874 tree d
= range_decl
;
11875 range_decl
= TREE_OPERAND (v
, 0);
11876 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11877 decomp_first_name
= d
;
11878 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11880 tree name
= DECL_NAME (d
);
11881 names
.safe_push (name
);
11882 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11883 IDENTIFIER_BINDING (name
)
11884 = IDENTIFIER_BINDING (name
)->previous
;
11888 if (names
.is_empty ())
11890 tree name
= DECL_NAME (range_decl
);
11891 names
.safe_push (name
);
11892 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11893 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11899 bool expr_non_constant_p
;
11900 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11903 range_expr
= cp_parser_expression (parser
);
11905 /* Put the range declaration(s) back into scope. */
11906 for (unsigned int i
= 0; i
< names
.length (); i
++)
11908 cxx_binding
*binding
= bindings
[i
];
11909 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11910 IDENTIFIER_BINDING (names
[i
]) = binding
;
11913 /* If in template, STMT is converted to a normal for-statement
11914 at instantiation. If not, it is done just ahead. */
11915 if (processing_template_decl
)
11917 if (check_for_bare_parameter_packs (range_expr
))
11918 range_expr
= error_mark_node
;
11919 stmt
= begin_range_for_stmt (scope
, init
);
11921 RANGE_FOR_IVDEP (stmt
) = 1;
11923 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11924 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11925 if (!type_dependent_expression_p (range_expr
)
11926 /* do_auto_deduction doesn't mess with template init-lists. */
11927 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11928 do_range_for_auto_deduction (range_decl
, range_expr
);
11932 stmt
= begin_for_stmt (scope
, init
);
11933 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11934 decomp_first_name
, decomp_cnt
, ivdep
,
11940 /* Subroutine of cp_convert_range_for: given the initializer expression,
11941 builds up the range temporary. */
11944 build_range_temp (tree range_expr
)
11946 tree range_type
, range_temp
;
11948 /* Find out the type deduced by the declaration
11949 `auto &&__range = range_expr'. */
11950 range_type
= cp_build_reference_type (make_auto (), true);
11951 range_type
= do_auto_deduction (range_type
, range_expr
,
11952 type_uses_auto (range_type
));
11954 /* Create the __range variable. */
11955 range_temp
= build_decl (input_location
, VAR_DECL
,
11956 get_identifier ("__for_range"), range_type
);
11957 TREE_USED (range_temp
) = 1;
11958 DECL_ARTIFICIAL (range_temp
) = 1;
11963 /* Used by cp_parser_range_for in template context: we aren't going to
11964 do a full conversion yet, but we still need to resolve auto in the
11965 type of the for-range-declaration if present. This is basically
11966 a shortcut version of cp_convert_range_for. */
11969 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11971 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11974 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11975 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11976 iter_type
= (cp_parser_perform_range_for_lookup
11977 (range_temp
, &begin_dummy
, &end_dummy
));
11980 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11982 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11984 tf_warning_or_error
);
11985 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11986 iter_decl
, auto_node
);
11991 /* Converts a range-based for-statement into a normal
11992 for-statement, as per the definition.
11994 for (RANGE_DECL : RANGE_EXPR)
11997 should be equivalent to:
12000 auto &&__range = RANGE_EXPR;
12001 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12005 RANGE_DECL = *__begin;
12010 If RANGE_EXPR is an array:
12011 BEGIN_EXPR = __range
12012 END_EXPR = __range + ARRAY_SIZE(__range)
12013 Else if RANGE_EXPR has a member 'begin' or 'end':
12014 BEGIN_EXPR = __range.begin()
12015 END_EXPR = __range.end()
12017 BEGIN_EXPR = begin(__range)
12018 END_EXPR = end(__range);
12020 If __range has a member 'begin' but not 'end', or vice versa, we must
12021 still use the second alternative (it will surely fail, however).
12022 When calling begin()/end() in the third alternative we must use
12023 argument dependent lookup, but always considering 'std' as an associated
12027 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12028 tree decomp_first_name
, unsigned int decomp_cnt
,
12029 bool ivdep
, unsigned short unroll
)
12032 tree iter_type
, begin_expr
, end_expr
;
12033 tree condition
, expression
;
12035 range_expr
= mark_lvalue_use (range_expr
);
12037 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12038 /* If an error happened previously do nothing or else a lot of
12039 unhelpful errors would be issued. */
12040 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12045 if (VAR_P (range_expr
)
12046 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12047 /* Can't bind a reference to an array of runtime bound. */
12048 range_temp
= range_expr
;
12051 range_temp
= build_range_temp (range_expr
);
12052 pushdecl (range_temp
);
12053 cp_finish_decl (range_temp
, range_expr
,
12054 /*is_constant_init*/false, NULL_TREE
,
12055 LOOKUP_ONLYCONVERTING
);
12056 range_temp
= convert_from_reference (range_temp
);
12058 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12059 &begin_expr
, &end_expr
);
12062 /* The new for initialization statement. */
12063 begin
= build_decl (input_location
, VAR_DECL
,
12064 get_identifier ("__for_begin"), iter_type
);
12065 TREE_USED (begin
) = 1;
12066 DECL_ARTIFICIAL (begin
) = 1;
12068 cp_finish_decl (begin
, begin_expr
,
12069 /*is_constant_init*/false, NULL_TREE
,
12070 LOOKUP_ONLYCONVERTING
);
12072 if (cxx_dialect
>= cxx17
)
12073 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12074 end
= build_decl (input_location
, VAR_DECL
,
12075 get_identifier ("__for_end"), iter_type
);
12076 TREE_USED (end
) = 1;
12077 DECL_ARTIFICIAL (end
) = 1;
12079 cp_finish_decl (end
, end_expr
,
12080 /*is_constant_init*/false, NULL_TREE
,
12081 LOOKUP_ONLYCONVERTING
);
12083 finish_init_stmt (statement
);
12085 /* The new for condition. */
12086 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12089 NULL
, tf_warning_or_error
);
12090 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12092 /* The new increment expression. */
12093 expression
= finish_unary_op_expr (input_location
,
12094 PREINCREMENT_EXPR
, begin
,
12095 tf_warning_or_error
);
12096 finish_for_expr (expression
, statement
);
12098 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12099 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12101 /* The declaration is initialized with *__begin inside the loop body. */
12102 cp_finish_decl (range_decl
,
12103 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12104 tf_warning_or_error
),
12105 /*is_constant_init*/false, NULL_TREE
,
12106 LOOKUP_ONLYCONVERTING
);
12107 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12108 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12113 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12114 We need to solve both at the same time because the method used
12115 depends on the existence of members begin or end.
12116 Returns the type deduced for the iterator expression. */
12119 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12121 if (error_operand_p (range
))
12123 *begin
= *end
= error_mark_node
;
12124 return error_mark_node
;
12127 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12129 error ("range-based %<for%> expression of type %qT "
12130 "has incomplete type", TREE_TYPE (range
));
12131 *begin
= *end
= error_mark_node
;
12132 return error_mark_node
;
12134 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12136 /* If RANGE is an array, we will use pointer arithmetic. */
12137 *begin
= decay_conversion (range
, tf_warning_or_error
);
12138 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12140 array_type_nelts_top (TREE_TYPE (range
)),
12142 return TREE_TYPE (*begin
);
12146 /* If it is not an array, we must do a bit of magic. */
12147 tree id_begin
, id_end
;
12148 tree member_begin
, member_end
;
12150 *begin
= *end
= error_mark_node
;
12152 id_begin
= get_identifier ("begin");
12153 id_end
= get_identifier ("end");
12154 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12155 /*protect=*/2, /*want_type=*/false,
12156 tf_warning_or_error
);
12157 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12158 /*protect=*/2, /*want_type=*/false,
12159 tf_warning_or_error
);
12161 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12163 /* Use the member functions. */
12164 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12165 *end
= cp_parser_range_for_member_function (range
, id_end
);
12169 /* Use global functions with ADL. */
12170 vec
<tree
, va_gc
> *vec
;
12171 vec
= make_tree_vector ();
12173 vec_safe_push (vec
, range
);
12175 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12176 tf_warning_or_error
);
12177 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12178 tf_warning_or_error
);
12179 member_end
= perform_koenig_lookup (id_end
, vec
,
12180 tf_warning_or_error
);
12181 *end
= finish_call_expr (member_end
, &vec
, false, true,
12182 tf_warning_or_error
);
12184 release_tree_vector (vec
);
12187 /* Last common checks. */
12188 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12190 /* If one of the expressions is an error do no more checks. */
12191 *begin
= *end
= error_mark_node
;
12192 return error_mark_node
;
12194 else if (type_dependent_expression_p (*begin
)
12195 || type_dependent_expression_p (*end
))
12196 /* Can happen, when, eg, in a template context, Koenig lookup
12197 can't resolve begin/end (c++/58503). */
12201 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12202 /* The unqualified type of the __begin and __end temporaries should
12203 be the same, as required by the multiple auto declaration. */
12204 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12206 if (cxx_dialect
>= cxx17
12207 && (build_x_binary_op (input_location
, NE_EXPR
,
12208 *begin
, ERROR_MARK
,
12211 != error_mark_node
))
12212 /* P0184R0 allows __begin and __end to have different types,
12213 but make sure they are comparable so we can give a better
12216 error ("inconsistent begin/end types in range-based %<for%> "
12217 "statement: %qT and %qT",
12218 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12225 /* Helper function for cp_parser_perform_range_for_lookup.
12226 Builds a tree for RANGE.IDENTIFIER(). */
12229 cp_parser_range_for_member_function (tree range
, tree identifier
)
12232 vec
<tree
, va_gc
> *vec
;
12234 member
= finish_class_member_access_expr (range
, identifier
,
12235 false, tf_warning_or_error
);
12236 if (member
== error_mark_node
)
12237 return error_mark_node
;
12239 vec
= make_tree_vector ();
12240 res
= finish_call_expr (member
, &vec
,
12241 /*disallow_virtual=*/false,
12242 /*koenig_p=*/false,
12243 tf_warning_or_error
);
12244 release_tree_vector (vec
);
12248 /* Parse an iteration-statement.
12250 iteration-statement:
12251 while ( condition ) statement
12252 do statement while ( expression ) ;
12253 for ( init-statement condition [opt] ; expression [opt] )
12256 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12259 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12260 unsigned short unroll
)
12265 unsigned char in_statement
;
12266 token_indent_info guard_tinfo
;
12268 /* Peek at the next token. */
12269 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12271 return error_mark_node
;
12273 guard_tinfo
= get_token_indent_info (token
);
12275 /* Remember whether or not we are already within an iteration
12277 in_statement
= parser
->in_statement
;
12279 /* See what kind of keyword it is. */
12280 keyword
= token
->keyword
;
12287 /* Begin the while-statement. */
12288 statement
= begin_while_stmt ();
12289 /* Look for the `('. */
12290 matching_parens parens
;
12291 parens
.require_open (parser
);
12292 /* Parse the condition. */
12293 condition
= cp_parser_condition (parser
);
12294 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12295 /* Look for the `)'. */
12296 parens
.require_close (parser
);
12297 /* Parse the dependent statement. */
12298 parser
->in_statement
= IN_ITERATION_STMT
;
12299 bool prev
= note_iteration_stmt_body_start ();
12300 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12301 note_iteration_stmt_body_end (prev
);
12302 parser
->in_statement
= in_statement
;
12303 /* We're done with the while-statement. */
12304 finish_while_stmt (statement
);
12312 /* Begin the do-statement. */
12313 statement
= begin_do_stmt ();
12314 /* Parse the body of the do-statement. */
12315 parser
->in_statement
= IN_ITERATION_STMT
;
12316 bool prev
= note_iteration_stmt_body_start ();
12317 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12318 note_iteration_stmt_body_end (prev
);
12319 parser
->in_statement
= in_statement
;
12320 finish_do_body (statement
);
12321 /* Look for the `while' keyword. */
12322 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12323 /* Look for the `('. */
12324 matching_parens parens
;
12325 parens
.require_open (parser
);
12326 /* Parse the expression. */
12327 expression
= cp_parser_expression (parser
);
12328 /* We're done with the do-statement. */
12329 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12330 /* Look for the `)'. */
12331 parens
.require_close (parser
);
12332 /* Look for the `;'. */
12333 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12339 /* Look for the `('. */
12340 matching_parens parens
;
12341 parens
.require_open (parser
);
12343 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12345 /* Look for the `)'. */
12346 parens
.require_close (parser
);
12348 /* Parse the body of the for-statement. */
12349 parser
->in_statement
= IN_ITERATION_STMT
;
12350 bool prev
= note_iteration_stmt_body_start ();
12351 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12352 note_iteration_stmt_body_end (prev
);
12353 parser
->in_statement
= in_statement
;
12355 /* We're done with the for-statement. */
12356 finish_for_stmt (statement
);
12361 cp_parser_error (parser
, "expected iteration-statement");
12362 statement
= error_mark_node
;
12369 /* Parse a init-statement or the declarator of a range-based-for.
12370 Returns true if a range-based-for declaration is seen.
12373 expression-statement
12374 simple-declaration */
12377 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12379 /* If the next token is a `;', then we have an empty
12380 expression-statement. Grammatically, this is also a
12381 simple-declaration, but an invalid one, because it does not
12382 declare anything. Therefore, if we did not handle this case
12383 specially, we would issue an error message about an invalid
12385 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12387 bool is_range_for
= false;
12388 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12390 /* Try to parse the init-statement. */
12391 if (cp_parser_range_based_for_with_init_p (parser
))
12394 cp_parser_parse_tentatively (parser
);
12395 /* Parse the declaration. */
12396 cp_parser_simple_declaration (parser
,
12397 /*function_definition_allowed_p=*/false,
12399 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12400 if (!cp_parser_parse_definitely (parser
))
12401 /* That didn't work, try to parse it as an expression-statement. */
12402 cp_parser_expression_statement (parser
, NULL_TREE
);
12404 if (cxx_dialect
< cxx2a
)
12406 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12407 "range-based %<for%> loops with initializer only "
12408 "available with -std=c++2a or -std=gnu++2a");
12409 *decl
= error_mark_node
;
12413 /* A colon is used in range-based for. */
12414 parser
->colon_corrects_to_scope_p
= false;
12416 /* We're going to speculatively look for a declaration, falling back
12417 to an expression, if necessary. */
12418 cp_parser_parse_tentatively (parser
);
12419 /* Parse the declaration. */
12420 cp_parser_simple_declaration (parser
,
12421 /*function_definition_allowed_p=*/false,
12423 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12426 /* It is a range-for, consume the ':'. */
12427 cp_lexer_consume_token (parser
->lexer
);
12428 is_range_for
= true;
12429 if (cxx_dialect
< cxx11
)
12430 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12431 "range-based %<for%> loops only available with "
12432 "-std=c++11 or -std=gnu++11");
12435 /* The ';' is not consumed yet because we told
12436 cp_parser_simple_declaration not to. */
12437 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12439 if (cp_parser_parse_definitely (parser
))
12440 return is_range_for
;
12441 /* If the tentative parse failed, then we shall need to look for an
12442 expression-statement. */
12444 /* If we are here, it is an expression-statement. */
12445 cp_parser_expression_statement (parser
, NULL_TREE
);
12449 /* Parse a jump-statement.
12454 return expression [opt] ;
12455 return braced-init-list ;
12461 goto * expression ;
12463 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12466 cp_parser_jump_statement (cp_parser
* parser
)
12468 tree statement
= error_mark_node
;
12471 unsigned char in_statement
;
12473 /* Peek at the next token. */
12474 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12476 return error_mark_node
;
12478 /* See what kind of keyword it is. */
12479 keyword
= token
->keyword
;
12483 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12484 switch (in_statement
)
12487 error_at (token
->location
, "break statement not within loop or switch");
12490 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12491 || in_statement
== IN_ITERATION_STMT
);
12492 statement
= finish_break_stmt ();
12493 if (in_statement
== IN_ITERATION_STMT
)
12494 break_maybe_infinite_loop ();
12497 error_at (token
->location
, "invalid exit from OpenMP structured block");
12500 error_at (token
->location
, "break statement used with OpenMP for loop");
12503 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12507 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12510 error_at (token
->location
, "continue statement not within a loop");
12512 /* Fall through. */
12513 case IN_ITERATION_STMT
:
12515 statement
= finish_continue_stmt ();
12518 error_at (token
->location
, "invalid exit from OpenMP structured block");
12521 gcc_unreachable ();
12523 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12529 bool expr_non_constant_p
;
12531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12533 cp_lexer_set_source_position (parser
->lexer
);
12534 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12535 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12537 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12538 expr
= cp_parser_expression (parser
);
12540 /* If the next token is a `;', then there is no
12543 /* Build the return-statement. */
12544 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12545 /* Don't deduce from a discarded return statement. */;
12547 statement
= finish_return_stmt (expr
);
12548 /* Look for the final `;'. */
12549 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12554 if (parser
->in_function_body
12555 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12557 error ("%<goto%> in %<constexpr%> function");
12558 cp_function_chain
->invalid_constexpr
= true;
12561 /* Create the goto-statement. */
12562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12564 /* Issue a warning about this use of a GNU extension. */
12565 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12566 /* Consume the '*' token. */
12567 cp_lexer_consume_token (parser
->lexer
);
12568 /* Parse the dependent expression. */
12569 finish_goto_stmt (cp_parser_expression (parser
));
12572 finish_goto_stmt (cp_parser_identifier (parser
));
12573 /* Look for the final `;'. */
12574 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12578 cp_parser_error (parser
, "expected jump-statement");
12585 /* Parse a declaration-statement.
12587 declaration-statement:
12588 block-declaration */
12591 cp_parser_declaration_statement (cp_parser
* parser
)
12595 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12596 p
= obstack_alloc (&declarator_obstack
, 0);
12598 /* Parse the block-declaration. */
12599 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12601 /* Free any declarators allocated. */
12602 obstack_free (&declarator_obstack
, p
);
12605 /* Some dependent statements (like `if (cond) statement'), are
12606 implicitly in their own scope. In other words, if the statement is
12607 a single statement (as opposed to a compound-statement), it is
12608 none-the-less treated as if it were enclosed in braces. Any
12609 declarations appearing in the dependent statement are out of scope
12610 after control passes that point. This function parses a statement,
12611 but ensures that is in its own scope, even if it is not a
12612 compound-statement.
12614 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12615 is a (possibly labeled) if statement which is not enclosed in
12616 braces and has an else clause. This is used to implement
12619 CHAIN is a vector of if-else-if conditions. This is used to implement
12622 Returns the new statement. */
12625 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12626 const token_indent_info
&guard_tinfo
,
12630 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12631 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12638 /* Mark if () ; with a special NOP_EXPR. */
12639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12641 cp_lexer_consume_token (parser
->lexer
);
12642 statement
= add_stmt (build_empty_stmt (body_loc
));
12644 if (guard_tinfo
.keyword
== RID_IF
12645 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12646 warning_at (body_loc
, OPT_Wempty_body
,
12647 "suggest braces around empty body in an %<if%> statement");
12648 else if (guard_tinfo
.keyword
== RID_ELSE
)
12649 warning_at (body_loc
, OPT_Wempty_body
,
12650 "suggest braces around empty body in an %<else%> statement");
12652 /* if a compound is opened, we simply parse the statement directly. */
12653 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12654 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12655 /* If the token is not a `{', then we must take special action. */
12658 /* Create a compound-statement. */
12659 statement
= begin_compound_stmt (0);
12660 /* Parse the dependent-statement. */
12661 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12662 &body_loc_after_labels
);
12663 /* Finish the dummy compound-statement. */
12664 finish_compound_stmt (statement
);
12667 token_indent_info next_tinfo
12668 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12669 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12671 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12672 && next_tinfo
.type
!= CPP_SEMICOLON
)
12673 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12674 guard_tinfo
.location
, guard_tinfo
.keyword
);
12676 /* Return the statement. */
12680 /* For some dependent statements (like `while (cond) statement'), we
12681 have already created a scope. Therefore, even if the dependent
12682 statement is a compound-statement, we do not want to create another
12686 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12687 const token_indent_info
&guard_tinfo
)
12689 /* If the token is a `{', then we must take special action. */
12690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12692 token_indent_info body_tinfo
12693 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12694 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12696 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12697 &loc_after_labels
);
12698 token_indent_info next_tinfo
12699 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12700 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12702 if (loc_after_labels
!= UNKNOWN_LOCATION
12703 && next_tinfo
.type
!= CPP_SEMICOLON
)
12704 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12705 guard_tinfo
.location
,
12706 guard_tinfo
.keyword
);
12710 /* Avoid calling cp_parser_compound_statement, so that we
12711 don't create a new scope. Do everything else by hand. */
12712 matching_braces braces
;
12713 braces
.require_open (parser
);
12714 /* If the next keyword is `__label__' we have a label declaration. */
12715 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12716 cp_parser_label_declaration (parser
);
12717 /* Parse an (optional) statement-seq. */
12718 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12719 braces
.require_close (parser
);
12723 /* Declarations [gram.dcl.dcl] */
12725 /* Parse an optional declaration-sequence.
12729 declaration-seq declaration */
12732 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12738 token
= cp_lexer_peek_token (parser
->lexer
);
12740 if (token
->type
== CPP_CLOSE_BRACE
12741 || token
->type
== CPP_EOF
12742 || token
->type
== CPP_PRAGMA_EOL
)
12745 if (token
->type
== CPP_SEMICOLON
)
12747 /* A declaration consisting of a single semicolon is
12748 invalid. Allow it unless we're being pedantic. */
12749 cp_lexer_consume_token (parser
->lexer
);
12750 if (!in_system_header_at (input_location
))
12751 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12755 /* If we're entering or exiting a region that's implicitly
12756 extern "C", modify the lang context appropriately. */
12757 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12759 push_lang_context (lang_name_c
);
12760 parser
->implicit_extern_c
= true;
12762 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12764 pop_lang_context ();
12765 parser
->implicit_extern_c
= false;
12768 if (token
->type
== CPP_PRAGMA
)
12770 /* A top-level declaration can consist solely of a #pragma.
12771 A nested declaration cannot, so this is done here and not
12772 in cp_parser_declaration. (A #pragma at block scope is
12773 handled in cp_parser_statement.) */
12774 cp_parser_pragma (parser
, pragma_external
, NULL
);
12778 /* Parse the declaration itself. */
12779 cp_parser_declaration (parser
);
12783 /* Parse a declaration.
12787 function-definition
12788 template-declaration
12789 explicit-instantiation
12790 explicit-specialization
12791 linkage-specification
12792 namespace-definition
12800 __extension__ declaration */
12803 cp_parser_declaration (cp_parser
* parser
)
12807 int saved_pedantic
;
12809 tree attributes
= NULL_TREE
;
12811 /* Check for the `__extension__' keyword. */
12812 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12814 /* Parse the qualified declaration. */
12815 cp_parser_declaration (parser
);
12816 /* Restore the PEDANTIC flag. */
12817 pedantic
= saved_pedantic
;
12822 /* Try to figure out what kind of declaration is present. */
12823 token1
= *cp_lexer_peek_token (parser
->lexer
);
12825 if (token1
.type
!= CPP_EOF
)
12826 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12829 token2
.type
= CPP_EOF
;
12830 token2
.keyword
= RID_MAX
;
12833 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12834 p
= obstack_alloc (&declarator_obstack
, 0);
12836 /* If the next token is `extern' and the following token is a string
12837 literal, then we have a linkage specification. */
12838 if (token1
.keyword
== RID_EXTERN
12839 && cp_parser_is_pure_string_literal (&token2
))
12840 cp_parser_linkage_specification (parser
);
12841 /* If the next token is `template', then we have either a template
12842 declaration, an explicit instantiation, or an explicit
12844 else if (token1
.keyword
== RID_TEMPLATE
)
12846 /* `template <>' indicates a template specialization. */
12847 if (token2
.type
== CPP_LESS
12848 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12849 cp_parser_explicit_specialization (parser
);
12850 /* `template <' indicates a template declaration. */
12851 else if (token2
.type
== CPP_LESS
)
12852 cp_parser_template_declaration (parser
, /*member_p=*/false);
12853 /* Anything else must be an explicit instantiation. */
12855 cp_parser_explicit_instantiation (parser
);
12857 /* If the next token is `export', then we have a template
12859 else if (token1
.keyword
== RID_EXPORT
)
12860 cp_parser_template_declaration (parser
, /*member_p=*/false);
12861 /* If the next token is `extern', 'static' or 'inline' and the one
12862 after that is `template', we have a GNU extended explicit
12863 instantiation directive. */
12864 else if (cp_parser_allow_gnu_extensions_p (parser
)
12865 && (token1
.keyword
== RID_EXTERN
12866 || token1
.keyword
== RID_STATIC
12867 || token1
.keyword
== RID_INLINE
)
12868 && token2
.keyword
== RID_TEMPLATE
)
12869 cp_parser_explicit_instantiation (parser
);
12870 /* If the next token is `namespace', check for a named or unnamed
12871 namespace definition. */
12872 else if (token1
.keyword
== RID_NAMESPACE
12873 && (/* A named namespace definition. */
12874 (token2
.type
== CPP_NAME
12875 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12877 || (token2
.type
== CPP_OPEN_SQUARE
12878 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12879 == CPP_OPEN_SQUARE
)
12880 /* An unnamed namespace definition. */
12881 || token2
.type
== CPP_OPEN_BRACE
12882 || token2
.keyword
== RID_ATTRIBUTE
))
12883 cp_parser_namespace_definition (parser
);
12884 /* An inline (associated) namespace definition. */
12885 else if (token1
.keyword
== RID_INLINE
12886 && token2
.keyword
== RID_NAMESPACE
)
12887 cp_parser_namespace_definition (parser
);
12888 /* Objective-C++ declaration/definition. */
12889 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12890 cp_parser_objc_declaration (parser
, NULL_TREE
);
12891 else if (c_dialect_objc ()
12892 && token1
.keyword
== RID_ATTRIBUTE
12893 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12894 cp_parser_objc_declaration (parser
, attributes
);
12895 /* At this point we may have a template declared by a concept
12897 else if (flag_concepts
12898 && cp_parser_template_declaration_after_export (parser
,
12899 /*member_p=*/false))
12902 /* Try to parse a block-declaration, or a function-definition. */
12903 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12905 /* Free any declarators allocated. */
12906 obstack_free (&declarator_obstack
, p
);
12909 /* Parse a block-declaration.
12914 namespace-alias-definition
12921 __extension__ block-declaration
12926 static_assert-declaration
12928 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12929 part of a declaration-statement. */
12932 cp_parser_block_declaration (cp_parser
*parser
,
12936 int saved_pedantic
;
12938 /* Check for the `__extension__' keyword. */
12939 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12941 /* Parse the qualified declaration. */
12942 cp_parser_block_declaration (parser
, statement_p
);
12943 /* Restore the PEDANTIC flag. */
12944 pedantic
= saved_pedantic
;
12949 /* Peek at the next token to figure out which kind of declaration is
12951 token1
= cp_lexer_peek_token (parser
->lexer
);
12953 /* If the next keyword is `asm', we have an asm-definition. */
12954 if (token1
->keyword
== RID_ASM
)
12957 cp_parser_commit_to_tentative_parse (parser
);
12958 cp_parser_asm_definition (parser
);
12960 /* If the next keyword is `namespace', we have a
12961 namespace-alias-definition. */
12962 else if (token1
->keyword
== RID_NAMESPACE
)
12963 cp_parser_namespace_alias_definition (parser
);
12964 /* If the next keyword is `using', we have a
12965 using-declaration, a using-directive, or an alias-declaration. */
12966 else if (token1
->keyword
== RID_USING
)
12971 cp_parser_commit_to_tentative_parse (parser
);
12972 /* If the token after `using' is `namespace', then we have a
12973 using-directive. */
12974 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12975 if (token2
->keyword
== RID_NAMESPACE
)
12976 cp_parser_using_directive (parser
);
12977 /* If the second token after 'using' is '=', then we have an
12978 alias-declaration. */
12979 else if (cxx_dialect
>= cxx11
12980 && token2
->type
== CPP_NAME
12981 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12982 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12983 cp_parser_alias_declaration (parser
);
12984 /* Otherwise, it's a using-declaration. */
12986 cp_parser_using_declaration (parser
,
12987 /*access_declaration_p=*/false);
12989 /* If the next keyword is `__label__' we have a misplaced label
12991 else if (token1
->keyword
== RID_LABEL
)
12993 cp_lexer_consume_token (parser
->lexer
);
12994 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
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
);
13000 /* If the next token is `static_assert' we have a static assertion. */
13001 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13002 cp_parser_static_assert (parser
, /*member_p=*/false);
13003 /* Anything else must be a simple-declaration. */
13005 cp_parser_simple_declaration (parser
, !statement_p
,
13006 /*maybe_range_for_decl*/NULL
);
13009 /* Parse a simple-declaration.
13011 simple-declaration:
13012 decl-specifier-seq [opt] init-declarator-list [opt] ;
13013 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13014 brace-or-equal-initializer ;
13016 init-declarator-list:
13018 init-declarator-list , init-declarator
13020 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13021 function-definition as a simple-declaration.
13023 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13024 parsed declaration if it is an uninitialized single declarator not followed
13025 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13026 if present, will not be consumed. */
13029 cp_parser_simple_declaration (cp_parser
* parser
,
13030 bool function_definition_allowed_p
,
13031 tree
*maybe_range_for_decl
)
13033 cp_decl_specifier_seq decl_specifiers
;
13034 int declares_class_or_enum
;
13035 bool saw_declarator
;
13036 location_t comma_loc
= UNKNOWN_LOCATION
;
13037 location_t init_loc
= UNKNOWN_LOCATION
;
13039 if (maybe_range_for_decl
)
13040 *maybe_range_for_decl
= NULL_TREE
;
13042 /* Defer access checks until we know what is being declared; the
13043 checks for names appearing in the decl-specifier-seq should be
13044 done as if we were in the scope of the thing being declared. */
13045 push_deferring_access_checks (dk_deferred
);
13047 /* Parse the decl-specifier-seq. We have to keep track of whether
13048 or not the decl-specifier-seq declares a named class or
13049 enumeration type, since that is the only case in which the
13050 init-declarator-list is allowed to be empty.
13054 In a simple-declaration, the optional init-declarator-list can be
13055 omitted only when declaring a class or enumeration, that is when
13056 the decl-specifier-seq contains either a class-specifier, an
13057 elaborated-type-specifier, or an enum-specifier. */
13058 cp_parser_decl_specifier_seq (parser
,
13059 CP_PARSER_FLAGS_OPTIONAL
,
13061 &declares_class_or_enum
);
13062 /* We no longer need to defer access checks. */
13063 stop_deferring_access_checks ();
13065 /* In a block scope, a valid declaration must always have a
13066 decl-specifier-seq. By not trying to parse declarators, we can
13067 resolve the declaration/expression ambiguity more quickly. */
13068 if (!function_definition_allowed_p
13069 && !decl_specifiers
.any_specifiers_p
)
13071 cp_parser_error (parser
, "expected declaration");
13075 /* If the next two tokens are both identifiers, the code is
13076 erroneous. The usual cause of this situation is code like:
13080 where "T" should name a type -- but does not. */
13081 if (!decl_specifiers
.any_type_specifiers_p
13082 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13084 /* If parsing tentatively, we should commit; we really are
13085 looking at a declaration. */
13086 cp_parser_commit_to_tentative_parse (parser
);
13091 cp_parser_maybe_commit_to_declaration (parser
,
13092 decl_specifiers
.any_specifiers_p
);
13094 /* Look for C++17 decomposition declaration. */
13095 for (size_t n
= 1; ; n
++)
13096 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13097 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13099 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13100 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13101 && decl_specifiers
.any_specifiers_p
)
13104 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13105 maybe_range_for_decl
,
13108 /* The next token should be either a `,' or a `;'. */
13109 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13110 /* If it's a `;', we are done. */
13111 if (token
->type
== CPP_SEMICOLON
)
13113 else if (maybe_range_for_decl
)
13115 if (*maybe_range_for_decl
== NULL_TREE
)
13116 *maybe_range_for_decl
= error_mark_node
;
13119 /* Anything else is an error. */
13122 /* If we have already issued an error message we don't need
13123 to issue another one. */
13124 if ((decl
!= error_mark_node
13125 && DECL_INITIAL (decl
) != error_mark_node
)
13126 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13127 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13128 /* Skip tokens until we reach the end of the statement. */
13129 cp_parser_skip_to_end_of_statement (parser
);
13130 /* If the next token is now a `;', consume it. */
13131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13132 cp_lexer_consume_token (parser
->lexer
);
13140 bool auto_specifier_p
;
13141 /* NULL_TREE if both variable and function declaration are allowed,
13142 error_mark_node if function declaration are not allowed and
13143 a FUNCTION_DECL that should be diagnosed if it is followed by
13144 variable declarations. */
13145 tree auto_function_declaration
;
13147 last_type
= NULL_TREE
;
13149 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13150 auto_function_declaration
= NULL_TREE
;
13152 /* Keep going until we hit the `;' at the end of the simple
13154 saw_declarator
= false;
13155 while (cp_lexer_next_token_is_not (parser
->lexer
,
13159 bool function_definition_p
;
13161 tree auto_result
= NULL_TREE
;
13163 if (saw_declarator
)
13165 /* If we are processing next declarator, comma is expected */
13166 token
= cp_lexer_peek_token (parser
->lexer
);
13167 gcc_assert (token
->type
== CPP_COMMA
);
13168 cp_lexer_consume_token (parser
->lexer
);
13169 if (maybe_range_for_decl
)
13171 *maybe_range_for_decl
= error_mark_node
;
13172 if (comma_loc
== UNKNOWN_LOCATION
)
13173 comma_loc
= token
->location
;
13177 saw_declarator
= true;
13179 /* Parse the init-declarator. */
13180 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13182 function_definition_allowed_p
,
13183 /*member_p=*/false,
13184 declares_class_or_enum
,
13185 &function_definition_p
,
13186 maybe_range_for_decl
,
13189 /* If an error occurred while parsing tentatively, exit quickly.
13190 (That usually happens when in the body of a function; each
13191 statement is treated as a declaration-statement until proven
13193 if (cp_parser_error_occurred (parser
))
13196 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13198 /* If the init-declarator-list contains more than one
13199 init-declarator, they shall all form declarations of
13201 if (auto_function_declaration
== NULL_TREE
)
13202 auto_function_declaration
13203 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13204 else if (TREE_CODE (decl
) == FUNCTION_DECL
13205 || auto_function_declaration
!= error_mark_node
)
13207 error_at (decl_specifiers
.locations
[ds_type_spec
],
13208 "non-variable %qD in declaration with more than one "
13209 "declarator with placeholder type",
13210 TREE_CODE (decl
) == FUNCTION_DECL
13211 ? decl
: auto_function_declaration
);
13212 auto_function_declaration
= error_mark_node
;
13217 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13220 && last_type
!= error_mark_node
13221 && !same_type_p (auto_result
, last_type
))
13223 /* If the list of declarators contains more than one declarator,
13224 the type of each declared variable is determined as described
13225 above. If the type deduced for the template parameter U is not
13226 the same in each deduction, the program is ill-formed. */
13227 error_at (decl_specifiers
.locations
[ds_type_spec
],
13228 "inconsistent deduction for %qT: %qT and then %qT",
13229 decl_specifiers
.type
, last_type
, auto_result
);
13230 last_type
= error_mark_node
;
13233 last_type
= auto_result
;
13236 /* Handle function definitions specially. */
13237 if (function_definition_p
)
13239 /* If the next token is a `,', then we are probably
13240 processing something like:
13244 which is erroneous. */
13245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13247 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13248 error_at (token
->location
,
13250 " declarations and function-definitions is forbidden");
13252 /* Otherwise, we're done with the list of declarators. */
13255 pop_deferring_access_checks ();
13259 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13260 *maybe_range_for_decl
= decl
;
13261 /* The next token should be either a `,' or a `;'. */
13262 token
= cp_lexer_peek_token (parser
->lexer
);
13263 /* If it's a `,', there are more declarators to come. */
13264 if (token
->type
== CPP_COMMA
)
13265 /* will be consumed next time around */;
13266 /* If it's a `;', we are done. */
13267 else if (token
->type
== CPP_SEMICOLON
)
13269 else if (maybe_range_for_decl
)
13271 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13272 permerror (decl_specifiers
.locations
[ds_type_spec
],
13273 "types may not be defined in a for-range-declaration");
13276 /* Anything else is an error. */
13279 /* If we have already issued an error message we don't need
13280 to issue another one. */
13281 if ((decl
!= error_mark_node
13282 && DECL_INITIAL (decl
) != error_mark_node
)
13283 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13284 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13285 /* Skip tokens until we reach the end of the statement. */
13286 cp_parser_skip_to_end_of_statement (parser
);
13287 /* If the next token is now a `;', consume it. */
13288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13289 cp_lexer_consume_token (parser
->lexer
);
13292 /* After the first time around, a function-definition is not
13293 allowed -- even if it was OK at first. For example:
13298 function_definition_allowed_p
= false;
13301 /* Issue an error message if no declarators are present, and the
13302 decl-specifier-seq does not itself declare a class or
13303 enumeration: [dcl.dcl]/3. */
13304 if (!saw_declarator
)
13306 if (cp_parser_declares_only_class_p (parser
))
13308 if (!declares_class_or_enum
13309 && decl_specifiers
.type
13310 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13311 /* Ensure an error is issued anyway when finish_decltype_type,
13312 called via cp_parser_decl_specifier_seq, returns a class or
13313 an enumeration (c++/51786). */
13314 decl_specifiers
.type
= NULL_TREE
;
13315 shadow_tag (&decl_specifiers
);
13317 /* Perform any deferred access checks. */
13318 perform_deferred_access_checks (tf_warning_or_error
);
13321 /* Consume the `;'. */
13323 if (!maybe_range_for_decl
)
13324 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13325 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13327 if (init_loc
!= UNKNOWN_LOCATION
)
13328 error_at (init_loc
, "initializer in range-based %<for%> loop");
13329 if (comma_loc
!= UNKNOWN_LOCATION
)
13330 error_at (comma_loc
,
13331 "multiple declarations in range-based %<for%> loop");
13335 pop_deferring_access_checks ();
13338 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13339 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13343 cp_parser_decomposition_declaration (cp_parser
*parser
,
13344 cp_decl_specifier_seq
*decl_specifiers
,
13345 tree
*maybe_range_for_decl
,
13346 location_t
*init_loc
)
13348 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13349 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13350 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13352 /* Parse the identifier-list. */
13353 auto_vec
<cp_expr
, 10> v
;
13354 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13357 cp_expr e
= cp_parser_identifier (parser
);
13358 if (e
.get_value () == error_mark_node
)
13361 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13363 cp_lexer_consume_token (parser
->lexer
);
13366 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13367 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13369 end_loc
= UNKNOWN_LOCATION
;
13370 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13373 cp_lexer_consume_token (parser
->lexer
);
13376 cp_parser_skip_to_end_of_statement (parser
);
13377 return error_mark_node
;
13381 if (cxx_dialect
< cxx17
)
13382 pedwarn (loc
, 0, "structured bindings only available with "
13383 "-std=c++17 or -std=gnu++17");
13386 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13387 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13388 declarator
->id_loc
= loc
;
13389 if (ref_qual
!= REF_QUAL_NONE
)
13390 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13391 ref_qual
== REF_QUAL_RVALUE
,
13393 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13394 NULL_TREE
, decl_specifiers
->attributes
,
13396 tree orig_decl
= decl
;
13400 cp_decl_specifier_seq decl_specs
;
13401 clear_decl_specs (&decl_specs
);
13402 decl_specs
.type
= make_auto ();
13404 FOR_EACH_VEC_ELT (v
, i
, e
)
13407 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13409 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13410 declarator
->id_loc
= e
.get_location ();
13411 tree elt_pushed_scope
;
13412 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13413 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13414 if (decl2
== error_mark_node
)
13415 decl
= error_mark_node
;
13416 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13418 /* Ensure we've diagnosed redeclaration if we aren't creating
13420 gcc_assert (errorcount
);
13421 decl
= error_mark_node
;
13425 if (elt_pushed_scope
)
13426 pop_scope (elt_pushed_scope
);
13431 error_at (loc
, "empty structured binding declaration");
13432 decl
= error_mark_node
;
13435 if (maybe_range_for_decl
== NULL
13436 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13438 bool non_constant_p
= false, is_direct_init
= false;
13439 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13440 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13442 if (initializer
== NULL_TREE
13443 || (TREE_CODE (initializer
) == TREE_LIST
13444 && TREE_CHAIN (initializer
))
13446 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13447 && CONSTRUCTOR_NELTS (initializer
) != 1))
13449 error_at (loc
, "invalid initializer for structured binding "
13451 initializer
= error_mark_node
;
13454 if (decl
!= error_mark_node
)
13456 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13457 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13458 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13459 cp_finish_decomp (decl
, prev
, v
.length ());
13462 else if (decl
!= error_mark_node
)
13464 *maybe_range_for_decl
= prev
;
13465 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13466 the underlying DECL. */
13467 cp_finish_decomp (decl
, prev
, v
.length ());
13471 pop_scope (pushed_scope
);
13473 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13475 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13476 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13482 /* Parse a decl-specifier-seq.
13484 decl-specifier-seq:
13485 decl-specifier-seq [opt] decl-specifier
13486 decl-specifier attribute-specifier-seq [opt] (C++11)
13489 storage-class-specifier
13500 Concepts Extension:
13505 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13507 The parser flags FLAGS is used to control type-specifier parsing.
13509 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13512 1: one of the decl-specifiers is an elaborated-type-specifier
13513 (i.e., a type declaration)
13514 2: one of the decl-specifiers is an enum-specifier or a
13515 class-specifier (i.e., a type definition)
13520 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13521 cp_parser_flags flags
,
13522 cp_decl_specifier_seq
*decl_specs
,
13523 int* declares_class_or_enum
)
13525 bool constructor_possible_p
= !parser
->in_declarator_p
;
13526 bool found_decl_spec
= false;
13527 cp_token
*start_token
= NULL
;
13530 /* Clear DECL_SPECS. */
13531 clear_decl_specs (decl_specs
);
13533 /* Assume no class or enumeration type is declared. */
13534 *declares_class_or_enum
= 0;
13536 /* Keep reading specifiers until there are no more to read. */
13539 bool constructor_p
;
13543 /* Peek at the next token. */
13544 token
= cp_lexer_peek_token (parser
->lexer
);
13546 /* Save the first token of the decl spec list for error
13549 start_token
= token
;
13550 /* Handle attributes. */
13551 if (cp_next_tokens_can_be_attribute_p (parser
))
13553 /* Parse the attributes. */
13554 tree attrs
= cp_parser_attributes_opt (parser
);
13556 /* In a sequence of declaration specifiers, c++11 attributes
13557 appertain to the type that precede them. In that case
13560 The attribute-specifier-seq affects the type only for
13561 the declaration it appears in, not other declarations
13562 involving the same type.
13564 But for now let's force the user to position the
13565 attribute either at the beginning of the declaration or
13566 after the declarator-id, which would clearly mean that it
13567 applies to the declarator. */
13568 if (cxx11_attribute_p (attrs
))
13570 if (!found_decl_spec
)
13571 /* The c++11 attribute is at the beginning of the
13572 declaration. It appertains to the entity being
13576 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13578 /* This is an attribute following a
13579 class-specifier. */
13580 if (decl_specs
->type_definition_p
)
13581 warn_misplaced_attr_for_class_type (token
->location
,
13587 decl_specs
->std_attributes
13588 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13589 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13590 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13596 decl_specs
->attributes
13597 = attr_chainon (decl_specs
->attributes
, attrs
);
13598 if (decl_specs
->locations
[ds_attribute
] == 0)
13599 decl_specs
->locations
[ds_attribute
] = token
->location
;
13602 /* Assume we will find a decl-specifier keyword. */
13603 found_decl_spec
= true;
13604 /* If the next token is an appropriate keyword, we can simply
13605 add it to the list. */
13606 switch (token
->keyword
)
13612 if (!at_class_scope_p ())
13614 gcc_rich_location
richloc (token
->location
);
13615 richloc
.add_fixit_remove ();
13616 error_at (&richloc
, "%<friend%> used outside of class");
13617 cp_lexer_purge_token (parser
->lexer
);
13622 /* Consume the token. */
13623 cp_lexer_consume_token (parser
->lexer
);
13627 case RID_CONSTEXPR
:
13629 cp_lexer_consume_token (parser
->lexer
);
13634 cp_lexer_consume_token (parser
->lexer
);
13637 /* function-specifier:
13644 cp_parser_function_specifier_opt (parser
, decl_specs
);
13651 /* Consume the token. */
13652 cp_lexer_consume_token (parser
->lexer
);
13653 /* A constructor declarator cannot appear in a typedef. */
13654 constructor_possible_p
= false;
13655 /* The "typedef" keyword can only occur in a declaration; we
13656 may as well commit at this point. */
13657 cp_parser_commit_to_tentative_parse (parser
);
13659 if (decl_specs
->storage_class
!= sc_none
)
13660 decl_specs
->conflicting_specifiers_p
= true;
13663 /* storage-class-specifier:
13673 if (cxx_dialect
== cxx98
)
13675 /* Consume the token. */
13676 cp_lexer_consume_token (parser
->lexer
);
13678 /* Complain about `auto' as a storage specifier, if
13679 we're complaining about C++0x compatibility. */
13680 gcc_rich_location
richloc (token
->location
);
13681 richloc
.add_fixit_remove ();
13682 warning_at (&richloc
, OPT_Wc__11_compat
,
13683 "%<auto%> changes meaning in C++11; "
13684 "please remove it");
13686 /* Set the storage class anyway. */
13687 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13691 /* C++0x auto type-specifier. */
13692 found_decl_spec
= false;
13699 /* Consume the token. */
13700 cp_lexer_consume_token (parser
->lexer
);
13701 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13705 /* Consume the token. */
13707 cp_lexer_consume_token (parser
->lexer
);
13711 /* We did not yet find a decl-specifier yet. */
13712 found_decl_spec
= false;
13716 if (found_decl_spec
13717 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13718 && token
->keyword
!= RID_CONSTEXPR
)
13719 error ("decl-specifier invalid in condition");
13721 if (found_decl_spec
13722 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13723 && token
->keyword
!= RID_MUTABLE
13724 && token
->keyword
!= RID_CONSTEXPR
)
13725 error_at (token
->location
, "%qD invalid in lambda",
13726 ridpointers
[token
->keyword
]);
13729 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13731 /* Constructors are a special case. The `S' in `S()' is not a
13732 decl-specifier; it is the beginning of the declarator. */
13734 = (!found_decl_spec
13735 && constructor_possible_p
13736 && (cp_parser_constructor_declarator_p
13737 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13739 /* If we don't have a DECL_SPEC yet, then we must be looking at
13740 a type-specifier. */
13741 if (!found_decl_spec
&& !constructor_p
)
13743 int decl_spec_declares_class_or_enum
;
13744 bool is_cv_qualifier
;
13748 = cp_parser_type_specifier (parser
, flags
,
13750 /*is_declaration=*/true,
13751 &decl_spec_declares_class_or_enum
,
13753 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13755 /* If this type-specifier referenced a user-defined type
13756 (a typedef, class-name, etc.), then we can't allow any
13757 more such type-specifiers henceforth.
13761 The longest sequence of decl-specifiers that could
13762 possibly be a type name is taken as the
13763 decl-specifier-seq of a declaration. The sequence shall
13764 be self-consistent as described below.
13768 As a general rule, at most one type-specifier is allowed
13769 in the complete decl-specifier-seq of a declaration. The
13770 only exceptions are the following:
13772 -- const or volatile can be combined with any other
13775 -- signed or unsigned can be combined with char, long,
13783 void g (const int Pc);
13785 Here, Pc is *not* part of the decl-specifier seq; it's
13786 the declarator. Therefore, once we see a type-specifier
13787 (other than a cv-qualifier), we forbid any additional
13788 user-defined types. We *do* still allow things like `int
13789 int' to be considered a decl-specifier-seq, and issue the
13790 error message later. */
13791 if (type_spec
&& !is_cv_qualifier
)
13792 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13793 /* A constructor declarator cannot follow a type-specifier. */
13796 constructor_possible_p
= false;
13797 found_decl_spec
= true;
13798 if (!is_cv_qualifier
)
13799 decl_specs
->any_type_specifiers_p
= true;
13803 /* If we still do not have a DECL_SPEC, then there are no more
13804 decl-specifiers. */
13805 if (!found_decl_spec
)
13808 decl_specs
->any_specifiers_p
= true;
13809 /* After we see one decl-specifier, further decl-specifiers are
13810 always optional. */
13811 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13814 /* Don't allow a friend specifier with a class definition. */
13815 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13816 && (*declares_class_or_enum
& 2))
13817 error_at (decl_specs
->locations
[ds_friend
],
13818 "class definition may not be declared a friend");
13821 /* Parse an (optional) storage-class-specifier.
13823 storage-class-specifier:
13832 storage-class-specifier:
13835 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13838 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13840 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13843 if (cxx_dialect
!= cxx98
)
13845 /* Fall through for C++98. */
13846 gcc_fallthrough ();
13853 /* Consume the token. */
13854 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13861 /* Parse an (optional) function-specifier.
13863 function-specifier:
13868 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13869 Updates DECL_SPECS, if it is non-NULL. */
13872 cp_parser_function_specifier_opt (cp_parser
* parser
,
13873 cp_decl_specifier_seq
*decl_specs
)
13875 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13876 switch (token
->keyword
)
13879 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13883 /* 14.5.2.3 [temp.mem]
13885 A member function template shall not be virtual. */
13886 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13887 && current_class_type
)
13888 error_at (token
->location
, "templates may not be %<virtual%>");
13890 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13894 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13901 /* Consume the token. */
13902 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13905 /* Parse a linkage-specification.
13907 linkage-specification:
13908 extern string-literal { declaration-seq [opt] }
13909 extern string-literal declaration */
13912 cp_parser_linkage_specification (cp_parser
* parser
)
13916 /* Look for the `extern' keyword. */
13917 cp_token
*extern_token
13918 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13920 /* Look for the string-literal. */
13921 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13922 linkage
= cp_parser_string_literal (parser
, false, false);
13924 /* Transform the literal into an identifier. If the literal is a
13925 wide-character string, or contains embedded NULs, then we can't
13926 handle it as the user wants. */
13927 if (strlen (TREE_STRING_POINTER (linkage
))
13928 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13930 cp_parser_error (parser
, "invalid linkage-specification");
13931 /* Assume C++ linkage. */
13932 linkage
= lang_name_cplusplus
;
13935 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13937 /* We're now using the new linkage. */
13938 push_lang_context (linkage
);
13940 /* Preserve the location of the the innermost linkage specification,
13941 tracking the locations of nested specifications via a local. */
13942 location_t saved_location
13943 = parser
->innermost_linkage_specification_location
;
13944 /* Construct a location ranging from the start of the "extern" to
13945 the end of the string-literal, with the caret at the start, e.g.:
13949 parser
->innermost_linkage_specification_location
13950 = make_location (extern_token
->location
,
13951 extern_token
->location
,
13952 get_finish (string_token
->location
));
13954 /* If the next token is a `{', then we're using the first
13956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13958 cp_ensure_no_omp_declare_simd (parser
);
13959 cp_ensure_no_oacc_routine (parser
);
13961 /* Consume the `{' token. */
13962 matching_braces braces
;
13963 braces
.consume_open (parser
)->location
;
13964 /* Parse the declarations. */
13965 cp_parser_declaration_seq_opt (parser
);
13966 /* Look for the closing `}'. */
13967 braces
.require_close (parser
);
13969 /* Otherwise, there's just one declaration. */
13972 bool saved_in_unbraced_linkage_specification_p
;
13974 saved_in_unbraced_linkage_specification_p
13975 = parser
->in_unbraced_linkage_specification_p
;
13976 parser
->in_unbraced_linkage_specification_p
= true;
13977 cp_parser_declaration (parser
);
13978 parser
->in_unbraced_linkage_specification_p
13979 = saved_in_unbraced_linkage_specification_p
;
13982 /* We're done with the linkage-specification. */
13983 pop_lang_context ();
13985 /* Restore location of parent linkage specification, if any. */
13986 parser
->innermost_linkage_specification_location
= saved_location
;
13989 /* Parse a static_assert-declaration.
13991 static_assert-declaration:
13992 static_assert ( constant-expression , string-literal ) ;
13993 static_assert ( constant-expression ) ; (C++17)
13995 If MEMBER_P, this static_assert is a class member. */
13998 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14001 location_t token_loc
;
14005 /* Peek at the `static_assert' token so we can keep track of exactly
14006 where the static assertion started. */
14007 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14009 /* Look for the `static_assert' keyword. */
14010 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14014 /* We know we are in a static assertion; commit to any tentative
14016 if (cp_parser_parsing_tentatively (parser
))
14017 cp_parser_commit_to_tentative_parse (parser
);
14019 /* Parse the `(' starting the static assertion condition. */
14020 matching_parens parens
;
14021 parens
.require_open (parser
);
14023 /* Parse the constant-expression. Allow a non-constant expression
14024 here in order to give better diagnostics in finish_static_assert. */
14026 cp_parser_constant_expression (parser
,
14027 /*allow_non_constant_p=*/true,
14028 /*non_constant_p=*/&dummy
);
14030 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14032 if (cxx_dialect
< cxx17
)
14033 pedwarn (input_location
, OPT_Wpedantic
,
14034 "static_assert without a message "
14035 "only available with -std=c++17 or -std=gnu++17");
14037 cp_lexer_consume_token (parser
->lexer
);
14038 message
= build_string (1, "");
14039 TREE_TYPE (message
) = char_array_type_node
;
14040 fix_string_type (message
);
14044 /* Parse the separating `,'. */
14045 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14047 /* Parse the string-literal message. */
14048 message
= cp_parser_string_literal (parser
,
14049 /*translate=*/false,
14052 /* A `)' completes the static assertion. */
14053 if (!parens
.require_close (parser
))
14054 cp_parser_skip_to_closing_parenthesis (parser
,
14055 /*recovering=*/true,
14056 /*or_comma=*/false,
14057 /*consume_paren=*/true);
14060 /* A semicolon terminates the declaration. */
14061 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14063 /* Get the location for the static assertion. Use that of the
14064 condition if available, otherwise, use that of the "static_assert"
14066 location_t assert_loc
= condition
.get_location ();
14067 if (assert_loc
== UNKNOWN_LOCATION
)
14068 assert_loc
= token_loc
;
14070 /* Complete the static assertion, which may mean either processing
14071 the static assert now or saving it for template instantiation. */
14072 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14075 /* Parse the expression in decltype ( expression ). */
14078 cp_parser_decltype_expr (cp_parser
*parser
,
14079 bool &id_expression_or_member_access_p
)
14081 cp_token
*id_expr_start_token
;
14084 /* Since we're going to preserve any side-effects from this parse, set up a
14085 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14086 in the expression. */
14087 tentative_firewall
firewall (parser
);
14089 /* First, try parsing an id-expression. */
14090 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14091 cp_parser_parse_tentatively (parser
);
14092 expr
= cp_parser_id_expression (parser
,
14093 /*template_keyword_p=*/false,
14094 /*check_dependency_p=*/true,
14095 /*template_p=*/NULL
,
14096 /*declarator_p=*/false,
14097 /*optional_p=*/false);
14099 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14101 bool non_integral_constant_expression_p
= false;
14102 tree id_expression
= expr
;
14104 const char *error_msg
;
14106 if (identifier_p (expr
))
14107 /* Lookup the name we got back from the id-expression. */
14108 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14109 id_expr_start_token
->location
);
14111 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14112 /* A template without args is not a complete id-expression. */
14113 expr
= error_mark_node
;
14116 && expr
!= error_mark_node
14117 && TREE_CODE (expr
) != TYPE_DECL
14118 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14119 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14120 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14122 /* Complete lookup of the id-expression. */
14123 expr
= (finish_id_expression
14124 (id_expression
, expr
, parser
->scope
, &idk
,
14125 /*integral_constant_expression_p=*/false,
14126 /*allow_non_integral_constant_expression_p=*/true,
14127 &non_integral_constant_expression_p
,
14128 /*template_p=*/false,
14130 /*address_p=*/false,
14131 /*template_arg_p=*/false,
14133 id_expr_start_token
->location
));
14135 if (expr
== error_mark_node
)
14136 /* We found an id-expression, but it was something that we
14137 should not have found. This is an error, not something
14138 we can recover from, so note that we found an
14139 id-expression and we'll recover as gracefully as
14141 id_expression_or_member_access_p
= true;
14145 && expr
!= error_mark_node
14146 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14147 /* We have an id-expression. */
14148 id_expression_or_member_access_p
= true;
14151 if (!id_expression_or_member_access_p
)
14153 /* Abort the id-expression parse. */
14154 cp_parser_abort_tentative_parse (parser
);
14156 /* Parsing tentatively, again. */
14157 cp_parser_parse_tentatively (parser
);
14159 /* Parse a class member access. */
14160 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14161 /*cast_p=*/false, /*decltype*/true,
14162 /*member_access_only_p=*/true, NULL
);
14165 && expr
!= error_mark_node
14166 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14167 /* We have an id-expression. */
14168 id_expression_or_member_access_p
= true;
14171 if (id_expression_or_member_access_p
)
14172 /* We have parsed the complete id-expression or member access. */
14173 cp_parser_parse_definitely (parser
);
14176 /* Abort our attempt to parse an id-expression or member access
14178 cp_parser_abort_tentative_parse (parser
);
14180 /* Commit to the tentative_firewall so we get syntax errors. */
14181 cp_parser_commit_to_tentative_parse (parser
);
14183 /* Parse a full expression. */
14184 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14185 /*decltype_p=*/true);
14191 /* Parse a `decltype' type. Returns the type.
14193 simple-type-specifier:
14194 decltype ( expression )
14196 decltype ( auto ) */
14199 cp_parser_decltype (cp_parser
*parser
)
14201 bool id_expression_or_member_access_p
= false;
14202 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14204 if (start_token
->type
== CPP_DECLTYPE
)
14206 /* Already parsed. */
14207 cp_lexer_consume_token (parser
->lexer
);
14208 return saved_checks_value (start_token
->u
.tree_check_value
);
14211 /* Look for the `decltype' token. */
14212 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14213 return error_mark_node
;
14215 /* Parse the opening `('. */
14216 matching_parens parens
;
14217 if (!parens
.require_open (parser
))
14218 return error_mark_node
;
14220 push_deferring_access_checks (dk_deferred
);
14222 tree expr
= NULL_TREE
;
14224 if (cxx_dialect
>= cxx14
14225 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14226 /* decltype (auto) */
14227 cp_lexer_consume_token (parser
->lexer
);
14230 /* decltype (expression) */
14232 /* Types cannot be defined in a `decltype' expression. Save away the
14233 old message and set the new one. */
14234 const char *saved_message
= parser
->type_definition_forbidden_message
;
14235 parser
->type_definition_forbidden_message
14236 = G_("types may not be defined in %<decltype%> expressions");
14238 /* The restrictions on constant-expressions do not apply inside
14239 decltype expressions. */
14240 bool saved_integral_constant_expression_p
14241 = parser
->integral_constant_expression_p
;
14242 bool saved_non_integral_constant_expression_p
14243 = parser
->non_integral_constant_expression_p
;
14244 parser
->integral_constant_expression_p
= false;
14246 /* Within a parenthesized expression, a `>' token is always
14247 the greater-than operator. */
14248 bool saved_greater_than_is_operator_p
14249 = parser
->greater_than_is_operator_p
;
14250 parser
->greater_than_is_operator_p
= true;
14252 /* Do not actually evaluate the expression. */
14253 ++cp_unevaluated_operand
;
14255 /* Do not warn about problems with the expression. */
14256 ++c_inhibit_evaluation_warnings
;
14258 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14260 /* Go back to evaluating expressions. */
14261 --cp_unevaluated_operand
;
14262 --c_inhibit_evaluation_warnings
;
14264 /* The `>' token might be the end of a template-id or
14265 template-parameter-list now. */
14266 parser
->greater_than_is_operator_p
14267 = saved_greater_than_is_operator_p
;
14269 /* Restore the old message and the integral constant expression
14271 parser
->type_definition_forbidden_message
= saved_message
;
14272 parser
->integral_constant_expression_p
14273 = saved_integral_constant_expression_p
;
14274 parser
->non_integral_constant_expression_p
14275 = saved_non_integral_constant_expression_p
;
14278 /* Parse to the closing `)'. */
14279 if (!parens
.require_close (parser
))
14281 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14282 /*consume_paren=*/true);
14283 pop_deferring_access_checks ();
14284 return error_mark_node
;
14290 expr
= make_decltype_auto ();
14291 AUTO_IS_DECLTYPE (expr
) = true;
14294 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14295 tf_warning_or_error
);
14297 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14299 start_token
->type
= CPP_DECLTYPE
;
14300 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14301 start_token
->u
.tree_check_value
->value
= expr
;
14302 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14303 start_token
->keyword
= RID_MAX
;
14304 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14306 pop_to_parent_deferring_access_checks ();
14311 /* Special member functions [gram.special] */
14313 /* Parse a conversion-function-id.
14315 conversion-function-id:
14316 operator conversion-type-id
14318 Returns an IDENTIFIER_NODE representing the operator. */
14321 cp_parser_conversion_function_id (cp_parser
* parser
)
14325 tree saved_qualifying_scope
;
14326 tree saved_object_scope
;
14327 tree pushed_scope
= NULL_TREE
;
14329 /* Look for the `operator' token. */
14330 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14331 return error_mark_node
;
14332 /* When we parse the conversion-type-id, the current scope will be
14333 reset. However, we need that information in able to look up the
14334 conversion function later, so we save it here. */
14335 saved_scope
= parser
->scope
;
14336 saved_qualifying_scope
= parser
->qualifying_scope
;
14337 saved_object_scope
= parser
->object_scope
;
14338 /* We must enter the scope of the class so that the names of
14339 entities declared within the class are available in the
14340 conversion-type-id. For example, consider:
14347 S::operator I() { ... }
14349 In order to see that `I' is a type-name in the definition, we
14350 must be in the scope of `S'. */
14352 pushed_scope
= push_scope (saved_scope
);
14353 /* Parse the conversion-type-id. */
14354 type
= cp_parser_conversion_type_id (parser
);
14355 /* Leave the scope of the class, if any. */
14357 pop_scope (pushed_scope
);
14358 /* Restore the saved scope. */
14359 parser
->scope
= saved_scope
;
14360 parser
->qualifying_scope
= saved_qualifying_scope
;
14361 parser
->object_scope
= saved_object_scope
;
14362 /* If the TYPE is invalid, indicate failure. */
14363 if (type
== error_mark_node
)
14364 return error_mark_node
;
14365 return make_conv_op_name (type
);
14368 /* Parse a conversion-type-id:
14370 conversion-type-id:
14371 type-specifier-seq conversion-declarator [opt]
14373 Returns the TYPE specified. */
14376 cp_parser_conversion_type_id (cp_parser
* parser
)
14379 cp_decl_specifier_seq type_specifiers
;
14380 cp_declarator
*declarator
;
14381 tree type_specified
;
14382 const char *saved_message
;
14384 /* Parse the attributes. */
14385 attributes
= cp_parser_attributes_opt (parser
);
14387 saved_message
= parser
->type_definition_forbidden_message
;
14388 parser
->type_definition_forbidden_message
14389 = G_("types may not be defined in a conversion-type-id");
14391 /* Parse the type-specifiers. */
14392 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14393 /*is_trailing_return=*/false,
14396 parser
->type_definition_forbidden_message
= saved_message
;
14398 /* If that didn't work, stop. */
14399 if (type_specifiers
.type
== error_mark_node
)
14400 return error_mark_node
;
14401 /* Parse the conversion-declarator. */
14402 declarator
= cp_parser_conversion_declarator_opt (parser
);
14404 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14405 /*initialized=*/0, &attributes
);
14407 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14409 /* Don't give this error when parsing tentatively. This happens to
14410 work because we always parse this definitively once. */
14411 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14412 && type_uses_auto (type_specified
))
14414 if (cxx_dialect
< cxx14
)
14416 error ("invalid use of %<auto%> in conversion operator");
14417 return error_mark_node
;
14419 else if (template_parm_scope_p ())
14420 warning (0, "use of %<auto%> in member template "
14421 "conversion operator can never be deduced");
14424 return type_specified
;
14427 /* Parse an (optional) conversion-declarator.
14429 conversion-declarator:
14430 ptr-operator conversion-declarator [opt]
14434 static cp_declarator
*
14435 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14437 enum tree_code code
;
14438 tree class_type
, std_attributes
= NULL_TREE
;
14439 cp_cv_quals cv_quals
;
14441 /* We don't know if there's a ptr-operator next, or not. */
14442 cp_parser_parse_tentatively (parser
);
14443 /* Try the ptr-operator. */
14444 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14446 /* If it worked, look for more conversion-declarators. */
14447 if (cp_parser_parse_definitely (parser
))
14449 cp_declarator
*declarator
;
14451 /* Parse another optional declarator. */
14452 declarator
= cp_parser_conversion_declarator_opt (parser
);
14454 declarator
= cp_parser_make_indirect_declarator
14455 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14463 /* Parse an (optional) ctor-initializer.
14466 : mem-initializer-list */
14469 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14471 /* If the next token is not a `:', then there is no
14472 ctor-initializer. */
14473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14475 /* Do default initialization of any bases and members. */
14476 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14477 finish_mem_initializers (NULL_TREE
);
14481 /* Consume the `:' token. */
14482 cp_lexer_consume_token (parser
->lexer
);
14483 /* And the mem-initializer-list. */
14484 cp_parser_mem_initializer_list (parser
);
14487 /* Parse a mem-initializer-list.
14489 mem-initializer-list:
14490 mem-initializer ... [opt]
14491 mem-initializer ... [opt] , mem-initializer-list */
14494 cp_parser_mem_initializer_list (cp_parser
* parser
)
14496 tree mem_initializer_list
= NULL_TREE
;
14497 tree target_ctor
= error_mark_node
;
14498 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14500 /* Let the semantic analysis code know that we are starting the
14501 mem-initializer-list. */
14502 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14503 error_at (token
->location
,
14504 "only constructors take member initializers");
14506 /* Loop through the list. */
14509 tree mem_initializer
;
14511 token
= cp_lexer_peek_token (parser
->lexer
);
14512 /* Parse the mem-initializer. */
14513 mem_initializer
= cp_parser_mem_initializer (parser
);
14514 /* If the next token is a `...', we're expanding member initializers. */
14515 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14517 || (mem_initializer
!= error_mark_node
14518 && check_for_bare_parameter_packs (TREE_PURPOSE
14519 (mem_initializer
))))
14521 /* Consume the `...'. */
14523 cp_lexer_consume_token (parser
->lexer
);
14525 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14526 can be expanded but members cannot. */
14527 if (mem_initializer
!= error_mark_node
14528 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14530 error_at (token
->location
,
14531 "cannot expand initializer for member %qD",
14532 TREE_PURPOSE (mem_initializer
));
14533 mem_initializer
= error_mark_node
;
14536 /* Construct the pack expansion type. */
14537 if (mem_initializer
!= error_mark_node
)
14538 mem_initializer
= make_pack_expansion (mem_initializer
);
14540 if (target_ctor
!= error_mark_node
14541 && mem_initializer
!= error_mark_node
)
14543 error ("mem-initializer for %qD follows constructor delegation",
14544 TREE_PURPOSE (mem_initializer
));
14545 mem_initializer
= error_mark_node
;
14547 /* Look for a target constructor. */
14548 if (mem_initializer
!= error_mark_node
14549 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14550 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14552 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14553 if (mem_initializer_list
)
14555 error ("constructor delegation follows mem-initializer for %qD",
14556 TREE_PURPOSE (mem_initializer_list
));
14557 mem_initializer
= error_mark_node
;
14559 target_ctor
= mem_initializer
;
14561 /* Add it to the list, unless it was erroneous. */
14562 if (mem_initializer
!= error_mark_node
)
14564 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14565 mem_initializer_list
= mem_initializer
;
14567 /* If the next token is not a `,', we're done. */
14568 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14570 /* Consume the `,' token. */
14571 cp_lexer_consume_token (parser
->lexer
);
14574 /* Perform semantic analysis. */
14575 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14576 finish_mem_initializers (mem_initializer_list
);
14579 /* Parse a mem-initializer.
14582 mem-initializer-id ( expression-list [opt] )
14583 mem-initializer-id braced-init-list
14588 ( expression-list [opt] )
14590 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14591 class) or FIELD_DECL (for a non-static data member) to initialize;
14592 the TREE_VALUE is the expression-list. An empty initialization
14593 list is represented by void_list_node. */
14596 cp_parser_mem_initializer (cp_parser
* parser
)
14598 tree mem_initializer_id
;
14599 tree expression_list
;
14601 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14603 /* Find out what is being initialized. */
14604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14606 permerror (token
->location
,
14607 "anachronistic old-style base class initializer");
14608 mem_initializer_id
= NULL_TREE
;
14612 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14613 if (mem_initializer_id
== error_mark_node
)
14614 return mem_initializer_id
;
14616 member
= expand_member_init (mem_initializer_id
);
14617 if (member
&& !DECL_P (member
))
14618 in_base_initializer
= 1;
14620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14622 bool expr_non_constant_p
;
14623 cp_lexer_set_source_position (parser
->lexer
);
14624 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14625 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14626 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14627 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14631 vec
<tree
, va_gc
> *vec
;
14632 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14634 /*allow_expansion_p=*/true,
14635 /*non_constant_p=*/NULL
);
14637 return error_mark_node
;
14638 expression_list
= build_tree_list_vec (vec
);
14639 release_tree_vector (vec
);
14642 if (expression_list
== error_mark_node
)
14643 return error_mark_node
;
14644 if (!expression_list
)
14645 expression_list
= void_type_node
;
14647 in_base_initializer
= 0;
14649 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14652 /* Parse a mem-initializer-id.
14654 mem-initializer-id:
14655 :: [opt] nested-name-specifier [opt] class-name
14656 decltype-specifier (C++11)
14659 Returns a TYPE indicating the class to be initialized for the first
14660 production (and the second in C++11). Returns an IDENTIFIER_NODE
14661 indicating the data member to be initialized for the last production. */
14664 cp_parser_mem_initializer_id (cp_parser
* parser
)
14666 bool global_scope_p
;
14667 bool nested_name_specifier_p
;
14668 bool template_p
= false;
14671 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14673 /* `typename' is not allowed in this context ([temp.res]). */
14674 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14676 error_at (token
->location
,
14677 "keyword %<typename%> not allowed in this context (a qualified "
14678 "member initializer is implicitly a type)");
14679 cp_lexer_consume_token (parser
->lexer
);
14681 /* Look for the optional `::' operator. */
14683 = (cp_parser_global_scope_opt (parser
,
14684 /*current_scope_valid_p=*/false)
14686 /* Look for the optional nested-name-specifier. The simplest way to
14691 The keyword `typename' is not permitted in a base-specifier or
14692 mem-initializer; in these contexts a qualified name that
14693 depends on a template-parameter is implicitly assumed to be a
14696 is to assume that we have seen the `typename' keyword at this
14698 nested_name_specifier_p
14699 = (cp_parser_nested_name_specifier_opt (parser
,
14700 /*typename_keyword_p=*/true,
14701 /*check_dependency_p=*/true,
14703 /*is_declaration=*/true)
14705 if (nested_name_specifier_p
)
14706 template_p
= cp_parser_optional_template_keyword (parser
);
14707 /* If there is a `::' operator or a nested-name-specifier, then we
14708 are definitely looking for a class-name. */
14709 if (global_scope_p
|| nested_name_specifier_p
)
14710 return cp_parser_class_name (parser
,
14711 /*typename_keyword_p=*/true,
14712 /*template_keyword_p=*/template_p
,
14714 /*check_dependency_p=*/true,
14715 /*class_head_p=*/false,
14716 /*is_declaration=*/true);
14717 /* Otherwise, we could also be looking for an ordinary identifier. */
14718 cp_parser_parse_tentatively (parser
);
14719 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14720 /* Try a decltype-specifier. */
14721 id
= cp_parser_decltype (parser
);
14723 /* Otherwise, try a class-name. */
14724 id
= cp_parser_class_name (parser
,
14725 /*typename_keyword_p=*/true,
14726 /*template_keyword_p=*/false,
14728 /*check_dependency_p=*/true,
14729 /*class_head_p=*/false,
14730 /*is_declaration=*/true);
14731 /* If we found one, we're done. */
14732 if (cp_parser_parse_definitely (parser
))
14734 /* Otherwise, look for an ordinary identifier. */
14735 return cp_parser_identifier (parser
);
14738 /* Overloading [gram.over] */
14740 /* Parse an operator-function-id.
14742 operator-function-id:
14745 Returns an IDENTIFIER_NODE for the operator which is a
14746 human-readable spelling of the identifier, e.g., `operator +'. */
14749 cp_parser_operator_function_id (cp_parser
* parser
)
14751 /* Look for the `operator' keyword. */
14752 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14753 return error_mark_node
;
14754 /* And then the name of the operator itself. */
14755 return cp_parser_operator (parser
);
14758 /* Return an identifier node for a user-defined literal operator.
14759 The suffix identifier is chained to the operator name identifier. */
14762 cp_literal_operator_id (const char* name
)
14765 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14766 + strlen (name
) + 10);
14767 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14768 identifier
= get_identifier (buffer
);
14773 /* Parse an operator.
14776 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14777 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14778 || ++ -- , ->* -> () []
14785 Returns an IDENTIFIER_NODE for the operator which is a
14786 human-readable spelling of the identifier, e.g., `operator +'. */
14789 cp_parser_operator (cp_parser
* parser
)
14791 tree id
= NULL_TREE
;
14795 /* Peek at the next token. */
14796 token
= cp_lexer_peek_token (parser
->lexer
);
14798 location_t start_loc
= token
->location
;
14800 /* Figure out which operator we have. */
14801 enum tree_code op
= ERROR_MARK
;
14802 bool assop
= false;
14803 bool consumed
= false;
14804 switch (token
->type
)
14808 /* The keyword should be either `new' or `delete'. */
14809 if (token
->keyword
== RID_NEW
)
14811 else if (token
->keyword
== RID_DELETE
)
14816 /* Consume the `new' or `delete' token. */
14817 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14819 /* Peek at the next token. */
14820 token
= cp_lexer_peek_token (parser
->lexer
);
14821 /* If it's a `[' token then this is the array variant of the
14823 if (token
->type
== CPP_OPEN_SQUARE
)
14825 /* Consume the `[' token. */
14826 cp_lexer_consume_token (parser
->lexer
);
14827 /* Look for the `]' token. */
14828 if (cp_token
*close_token
14829 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14830 end_loc
= close_token
->location
;
14831 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14833 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14851 op
= TRUNC_DIV_EXPR
;
14855 op
= TRUNC_MOD_EXPR
;
14875 op
= TRUTH_NOT_EXPR
;
14908 op
= TRUNC_DIV_EXPR
;
14913 op
= TRUNC_MOD_EXPR
;
14939 case CPP_LSHIFT_EQ
:
14944 case CPP_RSHIFT_EQ
:
14961 case CPP_GREATER_EQ
:
14966 op
= TRUTH_ANDIF_EXPR
;
14970 op
= TRUTH_ORIF_EXPR
;
14973 case CPP_PLUS_PLUS
:
14974 op
= POSTINCREMENT_EXPR
;
14977 case CPP_MINUS_MINUS
:
14978 op
= PREDECREMENT_EXPR
;
14982 op
= COMPOUND_EXPR
;
14985 case CPP_DEREF_STAR
:
14990 op
= COMPONENT_REF
;
14993 case CPP_OPEN_PAREN
:
14995 /* Consume the `('. */
14996 matching_parens parens
;
14997 parens
.consume_open (parser
);
14998 /* Look for the matching `)'. */
14999 parens
.require_close (parser
);
15005 case CPP_OPEN_SQUARE
:
15006 /* Consume the `['. */
15007 cp_lexer_consume_token (parser
->lexer
);
15008 /* Look for the matching `]'. */
15009 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15014 case CPP_UTF8STRING
:
15015 case CPP_UTF8STRING_USERDEF
:
15022 case CPP_STRING_USERDEF
:
15023 case CPP_WSTRING_USERDEF
:
15024 case CPP_STRING16_USERDEF
:
15025 case CPP_STRING32_USERDEF
:
15027 tree str
, string_tree
;
15030 if (cxx_dialect
== cxx98
)
15031 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15033 /* Consume the string. */
15034 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15035 /*wide_ok=*/true, /*lookup_udlit=*/false);
15036 if (str
== error_mark_node
)
15037 return error_mark_node
;
15038 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15040 string_tree
= USERDEF_LITERAL_VALUE (str
);
15041 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15046 /* Look for the suffix identifier. */
15047 token
= cp_lexer_peek_token (parser
->lexer
);
15048 if (token
->type
== CPP_NAME
)
15049 id
= cp_parser_identifier (parser
);
15050 else if (token
->type
== CPP_KEYWORD
)
15052 error ("unexpected keyword;"
15053 " remove space between quotes and suffix identifier");
15054 return error_mark_node
;
15058 error ("expected suffix identifier");
15059 return error_mark_node
;
15062 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15063 (TREE_TYPE (TREE_TYPE (string_tree
))));
15064 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15067 error ("expected empty string after %<operator%> keyword");
15068 return error_mark_node
;
15070 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15073 error ("invalid encoding prefix in literal operator");
15074 return error_mark_node
;
15076 if (id
!= error_mark_node
)
15078 const char *name
= IDENTIFIER_POINTER (id
);
15079 id
= cp_literal_operator_id (name
);
15085 /* Anything else is an error. */
15089 /* If we have selected an identifier, we need to consume the
15091 if (op
!= ERROR_MARK
)
15093 id
= ovl_op_identifier (assop
, op
);
15095 cp_lexer_consume_token (parser
->lexer
);
15097 /* Otherwise, no valid operator name was present. */
15100 cp_parser_error (parser
, "expected operator");
15101 id
= error_mark_node
;
15104 return cp_expr (id
, start_loc
);
15107 /* Parse a template-declaration.
15109 template-declaration:
15110 export [opt] template < template-parameter-list > declaration
15112 If MEMBER_P is TRUE, this template-declaration occurs within a
15115 The grammar rule given by the standard isn't correct. What
15116 is really meant is:
15118 template-declaration:
15119 export [opt] template-parameter-list-seq
15120 decl-specifier-seq [opt] init-declarator [opt] ;
15121 export [opt] template-parameter-list-seq
15122 function-definition
15124 template-parameter-list-seq:
15125 template-parameter-list-seq [opt]
15126 template < template-parameter-list >
15128 Concept Extensions:
15130 template-parameter-list-seq:
15131 template < template-parameter-list > requires-clause [opt]
15134 requires logical-or-expression */
15137 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15139 /* Check for `export'. */
15140 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15142 /* Consume the `export' token. */
15143 cp_lexer_consume_token (parser
->lexer
);
15144 /* Warn that we do not support `export'. */
15145 warning (0, "keyword %<export%> not implemented, and will be ignored");
15148 cp_parser_template_declaration_after_export (parser
, member_p
);
15151 /* Parse a template-parameter-list.
15153 template-parameter-list:
15155 template-parameter-list , template-parameter
15157 Returns a TREE_LIST. Each node represents a template parameter.
15158 The nodes are connected via their TREE_CHAINs. */
15161 cp_parser_template_parameter_list (cp_parser
* parser
)
15163 tree parameter_list
= NULL_TREE
;
15165 begin_template_parm_list ();
15167 /* The loop below parses the template parms. We first need to know
15168 the total number of template parms to be able to compute proper
15169 canonical types of each dependent type. So after the loop, when
15170 we know the total number of template parms,
15171 end_template_parm_list computes the proper canonical types and
15172 fixes up the dependent types accordingly. */
15177 bool is_parameter_pack
;
15178 location_t parm_loc
;
15180 /* Parse the template-parameter. */
15181 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15182 parameter
= cp_parser_template_parameter (parser
,
15184 &is_parameter_pack
);
15185 /* Add it to the list. */
15186 if (parameter
!= error_mark_node
)
15187 parameter_list
= process_template_parm (parameter_list
,
15191 is_parameter_pack
);
15194 tree err_parm
= build_tree_list (parameter
, parameter
);
15195 parameter_list
= chainon (parameter_list
, err_parm
);
15198 /* If the next token is not a `,', we're done. */
15199 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15201 /* Otherwise, consume the `,' token. */
15202 cp_lexer_consume_token (parser
->lexer
);
15205 return end_template_parm_list (parameter_list
);
15208 /* Parse a introduction-list.
15211 introduced-parameter
15212 introduction-list , introduced-parameter
15214 introduced-parameter:
15215 ...[opt] identifier
15217 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15218 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15219 WILDCARD_DECL will also have DECL_NAME set and token location in
15220 DECL_SOURCE_LOCATION. */
15223 cp_parser_introduction_list (cp_parser
*parser
)
15225 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15229 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15231 cp_lexer_consume_token (parser
->lexer
);
15233 /* Build placeholder. */
15234 tree parm
= build_nt (WILDCARD_DECL
);
15235 DECL_SOURCE_LOCATION (parm
)
15236 = cp_lexer_peek_token (parser
->lexer
)->location
;
15237 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15238 WILDCARD_PACK_P (parm
) = is_pack
;
15239 vec_safe_push (introduction_vec
, parm
);
15241 /* If the next token is not a `,', we're done. */
15242 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15244 /* Otherwise, consume the `,' token. */
15245 cp_lexer_consume_token (parser
->lexer
);
15248 /* Convert the vec into a TREE_VEC. */
15249 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15252 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15253 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15255 release_tree_vector (introduction_vec
);
15256 return introduction_list
;
15259 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15260 is an abstract declarator. */
15262 static inline cp_declarator
*
15263 get_id_declarator (cp_declarator
*declarator
)
15265 cp_declarator
*d
= declarator
;
15266 while (d
&& d
->kind
!= cdk_id
)
15271 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15272 is an abstract declarator. */
15275 get_unqualified_id (cp_declarator
*declarator
)
15277 declarator
= get_id_declarator (declarator
);
15279 return declarator
->u
.id
.unqualified_name
;
15284 /* Returns true if DECL represents a constrained-parameter. */
15287 is_constrained_parameter (tree decl
)
15290 && TREE_CODE (decl
) == TYPE_DECL
15291 && CONSTRAINED_PARM_CONCEPT (decl
)
15292 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15295 /* Returns true if PARM declares a constrained-parameter. */
15298 is_constrained_parameter (cp_parameter_declarator
*parm
)
15300 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15303 /* Check that the type parameter is only a declarator-id, and that its
15304 type is not cv-qualified. */
15307 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15308 cp_parameter_declarator
*parm
)
15310 if (!parm
->declarator
)
15313 if (parm
->declarator
->kind
!= cdk_id
)
15315 cp_parser_error (parser
, "invalid constrained type parameter");
15319 /* Don't allow cv-qualified type parameters. */
15320 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15321 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15323 cp_parser_error (parser
, "cv-qualified type parameter");
15330 /* Finish parsing/processing a template type parameter and checking
15331 various restrictions. */
15334 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15336 cp_parameter_declarator
* parmdecl
)
15338 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15339 return finish_template_type_parm (class_type_node
, id
);
15341 return error_mark_node
;
15345 finish_constrained_template_template_parm (tree proto
, tree id
)
15347 /* FIXME: This should probably be copied, and we may need to adjust
15348 the template parameter depths. */
15349 tree saved_parms
= current_template_parms
;
15350 begin_template_parm_list ();
15351 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15352 end_template_parm_list ();
15354 tree parm
= finish_template_template_parm (class_type_node
, id
);
15355 current_template_parms
= saved_parms
;
15360 /* Finish parsing/processing a template template parameter by borrowing
15361 the template parameter list from the prototype parameter. */
15364 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15367 cp_parameter_declarator
*parmdecl
)
15369 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15370 return error_mark_node
;
15371 return finish_constrained_template_template_parm (proto
, id
);
15374 /* Create a new non-type template parameter from the given PARM
15378 constrained_non_type_template_parm (bool *is_non_type
,
15379 cp_parameter_declarator
*parm
)
15381 *is_non_type
= true;
15382 cp_declarator
*decl
= parm
->declarator
;
15383 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15384 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15385 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15388 /* Build a constrained template parameter based on the PARMDECL
15389 declarator. The type of PARMDECL is the constrained type, which
15390 refers to the prototype template parameter that ultimately
15391 specifies the type of the declared parameter. */
15394 finish_constrained_parameter (cp_parser
*parser
,
15395 cp_parameter_declarator
*parmdecl
,
15397 bool *is_parameter_pack
)
15399 tree decl
= parmdecl
->decl_specifiers
.type
;
15400 tree id
= get_unqualified_id (parmdecl
->declarator
);
15401 tree def
= parmdecl
->default_argument
;
15402 tree proto
= DECL_INITIAL (decl
);
15404 /* A template parameter constrained by a variadic concept shall also
15405 be declared as a template parameter pack. */
15406 bool is_variadic
= template_parameter_pack_p (proto
);
15407 if (is_variadic
&& !*is_parameter_pack
)
15408 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15410 /* Build the parameter. Return an error if the declarator was invalid. */
15412 if (TREE_CODE (proto
) == TYPE_DECL
)
15413 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15414 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15415 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15418 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15419 if (parm
== error_mark_node
)
15420 return error_mark_node
;
15422 /* Finish the parameter decl and create a node attaching the
15423 default argument and constraint. */
15424 parm
= build_tree_list (def
, parm
);
15425 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15430 /* Returns true if the parsed type actually represents the declaration
15431 of a type template-parameter. */
15434 declares_constrained_type_template_parameter (tree type
)
15436 return (is_constrained_parameter (type
)
15437 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15441 /* Returns true if the parsed type actually represents the declaration of
15442 a template template-parameter. */
15445 declares_constrained_template_template_parameter (tree type
)
15447 return (is_constrained_parameter (type
)
15448 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15451 /* Parse a default argument for a type template-parameter.
15452 Note that diagnostics are handled in cp_parser_template_parameter. */
15455 cp_parser_default_type_template_argument (cp_parser
*parser
)
15457 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15459 /* Consume the `=' token. */
15460 cp_lexer_consume_token (parser
->lexer
);
15462 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15464 /* Parse the default-argument. */
15465 push_deferring_access_checks (dk_no_deferred
);
15466 tree default_argument
= cp_parser_type_id (parser
);
15467 pop_deferring_access_checks ();
15469 if (flag_concepts
&& type_uses_auto (default_argument
))
15471 error_at (token
->location
,
15472 "invalid use of %<auto%> in default template argument");
15473 return error_mark_node
;
15476 return default_argument
;
15479 /* Parse a default argument for a template template-parameter. */
15482 cp_parser_default_template_template_argument (cp_parser
*parser
)
15484 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15488 /* Consume the `='. */
15489 cp_lexer_consume_token (parser
->lexer
);
15490 /* Parse the id-expression. */
15491 push_deferring_access_checks (dk_no_deferred
);
15492 /* save token before parsing the id-expression, for error
15494 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15495 tree default_argument
15496 = cp_parser_id_expression (parser
,
15497 /*template_keyword_p=*/false,
15498 /*check_dependency_p=*/true,
15499 /*template_p=*/&is_template
,
15500 /*declarator_p=*/false,
15501 /*optional_p=*/false);
15502 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15503 /* If the id-expression was a template-id that refers to
15504 a template-class, we already have the declaration here,
15505 so no further lookup is needed. */
15508 /* Look up the name. */
15510 = cp_parser_lookup_name (parser
, default_argument
,
15512 /*is_template=*/is_template
,
15513 /*is_namespace=*/false,
15514 /*check_dependency=*/true,
15515 /*ambiguous_decls=*/NULL
,
15517 /* See if the default argument is valid. */
15518 default_argument
= check_template_template_default_arg (default_argument
);
15519 pop_deferring_access_checks ();
15520 return default_argument
;
15523 /* Parse a template-parameter.
15525 template-parameter:
15527 parameter-declaration
15529 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15530 the parameter. The TREE_PURPOSE is the default value, if any.
15531 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15532 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15533 set to true iff this parameter is a parameter pack. */
15536 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15537 bool *is_parameter_pack
)
15540 cp_parameter_declarator
*parameter_declarator
;
15543 /* Assume it is a type parameter or a template parameter. */
15544 *is_non_type
= false;
15545 /* Assume it not a parameter pack. */
15546 *is_parameter_pack
= false;
15547 /* Peek at the next token. */
15548 token
= cp_lexer_peek_token (parser
->lexer
);
15549 /* If it is `template', we have a type-parameter. */
15550 if (token
->keyword
== RID_TEMPLATE
)
15551 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15552 /* If it is `class' or `typename' we do not know yet whether it is a
15553 type parameter or a non-type parameter. Consider:
15555 template <typename T, typename T::X X> ...
15559 template <class C, class D*> ...
15561 Here, the first parameter is a type parameter, and the second is
15562 a non-type parameter. We can tell by looking at the token after
15563 the identifier -- if it is a `,', `=', or `>' then we have a type
15565 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15567 /* Peek at the token after `class' or `typename'. */
15568 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15569 /* If it's an ellipsis, we have a template type parameter
15571 if (token
->type
== CPP_ELLIPSIS
)
15572 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15573 /* If it's an identifier, skip it. */
15574 if (token
->type
== CPP_NAME
)
15575 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15576 /* Now, see if the token looks like the end of a template
15578 if (token
->type
== CPP_COMMA
15579 || token
->type
== CPP_EQ
15580 || token
->type
== CPP_GREATER
)
15581 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15584 /* Otherwise, it is a non-type parameter or a constrained parameter.
15588 When parsing a default template-argument for a non-type
15589 template-parameter, the first non-nested `>' is taken as the end
15590 of the template parameter-list rather than a greater-than
15592 parameter_declarator
15593 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15594 /*parenthesized_p=*/NULL
);
15596 if (!parameter_declarator
)
15597 return error_mark_node
;
15599 /* If the parameter declaration is marked as a parameter pack, set
15600 *IS_PARAMETER_PACK to notify the caller. */
15601 if (parameter_declarator
->template_parameter_pack_p
)
15602 *is_parameter_pack
= true;
15604 if (parameter_declarator
->default_argument
)
15606 /* Can happen in some cases of erroneous input (c++/34892). */
15607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15608 /* Consume the `...' for better error recovery. */
15609 cp_lexer_consume_token (parser
->lexer
);
15612 // The parameter may have been constrained.
15613 if (is_constrained_parameter (parameter_declarator
))
15614 return finish_constrained_parameter (parser
,
15615 parameter_declarator
,
15617 is_parameter_pack
);
15619 // Now we're sure that the parameter is a non-type parameter.
15620 *is_non_type
= true;
15622 parm
= grokdeclarator (parameter_declarator
->declarator
,
15623 ¶meter_declarator
->decl_specifiers
,
15624 TPARM
, /*initialized=*/0,
15625 /*attrlist=*/NULL
);
15626 if (parm
== error_mark_node
)
15627 return error_mark_node
;
15629 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15632 /* Parse a type-parameter.
15635 class identifier [opt]
15636 class identifier [opt] = type-id
15637 typename identifier [opt]
15638 typename identifier [opt] = type-id
15639 template < template-parameter-list > class identifier [opt]
15640 template < template-parameter-list > class identifier [opt]
15643 GNU Extension (variadic templates):
15646 class ... identifier [opt]
15647 typename ... identifier [opt]
15649 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15650 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15651 the declaration of the parameter.
15653 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15656 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15661 /* Look for a keyword to tell us what kind of parameter this is. */
15662 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15664 return error_mark_node
;
15666 switch (token
->keyword
)
15672 tree default_argument
;
15674 /* If the next token is an ellipsis, we have a template
15676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15678 /* Consume the `...' token. */
15679 cp_lexer_consume_token (parser
->lexer
);
15680 maybe_warn_variadic_templates ();
15682 *is_parameter_pack
= true;
15685 /* If the next token is an identifier, then it names the
15687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15688 identifier
= cp_parser_identifier (parser
);
15690 identifier
= NULL_TREE
;
15692 /* Create the parameter. */
15693 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15695 /* If the next token is an `=', we have a default argument. */
15696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15699 = cp_parser_default_type_template_argument (parser
);
15701 /* Template parameter packs cannot have default
15703 if (*is_parameter_pack
)
15706 error_at (token
->location
,
15707 "template parameter pack %qD cannot have a "
15708 "default argument", identifier
);
15710 error_at (token
->location
,
15711 "template parameter packs cannot have "
15712 "default arguments");
15713 default_argument
= NULL_TREE
;
15715 else if (check_for_bare_parameter_packs (default_argument
))
15716 default_argument
= error_mark_node
;
15719 default_argument
= NULL_TREE
;
15721 /* Create the combined representation of the parameter and the
15722 default argument. */
15723 parameter
= build_tree_list (default_argument
, parameter
);
15730 tree default_argument
;
15732 /* Look for the `<'. */
15733 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15734 /* Parse the template-parameter-list. */
15735 cp_parser_template_parameter_list (parser
);
15736 /* Look for the `>'. */
15737 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15739 // If template requirements are present, parse them.
15742 tree reqs
= get_shorthand_constraints (current_template_parms
);
15743 if (tree r
= cp_parser_requires_clause_opt (parser
))
15744 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15745 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15748 /* Look for the `class' or 'typename' keywords. */
15749 cp_parser_type_parameter_key (parser
);
15750 /* If the next token is an ellipsis, we have a template
15752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15754 /* Consume the `...' token. */
15755 cp_lexer_consume_token (parser
->lexer
);
15756 maybe_warn_variadic_templates ();
15758 *is_parameter_pack
= true;
15760 /* If the next token is an `=', then there is a
15761 default-argument. If the next token is a `>', we are at
15762 the end of the parameter-list. If the next token is a `,',
15763 then we are at the end of this parameter. */
15764 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15765 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15766 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15768 identifier
= cp_parser_identifier (parser
);
15769 /* Treat invalid names as if the parameter were nameless. */
15770 if (identifier
== error_mark_node
)
15771 identifier
= NULL_TREE
;
15774 identifier
= NULL_TREE
;
15776 /* Create the template parameter. */
15777 parameter
= finish_template_template_parm (class_type_node
,
15780 /* If the next token is an `=', then there is a
15781 default-argument. */
15782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15785 = cp_parser_default_template_template_argument (parser
);
15787 /* Template parameter packs cannot have default
15789 if (*is_parameter_pack
)
15792 error_at (token
->location
,
15793 "template parameter pack %qD cannot "
15794 "have a default argument",
15797 error_at (token
->location
, "template parameter packs cannot "
15798 "have default arguments");
15799 default_argument
= NULL_TREE
;
15803 default_argument
= NULL_TREE
;
15805 /* Create the combined representation of the parameter and the
15806 default argument. */
15807 parameter
= build_tree_list (default_argument
, parameter
);
15812 gcc_unreachable ();
15819 /* Parse a template-id.
15822 template-name < template-argument-list [opt] >
15824 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15825 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15826 returned. Otherwise, if the template-name names a function, or set
15827 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15828 names a class, returns a TYPE_DECL for the specialization.
15830 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15831 uninstantiated templates. */
15834 cp_parser_template_id (cp_parser
*parser
,
15835 bool template_keyword_p
,
15836 bool check_dependency_p
,
15837 enum tag_types tag_type
,
15838 bool is_declaration
)
15843 cp_token_position start_of_id
= 0;
15844 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15845 bool is_identifier
;
15847 /* If the next token corresponds to a template-id, there is no need
15849 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15850 if (token
->type
== CPP_TEMPLATE_ID
)
15852 cp_lexer_consume_token (parser
->lexer
);
15853 return saved_checks_value (token
->u
.tree_check_value
);
15856 /* Avoid performing name lookup if there is no possibility of
15857 finding a template-id. */
15858 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15859 || (token
->type
== CPP_NAME
15860 && !cp_parser_nth_token_starts_template_argument_list_p
15863 cp_parser_error (parser
, "expected template-id");
15864 return error_mark_node
;
15867 /* Remember where the template-id starts. */
15868 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15869 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15871 push_deferring_access_checks (dk_deferred
);
15873 /* Parse the template-name. */
15874 is_identifier
= false;
15875 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15876 check_dependency_p
,
15880 if (templ
== error_mark_node
|| is_identifier
)
15882 pop_deferring_access_checks ();
15886 /* Since we're going to preserve any side-effects from this parse, set up a
15887 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15888 in the template arguments. */
15889 tentative_firewall
firewall (parser
);
15891 /* If we find the sequence `[:' after a template-name, it's probably
15892 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15893 parse correctly the argument list. */
15894 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15895 == CPP_OPEN_SQUARE
)
15896 && next_token
->flags
& DIGRAPH
15897 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15899 && !(next_token_2
->flags
& PREV_WHITE
))
15901 cp_parser_parse_tentatively (parser
);
15902 /* Change `:' into `::'. */
15903 next_token_2
->type
= CPP_SCOPE
;
15904 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15906 cp_lexer_consume_token (parser
->lexer
);
15908 /* Parse the arguments. */
15909 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15910 if (!cp_parser_parse_definitely (parser
))
15912 /* If we couldn't parse an argument list, then we revert our changes
15913 and return simply an error. Maybe this is not a template-id
15915 next_token_2
->type
= CPP_COLON
;
15916 cp_parser_error (parser
, "expected %<<%>");
15917 pop_deferring_access_checks ();
15918 return error_mark_node
;
15920 /* Otherwise, emit an error about the invalid digraph, but continue
15921 parsing because we got our argument list. */
15922 if (permerror (next_token
->location
,
15923 "%<<::%> cannot begin a template-argument list"))
15925 static bool hint
= false;
15926 inform (next_token
->location
,
15927 "%<<:%> is an alternate spelling for %<[%>."
15928 " Insert whitespace between %<<%> and %<::%>");
15929 if (!hint
&& !flag_permissive
)
15931 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15932 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15933 "accept your code)");
15940 /* Look for the `<' that starts the template-argument-list. */
15941 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15943 pop_deferring_access_checks ();
15944 return error_mark_node
;
15946 /* Parse the arguments. */
15947 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15950 /* Set the location to be of the form:
15951 template-name < template-argument-list [opt] >
15952 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15953 with caret == start at the start of the template-name,
15954 ranging until the closing '>'. */
15955 location_t finish_loc
15956 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15957 location_t combined_loc
15958 = make_location (token
->location
, token
->location
, finish_loc
);
15960 /* Check for concepts autos where they don't belong. We could
15961 identify types in some cases of idnetifier TEMPL, looking ahead
15962 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15963 types. We reject them in functions, but if what we have is an
15964 identifier, even with none_type we can't conclude it's NOT a
15965 type, we have to wait for template substitution. */
15966 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15967 template_id
= error_mark_node
;
15968 /* Build a representation of the specialization. */
15969 else if (identifier_p (templ
))
15970 template_id
= build_min_nt_loc (combined_loc
,
15973 else if (DECL_TYPE_TEMPLATE_P (templ
)
15974 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15976 bool entering_scope
;
15977 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15978 template (rather than some instantiation thereof) only if
15979 is not nested within some other construct. For example, in
15980 "template <typename T> void f(T) { A<T>::", A<T> is just an
15981 instantiation of A. */
15982 entering_scope
= (template_parm_scope_p ()
15983 && cp_lexer_next_token_is (parser
->lexer
,
15986 = finish_template_type (templ
, arguments
, entering_scope
);
15988 /* A template-like identifier may be a partial concept id. */
15989 else if (flag_concepts
15990 && (template_id
= (cp_parser_maybe_partial_concept_id
15991 (parser
, templ
, arguments
))))
15992 return template_id
;
15993 else if (variable_template_p (templ
))
15995 template_id
= lookup_template_variable (templ
, arguments
);
15996 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15997 SET_EXPR_LOCATION (template_id
, combined_loc
);
16001 /* If it's not a class-template or a template-template, it should be
16002 a function-template. */
16003 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
16004 || TREE_CODE (templ
) == OVERLOAD
16005 || BASELINK_P (templ
)));
16007 template_id
= lookup_template_function (templ
, arguments
);
16008 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16009 SET_EXPR_LOCATION (template_id
, combined_loc
);
16012 /* If parsing tentatively, replace the sequence of tokens that makes
16013 up the template-id with a CPP_TEMPLATE_ID token. That way,
16014 should we re-parse the token stream, we will not have to repeat
16015 the effort required to do the parse, nor will we issue duplicate
16016 error messages about problems during instantiation of the
16019 /* Don't do this if we had a parse error in a declarator; re-parsing
16020 might succeed if a name changes meaning (60361). */
16021 && !(cp_parser_error_occurred (parser
)
16022 && cp_parser_parsing_tentatively (parser
)
16023 && parser
->in_declarator_p
))
16025 /* Reset the contents of the START_OF_ID token. */
16026 token
->type
= CPP_TEMPLATE_ID
;
16027 token
->location
= combined_loc
;
16029 /* Retrieve any deferred checks. Do not pop this access checks yet
16030 so the memory will not be reclaimed during token replacing below. */
16031 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16032 token
->u
.tree_check_value
->value
= template_id
;
16033 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16034 token
->keyword
= RID_MAX
;
16036 /* Purge all subsequent tokens. */
16037 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16039 /* ??? Can we actually assume that, if template_id ==
16040 error_mark_node, we will have issued a diagnostic to the
16041 user, as opposed to simply marking the tentative parse as
16043 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16044 error_at (token
->location
, "parse error in template argument list");
16047 pop_to_parent_deferring_access_checks ();
16048 return template_id
;
16051 /* Parse a template-name.
16056 The standard should actually say:
16060 operator-function-id
16062 A defect report has been filed about this issue.
16064 A conversion-function-id cannot be a template name because they cannot
16065 be part of a template-id. In fact, looking at this code:
16067 a.operator K<int>()
16069 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16070 It is impossible to call a templated conversion-function-id with an
16071 explicit argument list, since the only allowed template parameter is
16072 the type to which it is converting.
16074 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16075 `template' keyword, in a construction like:
16079 In that case `f' is taken to be a template-name, even though there
16080 is no way of knowing for sure.
16082 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16083 name refers to a set of overloaded functions, at least one of which
16084 is a template, or an IDENTIFIER_NODE with the name of the template,
16085 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16086 names are looked up inside uninstantiated templates. */
16089 cp_parser_template_name (cp_parser
* parser
,
16090 bool template_keyword_p
,
16091 bool check_dependency_p
,
16092 bool is_declaration
,
16093 enum tag_types tag_type
,
16094 bool *is_identifier
)
16098 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16100 /* If the next token is `operator', then we have either an
16101 operator-function-id or a conversion-function-id. */
16102 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16104 /* We don't know whether we're looking at an
16105 operator-function-id or a conversion-function-id. */
16106 cp_parser_parse_tentatively (parser
);
16107 /* Try an operator-function-id. */
16108 identifier
= cp_parser_operator_function_id (parser
);
16109 /* If that didn't work, try a conversion-function-id. */
16110 if (!cp_parser_parse_definitely (parser
))
16112 cp_parser_error (parser
, "expected template-name");
16113 return error_mark_node
;
16116 /* Look for the identifier. */
16118 identifier
= cp_parser_identifier (parser
);
16120 /* If we didn't find an identifier, we don't have a template-id. */
16121 if (identifier
== error_mark_node
)
16122 return error_mark_node
;
16124 /* If the name immediately followed the `template' keyword, then it
16125 is a template-name. However, if the next token is not `<', then
16126 we do not treat it as a template-name, since it is not being used
16127 as part of a template-id. This enables us to handle constructs
16130 template <typename T> struct S { S(); };
16131 template <typename T> S<T>::S();
16133 correctly. We would treat `S' as a template -- if it were `S<T>'
16134 -- but we do not if there is no `<'. */
16136 if (processing_template_decl
16137 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16139 /* In a declaration, in a dependent context, we pretend that the
16140 "template" keyword was present in order to improve error
16141 recovery. For example, given:
16143 template <typename T> void f(T::X<int>);
16145 we want to treat "X<int>" as a template-id. */
16147 && !template_keyword_p
16148 && parser
->scope
&& TYPE_P (parser
->scope
)
16149 && check_dependency_p
16150 && dependent_scope_p (parser
->scope
)
16151 /* Do not do this for dtors (or ctors), since they never
16152 need the template keyword before their name. */
16153 && !constructor_name_p (identifier
, parser
->scope
))
16155 cp_token_position start
= 0;
16157 /* Explain what went wrong. */
16158 error_at (token
->location
, "non-template %qD used as template",
16160 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16161 parser
->scope
, identifier
);
16162 /* If parsing tentatively, find the location of the "<" token. */
16163 if (cp_parser_simulate_error (parser
))
16164 start
= cp_lexer_token_position (parser
->lexer
, true);
16165 /* Parse the template arguments so that we can issue error
16166 messages about them. */
16167 cp_lexer_consume_token (parser
->lexer
);
16168 cp_parser_enclosed_template_argument_list (parser
);
16169 /* Skip tokens until we find a good place from which to
16170 continue parsing. */
16171 cp_parser_skip_to_closing_parenthesis (parser
,
16172 /*recovering=*/true,
16174 /*consume_paren=*/false);
16175 /* If parsing tentatively, permanently remove the
16176 template argument list. That will prevent duplicate
16177 error messages from being issued about the missing
16178 "template" keyword. */
16180 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16182 *is_identifier
= true;
16183 parser
->context
->object_type
= NULL_TREE
;
16187 /* If the "template" keyword is present, then there is generally
16188 no point in doing name-lookup, so we just return IDENTIFIER.
16189 But, if the qualifying scope is non-dependent then we can
16190 (and must) do name-lookup normally. */
16191 if (template_keyword_p
)
16193 tree scope
= (parser
->scope
? parser
->scope
16194 : parser
->context
->object_type
);
16195 if (scope
&& TYPE_P (scope
)
16196 && (!CLASS_TYPE_P (scope
)
16197 || (check_dependency_p
&& dependent_type_p (scope
))))
16199 /* We're optimizing away the call to cp_parser_lookup_name, but
16200 we still need to do this. */
16201 parser
->context
->object_type
= NULL_TREE
;
16207 /* Look up the name. */
16208 decl
= cp_parser_lookup_name (parser
, identifier
,
16210 /*is_template=*/true,
16211 /*is_namespace=*/false,
16212 check_dependency_p
,
16213 /*ambiguous_decls=*/NULL
,
16216 decl
= strip_using_decl (decl
);
16218 /* If DECL is a template, then the name was a template-name. */
16219 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16221 if (TREE_DEPRECATED (decl
)
16222 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16223 warn_deprecated_use (decl
, NULL_TREE
);
16227 /* The standard does not explicitly indicate whether a name that
16228 names a set of overloaded declarations, some of which are
16229 templates, is a template-name. However, such a name should
16230 be a template-name; otherwise, there is no way to form a
16231 template-id for the overloaded templates. */
16232 bool found
= false;
16234 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16235 !found
&& iter
; ++iter
)
16236 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16241 /* The name does not name a template. */
16242 cp_parser_error (parser
, "expected template-name");
16243 return error_mark_node
;
16247 /* If DECL is dependent, and refers to a function, then just return
16248 its name; we will look it up again during template instantiation. */
16249 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16251 tree scope
= ovl_scope (decl
);
16252 if (TYPE_P (scope
) && dependent_type_p (scope
))
16259 /* Parse a template-argument-list.
16261 template-argument-list:
16262 template-argument ... [opt]
16263 template-argument-list , template-argument ... [opt]
16265 Returns a TREE_VEC containing the arguments. */
16268 cp_parser_template_argument_list (cp_parser
* parser
)
16270 tree fixed_args
[10];
16271 unsigned n_args
= 0;
16272 unsigned alloced
= 10;
16273 tree
*arg_ary
= fixed_args
;
16275 bool saved_in_template_argument_list_p
;
16277 bool saved_non_ice_p
;
16279 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16280 parser
->in_template_argument_list_p
= true;
16281 /* Even if the template-id appears in an integral
16282 constant-expression, the contents of the argument list do
16284 saved_ice_p
= parser
->integral_constant_expression_p
;
16285 parser
->integral_constant_expression_p
= false;
16286 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16287 parser
->non_integral_constant_expression_p
= false;
16289 /* Parse the arguments. */
16295 /* Consume the comma. */
16296 cp_lexer_consume_token (parser
->lexer
);
16298 /* Parse the template-argument. */
16299 argument
= cp_parser_template_argument (parser
);
16301 /* If the next token is an ellipsis, we're expanding a template
16303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16305 if (argument
== error_mark_node
)
16307 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16308 error_at (token
->location
,
16309 "expected parameter pack before %<...%>");
16311 /* Consume the `...' token. */
16312 cp_lexer_consume_token (parser
->lexer
);
16314 /* Make the argument into a TYPE_PACK_EXPANSION or
16315 EXPR_PACK_EXPANSION. */
16316 argument
= make_pack_expansion (argument
);
16319 if (n_args
== alloced
)
16323 if (arg_ary
== fixed_args
)
16325 arg_ary
= XNEWVEC (tree
, alloced
);
16326 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16329 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16331 arg_ary
[n_args
++] = argument
;
16333 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16335 vec
= make_tree_vec (n_args
);
16338 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16340 if (arg_ary
!= fixed_args
)
16342 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16343 parser
->integral_constant_expression_p
= saved_ice_p
;
16344 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16350 /* Parse a template-argument.
16353 assignment-expression
16357 The representation is that of an assignment-expression, type-id, or
16358 id-expression -- except that the qualified id-expression is
16359 evaluated, so that the value returned is either a DECL or an
16362 Although the standard says "assignment-expression", it forbids
16363 throw-expressions or assignments in the template argument.
16364 Therefore, we use "conditional-expression" instead. */
16367 cp_parser_template_argument (cp_parser
* parser
)
16372 bool maybe_type_id
= false;
16373 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16374 location_t loc
= 0;
16377 /* There's really no way to know what we're looking at, so we just
16378 try each alternative in order.
16382 In a template-argument, an ambiguity between a type-id and an
16383 expression is resolved to a type-id, regardless of the form of
16384 the corresponding template-parameter.
16386 Therefore, we try a type-id first. */
16387 cp_parser_parse_tentatively (parser
);
16388 argument
= cp_parser_template_type_arg (parser
);
16389 /* If there was no error parsing the type-id but the next token is a
16390 '>>', our behavior depends on which dialect of C++ we're
16391 parsing. In C++98, we probably found a typo for '> >'. But there
16392 are type-id which are also valid expressions. For instance:
16394 struct X { int operator >> (int); };
16395 template <int V> struct Foo {};
16398 Here 'X()' is a valid type-id of a function type, but the user just
16399 wanted to write the expression "X() >> 5". Thus, we remember that we
16400 found a valid type-id, but we still try to parse the argument as an
16401 expression to see what happens.
16403 In C++0x, the '>>' will be considered two separate '>'
16405 if (!cp_parser_error_occurred (parser
)
16406 && cxx_dialect
== cxx98
16407 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16409 maybe_type_id
= true;
16410 cp_parser_abort_tentative_parse (parser
);
16414 /* If the next token isn't a `,' or a `>', then this argument wasn't
16415 really finished. This means that the argument is not a valid
16417 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16418 cp_parser_error (parser
, "expected template-argument");
16419 /* If that worked, we're done. */
16420 if (cp_parser_parse_definitely (parser
))
16423 /* We're still not sure what the argument will be. */
16424 cp_parser_parse_tentatively (parser
);
16425 /* Try a template. */
16426 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16427 argument
= cp_parser_id_expression (parser
,
16428 /*template_keyword_p=*/false,
16429 /*check_dependency_p=*/true,
16431 /*declarator_p=*/false,
16432 /*optional_p=*/false);
16433 /* If the next token isn't a `,' or a `>', then this argument wasn't
16434 really finished. */
16435 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16436 cp_parser_error (parser
, "expected template-argument");
16437 if (!cp_parser_error_occurred (parser
))
16439 /* Figure out what is being referred to. If the id-expression
16440 was for a class template specialization, then we will have a
16441 TYPE_DECL at this point. There is no need to do name lookup
16442 at this point in that case. */
16443 if (TREE_CODE (argument
) != TYPE_DECL
)
16444 argument
= cp_parser_lookup_name (parser
, argument
,
16446 /*is_template=*/template_p
,
16447 /*is_namespace=*/false,
16448 /*check_dependency=*/true,
16449 /*ambiguous_decls=*/NULL
,
16450 argument_start_token
->location
);
16451 /* Handle a constrained-type-specifier for a non-type template
16453 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16455 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16456 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16457 cp_parser_error (parser
, "expected template-name");
16459 if (cp_parser_parse_definitely (parser
))
16461 if (TREE_DEPRECATED (argument
))
16462 warn_deprecated_use (argument
, NULL_TREE
);
16465 /* It must be a non-type argument. In C++17 any constant-expression is
16467 if (cxx_dialect
> cxx14
)
16470 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16472 -- an integral constant-expression of integral or enumeration
16475 -- the name of a non-type template-parameter; or
16477 -- the name of an object or function with external linkage...
16479 -- the address of an object or function with external linkage...
16481 -- a pointer to member... */
16482 /* Look for a non-type template parameter. */
16483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16485 cp_parser_parse_tentatively (parser
);
16486 argument
= cp_parser_primary_expression (parser
,
16487 /*address_p=*/false,
16489 /*template_arg_p=*/true,
16491 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16492 || !cp_parser_next_token_ends_template_argument_p (parser
))
16493 cp_parser_simulate_error (parser
);
16494 if (cp_parser_parse_definitely (parser
))
16498 /* If the next token is "&", the argument must be the address of an
16499 object or function with external linkage. */
16500 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16503 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16504 cp_lexer_consume_token (parser
->lexer
);
16506 /* See if we might have an id-expression. */
16507 token
= cp_lexer_peek_token (parser
->lexer
);
16508 if (token
->type
== CPP_NAME
16509 || token
->keyword
== RID_OPERATOR
16510 || token
->type
== CPP_SCOPE
16511 || token
->type
== CPP_TEMPLATE_ID
16512 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16514 cp_parser_parse_tentatively (parser
);
16515 argument
= cp_parser_primary_expression (parser
,
16518 /*template_arg_p=*/true,
16520 if (cp_parser_error_occurred (parser
)
16521 || !cp_parser_next_token_ends_template_argument_p (parser
))
16522 cp_parser_abort_tentative_parse (parser
);
16527 if (INDIRECT_REF_P (argument
))
16529 /* Strip the dereference temporarily. */
16530 gcc_assert (REFERENCE_REF_P (argument
));
16531 argument
= TREE_OPERAND (argument
, 0);
16534 /* If we're in a template, we represent a qualified-id referring
16535 to a static data member as a SCOPE_REF even if the scope isn't
16536 dependent so that we can check access control later. */
16538 if (TREE_CODE (probe
) == SCOPE_REF
)
16539 probe
= TREE_OPERAND (probe
, 1);
16542 /* A variable without external linkage might still be a
16543 valid constant-expression, so no error is issued here
16544 if the external-linkage check fails. */
16545 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16546 cp_parser_simulate_error (parser
);
16548 else if (is_overloaded_fn (argument
))
16549 /* All overloaded functions are allowed; if the external
16550 linkage test does not pass, an error will be issued
16554 && (TREE_CODE (argument
) == OFFSET_REF
16555 || TREE_CODE (argument
) == SCOPE_REF
))
16556 /* A pointer-to-member. */
16558 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16561 cp_parser_simulate_error (parser
);
16563 if (cp_parser_parse_definitely (parser
))
16566 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16567 tf_warning_or_error
);
16569 argument
= convert_from_reference (argument
);
16574 /* If the argument started with "&", there are no other valid
16575 alternatives at this point. */
16578 cp_parser_error (parser
, "invalid non-type template argument");
16579 return error_mark_node
;
16583 /* If the argument wasn't successfully parsed as a type-id followed
16584 by '>>', the argument can only be a constant expression now.
16585 Otherwise, we try parsing the constant-expression tentatively,
16586 because the argument could really be a type-id. */
16588 cp_parser_parse_tentatively (parser
);
16590 if (cxx_dialect
<= cxx14
)
16591 argument
= cp_parser_constant_expression (parser
);
16594 /* With C++17 generalized non-type template arguments we need to handle
16595 lvalue constant expressions, too. */
16596 argument
= cp_parser_assignment_expression (parser
);
16597 require_potential_constant_expression (argument
);
16600 if (!maybe_type_id
)
16602 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16603 cp_parser_error (parser
, "expected template-argument");
16604 if (cp_parser_parse_definitely (parser
))
16606 /* We did our best to parse the argument as a non type-id, but that
16607 was the only alternative that matched (albeit with a '>' after
16608 it). We can assume it's just a typo from the user, and a
16609 diagnostic will then be issued. */
16610 return cp_parser_template_type_arg (parser
);
16613 /* Parse an explicit-instantiation.
16615 explicit-instantiation:
16616 template declaration
16618 Although the standard says `declaration', what it really means is:
16620 explicit-instantiation:
16621 template decl-specifier-seq [opt] declarator [opt] ;
16623 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16624 supposed to be allowed. A defect report has been filed about this
16629 explicit-instantiation:
16630 storage-class-specifier template
16631 decl-specifier-seq [opt] declarator [opt] ;
16632 function-specifier template
16633 decl-specifier-seq [opt] declarator [opt] ; */
16636 cp_parser_explicit_instantiation (cp_parser
* parser
)
16638 int declares_class_or_enum
;
16639 cp_decl_specifier_seq decl_specifiers
;
16640 tree extension_specifier
= NULL_TREE
;
16642 timevar_push (TV_TEMPLATE_INST
);
16644 /* Look for an (optional) storage-class-specifier or
16645 function-specifier. */
16646 if (cp_parser_allow_gnu_extensions_p (parser
))
16648 extension_specifier
16649 = cp_parser_storage_class_specifier_opt (parser
);
16650 if (!extension_specifier
)
16651 extension_specifier
16652 = cp_parser_function_specifier_opt (parser
,
16653 /*decl_specs=*/NULL
);
16656 /* Look for the `template' keyword. */
16657 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16658 /* Let the front end know that we are processing an explicit
16660 begin_explicit_instantiation ();
16661 /* [temp.explicit] says that we are supposed to ignore access
16662 control while processing explicit instantiation directives. */
16663 push_deferring_access_checks (dk_no_check
);
16664 /* Parse a decl-specifier-seq. */
16665 cp_parser_decl_specifier_seq (parser
,
16666 CP_PARSER_FLAGS_OPTIONAL
,
16668 &declares_class_or_enum
);
16669 /* If there was exactly one decl-specifier, and it declared a class,
16670 and there's no declarator, then we have an explicit type
16672 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16676 type
= check_tag_decl (&decl_specifiers
,
16677 /*explicit_type_instantiation_p=*/true);
16678 /* Turn access control back on for names used during
16679 template instantiation. */
16680 pop_deferring_access_checks ();
16682 do_type_instantiation (type
, extension_specifier
,
16683 /*complain=*/tf_error
);
16687 cp_declarator
*declarator
;
16690 /* Parse the declarator. */
16692 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16693 /*ctor_dtor_or_conv_p=*/NULL
,
16694 /*parenthesized_p=*/NULL
,
16695 /*member_p=*/false,
16696 /*friend_p=*/false);
16697 if (declares_class_or_enum
& 2)
16698 cp_parser_check_for_definition_in_return_type (declarator
,
16699 decl_specifiers
.type
,
16700 decl_specifiers
.locations
[ds_type_spec
]);
16701 if (declarator
!= cp_error_declarator
)
16703 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16704 permerror (decl_specifiers
.locations
[ds_inline
],
16705 "explicit instantiation shall not use"
16706 " %<inline%> specifier");
16707 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16708 permerror (decl_specifiers
.locations
[ds_constexpr
],
16709 "explicit instantiation shall not use"
16710 " %<constexpr%> specifier");
16712 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16713 NORMAL
, 0, &decl_specifiers
.attributes
);
16714 /* Turn access control back on for names used during
16715 template instantiation. */
16716 pop_deferring_access_checks ();
16717 /* Do the explicit instantiation. */
16718 do_decl_instantiation (decl
, extension_specifier
);
16722 pop_deferring_access_checks ();
16723 /* Skip the body of the explicit instantiation. */
16724 cp_parser_skip_to_end_of_statement (parser
);
16727 /* We're done with the instantiation. */
16728 end_explicit_instantiation ();
16730 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16732 timevar_pop (TV_TEMPLATE_INST
);
16735 /* Parse an explicit-specialization.
16737 explicit-specialization:
16738 template < > declaration
16740 Although the standard says `declaration', what it really means is:
16742 explicit-specialization:
16743 template <> decl-specifier [opt] init-declarator [opt] ;
16744 template <> function-definition
16745 template <> explicit-specialization
16746 template <> template-declaration */
16749 cp_parser_explicit_specialization (cp_parser
* parser
)
16751 bool need_lang_pop
;
16752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16754 /* Look for the `template' keyword. */
16755 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16756 /* Look for the `<'. */
16757 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16758 /* Look for the `>'. */
16759 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16760 /* We have processed another parameter list. */
16761 ++parser
->num_template_parameter_lists
;
16764 A template ... explicit specialization ... shall not have C
16766 if (current_lang_name
== lang_name_c
)
16768 error_at (token
->location
, "template specialization with C linkage");
16769 maybe_show_extern_c_location ();
16770 /* Give it C++ linkage to avoid confusing other parts of the
16772 push_lang_context (lang_name_cplusplus
);
16773 need_lang_pop
= true;
16776 need_lang_pop
= false;
16777 /* Let the front end know that we are beginning a specialization. */
16778 if (!begin_specialization ())
16780 end_specialization ();
16784 /* If the next keyword is `template', we need to figure out whether
16785 or not we're looking a template-declaration. */
16786 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16788 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16789 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16790 cp_parser_template_declaration_after_export (parser
,
16791 /*member_p=*/false);
16793 cp_parser_explicit_specialization (parser
);
16796 /* Parse the dependent declaration. */
16797 cp_parser_single_declaration (parser
,
16799 /*member_p=*/false,
16800 /*explicit_specialization_p=*/true,
16801 /*friend_p=*/NULL
);
16802 /* We're done with the specialization. */
16803 end_specialization ();
16804 /* For the erroneous case of a template with C linkage, we pushed an
16805 implicit C++ linkage scope; exit that scope now. */
16807 pop_lang_context ();
16808 /* We're done with this parameter list. */
16809 --parser
->num_template_parameter_lists
;
16812 /* Parse a type-specifier.
16815 simple-type-specifier
16818 elaborated-type-specifier
16826 Returns a representation of the type-specifier. For a
16827 class-specifier, enum-specifier, or elaborated-type-specifier, a
16828 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16830 The parser flags FLAGS is used to control type-specifier parsing.
16832 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16833 in a decl-specifier-seq.
16835 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16836 class-specifier, enum-specifier, or elaborated-type-specifier, then
16837 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16838 if a type is declared; 2 if it is defined. Otherwise, it is set to
16841 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16842 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16843 is set to FALSE. */
16846 cp_parser_type_specifier (cp_parser
* parser
,
16847 cp_parser_flags flags
,
16848 cp_decl_specifier_seq
*decl_specs
,
16849 bool is_declaration
,
16850 int* declares_class_or_enum
,
16851 bool* is_cv_qualifier
)
16853 tree type_spec
= NULL_TREE
;
16856 cp_decl_spec ds
= ds_last
;
16858 /* Assume this type-specifier does not declare a new type. */
16859 if (declares_class_or_enum
)
16860 *declares_class_or_enum
= 0;
16861 /* And that it does not specify a cv-qualifier. */
16862 if (is_cv_qualifier
)
16863 *is_cv_qualifier
= false;
16864 /* Peek at the next token. */
16865 token
= cp_lexer_peek_token (parser
->lexer
);
16867 /* If we're looking at a keyword, we can use that to guide the
16868 production we choose. */
16869 keyword
= token
->keyword
;
16873 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16874 goto elaborated_type_specifier
;
16876 /* Look for the enum-specifier. */
16877 type_spec
= cp_parser_enum_specifier (parser
);
16878 /* If that worked, we're done. */
16881 if (declares_class_or_enum
)
16882 *declares_class_or_enum
= 2;
16884 cp_parser_set_decl_spec_type (decl_specs
,
16887 /*type_definition_p=*/true);
16891 goto elaborated_type_specifier
;
16893 /* Any of these indicate either a class-specifier, or an
16894 elaborated-type-specifier. */
16898 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16899 goto elaborated_type_specifier
;
16901 /* Parse tentatively so that we can back up if we don't find a
16902 class-specifier. */
16903 cp_parser_parse_tentatively (parser
);
16904 /* Look for the class-specifier. */
16905 type_spec
= cp_parser_class_specifier (parser
);
16906 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16907 /* If that worked, we're done. */
16908 if (cp_parser_parse_definitely (parser
))
16910 if (declares_class_or_enum
)
16911 *declares_class_or_enum
= 2;
16913 cp_parser_set_decl_spec_type (decl_specs
,
16916 /*type_definition_p=*/true);
16920 /* Fall through. */
16921 elaborated_type_specifier
:
16922 /* We're declaring (not defining) a class or enum. */
16923 if (declares_class_or_enum
)
16924 *declares_class_or_enum
= 1;
16926 /* Fall through. */
16928 /* Look for an elaborated-type-specifier. */
16930 = (cp_parser_elaborated_type_specifier
16932 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16935 cp_parser_set_decl_spec_type (decl_specs
,
16938 /*type_definition_p=*/false);
16943 if (is_cv_qualifier
)
16944 *is_cv_qualifier
= true;
16949 if (is_cv_qualifier
)
16950 *is_cv_qualifier
= true;
16955 if (is_cv_qualifier
)
16956 *is_cv_qualifier
= true;
16960 /* The `__complex__' keyword is a GNU extension. */
16968 /* Handle simple keywords. */
16973 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16974 decl_specs
->any_specifiers_p
= true;
16976 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16979 /* If we do not already have a type-specifier, assume we are looking
16980 at a simple-type-specifier. */
16981 type_spec
= cp_parser_simple_type_specifier (parser
,
16985 /* If we didn't find a type-specifier, and a type-specifier was not
16986 optional in this context, issue an error message. */
16987 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16989 cp_parser_error (parser
, "expected type specifier");
16990 return error_mark_node
;
16996 /* Parse a simple-type-specifier.
16998 simple-type-specifier:
16999 :: [opt] nested-name-specifier [opt] type-name
17000 :: [opt] nested-name-specifier template template-id
17015 simple-type-specifier:
17017 decltype ( expression )
17020 __underlying_type ( type-id )
17024 nested-name-specifier(opt) template-name
17028 simple-type-specifier:
17030 __typeof__ unary-expression
17031 __typeof__ ( type-id )
17032 __typeof__ ( type-id ) { initializer-list , [opt] }
17034 Concepts Extension:
17036 simple-type-specifier:
17037 constrained-type-specifier
17039 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17040 appropriately updated. */
17043 cp_parser_simple_type_specifier (cp_parser
* parser
,
17044 cp_decl_specifier_seq
*decl_specs
,
17045 cp_parser_flags flags
)
17047 tree type
= NULL_TREE
;
17051 /* Peek at the next token. */
17052 token
= cp_lexer_peek_token (parser
->lexer
);
17054 /* If we're looking at a keyword, things are easy. */
17055 switch (token
->keyword
)
17059 decl_specs
->explicit_char_p
= true;
17060 type
= char_type_node
;
17063 type
= char16_type_node
;
17066 type
= char32_type_node
;
17069 type
= wchar_type_node
;
17072 type
= boolean_type_node
;
17075 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17076 type
= short_integer_type_node
;
17080 decl_specs
->explicit_int_p
= true;
17081 type
= integer_type_node
;
17087 idx
= token
->keyword
- RID_INT_N_0
;
17088 if (! int_n_enabled_p
[idx
])
17092 decl_specs
->explicit_intN_p
= true;
17093 decl_specs
->int_n_idx
= idx
;
17095 type
= int_n_trees
[idx
].signed_type
;
17099 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17100 type
= long_integer_type_node
;
17103 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17104 type
= integer_type_node
;
17107 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17108 type
= unsigned_type_node
;
17111 type
= float_type_node
;
17114 type
= double_type_node
;
17117 type
= void_type_node
;
17121 maybe_warn_cpp0x (CPP0X_AUTO
);
17122 if (parser
->auto_is_implicit_function_template_parm_p
)
17124 /* The 'auto' might be the placeholder return type for a function decl
17125 with trailing return type. */
17126 bool have_trailing_return_fn_decl
= false;
17128 cp_parser_parse_tentatively (parser
);
17129 cp_lexer_consume_token (parser
->lexer
);
17130 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17131 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17132 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17133 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17137 cp_lexer_consume_token (parser
->lexer
);
17138 cp_parser_skip_to_closing_parenthesis (parser
,
17139 /*recovering*/false,
17141 /*consume_paren*/true);
17145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17147 have_trailing_return_fn_decl
= true;
17151 cp_lexer_consume_token (parser
->lexer
);
17153 cp_parser_abort_tentative_parse (parser
);
17155 if (have_trailing_return_fn_decl
)
17157 type
= make_auto ();
17161 if (cxx_dialect
>= cxx14
)
17163 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17164 type
= TREE_TYPE (type
);
17167 type
= error_mark_node
;
17169 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17171 if (cxx_dialect
< cxx14
)
17172 error_at (token
->location
,
17173 "use of %<auto%> in lambda parameter declaration "
17174 "only available with "
17175 "-std=c++14 or -std=gnu++14");
17177 else if (cxx_dialect
< cxx14
)
17178 error_at (token
->location
,
17179 "use of %<auto%> in parameter declaration "
17180 "only available with "
17181 "-std=c++14 or -std=gnu++14");
17182 else if (!flag_concepts
)
17183 pedwarn (token
->location
, 0,
17184 "use of %<auto%> in parameter declaration "
17185 "only available with -fconcepts");
17188 type
= make_auto ();
17192 /* Since DR 743, decltype can either be a simple-type-specifier by
17193 itself or begin a nested-name-specifier. Parsing it will replace
17194 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17195 handling below decide what to do. */
17196 cp_parser_decltype (parser
);
17197 cp_lexer_set_token_position (parser
->lexer
, token
);
17201 /* Consume the `typeof' token. */
17202 cp_lexer_consume_token (parser
->lexer
);
17203 /* Parse the operand to `typeof'. */
17204 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17205 /* If it is not already a TYPE, take its type. */
17206 if (!TYPE_P (type
))
17207 type
= finish_typeof (type
);
17210 cp_parser_set_decl_spec_type (decl_specs
, type
,
17212 /*type_definition_p=*/false);
17216 case RID_UNDERLYING_TYPE
:
17217 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17219 cp_parser_set_decl_spec_type (decl_specs
, type
,
17221 /*type_definition_p=*/false);
17226 case RID_DIRECT_BASES
:
17227 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17229 cp_parser_set_decl_spec_type (decl_specs
, type
,
17231 /*type_definition_p=*/false);
17237 /* If token is an already-parsed decltype not followed by ::,
17238 it's a simple-type-specifier. */
17239 if (token
->type
== CPP_DECLTYPE
17240 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17242 type
= saved_checks_value (token
->u
.tree_check_value
);
17245 cp_parser_set_decl_spec_type (decl_specs
, type
,
17247 /*type_definition_p=*/false);
17248 /* Remember that we are handling a decltype in order to
17249 implement the resolution of DR 1510 when the argument
17250 isn't instantiation dependent. */
17251 decl_specs
->decltype_p
= true;
17253 cp_lexer_consume_token (parser
->lexer
);
17257 /* If the type-specifier was for a built-in type, we're done. */
17260 /* Record the type. */
17262 && (token
->keyword
!= RID_SIGNED
17263 && token
->keyword
!= RID_UNSIGNED
17264 && token
->keyword
!= RID_SHORT
17265 && token
->keyword
!= RID_LONG
))
17266 cp_parser_set_decl_spec_type (decl_specs
,
17269 /*type_definition_p=*/false);
17271 decl_specs
->any_specifiers_p
= true;
17273 /* Consume the token. */
17274 cp_lexer_consume_token (parser
->lexer
);
17276 if (type
== error_mark_node
)
17277 return error_mark_node
;
17279 /* There is no valid C++ program where a non-template type is
17280 followed by a "<". That usually indicates that the user thought
17281 that the type was a template. */
17282 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17285 return TYPE_NAME (type
);
17288 /* The type-specifier must be a user-defined type. */
17289 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17294 /* Don't gobble tokens or issue error messages if this is an
17295 optional type-specifier. */
17296 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17297 cp_parser_parse_tentatively (parser
);
17299 token
= cp_lexer_peek_token (parser
->lexer
);
17301 /* Look for the optional `::' operator. */
17303 = (cp_parser_global_scope_opt (parser
,
17304 /*current_scope_valid_p=*/false)
17306 /* Look for the nested-name specifier. */
17308 = (cp_parser_nested_name_specifier_opt (parser
,
17309 /*typename_keyword_p=*/false,
17310 /*check_dependency_p=*/true,
17312 /*is_declaration=*/false)
17314 /* If we have seen a nested-name-specifier, and the next token
17315 is `template', then we are using the template-id production. */
17317 && cp_parser_optional_template_keyword (parser
))
17319 /* Look for the template-id. */
17320 type
= cp_parser_template_id (parser
,
17321 /*template_keyword_p=*/true,
17322 /*check_dependency_p=*/true,
17324 /*is_declaration=*/false);
17325 /* If the template-id did not name a type, we are out of
17327 if (TREE_CODE (type
) != TYPE_DECL
)
17329 cp_parser_error (parser
, "expected template-id for type");
17333 /* Otherwise, look for a type-name. */
17335 type
= cp_parser_type_name (parser
);
17336 /* Keep track of all name-lookups performed in class scopes. */
17340 && TREE_CODE (type
) == TYPE_DECL
17341 && identifier_p (DECL_NAME (type
)))
17342 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17343 /* If it didn't work out, we don't have a TYPE. */
17344 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17345 && !cp_parser_parse_definitely (parser
))
17347 if (!type
&& cxx_dialect
>= cxx17
)
17349 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17350 cp_parser_parse_tentatively (parser
);
17352 cp_parser_global_scope_opt (parser
,
17353 /*current_scope_valid_p=*/false);
17354 cp_parser_nested_name_specifier_opt (parser
,
17355 /*typename_keyword_p=*/false,
17356 /*check_dependency_p=*/true,
17358 /*is_declaration=*/false);
17359 tree name
= cp_parser_identifier (parser
);
17360 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17361 && parser
->scope
!= error_mark_node
)
17363 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17365 /*is_template=*/false,
17366 /*is_namespace=*/false,
17367 /*check_dependency=*/true,
17368 /*ambiguous_decls=*/NULL
,
17370 if (tmpl
&& tmpl
!= error_mark_node
17371 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17372 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17373 type
= make_template_placeholder (tmpl
);
17376 type
= error_mark_node
;
17377 if (!cp_parser_simulate_error (parser
))
17378 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17379 NLE_TYPE
, token
->location
);
17383 type
= error_mark_node
;
17385 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17386 && !cp_parser_parse_definitely (parser
))
17389 if (type
&& decl_specs
)
17390 cp_parser_set_decl_spec_type (decl_specs
, type
,
17392 /*type_definition_p=*/false);
17395 /* If we didn't get a type-name, issue an error message. */
17396 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17398 cp_parser_error (parser
, "expected type-name");
17399 return error_mark_node
;
17402 if (type
&& type
!= error_mark_node
)
17404 /* See if TYPE is an Objective-C type, and if so, parse and
17405 accept any protocol references following it. Do this before
17406 the cp_parser_check_for_invalid_template_id() call, because
17407 Objective-C types can be followed by '<...>' which would
17408 enclose protocol names rather than template arguments, and so
17409 everything is fine. */
17410 if (c_dialect_objc () && !parser
->scope
17411 && (objc_is_id (type
) || objc_is_class_name (type
)))
17413 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17414 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17416 /* Clobber the "unqualified" type previously entered into
17417 DECL_SPECS with the new, improved protocol-qualified version. */
17419 decl_specs
->type
= qual_type
;
17424 /* There is no valid C++ program where a non-template type is
17425 followed by a "<". That usually indicates that the user
17426 thought that the type was a template. */
17427 cp_parser_check_for_invalid_template_id (parser
, type
,
17435 /* Parse a type-name.
17441 simple-template-id [in c++0x]
17458 Returns a TYPE_DECL for the type. */
17461 cp_parser_type_name (cp_parser
* parser
)
17463 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17468 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17472 /* We can't know yet whether it is a class-name or not. */
17473 cp_parser_parse_tentatively (parser
);
17474 /* Try a class-name. */
17475 type_decl
= cp_parser_class_name (parser
,
17476 typename_keyword_p
,
17477 /*template_keyword_p=*/false,
17479 /*check_dependency_p=*/true,
17480 /*class_head_p=*/false,
17481 /*is_declaration=*/false);
17482 /* If it's not a class-name, keep looking. */
17483 if (!cp_parser_parse_definitely (parser
))
17485 if (cxx_dialect
< cxx11
)
17486 /* It must be a typedef-name or an enum-name. */
17487 return cp_parser_nonclass_name (parser
);
17489 cp_parser_parse_tentatively (parser
);
17490 /* It is either a simple-template-id representing an
17491 instantiation of an alias template... */
17492 type_decl
= cp_parser_template_id (parser
,
17493 /*template_keyword_p=*/false,
17494 /*check_dependency_p=*/true,
17496 /*is_declaration=*/false);
17497 /* Note that this must be an instantiation of an alias template
17498 because [temp.names]/6 says:
17500 A template-id that names an alias template specialization
17503 Whereas [temp.names]/7 says:
17505 A simple-template-id that names a class template
17506 specialization is a class-name.
17508 With concepts, this could also be a partial-concept-id that
17509 declares a non-type template parameter. */
17510 if (type_decl
!= NULL_TREE
17511 && TREE_CODE (type_decl
) == TYPE_DECL
17512 && TYPE_DECL_ALIAS_P (type_decl
))
17513 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17514 else if (is_constrained_parameter (type_decl
))
17515 /* Don't do anything. */ ;
17517 cp_parser_simulate_error (parser
);
17519 if (!cp_parser_parse_definitely (parser
))
17520 /* ... Or a typedef-name or an enum-name. */
17521 return cp_parser_nonclass_name (parser
);
17527 /* Check if DECL and ARGS can form a constrained-type-specifier.
17528 If ARGS is non-null, we try to form a concept check of the
17529 form DECL<?, ARGS> where ? is a wildcard that matches any
17530 kind of template argument. If ARGS is NULL, then we try to
17531 form a concept check of the form DECL<?>. */
17534 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17535 tree decl
, tree args
)
17537 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17539 /* If we a constrained-type-specifier cannot be deduced. */
17540 if (parser
->prevent_constrained_type_specifiers
)
17543 /* A constrained type specifier can only be found in an
17544 overload set or as a reference to a template declaration.
17546 FIXME: This might be masking a bug. It's possible that
17547 that the deduction below is causing template specializations
17548 to be formed with the wildcard as an argument. */
17549 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17552 /* Try to build a call expression that evaluates the
17553 concept. This can fail if the overload set refers
17554 only to non-templates. */
17555 tree placeholder
= build_nt (WILDCARD_DECL
);
17556 tree check
= build_concept_check (decl
, placeholder
, args
);
17557 if (check
== error_mark_node
)
17560 /* Deduce the checked constraint and the prototype parameter.
17562 FIXME: In certain cases, failure to deduce should be a
17563 diagnosable error. */
17566 if (!deduce_constrained_parameter (check
, conc
, proto
))
17569 /* In template parameter scope, this results in a constrained
17570 parameter. Return a descriptor of that parm. */
17571 if (processing_template_parmlist
)
17572 return build_constrained_parameter (conc
, proto
, args
);
17574 /* In a parameter-declaration-clause, constrained-type
17575 specifiers result in invented template parameters. */
17576 if (parser
->auto_is_implicit_function_template_parm_p
)
17578 tree x
= build_constrained_parameter (conc
, proto
, args
);
17579 return synthesize_implicit_template_parm (parser
, x
);
17583 /* Otherwise, we're in a context where the constrained
17584 type name is deduced and the constraint applies
17585 after deduction. */
17586 return make_constrained_auto (conc
, args
);
17592 /* If DECL refers to a concept, return a TYPE_DECL representing
17593 the result of using the constrained type specifier in the
17594 current context. DECL refers to a concept if
17596 - it is an overload set containing a function concept taking a single
17599 - it is a variable concept taking a single type argument. */
17602 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17605 && (TREE_CODE (decl
) == OVERLOAD
17606 || BASELINK_P (decl
)
17607 || variable_concept_p (decl
)))
17608 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17613 /* Check if DECL and ARGS form a partial-concept-id. If so,
17614 assign ID to the resulting constrained placeholder.
17616 Returns true if the partial-concept-id designates a placeholder
17617 and false otherwise. Note that *id is set to NULL_TREE in
17621 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17623 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17626 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17638 Returns a TYPE_DECL for the type. */
17641 cp_parser_nonclass_name (cp_parser
* parser
)
17646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17647 identifier
= cp_parser_identifier (parser
);
17648 if (identifier
== error_mark_node
)
17649 return error_mark_node
;
17651 /* Look up the type-name. */
17652 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17654 type_decl
= strip_using_decl (type_decl
);
17656 /* If we found an overload set, then it may refer to a concept-name. */
17657 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17660 if (TREE_CODE (type_decl
) != TYPE_DECL
17661 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17663 /* See if this is an Objective-C type. */
17664 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17665 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17667 type_decl
= TYPE_NAME (type
);
17670 /* Issue an error if we did not find a type-name. */
17671 if (TREE_CODE (type_decl
) != TYPE_DECL
17672 /* In Objective-C, we have the complication that class names are
17673 normally type names and start declarations (eg, the
17674 "NSObject" in "NSObject *object;"), but can be used in an
17675 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17676 is an expression. So, a classname followed by a dot is not a
17677 valid type-name. */
17678 || (objc_is_class_name (TREE_TYPE (type_decl
))
17679 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17681 if (!cp_parser_simulate_error (parser
))
17682 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17683 NLE_TYPE
, token
->location
);
17684 return error_mark_node
;
17686 /* Remember that the name was used in the definition of the
17687 current class so that we can check later to see if the
17688 meaning would have been different after the class was
17689 entirely defined. */
17690 else if (type_decl
!= error_mark_node
17692 maybe_note_name_used_in_class (identifier
, type_decl
);
17697 /* Parse an elaborated-type-specifier. Note that the grammar given
17698 here incorporates the resolution to DR68.
17700 elaborated-type-specifier:
17701 class-key :: [opt] nested-name-specifier [opt] identifier
17702 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17703 enum-key :: [opt] nested-name-specifier [opt] identifier
17704 typename :: [opt] nested-name-specifier identifier
17705 typename :: [opt] nested-name-specifier template [opt]
17710 elaborated-type-specifier:
17711 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17712 class-key attributes :: [opt] nested-name-specifier [opt]
17713 template [opt] template-id
17714 enum attributes :: [opt] nested-name-specifier [opt] identifier
17716 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17717 declared `friend'. If IS_DECLARATION is TRUE, then this
17718 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17719 something is being declared.
17721 Returns the TYPE specified. */
17724 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17726 bool is_declaration
)
17728 enum tag_types tag_type
;
17730 tree type
= NULL_TREE
;
17731 tree attributes
= NULL_TREE
;
17733 cp_token
*token
= NULL
;
17735 /* See if we're looking at the `enum' keyword. */
17736 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17738 /* Consume the `enum' token. */
17739 cp_lexer_consume_token (parser
->lexer
);
17740 /* Remember that it's an enumeration type. */
17741 tag_type
= enum_type
;
17742 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17743 enums) is used here. */
17744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17745 if (cp_parser_is_keyword (token
, RID_CLASS
)
17746 || cp_parser_is_keyword (token
, RID_STRUCT
))
17748 gcc_rich_location
richloc (token
->location
);
17749 richloc
.add_range (input_location
, false);
17750 richloc
.add_fixit_remove ();
17751 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17752 "a scoped enum must not use the %qD keyword",
17754 /* Consume the `struct' or `class' and parse it anyway. */
17755 cp_lexer_consume_token (parser
->lexer
);
17757 /* Parse the attributes. */
17758 attributes
= cp_parser_attributes_opt (parser
);
17760 /* Or, it might be `typename'. */
17761 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17764 /* Consume the `typename' token. */
17765 cp_lexer_consume_token (parser
->lexer
);
17766 /* Remember that it's a `typename' type. */
17767 tag_type
= typename_type
;
17769 /* Otherwise it must be a class-key. */
17772 tag_type
= cp_parser_class_key (parser
);
17773 if (tag_type
== none_type
)
17774 return error_mark_node
;
17775 /* Parse the attributes. */
17776 attributes
= cp_parser_attributes_opt (parser
);
17779 /* Look for the `::' operator. */
17780 globalscope
= cp_parser_global_scope_opt (parser
,
17781 /*current_scope_valid_p=*/false);
17782 /* Look for the nested-name-specifier. */
17783 tree nested_name_specifier
;
17784 if (tag_type
== typename_type
&& !globalscope
)
17786 nested_name_specifier
17787 = cp_parser_nested_name_specifier (parser
,
17788 /*typename_keyword_p=*/true,
17789 /*check_dependency_p=*/true,
17792 if (!nested_name_specifier
)
17793 return error_mark_node
;
17796 /* Even though `typename' is not present, the proposed resolution
17797 to Core Issue 180 says that in `class A<T>::B', `B' should be
17798 considered a type-name, even if `A<T>' is dependent. */
17799 nested_name_specifier
17800 = cp_parser_nested_name_specifier_opt (parser
,
17801 /*typename_keyword_p=*/true,
17802 /*check_dependency_p=*/true,
17805 /* For everything but enumeration types, consider a template-id.
17806 For an enumeration type, consider only a plain identifier. */
17807 if (tag_type
!= enum_type
)
17809 bool template_p
= false;
17812 /* Allow the `template' keyword. */
17813 template_p
= cp_parser_optional_template_keyword (parser
);
17814 /* If we didn't see `template', we don't know if there's a
17815 template-id or not. */
17817 cp_parser_parse_tentatively (parser
);
17818 /* Parse the template-id. */
17819 token
= cp_lexer_peek_token (parser
->lexer
);
17820 decl
= cp_parser_template_id (parser
, template_p
,
17821 /*check_dependency_p=*/true,
17824 /* If we didn't find a template-id, look for an ordinary
17826 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17828 /* We can get here when cp_parser_template_id, called by
17829 cp_parser_class_name with tag_type == none_type, succeeds
17830 and caches a BASELINK. Then, when called again here,
17831 instead of failing and returning an error_mark_node
17832 returns it (see template/typename17.C in C++11).
17833 ??? Could we diagnose this earlier? */
17834 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17836 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17837 type
= error_mark_node
;
17839 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17840 in effect, then we must assume that, upon instantiation, the
17841 template will correspond to a class. */
17842 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17843 && tag_type
== typename_type
)
17844 type
= make_typename_type (parser
->scope
, decl
,
17846 /*complain=*/tf_error
);
17847 /* If the `typename' keyword is in effect and DECL is not a type
17848 decl, then type is non existent. */
17849 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17851 else if (TREE_CODE (decl
) == TYPE_DECL
)
17853 type
= check_elaborated_type_specifier (tag_type
, decl
,
17854 /*allow_template_p=*/true);
17856 /* If the next token is a semicolon, this must be a specialization,
17857 instantiation, or friend declaration. Check the scope while we
17858 still know whether or not we had a nested-name-specifier. */
17859 if (type
!= error_mark_node
17860 && !nested_name_specifier
&& !is_friend
17861 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17862 check_unqualified_spec_or_inst (type
, token
->location
);
17864 else if (decl
== error_mark_node
)
17865 type
= error_mark_node
;
17870 token
= cp_lexer_peek_token (parser
->lexer
);
17871 identifier
= cp_parser_identifier (parser
);
17873 if (identifier
== error_mark_node
)
17875 parser
->scope
= NULL_TREE
;
17876 return error_mark_node
;
17879 /* For a `typename', we needn't call xref_tag. */
17880 if (tag_type
== typename_type
17881 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17882 return cp_parser_make_typename_type (parser
, identifier
,
17885 /* Template parameter lists apply only if we are not within a
17886 function parameter list. */
17887 bool template_parm_lists_apply
17888 = parser
->num_template_parameter_lists
;
17889 if (template_parm_lists_apply
)
17890 for (cp_binding_level
*s
= current_binding_level
;
17891 s
&& s
->kind
!= sk_template_parms
;
17892 s
= s
->level_chain
)
17893 if (s
->kind
== sk_function_parms
)
17894 template_parm_lists_apply
= false;
17896 /* Look up a qualified name in the usual way. */
17900 tree ambiguous_decls
;
17902 decl
= cp_parser_lookup_name (parser
, identifier
,
17904 /*is_template=*/false,
17905 /*is_namespace=*/false,
17906 /*check_dependency=*/true,
17910 /* If the lookup was ambiguous, an error will already have been
17912 if (ambiguous_decls
)
17913 return error_mark_node
;
17915 /* If we are parsing friend declaration, DECL may be a
17916 TEMPLATE_DECL tree node here. However, we need to check
17917 whether this TEMPLATE_DECL results in valid code. Consider
17918 the following example:
17921 template <class T> class C {};
17924 template <class T> friend class N::C; // #1, valid code
17926 template <class T> class Y {
17927 friend class N::C; // #2, invalid code
17930 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17931 name lookup of `N::C'. We see that friend declaration must
17932 be template for the code to be valid. Note that
17933 processing_template_decl does not work here since it is
17934 always 1 for the above two cases. */
17936 decl
= (cp_parser_maybe_treat_template_as_class
17937 (decl
, /*tag_name_p=*/is_friend
17938 && template_parm_lists_apply
));
17940 if (TREE_CODE (decl
) != TYPE_DECL
)
17942 cp_parser_diagnose_invalid_type_name (parser
,
17945 return error_mark_node
;
17948 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17950 bool allow_template
= (template_parm_lists_apply
17951 || DECL_SELF_REFERENCE_P (decl
));
17952 type
= check_elaborated_type_specifier (tag_type
, decl
,
17955 if (type
== error_mark_node
)
17956 return error_mark_node
;
17959 /* Forward declarations of nested types, such as
17964 are invalid unless all components preceding the final '::'
17965 are complete. If all enclosing types are complete, these
17966 declarations become merely pointless.
17968 Invalid forward declarations of nested types are errors
17969 caught elsewhere in parsing. Those that are pointless arrive
17972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17973 && !is_friend
&& !processing_explicit_instantiation
)
17974 warning (0, "declaration %qD does not declare anything", decl
);
17976 type
= TREE_TYPE (decl
);
17980 /* An elaborated-type-specifier sometimes introduces a new type and
17981 sometimes names an existing type. Normally, the rule is that it
17982 introduces a new type only if there is not an existing type of
17983 the same name already in scope. For example, given:
17986 void f() { struct S s; }
17988 the `struct S' in the body of `f' is the same `struct S' as in
17989 the global scope; the existing definition is used. However, if
17990 there were no global declaration, this would introduce a new
17991 local class named `S'.
17993 An exception to this rule applies to the following code:
17995 namespace N { struct S; }
17997 Here, the elaborated-type-specifier names a new type
17998 unconditionally; even if there is already an `S' in the
17999 containing scope this declaration names a new type.
18000 This exception only applies if the elaborated-type-specifier
18001 forms the complete declaration:
18005 A declaration consisting solely of `class-key identifier ;' is
18006 either a redeclaration of the name in the current scope or a
18007 forward declaration of the identifier as a class name. It
18008 introduces the name into the current scope.
18010 We are in this situation precisely when the next token is a `;'.
18012 An exception to the exception is that a `friend' declaration does
18013 *not* name a new type; i.e., given:
18015 struct S { friend struct T; };
18017 `T' is not a new type in the scope of `S'.
18019 Also, `new struct S' or `sizeof (struct S)' never results in the
18020 definition of a new type; a new type can only be declared in a
18021 declaration context. */
18027 /* Friends have special name lookup rules. */
18028 ts
= ts_within_enclosing_non_class
;
18029 else if (is_declaration
18030 && cp_lexer_next_token_is (parser
->lexer
,
18032 /* This is a `class-key identifier ;' */
18038 (template_parm_lists_apply
18039 && (cp_parser_next_token_starts_class_definition_p (parser
)
18040 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18041 /* An unqualified name was used to reference this type, so
18042 there were no qualifying templates. */
18043 if (template_parm_lists_apply
18044 && !cp_parser_check_template_parameters (parser
,
18045 /*num_templates=*/0,
18046 /*template_id*/false,
18048 /*declarator=*/NULL
))
18049 return error_mark_node
;
18050 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18054 if (type
== error_mark_node
)
18055 return error_mark_node
;
18057 /* Allow attributes on forward declarations of classes. */
18060 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18061 warning (OPT_Wattributes
,
18062 "attributes ignored on uninstantiated type");
18063 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18064 && ! processing_explicit_instantiation
)
18065 warning (OPT_Wattributes
,
18066 "attributes ignored on template instantiation");
18067 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18068 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18070 warning (OPT_Wattributes
,
18071 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18074 if (tag_type
!= enum_type
)
18076 /* Indicate whether this class was declared as a `class' or as a
18078 if (CLASS_TYPE_P (type
))
18079 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18080 cp_parser_check_class_key (tag_type
, type
);
18083 /* A "<" cannot follow an elaborated type specifier. If that
18084 happens, the user was probably trying to form a template-id. */
18085 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18091 /* Parse an enum-specifier.
18094 enum-head { enumerator-list [opt] }
18095 enum-head { enumerator-list , } [C++0x]
18098 enum-key identifier [opt] enum-base [opt]
18099 enum-key nested-name-specifier identifier enum-base [opt]
18104 enum struct [C++0x]
18107 : type-specifier-seq
18109 opaque-enum-specifier:
18110 enum-key identifier enum-base [opt] ;
18113 enum-key attributes[opt] identifier [opt] enum-base [opt]
18114 { enumerator-list [opt] }attributes[opt]
18115 enum-key attributes[opt] identifier [opt] enum-base [opt]
18116 { enumerator-list, }attributes[opt] [C++0x]
18118 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18119 if the token stream isn't an enum-specifier after all. */
18122 cp_parser_enum_specifier (cp_parser
* parser
)
18125 tree type
= NULL_TREE
;
18127 tree nested_name_specifier
= NULL_TREE
;
18129 bool scoped_enum_p
= false;
18130 bool has_underlying_type
= false;
18131 bool nested_being_defined
= false;
18132 bool new_value_list
= false;
18133 bool is_new_type
= false;
18134 bool is_unnamed
= false;
18135 tree underlying_type
= NULL_TREE
;
18136 cp_token
*type_start_token
= NULL
;
18137 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18139 parser
->colon_corrects_to_scope_p
= false;
18141 /* Parse tentatively so that we can back up if we don't find a
18143 cp_parser_parse_tentatively (parser
);
18145 /* Caller guarantees that the current token is 'enum', an identifier
18146 possibly follows, and the token after that is an opening brace.
18147 If we don't have an identifier, fabricate an anonymous name for
18148 the enumeration being defined. */
18149 cp_lexer_consume_token (parser
->lexer
);
18151 /* Parse the "class" or "struct", which indicates a scoped
18152 enumeration type in C++0x. */
18153 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18154 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18156 if (cxx_dialect
< cxx11
)
18157 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18159 /* Consume the `struct' or `class' token. */
18160 cp_lexer_consume_token (parser
->lexer
);
18162 scoped_enum_p
= true;
18165 attributes
= cp_parser_attributes_opt (parser
);
18167 /* Clear the qualification. */
18168 parser
->scope
= NULL_TREE
;
18169 parser
->qualifying_scope
= NULL_TREE
;
18170 parser
->object_scope
= NULL_TREE
;
18172 /* Figure out in what scope the declaration is being placed. */
18173 prev_scope
= current_scope ();
18175 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18177 push_deferring_access_checks (dk_no_check
);
18178 nested_name_specifier
18179 = cp_parser_nested_name_specifier_opt (parser
,
18180 /*typename_keyword_p=*/true,
18181 /*check_dependency_p=*/false,
18183 /*is_declaration=*/false);
18185 if (nested_name_specifier
)
18189 identifier
= cp_parser_identifier (parser
);
18190 name
= cp_parser_lookup_name (parser
, identifier
,
18192 /*is_template=*/false,
18193 /*is_namespace=*/false,
18194 /*check_dependency=*/true,
18195 /*ambiguous_decls=*/NULL
,
18197 if (name
&& name
!= error_mark_node
)
18199 type
= TREE_TYPE (name
);
18200 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18202 /* Are template enums allowed in ISO? */
18203 if (template_parm_scope_p ())
18204 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18205 "%qD is an enumeration template", name
);
18206 /* ignore a typename reference, for it will be solved by name
18211 else if (nested_name_specifier
== error_mark_node
)
18212 /* We already issued an error. */;
18215 error_at (type_start_token
->location
,
18216 "%qD does not name an enumeration in %qT",
18217 identifier
, nested_name_specifier
);
18218 nested_name_specifier
= error_mark_node
;
18223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18224 identifier
= cp_parser_identifier (parser
);
18227 identifier
= make_anon_name ();
18230 error_at (type_start_token
->location
,
18231 "unnamed scoped enum is not allowed");
18234 pop_deferring_access_checks ();
18236 /* Check for the `:' that denotes a specified underlying type in C++0x.
18237 Note that a ':' could also indicate a bitfield width, however. */
18238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18240 cp_decl_specifier_seq type_specifiers
;
18242 /* Consume the `:'. */
18243 cp_lexer_consume_token (parser
->lexer
);
18245 /* Parse the type-specifier-seq. */
18246 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18247 /*is_trailing_return=*/false,
18250 /* At this point this is surely not elaborated type specifier. */
18251 if (!cp_parser_parse_definitely (parser
))
18254 if (cxx_dialect
< cxx11
)
18255 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18257 has_underlying_type
= true;
18259 /* If that didn't work, stop. */
18260 if (type_specifiers
.type
!= error_mark_node
)
18262 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18263 /*initialized=*/0, NULL
);
18264 if (underlying_type
== error_mark_node
18265 || check_for_bare_parameter_packs (underlying_type
))
18266 underlying_type
= NULL_TREE
;
18270 /* Look for the `{' but don't consume it yet. */
18271 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18273 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18275 cp_parser_error (parser
, "expected %<{%>");
18276 if (has_underlying_type
)
18282 /* An opaque-enum-specifier must have a ';' here. */
18283 if ((scoped_enum_p
|| underlying_type
)
18284 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18286 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18287 if (has_underlying_type
)
18295 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18298 if (nested_name_specifier
)
18300 if (CLASS_TYPE_P (nested_name_specifier
))
18302 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18303 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18304 push_scope (nested_name_specifier
);
18306 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18308 push_nested_namespace (nested_name_specifier
);
18312 /* Issue an error message if type-definitions are forbidden here. */
18313 if (!cp_parser_check_type_definition (parser
))
18314 type
= error_mark_node
;
18316 /* Create the new type. We do this before consuming the opening
18317 brace so the enum will be recorded as being on the line of its
18318 tag (or the 'enum' keyword, if there is no tag). */
18319 type
= start_enum (identifier
, type
, underlying_type
,
18320 attributes
, scoped_enum_p
, &is_new_type
);
18322 /* If the next token is not '{' it is an opaque-enum-specifier or an
18323 elaborated-type-specifier. */
18324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18326 timevar_push (TV_PARSE_ENUM
);
18327 if (nested_name_specifier
18328 && nested_name_specifier
!= error_mark_node
)
18330 /* The following catches invalid code such as:
18331 enum class S<int>::E { A, B, C }; */
18332 if (!processing_specialization
18333 && CLASS_TYPE_P (nested_name_specifier
)
18334 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18335 error_at (type_start_token
->location
, "cannot add an enumerator "
18336 "list to a template instantiation");
18338 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18340 error_at (type_start_token
->location
,
18341 "%<%T::%E%> has not been declared",
18342 TYPE_CONTEXT (nested_name_specifier
),
18343 nested_name_specifier
);
18344 type
= error_mark_node
;
18346 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18347 && !CLASS_TYPE_P (nested_name_specifier
))
18349 error_at (type_start_token
->location
, "nested name specifier "
18350 "%qT for enum declaration does not name a class "
18351 "or namespace", nested_name_specifier
);
18352 type
= error_mark_node
;
18354 /* If that scope does not contain the scope in which the
18355 class was originally declared, the program is invalid. */
18356 else if (prev_scope
&& !is_ancestor (prev_scope
,
18357 nested_name_specifier
))
18359 if (at_namespace_scope_p ())
18360 error_at (type_start_token
->location
,
18361 "declaration of %qD in namespace %qD which does not "
18363 type
, prev_scope
, nested_name_specifier
);
18365 error_at (type_start_token
->location
,
18366 "declaration of %qD in %qD which does not "
18368 type
, prev_scope
, nested_name_specifier
);
18369 type
= error_mark_node
;
18371 /* If that scope is the scope where the declaration is being placed
18372 the program is invalid. */
18373 else if (CLASS_TYPE_P (nested_name_specifier
)
18374 && CLASS_TYPE_P (prev_scope
)
18375 && same_type_p (nested_name_specifier
, prev_scope
))
18377 permerror (type_start_token
->location
,
18378 "extra qualification not allowed");
18379 nested_name_specifier
= NULL_TREE
;
18384 begin_scope (sk_scoped_enum
, type
);
18386 /* Consume the opening brace. */
18387 matching_braces braces
;
18388 braces
.consume_open (parser
);
18390 if (type
== error_mark_node
)
18391 ; /* Nothing to add */
18392 else if (OPAQUE_ENUM_P (type
)
18393 || (cxx_dialect
> cxx98
&& processing_specialization
))
18395 new_value_list
= true;
18396 SET_OPAQUE_ENUM_P (type
, false);
18397 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18401 error_at (type_start_token
->location
,
18402 "multiple definition of %q#T", type
);
18403 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18404 "previous definition here");
18405 type
= error_mark_node
;
18408 if (type
== error_mark_node
)
18409 cp_parser_skip_to_end_of_block_or_statement (parser
);
18410 /* If the next token is not '}', then there are some enumerators. */
18411 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18413 if (is_unnamed
&& !scoped_enum_p
)
18414 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18415 "ISO C++ forbids empty unnamed enum");
18418 cp_parser_enumerator_list (parser
, type
);
18420 /* Consume the final '}'. */
18421 braces
.require_close (parser
);
18425 timevar_pop (TV_PARSE_ENUM
);
18429 /* If a ';' follows, then it is an opaque-enum-specifier
18430 and additional restrictions apply. */
18431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18434 error_at (type_start_token
->location
,
18435 "opaque-enum-specifier without name");
18436 else if (nested_name_specifier
)
18437 error_at (type_start_token
->location
,
18438 "opaque-enum-specifier must use a simple identifier");
18442 /* Look for trailing attributes to apply to this enumeration, and
18443 apply them if appropriate. */
18444 if (cp_parser_allow_gnu_extensions_p (parser
))
18446 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18447 cplus_decl_attributes (&type
,
18449 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18452 /* Finish up the enumeration. */
18453 if (type
!= error_mark_node
)
18455 if (new_value_list
)
18456 finish_enum_value_list (type
);
18458 finish_enum (type
);
18461 if (nested_name_specifier
)
18463 if (CLASS_TYPE_P (nested_name_specifier
))
18465 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18466 pop_scope (nested_name_specifier
);
18468 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18470 pop_nested_namespace (nested_name_specifier
);
18474 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18478 /* Parse an enumerator-list. The enumerators all have the indicated
18482 enumerator-definition
18483 enumerator-list , enumerator-definition */
18486 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18490 /* Parse an enumerator-definition. */
18491 cp_parser_enumerator_definition (parser
, type
);
18493 /* If the next token is not a ',', we've reached the end of
18495 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18497 /* Otherwise, consume the `,' and keep going. */
18498 cp_lexer_consume_token (parser
->lexer
);
18499 /* If the next token is a `}', there is a trailing comma. */
18500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18502 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18503 pedwarn (input_location
, OPT_Wpedantic
,
18504 "comma at end of enumerator list");
18510 /* Parse an enumerator-definition. The enumerator has the indicated
18513 enumerator-definition:
18515 enumerator = constant-expression
18522 enumerator-definition:
18523 enumerator attributes [opt]
18524 enumerator attributes [opt] = constant-expression */
18527 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18533 /* Save the input location because we are interested in the location
18534 of the identifier and not the location of the explicit value. */
18535 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18537 /* Look for the identifier. */
18538 identifier
= cp_parser_identifier (parser
);
18539 if (identifier
== error_mark_node
)
18542 /* Parse any specified attributes. */
18543 tree attrs
= cp_parser_attributes_opt (parser
);
18545 /* If the next token is an '=', then there is an explicit value. */
18546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18548 /* Consume the `=' token. */
18549 cp_lexer_consume_token (parser
->lexer
);
18550 /* Parse the value. */
18551 value
= cp_parser_constant_expression (parser
);
18556 /* If we are processing a template, make sure the initializer of the
18557 enumerator doesn't contain any bare template parameter pack. */
18558 if (check_for_bare_parameter_packs (value
))
18559 value
= error_mark_node
;
18561 /* Create the enumerator. */
18562 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18565 /* Parse a namespace-name.
18568 original-namespace-name
18571 Returns the NAMESPACE_DECL for the namespace. */
18574 cp_parser_namespace_name (cp_parser
* parser
)
18577 tree namespace_decl
;
18579 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18581 /* Get the name of the namespace. */
18582 identifier
= cp_parser_identifier (parser
);
18583 if (identifier
== error_mark_node
)
18584 return error_mark_node
;
18586 /* Look up the identifier in the currently active scope. Look only
18587 for namespaces, due to:
18589 [basic.lookup.udir]
18591 When looking up a namespace-name in a using-directive or alias
18592 definition, only namespace names are considered.
18596 [basic.lookup.qual]
18598 During the lookup of a name preceding the :: scope resolution
18599 operator, object, function, and enumerator names are ignored.
18601 (Note that cp_parser_qualifying_entity only calls this
18602 function if the token after the name is the scope resolution
18604 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18606 /*is_template=*/false,
18607 /*is_namespace=*/true,
18608 /*check_dependency=*/true,
18609 /*ambiguous_decls=*/NULL
,
18611 /* If it's not a namespace, issue an error. */
18612 if (namespace_decl
== error_mark_node
18613 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18615 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18617 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18618 if (namespace_decl
== error_mark_node
18619 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18620 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18623 cp_parser_error (parser
, "expected namespace-name");
18624 namespace_decl
= error_mark_node
;
18627 return namespace_decl
;
18630 /* Parse a namespace-definition.
18632 namespace-definition:
18633 named-namespace-definition
18634 unnamed-namespace-definition
18636 named-namespace-definition:
18637 original-namespace-definition
18638 extension-namespace-definition
18640 original-namespace-definition:
18641 namespace identifier { namespace-body }
18643 extension-namespace-definition:
18644 namespace original-namespace-name { namespace-body }
18646 unnamed-namespace-definition:
18647 namespace { namespace-body } */
18650 cp_parser_namespace_definition (cp_parser
* parser
)
18653 int nested_definition_count
= 0;
18655 cp_ensure_no_omp_declare_simd (parser
);
18656 cp_ensure_no_oacc_routine (parser
);
18658 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18662 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18663 cp_lexer_consume_token (parser
->lexer
);
18666 /* Look for the `namespace' keyword. */
18668 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18670 /* Parse any specified attributes before the identifier. */
18671 tree attribs
= cp_parser_attributes_opt (parser
);
18675 identifier
= NULL_TREE
;
18677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18679 identifier
= cp_parser_identifier (parser
);
18681 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18682 pedwarn (input_location
, OPT_Wpedantic
,
18683 "standard attributes on namespaces must precede "
18684 "the namespace name");
18686 /* Parse any attributes specified after the identifier. */
18687 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18690 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18693 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18694 pedwarn (input_location
, OPT_Wpedantic
,
18695 "nested namespace definitions only available with "
18696 "-std=c++17 or -std=gnu++17");
18698 /* Nested namespace names can create new namespaces (unlike
18699 other qualified-ids). */
18700 if (int count
= identifier
? push_namespace (identifier
) : 0)
18701 nested_definition_count
+= count
;
18703 cp_parser_error (parser
, "nested namespace name required");
18704 cp_lexer_consume_token (parser
->lexer
);
18707 if (nested_definition_count
&& !identifier
)
18708 cp_parser_error (parser
, "namespace name required");
18710 if (nested_definition_count
&& attribs
)
18711 error_at (token
->location
,
18712 "a nested namespace definition cannot have attributes");
18713 if (nested_definition_count
&& is_inline
)
18714 error_at (token
->location
,
18715 "a nested namespace definition cannot be inline");
18717 /* Start the namespace. */
18718 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18720 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18722 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18724 /* Look for the `{' to validate starting the namespace. */
18725 matching_braces braces
;
18726 if (braces
.require_open (parser
))
18728 /* Parse the body of the namespace. */
18729 cp_parser_namespace_body (parser
);
18731 /* Look for the final `}'. */
18732 braces
.require_close (parser
);
18735 if (has_visibility
)
18736 pop_visibility (1);
18738 /* Pop the nested namespace definitions. */
18739 while (nested_definition_count
--)
18743 /* Parse a namespace-body.
18746 declaration-seq [opt] */
18749 cp_parser_namespace_body (cp_parser
* parser
)
18751 cp_parser_declaration_seq_opt (parser
);
18754 /* Parse a namespace-alias-definition.
18756 namespace-alias-definition:
18757 namespace identifier = qualified-namespace-specifier ; */
18760 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18763 tree namespace_specifier
;
18765 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18767 /* Look for the `namespace' keyword. */
18768 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18769 /* Look for the identifier. */
18770 identifier
= cp_parser_identifier (parser
);
18771 if (identifier
== error_mark_node
)
18773 /* Look for the `=' token. */
18774 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18775 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18777 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18778 /* Skip the definition. */
18779 cp_lexer_consume_token (parser
->lexer
);
18780 if (cp_parser_skip_to_closing_brace (parser
))
18781 cp_lexer_consume_token (parser
->lexer
);
18784 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18785 /* Look for the qualified-namespace-specifier. */
18786 namespace_specifier
18787 = cp_parser_qualified_namespace_specifier (parser
);
18788 /* Look for the `;' token. */
18789 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18791 /* Register the alias in the symbol table. */
18792 do_namespace_alias (identifier
, namespace_specifier
);
18795 /* Parse a qualified-namespace-specifier.
18797 qualified-namespace-specifier:
18798 :: [opt] nested-name-specifier [opt] namespace-name
18800 Returns a NAMESPACE_DECL corresponding to the specified
18804 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18806 /* Look for the optional `::'. */
18807 cp_parser_global_scope_opt (parser
,
18808 /*current_scope_valid_p=*/false);
18810 /* Look for the optional nested-name-specifier. */
18811 cp_parser_nested_name_specifier_opt (parser
,
18812 /*typename_keyword_p=*/false,
18813 /*check_dependency_p=*/true,
18815 /*is_declaration=*/true);
18817 return cp_parser_namespace_name (parser
);
18820 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18821 access declaration.
18824 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18825 using :: unqualified-id ;
18827 access-declaration:
18833 cp_parser_using_declaration (cp_parser
* parser
,
18834 bool access_declaration_p
)
18837 bool typename_p
= false;
18838 bool global_scope_p
;
18842 int oldcount
= errorcount
;
18843 cp_token
*diag_token
= NULL
;
18845 if (access_declaration_p
)
18847 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18848 cp_parser_parse_tentatively (parser
);
18852 /* Look for the `using' keyword. */
18853 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18856 /* Peek at the next token. */
18857 token
= cp_lexer_peek_token (parser
->lexer
);
18858 /* See if it's `typename'. */
18859 if (token
->keyword
== RID_TYPENAME
)
18861 /* Remember that we've seen it. */
18863 /* Consume the `typename' token. */
18864 cp_lexer_consume_token (parser
->lexer
);
18868 /* Look for the optional global scope qualification. */
18870 = (cp_parser_global_scope_opt (parser
,
18871 /*current_scope_valid_p=*/false)
18874 /* If we saw `typename', or didn't see `::', then there must be a
18875 nested-name-specifier present. */
18876 if (typename_p
|| !global_scope_p
)
18878 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18879 /*check_dependency_p=*/true,
18881 /*is_declaration=*/true);
18882 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18884 cp_parser_skip_to_end_of_block_or_statement (parser
);
18888 /* Otherwise, we could be in either of the two productions. In that
18889 case, treat the nested-name-specifier as optional. */
18891 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18892 /*typename_keyword_p=*/false,
18893 /*check_dependency_p=*/true,
18895 /*is_declaration=*/true);
18897 qscope
= global_namespace
;
18898 else if (UNSCOPED_ENUM_P (qscope
))
18899 qscope
= CP_TYPE_CONTEXT (qscope
);
18901 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18902 /* Something has already gone wrong; there's no need to parse
18903 further. Since an error has occurred, the return value of
18904 cp_parser_parse_definitely will be false, as required. */
18905 return cp_parser_parse_definitely (parser
);
18907 token
= cp_lexer_peek_token (parser
->lexer
);
18908 /* Parse the unqualified-id. */
18909 identifier
= cp_parser_unqualified_id (parser
,
18910 /*template_keyword_p=*/false,
18911 /*check_dependency_p=*/true,
18912 /*declarator_p=*/true,
18913 /*optional_p=*/false);
18915 if (access_declaration_p
)
18917 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18918 cp_parser_simulate_error (parser
);
18919 if (!cp_parser_parse_definitely (parser
))
18922 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18924 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18925 if (cxx_dialect
< cxx17
18926 && !in_system_header_at (ell
->location
))
18927 pedwarn (ell
->location
, 0,
18928 "pack expansion in using-declaration only available "
18929 "with -std=c++17 or -std=gnu++17");
18930 qscope
= make_pack_expansion (qscope
);
18933 /* The function we call to handle a using-declaration is different
18934 depending on what scope we are in. */
18935 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18937 else if (!identifier_p (identifier
)
18938 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18939 /* [namespace.udecl]
18941 A using declaration shall not name a template-id. */
18942 error_at (token
->location
,
18943 "a template-id may not appear in a using-declaration");
18946 if (at_class_scope_p ())
18948 /* Create the USING_DECL. */
18949 decl
= do_class_using_decl (qscope
, identifier
);
18951 if (decl
&& typename_p
)
18952 USING_DECL_TYPENAME_P (decl
) = 1;
18954 if (check_for_bare_parameter_packs (decl
))
18956 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18960 /* Add it to the list of members in this class. */
18961 finish_member_declaration (decl
);
18965 decl
= cp_parser_lookup_name_simple (parser
,
18968 if (decl
== error_mark_node
)
18969 cp_parser_name_lookup_error (parser
, identifier
,
18972 else if (check_for_bare_parameter_packs (decl
))
18974 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18977 else if (!at_namespace_scope_p ())
18978 finish_local_using_decl (decl
, qscope
, identifier
);
18980 finish_namespace_using_decl (decl
, qscope
, identifier
);
18984 if (!access_declaration_p
18985 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18987 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18988 if (cxx_dialect
< cxx17
)
18989 pedwarn (comma
->location
, 0,
18990 "comma-separated list in using-declaration only available "
18991 "with -std=c++17 or -std=gnu++17");
18995 /* Look for the final `;'. */
18996 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18998 if (access_declaration_p
&& errorcount
== oldcount
)
18999 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19000 "access declarations are deprecated "
19001 "in favour of using-declarations; "
19002 "suggestion: add the %<using%> keyword");
19007 /* Parse an alias-declaration.
19010 using identifier attribute-specifier-seq [opt] = type-id */
19013 cp_parser_alias_declaration (cp_parser
* parser
)
19015 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19016 location_t id_location
;
19017 cp_declarator
*declarator
;
19018 cp_decl_specifier_seq decl_specs
;
19020 const char *saved_message
= NULL
;
19022 /* Look for the `using' keyword. */
19023 cp_token
*using_token
19024 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19025 if (using_token
== NULL
)
19026 return error_mark_node
;
19028 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19029 id
= cp_parser_identifier (parser
);
19030 if (id
== error_mark_node
)
19031 return error_mark_node
;
19033 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19034 attributes
= cp_parser_attributes_opt (parser
);
19035 if (attributes
== error_mark_node
)
19036 return error_mark_node
;
19038 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19040 if (cp_parser_error_occurred (parser
))
19041 return error_mark_node
;
19043 cp_parser_commit_to_tentative_parse (parser
);
19045 /* Now we are going to parse the type-id of the declaration. */
19050 "A type-specifier-seq shall not define a class or enumeration
19051 unless it appears in the type-id of an alias-declaration (7.1.3) that
19052 is not the declaration of a template-declaration."
19054 In other words, if we currently are in an alias template, the
19055 type-id should not define a type.
19057 So let's set parser->type_definition_forbidden_message in that
19058 case; cp_parser_check_type_definition (called by
19059 cp_parser_class_specifier) will then emit an error if a type is
19060 defined in the type-id. */
19061 if (parser
->num_template_parameter_lists
)
19063 saved_message
= parser
->type_definition_forbidden_message
;
19064 parser
->type_definition_forbidden_message
=
19065 G_("types may not be defined in alias template declarations");
19068 type
= cp_parser_type_id (parser
);
19070 /* Restore the error message if need be. */
19071 if (parser
->num_template_parameter_lists
)
19072 parser
->type_definition_forbidden_message
= saved_message
;
19074 if (type
== error_mark_node
19075 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19077 cp_parser_skip_to_end_of_block_or_statement (parser
);
19078 return error_mark_node
;
19081 /* A typedef-name can also be introduced by an alias-declaration. The
19082 identifier following the using keyword becomes a typedef-name. It has
19083 the same semantics as if it were introduced by the typedef
19084 specifier. In particular, it does not define a new type and it shall
19085 not appear in the type-id. */
19087 clear_decl_specs (&decl_specs
);
19088 decl_specs
.type
= type
;
19089 if (attributes
!= NULL_TREE
)
19091 decl_specs
.attributes
= attributes
;
19092 set_and_check_decl_spec_loc (&decl_specs
,
19096 set_and_check_decl_spec_loc (&decl_specs
,
19099 set_and_check_decl_spec_loc (&decl_specs
,
19103 if (parser
->num_template_parameter_lists
19104 && !cp_parser_check_template_parameters (parser
,
19105 /*num_templates=*/0,
19106 /*template_id*/false,
19108 /*declarator=*/NULL
))
19109 return error_mark_node
;
19111 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19112 declarator
->id_loc
= id_location
;
19114 member_p
= at_class_scope_p ();
19116 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19117 NULL_TREE
, attributes
);
19119 decl
= start_decl (declarator
, &decl_specs
, 0,
19120 attributes
, NULL_TREE
, &pushed_scope
);
19121 if (decl
== error_mark_node
)
19124 // Attach constraints to the alias declaration.
19125 if (flag_concepts
&& current_template_parms
)
19127 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19128 tree constr
= build_constraints (reqs
, NULL_TREE
);
19129 set_constraints (decl
, constr
);
19132 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19135 pop_scope (pushed_scope
);
19137 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19138 added into the symbol table; otherwise, return the TYPE_DECL. */
19139 if (DECL_LANG_SPECIFIC (decl
)
19140 && DECL_TEMPLATE_INFO (decl
)
19141 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19143 decl
= DECL_TI_TEMPLATE (decl
);
19145 check_member_template (decl
);
19151 /* Parse a using-directive.
19154 using namespace :: [opt] nested-name-specifier [opt]
19155 namespace-name ; */
19158 cp_parser_using_directive (cp_parser
* parser
)
19160 tree namespace_decl
;
19163 /* Look for the `using' keyword. */
19164 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19165 /* And the `namespace' keyword. */
19166 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19167 /* Look for the optional `::' operator. */
19168 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19169 /* And the optional nested-name-specifier. */
19170 cp_parser_nested_name_specifier_opt (parser
,
19171 /*typename_keyword_p=*/false,
19172 /*check_dependency_p=*/true,
19174 /*is_declaration=*/true);
19175 /* Get the namespace being used. */
19176 namespace_decl
= cp_parser_namespace_name (parser
);
19177 /* And any specified attributes. */
19178 attribs
= cp_parser_attributes_opt (parser
);
19180 /* Update the symbol table. */
19181 if (namespace_bindings_p ())
19182 finish_namespace_using_directive (namespace_decl
, attribs
);
19184 finish_local_using_directive (namespace_decl
, attribs
);
19186 /* Look for the final `;'. */
19187 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19190 /* Parse an asm-definition.
19193 asm ( string-literal ) ;
19198 asm volatile [opt] ( string-literal ) ;
19199 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19200 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19201 : asm-operand-list [opt] ) ;
19202 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19203 : asm-operand-list [opt]
19204 : asm-clobber-list [opt] ) ;
19205 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19206 : asm-clobber-list [opt]
19207 : asm-goto-list ) ; */
19210 cp_parser_asm_definition (cp_parser
* parser
)
19213 tree outputs
= NULL_TREE
;
19214 tree inputs
= NULL_TREE
;
19215 tree clobbers
= NULL_TREE
;
19216 tree labels
= NULL_TREE
;
19218 bool volatile_p
= false;
19219 bool extended_p
= false;
19220 bool invalid_inputs_p
= false;
19221 bool invalid_outputs_p
= false;
19222 bool goto_p
= false;
19223 required_token missing
= RT_NONE
;
19225 /* Look for the `asm' keyword. */
19226 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19228 if (parser
->in_function_body
19229 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19231 error ("%<asm%> in %<constexpr%> function");
19232 cp_function_chain
->invalid_constexpr
= true;
19235 /* See if the next token is `volatile'. */
19236 if (cp_parser_allow_gnu_extensions_p (parser
)
19237 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19239 /* Remember that we saw the `volatile' keyword. */
19241 /* Consume the token. */
19242 cp_lexer_consume_token (parser
->lexer
);
19244 if (cp_parser_allow_gnu_extensions_p (parser
)
19245 && parser
->in_function_body
19246 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19248 /* Remember that we saw the `goto' keyword. */
19250 /* Consume the token. */
19251 cp_lexer_consume_token (parser
->lexer
);
19253 /* Look for the opening `('. */
19254 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19256 /* Look for the string. */
19257 string
= cp_parser_string_literal (parser
, false, false);
19258 if (string
== error_mark_node
)
19260 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19261 /*consume_paren=*/true);
19265 /* If we're allowing GNU extensions, check for the extended assembly
19266 syntax. Unfortunately, the `:' tokens need not be separated by
19267 a space in C, and so, for compatibility, we tolerate that here
19268 too. Doing that means that we have to treat the `::' operator as
19270 if (cp_parser_allow_gnu_extensions_p (parser
)
19271 && parser
->in_function_body
19272 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19273 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19275 bool inputs_p
= false;
19276 bool clobbers_p
= false;
19277 bool labels_p
= false;
19279 /* The extended syntax was used. */
19282 /* Look for outputs. */
19283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19285 /* Consume the `:'. */
19286 cp_lexer_consume_token (parser
->lexer
);
19287 /* Parse the output-operands. */
19288 if (cp_lexer_next_token_is_not (parser
->lexer
,
19290 && cp_lexer_next_token_is_not (parser
->lexer
,
19292 && cp_lexer_next_token_is_not (parser
->lexer
,
19296 outputs
= cp_parser_asm_operand_list (parser
);
19297 if (outputs
== error_mark_node
)
19298 invalid_outputs_p
= true;
19301 /* If the next token is `::', there are no outputs, and the
19302 next token is the beginning of the inputs. */
19303 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19304 /* The inputs are coming next. */
19307 /* Look for inputs. */
19309 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19311 /* Consume the `:' or `::'. */
19312 cp_lexer_consume_token (parser
->lexer
);
19313 /* Parse the output-operands. */
19314 if (cp_lexer_next_token_is_not (parser
->lexer
,
19316 && cp_lexer_next_token_is_not (parser
->lexer
,
19318 && cp_lexer_next_token_is_not (parser
->lexer
,
19321 inputs
= cp_parser_asm_operand_list (parser
);
19322 if (inputs
== error_mark_node
)
19323 invalid_inputs_p
= true;
19326 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19327 /* The clobbers are coming next. */
19330 /* Look for clobbers. */
19332 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19335 /* Consume the `:' or `::'. */
19336 cp_lexer_consume_token (parser
->lexer
);
19337 /* Parse the clobbers. */
19338 if (cp_lexer_next_token_is_not (parser
->lexer
,
19340 && cp_lexer_next_token_is_not (parser
->lexer
,
19342 clobbers
= cp_parser_asm_clobber_list (parser
);
19345 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19346 /* The labels are coming next. */
19349 /* Look for labels. */
19351 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19354 /* Consume the `:' or `::'. */
19355 cp_lexer_consume_token (parser
->lexer
);
19356 /* Parse the labels. */
19357 labels
= cp_parser_asm_label_list (parser
);
19360 if (goto_p
&& !labels_p
)
19361 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19364 missing
= RT_COLON_SCOPE
;
19366 /* Look for the closing `)'. */
19367 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19368 missing
? missing
: RT_CLOSE_PAREN
))
19369 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19370 /*consume_paren=*/true);
19371 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19373 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19375 /* Create the ASM_EXPR. */
19376 if (parser
->in_function_body
)
19378 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19379 inputs
, clobbers
, labels
);
19380 /* If the extended syntax was not used, mark the ASM_EXPR. */
19383 tree temp
= asm_stmt
;
19384 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19385 temp
= TREE_OPERAND (temp
, 0);
19387 ASM_INPUT_P (temp
) = 1;
19391 symtab
->finalize_toplevel_asm (string
);
19395 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19396 type that comes from the decl-specifier-seq. */
19399 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19401 for (cp_declarator
*d
= declarator
; d
;)
19411 if (TYPE_PTRMEMFUNC_P (type
))
19412 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19413 type
= TREE_TYPE (type
);
19421 /* Declarators [gram.dcl.decl] */
19423 /* Parse an init-declarator.
19426 declarator initializer [opt]
19431 declarator asm-specification [opt] attributes [opt] initializer [opt]
19433 function-definition:
19434 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19436 decl-specifier-seq [opt] declarator function-try-block
19440 function-definition:
19441 __extension__ function-definition
19445 function-definition:
19446 decl-specifier-seq [opt] declarator function-transaction-block
19448 The DECL_SPECIFIERS apply to this declarator. Returns a
19449 representation of the entity declared. If MEMBER_P is TRUE, then
19450 this declarator appears in a class scope. The new DECL created by
19451 this declarator is returned.
19453 The CHECKS are access checks that should be performed once we know
19454 what entity is being declared (and, therefore, what classes have
19457 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19458 for a function-definition here as well. If the declarator is a
19459 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19460 be TRUE upon return. By that point, the function-definition will
19461 have been completely parsed.
19463 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19466 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19467 parsed declaration if it is an uninitialized single declarator not followed
19468 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19469 if present, will not be consumed. If returned, this declarator will be
19470 created with SD_INITIALIZED but will not call cp_finish_decl.
19472 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19473 and there is an initializer, the pointed location_t is set to the
19474 location of the '=' or `(', or '{' in C++11 token introducing the
19478 cp_parser_init_declarator (cp_parser
* parser
,
19479 cp_decl_specifier_seq
*decl_specifiers
,
19480 vec
<deferred_access_check
, va_gc
> *checks
,
19481 bool function_definition_allowed_p
,
19483 int declares_class_or_enum
,
19484 bool* function_definition_p
,
19485 tree
* maybe_range_for_decl
,
19486 location_t
* init_loc
,
19489 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19490 *attributes_start_token
= NULL
;
19491 cp_declarator
*declarator
;
19492 tree prefix_attributes
;
19493 tree attributes
= NULL
;
19494 tree asm_specification
;
19496 tree decl
= NULL_TREE
;
19498 int is_initialized
;
19499 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19500 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19502 enum cpp_ttype initialization_kind
;
19503 bool is_direct_init
= false;
19504 bool is_non_constant_init
;
19505 int ctor_dtor_or_conv_p
;
19506 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19507 tree pushed_scope
= NULL_TREE
;
19508 bool range_for_decl_p
= false;
19509 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19510 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19512 /* Gather the attributes that were provided with the
19513 decl-specifiers. */
19514 prefix_attributes
= decl_specifiers
->attributes
;
19516 /* Assume that this is not the declarator for a function
19518 if (function_definition_p
)
19519 *function_definition_p
= false;
19521 /* Default arguments are only permitted for function parameters. */
19522 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19523 parser
->default_arg_ok_p
= false;
19525 /* Defer access checks while parsing the declarator; we cannot know
19526 what names are accessible until we know what is being
19528 resume_deferring_access_checks ();
19530 token
= cp_lexer_peek_token (parser
->lexer
);
19532 /* Parse the declarator. */
19534 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19535 &ctor_dtor_or_conv_p
,
19536 /*parenthesized_p=*/NULL
,
19537 member_p
, friend_p
);
19538 /* Gather up the deferred checks. */
19539 stop_deferring_access_checks ();
19541 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19543 /* If the DECLARATOR was erroneous, there's no need to go
19545 if (declarator
== cp_error_declarator
)
19546 return error_mark_node
;
19548 /* Check that the number of template-parameter-lists is OK. */
19549 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19551 return error_mark_node
;
19553 if (declares_class_or_enum
& 2)
19554 cp_parser_check_for_definition_in_return_type (declarator
,
19555 decl_specifiers
->type
,
19556 decl_specifiers
->locations
[ds_type_spec
]);
19558 /* Figure out what scope the entity declared by the DECLARATOR is
19559 located in. `grokdeclarator' sometimes changes the scope, so
19560 we compute it now. */
19561 scope
= get_scope_of_declarator (declarator
);
19563 /* Perform any lookups in the declared type which were thought to be
19564 dependent, but are not in the scope of the declarator. */
19565 decl_specifiers
->type
19566 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19568 /* If we're allowing GNU extensions, look for an
19569 asm-specification. */
19570 if (cp_parser_allow_gnu_extensions_p (parser
))
19572 /* Look for an asm-specification. */
19573 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19574 asm_specification
= cp_parser_asm_specification_opt (parser
);
19577 asm_specification
= NULL_TREE
;
19579 /* Look for attributes. */
19580 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19581 attributes
= cp_parser_attributes_opt (parser
);
19583 /* Peek at the next token. */
19584 token
= cp_lexer_peek_token (parser
->lexer
);
19586 bool bogus_implicit_tmpl
= false;
19588 if (function_declarator_p (declarator
))
19590 /* Handle C++17 deduction guides. */
19591 if (!decl_specifiers
->type
19592 && ctor_dtor_or_conv_p
<= 0
19593 && cxx_dialect
>= cxx17
)
19595 cp_declarator
*id
= get_id_declarator (declarator
);
19596 tree name
= id
->u
.id
.unqualified_name
;
19597 parser
->scope
= id
->u
.id
.qualifying_scope
;
19598 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19600 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19601 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19603 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19604 id
->u
.id
.sfk
= sfk_deduction_guide
;
19605 ctor_dtor_or_conv_p
= 1;
19609 /* Check to see if the token indicates the start of a
19610 function-definition. */
19611 if (cp_parser_token_starts_function_definition_p (token
))
19613 if (!function_definition_allowed_p
)
19615 /* If a function-definition should not appear here, issue an
19617 cp_parser_error (parser
,
19618 "a function-definition is not allowed here");
19619 return error_mark_node
;
19622 location_t func_brace_location
19623 = cp_lexer_peek_token (parser
->lexer
)->location
;
19625 /* Neither attributes nor an asm-specification are allowed
19626 on a function-definition. */
19627 if (asm_specification
)
19628 error_at (asm_spec_start_token
->location
,
19629 "an asm-specification is not allowed "
19630 "on a function-definition");
19632 error_at (attributes_start_token
->location
,
19633 "attributes are not allowed "
19634 "on a function-definition");
19635 /* This is a function-definition. */
19636 *function_definition_p
= true;
19638 /* Parse the function definition. */
19640 decl
= cp_parser_save_member_function_body (parser
,
19643 prefix_attributes
);
19646 (cp_parser_function_definition_from_specifiers_and_declarator
19647 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19649 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19651 /* This is where the prologue starts... */
19652 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19653 = func_brace_location
;
19659 else if (parser
->fully_implicit_function_template_p
)
19661 /* A non-template declaration involving a function parameter list
19662 containing an implicit template parameter will be made into a
19663 template. If the resulting declaration is not going to be an
19664 actual function then finish the template scope here to prevent it.
19665 An error message will be issued once we have a decl to talk about.
19667 FIXME probably we should do type deduction rather than create an
19668 implicit template, but the standard currently doesn't allow it. */
19669 bogus_implicit_tmpl
= true;
19670 finish_fully_implicit_template (parser
, NULL_TREE
);
19675 Only in function declarations for constructors, destructors, type
19676 conversions, and deduction guides can the decl-specifier-seq be omitted.
19678 We explicitly postpone this check past the point where we handle
19679 function-definitions because we tolerate function-definitions
19680 that are missing their return types in some modes. */
19681 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19683 cp_parser_error (parser
,
19684 "expected constructor, destructor, or type conversion");
19685 return error_mark_node
;
19688 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19689 if (token
->type
== CPP_EQ
19690 || token
->type
== CPP_OPEN_PAREN
19691 || token
->type
== CPP_OPEN_BRACE
)
19693 is_initialized
= SD_INITIALIZED
;
19694 initialization_kind
= token
->type
;
19695 if (maybe_range_for_decl
)
19696 *maybe_range_for_decl
= error_mark_node
;
19697 tmp_init_loc
= token
->location
;
19698 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19699 *init_loc
= tmp_init_loc
;
19701 if (token
->type
== CPP_EQ
19702 && function_declarator_p (declarator
))
19704 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19705 if (t2
->keyword
== RID_DEFAULT
)
19706 is_initialized
= SD_DEFAULTED
;
19707 else if (t2
->keyword
== RID_DELETE
)
19708 is_initialized
= SD_DELETED
;
19713 /* If the init-declarator isn't initialized and isn't followed by a
19714 `,' or `;', it's not a valid init-declarator. */
19715 if (token
->type
!= CPP_COMMA
19716 && token
->type
!= CPP_SEMICOLON
)
19718 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19719 range_for_decl_p
= true;
19722 if (!maybe_range_for_decl
)
19723 cp_parser_error (parser
, "expected initializer");
19724 return error_mark_node
;
19727 is_initialized
= SD_UNINITIALIZED
;
19728 initialization_kind
= CPP_EOF
;
19731 /* Because start_decl has side-effects, we should only call it if we
19732 know we're going ahead. By this point, we know that we cannot
19733 possibly be looking at any other construct. */
19734 cp_parser_commit_to_tentative_parse (parser
);
19736 /* Enter the newly declared entry in the symbol table. If we're
19737 processing a declaration in a class-specifier, we wait until
19738 after processing the initializer. */
19741 if (parser
->in_unbraced_linkage_specification_p
)
19742 decl_specifiers
->storage_class
= sc_extern
;
19743 decl
= start_decl (declarator
, decl_specifiers
,
19744 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19745 attributes
, prefix_attributes
, &pushed_scope
);
19746 cp_finalize_omp_declare_simd (parser
, decl
);
19747 cp_finalize_oacc_routine (parser
, decl
, false);
19748 /* Adjust location of decl if declarator->id_loc is more appropriate:
19749 set, and decl wasn't merged with another decl, in which case its
19750 location would be different from input_location, and more accurate. */
19752 && declarator
->id_loc
!= UNKNOWN_LOCATION
19753 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19754 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19757 /* Enter the SCOPE. That way unqualified names appearing in the
19758 initializer will be looked up in SCOPE. */
19759 pushed_scope
= push_scope (scope
);
19761 /* Perform deferred access control checks, now that we know in which
19762 SCOPE the declared entity resides. */
19763 if (!member_p
&& decl
)
19765 tree saved_current_function_decl
= NULL_TREE
;
19767 /* If the entity being declared is a function, pretend that we
19768 are in its scope. If it is a `friend', it may have access to
19769 things that would not otherwise be accessible. */
19770 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19772 saved_current_function_decl
= current_function_decl
;
19773 current_function_decl
= decl
;
19776 /* Perform access checks for template parameters. */
19777 cp_parser_perform_template_parameter_access_checks (checks
);
19779 /* Perform the access control checks for the declarator and the
19780 decl-specifiers. */
19781 perform_deferred_access_checks (tf_warning_or_error
);
19783 /* Restore the saved value. */
19784 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19785 current_function_decl
= saved_current_function_decl
;
19788 /* Parse the initializer. */
19789 initializer
= NULL_TREE
;
19790 is_direct_init
= false;
19791 is_non_constant_init
= true;
19792 if (is_initialized
)
19794 if (function_declarator_p (declarator
))
19796 if (initialization_kind
== CPP_EQ
)
19797 initializer
= cp_parser_pure_specifier (parser
);
19800 /* If the declaration was erroneous, we don't really
19801 know what the user intended, so just silently
19802 consume the initializer. */
19803 if (decl
!= error_mark_node
)
19804 error_at (tmp_init_loc
, "initializer provided for function");
19805 cp_parser_skip_to_closing_parenthesis (parser
,
19806 /*recovering=*/true,
19807 /*or_comma=*/false,
19808 /*consume_paren=*/true);
19813 /* We want to record the extra mangling scope for in-class
19814 initializers of class members and initializers of static data
19815 member templates. The former involves deferring
19816 parsing of the initializer until end of class as with default
19817 arguments. So right here we only handle the latter. */
19818 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19819 start_lambda_scope (decl
);
19820 initializer
= cp_parser_initializer (parser
,
19822 &is_non_constant_init
);
19823 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19824 finish_lambda_scope ();
19825 if (initializer
== error_mark_node
)
19826 cp_parser_skip_to_end_of_statement (parser
);
19830 /* The old parser allows attributes to appear after a parenthesized
19831 initializer. Mark Mitchell proposed removing this functionality
19832 on the GCC mailing lists on 2002-08-13. This parser accepts the
19833 attributes -- but ignores them. Made a permerror in GCC 8. */
19834 if (cp_parser_allow_gnu_extensions_p (parser
)
19835 && initialization_kind
== CPP_OPEN_PAREN
19836 && cp_parser_attributes_opt (parser
)
19837 && permerror (input_location
,
19838 "attributes after parenthesized initializer ignored"))
19841 if (flag_permissive
&& !hint
)
19844 inform (input_location
,
19845 "this flexibility is deprecated and will be removed");
19849 /* And now complain about a non-function implicit template. */
19850 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19851 error_at (DECL_SOURCE_LOCATION (decl
),
19852 "non-function %qD declared as implicit template", decl
);
19854 /* For an in-class declaration, use `grokfield' to create the
19860 pop_scope (pushed_scope
);
19861 pushed_scope
= NULL_TREE
;
19863 decl
= grokfield (declarator
, decl_specifiers
,
19864 initializer
, !is_non_constant_init
,
19865 /*asmspec=*/NULL_TREE
,
19866 attr_chainon (attributes
, prefix_attributes
));
19867 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19868 cp_parser_save_default_args (parser
, decl
);
19869 cp_finalize_omp_declare_simd (parser
, decl
);
19870 cp_finalize_oacc_routine (parser
, decl
, false);
19873 /* Finish processing the declaration. But, skip member
19875 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19877 cp_finish_decl (decl
,
19878 initializer
, !is_non_constant_init
,
19880 /* If the initializer is in parentheses, then this is
19881 a direct-initialization, which means that an
19882 `explicit' constructor is OK. Otherwise, an
19883 `explicit' constructor cannot be used. */
19884 ((is_direct_init
|| !is_initialized
)
19885 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19887 else if ((cxx_dialect
!= cxx98
) && friend_p
19888 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19889 /* Core issue #226 (C++0x only): A default template-argument
19890 shall not be specified in a friend class template
19892 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19893 /*is_partial=*/false, /*is_friend_decl=*/1);
19895 if (!friend_p
&& pushed_scope
)
19896 pop_scope (pushed_scope
);
19898 if (function_declarator_p (declarator
)
19899 && parser
->fully_implicit_function_template_p
)
19902 decl
= finish_fully_implicit_template (parser
, decl
);
19904 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19907 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19908 && type_uses_auto (decl_specifiers
->type
))
19909 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19914 /* Parse a declarator.
19918 ptr-operator declarator
19920 abstract-declarator:
19921 ptr-operator abstract-declarator [opt]
19922 direct-abstract-declarator
19927 attributes [opt] direct-declarator
19928 attributes [opt] ptr-operator declarator
19930 abstract-declarator:
19931 attributes [opt] ptr-operator abstract-declarator [opt]
19932 attributes [opt] direct-abstract-declarator
19934 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19935 detect constructors, destructors, deduction guides, or conversion operators.
19936 It is set to -1 if the declarator is a name, and +1 if it is a
19937 function. Otherwise it is set to zero. Usually you just want to
19938 test for >0, but internally the negative value is used.
19940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19941 a decl-specifier-seq unless it declares a constructor, destructor,
19942 or conversion. It might seem that we could check this condition in
19943 semantic analysis, rather than parsing, but that makes it difficult
19944 to handle something like `f()'. We want to notice that there are
19945 no decl-specifiers, and therefore realize that this is an
19946 expression, not a declaration.)
19948 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19949 the declarator is a direct-declarator of the form "(...)".
19951 MEMBER_P is true iff this declarator is a member-declarator.
19953 FRIEND_P is true iff this declarator is a friend. */
19955 static cp_declarator
*
19956 cp_parser_declarator (cp_parser
* parser
,
19957 cp_parser_declarator_kind dcl_kind
,
19958 int* ctor_dtor_or_conv_p
,
19959 bool* parenthesized_p
,
19960 bool member_p
, bool friend_p
)
19962 cp_declarator
*declarator
;
19963 enum tree_code code
;
19964 cp_cv_quals cv_quals
;
19966 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19968 /* Assume this is not a constructor, destructor, or type-conversion
19970 if (ctor_dtor_or_conv_p
)
19971 *ctor_dtor_or_conv_p
= 0;
19973 if (cp_parser_allow_gnu_extensions_p (parser
))
19974 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19976 /* Check for the ptr-operator production. */
19977 cp_parser_parse_tentatively (parser
);
19978 /* Parse the ptr-operator. */
19979 code
= cp_parser_ptr_operator (parser
,
19984 /* If that worked, then we have a ptr-operator. */
19985 if (cp_parser_parse_definitely (parser
))
19987 /* If a ptr-operator was found, then this declarator was not
19989 if (parenthesized_p
)
19990 *parenthesized_p
= true;
19991 /* The dependent declarator is optional if we are parsing an
19992 abstract-declarator. */
19993 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19994 cp_parser_parse_tentatively (parser
);
19996 /* Parse the dependent declarator. */
19997 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19998 /*ctor_dtor_or_conv_p=*/NULL
,
19999 /*parenthesized_p=*/NULL
,
20000 /*member_p=*/false,
20003 /* If we are parsing an abstract-declarator, we must handle the
20004 case where the dependent declarator is absent. */
20005 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20006 && !cp_parser_parse_definitely (parser
))
20009 declarator
= cp_parser_make_indirect_declarator
20010 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20012 /* Everything else is a direct-declarator. */
20015 if (parenthesized_p
)
20016 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20018 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20019 ctor_dtor_or_conv_p
,
20020 member_p
, friend_p
);
20023 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20024 declarator
->attributes
= gnu_attributes
;
20028 /* Parse a direct-declarator or direct-abstract-declarator.
20032 direct-declarator ( parameter-declaration-clause )
20033 cv-qualifier-seq [opt]
20034 ref-qualifier [opt]
20035 exception-specification [opt]
20036 direct-declarator [ constant-expression [opt] ]
20039 direct-abstract-declarator:
20040 direct-abstract-declarator [opt]
20041 ( parameter-declaration-clause )
20042 cv-qualifier-seq [opt]
20043 ref-qualifier [opt]
20044 exception-specification [opt]
20045 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20046 ( abstract-declarator )
20048 Returns a representation of the declarator. DCL_KIND is
20049 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20050 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20051 we are parsing a direct-declarator. It is
20052 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20053 of ambiguity we prefer an abstract declarator, as per
20054 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20055 as for cp_parser_declarator. */
20057 static cp_declarator
*
20058 cp_parser_direct_declarator (cp_parser
* parser
,
20059 cp_parser_declarator_kind dcl_kind
,
20060 int* ctor_dtor_or_conv_p
,
20061 bool member_p
, bool friend_p
)
20064 cp_declarator
*declarator
= NULL
;
20065 tree scope
= NULL_TREE
;
20066 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20067 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20069 tree pushed_scope
= NULL_TREE
;
20070 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20074 /* Peek at the next token. */
20075 token
= cp_lexer_peek_token (parser
->lexer
);
20076 if (token
->type
== CPP_OPEN_PAREN
)
20078 /* This is either a parameter-declaration-clause, or a
20079 parenthesized declarator. When we know we are parsing a
20080 named declarator, it must be a parenthesized declarator
20081 if FIRST is true. For instance, `(int)' is a
20082 parameter-declaration-clause, with an omitted
20083 direct-abstract-declarator. But `((*))', is a
20084 parenthesized abstract declarator. Finally, when T is a
20085 template parameter `(T)' is a
20086 parameter-declaration-clause, and not a parenthesized
20089 We first try and parse a parameter-declaration-clause,
20090 and then try a nested declarator (if FIRST is true).
20092 It is not an error for it not to be a
20093 parameter-declaration-clause, even when FIRST is
20099 The first is the declaration of a function while the
20100 second is the definition of a variable, including its
20103 Having seen only the parenthesis, we cannot know which of
20104 these two alternatives should be selected. Even more
20105 complex are examples like:
20110 The former is a function-declaration; the latter is a
20111 variable initialization.
20113 Thus again, we try a parameter-declaration-clause, and if
20114 that fails, we back out and return. */
20116 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20119 bool is_declarator
= false;
20123 /* In a member-declarator, the only valid interpretation
20124 of a parenthesis is the start of a
20125 parameter-declaration-clause. (It is invalid to
20126 initialize a static data member with a parenthesized
20127 initializer; only the "=" form of initialization is
20130 cp_parser_parse_tentatively (parser
);
20132 /* Consume the `('. */
20133 matching_parens parens
;
20134 parens
.consume_open (parser
);
20137 /* If this is going to be an abstract declarator, we're
20138 in a declarator and we can't have default args. */
20139 parser
->default_arg_ok_p
= false;
20140 parser
->in_declarator_p
= true;
20143 begin_scope (sk_function_parms
, NULL_TREE
);
20145 /* Parse the parameter-declaration-clause. */
20146 params
= cp_parser_parameter_declaration_clause (parser
);
20148 /* Consume the `)'. */
20149 parens
.require_close (parser
);
20151 /* If all went well, parse the cv-qualifier-seq,
20152 ref-qualifier and the exception-specification. */
20153 if (member_p
|| cp_parser_parse_definitely (parser
))
20155 cp_cv_quals cv_quals
;
20156 cp_virt_specifiers virt_specifiers
;
20157 cp_ref_qualifier ref_qual
;
20158 tree exception_specification
;
20161 bool memfn
= (member_p
|| (pushed_scope
20162 && CLASS_TYPE_P (pushed_scope
)));
20164 is_declarator
= true;
20166 if (ctor_dtor_or_conv_p
)
20167 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20170 /* Parse the cv-qualifier-seq. */
20171 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20172 /* Parse the ref-qualifier. */
20173 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20174 /* Parse the tx-qualifier. */
20175 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20176 /* And the exception-specification. */
20177 exception_specification
20178 = cp_parser_exception_specification_opt (parser
);
20180 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20182 /* In here, we handle cases where attribute is used after
20183 the function declaration. For example:
20184 void func (int x) __attribute__((vector(..))); */
20185 tree gnu_attrs
= NULL_TREE
;
20186 tree requires_clause
= NULL_TREE
;
20187 late_return
= (cp_parser_late_return_type_opt
20188 (parser
, declarator
, requires_clause
,
20189 memfn
? cv_quals
: -1));
20191 /* Parse the virt-specifier-seq. */
20192 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20194 /* Create the function-declarator. */
20195 declarator
= make_call_declarator (declarator
,
20201 exception_specification
,
20204 declarator
->std_attributes
= attrs
;
20205 declarator
->attributes
= gnu_attrs
;
20206 /* Any subsequent parameter lists are to do with
20207 return type, so are not those of the declared
20209 parser
->default_arg_ok_p
= false;
20212 /* Remove the function parms from scope. */
20213 pop_bindings_and_leave_scope ();
20216 /* Repeat the main loop. */
20220 /* If this is the first, we can try a parenthesized
20224 bool saved_in_type_id_in_expr_p
;
20226 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20227 parser
->in_declarator_p
= saved_in_declarator_p
;
20229 open_paren
= token
;
20230 /* Consume the `('. */
20231 matching_parens parens
;
20232 parens
.consume_open (parser
);
20233 /* Parse the nested declarator. */
20234 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20235 parser
->in_type_id_in_expr_p
= true;
20237 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20238 /*parenthesized_p=*/NULL
,
20239 member_p
, friend_p
);
20240 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20242 /* Expect a `)'. */
20243 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20244 if (!parens
.require_close (parser
))
20245 declarator
= cp_error_declarator
;
20246 if (declarator
== cp_error_declarator
)
20249 goto handle_declarator
;
20251 /* Otherwise, we must be done. */
20255 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20256 && token
->type
== CPP_OPEN_SQUARE
20257 && !cp_next_tokens_can_be_attribute_p (parser
))
20259 /* Parse an array-declarator. */
20260 tree bounds
, attrs
;
20262 if (ctor_dtor_or_conv_p
)
20263 *ctor_dtor_or_conv_p
= 0;
20267 parser
->default_arg_ok_p
= false;
20268 parser
->in_declarator_p
= true;
20269 /* Consume the `['. */
20270 cp_lexer_consume_token (parser
->lexer
);
20271 /* Peek at the next token. */
20272 token
= cp_lexer_peek_token (parser
->lexer
);
20273 /* If the next token is `]', then there is no
20274 constant-expression. */
20275 if (token
->type
!= CPP_CLOSE_SQUARE
)
20277 bool non_constant_p
;
20279 = cp_parser_constant_expression (parser
,
20280 /*allow_non_constant=*/true,
20282 if (!non_constant_p
)
20284 else if (error_operand_p (bounds
))
20285 /* Already gave an error. */;
20286 else if (!parser
->in_function_body
20287 || current_binding_level
->kind
== sk_function_parms
)
20289 /* Normally, the array bound must be an integral constant
20290 expression. However, as an extension, we allow VLAs
20291 in function scopes as long as they aren't part of a
20292 parameter declaration. */
20293 cp_parser_error (parser
,
20294 "array bound is not an integer constant");
20295 bounds
= error_mark_node
;
20297 else if (processing_template_decl
20298 && !type_dependent_expression_p (bounds
))
20300 /* Remember this wasn't a constant-expression. */
20301 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20302 TREE_SIDE_EFFECTS (bounds
) = 1;
20306 bounds
= NULL_TREE
;
20307 /* Look for the closing `]'. */
20308 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20310 declarator
= cp_error_declarator
;
20314 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20315 declarator
= make_array_declarator (declarator
, bounds
);
20316 declarator
->std_attributes
= attrs
;
20318 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20321 tree qualifying_scope
;
20322 tree unqualified_name
;
20324 special_function_kind sfk
;
20326 bool pack_expansion_p
= false;
20327 cp_token
*declarator_id_start_token
;
20329 /* Parse a declarator-id */
20330 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20333 cp_parser_parse_tentatively (parser
);
20335 /* If we see an ellipsis, we should be looking at a
20337 if (token
->type
== CPP_ELLIPSIS
)
20339 /* Consume the `...' */
20340 cp_lexer_consume_token (parser
->lexer
);
20342 pack_expansion_p
= true;
20346 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20348 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20349 qualifying_scope
= parser
->scope
;
20354 if (!unqualified_name
&& pack_expansion_p
)
20356 /* Check whether an error occurred. */
20357 okay
= !cp_parser_error_occurred (parser
);
20359 /* We already consumed the ellipsis to mark a
20360 parameter pack, but we have no way to report it,
20361 so abort the tentative parse. We will be exiting
20362 immediately anyway. */
20363 cp_parser_abort_tentative_parse (parser
);
20366 okay
= cp_parser_parse_definitely (parser
);
20369 unqualified_name
= error_mark_node
;
20370 else if (unqualified_name
20371 && (qualifying_scope
20372 || (!identifier_p (unqualified_name
))))
20374 cp_parser_error (parser
, "expected unqualified-id");
20375 unqualified_name
= error_mark_node
;
20379 if (!unqualified_name
)
20381 if (unqualified_name
== error_mark_node
)
20383 declarator
= cp_error_declarator
;
20384 pack_expansion_p
= false;
20385 declarator
->parameter_pack_p
= false;
20389 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20391 if (qualifying_scope
&& at_namespace_scope_p ()
20392 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20394 /* In the declaration of a member of a template class
20395 outside of the class itself, the SCOPE will sometimes
20396 be a TYPENAME_TYPE. For example, given:
20398 template <typename T>
20399 int S<T>::R::i = 3;
20401 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20402 this context, we must resolve S<T>::R to an ordinary
20403 type, rather than a typename type.
20405 The reason we normally avoid resolving TYPENAME_TYPEs
20406 is that a specialization of `S' might render
20407 `S<T>::R' not a type. However, if `S' is
20408 specialized, then this `i' will not be used, so there
20409 is no harm in resolving the types here. */
20412 /* Resolve the TYPENAME_TYPE. */
20413 type
= resolve_typename_type (qualifying_scope
,
20414 /*only_current_p=*/false);
20415 /* If that failed, the declarator is invalid. */
20416 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20418 if (typedef_variant_p (type
))
20419 error_at (declarator_id_start_token
->location
,
20420 "cannot define member of dependent typedef "
20423 error_at (declarator_id_start_token
->location
,
20424 "%<%T::%E%> is not a type",
20425 TYPE_CONTEXT (qualifying_scope
),
20426 TYPE_IDENTIFIER (qualifying_scope
));
20428 qualifying_scope
= type
;
20433 if (unqualified_name
)
20437 if (qualifying_scope
20438 && CLASS_TYPE_P (qualifying_scope
))
20439 class_type
= qualifying_scope
;
20441 class_type
= current_class_type
;
20443 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20445 tree name_type
= TREE_TYPE (unqualified_name
);
20447 if (!class_type
|| !same_type_p (name_type
, class_type
))
20449 /* We do not attempt to print the declarator
20450 here because we do not have enough
20451 information about its original syntactic
20453 cp_parser_error (parser
, "invalid declarator");
20454 declarator
= cp_error_declarator
;
20457 else if (qualifying_scope
20458 && CLASSTYPE_USE_TEMPLATE (name_type
))
20460 error_at (declarator_id_start_token
->location
,
20461 "invalid use of constructor as a template");
20462 inform (declarator_id_start_token
->location
,
20463 "use %<%T::%D%> instead of %<%T::%D%> to "
20464 "name the constructor in a qualified name",
20466 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20467 class_type
, name_type
);
20468 declarator
= cp_error_declarator
;
20471 unqualified_name
= constructor_name (class_type
);
20476 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20477 sfk
= sfk_destructor
;
20478 else if (identifier_p (unqualified_name
)
20479 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20480 sfk
= sfk_conversion
;
20481 else if (/* There's no way to declare a constructor
20482 for an unnamed type, even if the type
20483 got a name for linkage purposes. */
20484 !TYPE_WAS_UNNAMED (class_type
)
20485 /* Handle correctly (c++/19200):
20499 friend void N::S();
20501 && (!friend_p
|| class_type
== qualifying_scope
)
20502 && constructor_name_p (unqualified_name
,
20504 sfk
= sfk_constructor
;
20505 else if (is_overloaded_fn (unqualified_name
)
20506 && DECL_CONSTRUCTOR_P (get_first_fn
20507 (unqualified_name
)))
20508 sfk
= sfk_constructor
;
20510 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20511 *ctor_dtor_or_conv_p
= -1;
20514 declarator
= make_id_declarator (qualifying_scope
,
20517 declarator
->std_attributes
= attrs
;
20518 declarator
->id_loc
= token
->location
;
20519 declarator
->parameter_pack_p
= pack_expansion_p
;
20521 if (pack_expansion_p
)
20522 maybe_warn_variadic_templates ();
20525 handle_declarator
:;
20526 scope
= get_scope_of_declarator (declarator
);
20529 /* Any names that appear after the declarator-id for a
20530 member are looked up in the containing scope. */
20531 if (at_function_scope_p ())
20533 /* But declarations with qualified-ids can't appear in a
20535 cp_parser_error (parser
, "qualified-id in declaration");
20536 declarator
= cp_error_declarator
;
20539 pushed_scope
= push_scope (scope
);
20541 parser
->in_declarator_p
= true;
20542 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20543 || (declarator
&& declarator
->kind
== cdk_id
))
20544 /* Default args are only allowed on function
20546 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20548 parser
->default_arg_ok_p
= false;
20557 /* For an abstract declarator, we might wind up with nothing at this
20558 point. That's an error; the declarator is not optional. */
20560 cp_parser_error (parser
, "expected declarator");
20561 else if (open_paren
)
20563 /* Record overly parenthesized declarator so we can give a
20564 diagnostic about confusing decl/expr disambiguation. */
20565 if (declarator
->kind
== cdk_array
)
20567 /* If the open and close parens are on different lines, this
20568 is probably a formatting thing, so ignore. */
20569 expanded_location open
= expand_location (open_paren
->location
);
20570 expanded_location close
= expand_location (close_paren
->location
);
20571 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20575 declarator
->parenthesized
= open_paren
->location
;
20578 /* If we entered a scope, we must exit it now. */
20580 pop_scope (pushed_scope
);
20582 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20583 parser
->in_declarator_p
= saved_in_declarator_p
;
20588 /* Parse a ptr-operator.
20591 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20592 * cv-qualifier-seq [opt]
20594 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20595 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20600 & cv-qualifier-seq [opt]
20602 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20603 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20604 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20605 filled in with the TYPE containing the member. *CV_QUALS is
20606 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20607 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20608 Note that the tree codes returned by this function have nothing
20609 to do with the types of trees that will be eventually be created
20610 to represent the pointer or reference type being parsed. They are
20611 just constants with suggestive names. */
20612 static enum tree_code
20613 cp_parser_ptr_operator (cp_parser
* parser
,
20615 cp_cv_quals
*cv_quals
,
20618 enum tree_code code
= ERROR_MARK
;
20620 tree attrs
= NULL_TREE
;
20622 /* Assume that it's not a pointer-to-member. */
20624 /* And that there are no cv-qualifiers. */
20625 *cv_quals
= TYPE_UNQUALIFIED
;
20627 /* Peek at the next token. */
20628 token
= cp_lexer_peek_token (parser
->lexer
);
20630 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20631 if (token
->type
== CPP_MULT
)
20632 code
= INDIRECT_REF
;
20633 else if (token
->type
== CPP_AND
)
20635 else if ((cxx_dialect
!= cxx98
) &&
20636 token
->type
== CPP_AND_AND
) /* C++0x only */
20637 code
= NON_LVALUE_EXPR
;
20639 if (code
!= ERROR_MARK
)
20641 /* Consume the `*', `&' or `&&'. */
20642 cp_lexer_consume_token (parser
->lexer
);
20644 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20645 `&', if we are allowing GNU extensions. (The only qualifier
20646 that can legally appear after `&' is `restrict', but that is
20647 enforced during semantic analysis. */
20648 if (code
== INDIRECT_REF
20649 || cp_parser_allow_gnu_extensions_p (parser
))
20650 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20652 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20653 if (attributes
!= NULL
)
20654 *attributes
= attrs
;
20658 /* Try the pointer-to-member case. */
20659 cp_parser_parse_tentatively (parser
);
20660 /* Look for the optional `::' operator. */
20661 cp_parser_global_scope_opt (parser
,
20662 /*current_scope_valid_p=*/false);
20663 /* Look for the nested-name specifier. */
20664 token
= cp_lexer_peek_token (parser
->lexer
);
20665 cp_parser_nested_name_specifier (parser
,
20666 /*typename_keyword_p=*/false,
20667 /*check_dependency_p=*/true,
20669 /*is_declaration=*/false);
20670 /* If we found it, and the next token is a `*', then we are
20671 indeed looking at a pointer-to-member operator. */
20672 if (!cp_parser_error_occurred (parser
)
20673 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20675 /* Indicate that the `*' operator was used. */
20676 code
= INDIRECT_REF
;
20678 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20679 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20680 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20681 error_at (token
->location
, "cannot form pointer to member of "
20682 "non-class %q#T", parser
->scope
);
20685 /* The type of which the member is a member is given by the
20687 *type
= parser
->scope
;
20688 /* The next name will not be qualified. */
20689 parser
->scope
= NULL_TREE
;
20690 parser
->qualifying_scope
= NULL_TREE
;
20691 parser
->object_scope
= NULL_TREE
;
20692 /* Look for optional c++11 attributes. */
20693 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20694 if (attributes
!= NULL
)
20695 *attributes
= attrs
;
20696 /* Look for the optional cv-qualifier-seq. */
20697 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20700 /* If that didn't work we don't have a ptr-operator. */
20701 if (!cp_parser_parse_definitely (parser
))
20702 cp_parser_error (parser
, "expected ptr-operator");
20708 /* Parse an (optional) cv-qualifier-seq.
20711 cv-qualifier cv-qualifier-seq [opt]
20722 Returns a bitmask representing the cv-qualifiers. */
20725 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20727 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20732 cp_cv_quals cv_qualifier
;
20734 /* Peek at the next token. */
20735 token
= cp_lexer_peek_token (parser
->lexer
);
20736 /* See if it's a cv-qualifier. */
20737 switch (token
->keyword
)
20740 cv_qualifier
= TYPE_QUAL_CONST
;
20744 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20748 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20752 cv_qualifier
= TYPE_UNQUALIFIED
;
20759 if (cv_quals
& cv_qualifier
)
20761 gcc_rich_location
richloc (token
->location
);
20762 richloc
.add_fixit_remove ();
20763 error_at (&richloc
, "duplicate cv-qualifier");
20764 cp_lexer_purge_token (parser
->lexer
);
20768 cp_lexer_consume_token (parser
->lexer
);
20769 cv_quals
|= cv_qualifier
;
20776 /* Parse an (optional) ref-qualifier
20782 Returns cp_ref_qualifier representing ref-qualifier. */
20784 static cp_ref_qualifier
20785 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20787 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20789 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20790 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20795 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20798 switch (token
->type
)
20801 curr_ref_qual
= REF_QUAL_LVALUE
;
20805 curr_ref_qual
= REF_QUAL_RVALUE
;
20809 curr_ref_qual
= REF_QUAL_NONE
;
20813 if (!curr_ref_qual
)
20817 error_at (token
->location
, "multiple ref-qualifiers");
20818 cp_lexer_purge_token (parser
->lexer
);
20822 ref_qual
= curr_ref_qual
;
20823 cp_lexer_consume_token (parser
->lexer
);
20830 /* Parse an optional tx-qualifier.
20834 transaction_safe_dynamic */
20837 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20839 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20840 if (token
->type
== CPP_NAME
)
20842 tree name
= token
->u
.value
;
20843 const char *p
= IDENTIFIER_POINTER (name
);
20844 const int len
= strlen ("transaction_safe");
20845 if (!strncmp (p
, "transaction_safe", len
))
20849 || !strcmp (p
, "_dynamic"))
20851 cp_lexer_consume_token (parser
->lexer
);
20854 error ("%qE requires %<-fgnu-tm%>", name
);
20865 /* Parse an (optional) virt-specifier-seq.
20867 virt-specifier-seq:
20868 virt-specifier virt-specifier-seq [opt]
20874 Returns a bitmask representing the virt-specifiers. */
20876 static cp_virt_specifiers
20877 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20879 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20884 cp_virt_specifiers virt_specifier
;
20886 /* Peek at the next token. */
20887 token
= cp_lexer_peek_token (parser
->lexer
);
20888 /* See if it's a virt-specifier-qualifier. */
20889 if (token
->type
!= CPP_NAME
)
20891 if (id_equal (token
->u
.value
, "override"))
20893 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20894 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20896 else if (id_equal (token
->u
.value
, "final"))
20898 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20899 virt_specifier
= VIRT_SPEC_FINAL
;
20901 else if (id_equal (token
->u
.value
, "__final"))
20903 virt_specifier
= VIRT_SPEC_FINAL
;
20908 if (virt_specifiers
& virt_specifier
)
20910 gcc_rich_location
richloc (token
->location
);
20911 richloc
.add_fixit_remove ();
20912 error_at (&richloc
, "duplicate virt-specifier");
20913 cp_lexer_purge_token (parser
->lexer
);
20917 cp_lexer_consume_token (parser
->lexer
);
20918 virt_specifiers
|= virt_specifier
;
20921 return virt_specifiers
;
20924 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20925 is in scope even though it isn't real. */
20928 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20932 if (current_class_ptr
)
20934 /* We don't clear this between NSDMIs. Is it already what we want? */
20935 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20936 if (DECL_P (current_class_ptr
)
20937 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20938 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20939 && cp_type_quals (type
) == quals
)
20943 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20944 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20945 current_class_ptr
= NULL_TREE
;
20947 = cp_build_fold_indirect_ref (this_parm
);
20948 current_class_ptr
= this_parm
;
20951 /* Return true iff our current scope is a non-static data member
20955 parsing_nsdmi (void)
20957 /* We recognize NSDMI context by the context-less 'this' pointer set up
20958 by the function above. */
20959 if (current_class_ptr
20960 && TREE_CODE (current_class_ptr
) == PARM_DECL
20961 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20966 /* Parse a late-specified return type, if any. This is not a separate
20967 non-terminal, but part of a function declarator, which looks like
20969 -> trailing-type-specifier-seq abstract-declarator(opt)
20971 Returns the type indicated by the type-id.
20973 In addition to this, parse any queued up #pragma omp declare simd
20974 clauses, and #pragma acc routine clauses.
20976 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20980 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20981 tree
& requires_clause
, cp_cv_quals quals
)
20984 tree type
= NULL_TREE
;
20985 bool declare_simd_p
= (parser
->omp_declare_simd
20987 && declarator
->kind
== cdk_id
);
20989 bool oacc_routine_p
= (parser
->oacc_routine
20991 && declarator
->kind
== cdk_id
);
20993 /* Peek at the next token. */
20994 token
= cp_lexer_peek_token (parser
->lexer
);
20995 /* A late-specified return type is indicated by an initial '->'. */
20996 if (token
->type
!= CPP_DEREF
20997 && token
->keyword
!= RID_REQUIRES
20998 && !(token
->type
== CPP_NAME
20999 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21000 && !(declare_simd_p
|| oacc_routine_p
))
21003 tree save_ccp
= current_class_ptr
;
21004 tree save_ccr
= current_class_ref
;
21007 /* DR 1207: 'this' is in scope in the trailing return type. */
21008 inject_this_parameter (current_class_type
, quals
);
21011 if (token
->type
== CPP_DEREF
)
21013 /* Consume the ->. */
21014 cp_lexer_consume_token (parser
->lexer
);
21016 type
= cp_parser_trailing_type_id (parser
);
21019 /* Function declarations may be followed by a trailing
21020 requires-clause. */
21021 requires_clause
= cp_parser_requires_clause_opt (parser
);
21023 if (declare_simd_p
)
21024 declarator
->attributes
21025 = cp_parser_late_parsing_omp_declare_simd (parser
,
21026 declarator
->attributes
);
21027 if (oacc_routine_p
)
21028 declarator
->attributes
21029 = cp_parser_late_parsing_oacc_routine (parser
,
21030 declarator
->attributes
);
21034 current_class_ptr
= save_ccp
;
21035 current_class_ref
= save_ccr
;
21041 /* Parse a declarator-id.
21045 :: [opt] nested-name-specifier [opt] type-name
21047 In the `id-expression' case, the value returned is as for
21048 cp_parser_id_expression if the id-expression was an unqualified-id.
21049 If the id-expression was a qualified-id, then a SCOPE_REF is
21050 returned. The first operand is the scope (either a NAMESPACE_DECL
21051 or TREE_TYPE), but the second is still just a representation of an
21055 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21058 /* The expression must be an id-expression. Assume that qualified
21059 names are the names of types so that:
21062 int S<T>::R::i = 3;
21064 will work; we must treat `S<T>::R' as the name of a type.
21065 Similarly, assume that qualified names are templates, where
21069 int S<T>::R<T>::i = 3;
21072 id
= cp_parser_id_expression (parser
,
21073 /*template_keyword_p=*/false,
21074 /*check_dependency_p=*/false,
21075 /*template_p=*/NULL
,
21076 /*declarator_p=*/true,
21078 if (id
&& BASELINK_P (id
))
21079 id
= BASELINK_FUNCTIONS (id
);
21083 /* Parse a type-id.
21086 type-specifier-seq abstract-declarator [opt]
21088 Returns the TYPE specified. */
21091 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21092 bool is_trailing_return
)
21094 cp_decl_specifier_seq type_specifier_seq
;
21095 cp_declarator
*abstract_declarator
;
21097 /* Parse the type-specifier-seq. */
21098 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21099 is_trailing_return
,
21100 &type_specifier_seq
);
21101 if (is_template_arg
&& type_specifier_seq
.type
21102 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21103 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21104 /* A bare template name as a template argument is a template template
21105 argument, not a placeholder, so fail parsing it as a type argument. */
21107 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21108 cp_parser_simulate_error (parser
);
21109 return error_mark_node
;
21111 if (type_specifier_seq
.type
== error_mark_node
)
21112 return error_mark_node
;
21114 /* There might or might not be an abstract declarator. */
21115 cp_parser_parse_tentatively (parser
);
21116 /* Look for the declarator. */
21117 abstract_declarator
21118 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21119 /*parenthesized_p=*/NULL
,
21120 /*member_p=*/false,
21121 /*friend_p=*/false);
21122 /* Check to see if there really was a declarator. */
21123 if (!cp_parser_parse_definitely (parser
))
21124 abstract_declarator
= NULL
;
21126 if (type_specifier_seq
.type
21127 /* The concepts TS allows 'auto' as a type-id. */
21128 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21129 /* None of the valid uses of 'auto' in C++14 involve the type-id
21130 nonterminal, but it is valid in a trailing-return-type. */
21131 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21132 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21134 /* A type-id with type 'auto' is only ok if the abstract declarator
21135 is a function declarator with a late-specified return type.
21137 A type-id with 'auto' is also valid in a trailing-return-type
21138 in a compound-requirement. */
21139 if (abstract_declarator
21140 && abstract_declarator
->kind
== cdk_function
21141 && abstract_declarator
->u
.function
.late_return_type
)
21143 else if (parser
->in_result_type_constraint_p
)
21147 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21148 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21150 error_at (loc
, "missing template arguments after %qT",
21152 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21156 error_at (loc
, "invalid use of %qT", auto_node
);
21157 return error_mark_node
;
21161 return groktypename (&type_specifier_seq
, abstract_declarator
,
21166 cp_parser_type_id (cp_parser
*parser
)
21168 return cp_parser_type_id_1 (parser
, false, false);
21172 cp_parser_template_type_arg (cp_parser
*parser
)
21175 const char *saved_message
= parser
->type_definition_forbidden_message
;
21176 parser
->type_definition_forbidden_message
21177 = G_("types may not be defined in template arguments");
21178 r
= cp_parser_type_id_1 (parser
, true, false);
21179 parser
->type_definition_forbidden_message
= saved_message
;
21180 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21182 error ("invalid use of %<auto%> in template argument");
21183 r
= error_mark_node
;
21189 cp_parser_trailing_type_id (cp_parser
*parser
)
21191 return cp_parser_type_id_1 (parser
, false, true);
21194 /* Parse a type-specifier-seq.
21196 type-specifier-seq:
21197 type-specifier type-specifier-seq [opt]
21201 type-specifier-seq:
21202 attributes type-specifier-seq [opt]
21204 If IS_DECLARATION is true, we are at the start of a "condition" or
21205 exception-declaration, so we might be followed by a declarator-id.
21207 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21208 i.e. we've just seen "->".
21210 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21213 cp_parser_type_specifier_seq (cp_parser
* parser
,
21214 bool is_declaration
,
21215 bool is_trailing_return
,
21216 cp_decl_specifier_seq
*type_specifier_seq
)
21218 bool seen_type_specifier
= false;
21219 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21220 cp_token
*start_token
= NULL
;
21222 /* Clear the TYPE_SPECIFIER_SEQ. */
21223 clear_decl_specs (type_specifier_seq
);
21225 /* In the context of a trailing return type, enum E { } is an
21226 elaborated-type-specifier followed by a function-body, not an
21228 if (is_trailing_return
)
21229 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21231 /* Parse the type-specifiers and attributes. */
21234 tree type_specifier
;
21235 bool is_cv_qualifier
;
21237 /* Check for attributes first. */
21238 if (cp_next_tokens_can_be_attribute_p (parser
))
21240 type_specifier_seq
->attributes
21241 = attr_chainon (type_specifier_seq
->attributes
,
21242 cp_parser_attributes_opt (parser
));
21246 /* record the token of the beginning of the type specifier seq,
21247 for error reporting purposes*/
21249 start_token
= cp_lexer_peek_token (parser
->lexer
);
21251 /* Look for the type-specifier. */
21252 type_specifier
= cp_parser_type_specifier (parser
,
21254 type_specifier_seq
,
21255 /*is_declaration=*/false,
21258 if (!type_specifier
)
21260 /* If the first type-specifier could not be found, this is not a
21261 type-specifier-seq at all. */
21262 if (!seen_type_specifier
)
21264 /* Set in_declarator_p to avoid skipping to the semicolon. */
21265 int in_decl
= parser
->in_declarator_p
;
21266 parser
->in_declarator_p
= true;
21268 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21269 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21270 cp_parser_error (parser
, "expected type-specifier");
21272 parser
->in_declarator_p
= in_decl
;
21274 type_specifier_seq
->type
= error_mark_node
;
21277 /* If subsequent type-specifiers could not be found, the
21278 type-specifier-seq is complete. */
21282 seen_type_specifier
= true;
21283 /* The standard says that a condition can be:
21285 type-specifier-seq declarator = assignment-expression
21292 we should treat the "S" as a declarator, not as a
21293 type-specifier. The standard doesn't say that explicitly for
21294 type-specifier-seq, but it does say that for
21295 decl-specifier-seq in an ordinary declaration. Perhaps it
21296 would be clearer just to allow a decl-specifier-seq here, and
21297 then add a semantic restriction that if any decl-specifiers
21298 that are not type-specifiers appear, the program is invalid. */
21299 if (is_declaration
&& !is_cv_qualifier
)
21300 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21304 /* Return whether the function currently being declared has an associated
21305 template parameter list. */
21308 function_being_declared_is_template_p (cp_parser
* parser
)
21310 if (!current_template_parms
|| processing_template_parmlist
)
21313 if (parser
->implicit_template_scope
)
21316 if (at_class_scope_p ()
21317 && TYPE_BEING_DEFINED (current_class_type
))
21318 return parser
->num_template_parameter_lists
!= 0;
21320 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21321 (current_class_type
));
21324 /* Parse a parameter-declaration-clause.
21326 parameter-declaration-clause:
21327 parameter-declaration-list [opt] ... [opt]
21328 parameter-declaration-list , ...
21330 Returns a representation for the parameter declarations. A return
21331 value of NULL indicates a parameter-declaration-clause consisting
21332 only of an ellipsis. */
21335 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21341 temp_override
<bool> cleanup
21342 (parser
->auto_is_implicit_function_template_parm_p
);
21344 if (!processing_specialization
21345 && !processing_template_parmlist
21346 && !processing_explicit_instantiation
21347 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21348 actual function or a random abstract declarator. */
21349 && parser
->default_arg_ok_p
)
21350 if (!current_function_decl
21351 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21352 parser
->auto_is_implicit_function_template_parm_p
= true;
21354 /* Peek at the next token. */
21355 token
= cp_lexer_peek_token (parser
->lexer
);
21356 /* Check for trivial parameter-declaration-clauses. */
21357 if (token
->type
== CPP_ELLIPSIS
)
21359 /* Consume the `...' token. */
21360 cp_lexer_consume_token (parser
->lexer
);
21363 else if (token
->type
== CPP_CLOSE_PAREN
)
21364 /* There are no parameters. */
21366 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21367 if (in_system_header_at (input_location
)
21368 && current_class_type
== NULL
21369 && current_lang_name
== lang_name_c
)
21373 return void_list_node
;
21375 /* Check for `(void)', too, which is a special case. */
21376 else if (token
->keyword
== RID_VOID
21377 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21378 == CPP_CLOSE_PAREN
))
21380 /* Consume the `void' token. */
21381 cp_lexer_consume_token (parser
->lexer
);
21382 /* There are no parameters. */
21383 return void_list_node
;
21386 /* Parse the parameter-declaration-list. */
21387 parameters
= cp_parser_parameter_declaration_list (parser
);
21388 /* If a parse error occurred while parsing the
21389 parameter-declaration-list, then the entire
21390 parameter-declaration-clause is erroneous. */
21391 if (parameters
== error_mark_node
)
21394 /* Peek at the next token. */
21395 token
= cp_lexer_peek_token (parser
->lexer
);
21396 /* If it's a `,', the clause should terminate with an ellipsis. */
21397 if (token
->type
== CPP_COMMA
)
21399 /* Consume the `,'. */
21400 cp_lexer_consume_token (parser
->lexer
);
21401 /* Expect an ellipsis. */
21403 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21405 /* It might also be `...' if the optional trailing `,' was
21407 else if (token
->type
== CPP_ELLIPSIS
)
21409 /* Consume the `...' token. */
21410 cp_lexer_consume_token (parser
->lexer
);
21411 /* And remember that we saw it. */
21415 ellipsis_p
= false;
21417 /* Finish the parameter list. */
21419 parameters
= chainon (parameters
, void_list_node
);
21424 /* Parse a parameter-declaration-list.
21426 parameter-declaration-list:
21427 parameter-declaration
21428 parameter-declaration-list , parameter-declaration
21430 Returns a representation of the parameter-declaration-list, as for
21431 cp_parser_parameter_declaration_clause. However, the
21432 `void_list_node' is never appended to the list. */
21435 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21437 tree parameters
= NULL_TREE
;
21438 tree
*tail
= ¶meters
;
21439 bool saved_in_unbraced_linkage_specification_p
;
21442 /* The special considerations that apply to a function within an
21443 unbraced linkage specifications do not apply to the parameters
21444 to the function. */
21445 saved_in_unbraced_linkage_specification_p
21446 = parser
->in_unbraced_linkage_specification_p
;
21447 parser
->in_unbraced_linkage_specification_p
= false;
21449 /* Look for more parameters. */
21452 cp_parameter_declarator
*parameter
;
21453 tree decl
= error_mark_node
;
21454 bool parenthesized_p
= false;
21456 /* Parse the parameter. */
21458 = cp_parser_parameter_declaration (parser
,
21459 /*template_parm_p=*/false,
21462 /* We don't know yet if the enclosing context is deprecated, so wait
21463 and warn in grokparms if appropriate. */
21464 deprecated_state
= DEPRECATED_SUPPRESS
;
21468 decl
= grokdeclarator (parameter
->declarator
,
21469 ¶meter
->decl_specifiers
,
21471 parameter
->default_argument
!= NULL_TREE
,
21472 ¶meter
->decl_specifiers
.attributes
);
21473 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21474 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21477 deprecated_state
= DEPRECATED_NORMAL
;
21479 /* If a parse error occurred parsing the parameter declaration,
21480 then the entire parameter-declaration-list is erroneous. */
21481 if (decl
== error_mark_node
)
21483 parameters
= error_mark_node
;
21487 if (parameter
->decl_specifiers
.attributes
)
21488 cplus_decl_attributes (&decl
,
21489 parameter
->decl_specifiers
.attributes
,
21491 if (DECL_NAME (decl
))
21492 decl
= pushdecl (decl
);
21494 if (decl
!= error_mark_node
)
21496 retrofit_lang_decl (decl
);
21497 DECL_PARM_INDEX (decl
) = ++index
;
21498 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21501 /* Add the new parameter to the list. */
21502 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21503 tail
= &TREE_CHAIN (*tail
);
21505 /* Peek at the next token. */
21506 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21507 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21508 /* These are for Objective-C++ */
21509 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21510 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21511 /* The parameter-declaration-list is complete. */
21513 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21517 /* Peek at the next token. */
21518 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21519 /* If it's an ellipsis, then the list is complete. */
21520 if (token
->type
== CPP_ELLIPSIS
)
21522 /* Otherwise, there must be more parameters. Consume the
21524 cp_lexer_consume_token (parser
->lexer
);
21525 /* When parsing something like:
21527 int i(float f, double d)
21529 we can tell after seeing the declaration for "f" that we
21530 are not looking at an initialization of a variable "i",
21531 but rather at the declaration of a function "i".
21533 Due to the fact that the parsing of template arguments
21534 (as specified to a template-id) requires backtracking we
21535 cannot use this technique when inside a template argument
21537 if (!parser
->in_template_argument_list_p
21538 && !parser
->in_type_id_in_expr_p
21539 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21540 /* However, a parameter-declaration of the form
21541 "float(f)" (which is a valid declaration of a
21542 parameter "f") can also be interpreted as an
21543 expression (the conversion of "f" to "float"). */
21544 && !parenthesized_p
)
21545 cp_parser_commit_to_tentative_parse (parser
);
21549 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21551 cp_parser_skip_to_closing_parenthesis (parser
,
21552 /*recovering=*/true,
21553 /*or_comma=*/false,
21554 /*consume_paren=*/false);
21559 parser
->in_unbraced_linkage_specification_p
21560 = saved_in_unbraced_linkage_specification_p
;
21562 /* Reset implicit_template_scope if we are about to leave the function
21563 parameter list that introduced it. Note that for out-of-line member
21564 definitions, there will be one or more class scopes before we get to
21565 the template parameter scope. */
21567 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21568 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21570 while (maybe_its
->kind
== sk_class
)
21571 maybe_its
= maybe_its
->level_chain
;
21572 if (maybe_its
== its
)
21574 parser
->implicit_template_parms
= 0;
21575 parser
->implicit_template_scope
= 0;
21582 /* Parse a parameter declaration.
21584 parameter-declaration:
21585 decl-specifier-seq ... [opt] declarator
21586 decl-specifier-seq declarator = assignment-expression
21587 decl-specifier-seq ... [opt] abstract-declarator [opt]
21588 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21590 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21591 declares a template parameter. (In that case, a non-nested `>'
21592 token encountered during the parsing of the assignment-expression
21593 is not interpreted as a greater-than operator.)
21595 Returns a representation of the parameter, or NULL if an error
21596 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21597 true iff the declarator is of the form "(p)". */
21599 static cp_parameter_declarator
*
21600 cp_parser_parameter_declaration (cp_parser
*parser
,
21601 bool template_parm_p
,
21602 bool *parenthesized_p
)
21604 int declares_class_or_enum
;
21605 cp_decl_specifier_seq decl_specifiers
;
21606 cp_declarator
*declarator
;
21607 tree default_argument
;
21608 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21609 const char *saved_message
;
21610 bool template_parameter_pack_p
= false;
21612 /* In a template parameter, `>' is not an operator.
21616 When parsing a default template-argument for a non-type
21617 template-parameter, the first non-nested `>' is taken as the end
21618 of the template parameter-list rather than a greater-than
21621 /* Type definitions may not appear in parameter types. */
21622 saved_message
= parser
->type_definition_forbidden_message
;
21623 parser
->type_definition_forbidden_message
21624 = G_("types may not be defined in parameter types");
21626 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21627 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21628 (current_template_parms
)) : 0);
21630 /* Parse the declaration-specifiers. */
21631 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21632 cp_parser_decl_specifier_seq (parser
,
21633 CP_PARSER_FLAGS_NONE
,
21635 &declares_class_or_enum
);
21637 /* Complain about missing 'typename' or other invalid type names. */
21638 if (!decl_specifiers
.any_type_specifiers_p
21639 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21640 decl_specifiers
.type
= error_mark_node
;
21642 /* If an error occurred, there's no reason to attempt to parse the
21643 rest of the declaration. */
21644 if (cp_parser_error_occurred (parser
))
21646 parser
->type_definition_forbidden_message
= saved_message
;
21650 /* Peek at the next token. */
21651 token
= cp_lexer_peek_token (parser
->lexer
);
21653 /* If the next token is a `)', `,', `=', `>', or `...', then there
21654 is no declarator. However, when variadic templates are enabled,
21655 there may be a declarator following `...'. */
21656 if (token
->type
== CPP_CLOSE_PAREN
21657 || token
->type
== CPP_COMMA
21658 || token
->type
== CPP_EQ
21659 || token
->type
== CPP_GREATER
)
21662 if (parenthesized_p
)
21663 *parenthesized_p
= false;
21665 /* Otherwise, there should be a declarator. */
21668 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21669 parser
->default_arg_ok_p
= false;
21671 /* After seeing a decl-specifier-seq, if the next token is not a
21672 "(", there is no possibility that the code is a valid
21673 expression. Therefore, if parsing tentatively, we commit at
21675 if (!parser
->in_template_argument_list_p
21676 /* In an expression context, having seen:
21680 we cannot be sure whether we are looking at a
21681 function-type (taking a "char" as a parameter) or a cast
21682 of some object of type "char" to "int". */
21683 && !parser
->in_type_id_in_expr_p
21684 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21685 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21686 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21687 cp_parser_commit_to_tentative_parse (parser
);
21688 /* Parse the declarator. */
21689 declarator_token_start
= token
;
21690 declarator
= cp_parser_declarator (parser
,
21691 CP_PARSER_DECLARATOR_EITHER
,
21692 /*ctor_dtor_or_conv_p=*/NULL
,
21694 /*member_p=*/false,
21695 /*friend_p=*/false);
21696 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21697 /* After the declarator, allow more attributes. */
21698 decl_specifiers
.attributes
21699 = attr_chainon (decl_specifiers
.attributes
,
21700 cp_parser_attributes_opt (parser
));
21702 /* If the declarator is a template parameter pack, remember that and
21703 clear the flag in the declarator itself so we don't get errors
21704 from grokdeclarator. */
21705 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21707 declarator
->parameter_pack_p
= false;
21708 template_parameter_pack_p
= true;
21712 /* If the next token is an ellipsis, and we have not seen a declarator
21713 name, and if either the type of the declarator contains parameter
21714 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21715 for, eg, abbreviated integral type names), then we actually have a
21716 parameter pack expansion expression. Otherwise, leave the ellipsis
21717 for a C-style variadic function. */
21718 token
= cp_lexer_peek_token (parser
->lexer
);
21720 /* If a function parameter pack was specified and an implicit template
21721 parameter was introduced during cp_parser_parameter_declaration,
21722 change any implicit parameters introduced into packs. */
21723 if (parser
->implicit_template_parms
21724 && (token
->type
== CPP_ELLIPSIS
21725 || (declarator
&& declarator
->parameter_pack_p
)))
21727 int latest_template_parm_idx
= TREE_VEC_LENGTH
21728 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21730 if (latest_template_parm_idx
!= template_parm_idx
)
21731 decl_specifiers
.type
= convert_generic_types_to_packs
21732 (decl_specifiers
.type
,
21733 template_parm_idx
, latest_template_parm_idx
);
21736 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21738 tree type
= decl_specifiers
.type
;
21740 if (type
&& DECL_P (type
))
21741 type
= TREE_TYPE (type
);
21744 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21745 && (template_parm_p
|| uses_parameter_packs (type
)))
21746 || (!type
&& template_parm_p
))
21747 && declarator_can_be_parameter_pack (declarator
))
21749 /* Consume the `...'. */
21750 cp_lexer_consume_token (parser
->lexer
);
21751 maybe_warn_variadic_templates ();
21753 /* Build a pack expansion type */
21754 if (template_parm_p
)
21755 template_parameter_pack_p
= true;
21756 else if (declarator
)
21757 declarator
->parameter_pack_p
= true;
21759 decl_specifiers
.type
= make_pack_expansion (type
);
21763 /* The restriction on defining new types applies only to the type
21764 of the parameter, not to the default argument. */
21765 parser
->type_definition_forbidden_message
= saved_message
;
21767 /* If the next token is `=', then process a default argument. */
21768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21770 tree type
= decl_specifiers
.type
;
21771 token
= cp_lexer_peek_token (parser
->lexer
);
21772 /* If we are defining a class, then the tokens that make up the
21773 default argument must be saved and processed later. */
21774 if (!template_parm_p
&& at_class_scope_p ()
21775 && TYPE_BEING_DEFINED (current_class_type
)
21776 && !LAMBDA_TYPE_P (current_class_type
))
21777 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21779 // A constrained-type-specifier may declare a type template-parameter.
21780 else if (declares_constrained_type_template_parameter (type
))
21782 = cp_parser_default_type_template_argument (parser
);
21784 // A constrained-type-specifier may declare a template-template-parameter.
21785 else if (declares_constrained_template_template_parameter (type
))
21787 = cp_parser_default_template_template_argument (parser
);
21789 /* Outside of a class definition, we can just parse the
21790 assignment-expression. */
21793 = cp_parser_default_argument (parser
, template_parm_p
);
21795 if (!parser
->default_arg_ok_p
)
21797 permerror (token
->location
,
21798 "default arguments are only "
21799 "permitted for function parameters");
21801 else if ((declarator
&& declarator
->parameter_pack_p
)
21802 || template_parameter_pack_p
21803 || (decl_specifiers
.type
21804 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21806 /* Find the name of the parameter pack. */
21807 cp_declarator
*id_declarator
= declarator
;
21808 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21809 id_declarator
= id_declarator
->declarator
;
21811 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21812 error_at (declarator_token_start
->location
,
21814 ? G_("template parameter pack %qD "
21815 "cannot have a default argument")
21816 : G_("parameter pack %qD cannot have "
21817 "a default argument"),
21818 id_declarator
->u
.id
.unqualified_name
);
21820 error_at (declarator_token_start
->location
,
21822 ? G_("template parameter pack cannot have "
21823 "a default argument")
21824 : G_("parameter pack cannot have a "
21825 "default argument"));
21827 default_argument
= NULL_TREE
;
21831 default_argument
= NULL_TREE
;
21833 /* Generate a location for the parameter, ranging from the start of the
21834 initial token to the end of the final token (using input_location for
21835 the latter, set up by cp_lexer_set_source_position_from_token when
21838 If we have a identifier, then use it for the caret location, e.g.
21840 extern int callee (int one, int (*two)(int, int), float three);
21841 ~~~~~~^~~~~~~~~~~~~~
21843 otherwise, reuse the start location for the caret location e.g.:
21845 extern int callee (int one, int (*)(int, int), float three);
21849 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21850 ? declarator
->id_loc
21851 : decl_spec_token_start
->location
);
21852 location_t param_loc
= make_location (caret_loc
,
21853 decl_spec_token_start
->location
,
21856 return make_parameter_declarator (&decl_specifiers
,
21860 template_parameter_pack_p
);
21863 /* Parse a default argument and return it.
21865 TEMPLATE_PARM_P is true if this is a default argument for a
21866 non-type template parameter. */
21868 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21870 tree default_argument
= NULL_TREE
;
21871 bool saved_greater_than_is_operator_p
;
21872 bool saved_local_variables_forbidden_p
;
21873 bool non_constant_p
, is_direct_init
;
21875 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21877 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21878 parser
->greater_than_is_operator_p
= !template_parm_p
;
21879 /* Local variable names (and the `this' keyword) may not
21880 appear in a default argument. */
21881 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21882 parser
->local_variables_forbidden_p
= true;
21883 /* Parse the assignment-expression. */
21884 if (template_parm_p
)
21885 push_deferring_access_checks (dk_no_deferred
);
21886 tree saved_class_ptr
= NULL_TREE
;
21887 tree saved_class_ref
= NULL_TREE
;
21888 /* The "this" pointer is not valid in a default argument. */
21891 saved_class_ptr
= current_class_ptr
;
21892 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21893 saved_class_ref
= current_class_ref
;
21894 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21897 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21898 /* Restore the "this" pointer. */
21901 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21902 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21904 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21905 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21906 if (template_parm_p
)
21907 pop_deferring_access_checks ();
21908 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21909 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21911 return default_argument
;
21914 /* Parse a function-body.
21917 compound_statement */
21920 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21922 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21923 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21927 /* Parse a ctor-initializer-opt followed by a function-body. Return
21928 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21929 is true we are parsing a function-try-block. */
21932 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21933 bool in_function_try_block
)
21936 const bool check_body_p
=
21937 DECL_CONSTRUCTOR_P (current_function_decl
)
21938 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21941 /* Begin the function body. */
21942 body
= begin_function_body ();
21943 /* Parse the optional ctor-initializer. */
21944 cp_parser_ctor_initializer_opt (parser
);
21946 /* If we're parsing a constexpr constructor definition, we need
21947 to check that the constructor body is indeed empty. However,
21948 before we get to cp_parser_function_body lot of junk has been
21949 generated, so we can't just check that we have an empty block.
21950 Rather we take a snapshot of the outermost block, and check whether
21951 cp_parser_function_body changed its state. */
21954 list
= cur_stmt_list
;
21955 if (STATEMENT_LIST_TAIL (list
))
21956 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21958 /* Parse the function-body. */
21959 cp_parser_function_body (parser
, in_function_try_block
);
21961 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21962 /* Finish the function body. */
21963 finish_function_body (body
);
21966 /* Parse an initializer.
21969 = initializer-clause
21970 ( expression-list )
21972 Returns an expression representing the initializer. If no
21973 initializer is present, NULL_TREE is returned.
21975 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21976 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21977 set to TRUE if there is no initializer present. If there is an
21978 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21979 is set to true; otherwise it is set to false. */
21982 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21983 bool* non_constant_p
, bool subexpression_p
)
21988 /* Peek at the next token. */
21989 token
= cp_lexer_peek_token (parser
->lexer
);
21991 /* Let our caller know whether or not this initializer was
21993 *is_direct_init
= (token
->type
!= CPP_EQ
);
21994 /* Assume that the initializer is constant. */
21995 *non_constant_p
= false;
21997 if (token
->type
== CPP_EQ
)
21999 /* Consume the `='. */
22000 cp_lexer_consume_token (parser
->lexer
);
22001 /* Parse the initializer-clause. */
22002 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22004 else if (token
->type
== CPP_OPEN_PAREN
)
22006 vec
<tree
, va_gc
> *vec
;
22007 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22009 /*allow_expansion_p=*/true,
22012 return error_mark_node
;
22013 init
= build_tree_list_vec (vec
);
22014 release_tree_vector (vec
);
22016 else if (token
->type
== CPP_OPEN_BRACE
)
22018 cp_lexer_set_source_position (parser
->lexer
);
22019 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22020 init
= cp_parser_braced_list (parser
, non_constant_p
);
22021 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22025 /* Anything else is an error. */
22026 cp_parser_error (parser
, "expected initializer");
22027 init
= error_mark_node
;
22030 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22031 init
= error_mark_node
;
22036 /* Parse an initializer-clause.
22038 initializer-clause:
22039 assignment-expression
22042 Returns an expression representing the initializer.
22044 If the `assignment-expression' production is used the value
22045 returned is simply a representation for the expression.
22047 Otherwise, calls cp_parser_braced_list. */
22050 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22052 cp_expr initializer
;
22054 /* Assume the expression is constant. */
22055 *non_constant_p
= false;
22057 /* If it is not a `{', then we are looking at an
22058 assignment-expression. */
22059 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22062 = cp_parser_constant_expression (parser
,
22063 /*allow_non_constant_p=*/true,
22067 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22069 return initializer
;
22072 /* Parse a brace-enclosed initializer list.
22075 { initializer-list , [opt] }
22076 { designated-initializer-list , [opt] }
22079 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22080 the elements of the initializer-list (or NULL, if the last
22081 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22082 NULL_TREE. There is no way to detect whether or not the optional
22083 trailing `,' was provided. NON_CONSTANT_P is as for
22084 cp_parser_initializer. */
22087 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22090 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22092 /* Consume the `{' token. */
22093 matching_braces braces
;
22094 braces
.require_open (parser
);
22095 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22096 initializer
= make_node (CONSTRUCTOR
);
22097 /* If it's not a `}', then there is a non-trivial initializer. */
22098 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22100 /* Parse the initializer list. */
22101 CONSTRUCTOR_ELTS (initializer
)
22102 = cp_parser_initializer_list (parser
, non_constant_p
);
22103 /* A trailing `,' token is allowed. */
22104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22105 cp_lexer_consume_token (parser
->lexer
);
22108 *non_constant_p
= false;
22109 /* Now, there should be a trailing `}'. */
22110 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22111 braces
.require_close (parser
);
22112 TREE_TYPE (initializer
) = init_list_type_node
;
22114 cp_expr
result (initializer
);
22115 /* Build a location of the form:
22118 with caret==start at the open brace, finish at the close brace. */
22119 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22120 result
.set_location (combined_loc
);
22124 /* Consume tokens up to, and including, the next non-nested closing `]'.
22125 Returns true iff we found a closing `]'. */
22128 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22130 unsigned square_depth
= 0;
22134 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22136 switch (token
->type
)
22139 case CPP_PRAGMA_EOL
:
22140 /* If we've run out of tokens, then there is no closing `]'. */
22143 case CPP_OPEN_SQUARE
:
22147 case CPP_CLOSE_SQUARE
:
22148 if (!square_depth
--)
22150 cp_lexer_consume_token (parser
->lexer
);
22159 /* Consume the token. */
22160 cp_lexer_consume_token (parser
->lexer
);
22164 /* Return true if we are looking at an array-designator, false otherwise. */
22167 cp_parser_array_designator_p (cp_parser
*parser
)
22169 /* Consume the `['. */
22170 cp_lexer_consume_token (parser
->lexer
);
22172 cp_lexer_save_tokens (parser
->lexer
);
22174 /* Skip tokens until the next token is a closing square bracket.
22175 If we find the closing `]', and the next token is a `=', then
22176 we are looking at an array designator. */
22177 bool array_designator_p
22178 = (cp_parser_skip_to_closing_square_bracket (parser
)
22179 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22181 /* Roll back the tokens we skipped. */
22182 cp_lexer_rollback_tokens (parser
->lexer
);
22184 return array_designator_p
;
22187 /* Parse an initializer-list.
22190 initializer-clause ... [opt]
22191 initializer-list , initializer-clause ... [opt]
22195 designated-initializer-list:
22196 designated-initializer-clause
22197 designated-initializer-list , designated-initializer-clause
22199 designated-initializer-clause:
22200 designator brace-or-equal-initializer
22208 designation initializer-clause ...[opt]
22209 initializer-list , designation initializer-clause ...[opt]
22214 [ constant-expression ] =
22216 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22217 for the initializer. If the INDEX of the elt is non-NULL, it is the
22218 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22219 as for cp_parser_initializer. */
22221 static vec
<constructor_elt
, va_gc
> *
22222 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22224 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22225 bool first_p
= true;
22226 tree first_designator
= NULL_TREE
;
22228 /* Assume all of the expressions are constant. */
22229 *non_constant_p
= false;
22231 /* Parse the rest of the list. */
22237 bool clause_non_constant_p
;
22238 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22240 /* Handle the C++2A syntax, '. id ='. */
22241 if ((cxx_dialect
>= cxx2a
22242 || cp_parser_allow_gnu_extensions_p (parser
))
22243 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22244 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22245 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22246 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22247 == CPP_OPEN_BRACE
)))
22249 if (cxx_dialect
< cxx2a
)
22250 pedwarn (loc
, OPT_Wpedantic
,
22251 "C++ designated initializers only available with "
22252 "-std=c++2a or -std=gnu++2a");
22253 /* Consume the `.'. */
22254 cp_lexer_consume_token (parser
->lexer
);
22255 /* Consume the identifier. */
22256 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22258 /* Consume the `='. */
22259 cp_lexer_consume_token (parser
->lexer
);
22261 /* Also, if the next token is an identifier and the following one is a
22262 colon, we are looking at the GNU designated-initializer
22264 else if (cp_parser_allow_gnu_extensions_p (parser
)
22265 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22266 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22269 /* Warn the user that they are using an extension. */
22270 pedwarn (loc
, OPT_Wpedantic
,
22271 "ISO C++ does not allow GNU designated initializers");
22272 /* Consume the identifier. */
22273 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22274 /* Consume the `:'. */
22275 cp_lexer_consume_token (parser
->lexer
);
22277 /* Also handle C99 array designators, '[ const ] ='. */
22278 else if (cp_parser_allow_gnu_extensions_p (parser
)
22279 && !c_dialect_objc ()
22280 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22282 /* In C++11, [ could start a lambda-introducer. */
22283 bool non_const
= false;
22285 cp_parser_parse_tentatively (parser
);
22287 if (!cp_parser_array_designator_p (parser
))
22289 cp_parser_simulate_error (parser
);
22290 designator
= NULL_TREE
;
22294 designator
= cp_parser_constant_expression (parser
, true,
22296 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22297 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22300 if (!cp_parser_parse_definitely (parser
))
22301 designator
= NULL_TREE
;
22303 && (!require_potential_rvalue_constant_expression
22305 designator
= NULL_TREE
;
22307 /* Warn the user that they are using an extension. */
22308 pedwarn (loc
, OPT_Wpedantic
,
22309 "ISO C++ does not allow C99 designated initializers");
22312 designator
= NULL_TREE
;
22316 first_designator
= designator
;
22319 else if (cxx_dialect
>= cxx2a
22320 && first_designator
!= error_mark_node
22321 && (!first_designator
!= !designator
))
22323 error_at (loc
, "either all initializer clauses should be designated "
22324 "or none of them should be");
22325 first_designator
= error_mark_node
;
22327 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22328 first_designator
= designator
;
22330 /* Parse the initializer. */
22331 initializer
= cp_parser_initializer_clause (parser
,
22332 &clause_non_constant_p
);
22333 /* If any clause is non-constant, so is the entire initializer. */
22334 if (clause_non_constant_p
)
22335 *non_constant_p
= true;
22337 /* If we have an ellipsis, this is an initializer pack
22339 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22341 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22343 /* Consume the `...'. */
22344 cp_lexer_consume_token (parser
->lexer
);
22346 if (designator
&& cxx_dialect
>= cxx2a
)
22348 "%<...%> not allowed in designated initializer list");
22350 /* Turn the initializer into an initializer expansion. */
22351 initializer
= make_pack_expansion (initializer
);
22354 /* Add it to the vector. */
22355 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22357 /* If the next token is not a comma, we have reached the end of
22359 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22362 /* Peek at the next token. */
22363 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22364 /* If the next token is a `}', then we're still done. An
22365 initializer-clause can have a trailing `,' after the
22366 initializer-list and before the closing `}'. */
22367 if (token
->type
== CPP_CLOSE_BRACE
)
22370 /* Consume the `,' token. */
22371 cp_lexer_consume_token (parser
->lexer
);
22374 /* The same identifier shall not appear in multiple designators
22375 of a designated-initializer-list. */
22376 if (first_designator
)
22379 tree designator
, val
;
22380 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22381 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22383 if (IDENTIFIER_MARKED (designator
))
22385 error_at (cp_expr_loc_or_loc (val
, input_location
),
22386 "%<.%s%> designator used multiple times in "
22387 "the same initializer list",
22388 IDENTIFIER_POINTER (designator
));
22389 (*v
)[i
].index
= NULL_TREE
;
22392 IDENTIFIER_MARKED (designator
) = 1;
22394 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22395 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22396 IDENTIFIER_MARKED (designator
) = 0;
22402 /* Classes [gram.class] */
22404 /* Parse a class-name.
22410 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22411 to indicate that names looked up in dependent types should be
22412 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22413 keyword has been used to indicate that the name that appears next
22414 is a template. TAG_TYPE indicates the explicit tag given before
22415 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22416 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22417 is the class being defined in a class-head. If ENUM_OK is TRUE,
22418 enum-names are also accepted.
22420 Returns the TYPE_DECL representing the class. */
22423 cp_parser_class_name (cp_parser
*parser
,
22424 bool typename_keyword_p
,
22425 bool template_keyword_p
,
22426 enum tag_types tag_type
,
22427 bool check_dependency_p
,
22429 bool is_declaration
,
22436 tree identifier
= NULL_TREE
;
22438 /* All class-names start with an identifier. */
22439 token
= cp_lexer_peek_token (parser
->lexer
);
22440 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22442 cp_parser_error (parser
, "expected class-name");
22443 return error_mark_node
;
22446 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22447 to a template-id, so we save it here. */
22448 scope
= parser
->scope
;
22449 if (scope
== error_mark_node
)
22450 return error_mark_node
;
22452 /* Any name names a type if we're following the `typename' keyword
22453 in a qualified name where the enclosing scope is type-dependent. */
22454 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22455 && dependent_type_p (scope
));
22456 /* Handle the common case (an identifier, but not a template-id)
22458 if (token
->type
== CPP_NAME
22459 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22461 cp_token
*identifier_token
;
22464 /* Look for the identifier. */
22465 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22466 ambiguous_p
= identifier_token
->error_reported
;
22467 identifier
= cp_parser_identifier (parser
);
22468 /* If the next token isn't an identifier, we are certainly not
22469 looking at a class-name. */
22470 if (identifier
== error_mark_node
)
22471 decl
= error_mark_node
;
22472 /* If we know this is a type-name, there's no need to look it
22474 else if (typename_p
)
22478 tree ambiguous_decls
;
22479 /* If we already know that this lookup is ambiguous, then
22480 we've already issued an error message; there's no reason
22484 cp_parser_simulate_error (parser
);
22485 return error_mark_node
;
22487 /* If the next token is a `::', then the name must be a type
22490 [basic.lookup.qual]
22492 During the lookup for a name preceding the :: scope
22493 resolution operator, object, function, and enumerator
22494 names are ignored. */
22495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22496 tag_type
= scope_type
;
22497 /* Look up the name. */
22498 decl
= cp_parser_lookup_name (parser
, identifier
,
22500 /*is_template=*/false,
22501 /*is_namespace=*/false,
22502 check_dependency_p
,
22504 identifier_token
->location
);
22505 if (ambiguous_decls
)
22507 if (cp_parser_parsing_tentatively (parser
))
22508 cp_parser_simulate_error (parser
);
22509 return error_mark_node
;
22515 /* Try a template-id. */
22516 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22517 check_dependency_p
,
22520 if (decl
== error_mark_node
)
22521 return error_mark_node
;
22524 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22526 /* If this is a typename, create a TYPENAME_TYPE. */
22527 if (typename_p
&& decl
!= error_mark_node
)
22529 decl
= make_typename_type (scope
, decl
, typename_type
,
22530 /*complain=*/tf_error
);
22531 if (decl
!= error_mark_node
)
22532 decl
= TYPE_NAME (decl
);
22535 decl
= strip_using_decl (decl
);
22537 /* Check to see that it is really the name of a class. */
22538 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22539 && identifier_p (TREE_OPERAND (decl
, 0))
22540 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22541 /* Situations like this:
22543 template <typename T> struct A {
22544 typename T::template X<int>::I i;
22547 are problematic. Is `T::template X<int>' a class-name? The
22548 standard does not seem to be definitive, but there is no other
22549 valid interpretation of the following `::'. Therefore, those
22550 names are considered class-names. */
22552 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22553 if (decl
!= error_mark_node
)
22554 decl
= TYPE_NAME (decl
);
22556 else if (TREE_CODE (decl
) != TYPE_DECL
22557 || TREE_TYPE (decl
) == error_mark_node
22558 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22559 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22560 /* In Objective-C 2.0, a classname followed by '.' starts a
22561 dot-syntax expression, and it's not a type-name. */
22562 || (c_dialect_objc ()
22563 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22564 && objc_is_class_name (decl
)))
22565 decl
= error_mark_node
;
22567 if (decl
== error_mark_node
)
22568 cp_parser_error (parser
, "expected class-name");
22569 else if (identifier
&& !parser
->scope
)
22570 maybe_note_name_used_in_class (identifier
, decl
);
22575 /* Parse a class-specifier.
22578 class-head { member-specification [opt] }
22580 Returns the TREE_TYPE representing the class. */
22583 cp_parser_class_specifier_1 (cp_parser
* parser
)
22586 tree attributes
= NULL_TREE
;
22587 bool nested_name_specifier_p
;
22588 unsigned saved_num_template_parameter_lists
;
22589 bool saved_in_function_body
;
22590 unsigned char in_statement
;
22591 bool in_switch_statement_p
;
22592 bool saved_in_unbraced_linkage_specification_p
;
22593 tree old_scope
= NULL_TREE
;
22594 tree scope
= NULL_TREE
;
22595 cp_token
*closing_brace
;
22597 push_deferring_access_checks (dk_no_deferred
);
22599 /* Parse the class-head. */
22600 type
= cp_parser_class_head (parser
,
22601 &nested_name_specifier_p
);
22602 /* If the class-head was a semantic disaster, skip the entire body
22606 cp_parser_skip_to_end_of_block_or_statement (parser
);
22607 pop_deferring_access_checks ();
22608 return error_mark_node
;
22611 /* Look for the `{'. */
22612 matching_braces braces
;
22613 if (!braces
.require_open (parser
))
22615 pop_deferring_access_checks ();
22616 return error_mark_node
;
22619 cp_ensure_no_omp_declare_simd (parser
);
22620 cp_ensure_no_oacc_routine (parser
);
22622 /* Issue an error message if type-definitions are forbidden here. */
22623 cp_parser_check_type_definition (parser
);
22624 /* Remember that we are defining one more class. */
22625 ++parser
->num_classes_being_defined
;
22626 /* Inside the class, surrounding template-parameter-lists do not
22628 saved_num_template_parameter_lists
22629 = parser
->num_template_parameter_lists
;
22630 parser
->num_template_parameter_lists
= 0;
22631 /* We are not in a function body. */
22632 saved_in_function_body
= parser
->in_function_body
;
22633 parser
->in_function_body
= false;
22634 /* Or in a loop. */
22635 in_statement
= parser
->in_statement
;
22636 parser
->in_statement
= 0;
22637 /* Or in a switch. */
22638 in_switch_statement_p
= parser
->in_switch_statement_p
;
22639 parser
->in_switch_statement_p
= false;
22640 /* We are not immediately inside an extern "lang" block. */
22641 saved_in_unbraced_linkage_specification_p
22642 = parser
->in_unbraced_linkage_specification_p
;
22643 parser
->in_unbraced_linkage_specification_p
= false;
22645 // Associate constraints with the type.
22647 type
= associate_classtype_constraints (type
);
22649 /* Start the class. */
22650 if (nested_name_specifier_p
)
22652 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22653 old_scope
= push_inner_scope (scope
);
22655 type
= begin_class_definition (type
);
22657 if (type
== error_mark_node
)
22658 /* If the type is erroneous, skip the entire body of the class. */
22659 cp_parser_skip_to_closing_brace (parser
);
22661 /* Parse the member-specification. */
22662 cp_parser_member_specification_opt (parser
);
22664 /* Look for the trailing `}'. */
22665 closing_brace
= braces
.require_close (parser
);
22666 /* Look for trailing attributes to apply to this class. */
22667 if (cp_parser_allow_gnu_extensions_p (parser
))
22668 attributes
= cp_parser_gnu_attributes_opt (parser
);
22669 if (type
!= error_mark_node
)
22670 type
= finish_struct (type
, attributes
);
22671 if (nested_name_specifier_p
)
22672 pop_inner_scope (old_scope
, scope
);
22674 /* We've finished a type definition. Check for the common syntax
22675 error of forgetting a semicolon after the definition. We need to
22676 be careful, as we can't just check for not-a-semicolon and be done
22677 with it; the user might have typed:
22679 class X { } c = ...;
22680 class X { } *p = ...;
22682 and so forth. Instead, enumerate all the possible tokens that
22683 might follow this production; if we don't see one of them, then
22684 complain and silently insert the semicolon. */
22686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22687 bool want_semicolon
= true;
22689 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22690 /* Don't try to parse c++11 attributes here. As per the
22691 grammar, that should be a task for
22692 cp_parser_decl_specifier_seq. */
22693 want_semicolon
= false;
22695 switch (token
->type
)
22698 case CPP_SEMICOLON
:
22701 case CPP_OPEN_PAREN
:
22702 case CPP_CLOSE_PAREN
:
22704 want_semicolon
= false;
22707 /* While it's legal for type qualifiers and storage class
22708 specifiers to follow type definitions in the grammar, only
22709 compiler testsuites contain code like that. Assume that if
22710 we see such code, then what we're really seeing is a case
22714 const <type> var = ...;
22719 static <type> func (...) ...
22721 i.e. the qualifier or specifier applies to the next
22722 declaration. To do so, however, we need to look ahead one
22723 more token to see if *that* token is a type specifier.
22725 This code could be improved to handle:
22728 static const <type> var = ...; */
22730 if (keyword_is_decl_specifier (token
->keyword
))
22732 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22734 /* Handling user-defined types here would be nice, but very
22737 = (lookahead
->type
== CPP_KEYWORD
22738 && keyword_begins_type_specifier (lookahead
->keyword
));
22745 /* If we don't have a type, then something is very wrong and we
22746 shouldn't try to do anything clever. Likewise for not seeing the
22748 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22750 /* Locate the closing brace. */
22751 cp_token_position prev
22752 = cp_lexer_previous_token_position (parser
->lexer
);
22753 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22754 location_t loc
= prev_token
->location
;
22756 /* We want to suggest insertion of a ';' immediately *after* the
22757 closing brace, so, if we can, offset the location by 1 column. */
22758 location_t next_loc
= loc
;
22759 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22760 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22762 rich_location
richloc (line_table
, next_loc
);
22764 /* If we successfully offset the location, suggest the fix-it. */
22765 if (next_loc
!= loc
)
22766 richloc
.add_fixit_insert_before (next_loc
, ";");
22768 if (CLASSTYPE_DECLARED_CLASS (type
))
22769 error_at (&richloc
,
22770 "expected %<;%> after class definition");
22771 else if (TREE_CODE (type
) == RECORD_TYPE
)
22772 error_at (&richloc
,
22773 "expected %<;%> after struct definition");
22774 else if (TREE_CODE (type
) == UNION_TYPE
)
22775 error_at (&richloc
,
22776 "expected %<;%> after union definition");
22778 gcc_unreachable ();
22780 /* Unget one token and smash it to look as though we encountered
22781 a semicolon in the input stream. */
22782 cp_lexer_set_token_position (parser
->lexer
, prev
);
22783 token
= cp_lexer_peek_token (parser
->lexer
);
22784 token
->type
= CPP_SEMICOLON
;
22785 token
->keyword
= RID_MAX
;
22789 /* If this class is not itself within the scope of another class,
22790 then we need to parse the bodies of all of the queued function
22791 definitions. Note that the queued functions defined in a class
22792 are not always processed immediately following the
22793 class-specifier for that class. Consider:
22796 struct B { void f() { sizeof (A); } };
22799 If `f' were processed before the processing of `A' were
22800 completed, there would be no way to compute the size of `A'.
22801 Note that the nesting we are interested in here is lexical --
22802 not the semantic nesting given by TYPE_CONTEXT. In particular,
22805 struct A { struct B; };
22806 struct A::B { void f() { } };
22808 there is no need to delay the parsing of `A::B::f'. */
22809 if (--parser
->num_classes_being_defined
== 0)
22812 tree class_type
= NULL_TREE
;
22813 tree pushed_scope
= NULL_TREE
;
22815 cp_default_arg_entry
*e
;
22816 tree save_ccp
, save_ccr
;
22818 if (any_erroneous_template_args_p (type
))
22820 /* Skip default arguments, NSDMIs, etc, in order to improve
22821 error recovery (c++/71169, c++/71832). */
22822 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22823 vec_safe_truncate (unparsed_nsdmis
, 0);
22824 vec_safe_truncate (unparsed_classes
, 0);
22825 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22828 /* In a first pass, parse default arguments to the functions.
22829 Then, in a second pass, parse the bodies of the functions.
22830 This two-phased approach handles cases like:
22838 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22841 /* If there are default arguments that have not yet been processed,
22842 take care of them now. */
22843 if (class_type
!= e
->class_type
)
22846 pop_scope (pushed_scope
);
22847 class_type
= e
->class_type
;
22848 pushed_scope
= push_scope (class_type
);
22850 /* Make sure that any template parameters are in scope. */
22851 maybe_begin_member_template_processing (decl
);
22852 /* Parse the default argument expressions. */
22853 cp_parser_late_parsing_default_args (parser
, decl
);
22854 /* Remove any template parameters from the symbol table. */
22855 maybe_end_member_template_processing ();
22857 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22858 /* Now parse any NSDMIs. */
22859 save_ccp
= current_class_ptr
;
22860 save_ccr
= current_class_ref
;
22861 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22863 if (class_type
!= DECL_CONTEXT (decl
))
22866 pop_scope (pushed_scope
);
22867 class_type
= DECL_CONTEXT (decl
);
22868 pushed_scope
= push_scope (class_type
);
22870 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22871 cp_parser_late_parsing_nsdmi (parser
, decl
);
22873 vec_safe_truncate (unparsed_nsdmis
, 0);
22874 current_class_ptr
= save_ccp
;
22875 current_class_ref
= save_ccr
;
22877 pop_scope (pushed_scope
);
22879 /* Now do some post-NSDMI bookkeeping. */
22880 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22881 after_nsdmi_defaulted_late_checks (class_type
);
22882 vec_safe_truncate (unparsed_classes
, 0);
22883 after_nsdmi_defaulted_late_checks (type
);
22885 /* Now parse the body of the functions. */
22888 /* OpenMP UDRs need to be parsed before all other functions. */
22889 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22890 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22891 cp_parser_late_parsing_for_member (parser
, decl
);
22892 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22893 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22894 cp_parser_late_parsing_for_member (parser
, decl
);
22897 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22898 cp_parser_late_parsing_for_member (parser
, decl
);
22899 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22902 vec_safe_push (unparsed_classes
, type
);
22904 /* Put back any saved access checks. */
22905 pop_deferring_access_checks ();
22907 /* Restore saved state. */
22908 parser
->in_switch_statement_p
= in_switch_statement_p
;
22909 parser
->in_statement
= in_statement
;
22910 parser
->in_function_body
= saved_in_function_body
;
22911 parser
->num_template_parameter_lists
22912 = saved_num_template_parameter_lists
;
22913 parser
->in_unbraced_linkage_specification_p
22914 = saved_in_unbraced_linkage_specification_p
;
22920 cp_parser_class_specifier (cp_parser
* parser
)
22923 timevar_push (TV_PARSE_STRUCT
);
22924 ret
= cp_parser_class_specifier_1 (parser
);
22925 timevar_pop (TV_PARSE_STRUCT
);
22929 /* Parse a class-head.
22932 class-key identifier [opt] base-clause [opt]
22933 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22934 class-key nested-name-specifier [opt] template-id
22937 class-virt-specifier:
22941 class-key attributes identifier [opt] base-clause [opt]
22942 class-key attributes nested-name-specifier identifier base-clause [opt]
22943 class-key attributes nested-name-specifier [opt] template-id
22946 Upon return BASES is initialized to the list of base classes (or
22947 NULL, if there are none) in the same form returned by
22948 cp_parser_base_clause.
22950 Returns the TYPE of the indicated class. Sets
22951 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22952 involving a nested-name-specifier was used, and FALSE otherwise.
22954 Returns error_mark_node if this is not a class-head.
22956 Returns NULL_TREE if the class-head is syntactically valid, but
22957 semantically invalid in a way that means we should skip the entire
22958 body of the class. */
22961 cp_parser_class_head (cp_parser
* parser
,
22962 bool* nested_name_specifier_p
)
22964 tree nested_name_specifier
;
22965 enum tag_types class_key
;
22966 tree id
= NULL_TREE
;
22967 tree type
= NULL_TREE
;
22970 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22971 bool template_id_p
= false;
22972 bool qualified_p
= false;
22973 bool invalid_nested_name_p
= false;
22974 bool invalid_explicit_specialization_p
= false;
22975 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22976 tree pushed_scope
= NULL_TREE
;
22977 unsigned num_templates
;
22978 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22979 /* Assume no nested-name-specifier will be present. */
22980 *nested_name_specifier_p
= false;
22981 /* Assume no template parameter lists will be used in defining the
22984 parser
->colon_corrects_to_scope_p
= false;
22986 /* Look for the class-key. */
22987 class_key
= cp_parser_class_key (parser
);
22988 if (class_key
== none_type
)
22989 return error_mark_node
;
22991 location_t class_head_start_location
= input_location
;
22993 /* Parse the attributes. */
22994 attributes
= cp_parser_attributes_opt (parser
);
22996 /* If the next token is `::', that is invalid -- but sometimes
22997 people do try to write:
23001 Handle this gracefully by accepting the extra qualifier, and then
23002 issuing an error about it later if this really is a
23003 class-head. If it turns out just to be an elaborated type
23004 specifier, remain silent. */
23005 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
23006 qualified_p
= true;
23008 push_deferring_access_checks (dk_no_check
);
23010 /* Determine the name of the class. Begin by looking for an
23011 optional nested-name-specifier. */
23012 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
23013 nested_name_specifier
23014 = cp_parser_nested_name_specifier_opt (parser
,
23015 /*typename_keyword_p=*/false,
23016 /*check_dependency_p=*/false,
23018 /*is_declaration=*/false);
23019 /* If there was a nested-name-specifier, then there *must* be an
23022 cp_token
*bad_template_keyword
= NULL
;
23024 if (nested_name_specifier
)
23026 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23027 /* Although the grammar says `identifier', it really means
23028 `class-name' or `template-name'. You are only allowed to
23029 define a class that has already been declared with this
23032 The proposed resolution for Core Issue 180 says that wherever
23033 you see `class T::X' you should treat `X' as a type-name.
23035 It is OK to define an inaccessible class; for example:
23037 class A { class B; };
23040 We do not know if we will see a class-name, or a
23041 template-name. We look for a class-name first, in case the
23042 class-name is a template-id; if we looked for the
23043 template-name first we would stop after the template-name. */
23044 cp_parser_parse_tentatively (parser
);
23045 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23046 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23047 type
= cp_parser_class_name (parser
,
23048 /*typename_keyword_p=*/false,
23049 /*template_keyword_p=*/false,
23051 /*check_dependency_p=*/false,
23052 /*class_head_p=*/true,
23053 /*is_declaration=*/false);
23054 /* If that didn't work, ignore the nested-name-specifier. */
23055 if (!cp_parser_parse_definitely (parser
))
23057 invalid_nested_name_p
= true;
23058 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23059 id
= cp_parser_identifier (parser
);
23060 if (id
== error_mark_node
)
23063 /* If we could not find a corresponding TYPE, treat this
23064 declaration like an unqualified declaration. */
23065 if (type
== error_mark_node
)
23066 nested_name_specifier
= NULL_TREE
;
23067 /* Otherwise, count the number of templates used in TYPE and its
23068 containing scopes. */
23070 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23072 /* Otherwise, the identifier is optional. */
23075 /* We don't know whether what comes next is a template-id,
23076 an identifier, or nothing at all. */
23077 cp_parser_parse_tentatively (parser
);
23078 /* Check for a template-id. */
23079 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23080 id
= cp_parser_template_id (parser
,
23081 /*template_keyword_p=*/false,
23082 /*check_dependency_p=*/true,
23084 /*is_declaration=*/true);
23085 /* If that didn't work, it could still be an identifier. */
23086 if (!cp_parser_parse_definitely (parser
))
23088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23090 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23091 id
= cp_parser_identifier (parser
);
23098 template_id_p
= true;
23103 pop_deferring_access_checks ();
23107 cp_parser_check_for_invalid_template_id (parser
, id
,
23109 type_start_token
->location
);
23111 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23113 /* If it's not a `:' or a `{' then we can't really be looking at a
23114 class-head, since a class-head only appears as part of a
23115 class-specifier. We have to detect this situation before calling
23116 xref_tag, since that has irreversible side-effects. */
23117 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23119 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23120 type
= error_mark_node
;
23124 /* At this point, we're going ahead with the class-specifier, even
23125 if some other problem occurs. */
23126 cp_parser_commit_to_tentative_parse (parser
);
23127 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23129 cp_parser_error (parser
,
23130 "cannot specify %<override%> for a class");
23131 type
= error_mark_node
;
23134 /* Issue the error about the overly-qualified name now. */
23137 cp_parser_error (parser
,
23138 "global qualification of class name is invalid");
23139 type
= error_mark_node
;
23142 else if (invalid_nested_name_p
)
23144 cp_parser_error (parser
,
23145 "qualified name does not name a class");
23146 type
= error_mark_node
;
23149 else if (nested_name_specifier
)
23153 if (bad_template_keyword
)
23154 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23155 keyword template shall not appear at the top level. */
23156 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23157 "keyword %<template%> not allowed in class-head-name");
23159 /* Reject typedef-names in class heads. */
23160 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23162 error_at (type_start_token
->location
,
23163 "invalid class name in declaration of %qD",
23169 /* Figure out in what scope the declaration is being placed. */
23170 scope
= current_scope ();
23171 /* If that scope does not contain the scope in which the
23172 class was originally declared, the program is invalid. */
23173 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23175 if (at_namespace_scope_p ())
23176 error_at (type_start_token
->location
,
23177 "declaration of %qD in namespace %qD which does not "
23179 type
, scope
, nested_name_specifier
);
23181 error_at (type_start_token
->location
,
23182 "declaration of %qD in %qD which does not enclose %qD",
23183 type
, scope
, nested_name_specifier
);
23189 A declarator-id shall not be qualified except for the
23190 definition of a ... nested class outside of its class
23191 ... [or] the definition or explicit instantiation of a
23192 class member of a namespace outside of its namespace. */
23193 if (scope
== nested_name_specifier
)
23195 permerror (nested_name_specifier_token_start
->location
,
23196 "extra qualification not allowed");
23197 nested_name_specifier
= NULL_TREE
;
23201 /* An explicit-specialization must be preceded by "template <>". If
23202 it is not, try to recover gracefully. */
23203 if (at_namespace_scope_p ()
23204 && parser
->num_template_parameter_lists
== 0
23205 && !processing_template_parmlist
23208 /* Build a location of this form:
23209 struct typename <ARGS>
23210 ^~~~~~~~~~~~~~~~~~~~~~
23211 with caret==start at the start token, and
23212 finishing at the end of the type. */
23213 location_t reported_loc
23214 = make_location (class_head_start_location
,
23215 class_head_start_location
,
23216 get_finish (type_start_token
->location
));
23217 rich_location
richloc (line_table
, reported_loc
);
23218 richloc
.add_fixit_insert_before (class_head_start_location
,
23220 error_at (&richloc
,
23221 "an explicit specialization must be preceded by"
23222 " %<template <>%>");
23223 invalid_explicit_specialization_p
= true;
23224 /* Take the same action that would have been taken by
23225 cp_parser_explicit_specialization. */
23226 ++parser
->num_template_parameter_lists
;
23227 begin_specialization ();
23229 /* There must be no "return" statements between this point and the
23230 end of this function; set "type "to the correct return value and
23231 use "goto done;" to return. */
23232 /* Make sure that the right number of template parameters were
23234 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23236 type_start_token
->location
,
23237 /*declarator=*/NULL
))
23239 /* If something went wrong, there is no point in even trying to
23240 process the class-definition. */
23245 /* Look up the type. */
23248 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23249 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23250 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23252 error_at (type_start_token
->location
,
23253 "function template %qD redeclared as a class template", id
);
23254 type
= error_mark_node
;
23258 type
= TREE_TYPE (id
);
23259 type
= maybe_process_partial_specialization (type
);
23261 /* Check the scope while we still know whether or not we had a
23262 nested-name-specifier. */
23263 if (type
!= error_mark_node
)
23264 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23266 if (nested_name_specifier
)
23267 pushed_scope
= push_scope (nested_name_specifier
);
23269 else if (nested_name_specifier
)
23275 template <typename T> struct S { struct T };
23276 template <typename T> struct S<T>::T { };
23278 we will get a TYPENAME_TYPE when processing the definition of
23279 `S::T'. We need to resolve it to the actual type before we
23280 try to define it. */
23281 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23283 class_type
= resolve_typename_type (TREE_TYPE (type
),
23284 /*only_current_p=*/false);
23285 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23286 type
= TYPE_NAME (class_type
);
23289 cp_parser_error (parser
, "could not resolve typename type");
23290 type
= error_mark_node
;
23294 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23295 == error_mark_node
)
23301 class_type
= current_class_type
;
23302 /* Enter the scope indicated by the nested-name-specifier. */
23303 pushed_scope
= push_scope (nested_name_specifier
);
23304 /* Get the canonical version of this type. */
23305 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23306 /* Call push_template_decl if it seems like we should be defining a
23307 template either from the template headers or the type we're
23308 defining, so that we diagnose both extra and missing headers. */
23309 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23310 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23311 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23313 type
= push_template_decl (type
);
23314 if (type
== error_mark_node
)
23321 type
= TREE_TYPE (type
);
23322 *nested_name_specifier_p
= true;
23324 else /* The name is not a nested name. */
23326 /* If the class was unnamed, create a dummy name. */
23328 id
= make_anon_name ();
23329 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23330 ? ts_within_enclosing_non_class
23332 type
= xref_tag (class_key
, id
, tag_scope
,
23333 parser
->num_template_parameter_lists
);
23336 /* Indicate whether this class was declared as a `class' or as a
23338 if (TREE_CODE (type
) == RECORD_TYPE
)
23339 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23340 cp_parser_check_class_key (class_key
, type
);
23342 /* If this type was already complete, and we see another definition,
23343 that's an error. */
23344 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23346 error_at (type_start_token
->location
, "redefinition of %q#T",
23348 inform (location_of (type
), "previous definition of %q#T",
23353 else if (type
== error_mark_node
)
23358 /* Apply attributes now, before any use of the class as a template
23359 argument in its base list. */
23360 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23361 fixup_attribute_variants (type
);
23364 /* We will have entered the scope containing the class; the names of
23365 base classes should be looked up in that context. For example:
23367 struct A { struct B {}; struct C; };
23368 struct A::C : B {};
23372 /* Get the list of base-classes, if there is one. */
23373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23375 /* PR59482: enter the class scope so that base-specifiers are looked
23379 bases
= cp_parser_base_clause (parser
);
23380 /* PR59482: get out of the previously pushed class scope so that the
23381 subsequent pops pop the right thing. */
23388 /* If we're really defining a class, process the base classes.
23389 If they're invalid, fail. */
23390 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23391 xref_basetypes (type
, bases
);
23394 /* Leave the scope given by the nested-name-specifier. We will
23395 enter the class scope itself while processing the members. */
23397 pop_scope (pushed_scope
);
23399 if (invalid_explicit_specialization_p
)
23401 end_specialization ();
23402 --parser
->num_template_parameter_lists
;
23406 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23407 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23408 CLASSTYPE_FINAL (type
) = 1;
23410 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23414 /* Parse a class-key.
23421 Returns the kind of class-key specified, or none_type to indicate
23424 static enum tag_types
23425 cp_parser_class_key (cp_parser
* parser
)
23428 enum tag_types tag_type
;
23430 /* Look for the class-key. */
23431 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23435 /* Check to see if the TOKEN is a class-key. */
23436 tag_type
= cp_parser_token_is_class_key (token
);
23438 cp_parser_error (parser
, "expected class-key");
23442 /* Parse a type-parameter-key.
23444 type-parameter-key:
23450 cp_parser_type_parameter_key (cp_parser
* parser
)
23452 /* Look for the type-parameter-key. */
23453 enum tag_types tag_type
= none_type
;
23454 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23455 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23457 cp_lexer_consume_token (parser
->lexer
);
23458 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23459 /* typename is not allowed in a template template parameter
23460 by the standard until C++17. */
23461 pedwarn (token
->location
, OPT_Wpedantic
,
23462 "ISO C++ forbids typename key in template template parameter;"
23463 " use -std=c++17 or -std=gnu++17");
23466 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23471 /* Parse an (optional) member-specification.
23473 member-specification:
23474 member-declaration member-specification [opt]
23475 access-specifier : member-specification [opt] */
23478 cp_parser_member_specification_opt (cp_parser
* parser
)
23485 /* Peek at the next token. */
23486 token
= cp_lexer_peek_token (parser
->lexer
);
23487 /* If it's a `}', or EOF then we've seen all the members. */
23488 if (token
->type
== CPP_CLOSE_BRACE
23489 || token
->type
== CPP_EOF
23490 || token
->type
== CPP_PRAGMA_EOL
)
23493 /* See if this token is a keyword. */
23494 keyword
= token
->keyword
;
23498 case RID_PROTECTED
:
23500 /* Consume the access-specifier. */
23501 cp_lexer_consume_token (parser
->lexer
);
23502 /* Remember which access-specifier is active. */
23503 current_access_specifier
= token
->u
.value
;
23504 /* Look for the `:'. */
23505 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23509 /* Accept #pragmas at class scope. */
23510 if (token
->type
== CPP_PRAGMA
)
23512 cp_parser_pragma (parser
, pragma_member
, NULL
);
23516 /* Otherwise, the next construction must be a
23517 member-declaration. */
23518 cp_parser_member_declaration (parser
);
23523 /* Parse a member-declaration.
23525 member-declaration:
23526 decl-specifier-seq [opt] member-declarator-list [opt] ;
23527 function-definition ; [opt]
23528 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23530 template-declaration
23533 member-declarator-list:
23535 member-declarator-list , member-declarator
23538 declarator pure-specifier [opt]
23539 declarator constant-initializer [opt]
23540 identifier [opt] : constant-expression
23544 member-declaration:
23545 __extension__ member-declaration
23548 declarator attributes [opt] pure-specifier [opt]
23549 declarator attributes [opt] constant-initializer [opt]
23550 identifier [opt] attributes [opt] : constant-expression
23554 member-declaration:
23555 static_assert-declaration */
23558 cp_parser_member_declaration (cp_parser
* parser
)
23560 cp_decl_specifier_seq decl_specifiers
;
23561 tree prefix_attributes
;
23563 int declares_class_or_enum
;
23565 cp_token
*token
= NULL
;
23566 cp_token
*decl_spec_token_start
= NULL
;
23567 cp_token
*initializer_token_start
= NULL
;
23568 int saved_pedantic
;
23569 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23571 /* Check for the `__extension__' keyword. */
23572 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23575 cp_parser_member_declaration (parser
);
23576 /* Restore the old value of the PEDANTIC flag. */
23577 pedantic
= saved_pedantic
;
23582 /* Check for a template-declaration. */
23583 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23585 /* An explicit specialization here is an error condition, and we
23586 expect the specialization handler to detect and report this. */
23587 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23588 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23589 cp_parser_explicit_specialization (parser
);
23591 cp_parser_template_declaration (parser
, /*member_p=*/true);
23595 /* Check for a template introduction. */
23596 else if (cp_parser_template_declaration_after_export (parser
, true))
23599 /* Check for a using-declaration. */
23600 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23602 if (cxx_dialect
< cxx11
)
23604 /* Parse the using-declaration. */
23605 cp_parser_using_declaration (parser
,
23606 /*access_declaration_p=*/false);
23612 bool alias_decl_expected
;
23613 cp_parser_parse_tentatively (parser
);
23614 decl
= cp_parser_alias_declaration (parser
);
23615 /* Note that if we actually see the '=' token after the
23616 identifier, cp_parser_alias_declaration commits the
23617 tentative parse. In that case, we really expect an
23618 alias-declaration. Otherwise, we expect a using
23620 alias_decl_expected
=
23621 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23622 cp_parser_parse_definitely (parser
);
23624 if (alias_decl_expected
)
23625 finish_member_declaration (decl
);
23627 cp_parser_using_declaration (parser
,
23628 /*access_declaration_p=*/false);
23633 /* Check for @defs. */
23634 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23637 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23638 ivar
= ivar_chains
;
23642 ivar
= TREE_CHAIN (member
);
23643 TREE_CHAIN (member
) = NULL_TREE
;
23644 finish_member_declaration (member
);
23649 /* If the next token is `static_assert' we have a static assertion. */
23650 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23652 cp_parser_static_assert (parser
, /*member_p=*/true);
23656 parser
->colon_corrects_to_scope_p
= false;
23658 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23661 /* Parse the decl-specifier-seq. */
23662 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23663 cp_parser_decl_specifier_seq (parser
,
23664 CP_PARSER_FLAGS_OPTIONAL
,
23666 &declares_class_or_enum
);
23667 /* Check for an invalid type-name. */
23668 if (!decl_specifiers
.any_type_specifiers_p
23669 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23671 /* If there is no declarator, then the decl-specifier-seq should
23673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23675 /* If there was no decl-specifier-seq, and the next token is a
23676 `;', then we have something like:
23682 Each member-declaration shall declare at least one member
23683 name of the class. */
23684 if (!decl_specifiers
.any_specifiers_p
)
23686 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23687 if (!in_system_header_at (token
->location
))
23689 gcc_rich_location
richloc (token
->location
);
23690 richloc
.add_fixit_remove ();
23691 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23698 /* See if this declaration is a friend. */
23699 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23700 /* If there were decl-specifiers, check to see if there was
23701 a class-declaration. */
23702 type
= check_tag_decl (&decl_specifiers
,
23703 /*explicit_type_instantiation_p=*/false);
23704 /* Nested classes have already been added to the class, but
23705 a `friend' needs to be explicitly registered. */
23708 /* If the `friend' keyword was present, the friend must
23709 be introduced with a class-key. */
23710 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23711 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23712 "in C++03 a class-key must be used "
23713 "when declaring a friend");
23716 template <typename T> struct A {
23717 friend struct A<T>::B;
23720 A<T>::B will be represented by a TYPENAME_TYPE, and
23721 therefore not recognized by check_tag_decl. */
23724 type
= decl_specifiers
.type
;
23725 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23726 type
= TREE_TYPE (type
);
23728 if (!type
|| !TYPE_P (type
))
23729 error_at (decl_spec_token_start
->location
,
23730 "friend declaration does not name a class or "
23733 make_friend_class (current_class_type
, type
,
23734 /*complain=*/true);
23736 /* If there is no TYPE, an error message will already have
23738 else if (!type
|| type
== error_mark_node
)
23740 /* An anonymous aggregate has to be handled specially; such
23741 a declaration really declares a data member (with a
23742 particular type), as opposed to a nested class. */
23743 else if (ANON_AGGR_TYPE_P (type
))
23746 if (decl_specifiers
.storage_class
!= sc_none
)
23747 error_at (decl_spec_token_start
->location
,
23748 "a storage class on an anonymous aggregate "
23749 "in class scope is not allowed");
23751 /* Remove constructors and such from TYPE, now that we
23752 know it is an anonymous aggregate. */
23753 fixup_anonymous_aggr (type
);
23754 /* And make the corresponding data member. */
23755 decl
= build_decl (decl_spec_token_start
->location
,
23756 FIELD_DECL
, NULL_TREE
, type
);
23757 /* Add it to the class. */
23758 finish_member_declaration (decl
);
23761 cp_parser_check_access_in_redeclaration
23763 decl_spec_token_start
->location
);
23768 bool assume_semicolon
= false;
23770 /* Clear attributes from the decl_specifiers but keep them
23771 around as prefix attributes that apply them to the entity
23773 prefix_attributes
= decl_specifiers
.attributes
;
23774 decl_specifiers
.attributes
= NULL_TREE
;
23776 /* See if these declarations will be friends. */
23777 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23779 /* Keep going until we hit the `;' at the end of the
23781 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23783 tree attributes
= NULL_TREE
;
23784 tree first_attribute
;
23786 bool named_bitfld
= false;
23788 /* Peek at the next token. */
23789 token
= cp_lexer_peek_token (parser
->lexer
);
23791 /* The following code wants to know early if it is a bit-field
23792 or some other declaration. Attributes can appear before
23793 the `:' token. Skip over them without consuming any tokens
23794 to peek if they are followed by `:'. */
23795 if (cp_next_tokens_can_be_attribute_p (parser
)
23796 || (token
->type
== CPP_NAME
23797 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23798 && (named_bitfld
= true)))
23801 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23802 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23805 /* Check for a bitfield declaration. */
23806 if (token
->type
== CPP_COLON
23807 || (token
->type
== CPP_NAME
23808 && token
== cp_lexer_peek_token (parser
->lexer
)
23809 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23810 && (named_bitfld
= true)))
23814 tree late_attributes
= NULL_TREE
;
23817 identifier
= cp_parser_identifier (parser
);
23819 identifier
= NULL_TREE
;
23821 /* Look for attributes that apply to the bitfield. */
23822 attributes
= cp_parser_attributes_opt (parser
);
23824 /* Consume the `:' token. */
23825 cp_lexer_consume_token (parser
->lexer
);
23827 /* Get the width of the bitfield. */
23828 width
= cp_parser_constant_expression (parser
, false, NULL
,
23829 cxx_dialect
>= cxx11
);
23831 /* In C++2A and as extension for C++11 and above we allow
23832 default member initializers for bit-fields. */
23833 initializer
= NULL_TREE
;
23834 if (cxx_dialect
>= cxx11
23835 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23836 || cp_lexer_next_token_is (parser
->lexer
,
23840 = cp_lexer_peek_token (parser
->lexer
)->location
;
23841 if (cxx_dialect
< cxx2a
23842 && !in_system_header_at (loc
)
23843 && identifier
!= NULL_TREE
)
23845 "default member initializers for bit-fields "
23846 "only available with -std=c++2a or "
23849 initializer
= cp_parser_save_nsdmi (parser
);
23850 if (identifier
== NULL_TREE
)
23852 error_at (loc
, "default member initializer for "
23853 "unnamed bit-field");
23854 initializer
= NULL_TREE
;
23859 /* Look for attributes that apply to the bitfield after
23860 the `:' token and width. This is where GCC used to
23861 parse attributes in the past, pedwarn if there is
23862 a std attribute. */
23863 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23864 pedwarn (input_location
, OPT_Wpedantic
,
23865 "ISO C++ allows bit-field attributes only "
23866 "before the %<:%> token");
23868 late_attributes
= cp_parser_attributes_opt (parser
);
23871 attributes
= attr_chainon (attributes
, late_attributes
);
23873 /* Remember which attributes are prefix attributes and
23875 first_attribute
= attributes
;
23876 /* Combine the attributes. */
23877 attributes
= attr_chainon (prefix_attributes
, attributes
);
23879 /* Create the bitfield declaration. */
23880 decl
= grokbitfield (identifier
23881 ? make_id_declarator (NULL_TREE
,
23886 width
, initializer
,
23891 cp_declarator
*declarator
;
23892 tree asm_specification
;
23893 int ctor_dtor_or_conv_p
;
23895 /* Parse the declarator. */
23897 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23898 &ctor_dtor_or_conv_p
,
23899 /*parenthesized_p=*/NULL
,
23903 /* If something went wrong parsing the declarator, make sure
23904 that we at least consume some tokens. */
23905 if (declarator
== cp_error_declarator
)
23907 /* Skip to the end of the statement. */
23908 cp_parser_skip_to_end_of_statement (parser
);
23909 /* If the next token is not a semicolon, that is
23910 probably because we just skipped over the body of
23911 a function. So, we consume a semicolon if
23912 present, but do not issue an error message if it
23914 if (cp_lexer_next_token_is (parser
->lexer
,
23916 cp_lexer_consume_token (parser
->lexer
);
23920 if (declares_class_or_enum
& 2)
23921 cp_parser_check_for_definition_in_return_type
23922 (declarator
, decl_specifiers
.type
,
23923 decl_specifiers
.locations
[ds_type_spec
]);
23925 /* Look for an asm-specification. */
23926 asm_specification
= cp_parser_asm_specification_opt (parser
);
23927 /* Look for attributes that apply to the declaration. */
23928 attributes
= cp_parser_attributes_opt (parser
);
23929 /* Remember which attributes are prefix attributes and
23931 first_attribute
= attributes
;
23932 /* Combine the attributes. */
23933 attributes
= attr_chainon (prefix_attributes
, attributes
);
23935 /* If it's an `=', then we have a constant-initializer or a
23936 pure-specifier. It is not correct to parse the
23937 initializer before registering the member declaration
23938 since the member declaration should be in scope while
23939 its initializer is processed. However, the rest of the
23940 front end does not yet provide an interface that allows
23941 us to handle this correctly. */
23942 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23946 A pure-specifier shall be used only in the declaration of
23947 a virtual function.
23949 A member-declarator can contain a constant-initializer
23950 only if it declares a static member of integral or
23953 Therefore, if the DECLARATOR is for a function, we look
23954 for a pure-specifier; otherwise, we look for a
23955 constant-initializer. When we call `grokfield', it will
23956 perform more stringent semantics checks. */
23957 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23958 if (function_declarator_p (declarator
)
23959 || (decl_specifiers
.type
23960 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23961 && declarator
->kind
== cdk_id
23962 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23963 == FUNCTION_TYPE
)))
23964 initializer
= cp_parser_pure_specifier (parser
);
23965 else if (decl_specifiers
.storage_class
!= sc_static
)
23966 initializer
= cp_parser_save_nsdmi (parser
);
23967 else if (cxx_dialect
>= cxx11
)
23970 /* Don't require a constant rvalue in C++11, since we
23971 might want a reference constant. We'll enforce
23972 constancy later. */
23973 cp_lexer_consume_token (parser
->lexer
);
23974 /* Parse the initializer. */
23975 initializer
= cp_parser_initializer_clause (parser
,
23979 /* Parse the initializer. */
23980 initializer
= cp_parser_constant_initializer (parser
);
23982 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23983 && !function_declarator_p (declarator
))
23986 if (decl_specifiers
.storage_class
!= sc_static
)
23987 initializer
= cp_parser_save_nsdmi (parser
);
23989 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23991 /* Otherwise, there is no initializer. */
23993 initializer
= NULL_TREE
;
23995 /* See if we are probably looking at a function
23996 definition. We are certainly not looking at a
23997 member-declarator. Calling `grokfield' has
23998 side-effects, so we must not do it unless we are sure
23999 that we are looking at a member-declarator. */
24000 if (cp_parser_token_starts_function_definition_p
24001 (cp_lexer_peek_token (parser
->lexer
)))
24003 /* The grammar does not allow a pure-specifier to be
24004 used when a member function is defined. (It is
24005 possible that this fact is an oversight in the
24006 standard, since a pure function may be defined
24007 outside of the class-specifier. */
24008 if (initializer
&& initializer_token_start
)
24009 error_at (initializer_token_start
->location
,
24010 "pure-specifier on function-definition");
24011 decl
= cp_parser_save_member_function_body (parser
,
24015 if (parser
->fully_implicit_function_template_p
)
24016 decl
= finish_fully_implicit_template (parser
, decl
);
24017 /* If the member was not a friend, declare it here. */
24019 finish_member_declaration (decl
);
24020 /* Peek at the next token. */
24021 token
= cp_lexer_peek_token (parser
->lexer
);
24022 /* If the next token is a semicolon, consume it. */
24023 if (token
->type
== CPP_SEMICOLON
)
24025 location_t semicolon_loc
24026 = cp_lexer_consume_token (parser
->lexer
)->location
;
24027 gcc_rich_location
richloc (semicolon_loc
);
24028 richloc
.add_fixit_remove ();
24029 warning_at (&richloc
, OPT_Wextra_semi
,
24030 "extra %<;%> after in-class "
24031 "function definition");
24036 if (declarator
->kind
== cdk_function
)
24037 declarator
->id_loc
= token
->location
;
24038 /* Create the declaration. */
24039 decl
= grokfield (declarator
, &decl_specifiers
,
24040 initializer
, /*init_const_expr_p=*/true,
24041 asm_specification
, attributes
);
24042 if (parser
->fully_implicit_function_template_p
)
24045 finish_fully_implicit_template (parser
, 0);
24047 decl
= finish_fully_implicit_template (parser
, decl
);
24051 cp_finalize_omp_declare_simd (parser
, decl
);
24052 cp_finalize_oacc_routine (parser
, decl
, false);
24054 /* Reset PREFIX_ATTRIBUTES. */
24055 if (attributes
!= error_mark_node
)
24057 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24058 attributes
= TREE_CHAIN (attributes
);
24060 TREE_CHAIN (attributes
) = NULL_TREE
;
24063 /* If there is any qualification still in effect, clear it
24064 now; we will be starting fresh with the next declarator. */
24065 parser
->scope
= NULL_TREE
;
24066 parser
->qualifying_scope
= NULL_TREE
;
24067 parser
->object_scope
= NULL_TREE
;
24068 /* If it's a `,', then there are more declarators. */
24069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24071 cp_lexer_consume_token (parser
->lexer
);
24072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24074 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24075 gcc_rich_location
richloc (token
->location
);
24076 richloc
.add_fixit_remove ();
24077 error_at (&richloc
, "stray %<,%> at end of "
24078 "member declaration");
24081 /* If the next token isn't a `;', then we have a parse error. */
24082 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24085 /* The next token might be a ways away from where the
24086 actual semicolon is missing. Find the previous token
24087 and use that for our error position. */
24088 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24089 gcc_rich_location
richloc (token
->location
);
24090 richloc
.add_fixit_insert_after (";");
24091 error_at (&richloc
, "expected %<;%> at end of "
24092 "member declaration");
24094 /* Assume that the user meant to provide a semicolon. If
24095 we were to cp_parser_skip_to_end_of_statement, we might
24096 skip to a semicolon inside a member function definition
24097 and issue nonsensical error messages. */
24098 assume_semicolon
= true;
24103 /* Add DECL to the list of members. */
24105 /* Explicitly include, eg, NSDMIs, for better error
24106 recovery (c++/58650). */
24107 || !DECL_DECLARES_FUNCTION_P (decl
))
24108 finish_member_declaration (decl
);
24110 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24111 cp_parser_save_default_args (parser
, decl
);
24112 else if (TREE_CODE (decl
) == FIELD_DECL
24113 && DECL_INITIAL (decl
))
24114 /* Add DECL to the queue of NSDMI to be parsed later. */
24115 vec_safe_push (unparsed_nsdmis
, decl
);
24118 if (assume_semicolon
)
24123 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24125 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24128 /* Parse a pure-specifier.
24133 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24134 Otherwise, ERROR_MARK_NODE is returned. */
24137 cp_parser_pure_specifier (cp_parser
* parser
)
24141 /* Look for the `=' token. */
24142 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24143 return error_mark_node
;
24144 /* Look for the `0' token. */
24145 token
= cp_lexer_peek_token (parser
->lexer
);
24147 if (token
->type
== CPP_EOF
24148 || token
->type
== CPP_PRAGMA_EOL
)
24149 return error_mark_node
;
24151 cp_lexer_consume_token (parser
->lexer
);
24153 /* Accept = default or = delete in c++0x mode. */
24154 if (token
->keyword
== RID_DEFAULT
24155 || token
->keyword
== RID_DELETE
)
24157 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24158 return token
->u
.value
;
24161 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24162 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24164 cp_parser_error (parser
,
24165 "invalid pure specifier (only %<= 0%> is allowed)");
24166 cp_parser_skip_to_end_of_statement (parser
);
24167 return error_mark_node
;
24169 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24171 error_at (token
->location
, "templates may not be %<virtual%>");
24172 return error_mark_node
;
24175 return integer_zero_node
;
24178 /* Parse a constant-initializer.
24180 constant-initializer:
24181 = constant-expression
24183 Returns a representation of the constant-expression. */
24186 cp_parser_constant_initializer (cp_parser
* parser
)
24188 /* Look for the `=' token. */
24189 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24190 return error_mark_node
;
24192 /* It is invalid to write:
24194 struct S { static const int i = { 7 }; };
24197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24199 cp_parser_error (parser
,
24200 "a brace-enclosed initializer is not allowed here");
24201 /* Consume the opening brace. */
24202 matching_braces braces
;
24203 braces
.consume_open (parser
);
24204 /* Skip the initializer. */
24205 cp_parser_skip_to_closing_brace (parser
);
24206 /* Look for the trailing `}'. */
24207 braces
.require_close (parser
);
24209 return error_mark_node
;
24212 return cp_parser_constant_expression (parser
);
24215 /* Derived classes [gram.class.derived] */
24217 /* Parse a base-clause.
24220 : base-specifier-list
24222 base-specifier-list:
24223 base-specifier ... [opt]
24224 base-specifier-list , base-specifier ... [opt]
24226 Returns a TREE_LIST representing the base-classes, in the order in
24227 which they were declared. The representation of each node is as
24228 described by cp_parser_base_specifier.
24230 In the case that no bases are specified, this function will return
24231 NULL_TREE, not ERROR_MARK_NODE. */
24234 cp_parser_base_clause (cp_parser
* parser
)
24236 tree bases
= NULL_TREE
;
24238 /* Look for the `:' that begins the list. */
24239 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24241 /* Scan the base-specifier-list. */
24246 bool pack_expansion_p
= false;
24248 /* Look for the base-specifier. */
24249 base
= cp_parser_base_specifier (parser
);
24250 /* Look for the (optional) ellipsis. */
24251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24253 /* Consume the `...'. */
24254 cp_lexer_consume_token (parser
->lexer
);
24256 pack_expansion_p
= true;
24259 /* Add BASE to the front of the list. */
24260 if (base
&& base
!= error_mark_node
)
24262 if (pack_expansion_p
)
24263 /* Make this a pack expansion type. */
24264 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24266 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24268 TREE_CHAIN (base
) = bases
;
24272 /* Peek at the next token. */
24273 token
= cp_lexer_peek_token (parser
->lexer
);
24274 /* If it's not a comma, then the list is complete. */
24275 if (token
->type
!= CPP_COMMA
)
24277 /* Consume the `,'. */
24278 cp_lexer_consume_token (parser
->lexer
);
24281 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24282 base class had a qualified name. However, the next name that
24283 appears is certainly not qualified. */
24284 parser
->scope
= NULL_TREE
;
24285 parser
->qualifying_scope
= NULL_TREE
;
24286 parser
->object_scope
= NULL_TREE
;
24288 return nreverse (bases
);
24291 /* Parse a base-specifier.
24294 :: [opt] nested-name-specifier [opt] class-name
24295 virtual access-specifier [opt] :: [opt] nested-name-specifier
24297 access-specifier virtual [opt] :: [opt] nested-name-specifier
24300 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24301 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24302 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24303 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24306 cp_parser_base_specifier (cp_parser
* parser
)
24310 bool virtual_p
= false;
24311 bool duplicate_virtual_error_issued_p
= false;
24312 bool duplicate_access_error_issued_p
= false;
24313 bool class_scope_p
, template_p
;
24314 tree access
= access_default_node
;
24317 /* Process the optional `virtual' and `access-specifier'. */
24320 /* Peek at the next token. */
24321 token
= cp_lexer_peek_token (parser
->lexer
);
24322 /* Process `virtual'. */
24323 switch (token
->keyword
)
24326 /* If `virtual' appears more than once, issue an error. */
24327 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24329 cp_parser_error (parser
,
24330 "%<virtual%> specified more than once in base-specifier");
24331 duplicate_virtual_error_issued_p
= true;
24336 /* Consume the `virtual' token. */
24337 cp_lexer_consume_token (parser
->lexer
);
24342 case RID_PROTECTED
:
24344 /* If more than one access specifier appears, issue an
24346 if (access
!= access_default_node
24347 && !duplicate_access_error_issued_p
)
24349 cp_parser_error (parser
,
24350 "more than one access specifier in base-specifier");
24351 duplicate_access_error_issued_p
= true;
24354 access
= ridpointers
[(int) token
->keyword
];
24356 /* Consume the access-specifier. */
24357 cp_lexer_consume_token (parser
->lexer
);
24366 /* It is not uncommon to see programs mechanically, erroneously, use
24367 the 'typename' keyword to denote (dependent) qualified types
24368 as base classes. */
24369 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24371 token
= cp_lexer_peek_token (parser
->lexer
);
24372 if (!processing_template_decl
)
24373 error_at (token
->location
,
24374 "keyword %<typename%> not allowed outside of templates");
24376 error_at (token
->location
,
24377 "keyword %<typename%> not allowed in this context "
24378 "(the base class is implicitly a type)");
24379 cp_lexer_consume_token (parser
->lexer
);
24382 /* Look for the optional `::' operator. */
24383 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24384 /* Look for the nested-name-specifier. The simplest way to
24389 The keyword `typename' is not permitted in a base-specifier or
24390 mem-initializer; in these contexts a qualified name that
24391 depends on a template-parameter is implicitly assumed to be a
24394 is to pretend that we have seen the `typename' keyword at this
24396 cp_parser_nested_name_specifier_opt (parser
,
24397 /*typename_keyword_p=*/true,
24398 /*check_dependency_p=*/true,
24400 /*is_declaration=*/true);
24401 /* If the base class is given by a qualified name, assume that names
24402 we see are type names or templates, as appropriate. */
24403 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24404 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24407 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24408 /* DR 950 allows decltype as a base-specifier. */
24409 type
= cp_parser_decltype (parser
);
24412 /* Otherwise, look for the class-name. */
24413 type
= cp_parser_class_name (parser
,
24417 /*check_dependency_p=*/true,
24418 /*class_head_p=*/false,
24419 /*is_declaration=*/true);
24420 type
= TREE_TYPE (type
);
24423 if (type
== error_mark_node
)
24424 return error_mark_node
;
24426 return finish_base_specifier (type
, access
, virtual_p
);
24429 /* Exception handling [gram.exception] */
24431 /* Parse an (optional) noexcept-specification.
24433 noexcept-specification:
24434 noexcept ( constant-expression ) [opt]
24436 If no noexcept-specification is present, returns NULL_TREE.
24437 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24438 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24439 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24440 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24441 in which case a boolean condition is returned instead. */
24444 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24445 bool require_constexpr
,
24446 bool* consumed_expr
,
24450 const char *saved_message
;
24452 /* Peek at the next token. */
24453 token
= cp_lexer_peek_token (parser
->lexer
);
24455 /* Is it a noexcept-specification? */
24456 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24459 cp_lexer_consume_token (parser
->lexer
);
24461 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24463 matching_parens parens
;
24464 parens
.consume_open (parser
);
24466 if (require_constexpr
)
24468 /* Types may not be defined in an exception-specification. */
24469 saved_message
= parser
->type_definition_forbidden_message
;
24470 parser
->type_definition_forbidden_message
24471 = G_("types may not be defined in an exception-specification");
24473 expr
= cp_parser_constant_expression (parser
);
24475 /* Restore the saved message. */
24476 parser
->type_definition_forbidden_message
= saved_message
;
24480 expr
= cp_parser_expression (parser
);
24481 *consumed_expr
= true;
24484 parens
.require_close (parser
);
24488 expr
= boolean_true_node
;
24489 if (!require_constexpr
)
24490 *consumed_expr
= false;
24493 /* We cannot build a noexcept-spec right away because this will check
24494 that expr is a constexpr. */
24496 return build_noexcept_spec (expr
, tf_warning_or_error
);
24504 /* Parse an (optional) exception-specification.
24506 exception-specification:
24507 throw ( type-id-list [opt] )
24509 Returns a TREE_LIST representing the exception-specification. The
24510 TREE_VALUE of each node is a type. */
24513 cp_parser_exception_specification_opt (cp_parser
* parser
)
24517 const char *saved_message
;
24519 /* Peek at the next token. */
24520 token
= cp_lexer_peek_token (parser
->lexer
);
24522 /* Is it a noexcept-specification? */
24523 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24525 if (type_id_list
!= NULL_TREE
)
24526 return type_id_list
;
24528 /* If it's not `throw', then there's no exception-specification. */
24529 if (!cp_parser_is_keyword (token
, RID_THROW
))
24532 location_t loc
= token
->location
;
24534 /* Consume the `throw'. */
24535 cp_lexer_consume_token (parser
->lexer
);
24537 /* Look for the `('. */
24538 matching_parens parens
;
24539 parens
.require_open (parser
);
24541 /* Peek at the next token. */
24542 token
= cp_lexer_peek_token (parser
->lexer
);
24543 /* If it's not a `)', then there is a type-id-list. */
24544 if (token
->type
!= CPP_CLOSE_PAREN
)
24546 /* Types may not be defined in an exception-specification. */
24547 saved_message
= parser
->type_definition_forbidden_message
;
24548 parser
->type_definition_forbidden_message
24549 = G_("types may not be defined in an exception-specification");
24550 /* Parse the type-id-list. */
24551 type_id_list
= cp_parser_type_id_list (parser
);
24552 /* Restore the saved message. */
24553 parser
->type_definition_forbidden_message
= saved_message
;
24555 if (cxx_dialect
>= cxx17
)
24557 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24559 type_id_list
= NULL_TREE
;
24561 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24562 warning_at (loc
, OPT_Wdeprecated
,
24563 "dynamic exception specifications are deprecated in "
24566 /* In C++17, throw() is equivalent to noexcept (true). throw()
24567 is deprecated in C++11 and above as well, but is still widely used,
24568 so don't warn about it yet. */
24569 else if (cxx_dialect
>= cxx17
)
24570 type_id_list
= noexcept_true_spec
;
24572 type_id_list
= empty_except_spec
;
24574 /* Look for the `)'. */
24575 parens
.require_close (parser
);
24577 return type_id_list
;
24580 /* Parse an (optional) type-id-list.
24584 type-id-list , type-id ... [opt]
24586 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24587 in the order that the types were presented. */
24590 cp_parser_type_id_list (cp_parser
* parser
)
24592 tree types
= NULL_TREE
;
24599 token
= cp_lexer_peek_token (parser
->lexer
);
24601 /* Get the next type-id. */
24602 type
= cp_parser_type_id (parser
);
24603 /* Check for invalid 'auto'. */
24604 if (flag_concepts
&& type_uses_auto (type
))
24606 error_at (token
->location
,
24607 "invalid use of %<auto%> in exception-specification");
24608 type
= error_mark_node
;
24610 /* Parse the optional ellipsis. */
24611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24613 /* Consume the `...'. */
24614 cp_lexer_consume_token (parser
->lexer
);
24616 /* Turn the type into a pack expansion expression. */
24617 type
= make_pack_expansion (type
);
24619 /* Add it to the list. */
24620 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24621 /* Peek at the next token. */
24622 token
= cp_lexer_peek_token (parser
->lexer
);
24623 /* If it is not a `,', we are done. */
24624 if (token
->type
!= CPP_COMMA
)
24626 /* Consume the `,'. */
24627 cp_lexer_consume_token (parser
->lexer
);
24630 return nreverse (types
);
24633 /* Parse a try-block.
24636 try compound-statement handler-seq */
24639 cp_parser_try_block (cp_parser
* parser
)
24643 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24644 if (parser
->in_function_body
24645 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24646 error ("%<try%> in %<constexpr%> function");
24648 try_block
= begin_try_block ();
24649 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24650 finish_try_block (try_block
);
24651 cp_parser_handler_seq (parser
);
24652 finish_handler_sequence (try_block
);
24657 /* Parse a function-try-block.
24659 function-try-block:
24660 try ctor-initializer [opt] function-body handler-seq */
24663 cp_parser_function_try_block (cp_parser
* parser
)
24665 tree compound_stmt
;
24668 /* Look for the `try' keyword. */
24669 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24671 /* Let the rest of the front end know where we are. */
24672 try_block
= begin_function_try_block (&compound_stmt
);
24673 /* Parse the function-body. */
24674 cp_parser_ctor_initializer_opt_and_function_body
24675 (parser
, /*in_function_try_block=*/true);
24676 /* We're done with the `try' part. */
24677 finish_function_try_block (try_block
);
24678 /* Parse the handlers. */
24679 cp_parser_handler_seq (parser
);
24680 /* We're done with the handlers. */
24681 finish_function_handler_sequence (try_block
, compound_stmt
);
24684 /* Parse a handler-seq.
24687 handler handler-seq [opt] */
24690 cp_parser_handler_seq (cp_parser
* parser
)
24696 /* Parse the handler. */
24697 cp_parser_handler (parser
);
24698 /* Peek at the next token. */
24699 token
= cp_lexer_peek_token (parser
->lexer
);
24700 /* If it's not `catch' then there are no more handlers. */
24701 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24706 /* Parse a handler.
24709 catch ( exception-declaration ) compound-statement */
24712 cp_parser_handler (cp_parser
* parser
)
24717 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24718 handler
= begin_handler ();
24719 matching_parens parens
;
24720 parens
.require_open (parser
);
24721 declaration
= cp_parser_exception_declaration (parser
);
24722 finish_handler_parms (declaration
, handler
);
24723 parens
.require_close (parser
);
24724 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24725 finish_handler (handler
);
24728 /* Parse an exception-declaration.
24730 exception-declaration:
24731 type-specifier-seq declarator
24732 type-specifier-seq abstract-declarator
24736 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24737 ellipsis variant is used. */
24740 cp_parser_exception_declaration (cp_parser
* parser
)
24742 cp_decl_specifier_seq type_specifiers
;
24743 cp_declarator
*declarator
;
24744 const char *saved_message
;
24746 /* If it's an ellipsis, it's easy to handle. */
24747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24749 /* Consume the `...' token. */
24750 cp_lexer_consume_token (parser
->lexer
);
24754 /* Types may not be defined in exception-declarations. */
24755 saved_message
= parser
->type_definition_forbidden_message
;
24756 parser
->type_definition_forbidden_message
24757 = G_("types may not be defined in exception-declarations");
24759 /* Parse the type-specifier-seq. */
24760 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24761 /*is_trailing_return=*/false,
24763 /* If it's a `)', then there is no declarator. */
24764 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24767 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24768 /*ctor_dtor_or_conv_p=*/NULL
,
24769 /*parenthesized_p=*/NULL
,
24770 /*member_p=*/false,
24771 /*friend_p=*/false);
24773 /* Restore the saved message. */
24774 parser
->type_definition_forbidden_message
= saved_message
;
24776 if (!type_specifiers
.any_specifiers_p
)
24777 return error_mark_node
;
24779 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24782 /* Parse a throw-expression.
24785 throw assignment-expression [opt]
24787 Returns a THROW_EXPR representing the throw-expression. */
24790 cp_parser_throw_expression (cp_parser
* parser
)
24795 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24796 token
= cp_lexer_peek_token (parser
->lexer
);
24797 /* Figure out whether or not there is an assignment-expression
24798 following the "throw" keyword. */
24799 if (token
->type
== CPP_COMMA
24800 || token
->type
== CPP_SEMICOLON
24801 || token
->type
== CPP_CLOSE_PAREN
24802 || token
->type
== CPP_CLOSE_SQUARE
24803 || token
->type
== CPP_CLOSE_BRACE
24804 || token
->type
== CPP_COLON
)
24805 expression
= NULL_TREE
;
24807 expression
= cp_parser_assignment_expression (parser
);
24809 return build_throw (expression
);
24812 /* GNU Extensions */
24814 /* Parse an (optional) asm-specification.
24817 asm ( string-literal )
24819 If the asm-specification is present, returns a STRING_CST
24820 corresponding to the string-literal. Otherwise, returns
24824 cp_parser_asm_specification_opt (cp_parser
* parser
)
24827 tree asm_specification
;
24829 /* Peek at the next token. */
24830 token
= cp_lexer_peek_token (parser
->lexer
);
24831 /* If the next token isn't the `asm' keyword, then there's no
24832 asm-specification. */
24833 if (!cp_parser_is_keyword (token
, RID_ASM
))
24836 /* Consume the `asm' token. */
24837 cp_lexer_consume_token (parser
->lexer
);
24838 /* Look for the `('. */
24839 matching_parens parens
;
24840 parens
.require_open (parser
);
24842 /* Look for the string-literal. */
24843 asm_specification
= cp_parser_string_literal (parser
, false, false);
24845 /* Look for the `)'. */
24846 parens
.require_close (parser
);
24848 return asm_specification
;
24851 /* Parse an asm-operand-list.
24855 asm-operand-list , asm-operand
24858 string-literal ( expression )
24859 [ string-literal ] string-literal ( expression )
24861 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24862 each node is the expression. The TREE_PURPOSE is itself a
24863 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24864 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24865 is a STRING_CST for the string literal before the parenthesis. Returns
24866 ERROR_MARK_NODE if any of the operands are invalid. */
24869 cp_parser_asm_operand_list (cp_parser
* parser
)
24871 tree asm_operands
= NULL_TREE
;
24872 bool invalid_operands
= false;
24876 tree string_literal
;
24880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24882 /* Consume the `[' token. */
24883 cp_lexer_consume_token (parser
->lexer
);
24884 /* Read the operand name. */
24885 name
= cp_parser_identifier (parser
);
24886 if (name
!= error_mark_node
)
24887 name
= build_string (IDENTIFIER_LENGTH (name
),
24888 IDENTIFIER_POINTER (name
));
24889 /* Look for the closing `]'. */
24890 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24894 /* Look for the string-literal. */
24895 string_literal
= cp_parser_string_literal (parser
, false, false);
24897 /* Look for the `('. */
24898 matching_parens parens
;
24899 parens
.require_open (parser
);
24900 /* Parse the expression. */
24901 expression
= cp_parser_expression (parser
);
24902 /* Look for the `)'. */
24903 parens
.require_close (parser
);
24905 if (name
== error_mark_node
24906 || string_literal
== error_mark_node
24907 || expression
== error_mark_node
)
24908 invalid_operands
= true;
24910 /* Add this operand to the list. */
24911 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24914 /* If the next token is not a `,', there are no more
24916 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24918 /* Consume the `,'. */
24919 cp_lexer_consume_token (parser
->lexer
);
24922 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24925 /* Parse an asm-clobber-list.
24929 asm-clobber-list , string-literal
24931 Returns a TREE_LIST, indicating the clobbers in the order that they
24932 appeared. The TREE_VALUE of each node is a STRING_CST. */
24935 cp_parser_asm_clobber_list (cp_parser
* parser
)
24937 tree clobbers
= NULL_TREE
;
24941 tree string_literal
;
24943 /* Look for the string literal. */
24944 string_literal
= cp_parser_string_literal (parser
, false, false);
24945 /* Add it to the list. */
24946 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24947 /* If the next token is not a `,', then the list is
24949 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24951 /* Consume the `,' token. */
24952 cp_lexer_consume_token (parser
->lexer
);
24958 /* Parse an asm-label-list.
24962 asm-label-list , identifier
24964 Returns a TREE_LIST, indicating the labels in the order that they
24965 appeared. The TREE_VALUE of each node is a label. */
24968 cp_parser_asm_label_list (cp_parser
* parser
)
24970 tree labels
= NULL_TREE
;
24974 tree identifier
, label
, name
;
24976 /* Look for the identifier. */
24977 identifier
= cp_parser_identifier (parser
);
24978 if (!error_operand_p (identifier
))
24980 label
= lookup_label (identifier
);
24981 if (TREE_CODE (label
) == LABEL_DECL
)
24983 TREE_USED (label
) = 1;
24984 check_goto (label
);
24985 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24986 IDENTIFIER_POINTER (identifier
));
24987 labels
= tree_cons (name
, label
, labels
);
24990 /* If the next token is not a `,', then the list is
24992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24994 /* Consume the `,' token. */
24995 cp_lexer_consume_token (parser
->lexer
);
24998 return nreverse (labels
);
25001 /* Return TRUE iff the next tokens in the stream are possibly the
25002 beginning of a GNU extension attribute. */
25005 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
25007 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
25010 /* Return TRUE iff the next tokens in the stream are possibly the
25011 beginning of a standard C++-11 attribute specifier. */
25014 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
25016 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
25019 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25020 beginning of a standard C++-11 attribute specifier. */
25023 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
25025 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25027 return (cxx_dialect
>= cxx11
25028 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
25029 || (token
->type
== CPP_OPEN_SQUARE
25030 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
25031 && token
->type
== CPP_OPEN_SQUARE
)));
25034 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25035 beginning of a GNU extension attribute. */
25038 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25040 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25042 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25045 /* Return true iff the next tokens can be the beginning of either a
25046 GNU attribute list, or a standard C++11 attribute sequence. */
25049 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25051 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25052 || cp_next_tokens_can_be_std_attribute_p (parser
));
25055 /* Return true iff the next Nth tokens can be the beginning of either
25056 a GNU attribute list, or a standard C++11 attribute sequence. */
25059 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25061 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25062 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25065 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25066 of GNU attributes, or return NULL. */
25069 cp_parser_attributes_opt (cp_parser
*parser
)
25071 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25072 return cp_parser_gnu_attributes_opt (parser
);
25073 return cp_parser_std_attribute_spec_seq (parser
);
25076 /* Parse an (optional) series of attributes.
25079 attributes attribute
25082 __attribute__ (( attribute-list [opt] ))
25084 The return value is as for cp_parser_gnu_attribute_list. */
25087 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25089 tree attributes
= NULL_TREE
;
25091 temp_override
<bool> cleanup
25092 (parser
->auto_is_implicit_function_template_parm_p
, false);
25097 tree attribute_list
;
25100 /* Peek at the next token. */
25101 token
= cp_lexer_peek_token (parser
->lexer
);
25102 /* If it's not `__attribute__', then we're done. */
25103 if (token
->keyword
!= RID_ATTRIBUTE
)
25106 /* Consume the `__attribute__' keyword. */
25107 cp_lexer_consume_token (parser
->lexer
);
25108 /* Look for the two `(' tokens. */
25109 matching_parens outer_parens
;
25110 outer_parens
.require_open (parser
);
25111 matching_parens inner_parens
;
25112 inner_parens
.require_open (parser
);
25114 /* Peek at the next token. */
25115 token
= cp_lexer_peek_token (parser
->lexer
);
25116 if (token
->type
!= CPP_CLOSE_PAREN
)
25117 /* Parse the attribute-list. */
25118 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25120 /* If the next token is a `)', then there is no attribute
25122 attribute_list
= NULL
;
25124 /* Look for the two `)' tokens. */
25125 if (!inner_parens
.require_close (parser
))
25127 if (!outer_parens
.require_close (parser
))
25130 cp_parser_skip_to_end_of_statement (parser
);
25132 /* Add these new attributes to the list. */
25133 attributes
= attr_chainon (attributes
, attribute_list
);
25139 /* Parse a GNU attribute-list.
25143 attribute-list , attribute
25147 identifier ( identifier )
25148 identifier ( identifier , expression-list )
25149 identifier ( expression-list )
25151 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25152 to an attribute. The TREE_PURPOSE of each node is the identifier
25153 indicating which attribute is in use. The TREE_VALUE represents
25154 the arguments, if any. */
25157 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25159 tree attribute_list
= NULL_TREE
;
25160 bool save_translate_strings_p
= parser
->translate_strings_p
;
25162 parser
->translate_strings_p
= false;
25169 /* Look for the identifier. We also allow keywords here; for
25170 example `__attribute__ ((const))' is legal. */
25171 token
= cp_lexer_peek_token (parser
->lexer
);
25172 if (token
->type
== CPP_NAME
25173 || token
->type
== CPP_KEYWORD
)
25175 tree arguments
= NULL_TREE
;
25177 /* Consume the token, but save it since we need it for the
25178 SIMD enabled function parsing. */
25179 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25181 /* Save away the identifier that indicates which attribute
25183 identifier
= (token
->type
== CPP_KEYWORD
)
25184 /* For keywords, use the canonical spelling, not the
25185 parsed identifier. */
25186 ? ridpointers
[(int) token
->keyword
]
25187 : id_token
->u
.value
;
25189 identifier
= canonicalize_attr_name (identifier
);
25190 attribute
= build_tree_list (identifier
, NULL_TREE
);
25192 /* Peek at the next token. */
25193 token
= cp_lexer_peek_token (parser
->lexer
);
25194 /* If it's an `(', then parse the attribute arguments. */
25195 if (token
->type
== CPP_OPEN_PAREN
)
25197 vec
<tree
, va_gc
> *vec
;
25198 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25199 ? id_attr
: normal_attr
);
25200 vec
= cp_parser_parenthesized_expression_list
25201 (parser
, attr_flag
, /*cast_p=*/false,
25202 /*allow_expansion_p=*/false,
25203 /*non_constant_p=*/NULL
);
25205 arguments
= error_mark_node
;
25208 arguments
= build_tree_list_vec (vec
);
25209 release_tree_vector (vec
);
25211 /* Save the arguments away. */
25212 TREE_VALUE (attribute
) = arguments
;
25215 if (arguments
!= error_mark_node
)
25217 /* Add this attribute to the list. */
25218 TREE_CHAIN (attribute
) = attribute_list
;
25219 attribute_list
= attribute
;
25222 token
= cp_lexer_peek_token (parser
->lexer
);
25224 /* Now, look for more attributes. If the next token isn't a
25225 `,', we're done. */
25226 if (token
->type
!= CPP_COMMA
)
25229 /* Consume the comma and keep going. */
25230 cp_lexer_consume_token (parser
->lexer
);
25232 parser
->translate_strings_p
= save_translate_strings_p
;
25234 /* We built up the list in reverse order. */
25235 return nreverse (attribute_list
);
25238 /* Parse a standard C++11 attribute.
25240 The returned representation is a TREE_LIST which TREE_PURPOSE is
25241 the scoped name of the attribute, and the TREE_VALUE is its
25244 Note that the scoped name of the attribute is itself a TREE_LIST
25245 which TREE_PURPOSE is the namespace of the attribute, and
25246 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25247 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25248 and which TREE_PURPOSE is directly the attribute name.
25250 Clients of the attribute code should use get_attribute_namespace
25251 and get_attribute_name to get the actual namespace and name of
25252 attributes, regardless of their being GNU or C++11 attributes.
25255 attribute-token attribute-argument-clause [opt]
25259 attribute-scoped-token
25261 attribute-scoped-token:
25262 attribute-namespace :: identifier
25264 attribute-namespace:
25267 attribute-argument-clause:
25268 ( balanced-token-seq )
25270 balanced-token-seq:
25271 balanced-token [opt]
25272 balanced-token-seq balanced-token
25275 ( balanced-token-seq )
25276 [ balanced-token-seq ]
25277 { balanced-token-seq }. */
25280 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25282 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25285 temp_override
<bool> cleanup
25286 (parser
->auto_is_implicit_function_template_parm_p
, false);
25288 /* First, parse name of the attribute, a.k.a attribute-token. */
25290 token
= cp_lexer_peek_token (parser
->lexer
);
25291 if (token
->type
== CPP_NAME
)
25292 attr_id
= token
->u
.value
;
25293 else if (token
->type
== CPP_KEYWORD
)
25294 attr_id
= ridpointers
[(int) token
->keyword
];
25295 else if (token
->flags
& NAMED_OP
)
25296 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25298 if (attr_id
== NULL_TREE
)
25301 cp_lexer_consume_token (parser
->lexer
);
25303 token
= cp_lexer_peek_token (parser
->lexer
);
25304 if (token
->type
== CPP_SCOPE
)
25306 /* We are seeing a scoped attribute token. */
25308 cp_lexer_consume_token (parser
->lexer
);
25310 error_at (token
->location
, "attribute using prefix used together "
25311 "with scoped attribute token");
25314 token
= cp_lexer_consume_token (parser
->lexer
);
25315 if (token
->type
== CPP_NAME
)
25316 attr_id
= token
->u
.value
;
25317 else if (token
->type
== CPP_KEYWORD
)
25318 attr_id
= ridpointers
[(int) token
->keyword
];
25319 else if (token
->flags
& NAMED_OP
)
25320 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25323 error_at (token
->location
,
25324 "expected an identifier for the attribute name");
25325 return error_mark_node
;
25328 attr_id
= canonicalize_attr_name (attr_id
);
25329 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25331 token
= cp_lexer_peek_token (parser
->lexer
);
25334 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25338 attr_id
= canonicalize_attr_name (attr_id
);
25339 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25341 /* C++11 noreturn attribute is equivalent to GNU's. */
25342 if (is_attribute_p ("noreturn", attr_id
))
25343 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25344 /* C++14 deprecated attribute is equivalent to GNU's. */
25345 else if (is_attribute_p ("deprecated", attr_id
))
25346 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25347 /* C++17 fallthrough attribute is equivalent to GNU's. */
25348 else if (is_attribute_p ("fallthrough", attr_id
))
25349 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25350 /* Transactional Memory TS optimize_for_synchronized attribute is
25351 equivalent to GNU transaction_callable. */
25352 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25353 TREE_PURPOSE (attribute
)
25354 = get_identifier ("transaction_callable");
25355 /* Transactional Memory attributes are GNU attributes. */
25356 else if (tm_attr_to_mask (attr_id
))
25357 TREE_PURPOSE (attribute
) = attr_id
;
25360 /* Now parse the optional argument clause of the attribute. */
25362 if (token
->type
!= CPP_OPEN_PAREN
)
25366 vec
<tree
, va_gc
> *vec
;
25367 int attr_flag
= normal_attr
;
25369 if (attr_ns
== get_identifier ("gnu")
25370 && attribute_takes_identifier_p (attr_id
))
25371 /* A GNU attribute that takes an identifier in parameter. */
25372 attr_flag
= id_attr
;
25374 vec
= cp_parser_parenthesized_expression_list
25375 (parser
, attr_flag
, /*cast_p=*/false,
25376 /*allow_expansion_p=*/true,
25377 /*non_constant_p=*/NULL
);
25379 arguments
= error_mark_node
;
25382 arguments
= build_tree_list_vec (vec
);
25383 release_tree_vector (vec
);
25386 if (arguments
== error_mark_node
)
25387 attribute
= error_mark_node
;
25389 TREE_VALUE (attribute
) = arguments
;
25395 /* Check that the attribute ATTRIBUTE appears at most once in the
25396 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25397 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25398 isn't implemented yet in GCC. */
25401 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25405 tree name
= get_attribute_name (attribute
);
25406 if (is_attribute_p ("noreturn", name
)
25407 && lookup_attribute ("noreturn", attributes
))
25408 error ("attribute %<noreturn%> can appear at most once "
25409 "in an attribute-list");
25410 else if (is_attribute_p ("deprecated", name
)
25411 && lookup_attribute ("deprecated", attributes
))
25412 error ("attribute %<deprecated%> can appear at most once "
25413 "in an attribute-list");
25417 /* Parse a list of standard C++-11 attributes.
25421 attribute-list , attribute[opt]
25423 attribute-list , attribute ...
25427 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25429 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25430 cp_token
*token
= NULL
;
25434 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25435 if (attribute
== error_mark_node
)
25437 if (attribute
!= NULL_TREE
)
25439 cp_parser_check_std_attribute (attributes
, attribute
);
25440 TREE_CHAIN (attribute
) = attributes
;
25441 attributes
= attribute
;
25443 token
= cp_lexer_peek_token (parser
->lexer
);
25444 if (token
->type
== CPP_ELLIPSIS
)
25446 cp_lexer_consume_token (parser
->lexer
);
25447 if (attribute
== NULL_TREE
)
25448 error_at (token
->location
,
25449 "expected attribute before %<...%>");
25452 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25453 if (pack
== error_mark_node
)
25454 return error_mark_node
;
25455 TREE_VALUE (attribute
) = pack
;
25457 token
= cp_lexer_peek_token (parser
->lexer
);
25459 if (token
->type
!= CPP_COMMA
)
25461 cp_lexer_consume_token (parser
->lexer
);
25463 attributes
= nreverse (attributes
);
25467 /* Parse a standard C++-11 attribute specifier.
25469 attribute-specifier:
25470 [ [ attribute-using-prefix [opt] attribute-list ] ]
25471 alignment-specifier
25473 attribute-using-prefix:
25474 using attribute-namespace :
25476 alignment-specifier:
25477 alignas ( type-id ... [opt] )
25478 alignas ( alignment-expression ... [opt] ). */
25481 cp_parser_std_attribute_spec (cp_parser
*parser
)
25483 tree attributes
= NULL_TREE
;
25484 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25486 if (token
->type
== CPP_OPEN_SQUARE
25487 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25489 tree attr_ns
= NULL_TREE
;
25491 cp_lexer_consume_token (parser
->lexer
);
25492 cp_lexer_consume_token (parser
->lexer
);
25494 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25496 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25497 if (token
->type
== CPP_NAME
)
25498 attr_ns
= token
->u
.value
;
25499 else if (token
->type
== CPP_KEYWORD
)
25500 attr_ns
= ridpointers
[(int) token
->keyword
];
25501 else if (token
->flags
& NAMED_OP
)
25502 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25505 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25507 if (cxx_dialect
< cxx17
25508 && !in_system_header_at (input_location
))
25509 pedwarn (input_location
, 0,
25510 "attribute using prefix only available "
25511 "with -std=c++17 or -std=gnu++17");
25513 cp_lexer_consume_token (parser
->lexer
);
25514 cp_lexer_consume_token (parser
->lexer
);
25515 cp_lexer_consume_token (parser
->lexer
);
25518 attr_ns
= NULL_TREE
;
25521 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25523 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25524 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25525 cp_parser_skip_to_end_of_statement (parser
);
25527 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25528 when we are sure that we have actually parsed them. */
25529 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25535 /* Look for an alignment-specifier. */
25537 token
= cp_lexer_peek_token (parser
->lexer
);
25539 if (token
->type
!= CPP_KEYWORD
25540 || token
->keyword
!= RID_ALIGNAS
)
25543 cp_lexer_consume_token (parser
->lexer
);
25544 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25546 matching_parens parens
;
25547 if (!parens
.require_open (parser
))
25548 return error_mark_node
;
25550 cp_parser_parse_tentatively (parser
);
25551 alignas_expr
= cp_parser_type_id (parser
);
25553 if (!cp_parser_parse_definitely (parser
))
25555 alignas_expr
= cp_parser_assignment_expression (parser
);
25556 if (alignas_expr
== error_mark_node
)
25557 cp_parser_skip_to_end_of_statement (parser
);
25558 if (alignas_expr
== NULL_TREE
25559 || alignas_expr
== error_mark_node
)
25560 return alignas_expr
;
25563 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25564 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25566 /* Handle alignas (pack...). */
25567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25569 cp_lexer_consume_token (parser
->lexer
);
25570 alignas_expr
= make_pack_expansion (alignas_expr
);
25573 /* Something went wrong, so don't build the attribute. */
25574 if (alignas_expr
== error_mark_node
)
25575 return error_mark_node
;
25577 if (!parens
.require_close (parser
))
25578 return error_mark_node
;
25580 /* Build the C++-11 representation of an 'aligned'
25583 build_tree_list (build_tree_list (get_identifier ("gnu"),
25584 get_identifier ("aligned")),
25591 /* Parse a standard C++-11 attribute-specifier-seq.
25593 attribute-specifier-seq:
25594 attribute-specifier-seq [opt] attribute-specifier
25598 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25600 tree attr_specs
= NULL_TREE
;
25601 tree attr_last
= NULL_TREE
;
25605 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25606 if (attr_spec
== NULL_TREE
)
25608 if (attr_spec
== error_mark_node
)
25609 return error_mark_node
;
25612 TREE_CHAIN (attr_last
) = attr_spec
;
25614 attr_specs
= attr_last
= attr_spec
;
25615 attr_last
= tree_last (attr_last
);
25621 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25622 return index of the first token after balanced-token, or N on failure. */
25625 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25628 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25630 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25633 case CPP_PRAGMA_EOL
:
25634 /* Ran out of tokens. */
25636 case CPP_OPEN_PAREN
:
25639 case CPP_OPEN_BRACE
:
25642 case CPP_OPEN_SQUARE
:
25645 case CPP_CLOSE_PAREN
:
25648 case CPP_CLOSE_BRACE
:
25651 case CPP_CLOSE_SQUARE
:
25657 while (nparens
|| nbraces
|| nsquares
);
25661 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25662 return index of the first token after the GNU attribute tokens, or N on
25666 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25670 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25671 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25672 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25675 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25678 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25685 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25686 next token), return index of the first token after the standard C++11
25687 attribute tokens, or N on failure. */
25690 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25694 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25695 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25697 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25700 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25704 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25705 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25707 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25718 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25719 as the next token), return index of the first token after the attribute
25720 tokens, or N on failure. */
25723 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25725 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25726 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25727 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25730 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25731 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25732 current value of the PEDANTIC flag, regardless of whether or not
25733 the `__extension__' keyword is present. The caller is responsible
25734 for restoring the value of the PEDANTIC flag. */
25737 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25739 /* Save the old value of the PEDANTIC flag. */
25740 *saved_pedantic
= pedantic
;
25742 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25744 /* Consume the `__extension__' token. */
25745 cp_lexer_consume_token (parser
->lexer
);
25746 /* We're not being pedantic while the `__extension__' keyword is
25756 /* Parse a label declaration.
25759 __label__ label-declarator-seq ;
25761 label-declarator-seq:
25762 identifier , label-declarator-seq
25766 cp_parser_label_declaration (cp_parser
* parser
)
25768 /* Look for the `__label__' keyword. */
25769 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25775 /* Look for an identifier. */
25776 identifier
= cp_parser_identifier (parser
);
25777 /* If we failed, stop. */
25778 if (identifier
== error_mark_node
)
25780 /* Declare it as a label. */
25781 finish_label_decl (identifier
);
25782 /* If the next token is a `;', stop. */
25783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25785 /* Look for the `,' separating the label declarations. */
25786 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25789 /* Look for the final `;'. */
25790 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25793 // -------------------------------------------------------------------------- //
25796 // Parse a requires clause.
25798 // requires-clause:
25799 // 'requires' logical-or-expression
25801 // The required logical-or-expression must be a constant expression. Note
25802 // that we don't check that the expression is constepxr here. We defer until
25803 // we analyze constraints and then, we only check atomic constraints.
25805 cp_parser_requires_clause (cp_parser
*parser
)
25807 // Parse the requires clause so that it is not automatically folded.
25808 ++processing_template_decl
;
25809 tree expr
= cp_parser_binary_expression (parser
, false, false,
25810 PREC_NOT_OPERATOR
, NULL
);
25811 if (check_for_bare_parameter_packs (expr
))
25812 expr
= error_mark_node
;
25813 --processing_template_decl
;
25817 // Optionally parse a requires clause:
25819 cp_parser_requires_clause_opt (cp_parser
*parser
)
25821 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25822 if (tok
->keyword
!= RID_REQUIRES
)
25824 if (!flag_concepts
&& tok
->type
== CPP_NAME
25825 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25827 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25828 "%<requires%> only available with -fconcepts");
25829 /* Parse and discard the requires-clause. */
25830 cp_lexer_consume_token (parser
->lexer
);
25831 cp_parser_requires_clause (parser
);
25835 cp_lexer_consume_token (parser
->lexer
);
25836 return cp_parser_requires_clause (parser
);
25840 /*---------------------------------------------------------------------------
25841 Requires expressions
25842 ---------------------------------------------------------------------------*/
25844 /* Parse a requires expression
25846 requirement-expression:
25847 'requires' requirement-parameter-list [opt] requirement-body */
25849 cp_parser_requires_expression (cp_parser
*parser
)
25851 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25852 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25854 /* A requires-expression shall appear only within a concept
25855 definition or a requires-clause.
25857 TODO: Implement this diagnostic correctly. */
25858 if (!processing_template_decl
)
25860 error_at (loc
, "a requires expression cannot appear outside a template");
25861 cp_parser_skip_to_end_of_statement (parser
);
25862 return error_mark_node
;
25867 /* Local parameters are delared as variables within the scope
25868 of the expression. They are not visible past the end of
25869 the expression. Expressions within the requires-expression
25870 are unevaluated. */
25871 struct scope_sentinel
25875 ++cp_unevaluated_operand
;
25876 begin_scope (sk_block
, NULL_TREE
);
25881 pop_bindings_and_leave_scope ();
25882 --cp_unevaluated_operand
;
25886 /* Parse the optional parameter list. */
25887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25889 parms
= cp_parser_requirement_parameter_list (parser
);
25890 if (parms
== error_mark_node
)
25891 return error_mark_node
;
25896 /* Parse the requirement body. */
25897 reqs
= cp_parser_requirement_body (parser
);
25898 if (reqs
== error_mark_node
)
25899 return error_mark_node
;
25902 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25904 grokparms (parms
, &parms
);
25905 return finish_requires_expr (parms
, reqs
);
25908 /* Parse a parameterized requirement.
25910 requirement-parameter-list:
25911 '(' parameter-declaration-clause ')' */
25913 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25915 matching_parens parens
;
25916 if (!parens
.require_open (parser
))
25917 return error_mark_node
;
25919 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25921 if (!parens
.require_close (parser
))
25922 return error_mark_node
;
25927 /* Parse the body of a requirement.
25930 '{' requirement-list '}' */
25932 cp_parser_requirement_body (cp_parser
*parser
)
25934 matching_braces braces
;
25935 if (!braces
.require_open (parser
))
25936 return error_mark_node
;
25938 tree reqs
= cp_parser_requirement_list (parser
);
25940 if (!braces
.require_close (parser
))
25941 return error_mark_node
;
25946 /* Parse a list of requirements.
25950 requirement-list ';' requirement[opt] */
25952 cp_parser_requirement_list (cp_parser
*parser
)
25954 tree result
= NULL_TREE
;
25957 tree req
= cp_parser_requirement (parser
);
25958 if (req
== error_mark_node
)
25959 return error_mark_node
;
25961 result
= tree_cons (NULL_TREE
, req
, result
);
25963 /* If we see a semi-colon, consume it. */
25964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25965 cp_lexer_consume_token (parser
->lexer
);
25967 /* Stop processing at the end of the list. */
25968 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25972 /* Reverse the order of requirements so they are analyzed in
25973 declaration order. */
25974 return nreverse (result
);
25977 /* Parse a syntactic requirement or type requirement.
25981 compound-requirement
25983 nested-requirement */
25985 cp_parser_requirement (cp_parser
*parser
)
25987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25988 return cp_parser_compound_requirement (parser
);
25989 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25990 return cp_parser_type_requirement (parser
);
25991 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25992 return cp_parser_nested_requirement (parser
);
25994 return cp_parser_simple_requirement (parser
);
25997 /* Parse a simple requirement.
25999 simple-requirement:
26002 cp_parser_simple_requirement (cp_parser
*parser
)
26004 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26005 if (!expr
|| expr
== error_mark_node
)
26006 return error_mark_node
;
26008 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26009 return error_mark_node
;
26011 return finish_simple_requirement (expr
);
26014 /* Parse a type requirement
26017 nested-name-specifier [opt] required-type-name ';'
26019 required-type-name:
26021 'template' [opt] simple-template-id */
26023 cp_parser_type_requirement (cp_parser
*parser
)
26025 cp_lexer_consume_token (parser
->lexer
);
26027 // Save the scope before parsing name specifiers.
26028 tree saved_scope
= parser
->scope
;
26029 tree saved_object_scope
= parser
->object_scope
;
26030 tree saved_qualifying_scope
= parser
->qualifying_scope
;
26031 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
26032 cp_parser_nested_name_specifier_opt (parser
,
26033 /*typename_keyword_p=*/true,
26034 /*check_dependency_p=*/false,
26036 /*is_declaration=*/false);
26039 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26041 cp_lexer_consume_token (parser
->lexer
);
26042 type
= cp_parser_template_id (parser
,
26043 /*template_keyword_p=*/true,
26044 /*check_dependency=*/false,
26045 /*tag_type=*/none_type
,
26046 /*is_declaration=*/false);
26047 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26048 /*complain=*/tf_error
);
26051 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26053 if (TREE_CODE (type
) == TYPE_DECL
)
26054 type
= TREE_TYPE (type
);
26056 parser
->scope
= saved_scope
;
26057 parser
->object_scope
= saved_object_scope
;
26058 parser
->qualifying_scope
= saved_qualifying_scope
;
26060 if (type
== error_mark_node
)
26061 cp_parser_skip_to_end_of_statement (parser
);
26063 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26064 return error_mark_node
;
26065 if (type
== error_mark_node
)
26066 return error_mark_node
;
26068 return finish_type_requirement (type
);
26071 /* Parse a compound requirement
26073 compound-requirement:
26074 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26076 cp_parser_compound_requirement (cp_parser
*parser
)
26078 /* Parse an expression enclosed in '{ }'s. */
26079 matching_braces braces
;
26080 if (!braces
.require_open (parser
))
26081 return error_mark_node
;
26083 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26084 if (!expr
|| expr
== error_mark_node
)
26085 return error_mark_node
;
26087 if (!braces
.require_close (parser
))
26088 return error_mark_node
;
26090 /* Parse the optional noexcept. */
26091 bool noexcept_p
= false;
26092 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26094 cp_lexer_consume_token (parser
->lexer
);
26098 /* Parse the optional trailing return type. */
26099 tree type
= NULL_TREE
;
26100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26102 cp_lexer_consume_token (parser
->lexer
);
26103 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26104 parser
->in_result_type_constraint_p
= true;
26105 type
= cp_parser_trailing_type_id (parser
);
26106 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26107 if (type
== error_mark_node
)
26108 return error_mark_node
;
26111 return finish_compound_requirement (expr
, type
, noexcept_p
);
26114 /* Parse a nested requirement. This is the same as a requires clause.
26116 nested-requirement:
26119 cp_parser_nested_requirement (cp_parser
*parser
)
26121 cp_lexer_consume_token (parser
->lexer
);
26122 tree req
= cp_parser_requires_clause (parser
);
26123 if (req
== error_mark_node
)
26124 return error_mark_node
;
26125 return finish_nested_requirement (req
);
26128 /* Support Functions */
26130 /* Return the appropriate prefer_type argument for lookup_name_real based on
26131 tag_type and template_mem_access. */
26134 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26136 /* DR 141: When looking in the current enclosing context for a template-name
26137 after -> or ., only consider class templates. */
26138 if (template_mem_access
)
26142 case none_type
: return 0; // No preference.
26143 case scope_type
: return 1; // Type or namespace.
26144 default: return 2; // Type only.
26148 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26149 NAME should have one of the representations used for an
26150 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26151 is returned. If PARSER->SCOPE is a dependent type, then a
26152 SCOPE_REF is returned.
26154 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26155 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26156 was formed. Abstractly, such entities should not be passed to this
26157 function, because they do not need to be looked up, but it is
26158 simpler to check for this special case here, rather than at the
26161 In cases not explicitly covered above, this function returns a
26162 DECL, OVERLOAD, or baselink representing the result of the lookup.
26163 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26166 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26167 (e.g., "struct") that was used. In that case bindings that do not
26168 refer to types are ignored.
26170 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26173 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26176 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26179 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26180 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26181 NULL_TREE otherwise. */
26184 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26185 enum tag_types tag_type
,
26188 bool check_dependency
,
26189 tree
*ambiguous_decls
,
26190 location_t name_location
)
26193 tree object_type
= parser
->context
->object_type
;
26195 /* Assume that the lookup will be unambiguous. */
26196 if (ambiguous_decls
)
26197 *ambiguous_decls
= NULL_TREE
;
26199 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26200 no longer valid. Note that if we are parsing tentatively, and
26201 the parse fails, OBJECT_TYPE will be automatically restored. */
26202 parser
->context
->object_type
= NULL_TREE
;
26204 if (name
== error_mark_node
)
26205 return error_mark_node
;
26207 /* A template-id has already been resolved; there is no lookup to
26209 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26211 if (BASELINK_P (name
))
26213 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26214 == TEMPLATE_ID_EXPR
);
26218 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26219 it should already have been checked to make sure that the name
26220 used matches the type being destroyed. */
26221 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26225 /* Figure out to which type this destructor applies. */
26227 type
= parser
->scope
;
26228 else if (object_type
)
26229 type
= object_type
;
26231 type
= current_class_type
;
26232 /* If that's not a class type, there is no destructor. */
26233 if (!type
|| !CLASS_TYPE_P (type
))
26234 return error_mark_node
;
26236 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26237 lazily_declare_fn (sfk_destructor
, type
);
26239 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26242 return error_mark_node
;
26245 /* By this point, the NAME should be an ordinary identifier. If
26246 the id-expression was a qualified name, the qualifying scope is
26247 stored in PARSER->SCOPE at this point. */
26248 gcc_assert (identifier_p (name
));
26250 /* Perform the lookup. */
26255 if (parser
->scope
== error_mark_node
)
26256 return error_mark_node
;
26258 /* If the SCOPE is dependent, the lookup must be deferred until
26259 the template is instantiated -- unless we are explicitly
26260 looking up names in uninstantiated templates. Even then, we
26261 cannot look up the name if the scope is not a class type; it
26262 might, for example, be a template type parameter. */
26263 dependent_p
= (TYPE_P (parser
->scope
)
26264 && dependent_scope_p (parser
->scope
));
26265 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26267 /* Defer lookup. */
26268 decl
= error_mark_node
;
26271 tree pushed_scope
= NULL_TREE
;
26273 /* If PARSER->SCOPE is a dependent type, then it must be a
26274 class type, and we must not be checking dependencies;
26275 otherwise, we would have processed this lookup above. So
26276 that PARSER->SCOPE is not considered a dependent base by
26277 lookup_member, we must enter the scope here. */
26279 pushed_scope
= push_scope (parser
->scope
);
26281 /* If the PARSER->SCOPE is a template specialization, it
26282 may be instantiated during name lookup. In that case,
26283 errors may be issued. Even if we rollback the current
26284 tentative parse, those errors are valid. */
26285 decl
= lookup_qualified_name (parser
->scope
, name
,
26286 prefer_type_arg (tag_type
),
26287 /*complain=*/true);
26289 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26290 lookup result and the nested-name-specifier nominates a class C:
26291 * if the name specified after the nested-name-specifier, when
26292 looked up in C, is the injected-class-name of C (Clause 9), or
26293 * if the name specified after the nested-name-specifier is the
26294 same as the identifier or the simple-template-id's template-
26295 name in the last component of the nested-name-specifier,
26296 the name is instead considered to name the constructor of
26297 class C. [ Note: for example, the constructor is not an
26298 acceptable lookup result in an elaborated-type-specifier so
26299 the constructor would not be used in place of the
26300 injected-class-name. --end note ] Such a constructor name
26301 shall be used only in the declarator-id of a declaration that
26302 names a constructor or in a using-declaration. */
26303 if (tag_type
== none_type
26304 && DECL_SELF_REFERENCE_P (decl
)
26305 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26306 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26307 prefer_type_arg (tag_type
),
26308 /*complain=*/true);
26310 /* If we have a single function from a using decl, pull it out. */
26311 if (TREE_CODE (decl
) == OVERLOAD
26312 && !really_overloaded_fn (decl
))
26313 decl
= OVL_FUNCTION (decl
);
26316 pop_scope (pushed_scope
);
26319 /* If the scope is a dependent type and either we deferred lookup or
26320 we did lookup but didn't find the name, rememeber the name. */
26321 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26322 && dependent_type_p (parser
->scope
))
26328 /* The resolution to Core Issue 180 says that `struct
26329 A::B' should be considered a type-name, even if `A'
26331 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26332 /*complain=*/tf_error
);
26333 if (type
!= error_mark_node
)
26334 decl
= TYPE_NAME (type
);
26336 else if (is_template
26337 && (cp_parser_next_token_ends_template_argument_p (parser
)
26338 || cp_lexer_next_token_is (parser
->lexer
,
26340 decl
= make_unbound_class_template (parser
->scope
,
26342 /*complain=*/tf_error
);
26344 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26345 parser
->scope
, name
,
26348 parser
->qualifying_scope
= parser
->scope
;
26349 parser
->object_scope
= NULL_TREE
;
26351 else if (object_type
)
26353 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26354 OBJECT_TYPE is not a class. */
26355 if (CLASS_TYPE_P (object_type
))
26356 /* If the OBJECT_TYPE is a template specialization, it may
26357 be instantiated during name lookup. In that case, errors
26358 may be issued. Even if we rollback the current tentative
26359 parse, those errors are valid. */
26360 decl
= lookup_member (object_type
,
26363 prefer_type_arg (tag_type
),
26364 tf_warning_or_error
);
26369 /* Look it up in the enclosing context. DR 141: When looking for a
26370 template-name after -> or ., only consider class templates. */
26371 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26373 /*block_p=*/true, is_namespace
, 0);
26374 if (object_type
== unknown_type_node
)
26375 /* The object is type-dependent, so we can't look anything up; we used
26376 this to get the DR 141 behavior. */
26377 object_type
= NULL_TREE
;
26378 parser
->object_scope
= object_type
;
26379 parser
->qualifying_scope
= NULL_TREE
;
26383 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26385 /*block_p=*/true, is_namespace
, 0);
26386 parser
->qualifying_scope
= NULL_TREE
;
26387 parser
->object_scope
= NULL_TREE
;
26390 /* If the lookup failed, let our caller know. */
26391 if (!decl
|| decl
== error_mark_node
)
26392 return error_mark_node
;
26394 /* Pull out the template from an injected-class-name (or multiple). */
26396 decl
= maybe_get_template_decl_from_type_decl (decl
);
26398 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26399 if (TREE_CODE (decl
) == TREE_LIST
)
26401 if (ambiguous_decls
)
26402 *ambiguous_decls
= decl
;
26403 /* The error message we have to print is too complicated for
26404 cp_parser_error, so we incorporate its actions directly. */
26405 if (!cp_parser_simulate_error (parser
))
26407 error_at (name_location
, "reference to %qD is ambiguous",
26409 print_candidates (decl
);
26411 return error_mark_node
;
26414 gcc_assert (DECL_P (decl
)
26415 || TREE_CODE (decl
) == OVERLOAD
26416 || TREE_CODE (decl
) == SCOPE_REF
26417 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26418 || BASELINK_P (decl
));
26420 /* If we have resolved the name of a member declaration, check to
26421 see if the declaration is accessible. When the name resolves to
26422 set of overloaded functions, accessibility is checked when
26423 overload resolution is done.
26425 During an explicit instantiation, access is not checked at all,
26426 as per [temp.explicit]. */
26428 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26430 maybe_record_typedef_use (decl
);
26432 return cp_expr (decl
, name_location
);
26435 /* Like cp_parser_lookup_name, but for use in the typical case where
26436 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26437 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26440 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26442 return cp_parser_lookup_name (parser
, name
,
26444 /*is_template=*/false,
26445 /*is_namespace=*/false,
26446 /*check_dependency=*/true,
26447 /*ambiguous_decls=*/NULL
,
26451 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26452 the current context, return the TYPE_DECL. If TAG_NAME_P is
26453 true, the DECL indicates the class being defined in a class-head,
26454 or declared in an elaborated-type-specifier.
26456 Otherwise, return DECL. */
26459 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26461 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26462 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26465 template <typename T> struct B;
26468 template <typename T> struct A::B {};
26470 Similarly, in an elaborated-type-specifier:
26472 namespace N { struct X{}; }
26475 template <typename T> friend struct N::X;
26478 However, if the DECL refers to a class type, and we are in
26479 the scope of the class, then the name lookup automatically
26480 finds the TYPE_DECL created by build_self_reference rather
26481 than a TEMPLATE_DECL. For example, in:
26483 template <class T> struct S {
26487 there is no need to handle such case. */
26489 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26490 return DECL_TEMPLATE_RESULT (decl
);
26495 /* If too many, or too few, template-parameter lists apply to the
26496 declarator, issue an error message. Returns TRUE if all went well,
26497 and FALSE otherwise. */
26500 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26501 cp_declarator
*declarator
,
26502 location_t declarator_location
)
26504 switch (declarator
->kind
)
26508 unsigned num_templates
= 0;
26509 tree scope
= declarator
->u
.id
.qualifying_scope
;
26510 bool template_id_p
= false;
26513 num_templates
= num_template_headers_for_class (scope
);
26514 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26515 == TEMPLATE_ID_EXPR
)
26517 /* If the DECLARATOR has the form `X<y>' then it uses one
26518 additional level of template parameters. */
26520 template_id_p
= true;
26523 return cp_parser_check_template_parameters
26524 (parser
, num_templates
, template_id_p
, declarator_location
,
26531 case cdk_reference
:
26533 return (cp_parser_check_declarator_template_parameters
26534 (parser
, declarator
->declarator
, declarator_location
));
26541 gcc_unreachable ();
26546 /* NUM_TEMPLATES were used in the current declaration. If that is
26547 invalid, return FALSE and issue an error messages. Otherwise,
26548 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26549 declarator and we can print more accurate diagnostics. */
26552 cp_parser_check_template_parameters (cp_parser
* parser
,
26553 unsigned num_templates
,
26554 bool template_id_p
,
26555 location_t location
,
26556 cp_declarator
*declarator
)
26558 /* If there are the same number of template classes and parameter
26559 lists, that's OK. */
26560 if (parser
->num_template_parameter_lists
== num_templates
)
26562 /* If there are more, but only one more, and the name ends in an identifier,
26563 then we are declaring a primary template. That's OK too. */
26565 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26567 /* If there are more template classes than parameter lists, we have
26570 template <class T> void S<T>::R<T>::f (); */
26571 if (parser
->num_template_parameter_lists
< num_templates
)
26573 if (declarator
&& !current_function_decl
)
26574 error_at (location
, "specializing member %<%T::%E%> "
26575 "requires %<template<>%> syntax",
26576 declarator
->u
.id
.qualifying_scope
,
26577 declarator
->u
.id
.unqualified_name
);
26578 else if (declarator
)
26579 error_at (location
, "invalid declaration of %<%T::%E%>",
26580 declarator
->u
.id
.qualifying_scope
,
26581 declarator
->u
.id
.unqualified_name
);
26583 error_at (location
, "too few template-parameter-lists");
26586 /* Otherwise, there are too many template parameter lists. We have
26589 template <class T> template <class U> void S::f(); */
26590 error_at (location
, "too many template-parameter-lists");
26594 /* Parse an optional `::' token indicating that the following name is
26595 from the global namespace. If so, PARSER->SCOPE is set to the
26596 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26597 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26598 Returns the new value of PARSER->SCOPE, if the `::' token is
26599 present, and NULL_TREE otherwise. */
26602 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26606 /* Peek at the next token. */
26607 token
= cp_lexer_peek_token (parser
->lexer
);
26608 /* If we're looking at a `::' token then we're starting from the
26609 global namespace, not our current location. */
26610 if (token
->type
== CPP_SCOPE
)
26612 /* Consume the `::' token. */
26613 cp_lexer_consume_token (parser
->lexer
);
26614 /* Set the SCOPE so that we know where to start the lookup. */
26615 parser
->scope
= global_namespace
;
26616 parser
->qualifying_scope
= global_namespace
;
26617 parser
->object_scope
= NULL_TREE
;
26619 return parser
->scope
;
26621 else if (!current_scope_valid_p
)
26623 parser
->scope
= NULL_TREE
;
26624 parser
->qualifying_scope
= NULL_TREE
;
26625 parser
->object_scope
= NULL_TREE
;
26631 /* Returns TRUE if the upcoming token sequence is the start of a
26632 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26633 declarator is preceded by the `friend' specifier. */
26636 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26638 bool constructor_p
;
26639 bool outside_class_specifier_p
;
26640 tree nested_name_specifier
;
26641 cp_token
*next_token
;
26643 /* The common case is that this is not a constructor declarator, so
26644 try to avoid doing lots of work if at all possible. It's not
26645 valid declare a constructor at function scope. */
26646 if (parser
->in_function_body
)
26648 /* And only certain tokens can begin a constructor declarator. */
26649 next_token
= cp_lexer_peek_token (parser
->lexer
);
26650 if (next_token
->type
!= CPP_NAME
26651 && next_token
->type
!= CPP_SCOPE
26652 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26653 && next_token
->type
!= CPP_TEMPLATE_ID
)
26656 /* Parse tentatively; we are going to roll back all of the tokens
26658 cp_parser_parse_tentatively (parser
);
26659 /* Assume that we are looking at a constructor declarator. */
26660 constructor_p
= true;
26662 /* Look for the optional `::' operator. */
26663 cp_parser_global_scope_opt (parser
,
26664 /*current_scope_valid_p=*/false);
26665 /* Look for the nested-name-specifier. */
26666 nested_name_specifier
26667 = (cp_parser_nested_name_specifier_opt (parser
,
26668 /*typename_keyword_p=*/false,
26669 /*check_dependency_p=*/false,
26671 /*is_declaration=*/false));
26673 outside_class_specifier_p
= (!at_class_scope_p ()
26674 || !TYPE_BEING_DEFINED (current_class_type
)
26677 /* Outside of a class-specifier, there must be a
26678 nested-name-specifier. Except in C++17 mode, where we
26679 might be declaring a guiding declaration. */
26680 if (!nested_name_specifier
&& outside_class_specifier_p
26681 && cxx_dialect
< cxx17
)
26682 constructor_p
= false;
26683 else if (nested_name_specifier
== error_mark_node
)
26684 constructor_p
= false;
26686 /* If we have a class scope, this is easy; DR 147 says that S::S always
26687 names the constructor, and no other qualified name could. */
26688 if (constructor_p
&& nested_name_specifier
26689 && CLASS_TYPE_P (nested_name_specifier
))
26691 tree id
= cp_parser_unqualified_id (parser
,
26692 /*template_keyword_p=*/false,
26693 /*check_dependency_p=*/false,
26694 /*declarator_p=*/true,
26695 /*optional_p=*/false);
26696 if (is_overloaded_fn (id
))
26697 id
= DECL_NAME (get_first_fn (id
));
26698 if (!constructor_name_p (id
, nested_name_specifier
))
26699 constructor_p
= false;
26701 /* If we still think that this might be a constructor-declarator,
26702 look for a class-name. */
26703 else if (constructor_p
)
26707 template <typename T> struct S {
26711 we must recognize that the nested `S' names a class. */
26712 if (cxx_dialect
>= cxx17
)
26713 cp_parser_parse_tentatively (parser
);
26716 type_decl
= cp_parser_class_name (parser
,
26717 /*typename_keyword_p=*/false,
26718 /*template_keyword_p=*/false,
26720 /*check_dependency_p=*/false,
26721 /*class_head_p=*/false,
26722 /*is_declaration=*/false);
26724 if (cxx_dialect
>= cxx17
26725 && !cp_parser_parse_definitely (parser
))
26727 type_decl
= NULL_TREE
;
26728 tree tmpl
= cp_parser_template_name (parser
,
26729 /*template_keyword*/false,
26730 /*check_dependency_p*/false,
26731 /*is_declaration*/false,
26733 /*is_identifier*/NULL
);
26734 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26735 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26736 /* It's a deduction guide, return true. */;
26738 cp_parser_simulate_error (parser
);
26741 /* If there was no class-name, then this is not a constructor.
26742 Otherwise, if we are in a class-specifier and we aren't
26743 handling a friend declaration, check that its type matches
26744 current_class_type (c++/38313). Note: error_mark_node
26745 is left alone for error recovery purposes. */
26746 constructor_p
= (!cp_parser_error_occurred (parser
)
26747 && (outside_class_specifier_p
26748 || type_decl
== NULL_TREE
26749 || type_decl
== error_mark_node
26750 || same_type_p (current_class_type
,
26751 TREE_TYPE (type_decl
))));
26753 /* If we're still considering a constructor, we have to see a `(',
26754 to begin the parameter-declaration-clause, followed by either a
26755 `)', an `...', or a decl-specifier. We need to check for a
26756 type-specifier to avoid being fooled into thinking that:
26760 is a constructor. (It is actually a function named `f' that
26761 takes one parameter (of type `int') and returns a value of type
26764 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26765 constructor_p
= false;
26768 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26769 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26770 /* A parameter declaration begins with a decl-specifier,
26771 which is either the "attribute" keyword, a storage class
26772 specifier, or (usually) a type-specifier. */
26773 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26776 tree pushed_scope
= NULL_TREE
;
26777 unsigned saved_num_template_parameter_lists
;
26779 /* Names appearing in the type-specifier should be looked up
26780 in the scope of the class. */
26781 if (current_class_type
)
26783 else if (type_decl
)
26785 type
= TREE_TYPE (type_decl
);
26786 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26788 type
= resolve_typename_type (type
,
26789 /*only_current_p=*/false);
26790 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26792 cp_parser_abort_tentative_parse (parser
);
26796 pushed_scope
= push_scope (type
);
26799 /* Inside the constructor parameter list, surrounding
26800 template-parameter-lists do not apply. */
26801 saved_num_template_parameter_lists
26802 = parser
->num_template_parameter_lists
;
26803 parser
->num_template_parameter_lists
= 0;
26805 /* Look for the type-specifier. */
26806 cp_parser_type_specifier (parser
,
26807 CP_PARSER_FLAGS_NONE
,
26808 /*decl_specs=*/NULL
,
26809 /*is_declarator=*/true,
26810 /*declares_class_or_enum=*/NULL
,
26811 /*is_cv_qualifier=*/NULL
);
26813 parser
->num_template_parameter_lists
26814 = saved_num_template_parameter_lists
;
26816 /* Leave the scope of the class. */
26818 pop_scope (pushed_scope
);
26820 constructor_p
= !cp_parser_error_occurred (parser
);
26824 /* We did not really want to consume any tokens. */
26825 cp_parser_abort_tentative_parse (parser
);
26827 return constructor_p
;
26830 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26831 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26832 they must be performed once we are in the scope of the function.
26834 Returns the function defined. */
26837 cp_parser_function_definition_from_specifiers_and_declarator
26838 (cp_parser
* parser
,
26839 cp_decl_specifier_seq
*decl_specifiers
,
26841 const cp_declarator
*declarator
)
26846 /* Begin the function-definition. */
26847 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26849 /* The things we're about to see are not directly qualified by any
26850 template headers we've seen thus far. */
26851 reset_specialization ();
26853 /* If there were names looked up in the decl-specifier-seq that we
26854 did not check, check them now. We must wait until we are in the
26855 scope of the function to perform the checks, since the function
26856 might be a friend. */
26857 perform_deferred_access_checks (tf_warning_or_error
);
26861 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26862 parser
->omp_declare_simd
= NULL
;
26863 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26864 parser
->oacc_routine
= NULL
;
26869 /* Skip the entire function. */
26870 cp_parser_skip_to_end_of_block_or_statement (parser
);
26871 fn
= error_mark_node
;
26873 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26875 /* Seen already, skip it. An error message has already been output. */
26876 cp_parser_skip_to_end_of_block_or_statement (parser
);
26877 fn
= current_function_decl
;
26878 current_function_decl
= NULL_TREE
;
26879 /* If this is a function from a class, pop the nested class. */
26880 if (current_class_name
)
26881 pop_nested_class ();
26886 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26887 tv
= TV_PARSE_INLINE
;
26889 tv
= TV_PARSE_FUNC
;
26891 fn
= cp_parser_function_definition_after_declarator (parser
,
26892 /*inline_p=*/false);
26899 /* Parse the part of a function-definition that follows the
26900 declarator. INLINE_P is TRUE iff this function is an inline
26901 function defined within a class-specifier.
26903 Returns the function defined. */
26906 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26910 bool saved_in_unbraced_linkage_specification_p
;
26911 bool saved_in_function_body
;
26912 unsigned saved_num_template_parameter_lists
;
26914 bool fully_implicit_function_template_p
26915 = parser
->fully_implicit_function_template_p
;
26916 parser
->fully_implicit_function_template_p
= false;
26917 tree implicit_template_parms
26918 = parser
->implicit_template_parms
;
26919 parser
->implicit_template_parms
= 0;
26920 cp_binding_level
* implicit_template_scope
26921 = parser
->implicit_template_scope
;
26922 parser
->implicit_template_scope
= 0;
26924 saved_in_function_body
= parser
->in_function_body
;
26925 parser
->in_function_body
= true;
26926 /* If the next token is `return', then the code may be trying to
26927 make use of the "named return value" extension that G++ used to
26929 token
= cp_lexer_peek_token (parser
->lexer
);
26930 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26932 /* Consume the `return' keyword. */
26933 cp_lexer_consume_token (parser
->lexer
);
26934 /* Look for the identifier that indicates what value is to be
26936 cp_parser_identifier (parser
);
26937 /* Issue an error message. */
26938 error_at (token
->location
,
26939 "named return values are no longer supported");
26940 /* Skip tokens until we reach the start of the function body. */
26943 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26944 if (token
->type
== CPP_OPEN_BRACE
26945 || token
->type
== CPP_EOF
26946 || token
->type
== CPP_PRAGMA_EOL
)
26948 cp_lexer_consume_token (parser
->lexer
);
26951 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26952 anything declared inside `f'. */
26953 saved_in_unbraced_linkage_specification_p
26954 = parser
->in_unbraced_linkage_specification_p
;
26955 parser
->in_unbraced_linkage_specification_p
= false;
26956 /* Inside the function, surrounding template-parameter-lists do not
26958 saved_num_template_parameter_lists
26959 = parser
->num_template_parameter_lists
;
26960 parser
->num_template_parameter_lists
= 0;
26962 /* If the next token is `try', `__transaction_atomic', or
26963 `__transaction_relaxed`, then we are looking at either function-try-block
26964 or function-transaction-block. Note that all of these include the
26966 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26967 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26968 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26969 RID_TRANSACTION_RELAXED
))
26970 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26971 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26972 cp_parser_function_try_block (parser
);
26974 cp_parser_ctor_initializer_opt_and_function_body
26975 (parser
, /*in_function_try_block=*/false);
26977 /* Finish the function. */
26978 fn
= finish_function (inline_p
);
26979 /* Generate code for it, if necessary. */
26980 expand_or_defer_fn (fn
);
26981 /* Restore the saved values. */
26982 parser
->in_unbraced_linkage_specification_p
26983 = saved_in_unbraced_linkage_specification_p
;
26984 parser
->num_template_parameter_lists
26985 = saved_num_template_parameter_lists
;
26986 parser
->in_function_body
= saved_in_function_body
;
26988 parser
->fully_implicit_function_template_p
26989 = fully_implicit_function_template_p
;
26990 parser
->implicit_template_parms
26991 = implicit_template_parms
;
26992 parser
->implicit_template_scope
26993 = implicit_template_scope
;
26995 if (parser
->fully_implicit_function_template_p
)
26996 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
27001 /* Parse a template-declaration body (following argument list). */
27004 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
27005 tree parameter_list
,
27008 tree decl
= NULL_TREE
;
27009 bool friend_p
= false;
27011 /* We just processed one more parameter list. */
27012 ++parser
->num_template_parameter_lists
;
27014 /* Get the deferred access checks from the parameter list. These
27015 will be checked once we know what is being declared, as for a
27016 member template the checks must be performed in the scope of the
27017 class containing the member. */
27018 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
27020 /* Tentatively parse for a new template parameter list, which can either be
27021 the template keyword or a template introduction. */
27022 if (cp_parser_template_declaration_after_export (parser
, member_p
))
27024 else if (cxx_dialect
>= cxx11
27025 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27026 decl
= cp_parser_alias_declaration (parser
);
27029 /* There are no access checks when parsing a template, as we do not
27030 know if a specialization will be a friend. */
27031 push_deferring_access_checks (dk_no_check
);
27032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27033 decl
= cp_parser_single_declaration (parser
,
27036 /*explicit_specialization_p=*/false,
27038 pop_deferring_access_checks ();
27040 /* If this is a member template declaration, let the front
27042 if (member_p
&& !friend_p
&& decl
)
27044 if (TREE_CODE (decl
) == TYPE_DECL
)
27045 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27047 decl
= finish_member_template_decl (decl
);
27049 else if (friend_p
&& decl
27050 && DECL_DECLARES_TYPE_P (decl
))
27051 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27052 /*complain=*/true);
27054 /* We are done with the current parameter list. */
27055 --parser
->num_template_parameter_lists
;
27057 pop_deferring_access_checks ();
27060 finish_template_decl (parameter_list
);
27062 /* Check the template arguments for a literal operator template. */
27064 && DECL_DECLARES_FUNCTION_P (decl
)
27065 && UDLIT_OPER_P (DECL_NAME (decl
)))
27068 if (parameter_list
== NULL_TREE
)
27072 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27073 if (num_parms
== 1)
27075 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27076 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27077 if (TREE_TYPE (parm
) != char_type_node
27078 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27081 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27083 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27084 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27085 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27086 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27087 if (parm
== error_mark_node
27088 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27089 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27097 if (cxx_dialect
>= cxx14
)
27098 error ("literal operator template %qD has invalid parameter list."
27099 " Expected non-type template argument pack <char...>"
27100 " or <typename CharT, CharT...>",
27103 error ("literal operator template %qD has invalid parameter list."
27104 " Expected non-type template argument pack <char...>",
27109 /* Register member declarations. */
27110 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27111 finish_member_declaration (decl
);
27112 /* If DECL is a function template, we must return to parse it later.
27113 (Even though there is no definition, there might be default
27114 arguments that need handling.) */
27115 if (member_p
&& decl
27116 && DECL_DECLARES_FUNCTION_P (decl
))
27117 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27120 /* Parse a template introduction header for a template-declaration. Returns
27121 false if tentative parse fails. */
27124 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27126 cp_parser_parse_tentatively (parser
);
27128 tree saved_scope
= parser
->scope
;
27129 tree saved_object_scope
= parser
->object_scope
;
27130 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27132 /* Look for the optional `::' operator. */
27133 cp_parser_global_scope_opt (parser
,
27134 /*current_scope_valid_p=*/false);
27135 /* Look for the nested-name-specifier. */
27136 cp_parser_nested_name_specifier_opt (parser
,
27137 /*typename_keyword_p=*/false,
27138 /*check_dependency_p=*/true,
27140 /*is_declaration=*/false);
27142 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27143 tree concept_name
= cp_parser_identifier (parser
);
27145 /* Look up the concept for which we will be matching
27146 template parameters. */
27147 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27149 parser
->scope
= saved_scope
;
27150 parser
->object_scope
= saved_object_scope
;
27151 parser
->qualifying_scope
= saved_qualifying_scope
;
27153 if (concept_name
== error_mark_node
)
27154 cp_parser_simulate_error (parser
);
27156 /* Look for opening brace for introduction. */
27157 matching_braces braces
;
27158 braces
.require_open (parser
);
27160 if (!cp_parser_parse_definitely (parser
))
27163 push_deferring_access_checks (dk_deferred
);
27165 /* Build vector of placeholder parameters and grab
27166 matching identifiers. */
27167 tree introduction_list
= cp_parser_introduction_list (parser
);
27169 /* The introduction-list shall not be empty. */
27170 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27173 error ("empty introduction-list");
27177 /* Look for closing brace for introduction. */
27178 if (!braces
.require_close (parser
))
27181 if (tmpl_decl
== error_mark_node
)
27183 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27188 /* Build and associate the constraint. */
27189 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27190 if (parms
&& parms
!= error_mark_node
)
27192 cp_parser_template_declaration_after_parameters (parser
, parms
,
27197 error_at (token
->location
, "no matching concept for template-introduction");
27201 /* Parse a normal template-declaration following the template keyword. */
27204 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27206 tree parameter_list
;
27207 bool need_lang_pop
;
27208 location_t location
= input_location
;
27210 /* Look for the `<' token. */
27211 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27213 if (at_class_scope_p () && current_function_decl
)
27215 /* 14.5.2.2 [temp.mem]
27217 A local class shall not have member templates. */
27218 error_at (location
,
27219 "invalid declaration of member template in local class");
27220 cp_parser_skip_to_end_of_block_or_statement (parser
);
27225 A template ... shall not have C linkage. */
27226 if (current_lang_name
== lang_name_c
)
27228 error_at (location
, "template with C linkage");
27229 maybe_show_extern_c_location ();
27230 /* Give it C++ linkage to avoid confusing other parts of the
27232 push_lang_context (lang_name_cplusplus
);
27233 need_lang_pop
= true;
27236 need_lang_pop
= false;
27238 /* We cannot perform access checks on the template parameter
27239 declarations until we know what is being declared, just as we
27240 cannot check the decl-specifier list. */
27241 push_deferring_access_checks (dk_deferred
);
27243 /* If the next token is `>', then we have an invalid
27244 specialization. Rather than complain about an invalid template
27245 parameter, issue an error message here. */
27246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27248 cp_parser_error (parser
, "invalid explicit specialization");
27249 begin_specialization ();
27250 parameter_list
= NULL_TREE
;
27254 /* Parse the template parameters. */
27255 parameter_list
= cp_parser_template_parameter_list (parser
);
27258 /* Look for the `>'. */
27259 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27261 /* Manage template requirements */
27264 tree reqs
= get_shorthand_constraints (current_template_parms
);
27265 if (tree r
= cp_parser_requires_clause_opt (parser
))
27266 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27267 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27270 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27273 /* For the erroneous case of a template with C linkage, we pushed an
27274 implicit C++ linkage scope; exit that scope now. */
27276 pop_lang_context ();
27279 /* Parse a template-declaration, assuming that the `export' (and
27280 `extern') keywords, if present, has already been scanned. MEMBER_P
27281 is as for cp_parser_template_declaration. */
27284 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27286 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27288 cp_lexer_consume_token (parser
->lexer
);
27289 cp_parser_explicit_template_declaration (parser
, member_p
);
27292 else if (flag_concepts
)
27293 return cp_parser_template_introduction (parser
, member_p
);
27298 /* Perform the deferred access checks from a template-parameter-list.
27299 CHECKS is a TREE_LIST of access checks, as returned by
27300 get_deferred_access_checks. */
27303 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27305 ++processing_template_parmlist
;
27306 perform_access_checks (checks
, tf_warning_or_error
);
27307 --processing_template_parmlist
;
27310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27311 `function-definition' sequence that follows a template header.
27312 If MEMBER_P is true, this declaration appears in a class scope.
27314 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27315 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27318 cp_parser_single_declaration (cp_parser
* parser
,
27319 vec
<deferred_access_check
, va_gc
> *checks
,
27321 bool explicit_specialization_p
,
27324 int declares_class_or_enum
;
27325 tree decl
= NULL_TREE
;
27326 cp_decl_specifier_seq decl_specifiers
;
27327 bool function_definition_p
= false;
27328 cp_token
*decl_spec_token_start
;
27330 /* This function is only used when processing a template
27332 gcc_assert (innermost_scope_kind () == sk_template_parms
27333 || innermost_scope_kind () == sk_template_spec
);
27335 /* Defer access checks until we know what is being declared. */
27336 push_deferring_access_checks (dk_deferred
);
27338 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27340 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27341 cp_parser_decl_specifier_seq (parser
,
27342 CP_PARSER_FLAGS_OPTIONAL
,
27344 &declares_class_or_enum
);
27346 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27348 /* There are no template typedefs. */
27349 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27351 error_at (decl_spec_token_start
->location
,
27352 "template declaration of %<typedef%>");
27353 decl
= error_mark_node
;
27356 /* Gather up the access checks that occurred the
27357 decl-specifier-seq. */
27358 stop_deferring_access_checks ();
27360 /* Check for the declaration of a template class. */
27361 if (declares_class_or_enum
)
27363 if (cp_parser_declares_only_class_p (parser
)
27364 || (declares_class_or_enum
& 2))
27366 // If this is a declaration, but not a definition, associate
27367 // any constraints with the type declaration. Constraints
27368 // are associated with definitions in cp_parser_class_specifier.
27369 if (declares_class_or_enum
== 1)
27370 associate_classtype_constraints (decl_specifiers
.type
);
27372 decl
= shadow_tag (&decl_specifiers
);
27377 friend template <typename T> struct A<T>::B;
27380 A<T>::B will be represented by a TYPENAME_TYPE, and
27381 therefore not recognized by shadow_tag. */
27382 if (friend_p
&& *friend_p
27384 && decl_specifiers
.type
27385 && TYPE_P (decl_specifiers
.type
))
27386 decl
= decl_specifiers
.type
;
27388 if (decl
&& decl
!= error_mark_node
)
27389 decl
= TYPE_NAME (decl
);
27391 decl
= error_mark_node
;
27393 /* Perform access checks for template parameters. */
27394 cp_parser_perform_template_parameter_access_checks (checks
);
27396 /* Give a helpful diagnostic for
27397 template <class T> struct A { } a;
27398 if we aren't already recovering from an error. */
27399 if (!cp_parser_declares_only_class_p (parser
)
27402 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27403 "a class template declaration must not declare "
27405 cp_parser_skip_to_end_of_block_or_statement (parser
);
27411 /* Complain about missing 'typename' or other invalid type names. */
27412 if (!decl_specifiers
.any_type_specifiers_p
27413 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27415 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27416 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27417 the rest of this declaration. */
27418 decl
= error_mark_node
;
27422 /* If it's not a template class, try for a template function. If
27423 the next token is a `;', then this declaration does not declare
27424 anything. But, if there were errors in the decl-specifiers, then
27425 the error might well have come from an attempted class-specifier.
27426 In that case, there's no need to warn about a missing declarator. */
27428 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27429 || decl_specifiers
.type
!= error_mark_node
))
27431 decl
= cp_parser_init_declarator (parser
,
27434 /*function_definition_allowed_p=*/true,
27436 declares_class_or_enum
,
27437 &function_definition_p
,
27440 /* 7.1.1-1 [dcl.stc]
27442 A storage-class-specifier shall not be specified in an explicit
27443 specialization... */
27445 && explicit_specialization_p
27446 && decl_specifiers
.storage_class
!= sc_none
)
27448 error_at (decl_spec_token_start
->location
,
27449 "explicit template specialization cannot have a storage class");
27450 decl
= error_mark_node
;
27453 if (decl
&& VAR_P (decl
))
27454 check_template_variable (decl
);
27457 /* Look for a trailing `;' after the declaration. */
27458 if (!function_definition_p
27459 && (decl
== error_mark_node
27460 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27461 cp_parser_skip_to_end_of_block_or_statement (parser
);
27464 pop_deferring_access_checks ();
27466 /* Clear any current qualification; whatever comes next is the start
27467 of something new. */
27468 parser
->scope
= NULL_TREE
;
27469 parser
->qualifying_scope
= NULL_TREE
;
27470 parser
->object_scope
= NULL_TREE
;
27475 /* Parse a cast-expression that is not the operand of a unary "&". */
27478 cp_parser_simple_cast_expression (cp_parser
*parser
)
27480 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27481 /*cast_p=*/false, /*decltype*/false, NULL
);
27484 /* Parse a functional cast to TYPE. Returns an expression
27485 representing the cast. */
27488 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27490 vec
<tree
, va_gc
> *vec
;
27491 tree expression_list
;
27495 location_t start_loc
= input_location
;
27498 type
= error_mark_node
;
27500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27502 cp_lexer_set_source_position (parser
->lexer
);
27503 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27504 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27505 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27506 if (TREE_CODE (type
) == TYPE_DECL
)
27507 type
= TREE_TYPE (type
);
27509 cast
= finish_compound_literal (type
, expression_list
,
27510 tf_warning_or_error
, fcl_functional
);
27511 /* Create a location of the form:
27514 with caret == start at the start of the type name,
27515 finishing at the closing brace. */
27516 location_t finish_loc
27517 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27518 location_t combined_loc
= make_location (start_loc
, start_loc
,
27520 cast
.set_location (combined_loc
);
27525 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27527 /*allow_expansion_p=*/true,
27528 /*non_constant_p=*/NULL
);
27530 expression_list
= error_mark_node
;
27533 expression_list
= build_tree_list_vec (vec
);
27534 release_tree_vector (vec
);
27537 cast
= build_functional_cast (type
, expression_list
,
27538 tf_warning_or_error
);
27539 /* [expr.const]/1: In an integral constant expression "only type
27540 conversions to integral or enumeration type can be used". */
27541 if (TREE_CODE (type
) == TYPE_DECL
)
27542 type
= TREE_TYPE (type
);
27543 if (cast
!= error_mark_node
27544 && !cast_valid_in_integral_constant_expression_p (type
)
27545 && cp_parser_non_integral_constant_expression (parser
,
27547 return error_mark_node
;
27549 /* Create a location of the form:
27552 with caret == start at the start of the type name,
27553 finishing at the closing paren. */
27554 location_t finish_loc
27555 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27556 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27557 cast
.set_location (combined_loc
);
27561 /* Save the tokens that make up the body of a member function defined
27562 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27563 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27564 specifiers applied to the declaration. Returns the FUNCTION_DECL
27565 for the member function. */
27568 cp_parser_save_member_function_body (cp_parser
* parser
,
27569 cp_decl_specifier_seq
*decl_specifiers
,
27570 cp_declarator
*declarator
,
27576 bool function_try_block
= false;
27578 /* Create the FUNCTION_DECL. */
27579 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27580 cp_finalize_omp_declare_simd (parser
, fn
);
27581 cp_finalize_oacc_routine (parser
, fn
, true);
27582 /* If something went badly wrong, bail out now. */
27583 if (fn
== error_mark_node
)
27585 /* If there's a function-body, skip it. */
27586 if (cp_parser_token_starts_function_definition_p
27587 (cp_lexer_peek_token (parser
->lexer
)))
27588 cp_parser_skip_to_end_of_block_or_statement (parser
);
27589 return error_mark_node
;
27592 /* Remember it, if there default args to post process. */
27593 cp_parser_save_default_args (parser
, fn
);
27595 /* Save away the tokens that make up the body of the
27597 first
= parser
->lexer
->next_token
;
27599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27600 cp_lexer_consume_token (parser
->lexer
);
27601 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27602 RID_TRANSACTION_ATOMIC
))
27604 cp_lexer_consume_token (parser
->lexer
);
27605 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27607 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27608 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27609 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27610 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27611 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27613 cp_lexer_consume_token (parser
->lexer
);
27614 cp_lexer_consume_token (parser
->lexer
);
27615 cp_lexer_consume_token (parser
->lexer
);
27616 cp_lexer_consume_token (parser
->lexer
);
27617 cp_lexer_consume_token (parser
->lexer
);
27620 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27621 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27623 cp_lexer_consume_token (parser
->lexer
);
27624 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27629 /* Handle function try blocks. */
27630 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27632 cp_lexer_consume_token (parser
->lexer
);
27633 function_try_block
= true;
27635 /* We can have braced-init-list mem-initializers before the fn body. */
27636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27638 cp_lexer_consume_token (parser
->lexer
);
27639 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27641 /* cache_group will stop after an un-nested { } pair, too. */
27642 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27645 /* variadic mem-inits have ... after the ')'. */
27646 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27647 cp_lexer_consume_token (parser
->lexer
);
27650 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27651 /* Handle function try blocks. */
27652 if (function_try_block
)
27653 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27654 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27655 last
= parser
->lexer
->next_token
;
27657 /* Save away the inline definition; we will process it when the
27658 class is complete. */
27659 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27660 DECL_PENDING_INLINE_P (fn
) = 1;
27662 /* We need to know that this was defined in the class, so that
27663 friend templates are handled correctly. */
27664 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27666 /* Add FN to the queue of functions to be parsed later. */
27667 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27672 /* Save the tokens that make up the in-class initializer for a non-static
27673 data member. Returns a DEFAULT_ARG. */
27676 cp_parser_save_nsdmi (cp_parser
* parser
)
27678 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27681 /* Parse a template-argument-list, as well as the trailing ">" (but
27682 not the opening "<"). See cp_parser_template_argument_list for the
27686 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27690 tree saved_qualifying_scope
;
27691 tree saved_object_scope
;
27692 bool saved_greater_than_is_operator_p
;
27693 int saved_unevaluated_operand
;
27694 int saved_inhibit_evaluation_warnings
;
27698 When parsing a template-id, the first non-nested `>' is taken as
27699 the end of the template-argument-list rather than a greater-than
27701 saved_greater_than_is_operator_p
27702 = parser
->greater_than_is_operator_p
;
27703 parser
->greater_than_is_operator_p
= false;
27704 /* Parsing the argument list may modify SCOPE, so we save it
27706 saved_scope
= parser
->scope
;
27707 saved_qualifying_scope
= parser
->qualifying_scope
;
27708 saved_object_scope
= parser
->object_scope
;
27709 /* We need to evaluate the template arguments, even though this
27710 template-id may be nested within a "sizeof". */
27711 saved_unevaluated_operand
= cp_unevaluated_operand
;
27712 cp_unevaluated_operand
= 0;
27713 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27714 c_inhibit_evaluation_warnings
= 0;
27715 /* Parse the template-argument-list itself. */
27716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27717 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27718 arguments
= NULL_TREE
;
27720 arguments
= cp_parser_template_argument_list (parser
);
27721 /* Look for the `>' that ends the template-argument-list. If we find
27722 a '>>' instead, it's probably just a typo. */
27723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27725 if (cxx_dialect
!= cxx98
)
27727 /* In C++0x, a `>>' in a template argument list or cast
27728 expression is considered to be two separate `>'
27729 tokens. So, change the current token to a `>', but don't
27730 consume it: it will be consumed later when the outer
27731 template argument list (or cast expression) is parsed.
27732 Note that this replacement of `>' for `>>' is necessary
27733 even if we are parsing tentatively: in the tentative
27734 case, after calling
27735 cp_parser_enclosed_template_argument_list we will always
27736 throw away all of the template arguments and the first
27737 closing `>', either because the template argument list
27738 was erroneous or because we are replacing those tokens
27739 with a CPP_TEMPLATE_ID token. The second `>' (which will
27740 not have been thrown away) is needed either to close an
27741 outer template argument list or to complete a new-style
27743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27744 token
->type
= CPP_GREATER
;
27746 else if (!saved_greater_than_is_operator_p
)
27748 /* If we're in a nested template argument list, the '>>' has
27749 to be a typo for '> >'. We emit the error message, but we
27750 continue parsing and we push a '>' as next token, so that
27751 the argument list will be parsed correctly. Note that the
27752 global source location is still on the token before the
27753 '>>', so we need to say explicitly where we want it. */
27754 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27755 gcc_rich_location
richloc (token
->location
);
27756 richloc
.add_fixit_replace ("> >");
27757 error_at (&richloc
, "%<>>%> should be %<> >%> "
27758 "within a nested template argument list");
27760 token
->type
= CPP_GREATER
;
27764 /* If this is not a nested template argument list, the '>>'
27765 is a typo for '>'. Emit an error message and continue.
27766 Same deal about the token location, but here we can get it
27767 right by consuming the '>>' before issuing the diagnostic. */
27768 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27769 error_at (token
->location
,
27770 "spurious %<>>%>, use %<>%> to terminate "
27771 "a template argument list");
27775 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27776 /* The `>' token might be a greater-than operator again now. */
27777 parser
->greater_than_is_operator_p
27778 = saved_greater_than_is_operator_p
;
27779 /* Restore the SAVED_SCOPE. */
27780 parser
->scope
= saved_scope
;
27781 parser
->qualifying_scope
= saved_qualifying_scope
;
27782 parser
->object_scope
= saved_object_scope
;
27783 cp_unevaluated_operand
= saved_unevaluated_operand
;
27784 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27789 /* MEMBER_FUNCTION is a member function, or a friend. If default
27790 arguments, or the body of the function have not yet been parsed,
27794 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27796 timevar_push (TV_PARSE_INMETH
);
27797 /* If this member is a template, get the underlying
27799 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27800 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27802 /* There should not be any class definitions in progress at this
27803 point; the bodies of members are only parsed outside of all class
27805 gcc_assert (parser
->num_classes_being_defined
== 0);
27806 /* While we're parsing the member functions we might encounter more
27807 classes. We want to handle them right away, but we don't want
27808 them getting mixed up with functions that are currently in the
27810 push_unparsed_function_queues (parser
);
27812 /* Make sure that any template parameters are in scope. */
27813 maybe_begin_member_template_processing (member_function
);
27815 /* If the body of the function has not yet been parsed, parse it
27817 if (DECL_PENDING_INLINE_P (member_function
))
27819 tree function_scope
;
27820 cp_token_cache
*tokens
;
27822 /* The function is no longer pending; we are processing it. */
27823 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27824 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27825 DECL_PENDING_INLINE_P (member_function
) = 0;
27827 /* If this is a local class, enter the scope of the containing
27829 function_scope
= current_function_decl
;
27830 if (function_scope
)
27831 push_function_context ();
27833 /* Push the body of the function onto the lexer stack. */
27834 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27836 /* Let the front end know that we going to be defining this
27838 start_preparsed_function (member_function
, NULL_TREE
,
27839 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27841 /* Don't do access checking if it is a templated function. */
27842 if (processing_template_decl
)
27843 push_deferring_access_checks (dk_no_check
);
27845 /* #pragma omp declare reduction needs special parsing. */
27846 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27848 parser
->lexer
->in_pragma
= true;
27849 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27850 finish_function (/*inline_p=*/true);
27851 cp_check_omp_declare_reduction (member_function
);
27854 /* Now, parse the body of the function. */
27855 cp_parser_function_definition_after_declarator (parser
,
27856 /*inline_p=*/true);
27858 if (processing_template_decl
)
27859 pop_deferring_access_checks ();
27861 /* Leave the scope of the containing function. */
27862 if (function_scope
)
27863 pop_function_context ();
27864 cp_parser_pop_lexer (parser
);
27867 /* Remove any template parameters from the symbol table. */
27868 maybe_end_member_template_processing ();
27870 /* Restore the queue. */
27871 pop_unparsed_function_queues (parser
);
27872 timevar_pop (TV_PARSE_INMETH
);
27875 /* If DECL contains any default args, remember it on the unparsed
27876 functions queue. */
27879 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27883 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27885 probe
= TREE_CHAIN (probe
))
27886 if (TREE_PURPOSE (probe
))
27888 cp_default_arg_entry entry
= {current_class_type
, decl
};
27889 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27894 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27895 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27896 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27897 from the parameter-type-list. */
27900 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27901 tree default_arg
, tree parmtype
)
27903 cp_token_cache
*tokens
;
27907 if (default_arg
== error_mark_node
)
27908 return error_mark_node
;
27910 /* Push the saved tokens for the default argument onto the parser's
27912 tokens
= DEFARG_TOKENS (default_arg
);
27913 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27915 start_lambda_scope (decl
);
27917 /* Parse the default argument. */
27918 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27919 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27920 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27922 finish_lambda_scope ();
27924 if (parsed_arg
== error_mark_node
)
27925 cp_parser_skip_to_end_of_statement (parser
);
27927 if (!processing_template_decl
)
27929 /* In a non-template class, check conversions now. In a template,
27930 we'll wait and instantiate these as needed. */
27931 if (TREE_CODE (decl
) == PARM_DECL
)
27932 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27933 tf_warning_or_error
);
27934 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27935 parsed_arg
= error_mark_node
;
27937 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27940 /* If the token stream has not been completely used up, then
27941 there was extra junk after the end of the default
27943 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27945 if (TREE_CODE (decl
) == PARM_DECL
)
27946 cp_parser_error (parser
, "expected %<,%>");
27948 cp_parser_error (parser
, "expected %<;%>");
27951 /* Revert to the main lexer. */
27952 cp_parser_pop_lexer (parser
);
27957 /* FIELD is a non-static data member with an initializer which we saved for
27958 later; parse it now. */
27961 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27965 maybe_begin_member_template_processing (field
);
27967 push_unparsed_function_queues (parser
);
27968 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27969 DECL_INITIAL (field
),
27971 pop_unparsed_function_queues (parser
);
27973 maybe_end_member_template_processing ();
27975 DECL_INITIAL (field
) = def
;
27978 /* FN is a FUNCTION_DECL which may contains a parameter with an
27979 unparsed DEFAULT_ARG. Parse the default args now. This function
27980 assumes that the current scope is the scope in which the default
27981 argument should be processed. */
27984 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27986 bool saved_local_variables_forbidden_p
;
27987 tree parm
, parmdecl
;
27989 /* While we're parsing the default args, we might (due to the
27990 statement expression extension) encounter more classes. We want
27991 to handle them right away, but we don't want them getting mixed
27992 up with default args that are currently in the queue. */
27993 push_unparsed_function_queues (parser
);
27995 /* Local variable names (and the `this' keyword) may not appear
27996 in a default argument. */
27997 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27998 parser
->local_variables_forbidden_p
= true;
28000 push_defarg_context (fn
);
28002 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
28003 parmdecl
= DECL_ARGUMENTS (fn
);
28004 parm
&& parm
!= void_list_node
;
28005 parm
= TREE_CHAIN (parm
),
28006 parmdecl
= DECL_CHAIN (parmdecl
))
28008 tree default_arg
= TREE_PURPOSE (parm
);
28010 vec
<tree
, va_gc
> *insts
;
28017 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
28018 /* This can happen for a friend declaration for a function
28019 already declared with default arguments. */
28023 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
28025 TREE_VALUE (parm
));
28026 TREE_PURPOSE (parm
) = parsed_arg
;
28028 /* Update any instantiations we've already created. */
28029 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
28030 vec_safe_iterate (insts
, ix
, ©
); ix
++)
28031 TREE_PURPOSE (copy
) = parsed_arg
;
28034 pop_defarg_context ();
28036 /* Make sure no default arg is missing. */
28037 check_default_args (fn
);
28039 /* Restore the state of local_variables_forbidden_p. */
28040 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28042 /* Restore the queue. */
28043 pop_unparsed_function_queues (parser
);
28046 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28048 sizeof ... ( identifier )
28050 where the 'sizeof' token has already been consumed. */
28053 cp_parser_sizeof_pack (cp_parser
*parser
)
28055 /* Consume the `...'. */
28056 cp_lexer_consume_token (parser
->lexer
);
28057 maybe_warn_variadic_templates ();
28059 matching_parens parens
;
28060 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28062 parens
.consume_open (parser
);
28064 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28065 "%<sizeof...%> argument must be surrounded by parentheses");
28067 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28068 tree name
= cp_parser_identifier (parser
);
28069 if (name
== error_mark_node
)
28070 return error_mark_node
;
28071 /* The name is not qualified. */
28072 parser
->scope
= NULL_TREE
;
28073 parser
->qualifying_scope
= NULL_TREE
;
28074 parser
->object_scope
= NULL_TREE
;
28075 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28076 if (expr
== error_mark_node
)
28077 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28079 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28080 expr
= TREE_TYPE (expr
);
28081 else if (TREE_CODE (expr
) == CONST_DECL
)
28082 expr
= DECL_INITIAL (expr
);
28083 expr
= make_pack_expansion (expr
);
28084 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28087 parens
.require_close (parser
);
28092 /* Parse the operand of `sizeof' (or a similar operator). Returns
28093 either a TYPE or an expression, depending on the form of the
28094 input. The KEYWORD indicates which kind of expression we have
28098 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28100 tree expr
= NULL_TREE
;
28101 const char *saved_message
;
28103 bool saved_integral_constant_expression_p
;
28104 bool saved_non_integral_constant_expression_p
;
28106 /* If it's a `...', then we are computing the length of a parameter
28108 if (keyword
== RID_SIZEOF
28109 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28110 return cp_parser_sizeof_pack (parser
);
28112 /* Types cannot be defined in a `sizeof' expression. Save away the
28114 saved_message
= parser
->type_definition_forbidden_message
;
28115 /* And create the new one. */
28116 tmp
= concat ("types may not be defined in %<",
28117 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28118 "%> expressions", NULL
);
28119 parser
->type_definition_forbidden_message
= tmp
;
28121 /* The restrictions on constant-expressions do not apply inside
28122 sizeof expressions. */
28123 saved_integral_constant_expression_p
28124 = parser
->integral_constant_expression_p
;
28125 saved_non_integral_constant_expression_p
28126 = parser
->non_integral_constant_expression_p
;
28127 parser
->integral_constant_expression_p
= false;
28129 /* Do not actually evaluate the expression. */
28130 ++cp_unevaluated_operand
;
28131 ++c_inhibit_evaluation_warnings
;
28132 /* If it's a `(', then we might be looking at the type-id
28134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28136 tree type
= NULL_TREE
;
28138 /* We can't be sure yet whether we're looking at a type-id or an
28140 cp_parser_parse_tentatively (parser
);
28142 matching_parens parens
;
28143 parens
.consume_open (parser
);
28145 /* Note: as a GNU Extension, compound literals are considered
28146 postfix-expressions as they are in C99, so they are valid
28147 arguments to sizeof. See comment in cp_parser_cast_expression
28149 if (cp_parser_compound_literal_p (parser
))
28150 cp_parser_simulate_error (parser
);
28153 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28154 parser
->in_type_id_in_expr_p
= true;
28155 /* Look for the type-id. */
28156 type
= cp_parser_type_id (parser
);
28157 /* Look for the closing `)'. */
28158 parens
.require_close (parser
);
28159 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28162 /* If all went well, then we're done. */
28163 if (cp_parser_parse_definitely (parser
))
28165 cp_decl_specifier_seq decl_specs
;
28167 /* Build a trivial decl-specifier-seq. */
28168 clear_decl_specs (&decl_specs
);
28169 decl_specs
.type
= type
;
28171 /* Call grokdeclarator to figure out what type this is. */
28172 expr
= grokdeclarator (NULL
,
28176 /*attrlist=*/NULL
);
28180 /* If the type-id production did not work out, then we must be
28181 looking at the unary-expression production. */
28183 expr
= cp_parser_unary_expression (parser
);
28185 /* Go back to evaluating expressions. */
28186 --cp_unevaluated_operand
;
28187 --c_inhibit_evaluation_warnings
;
28189 /* Free the message we created. */
28191 /* And restore the old one. */
28192 parser
->type_definition_forbidden_message
= saved_message
;
28193 parser
->integral_constant_expression_p
28194 = saved_integral_constant_expression_p
;
28195 parser
->non_integral_constant_expression_p
28196 = saved_non_integral_constant_expression_p
;
28201 /* If the current declaration has no declarator, return true. */
28204 cp_parser_declares_only_class_p (cp_parser
*parser
)
28206 /* If the next token is a `;' or a `,' then there is no
28208 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28209 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28212 /* Update the DECL_SPECS to reflect the storage class indicated by
28216 cp_parser_set_storage_class (cp_parser
*parser
,
28217 cp_decl_specifier_seq
*decl_specs
,
28221 cp_storage_class storage_class
;
28223 if (parser
->in_unbraced_linkage_specification_p
)
28225 error_at (token
->location
, "invalid use of %qD in linkage specification",
28226 ridpointers
[keyword
]);
28229 else if (decl_specs
->storage_class
!= sc_none
)
28231 decl_specs
->conflicting_specifiers_p
= true;
28235 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28236 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28237 && decl_specs
->gnu_thread_keyword_p
)
28239 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28240 "%<__thread%> before %qD", ridpointers
[keyword
]);
28246 storage_class
= sc_auto
;
28249 storage_class
= sc_register
;
28252 storage_class
= sc_static
;
28255 storage_class
= sc_extern
;
28258 storage_class
= sc_mutable
;
28261 gcc_unreachable ();
28263 decl_specs
->storage_class
= storage_class
;
28264 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28266 /* A storage class specifier cannot be applied alongside a typedef
28267 specifier. If there is a typedef specifier present then set
28268 conflicting_specifiers_p which will trigger an error later
28269 on in grokdeclarator. */
28270 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28271 decl_specs
->conflicting_specifiers_p
= true;
28274 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28275 is true, the type is a class or enum definition. */
28278 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28281 bool type_definition_p
)
28283 decl_specs
->any_specifiers_p
= true;
28285 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28286 (with, for example, in "typedef int wchar_t;") we remember that
28287 this is what happened. In system headers, we ignore these
28288 declarations so that G++ can work with system headers that are not
28290 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28291 && !type_definition_p
28292 && (type_spec
== boolean_type_node
28293 || type_spec
== char16_type_node
28294 || type_spec
== char32_type_node
28295 || type_spec
== wchar_type_node
)
28296 && (decl_specs
->type
28297 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28298 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28299 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28300 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28302 decl_specs
->redefined_builtin_type
= type_spec
;
28303 set_and_check_decl_spec_loc (decl_specs
,
28304 ds_redefined_builtin_type_spec
,
28306 if (!decl_specs
->type
)
28308 decl_specs
->type
= type_spec
;
28309 decl_specs
->type_definition_p
= false;
28310 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28313 else if (decl_specs
->type
)
28314 decl_specs
->multiple_types_p
= true;
28317 decl_specs
->type
= type_spec
;
28318 decl_specs
->type_definition_p
= type_definition_p
;
28319 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28320 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28324 /* True iff TOKEN is the GNU keyword __thread. */
28327 token_is__thread (cp_token
*token
)
28329 gcc_assert (token
->keyword
== RID_THREAD
);
28330 return id_equal (token
->u
.value
, "__thread");
28333 /* Set the location for a declarator specifier and check if it is
28336 DECL_SPECS is the sequence of declarator specifiers onto which to
28339 DS is the single declarator specifier to set which location is to
28340 be set onto the existing sequence of declarators.
28342 LOCATION is the location for the declarator specifier to
28346 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28347 cp_decl_spec ds
, cp_token
*token
)
28349 gcc_assert (ds
< ds_last
);
28351 if (decl_specs
== NULL
)
28354 source_location location
= token
->location
;
28356 if (decl_specs
->locations
[ds
] == 0)
28358 decl_specs
->locations
[ds
] = location
;
28359 if (ds
== ds_thread
)
28360 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28366 if (decl_specs
->locations
[ds_long_long
] != 0)
28367 error_at (location
,
28368 "%<long long long%> is too long for GCC");
28371 decl_specs
->locations
[ds_long_long
] = location
;
28372 pedwarn_cxx98 (location
,
28374 "ISO C++ 1998 does not support %<long long%>");
28377 else if (ds
== ds_thread
)
28379 bool gnu
= token_is__thread (token
);
28380 gcc_rich_location
richloc (location
);
28381 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28383 richloc
.add_range (decl_specs
->locations
[ds_thread
], false);
28384 error_at (&richloc
,
28385 "both %<__thread%> and %<thread_local%> specified");
28389 richloc
.add_fixit_remove ();
28390 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28395 static const char *const decl_spec_names
[] = {
28412 gcc_rich_location
richloc (location
);
28413 richloc
.add_fixit_remove ();
28414 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28419 /* Return true iff the declarator specifier DS is present in the
28420 sequence of declarator specifiers DECL_SPECS. */
28423 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28426 gcc_assert (ds
< ds_last
);
28428 if (decl_specs
== NULL
)
28431 return decl_specs
->locations
[ds
] != 0;
28434 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28435 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28438 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28440 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28443 /* Issue an error message indicating that TOKEN_DESC was expected.
28444 If KEYWORD is true, it indicated this function is called by
28445 cp_parser_require_keword and the required token can only be
28446 a indicated keyword.
28448 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28449 within any error as the location of an "opening" token matching
28450 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28451 RT_CLOSE_PAREN). */
28454 cp_parser_required_error (cp_parser
*parser
,
28455 required_token token_desc
,
28457 location_t matching_location
)
28459 if (cp_parser_simulate_error (parser
))
28462 const char *gmsgid
= NULL
;
28463 switch (token_desc
)
28466 gmsgid
= G_("expected %<new%>");
28469 gmsgid
= G_("expected %<delete%>");
28472 gmsgid
= G_("expected %<return%>");
28475 gmsgid
= G_("expected %<while%>");
28478 gmsgid
= G_("expected %<extern%>");
28480 case RT_STATIC_ASSERT
:
28481 gmsgid
= G_("expected %<static_assert%>");
28484 gmsgid
= G_("expected %<decltype%>");
28487 gmsgid
= G_("expected %<operator%>");
28490 gmsgid
= G_("expected %<class%>");
28493 gmsgid
= G_("expected %<template%>");
28496 gmsgid
= G_("expected %<namespace%>");
28499 gmsgid
= G_("expected %<using%>");
28502 gmsgid
= G_("expected %<asm%>");
28505 gmsgid
= G_("expected %<try%>");
28508 gmsgid
= G_("expected %<catch%>");
28511 gmsgid
= G_("expected %<throw%>");
28514 gmsgid
= G_("expected %<__label__%>");
28517 gmsgid
= G_("expected %<@try%>");
28519 case RT_AT_SYNCHRONIZED
:
28520 gmsgid
= G_("expected %<@synchronized%>");
28523 gmsgid
= G_("expected %<@throw%>");
28525 case RT_TRANSACTION_ATOMIC
:
28526 gmsgid
= G_("expected %<__transaction_atomic%>");
28528 case RT_TRANSACTION_RELAXED
:
28529 gmsgid
= G_("expected %<__transaction_relaxed%>");
28535 if (!gmsgid
&& !keyword
)
28537 switch (token_desc
)
28540 gmsgid
= G_("expected %<;%>");
28542 case RT_OPEN_PAREN
:
28543 gmsgid
= G_("expected %<(%>");
28545 case RT_CLOSE_BRACE
:
28546 gmsgid
= G_("expected %<}%>");
28548 case RT_OPEN_BRACE
:
28549 gmsgid
= G_("expected %<{%>");
28551 case RT_CLOSE_SQUARE
:
28552 gmsgid
= G_("expected %<]%>");
28554 case RT_OPEN_SQUARE
:
28555 gmsgid
= G_("expected %<[%>");
28558 gmsgid
= G_("expected %<,%>");
28561 gmsgid
= G_("expected %<::%>");
28564 gmsgid
= G_("expected %<<%>");
28567 gmsgid
= G_("expected %<>%>");
28570 gmsgid
= G_("expected %<=%>");
28573 gmsgid
= G_("expected %<...%>");
28576 gmsgid
= G_("expected %<*%>");
28579 gmsgid
= G_("expected %<~%>");
28582 gmsgid
= G_("expected %<:%>");
28584 case RT_COLON_SCOPE
:
28585 gmsgid
= G_("expected %<:%> or %<::%>");
28587 case RT_CLOSE_PAREN
:
28588 gmsgid
= G_("expected %<)%>");
28590 case RT_COMMA_CLOSE_PAREN
:
28591 gmsgid
= G_("expected %<,%> or %<)%>");
28593 case RT_PRAGMA_EOL
:
28594 gmsgid
= G_("expected end of line");
28597 gmsgid
= G_("expected identifier");
28600 gmsgid
= G_("expected selection-statement");
28603 gmsgid
= G_("expected iteration-statement");
28606 gmsgid
= G_("expected jump-statement");
28609 gmsgid
= G_("expected class-key");
28611 case RT_CLASS_TYPENAME_TEMPLATE
:
28612 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28615 gcc_unreachable ();
28620 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28624 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28625 issue an error message indicating that TOKEN_DESC was expected.
28627 Returns the token consumed, if the token had the appropriate type.
28628 Otherwise, returns NULL.
28630 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28631 within any error as the location of an "opening" token matching
28632 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28633 RT_CLOSE_PAREN). */
28636 cp_parser_require (cp_parser
* parser
,
28637 enum cpp_ttype type
,
28638 required_token token_desc
,
28639 location_t matching_location
)
28641 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28642 return cp_lexer_consume_token (parser
->lexer
);
28645 /* Output the MESSAGE -- unless we're parsing tentatively. */
28646 if (!cp_parser_simulate_error (parser
))
28647 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28648 matching_location
);
28653 /* An error message is produced if the next token is not '>'.
28654 All further tokens are skipped until the desired token is
28655 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28658 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28660 /* Current level of '< ... >'. */
28661 unsigned level
= 0;
28662 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28663 unsigned nesting_depth
= 0;
28665 /* Are we ready, yet? If not, issue error message. */
28666 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28669 /* Skip tokens until the desired token is found. */
28672 /* Peek at the next token. */
28673 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28676 if (!nesting_depth
)
28681 if (cxx_dialect
== cxx98
)
28682 /* C++0x views the `>>' operator as two `>' tokens, but
28685 else if (!nesting_depth
&& level
-- == 0)
28687 /* We've hit a `>>' where the first `>' closes the
28688 template argument list, and the second `>' is
28689 spurious. Just consume the `>>' and stop; we've
28690 already produced at least one error. */
28691 cp_lexer_consume_token (parser
->lexer
);
28694 /* Fall through for C++0x, so we handle the second `>' in
28696 gcc_fallthrough ();
28699 if (!nesting_depth
&& level
-- == 0)
28701 /* We've reached the token we want, consume it and stop. */
28702 cp_lexer_consume_token (parser
->lexer
);
28707 case CPP_OPEN_PAREN
:
28708 case CPP_OPEN_SQUARE
:
28712 case CPP_CLOSE_PAREN
:
28713 case CPP_CLOSE_SQUARE
:
28714 if (nesting_depth
-- == 0)
28719 case CPP_PRAGMA_EOL
:
28720 case CPP_SEMICOLON
:
28721 case CPP_OPEN_BRACE
:
28722 case CPP_CLOSE_BRACE
:
28723 /* The '>' was probably forgotten, don't look further. */
28730 /* Consume this token. */
28731 cp_lexer_consume_token (parser
->lexer
);
28735 /* If the next token is the indicated keyword, consume it. Otherwise,
28736 issue an error message indicating that TOKEN_DESC was expected.
28738 Returns the token consumed, if the token had the appropriate type.
28739 Otherwise, returns NULL. */
28742 cp_parser_require_keyword (cp_parser
* parser
,
28744 required_token token_desc
)
28746 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28748 if (token
&& token
->keyword
!= keyword
)
28750 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28758 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28759 function-definition. */
28762 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28764 return (/* An ordinary function-body begins with an `{'. */
28765 token
->type
== CPP_OPEN_BRACE
28766 /* A ctor-initializer begins with a `:'. */
28767 || token
->type
== CPP_COLON
28768 /* A function-try-block begins with `try'. */
28769 || token
->keyword
== RID_TRY
28770 /* A function-transaction-block begins with `__transaction_atomic'
28771 or `__transaction_relaxed'. */
28772 || token
->keyword
== RID_TRANSACTION_ATOMIC
28773 || token
->keyword
== RID_TRANSACTION_RELAXED
28774 /* The named return value extension begins with `return'. */
28775 || token
->keyword
== RID_RETURN
);
28778 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28782 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28786 token
= cp_lexer_peek_token (parser
->lexer
);
28787 return (token
->type
== CPP_OPEN_BRACE
28788 || (token
->type
== CPP_COLON
28789 && !parser
->colon_doesnt_start_class_def_p
));
28792 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28793 C++0x) ending a template-argument. */
28796 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28800 token
= cp_lexer_peek_token (parser
->lexer
);
28801 return (token
->type
== CPP_COMMA
28802 || token
->type
== CPP_GREATER
28803 || token
->type
== CPP_ELLIPSIS
28804 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28807 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28808 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28811 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28816 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28817 if (token
->type
== CPP_LESS
)
28819 /* Check for the sequence `<::' in the original code. It would be lexed as
28820 `[:', where `[' is a digraph, and there is no whitespace before
28822 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28825 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28826 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28832 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28833 or none_type otherwise. */
28835 static enum tag_types
28836 cp_parser_token_is_class_key (cp_token
* token
)
28838 switch (token
->keyword
)
28843 return record_type
;
28852 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28853 or none_type otherwise or if the token is null. */
28855 static enum tag_types
28856 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28861 switch (token
->keyword
)
28866 return typename_type
;
28873 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28876 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28878 if (type
== error_mark_node
)
28880 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28882 if (permerror (input_location
, "%qs tag used in naming %q#T",
28883 class_key
== union_type
? "union"
28884 : class_key
== record_type
? "struct" : "class",
28886 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28887 "%q#T was previously declared here", type
);
28891 /* Issue an error message if DECL is redeclared with different
28892 access than its original declaration [class.access.spec/3].
28893 This applies to nested classes, nested class templates and
28894 enumerations [class.mem/1]. */
28897 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28900 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28901 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28904 if ((TREE_PRIVATE (decl
)
28905 != (current_access_specifier
== access_private_node
))
28906 || (TREE_PROTECTED (decl
)
28907 != (current_access_specifier
== access_protected_node
)))
28908 error_at (location
, "%qD redeclared with different access", decl
);
28911 /* Look for the `template' keyword, as a syntactic disambiguator.
28912 Return TRUE iff it is present, in which case it will be
28916 cp_parser_optional_template_keyword (cp_parser
*parser
)
28918 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28920 /* In C++98 the `template' keyword can only be used within templates;
28921 outside templates the parser can always figure out what is a
28922 template and what is not. In C++11, per the resolution of DR 468,
28923 `template' is allowed in cases where it is not strictly necessary. */
28924 if (!processing_template_decl
28925 && pedantic
&& cxx_dialect
== cxx98
)
28927 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28928 pedwarn (token
->location
, OPT_Wpedantic
,
28929 "in C++98 %<template%> (as a disambiguator) is only "
28930 "allowed within templates");
28931 /* If this part of the token stream is rescanned, the same
28932 error message would be generated. So, we purge the token
28933 from the stream. */
28934 cp_lexer_purge_token (parser
->lexer
);
28939 /* Consume the `template' keyword. */
28940 cp_lexer_consume_token (parser
->lexer
);
28947 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28948 set PARSER->SCOPE, and perform other related actions. */
28951 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28953 struct tree_check
*check_value
;
28955 /* Get the stored value. */
28956 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28957 /* Set the scope from the stored value. */
28958 parser
->scope
= saved_checks_value (check_value
);
28959 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28960 parser
->object_scope
= NULL_TREE
;
28963 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28964 encounter the end of a block before what we were looking for. */
28967 cp_parser_cache_group (cp_parser
*parser
,
28968 enum cpp_ttype end
,
28973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28975 /* Abort a parenthesized expression if we encounter a semicolon. */
28976 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28977 && token
->type
== CPP_SEMICOLON
)
28979 /* If we've reached the end of the file, stop. */
28980 if (token
->type
== CPP_EOF
28981 || (end
!= CPP_PRAGMA_EOL
28982 && token
->type
== CPP_PRAGMA_EOL
))
28984 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28985 /* We've hit the end of an enclosing block, so there's been some
28986 kind of syntax error. */
28989 /* Consume the token. */
28990 cp_lexer_consume_token (parser
->lexer
);
28991 /* See if it starts a new group. */
28992 if (token
->type
== CPP_OPEN_BRACE
)
28994 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28995 /* In theory this should probably check end == '}', but
28996 cp_parser_save_member_function_body needs it to exit
28997 after either '}' or ')' when called with ')'. */
29001 else if (token
->type
== CPP_OPEN_PAREN
)
29003 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
29004 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
29007 else if (token
->type
== CPP_PRAGMA
)
29008 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
29009 else if (token
->type
== end
)
29014 /* Like above, for caching a default argument or NSDMI. Both of these are
29015 terminated by a non-nested comma, but it can be unclear whether or not a
29016 comma is nested in a template argument list unless we do more parsing.
29017 In order to handle this ambiguity, when we encounter a ',' after a '<'
29018 we try to parse what follows as a parameter-declaration-list (in the
29019 case of a default argument) or a member-declarator (in the case of an
29020 NSDMI). If that succeeds, then we stop caching. */
29023 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
29025 unsigned depth
= 0;
29026 int maybe_template_id
= 0;
29027 cp_token
*first_token
;
29029 tree default_argument
;
29031 /* Add tokens until we have processed the entire default
29032 argument. We add the range [first_token, token). */
29033 first_token
= cp_lexer_peek_token (parser
->lexer
);
29034 if (first_token
->type
== CPP_OPEN_BRACE
)
29036 /* For list-initialization, this is straightforward. */
29037 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29038 token
= cp_lexer_peek_token (parser
->lexer
);
29044 /* Peek at the next token. */
29045 token
= cp_lexer_peek_token (parser
->lexer
);
29046 /* What we do depends on what token we have. */
29047 switch (token
->type
)
29049 /* In valid code, a default argument must be
29050 immediately followed by a `,' `)', or `...'. */
29052 if (depth
== 0 && maybe_template_id
)
29054 /* If we've seen a '<', we might be in a
29055 template-argument-list. Until Core issue 325 is
29056 resolved, we don't know how this situation ought
29057 to be handled, so try to DTRT. We check whether
29058 what comes after the comma is a valid parameter
29059 declaration list. If it is, then the comma ends
29060 the default argument; otherwise the default
29061 argument continues. */
29062 bool error
= false;
29065 /* Set ITALP so cp_parser_parameter_declaration_list
29066 doesn't decide to commit to this parse. */
29067 bool saved_italp
= parser
->in_template_argument_list_p
;
29068 parser
->in_template_argument_list_p
= true;
29070 cp_parser_parse_tentatively (parser
);
29074 /* Parse declarators until we reach a non-comma or
29075 somthing that cannot be an initializer.
29076 Just checking whether we're looking at a single
29077 declarator is insufficient. Consider:
29078 int var = tuple<T,U>::x;
29079 The template parameter 'U' looks exactly like a
29083 int ctor_dtor_or_conv_p
;
29084 cp_lexer_consume_token (parser
->lexer
);
29085 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29086 &ctor_dtor_or_conv_p
,
29087 /*parenthesized_p=*/NULL
,
29089 /*friend_p=*/false);
29090 peek
= cp_lexer_peek_token (parser
->lexer
);
29091 if (cp_parser_error_occurred (parser
))
29094 while (peek
->type
== CPP_COMMA
);
29095 /* If we met an '=' or ';' then the original comma
29096 was the end of the NSDMI. Otherwise assume
29097 we're still in the NSDMI. */
29098 error
= (peek
->type
!= CPP_EQ
29099 && peek
->type
!= CPP_SEMICOLON
);
29103 cp_lexer_consume_token (parser
->lexer
);
29104 begin_scope (sk_function_parms
, NULL_TREE
);
29105 if (cp_parser_parameter_declaration_list (parser
)
29106 == error_mark_node
)
29108 pop_bindings_and_leave_scope ();
29110 if (!cp_parser_error_occurred (parser
) && !error
)
29112 cp_parser_abort_tentative_parse (parser
);
29114 parser
->in_template_argument_list_p
= saved_italp
;
29118 case CPP_CLOSE_PAREN
:
29120 /* If we run into a non-nested `;', `}', or `]',
29121 then the code is invalid -- but the default
29122 argument is certainly over. */
29123 case CPP_SEMICOLON
:
29124 case CPP_CLOSE_BRACE
:
29125 case CPP_CLOSE_SQUARE
:
29127 /* Handle correctly int n = sizeof ... ( p ); */
29128 && token
->type
!= CPP_ELLIPSIS
)
29130 /* Update DEPTH, if necessary. */
29131 else if (token
->type
== CPP_CLOSE_PAREN
29132 || token
->type
== CPP_CLOSE_BRACE
29133 || token
->type
== CPP_CLOSE_SQUARE
)
29137 case CPP_OPEN_PAREN
:
29138 case CPP_OPEN_SQUARE
:
29139 case CPP_OPEN_BRACE
:
29145 /* This might be the comparison operator, or it might
29146 start a template argument list. */
29147 ++maybe_template_id
;
29151 if (cxx_dialect
== cxx98
)
29153 /* Fall through for C++0x, which treats the `>>'
29154 operator like two `>' tokens in certain
29156 gcc_fallthrough ();
29161 /* This might be an operator, or it might close a
29162 template argument list. But if a previous '<'
29163 started a template argument list, this will have
29164 closed it, so we can't be in one anymore. */
29165 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29166 if (maybe_template_id
< 0)
29167 maybe_template_id
= 0;
29171 /* If we run out of tokens, issue an error message. */
29173 case CPP_PRAGMA_EOL
:
29174 error_at (token
->location
, "file ends in default argument");
29175 return error_mark_node
;
29179 /* In these cases, we should look for template-ids.
29180 For example, if the default argument is
29181 `X<int, double>()', we need to do name lookup to
29182 figure out whether or not `X' is a template; if
29183 so, the `,' does not end the default argument.
29185 That is not yet done. */
29192 /* If we've reached the end, stop. */
29196 /* Add the token to the token block. */
29197 token
= cp_lexer_consume_token (parser
->lexer
);
29200 /* Create a DEFAULT_ARG to represent the unparsed default
29202 default_argument
= make_node (DEFAULT_ARG
);
29203 DEFARG_TOKENS (default_argument
)
29204 = cp_token_cache_new (first_token
, token
);
29205 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29207 return default_argument
;
29210 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29213 defarg_location (tree default_argument
)
29215 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29216 location_t start
= tokens
->first
->location
;
29217 location_t end
= tokens
->last
->location
;
29218 return make_location (start
, start
, end
);
29221 /* Begin parsing tentatively. We always save tokens while parsing
29222 tentatively so that if the tentative parsing fails we can restore the
29226 cp_parser_parse_tentatively (cp_parser
* parser
)
29228 /* Enter a new parsing context. */
29229 parser
->context
= cp_parser_context_new (parser
->context
);
29230 /* Begin saving tokens. */
29231 cp_lexer_save_tokens (parser
->lexer
);
29232 /* In order to avoid repetitive access control error messages,
29233 access checks are queued up until we are no longer parsing
29235 push_deferring_access_checks (dk_deferred
);
29238 /* Commit to the currently active tentative parse. */
29241 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29243 cp_parser_context
*context
;
29246 /* Mark all of the levels as committed. */
29247 lexer
= parser
->lexer
;
29248 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29250 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29252 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29253 while (!cp_lexer_saving_tokens (lexer
))
29254 lexer
= lexer
->next
;
29255 cp_lexer_commit_tokens (lexer
);
29259 /* Commit to the topmost currently active tentative parse.
29261 Note that this function shouldn't be called when there are
29262 irreversible side-effects while in a tentative state. For
29263 example, we shouldn't create a permanent entry in the symbol
29264 table, or issue an error message that might not apply if the
29265 tentative parse is aborted. */
29268 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29270 cp_parser_context
*context
= parser
->context
;
29271 cp_lexer
*lexer
= parser
->lexer
;
29275 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29277 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29279 while (!cp_lexer_saving_tokens (lexer
))
29280 lexer
= lexer
->next
;
29281 cp_lexer_commit_tokens (lexer
);
29285 /* Abort the currently active tentative parse. All consumed tokens
29286 will be rolled back, and no diagnostics will be issued. */
29289 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29291 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29292 || errorcount
> 0);
29293 cp_parser_simulate_error (parser
);
29294 /* Now, pretend that we want to see if the construct was
29295 successfully parsed. */
29296 cp_parser_parse_definitely (parser
);
29299 /* Stop parsing tentatively. If a parse error has occurred, restore the
29300 token stream. Otherwise, commit to the tokens we have consumed.
29301 Returns true if no error occurred; false otherwise. */
29304 cp_parser_parse_definitely (cp_parser
* parser
)
29306 bool error_occurred
;
29307 cp_parser_context
*context
;
29309 /* Remember whether or not an error occurred, since we are about to
29310 destroy that information. */
29311 error_occurred
= cp_parser_error_occurred (parser
);
29312 /* Remove the topmost context from the stack. */
29313 context
= parser
->context
;
29314 parser
->context
= context
->next
;
29315 /* If no parse errors occurred, commit to the tentative parse. */
29316 if (!error_occurred
)
29318 /* Commit to the tokens read tentatively, unless that was
29320 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29321 cp_lexer_commit_tokens (parser
->lexer
);
29323 pop_to_parent_deferring_access_checks ();
29325 /* Otherwise, if errors occurred, roll back our state so that things
29326 are just as they were before we began the tentative parse. */
29329 cp_lexer_rollback_tokens (parser
->lexer
);
29330 pop_deferring_access_checks ();
29332 /* Add the context to the front of the free list. */
29333 context
->next
= cp_parser_context_free_list
;
29334 cp_parser_context_free_list
= context
;
29336 return !error_occurred
;
29339 /* Returns true if we are parsing tentatively and are not committed to
29340 this tentative parse. */
29343 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29345 return (cp_parser_parsing_tentatively (parser
)
29346 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29349 /* Returns nonzero iff an error has occurred during the most recent
29350 tentative parse. */
29353 cp_parser_error_occurred (cp_parser
* parser
)
29355 return (cp_parser_parsing_tentatively (parser
)
29356 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29359 /* Returns nonzero if GNU extensions are allowed. */
29362 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29364 return parser
->allow_gnu_extensions_p
;
29367 /* Objective-C++ Productions */
29370 /* Parse an Objective-C expression, which feeds into a primary-expression
29374 objc-message-expression
29375 objc-string-literal
29376 objc-encode-expression
29377 objc-protocol-expression
29378 objc-selector-expression
29380 Returns a tree representation of the expression. */
29383 cp_parser_objc_expression (cp_parser
* parser
)
29385 /* Try to figure out what kind of declaration is present. */
29386 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29390 case CPP_OPEN_SQUARE
:
29391 return cp_parser_objc_message_expression (parser
);
29393 case CPP_OBJC_STRING
:
29394 kwd
= cp_lexer_consume_token (parser
->lexer
);
29395 return objc_build_string_object (kwd
->u
.value
);
29398 switch (kwd
->keyword
)
29400 case RID_AT_ENCODE
:
29401 return cp_parser_objc_encode_expression (parser
);
29403 case RID_AT_PROTOCOL
:
29404 return cp_parser_objc_protocol_expression (parser
);
29406 case RID_AT_SELECTOR
:
29407 return cp_parser_objc_selector_expression (parser
);
29414 error_at (kwd
->location
,
29415 "misplaced %<@%D%> Objective-C++ construct",
29417 cp_parser_skip_to_end_of_block_or_statement (parser
);
29420 return error_mark_node
;
29423 /* Parse an Objective-C message expression.
29425 objc-message-expression:
29426 [ objc-message-receiver objc-message-args ]
29428 Returns a representation of an Objective-C message. */
29431 cp_parser_objc_message_expression (cp_parser
* parser
)
29433 tree receiver
, messageargs
;
29435 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29436 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29437 receiver
= cp_parser_objc_message_receiver (parser
);
29438 messageargs
= cp_parser_objc_message_args (parser
);
29439 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29440 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29442 tree result
= objc_build_message_expr (receiver
, messageargs
);
29444 /* Construct a location e.g.
29447 ranging from the '[' to the ']', with the caret at the start. */
29448 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29449 protected_set_expr_location (result
, combined_loc
);
29454 /* Parse an objc-message-receiver.
29456 objc-message-receiver:
29458 simple-type-specifier
29460 Returns a representation of the type or expression. */
29463 cp_parser_objc_message_receiver (cp_parser
* parser
)
29467 /* An Objective-C message receiver may be either (1) a type
29468 or (2) an expression. */
29469 cp_parser_parse_tentatively (parser
);
29470 rcv
= cp_parser_expression (parser
);
29472 /* If that worked out, fine. */
29473 if (cp_parser_parse_definitely (parser
))
29476 cp_parser_parse_tentatively (parser
);
29477 rcv
= cp_parser_simple_type_specifier (parser
,
29478 /*decl_specs=*/NULL
,
29479 CP_PARSER_FLAGS_NONE
);
29481 if (cp_parser_parse_definitely (parser
))
29482 return objc_get_class_reference (rcv
);
29484 cp_parser_error (parser
, "objective-c++ message receiver expected");
29485 return error_mark_node
;
29488 /* Parse the arguments and selectors comprising an Objective-C message.
29493 objc-selector-args , objc-comma-args
29495 objc-selector-args:
29496 objc-selector [opt] : assignment-expression
29497 objc-selector-args objc-selector [opt] : assignment-expression
29500 assignment-expression
29501 objc-comma-args , assignment-expression
29503 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29504 selector arguments and TREE_VALUE containing a list of comma
29508 cp_parser_objc_message_args (cp_parser
* parser
)
29510 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29511 bool maybe_unary_selector_p
= true;
29512 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29514 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29516 tree selector
= NULL_TREE
, arg
;
29518 if (token
->type
!= CPP_COLON
)
29519 selector
= cp_parser_objc_selector (parser
);
29521 /* Detect if we have a unary selector. */
29522 if (maybe_unary_selector_p
29523 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29524 return build_tree_list (selector
, NULL_TREE
);
29526 maybe_unary_selector_p
= false;
29527 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29528 arg
= cp_parser_assignment_expression (parser
);
29531 = chainon (sel_args
,
29532 build_tree_list (selector
, arg
));
29534 token
= cp_lexer_peek_token (parser
->lexer
);
29537 /* Handle non-selector arguments, if any. */
29538 while (token
->type
== CPP_COMMA
)
29542 cp_lexer_consume_token (parser
->lexer
);
29543 arg
= cp_parser_assignment_expression (parser
);
29546 = chainon (addl_args
,
29547 build_tree_list (NULL_TREE
, arg
));
29549 token
= cp_lexer_peek_token (parser
->lexer
);
29552 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29554 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29555 return build_tree_list (error_mark_node
, error_mark_node
);
29558 return build_tree_list (sel_args
, addl_args
);
29561 /* Parse an Objective-C encode expression.
29563 objc-encode-expression:
29564 @encode objc-typename
29566 Returns an encoded representation of the type argument. */
29569 cp_parser_objc_encode_expression (cp_parser
* parser
)
29573 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29575 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29576 matching_parens parens
;
29577 parens
.require_open (parser
);
29578 token
= cp_lexer_peek_token (parser
->lexer
);
29579 type
= complete_type (cp_parser_type_id (parser
));
29580 parens
.require_close (parser
);
29584 error_at (token
->location
,
29585 "%<@encode%> must specify a type as an argument");
29586 return error_mark_node
;
29589 /* This happens if we find @encode(T) (where T is a template
29590 typename or something dependent on a template typename) when
29591 parsing a template. In that case, we can't compile it
29592 immediately, but we rather create an AT_ENCODE_EXPR which will
29593 need to be instantiated when the template is used.
29595 if (dependent_type_p (type
))
29597 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29598 TREE_READONLY (value
) = 1;
29603 /* Build a location of the form:
29606 with caret==start at the @ token, finishing at the close paren. */
29607 location_t combined_loc
29608 = make_location (start_loc
, start_loc
,
29609 cp_lexer_previous_token (parser
->lexer
)->location
);
29611 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29614 /* Parse an Objective-C @defs expression. */
29617 cp_parser_objc_defs_expression (cp_parser
*parser
)
29621 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29622 matching_parens parens
;
29623 parens
.require_open (parser
);
29624 name
= cp_parser_identifier (parser
);
29625 parens
.require_close (parser
);
29627 return objc_get_class_ivars (name
);
29630 /* Parse an Objective-C protocol expression.
29632 objc-protocol-expression:
29633 @protocol ( identifier )
29635 Returns a representation of the protocol expression. */
29638 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29641 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29643 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29644 matching_parens parens
;
29645 parens
.require_open (parser
);
29646 proto
= cp_parser_identifier (parser
);
29647 parens
.require_close (parser
);
29649 /* Build a location of the form:
29652 with caret==start at the @ token, finishing at the close paren. */
29653 location_t combined_loc
29654 = make_location (start_loc
, start_loc
,
29655 cp_lexer_previous_token (parser
->lexer
)->location
);
29656 tree result
= objc_build_protocol_expr (proto
);
29657 protected_set_expr_location (result
, combined_loc
);
29661 /* Parse an Objective-C selector expression.
29663 objc-selector-expression:
29664 @selector ( objc-method-signature )
29666 objc-method-signature:
29672 objc-selector-seq objc-selector :
29674 Returns a representation of the method selector. */
29677 cp_parser_objc_selector_expression (cp_parser
* parser
)
29679 tree sel_seq
= NULL_TREE
;
29680 bool maybe_unary_selector_p
= true;
29682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29684 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29685 matching_parens parens
;
29686 parens
.require_open (parser
);
29687 token
= cp_lexer_peek_token (parser
->lexer
);
29689 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29690 || token
->type
== CPP_SCOPE
)
29692 tree selector
= NULL_TREE
;
29694 if (token
->type
!= CPP_COLON
29695 || token
->type
== CPP_SCOPE
)
29696 selector
= cp_parser_objc_selector (parser
);
29698 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29699 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29701 /* Detect if we have a unary selector. */
29702 if (maybe_unary_selector_p
)
29704 sel_seq
= selector
;
29705 goto finish_selector
;
29709 cp_parser_error (parser
, "expected %<:%>");
29712 maybe_unary_selector_p
= false;
29713 token
= cp_lexer_consume_token (parser
->lexer
);
29715 if (token
->type
== CPP_SCOPE
)
29718 = chainon (sel_seq
,
29719 build_tree_list (selector
, NULL_TREE
));
29721 = chainon (sel_seq
,
29722 build_tree_list (NULL_TREE
, NULL_TREE
));
29726 = chainon (sel_seq
,
29727 build_tree_list (selector
, NULL_TREE
));
29729 token
= cp_lexer_peek_token (parser
->lexer
);
29733 parens
.require_close (parser
);
29736 /* Build a location of the form:
29739 with caret==start at the @ token, finishing at the close paren. */
29740 location_t combined_loc
29741 = make_location (loc
, loc
,
29742 cp_lexer_previous_token (parser
->lexer
)->location
);
29743 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29744 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29745 protected_set_expr_location (result
, combined_loc
);
29749 /* Parse a list of identifiers.
29751 objc-identifier-list:
29753 objc-identifier-list , identifier
29755 Returns a TREE_LIST of identifier nodes. */
29758 cp_parser_objc_identifier_list (cp_parser
* parser
)
29764 identifier
= cp_parser_identifier (parser
);
29765 if (identifier
== error_mark_node
)
29766 return error_mark_node
;
29768 list
= build_tree_list (NULL_TREE
, identifier
);
29769 sep
= cp_lexer_peek_token (parser
->lexer
);
29771 while (sep
->type
== CPP_COMMA
)
29773 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29774 identifier
= cp_parser_identifier (parser
);
29775 if (identifier
== error_mark_node
)
29778 list
= chainon (list
, build_tree_list (NULL_TREE
,
29780 sep
= cp_lexer_peek_token (parser
->lexer
);
29786 /* Parse an Objective-C alias declaration.
29788 objc-alias-declaration:
29789 @compatibility_alias identifier identifier ;
29791 This function registers the alias mapping with the Objective-C front end.
29792 It returns nothing. */
29795 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29799 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29800 alias
= cp_parser_identifier (parser
);
29801 orig
= cp_parser_identifier (parser
);
29802 objc_declare_alias (alias
, orig
);
29803 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29806 /* Parse an Objective-C class forward-declaration.
29808 objc-class-declaration:
29809 @class objc-identifier-list ;
29811 The function registers the forward declarations with the Objective-C
29812 front end. It returns nothing. */
29815 cp_parser_objc_class_declaration (cp_parser
* parser
)
29817 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29822 id
= cp_parser_identifier (parser
);
29823 if (id
== error_mark_node
)
29826 objc_declare_class (id
);
29828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29829 cp_lexer_consume_token (parser
->lexer
);
29833 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29836 /* Parse a list of Objective-C protocol references.
29838 objc-protocol-refs-opt:
29839 objc-protocol-refs [opt]
29841 objc-protocol-refs:
29842 < objc-identifier-list >
29844 Returns a TREE_LIST of identifiers, if any. */
29847 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29849 tree protorefs
= NULL_TREE
;
29851 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29853 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29854 protorefs
= cp_parser_objc_identifier_list (parser
);
29855 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29861 /* Parse a Objective-C visibility specification. */
29864 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29866 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29868 switch (vis
->keyword
)
29870 case RID_AT_PRIVATE
:
29871 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29873 case RID_AT_PROTECTED
:
29874 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29876 case RID_AT_PUBLIC
:
29877 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29879 case RID_AT_PACKAGE
:
29880 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29886 /* Eat '@private'/'@protected'/'@public'. */
29887 cp_lexer_consume_token (parser
->lexer
);
29890 /* Parse an Objective-C method type. Return 'true' if it is a class
29891 (+) method, and 'false' if it is an instance (-) method. */
29894 cp_parser_objc_method_type (cp_parser
* parser
)
29896 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29902 /* Parse an Objective-C protocol qualifier. */
29905 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29907 tree quals
= NULL_TREE
, node
;
29908 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29910 node
= token
->u
.value
;
29912 while (node
&& identifier_p (node
)
29913 && (node
== ridpointers
[(int) RID_IN
]
29914 || node
== ridpointers
[(int) RID_OUT
]
29915 || node
== ridpointers
[(int) RID_INOUT
]
29916 || node
== ridpointers
[(int) RID_BYCOPY
]
29917 || node
== ridpointers
[(int) RID_BYREF
]
29918 || node
== ridpointers
[(int) RID_ONEWAY
]))
29920 quals
= tree_cons (NULL_TREE
, node
, quals
);
29921 cp_lexer_consume_token (parser
->lexer
);
29922 token
= cp_lexer_peek_token (parser
->lexer
);
29923 node
= token
->u
.value
;
29929 /* Parse an Objective-C typename. */
29932 cp_parser_objc_typename (cp_parser
* parser
)
29934 tree type_name
= NULL_TREE
;
29936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29938 tree proto_quals
, cp_type
= NULL_TREE
;
29940 matching_parens parens
;
29941 parens
.consume_open (parser
); /* Eat '('. */
29942 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29944 /* An ObjC type name may consist of just protocol qualifiers, in which
29945 case the type shall default to 'id'. */
29946 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29948 cp_type
= cp_parser_type_id (parser
);
29950 /* If the type could not be parsed, an error has already
29951 been produced. For error recovery, behave as if it had
29952 not been specified, which will use the default type
29954 if (cp_type
== error_mark_node
)
29956 cp_type
= NULL_TREE
;
29957 /* We need to skip to the closing parenthesis as
29958 cp_parser_type_id() does not seem to do it for
29960 cp_parser_skip_to_closing_parenthesis (parser
,
29961 /*recovering=*/true,
29962 /*or_comma=*/false,
29963 /*consume_paren=*/false);
29967 parens
.require_close (parser
);
29968 type_name
= build_tree_list (proto_quals
, cp_type
);
29974 /* Check to see if TYPE refers to an Objective-C selector name. */
29977 cp_parser_objc_selector_p (enum cpp_ttype type
)
29979 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29980 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29981 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29982 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29983 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29986 /* Parse an Objective-C selector. */
29989 cp_parser_objc_selector (cp_parser
* parser
)
29991 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29993 if (!cp_parser_objc_selector_p (token
->type
))
29995 error_at (token
->location
, "invalid Objective-C++ selector name");
29996 return error_mark_node
;
29999 /* C++ operator names are allowed to appear in ObjC selectors. */
30000 switch (token
->type
)
30002 case CPP_AND_AND
: return get_identifier ("and");
30003 case CPP_AND_EQ
: return get_identifier ("and_eq");
30004 case CPP_AND
: return get_identifier ("bitand");
30005 case CPP_OR
: return get_identifier ("bitor");
30006 case CPP_COMPL
: return get_identifier ("compl");
30007 case CPP_NOT
: return get_identifier ("not");
30008 case CPP_NOT_EQ
: return get_identifier ("not_eq");
30009 case CPP_OR_OR
: return get_identifier ("or");
30010 case CPP_OR_EQ
: return get_identifier ("or_eq");
30011 case CPP_XOR
: return get_identifier ("xor");
30012 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
30013 default: return token
->u
.value
;
30017 /* Parse an Objective-C params list. */
30020 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
30022 tree params
= NULL_TREE
;
30023 bool maybe_unary_selector_p
= true;
30024 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30026 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
30028 tree selector
= NULL_TREE
, type_name
, identifier
;
30029 tree parm_attr
= NULL_TREE
;
30031 if (token
->keyword
== RID_ATTRIBUTE
)
30034 if (token
->type
!= CPP_COLON
)
30035 selector
= cp_parser_objc_selector (parser
);
30037 /* Detect if we have a unary selector. */
30038 if (maybe_unary_selector_p
30039 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30041 params
= selector
; /* Might be followed by attributes. */
30045 maybe_unary_selector_p
= false;
30046 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30048 /* Something went quite wrong. There should be a colon
30049 here, but there is not. Stop parsing parameters. */
30052 type_name
= cp_parser_objc_typename (parser
);
30053 /* New ObjC allows attributes on parameters too. */
30054 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30055 parm_attr
= cp_parser_attributes_opt (parser
);
30056 identifier
= cp_parser_identifier (parser
);
30060 objc_build_keyword_decl (selector
,
30065 token
= cp_lexer_peek_token (parser
->lexer
);
30068 if (params
== NULL_TREE
)
30070 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30071 return error_mark_node
;
30074 /* We allow tail attributes for the method. */
30075 if (token
->keyword
== RID_ATTRIBUTE
)
30077 *attributes
= cp_parser_attributes_opt (parser
);
30078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30079 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30081 cp_parser_error (parser
,
30082 "method attributes must be specified at the end");
30083 return error_mark_node
;
30086 if (params
== NULL_TREE
)
30088 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30089 return error_mark_node
;
30094 /* Parse the non-keyword Objective-C params. */
30097 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30100 tree params
= make_node (TREE_LIST
);
30101 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30102 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30104 while (token
->type
== CPP_COMMA
)
30106 cp_parameter_declarator
*parmdecl
;
30109 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30110 token
= cp_lexer_peek_token (parser
->lexer
);
30112 if (token
->type
== CPP_ELLIPSIS
)
30114 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30116 token
= cp_lexer_peek_token (parser
->lexer
);
30120 /* TODO: parse attributes for tail parameters. */
30121 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30122 parm
= grokdeclarator (parmdecl
->declarator
,
30123 &parmdecl
->decl_specifiers
,
30124 PARM
, /*initialized=*/0,
30125 /*attrlist=*/NULL
);
30127 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30128 token
= cp_lexer_peek_token (parser
->lexer
);
30131 /* We allow tail attributes for the method. */
30132 if (token
->keyword
== RID_ATTRIBUTE
)
30134 if (*attributes
== NULL_TREE
)
30136 *attributes
= cp_parser_attributes_opt (parser
);
30137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30138 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30142 /* We have an error, but parse the attributes, so that we can
30144 *attributes
= cp_parser_attributes_opt (parser
);
30146 cp_parser_error (parser
,
30147 "method attributes must be specified at the end");
30148 return error_mark_node
;
30154 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30157 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30159 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30161 /* If the next token is `extern' and the following token is a string
30162 literal, then we have a linkage specification. */
30163 if (token
->keyword
== RID_EXTERN
30164 && cp_parser_is_pure_string_literal
30165 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30166 cp_parser_linkage_specification (parser
);
30167 /* Handle #pragma, if any. */
30168 else if (token
->type
== CPP_PRAGMA
)
30169 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30170 /* Allow stray semicolons. */
30171 else if (token
->type
== CPP_SEMICOLON
)
30172 cp_lexer_consume_token (parser
->lexer
);
30173 /* Mark methods as optional or required, when building protocols. */
30174 else if (token
->keyword
== RID_AT_OPTIONAL
)
30176 cp_lexer_consume_token (parser
->lexer
);
30177 objc_set_method_opt (true);
30179 else if (token
->keyword
== RID_AT_REQUIRED
)
30181 cp_lexer_consume_token (parser
->lexer
);
30182 objc_set_method_opt (false);
30184 else if (token
->keyword
== RID_NAMESPACE
)
30185 cp_parser_namespace_definition (parser
);
30186 /* Other stray characters must generate errors. */
30187 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30189 cp_lexer_consume_token (parser
->lexer
);
30190 error ("stray %qs between Objective-C++ methods",
30191 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30193 /* Finally, try to parse a block-declaration, or a function-definition. */
30195 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30198 /* Parse a method signature. */
30201 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30203 tree rettype
, kwdparms
, optparms
;
30204 bool ellipsis
= false;
30205 bool is_class_method
;
30207 is_class_method
= cp_parser_objc_method_type (parser
);
30208 rettype
= cp_parser_objc_typename (parser
);
30209 *attributes
= NULL_TREE
;
30210 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30211 if (kwdparms
== error_mark_node
)
30212 return error_mark_node
;
30213 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30214 if (optparms
== error_mark_node
)
30215 return error_mark_node
;
30217 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30221 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30224 cp_lexer_save_tokens (parser
->lexer
);
30225 tattr
= cp_parser_attributes_opt (parser
);
30226 gcc_assert (tattr
) ;
30228 /* If the attributes are followed by a method introducer, this is not allowed.
30229 Dump the attributes and flag the situation. */
30230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30231 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30234 /* Otherwise, the attributes introduce some interstitial code, possibly so
30235 rewind to allow that check. */
30236 cp_lexer_rollback_tokens (parser
->lexer
);
30240 /* Parse an Objective-C method prototype list. */
30243 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30247 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30249 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30251 tree attributes
, sig
;
30252 bool is_class_method
;
30253 if (token
->type
== CPP_PLUS
)
30254 is_class_method
= true;
30256 is_class_method
= false;
30257 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30258 if (sig
== error_mark_node
)
30260 cp_parser_skip_to_end_of_block_or_statement (parser
);
30261 token
= cp_lexer_peek_token (parser
->lexer
);
30264 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30265 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30267 else if (token
->keyword
== RID_AT_PROPERTY
)
30268 cp_parser_objc_at_property_declaration (parser
);
30269 else if (token
->keyword
== RID_ATTRIBUTE
30270 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30271 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30273 "prefix attributes are ignored for methods");
30275 /* Allow for interspersed non-ObjC++ code. */
30276 cp_parser_objc_interstitial_code (parser
);
30278 token
= cp_lexer_peek_token (parser
->lexer
);
30281 if (token
->type
!= CPP_EOF
)
30282 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30284 cp_parser_error (parser
, "expected %<@end%>");
30286 objc_finish_interface ();
30289 /* Parse an Objective-C method definition list. */
30292 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30294 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30296 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30300 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30303 tree sig
, attribute
;
30304 bool is_class_method
;
30305 if (token
->type
== CPP_PLUS
)
30306 is_class_method
= true;
30308 is_class_method
= false;
30309 push_deferring_access_checks (dk_deferred
);
30310 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30311 if (sig
== error_mark_node
)
30313 cp_parser_skip_to_end_of_block_or_statement (parser
);
30314 token
= cp_lexer_peek_token (parser
->lexer
);
30317 objc_start_method_definition (is_class_method
, sig
, attribute
,
30320 /* For historical reasons, we accept an optional semicolon. */
30321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30322 cp_lexer_consume_token (parser
->lexer
);
30324 ptk
= cp_lexer_peek_token (parser
->lexer
);
30325 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30326 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30328 perform_deferred_access_checks (tf_warning_or_error
);
30329 stop_deferring_access_checks ();
30330 meth
= cp_parser_function_definition_after_declarator (parser
,
30332 pop_deferring_access_checks ();
30333 objc_finish_method_definition (meth
);
30336 /* The following case will be removed once @synthesize is
30337 completely implemented. */
30338 else if (token
->keyword
== RID_AT_PROPERTY
)
30339 cp_parser_objc_at_property_declaration (parser
);
30340 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30341 cp_parser_objc_at_synthesize_declaration (parser
);
30342 else if (token
->keyword
== RID_AT_DYNAMIC
)
30343 cp_parser_objc_at_dynamic_declaration (parser
);
30344 else if (token
->keyword
== RID_ATTRIBUTE
30345 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30346 warning_at (token
->location
, OPT_Wattributes
,
30347 "prefix attributes are ignored for methods");
30349 /* Allow for interspersed non-ObjC++ code. */
30350 cp_parser_objc_interstitial_code (parser
);
30352 token
= cp_lexer_peek_token (parser
->lexer
);
30355 if (token
->type
!= CPP_EOF
)
30356 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30358 cp_parser_error (parser
, "expected %<@end%>");
30360 objc_finish_implementation ();
30363 /* Parse Objective-C ivars. */
30366 cp_parser_objc_class_ivars (cp_parser
* parser
)
30368 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30370 if (token
->type
!= CPP_OPEN_BRACE
)
30371 return; /* No ivars specified. */
30373 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30374 token
= cp_lexer_peek_token (parser
->lexer
);
30376 while (token
->type
!= CPP_CLOSE_BRACE
30377 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30379 cp_decl_specifier_seq declspecs
;
30380 int decl_class_or_enum_p
;
30381 tree prefix_attributes
;
30383 cp_parser_objc_visibility_spec (parser
);
30385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30388 cp_parser_decl_specifier_seq (parser
,
30389 CP_PARSER_FLAGS_OPTIONAL
,
30391 &decl_class_or_enum_p
);
30393 /* auto, register, static, extern, mutable. */
30394 if (declspecs
.storage_class
!= sc_none
)
30396 cp_parser_error (parser
, "invalid type for instance variable");
30397 declspecs
.storage_class
= sc_none
;
30400 /* thread_local. */
30401 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30403 cp_parser_error (parser
, "invalid type for instance variable");
30404 declspecs
.locations
[ds_thread
] = 0;
30408 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30410 cp_parser_error (parser
, "invalid type for instance variable");
30411 declspecs
.locations
[ds_typedef
] = 0;
30414 prefix_attributes
= declspecs
.attributes
;
30415 declspecs
.attributes
= NULL_TREE
;
30417 /* Keep going until we hit the `;' at the end of the
30419 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30421 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30422 cp_declarator
*declarator
= NULL
;
30423 int ctor_dtor_or_conv_p
;
30425 /* Check for a (possibly unnamed) bitfield declaration. */
30426 token
= cp_lexer_peek_token (parser
->lexer
);
30427 if (token
->type
== CPP_COLON
)
30430 if (token
->type
== CPP_NAME
30431 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30434 /* Get the name of the bitfield. */
30435 declarator
= make_id_declarator (NULL_TREE
,
30436 cp_parser_identifier (parser
),
30440 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30441 /* Get the width of the bitfield. */
30443 = cp_parser_constant_expression (parser
);
30447 /* Parse the declarator. */
30449 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30450 &ctor_dtor_or_conv_p
,
30451 /*parenthesized_p=*/NULL
,
30452 /*member_p=*/false,
30453 /*friend_p=*/false);
30456 /* Look for attributes that apply to the ivar. */
30457 attributes
= cp_parser_attributes_opt (parser
);
30458 /* Remember which attributes are prefix attributes and
30460 first_attribute
= attributes
;
30461 /* Combine the attributes. */
30462 attributes
= attr_chainon (prefix_attributes
, attributes
);
30465 /* Create the bitfield declaration. */
30466 decl
= grokbitfield (declarator
, &declspecs
,
30467 width
, NULL_TREE
, attributes
);
30469 decl
= grokfield (declarator
, &declspecs
,
30470 NULL_TREE
, /*init_const_expr_p=*/false,
30471 NULL_TREE
, attributes
);
30473 /* Add the instance variable. */
30474 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30475 objc_add_instance_variable (decl
);
30477 /* Reset PREFIX_ATTRIBUTES. */
30478 if (attributes
!= error_mark_node
)
30480 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30481 attributes
= TREE_CHAIN (attributes
);
30483 TREE_CHAIN (attributes
) = NULL_TREE
;
30486 token
= cp_lexer_peek_token (parser
->lexer
);
30488 if (token
->type
== CPP_COMMA
)
30490 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30496 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30497 token
= cp_lexer_peek_token (parser
->lexer
);
30500 if (token
->keyword
== RID_AT_END
)
30501 cp_parser_error (parser
, "expected %<}%>");
30503 /* Do not consume the RID_AT_END, so it will be read again as terminating
30504 the @interface of @implementation. */
30505 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30506 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30508 /* For historical reasons, we accept an optional semicolon. */
30509 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30510 cp_lexer_consume_token (parser
->lexer
);
30513 /* Parse an Objective-C protocol declaration. */
30516 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30518 tree proto
, protorefs
;
30521 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30522 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30524 tok
= cp_lexer_peek_token (parser
->lexer
);
30525 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30526 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30530 /* See if we have a forward declaration or a definition. */
30531 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30533 /* Try a forward declaration first. */
30534 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30540 id
= cp_parser_identifier (parser
);
30541 if (id
== error_mark_node
)
30544 objc_declare_protocol (id
, attributes
);
30546 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30547 cp_lexer_consume_token (parser
->lexer
);
30551 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30554 /* Ok, we got a full-fledged definition (or at least should). */
30557 proto
= cp_parser_identifier (parser
);
30558 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30559 objc_start_protocol (proto
, protorefs
, attributes
);
30560 cp_parser_objc_method_prototype_list (parser
);
30564 /* Parse an Objective-C superclass or category. */
30567 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30570 tree
*categ
, bool *is_class_extension
)
30572 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30574 *super
= *categ
= NULL_TREE
;
30575 *is_class_extension
= false;
30576 if (next
->type
== CPP_COLON
)
30578 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30579 *super
= cp_parser_identifier (parser
);
30581 else if (next
->type
== CPP_OPEN_PAREN
)
30583 matching_parens parens
;
30584 parens
.consume_open (parser
); /* Eat '('. */
30586 /* If there is no category name, and this is an @interface, we
30587 have a class extension. */
30588 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30590 *categ
= NULL_TREE
;
30591 *is_class_extension
= true;
30594 *categ
= cp_parser_identifier (parser
);
30596 parens
.require_close (parser
);
30600 /* Parse an Objective-C class interface. */
30603 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30605 tree name
, super
, categ
, protos
;
30606 bool is_class_extension
;
30608 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30609 name
= cp_parser_identifier (parser
);
30610 if (name
== error_mark_node
)
30612 /* It's hard to recover because even if valid @interface stuff
30613 is to follow, we can't compile it (or validate it) if we
30614 don't even know which class it refers to. Let's assume this
30615 was a stray '@interface' token in the stream and skip it.
30619 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30620 &is_class_extension
);
30621 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30623 /* We have either a class or a category on our hands. */
30624 if (categ
|| is_class_extension
)
30625 objc_start_category_interface (name
, categ
, protos
, attributes
);
30628 objc_start_class_interface (name
, super
, protos
, attributes
);
30629 /* Handle instance variable declarations, if any. */
30630 cp_parser_objc_class_ivars (parser
);
30631 objc_continue_interface ();
30634 cp_parser_objc_method_prototype_list (parser
);
30637 /* Parse an Objective-C class implementation. */
30640 cp_parser_objc_class_implementation (cp_parser
* parser
)
30642 tree name
, super
, categ
;
30643 bool is_class_extension
;
30645 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30646 name
= cp_parser_identifier (parser
);
30647 if (name
== error_mark_node
)
30649 /* It's hard to recover because even if valid @implementation
30650 stuff is to follow, we can't compile it (or validate it) if
30651 we don't even know which class it refers to. Let's assume
30652 this was a stray '@implementation' token in the stream and
30657 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30658 &is_class_extension
);
30660 /* We have either a class or a category on our hands. */
30662 objc_start_category_implementation (name
, categ
);
30665 objc_start_class_implementation (name
, super
);
30666 /* Handle instance variable declarations, if any. */
30667 cp_parser_objc_class_ivars (parser
);
30668 objc_continue_implementation ();
30671 cp_parser_objc_method_definition_list (parser
);
30674 /* Consume the @end token and finish off the implementation. */
30677 cp_parser_objc_end_implementation (cp_parser
* parser
)
30679 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30680 objc_finish_implementation ();
30683 /* Parse an Objective-C declaration. */
30686 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30688 /* Try to figure out what kind of declaration is present. */
30689 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30692 switch (kwd
->keyword
)
30697 error_at (kwd
->location
, "attributes may not be specified before"
30698 " the %<@%D%> Objective-C++ keyword",
30702 case RID_AT_IMPLEMENTATION
:
30703 warning_at (kwd
->location
, OPT_Wattributes
,
30704 "prefix attributes are ignored before %<@%D%>",
30711 switch (kwd
->keyword
)
30714 cp_parser_objc_alias_declaration (parser
);
30717 cp_parser_objc_class_declaration (parser
);
30719 case RID_AT_PROTOCOL
:
30720 cp_parser_objc_protocol_declaration (parser
, attributes
);
30722 case RID_AT_INTERFACE
:
30723 cp_parser_objc_class_interface (parser
, attributes
);
30725 case RID_AT_IMPLEMENTATION
:
30726 cp_parser_objc_class_implementation (parser
);
30729 cp_parser_objc_end_implementation (parser
);
30732 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30734 cp_parser_skip_to_end_of_block_or_statement (parser
);
30738 /* Parse an Objective-C try-catch-finally statement.
30740 objc-try-catch-finally-stmt:
30741 @try compound-statement objc-catch-clause-seq [opt]
30742 objc-finally-clause [opt]
30744 objc-catch-clause-seq:
30745 objc-catch-clause objc-catch-clause-seq [opt]
30748 @catch ( objc-exception-declaration ) compound-statement
30750 objc-finally-clause:
30751 @finally compound-statement
30753 objc-exception-declaration:
30754 parameter-declaration
30757 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30761 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30762 for C. Keep them in sync. */
30765 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30767 location_t location
;
30770 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30771 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30772 objc_maybe_warn_exceptions (location
);
30773 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30774 node, lest it get absorbed into the surrounding block. */
30775 stmt
= push_stmt_list ();
30776 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30777 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30779 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30781 cp_parameter_declarator
*parm
;
30782 tree parameter_declaration
= error_mark_node
;
30783 bool seen_open_paren
= false;
30784 matching_parens parens
;
30786 cp_lexer_consume_token (parser
->lexer
);
30787 if (parens
.require_open (parser
))
30788 seen_open_paren
= true;
30789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30791 /* We have "@catch (...)" (where the '...' are literally
30792 what is in the code). Skip the '...'.
30793 parameter_declaration is set to NULL_TREE, and
30794 objc_being_catch_clauses() knows that that means
30796 cp_lexer_consume_token (parser
->lexer
);
30797 parameter_declaration
= NULL_TREE
;
30801 /* We have "@catch (NSException *exception)" or something
30802 like that. Parse the parameter declaration. */
30803 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30805 parameter_declaration
= error_mark_node
;
30807 parameter_declaration
= grokdeclarator (parm
->declarator
,
30808 &parm
->decl_specifiers
,
30809 PARM
, /*initialized=*/0,
30810 /*attrlist=*/NULL
);
30812 if (seen_open_paren
)
30813 parens
.require_close (parser
);
30816 /* If there was no open parenthesis, we are recovering from
30817 an error, and we are trying to figure out what mistake
30818 the user has made. */
30820 /* If there is an immediate closing parenthesis, the user
30821 probably forgot the opening one (ie, they typed "@catch
30822 NSException *e)". Parse the closing parenthesis and keep
30824 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30825 cp_lexer_consume_token (parser
->lexer
);
30827 /* If these is no immediate closing parenthesis, the user
30828 probably doesn't know that parenthesis are required at
30829 all (ie, they typed "@catch NSException *e"). So, just
30830 forget about the closing parenthesis and keep going. */
30832 objc_begin_catch_clause (parameter_declaration
);
30833 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30834 objc_finish_catch_clause ();
30836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30838 cp_lexer_consume_token (parser
->lexer
);
30839 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30840 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30841 node, lest it get absorbed into the surrounding block. */
30842 stmt
= push_stmt_list ();
30843 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30844 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30847 return objc_finish_try_stmt ();
30850 /* Parse an Objective-C synchronized statement.
30852 objc-synchronized-stmt:
30853 @synchronized ( expression ) compound-statement
30855 Returns NULL_TREE. */
30858 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30860 location_t location
;
30863 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30865 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30866 objc_maybe_warn_exceptions (location
);
30867 matching_parens parens
;
30868 parens
.require_open (parser
);
30869 lock
= cp_parser_expression (parser
);
30870 parens
.require_close (parser
);
30872 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30873 node, lest it get absorbed into the surrounding block. */
30874 stmt
= push_stmt_list ();
30875 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30877 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30880 /* Parse an Objective-C throw statement.
30883 @throw assignment-expression [opt] ;
30885 Returns a constructed '@throw' statement. */
30888 cp_parser_objc_throw_statement (cp_parser
*parser
)
30890 tree expr
= NULL_TREE
;
30891 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30893 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30895 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30896 expr
= cp_parser_expression (parser
);
30898 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30900 return objc_build_throw_stmt (loc
, expr
);
30903 /* Parse an Objective-C statement. */
30906 cp_parser_objc_statement (cp_parser
* parser
)
30908 /* Try to figure out what kind of declaration is present. */
30909 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30911 switch (kwd
->keyword
)
30914 return cp_parser_objc_try_catch_finally_statement (parser
);
30915 case RID_AT_SYNCHRONIZED
:
30916 return cp_parser_objc_synchronized_statement (parser
);
30918 return cp_parser_objc_throw_statement (parser
);
30920 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30922 cp_parser_skip_to_end_of_block_or_statement (parser
);
30925 return error_mark_node
;
30928 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30929 look ahead to see if an objc keyword follows the attributes. This
30930 is to detect the use of prefix attributes on ObjC @interface and
30934 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30936 cp_lexer_save_tokens (parser
->lexer
);
30937 *attrib
= cp_parser_attributes_opt (parser
);
30938 gcc_assert (*attrib
);
30939 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30941 cp_lexer_commit_tokens (parser
->lexer
);
30944 cp_lexer_rollback_tokens (parser
->lexer
);
30948 /* This routine is a minimal replacement for
30949 c_parser_struct_declaration () used when parsing the list of
30950 types/names or ObjC++ properties. For example, when parsing the
30953 @property (readonly) int a, b, c;
30955 this function is responsible for parsing "int a, int b, int c" and
30956 returning the declarations as CHAIN of DECLs.
30958 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30959 similar parsing. */
30961 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30963 tree decls
= NULL_TREE
;
30964 cp_decl_specifier_seq declspecs
;
30965 int decl_class_or_enum_p
;
30966 tree prefix_attributes
;
30968 cp_parser_decl_specifier_seq (parser
,
30969 CP_PARSER_FLAGS_NONE
,
30971 &decl_class_or_enum_p
);
30973 if (declspecs
.type
== error_mark_node
)
30974 return error_mark_node
;
30976 /* auto, register, static, extern, mutable. */
30977 if (declspecs
.storage_class
!= sc_none
)
30979 cp_parser_error (parser
, "invalid type for property");
30980 declspecs
.storage_class
= sc_none
;
30983 /* thread_local. */
30984 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30986 cp_parser_error (parser
, "invalid type for property");
30987 declspecs
.locations
[ds_thread
] = 0;
30991 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30993 cp_parser_error (parser
, "invalid type for property");
30994 declspecs
.locations
[ds_typedef
] = 0;
30997 prefix_attributes
= declspecs
.attributes
;
30998 declspecs
.attributes
= NULL_TREE
;
31000 /* Keep going until we hit the `;' at the end of the declaration. */
31001 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
31003 tree attributes
, first_attribute
, decl
;
31004 cp_declarator
*declarator
;
31007 /* Parse the declarator. */
31008 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31009 NULL
, NULL
, false, false);
31011 /* Look for attributes that apply to the ivar. */
31012 attributes
= cp_parser_attributes_opt (parser
);
31013 /* Remember which attributes are prefix attributes and
31015 first_attribute
= attributes
;
31016 /* Combine the attributes. */
31017 attributes
= attr_chainon (prefix_attributes
, attributes
);
31019 decl
= grokfield (declarator
, &declspecs
,
31020 NULL_TREE
, /*init_const_expr_p=*/false,
31021 NULL_TREE
, attributes
);
31023 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
31024 return error_mark_node
;
31026 /* Reset PREFIX_ATTRIBUTES. */
31027 if (attributes
!= error_mark_node
)
31029 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
31030 attributes
= TREE_CHAIN (attributes
);
31032 TREE_CHAIN (attributes
) = NULL_TREE
;
31035 DECL_CHAIN (decl
) = decls
;
31038 token
= cp_lexer_peek_token (parser
->lexer
);
31039 if (token
->type
== CPP_COMMA
)
31041 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31050 /* Parse an Objective-C @property declaration. The syntax is:
31052 objc-property-declaration:
31053 '@property' objc-property-attributes[opt] struct-declaration ;
31055 objc-property-attributes:
31056 '(' objc-property-attribute-list ')'
31058 objc-property-attribute-list:
31059 objc-property-attribute
31060 objc-property-attribute-list, objc-property-attribute
31062 objc-property-attribute
31063 'getter' = identifier
31064 'setter' = identifier
31073 @property NSString *name;
31074 @property (readonly) id object;
31075 @property (retain, nonatomic, getter=getTheName) id name;
31076 @property int a, b, c;
31078 PS: This function is identical to
31079 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31081 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31083 /* The following variables hold the attributes of the properties as
31084 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31085 seen. When we see an attribute, we set them to 'true' (if they
31086 are boolean properties) or to the identifier (if they have an
31087 argument, ie, for getter and setter). Note that here we only
31088 parse the list of attributes, check the syntax and accumulate the
31089 attributes that we find. objc_add_property_declaration() will
31090 then process the information. */
31091 bool property_assign
= false;
31092 bool property_copy
= false;
31093 tree property_getter_ident
= NULL_TREE
;
31094 bool property_nonatomic
= false;
31095 bool property_readonly
= false;
31096 bool property_readwrite
= false;
31097 bool property_retain
= false;
31098 tree property_setter_ident
= NULL_TREE
;
31100 /* 'properties' is the list of properties that we read. Usually a
31101 single one, but maybe more (eg, in "@property int a, b, c;" there
31106 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31108 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31110 /* Parse the optional attribute list... */
31111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31114 matching_parens parens
;
31115 parens
.consume_open (parser
);
31119 bool syntax_error
= false;
31120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31123 if (token
->type
!= CPP_NAME
)
31125 cp_parser_error (parser
, "expected identifier");
31128 keyword
= C_RID_CODE (token
->u
.value
);
31129 cp_lexer_consume_token (parser
->lexer
);
31132 case RID_ASSIGN
: property_assign
= true; break;
31133 case RID_COPY
: property_copy
= true; break;
31134 case RID_NONATOMIC
: property_nonatomic
= true; break;
31135 case RID_READONLY
: property_readonly
= true; break;
31136 case RID_READWRITE
: property_readwrite
= true; break;
31137 case RID_RETAIN
: property_retain
= true; break;
31141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31143 if (keyword
== RID_GETTER
)
31144 cp_parser_error (parser
,
31145 "missing %<=%> (after %<getter%> attribute)");
31147 cp_parser_error (parser
,
31148 "missing %<=%> (after %<setter%> attribute)");
31149 syntax_error
= true;
31152 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31153 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31155 cp_parser_error (parser
, "expected identifier");
31156 syntax_error
= true;
31159 if (keyword
== RID_SETTER
)
31161 if (property_setter_ident
!= NULL_TREE
)
31163 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31164 cp_lexer_consume_token (parser
->lexer
);
31167 property_setter_ident
= cp_parser_objc_selector (parser
);
31168 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31169 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31171 cp_lexer_consume_token (parser
->lexer
);
31175 if (property_getter_ident
!= NULL_TREE
)
31177 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31178 cp_lexer_consume_token (parser
->lexer
);
31181 property_getter_ident
= cp_parser_objc_selector (parser
);
31185 cp_parser_error (parser
, "unknown property attribute");
31186 syntax_error
= true;
31193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31194 cp_lexer_consume_token (parser
->lexer
);
31199 /* FIXME: "@property (setter, assign);" will generate a spurious
31200 "error: expected ‘)’ before ‘,’ token". This is because
31201 cp_parser_require, unlike the C counterpart, will produce an
31202 error even if we are in error recovery. */
31203 if (!parens
.require_close (parser
))
31205 cp_parser_skip_to_closing_parenthesis (parser
,
31206 /*recovering=*/true,
31207 /*or_comma=*/false,
31208 /*consume_paren=*/true);
31212 /* ... and the property declaration(s). */
31213 properties
= cp_parser_objc_struct_declaration (parser
);
31215 if (properties
== error_mark_node
)
31217 cp_parser_skip_to_end_of_statement (parser
);
31218 /* If the next token is now a `;', consume it. */
31219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31220 cp_lexer_consume_token (parser
->lexer
);
31224 if (properties
== NULL_TREE
)
31225 cp_parser_error (parser
, "expected identifier");
31228 /* Comma-separated properties are chained together in
31229 reverse order; add them one by one. */
31230 properties
= nreverse (properties
);
31232 for (; properties
; properties
= TREE_CHAIN (properties
))
31233 objc_add_property_declaration (loc
, copy_node (properties
),
31234 property_readonly
, property_readwrite
,
31235 property_assign
, property_retain
,
31236 property_copy
, property_nonatomic
,
31237 property_getter_ident
, property_setter_ident
);
31240 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31243 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31245 objc-synthesize-declaration:
31246 @synthesize objc-synthesize-identifier-list ;
31248 objc-synthesize-identifier-list:
31249 objc-synthesize-identifier
31250 objc-synthesize-identifier-list, objc-synthesize-identifier
31252 objc-synthesize-identifier
31254 identifier = identifier
31257 @synthesize MyProperty;
31258 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31260 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31261 for C. Keep them in sync.
31264 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31266 tree list
= NULL_TREE
;
31268 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31270 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31273 tree property
, ivar
;
31274 property
= cp_parser_identifier (parser
);
31275 if (property
== error_mark_node
)
31277 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31280 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31282 cp_lexer_consume_token (parser
->lexer
);
31283 ivar
= cp_parser_identifier (parser
);
31284 if (ivar
== error_mark_node
)
31286 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31292 list
= chainon (list
, build_tree_list (ivar
, property
));
31293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31294 cp_lexer_consume_token (parser
->lexer
);
31298 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31299 objc_add_synthesize_declaration (loc
, list
);
31302 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31304 objc-dynamic-declaration:
31305 @dynamic identifier-list ;
31308 @dynamic MyProperty;
31309 @dynamic MyProperty, AnotherProperty;
31311 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31312 for C. Keep them in sync.
31315 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31317 tree list
= NULL_TREE
;
31319 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31321 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31325 property
= cp_parser_identifier (parser
);
31326 if (property
== error_mark_node
)
31328 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31331 list
= chainon (list
, build_tree_list (NULL
, property
));
31332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31333 cp_lexer_consume_token (parser
->lexer
);
31337 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31338 objc_add_dynamic_declaration (loc
, list
);
31342 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31344 /* Returns name of the next clause.
31345 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31346 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31347 returned and the token is consumed. */
31349 static pragma_omp_clause
31350 cp_parser_omp_clause_name (cp_parser
*parser
)
31352 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31354 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31355 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31356 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31357 result
= PRAGMA_OMP_CLAUSE_IF
;
31358 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31359 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31360 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31361 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31362 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31363 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31364 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31365 result
= PRAGMA_OMP_CLAUSE_FOR
;
31366 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31368 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31369 const char *p
= IDENTIFIER_POINTER (id
);
31374 if (!strcmp ("aligned", p
))
31375 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31376 else if (!strcmp ("async", p
))
31377 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31380 if (!strcmp ("collapse", p
))
31381 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31382 else if (!strcmp ("copy", p
))
31383 result
= PRAGMA_OACC_CLAUSE_COPY
;
31384 else if (!strcmp ("copyin", p
))
31385 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31386 else if (!strcmp ("copyout", p
))
31387 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31388 else if (!strcmp ("copyprivate", p
))
31389 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31390 else if (!strcmp ("create", p
))
31391 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31394 if (!strcmp ("defaultmap", p
))
31395 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31396 else if (!strcmp ("depend", p
))
31397 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31398 else if (!strcmp ("device", p
))
31399 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31400 else if (!strcmp ("deviceptr", p
))
31401 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31402 else if (!strcmp ("device_resident", p
))
31403 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31404 else if (!strcmp ("dist_schedule", p
))
31405 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31408 if (!strcmp ("final", p
))
31409 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31410 else if (!strcmp ("finalize", p
))
31411 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
31412 else if (!strcmp ("firstprivate", p
))
31413 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31414 else if (!strcmp ("from", p
))
31415 result
= PRAGMA_OMP_CLAUSE_FROM
;
31418 if (!strcmp ("gang", p
))
31419 result
= PRAGMA_OACC_CLAUSE_GANG
;
31420 else if (!strcmp ("grainsize", p
))
31421 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31424 if (!strcmp ("hint", p
))
31425 result
= PRAGMA_OMP_CLAUSE_HINT
;
31426 else if (!strcmp ("host", p
))
31427 result
= PRAGMA_OACC_CLAUSE_HOST
;
31430 if (!strcmp ("if_present", p
))
31431 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
31432 else if (!strcmp ("inbranch", p
))
31433 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31434 else if (!strcmp ("independent", p
))
31435 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31436 else if (!strcmp ("is_device_ptr", p
))
31437 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31440 if (!strcmp ("lastprivate", p
))
31441 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31442 else if (!strcmp ("linear", p
))
31443 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31444 else if (!strcmp ("link", p
))
31445 result
= PRAGMA_OMP_CLAUSE_LINK
;
31448 if (!strcmp ("map", p
))
31449 result
= PRAGMA_OMP_CLAUSE_MAP
;
31450 else if (!strcmp ("mergeable", p
))
31451 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31454 if (!strcmp ("nogroup", p
))
31455 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31456 else if (!strcmp ("notinbranch", p
))
31457 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31458 else if (!strcmp ("nowait", p
))
31459 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31460 else if (!strcmp ("num_gangs", p
))
31461 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31462 else if (!strcmp ("num_tasks", p
))
31463 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31464 else if (!strcmp ("num_teams", p
))
31465 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31466 else if (!strcmp ("num_threads", p
))
31467 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31468 else if (!strcmp ("num_workers", p
))
31469 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31472 if (!strcmp ("ordered", p
))
31473 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31476 if (!strcmp ("parallel", p
))
31477 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31478 else if (!strcmp ("present", p
))
31479 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31480 else if (!strcmp ("present_or_copy", p
)
31481 || !strcmp ("pcopy", p
))
31482 result
= PRAGMA_OACC_CLAUSE_COPY
;
31483 else if (!strcmp ("present_or_copyin", p
)
31484 || !strcmp ("pcopyin", p
))
31485 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
31486 else if (!strcmp ("present_or_copyout", p
)
31487 || !strcmp ("pcopyout", p
))
31488 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31489 else if (!strcmp ("present_or_create", p
)
31490 || !strcmp ("pcreate", p
))
31491 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31492 else if (!strcmp ("priority", p
))
31493 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31494 else if (!strcmp ("proc_bind", p
))
31495 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31498 if (!strcmp ("reduction", p
))
31499 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31502 if (!strcmp ("safelen", p
))
31503 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31504 else if (!strcmp ("schedule", p
))
31505 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31506 else if (!strcmp ("sections", p
))
31507 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31508 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
31509 result
= PRAGMA_OACC_CLAUSE_HOST
;
31510 else if (!strcmp ("seq", p
))
31511 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31512 else if (!strcmp ("shared", p
))
31513 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31514 else if (!strcmp ("simd", p
))
31515 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31516 else if (!strcmp ("simdlen", p
))
31517 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31520 if (!strcmp ("taskgroup", p
))
31521 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31522 else if (!strcmp ("thread_limit", p
))
31523 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31524 else if (!strcmp ("threads", p
))
31525 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31526 else if (!strcmp ("tile", p
))
31527 result
= PRAGMA_OACC_CLAUSE_TILE
;
31528 else if (!strcmp ("to", p
))
31529 result
= PRAGMA_OMP_CLAUSE_TO
;
31532 if (!strcmp ("uniform", p
))
31533 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31534 else if (!strcmp ("untied", p
))
31535 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31536 else if (!strcmp ("use_device", p
))
31537 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31538 else if (!strcmp ("use_device_ptr", p
))
31539 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31542 if (!strcmp ("vector", p
))
31543 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31544 else if (!strcmp ("vector_length", p
))
31545 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31548 if (!strcmp ("wait", p
))
31549 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31550 else if (!strcmp ("worker", p
))
31551 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31556 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31557 cp_lexer_consume_token (parser
->lexer
);
31562 /* Validate that a clause of the given type does not already exist. */
31565 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31566 const char *name
, location_t location
)
31570 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31571 if (OMP_CLAUSE_CODE (c
) == code
)
31573 error_at (location
, "too many %qs clauses", name
);
31581 variable-list , identifier
31583 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31584 colon). An opening parenthesis will have been consumed by the caller.
31586 If KIND is nonzero, create the appropriate node and install the decl
31587 in OMP_CLAUSE_DECL and add the node to the head of the list.
31589 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31590 return the list created.
31592 COLON can be NULL if only closing parenthesis should end the list,
31593 or pointer to bool which will receive false if the list is terminated
31594 by closing parenthesis or true if the list is terminated by colon. */
31597 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31598 tree list
, bool *colon
)
31601 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31604 parser
->colon_corrects_to_scope_p
= false;
31611 token
= cp_lexer_peek_token (parser
->lexer
);
31613 && current_class_ptr
31614 && cp_parser_is_keyword (token
, RID_THIS
))
31616 decl
= finish_this_expr ();
31617 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31618 || CONVERT_EXPR_P (decl
))
31619 decl
= TREE_OPERAND (decl
, 0);
31620 cp_lexer_consume_token (parser
->lexer
);
31624 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31625 /*check_dependency_p=*/true,
31626 /*template_p=*/NULL
,
31627 /*declarator_p=*/false,
31628 /*optional_p=*/false);
31629 if (name
== error_mark_node
)
31632 if (identifier_p (name
))
31633 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31636 if (decl
== error_mark_node
)
31637 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31640 if (decl
== error_mark_node
)
31642 else if (kind
!= 0)
31646 case OMP_CLAUSE__CACHE_
:
31647 /* The OpenACC cache directive explicitly only allows "array
31648 elements or subarrays". */
31649 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31651 error_at (token
->location
, "expected %<[%>");
31652 decl
= error_mark_node
;
31656 case OMP_CLAUSE_MAP
:
31657 case OMP_CLAUSE_FROM
:
31658 case OMP_CLAUSE_TO
:
31659 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31662 = cp_lexer_peek_token (parser
->lexer
)->location
;
31663 cp_id_kind idk
= CP_ID_KIND_NONE
;
31664 cp_lexer_consume_token (parser
->lexer
);
31665 decl
= convert_from_reference (decl
);
31667 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31672 case OMP_CLAUSE_DEPEND
:
31673 case OMP_CLAUSE_REDUCTION
:
31674 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31676 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31678 parser
->colon_corrects_to_scope_p
= false;
31679 cp_lexer_consume_token (parser
->lexer
);
31680 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31681 low_bound
= cp_parser_expression (parser
);
31683 parser
->colon_corrects_to_scope_p
31684 = saved_colon_corrects_to_scope_p
;
31685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31686 length
= integer_one_node
;
31689 /* Look for `:'. */
31690 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31692 if (!cp_lexer_next_token_is (parser
->lexer
,
31694 length
= cp_parser_expression (parser
);
31696 /* Look for the closing `]'. */
31697 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31701 decl
= tree_cons (low_bound
, length
, decl
);
31708 tree u
= build_omp_clause (token
->location
, kind
);
31709 OMP_CLAUSE_DECL (u
) = decl
;
31710 OMP_CLAUSE_CHAIN (u
) = list
;
31714 list
= tree_cons (decl
, NULL_TREE
, list
);
31717 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31719 cp_lexer_consume_token (parser
->lexer
);
31723 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31725 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31728 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31732 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31736 /* Try to resync to an unnested comma. Copied from
31737 cp_parser_parenthesized_expression_list. */
31740 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31741 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31742 /*recovering=*/true,
31744 /*consume_paren=*/true);
31752 /* Similarly, but expect leading and trailing parenthesis. This is a very
31753 common case for omp clauses. */
31756 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31758 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31759 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31764 copy ( variable-list )
31765 copyin ( variable-list )
31766 copyout ( variable-list )
31767 create ( variable-list )
31768 delete ( variable-list )
31769 present ( variable-list ) */
31772 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31775 enum gomp_map_kind kind
;
31778 case PRAGMA_OACC_CLAUSE_COPY
:
31779 kind
= GOMP_MAP_TOFROM
;
31781 case PRAGMA_OACC_CLAUSE_COPYIN
:
31782 kind
= GOMP_MAP_TO
;
31784 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31785 kind
= GOMP_MAP_FROM
;
31787 case PRAGMA_OACC_CLAUSE_CREATE
:
31788 kind
= GOMP_MAP_ALLOC
;
31790 case PRAGMA_OACC_CLAUSE_DELETE
:
31791 kind
= GOMP_MAP_RELEASE
;
31793 case PRAGMA_OACC_CLAUSE_DEVICE
:
31794 kind
= GOMP_MAP_FORCE_TO
;
31796 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31797 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31799 case PRAGMA_OACC_CLAUSE_HOST
:
31800 kind
= GOMP_MAP_FORCE_FROM
;
31802 case PRAGMA_OACC_CLAUSE_LINK
:
31803 kind
= GOMP_MAP_LINK
;
31805 case PRAGMA_OACC_CLAUSE_PRESENT
:
31806 kind
= GOMP_MAP_FORCE_PRESENT
;
31809 gcc_unreachable ();
31812 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31814 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31815 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31821 deviceptr ( variable-list ) */
31824 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31826 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31829 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31830 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31831 variable-list must only allow for pointer variables. */
31832 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31833 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31835 tree v
= TREE_PURPOSE (t
);
31836 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31837 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31838 OMP_CLAUSE_DECL (u
) = v
;
31839 OMP_CLAUSE_CHAIN (u
) = list
;
31854 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31855 enum omp_clause_code code
,
31856 tree list
, location_t location
)
31858 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31859 tree c
= build_omp_clause (location
, code
);
31860 OMP_CLAUSE_CHAIN (c
) = list
;
31865 num_gangs ( expression )
31866 num_workers ( expression )
31867 vector_length ( expression ) */
31870 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31871 const char *str
, tree list
)
31873 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31875 matching_parens parens
;
31876 if (!parens
.require_open (parser
))
31879 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31881 if (t
== error_mark_node
31882 || !parens
.require_close (parser
))
31884 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31885 /*or_comma=*/false,
31886 /*consume_paren=*/true);
31890 check_no_duplicate_clause (list
, code
, str
, loc
);
31892 tree c
= build_omp_clause (loc
, code
);
31893 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31894 OMP_CLAUSE_CHAIN (c
) = list
;
31900 gang [( gang-arg-list )]
31901 worker [( [num:] int-expr )]
31902 vector [( [length:] int-expr )]
31904 where gang-arg is one of:
31909 and size-expr may be:
31916 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31917 const char *str
, tree list
)
31919 const char *id
= "num";
31920 cp_lexer
*lexer
= parser
->lexer
;
31921 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31922 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31924 if (kind
== OMP_CLAUSE_VECTOR
)
31927 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31929 matching_parens parens
;
31930 parens
.consume_open (parser
);
31934 cp_token
*next
= cp_lexer_peek_token (lexer
);
31937 /* Gang static argument. */
31938 if (kind
== OMP_CLAUSE_GANG
31939 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31941 cp_lexer_consume_token (lexer
);
31943 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31944 goto cleanup_error
;
31947 if (ops
[idx
] != NULL
)
31949 cp_parser_error (parser
, "too many %<static%> arguments");
31950 goto cleanup_error
;
31953 /* Check for the '*' argument. */
31954 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31955 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31956 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31959 cp_lexer_consume_token (lexer
);
31960 ops
[idx
] = integer_minus_one_node
;
31962 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31964 cp_lexer_consume_token (lexer
);
31970 /* Worker num: argument and vector length: arguments. */
31971 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31972 && id_equal (next
->u
.value
, id
)
31973 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31975 cp_lexer_consume_token (lexer
); /* id */
31976 cp_lexer_consume_token (lexer
); /* ':' */
31979 /* Now collect the actual argument. */
31980 if (ops
[idx
] != NULL_TREE
)
31982 cp_parser_error (parser
, "unexpected argument");
31983 goto cleanup_error
;
31986 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31988 if (expr
== error_mark_node
)
31989 goto cleanup_error
;
31991 mark_exp_read (expr
);
31994 if (kind
== OMP_CLAUSE_GANG
31995 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31997 cp_lexer_consume_token (lexer
);
32004 if (!parens
.require_close (parser
))
32005 goto cleanup_error
;
32008 check_no_duplicate_clause (list
, kind
, str
, loc
);
32010 c
= build_omp_clause (loc
, kind
);
32013 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
32015 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
32016 OMP_CLAUSE_CHAIN (c
) = list
;
32021 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32026 tile ( size-expr-list ) */
32029 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32031 tree c
, expr
= error_mark_node
;
32032 tree tile
= NULL_TREE
;
32034 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32035 so, but the spec authors never considered such a case and have
32036 differing opinions on what it might mean, including 'not
32038 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32039 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32042 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32047 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32051 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32052 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32054 cp_lexer_consume_token (parser
->lexer
);
32055 expr
= integer_zero_node
;
32058 expr
= cp_parser_constant_expression (parser
);
32060 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32062 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32064 /* Consume the trailing ')'. */
32065 cp_lexer_consume_token (parser
->lexer
);
32067 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32068 tile
= nreverse (tile
);
32069 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32070 OMP_CLAUSE_CHAIN (c
) = list
;
32075 Parse wait clause or directive parameters. */
32078 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32080 vec
<tree
, va_gc
> *args
;
32083 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32085 /*allow_expansion_p=*/true,
32086 /*non_constant_p=*/NULL
);
32088 if (args
== NULL
|| args
->length () == 0)
32090 cp_parser_error (parser
, "expected integer expression before ')'");
32092 release_tree_vector (args
);
32096 args_tree
= build_tree_list_vec (args
);
32098 release_tree_vector (args
);
32100 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32102 tree targ
= TREE_VALUE (t
);
32104 if (targ
!= error_mark_node
)
32106 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32107 error ("%<wait%> expression must be integral");
32110 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32112 targ
= mark_rvalue_use (targ
);
32113 OMP_CLAUSE_DECL (c
) = targ
;
32114 OMP_CLAUSE_CHAIN (c
) = list
;
32124 wait ( int-expr-list ) */
32127 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32129 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32131 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32134 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32140 collapse ( constant-expression ) */
32143 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32149 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32150 matching_parens parens
;
32151 if (!parens
.require_open (parser
))
32154 num
= cp_parser_constant_expression (parser
);
32156 if (!parens
.require_close (parser
))
32157 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32158 /*or_comma=*/false,
32159 /*consume_paren=*/true);
32161 if (num
== error_mark_node
)
32163 num
= fold_non_dependent_expr (num
);
32164 if (!tree_fits_shwi_p (num
)
32165 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32166 || (n
= tree_to_shwi (num
)) <= 0
32169 error_at (loc
, "collapse argument needs positive constant integer expression");
32173 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32174 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32175 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32176 OMP_CLAUSE_CHAIN (c
) = list
;
32177 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32183 default ( none | shared )
32186 default ( none | present ) */
32189 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32190 location_t location
, bool is_oacc
)
32192 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32195 matching_parens parens
;
32196 if (!parens
.require_open (parser
))
32198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32200 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32201 const char *p
= IDENTIFIER_POINTER (id
);
32206 if (strcmp ("none", p
) != 0)
32208 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32212 if (strcmp ("present", p
) != 0 || !is_oacc
)
32214 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32218 if (strcmp ("shared", p
) != 0 || is_oacc
)
32220 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32227 cp_lexer_consume_token (parser
->lexer
);
32233 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32235 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32238 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32239 || !parens
.require_close (parser
))
32240 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32241 /*or_comma=*/false,
32242 /*consume_paren=*/true);
32244 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32247 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32248 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32249 OMP_CLAUSE_CHAIN (c
) = list
;
32250 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32256 final ( expression ) */
32259 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32263 matching_parens parens
;
32264 if (!parens
.require_open (parser
))
32267 t
= cp_parser_condition (parser
);
32269 if (t
== error_mark_node
32270 || !parens
.require_close (parser
))
32271 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32277 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32278 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32279 OMP_CLAUSE_CHAIN (c
) = list
;
32288 if ( directive-name-modifier : expression )
32290 directive-name-modifier:
32291 parallel | task | taskloop | target data | target | target update
32292 | target enter data | target exit data */
32295 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32299 enum tree_code if_modifier
= ERROR_MARK
;
32301 matching_parens parens
;
32302 if (!parens
.require_open (parser
))
32305 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32308 const char *p
= IDENTIFIER_POINTER (id
);
32311 if (strcmp ("parallel", p
) == 0)
32312 if_modifier
= OMP_PARALLEL
;
32313 else if (strcmp ("task", p
) == 0)
32314 if_modifier
= OMP_TASK
;
32315 else if (strcmp ("taskloop", p
) == 0)
32316 if_modifier
= OMP_TASKLOOP
;
32317 else if (strcmp ("target", p
) == 0)
32319 if_modifier
= OMP_TARGET
;
32320 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32322 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32323 p
= IDENTIFIER_POINTER (id
);
32324 if (strcmp ("data", p
) == 0)
32325 if_modifier
= OMP_TARGET_DATA
;
32326 else if (strcmp ("update", p
) == 0)
32327 if_modifier
= OMP_TARGET_UPDATE
;
32328 else if (strcmp ("enter", p
) == 0)
32329 if_modifier
= OMP_TARGET_ENTER_DATA
;
32330 else if (strcmp ("exit", p
) == 0)
32331 if_modifier
= OMP_TARGET_EXIT_DATA
;
32332 if (if_modifier
!= OMP_TARGET
)
32337 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32338 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32340 if_modifier
= ERROR_MARK
;
32342 if (if_modifier
== OMP_TARGET_ENTER_DATA
32343 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32345 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32347 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32348 p
= IDENTIFIER_POINTER (id
);
32349 if (strcmp ("data", p
) == 0)
32355 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32356 error_at (loc
, "expected %<data%>");
32357 if_modifier
= ERROR_MARK
;
32362 if (if_modifier
!= ERROR_MARK
)
32364 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32367 cp_lexer_consume_token (parser
->lexer
);
32374 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32375 error_at (loc
, "expected %<:%>");
32377 if_modifier
= ERROR_MARK
;
32382 t
= cp_parser_condition (parser
);
32384 if (t
== error_mark_node
32385 || !parens
.require_close (parser
))
32386 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32387 /*or_comma=*/false,
32388 /*consume_paren=*/true);
32390 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32391 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32393 if (if_modifier
!= ERROR_MARK
32394 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32396 const char *p
= NULL
;
32397 switch (if_modifier
)
32399 case OMP_PARALLEL
: p
= "parallel"; break;
32400 case OMP_TASK
: p
= "task"; break;
32401 case OMP_TASKLOOP
: p
= "taskloop"; break;
32402 case OMP_TARGET_DATA
: p
= "target data"; break;
32403 case OMP_TARGET
: p
= "target"; break;
32404 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32405 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32406 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32407 default: gcc_unreachable ();
32409 error_at (location
, "too many %<if%> clauses with %qs modifier",
32413 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32416 error_at (location
, "too many %<if%> clauses");
32418 error_at (location
, "too many %<if%> clauses without modifier");
32421 else if (if_modifier
== ERROR_MARK
32422 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32424 error_at (location
, "if any %<if%> clause has modifier, then all "
32425 "%<if%> clauses have to use modifier");
32430 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32431 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32432 OMP_CLAUSE_IF_EXPR (c
) = t
;
32433 OMP_CLAUSE_CHAIN (c
) = list
;
32442 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32443 tree list
, location_t location
)
32447 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32450 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32451 OMP_CLAUSE_CHAIN (c
) = list
;
32459 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32460 tree list
, location_t location
)
32464 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32466 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32467 OMP_CLAUSE_CHAIN (c
) = list
;
32472 num_threads ( expression ) */
32475 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32476 location_t location
)
32480 matching_parens parens
;
32481 if (!parens
.require_open (parser
))
32484 t
= cp_parser_expression (parser
);
32486 if (t
== error_mark_node
32487 || !parens
.require_close (parser
))
32488 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32489 /*or_comma=*/false,
32490 /*consume_paren=*/true);
32492 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32493 "num_threads", location
);
32495 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32496 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32497 OMP_CLAUSE_CHAIN (c
) = list
;
32503 num_tasks ( expression ) */
32506 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32507 location_t location
)
32511 matching_parens parens
;
32512 if (!parens
.require_open (parser
))
32515 t
= cp_parser_expression (parser
);
32517 if (t
== error_mark_node
32518 || !parens
.require_close (parser
))
32519 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32520 /*or_comma=*/false,
32521 /*consume_paren=*/true);
32523 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32524 "num_tasks", location
);
32526 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32527 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32528 OMP_CLAUSE_CHAIN (c
) = list
;
32534 grainsize ( expression ) */
32537 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32538 location_t location
)
32542 matching_parens parens
;
32543 if (!parens
.require_open (parser
))
32546 t
= cp_parser_expression (parser
);
32548 if (t
== error_mark_node
32549 || !parens
.require_close (parser
))
32550 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32551 /*or_comma=*/false,
32552 /*consume_paren=*/true);
32554 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32555 "grainsize", location
);
32557 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32558 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32559 OMP_CLAUSE_CHAIN (c
) = list
;
32565 priority ( expression ) */
32568 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32569 location_t location
)
32573 matching_parens parens
;
32574 if (!parens
.require_open (parser
))
32577 t
= cp_parser_expression (parser
);
32579 if (t
== error_mark_node
32580 || !parens
.require_close (parser
))
32581 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32582 /*or_comma=*/false,
32583 /*consume_paren=*/true);
32585 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32586 "priority", location
);
32588 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32589 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32590 OMP_CLAUSE_CHAIN (c
) = list
;
32596 hint ( expression ) */
32599 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32600 location_t location
)
32604 matching_parens parens
;
32605 if (!parens
.require_open (parser
))
32608 t
= cp_parser_expression (parser
);
32610 if (t
== error_mark_node
32611 || !parens
.require_close (parser
))
32612 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32613 /*or_comma=*/false,
32614 /*consume_paren=*/true);
32616 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32618 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32619 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32620 OMP_CLAUSE_CHAIN (c
) = list
;
32626 defaultmap ( tofrom : scalar ) */
32629 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32630 location_t location
)
32635 matching_parens parens
;
32636 if (!parens
.require_open (parser
))
32639 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32641 cp_parser_error (parser
, "expected %<tofrom%>");
32644 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32645 p
= IDENTIFIER_POINTER (id
);
32646 if (strcmp (p
, "tofrom") != 0)
32648 cp_parser_error (parser
, "expected %<tofrom%>");
32651 cp_lexer_consume_token (parser
->lexer
);
32652 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32655 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32657 cp_parser_error (parser
, "expected %<scalar%>");
32660 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32661 p
= IDENTIFIER_POINTER (id
);
32662 if (strcmp (p
, "scalar") != 0)
32664 cp_parser_error (parser
, "expected %<scalar%>");
32667 cp_lexer_consume_token (parser
->lexer
);
32668 if (!parens
.require_close (parser
))
32671 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32674 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32675 OMP_CLAUSE_CHAIN (c
) = list
;
32679 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32680 /*or_comma=*/false,
32681 /*consume_paren=*/true);
32689 ordered ( constant-expression ) */
32692 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32693 tree list
, location_t location
)
32695 tree c
, num
= NULL_TREE
;
32698 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32699 "ordered", location
);
32701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32703 matching_parens parens
;
32704 parens
.consume_open (parser
);
32706 num
= cp_parser_constant_expression (parser
);
32708 if (!parens
.require_close (parser
))
32709 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32710 /*or_comma=*/false,
32711 /*consume_paren=*/true);
32713 if (num
== error_mark_node
)
32715 num
= fold_non_dependent_expr (num
);
32716 if (!tree_fits_shwi_p (num
)
32717 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32718 || (n
= tree_to_shwi (num
)) <= 0
32721 error_at (location
,
32722 "ordered argument needs positive constant integer "
32728 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32729 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32730 OMP_CLAUSE_CHAIN (c
) = list
;
32735 reduction ( reduction-operator : variable-list )
32737 reduction-operator:
32738 One of: + * - & ^ | && ||
32742 reduction-operator:
32743 One of: + * - & ^ | && || min max
32747 reduction-operator:
32748 One of: + * - & ^ | && ||
32752 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32754 enum tree_code code
= ERROR_MARK
;
32755 tree nlist
, c
, id
= NULL_TREE
;
32757 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32760 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32762 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32763 case CPP_MULT
: code
= MULT_EXPR
; break;
32764 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32765 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32766 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32767 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32768 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32769 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32773 if (code
!= ERROR_MARK
)
32774 cp_lexer_consume_token (parser
->lexer
);
32777 bool saved_colon_corrects_to_scope_p
;
32778 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32779 parser
->colon_corrects_to_scope_p
= false;
32780 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32781 /*check_dependency_p=*/true,
32782 /*template_p=*/NULL
,
32783 /*declarator_p=*/false,
32784 /*optional_p=*/false);
32785 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32786 if (identifier_p (id
))
32788 const char *p
= IDENTIFIER_POINTER (id
);
32790 if (strcmp (p
, "min") == 0)
32792 else if (strcmp (p
, "max") == 0)
32794 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32796 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32798 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32800 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32801 code
= BIT_AND_EXPR
;
32802 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32803 code
= BIT_IOR_EXPR
;
32804 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32805 code
= BIT_XOR_EXPR
;
32806 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32807 code
= TRUTH_ANDIF_EXPR
;
32808 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32809 code
= TRUTH_ORIF_EXPR
;
32810 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32811 tree scope
= parser
->scope
;
32813 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32814 parser
->scope
= NULL_TREE
;
32815 parser
->qualifying_scope
= NULL_TREE
;
32816 parser
->object_scope
= NULL_TREE
;
32820 error ("invalid reduction-identifier");
32822 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32829 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32832 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32834 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32836 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32837 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32844 schedule ( schedule-kind )
32845 schedule ( schedule-kind , expression )
32848 static | dynamic | guided | runtime | auto
32851 schedule ( schedule-modifier : schedule-kind )
32852 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32860 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32863 int modifiers
= 0, nmodifiers
= 0;
32865 matching_parens parens
;
32866 if (!parens
.require_open (parser
))
32869 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32871 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32873 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32874 const char *p
= IDENTIFIER_POINTER (id
);
32875 if (strcmp ("simd", p
) == 0)
32876 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32877 else if (strcmp ("monotonic", p
) == 0)
32878 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32879 else if (strcmp ("nonmonotonic", p
) == 0)
32880 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32883 cp_lexer_consume_token (parser
->lexer
);
32884 if (nmodifiers
++ == 0
32885 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32886 cp_lexer_consume_token (parser
->lexer
);
32889 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32896 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32897 const char *p
= IDENTIFIER_POINTER (id
);
32902 if (strcmp ("dynamic", p
) != 0)
32904 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32908 if (strcmp ("guided", p
) != 0)
32910 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32914 if (strcmp ("runtime", p
) != 0)
32916 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32923 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32924 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32925 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32926 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32929 cp_lexer_consume_token (parser
->lexer
);
32931 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32932 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32933 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32934 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32936 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32944 cp_lexer_consume_token (parser
->lexer
);
32946 token
= cp_lexer_peek_token (parser
->lexer
);
32947 t
= cp_parser_assignment_expression (parser
);
32949 if (t
== error_mark_node
)
32951 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32952 error_at (token
->location
, "schedule %<runtime%> does not take "
32953 "a %<chunk_size%> parameter");
32954 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32955 error_at (token
->location
, "schedule %<auto%> does not take "
32956 "a %<chunk_size%> parameter");
32958 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32960 if (!parens
.require_close (parser
))
32963 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32966 OMP_CLAUSE_SCHEDULE_KIND (c
)
32967 = (enum omp_clause_schedule_kind
)
32968 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32970 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32971 OMP_CLAUSE_CHAIN (c
) = list
;
32975 cp_parser_error (parser
, "invalid schedule kind");
32977 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32978 /*or_comma=*/false,
32979 /*consume_paren=*/true);
32987 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32988 tree list
, location_t location
)
32992 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32994 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32995 OMP_CLAUSE_CHAIN (c
) = list
;
33004 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
33005 tree list
, location_t location
)
33007 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33008 tree c
= build_omp_clause (location
, code
);
33009 OMP_CLAUSE_CHAIN (c
) = list
;
33020 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
33021 enum omp_clause_code code
,
33022 tree list
, location_t location
)
33024 tree c
= build_omp_clause (location
, code
);
33025 OMP_CLAUSE_CHAIN (c
) = list
;
33033 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33034 tree list
, location_t location
)
33036 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33037 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33038 OMP_CLAUSE_CHAIN (c
) = list
;
33047 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33048 enum omp_clause_code code
,
33049 tree list
, location_t location
)
33051 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33052 tree c
= build_omp_clause (location
, code
);
33053 OMP_CLAUSE_CHAIN (c
) = list
;
33058 num_teams ( expression ) */
33061 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33062 location_t location
)
33066 matching_parens parens
;
33067 if (!parens
.require_open (parser
))
33070 t
= cp_parser_expression (parser
);
33072 if (t
== error_mark_node
33073 || !parens
.require_close (parser
))
33074 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33075 /*or_comma=*/false,
33076 /*consume_paren=*/true);
33078 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33079 "num_teams", location
);
33081 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33082 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33083 OMP_CLAUSE_CHAIN (c
) = list
;
33089 thread_limit ( expression ) */
33092 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33093 location_t location
)
33097 matching_parens parens
;
33098 if (!parens
.require_open (parser
))
33101 t
= cp_parser_expression (parser
);
33103 if (t
== error_mark_node
33104 || !parens
.require_close (parser
))
33105 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33106 /*or_comma=*/false,
33107 /*consume_paren=*/true);
33109 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33110 "thread_limit", location
);
33112 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33113 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33114 OMP_CLAUSE_CHAIN (c
) = list
;
33120 aligned ( variable-list )
33121 aligned ( variable-list : constant-expression ) */
33124 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33126 tree nlist
, c
, alignment
= NULL_TREE
;
33129 matching_parens parens
;
33130 if (!parens
.require_open (parser
))
33133 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33138 alignment
= cp_parser_constant_expression (parser
);
33140 if (!parens
.require_close (parser
))
33141 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33142 /*or_comma=*/false,
33143 /*consume_paren=*/true);
33145 if (alignment
== error_mark_node
)
33146 alignment
= NULL_TREE
;
33149 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33150 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33156 linear ( variable-list )
33157 linear ( variable-list : expression )
33160 linear ( modifier ( variable-list ) )
33161 linear ( modifier ( variable-list ) : expression ) */
33164 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33167 tree nlist
, c
, step
= integer_one_node
;
33169 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33171 matching_parens parens
;
33172 if (!parens
.require_open (parser
))
33175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33177 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33178 const char *p
= IDENTIFIER_POINTER (id
);
33180 if (strcmp ("ref", p
) == 0)
33181 kind
= OMP_CLAUSE_LINEAR_REF
;
33182 else if (strcmp ("val", p
) == 0)
33183 kind
= OMP_CLAUSE_LINEAR_VAL
;
33184 else if (strcmp ("uval", p
) == 0)
33185 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33186 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33187 cp_lexer_consume_token (parser
->lexer
);
33189 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33192 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33193 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33197 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33198 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33200 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33201 else if (!parens
.require_close (parser
))
33202 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33203 /*or_comma=*/false,
33204 /*consume_paren=*/true);
33211 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33212 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33214 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33215 cp_parser_parse_tentatively (parser
);
33216 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33217 /*check_dependency_p=*/true,
33218 /*template_p=*/NULL
,
33219 /*declarator_p=*/false,
33220 /*optional_p=*/false);
33221 if (step
!= error_mark_node
)
33222 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33223 if (step
== error_mark_node
)
33226 cp_parser_abort_tentative_parse (parser
);
33228 else if (!cp_parser_parse_definitely (parser
))
33232 step
= cp_parser_expression (parser
);
33234 if (!parens
.require_close (parser
))
33235 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33236 /*or_comma=*/false,
33237 /*consume_paren=*/true);
33239 if (step
== error_mark_node
)
33243 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33245 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33246 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33253 safelen ( constant-expression ) */
33256 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33257 location_t location
)
33261 matching_parens parens
;
33262 if (!parens
.require_open (parser
))
33265 t
= cp_parser_constant_expression (parser
);
33267 if (t
== error_mark_node
33268 || !parens
.require_close (parser
))
33269 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33270 /*or_comma=*/false,
33271 /*consume_paren=*/true);
33273 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33275 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33276 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33277 OMP_CLAUSE_CHAIN (c
) = list
;
33283 simdlen ( constant-expression ) */
33286 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33287 location_t location
)
33291 matching_parens parens
;
33292 if (!parens
.require_open (parser
))
33295 t
= cp_parser_constant_expression (parser
);
33297 if (t
== error_mark_node
33298 || !parens
.require_close (parser
))
33299 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33300 /*or_comma=*/false,
33301 /*consume_paren=*/true);
33303 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33305 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33306 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33307 OMP_CLAUSE_CHAIN (c
) = list
;
33314 identifier [+/- integer]
33315 vec , identifier [+/- integer]
33319 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33326 cp_parser_error (parser
, "expected identifier");
33330 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33332 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33333 tree t
, identifier
= cp_parser_identifier (parser
);
33334 tree addend
= NULL
;
33336 if (identifier
== error_mark_node
)
33337 t
= error_mark_node
;
33340 t
= cp_parser_lookup_name_simple
33341 (parser
, identifier
,
33342 cp_lexer_peek_token (parser
->lexer
)->location
);
33343 if (t
== error_mark_node
)
33344 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33351 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33353 addend
= integer_zero_node
;
33354 goto add_to_vector
;
33356 cp_lexer_consume_token (parser
->lexer
);
33358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33360 cp_parser_error (parser
, "expected integer");
33364 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33365 if (TREE_CODE (addend
) != INTEGER_CST
)
33367 cp_parser_error (parser
, "expected integer");
33370 cp_lexer_consume_token (parser
->lexer
);
33373 if (t
!= error_mark_node
)
33375 vec
= tree_cons (addend
, t
, vec
);
33377 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33380 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33383 cp_lexer_consume_token (parser
->lexer
);
33386 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33388 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33389 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33390 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33391 OMP_CLAUSE_CHAIN (u
) = list
;
33398 depend ( depend-kind : variable-list )
33406 depend ( sink : vec ) */
33409 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33412 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33414 matching_parens parens
;
33415 if (!parens
.require_open (parser
))
33418 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33420 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33421 const char *p
= IDENTIFIER_POINTER (id
);
33423 if (strcmp ("in", p
) == 0)
33424 kind
= OMP_CLAUSE_DEPEND_IN
;
33425 else if (strcmp ("inout", p
) == 0)
33426 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33427 else if (strcmp ("out", p
) == 0)
33428 kind
= OMP_CLAUSE_DEPEND_OUT
;
33429 else if (strcmp ("source", p
) == 0)
33430 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33431 else if (strcmp ("sink", p
) == 0)
33432 kind
= OMP_CLAUSE_DEPEND_SINK
;
33439 cp_lexer_consume_token (parser
->lexer
);
33441 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33443 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33444 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33445 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33446 OMP_CLAUSE_CHAIN (c
) = list
;
33447 if (!parens
.require_close (parser
))
33448 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33449 /*or_comma=*/false,
33450 /*consume_paren=*/true);
33454 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33457 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33458 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33461 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33464 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33465 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33470 cp_parser_error (parser
, "invalid depend kind");
33472 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33473 /*or_comma=*/false,
33474 /*consume_paren=*/true);
33479 map ( map-kind : variable-list )
33480 map ( variable-list )
33483 alloc | to | from | tofrom
33487 alloc | to | from | tofrom | release | delete
33489 map ( always [,] map-kind: variable-list ) */
33492 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33495 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33496 bool always
= false;
33498 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33503 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33504 const char *p
= IDENTIFIER_POINTER (id
);
33506 if (strcmp ("always", p
) == 0)
33509 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33511 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33512 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33514 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33518 cp_lexer_consume_token (parser
->lexer
);
33520 cp_lexer_consume_token (parser
->lexer
);
33525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33526 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33528 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33529 const char *p
= IDENTIFIER_POINTER (id
);
33531 if (strcmp ("alloc", p
) == 0)
33532 kind
= GOMP_MAP_ALLOC
;
33533 else if (strcmp ("to", p
) == 0)
33534 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33535 else if (strcmp ("from", p
) == 0)
33536 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33537 else if (strcmp ("tofrom", p
) == 0)
33538 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33539 else if (strcmp ("release", p
) == 0)
33540 kind
= GOMP_MAP_RELEASE
;
33543 cp_parser_error (parser
, "invalid map kind");
33544 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33545 /*or_comma=*/false,
33546 /*consume_paren=*/true);
33549 cp_lexer_consume_token (parser
->lexer
);
33550 cp_lexer_consume_token (parser
->lexer
);
33552 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33553 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33555 kind
= GOMP_MAP_DELETE
;
33556 cp_lexer_consume_token (parser
->lexer
);
33557 cp_lexer_consume_token (parser
->lexer
);
33560 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33563 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33564 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33570 device ( expression ) */
33573 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33574 location_t location
)
33578 matching_parens parens
;
33579 if (!parens
.require_open (parser
))
33582 t
= cp_parser_expression (parser
);
33584 if (t
== error_mark_node
33585 || !parens
.require_close (parser
))
33586 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33587 /*or_comma=*/false,
33588 /*consume_paren=*/true);
33590 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33591 "device", location
);
33593 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33594 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33595 OMP_CLAUSE_CHAIN (c
) = list
;
33601 dist_schedule ( static )
33602 dist_schedule ( static , expression ) */
33605 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33606 location_t location
)
33610 matching_parens parens
;
33611 if (!parens
.require_open (parser
))
33614 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33616 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33618 cp_lexer_consume_token (parser
->lexer
);
33620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33622 cp_lexer_consume_token (parser
->lexer
);
33624 t
= cp_parser_assignment_expression (parser
);
33626 if (t
== error_mark_node
)
33628 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33630 if (!parens
.require_close (parser
))
33633 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33636 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33638 OMP_CLAUSE_CHAIN (c
) = list
;
33642 cp_parser_error (parser
, "invalid dist_schedule kind");
33644 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33645 /*or_comma=*/false,
33646 /*consume_paren=*/true);
33651 proc_bind ( proc-bind-kind )
33654 master | close | spread */
33657 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33658 location_t location
)
33661 enum omp_clause_proc_bind_kind kind
;
33663 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33668 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33669 const char *p
= IDENTIFIER_POINTER (id
);
33671 if (strcmp ("master", p
) == 0)
33672 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33673 else if (strcmp ("close", p
) == 0)
33674 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33675 else if (strcmp ("spread", p
) == 0)
33676 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33683 cp_lexer_consume_token (parser
->lexer
);
33684 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33687 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33688 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33690 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33691 OMP_CLAUSE_CHAIN (c
) = list
;
33695 cp_parser_error (parser
, "invalid depend kind");
33697 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33698 /*or_comma=*/false,
33699 /*consume_paren=*/true);
33704 async [( int-expr )] */
33707 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33710 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33712 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33714 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33716 matching_parens parens
;
33717 parens
.consume_open (parser
);
33719 t
= cp_parser_expression (parser
);
33720 if (t
== error_mark_node
33721 || !parens
.require_close (parser
))
33722 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33723 /*or_comma=*/false,
33724 /*consume_paren=*/true);
33727 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33729 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33730 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33731 OMP_CLAUSE_CHAIN (c
) = list
;
33737 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33738 is a bitmask in MASK. Return the list of clauses found. */
33741 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33742 const char *where
, cp_token
*pragma_tok
,
33743 bool finish_p
= true)
33745 tree clauses
= NULL
;
33748 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33751 pragma_omp_clause c_kind
;
33752 omp_clause_code code
;
33753 const char *c_name
;
33754 tree prev
= clauses
;
33756 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33757 cp_lexer_consume_token (parser
->lexer
);
33759 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33760 c_kind
= cp_parser_omp_clause_name (parser
);
33764 case PRAGMA_OACC_CLAUSE_ASYNC
:
33765 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33768 case PRAGMA_OACC_CLAUSE_AUTO
:
33769 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33773 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33774 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33775 c_name
= "collapse";
33777 case PRAGMA_OACC_CLAUSE_COPY
:
33778 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33781 case PRAGMA_OACC_CLAUSE_COPYIN
:
33782 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33785 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33786 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33787 c_name
= "copyout";
33789 case PRAGMA_OACC_CLAUSE_CREATE
:
33790 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33793 case PRAGMA_OACC_CLAUSE_DELETE
:
33794 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33797 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33798 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33799 c_name
= "default";
33801 case PRAGMA_OACC_CLAUSE_DEVICE
:
33802 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33805 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33806 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33807 c_name
= "deviceptr";
33809 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33810 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33811 c_name
= "device_resident";
33813 case PRAGMA_OACC_CLAUSE_FINALIZE
:
33814 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_FINALIZE
,
33816 c_name
= "finalize";
33818 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33819 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33821 c_name
= "firstprivate";
33823 case PRAGMA_OACC_CLAUSE_GANG
:
33825 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33828 case PRAGMA_OACC_CLAUSE_HOST
:
33829 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33832 case PRAGMA_OACC_CLAUSE_IF
:
33833 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33836 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
33837 clauses
= cp_parser_oacc_simple_clause (parser
,
33838 OMP_CLAUSE_IF_PRESENT
,
33840 c_name
= "if_present";
33842 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33843 clauses
= cp_parser_oacc_simple_clause (parser
,
33844 OMP_CLAUSE_INDEPENDENT
,
33846 c_name
= "independent";
33848 case PRAGMA_OACC_CLAUSE_LINK
:
33849 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33852 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33853 code
= OMP_CLAUSE_NUM_GANGS
;
33854 c_name
= "num_gangs";
33855 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33858 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33859 c_name
= "num_workers";
33860 code
= OMP_CLAUSE_NUM_WORKERS
;
33861 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33864 case PRAGMA_OACC_CLAUSE_PRESENT
:
33865 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33866 c_name
= "present";
33868 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33869 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33871 c_name
= "private";
33873 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33874 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33875 c_name
= "reduction";
33877 case PRAGMA_OACC_CLAUSE_SEQ
:
33878 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33882 case PRAGMA_OACC_CLAUSE_TILE
:
33883 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33886 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33887 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33889 c_name
= "use_device";
33891 case PRAGMA_OACC_CLAUSE_VECTOR
:
33893 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33896 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33897 c_name
= "vector_length";
33898 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33899 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33902 case PRAGMA_OACC_CLAUSE_WAIT
:
33903 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33906 case PRAGMA_OACC_CLAUSE_WORKER
:
33908 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33912 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33918 if (((mask
>> c_kind
) & 1) == 0)
33920 /* Remove the invalid clause(s) from the list to avoid
33921 confusing the rest of the compiler. */
33923 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33931 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33936 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33937 is a bitmask in MASK. Return the list of clauses found; the result
33938 of clause default goes in *pdefault. */
33941 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33942 const char *where
, cp_token
*pragma_tok
,
33943 bool finish_p
= true)
33945 tree clauses
= NULL
;
33947 cp_token
*token
= NULL
;
33949 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33951 pragma_omp_clause c_kind
;
33952 const char *c_name
;
33953 tree prev
= clauses
;
33955 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33956 cp_lexer_consume_token (parser
->lexer
);
33958 token
= cp_lexer_peek_token (parser
->lexer
);
33959 c_kind
= cp_parser_omp_clause_name (parser
);
33963 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33964 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33966 c_name
= "collapse";
33968 case PRAGMA_OMP_CLAUSE_COPYIN
:
33969 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33972 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33973 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33975 c_name
= "copyprivate";
33977 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33978 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33979 token
->location
, false);
33980 c_name
= "default";
33982 case PRAGMA_OMP_CLAUSE_FINAL
:
33983 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33986 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33987 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33989 c_name
= "firstprivate";
33991 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33992 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33994 c_name
= "grainsize";
33996 case PRAGMA_OMP_CLAUSE_HINT
:
33997 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
34001 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
34002 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
34004 c_name
= "defaultmap";
34006 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
34007 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
34009 c_name
= "use_device_ptr";
34011 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
34012 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34014 c_name
= "is_device_ptr";
34016 case PRAGMA_OMP_CLAUSE_IF
:
34017 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34021 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34022 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34024 c_name
= "lastprivate";
34026 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34027 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34029 c_name
= "mergeable";
34031 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34032 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34035 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34036 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34038 c_name
= "num_tasks";
34040 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34041 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34043 c_name
= "num_threads";
34045 case PRAGMA_OMP_CLAUSE_ORDERED
:
34046 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34048 c_name
= "ordered";
34050 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34051 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34053 c_name
= "priority";
34055 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34056 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34058 c_name
= "private";
34060 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34061 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34062 c_name
= "reduction";
34064 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34065 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34067 c_name
= "schedule";
34069 case PRAGMA_OMP_CLAUSE_SHARED
:
34070 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34074 case PRAGMA_OMP_CLAUSE_UNTIED
:
34075 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34079 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34080 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34081 clauses
, token
->location
);
34082 c_name
= "inbranch";
34084 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34085 clauses
= cp_parser_omp_clause_branch (parser
,
34086 OMP_CLAUSE_NOTINBRANCH
,
34087 clauses
, token
->location
);
34088 c_name
= "notinbranch";
34090 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34091 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34092 clauses
, token
->location
);
34093 c_name
= "parallel";
34097 error_at (token
->location
, "%qs must be the first clause of %qs",
34102 case PRAGMA_OMP_CLAUSE_FOR
:
34103 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34104 clauses
, token
->location
);
34107 goto clause_not_first
;
34109 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34110 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34111 clauses
, token
->location
);
34112 c_name
= "sections";
34114 goto clause_not_first
;
34116 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34117 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34118 clauses
, token
->location
);
34119 c_name
= "taskgroup";
34121 goto clause_not_first
;
34123 case PRAGMA_OMP_CLAUSE_LINK
:
34124 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34127 case PRAGMA_OMP_CLAUSE_TO
:
34128 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34129 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34132 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34135 case PRAGMA_OMP_CLAUSE_FROM
:
34136 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34139 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34140 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34142 c_name
= "uniform";
34144 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34145 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34147 c_name
= "num_teams";
34149 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34150 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34152 c_name
= "thread_limit";
34154 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34155 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34156 c_name
= "aligned";
34158 case PRAGMA_OMP_CLAUSE_LINEAR
:
34160 bool declare_simd
= false;
34161 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34162 declare_simd
= true;
34163 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34167 case PRAGMA_OMP_CLAUSE_DEPEND
:
34168 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34172 case PRAGMA_OMP_CLAUSE_MAP
:
34173 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34176 case PRAGMA_OMP_CLAUSE_DEVICE
:
34177 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34181 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34182 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34184 c_name
= "dist_schedule";
34186 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34187 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34189 c_name
= "proc_bind";
34191 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34192 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34194 c_name
= "safelen";
34196 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34197 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34199 c_name
= "simdlen";
34201 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34202 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34204 c_name
= "nogroup";
34206 case PRAGMA_OMP_CLAUSE_THREADS
:
34208 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34209 clauses
, token
->location
);
34210 c_name
= "threads";
34212 case PRAGMA_OMP_CLAUSE_SIMD
:
34214 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34215 clauses
, token
->location
);
34219 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34225 if (((mask
>> c_kind
) & 1) == 0)
34227 /* Remove the invalid clause(s) from the list to avoid
34228 confusing the rest of the compiler. */
34230 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34234 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34237 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34238 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34240 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34249 In practice, we're also interested in adding the statement to an
34250 outer node. So it is convenient if we work around the fact that
34251 cp_parser_statement calls add_stmt. */
34254 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34256 unsigned save
= parser
->in_statement
;
34258 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34259 This preserves the "not within loop or switch" style error messages
34260 for nonsense cases like
34266 if (parser
->in_statement
)
34267 parser
->in_statement
= IN_OMP_BLOCK
;
34273 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34275 parser
->in_statement
= save
;
34279 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34281 tree stmt
= begin_omp_structured_block ();
34282 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34284 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34286 cp_parser_end_omp_structured_block (parser
, save
);
34287 return finish_omp_structured_block (stmt
);
34291 # pragma omp atomic new-line
34295 x binop= expr | x++ | ++x | x-- | --x
34297 +, *, -, /, &, ^, |, <<, >>
34299 where x is an lvalue expression with scalar type.
34302 # pragma omp atomic new-line
34305 # pragma omp atomic read new-line
34308 # pragma omp atomic write new-line
34311 # pragma omp atomic update new-line
34314 # pragma omp atomic capture new-line
34317 # pragma omp atomic capture new-line
34325 expression-stmt | x = x binop expr
34327 v = expression-stmt
34329 { v = x; update-stmt; } | { update-stmt; v = x; }
34333 expression-stmt | x = x binop expr | x = expr binop x
34337 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34339 where x and v are lvalue expressions with scalar type. */
34342 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34344 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34345 tree rhs1
= NULL_TREE
, orig_lhs
;
34346 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34347 bool structured_block
= false;
34348 bool seq_cst
= false;
34350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34352 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34353 const char *p
= IDENTIFIER_POINTER (id
);
34355 if (!strcmp (p
, "seq_cst"))
34358 cp_lexer_consume_token (parser
->lexer
);
34359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34360 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34361 cp_lexer_consume_token (parser
->lexer
);
34364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34366 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34367 const char *p
= IDENTIFIER_POINTER (id
);
34369 if (!strcmp (p
, "read"))
34370 code
= OMP_ATOMIC_READ
;
34371 else if (!strcmp (p
, "write"))
34373 else if (!strcmp (p
, "update"))
34375 else if (!strcmp (p
, "capture"))
34376 code
= OMP_ATOMIC_CAPTURE_NEW
;
34380 cp_lexer_consume_token (parser
->lexer
);
34384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34385 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34386 cp_lexer_consume_token (parser
->lexer
);
34388 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34390 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34391 const char *p
= IDENTIFIER_POINTER (id
);
34393 if (!strcmp (p
, "seq_cst"))
34396 cp_lexer_consume_token (parser
->lexer
);
34400 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34404 case OMP_ATOMIC_READ
:
34405 case NOP_EXPR
: /* atomic write */
34406 v
= cp_parser_unary_expression (parser
);
34407 if (v
== error_mark_node
)
34409 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34411 if (code
== NOP_EXPR
)
34412 lhs
= cp_parser_expression (parser
);
34414 lhs
= cp_parser_unary_expression (parser
);
34415 if (lhs
== error_mark_node
)
34417 if (code
== NOP_EXPR
)
34419 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34427 case OMP_ATOMIC_CAPTURE_NEW
:
34428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34430 cp_lexer_consume_token (parser
->lexer
);
34431 structured_block
= true;
34435 v
= cp_parser_unary_expression (parser
);
34436 if (v
== error_mark_node
)
34438 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34446 lhs
= cp_parser_unary_expression (parser
);
34448 switch (TREE_CODE (lhs
))
34453 case POSTINCREMENT_EXPR
:
34454 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34455 code
= OMP_ATOMIC_CAPTURE_OLD
;
34457 case PREINCREMENT_EXPR
:
34458 lhs
= TREE_OPERAND (lhs
, 0);
34459 opcode
= PLUS_EXPR
;
34460 rhs
= integer_one_node
;
34463 case POSTDECREMENT_EXPR
:
34464 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34465 code
= OMP_ATOMIC_CAPTURE_OLD
;
34467 case PREDECREMENT_EXPR
:
34468 lhs
= TREE_OPERAND (lhs
, 0);
34469 opcode
= MINUS_EXPR
;
34470 rhs
= integer_one_node
;
34473 case COMPOUND_EXPR
:
34474 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34475 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34476 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34477 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34478 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34479 (TREE_OPERAND (lhs
, 1), 0), 0)))
34481 /* Undo effects of boolean_increment for post {in,de}crement. */
34482 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34485 if (TREE_CODE (lhs
) == MODIFY_EXPR
34486 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34488 /* Undo effects of boolean_increment. */
34489 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34491 /* This is pre or post increment. */
34492 rhs
= TREE_OPERAND (lhs
, 1);
34493 lhs
= TREE_OPERAND (lhs
, 0);
34495 if (code
== OMP_ATOMIC_CAPTURE_NEW
34496 && !structured_block
34497 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34498 code
= OMP_ATOMIC_CAPTURE_OLD
;
34504 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34507 opcode
= MULT_EXPR
;
34510 opcode
= TRUNC_DIV_EXPR
;
34513 opcode
= PLUS_EXPR
;
34516 opcode
= MINUS_EXPR
;
34518 case CPP_LSHIFT_EQ
:
34519 opcode
= LSHIFT_EXPR
;
34521 case CPP_RSHIFT_EQ
:
34522 opcode
= RSHIFT_EXPR
;
34525 opcode
= BIT_AND_EXPR
;
34528 opcode
= BIT_IOR_EXPR
;
34531 opcode
= BIT_XOR_EXPR
;
34534 enum cp_parser_prec oprec
;
34536 cp_lexer_consume_token (parser
->lexer
);
34537 cp_parser_parse_tentatively (parser
);
34538 rhs1
= cp_parser_simple_cast_expression (parser
);
34539 if (rhs1
== error_mark_node
)
34541 cp_parser_abort_tentative_parse (parser
);
34542 cp_parser_simple_cast_expression (parser
);
34545 token
= cp_lexer_peek_token (parser
->lexer
);
34546 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34548 cp_parser_abort_tentative_parse (parser
);
34549 cp_parser_parse_tentatively (parser
);
34550 rhs
= cp_parser_binary_expression (parser
, false, true,
34551 PREC_NOT_OPERATOR
, NULL
);
34552 if (rhs
== error_mark_node
)
34554 cp_parser_abort_tentative_parse (parser
);
34555 cp_parser_binary_expression (parser
, false, true,
34556 PREC_NOT_OPERATOR
, NULL
);
34559 switch (TREE_CODE (rhs
))
34562 case TRUNC_DIV_EXPR
:
34571 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34573 if (cp_parser_parse_definitely (parser
))
34575 opcode
= TREE_CODE (rhs
);
34576 rhs1
= TREE_OPERAND (rhs
, 0);
34577 rhs
= TREE_OPERAND (rhs
, 1);
34587 cp_parser_abort_tentative_parse (parser
);
34588 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34590 rhs
= cp_parser_expression (parser
);
34591 if (rhs
== error_mark_node
)
34597 cp_parser_error (parser
,
34598 "invalid form of %<#pragma omp atomic%>");
34601 if (!cp_parser_parse_definitely (parser
))
34603 switch (token
->type
)
34605 case CPP_SEMICOLON
:
34606 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34608 code
= OMP_ATOMIC_CAPTURE_OLD
;
34613 cp_lexer_consume_token (parser
->lexer
);
34616 else if (structured_block
)
34623 cp_parser_error (parser
,
34624 "invalid form of %<#pragma omp atomic%>");
34627 opcode
= MULT_EXPR
;
34630 opcode
= TRUNC_DIV_EXPR
;
34633 opcode
= PLUS_EXPR
;
34636 opcode
= MINUS_EXPR
;
34639 opcode
= LSHIFT_EXPR
;
34642 opcode
= RSHIFT_EXPR
;
34645 opcode
= BIT_AND_EXPR
;
34648 opcode
= BIT_IOR_EXPR
;
34651 opcode
= BIT_XOR_EXPR
;
34654 cp_parser_error (parser
,
34655 "invalid operator for %<#pragma omp atomic%>");
34658 oprec
= TOKEN_PRECEDENCE (token
);
34659 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34660 if (commutative_tree_code (opcode
))
34661 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34662 cp_lexer_consume_token (parser
->lexer
);
34663 rhs
= cp_parser_binary_expression (parser
, false, false,
34665 if (rhs
== error_mark_node
)
34670 cp_parser_error (parser
,
34671 "invalid operator for %<#pragma omp atomic%>");
34674 cp_lexer_consume_token (parser
->lexer
);
34676 rhs
= cp_parser_expression (parser
);
34677 if (rhs
== error_mark_node
)
34682 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34684 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34686 v
= cp_parser_unary_expression (parser
);
34687 if (v
== error_mark_node
)
34689 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34691 lhs1
= cp_parser_unary_expression (parser
);
34692 if (lhs1
== error_mark_node
)
34695 if (structured_block
)
34697 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34698 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34701 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34702 if (!structured_block
)
34703 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34707 cp_parser_skip_to_end_of_block_or_statement (parser
);
34708 if (structured_block
)
34710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34711 cp_lexer_consume_token (parser
->lexer
);
34712 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34714 cp_parser_skip_to_end_of_block_or_statement (parser
);
34715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34716 cp_lexer_consume_token (parser
->lexer
);
34723 # pragma omp barrier new-line */
34726 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34728 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34729 finish_omp_barrier ();
34733 # pragma omp critical [(name)] new-line
34737 # pragma omp critical [(name) [hint(expression)]] new-line
34738 structured-block */
34740 #define OMP_CRITICAL_CLAUSE_MASK \
34741 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34744 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34746 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34750 matching_parens parens
;
34751 parens
.consume_open (parser
);
34753 name
= cp_parser_identifier (parser
);
34755 if (name
== error_mark_node
34756 || !parens
.require_close (parser
))
34757 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34758 /*or_comma=*/false,
34759 /*consume_paren=*/true);
34760 if (name
== error_mark_node
)
34763 clauses
= cp_parser_omp_all_clauses (parser
,
34764 OMP_CRITICAL_CLAUSE_MASK
,
34765 "#pragma omp critical", pragma_tok
);
34768 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34770 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34771 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34775 # pragma omp flush flush-vars[opt] new-line
34778 ( variable-list ) */
34781 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34784 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34785 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34787 finish_omp_flush ();
34790 /* Helper function, to parse omp for increment expression. */
34793 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34795 tree cond
= cp_parser_binary_expression (parser
, false, true,
34796 PREC_NOT_OPERATOR
, NULL
);
34797 if (cond
== error_mark_node
34798 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34800 cp_parser_skip_to_end_of_statement (parser
);
34801 return error_mark_node
;
34804 switch (TREE_CODE (cond
))
34812 /* Fall through: OpenMP disallows NE_EXPR. */
34813 gcc_fallthrough ();
34815 return error_mark_node
;
34818 /* If decl is an iterator, preserve LHS and RHS of the relational
34819 expr until finish_omp_for. */
34821 && (type_dependent_expression_p (decl
)
34822 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34825 return build_x_binary_op (cp_expr_loc_or_loc (cond
, input_location
),
34827 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34828 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34829 /*overload=*/NULL
, tf_warning_or_error
);
34832 /* Helper function, to parse omp for increment expression. */
34835 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34837 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34843 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34845 op
= (token
->type
== CPP_PLUS_PLUS
34846 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34847 cp_lexer_consume_token (parser
->lexer
);
34848 lhs
= cp_parser_simple_cast_expression (parser
);
34850 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34851 return error_mark_node
;
34852 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34855 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34857 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34858 return error_mark_node
;
34860 token
= cp_lexer_peek_token (parser
->lexer
);
34861 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34863 op
= (token
->type
== CPP_PLUS_PLUS
34864 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34865 cp_lexer_consume_token (parser
->lexer
);
34866 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34869 op
= cp_parser_assignment_operator_opt (parser
);
34870 if (op
== ERROR_MARK
)
34871 return error_mark_node
;
34873 if (op
!= NOP_EXPR
)
34875 rhs
= cp_parser_assignment_expression (parser
);
34876 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34877 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34880 lhs
= cp_parser_binary_expression (parser
, false, false,
34881 PREC_ADDITIVE_EXPRESSION
, NULL
);
34882 token
= cp_lexer_peek_token (parser
->lexer
);
34883 decl_first
= (lhs
== decl
34884 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34887 if (token
->type
!= CPP_PLUS
34888 && token
->type
!= CPP_MINUS
)
34889 return error_mark_node
;
34893 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34894 cp_lexer_consume_token (parser
->lexer
);
34895 rhs
= cp_parser_binary_expression (parser
, false, false,
34896 PREC_ADDITIVE_EXPRESSION
, NULL
);
34897 token
= cp_lexer_peek_token (parser
->lexer
);
34898 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34900 if (lhs
== NULL_TREE
)
34902 if (op
== PLUS_EXPR
)
34905 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34906 tf_warning_or_error
);
34909 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34910 ERROR_MARK
, NULL
, tf_warning_or_error
);
34913 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34918 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34919 || op
== MINUS_EXPR
)
34920 return error_mark_node
;
34921 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34924 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34926 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34929 /* Parse the initialization statement of an OpenMP for loop.
34931 Return true if the resulting construct should have an
34932 OMP_CLAUSE_PRIVATE added to it. */
34935 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34936 tree
&this_pre_body
,
34937 vec
<tree
, va_gc
> *&for_block
,
34943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34946 tree add_private_clause
= NULL_TREE
;
34948 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34952 integer-type var = lb
34953 random-access-iterator-type var = lb
34954 pointer-type var = lb
34956 cp_decl_specifier_seq type_specifiers
;
34958 /* First, try to parse as an initialized declaration. See
34959 cp_parser_condition, from whence the bulk of this is copied. */
34961 cp_parser_parse_tentatively (parser
);
34962 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34963 /*is_trailing_return=*/false,
34965 if (cp_parser_parse_definitely (parser
))
34967 /* If parsing a type specifier seq succeeded, then this
34968 MUST be a initialized declaration. */
34969 tree asm_specification
, attributes
;
34970 cp_declarator
*declarator
;
34972 declarator
= cp_parser_declarator (parser
,
34973 CP_PARSER_DECLARATOR_NAMED
,
34974 /*ctor_dtor_or_conv_p=*/NULL
,
34975 /*parenthesized_p=*/NULL
,
34976 /*member_p=*/false,
34977 /*friend_p=*/false);
34978 attributes
= cp_parser_attributes_opt (parser
);
34979 asm_specification
= cp_parser_asm_specification_opt (parser
);
34981 if (declarator
== cp_error_declarator
)
34982 cp_parser_skip_to_end_of_statement (parser
);
34986 tree pushed_scope
, auto_node
;
34988 decl
= start_decl (declarator
, &type_specifiers
,
34989 SD_INITIALIZED
, attributes
,
34990 /*prefix_attributes=*/NULL_TREE
,
34993 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34996 if (cp_lexer_next_token_is (parser
->lexer
,
34998 error ("parenthesized initialization is not allowed in "
34999 "OpenMP %<for%> loop");
35001 /* Trigger an error. */
35002 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35004 init
= error_mark_node
;
35005 cp_parser_skip_to_end_of_statement (parser
);
35007 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
35008 || type_dependent_expression_p (decl
)
35011 bool is_direct_init
, is_non_constant_init
;
35013 init
= cp_parser_initializer (parser
,
35015 &is_non_constant_init
);
35020 = do_auto_deduction (TREE_TYPE (decl
), init
,
35023 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35024 && !type_dependent_expression_p (decl
))
35028 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35030 LOOKUP_ONLYCONVERTING
);
35032 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35034 vec_safe_push (for_block
, this_pre_body
);
35039 init
= pop_stmt_list (this_pre_body
);
35040 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35042 tree_stmt_iterator i
= tsi_start (init
);
35043 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35044 while (!tsi_end_p (i
))
35046 tree t
= tsi_stmt (i
);
35047 if (TREE_CODE (t
) == DECL_EXPR
35048 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35051 vec_safe_push (for_block
, t
);
35056 if (tsi_one_before_end_p (i
))
35058 tree t
= tsi_stmt (i
);
35060 free_stmt_list (init
);
35065 this_pre_body
= NULL_TREE
;
35070 cp_lexer_consume_token (parser
->lexer
);
35071 init
= cp_parser_assignment_expression (parser
);
35074 if (TYPE_REF_P (TREE_TYPE (decl
)))
35075 init
= error_mark_node
;
35077 cp_finish_decl (decl
, NULL_TREE
,
35078 /*init_const_expr_p=*/false,
35080 LOOKUP_ONLYCONVERTING
);
35084 pop_scope (pushed_scope
);
35090 /* If parsing a type specifier sequence failed, then
35091 this MUST be a simple expression. */
35092 cp_parser_parse_tentatively (parser
);
35093 decl
= cp_parser_primary_expression (parser
, false, false,
35095 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35096 if (!cp_parser_error_occurred (parser
)
35098 && (TREE_CODE (decl
) == COMPONENT_REF
35099 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35101 cp_parser_abort_tentative_parse (parser
);
35102 cp_parser_parse_tentatively (parser
);
35103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35104 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35105 /*check_dependency_p=*/true,
35106 /*template_p=*/NULL
,
35107 /*declarator_p=*/false,
35108 /*optional_p=*/false);
35109 if (name
!= error_mark_node
35110 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35112 decl
= cp_parser_lookup_name_simple (parser
, name
,
35114 if (TREE_CODE (decl
) == FIELD_DECL
)
35115 add_private_clause
= omp_privatize_field (decl
, false);
35117 cp_parser_abort_tentative_parse (parser
);
35118 cp_parser_parse_tentatively (parser
);
35119 decl
= cp_parser_primary_expression (parser
, false, false,
35122 if (!cp_parser_error_occurred (parser
)
35125 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35129 cp_parser_parse_definitely (parser
);
35130 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35131 rhs
= cp_parser_assignment_expression (parser
);
35133 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35136 tf_warning_or_error
));
35137 if (!add_private_clause
)
35138 add_private_clause
= decl
;
35143 cp_parser_abort_tentative_parse (parser
);
35144 init
= cp_parser_expression (parser
);
35147 if (TREE_CODE (init
) == MODIFY_EXPR
35148 || TREE_CODE (init
) == MODOP_EXPR
)
35149 real_decl
= TREE_OPERAND (init
, 0);
35153 return add_private_clause
;
35156 /* Parse the restricted form of the for statement allowed by OpenMP. */
35159 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35160 tree
*cclauses
, bool *if_p
)
35162 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35163 tree real_decl
, initv
, condv
, incrv
, declv
;
35164 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35165 location_t loc_first
;
35166 bool collapse_err
= false;
35167 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35168 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35169 auto_vec
<tree
, 4> orig_inits
;
35170 bool tiling
= false;
35172 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35173 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35174 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35175 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35178 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35180 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35181 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35184 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35187 if (ordered
&& ordered
< collapse
)
35189 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35190 "%<ordered%> clause parameter is less than %<collapse%>");
35191 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35192 = build_int_cst (NULL_TREE
, collapse
);
35193 ordered
= collapse
;
35197 for (tree
*pc
= &clauses
; *pc
; )
35198 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35200 error_at (OMP_CLAUSE_LOCATION (*pc
),
35201 "%<linear%> clause may not be specified together "
35202 "with %<ordered%> clause with a parameter");
35203 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35206 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35209 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35210 count
= ordered
? ordered
: collapse
;
35212 declv
= make_tree_vec (count
);
35213 initv
= make_tree_vec (count
);
35214 condv
= make_tree_vec (count
);
35215 incrv
= make_tree_vec (count
);
35217 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35219 for (i
= 0; i
< count
; i
++)
35221 int bracecount
= 0;
35222 tree add_private_clause
= NULL_TREE
;
35225 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35228 cp_parser_error (parser
, "for statement expected");
35231 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35233 matching_parens parens
;
35234 if (!parens
.require_open (parser
))
35237 init
= orig_init
= decl
= real_decl
= NULL
;
35238 this_pre_body
= push_stmt_list ();
35241 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35242 init
, orig_init
, decl
, real_decl
);
35244 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35247 this_pre_body
= pop_stmt_list (this_pre_body
);
35251 pre_body
= push_stmt_list ();
35253 add_stmt (this_pre_body
);
35254 pre_body
= pop_stmt_list (pre_body
);
35257 pre_body
= this_pre_body
;
35262 if (cclauses
!= NULL
35263 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35264 && real_decl
!= NULL_TREE
)
35267 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35268 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35269 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35271 error_at (loc
, "iteration variable %qD"
35272 " should not be firstprivate", real_decl
);
35273 *c
= OMP_CLAUSE_CHAIN (*c
);
35275 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35276 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35278 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35280 *c
= OMP_CLAUSE_CHAIN (*c
);
35281 if (code
== OMP_SIMD
)
35283 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35284 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35288 OMP_CLAUSE_CHAIN (l
) = clauses
;
35291 add_private_clause
= NULL_TREE
;
35295 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35296 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35297 add_private_clause
= NULL_TREE
;
35298 c
= &OMP_CLAUSE_CHAIN (*c
);
35302 if (add_private_clause
)
35305 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35307 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35308 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35309 && OMP_CLAUSE_DECL (c
) == decl
)
35311 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35312 && OMP_CLAUSE_DECL (c
) == decl
)
35313 error_at (loc
, "iteration variable %qD "
35314 "should not be firstprivate",
35316 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35317 && OMP_CLAUSE_DECL (c
) == decl
)
35318 error_at (loc
, "iteration variable %qD should not be reduction",
35323 if (code
!= OMP_SIMD
)
35324 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35325 else if (collapse
== 1)
35326 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35328 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35329 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35330 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35333 OMP_CLAUSE_CHAIN (c
) = clauses
;
35335 /* For linear, signal that we need to fill up
35336 the so far unknown linear step. */
35337 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35338 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35344 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35345 cond
= cp_parser_omp_for_cond (parser
, decl
);
35346 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35349 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35351 /* If decl is an iterator, preserve the operator on decl
35352 until finish_omp_for. */
35354 && ((processing_template_decl
35355 && (TREE_TYPE (real_decl
) == NULL_TREE
35356 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35357 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35358 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35360 incr
= cp_parser_expression (parser
);
35361 if (!EXPR_HAS_LOCATION (incr
))
35362 protected_set_expr_location (incr
, input_location
);
35365 if (!parens
.require_close (parser
))
35366 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35367 /*or_comma=*/false,
35368 /*consume_paren=*/true);
35370 TREE_VEC_ELT (declv
, i
) = decl
;
35371 TREE_VEC_ELT (initv
, i
) = init
;
35372 TREE_VEC_ELT (condv
, i
) = cond
;
35373 TREE_VEC_ELT (incrv
, i
) = incr
;
35376 orig_inits
.safe_grow_cleared (i
+ 1);
35377 orig_inits
[i
] = orig_init
;
35380 if (i
== count
- 1)
35383 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35384 in between the collapsed for loops to be still considered perfectly
35385 nested. Hopefully the final version clarifies this.
35386 For now handle (multiple) {'s and empty statements. */
35387 cp_parser_parse_tentatively (parser
);
35390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35392 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35394 cp_lexer_consume_token (parser
->lexer
);
35397 else if (bracecount
35398 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35399 cp_lexer_consume_token (parser
->lexer
);
35402 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35403 error_at (loc
, "not enough for loops to collapse");
35404 collapse_err
= true;
35405 cp_parser_abort_tentative_parse (parser
);
35413 cp_parser_parse_definitely (parser
);
35414 nbraces
+= bracecount
;
35421 /* Note that we saved the original contents of this flag when we entered
35422 the structured block, and so we don't need to re-save it here. */
35423 parser
->in_statement
= IN_OMP_FOR
;
35425 /* Note that the grammar doesn't call for a structured block here,
35426 though the loop as a whole is a structured block. */
35427 body
= push_stmt_list ();
35428 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35429 body
= pop_stmt_list (body
);
35431 if (declv
== NULL_TREE
)
35434 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35435 body
, pre_body
, &orig_inits
, clauses
);
35439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35441 cp_lexer_consume_token (parser
->lexer
);
35444 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35445 cp_lexer_consume_token (parser
->lexer
);
35450 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35451 "collapsed loops not perfectly nested");
35453 collapse_err
= true;
35454 cp_parser_statement_seq_opt (parser
, NULL
);
35455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35460 while (!for_block
->is_empty ())
35462 tree t
= for_block
->pop ();
35463 if (TREE_CODE (t
) == STATEMENT_LIST
)
35464 add_stmt (pop_stmt_list (t
));
35468 release_tree_vector (for_block
);
35473 /* Helper function for OpenMP parsing, split clauses and call
35474 finish_omp_clauses on each of the set of clauses afterwards. */
35477 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35478 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35481 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35482 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35484 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35488 #pragma omp simd simd-clause[optseq] new-line
35491 #define OMP_SIMD_CLAUSE_MASK \
35492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35502 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35503 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35506 tree clauses
, sb
, ret
;
35508 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35510 strcat (p_name
, " simd");
35511 mask
|= OMP_SIMD_CLAUSE_MASK
;
35513 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35517 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35518 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35519 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35520 OMP_CLAUSE_ORDERED
);
35521 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35523 error_at (OMP_CLAUSE_LOCATION (c
),
35524 "%<ordered%> clause with parameter may not be specified "
35525 "on %qs construct", p_name
);
35526 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35530 sb
= begin_omp_structured_block ();
35531 save
= cp_parser_begin_omp_structured_block (parser
);
35533 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35535 cp_parser_end_omp_structured_block (parser
, save
);
35536 add_stmt (finish_omp_structured_block (sb
));
35542 #pragma omp for for-clause[optseq] new-line
35546 #pragma omp for simd for-simd-clause[optseq] new-line
35549 #define OMP_FOR_CLAUSE_MASK \
35550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35561 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35562 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35565 tree clauses
, sb
, ret
;
35567 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35569 strcat (p_name
, " for");
35570 mask
|= OMP_FOR_CLAUSE_MASK
;
35571 /* parallel for{, simd} disallows nowait clause, but for
35572 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35573 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35574 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35575 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35576 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35577 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35581 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35582 const char *p
= IDENTIFIER_POINTER (id
);
35584 if (strcmp (p
, "simd") == 0)
35586 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35587 if (cclauses
== NULL
)
35588 cclauses
= cclauses_buf
;
35590 cp_lexer_consume_token (parser
->lexer
);
35591 if (!flag_openmp
) /* flag_openmp_simd */
35592 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35594 sb
= begin_omp_structured_block ();
35595 save
= cp_parser_begin_omp_structured_block (parser
);
35596 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35598 cp_parser_end_omp_structured_block (parser
, save
);
35599 tree body
= finish_omp_structured_block (sb
);
35602 ret
= make_node (OMP_FOR
);
35603 TREE_TYPE (ret
) = void_type_node
;
35604 OMP_FOR_BODY (ret
) = body
;
35605 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35606 SET_EXPR_LOCATION (ret
, loc
);
35611 if (!flag_openmp
) /* flag_openmp_simd */
35613 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35617 /* Composite distribute parallel for disallows linear clause. */
35618 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35619 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35621 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35625 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35626 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35629 sb
= begin_omp_structured_block ();
35630 save
= cp_parser_begin_omp_structured_block (parser
);
35632 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35634 cp_parser_end_omp_structured_block (parser
, save
);
35635 add_stmt (finish_omp_structured_block (sb
));
35641 # pragma omp master new-line
35642 structured-block */
35645 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35647 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35648 return c_finish_omp_master (input_location
,
35649 cp_parser_omp_structured_block (parser
, if_p
));
35653 # pragma omp ordered new-line
35657 # pragma omp ordered ordered-clauses new-line
35658 structured-block */
35660 #define OMP_ORDERED_CLAUSE_MASK \
35661 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35664 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35665 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35668 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35669 enum pragma_context context
, bool *if_p
)
35671 location_t loc
= pragma_tok
->location
;
35673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35675 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35676 const char *p
= IDENTIFIER_POINTER (id
);
35678 if (strcmp (p
, "depend") == 0)
35680 if (!flag_openmp
) /* flag_openmp_simd */
35682 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35685 if (context
== pragma_stmt
)
35687 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35688 "%<depend%> clause may only be used in compound "
35690 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35694 = cp_parser_omp_all_clauses (parser
,
35695 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35696 "#pragma omp ordered", pragma_tok
);
35697 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35703 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35704 "#pragma omp ordered", pragma_tok
);
35706 if (!flag_openmp
/* flag_openmp_simd */
35707 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35710 c_finish_omp_ordered (loc
, clauses
,
35711 cp_parser_omp_structured_block (parser
, if_p
));
35718 { section-sequence }
35721 section-directive[opt] structured-block
35722 section-sequence section-directive structured-block */
35725 cp_parser_omp_sections_scope (cp_parser
*parser
)
35727 tree stmt
, substmt
;
35728 bool error_suppress
= false;
35731 matching_braces braces
;
35732 if (!braces
.require_open (parser
))
35735 stmt
= push_stmt_list ();
35737 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35738 != PRAGMA_OMP_SECTION
)
35740 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35741 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35742 add_stmt (substmt
);
35747 tok
= cp_lexer_peek_token (parser
->lexer
);
35748 if (tok
->type
== CPP_CLOSE_BRACE
)
35750 if (tok
->type
== CPP_EOF
)
35753 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35755 cp_lexer_consume_token (parser
->lexer
);
35756 cp_parser_require_pragma_eol (parser
, tok
);
35757 error_suppress
= false;
35759 else if (!error_suppress
)
35761 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35762 error_suppress
= true;
35765 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35766 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35767 add_stmt (substmt
);
35769 braces
.require_close (parser
);
35771 substmt
= pop_stmt_list (stmt
);
35773 stmt
= make_node (OMP_SECTIONS
);
35774 TREE_TYPE (stmt
) = void_type_node
;
35775 OMP_SECTIONS_BODY (stmt
) = substmt
;
35782 # pragma omp sections sections-clause[optseq] newline
35785 #define OMP_SECTIONS_CLAUSE_MASK \
35786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35794 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35797 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35799 strcat (p_name
, " sections");
35800 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35802 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35804 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35808 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35809 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35812 ret
= cp_parser_omp_sections_scope (parser
);
35814 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35820 # pragma omp parallel parallel-clause[optseq] new-line
35822 # pragma omp parallel for parallel-for-clause[optseq] new-line
35824 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35828 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35829 structured-block */
35831 #define OMP_PARALLEL_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35843 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35844 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35847 tree stmt
, clauses
, block
;
35849 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35851 strcat (p_name
, " parallel");
35852 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35853 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35854 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35855 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35856 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35858 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35860 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35861 if (cclauses
== NULL
)
35862 cclauses
= cclauses_buf
;
35864 cp_lexer_consume_token (parser
->lexer
);
35865 if (!flag_openmp
) /* flag_openmp_simd */
35866 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35868 block
= begin_omp_parallel ();
35869 save
= cp_parser_begin_omp_structured_block (parser
);
35870 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35872 cp_parser_end_omp_structured_block (parser
, save
);
35873 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35875 if (ret
== NULL_TREE
)
35877 OMP_PARALLEL_COMBINED (stmt
) = 1;
35880 /* When combined with distribute, parallel has to be followed by for.
35881 #pragma omp target parallel is allowed though. */
35883 && (mask
& (OMP_CLAUSE_MASK_1
35884 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35886 error_at (loc
, "expected %<for%> after %qs", p_name
);
35887 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35890 else if (!flag_openmp
) /* flag_openmp_simd */
35892 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35895 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35897 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35898 const char *p
= IDENTIFIER_POINTER (id
);
35899 if (strcmp (p
, "sections") == 0)
35901 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35902 cclauses
= cclauses_buf
;
35904 cp_lexer_consume_token (parser
->lexer
);
35905 block
= begin_omp_parallel ();
35906 save
= cp_parser_begin_omp_structured_block (parser
);
35907 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35908 cp_parser_end_omp_structured_block (parser
, save
);
35909 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35911 OMP_PARALLEL_COMBINED (stmt
) = 1;
35916 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35920 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35921 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35924 block
= begin_omp_parallel ();
35925 save
= cp_parser_begin_omp_structured_block (parser
);
35926 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35927 cp_parser_end_omp_structured_block (parser
, save
);
35928 stmt
= finish_omp_parallel (clauses
, block
);
35933 # pragma omp single single-clause[optseq] new-line
35934 structured-block */
35936 #define OMP_SINGLE_CLAUSE_MASK \
35937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35943 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35945 tree stmt
= make_node (OMP_SINGLE
);
35946 TREE_TYPE (stmt
) = void_type_node
;
35948 OMP_SINGLE_CLAUSES (stmt
)
35949 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35950 "#pragma omp single", pragma_tok
);
35951 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35953 return add_stmt (stmt
);
35957 # pragma omp task task-clause[optseq] new-line
35958 structured-block */
35960 #define OMP_TASK_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35973 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35975 tree clauses
, block
;
35978 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35979 "#pragma omp task", pragma_tok
);
35980 block
= begin_omp_task ();
35981 save
= cp_parser_begin_omp_structured_block (parser
);
35982 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35983 cp_parser_end_omp_structured_block (parser
, save
);
35984 return finish_omp_task (clauses
, block
);
35988 # pragma omp taskwait new-line */
35991 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35993 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35994 finish_omp_taskwait ();
35998 # pragma omp taskyield new-line */
36001 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
36003 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36004 finish_omp_taskyield ();
36008 # pragma omp taskgroup new-line
36009 structured-block */
36012 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36014 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36015 return c_finish_omp_taskgroup (input_location
,
36016 cp_parser_omp_structured_block (parser
,
36022 # pragma omp threadprivate (variable-list) */
36025 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36029 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36030 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36032 finish_omp_threadprivate (vars
);
36036 # pragma omp cancel cancel-clause[optseq] new-line */
36038 #define OMP_CANCEL_CLAUSE_MASK \
36039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36046 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36048 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36049 "#pragma omp cancel", pragma_tok
);
36050 finish_omp_cancel (clauses
);
36054 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36056 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36063 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36064 enum pragma_context context
)
36067 bool point_seen
= false;
36069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36071 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36072 const char *p
= IDENTIFIER_POINTER (id
);
36074 if (strcmp (p
, "point") == 0)
36076 cp_lexer_consume_token (parser
->lexer
);
36082 cp_parser_error (parser
, "expected %<point%>");
36083 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36087 if (context
!= pragma_compound
)
36089 if (context
== pragma_stmt
)
36090 error_at (pragma_tok
->location
,
36091 "%<#pragma %s%> may only be used in compound statements",
36092 "omp cancellation point");
36094 cp_parser_error (parser
, "expected declaration specifiers");
36095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36099 clauses
= cp_parser_omp_all_clauses (parser
,
36100 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36101 "#pragma omp cancellation point",
36103 finish_omp_cancellation_point (clauses
);
36107 #pragma omp distribute distribute-clause[optseq] new-line
36110 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36118 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36119 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36122 tree clauses
, sb
, ret
;
36124 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36126 strcat (p_name
, " distribute");
36127 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36131 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36132 const char *p
= IDENTIFIER_POINTER (id
);
36134 bool parallel
= false;
36136 if (strcmp (p
, "simd") == 0)
36139 parallel
= strcmp (p
, "parallel") == 0;
36140 if (parallel
|| simd
)
36142 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36143 if (cclauses
== NULL
)
36144 cclauses
= cclauses_buf
;
36145 cp_lexer_consume_token (parser
->lexer
);
36146 if (!flag_openmp
) /* flag_openmp_simd */
36149 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36152 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36155 sb
= begin_omp_structured_block ();
36156 save
= cp_parser_begin_omp_structured_block (parser
);
36158 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36161 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36163 cp_parser_end_omp_structured_block (parser
, save
);
36164 tree body
= finish_omp_structured_block (sb
);
36167 ret
= make_node (OMP_DISTRIBUTE
);
36168 TREE_TYPE (ret
) = void_type_node
;
36169 OMP_FOR_BODY (ret
) = body
;
36170 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36171 SET_EXPR_LOCATION (ret
, loc
);
36176 if (!flag_openmp
) /* flag_openmp_simd */
36178 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36182 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36186 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36187 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36190 sb
= begin_omp_structured_block ();
36191 save
= cp_parser_begin_omp_structured_block (parser
);
36193 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36195 cp_parser_end_omp_structured_block (parser
, save
);
36196 add_stmt (finish_omp_structured_block (sb
));
36202 # pragma omp teams teams-clause[optseq] new-line
36203 structured-block */
36205 #define OMP_TEAMS_CLAUSE_MASK \
36206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36215 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36216 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36219 tree clauses
, sb
, ret
;
36221 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36223 strcat (p_name
, " teams");
36224 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36228 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36229 const char *p
= IDENTIFIER_POINTER (id
);
36230 if (strcmp (p
, "distribute") == 0)
36232 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36233 if (cclauses
== NULL
)
36234 cclauses
= cclauses_buf
;
36236 cp_lexer_consume_token (parser
->lexer
);
36237 if (!flag_openmp
) /* flag_openmp_simd */
36238 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36240 sb
= begin_omp_structured_block ();
36241 save
= cp_parser_begin_omp_structured_block (parser
);
36242 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36244 cp_parser_end_omp_structured_block (parser
, save
);
36245 tree body
= finish_omp_structured_block (sb
);
36248 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36249 ret
= make_node (OMP_TEAMS
);
36250 TREE_TYPE (ret
) = void_type_node
;
36251 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36252 OMP_TEAMS_BODY (ret
) = body
;
36253 OMP_TEAMS_COMBINED (ret
) = 1;
36254 SET_EXPR_LOCATION (ret
, loc
);
36255 return add_stmt (ret
);
36258 if (!flag_openmp
) /* flag_openmp_simd */
36260 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36264 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36268 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36269 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36272 tree stmt
= make_node (OMP_TEAMS
);
36273 TREE_TYPE (stmt
) = void_type_node
;
36274 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36275 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36276 SET_EXPR_LOCATION (stmt
, loc
);
36278 return add_stmt (stmt
);
36282 # pragma omp target data target-data-clause[optseq] new-line
36283 structured-block */
36285 #define OMP_TARGET_DATA_CLAUSE_MASK \
36286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36292 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36295 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36296 "#pragma omp target data", pragma_tok
);
36298 for (tree
*pc
= &clauses
; *pc
;)
36300 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36301 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36304 case GOMP_MAP_ALWAYS_TO
:
36305 case GOMP_MAP_FROM
:
36306 case GOMP_MAP_ALWAYS_FROM
:
36307 case GOMP_MAP_TOFROM
:
36308 case GOMP_MAP_ALWAYS_TOFROM
:
36309 case GOMP_MAP_ALLOC
:
36312 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36313 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36314 case GOMP_MAP_ALWAYS_POINTER
:
36318 error_at (OMP_CLAUSE_LOCATION (*pc
),
36319 "%<#pragma omp target data%> with map-type other "
36320 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36321 "on %<map%> clause");
36322 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36325 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36331 error_at (pragma_tok
->location
,
36332 "%<#pragma omp target data%> must contain at least "
36333 "one %<map%> clause");
36337 tree stmt
= make_node (OMP_TARGET_DATA
);
36338 TREE_TYPE (stmt
) = void_type_node
;
36339 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36341 keep_next_level (true);
36342 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36344 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36345 return add_stmt (stmt
);
36349 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36350 structured-block */
36352 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36353 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36360 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36361 enum pragma_context context
)
36363 bool data_seen
= false;
36364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36366 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36367 const char *p
= IDENTIFIER_POINTER (id
);
36369 if (strcmp (p
, "data") == 0)
36371 cp_lexer_consume_token (parser
->lexer
);
36377 cp_parser_error (parser
, "expected %<data%>");
36378 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36382 if (context
== pragma_stmt
)
36384 error_at (pragma_tok
->location
,
36385 "%<#pragma %s%> may only be used in compound statements",
36386 "omp target enter data");
36387 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36392 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36393 "#pragma omp target enter data", pragma_tok
);
36395 for (tree
*pc
= &clauses
; *pc
;)
36397 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36398 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36401 case GOMP_MAP_ALWAYS_TO
:
36402 case GOMP_MAP_ALLOC
:
36405 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36406 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36407 case GOMP_MAP_ALWAYS_POINTER
:
36411 error_at (OMP_CLAUSE_LOCATION (*pc
),
36412 "%<#pragma omp target enter data%> with map-type other "
36413 "than %<to%> or %<alloc%> on %<map%> clause");
36414 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36417 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36423 error_at (pragma_tok
->location
,
36424 "%<#pragma omp target enter data%> must contain at least "
36425 "one %<map%> clause");
36429 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36430 TREE_TYPE (stmt
) = void_type_node
;
36431 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36432 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36433 return add_stmt (stmt
);
36437 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36438 structured-block */
36440 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36441 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36448 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36449 enum pragma_context context
)
36451 bool data_seen
= false;
36452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36454 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36455 const char *p
= IDENTIFIER_POINTER (id
);
36457 if (strcmp (p
, "data") == 0)
36459 cp_lexer_consume_token (parser
->lexer
);
36465 cp_parser_error (parser
, "expected %<data%>");
36466 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36470 if (context
== pragma_stmt
)
36472 error_at (pragma_tok
->location
,
36473 "%<#pragma %s%> may only be used in compound statements",
36474 "omp target exit data");
36475 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36480 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36481 "#pragma omp target exit data", pragma_tok
);
36483 for (tree
*pc
= &clauses
; *pc
;)
36485 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36486 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36488 case GOMP_MAP_FROM
:
36489 case GOMP_MAP_ALWAYS_FROM
:
36490 case GOMP_MAP_RELEASE
:
36491 case GOMP_MAP_DELETE
:
36494 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36495 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36496 case GOMP_MAP_ALWAYS_POINTER
:
36500 error_at (OMP_CLAUSE_LOCATION (*pc
),
36501 "%<#pragma omp target exit data%> with map-type other "
36502 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36504 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36507 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36513 error_at (pragma_tok
->location
,
36514 "%<#pragma omp target exit data%> must contain at least "
36515 "one %<map%> clause");
36519 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36520 TREE_TYPE (stmt
) = void_type_node
;
36521 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36522 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36523 return add_stmt (stmt
);
36527 # pragma omp target update target-update-clause[optseq] new-line */
36529 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36538 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36539 enum pragma_context context
)
36541 if (context
== pragma_stmt
)
36543 error_at (pragma_tok
->location
,
36544 "%<#pragma %s%> may only be used in compound statements",
36545 "omp target update");
36546 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36551 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36552 "#pragma omp target update", pragma_tok
);
36553 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36554 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36556 error_at (pragma_tok
->location
,
36557 "%<#pragma omp target update%> must contain at least one "
36558 "%<from%> or %<to%> clauses");
36562 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36563 TREE_TYPE (stmt
) = void_type_node
;
36564 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36565 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36571 # pragma omp target target-clause[optseq] new-line
36572 structured-block */
36574 #define OMP_TARGET_CLAUSE_MASK \
36575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36586 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36587 enum pragma_context context
, bool *if_p
)
36589 tree
*pc
= NULL
, stmt
;
36591 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36593 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36594 const char *p
= IDENTIFIER_POINTER (id
);
36595 enum tree_code ccode
= ERROR_MARK
;
36597 if (strcmp (p
, "teams") == 0)
36599 else if (strcmp (p
, "parallel") == 0)
36600 ccode
= OMP_PARALLEL
;
36601 else if (strcmp (p
, "simd") == 0)
36603 if (ccode
!= ERROR_MARK
)
36605 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36606 char p_name
[sizeof ("#pragma omp target teams distribute "
36607 "parallel for simd")];
36609 cp_lexer_consume_token (parser
->lexer
);
36610 strcpy (p_name
, "#pragma omp target");
36611 if (!flag_openmp
) /* flag_openmp_simd */
36617 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36618 OMP_TARGET_CLAUSE_MASK
,
36622 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36623 OMP_TARGET_CLAUSE_MASK
,
36627 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36628 OMP_TARGET_CLAUSE_MASK
,
36632 gcc_unreachable ();
36634 return stmt
!= NULL_TREE
;
36636 keep_next_level (true);
36637 tree sb
= begin_omp_structured_block (), ret
;
36638 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36642 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36643 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36647 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36648 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36652 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36653 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36657 gcc_unreachable ();
36659 cp_parser_end_omp_structured_block (parser
, save
);
36660 tree body
= finish_omp_structured_block (sb
);
36661 if (ret
== NULL_TREE
)
36663 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36665 /* For combined target teams, ensure the num_teams and
36666 thread_limit clause expressions are evaluated on the host,
36667 before entering the target construct. */
36669 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36670 c
; c
= OMP_CLAUSE_CHAIN (c
))
36671 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36672 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36673 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36675 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36676 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36677 if (expr
== error_mark_node
)
36679 tree tmp
= TARGET_EXPR_SLOT (expr
);
36681 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36682 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36683 OMP_CLAUSE_FIRSTPRIVATE
);
36684 OMP_CLAUSE_DECL (tc
) = tmp
;
36685 OMP_CLAUSE_CHAIN (tc
)
36686 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36687 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36690 tree stmt
= make_node (OMP_TARGET
);
36691 TREE_TYPE (stmt
) = void_type_node
;
36692 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36693 OMP_TARGET_BODY (stmt
) = body
;
36694 OMP_TARGET_COMBINED (stmt
) = 1;
36695 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36697 pc
= &OMP_TARGET_CLAUSES (stmt
);
36698 goto check_clauses
;
36700 else if (!flag_openmp
) /* flag_openmp_simd */
36702 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36705 else if (strcmp (p
, "data") == 0)
36707 cp_lexer_consume_token (parser
->lexer
);
36708 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36711 else if (strcmp (p
, "enter") == 0)
36713 cp_lexer_consume_token (parser
->lexer
);
36714 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36717 else if (strcmp (p
, "exit") == 0)
36719 cp_lexer_consume_token (parser
->lexer
);
36720 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36723 else if (strcmp (p
, "update") == 0)
36725 cp_lexer_consume_token (parser
->lexer
);
36726 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36729 if (!flag_openmp
) /* flag_openmp_simd */
36731 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36735 stmt
= make_node (OMP_TARGET
);
36736 TREE_TYPE (stmt
) = void_type_node
;
36738 OMP_TARGET_CLAUSES (stmt
)
36739 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36740 "#pragma omp target", pragma_tok
);
36741 pc
= &OMP_TARGET_CLAUSES (stmt
);
36742 keep_next_level (true);
36743 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36745 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36751 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36752 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36755 case GOMP_MAP_ALWAYS_TO
:
36756 case GOMP_MAP_FROM
:
36757 case GOMP_MAP_ALWAYS_FROM
:
36758 case GOMP_MAP_TOFROM
:
36759 case GOMP_MAP_ALWAYS_TOFROM
:
36760 case GOMP_MAP_ALLOC
:
36761 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36762 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36763 case GOMP_MAP_ALWAYS_POINTER
:
36766 error_at (OMP_CLAUSE_LOCATION (*pc
),
36767 "%<#pragma omp target%> with map-type other "
36768 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36769 "on %<map%> clause");
36770 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36773 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36779 # pragma acc cache (variable-list) new-line
36783 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36785 tree stmt
, clauses
;
36787 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36788 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36790 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36792 stmt
= make_node (OACC_CACHE
);
36793 TREE_TYPE (stmt
) = void_type_node
;
36794 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36795 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36802 # pragma acc data oacc-data-clause[optseq] new-line
36803 structured-block */
36805 #define OACC_DATA_CLAUSE_MASK \
36806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36815 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36817 tree stmt
, clauses
, block
;
36820 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36821 "#pragma acc data", pragma_tok
);
36823 block
= begin_omp_parallel ();
36824 save
= cp_parser_begin_omp_structured_block (parser
);
36825 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36826 cp_parser_end_omp_structured_block (parser
, save
);
36827 stmt
= finish_oacc_data (clauses
, block
);
36832 # pragma acc host_data <clauses> new-line
36833 structured-block */
36835 #define OACC_HOST_DATA_CLAUSE_MASK \
36836 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36839 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36841 tree stmt
, clauses
, block
;
36844 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36845 "#pragma acc host_data", pragma_tok
);
36847 block
= begin_omp_parallel ();
36848 save
= cp_parser_begin_omp_structured_block (parser
);
36849 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36850 cp_parser_end_omp_structured_block (parser
, save
);
36851 stmt
= finish_oacc_host_data (clauses
, block
);
36856 # pragma acc declare oacc-data-clause[optseq] new-line
36859 #define OACC_DECLARE_CLAUSE_MASK \
36860 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36870 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36872 tree clauses
, stmt
;
36873 bool error
= false;
36875 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36876 "#pragma acc declare", pragma_tok
, true);
36879 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36881 error_at (pragma_tok
->location
,
36882 "no valid clauses specified in %<#pragma acc declare%>");
36886 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36888 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36889 tree decl
= OMP_CLAUSE_DECL (t
);
36890 if (!DECL_P (decl
))
36892 error_at (loc
, "array section in %<#pragma acc declare%>");
36896 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36897 switch (OMP_CLAUSE_MAP_KIND (t
))
36899 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36900 case GOMP_MAP_ALLOC
:
36902 case GOMP_MAP_FORCE_DEVICEPTR
:
36903 case GOMP_MAP_DEVICE_RESIDENT
:
36906 case GOMP_MAP_LINK
:
36907 if (!global_bindings_p ()
36908 && (TREE_STATIC (decl
)
36909 || !DECL_EXTERNAL (decl
)))
36912 "%qD must be a global variable in "
36913 "%<#pragma acc declare link%>",
36921 if (global_bindings_p ())
36923 error_at (loc
, "invalid OpenACC clause at file scope");
36927 if (DECL_EXTERNAL (decl
))
36930 "invalid use of %<extern%> variable %qD "
36931 "in %<#pragma acc declare%>", decl
);
36935 else if (TREE_PUBLIC (decl
))
36938 "invalid use of %<global%> variable %qD "
36939 "in %<#pragma acc declare%>", decl
);
36946 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36947 || lookup_attribute ("omp declare target link",
36948 DECL_ATTRIBUTES (decl
)))
36950 error_at (loc
, "variable %qD used more than once with "
36951 "%<#pragma acc declare%>", decl
);
36960 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36961 id
= get_identifier ("omp declare target link");
36963 id
= get_identifier ("omp declare target");
36965 DECL_ATTRIBUTES (decl
)
36966 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36967 if (global_bindings_p ())
36969 symtab_node
*node
= symtab_node::get (decl
);
36972 node
->offloadable
= 1;
36973 if (ENABLE_OFFLOADING
)
36975 g
->have_offload
= true;
36976 if (is_a
<varpool_node
*> (node
))
36977 vec_safe_push (offload_vars
, decl
);
36984 if (error
|| global_bindings_p ())
36987 stmt
= make_node (OACC_DECLARE
);
36988 TREE_TYPE (stmt
) = void_type_node
;
36989 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36990 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36998 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37002 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37004 LOC is the location of the #pragma token.
37007 #define OACC_ENTER_DATA_CLAUSE_MASK \
37008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37014 #define OACC_EXIT_DATA_CLAUSE_MASK \
37015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37023 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37026 location_t loc
= pragma_tok
->location
;
37027 tree stmt
, clauses
;
37028 const char *p
= "";
37030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37031 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37033 if (strcmp (p
, "data") != 0)
37035 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37036 enter
? "enter" : "exit");
37037 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37041 cp_lexer_consume_token (parser
->lexer
);
37044 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37045 "#pragma acc enter data", pragma_tok
);
37047 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37048 "#pragma acc exit data", pragma_tok
);
37050 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37052 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37053 enter
? "enter" : "exit");
37057 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37058 TREE_TYPE (stmt
) = void_type_node
;
37059 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37060 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37066 # pragma acc loop oacc-loop-clause[optseq] new-line
37067 structured-block */
37069 #define OACC_LOOP_CLAUSE_MASK \
37070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37082 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37083 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37085 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37087 strcat (p_name
, " loop");
37088 mask
|= OACC_LOOP_CLAUSE_MASK
;
37090 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37094 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37096 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37098 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37101 tree block
= begin_omp_structured_block ();
37102 int save
= cp_parser_begin_omp_structured_block (parser
);
37103 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37104 cp_parser_end_omp_structured_block (parser
, save
);
37105 add_stmt (finish_omp_structured_block (block
));
37111 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37116 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37120 #define OACC_KERNELS_CLAUSE_MASK \
37121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37135 #define OACC_PARALLEL_CLAUSE_MASK \
37136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37154 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37155 char *p_name
, bool *if_p
)
37157 omp_clause_mask mask
;
37158 enum tree_code code
;
37159 switch (cp_parser_pragma_kind (pragma_tok
))
37161 case PRAGMA_OACC_KERNELS
:
37162 strcat (p_name
, " kernels");
37163 mask
= OACC_KERNELS_CLAUSE_MASK
;
37164 code
= OACC_KERNELS
;
37166 case PRAGMA_OACC_PARALLEL
:
37167 strcat (p_name
, " parallel");
37168 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37169 code
= OACC_PARALLEL
;
37172 gcc_unreachable ();
37175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37178 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37179 if (strcmp (p
, "loop") == 0)
37181 cp_lexer_consume_token (parser
->lexer
);
37182 tree block
= begin_omp_parallel ();
37184 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37186 return finish_omp_construct (code
, block
, clauses
);
37190 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37192 tree block
= begin_omp_parallel ();
37193 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37194 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37195 cp_parser_end_omp_structured_block (parser
, save
);
37196 return finish_omp_construct (code
, block
, clauses
);
37200 # pragma acc update oacc-update-clause[optseq] new-line
37203 #define OACC_UPDATE_CLAUSE_MASK \
37204 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37212 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37214 tree stmt
, clauses
;
37216 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37217 "#pragma acc update", pragma_tok
);
37219 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37221 error_at (pragma_tok
->location
,
37222 "%<#pragma acc update%> must contain at least one "
37223 "%<device%> or %<host%> or %<self%> clause");
37227 stmt
= make_node (OACC_UPDATE
);
37228 TREE_TYPE (stmt
) = void_type_node
;
37229 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37230 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37236 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37238 LOC is the location of the #pragma token.
37241 #define OACC_WAIT_CLAUSE_MASK \
37242 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37245 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37247 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37248 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37250 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37251 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37253 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37254 "#pragma acc wait", pragma_tok
);
37256 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37257 stmt
= finish_expr_stmt (stmt
);
37263 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37265 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37274 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37275 enum pragma_context context
)
37277 bool first_p
= parser
->omp_declare_simd
== NULL
;
37278 cp_omp_declare_simd_data data
;
37281 data
.error_seen
= false;
37282 data
.fndecl_seen
= false;
37283 data
.tokens
= vNULL
;
37284 data
.clauses
= NULL_TREE
;
37285 /* It is safe to take the address of a local variable; it will only be
37286 used while this scope is live. */
37287 parser
->omp_declare_simd
= &data
;
37290 /* Store away all pragma tokens. */
37291 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37292 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37293 cp_lexer_consume_token (parser
->lexer
);
37294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37295 parser
->omp_declare_simd
->error_seen
= true;
37296 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37297 struct cp_token_cache
*cp
37298 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37299 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37303 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37304 cp_parser_pragma (parser
, context
, NULL
);
37307 case pragma_external
:
37308 cp_parser_declaration (parser
);
37310 case pragma_member
:
37311 cp_parser_member_declaration (parser
);
37313 case pragma_objc_icode
:
37314 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37317 cp_parser_declaration_statement (parser
);
37320 if (parser
->omp_declare_simd
37321 && !parser
->omp_declare_simd
->error_seen
37322 && !parser
->omp_declare_simd
->fndecl_seen
)
37323 error_at (pragma_tok
->location
,
37324 "%<#pragma omp declare simd%> not immediately followed by "
37325 "function declaration or definition");
37326 data
.tokens
.release ();
37327 parser
->omp_declare_simd
= NULL
;
37331 /* Finalize #pragma omp declare simd clauses after direct declarator has
37332 been parsed, and put that into "omp declare simd" attribute. */
37335 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37337 struct cp_token_cache
*ce
;
37338 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37341 if (!data
->error_seen
&& data
->fndecl_seen
)
37343 error ("%<#pragma omp declare simd%> not immediately followed by "
37344 "a single function declaration or definition");
37345 data
->error_seen
= true;
37347 if (data
->error_seen
)
37350 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37354 cp_parser_push_lexer_for_tokens (parser
, ce
);
37355 parser
->lexer
->in_pragma
= true;
37356 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37357 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37358 cp_lexer_consume_token (parser
->lexer
);
37359 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37360 "#pragma omp declare simd", pragma_tok
);
37361 cp_parser_pop_lexer (parser
);
37363 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37364 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37365 TREE_CHAIN (c
) = attrs
;
37366 if (processing_template_decl
)
37367 ATTR_IS_DEPENDENT (c
) = 1;
37371 data
->fndecl_seen
= true;
37377 # pragma omp declare target new-line
37378 declarations and definitions
37379 # pragma omp end declare target new-line
37382 # pragma omp declare target ( extended-list ) new-line
37384 # pragma omp declare target declare-target-clauses[seq] new-line */
37386 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37391 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37393 tree clauses
= NULL_TREE
;
37394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37396 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37397 "#pragma omp declare target", pragma_tok
);
37398 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37400 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37402 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37403 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37407 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37408 scope_chain
->omp_declare_target_attribute
++;
37411 if (scope_chain
->omp_declare_target_attribute
)
37412 error_at (pragma_tok
->location
,
37413 "%<#pragma omp declare target%> with clauses in between "
37414 "%<#pragma omp declare target%> without clauses and "
37415 "%<#pragma omp end declare target%>");
37416 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37418 tree t
= OMP_CLAUSE_DECL (c
), id
;
37419 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37420 tree at2
= lookup_attribute ("omp declare target link",
37421 DECL_ATTRIBUTES (t
));
37422 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37424 id
= get_identifier ("omp declare target link");
37425 std::swap (at1
, at2
);
37428 id
= get_identifier ("omp declare target");
37431 error_at (OMP_CLAUSE_LOCATION (c
),
37432 "%qD specified both in declare target %<link%> and %<to%>"
37438 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37439 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37442 symtab_node
*node
= symtab_node::get (t
);
37445 node
->offloadable
= 1;
37446 if (ENABLE_OFFLOADING
)
37448 g
->have_offload
= true;
37449 if (is_a
<varpool_node
*> (node
))
37450 vec_safe_push (offload_vars
, t
);
37458 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37460 const char *p
= "";
37461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37463 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37464 p
= IDENTIFIER_POINTER (id
);
37466 if (strcmp (p
, "declare") == 0)
37468 cp_lexer_consume_token (parser
->lexer
);
37470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37472 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37473 p
= IDENTIFIER_POINTER (id
);
37475 if (strcmp (p
, "target") == 0)
37476 cp_lexer_consume_token (parser
->lexer
);
37479 cp_parser_error (parser
, "expected %<target%>");
37480 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37486 cp_parser_error (parser
, "expected %<declare%>");
37487 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37490 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37491 if (!scope_chain
->omp_declare_target_attribute
)
37492 error_at (pragma_tok
->location
,
37493 "%<#pragma omp end declare target%> without corresponding "
37494 "%<#pragma omp declare target%>");
37496 scope_chain
->omp_declare_target_attribute
--;
37499 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37500 expression and optional initializer clause of
37501 #pragma omp declare reduction. We store the expression(s) as
37502 either 3, 6 or 7 special statements inside of the artificial function's
37503 body. The first two statements are DECL_EXPRs for the artificial
37504 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37505 expression that uses those variables.
37506 If there was any INITIALIZER clause, this is followed by further statements,
37507 the fourth and fifth statements are DECL_EXPRs for the artificial
37508 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37509 constructor variant (first token after open paren is not omp_priv),
37510 then the sixth statement is a statement with the function call expression
37511 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37512 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37513 to initialize the OMP_PRIV artificial variable and there is seventh
37514 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37517 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37519 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37520 gcc_assert (TYPE_REF_P (type
));
37521 type
= TREE_TYPE (type
);
37522 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37523 DECL_ARTIFICIAL (omp_out
) = 1;
37524 pushdecl (omp_out
);
37525 add_decl_expr (omp_out
);
37526 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37527 DECL_ARTIFICIAL (omp_in
) = 1;
37529 add_decl_expr (omp_in
);
37531 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37533 keep_next_level (true);
37534 tree block
= begin_omp_structured_block ();
37535 combiner
= cp_parser_expression (parser
);
37536 finish_expr_stmt (combiner
);
37537 block
= finish_omp_structured_block (block
);
37540 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37543 const char *p
= "";
37544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37546 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37547 p
= IDENTIFIER_POINTER (id
);
37550 if (strcmp (p
, "initializer") == 0)
37552 cp_lexer_consume_token (parser
->lexer
);
37553 matching_parens parens
;
37554 if (!parens
.require_open (parser
))
37558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37560 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37561 p
= IDENTIFIER_POINTER (id
);
37564 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37565 DECL_ARTIFICIAL (omp_priv
) = 1;
37566 pushdecl (omp_priv
);
37567 add_decl_expr (omp_priv
);
37568 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37569 DECL_ARTIFICIAL (omp_orig
) = 1;
37570 pushdecl (omp_orig
);
37571 add_decl_expr (omp_orig
);
37573 keep_next_level (true);
37574 block
= begin_omp_structured_block ();
37577 if (strcmp (p
, "omp_priv") == 0)
37579 bool is_direct_init
, is_non_constant_init
;
37581 cp_lexer_consume_token (parser
->lexer
);
37582 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37584 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37585 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37587 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37588 == CPP_CLOSE_PAREN
))
37590 finish_omp_structured_block (block
);
37591 error ("invalid initializer clause");
37594 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37595 &is_non_constant_init
);
37596 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37597 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37601 cp_parser_parse_tentatively (parser
);
37602 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37603 /*check_dependency_p=*/true,
37604 /*template_p=*/NULL
,
37605 /*declarator_p=*/false,
37606 /*optional_p=*/false);
37607 vec
<tree
, va_gc
> *args
;
37608 if (fn_name
== error_mark_node
37609 || cp_parser_error_occurred (parser
)
37610 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37611 || ((args
= cp_parser_parenthesized_expression_list
37612 (parser
, non_attr
, /*cast_p=*/false,
37613 /*allow_expansion_p=*/true,
37614 /*non_constant_p=*/NULL
)),
37615 cp_parser_error_occurred (parser
)))
37617 finish_omp_structured_block (block
);
37618 cp_parser_abort_tentative_parse (parser
);
37619 cp_parser_error (parser
, "expected id-expression (arguments)");
37624 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37625 if (arg
== omp_priv
37626 || (TREE_CODE (arg
) == ADDR_EXPR
37627 && TREE_OPERAND (arg
, 0) == omp_priv
))
37629 cp_parser_abort_tentative_parse (parser
);
37630 if (arg
== NULL_TREE
)
37631 error ("one of the initializer call arguments should be %<omp_priv%>"
37632 " or %<&omp_priv%>");
37633 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37635 finish_expr_stmt (initializer
);
37638 block
= finish_omp_structured_block (block
);
37639 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37643 add_decl_expr (omp_orig
);
37645 if (!parens
.require_close (parser
))
37649 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37650 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37657 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37658 initializer-clause[opt] new-line
37660 initializer-clause:
37661 initializer (omp_priv initializer)
37662 initializer (function-name (argument-list)) */
37665 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37666 enum pragma_context
)
37668 auto_vec
<tree
> types
;
37669 enum tree_code reduc_code
= ERROR_MARK
;
37670 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37672 cp_token
*first_token
;
37673 cp_token_cache
*cp
;
37677 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37678 p
= obstack_alloc (&declarator_obstack
, 0);
37680 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37683 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37686 reduc_code
= PLUS_EXPR
;
37689 reduc_code
= MULT_EXPR
;
37692 reduc_code
= MINUS_EXPR
;
37695 reduc_code
= BIT_AND_EXPR
;
37698 reduc_code
= BIT_XOR_EXPR
;
37701 reduc_code
= BIT_IOR_EXPR
;
37704 reduc_code
= TRUTH_ANDIF_EXPR
;
37707 reduc_code
= TRUTH_ORIF_EXPR
;
37710 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37713 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37714 "%<|%>, %<&&%>, %<||%> or identifier");
37718 if (reduc_code
!= ERROR_MARK
)
37719 cp_lexer_consume_token (parser
->lexer
);
37721 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37722 if (reduc_id
== error_mark_node
)
37725 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37728 /* Types may not be defined in declare reduction type list. */
37729 const char *saved_message
;
37730 saved_message
= parser
->type_definition_forbidden_message
;
37731 parser
->type_definition_forbidden_message
37732 = G_("types may not be defined in declare reduction type list");
37733 bool saved_colon_corrects_to_scope_p
;
37734 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37735 parser
->colon_corrects_to_scope_p
= false;
37736 bool saved_colon_doesnt_start_class_def_p
;
37737 saved_colon_doesnt_start_class_def_p
37738 = parser
->colon_doesnt_start_class_def_p
;
37739 parser
->colon_doesnt_start_class_def_p
= true;
37743 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37744 type
= cp_parser_type_id (parser
);
37745 if (type
== error_mark_node
)
37747 else if (ARITHMETIC_TYPE_P (type
)
37748 && (orig_reduc_id
== NULL_TREE
37749 || (TREE_CODE (type
) != COMPLEX_TYPE
37750 && (id_equal (orig_reduc_id
, "min")
37751 || id_equal (orig_reduc_id
, "max")))))
37752 error_at (loc
, "predeclared arithmetic type %qT in "
37753 "%<#pragma omp declare reduction%>", type
);
37754 else if (TREE_CODE (type
) == FUNCTION_TYPE
37755 || TREE_CODE (type
) == METHOD_TYPE
37756 || TREE_CODE (type
) == ARRAY_TYPE
)
37757 error_at (loc
, "function or array type %qT in "
37758 "%<#pragma omp declare reduction%>", type
);
37759 else if (TYPE_REF_P (type
))
37760 error_at (loc
, "reference type %qT in "
37761 "%<#pragma omp declare reduction%>", type
);
37762 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37763 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37764 "%<#pragma omp declare reduction%>", type
);
37766 types
.safe_push (type
);
37768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37769 cp_lexer_consume_token (parser
->lexer
);
37774 /* Restore the saved message. */
37775 parser
->type_definition_forbidden_message
= saved_message
;
37776 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37777 parser
->colon_doesnt_start_class_def_p
37778 = saved_colon_doesnt_start_class_def_p
;
37780 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37781 || types
.is_empty ())
37784 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37788 first_token
= cp_lexer_peek_token (parser
->lexer
);
37791 FOR_EACH_VEC_ELT (types
, i
, type
)
37794 = build_function_type_list (void_type_node
,
37795 cp_build_reference_type (type
, false),
37797 tree this_reduc_id
= reduc_id
;
37798 if (!dependent_type_p (type
))
37799 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37800 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37801 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37802 DECL_ARTIFICIAL (fndecl
) = 1;
37803 DECL_EXTERNAL (fndecl
) = 1;
37804 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37805 DECL_IGNORED_P (fndecl
) = 1;
37806 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37807 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37808 DECL_ATTRIBUTES (fndecl
)
37809 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37810 DECL_ATTRIBUTES (fndecl
));
37811 if (processing_template_decl
)
37812 fndecl
= push_template_decl (fndecl
);
37813 bool block_scope
= false;
37814 tree block
= NULL_TREE
;
37815 if (current_function_decl
)
37817 block_scope
= true;
37818 DECL_CONTEXT (fndecl
) = global_namespace
;
37819 if (!processing_template_decl
)
37822 else if (current_class_type
)
37826 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37827 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37828 cp_lexer_consume_token (parser
->lexer
);
37829 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37831 cp
= cp_token_cache_new (first_token
,
37832 cp_lexer_peek_nth_token (parser
->lexer
,
37835 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37836 finish_member_declaration (fndecl
);
37837 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37838 DECL_PENDING_INLINE_P (fndecl
) = 1;
37839 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37844 DECL_CONTEXT (fndecl
) = current_namespace
;
37848 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37850 block
= begin_omp_structured_block ();
37853 cp_parser_push_lexer_for_tokens (parser
, cp
);
37854 parser
->lexer
->in_pragma
= true;
37856 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37859 finish_function (/*inline_p=*/false);
37861 DECL_CONTEXT (fndecl
) = current_function_decl
;
37863 cp_parser_pop_lexer (parser
);
37867 cp_parser_pop_lexer (parser
);
37869 finish_function (/*inline_p=*/false);
37872 DECL_CONTEXT (fndecl
) = current_function_decl
;
37873 block
= finish_omp_structured_block (block
);
37874 if (TREE_CODE (block
) == BIND_EXPR
)
37875 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37876 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37877 DECL_SAVED_TREE (fndecl
) = block
;
37878 if (processing_template_decl
)
37879 add_decl_expr (fndecl
);
37881 cp_check_omp_declare_reduction (fndecl
);
37882 if (cp
== NULL
&& types
.length () > 1)
37883 cp
= cp_token_cache_new (first_token
,
37884 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37885 if (errs
!= errorcount
)
37889 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37892 /* Free any declarators allocated. */
37893 obstack_free (&declarator_obstack
, p
);
37897 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37898 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37899 initializer-clause[opt] new-line
37900 #pragma omp declare target new-line */
37903 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37904 enum pragma_context context
)
37906 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37908 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37909 const char *p
= IDENTIFIER_POINTER (id
);
37911 if (strcmp (p
, "simd") == 0)
37913 cp_lexer_consume_token (parser
->lexer
);
37914 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37918 cp_ensure_no_omp_declare_simd (parser
);
37919 if (strcmp (p
, "reduction") == 0)
37921 cp_lexer_consume_token (parser
->lexer
);
37922 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37926 if (!flag_openmp
) /* flag_openmp_simd */
37928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37931 if (strcmp (p
, "target") == 0)
37933 cp_lexer_consume_token (parser
->lexer
);
37934 cp_parser_omp_declare_target (parser
, pragma_tok
);
37938 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37940 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37945 #pragma omp taskloop taskloop-clause[optseq] new-line
37948 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37951 #define OMP_TASKLOOP_CLAUSE_MASK \
37952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37968 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37969 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37972 tree clauses
, sb
, ret
;
37974 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37976 strcat (p_name
, " taskloop");
37977 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37981 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37982 const char *p
= IDENTIFIER_POINTER (id
);
37984 if (strcmp (p
, "simd") == 0)
37986 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37987 if (cclauses
== NULL
)
37988 cclauses
= cclauses_buf
;
37990 cp_lexer_consume_token (parser
->lexer
);
37991 if (!flag_openmp
) /* flag_openmp_simd */
37992 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37994 sb
= begin_omp_structured_block ();
37995 save
= cp_parser_begin_omp_structured_block (parser
);
37996 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37998 cp_parser_end_omp_structured_block (parser
, save
);
37999 tree body
= finish_omp_structured_block (sb
);
38002 ret
= make_node (OMP_TASKLOOP
);
38003 TREE_TYPE (ret
) = void_type_node
;
38004 OMP_FOR_BODY (ret
) = body
;
38005 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38006 SET_EXPR_LOCATION (ret
, loc
);
38011 if (!flag_openmp
) /* flag_openmp_simd */
38013 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38017 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38021 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38022 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38025 sb
= begin_omp_structured_block ();
38026 save
= cp_parser_begin_omp_structured_block (parser
);
38028 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38031 cp_parser_end_omp_structured_block (parser
, save
);
38032 add_stmt (finish_omp_structured_block (sb
));
38039 # pragma acc routine oacc-routine-clause[optseq] new-line
38040 function-definition
38042 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38045 #define OACC_ROUTINE_CLAUSE_MASK \
38046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38052 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38053 component, which must resolve to a declared namespace-scope
38054 function. The clauses are either processed directly (for a named
38055 function), or defered until the immediatley following declaration
38059 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38060 enum pragma_context context
)
38062 gcc_checking_assert (context
== pragma_external
);
38063 /* The checking for "another pragma following this one" in the "no optional
38064 '( name )'" case makes sure that we dont re-enter. */
38065 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38067 cp_oacc_routine_data data
;
38068 data
.error_seen
= false;
38069 data
.fndecl_seen
= false;
38070 data
.tokens
= vNULL
;
38071 data
.clauses
= NULL_TREE
;
38072 data
.loc
= pragma_tok
->location
;
38073 /* It is safe to take the address of a local variable; it will only be
38074 used while this scope is live. */
38075 parser
->oacc_routine
= &data
;
38077 /* Look for optional '( name )'. */
38078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38080 matching_parens parens
;
38081 parens
.consume_open (parser
); /* '(' */
38083 /* We parse the name as an id-expression. If it resolves to
38084 anything other than a non-overloaded function at namespace
38085 scope, it's an error. */
38086 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38087 tree name
= cp_parser_id_expression (parser
,
38088 /*template_keyword_p=*/false,
38089 /*check_dependency_p=*/false,
38090 /*template_p=*/NULL
,
38091 /*declarator_p=*/false,
38092 /*optional_p=*/false);
38093 tree decl
= (identifier_p (name
)
38094 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38096 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38097 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38099 if (decl
== error_mark_node
38100 || !parens
.require_close (parser
))
38102 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38103 parser
->oacc_routine
= NULL
;
38108 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38109 "#pragma acc routine",
38110 cp_lexer_peek_token (parser
->lexer
));
38112 if (decl
&& is_overloaded_fn (decl
)
38113 && (TREE_CODE (decl
) != FUNCTION_DECL
38114 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38116 error_at (name_loc
,
38117 "%<#pragma acc routine%> names a set of overloads");
38118 parser
->oacc_routine
= NULL
;
38122 /* Perhaps we should use the same rule as declarations in different
38124 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38126 error_at (name_loc
,
38127 "%qD does not refer to a namespace scope function", decl
);
38128 parser
->oacc_routine
= NULL
;
38132 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38134 error_at (name_loc
, "%qD does not refer to a function", decl
);
38135 parser
->oacc_routine
= NULL
;
38139 cp_finalize_oacc_routine (parser
, decl
, false);
38140 parser
->oacc_routine
= NULL
;
38142 else /* No optional '( name )'. */
38144 /* Store away all pragma tokens. */
38145 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38146 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38147 cp_lexer_consume_token (parser
->lexer
);
38148 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38149 parser
->oacc_routine
->error_seen
= true;
38150 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38151 struct cp_token_cache
*cp
38152 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38153 parser
->oacc_routine
->tokens
.safe_push (cp
);
38155 /* Emit a helpful diagnostic if there's another pragma following this
38157 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38159 cp_ensure_no_oacc_routine (parser
);
38160 data
.tokens
.release ();
38161 /* ..., and then just keep going. */
38165 /* We only have to consider the pragma_external case here. */
38166 cp_parser_declaration (parser
);
38167 if (parser
->oacc_routine
38168 && !parser
->oacc_routine
->fndecl_seen
)
38169 cp_ensure_no_oacc_routine (parser
);
38171 parser
->oacc_routine
= NULL
;
38172 data
.tokens
.release ();
38176 /* Finalize #pragma acc routine clauses after direct declarator has
38180 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38182 struct cp_token_cache
*ce
;
38183 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38185 if (!data
->error_seen
&& data
->fndecl_seen
)
38187 error_at (data
->loc
,
38188 "%<#pragma acc routine%> not immediately followed by "
38189 "a single function declaration or definition");
38190 data
->error_seen
= true;
38192 if (data
->error_seen
)
38195 gcc_checking_assert (data
->tokens
.length () == 1);
38196 ce
= data
->tokens
[0];
38198 cp_parser_push_lexer_for_tokens (parser
, ce
);
38199 parser
->lexer
->in_pragma
= true;
38200 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38202 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38203 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38204 parser
->oacc_routine
->clauses
38205 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38206 "#pragma acc routine", pragma_tok
);
38207 cp_parser_pop_lexer (parser
);
38208 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38214 /* Apply any saved OpenACC routine clauses to a just-parsed
38218 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38220 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38222 /* Keep going if we're in error reporting mode. */
38223 if (parser
->oacc_routine
->error_seen
38224 || fndecl
== error_mark_node
)
38227 if (parser
->oacc_routine
->fndecl_seen
)
38229 error_at (parser
->oacc_routine
->loc
,
38230 "%<#pragma acc routine%> not immediately followed by"
38231 " a single function declaration or definition");
38232 parser
->oacc_routine
= NULL
;
38235 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38237 cp_ensure_no_oacc_routine (parser
);
38241 if (oacc_get_fn_attrib (fndecl
))
38243 error_at (parser
->oacc_routine
->loc
,
38244 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38245 parser
->oacc_routine
= NULL
;
38249 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38251 error_at (parser
->oacc_routine
->loc
,
38253 ? G_("%<#pragma acc routine%> must be applied before use")
38254 : G_("%<#pragma acc routine%> must be applied before "
38256 parser
->oacc_routine
= NULL
;
38260 /* Process the routine's dimension clauses. */
38261 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38262 oacc_replace_fn_attrib (fndecl
, dims
);
38264 /* Add an "omp declare target" attribute. */
38265 DECL_ATTRIBUTES (fndecl
)
38266 = tree_cons (get_identifier ("omp declare target"),
38267 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38269 /* Don't unset parser->oacc_routine here: we may still need it to
38270 diagnose wrong usage. But, remember that we've used this "#pragma acc
38272 parser
->oacc_routine
->fndecl_seen
= true;
38276 /* Main entry point to OpenMP statement pragmas. */
38279 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38282 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38283 omp_clause_mask
mask (0);
38285 switch (cp_parser_pragma_kind (pragma_tok
))
38287 case PRAGMA_OACC_ATOMIC
:
38288 cp_parser_omp_atomic (parser
, pragma_tok
);
38290 case PRAGMA_OACC_CACHE
:
38291 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38293 case PRAGMA_OACC_DATA
:
38294 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38296 case PRAGMA_OACC_ENTER_DATA
:
38297 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38299 case PRAGMA_OACC_EXIT_DATA
:
38300 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38302 case PRAGMA_OACC_HOST_DATA
:
38303 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38305 case PRAGMA_OACC_KERNELS
:
38306 case PRAGMA_OACC_PARALLEL
:
38307 strcpy (p_name
, "#pragma acc");
38308 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38311 case PRAGMA_OACC_LOOP
:
38312 strcpy (p_name
, "#pragma acc");
38313 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38316 case PRAGMA_OACC_UPDATE
:
38317 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38319 case PRAGMA_OACC_WAIT
:
38320 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38322 case PRAGMA_OMP_ATOMIC
:
38323 cp_parser_omp_atomic (parser
, pragma_tok
);
38325 case PRAGMA_OMP_CRITICAL
:
38326 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38328 case PRAGMA_OMP_DISTRIBUTE
:
38329 strcpy (p_name
, "#pragma omp");
38330 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38333 case PRAGMA_OMP_FOR
:
38334 strcpy (p_name
, "#pragma omp");
38335 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38338 case PRAGMA_OMP_MASTER
:
38339 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38341 case PRAGMA_OMP_PARALLEL
:
38342 strcpy (p_name
, "#pragma omp");
38343 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38346 case PRAGMA_OMP_SECTIONS
:
38347 strcpy (p_name
, "#pragma omp");
38348 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38350 case PRAGMA_OMP_SIMD
:
38351 strcpy (p_name
, "#pragma omp");
38352 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38355 case PRAGMA_OMP_SINGLE
:
38356 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38358 case PRAGMA_OMP_TASK
:
38359 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38361 case PRAGMA_OMP_TASKGROUP
:
38362 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38364 case PRAGMA_OMP_TASKLOOP
:
38365 strcpy (p_name
, "#pragma omp");
38366 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38369 case PRAGMA_OMP_TEAMS
:
38370 strcpy (p_name
, "#pragma omp");
38371 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38375 gcc_unreachable ();
38378 protected_set_expr_location (stmt
, pragma_tok
->location
);
38381 /* Transactional Memory parsing routines. */
38383 /* Parse a transaction attribute.
38389 We use this instead of cp_parser_attributes_opt for transactions to avoid
38390 the pedwarn in C++98 mode. */
38393 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38396 tree attr_name
, attr
= NULL
;
38398 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38399 return cp_parser_attributes_opt (parser
);
38401 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38403 cp_lexer_consume_token (parser
->lexer
);
38404 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38407 token
= cp_lexer_peek_token (parser
->lexer
);
38408 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38410 token
= cp_lexer_consume_token (parser
->lexer
);
38412 attr_name
= (token
->type
== CPP_KEYWORD
38413 /* For keywords, use the canonical spelling,
38414 not the parsed identifier. */
38415 ? ridpointers
[(int) token
->keyword
]
38417 attr
= build_tree_list (attr_name
, NULL_TREE
);
38420 cp_parser_error (parser
, "expected identifier");
38422 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38424 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38428 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38430 transaction-statement:
38431 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38433 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38439 unsigned char old_in
= parser
->in_transaction
;
38440 unsigned char this_in
= 1, new_in
;
38441 enum rid keyword
= token
->keyword
;
38442 tree stmt
, attrs
, noex
;
38444 cp_lexer_consume_token (parser
->lexer
);
38446 if (keyword
== RID_TRANSACTION_RELAXED
38447 || keyword
== RID_SYNCHRONIZED
)
38448 this_in
|= TM_STMT_ATTR_RELAXED
;
38451 attrs
= cp_parser_txn_attribute_opt (parser
);
38453 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38456 /* Parse a noexcept specification. */
38457 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38458 noex
= boolean_true_node
;
38459 else if (keyword
== RID_ATOMIC_CANCEL
)
38461 /* cancel-and-throw is unimplemented. */
38462 sorry ("atomic_cancel");
38466 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38468 /* Keep track if we're in the lexical scope of an outer transaction. */
38469 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38471 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38473 parser
->in_transaction
= new_in
;
38474 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38475 parser
->in_transaction
= old_in
;
38477 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38482 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38484 transaction-expression:
38485 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38486 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38492 unsigned char old_in
= parser
->in_transaction
;
38493 unsigned char this_in
= 1;
38497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38499 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38500 || keyword
== RID_TRANSACTION_RELAXED
);
38504 keyword
== RID_TRANSACTION_RELAXED
38505 ? G_("%<__transaction_relaxed%> without transactional memory "
38507 : G_("%<__transaction_atomic%> without transactional memory "
38508 "support enabled"));
38510 token
= cp_parser_require_keyword (parser
, keyword
,
38511 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38512 : RT_TRANSACTION_RELAXED
));
38513 gcc_assert (token
!= NULL
);
38515 if (keyword
== RID_TRANSACTION_RELAXED
)
38516 this_in
|= TM_STMT_ATTR_RELAXED
;
38518 /* Set this early. This might mean that we allow transaction_cancel in
38519 an expression that we find out later actually has to be a constexpr.
38520 However, we expect that cxx_constant_value will be able to deal with
38521 this; also, if the noexcept has no constexpr, then what we parse next
38522 really is a transaction's body. */
38523 parser
->in_transaction
= this_in
;
38525 /* Parse a noexcept specification. */
38526 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38529 if (!noex
|| !noex_expr
38530 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38532 matching_parens parens
;
38533 parens
.require_open (parser
);
38535 expr
= cp_parser_expression (parser
);
38536 expr
= finish_parenthesized_expr (expr
);
38538 parens
.require_close (parser
);
38542 /* The only expression that is available got parsed for the noexcept
38543 already. noexcept is true then. */
38545 noex
= boolean_true_node
;
38548 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38549 parser
->in_transaction
= old_in
;
38551 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38552 return error_mark_node
;
38554 return (flag_tm
? expr
: error_mark_node
);
38557 /* Parse a function-transaction-block.
38559 function-transaction-block:
38560 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38562 __transaction_atomic txn-attribute[opt] function-try-block
38563 __transaction_relaxed ctor-initializer[opt] function-body
38564 __transaction_relaxed function-try-block
38568 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38570 unsigned char old_in
= parser
->in_transaction
;
38571 unsigned char new_in
= 1;
38572 tree compound_stmt
, stmt
, attrs
;
38575 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38576 || keyword
== RID_TRANSACTION_RELAXED
);
38577 token
= cp_parser_require_keyword (parser
, keyword
,
38578 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38579 : RT_TRANSACTION_RELAXED
));
38580 gcc_assert (token
!= NULL
);
38582 if (keyword
== RID_TRANSACTION_RELAXED
)
38583 new_in
|= TM_STMT_ATTR_RELAXED
;
38586 attrs
= cp_parser_txn_attribute_opt (parser
);
38588 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38591 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38593 parser
->in_transaction
= new_in
;
38595 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38596 cp_parser_function_try_block (parser
);
38598 cp_parser_ctor_initializer_opt_and_function_body
38599 (parser
, /*in_function_try_block=*/false);
38601 parser
->in_transaction
= old_in
;
38603 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38606 /* Parse a __transaction_cancel statement.
38609 __transaction_cancel txn-attribute[opt] ;
38610 __transaction_cancel txn-attribute[opt] throw-expression ;
38612 ??? Cancel and throw is not yet implemented. */
38615 cp_parser_transaction_cancel (cp_parser
*parser
)
38618 bool is_outer
= false;
38621 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38622 RT_TRANSACTION_CANCEL
);
38623 gcc_assert (token
!= NULL
);
38625 attrs
= cp_parser_txn_attribute_opt (parser
);
38627 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38629 /* ??? Parse cancel-and-throw here. */
38631 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38635 error_at (token
->location
, "%<__transaction_cancel%> without "
38636 "transactional memory support enabled");
38637 return error_mark_node
;
38639 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38641 error_at (token
->location
, "%<__transaction_cancel%> within a "
38642 "%<__transaction_relaxed%>");
38643 return error_mark_node
;
38647 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38648 && !is_tm_may_cancel_outer (current_function_decl
))
38650 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38651 "within outer %<__transaction_atomic%>");
38652 error_at (token
->location
,
38653 " or a %<transaction_may_cancel_outer%> function");
38654 return error_mark_node
;
38657 else if (parser
->in_transaction
== 0)
38659 error_at (token
->location
, "%<__transaction_cancel%> not within "
38660 "%<__transaction_atomic%>");
38661 return error_mark_node
;
38664 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38672 static GTY (()) cp_parser
*the_parser
;
38675 /* Special handling for the first token or line in the file. The first
38676 thing in the file might be #pragma GCC pch_preprocess, which loads a
38677 PCH file, which is a GC collection point. So we need to handle this
38678 first pragma without benefit of an existing lexer structure.
38680 Always returns one token to the caller in *FIRST_TOKEN. This is
38681 either the true first token of the file, or the first token after
38682 the initial pragma. */
38685 cp_parser_initial_pragma (cp_token
*first_token
)
38689 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38690 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38693 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38694 if (first_token
->type
== CPP_STRING
)
38696 name
= first_token
->u
.value
;
38698 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38699 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38700 error_at (first_token
->location
,
38701 "junk at end of %<#pragma GCC pch_preprocess%>");
38704 error_at (first_token
->location
, "expected string literal");
38706 /* Skip to the end of the pragma. */
38707 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38708 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38710 /* Now actually load the PCH file. */
38712 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38714 /* Read one more token to return to our caller. We have to do this
38715 after reading the PCH file in, since its pointers have to be
38717 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38720 /* Parse a pragma GCC ivdep. */
38723 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38725 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38729 /* Parse a pragma GCC unroll. */
38731 static unsigned short
38732 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38734 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38735 tree expr
= cp_parser_constant_expression (parser
);
38736 unsigned short unroll
;
38737 expr
= maybe_constant_value (expr
);
38738 HOST_WIDE_INT lunroll
= 0;
38739 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38740 || TREE_CODE (expr
) != INTEGER_CST
38741 || (lunroll
= tree_to_shwi (expr
)) < 0
38742 || lunroll
>= USHRT_MAX
)
38744 error_at (location
, "%<#pragma GCC unroll%> requires an"
38745 " assignment-expression that evaluates to a non-negative"
38746 " integral constant less than %u", USHRT_MAX
);
38751 unroll
= (unsigned short)lunroll
;
38755 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38759 /* Normal parsing of a pragma token. Here we can (and must) use the
38763 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38765 cp_token
*pragma_tok
;
38770 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38771 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38772 parser
->lexer
->in_pragma
= true;
38774 id
= cp_parser_pragma_kind (pragma_tok
);
38775 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38776 cp_ensure_no_omp_declare_simd (parser
);
38779 case PRAGMA_GCC_PCH_PREPROCESS
:
38780 error_at (pragma_tok
->location
,
38781 "%<#pragma GCC pch_preprocess%> must be first");
38784 case PRAGMA_OMP_BARRIER
:
38787 case pragma_compound
:
38788 cp_parser_omp_barrier (parser
, pragma_tok
);
38791 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38792 "used in compound statements", "omp barrier");
38799 case PRAGMA_OMP_FLUSH
:
38802 case pragma_compound
:
38803 cp_parser_omp_flush (parser
, pragma_tok
);
38806 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38807 "used in compound statements", "omp flush");
38814 case PRAGMA_OMP_TASKWAIT
:
38817 case pragma_compound
:
38818 cp_parser_omp_taskwait (parser
, pragma_tok
);
38821 error_at (pragma_tok
->location
,
38822 "%<#pragma %s%> may only be used in compound statements",
38830 case PRAGMA_OMP_TASKYIELD
:
38833 case pragma_compound
:
38834 cp_parser_omp_taskyield (parser
, pragma_tok
);
38837 error_at (pragma_tok
->location
,
38838 "%<#pragma %s%> may only be used in compound statements",
38846 case PRAGMA_OMP_CANCEL
:
38849 case pragma_compound
:
38850 cp_parser_omp_cancel (parser
, pragma_tok
);
38853 error_at (pragma_tok
->location
,
38854 "%<#pragma %s%> may only be used in compound statements",
38862 case PRAGMA_OMP_CANCELLATION_POINT
:
38863 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38866 case PRAGMA_OMP_THREADPRIVATE
:
38867 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38870 case PRAGMA_OMP_DECLARE
:
38871 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38873 case PRAGMA_OACC_DECLARE
:
38874 cp_parser_oacc_declare (parser
, pragma_tok
);
38877 case PRAGMA_OACC_ENTER_DATA
:
38878 if (context
== pragma_stmt
)
38880 error_at (pragma_tok
->location
,
38881 "%<#pragma %s%> may only be used in compound statements",
38885 else if (context
!= pragma_compound
)
38887 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38890 case PRAGMA_OACC_EXIT_DATA
:
38891 if (context
== pragma_stmt
)
38893 error_at (pragma_tok
->location
,
38894 "%<#pragma %s%> may only be used in compound statements",
38898 else if (context
!= pragma_compound
)
38900 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38903 case PRAGMA_OACC_ROUTINE
:
38904 if (context
!= pragma_external
)
38906 error_at (pragma_tok
->location
,
38907 "%<#pragma acc routine%> must be at file scope");
38910 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38913 case PRAGMA_OACC_UPDATE
:
38914 if (context
== pragma_stmt
)
38916 error_at (pragma_tok
->location
,
38917 "%<#pragma %s%> may only be used in compound statements",
38921 else if (context
!= pragma_compound
)
38923 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38926 case PRAGMA_OACC_WAIT
:
38927 if (context
== pragma_stmt
)
38929 error_at (pragma_tok
->location
,
38930 "%<#pragma %s%> may only be used in compound statements",
38934 else if (context
!= pragma_compound
)
38936 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38939 case PRAGMA_OACC_ATOMIC
:
38940 case PRAGMA_OACC_CACHE
:
38941 case PRAGMA_OACC_DATA
:
38942 case PRAGMA_OACC_HOST_DATA
:
38943 case PRAGMA_OACC_KERNELS
:
38944 case PRAGMA_OACC_PARALLEL
:
38945 case PRAGMA_OACC_LOOP
:
38946 case PRAGMA_OMP_ATOMIC
:
38947 case PRAGMA_OMP_CRITICAL
:
38948 case PRAGMA_OMP_DISTRIBUTE
:
38949 case PRAGMA_OMP_FOR
:
38950 case PRAGMA_OMP_MASTER
:
38951 case PRAGMA_OMP_PARALLEL
:
38952 case PRAGMA_OMP_SECTIONS
:
38953 case PRAGMA_OMP_SIMD
:
38954 case PRAGMA_OMP_SINGLE
:
38955 case PRAGMA_OMP_TASK
:
38956 case PRAGMA_OMP_TASKGROUP
:
38957 case PRAGMA_OMP_TASKLOOP
:
38958 case PRAGMA_OMP_TEAMS
:
38959 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38961 stmt
= push_omp_privatization_clauses (false);
38962 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38963 pop_omp_privatization_clauses (stmt
);
38966 case PRAGMA_OMP_ORDERED
:
38967 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38969 stmt
= push_omp_privatization_clauses (false);
38970 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38971 pop_omp_privatization_clauses (stmt
);
38974 case PRAGMA_OMP_TARGET
:
38975 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38977 stmt
= push_omp_privatization_clauses (false);
38978 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38979 pop_omp_privatization_clauses (stmt
);
38982 case PRAGMA_OMP_END_DECLARE_TARGET
:
38983 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38986 case PRAGMA_OMP_SECTION
:
38987 error_at (pragma_tok
->location
,
38988 "%<#pragma omp section%> may only be used in "
38989 "%<#pragma omp sections%> construct");
38994 if (context
== pragma_external
)
38996 error_at (pragma_tok
->location
,
38997 "%<#pragma GCC ivdep%> must be inside a function");
39000 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39001 unsigned short unroll
;
39002 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39003 if (tok
->type
== CPP_PRAGMA
39004 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39006 tok
= cp_lexer_consume_token (parser
->lexer
);
39007 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39008 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39012 if (tok
->type
!= CPP_KEYWORD
39013 || (tok
->keyword
!= RID_FOR
39014 && tok
->keyword
!= RID_WHILE
39015 && tok
->keyword
!= RID_DO
))
39017 cp_parser_error (parser
, "for, while or do statement expected");
39020 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39024 case PRAGMA_UNROLL
:
39026 if (context
== pragma_external
)
39028 error_at (pragma_tok
->location
,
39029 "%<#pragma GCC unroll%> must be inside a function");
39032 const unsigned short unroll
39033 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39035 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39036 if (tok
->type
== CPP_PRAGMA
39037 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39039 tok
= cp_lexer_consume_token (parser
->lexer
);
39040 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39041 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39045 if (tok
->type
!= CPP_KEYWORD
39046 || (tok
->keyword
!= RID_FOR
39047 && tok
->keyword
!= RID_WHILE
39048 && tok
->keyword
!= RID_DO
))
39050 cp_parser_error (parser
, "for, while or do statement expected");
39053 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39058 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39059 c_invoke_pragma_handler (id
);
39063 cp_parser_error (parser
, "expected declaration specifiers");
39067 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39071 /* The interface the pragma parsers have to the lexer. */
39074 pragma_lex (tree
*value
, location_t
*loc
)
39076 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39077 enum cpp_ttype ret
= tok
->type
;
39079 *value
= tok
->u
.value
;
39081 *loc
= tok
->location
;
39083 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39085 else if (ret
== CPP_STRING
)
39086 *value
= cp_parser_string_literal (the_parser
, false, false);
39089 if (ret
== CPP_KEYWORD
)
39091 cp_lexer_consume_token (the_parser
->lexer
);
39098 /* External interface. */
39100 /* Parse one entire translation unit. */
39103 c_parse_file (void)
39105 static bool already_called
= false;
39107 if (already_called
)
39108 fatal_error (input_location
,
39109 "inter-module optimizations not implemented for C++");
39110 already_called
= true;
39112 the_parser
= cp_parser_new ();
39113 push_deferring_access_checks (flag_access_control
39114 ? dk_no_deferred
: dk_no_check
);
39115 cp_parser_translation_unit (the_parser
);
39119 /* Create an identifier for a generic parameter type (a synthesized
39120 template parameter implied by `auto' or a concept identifier). */
39122 static GTY(()) int generic_parm_count
;
39124 make_generic_type_name ()
39127 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39128 return get_identifier (buf
);
39131 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39132 (creating a new template parameter list if necessary). Returns the newly
39133 created template type parm. */
39136 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39138 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39140 /* Before committing to modifying any scope, if we're in an
39141 implicit template scope, and we're trying to synthesize a
39142 constrained parameter, try to find a previous parameter with
39143 the same name. This is the same-type rule for abbreviated
39144 function templates.
39146 NOTE: We can generate implicit parameters when tentatively
39147 parsing a nested name specifier, only to reject that parse
39148 later. However, matching the same template-id as part of a
39149 direct-declarator should generate an identical template
39150 parameter, so this rule will merge them. */
39151 if (parser
->implicit_template_scope
&& constr
)
39153 tree t
= parser
->implicit_template_parms
;
39156 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39158 tree d
= TREE_VALUE (t
);
39159 if (TREE_CODE (d
) == PARM_DECL
)
39160 /* Return the TEMPLATE_PARM_INDEX. */
39161 d
= DECL_INITIAL (d
);
39164 t
= TREE_CHAIN (t
);
39168 /* We are either continuing a function template that already contains implicit
39169 template parameters, creating a new fully-implicit function template, or
39170 extending an existing explicit function template with implicit template
39173 cp_binding_level
*const entry_scope
= current_binding_level
;
39175 bool become_template
= false;
39176 cp_binding_level
*parent_scope
= 0;
39178 if (parser
->implicit_template_scope
)
39180 gcc_assert (parser
->implicit_template_parms
);
39182 current_binding_level
= parser
->implicit_template_scope
;
39186 /* Roll back to the existing template parameter scope (in the case of
39187 extending an explicit function template) or introduce a new template
39188 parameter scope ahead of the function parameter scope (or class scope
39189 in the case of out-of-line member definitions). The function scope is
39190 added back after template parameter synthesis below. */
39192 cp_binding_level
*scope
= entry_scope
;
39194 while (scope
->kind
== sk_function_parms
)
39196 parent_scope
= scope
;
39197 scope
= scope
->level_chain
;
39199 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39201 /* If not defining a class, then any class scope is a scope level in
39202 an out-of-line member definition. In this case simply wind back
39203 beyond the first such scope to inject the template parameter list.
39204 Otherwise wind back to the class being defined. The latter can
39205 occur in class member friend declarations such as:
39211 friend void A::foo (auto);
39214 The template parameter list synthesized for the friend declaration
39215 must be injected in the scope of 'B'. This can also occur in
39216 erroneous cases such as:
39222 void B::foo (auto) {}
39225 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39226 but, nevertheless, the template parameter list synthesized for the
39227 declarator should be injected into the scope of 'A' as if the
39228 ill-formed template was specified explicitly. */
39230 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39232 parent_scope
= scope
;
39233 scope
= scope
->level_chain
;
39237 current_binding_level
= scope
;
39239 if (scope
->kind
!= sk_template_parms
39240 || !function_being_declared_is_template_p (parser
))
39242 /* Introduce a new template parameter list for implicit template
39245 become_template
= true;
39247 parser
->implicit_template_scope
39248 = begin_scope (sk_template_parms
, NULL
);
39250 ++processing_template_decl
;
39252 parser
->fully_implicit_function_template_p
= true;
39253 ++parser
->num_template_parameter_lists
;
39257 /* Synthesize implicit template parameters at the end of the explicit
39258 template parameter list. */
39260 gcc_assert (current_template_parms
);
39262 parser
->implicit_template_scope
= scope
;
39264 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39265 parser
->implicit_template_parms
39266 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39270 /* Synthesize a new template parameter and track the current template
39271 parameter chain with implicit_template_parms. */
39273 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39274 tree synth_id
= make_generic_type_name ();
39275 tree synth_tmpl_parm
;
39276 bool non_type
= false;
39278 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39280 = finish_template_type_parm (class_type_node
, synth_id
);
39281 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39283 = finish_constrained_template_template_parm (proto
, synth_id
);
39286 synth_tmpl_parm
= copy_decl (proto
);
39287 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39291 // Attach the constraint to the parm before processing.
39292 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39293 TREE_TYPE (node
) = constr
;
39295 = process_template_parm (parser
->implicit_template_parms
,
39298 /*non_type=*/non_type
,
39299 /*param_pack=*/false);
39301 // Chain the new parameter to the list of implicit parameters.
39302 if (parser
->implicit_template_parms
)
39303 parser
->implicit_template_parms
39304 = TREE_CHAIN (parser
->implicit_template_parms
);
39306 parser
->implicit_template_parms
= new_parm
;
39308 tree new_decl
= get_local_decls ();
39310 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39311 new_decl
= DECL_INITIAL (new_decl
);
39313 /* If creating a fully implicit function template, start the new implicit
39314 template parameter list with this synthesized type, otherwise grow the
39315 current template parameter list. */
39317 if (become_template
)
39319 parent_scope
->level_chain
= current_binding_level
;
39321 tree new_parms
= make_tree_vec (1);
39322 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39323 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39324 new_parms
, current_template_parms
);
39328 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39329 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39330 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39331 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39334 // If the new parameter was constrained, we need to add that to the
39335 // constraints in the template parameter list.
39336 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39338 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39339 reqs
= conjoin_constraints (reqs
, req
);
39340 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39343 current_binding_level
= entry_scope
;
39348 /* Finish the declaration of a fully implicit function template. Such a
39349 template has no explicit template parameter list so has not been through the
39350 normal template head and tail processing. synthesize_implicit_template_parm
39351 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39352 provided if the declaration is a class member such that its template
39353 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39354 form is returned. Otherwise NULL_TREE is returned. */
39357 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39359 gcc_assert (parser
->fully_implicit_function_template_p
);
39361 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39362 && DECL_VIRTUAL_P (member_decl_opt
))
39364 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39365 "implicit templates may not be %<virtual%>");
39366 DECL_VIRTUAL_P (member_decl_opt
) = false;
39369 if (member_decl_opt
)
39370 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39371 end_template_decl ();
39373 parser
->fully_implicit_function_template_p
= false;
39374 parser
->implicit_template_parms
= 0;
39375 parser
->implicit_template_scope
= 0;
39376 --parser
->num_template_parameter_lists
;
39378 return member_decl_opt
;
39381 /* Like finish_fully_implicit_template, but to be used in error
39382 recovery, rearranging scopes so that we restore the state we had
39383 before synthesize_implicit_template_parm inserted the implement
39384 template parms scope. */
39387 abort_fully_implicit_template (cp_parser
*parser
)
39389 cp_binding_level
*return_to_scope
= current_binding_level
;
39391 if (parser
->implicit_template_scope
39392 && return_to_scope
!= parser
->implicit_template_scope
)
39394 cp_binding_level
*child
= return_to_scope
;
39395 for (cp_binding_level
*scope
= child
->level_chain
;
39396 scope
!= parser
->implicit_template_scope
;
39397 scope
= child
->level_chain
)
39399 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39400 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39401 current_binding_level
= parser
->implicit_template_scope
;
39404 return_to_scope
= return_to_scope
->level_chain
;
39406 finish_fully_implicit_template (parser
, NULL
);
39408 gcc_assert (current_binding_level
== return_to_scope
);
39411 /* Helper function for diagnostics that have complained about things
39412 being used with 'extern "C"' linkage.
39414 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39417 maybe_show_extern_c_location (void)
39419 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39420 inform (the_parser
->innermost_linkage_specification_location
,
39421 "%<extern \"C\"%> linkage started here");
39424 #include "gt-cp-parser.h"