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");
2869 gcc_rich_location
richloc (input_location
);
2871 bool added_matching_location
= false;
2873 if (missing_token_desc
!= RT_NONE
)
2875 /* Potentially supply a fix-it hint, suggesting to add the
2876 missing token immediately after the *previous* token.
2877 This may move the primary location within richloc. */
2878 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2879 location_t prev_token_loc
2880 = cp_lexer_previous_token (parser
->lexer
)->location
;
2881 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2883 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884 Attempt to consolidate diagnostics by printing it as a
2885 secondary range within the main diagnostic. */
2886 if (matching_location
!= UNKNOWN_LOCATION
)
2887 added_matching_location
2888 = richloc
.add_location_if_nearby (matching_location
);
2891 /* Actually emit the error. */
2892 c_parse_error (gmsgid
,
2893 /* Because c_parser_error does not understand
2894 CPP_KEYWORD, keywords are treated like
2896 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2897 token
->u
.value
, token
->flags
, &richloc
);
2899 if (missing_token_desc
!= RT_NONE
)
2901 /* If we weren't able to consolidate matching_location, then
2902 print it as a secondary diagnostic. */
2903 if (matching_location
!= UNKNOWN_LOCATION
2904 && !added_matching_location
)
2905 inform (matching_location
, "to match this %qs",
2906 get_matching_symbol (missing_token_desc
));
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911 FILE:LINE: MESSAGE before TOKEN
2912 where TOKEN is the next token in the input stream. MESSAGE
2913 (specified by the caller) is usually of the form "expected
2917 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2919 if (!cp_parser_simulate_error (parser
))
2920 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2923 /* Issue an error about name-lookup failing. NAME is the
2924 IDENTIFIER_NODE DECL is the result of
2925 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2926 the thing that we hoped to find. */
2929 cp_parser_name_lookup_error (cp_parser
* parser
,
2932 name_lookup_error desired
,
2933 location_t location
)
2935 /* If name lookup completely failed, tell the user that NAME was not
2937 if (decl
== error_mark_node
)
2939 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2940 error_at (location
, "%<%E::%E%> has not been declared",
2941 parser
->scope
, name
);
2942 else if (parser
->scope
== global_namespace
)
2943 error_at (location
, "%<::%E%> has not been declared", name
);
2944 else if (parser
->object_scope
2945 && !CLASS_TYPE_P (parser
->object_scope
))
2946 error_at (location
, "request for member %qE in non-class type %qT",
2947 name
, parser
->object_scope
);
2948 else if (parser
->object_scope
)
2949 error_at (location
, "%<%T::%E%> has not been declared",
2950 parser
->object_scope
, name
);
2952 error_at (location
, "%qE has not been declared", name
);
2954 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2959 error_at (location
, "%<%E::%E%> is not a type",
2960 parser
->scope
, name
);
2963 error_at (location
, "%<%E::%E%> is not a class or namespace",
2964 parser
->scope
, name
);
2968 "%<%E::%E%> is not a class, namespace, or enumeration",
2969 parser
->scope
, name
);
2976 else if (parser
->scope
== global_namespace
)
2981 error_at (location
, "%<::%E%> is not a type", name
);
2984 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2988 "%<::%E%> is not a class, namespace, or enumeration",
3000 error_at (location
, "%qE is not a type", name
);
3003 error_at (location
, "%qE is not a class or namespace", name
);
3007 "%qE is not a class, namespace, or enumeration", name
);
3015 /* If we are parsing tentatively, remember that an error has occurred
3016 during this tentative parse. Returns true if the error was
3017 simulated; false if a message should be issued by the caller. */
3020 cp_parser_simulate_error (cp_parser
* parser
)
3022 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3024 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3030 /* This function is called when a type is defined. If type
3031 definitions are forbidden at this point, an error message is
3035 cp_parser_check_type_definition (cp_parser
* parser
)
3037 /* If types are forbidden here, issue a message. */
3038 if (parser
->type_definition_forbidden_message
)
3040 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041 in the message need to be interpreted. */
3042 error (parser
->type_definition_forbidden_message
);
3048 /* This function is called when the DECLARATOR is processed. The TYPE
3049 was a type defined in the decl-specifiers. If it is invalid to
3050 define a type in the decl-specifiers for DECLARATOR, an error is
3051 issued. TYPE_LOCATION is the location of TYPE and is used
3052 for error reporting. */
3055 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3056 tree type
, location_t type_location
)
3058 /* [dcl.fct] forbids type definitions in return types.
3059 Unfortunately, it's not easy to know whether or not we are
3060 processing a return type until after the fact. */
3062 && (declarator
->kind
== cdk_pointer
3063 || declarator
->kind
== cdk_reference
3064 || declarator
->kind
== cdk_ptrmem
))
3065 declarator
= declarator
->declarator
;
3067 && declarator
->kind
== cdk_function
)
3069 error_at (type_location
,
3070 "new types may not be defined in a return type");
3071 inform (type_location
,
3072 "(perhaps a semicolon is missing after the definition of %qT)",
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078 "<" in any valid C++ program. If the next token is indeed "<",
3079 issue a message warning the user about what appears to be an
3080 invalid attempt to form a template-id. LOCATION is the location
3081 of the type-specifier (TYPE) */
3084 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3086 enum tag_types tag_type
,
3087 location_t location
)
3089 cp_token_position start
= 0;
3091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3093 if (TREE_CODE (type
) == TYPE_DECL
)
3094 type
= TREE_TYPE (type
);
3095 if (TYPE_P (type
) && !template_placeholder_p (type
))
3096 error_at (location
, "%qT is not a template", type
);
3097 else if (identifier_p (type
))
3099 if (tag_type
!= none_type
)
3100 error_at (location
, "%qE is not a class template", type
);
3102 error_at (location
, "%qE is not a template", type
);
3105 error_at (location
, "invalid template-id");
3106 /* Remember the location of the invalid "<". */
3107 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3108 start
= cp_lexer_token_position (parser
->lexer
, true);
3109 /* Consume the "<". */
3110 cp_lexer_consume_token (parser
->lexer
);
3111 /* Parse the template arguments. */
3112 cp_parser_enclosed_template_argument_list (parser
);
3113 /* Permanently remove the invalid template arguments so that
3114 this error message is not issued again. */
3116 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3120 /* If parsing an integral constant-expression, issue an error message
3121 about the fact that THING appeared and return true. Otherwise,
3122 return false. In either case, set
3123 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3126 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3127 non_integral_constant thing
)
3129 parser
->non_integral_constant_expression_p
= true;
3130 if (parser
->integral_constant_expression_p
)
3132 if (!parser
->allow_non_integral_constant_expression_p
)
3134 const char *msg
= NULL
;
3138 pedwarn (input_location
, OPT_Wpedantic
,
3139 "ISO C++ forbids using a floating-point literal "
3140 "in a constant-expression");
3143 error ("a cast to a type other than an integral or "
3144 "enumeration type cannot appear in a "
3145 "constant-expression");
3148 error ("%<typeid%> operator "
3149 "cannot appear in a constant-expression");
3152 error ("non-constant compound literals "
3153 "cannot appear in a constant-expression");
3156 error ("a function call "
3157 "cannot appear in a constant-expression");
3160 error ("an increment "
3161 "cannot appear in a constant-expression");
3164 error ("an decrement "
3165 "cannot appear in a constant-expression");
3168 error ("an array reference "
3169 "cannot appear in a constant-expression");
3171 case NIC_ADDR_LABEL
:
3172 error ("the address of a label "
3173 "cannot appear in a constant-expression");
3175 case NIC_OVERLOADED
:
3176 error ("calls to overloaded operators "
3177 "cannot appear in a constant-expression");
3179 case NIC_ASSIGNMENT
:
3180 error ("an assignment cannot appear in a constant-expression");
3183 error ("a comma operator "
3184 "cannot appear in a constant-expression");
3186 case NIC_CONSTRUCTOR
:
3187 error ("a call to a constructor "
3188 "cannot appear in a constant-expression");
3190 case NIC_TRANSACTION
:
3191 error ("a transaction expression "
3192 "cannot appear in a constant-expression");
3198 msg
= "__FUNCTION__";
3200 case NIC_PRETTY_FUNC
:
3201 msg
= "__PRETTY_FUNCTION__";
3221 case NIC_PREINCREMENT
:
3224 case NIC_PREDECREMENT
:
3237 error ("%qs cannot appear in a constant-expression", msg
);
3244 /* Emit a diagnostic for an invalid type name. This function commits
3245 to the current active tentative parse, if any. (Otherwise, the
3246 problematic construct might be encountered again later, resulting
3247 in duplicate error messages.) LOCATION is the location of ID. */
3250 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3251 location_t location
)
3253 tree decl
, ambiguous_decls
;
3254 cp_parser_commit_to_tentative_parse (parser
);
3255 /* Try to lookup the identifier. */
3256 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3257 /*is_template=*/false,
3258 /*is_namespace=*/false,
3259 /*check_dependency=*/true,
3260 &ambiguous_decls
, location
);
3261 if (ambiguous_decls
)
3262 /* If the lookup was ambiguous, an error will already have
3265 /* If the lookup found a template-name, it means that the user forgot
3266 to specify an argument list. Emit a useful error message. */
3267 if (DECL_TYPE_TEMPLATE_P (decl
))
3270 "invalid use of template-name %qE without an argument list",
3272 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3273 inform (location
, "class template argument deduction is only available "
3274 "with -std=c++17 or -std=gnu++17");
3275 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3277 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3278 error_at (location
, "invalid use of destructor %qD as a type", id
);
3279 else if (TREE_CODE (decl
) == TYPE_DECL
)
3280 /* Something like 'unsigned A a;' */
3281 error_at (location
, "invalid combination of multiple type-specifiers");
3282 else if (!parser
->scope
)
3284 /* Issue an error message. */
3286 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3287 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3290 gcc_rich_location
richloc (location
);
3291 richloc
.add_fixit_replace (hint
.suggestion ());
3293 "%qE does not name a type; did you mean %qs?",
3294 id
, hint
.suggestion ());
3297 error_at (location
, "%qE does not name a type", id
);
3298 /* If we're in a template class, it's possible that the user was
3299 referring to a type from a base class. For example:
3301 template <typename T> struct A { typedef T X; };
3302 template <typename T> struct B : public A<T> { X x; };
3304 The user should have said "typename A<T>::X". */
3305 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3306 inform (location
, "C++11 %<constexpr%> only available with "
3307 "-std=c++11 or -std=gnu++11");
3308 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3309 inform (location
, "C++11 %<noexcept%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (cxx_dialect
< cxx11
3312 && TREE_CODE (id
) == IDENTIFIER_NODE
3313 && id_equal (id
, "thread_local"))
3314 inform (location
, "C++11 %<thread_local%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3317 inform (location
, "%<concept%> only available with -fconcepts");
3318 else if (processing_template_decl
&& current_class_type
3319 && TYPE_BINFO (current_class_type
))
3323 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3327 tree base_type
= BINFO_TYPE (b
);
3328 if (CLASS_TYPE_P (base_type
)
3329 && dependent_type_p (base_type
))
3332 /* Go from a particular instantiation of the
3333 template (which will have an empty TYPE_FIELDs),
3334 to the main version. */
3335 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3336 for (field
= TYPE_FIELDS (base_type
);
3338 field
= DECL_CHAIN (field
))
3339 if (TREE_CODE (field
) == TYPE_DECL
3340 && DECL_NAME (field
) == id
)
3343 "(perhaps %<typename %T::%E%> was intended)",
3344 BINFO_TYPE (b
), id
);
3353 /* Here we diagnose qualified-ids where the scope is actually correct,
3354 but the identifier does not resolve to a valid type name. */
3355 else if (parser
->scope
!= error_mark_node
)
3357 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3360 error_at (location_of (id
),
3361 "%qE in namespace %qE does not name a template type",
3363 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3364 error_at (location_of (id
),
3365 "%qE in namespace %qE does not name a template type",
3366 TREE_OPERAND (id
, 0), parser
->scope
);
3368 error_at (location_of (id
),
3369 "%qE in namespace %qE does not name a type",
3372 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3373 else if (decl
== error_mark_node
)
3374 suggest_alternative_in_explicit_scope (location
, id
,
3377 else if (CLASS_TYPE_P (parser
->scope
)
3378 && constructor_name_p (id
, parser
->scope
))
3381 error_at (location
, "%<%T::%E%> names the constructor, not"
3382 " the type", parser
->scope
, id
);
3383 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3384 error_at (location
, "and %qT has no template constructors",
3387 else if (TYPE_P (parser
->scope
)
3388 && dependent_scope_p (parser
->scope
))
3390 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3392 "need %<typename%> before %<%T::%D::%E%> because "
3393 "%<%T::%D%> is a dependent scope",
3394 TYPE_CONTEXT (parser
->scope
),
3395 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3397 TYPE_CONTEXT (parser
->scope
),
3398 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3400 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3401 "%qT is a dependent scope",
3402 parser
->scope
, id
, parser
->scope
);
3404 else if (TYPE_P (parser
->scope
))
3406 if (!COMPLETE_TYPE_P (parser
->scope
))
3407 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3409 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3410 error_at (location_of (id
),
3411 "%qE in %q#T does not name a template type",
3413 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3414 error_at (location_of (id
),
3415 "%qE in %q#T does not name a template type",
3416 TREE_OPERAND (id
, 0), parser
->scope
);
3418 error_at (location_of (id
),
3419 "%qE in %q#T does not name a type",
3422 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3429 /* Check for a common situation where a type-name should be present,
3430 but is not, and issue a sensible error message. Returns true if an
3431 invalid type-name was detected.
3433 The situation handled by this function are variable declarations of the
3434 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435 Usually, `ID' should name a type, but if we got here it means that it
3436 does not. We try to emit the best possible error message depending on
3437 how exactly the id-expression looks like. */
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3445 /* Avoid duplicate error about ambiguous lookup. */
3446 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3448 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3449 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3453 cp_parser_parse_tentatively (parser
);
3454 id
= cp_parser_id_expression (parser
,
3455 /*template_keyword_p=*/false,
3456 /*check_dependency_p=*/true,
3457 /*template_p=*/NULL
,
3458 /*declarator_p=*/false,
3459 /*optional_p=*/false);
3460 /* If the next token is a (, this is a function with no explicit return
3461 type, i.e. constructor, destructor or conversion op. */
3462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3463 || TREE_CODE (id
) == TYPE_DECL
)
3465 cp_parser_abort_tentative_parse (parser
);
3468 if (!cp_parser_parse_definitely (parser
))
3471 /* Emit a diagnostic for the invalid type. */
3472 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3474 /* If we aren't in the middle of a declarator (i.e. in a
3475 parameter-declaration-clause), skip to the end of the declaration;
3476 there's no point in trying to process it. */
3477 if (!parser
->in_declarator_p
)
3478 cp_parser_skip_to_end_of_block_or_statement (parser
);
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3484 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485 found an unnested token of that type. */
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3493 unsigned paren_depth
= 0;
3494 unsigned brace_depth
= 0;
3495 unsigned square_depth
= 0;
3496 unsigned condop_depth
= 0;
3498 if (recovering
&& or_ttype
== CPP_EOF
3499 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3504 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3506 /* Have we found what we're looking for before the closing paren? */
3507 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3508 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3511 switch (token
->type
)
3514 case CPP_PRAGMA_EOL
:
3515 /* If we've run out of tokens, then there is no closing `)'. */
3518 /* This is good for lambda expression capture-lists. */
3519 case CPP_OPEN_SQUARE
:
3522 case CPP_CLOSE_SQUARE
:
3523 if (!square_depth
--)
3528 /* This matches the processing in skip_to_end_of_statement. */
3533 case CPP_OPEN_BRACE
:
3536 case CPP_CLOSE_BRACE
:
3541 case CPP_OPEN_PAREN
:
3546 case CPP_CLOSE_PAREN
:
3547 if (!brace_depth
&& !paren_depth
--)
3550 cp_lexer_consume_token (parser
->lexer
);
3556 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3561 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3569 /* Consume the token. */
3570 cp_lexer_consume_token (parser
->lexer
);
3574 /* Consume tokens up to, and including, the next non-nested closing `)'.
3575 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3576 are doing error recovery. Returns -1 if OR_COMMA is true and we
3577 found an unnested token of that type. */
3580 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3585 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3586 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3587 ttype
, consume_paren
);
3590 /* Consume tokens until we reach the end of the current statement.
3591 Normally, that will be just before consuming a `;'. However, if a
3592 non-nested `}' comes first, then we stop before consuming that. */
3595 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3597 unsigned nesting_depth
= 0;
3599 /* Unwind generic function template scope if necessary. */
3600 if (parser
->fully_implicit_function_template_p
)
3601 abort_fully_implicit_template (parser
);
3605 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3607 switch (token
->type
)
3610 case CPP_PRAGMA_EOL
:
3611 /* If we've run out of tokens, stop. */
3615 /* If the next token is a `;', we have reached the end of the
3621 case CPP_CLOSE_BRACE
:
3622 /* If this is a non-nested '}', stop before consuming it.
3623 That way, when confronted with something like:
3627 we stop before consuming the closing '}', even though we
3628 have not yet reached a `;'. */
3629 if (nesting_depth
== 0)
3632 /* If it is the closing '}' for a block that we have
3633 scanned, stop -- but only after consuming the token.
3639 we will stop after the body of the erroneously declared
3640 function, but before consuming the following `typedef'
3642 if (--nesting_depth
== 0)
3644 cp_lexer_consume_token (parser
->lexer
);
3649 case CPP_OPEN_BRACE
:
3657 /* Consume the token. */
3658 cp_lexer_consume_token (parser
->lexer
);
3662 /* This function is called at the end of a statement or declaration.
3663 If the next token is a semicolon, it is consumed; otherwise, error
3664 recovery is attempted. */
3667 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3669 /* Look for the trailing `;'. */
3670 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3672 /* If there is additional (erroneous) input, skip to the end of
3674 cp_parser_skip_to_end_of_statement (parser
);
3675 /* If the next token is now a `;', consume it. */
3676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3677 cp_lexer_consume_token (parser
->lexer
);
3681 /* Skip tokens until we have consumed an entire block, or until we
3682 have consumed a non-nested `;'. */
3685 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3687 int nesting_depth
= 0;
3689 /* Unwind generic function template scope if necessary. */
3690 if (parser
->fully_implicit_function_template_p
)
3691 abort_fully_implicit_template (parser
);
3693 while (nesting_depth
>= 0)
3695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3697 switch (token
->type
)
3700 case CPP_PRAGMA_EOL
:
3701 /* If we've run out of tokens, stop. */
3705 /* Stop if this is an unnested ';'. */
3710 case CPP_CLOSE_BRACE
:
3711 /* Stop if this is an unnested '}', or closes the outermost
3714 if (nesting_depth
< 0)
3720 case CPP_OPEN_BRACE
:
3729 /* Consume the token. */
3730 cp_lexer_consume_token (parser
->lexer
);
3734 /* Skip tokens until a non-nested closing curly brace is the next
3735 token, or there are no more tokens. Return true in the first case,
3739 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3741 unsigned nesting_depth
= 0;
3745 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3747 switch (token
->type
)
3750 case CPP_PRAGMA_EOL
:
3751 /* If we've run out of tokens, stop. */
3754 case CPP_CLOSE_BRACE
:
3755 /* If the next token is a non-nested `}', then we have reached
3756 the end of the current block. */
3757 if (nesting_depth
-- == 0)
3761 case CPP_OPEN_BRACE
:
3762 /* If it the next token is a `{', then we are entering a new
3763 block. Consume the entire block. */
3771 /* Consume the token. */
3772 cp_lexer_consume_token (parser
->lexer
);
3776 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3777 parameter is the PRAGMA token, allowing us to purge the entire pragma
3781 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3785 parser
->lexer
->in_pragma
= false;
3788 token
= cp_lexer_consume_token (parser
->lexer
);
3789 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3791 /* Ensure that the pragma is not parsed again. */
3792 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3795 /* Require pragma end of line, resyncing with it as necessary. The
3796 arguments are as for cp_parser_skip_to_pragma_eol. */
3799 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3801 parser
->lexer
->in_pragma
= false;
3802 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3803 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3806 /* This is a simple wrapper around make_typename_type. When the id is
3807 an unresolved identifier node, we can provide a superior diagnostic
3808 using cp_parser_diagnose_invalid_type_name. */
3811 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3812 location_t id_location
)
3815 if (identifier_p (id
))
3817 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3818 /*complain=*/tf_none
);
3819 if (result
== error_mark_node
)
3820 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3823 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3826 /* This is a wrapper around the
3827 make_{pointer,ptrmem,reference}_declarator functions that decides
3828 which one to call based on the CODE and CLASS_TYPE arguments. The
3829 CODE argument should be one of the values returned by
3830 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3831 appertain to the pointer or reference. */
3833 static cp_declarator
*
3834 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3835 cp_cv_quals cv_qualifiers
,
3836 cp_declarator
*target
,
3839 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3840 return cp_error_declarator
;
3842 if (code
== INDIRECT_REF
)
3843 if (class_type
== NULL_TREE
)
3844 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3846 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3847 target
, attributes
);
3848 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3849 return make_reference_declarator (cv_qualifiers
, target
,
3851 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3852 return make_reference_declarator (cv_qualifiers
, target
,
3857 /* Create a new C++ parser. */
3860 cp_parser_new (void)
3866 /* cp_lexer_new_main is called before doing GC allocation because
3867 cp_lexer_new_main might load a PCH file. */
3868 lexer
= cp_lexer_new_main ();
3870 /* Initialize the binops_by_token so that we can get the tree
3871 directly from the token. */
3872 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3873 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3875 parser
= ggc_cleared_alloc
<cp_parser
> ();
3876 parser
->lexer
= lexer
;
3877 parser
->context
= cp_parser_context_new (NULL
);
3879 /* For now, we always accept GNU extensions. */
3880 parser
->allow_gnu_extensions_p
= 1;
3882 /* The `>' token is a greater-than operator, not the end of a
3884 parser
->greater_than_is_operator_p
= true;
3886 parser
->default_arg_ok_p
= true;
3888 /* We are not parsing a constant-expression. */
3889 parser
->integral_constant_expression_p
= false;
3890 parser
->allow_non_integral_constant_expression_p
= false;
3891 parser
->non_integral_constant_expression_p
= false;
3893 /* Local variable names are not forbidden. */
3894 parser
->local_variables_forbidden_p
= false;
3896 /* We are not processing an `extern "C"' declaration. */
3897 parser
->in_unbraced_linkage_specification_p
= false;
3899 /* We are not processing a declarator. */
3900 parser
->in_declarator_p
= false;
3902 /* We are not processing a template-argument-list. */
3903 parser
->in_template_argument_list_p
= false;
3905 /* We are not in an iteration statement. */
3906 parser
->in_statement
= 0;
3908 /* We are not in a switch statement. */
3909 parser
->in_switch_statement_p
= false;
3911 /* We are not parsing a type-id inside an expression. */
3912 parser
->in_type_id_in_expr_p
= false;
3914 /* Declarations aren't implicitly extern "C". */
3915 parser
->implicit_extern_c
= false;
3917 /* String literals should be translated to the execution character set. */
3918 parser
->translate_strings_p
= true;
3920 /* We are not parsing a function body. */
3921 parser
->in_function_body
= false;
3923 /* We can correct until told otherwise. */
3924 parser
->colon_corrects_to_scope_p
= true;
3926 /* The unparsed function queue is empty. */
3927 push_unparsed_function_queues (parser
);
3929 /* There are no classes being defined. */
3930 parser
->num_classes_being_defined
= 0;
3932 /* No template parameters apply. */
3933 parser
->num_template_parameter_lists
= 0;
3935 /* Special parsing data structures. */
3936 parser
->omp_declare_simd
= NULL
;
3937 parser
->oacc_routine
= NULL
;
3939 /* Not declaring an implicit function template. */
3940 parser
->auto_is_implicit_function_template_parm_p
= false;
3941 parser
->fully_implicit_function_template_p
= false;
3942 parser
->implicit_template_parms
= 0;
3943 parser
->implicit_template_scope
= 0;
3945 /* Allow constrained-type-specifiers. */
3946 parser
->prevent_constrained_type_specifiers
= 0;
3948 /* We haven't yet seen an 'extern "C"'. */
3949 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
3954 /* Create a cp_lexer structure which will emit the tokens in CACHE
3955 and push it onto the parser's lexer stack. This is used for delayed
3956 parsing of in-class method bodies and default arguments, and should
3957 not be confused with tentative parsing. */
3959 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3961 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3962 lexer
->next
= parser
->lexer
;
3963 parser
->lexer
= lexer
;
3965 /* Move the current source position to that of the first token in the
3967 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3970 /* Pop the top lexer off the parser stack. This is never used for the
3971 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3973 cp_parser_pop_lexer (cp_parser
*parser
)
3975 cp_lexer
*lexer
= parser
->lexer
;
3976 parser
->lexer
= lexer
->next
;
3977 cp_lexer_destroy (lexer
);
3979 /* Put the current source position back where it was before this
3980 lexer was pushed. */
3981 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3984 /* Lexical conventions [gram.lex] */
3986 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3990 cp_parser_identifier (cp_parser
* parser
)
3994 /* Look for the identifier. */
3995 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3996 /* Return the value. */
3998 return cp_expr (token
->u
.value
, token
->location
);
4000 return error_mark_node
;
4003 /* Parse a sequence of adjacent string constants. Returns a
4004 TREE_STRING representing the combined, nul-terminated string
4005 constant. If TRANSLATE is true, translate the string to the
4006 execution character set. If WIDE_OK is true, a wide string is
4009 C++98 [lex.string] says that if a narrow string literal token is
4010 adjacent to a wide string literal token, the behavior is undefined.
4011 However, C99 6.4.5p4 says that this results in a wide string literal.
4012 We follow C99 here, for consistency with the C front end.
4014 This code is largely lifted from lex_string() in c-lex.c.
4016 FUTURE: ObjC++ will need to handle @-strings here. */
4018 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4019 bool lookup_udlit
= true)
4023 struct obstack str_ob
;
4024 cpp_string str
, istr
, *strs
;
4026 enum cpp_ttype type
, curr_type
;
4027 int have_suffix_p
= 0;
4029 tree suffix_id
= NULL_TREE
;
4030 bool curr_tok_is_userdef_p
= false;
4032 tok
= cp_lexer_peek_token (parser
->lexer
);
4033 if (!cp_parser_is_string_literal (tok
))
4035 cp_parser_error (parser
, "expected string-literal");
4036 return error_mark_node
;
4039 location_t loc
= tok
->location
;
4041 if (cpp_userdef_string_p (tok
->type
))
4043 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4044 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4045 curr_tok_is_userdef_p
= true;
4049 string_tree
= tok
->u
.value
;
4050 curr_type
= tok
->type
;
4054 /* Try to avoid the overhead of creating and destroying an obstack
4055 for the common case of just one string. */
4056 if (!cp_parser_is_string_literal
4057 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4059 cp_lexer_consume_token (parser
->lexer
);
4061 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4062 str
.len
= TREE_STRING_LENGTH (string_tree
);
4065 if (curr_tok_is_userdef_p
)
4067 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4069 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4072 curr_type
= tok
->type
;
4078 location_t last_tok_loc
= tok
->location
;
4079 gcc_obstack_init (&str_ob
);
4084 cp_lexer_consume_token (parser
->lexer
);
4086 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4087 str
.len
= TREE_STRING_LENGTH (string_tree
);
4089 if (curr_tok_is_userdef_p
)
4091 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4092 if (have_suffix_p
== 0)
4094 suffix_id
= curr_suffix_id
;
4097 else if (have_suffix_p
== 1
4098 && curr_suffix_id
!= suffix_id
)
4100 error ("inconsistent user-defined literal suffixes"
4101 " %qD and %qD in string literal",
4102 suffix_id
, curr_suffix_id
);
4105 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4108 curr_type
= tok
->type
;
4110 if (type
!= curr_type
)
4112 if (type
== CPP_STRING
)
4114 else if (curr_type
!= CPP_STRING
)
4116 rich_location
rich_loc (line_table
, tok
->location
);
4117 rich_loc
.add_range (last_tok_loc
, false);
4118 error_at (&rich_loc
,
4119 "unsupported non-standard concatenation "
4120 "of string literals");
4124 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4126 last_tok_loc
= tok
->location
;
4128 tok
= cp_lexer_peek_token (parser
->lexer
);
4129 if (cpp_userdef_string_p (tok
->type
))
4131 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4132 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4133 curr_tok_is_userdef_p
= true;
4137 string_tree
= tok
->u
.value
;
4138 curr_type
= tok
->type
;
4139 curr_tok_is_userdef_p
= false;
4142 while (cp_parser_is_string_literal (tok
));
4144 /* A string literal built by concatenation has its caret=start at
4145 the start of the initial string, and its finish at the finish of
4146 the final string literal. */
4147 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4149 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4152 if (type
!= CPP_STRING
&& !wide_ok
)
4154 cp_parser_error (parser
, "a wide string is invalid in this context");
4158 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4159 (parse_in
, strs
, count
, &istr
, type
))
4161 value
= build_string (istr
.len
, (const char *)istr
.text
);
4162 free (CONST_CAST (unsigned char *, istr
.text
));
4168 case CPP_UTF8STRING
:
4169 TREE_TYPE (value
) = char_array_type_node
;
4172 TREE_TYPE (value
) = char16_array_type_node
;
4175 TREE_TYPE (value
) = char32_array_type_node
;
4178 TREE_TYPE (value
) = wchar_array_type_node
;
4182 value
= fix_string_type (value
);
4186 tree literal
= build_userdef_literal (suffix_id
, value
,
4187 OT_NONE
, NULL_TREE
);
4189 value
= cp_parser_userdef_string_literal (literal
);
4195 /* cpp_interpret_string has issued an error. */
4196 value
= error_mark_node
;
4199 obstack_free (&str_ob
, 0);
4201 return cp_expr (value
, loc
);
4204 /* Look up a literal operator with the name and the exact arguments. */
4207 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4210 decl
= lookup_name (name
);
4211 if (!decl
|| !is_overloaded_fn (decl
))
4212 return error_mark_node
;
4214 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4219 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4220 if (parmtypes
!= NULL_TREE
)
4222 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4223 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4225 tree tparm
= TREE_VALUE (parmtypes
);
4226 tree targ
= TREE_TYPE ((*args
)[ix
]);
4227 bool ptr
= TYPE_PTR_P (tparm
);
4228 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4229 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4231 || !same_type_p (TREE_TYPE (tparm
),
4236 && ix
== vec_safe_length (args
)
4237 /* May be this should be sufficient_parms_p instead,
4238 depending on how exactly should user-defined literals
4239 work in presence of default arguments on the literal
4240 operator parameters. */
4241 && parmtypes
== void_list_node
)
4246 return error_mark_node
;
4249 /* Parse a user-defined char constant. Returns a call to a user-defined
4250 literal operator taking the character as an argument. */
4253 cp_parser_userdef_char_literal (cp_parser
*parser
)
4255 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4256 tree literal
= token
->u
.value
;
4257 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4258 tree value
= USERDEF_LITERAL_VALUE (literal
);
4259 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4262 /* Build up a call to the user-defined operator */
4263 /* Lookup the name we got back from the id-expression. */
4264 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4265 vec_safe_push (args
, value
);
4266 decl
= lookup_literal_operator (name
, args
);
4267 if (!decl
|| decl
== error_mark_node
)
4269 error ("unable to find character literal operator %qD with %qT argument",
4270 name
, TREE_TYPE (value
));
4271 release_tree_vector (args
);
4272 return error_mark_node
;
4274 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4275 release_tree_vector (args
);
4279 /* A subroutine of cp_parser_userdef_numeric_literal to
4280 create a char... template parameter pack from a string node. */
4283 make_char_string_pack (tree value
)
4286 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4287 const char *str
= TREE_STRING_POINTER (value
);
4288 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4289 tree argvec
= make_tree_vec (1);
4291 /* Fill in CHARVEC with all of the parameters. */
4292 charvec
= make_tree_vec (len
);
4293 for (i
= 0; i
< len
; ++i
)
4294 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4296 /* Build the argument packs. */
4297 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4299 TREE_VEC_ELT (argvec
, 0) = argpack
;
4304 /* A subroutine of cp_parser_userdef_numeric_literal to
4305 create a char... template parameter pack from a string node. */
4308 make_string_pack (tree value
)
4311 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4312 const unsigned char *str
4313 = (const unsigned char *) TREE_STRING_POINTER (value
);
4314 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4315 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4316 tree argvec
= make_tree_vec (2);
4318 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4319 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4321 /* First template parm is character type. */
4322 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4324 /* Fill in CHARVEC with all of the parameters. */
4325 charvec
= make_tree_vec (len
);
4326 for (int i
= 0; i
< len
; ++i
)
4327 TREE_VEC_ELT (charvec
, i
)
4328 = double_int_to_tree (str_char_type_node
,
4329 double_int::from_buffer (str
+ i
* sz
, sz
));
4331 /* Build the argument packs. */
4332 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4334 TREE_VEC_ELT (argvec
, 1) = argpack
;
4339 /* Parse a user-defined numeric constant. returns a call to a user-defined
4340 literal operator. */
4343 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4345 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4346 tree literal
= token
->u
.value
;
4347 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4348 tree value
= USERDEF_LITERAL_VALUE (literal
);
4349 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4350 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4351 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4353 vec
<tree
, va_gc
> *args
;
4355 /* Look for a literal operator taking the exact type of numeric argument
4356 as the literal value. */
4357 args
= make_tree_vector ();
4358 vec_safe_push (args
, value
);
4359 decl
= lookup_literal_operator (name
, args
);
4360 if (decl
&& decl
!= error_mark_node
)
4362 result
= finish_call_expr (decl
, &args
, false, true,
4363 tf_warning_or_error
);
4365 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4367 warning_at (token
->location
, OPT_Woverflow
,
4368 "integer literal exceeds range of %qT type",
4369 long_long_unsigned_type_node
);
4374 warning_at (token
->location
, OPT_Woverflow
,
4375 "floating literal exceeds range of %qT type",
4376 long_double_type_node
);
4377 else if (overflow
< 0)
4378 warning_at (token
->location
, OPT_Woverflow
,
4379 "floating literal truncated to zero");
4382 release_tree_vector (args
);
4385 release_tree_vector (args
);
4387 /* If the numeric argument didn't work, look for a raw literal
4388 operator taking a const char* argument consisting of the number
4389 in string format. */
4390 args
= make_tree_vector ();
4391 vec_safe_push (args
, num_string
);
4392 decl
= lookup_literal_operator (name
, args
);
4393 if (decl
&& decl
!= error_mark_node
)
4395 result
= finish_call_expr (decl
, &args
, false, true,
4396 tf_warning_or_error
);
4397 release_tree_vector (args
);
4400 release_tree_vector (args
);
4402 /* If the raw literal didn't work, look for a non-type template
4403 function with parameter pack char.... Call the function with
4404 template parameter characters representing the number. */
4405 args
= make_tree_vector ();
4406 decl
= lookup_literal_operator (name
, args
);
4407 if (decl
&& decl
!= error_mark_node
)
4409 tree tmpl_args
= make_char_string_pack (num_string
);
4410 decl
= lookup_template_function (decl
, tmpl_args
);
4411 result
= finish_call_expr (decl
, &args
, false, true,
4412 tf_warning_or_error
);
4413 release_tree_vector (args
);
4417 release_tree_vector (args
);
4419 /* In C++14 the standard library defines complex number suffixes that
4420 conflict with GNU extensions. Prefer them if <complex> is #included. */
4421 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4422 bool i14
= (cxx_dialect
> cxx11
4423 && (id_equal (suffix_id
, "i")
4424 || id_equal (suffix_id
, "if")
4425 || id_equal (suffix_id
, "il")));
4426 diagnostic_t kind
= DK_ERROR
;
4431 tree cxlit
= lookup_qualified_name (std_node
,
4432 get_identifier ("complex_literals"),
4434 if (cxlit
== error_mark_node
)
4436 /* No <complex>, so pedwarn and use GNU semantics. */
4438 opt
= OPT_Wpedantic
;
4443 = emit_diagnostic (kind
, input_location
, opt
,
4444 "unable to find numeric literal operator %qD", name
);
4447 /* Don't inform either. */;
4450 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4451 "(from <complex>) to enable the C++14 user-defined literal "
4454 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4455 "GNU built-in suffix");
4458 inform (token
->location
, "use -fext-numeric-literals "
4459 "to enable more built-in suffixes");
4461 if (kind
== DK_ERROR
)
4462 value
= error_mark_node
;
4465 /* Use the built-in semantics. */
4467 if (id_equal (suffix_id
, "i"))
4469 if (TREE_CODE (value
) == INTEGER_CST
)
4470 type
= integer_type_node
;
4472 type
= double_type_node
;
4474 else if (id_equal (suffix_id
, "if"))
4475 type
= float_type_node
;
4476 else /* if (id_equal (suffix_id, "il")) */
4477 type
= long_double_type_node
;
4479 value
= build_complex (build_complex_type (type
),
4480 fold_convert (type
, integer_zero_node
),
4481 fold_convert (type
, value
));
4484 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4485 /* Avoid repeated diagnostics. */
4486 token
->u
.value
= value
;
4490 /* Parse a user-defined string constant. Returns a call to a user-defined
4491 literal operator taking a character pointer and the length of the string
4495 cp_parser_userdef_string_literal (tree literal
)
4497 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4498 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4499 tree value
= USERDEF_LITERAL_VALUE (literal
);
4500 int len
= TREE_STRING_LENGTH (value
)
4501 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4503 vec
<tree
, va_gc
> *args
;
4505 /* Build up a call to the user-defined operator. */
4506 /* Lookup the name we got back from the id-expression. */
4507 args
= make_tree_vector ();
4508 vec_safe_push (args
, value
);
4509 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4510 decl
= lookup_literal_operator (name
, args
);
4512 if (decl
&& decl
!= error_mark_node
)
4514 result
= finish_call_expr (decl
, &args
, false, true,
4515 tf_warning_or_error
);
4516 release_tree_vector (args
);
4519 release_tree_vector (args
);
4521 /* Look for a template function with typename parameter CharT
4522 and parameter pack CharT... Call the function with
4523 template parameter characters representing the string. */
4524 args
= make_tree_vector ();
4525 decl
= lookup_literal_operator (name
, args
);
4526 if (decl
&& decl
!= error_mark_node
)
4528 tree tmpl_args
= make_string_pack (value
);
4529 decl
= lookup_template_function (decl
, tmpl_args
);
4530 result
= finish_call_expr (decl
, &args
, false, true,
4531 tf_warning_or_error
);
4532 release_tree_vector (args
);
4535 release_tree_vector (args
);
4537 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4538 name
, TREE_TYPE (value
), size_type_node
);
4539 return error_mark_node
;
4543 /* Basic concepts [gram.basic] */
4545 /* Parse a translation-unit.
4548 declaration-seq [opt]
4550 Returns TRUE if all went well. */
4553 cp_parser_translation_unit (cp_parser
* parser
)
4555 /* The address of the first non-permanent object on the declarator
4557 static void *declarator_obstack_base
;
4561 /* Create the declarator obstack, if necessary. */
4562 if (!cp_error_declarator
)
4564 gcc_obstack_init (&declarator_obstack
);
4565 /* Create the error declarator. */
4566 cp_error_declarator
= make_declarator (cdk_error
);
4567 /* Create the empty parameter list. */
4568 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4570 /* Remember where the base of the declarator obstack lies. */
4571 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4574 cp_parser_declaration_seq_opt (parser
);
4576 /* If there are no tokens left then all went well. */
4577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4579 /* Get rid of the token array; we don't need it any more. */
4580 cp_lexer_destroy (parser
->lexer
);
4581 parser
->lexer
= NULL
;
4583 /* This file might have been a context that's implicitly extern
4584 "C". If so, pop the lang context. (Only relevant for PCH.) */
4585 if (parser
->implicit_extern_c
)
4587 pop_lang_context ();
4588 parser
->implicit_extern_c
= false;
4592 finish_translation_unit ();
4598 cp_parser_error (parser
, "expected declaration");
4602 /* Make sure the declarator obstack was fully cleaned up. */
4603 gcc_assert (obstack_next_free (&declarator_obstack
)
4604 == declarator_obstack_base
);
4606 /* All went well. */
4610 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4611 decltype context. */
4613 static inline tsubst_flags_t
4614 complain_flags (bool decltype_p
)
4616 tsubst_flags_t complain
= tf_warning_or_error
;
4618 complain
|= tf_decltype
;
4622 /* We're about to parse a collection of statements. If we're currently
4623 parsing tentatively, set up a firewall so that any nested
4624 cp_parser_commit_to_tentative_parse won't affect the current context. */
4626 static cp_token_position
4627 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4632 cp_parser_parse_tentatively (parser
);
4633 cp_parser_commit_to_topmost_tentative_parse (parser
);
4634 return cp_lexer_token_position (parser
->lexer
, false);
4637 /* We've finished parsing the collection of statements. Wrap up the
4638 firewall and replace the relevant tokens with the parsed form. */
4641 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4647 /* Finish the firewall level. */
4648 cp_parser_parse_definitely (parser
);
4649 /* And remember the result of the parse for when we try again. */
4650 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4651 token
->type
= CPP_PREPARSED_EXPR
;
4652 token
->u
.value
= expr
;
4653 token
->keyword
= RID_MAX
;
4654 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4657 /* Like the above functions, but let the user modify the tokens. Used by
4658 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4659 later parses, so it makes sense to localize the effects of
4660 cp_parser_commit_to_tentative_parse. */
4662 struct tentative_firewall
4667 tentative_firewall (cp_parser
*p
): parser(p
)
4669 /* If we're currently parsing tentatively, start a committed level as a
4670 firewall and then an inner tentative parse. */
4671 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4673 cp_parser_parse_tentatively (parser
);
4674 cp_parser_commit_to_topmost_tentative_parse (parser
);
4675 cp_parser_parse_tentatively (parser
);
4679 ~tentative_firewall()
4683 /* Finish the inner tentative parse and the firewall, propagating any
4684 uncommitted error state to the outer tentative parse. */
4685 bool err
= cp_parser_error_occurred (parser
);
4686 cp_parser_parse_definitely (parser
);
4687 cp_parser_parse_definitely (parser
);
4689 cp_parser_simulate_error (parser
);
4694 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4695 This class is for tracking such a matching pair of symbols.
4696 In particular, it tracks the location of the first token,
4697 so that if the second token is missing, we can highlight the
4698 location of the first token when notifying the user about the
4701 template <typename traits_t
>
4705 /* token_pair's ctor. */
4706 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4708 /* If the next token is the opening symbol for this pair, consume it and
4710 Otherwise, issue an error and return false.
4711 In either case, record the location of the opening token. */
4713 bool require_open (cp_parser
*parser
)
4715 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4716 return cp_parser_require (parser
, traits_t::open_token_type
,
4717 traits_t::required_token_open
);
4720 /* Consume the next token from PARSER, recording its location as
4721 that of the opening token within the pair. */
4723 cp_token
* consume_open (cp_parser
*parser
)
4725 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4726 gcc_assert (tok
->type
== traits_t::open_token_type
);
4727 m_open_loc
= tok
->location
;
4731 /* If the next token is the closing symbol for this pair, consume it
4733 Otherwise, issue an error, highlighting the location of the
4734 corresponding opening token, and return NULL. */
4736 cp_token
*require_close (cp_parser
*parser
) const
4738 return cp_parser_require (parser
, traits_t::close_token_type
,
4739 traits_t::required_token_close
,
4744 location_t m_open_loc
;
4747 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4749 struct matching_paren_traits
4751 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4752 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4753 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4754 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4757 /* "matching_parens" is a token_pair<T> class for tracking matching
4758 pairs of parentheses. */
4760 typedef token_pair
<matching_paren_traits
> matching_parens
;
4762 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4764 struct matching_brace_traits
4766 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4767 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4768 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4769 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4772 /* "matching_braces" is a token_pair<T> class for tracking matching
4775 typedef token_pair
<matching_brace_traits
> matching_braces
;
4778 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4779 enclosing parentheses. */
4782 cp_parser_statement_expr (cp_parser
*parser
)
4784 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4786 /* Consume the '('. */
4787 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4788 matching_parens parens
;
4789 parens
.consume_open (parser
);
4790 /* Start the statement-expression. */
4791 tree expr
= begin_stmt_expr ();
4792 /* Parse the compound-statement. */
4793 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4795 expr
= finish_stmt_expr (expr
, false);
4796 /* Consume the ')'. */
4797 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4798 if (!parens
.require_close (parser
))
4799 cp_parser_skip_to_end_of_statement (parser
);
4801 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4802 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4803 return cp_expr (expr
, combined_loc
);
4806 /* Expressions [gram.expr] */
4808 /* Parse a fold-operator.
4811 - * / % ^ & | = < > << >>
4812 = -= *= /= %= ^= &= |= <<= >>=
4813 == != <= >= && || , .* ->*
4815 This returns the tree code corresponding to the matched operator
4816 as an int. When the current token matches a compound assignment
4817 opertor, the resulting tree code is the negative value of the
4818 non-assignment operator. */
4821 cp_parser_fold_operator (cp_token
*token
)
4823 switch (token
->type
)
4825 case CPP_PLUS
: return PLUS_EXPR
;
4826 case CPP_MINUS
: return MINUS_EXPR
;
4827 case CPP_MULT
: return MULT_EXPR
;
4828 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4829 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4830 case CPP_XOR
: return BIT_XOR_EXPR
;
4831 case CPP_AND
: return BIT_AND_EXPR
;
4832 case CPP_OR
: return BIT_IOR_EXPR
;
4833 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4834 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4836 case CPP_EQ
: return -NOP_EXPR
;
4837 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4838 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4839 case CPP_MULT_EQ
: return -MULT_EXPR
;
4840 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4841 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4842 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4843 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4844 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4845 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4846 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4848 case CPP_EQ_EQ
: return EQ_EXPR
;
4849 case CPP_NOT_EQ
: return NE_EXPR
;
4850 case CPP_LESS
: return LT_EXPR
;
4851 case CPP_GREATER
: return GT_EXPR
;
4852 case CPP_LESS_EQ
: return LE_EXPR
;
4853 case CPP_GREATER_EQ
: return GE_EXPR
;
4855 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4856 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4858 case CPP_COMMA
: return COMPOUND_EXPR
;
4860 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4861 case CPP_DEREF_STAR
: return MEMBER_REF
;
4863 default: return ERROR_MARK
;
4867 /* Returns true if CODE indicates a binary expression, which is not allowed in
4868 the LHS of a fold-expression. More codes will need to be added to use this
4869 function in other contexts. */
4872 is_binary_op (tree_code code
)
4877 case POINTER_PLUS_EXPR
:
4880 case TRUNC_DIV_EXPR
:
4881 case TRUNC_MOD_EXPR
:
4897 case TRUTH_ANDIF_EXPR
:
4898 case TRUTH_ORIF_EXPR
:
4911 /* If the next token is a suitable fold operator, consume it and return as
4912 the function above. */
4915 cp_parser_fold_operator (cp_parser
*parser
)
4917 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4918 int code
= cp_parser_fold_operator (token
);
4919 if (code
!= ERROR_MARK
)
4920 cp_lexer_consume_token (parser
->lexer
);
4924 /* Parse a fold-expression.
4927 ( ... folding-operator cast-expression)
4928 ( cast-expression folding-operator ... )
4929 ( cast-expression folding operator ... folding-operator cast-expression)
4931 Note that the '(' and ')' are matched in primary expression. */
4934 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4941 cp_lexer_consume_token (parser
->lexer
);
4942 int op
= cp_parser_fold_operator (parser
);
4943 if (op
== ERROR_MARK
)
4945 cp_parser_error (parser
, "expected binary operator");
4946 return error_mark_node
;
4949 tree expr
= cp_parser_cast_expression (parser
, false, false,
4951 if (expr
== error_mark_node
)
4952 return error_mark_node
;
4953 return finish_left_unary_fold_expr (expr
, op
);
4956 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4957 int op
= cp_parser_fold_operator (parser
);
4958 if (op
== ERROR_MARK
)
4960 cp_parser_error (parser
, "expected binary operator");
4961 return error_mark_node
;
4964 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4966 cp_parser_error (parser
, "expected ...");
4967 return error_mark_node
;
4969 cp_lexer_consume_token (parser
->lexer
);
4971 /* The operands of a fold-expression are cast-expressions, so binary or
4972 conditional expressions are not allowed. We check this here to avoid
4973 tentative parsing. */
4974 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
4975 /* OK, the expression was parenthesized. */;
4976 else if (is_binary_op (TREE_CODE (expr1
)))
4977 error_at (location_of (expr1
),
4978 "binary expression in operand of fold-expression");
4979 else if (TREE_CODE (expr1
) == COND_EXPR
4980 || (REFERENCE_REF_P (expr1
)
4981 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
4982 error_at (location_of (expr1
),
4983 "conditional expression in operand of fold-expression");
4986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4987 return finish_right_unary_fold_expr (expr1
, op
);
4989 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4991 cp_parser_error (parser
, "mismatched operator in fold-expression");
4992 return error_mark_node
;
4994 cp_lexer_consume_token (parser
->lexer
);
4996 // Binary left or right fold.
4997 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4998 if (expr2
== error_mark_node
)
4999 return error_mark_node
;
5000 return finish_binary_fold_expr (expr1
, expr2
, op
);
5003 /* Parse a primary-expression.
5010 lambda-expression (C++11)
5015 ( compound-statement )
5016 __builtin_va_arg ( assignment-expression , type-id )
5017 __builtin_offsetof ( type-id , offsetof-expression )
5020 __has_nothrow_assign ( type-id )
5021 __has_nothrow_constructor ( type-id )
5022 __has_nothrow_copy ( type-id )
5023 __has_trivial_assign ( type-id )
5024 __has_trivial_constructor ( type-id )
5025 __has_trivial_copy ( type-id )
5026 __has_trivial_destructor ( type-id )
5027 __has_virtual_destructor ( type-id )
5028 __is_abstract ( type-id )
5029 __is_base_of ( type-id , type-id )
5030 __is_class ( type-id )
5031 __is_empty ( type-id )
5032 __is_enum ( type-id )
5033 __is_final ( type-id )
5034 __is_literal_type ( type-id )
5035 __is_pod ( type-id )
5036 __is_polymorphic ( type-id )
5037 __is_std_layout ( type-id )
5038 __is_trivial ( type-id )
5039 __is_union ( type-id )
5041 Objective-C++ Extension:
5049 ADDRESS_P is true iff this expression was immediately preceded by
5050 "&" and therefore might denote a pointer-to-member. CAST_P is true
5051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5052 true iff this expression is a template argument.
5054 Returns a representation of the expression. Upon return, *IDK
5055 indicates what kind of id-expression (if any) was present. */
5058 cp_parser_primary_expression (cp_parser
*parser
,
5061 bool template_arg_p
,
5065 cp_token
*token
= NULL
;
5067 /* Assume the primary expression is not an id-expression. */
5068 *idk
= CP_ID_KIND_NONE
;
5070 /* Peek at the next token. */
5071 token
= cp_lexer_peek_token (parser
->lexer
);
5072 switch ((int) token
->type
)
5081 user-defined-literal */
5088 case CPP_PREPARSED_EXPR
:
5089 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5090 return cp_parser_userdef_numeric_literal (parser
);
5091 token
= cp_lexer_consume_token (parser
->lexer
);
5092 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5094 error_at (token
->location
,
5095 "fixed-point types not supported in C++");
5096 return error_mark_node
;
5098 /* Floating-point literals are only allowed in an integral
5099 constant expression if they are cast to an integral or
5100 enumeration type. */
5101 if (TREE_CODE (token
->u
.value
) == REAL_CST
5102 && parser
->integral_constant_expression_p
5105 /* CAST_P will be set even in invalid code like "int(2.7 +
5106 ...)". Therefore, we have to check that the next token
5107 is sure to end the cast. */
5110 cp_token
*next_token
;
5112 next_token
= cp_lexer_peek_token (parser
->lexer
);
5113 if (/* The comma at the end of an
5114 enumerator-definition. */
5115 next_token
->type
!= CPP_COMMA
5116 /* The curly brace at the end of an enum-specifier. */
5117 && next_token
->type
!= CPP_CLOSE_BRACE
5118 /* The end of a statement. */
5119 && next_token
->type
!= CPP_SEMICOLON
5120 /* The end of the cast-expression. */
5121 && next_token
->type
!= CPP_CLOSE_PAREN
5122 /* The end of an array bound. */
5123 && next_token
->type
!= CPP_CLOSE_SQUARE
5124 /* The closing ">" in a template-argument-list. */
5125 && (next_token
->type
!= CPP_GREATER
5126 || parser
->greater_than_is_operator_p
)
5127 /* C++0x only: A ">>" treated like two ">" tokens,
5128 in a template-argument-list. */
5129 && (next_token
->type
!= CPP_RSHIFT
5130 || (cxx_dialect
== cxx98
)
5131 || parser
->greater_than_is_operator_p
))
5135 /* If we are within a cast, then the constraint that the
5136 cast is to an integral or enumeration type will be
5137 checked at that point. If we are not within a cast, then
5138 this code is invalid. */
5140 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5142 return cp_expr (token
->u
.value
, token
->location
);
5144 case CPP_CHAR_USERDEF
:
5145 case CPP_CHAR16_USERDEF
:
5146 case CPP_CHAR32_USERDEF
:
5147 case CPP_WCHAR_USERDEF
:
5148 case CPP_UTF8CHAR_USERDEF
:
5149 return cp_parser_userdef_char_literal (parser
);
5155 case CPP_UTF8STRING
:
5156 case CPP_STRING_USERDEF
:
5157 case CPP_STRING16_USERDEF
:
5158 case CPP_STRING32_USERDEF
:
5159 case CPP_WSTRING_USERDEF
:
5160 case CPP_UTF8STRING_USERDEF
:
5161 /* ??? Should wide strings be allowed when parser->translate_strings_p
5162 is false (i.e. in attributes)? If not, we can kill the third
5163 argument to cp_parser_string_literal. */
5164 return cp_parser_string_literal (parser
,
5165 parser
->translate_strings_p
,
5168 case CPP_OPEN_PAREN
:
5169 /* If we see `( { ' then we are looking at the beginning of
5170 a GNU statement-expression. */
5171 if (cp_parser_allow_gnu_extensions_p (parser
)
5172 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5174 /* Statement-expressions are not allowed by the standard. */
5175 pedwarn (token
->location
, OPT_Wpedantic
,
5176 "ISO C++ forbids braced-groups within expressions");
5178 /* And they're not allowed outside of a function-body; you
5179 cannot, for example, write:
5181 int i = ({ int j = 3; j + 1; });
5183 at class or namespace scope. */
5184 if (!parser
->in_function_body
5185 || parser
->in_template_argument_list_p
)
5187 error_at (token
->location
,
5188 "statement-expressions are not allowed outside "
5189 "functions nor in template-argument lists");
5190 cp_parser_skip_to_end_of_block_or_statement (parser
);
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5192 cp_lexer_consume_token (parser
->lexer
);
5193 return error_mark_node
;
5196 return cp_parser_statement_expr (parser
);
5198 /* Otherwise it's a normal parenthesized expression. */
5201 bool saved_greater_than_is_operator_p
;
5203 location_t open_paren_loc
= token
->location
;
5205 /* Consume the `('. */
5206 matching_parens parens
;
5207 parens
.consume_open (parser
);
5208 /* Within a parenthesized expression, a `>' token is always
5209 the greater-than operator. */
5210 saved_greater_than_is_operator_p
5211 = parser
->greater_than_is_operator_p
;
5212 parser
->greater_than_is_operator_p
= true;
5214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5215 /* Left fold expression. */
5218 /* Parse the parenthesized expression. */
5219 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5221 token
= cp_lexer_peek_token (parser
->lexer
);
5222 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5224 expr
= cp_parser_fold_expression (parser
, expr
);
5225 if (expr
!= error_mark_node
5226 && cxx_dialect
< cxx17
5227 && !in_system_header_at (input_location
))
5228 pedwarn (input_location
, 0, "fold-expressions only available "
5229 "with -std=c++17 or -std=gnu++17");
5232 /* Let the front end know that this expression was
5233 enclosed in parentheses. This matters in case, for
5234 example, the expression is of the form `A::B', since
5235 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5237 expr
= finish_parenthesized_expr (expr
);
5239 /* DR 705: Wrapping an unqualified name in parentheses
5240 suppresses arg-dependent lookup. We want to pass back
5241 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5242 (c++/37862), but none of the others. */
5243 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5244 *idk
= CP_ID_KIND_NONE
;
5246 /* The `>' token might be the end of a template-id or
5247 template-parameter-list now. */
5248 parser
->greater_than_is_operator_p
5249 = saved_greater_than_is_operator_p
;
5251 /* Consume the `)'. */
5252 token
= cp_lexer_peek_token (parser
->lexer
);
5253 location_t close_paren_loc
= token
->location
;
5254 expr
.set_range (open_paren_loc
, close_paren_loc
);
5255 if (!parens
.require_close (parser
)
5256 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5257 cp_parser_skip_to_end_of_statement (parser
);
5262 case CPP_OPEN_SQUARE
:
5264 if (c_dialect_objc ())
5266 /* We might have an Objective-C++ message. */
5267 cp_parser_parse_tentatively (parser
);
5268 tree msg
= cp_parser_objc_message_expression (parser
);
5269 /* If that works out, we're done ... */
5270 if (cp_parser_parse_definitely (parser
))
5272 /* ... else, fall though to see if it's a lambda. */
5274 cp_expr lam
= cp_parser_lambda_expression (parser
);
5275 /* Don't warn about a failed tentative parse. */
5276 if (cp_parser_error_occurred (parser
))
5277 return error_mark_node
;
5278 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5282 case CPP_OBJC_STRING
:
5283 if (c_dialect_objc ())
5284 /* We have an Objective-C++ string literal. */
5285 return cp_parser_objc_expression (parser
);
5286 cp_parser_error (parser
, "expected primary-expression");
5287 return error_mark_node
;
5290 switch (token
->keyword
)
5292 /* These two are the boolean literals. */
5294 cp_lexer_consume_token (parser
->lexer
);
5295 return cp_expr (boolean_true_node
, token
->location
);
5297 cp_lexer_consume_token (parser
->lexer
);
5298 return cp_expr (boolean_false_node
, token
->location
);
5300 /* The `__null' literal. */
5302 cp_lexer_consume_token (parser
->lexer
);
5303 return cp_expr (null_node
, token
->location
);
5305 /* The `nullptr' literal. */
5307 cp_lexer_consume_token (parser
->lexer
);
5308 return cp_expr (nullptr_node
, token
->location
);
5310 /* Recognize the `this' keyword. */
5312 cp_lexer_consume_token (parser
->lexer
);
5313 if (parser
->local_variables_forbidden_p
)
5315 error_at (token
->location
,
5316 "%<this%> may not be used in this context");
5317 return error_mark_node
;
5319 /* Pointers cannot appear in constant-expressions. */
5320 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5321 return error_mark_node
;
5322 return cp_expr (finish_this_expr (), token
->location
);
5324 /* The `operator' keyword can be the beginning of an
5329 case RID_FUNCTION_NAME
:
5330 case RID_PRETTY_FUNCTION_NAME
:
5331 case RID_C99_FUNCTION_NAME
:
5333 non_integral_constant name
;
5335 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5336 __func__ are the names of variables -- but they are
5337 treated specially. Therefore, they are handled here,
5338 rather than relying on the generic id-expression logic
5339 below. Grammatically, these names are id-expressions.
5341 Consume the token. */
5342 token
= cp_lexer_consume_token (parser
->lexer
);
5344 switch (token
->keyword
)
5346 case RID_FUNCTION_NAME
:
5347 name
= NIC_FUNC_NAME
;
5349 case RID_PRETTY_FUNCTION_NAME
:
5350 name
= NIC_PRETTY_FUNC
;
5352 case RID_C99_FUNCTION_NAME
:
5353 name
= NIC_C99_FUNC
;
5359 if (cp_parser_non_integral_constant_expression (parser
, name
))
5360 return error_mark_node
;
5362 /* Look up the name. */
5363 return finish_fname (token
->u
.value
);
5370 source_location type_location
;
5371 location_t start_loc
5372 = cp_lexer_peek_token (parser
->lexer
)->location
;
5373 /* The `__builtin_va_arg' construct is used to handle
5374 `va_arg'. Consume the `__builtin_va_arg' token. */
5375 cp_lexer_consume_token (parser
->lexer
);
5376 /* Look for the opening `('. */
5377 matching_parens parens
;
5378 parens
.require_open (parser
);
5379 /* Now, parse the assignment-expression. */
5380 expression
= cp_parser_assignment_expression (parser
);
5381 /* Look for the `,'. */
5382 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5383 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5384 /* Parse the type-id. */
5386 type_id_in_expr_sentinel
s (parser
);
5387 type
= cp_parser_type_id (parser
);
5389 /* Look for the closing `)'. */
5390 location_t finish_loc
5391 = cp_lexer_peek_token (parser
->lexer
)->location
;
5392 parens
.require_close (parser
);
5393 /* Using `va_arg' in a constant-expression is not
5395 if (cp_parser_non_integral_constant_expression (parser
,
5397 return error_mark_node
;
5398 /* Construct a location of the form:
5399 __builtin_va_arg (v, int)
5400 ~~~~~~~~~~~~~~~~~~~~~^~~~
5401 with the caret at the type, ranging from the start of the
5402 "__builtin_va_arg" token to the close paren. */
5403 location_t combined_loc
5404 = make_location (type_location
, start_loc
, finish_loc
);
5405 return build_x_va_arg (combined_loc
, expression
, type
);
5409 return cp_parser_builtin_offsetof (parser
);
5411 case RID_HAS_NOTHROW_ASSIGN
:
5412 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5413 case RID_HAS_NOTHROW_COPY
:
5414 case RID_HAS_TRIVIAL_ASSIGN
:
5415 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5416 case RID_HAS_TRIVIAL_COPY
:
5417 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5418 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5419 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5420 case RID_IS_ABSTRACT
:
5421 case RID_IS_AGGREGATE
:
5422 case RID_IS_BASE_OF
:
5427 case RID_IS_LITERAL_TYPE
:
5429 case RID_IS_POLYMORPHIC
:
5430 case RID_IS_SAME_AS
:
5431 case RID_IS_STD_LAYOUT
:
5432 case RID_IS_TRIVIAL
:
5433 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5434 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5435 case RID_IS_TRIVIALLY_COPYABLE
:
5437 case RID_IS_ASSIGNABLE
:
5438 case RID_IS_CONSTRUCTIBLE
:
5439 return cp_parser_trait_expr (parser
, token
->keyword
);
5443 return cp_parser_requires_expression (parser
);
5445 /* Objective-C++ expressions. */
5447 case RID_AT_PROTOCOL
:
5448 case RID_AT_SELECTOR
:
5449 return cp_parser_objc_expression (parser
);
5452 if (parser
->in_function_body
5453 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5456 error_at (token
->location
,
5457 "a template declaration cannot appear at block scope");
5458 cp_parser_skip_to_end_of_block_or_statement (parser
);
5459 return error_mark_node
;
5463 cp_parser_error (parser
, "expected primary-expression");
5464 return error_mark_node
;
5467 /* An id-expression can start with either an identifier, a
5468 `::' as the beginning of a qualified-id, or the "operator"
5472 case CPP_TEMPLATE_ID
:
5473 case CPP_NESTED_NAME_SPECIFIER
:
5476 cp_expr id_expression
;
5478 const char *error_msg
;
5481 cp_token
*id_expr_token
;
5483 /* Parse the id-expression. */
5485 = cp_parser_id_expression (parser
,
5486 /*template_keyword_p=*/false,
5487 /*check_dependency_p=*/true,
5489 /*declarator_p=*/false,
5490 /*optional_p=*/false);
5491 if (id_expression
== error_mark_node
)
5492 return error_mark_node
;
5493 id_expr_token
= token
;
5494 token
= cp_lexer_peek_token (parser
->lexer
);
5495 done
= (token
->type
!= CPP_OPEN_SQUARE
5496 && token
->type
!= CPP_OPEN_PAREN
5497 && token
->type
!= CPP_DOT
5498 && token
->type
!= CPP_DEREF
5499 && token
->type
!= CPP_PLUS_PLUS
5500 && token
->type
!= CPP_MINUS_MINUS
);
5501 /* If we have a template-id, then no further lookup is
5502 required. If the template-id was for a template-class, we
5503 will sometimes have a TYPE_DECL at this point. */
5504 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5505 || TREE_CODE (id_expression
) == TYPE_DECL
)
5506 decl
= id_expression
;
5507 /* Look up the name. */
5510 tree ambiguous_decls
;
5512 /* If we already know that this lookup is ambiguous, then
5513 we've already issued an error message; there's no reason
5515 if (id_expr_token
->type
== CPP_NAME
5516 && id_expr_token
->error_reported
)
5518 cp_parser_simulate_error (parser
);
5519 return error_mark_node
;
5522 decl
= cp_parser_lookup_name (parser
, id_expression
,
5525 /*is_namespace=*/false,
5526 /*check_dependency=*/true,
5528 id_expr_token
->location
);
5529 /* If the lookup was ambiguous, an error will already have
5531 if (ambiguous_decls
)
5532 return error_mark_node
;
5534 /* In Objective-C++, we may have an Objective-C 2.0
5535 dot-syntax for classes here. */
5536 if (c_dialect_objc ()
5537 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5538 && TREE_CODE (decl
) == TYPE_DECL
5539 && objc_is_class_name (decl
))
5542 cp_lexer_consume_token (parser
->lexer
);
5543 component
= cp_parser_identifier (parser
);
5544 if (component
== error_mark_node
)
5545 return error_mark_node
;
5547 tree result
= objc_build_class_component_ref (id_expression
,
5549 /* Build a location of the form:
5552 with caret at the start of the component name (at
5553 input_location), ranging from the start of the id_expression
5554 to the end of the component name. */
5555 location_t combined_loc
5556 = make_location (input_location
, id_expression
.get_start (),
5557 get_finish (input_location
));
5558 protected_set_expr_location (result
, combined_loc
);
5562 /* In Objective-C++, an instance variable (ivar) may be preferred
5563 to whatever cp_parser_lookup_name() found.
5564 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5565 rest of c-family, we have to do a little extra work to preserve
5566 any location information in cp_expr "decl". Given that
5567 objc_lookup_ivar is implemented in "c-family" and "objc", we
5568 have a trip through the pure "tree" type, rather than cp_expr.
5569 Naively copying it back to "decl" would implicitly give the
5570 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5571 store an EXPR_LOCATION. Hence we only update "decl" (and
5572 hence its location_t) if we get back a different tree node. */
5573 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5575 if (decl_tree
!= decl
.get_value ())
5576 decl
= cp_expr (decl_tree
);
5578 /* If name lookup gives us a SCOPE_REF, then the
5579 qualifying scope was dependent. */
5580 if (TREE_CODE (decl
) == SCOPE_REF
)
5582 /* At this point, we do not know if DECL is a valid
5583 integral constant expression. We assume that it is
5584 in fact such an expression, so that code like:
5586 template <int N> struct A {
5590 is accepted. At template-instantiation time, we
5591 will check that B<N>::i is actually a constant. */
5594 /* Check to see if DECL is a local variable in a context
5595 where that is forbidden. */
5596 if (parser
->local_variables_forbidden_p
5597 && local_variable_p (decl
))
5599 error_at (id_expr_token
->location
,
5600 "local variable %qD may not appear in this context",
5602 return error_mark_node
;
5606 decl
= (finish_id_expression
5607 (id_expression
, decl
, parser
->scope
,
5609 parser
->integral_constant_expression_p
,
5610 parser
->allow_non_integral_constant_expression_p
,
5611 &parser
->non_integral_constant_expression_p
,
5612 template_p
, done
, address_p
,
5615 id_expression
.get_location ()));
5617 cp_parser_error (parser
, error_msg
);
5618 decl
.set_location (id_expr_token
->location
);
5622 /* Anything else is an error. */
5624 cp_parser_error (parser
, "expected primary-expression");
5625 return error_mark_node
;
5629 static inline cp_expr
5630 cp_parser_primary_expression (cp_parser
*parser
,
5633 bool template_arg_p
,
5636 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5637 /*decltype*/false, idk
);
5640 /* Parse an id-expression.
5647 :: [opt] nested-name-specifier template [opt] unqualified-id
5649 :: operator-function-id
5652 Return a representation of the unqualified portion of the
5653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5654 a `::' or nested-name-specifier.
5656 Often, if the id-expression was a qualified-id, the caller will
5657 want to make a SCOPE_REF to represent the qualified-id. This
5658 function does not do this in order to avoid wastefully creating
5659 SCOPE_REFs when they are not required.
5661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5665 uninstantiated templates.
5667 If *TEMPLATE_P is non-NULL, it is set to true iff the
5668 `template' keyword is used to explicitly indicate that the entity
5669 named is a template.
5671 If DECLARATOR_P is true, the id-expression is appearing as part of
5672 a declarator, rather than as part of an expression. */
5675 cp_parser_id_expression (cp_parser
*parser
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5682 bool global_scope_p
;
5683 bool nested_name_specifier_p
;
5685 /* Assume the `template' keyword was not used. */
5687 *template_p
= template_keyword_p
;
5689 /* Look for the optional `::' operator. */
5691 = (!template_keyword_p
5692 && (cp_parser_global_scope_opt (parser
,
5693 /*current_scope_valid_p=*/false)
5696 /* Look for the optional nested-name-specifier. */
5697 nested_name_specifier_p
5698 = (cp_parser_nested_name_specifier_opt (parser
,
5699 /*typename_keyword_p=*/false,
5706 /* If there is a nested-name-specifier, then we are looking at
5707 the first qualified-id production. */
5708 if (nested_name_specifier_p
)
5711 tree saved_object_scope
;
5712 tree saved_qualifying_scope
;
5713 cp_expr unqualified_id
;
5716 /* See if the next token is the `template' keyword. */
5718 template_p
= &is_template
;
5719 *template_p
= cp_parser_optional_template_keyword (parser
);
5720 /* Name lookup we do during the processing of the
5721 unqualified-id might obliterate SCOPE. */
5722 saved_scope
= parser
->scope
;
5723 saved_object_scope
= parser
->object_scope
;
5724 saved_qualifying_scope
= parser
->qualifying_scope
;
5725 /* Process the final unqualified-id. */
5726 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5729 /*optional_p=*/false);
5730 /* Restore the SAVED_SCOPE for our caller. */
5731 parser
->scope
= saved_scope
;
5732 parser
->object_scope
= saved_object_scope
;
5733 parser
->qualifying_scope
= saved_qualifying_scope
;
5735 return unqualified_id
;
5737 /* Otherwise, if we are in global scope, then we are looking at one
5738 of the other qualified-id productions. */
5739 else if (global_scope_p
)
5744 /* Peek at the next token. */
5745 token
= cp_lexer_peek_token (parser
->lexer
);
5747 /* If it's an identifier, and the next token is not a "<", then
5748 we can avoid the template-id case. This is an optimization
5749 for this common case. */
5750 if (token
->type
== CPP_NAME
5751 && !cp_parser_nth_token_starts_template_argument_list_p
5753 return cp_parser_identifier (parser
);
5755 cp_parser_parse_tentatively (parser
);
5756 /* Try a template-id. */
5757 id
= cp_parser_template_id (parser
,
5758 /*template_keyword_p=*/false,
5759 /*check_dependency_p=*/true,
5762 /* If that worked, we're done. */
5763 if (cp_parser_parse_definitely (parser
))
5766 /* Peek at the next token. (Changes in the token buffer may
5767 have invalidated the pointer obtained above.) */
5768 token
= cp_lexer_peek_token (parser
->lexer
);
5770 switch (token
->type
)
5773 return cp_parser_identifier (parser
);
5776 if (token
->keyword
== RID_OPERATOR
)
5777 return cp_parser_operator_function_id (parser
);
5781 cp_parser_error (parser
, "expected id-expression");
5782 return error_mark_node
;
5786 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5787 /*check_dependency_p=*/true,
5792 /* Parse an unqualified-id.
5796 operator-function-id
5797 conversion-function-id
5801 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5802 keyword, in a construct like `A::template ...'.
5804 Returns a representation of unqualified-id. For the `identifier'
5805 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5806 production a BIT_NOT_EXPR is returned; the operand of the
5807 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5808 other productions, see the documentation accompanying the
5809 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5810 names are looked up in uninstantiated templates. If DECLARATOR_P
5811 is true, the unqualified-id is appearing as part of a declarator,
5812 rather than as part of an expression. */
5815 cp_parser_unqualified_id (cp_parser
* parser
,
5816 bool template_keyword_p
,
5817 bool check_dependency_p
,
5823 /* Peek at the next token. */
5824 token
= cp_lexer_peek_token (parser
->lexer
);
5826 switch ((int) token
->type
)
5832 /* We don't know yet whether or not this will be a
5834 cp_parser_parse_tentatively (parser
);
5835 /* Try a template-id. */
5836 id
= cp_parser_template_id (parser
, template_keyword_p
,
5840 /* If it worked, we're done. */
5841 if (cp_parser_parse_definitely (parser
))
5843 /* Otherwise, it's an ordinary identifier. */
5844 return cp_parser_identifier (parser
);
5847 case CPP_TEMPLATE_ID
:
5848 return cp_parser_template_id (parser
, template_keyword_p
,
5856 tree qualifying_scope
;
5861 /* Consume the `~' token. */
5862 cp_lexer_consume_token (parser
->lexer
);
5863 /* Parse the class-name. The standard, as written, seems to
5866 template <typename T> struct S { ~S (); };
5867 template <typename T> S<T>::~S() {}
5869 is invalid, since `~' must be followed by a class-name, but
5870 `S<T>' is dependent, and so not known to be a class.
5871 That's not right; we need to look in uninstantiated
5872 templates. A further complication arises from:
5874 template <typename T> void f(T t) {
5878 Here, it is not possible to look up `T' in the scope of `T'
5879 itself. We must look in both the current scope, and the
5880 scope of the containing complete expression.
5882 Yet another issue is:
5891 The standard does not seem to say that the `S' in `~S'
5892 should refer to the type `S' and not the data member
5895 /* DR 244 says that we look up the name after the "~" in the
5896 same scope as we looked up the qualifying name. That idea
5897 isn't fully worked out; it's more complicated than that. */
5898 scope
= parser
->scope
;
5899 object_scope
= parser
->object_scope
;
5900 qualifying_scope
= parser
->qualifying_scope
;
5902 /* Check for invalid scopes. */
5903 if (scope
== error_mark_node
)
5905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5906 cp_lexer_consume_token (parser
->lexer
);
5907 return error_mark_node
;
5909 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5911 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5912 error_at (token
->location
,
5913 "scope %qT before %<~%> is not a class-name",
5915 cp_parser_simulate_error (parser
);
5916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5917 cp_lexer_consume_token (parser
->lexer
);
5918 return error_mark_node
;
5920 gcc_assert (!scope
|| TYPE_P (scope
));
5922 /* If the name is of the form "X::~X" it's OK even if X is a
5924 token
= cp_lexer_peek_token (parser
->lexer
);
5926 && token
->type
== CPP_NAME
5927 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5929 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5930 || (CLASS_TYPE_P (scope
)
5931 && constructor_name_p (token
->u
.value
, scope
))))
5933 cp_lexer_consume_token (parser
->lexer
);
5934 return build_nt (BIT_NOT_EXPR
, scope
);
5937 /* ~auto means the destructor of whatever the object is. */
5938 if (cp_parser_is_keyword (token
, RID_AUTO
))
5940 if (cxx_dialect
< cxx14
)
5941 pedwarn (input_location
, 0,
5942 "%<~auto%> only available with "
5943 "-std=c++14 or -std=gnu++14");
5944 cp_lexer_consume_token (parser
->lexer
);
5945 return build_nt (BIT_NOT_EXPR
, make_auto ());
5948 /* If there was an explicit qualification (S::~T), first look
5949 in the scope given by the qualification (i.e., S).
5951 Note: in the calls to cp_parser_class_name below we pass
5952 typename_type so that lookup finds the injected-class-name
5953 rather than the constructor. */
5955 type_decl
= NULL_TREE
;
5958 cp_parser_parse_tentatively (parser
);
5959 type_decl
= cp_parser_class_name (parser
,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5966 if (cp_parser_parse_definitely (parser
))
5969 /* In "N::S::~S", look in "N" as well. */
5970 if (!done
&& scope
&& qualifying_scope
)
5972 cp_parser_parse_tentatively (parser
);
5973 parser
->scope
= qualifying_scope
;
5974 parser
->object_scope
= NULL_TREE
;
5975 parser
->qualifying_scope
= NULL_TREE
;
5977 = cp_parser_class_name (parser
,
5978 /*typename_keyword_p=*/false,
5979 /*template_keyword_p=*/false,
5981 /*check_dependency=*/false,
5982 /*class_head_p=*/false,
5984 if (cp_parser_parse_definitely (parser
))
5987 /* In "p->S::~T", look in the scope given by "*p" as well. */
5988 else if (!done
&& object_scope
)
5990 cp_parser_parse_tentatively (parser
);
5991 parser
->scope
= object_scope
;
5992 parser
->object_scope
= NULL_TREE
;
5993 parser
->qualifying_scope
= NULL_TREE
;
5995 = cp_parser_class_name (parser
,
5996 /*typename_keyword_p=*/false,
5997 /*template_keyword_p=*/false,
5999 /*check_dependency=*/false,
6000 /*class_head_p=*/false,
6002 if (cp_parser_parse_definitely (parser
))
6005 /* Look in the surrounding context. */
6008 parser
->scope
= NULL_TREE
;
6009 parser
->object_scope
= NULL_TREE
;
6010 parser
->qualifying_scope
= NULL_TREE
;
6011 if (processing_template_decl
)
6012 cp_parser_parse_tentatively (parser
);
6014 = cp_parser_class_name (parser
,
6015 /*typename_keyword_p=*/false,
6016 /*template_keyword_p=*/false,
6018 /*check_dependency=*/false,
6019 /*class_head_p=*/false,
6021 if (processing_template_decl
6022 && ! cp_parser_parse_definitely (parser
))
6024 /* We couldn't find a type with this name. If we're parsing
6025 tentatively, fail and try something else. */
6026 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6028 cp_parser_simulate_error (parser
);
6029 return error_mark_node
;
6031 /* Otherwise, accept it and check for a match at instantiation
6033 type_decl
= cp_parser_identifier (parser
);
6034 if (type_decl
!= error_mark_node
)
6035 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
6039 /* If an error occurred, assume that the name of the
6040 destructor is the same as the name of the qualifying
6041 class. That allows us to keep parsing after running
6042 into ill-formed destructor names. */
6043 if (type_decl
== error_mark_node
&& scope
)
6044 return build_nt (BIT_NOT_EXPR
, scope
);
6045 else if (type_decl
== error_mark_node
)
6046 return error_mark_node
;
6048 /* Check that destructor name and scope match. */
6049 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6052 error_at (token
->location
,
6053 "declaration of %<~%T%> as member of %qT",
6055 cp_parser_simulate_error (parser
);
6056 return error_mark_node
;
6061 A typedef-name that names a class shall not be used as the
6062 identifier in the declarator for a destructor declaration. */
6064 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6065 && !DECL_SELF_REFERENCE_P (type_decl
)
6066 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6067 error_at (token
->location
,
6068 "typedef-name %qD used as destructor declarator",
6071 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6075 if (token
->keyword
== RID_OPERATOR
)
6079 /* This could be a template-id, so we try that first. */
6080 cp_parser_parse_tentatively (parser
);
6081 /* Try a template-id. */
6082 id
= cp_parser_template_id (parser
, template_keyword_p
,
6083 /*check_dependency_p=*/true,
6086 /* If that worked, we're done. */
6087 if (cp_parser_parse_definitely (parser
))
6089 /* We still don't know whether we're looking at an
6090 operator-function-id or a conversion-function-id. */
6091 cp_parser_parse_tentatively (parser
);
6092 /* Try an operator-function-id. */
6093 id
= cp_parser_operator_function_id (parser
);
6094 /* If that didn't work, try a conversion-function-id. */
6095 if (!cp_parser_parse_definitely (parser
))
6096 id
= cp_parser_conversion_function_id (parser
);
6105 cp_parser_error (parser
, "expected unqualified-id");
6106 return error_mark_node
;
6110 /* Parse an (optional) nested-name-specifier.
6112 nested-name-specifier: [C++98]
6113 class-or-namespace-name :: nested-name-specifier [opt]
6114 class-or-namespace-name :: template nested-name-specifier [opt]
6116 nested-name-specifier: [C++0x]
6119 nested-name-specifier identifier ::
6120 nested-name-specifier template [opt] simple-template-id ::
6122 PARSER->SCOPE should be set appropriately before this function is
6123 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6124 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6127 Sets PARSER->SCOPE to the class (TYPE) or namespace
6128 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6129 it unchanged if there is no nested-name-specifier. Returns the new
6130 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6132 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6133 part of a declaration and/or decl-specifier. */
6136 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6137 bool typename_keyword_p
,
6138 bool check_dependency_p
,
6140 bool is_declaration
,
6141 bool template_keyword_p
/* = false */)
6143 bool success
= false;
6144 cp_token_position start
= 0;
6147 /* Remember where the nested-name-specifier starts. */
6148 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6150 start
= cp_lexer_token_position (parser
->lexer
, false);
6151 push_deferring_access_checks (dk_deferred
);
6158 tree saved_qualifying_scope
;
6160 /* Spot cases that cannot be the beginning of a
6161 nested-name-specifier. */
6162 token
= cp_lexer_peek_token (parser
->lexer
);
6164 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6165 the already parsed nested-name-specifier. */
6166 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6168 /* Grab the nested-name-specifier and continue the loop. */
6169 cp_parser_pre_parsed_nested_name_specifier (parser
);
6170 /* If we originally encountered this nested-name-specifier
6171 with IS_DECLARATION set to false, we will not have
6172 resolved TYPENAME_TYPEs, so we must do so here. */
6174 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6176 new_scope
= resolve_typename_type (parser
->scope
,
6177 /*only_current_p=*/false);
6178 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6179 parser
->scope
= new_scope
;
6185 /* Spot cases that cannot be the beginning of a
6186 nested-name-specifier. On the second and subsequent times
6187 through the loop, we look for the `template' keyword. */
6188 if (success
&& token
->keyword
== RID_TEMPLATE
)
6190 /* A template-id can start a nested-name-specifier. */
6191 else if (token
->type
== CPP_TEMPLATE_ID
)
6193 /* DR 743: decltype can be used in a nested-name-specifier. */
6194 else if (token_is_decltype (token
))
6198 /* If the next token is not an identifier, then it is
6199 definitely not a type-name or namespace-name. */
6200 if (token
->type
!= CPP_NAME
)
6202 /* If the following token is neither a `<' (to begin a
6203 template-id), nor a `::', then we are not looking at a
6204 nested-name-specifier. */
6205 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6207 if (token
->type
== CPP_COLON
6208 && parser
->colon_corrects_to_scope_p
6209 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6211 gcc_rich_location
richloc (token
->location
);
6212 richloc
.add_fixit_replace ("::");
6214 "found %<:%> in nested-name-specifier, "
6216 token
->type
= CPP_SCOPE
;
6219 if (token
->type
!= CPP_SCOPE
6220 && !cp_parser_nth_token_starts_template_argument_list_p
6225 /* The nested-name-specifier is optional, so we parse
6227 cp_parser_parse_tentatively (parser
);
6229 /* Look for the optional `template' keyword, if this isn't the
6230 first time through the loop. */
6232 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6234 /* Save the old scope since the name lookup we are about to do
6235 might destroy it. */
6236 old_scope
= parser
->scope
;
6237 saved_qualifying_scope
= parser
->qualifying_scope
;
6238 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6239 look up names in "X<T>::I" in order to determine that "Y" is
6240 a template. So, if we have a typename at this point, we make
6241 an effort to look through it. */
6243 && !typename_keyword_p
6245 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6246 parser
->scope
= resolve_typename_type (parser
->scope
,
6247 /*only_current_p=*/false);
6248 /* Parse the qualifying entity. */
6250 = cp_parser_qualifying_entity (parser
,
6256 /* Look for the `::' token. */
6257 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6259 /* If we found what we wanted, we keep going; otherwise, we're
6261 if (!cp_parser_parse_definitely (parser
))
6263 bool error_p
= false;
6265 /* Restore the OLD_SCOPE since it was valid before the
6266 failed attempt at finding the last
6267 class-or-namespace-name. */
6268 parser
->scope
= old_scope
;
6269 parser
->qualifying_scope
= saved_qualifying_scope
;
6271 /* If the next token is a decltype, and the one after that is a
6272 `::', then the decltype has failed to resolve to a class or
6273 enumeration type. Give this error even when parsing
6274 tentatively since it can't possibly be valid--and we're going
6275 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6276 won't get another chance.*/
6277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6278 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6281 token
= cp_lexer_consume_token (parser
->lexer
);
6282 error_at (token
->location
, "decltype evaluates to %qT, "
6283 "which is not a class or enumeration type",
6284 token
->u
.tree_check_value
->value
);
6285 parser
->scope
= error_mark_node
;
6289 cp_lexer_consume_token (parser
->lexer
);
6292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6293 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6295 /* If we have a non-type template-id followed by ::, it can't
6296 possibly be valid. */
6297 token
= cp_lexer_peek_token (parser
->lexer
);
6298 tree tid
= token
->u
.tree_check_value
->value
;
6299 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6300 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6302 tree tmpl
= NULL_TREE
;
6303 if (is_overloaded_fn (tid
))
6305 tree fns
= get_fns (tid
);
6306 if (OVL_SINGLE_P (fns
))
6307 tmpl
= OVL_FIRST (fns
);
6308 error_at (token
->location
, "function template-id %qD "
6309 "in nested-name-specifier", tid
);
6313 /* Variable template. */
6314 tmpl
= TREE_OPERAND (tid
, 0);
6315 gcc_assert (variable_template_p (tmpl
));
6316 error_at (token
->location
, "variable template-id %qD "
6317 "in nested-name-specifier", tid
);
6320 inform (DECL_SOURCE_LOCATION (tmpl
),
6321 "%qD declared here", tmpl
);
6323 parser
->scope
= error_mark_node
;
6327 cp_lexer_consume_token (parser
->lexer
);
6328 cp_lexer_consume_token (parser
->lexer
);
6332 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6334 /* If the next token is an identifier, and the one after
6335 that is a `::', then any valid interpretation would have
6336 found a class-or-namespace-name. */
6337 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6338 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6340 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6343 token
= cp_lexer_consume_token (parser
->lexer
);
6346 if (!token
->error_reported
)
6349 tree ambiguous_decls
;
6351 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6353 /*is_template=*/false,
6354 /*is_namespace=*/false,
6355 /*check_dependency=*/true,
6358 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6359 error_at (token
->location
,
6360 "%qD used without template arguments",
6362 else if (ambiguous_decls
)
6364 // cp_parser_lookup_name has the same diagnostic,
6365 // thus make sure to emit it at most once.
6366 if (cp_parser_uncommitted_to_tentative_parse_p
6369 error_at (token
->location
,
6370 "reference to %qD is ambiguous",
6372 print_candidates (ambiguous_decls
);
6374 decl
= error_mark_node
;
6378 if (cxx_dialect
!= cxx98
)
6379 cp_parser_name_lookup_error
6380 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6383 cp_parser_name_lookup_error
6384 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6388 parser
->scope
= error_mark_node
;
6390 /* Treat this as a successful nested-name-specifier
6395 If the name found is not a class-name (clause
6396 _class_) or namespace-name (_namespace.def_), the
6397 program is ill-formed. */
6400 cp_lexer_consume_token (parser
->lexer
);
6404 /* We've found one valid nested-name-specifier. */
6406 /* Name lookup always gives us a DECL. */
6407 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6408 new_scope
= TREE_TYPE (new_scope
);
6409 /* Uses of "template" must be followed by actual templates. */
6410 if (template_keyword_p
6411 && !(CLASS_TYPE_P (new_scope
)
6412 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6413 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6414 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6415 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6416 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6417 == TEMPLATE_ID_EXPR
)))
6418 permerror (input_location
, TYPE_P (new_scope
)
6419 ? G_("%qT is not a template")
6420 : G_("%qD is not a template"),
6422 /* If it is a class scope, try to complete it; we are about to
6423 be looking up names inside the class. */
6424 if (TYPE_P (new_scope
)
6425 /* Since checking types for dependency can be expensive,
6426 avoid doing it if the type is already complete. */
6427 && !COMPLETE_TYPE_P (new_scope
)
6428 /* Do not try to complete dependent types. */
6429 && !dependent_type_p (new_scope
))
6431 new_scope
= complete_type (new_scope
);
6432 /* If it is a typedef to current class, use the current
6433 class instead, as the typedef won't have any names inside
6435 if (!COMPLETE_TYPE_P (new_scope
)
6436 && currently_open_class (new_scope
))
6437 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6439 /* Make sure we look in the right scope the next time through
6441 parser
->scope
= new_scope
;
6444 /* If parsing tentatively, replace the sequence of tokens that makes
6445 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6446 token. That way, should we re-parse the token stream, we will
6447 not have to repeat the effort required to do the parse, nor will
6448 we issue duplicate error messages. */
6449 if (success
&& start
)
6453 token
= cp_lexer_token_at (parser
->lexer
, start
);
6454 /* Reset the contents of the START token. */
6455 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6456 /* Retrieve any deferred checks. Do not pop this access checks yet
6457 so the memory will not be reclaimed during token replacing below. */
6458 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6459 token
->u
.tree_check_value
->value
= parser
->scope
;
6460 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6461 token
->u
.tree_check_value
->qualifying_scope
=
6462 parser
->qualifying_scope
;
6463 token
->keyword
= RID_MAX
;
6465 /* Purge all subsequent tokens. */
6466 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6470 pop_to_parent_deferring_access_checks ();
6472 return success
? parser
->scope
: NULL_TREE
;
6475 /* Parse a nested-name-specifier. See
6476 cp_parser_nested_name_specifier_opt for details. This function
6477 behaves identically, except that it will an issue an error if no
6478 nested-name-specifier is present. */
6481 cp_parser_nested_name_specifier (cp_parser
*parser
,
6482 bool typename_keyword_p
,
6483 bool check_dependency_p
,
6485 bool is_declaration
)
6489 /* Look for the nested-name-specifier. */
6490 scope
= cp_parser_nested_name_specifier_opt (parser
,
6495 /* If it was not present, issue an error message. */
6498 cp_parser_error (parser
, "expected nested-name-specifier");
6499 parser
->scope
= NULL_TREE
;
6505 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6506 this is either a class-name or a namespace-name (which corresponds
6507 to the class-or-namespace-name production in the grammar). For
6508 C++0x, it can also be a type-name that refers to an enumeration
6509 type or a simple-template-id.
6511 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6512 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6513 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6514 TYPE_P is TRUE iff the next name should be taken as a class-name,
6515 even the same name is declared to be another entity in the same
6518 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6519 specified by the class-or-namespace-name. If neither is found the
6520 ERROR_MARK_NODE is returned. */
6523 cp_parser_qualifying_entity (cp_parser
*parser
,
6524 bool typename_keyword_p
,
6525 bool template_keyword_p
,
6526 bool check_dependency_p
,
6528 bool is_declaration
)
6531 tree saved_qualifying_scope
;
6532 tree saved_object_scope
;
6535 bool successful_parse_p
;
6537 /* DR 743: decltype can appear in a nested-name-specifier. */
6538 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6540 scope
= cp_parser_decltype (parser
);
6541 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6542 && !MAYBE_CLASS_TYPE_P (scope
))
6544 cp_parser_simulate_error (parser
);
6545 return error_mark_node
;
6547 if (TYPE_NAME (scope
))
6548 scope
= TYPE_NAME (scope
);
6552 /* Before we try to parse the class-name, we must save away the
6553 current PARSER->SCOPE since cp_parser_class_name will destroy
6555 saved_scope
= parser
->scope
;
6556 saved_qualifying_scope
= parser
->qualifying_scope
;
6557 saved_object_scope
= parser
->object_scope
;
6558 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6559 there is no need to look for a namespace-name. */
6560 only_class_p
= template_keyword_p
6561 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6563 cp_parser_parse_tentatively (parser
);
6564 scope
= cp_parser_class_name (parser
,
6567 type_p
? class_type
: none_type
,
6569 /*class_head_p=*/false,
6571 /*enum_ok=*/cxx_dialect
> cxx98
);
6572 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6573 /* If that didn't work, try for a namespace-name. */
6574 if (!only_class_p
&& !successful_parse_p
)
6576 /* Restore the saved scope. */
6577 parser
->scope
= saved_scope
;
6578 parser
->qualifying_scope
= saved_qualifying_scope
;
6579 parser
->object_scope
= saved_object_scope
;
6580 /* If we are not looking at an identifier followed by the scope
6581 resolution operator, then this is not part of a
6582 nested-name-specifier. (Note that this function is only used
6583 to parse the components of a nested-name-specifier.) */
6584 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6585 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6586 return error_mark_node
;
6587 scope
= cp_parser_namespace_name (parser
);
6593 /* Return true if we are looking at a compound-literal, false otherwise. */
6596 cp_parser_compound_literal_p (cp_parser
*parser
)
6598 cp_lexer_save_tokens (parser
->lexer
);
6600 /* Skip tokens until the next token is a closing parenthesis.
6601 If we find the closing `)', and the next token is a `{', then
6602 we are looking at a compound-literal. */
6603 bool compound_literal_p
6604 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6605 /*consume_paren=*/true)
6606 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6608 /* Roll back the tokens we skipped. */
6609 cp_lexer_rollback_tokens (parser
->lexer
);
6611 return compound_literal_p
;
6614 /* Return true if EXPR is the integer constant zero or a complex constant
6615 of zero, without any folding, but ignoring location wrappers. */
6618 literal_integer_zerop (const_tree expr
)
6620 return (location_wrapper_p (expr
)
6621 && integer_zerop (TREE_OPERAND (expr
, 0)));
6624 /* Parse a postfix-expression.
6628 postfix-expression [ expression ]
6629 postfix-expression ( expression-list [opt] )
6630 simple-type-specifier ( expression-list [opt] )
6631 typename :: [opt] nested-name-specifier identifier
6632 ( expression-list [opt] )
6633 typename :: [opt] nested-name-specifier template [opt] template-id
6634 ( expression-list [opt] )
6635 postfix-expression . template [opt] id-expression
6636 postfix-expression -> template [opt] id-expression
6637 postfix-expression . pseudo-destructor-name
6638 postfix-expression -> pseudo-destructor-name
6639 postfix-expression ++
6640 postfix-expression --
6641 dynamic_cast < type-id > ( expression )
6642 static_cast < type-id > ( expression )
6643 reinterpret_cast < type-id > ( expression )
6644 const_cast < type-id > ( expression )
6645 typeid ( expression )
6651 ( type-id ) { initializer-list , [opt] }
6653 This extension is a GNU version of the C99 compound-literal
6654 construct. (The C99 grammar uses `type-name' instead of `type-id',
6655 but they are essentially the same concept.)
6657 If ADDRESS_P is true, the postfix expression is the operand of the
6658 `&' operator. CAST_P is true if this expression is the target of a
6661 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6662 class member access expressions [expr.ref].
6664 Returns a representation of the expression. */
6667 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6668 bool member_access_only_p
, bool decltype_p
,
6669 cp_id_kind
* pidk_return
)
6674 cp_id_kind idk
= CP_ID_KIND_NONE
;
6675 cp_expr postfix_expression
= NULL_TREE
;
6676 bool is_member_access
= false;
6678 /* Peek at the next token. */
6679 token
= cp_lexer_peek_token (parser
->lexer
);
6680 loc
= token
->location
;
6681 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6683 /* Some of the productions are determined by keywords. */
6684 keyword
= token
->keyword
;
6694 const char *saved_message
;
6695 bool saved_in_type_id_in_expr_p
;
6697 /* All of these can be handled in the same way from the point
6698 of view of parsing. Begin by consuming the token
6699 identifying the cast. */
6700 cp_lexer_consume_token (parser
->lexer
);
6702 /* New types cannot be defined in the cast. */
6703 saved_message
= parser
->type_definition_forbidden_message
;
6704 parser
->type_definition_forbidden_message
6705 = G_("types may not be defined in casts");
6707 /* Look for the opening `<'. */
6708 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6709 /* Parse the type to which we are casting. */
6710 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6711 parser
->in_type_id_in_expr_p
= true;
6712 type
= cp_parser_type_id (parser
);
6713 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6714 /* Look for the closing `>'. */
6715 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6716 /* Restore the old message. */
6717 parser
->type_definition_forbidden_message
= saved_message
;
6719 bool saved_greater_than_is_operator_p
6720 = parser
->greater_than_is_operator_p
;
6721 parser
->greater_than_is_operator_p
= true;
6723 /* And the expression which is being cast. */
6724 matching_parens parens
;
6725 parens
.require_open (parser
);
6726 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6727 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6729 location_t end_loc
= close_paren
?
6730 close_paren
->location
: UNKNOWN_LOCATION
;
6732 parser
->greater_than_is_operator_p
6733 = saved_greater_than_is_operator_p
;
6735 /* Only type conversions to integral or enumeration types
6736 can be used in constant-expressions. */
6737 if (!cast_valid_in_integral_constant_expression_p (type
)
6738 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6740 postfix_expression
= error_mark_node
;
6748 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6752 = build_static_cast (type
, expression
, tf_warning_or_error
);
6756 = build_reinterpret_cast (type
, expression
,
6757 tf_warning_or_error
);
6761 = build_const_cast (type
, expression
, tf_warning_or_error
);
6767 /* Construct a location e.g. :
6768 reinterpret_cast <int *> (expr)
6769 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6770 ranging from the start of the "*_cast" token to the final closing
6771 paren, with the caret at the start. */
6772 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6773 postfix_expression
.set_location (cp_cast_loc
);
6780 const char *saved_message
;
6781 bool saved_in_type_id_in_expr_p
;
6783 /* Consume the `typeid' token. */
6784 cp_lexer_consume_token (parser
->lexer
);
6785 /* Look for the `(' token. */
6786 matching_parens parens
;
6787 parens
.require_open (parser
);
6788 /* Types cannot be defined in a `typeid' expression. */
6789 saved_message
= parser
->type_definition_forbidden_message
;
6790 parser
->type_definition_forbidden_message
6791 = G_("types may not be defined in a %<typeid%> expression");
6792 /* We can't be sure yet whether we're looking at a type-id or an
6794 cp_parser_parse_tentatively (parser
);
6795 /* Try a type-id first. */
6796 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6797 parser
->in_type_id_in_expr_p
= true;
6798 type
= cp_parser_type_id (parser
);
6799 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6800 /* Look for the `)' token. Otherwise, we can't be sure that
6801 we're not looking at an expression: consider `typeid (int
6802 (3))', for example. */
6803 cp_token
*close_paren
= parens
.require_close (parser
);
6804 /* If all went well, simply lookup the type-id. */
6805 if (cp_parser_parse_definitely (parser
))
6806 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6807 /* Otherwise, fall back to the expression variant. */
6812 /* Look for an expression. */
6813 expression
= cp_parser_expression (parser
, & idk
);
6814 /* Compute its typeid. */
6815 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6816 /* Look for the `)' token. */
6817 close_paren
= parens
.require_close (parser
);
6819 /* Restore the saved message. */
6820 parser
->type_definition_forbidden_message
= saved_message
;
6821 /* `typeid' may not appear in an integral constant expression. */
6822 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6823 postfix_expression
= error_mark_node
;
6825 /* Construct a location e.g. :
6828 ranging from the start of the "typeid" token to the final closing
6829 paren, with the caret at the start. */
6832 location_t typeid_loc
6833 = make_location (start_loc
, start_loc
, close_paren
->location
);
6834 postfix_expression
.set_location (typeid_loc
);
6835 postfix_expression
.maybe_add_location_wrapper ();
6843 /* The syntax permitted here is the same permitted for an
6844 elaborated-type-specifier. */
6845 ++parser
->prevent_constrained_type_specifiers
;
6846 type
= cp_parser_elaborated_type_specifier (parser
,
6847 /*is_friend=*/false,
6848 /*is_declaration=*/false);
6849 --parser
->prevent_constrained_type_specifiers
;
6850 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6855 case RID_BUILTIN_SHUFFLE
:
6856 case RID_BUILTIN_LAUNDER
:
6858 vec
<tree
, va_gc
> *vec
;
6862 cp_lexer_consume_token (parser
->lexer
);
6863 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6864 /*cast_p=*/false, /*allow_expansion_p=*/true,
6865 /*non_constant_p=*/NULL
);
6868 postfix_expression
= error_mark_node
;
6872 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6878 if (vec
->length () == 1)
6880 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
6883 error_at (loc
, "wrong number of arguments to "
6884 "%<__builtin_addressof%>");
6885 postfix_expression
= error_mark_node
;
6889 case RID_BUILTIN_LAUNDER
:
6890 if (vec
->length () == 1)
6891 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
6892 tf_warning_or_error
);
6895 error_at (loc
, "wrong number of arguments to "
6896 "%<__builtin_launder%>");
6897 postfix_expression
= error_mark_node
;
6901 case RID_BUILTIN_SHUFFLE
:
6902 if (vec
->length () == 2)
6904 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
6905 (*vec
)[1], tf_warning_or_error
);
6906 else if (vec
->length () == 3)
6908 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
6909 (*vec
)[2], tf_warning_or_error
);
6912 error_at (loc
, "wrong number of arguments to "
6913 "%<__builtin_shuffle%>");
6914 postfix_expression
= error_mark_node
;
6928 /* If the next thing is a simple-type-specifier, we may be
6929 looking at a functional cast. We could also be looking at
6930 an id-expression. So, we try the functional cast, and if
6931 that doesn't work we fall back to the primary-expression. */
6932 cp_parser_parse_tentatively (parser
);
6933 /* Look for the simple-type-specifier. */
6934 ++parser
->prevent_constrained_type_specifiers
;
6935 type
= cp_parser_simple_type_specifier (parser
,
6936 /*decl_specs=*/NULL
,
6937 CP_PARSER_FLAGS_NONE
);
6938 --parser
->prevent_constrained_type_specifiers
;
6939 /* Parse the cast itself. */
6940 if (!cp_parser_error_occurred (parser
))
6942 = cp_parser_functional_cast (parser
, type
);
6943 /* If that worked, we're done. */
6944 if (cp_parser_parse_definitely (parser
))
6947 /* If the functional-cast didn't work out, try a
6948 compound-literal. */
6949 if (cp_parser_allow_gnu_extensions_p (parser
)
6950 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6952 cp_expr initializer
= NULL_TREE
;
6954 cp_parser_parse_tentatively (parser
);
6956 matching_parens parens
;
6957 parens
.consume_open (parser
);
6959 /* Avoid calling cp_parser_type_id pointlessly, see comment
6960 in cp_parser_cast_expression about c++/29234. */
6961 if (!cp_parser_compound_literal_p (parser
))
6962 cp_parser_simulate_error (parser
);
6965 /* Parse the type. */
6966 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6967 parser
->in_type_id_in_expr_p
= true;
6968 type
= cp_parser_type_id (parser
);
6969 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6970 parens
.require_close (parser
);
6973 /* If things aren't going well, there's no need to
6975 if (!cp_parser_error_occurred (parser
))
6977 bool non_constant_p
;
6978 /* Parse the brace-enclosed initializer list. */
6979 initializer
= cp_parser_braced_list (parser
,
6982 /* If that worked, we're definitely looking at a
6983 compound-literal expression. */
6984 if (cp_parser_parse_definitely (parser
))
6986 /* Warn the user that a compound literal is not
6987 allowed in standard C++. */
6988 pedwarn (input_location
, OPT_Wpedantic
,
6989 "ISO C++ forbids compound-literals");
6990 /* For simplicity, we disallow compound literals in
6991 constant-expressions. We could
6992 allow compound literals of integer type, whose
6993 initializer was a constant, in constant
6994 expressions. Permitting that usage, as a further
6995 extension, would not change the meaning of any
6996 currently accepted programs. (Of course, as
6997 compound literals are not part of ISO C++, the
6998 standard has nothing to say.) */
6999 if (cp_parser_non_integral_constant_expression (parser
,
7002 postfix_expression
= error_mark_node
;
7005 /* Form the representation of the compound-literal. */
7007 = finish_compound_literal (type
, initializer
,
7008 tf_warning_or_error
, fcl_c99
);
7009 postfix_expression
.set_location (initializer
.get_location ());
7014 /* It must be a primary-expression. */
7016 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7017 /*template_arg_p=*/false,
7024 /* Note that we don't need to worry about calling build_cplus_new on a
7025 class-valued CALL_EXPR in decltype when it isn't the end of the
7026 postfix-expression; unary_complex_lvalue will take care of that for
7029 /* Keep looping until the postfix-expression is complete. */
7032 if (idk
== CP_ID_KIND_UNQUALIFIED
7033 && identifier_p (postfix_expression
)
7034 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7035 /* It is not a Koenig lookup function call. */
7037 = unqualified_name_lookup_error (postfix_expression
);
7039 /* Peek at the next token. */
7040 token
= cp_lexer_peek_token (parser
->lexer
);
7042 switch (token
->type
)
7044 case CPP_OPEN_SQUARE
:
7045 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7047 cp_parser_error (parser
,
7048 "two consecutive %<[%> shall "
7049 "only introduce an attribute");
7050 return error_mark_node
;
7053 = cp_parser_postfix_open_square_expression (parser
,
7057 postfix_expression
.set_range (start_loc
,
7058 postfix_expression
.get_location ());
7060 idk
= CP_ID_KIND_NONE
;
7061 is_member_access
= false;
7064 case CPP_OPEN_PAREN
:
7065 /* postfix-expression ( expression-list [opt] ) */
7068 bool is_builtin_constant_p
;
7069 bool saved_integral_constant_expression_p
= false;
7070 bool saved_non_integral_constant_expression_p
= false;
7071 tsubst_flags_t complain
= complain_flags (decltype_p
);
7072 vec
<tree
, va_gc
> *args
;
7073 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7075 is_member_access
= false;
7077 is_builtin_constant_p
7078 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
7079 if (is_builtin_constant_p
)
7081 /* The whole point of __builtin_constant_p is to allow
7082 non-constant expressions to appear as arguments. */
7083 saved_integral_constant_expression_p
7084 = parser
->integral_constant_expression_p
;
7085 saved_non_integral_constant_expression_p
7086 = parser
->non_integral_constant_expression_p
;
7087 parser
->integral_constant_expression_p
= false;
7089 args
= (cp_parser_parenthesized_expression_list
7091 /*cast_p=*/false, /*allow_expansion_p=*/true,
7092 /*non_constant_p=*/NULL
,
7093 /*close_paren_loc=*/&close_paren_loc
,
7094 /*wrap_locations_p=*/true));
7095 if (is_builtin_constant_p
)
7097 parser
->integral_constant_expression_p
7098 = saved_integral_constant_expression_p
;
7099 parser
->non_integral_constant_expression_p
7100 = saved_non_integral_constant_expression_p
;
7105 postfix_expression
= error_mark_node
;
7109 /* Function calls are not permitted in
7110 constant-expressions. */
7111 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7112 && cp_parser_non_integral_constant_expression (parser
,
7115 postfix_expression
= error_mark_node
;
7116 release_tree_vector (args
);
7121 if (idk
== CP_ID_KIND_UNQUALIFIED
7122 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7124 if (identifier_p (postfix_expression
))
7126 if (!args
->is_empty ())
7129 if (!any_type_dependent_arguments_p (args
))
7131 = perform_koenig_lookup (postfix_expression
, args
,
7136 = unqualified_fn_lookup_error (postfix_expression
);
7138 /* We do not perform argument-dependent lookup if
7139 normal lookup finds a non-function, in accordance
7140 with the expected resolution of DR 218. */
7141 else if (!args
->is_empty ()
7142 && is_overloaded_fn (postfix_expression
))
7144 tree fn
= get_first_fn (postfix_expression
);
7145 fn
= STRIP_TEMPLATE (fn
);
7147 /* Do not do argument dependent lookup if regular
7148 lookup finds a member function or a block-scope
7149 function declaration. [basic.lookup.argdep]/3 */
7150 if (!DECL_FUNCTION_MEMBER_P (fn
)
7151 && !DECL_LOCAL_FUNCTION_P (fn
))
7154 if (!any_type_dependent_arguments_p (args
))
7156 = perform_koenig_lookup (postfix_expression
, args
,
7162 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7164 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7165 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7167 if (processing_template_decl
7168 && (type_dependent_object_expression_p (instance
)
7169 || (!BASELINK_P (fn
)
7170 && TREE_CODE (fn
) != FIELD_DECL
)
7171 || type_dependent_expression_p (fn
)
7172 || any_type_dependent_arguments_p (args
)))
7174 maybe_generic_this_capture (instance
, fn
);
7176 = build_min_nt_call_vec (postfix_expression
, args
);
7177 release_tree_vector (args
);
7181 if (BASELINK_P (fn
))
7184 = (build_new_method_call
7185 (instance
, fn
, &args
, NULL_TREE
,
7186 (idk
== CP_ID_KIND_QUALIFIED
7187 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7194 = finish_call_expr (postfix_expression
, &args
,
7195 /*disallow_virtual=*/false,
7199 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7200 || TREE_CODE (postfix_expression
) == MEMBER_REF
7201 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7202 postfix_expression
= (build_offset_ref_call_from_tree
7203 (postfix_expression
, &args
,
7205 else if (idk
== CP_ID_KIND_QUALIFIED
)
7206 /* A call to a static class member, or a namespace-scope
7209 = finish_call_expr (postfix_expression
, &args
,
7210 /*disallow_virtual=*/true,
7214 /* All other function calls. */
7216 = finish_call_expr (postfix_expression
, &args
,
7217 /*disallow_virtual=*/false,
7221 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7223 location_t combined_loc
= make_location (token
->location
,
7226 postfix_expression
.set_location (combined_loc
);
7229 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7230 idk
= CP_ID_KIND_NONE
;
7232 release_tree_vector (args
);
7238 /* postfix-expression . template [opt] id-expression
7239 postfix-expression . pseudo-destructor-name
7240 postfix-expression -> template [opt] id-expression
7241 postfix-expression -> pseudo-destructor-name */
7243 /* Consume the `.' or `->' operator. */
7244 cp_lexer_consume_token (parser
->lexer
);
7247 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7251 is_member_access
= true;
7255 /* postfix-expression ++ */
7256 /* Consume the `++' token. */
7257 cp_lexer_consume_token (parser
->lexer
);
7258 /* Generate a representation for the complete expression. */
7260 = finish_increment_expr (postfix_expression
,
7261 POSTINCREMENT_EXPR
);
7262 /* Increments may not appear in constant-expressions. */
7263 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7264 postfix_expression
= error_mark_node
;
7265 idk
= CP_ID_KIND_NONE
;
7266 is_member_access
= false;
7269 case CPP_MINUS_MINUS
:
7270 /* postfix-expression -- */
7271 /* Consume the `--' token. */
7272 cp_lexer_consume_token (parser
->lexer
);
7273 /* Generate a representation for the complete expression. */
7275 = finish_increment_expr (postfix_expression
,
7276 POSTDECREMENT_EXPR
);
7277 /* Decrements may not appear in constant-expressions. */
7278 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7279 postfix_expression
= error_mark_node
;
7280 idk
= CP_ID_KIND_NONE
;
7281 is_member_access
= false;
7285 if (pidk_return
!= NULL
)
7286 * pidk_return
= idk
;
7287 if (member_access_only_p
)
7288 return is_member_access
7289 ? postfix_expression
7290 : cp_expr (error_mark_node
);
7292 return postfix_expression
;
7296 /* We should never get here. */
7298 return error_mark_node
;
7301 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7302 by cp_parser_builtin_offsetof. We're looking for
7304 postfix-expression [ expression ]
7305 postfix-expression [ braced-init-list ] (C++11)
7307 FOR_OFFSETOF is set if we're being called in that context, which
7308 changes how we deal with integer constant expressions. */
7311 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7312 tree postfix_expression
,
7316 tree index
= NULL_TREE
;
7317 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7318 bool saved_greater_than_is_operator_p
;
7320 /* Consume the `[' token. */
7321 cp_lexer_consume_token (parser
->lexer
);
7323 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7324 parser
->greater_than_is_operator_p
= true;
7326 /* Parse the index expression. */
7327 /* ??? For offsetof, there is a question of what to allow here. If
7328 offsetof is not being used in an integral constant expression context,
7329 then we *could* get the right answer by computing the value at runtime.
7330 If we are in an integral constant expression context, then we might
7331 could accept any constant expression; hard to say without analysis.
7332 Rather than open the barn door too wide right away, allow only integer
7333 constant expressions here. */
7335 index
= cp_parser_constant_expression (parser
);
7338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7340 bool expr_nonconst_p
;
7341 cp_lexer_set_source_position (parser
->lexer
);
7342 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7343 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7346 index
= cp_parser_expression (parser
);
7349 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7351 /* Look for the closing `]'. */
7352 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7354 /* Build the ARRAY_REF. */
7355 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7358 /* When not doing offsetof, array references are not permitted in
7359 constant-expressions. */
7361 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7362 postfix_expression
= error_mark_node
;
7364 return postfix_expression
;
7367 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7368 dereference of incomplete type, returns true if error_mark_node should
7369 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7370 and *DEPENDENT_P. */
7373 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7376 /* In a template, be permissive by treating an object expression
7377 of incomplete type as dependent (after a pedwarn). */
7378 diagnostic_t kind
= (processing_template_decl
7379 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7381 switch (TREE_CODE (*postfix_expression
))
7384 case REINTERPRET_CAST_EXPR
:
7385 case CONST_CAST_EXPR
:
7386 case STATIC_CAST_EXPR
:
7387 case DYNAMIC_CAST_EXPR
:
7388 case IMPLICIT_CONV_EXPR
:
7389 case VIEW_CONVERT_EXPR
:
7390 case NON_LVALUE_EXPR
:
7394 /* Don't emit any diagnostic for OVERLOADs. */
7398 /* Avoid clobbering e.g. DECLs. */
7399 if (!EXPR_P (*postfix_expression
))
7404 if (kind
== DK_IGNORED
)
7407 location_t exploc
= location_of (*postfix_expression
);
7408 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7409 if (!MAYBE_CLASS_TYPE_P (*scope
))
7411 if (kind
== DK_ERROR
)
7412 *scope
= *postfix_expression
= error_mark_node
;
7413 else if (processing_template_decl
)
7415 *dependent_p
= true;
7416 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7421 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7422 by cp_parser_builtin_offsetof. We're looking for
7424 postfix-expression . template [opt] id-expression
7425 postfix-expression . pseudo-destructor-name
7426 postfix-expression -> template [opt] id-expression
7427 postfix-expression -> pseudo-destructor-name
7429 FOR_OFFSETOF is set if we're being called in that context. That sorta
7430 limits what of the above we'll actually accept, but nevermind.
7431 TOKEN_TYPE is the "." or "->" token, which will already have been
7432 removed from the stream. */
7435 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7436 enum cpp_ttype token_type
,
7437 cp_expr postfix_expression
,
7438 bool for_offsetof
, cp_id_kind
*idk
,
7439 location_t location
)
7443 bool pseudo_destructor_p
;
7444 tree scope
= NULL_TREE
;
7445 location_t start_loc
= postfix_expression
.get_start ();
7447 /* If this is a `->' operator, dereference the pointer. */
7448 if (token_type
== CPP_DEREF
)
7449 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7450 tf_warning_or_error
);
7451 /* Check to see whether or not the expression is type-dependent and
7452 not the current instantiation. */
7453 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7454 /* The identifier following the `->' or `.' is not qualified. */
7455 parser
->scope
= NULL_TREE
;
7456 parser
->qualifying_scope
= NULL_TREE
;
7457 parser
->object_scope
= NULL_TREE
;
7458 *idk
= CP_ID_KIND_NONE
;
7460 /* Enter the scope corresponding to the type of the object
7461 given by the POSTFIX_EXPRESSION. */
7464 scope
= TREE_TYPE (postfix_expression
);
7465 /* According to the standard, no expression should ever have
7466 reference type. Unfortunately, we do not currently match
7467 the standard in this respect in that our internal representation
7468 of an expression may have reference type even when the standard
7469 says it does not. Therefore, we have to manually obtain the
7470 underlying type here. */
7471 scope
= non_reference (scope
);
7472 /* The type of the POSTFIX_EXPRESSION must be complete. */
7473 /* Unlike the object expression in other contexts, *this is not
7474 required to be of complete type for purposes of class member
7475 access (5.2.5) outside the member function body. */
7476 if (postfix_expression
!= current_class_ref
7477 && scope
!= error_mark_node
7478 && !currently_open_class (scope
))
7480 scope
= complete_type (scope
);
7481 if (!COMPLETE_TYPE_P (scope
)
7482 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7484 return error_mark_node
;
7489 /* Let the name lookup machinery know that we are processing a
7490 class member access expression. */
7491 parser
->context
->object_type
= scope
;
7492 /* If something went wrong, we want to be able to discern that case,
7493 as opposed to the case where there was no SCOPE due to the type
7494 of expression being dependent. */
7496 scope
= error_mark_node
;
7497 /* If the SCOPE was erroneous, make the various semantic analysis
7498 functions exit quickly -- and without issuing additional error
7500 if (scope
== error_mark_node
)
7501 postfix_expression
= error_mark_node
;
7506 /* Tell cp_parser_lookup_name that there was an object, even though it's
7508 parser
->context
->object_type
= unknown_type_node
;
7510 /* Assume this expression is not a pseudo-destructor access. */
7511 pseudo_destructor_p
= false;
7513 /* If the SCOPE is a scalar type, then, if this is a valid program,
7514 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7515 is type dependent, it can be pseudo-destructor-name or something else.
7516 Try to parse it as pseudo-destructor-name first. */
7517 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7522 cp_parser_parse_tentatively (parser
);
7523 /* Parse the pseudo-destructor-name. */
7525 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7528 && (cp_parser_error_occurred (parser
)
7529 || !SCALAR_TYPE_P (type
)))
7530 cp_parser_abort_tentative_parse (parser
);
7531 else if (cp_parser_parse_definitely (parser
))
7533 pseudo_destructor_p
= true;
7535 = finish_pseudo_destructor_expr (postfix_expression
,
7540 if (!pseudo_destructor_p
)
7542 /* If the SCOPE is not a scalar type, we are looking at an
7543 ordinary class member access expression, rather than a
7544 pseudo-destructor-name. */
7546 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7547 /* Parse the id-expression. */
7548 name
= (cp_parser_id_expression
7550 cp_parser_optional_template_keyword (parser
),
7551 /*check_dependency_p=*/true,
7553 /*declarator_p=*/false,
7554 /*optional_p=*/false));
7555 /* In general, build a SCOPE_REF if the member name is qualified.
7556 However, if the name was not dependent and has already been
7557 resolved; there is no need to build the SCOPE_REF. For example;
7559 struct X { void f(); };
7560 template <typename T> void f(T* t) { t->X::f(); }
7562 Even though "t" is dependent, "X::f" is not and has been resolved
7563 to a BASELINK; there is no need to include scope information. */
7565 /* But we do need to remember that there was an explicit scope for
7566 virtual function calls. */
7568 *idk
= CP_ID_KIND_QUALIFIED
;
7570 /* If the name is a template-id that names a type, we will get a
7571 TYPE_DECL here. That is invalid code. */
7572 if (TREE_CODE (name
) == TYPE_DECL
)
7574 error_at (token
->location
, "invalid use of %qD", name
);
7575 postfix_expression
= error_mark_node
;
7579 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7581 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7583 error_at (token
->location
, "%<%D::%D%> is not a class member",
7584 parser
->scope
, name
);
7585 postfix_expression
= error_mark_node
;
7588 name
= build_qualified_name (/*type=*/NULL_TREE
,
7592 parser
->scope
= NULL_TREE
;
7593 parser
->qualifying_scope
= NULL_TREE
;
7594 parser
->object_scope
= NULL_TREE
;
7596 if (parser
->scope
&& name
&& BASELINK_P (name
))
7597 adjust_result_of_qualified_name_lookup
7598 (name
, parser
->scope
, scope
);
7600 = finish_class_member_access_expr (postfix_expression
, name
,
7602 tf_warning_or_error
);
7603 /* Build a location e.g.:
7606 where the caret is at the deref token, ranging from
7607 the start of postfix_expression to the end of the access expr. */
7609 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7610 location_t combined_loc
7611 = make_location (input_location
, start_loc
, end_loc
);
7612 protected_set_expr_location (postfix_expression
, combined_loc
);
7616 /* We no longer need to look up names in the scope of the object on
7617 the left-hand side of the `.' or `->' operator. */
7618 parser
->context
->object_type
= NULL_TREE
;
7620 /* Outside of offsetof, these operators may not appear in
7621 constant-expressions. */
7623 && (cp_parser_non_integral_constant_expression
7624 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7625 postfix_expression
= error_mark_node
;
7627 return postfix_expression
;
7630 /* Parse a parenthesized expression-list.
7633 assignment-expression
7634 expression-list, assignment-expression
7639 identifier, expression-list
7641 CAST_P is true if this expression is the target of a cast.
7643 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7646 WRAP_LOCATIONS_P is true if expressions within this list for which
7647 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7648 their source locations.
7650 Returns a vector of trees. Each element is a representation of an
7651 assignment-expression. NULL is returned if the ( and or ) are
7652 missing. An empty, but allocated, vector is returned on no
7653 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7654 if we are parsing an attribute list for an attribute that wants a
7655 plain identifier argument, normal_attr for an attribute that wants
7656 an expression, or non_attr if we aren't parsing an attribute list. If
7657 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7658 not all of the expressions in the list were constant.
7659 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7660 will be written to with the location of the closing parenthesis. If
7661 an error occurs, it may or may not be written to. */
7663 static vec
<tree
, va_gc
> *
7664 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7665 int is_attribute_list
,
7667 bool allow_expansion_p
,
7668 bool *non_constant_p
,
7669 location_t
*close_paren_loc
,
7670 bool wrap_locations_p
)
7672 vec
<tree
, va_gc
> *expression_list
;
7673 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7674 tree identifier
= NULL_TREE
;
7675 bool saved_greater_than_is_operator_p
;
7677 /* Assume all the expressions will be constant. */
7679 *non_constant_p
= false;
7681 matching_parens parens
;
7682 if (!parens
.require_open (parser
))
7685 expression_list
= make_tree_vector ();
7687 /* Within a parenthesized expression, a `>' token is always
7688 the greater-than operator. */
7689 saved_greater_than_is_operator_p
7690 = parser
->greater_than_is_operator_p
;
7691 parser
->greater_than_is_operator_p
= true;
7693 cp_expr
expr (NULL_TREE
);
7695 /* Consume expressions until there are no more. */
7696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7699 /* At the beginning of attribute lists, check to see if the
7700 next token is an identifier. */
7701 if (is_attribute_list
== id_attr
7702 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7706 /* Consume the identifier. */
7707 token
= cp_lexer_consume_token (parser
->lexer
);
7708 /* Save the identifier. */
7709 identifier
= token
->u
.value
;
7713 bool expr_non_constant_p
;
7715 /* Parse the next assignment-expression. */
7716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7718 /* A braced-init-list. */
7719 cp_lexer_set_source_position (parser
->lexer
);
7720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7721 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7722 if (non_constant_p
&& expr_non_constant_p
)
7723 *non_constant_p
= true;
7725 else if (non_constant_p
)
7727 expr
= (cp_parser_constant_expression
7728 (parser
, /*allow_non_constant_p=*/true,
7729 &expr_non_constant_p
));
7730 if (expr_non_constant_p
)
7731 *non_constant_p
= true;
7734 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7738 expr
= instantiate_non_dependent_expr (expr
);
7740 /* If we have an ellipsis, then this is an expression
7742 if (allow_expansion_p
7743 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7745 /* Consume the `...'. */
7746 cp_lexer_consume_token (parser
->lexer
);
7748 /* Build the argument pack. */
7749 expr
= make_pack_expansion (expr
);
7752 if (wrap_locations_p
)
7753 expr
.maybe_add_location_wrapper ();
7755 /* Add it to the list. We add error_mark_node
7756 expressions to the list, so that we can still tell if
7757 the correct form for a parenthesized expression-list
7758 is found. That gives better errors. */
7759 vec_safe_push (expression_list
, expr
.get_value ());
7761 if (expr
== error_mark_node
)
7765 /* After the first item, attribute lists look the same as
7766 expression lists. */
7767 is_attribute_list
= non_attr
;
7770 /* If the next token isn't a `,', then we are done. */
7771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7774 /* Otherwise, consume the `,' and keep going. */
7775 cp_lexer_consume_token (parser
->lexer
);
7778 if (close_paren_loc
)
7779 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7781 if (!parens
.require_close (parser
))
7786 /* We try and resync to an unnested comma, as that will give the
7787 user better diagnostics. */
7788 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7789 /*recovering=*/true,
7791 /*consume_paren=*/true);
7796 parser
->greater_than_is_operator_p
7797 = saved_greater_than_is_operator_p
;
7802 parser
->greater_than_is_operator_p
7803 = saved_greater_than_is_operator_p
;
7806 vec_safe_insert (expression_list
, 0, identifier
);
7808 return expression_list
;
7811 /* Parse a pseudo-destructor-name.
7813 pseudo-destructor-name:
7814 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7815 :: [opt] nested-name-specifier template template-id :: ~ type-name
7816 :: [opt] nested-name-specifier [opt] ~ type-name
7818 If either of the first two productions is used, sets *SCOPE to the
7819 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7820 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7821 or ERROR_MARK_NODE if the parse fails. */
7824 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7829 bool nested_name_specifier_p
;
7832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7833 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7834 && !type_dependent_expression_p (object
))
7836 if (cxx_dialect
< cxx14
)
7837 pedwarn (input_location
, 0,
7838 "%<~auto%> only available with "
7839 "-std=c++14 or -std=gnu++14");
7840 cp_lexer_consume_token (parser
->lexer
);
7841 cp_lexer_consume_token (parser
->lexer
);
7843 *type
= TREE_TYPE (object
);
7847 /* Assume that things will not work out. */
7848 *type
= error_mark_node
;
7850 /* Look for the optional `::' operator. */
7851 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7852 /* Look for the optional nested-name-specifier. */
7853 nested_name_specifier_p
7854 = (cp_parser_nested_name_specifier_opt (parser
,
7855 /*typename_keyword_p=*/false,
7856 /*check_dependency_p=*/true,
7858 /*is_declaration=*/false)
7860 /* Now, if we saw a nested-name-specifier, we might be doing the
7861 second production. */
7862 if (nested_name_specifier_p
7863 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7865 /* Consume the `template' keyword. */
7866 cp_lexer_consume_token (parser
->lexer
);
7867 /* Parse the template-id. */
7868 cp_parser_template_id (parser
,
7869 /*template_keyword_p=*/true,
7870 /*check_dependency_p=*/false,
7872 /*is_declaration=*/true);
7873 /* Look for the `::' token. */
7874 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7876 /* If the next token is not a `~', then there might be some
7877 additional qualification. */
7878 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7880 /* At this point, we're looking for "type-name :: ~". The type-name
7881 must not be a class-name, since this is a pseudo-destructor. So,
7882 it must be either an enum-name, or a typedef-name -- both of which
7883 are just identifiers. So, we peek ahead to check that the "::"
7884 and "~" tokens are present; if they are not, then we can avoid
7885 calling type_name. */
7886 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7887 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7888 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7890 cp_parser_error (parser
, "non-scalar type");
7894 /* Look for the type-name. */
7895 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7896 if (*scope
== error_mark_node
)
7899 /* Look for the `::' token. */
7900 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7905 /* Look for the `~'. */
7906 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7908 /* Once we see the ~, this has to be a pseudo-destructor. */
7909 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7910 cp_parser_commit_to_topmost_tentative_parse (parser
);
7912 /* Look for the type-name again. We are not responsible for
7913 checking that it matches the first type-name. */
7914 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7917 /* Parse a unary-expression.
7923 unary-operator cast-expression
7924 sizeof unary-expression
7926 alignof ( type-id ) [C++0x]
7933 __extension__ cast-expression
7934 __alignof__ unary-expression
7935 __alignof__ ( type-id )
7936 alignof unary-expression [C++0x]
7937 __real__ cast-expression
7938 __imag__ cast-expression
7940 sizeof ( type-id ) { initializer-list , [opt] }
7941 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7942 __alignof__ ( type-id ) { initializer-list , [opt] }
7944 ADDRESS_P is true iff the unary-expression is appearing as the
7945 operand of the `&' operator. CAST_P is true if this expression is
7946 the target of a cast.
7948 Returns a representation of the expression. */
7951 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7952 bool address_p
, bool cast_p
, bool decltype_p
)
7955 enum tree_code unary_operator
;
7957 /* Peek at the next token. */
7958 token
= cp_lexer_peek_token (parser
->lexer
);
7959 /* Some keywords give away the kind of expression. */
7960 if (token
->type
== CPP_KEYWORD
)
7962 enum rid keyword
= token
->keyword
;
7971 location_t start_loc
= token
->location
;
7973 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7974 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
7976 /* Consume the token. */
7977 cp_lexer_consume_token (parser
->lexer
);
7978 /* Parse the operand. */
7979 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7981 if (TYPE_P (operand
))
7982 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
7986 /* ISO C++ defines alignof only with types, not with
7987 expressions. So pedwarn if alignof is used with a non-
7988 type expression. However, __alignof__ is ok. */
7990 pedwarn (token
->location
, OPT_Wpedantic
,
7991 "ISO C++ does not allow %<alignof%> "
7994 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7996 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7997 SIZEOF_EXPR with the original operand. */
7998 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8000 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8002 if (!processing_template_decl
&& TYPE_P (operand
))
8004 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8005 build1 (NOP_EXPR
, operand
,
8007 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8010 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8011 TREE_SIDE_EFFECTS (ret
) = 0;
8012 TREE_READONLY (ret
) = 1;
8016 /* Construct a location e.g. :
8019 with start == caret at the start of the "alignof"/"sizeof"
8020 token, with the endpoint at the final closing paren. */
8021 location_t finish_loc
8022 = cp_lexer_previous_token (parser
->lexer
)->location
;
8023 location_t compound_loc
8024 = make_location (start_loc
, start_loc
, finish_loc
);
8026 cp_expr
ret_expr (ret
);
8027 ret_expr
.set_location (compound_loc
);
8028 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8033 return cp_parser_new_expression (parser
);
8036 return cp_parser_delete_expression (parser
);
8040 /* The saved value of the PEDANTIC flag. */
8044 /* Save away the PEDANTIC flag. */
8045 cp_parser_extension_opt (parser
, &saved_pedantic
);
8046 /* Parse the cast-expression. */
8047 expr
= cp_parser_simple_cast_expression (parser
);
8048 /* Restore the PEDANTIC flag. */
8049 pedantic
= saved_pedantic
;
8059 /* Consume the `__real__' or `__imag__' token. */
8060 cp_lexer_consume_token (parser
->lexer
);
8061 /* Parse the cast-expression. */
8062 expression
= cp_parser_simple_cast_expression (parser
);
8063 /* Create the complete representation. */
8064 return build_x_unary_op (token
->location
,
8065 (keyword
== RID_REALPART
8066 ? REALPART_EXPR
: IMAGPART_EXPR
),
8068 tf_warning_or_error
);
8072 case RID_TRANSACTION_ATOMIC
:
8073 case RID_TRANSACTION_RELAXED
:
8074 return cp_parser_transaction_expression (parser
, keyword
);
8079 const char *saved_message
;
8080 bool saved_integral_constant_expression_p
;
8081 bool saved_non_integral_constant_expression_p
;
8082 bool saved_greater_than_is_operator_p
;
8084 location_t start_loc
= token
->location
;
8086 cp_lexer_consume_token (parser
->lexer
);
8087 matching_parens parens
;
8088 parens
.require_open (parser
);
8090 saved_message
= parser
->type_definition_forbidden_message
;
8091 parser
->type_definition_forbidden_message
8092 = G_("types may not be defined in %<noexcept%> expressions");
8094 saved_integral_constant_expression_p
8095 = parser
->integral_constant_expression_p
;
8096 saved_non_integral_constant_expression_p
8097 = parser
->non_integral_constant_expression_p
;
8098 parser
->integral_constant_expression_p
= false;
8100 saved_greater_than_is_operator_p
8101 = parser
->greater_than_is_operator_p
;
8102 parser
->greater_than_is_operator_p
= true;
8104 ++cp_unevaluated_operand
;
8105 ++c_inhibit_evaluation_warnings
;
8106 ++cp_noexcept_operand
;
8107 expr
= cp_parser_expression (parser
);
8108 --cp_noexcept_operand
;
8109 --c_inhibit_evaluation_warnings
;
8110 --cp_unevaluated_operand
;
8112 parser
->greater_than_is_operator_p
8113 = saved_greater_than_is_operator_p
;
8115 parser
->integral_constant_expression_p
8116 = saved_integral_constant_expression_p
;
8117 parser
->non_integral_constant_expression_p
8118 = saved_non_integral_constant_expression_p
;
8120 parser
->type_definition_forbidden_message
= saved_message
;
8122 location_t finish_loc
8123 = cp_lexer_peek_token (parser
->lexer
)->location
;
8124 parens
.require_close (parser
);
8126 /* Construct a location of the form:
8129 with start == caret, finishing at the close-paren. */
8130 location_t noexcept_loc
8131 = make_location (start_loc
, start_loc
, finish_loc
);
8133 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8142 /* Look for the `:: new' and `:: delete', which also signal the
8143 beginning of a new-expression, or delete-expression,
8144 respectively. If the next token is `::', then it might be one of
8146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8150 /* See if the token after the `::' is one of the keywords in
8151 which we're interested. */
8152 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8153 /* If it's `new', we have a new-expression. */
8154 if (keyword
== RID_NEW
)
8155 return cp_parser_new_expression (parser
);
8156 /* Similarly, for `delete'. */
8157 else if (keyword
== RID_DELETE
)
8158 return cp_parser_delete_expression (parser
);
8161 /* Look for a unary operator. */
8162 unary_operator
= cp_parser_unary_operator (token
);
8163 /* The `++' and `--' operators can be handled similarly, even though
8164 they are not technically unary-operators in the grammar. */
8165 if (unary_operator
== ERROR_MARK
)
8167 if (token
->type
== CPP_PLUS_PLUS
)
8168 unary_operator
= PREINCREMENT_EXPR
;
8169 else if (token
->type
== CPP_MINUS_MINUS
)
8170 unary_operator
= PREDECREMENT_EXPR
;
8171 /* Handle the GNU address-of-label extension. */
8172 else if (cp_parser_allow_gnu_extensions_p (parser
)
8173 && token
->type
== CPP_AND_AND
)
8177 location_t start_loc
= token
->location
;
8179 /* Consume the '&&' token. */
8180 cp_lexer_consume_token (parser
->lexer
);
8181 /* Look for the identifier. */
8182 location_t finish_loc
8183 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
8184 identifier
= cp_parser_identifier (parser
);
8185 /* Construct a location of the form:
8188 with caret==start at the "&&", finish at the end of the label. */
8189 location_t combined_loc
8190 = make_location (start_loc
, start_loc
, finish_loc
);
8191 /* Create an expression representing the address. */
8192 expression
= finish_label_address_expr (identifier
, combined_loc
);
8193 if (cp_parser_non_integral_constant_expression (parser
,
8195 expression
= error_mark_node
;
8199 if (unary_operator
!= ERROR_MARK
)
8201 cp_expr cast_expression
;
8202 cp_expr expression
= error_mark_node
;
8203 non_integral_constant non_constant_p
= NIC_NONE
;
8204 location_t loc
= token
->location
;
8205 tsubst_flags_t complain
= complain_flags (decltype_p
);
8207 /* Consume the operator token. */
8208 token
= cp_lexer_consume_token (parser
->lexer
);
8209 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8211 /* Parse the cast-expression. */
8213 = cp_parser_cast_expression (parser
,
8214 unary_operator
== ADDR_EXPR
,
8220 OP_TOKEN CAST_EXPRESSION
8221 ^~~~~~~~~~~~~~~~~~~~~~~~~
8222 with start==caret at the operator token, and
8223 extending to the end of the cast_expression. */
8224 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8226 /* Now, build an appropriate representation. */
8227 switch (unary_operator
)
8230 non_constant_p
= NIC_STAR
;
8231 expression
= build_x_indirect_ref (loc
, cast_expression
,
8234 /* TODO: build_x_indirect_ref does not always honor the
8235 location, so ensure it is set. */
8236 expression
.set_location (loc
);
8240 non_constant_p
= NIC_ADDR
;
8243 expression
= build_x_unary_op (loc
, unary_operator
,
8246 /* TODO: build_x_unary_op does not always honor the location,
8247 so ensure it is set. */
8248 expression
.set_location (loc
);
8251 case PREINCREMENT_EXPR
:
8252 case PREDECREMENT_EXPR
:
8253 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8254 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8257 /* Immediately fold negation of a constant, unless the constant is 0
8258 (since -0 == 0) or it would overflow. */
8259 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
8260 && CONSTANT_CLASS_P (cast_expression
)
8261 && !integer_zerop (cast_expression
)
8262 && !TREE_OVERFLOW (cast_expression
))
8264 tree folded
= fold_build1 (unary_operator
,
8265 TREE_TYPE (cast_expression
),
8267 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8269 expression
= cp_expr (folded
, loc
);
8274 case UNARY_PLUS_EXPR
:
8275 case TRUTH_NOT_EXPR
:
8276 expression
= finish_unary_op_expr (loc
, unary_operator
,
8277 cast_expression
, complain
);
8284 if (non_constant_p
!= NIC_NONE
8285 && cp_parser_non_integral_constant_expression (parser
,
8287 expression
= error_mark_node
;
8292 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8293 /*member_access_only_p=*/false,
8298 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8299 unary-operator, the corresponding tree code is returned. */
8301 static enum tree_code
8302 cp_parser_unary_operator (cp_token
* token
)
8304 switch (token
->type
)
8307 return INDIRECT_REF
;
8313 return UNARY_PLUS_EXPR
;
8319 return TRUTH_NOT_EXPR
;
8322 return BIT_NOT_EXPR
;
8329 /* Parse a new-expression.
8332 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8333 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8335 Returns a representation of the expression. */
8338 cp_parser_new_expression (cp_parser
* parser
)
8340 bool global_scope_p
;
8341 vec
<tree
, va_gc
> *placement
;
8343 vec
<tree
, va_gc
> *initializer
;
8344 tree nelts
= NULL_TREE
;
8347 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8349 /* Look for the optional `::' operator. */
8351 = (cp_parser_global_scope_opt (parser
,
8352 /*current_scope_valid_p=*/false)
8354 /* Look for the `new' operator. */
8355 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8356 /* There's no easy way to tell a new-placement from the
8357 `( type-id )' construct. */
8358 cp_parser_parse_tentatively (parser
);
8359 /* Look for a new-placement. */
8360 placement
= cp_parser_new_placement (parser
);
8361 /* If that didn't work out, there's no new-placement. */
8362 if (!cp_parser_parse_definitely (parser
))
8364 if (placement
!= NULL
)
8365 release_tree_vector (placement
);
8369 /* If the next token is a `(', then we have a parenthesized
8371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8374 const char *saved_message
= parser
->type_definition_forbidden_message
;
8376 /* Consume the `('. */
8377 matching_parens parens
;
8378 parens
.consume_open (parser
);
8380 /* Parse the type-id. */
8381 parser
->type_definition_forbidden_message
8382 = G_("types may not be defined in a new-expression");
8384 type_id_in_expr_sentinel
s (parser
);
8385 type
= cp_parser_type_id (parser
);
8387 parser
->type_definition_forbidden_message
= saved_message
;
8389 /* Look for the closing `)'. */
8390 parens
.require_close (parser
);
8391 token
= cp_lexer_peek_token (parser
->lexer
);
8392 /* There should not be a direct-new-declarator in this production,
8393 but GCC used to allowed this, so we check and emit a sensible error
8394 message for this case. */
8395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8397 error_at (token
->location
,
8398 "array bound forbidden after parenthesized type-id");
8399 inform (token
->location
,
8400 "try removing the parentheses around the type-id");
8401 cp_parser_direct_new_declarator (parser
);
8404 /* Otherwise, there must be a new-type-id. */
8406 type
= cp_parser_new_type_id (parser
, &nelts
);
8408 /* If the next token is a `(' or '{', then we have a new-initializer. */
8409 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8410 if (token
->type
== CPP_OPEN_PAREN
8411 || token
->type
== CPP_OPEN_BRACE
)
8412 initializer
= cp_parser_new_initializer (parser
);
8416 /* A new-expression may not appear in an integral constant
8418 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8419 ret
= error_mark_node
;
8420 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8421 of a new-type-id or type-id of a new-expression, the new-expression shall
8422 contain a new-initializer of the form ( assignment-expression )".
8423 Additionally, consistently with the spirit of DR 1467, we want to accept
8424 'new auto { 2 }' too. */
8425 else if ((ret
= type_uses_auto (type
))
8426 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8427 && (vec_safe_length (initializer
) != 1
8428 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8429 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8431 error_at (token
->location
,
8432 "initialization of new-expression for type %<auto%> "
8433 "requires exactly one element");
8434 ret
= error_mark_node
;
8438 /* Construct a location e.g.:
8441 with caret == start at the start of the "new" token, and the end
8442 at the end of the final token we consumed. */
8443 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8444 location_t end_loc
= get_finish (end_tok
->location
);
8445 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8447 /* Create a representation of the new-expression. */
8448 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8449 tf_warning_or_error
);
8450 protected_set_expr_location (ret
, combined_loc
);
8453 if (placement
!= NULL
)
8454 release_tree_vector (placement
);
8455 if (initializer
!= NULL
)
8456 release_tree_vector (initializer
);
8461 /* Parse a new-placement.
8466 Returns the same representation as for an expression-list. */
8468 static vec
<tree
, va_gc
> *
8469 cp_parser_new_placement (cp_parser
* parser
)
8471 vec
<tree
, va_gc
> *expression_list
;
8473 /* Parse the expression-list. */
8474 expression_list
= (cp_parser_parenthesized_expression_list
8475 (parser
, non_attr
, /*cast_p=*/false,
8476 /*allow_expansion_p=*/true,
8477 /*non_constant_p=*/NULL
));
8479 if (expression_list
&& expression_list
->is_empty ())
8480 error ("expected expression-list or type-id");
8482 return expression_list
;
8485 /* Parse a new-type-id.
8488 type-specifier-seq new-declarator [opt]
8490 Returns the TYPE allocated. If the new-type-id indicates an array
8491 type, *NELTS is set to the number of elements in the last array
8492 bound; the TYPE will not include the last array bound. */
8495 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8497 cp_decl_specifier_seq type_specifier_seq
;
8498 cp_declarator
*new_declarator
;
8499 cp_declarator
*declarator
;
8500 cp_declarator
*outer_declarator
;
8501 const char *saved_message
;
8503 /* The type-specifier sequence must not contain type definitions.
8504 (It cannot contain declarations of new types either, but if they
8505 are not definitions we will catch that because they are not
8507 saved_message
= parser
->type_definition_forbidden_message
;
8508 parser
->type_definition_forbidden_message
8509 = G_("types may not be defined in a new-type-id");
8510 /* Parse the type-specifier-seq. */
8511 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8512 /*is_trailing_return=*/false,
8513 &type_specifier_seq
);
8514 /* Restore the old message. */
8515 parser
->type_definition_forbidden_message
= saved_message
;
8517 if (type_specifier_seq
.type
== error_mark_node
)
8518 return error_mark_node
;
8520 /* Parse the new-declarator. */
8521 new_declarator
= cp_parser_new_declarator_opt (parser
);
8523 /* Determine the number of elements in the last array dimension, if
8526 /* Skip down to the last array dimension. */
8527 declarator
= new_declarator
;
8528 outer_declarator
= NULL
;
8529 while (declarator
&& (declarator
->kind
== cdk_pointer
8530 || declarator
->kind
== cdk_ptrmem
))
8532 outer_declarator
= declarator
;
8533 declarator
= declarator
->declarator
;
8536 && declarator
->kind
== cdk_array
8537 && declarator
->declarator
8538 && declarator
->declarator
->kind
== cdk_array
)
8540 outer_declarator
= declarator
;
8541 declarator
= declarator
->declarator
;
8544 if (declarator
&& declarator
->kind
== cdk_array
)
8546 *nelts
= declarator
->u
.array
.bounds
;
8547 if (*nelts
== error_mark_node
)
8548 *nelts
= integer_one_node
;
8550 if (outer_declarator
)
8551 outer_declarator
->declarator
= declarator
->declarator
;
8553 new_declarator
= NULL
;
8556 return groktypename (&type_specifier_seq
, new_declarator
, false);
8559 /* Parse an (optional) new-declarator.
8562 ptr-operator new-declarator [opt]
8563 direct-new-declarator
8565 Returns the declarator. */
8567 static cp_declarator
*
8568 cp_parser_new_declarator_opt (cp_parser
* parser
)
8570 enum tree_code code
;
8571 tree type
, std_attributes
= NULL_TREE
;
8572 cp_cv_quals cv_quals
;
8574 /* We don't know if there's a ptr-operator next, or not. */
8575 cp_parser_parse_tentatively (parser
);
8576 /* Look for a ptr-operator. */
8577 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8578 /* If that worked, look for more new-declarators. */
8579 if (cp_parser_parse_definitely (parser
))
8581 cp_declarator
*declarator
;
8583 /* Parse another optional declarator. */
8584 declarator
= cp_parser_new_declarator_opt (parser
);
8586 declarator
= cp_parser_make_indirect_declarator
8587 (code
, type
, cv_quals
, declarator
, std_attributes
);
8592 /* If the next token is a `[', there is a direct-new-declarator. */
8593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8594 return cp_parser_direct_new_declarator (parser
);
8599 /* Parse a direct-new-declarator.
8601 direct-new-declarator:
8603 direct-new-declarator [constant-expression]
8607 static cp_declarator
*
8608 cp_parser_direct_new_declarator (cp_parser
* parser
)
8610 cp_declarator
*declarator
= NULL
;
8617 /* Look for the opening `['. */
8618 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8620 token
= cp_lexer_peek_token (parser
->lexer
);
8621 expression
= cp_parser_expression (parser
);
8622 /* The standard requires that the expression have integral
8623 type. DR 74 adds enumeration types. We believe that the
8624 real intent is that these expressions be handled like the
8625 expression in a `switch' condition, which also allows
8626 classes with a single conversion to integral or
8627 enumeration type. */
8628 if (!processing_template_decl
)
8631 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8636 error_at (token
->location
,
8637 "expression in new-declarator must have integral "
8638 "or enumeration type");
8639 expression
= error_mark_node
;
8643 /* Look for the closing `]'. */
8644 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8646 /* Add this bound to the declarator. */
8647 declarator
= make_array_declarator (declarator
, expression
);
8649 /* If the next token is not a `[', then there are no more
8651 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8658 /* Parse a new-initializer.
8661 ( expression-list [opt] )
8664 Returns a representation of the expression-list. */
8666 static vec
<tree
, va_gc
> *
8667 cp_parser_new_initializer (cp_parser
* parser
)
8669 vec
<tree
, va_gc
> *expression_list
;
8671 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8674 bool expr_non_constant_p
;
8675 cp_lexer_set_source_position (parser
->lexer
);
8676 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8677 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8678 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8679 expression_list
= make_tree_vector_single (t
);
8682 expression_list
= (cp_parser_parenthesized_expression_list
8683 (parser
, non_attr
, /*cast_p=*/false,
8684 /*allow_expansion_p=*/true,
8685 /*non_constant_p=*/NULL
));
8687 return expression_list
;
8690 /* Parse a delete-expression.
8693 :: [opt] delete cast-expression
8694 :: [opt] delete [ ] cast-expression
8696 Returns a representation of the expression. */
8699 cp_parser_delete_expression (cp_parser
* parser
)
8701 bool global_scope_p
;
8705 /* Look for the optional `::' operator. */
8707 = (cp_parser_global_scope_opt (parser
,
8708 /*current_scope_valid_p=*/false)
8710 /* Look for the `delete' keyword. */
8711 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8712 /* See if the array syntax is in use. */
8713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8715 /* Consume the `[' token. */
8716 cp_lexer_consume_token (parser
->lexer
);
8717 /* Look for the `]' token. */
8718 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8719 /* Remember that this is the `[]' construct. */
8725 /* Parse the cast-expression. */
8726 expression
= cp_parser_simple_cast_expression (parser
);
8728 /* A delete-expression may not appear in an integral constant
8730 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8731 return error_mark_node
;
8733 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8734 tf_warning_or_error
);
8737 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8738 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8742 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8745 switch (token
->type
)
8751 case CPP_CLOSE_SQUARE
:
8752 case CPP_CLOSE_PAREN
:
8753 case CPP_CLOSE_BRACE
:
8754 case CPP_OPEN_BRACE
:
8758 case CPP_DEREF_STAR
:
8766 case CPP_GREATER_EQ
:
8787 case CPP_OPEN_PAREN
:
8788 /* In ((type ()) () the last () isn't a valid cast-expression,
8789 so the whole must be parsed as postfix-expression. */
8790 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8793 case CPP_OPEN_SQUARE
:
8794 /* '[' may start a primary-expression in obj-c++ and in C++11,
8795 as a lambda-expression, eg, '(void)[]{}'. */
8796 if (cxx_dialect
>= cxx11
)
8798 return c_dialect_objc ();
8801 case CPP_MINUS_MINUS
:
8802 /* '++' and '--' may or may not start a cast-expression:
8804 struct T { void operator++(int); };
8805 void f() { (T())++; }
8818 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8819 in the order: const_cast, static_cast, reinterpret_cast.
8821 Don't suggest dynamic_cast.
8823 Return the first legal cast kind found, or NULL otherwise. */
8826 get_cast_suggestion (tree dst_type
, tree orig_expr
)
8830 /* Reuse the parser logic by attempting to build the various kinds of
8831 cast, with "complain" disabled.
8832 Identify the first such cast that is valid. */
8834 /* Don't attempt to run such logic within template processing. */
8835 if (processing_template_decl
)
8838 /* First try const_cast. */
8839 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
8840 if (trial
!= error_mark_node
)
8841 return "const_cast";
8843 /* If that fails, try static_cast. */
8844 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
8845 if (trial
!= error_mark_node
)
8846 return "static_cast";
8848 /* Finally, try reinterpret_cast. */
8849 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
8850 if (trial
!= error_mark_node
)
8851 return "reinterpret_cast";
8853 /* No such cast possible. */
8857 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8858 suggesting how to convert a C-style cast of the form:
8862 to a C++-style cast.
8864 The primary range of RICHLOC is asssumed to be that of the original
8865 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8866 of the parens in the C-style cast. */
8869 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
8870 location_t close_paren_loc
, tree orig_expr
,
8873 /* This function is non-trivial, so bail out now if the warning isn't
8874 going to be emitted. */
8875 if (!warn_old_style_cast
)
8878 /* Try to find a legal C++ cast, trying them in order:
8879 const_cast, static_cast, reinterpret_cast. */
8880 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
8881 if (!cast_suggestion
)
8884 /* Replace the open paren with "CAST_SUGGESTION<". */
8886 pp_printf (&pp
, "%s<", cast_suggestion
);
8887 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
8889 /* Replace the close paren with "> (". */
8890 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
8892 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8893 rich_loc
->add_fixit_insert_after (")");
8897 /* Parse a cast-expression.
8901 ( type-id ) cast-expression
8903 ADDRESS_P is true iff the unary-expression is appearing as the
8904 operand of the `&' operator. CAST_P is true if this expression is
8905 the target of a cast.
8907 Returns a representation of the expression. */
8910 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8911 bool decltype_p
, cp_id_kind
* pidk
)
8913 /* If it's a `(', then we might be looking at a cast. */
8914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8916 tree type
= NULL_TREE
;
8917 cp_expr
expr (NULL_TREE
);
8918 int cast_expression
= 0;
8919 const char *saved_message
;
8921 /* There's no way to know yet whether or not this is a cast.
8922 For example, `(int (3))' is a unary-expression, while `(int)
8923 3' is a cast. So, we resort to parsing tentatively. */
8924 cp_parser_parse_tentatively (parser
);
8925 /* Types may not be defined in a cast. */
8926 saved_message
= parser
->type_definition_forbidden_message
;
8927 parser
->type_definition_forbidden_message
8928 = G_("types may not be defined in casts");
8929 /* Consume the `('. */
8930 matching_parens parens
;
8931 cp_token
*open_paren
= parens
.consume_open (parser
);
8932 location_t open_paren_loc
= open_paren
->location
;
8933 location_t close_paren_loc
= UNKNOWN_LOCATION
;
8935 /* A very tricky bit is that `(struct S) { 3 }' is a
8936 compound-literal (which we permit in C++ as an extension).
8937 But, that construct is not a cast-expression -- it is a
8938 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8939 is legal; if the compound-literal were a cast-expression,
8940 you'd need an extra set of parentheses.) But, if we parse
8941 the type-id, and it happens to be a class-specifier, then we
8942 will commit to the parse at that point, because we cannot
8943 undo the action that is done when creating a new class. So,
8944 then we cannot back up and do a postfix-expression.
8946 Another tricky case is the following (c++/29234):
8948 struct S { void operator () (); };
8955 As a type-id we parse the parenthesized S()() as a function
8956 returning a function, groktypename complains and we cannot
8957 back up in this case either.
8959 Therefore, we scan ahead to the closing `)', and check to see
8960 if the tokens after the `)' can start a cast-expression. Otherwise
8961 we are dealing with an unary-expression, a postfix-expression
8964 Yet another tricky case, in C++11, is the following (c++/54891):
8968 The issue is that usually, besides the case of lambda-expressions,
8969 the parenthesized type-id cannot be followed by '[', and, eg, we
8970 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8971 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8972 we don't commit, we try a cast-expression, then an unary-expression.
8974 Save tokens so that we can put them back. */
8975 cp_lexer_save_tokens (parser
->lexer
);
8977 /* We may be looking at a cast-expression. */
8978 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8979 /*consume_paren=*/true))
8981 = cp_parser_tokens_start_cast_expression (parser
);
8983 /* Roll back the tokens we skipped. */
8984 cp_lexer_rollback_tokens (parser
->lexer
);
8985 /* If we aren't looking at a cast-expression, simulate an error so
8986 that the call to cp_parser_error_occurred below returns true. */
8987 if (!cast_expression
)
8988 cp_parser_simulate_error (parser
);
8991 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8992 parser
->in_type_id_in_expr_p
= true;
8993 /* Look for the type-id. */
8994 type
= cp_parser_type_id (parser
);
8995 /* Look for the closing `)'. */
8996 cp_token
*close_paren
= parens
.require_close (parser
);
8998 close_paren_loc
= close_paren
->location
;
8999 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9002 /* Restore the saved message. */
9003 parser
->type_definition_forbidden_message
= saved_message
;
9005 /* At this point this can only be either a cast or a
9006 parenthesized ctor such as `(T ())' that looks like a cast to
9007 function returning T. */
9008 if (!cp_parser_error_occurred (parser
))
9010 /* Only commit if the cast-expression doesn't start with
9011 '++', '--', or '[' in C++11. */
9012 if (cast_expression
> 0)
9013 cp_parser_commit_to_topmost_tentative_parse (parser
);
9015 expr
= cp_parser_cast_expression (parser
,
9016 /*address_p=*/false,
9018 /*decltype_p=*/false,
9021 if (cp_parser_parse_definitely (parser
))
9023 /* Warn about old-style casts, if so requested. */
9024 if (warn_old_style_cast
9025 && !in_system_header_at (input_location
)
9026 && !VOID_TYPE_P (type
)
9027 && current_lang_name
!= lang_name_c
)
9029 gcc_rich_location
rich_loc (input_location
);
9030 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9032 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9033 "use of old-style cast to %q#T", type
);
9036 /* Only type conversions to integral or enumeration types
9037 can be used in constant-expressions. */
9038 if (!cast_valid_in_integral_constant_expression_p (type
)
9039 && cp_parser_non_integral_constant_expression (parser
,
9041 return error_mark_node
;
9043 /* Perform the cast. */
9047 with start==caret at the open paren, extending to the
9049 location_t cast_loc
= make_location (open_paren_loc
,
9051 expr
.get_finish ());
9052 expr
= build_c_cast (cast_loc
, type
, expr
);
9057 cp_parser_abort_tentative_parse (parser
);
9060 /* If we get here, then it's not a cast, so it must be a
9061 unary-expression. */
9062 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9063 cast_p
, decltype_p
);
9066 /* Parse a binary expression of the general form:
9070 pm-expression .* cast-expression
9071 pm-expression ->* cast-expression
9073 multiplicative-expression:
9075 multiplicative-expression * pm-expression
9076 multiplicative-expression / pm-expression
9077 multiplicative-expression % pm-expression
9079 additive-expression:
9080 multiplicative-expression
9081 additive-expression + multiplicative-expression
9082 additive-expression - multiplicative-expression
9086 shift-expression << additive-expression
9087 shift-expression >> additive-expression
9089 relational-expression:
9091 relational-expression < shift-expression
9092 relational-expression > shift-expression
9093 relational-expression <= shift-expression
9094 relational-expression >= shift-expression
9098 relational-expression:
9099 relational-expression <? shift-expression
9100 relational-expression >? shift-expression
9102 equality-expression:
9103 relational-expression
9104 equality-expression == relational-expression
9105 equality-expression != relational-expression
9109 and-expression & equality-expression
9111 exclusive-or-expression:
9113 exclusive-or-expression ^ and-expression
9115 inclusive-or-expression:
9116 exclusive-or-expression
9117 inclusive-or-expression | exclusive-or-expression
9119 logical-and-expression:
9120 inclusive-or-expression
9121 logical-and-expression && inclusive-or-expression
9123 logical-or-expression:
9124 logical-and-expression
9125 logical-or-expression || logical-and-expression
9127 All these are implemented with a single function like:
9130 simple-cast-expression
9131 binary-expression <token> binary-expression
9133 CAST_P is true if this expression is the target of a cast.
9135 The binops_by_token map is used to get the tree codes for each <token> type.
9136 binary-expressions are associated according to a precedence table. */
9138 #define TOKEN_PRECEDENCE(token) \
9139 (((token->type == CPP_GREATER \
9140 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9141 && !parser->greater_than_is_operator_p) \
9142 ? PREC_NOT_OPERATOR \
9143 : binops_by_token[token->type].prec)
9146 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9147 bool no_toplevel_fold_p
,
9149 enum cp_parser_prec prec
,
9152 cp_parser_expression_stack stack
;
9153 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9154 cp_parser_expression_stack_entry current
;
9157 enum tree_code rhs_type
;
9158 enum cp_parser_prec new_prec
, lookahead_prec
;
9161 /* Parse the first expression. */
9162 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9163 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9164 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9165 cast_p
, decltype_p
, pidk
);
9166 current
.prec
= prec
;
9168 if (cp_parser_error_occurred (parser
))
9169 return error_mark_node
;
9173 /* Get an operator token. */
9174 token
= cp_lexer_peek_token (parser
->lexer
);
9176 if (warn_cxx11_compat
9177 && token
->type
== CPP_RSHIFT
9178 && !parser
->greater_than_is_operator_p
)
9180 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9181 "%<>>%> operator is treated"
9182 " as two right angle brackets in C++11"))
9183 inform (token
->location
,
9184 "suggest parentheses around %<>>%> expression");
9187 new_prec
= TOKEN_PRECEDENCE (token
);
9188 if (new_prec
!= PREC_NOT_OPERATOR
9189 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9190 /* This is a fold-expression; handle it later. */
9191 new_prec
= PREC_NOT_OPERATOR
;
9193 /* Popping an entry off the stack means we completed a subexpression:
9194 - either we found a token which is not an operator (`>' where it is not
9195 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9196 will happen repeatedly;
9197 - or, we found an operator which has lower priority. This is the case
9198 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9200 if (new_prec
<= current
.prec
)
9209 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9210 current
.loc
= token
->location
;
9212 /* We used the operator token. */
9213 cp_lexer_consume_token (parser
->lexer
);
9215 /* For "false && x" or "true || x", x will never be executed;
9216 disable warnings while evaluating it. */
9217 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9218 c_inhibit_evaluation_warnings
+=
9219 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9220 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9221 c_inhibit_evaluation_warnings
+=
9222 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9224 /* Extract another operand. It may be the RHS of this expression
9225 or the LHS of a new, higher priority expression. */
9226 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9227 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9228 rhs
= cp_parser_simple_cast_expression (parser
);
9230 /* Get another operator token. Look up its precedence to avoid
9231 building a useless (immediately popped) stack entry for common
9232 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9233 token
= cp_lexer_peek_token (parser
->lexer
);
9234 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9235 if (lookahead_prec
!= PREC_NOT_OPERATOR
9236 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9237 lookahead_prec
= PREC_NOT_OPERATOR
;
9238 if (lookahead_prec
> new_prec
)
9240 /* ... and prepare to parse the RHS of the new, higher priority
9241 expression. Since precedence levels on the stack are
9242 monotonically increasing, we do not have to care about
9247 current
.lhs_type
= rhs_type
;
9248 current
.prec
= new_prec
;
9249 new_prec
= lookahead_prec
;
9253 lookahead_prec
= new_prec
;
9254 /* If the stack is not empty, we have parsed into LHS the right side
9255 (`4' in the example above) of an expression we had suspended.
9256 We can use the information on the stack to recover the LHS (`3')
9257 from the stack together with the tree code (`MULT_EXPR'), and
9258 the precedence of the higher level subexpression
9259 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9260 which will be used to actually build the additive expression. */
9262 rhs_type
= current
.lhs_type
;
9267 /* Undo the disabling of warnings done above. */
9268 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
9269 c_inhibit_evaluation_warnings
-=
9270 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
9271 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
9272 c_inhibit_evaluation_warnings
-=
9273 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
9275 if (warn_logical_not_paren
9276 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9277 && current
.lhs_type
== TRUTH_NOT_EXPR
9278 /* Avoid warning for !!x == y. */
9279 && (TREE_CODE (current
.lhs
) != NE_EXPR
9280 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9281 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9282 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9283 /* Avoid warning for !b == y where b is boolean. */
9284 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9285 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9287 /* Avoid warning for !!b == y where b is boolean. */
9288 && (!DECL_P (current
.lhs
)
9289 || TREE_TYPE (current
.lhs
) == NULL_TREE
9290 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9291 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9292 current
.lhs
, maybe_constant_value (rhs
));
9296 location_t combined_loc
= make_location (current
.loc
,
9297 current
.lhs
.get_start (),
9300 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9301 ERROR_MARK for everything that is not a binary expression.
9302 This makes warn_about_parentheses miss some warnings that
9303 involve unary operators. For unary expressions we should
9304 pass the correct tree_code unless the unary expression was
9305 surrounded by parentheses.
9307 if (no_toplevel_fold_p
9308 && lookahead_prec
<= current
.prec
9311 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9312 current
.lhs
= error_mark_node
;
9316 = build_min (current
.tree_type
,
9317 TREE_CODE_CLASS (current
.tree_type
)
9319 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9320 current
.lhs
.get_value (), rhs
.get_value ());
9321 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9326 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
9327 current
.lhs
, current
.lhs_type
,
9328 rhs
, rhs_type
, &overload
,
9329 complain_flags (decltype_p
));
9330 /* TODO: build_x_binary_op doesn't always honor the location. */
9331 current
.lhs
.set_location (combined_loc
);
9333 current
.lhs_type
= current
.tree_type
;
9335 /* If the binary operator required the use of an overloaded operator,
9336 then this expression cannot be an integral constant-expression.
9337 An overloaded operator can be used even if both operands are
9338 otherwise permissible in an integral constant-expression if at
9339 least one of the operands is of enumeration type. */
9342 && cp_parser_non_integral_constant_expression (parser
,
9344 return error_mark_node
;
9351 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9352 bool no_toplevel_fold_p
,
9353 enum cp_parser_prec prec
,
9356 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9357 /*decltype*/false, prec
, pidk
);
9360 /* Parse the `? expression : assignment-expression' part of a
9361 conditional-expression. The LOGICAL_OR_EXPR is the
9362 logical-or-expression that started the conditional-expression.
9363 Returns a representation of the entire conditional-expression.
9365 This routine is used by cp_parser_assignment_expression.
9367 ? expression : assignment-expression
9371 ? : assignment-expression */
9374 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9376 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9377 cp_expr assignment_expr
;
9378 struct cp_token
*token
;
9379 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9381 /* Consume the `?' token. */
9382 cp_lexer_consume_token (parser
->lexer
);
9383 token
= cp_lexer_peek_token (parser
->lexer
);
9384 if (cp_parser_allow_gnu_extensions_p (parser
)
9385 && token
->type
== CPP_COLON
)
9387 pedwarn (token
->location
, OPT_Wpedantic
,
9388 "ISO C++ does not allow ?: with omitted middle operand");
9389 /* Implicit true clause. */
9391 c_inhibit_evaluation_warnings
+=
9392 folded_logical_or_expr
== truthvalue_true_node
;
9393 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9397 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9398 parser
->colon_corrects_to_scope_p
= false;
9399 /* Parse the expression. */
9400 c_inhibit_evaluation_warnings
+=
9401 folded_logical_or_expr
== truthvalue_false_node
;
9402 expr
= cp_parser_expression (parser
);
9403 c_inhibit_evaluation_warnings
+=
9404 ((folded_logical_or_expr
== truthvalue_true_node
)
9405 - (folded_logical_or_expr
== truthvalue_false_node
));
9406 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9409 /* The next token should be a `:'. */
9410 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9411 /* Parse the assignment-expression. */
9412 assignment_expr
= cp_parser_assignment_expression (parser
);
9413 c_inhibit_evaluation_warnings
-=
9414 folded_logical_or_expr
== truthvalue_true_node
;
9417 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9418 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9419 with the caret at the "?", ranging from the start of
9420 the logical_or_expr to the end of the assignment_expr. */
9421 loc
= make_location (loc
,
9422 logical_or_expr
.get_start (),
9423 assignment_expr
.get_finish ());
9425 /* Build the conditional-expression. */
9426 return build_x_conditional_expr (loc
, logical_or_expr
,
9429 tf_warning_or_error
);
9432 /* Parse an assignment-expression.
9434 assignment-expression:
9435 conditional-expression
9436 logical-or-expression assignment-operator assignment_expression
9439 CAST_P is true if this expression is the target of a cast.
9440 DECLTYPE_P is true if this expression is the operand of decltype.
9442 Returns a representation for the expression. */
9445 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9446 bool cast_p
, bool decltype_p
)
9450 /* If the next token is the `throw' keyword, then we're looking at
9451 a throw-expression. */
9452 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9453 expr
= cp_parser_throw_expression (parser
);
9454 /* Otherwise, it must be that we are looking at a
9455 logical-or-expression. */
9458 /* Parse the binary expressions (logical-or-expression). */
9459 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9461 PREC_NOT_OPERATOR
, pidk
);
9462 /* If the next token is a `?' then we're actually looking at a
9463 conditional-expression. */
9464 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9465 return cp_parser_question_colon_clause (parser
, expr
);
9468 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9470 /* If it's an assignment-operator, we're using the second
9472 enum tree_code assignment_operator
9473 = cp_parser_assignment_operator_opt (parser
);
9474 if (assignment_operator
!= ERROR_MARK
)
9476 bool non_constant_p
;
9478 /* Parse the right-hand side of the assignment. */
9479 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9482 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9485 /* An assignment may not appear in a
9486 constant-expression. */
9487 if (cp_parser_non_integral_constant_expression (parser
,
9489 return error_mark_node
;
9490 /* Build the assignment expression. Its default
9494 is the location of the '=' token as the
9495 caret, ranging from the start of the lhs to the
9497 loc
= make_location (loc
,
9500 expr
= build_x_modify_expr (loc
, expr
,
9501 assignment_operator
,
9503 complain_flags (decltype_p
));
9504 /* TODO: build_x_modify_expr doesn't honor the location,
9505 so we must set it here. */
9506 expr
.set_location (loc
);
9514 /* Parse an (optional) assignment-operator.
9516 assignment-operator: one of
9517 = *= /= %= += -= >>= <<= &= ^= |=
9521 assignment-operator: one of
9524 If the next token is an assignment operator, the corresponding tree
9525 code is returned, and the token is consumed. For example, for
9526 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9527 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9528 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9529 operator, ERROR_MARK is returned. */
9531 static enum tree_code
9532 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9537 /* Peek at the next token. */
9538 token
= cp_lexer_peek_token (parser
->lexer
);
9540 switch (token
->type
)
9551 op
= TRUNC_DIV_EXPR
;
9555 op
= TRUNC_MOD_EXPR
;
9587 /* Nothing else is an assignment operator. */
9591 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9592 if (op
!= ERROR_MARK
9593 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9596 /* If it was an assignment operator, consume it. */
9597 if (op
!= ERROR_MARK
)
9598 cp_lexer_consume_token (parser
->lexer
);
9603 /* Parse an expression.
9606 assignment-expression
9607 expression , assignment-expression
9609 CAST_P is true if this expression is the target of a cast.
9610 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9611 except possibly parenthesized or on the RHS of a comma (N3276).
9613 Returns a representation of the expression. */
9616 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9617 bool cast_p
, bool decltype_p
)
9619 cp_expr expression
= NULL_TREE
;
9620 location_t loc
= UNKNOWN_LOCATION
;
9624 cp_expr assignment_expression
;
9626 /* Parse the next assignment-expression. */
9627 assignment_expression
9628 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9630 /* We don't create a temporary for a call that is the immediate operand
9631 of decltype or on the RHS of a comma. But when we see a comma, we
9632 need to create a temporary for a call on the LHS. */
9633 if (decltype_p
&& !processing_template_decl
9634 && TREE_CODE (assignment_expression
) == CALL_EXPR
9635 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9636 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9637 assignment_expression
9638 = build_cplus_new (TREE_TYPE (assignment_expression
),
9639 assignment_expression
, tf_warning_or_error
);
9641 /* If this is the first assignment-expression, we can just
9644 expression
= assignment_expression
;
9647 /* Create a location with caret at the comma, ranging
9648 from the start of the LHS to the end of the RHS. */
9649 loc
= make_location (loc
,
9650 expression
.get_start (),
9651 assignment_expression
.get_finish ());
9652 expression
= build_x_compound_expr (loc
, expression
,
9653 assignment_expression
,
9654 complain_flags (decltype_p
));
9655 expression
.set_location (loc
);
9657 /* If the next token is not a comma, or we're in a fold-expression, then
9658 we are done with the expression. */
9659 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9660 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9662 /* Consume the `,'. */
9663 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9664 cp_lexer_consume_token (parser
->lexer
);
9665 /* A comma operator cannot appear in a constant-expression. */
9666 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9667 expression
= error_mark_node
;
9673 /* Parse a constant-expression.
9675 constant-expression:
9676 conditional-expression
9678 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9679 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9680 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9681 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9682 only parse a conditional-expression, otherwise parse an
9683 assignment-expression. See below for rationale. */
9686 cp_parser_constant_expression (cp_parser
* parser
,
9687 bool allow_non_constant_p
,
9688 bool *non_constant_p
,
9691 bool saved_integral_constant_expression_p
;
9692 bool saved_allow_non_integral_constant_expression_p
;
9693 bool saved_non_integral_constant_expression_p
;
9696 /* It might seem that we could simply parse the
9697 conditional-expression, and then check to see if it were
9698 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9699 one that the compiler can figure out is constant, possibly after
9700 doing some simplifications or optimizations. The standard has a
9701 precise definition of constant-expression, and we must honor
9702 that, even though it is somewhat more restrictive.
9708 is not a legal declaration, because `(2, 3)' is not a
9709 constant-expression. The `,' operator is forbidden in a
9710 constant-expression. However, GCC's constant-folding machinery
9711 will fold this operation to an INTEGER_CST for `3'. */
9713 /* Save the old settings. */
9714 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9715 saved_allow_non_integral_constant_expression_p
9716 = parser
->allow_non_integral_constant_expression_p
;
9717 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9718 /* We are now parsing a constant-expression. */
9719 parser
->integral_constant_expression_p
= true;
9720 parser
->allow_non_integral_constant_expression_p
9721 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9722 parser
->non_integral_constant_expression_p
= false;
9723 /* Although the grammar says "conditional-expression", when not STRICT_P,
9724 we parse an "assignment-expression", which also permits
9725 "throw-expression" and the use of assignment operators. In the case
9726 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9727 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9728 actually essential that we look for an assignment-expression.
9729 For example, cp_parser_initializer_clauses uses this function to
9730 determine whether a particular assignment-expression is in fact
9734 /* Parse the binary expressions (logical-or-expression). */
9735 expression
= cp_parser_binary_expression (parser
, false, false, false,
9736 PREC_NOT_OPERATOR
, NULL
);
9737 /* If the next token is a `?' then we're actually looking at
9738 a conditional-expression; otherwise we're done. */
9739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9740 expression
= cp_parser_question_colon_clause (parser
, expression
);
9743 expression
= cp_parser_assignment_expression (parser
);
9744 /* Restore the old settings. */
9745 parser
->integral_constant_expression_p
9746 = saved_integral_constant_expression_p
;
9747 parser
->allow_non_integral_constant_expression_p
9748 = saved_allow_non_integral_constant_expression_p
;
9749 if (cxx_dialect
>= cxx11
)
9751 /* Require an rvalue constant expression here; that's what our
9752 callers expect. Reference constant expressions are handled
9753 separately in e.g. cp_parser_template_argument. */
9754 tree decay
= expression
;
9755 if (TREE_TYPE (expression
)
9756 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
9757 decay
= build_address (expression
);
9758 bool is_const
= potential_rvalue_constant_expression (decay
);
9759 parser
->non_integral_constant_expression_p
= !is_const
;
9760 if (!is_const
&& !allow_non_constant_p
)
9761 require_potential_rvalue_constant_expression (decay
);
9763 if (allow_non_constant_p
)
9764 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9765 parser
->non_integral_constant_expression_p
9766 = saved_non_integral_constant_expression_p
;
9771 /* Parse __builtin_offsetof.
9773 offsetof-expression:
9774 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9776 offsetof-member-designator:
9778 | offsetof-member-designator "." id-expression
9779 | offsetof-member-designator "[" expression "]"
9780 | offsetof-member-designator "->" id-expression */
9783 cp_parser_builtin_offsetof (cp_parser
*parser
)
9785 int save_ice_p
, save_non_ice_p
;
9790 location_t finish_loc
;
9792 /* We're about to accept non-integral-constant things, but will
9793 definitely yield an integral constant expression. Save and
9794 restore these values around our local parsing. */
9795 save_ice_p
= parser
->integral_constant_expression_p
;
9796 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9798 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9800 /* Consume the "__builtin_offsetof" token. */
9801 cp_lexer_consume_token (parser
->lexer
);
9802 /* Consume the opening `('. */
9803 matching_parens parens
;
9804 parens
.require_open (parser
);
9805 /* Parse the type-id. */
9806 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9808 const char *saved_message
= parser
->type_definition_forbidden_message
;
9809 parser
->type_definition_forbidden_message
9810 = G_("types may not be defined within __builtin_offsetof");
9811 type
= cp_parser_type_id (parser
);
9812 parser
->type_definition_forbidden_message
= saved_message
;
9814 /* Look for the `,'. */
9815 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9816 token
= cp_lexer_peek_token (parser
->lexer
);
9818 /* Build the (type *)null that begins the traditional offsetof macro. */
9820 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
9821 tf_warning_or_error
);
9823 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9824 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
9825 true, &dummy
, token
->location
);
9828 token
= cp_lexer_peek_token (parser
->lexer
);
9829 switch (token
->type
)
9831 case CPP_OPEN_SQUARE
:
9832 /* offsetof-member-designator "[" expression "]" */
9833 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9838 /* offsetof-member-designator "->" identifier */
9839 expr
= grok_array_decl (token
->location
, expr
,
9840 integer_zero_node
, false);
9844 /* offsetof-member-designator "." identifier */
9845 cp_lexer_consume_token (parser
->lexer
);
9846 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9851 case CPP_CLOSE_PAREN
:
9852 /* Consume the ")" token. */
9853 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9854 cp_lexer_consume_token (parser
->lexer
);
9858 /* Error. We know the following require will fail, but
9859 that gives the proper error message. */
9860 parens
.require_close (parser
);
9861 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9862 expr
= error_mark_node
;
9868 /* Make a location of the form:
9869 __builtin_offsetof (struct s, f)
9870 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9871 with caret at the type-id, ranging from the start of the
9872 "_builtin_offsetof" token to the close paren. */
9873 loc
= make_location (loc
, start_loc
, finish_loc
);
9874 /* The result will be an INTEGER_CST, so we need to explicitly
9875 preserve the location. */
9876 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
9879 parser
->integral_constant_expression_p
= save_ice_p
;
9880 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9882 expr
= expr
.maybe_add_location_wrapper ();
9886 /* Parse a trait expression.
9888 Returns a representation of the expression, the underlying type
9889 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9892 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9895 tree type1
, type2
= NULL_TREE
;
9896 bool binary
= false;
9897 bool variadic
= false;
9901 case RID_HAS_NOTHROW_ASSIGN
:
9902 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9904 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9905 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9907 case RID_HAS_NOTHROW_COPY
:
9908 kind
= CPTK_HAS_NOTHROW_COPY
;
9910 case RID_HAS_TRIVIAL_ASSIGN
:
9911 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9913 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9914 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9916 case RID_HAS_TRIVIAL_COPY
:
9917 kind
= CPTK_HAS_TRIVIAL_COPY
;
9919 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9920 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9922 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
9923 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
9925 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9926 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9928 case RID_IS_ABSTRACT
:
9929 kind
= CPTK_IS_ABSTRACT
;
9931 case RID_IS_AGGREGATE
:
9932 kind
= CPTK_IS_AGGREGATE
;
9934 case RID_IS_BASE_OF
:
9935 kind
= CPTK_IS_BASE_OF
;
9939 kind
= CPTK_IS_CLASS
;
9942 kind
= CPTK_IS_EMPTY
;
9945 kind
= CPTK_IS_ENUM
;
9948 kind
= CPTK_IS_FINAL
;
9950 case RID_IS_LITERAL_TYPE
:
9951 kind
= CPTK_IS_LITERAL_TYPE
;
9956 case RID_IS_POLYMORPHIC
:
9957 kind
= CPTK_IS_POLYMORPHIC
;
9959 case RID_IS_SAME_AS
:
9960 kind
= CPTK_IS_SAME_AS
;
9963 case RID_IS_STD_LAYOUT
:
9964 kind
= CPTK_IS_STD_LAYOUT
;
9966 case RID_IS_TRIVIAL
:
9967 kind
= CPTK_IS_TRIVIAL
;
9969 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9970 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9973 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9974 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9977 case RID_IS_TRIVIALLY_COPYABLE
:
9978 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9981 kind
= CPTK_IS_UNION
;
9983 case RID_UNDERLYING_TYPE
:
9984 kind
= CPTK_UNDERLYING_TYPE
;
9989 case RID_DIRECT_BASES
:
9990 kind
= CPTK_DIRECT_BASES
;
9992 case RID_IS_ASSIGNABLE
:
9993 kind
= CPTK_IS_ASSIGNABLE
;
9996 case RID_IS_CONSTRUCTIBLE
:
9997 kind
= CPTK_IS_CONSTRUCTIBLE
;
10001 gcc_unreachable ();
10004 /* Get location of initial token. */
10005 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10007 /* Consume the token. */
10008 cp_lexer_consume_token (parser
->lexer
);
10010 matching_parens parens
;
10011 parens
.require_open (parser
);
10014 type_id_in_expr_sentinel
s (parser
);
10015 type1
= cp_parser_type_id (parser
);
10018 if (type1
== error_mark_node
)
10019 return error_mark_node
;
10023 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10026 type_id_in_expr_sentinel
s (parser
);
10027 type2
= cp_parser_type_id (parser
);
10030 if (type2
== error_mark_node
)
10031 return error_mark_node
;
10035 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10037 cp_lexer_consume_token (parser
->lexer
);
10038 tree elt
= cp_parser_type_id (parser
);
10039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10041 cp_lexer_consume_token (parser
->lexer
);
10042 elt
= make_pack_expansion (elt
);
10044 if (elt
== error_mark_node
)
10045 return error_mark_node
;
10046 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10050 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10051 parens
.require_close (parser
);
10053 /* Construct a location of the form:
10054 __is_trivially_copyable(_Tp)
10055 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10056 with start == caret, finishing at the close-paren. */
10057 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10059 /* Complete the trait expression, which may mean either processing
10060 the trait expr now or saving it for template instantiation. */
10063 case CPTK_UNDERLYING_TYPE
:
10064 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10066 return cp_expr (finish_bases (type1
, false), trait_loc
);
10067 case CPTK_DIRECT_BASES
:
10068 return cp_expr (finish_bases (type1
, true), trait_loc
);
10070 return cp_expr (finish_trait_expr (kind
, type1
, type2
), trait_loc
);
10074 /* Parse a lambda expression.
10077 lambda-introducer lambda-declarator [opt] compound-statement
10079 Returns a representation of the expression. */
10082 cp_parser_lambda_expression (cp_parser
* parser
)
10084 tree lambda_expr
= build_lambda_expr ();
10087 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10088 cp_token_position start
= 0;
10090 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10092 if (cp_unevaluated_operand
)
10094 if (!token
->error_reported
)
10096 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10097 "lambda-expression in unevaluated context");
10098 token
->error_reported
= true;
10102 else if (parser
->in_template_argument_list_p
)
10104 if (!token
->error_reported
)
10106 error_at (token
->location
, "lambda-expression in template-argument");
10107 token
->error_reported
= true;
10112 /* We may be in the middle of deferred access check. Disable
10114 push_deferring_access_checks (dk_no_deferred
);
10116 cp_parser_lambda_introducer (parser
, lambda_expr
);
10118 type
= begin_lambda_type (lambda_expr
);
10119 if (type
== error_mark_node
)
10120 return error_mark_node
;
10122 record_lambda_scope (lambda_expr
);
10124 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10125 determine_visibility (TYPE_NAME (type
));
10127 /* Now that we've started the type, add the capture fields for any
10128 explicit captures. */
10129 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10132 /* Inside the class, surrounding template-parameter-lists do not apply. */
10133 unsigned int saved_num_template_parameter_lists
10134 = parser
->num_template_parameter_lists
;
10135 unsigned char in_statement
= parser
->in_statement
;
10136 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10137 bool fully_implicit_function_template_p
10138 = parser
->fully_implicit_function_template_p
;
10139 tree implicit_template_parms
= parser
->implicit_template_parms
;
10140 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10141 bool auto_is_implicit_function_template_parm_p
10142 = parser
->auto_is_implicit_function_template_parm_p
;
10144 parser
->num_template_parameter_lists
= 0;
10145 parser
->in_statement
= 0;
10146 parser
->in_switch_statement_p
= false;
10147 parser
->fully_implicit_function_template_p
= false;
10148 parser
->implicit_template_parms
= 0;
10149 parser
->implicit_template_scope
= 0;
10150 parser
->auto_is_implicit_function_template_parm_p
= false;
10152 /* By virtue of defining a local class, a lambda expression has access to
10153 the private variables of enclosing classes. */
10155 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10157 if (ok
&& cp_parser_error_occurred (parser
))
10162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
10163 && cp_parser_start_tentative_firewall (parser
))
10165 cp_parser_lambda_body (parser
, lambda_expr
);
10167 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10169 if (cp_parser_skip_to_closing_brace (parser
))
10170 cp_lexer_consume_token (parser
->lexer
);
10173 /* The capture list was built up in reverse order; fix that now. */
10174 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10175 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10178 maybe_add_lambda_conv_op (type
);
10180 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
10182 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10183 parser
->in_statement
= in_statement
;
10184 parser
->in_switch_statement_p
= in_switch_statement_p
;
10185 parser
->fully_implicit_function_template_p
10186 = fully_implicit_function_template_p
;
10187 parser
->implicit_template_parms
= implicit_template_parms
;
10188 parser
->implicit_template_scope
= implicit_template_scope
;
10189 parser
->auto_is_implicit_function_template_parm_p
10190 = auto_is_implicit_function_template_parm_p
;
10193 /* This field is only used during parsing of the lambda. */
10194 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10196 /* This lambda shouldn't have any proxies left at this point. */
10197 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10198 /* And now that we're done, push proxies for an enclosing lambda. */
10199 insert_pending_capture_proxies ();
10202 lambda_expr
= build_lambda_object (lambda_expr
);
10204 lambda_expr
= error_mark_node
;
10206 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10208 pop_deferring_access_checks ();
10210 return lambda_expr
;
10213 /* Parse the beginning of a lambda expression.
10216 [ lambda-capture [opt] ]
10218 LAMBDA_EXPR is the current representation of the lambda expression. */
10221 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10223 /* Need commas after the first capture. */
10226 /* Eat the leading `['. */
10227 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10229 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10231 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
10232 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10233 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10234 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10236 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10238 cp_lexer_consume_token (parser
->lexer
);
10242 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10244 cp_token
* capture_token
;
10246 tree capture_init_expr
;
10247 cp_id_kind idk
= CP_ID_KIND_NONE
;
10248 bool explicit_init_p
= false;
10250 enum capture_kind_type
10255 enum capture_kind_type capture_kind
= BY_COPY
;
10257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10259 error ("expected end of capture-list");
10266 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10268 /* Possibly capture `this'. */
10269 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10271 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10272 if (cxx_dialect
< cxx2a
10273 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10274 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10275 "with by-copy capture default");
10276 cp_lexer_consume_token (parser
->lexer
);
10277 add_capture (lambda_expr
,
10278 /*id=*/this_identifier
,
10279 /*initializer=*/finish_this_expr (),
10280 /*by_reference_p=*/true,
10285 /* Possibly capture `*this'. */
10286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10287 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10289 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10290 if (cxx_dialect
< cxx17
)
10291 pedwarn (loc
, 0, "%<*this%> capture only available with "
10292 "-std=c++17 or -std=gnu++17");
10293 cp_lexer_consume_token (parser
->lexer
);
10294 cp_lexer_consume_token (parser
->lexer
);
10295 add_capture (lambda_expr
,
10296 /*id=*/this_identifier
,
10297 /*initializer=*/finish_this_expr (),
10298 /*by_reference_p=*/false,
10303 /* Remember whether we want to capture as a reference or not. */
10304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10306 capture_kind
= BY_REFERENCE
;
10307 cp_lexer_consume_token (parser
->lexer
);
10310 /* Get the identifier. */
10311 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10312 capture_id
= cp_parser_identifier (parser
);
10314 if (capture_id
== error_mark_node
)
10315 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10316 delimiters, but I modified this to stop on unnested ']' as well. It
10317 was already changed to stop on unnested '}', so the
10318 "closing_parenthesis" name is no more misleading with my change. */
10320 cp_parser_skip_to_closing_parenthesis (parser
,
10321 /*recovering=*/true,
10323 /*consume_paren=*/true);
10327 /* Find the initializer for this capture. */
10328 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10329 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10330 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10332 bool direct
, non_constant
;
10333 /* An explicit initializer exists. */
10334 if (cxx_dialect
< cxx14
)
10335 pedwarn (input_location
, 0,
10336 "lambda capture initializers "
10337 "only available with -std=c++14 or -std=gnu++14");
10338 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10339 &non_constant
, true);
10340 explicit_init_p
= true;
10341 if (capture_init_expr
== NULL_TREE
)
10343 error ("empty initializer for lambda init-capture");
10344 capture_init_expr
= error_mark_node
;
10349 const char* error_msg
;
10351 /* Turn the identifier into an id-expression. */
10353 = cp_parser_lookup_name_simple (parser
, capture_id
,
10354 capture_token
->location
);
10356 if (capture_init_expr
== error_mark_node
)
10358 unqualified_name_lookup_error (capture_id
);
10361 else if (!VAR_P (capture_init_expr
)
10362 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10364 error_at (capture_token
->location
,
10365 "capture of non-variable %qE",
10366 capture_init_expr
);
10367 if (DECL_P (capture_init_expr
))
10368 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10369 "%q#D declared here", capture_init_expr
);
10372 if (VAR_P (capture_init_expr
)
10373 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10375 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10376 "%qD with non-automatic storage duration",
10377 capture_init_expr
))
10378 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10379 "%q#D declared here", capture_init_expr
);
10384 = finish_id_expression
10389 /*integral_constant_expression_p=*/false,
10390 /*allow_non_integral_constant_expression_p=*/false,
10391 /*non_integral_constant_expression_p=*/NULL
,
10392 /*template_p=*/false,
10394 /*address_p=*/false,
10395 /*template_arg_p=*/false,
10397 capture_token
->location
);
10399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10401 cp_lexer_consume_token (parser
->lexer
);
10402 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10406 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10407 && !explicit_init_p
)
10409 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10410 && capture_kind
== BY_COPY
)
10411 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10412 "of %qD redundant with by-copy capture default",
10414 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10415 && capture_kind
== BY_REFERENCE
)
10416 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10417 "capture of %qD redundant with by-reference capture "
10418 "default", capture_id
);
10421 add_capture (lambda_expr
,
10424 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10427 /* If there is any qualification still in effect, clear it
10428 now; we will be starting fresh with the next capture. */
10429 parser
->scope
= NULL_TREE
;
10430 parser
->qualifying_scope
= NULL_TREE
;
10431 parser
->object_scope
= NULL_TREE
;
10434 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10437 /* Parse the (optional) middle of a lambda expression.
10440 < template-parameter-list [opt] >
10441 ( parameter-declaration-clause [opt] )
10442 attribute-specifier [opt]
10443 decl-specifier-seq [opt]
10444 exception-specification [opt]
10445 lambda-return-type-clause [opt]
10447 LAMBDA_EXPR is the current representation of the lambda expression. */
10450 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10452 /* 5.1.1.4 of the standard says:
10453 If a lambda-expression does not include a lambda-declarator, it is as if
10454 the lambda-declarator were ().
10455 This means an empty parameter list, no attributes, and no exception
10457 tree param_list
= void_list_node
;
10458 tree attributes
= NULL_TREE
;
10459 tree exception_spec
= NULL_TREE
;
10460 tree template_param_list
= NULL_TREE
;
10461 tree tx_qual
= NULL_TREE
;
10462 tree return_type
= NULL_TREE
;
10463 cp_decl_specifier_seq lambda_specs
;
10464 clear_decl_specs (&lambda_specs
);
10466 /* The template-parameter-list is optional, but must begin with
10467 an opening angle if present. */
10468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10470 if (cxx_dialect
< cxx14
)
10471 pedwarn (parser
->lexer
->next_token
->location
, 0,
10472 "lambda templates are only available with "
10473 "-std=c++14 or -std=gnu++14");
10474 else if (cxx_dialect
< cxx2a
)
10475 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10476 "lambda templates are only available with "
10477 "-std=c++2a or -std=gnu++2a");
10479 cp_lexer_consume_token (parser
->lexer
);
10481 template_param_list
= cp_parser_template_parameter_list (parser
);
10483 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10485 /* We just processed one more parameter list. */
10486 ++parser
->num_template_parameter_lists
;
10489 /* The parameter-declaration-clause is optional (unless
10490 template-parameter-list was given), but must begin with an
10491 opening parenthesis if present. */
10492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10494 matching_parens parens
;
10495 parens
.consume_open (parser
);
10497 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10499 /* Parse parameters. */
10500 param_list
= cp_parser_parameter_declaration_clause (parser
);
10502 /* Default arguments shall not be specified in the
10503 parameter-declaration-clause of a lambda-declarator. */
10504 if (cxx_dialect
< cxx14
)
10505 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10506 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10507 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10508 "default argument specified for lambda parameter");
10510 parens
.require_close (parser
);
10512 attributes
= cp_parser_attributes_opt (parser
);
10514 /* In the decl-specifier-seq of the lambda-declarator, each
10515 decl-specifier shall either be mutable or constexpr. */
10516 int declares_class_or_enum
;
10517 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
10518 cp_parser_decl_specifier_seq (parser
,
10519 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10520 &lambda_specs
, &declares_class_or_enum
);
10521 if (lambda_specs
.storage_class
== sc_mutable
)
10523 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10524 if (lambda_specs
.conflicting_specifiers_p
)
10525 error_at (lambda_specs
.locations
[ds_storage_class
],
10526 "duplicate %<mutable%>");
10529 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10531 /* Parse optional exception specification. */
10532 exception_spec
= cp_parser_exception_specification_opt (parser
);
10534 /* Parse optional trailing return type. */
10535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10537 cp_lexer_consume_token (parser
->lexer
);
10538 return_type
= cp_parser_trailing_type_id (parser
);
10541 /* The function parameters must be in scope all the way until after the
10542 trailing-return-type in case of decltype. */
10543 pop_bindings_and_leave_scope ();
10545 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10546 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10548 /* Create the function call operator.
10550 Messing with declarators like this is no uglier than building up the
10551 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10554 cp_decl_specifier_seq return_type_specs
;
10555 cp_declarator
* declarator
;
10560 clear_decl_specs (&return_type_specs
);
10561 return_type_specs
.type
= make_auto ();
10563 if (lambda_specs
.locations
[ds_constexpr
])
10565 if (cxx_dialect
>= cxx17
)
10566 return_type_specs
.locations
[ds_constexpr
]
10567 = lambda_specs
.locations
[ds_constexpr
];
10569 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
10570 "lambda only available with -std=c++17 or -std=gnu++17");
10573 p
= obstack_alloc (&declarator_obstack
, 0);
10575 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
);
10577 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10578 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10579 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10580 VIRT_SPEC_UNSPECIFIED
,
10584 /*late_return_type=*/NULL_TREE
,
10585 /*requires_clause*/NULL_TREE
);
10586 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10588 declarator
->u
.function
.late_return_type
= return_type
;
10590 fco
= grokmethod (&return_type_specs
,
10593 if (fco
!= error_mark_node
)
10595 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10596 DECL_ARTIFICIAL (fco
) = 1;
10597 /* Give the object parameter a different name. */
10598 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10600 if (template_param_list
)
10602 fco
= finish_member_template_decl (fco
);
10603 finish_template_decl (template_param_list
);
10604 --parser
->num_template_parameter_lists
;
10606 else if (parser
->fully_implicit_function_template_p
)
10607 fco
= finish_fully_implicit_template (parser
, fco
);
10609 finish_member_declaration (fco
);
10611 obstack_free (&declarator_obstack
, p
);
10613 return (fco
!= error_mark_node
);
10617 /* Parse the body of a lambda expression, which is simply
10621 but which requires special handling.
10622 LAMBDA_EXPR is the current representation of the lambda expression. */
10625 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10627 bool nested
= (current_function_decl
!= NULL_TREE
);
10628 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10629 bool in_function_body
= parser
->in_function_body
;
10632 push_function_context ();
10634 /* Still increment function_depth so that we don't GC in the
10635 middle of an expression. */
10638 vec
<tree
> omp_privatization_save
;
10639 save_omp_privatization_clauses (omp_privatization_save
);
10640 /* Clear this in case we're in the middle of a default argument. */
10641 parser
->local_variables_forbidden_p
= false;
10642 parser
->in_function_body
= true;
10645 local_specialization_stack
s (lss_copy
);
10646 tree fco
= lambda_function (lambda_expr
);
10647 tree body
= start_lambda_function (fco
, lambda_expr
);
10648 matching_braces braces
;
10650 if (braces
.require_open (parser
))
10652 tree compound_stmt
= begin_compound_stmt (0);
10654 /* Originally C++11 required us to peek for 'return expr'; and
10655 process it specially here to deduce the return type. N3638
10656 removed the need for that. */
10658 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10659 cp_parser_label_declaration (parser
);
10660 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10661 braces
.require_close (parser
);
10663 finish_compound_stmt (compound_stmt
);
10666 finish_lambda_function (body
);
10669 restore_omp_privatization_clauses (omp_privatization_save
);
10670 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10671 parser
->in_function_body
= in_function_body
;
10673 pop_function_context();
10678 /* Statements [gram.stmt.stmt] */
10680 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10683 add_debug_begin_stmt (location_t loc
)
10685 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
10687 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
10688 /* A concept is never expanded normally. */
10691 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
10692 SET_EXPR_LOCATION (stmt
, loc
);
10696 /* Parse a statement.
10700 expression-statement
10702 selection-statement
10703 iteration-statement
10705 declaration-statement
10712 attribute-specifier-seq (opt) expression-statement
10713 attribute-specifier-seq (opt) compound-statement
10714 attribute-specifier-seq (opt) selection-statement
10715 attribute-specifier-seq (opt) iteration-statement
10716 attribute-specifier-seq (opt) jump-statement
10717 declaration-statement
10718 attribute-specifier-seq (opt) try-block
10721 expression-statement
10729 IN_COMPOUND is true when the statement is nested inside a
10730 cp_parser_compound_statement; this matters for certain pragmas.
10732 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10733 is a (possibly labeled) if statement which is not enclosed in braces
10734 and has an else clause. This is used to implement -Wparentheses.
10736 CHAIN is a vector of if-else-if conditions. */
10739 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10740 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
10741 location_t
*loc_after_labels
)
10743 tree statement
, std_attrs
= NULL_TREE
;
10745 location_t statement_location
, attrs_location
;
10750 /* There is no statement yet. */
10751 statement
= NULL_TREE
;
10753 saved_token_sentinel
saved_tokens (parser
->lexer
);
10754 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10755 if (c_dialect_objc ())
10756 /* In obj-c++, seeing '[[' might be the either the beginning of
10757 c++11 attributes, or a nested objc-message-expression. So
10758 let's parse the c++11 attributes tentatively. */
10759 cp_parser_parse_tentatively (parser
);
10760 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10761 if (c_dialect_objc ())
10763 if (!cp_parser_parse_definitely (parser
))
10764 std_attrs
= NULL_TREE
;
10767 /* Peek at the next token. */
10768 token
= cp_lexer_peek_token (parser
->lexer
);
10769 /* Remember the location of the first token in the statement. */
10770 statement_location
= token
->location
;
10771 add_debug_begin_stmt (statement_location
);
10772 /* If this is a keyword, then that will often determine what kind of
10773 statement we have. */
10774 if (token
->type
== CPP_KEYWORD
)
10776 enum rid keyword
= token
->keyword
;
10782 /* Looks like a labeled-statement with a case label.
10783 Parse the label, and then use tail recursion to parse
10785 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10786 in_compound
= false;
10791 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10797 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
10804 statement
= cp_parser_jump_statement (parser
);
10807 /* Objective-C++ exception-handling constructs. */
10810 case RID_AT_FINALLY
:
10811 case RID_AT_SYNCHRONIZED
:
10813 statement
= cp_parser_objc_statement (parser
);
10817 statement
= cp_parser_try_block (parser
);
10820 case RID_NAMESPACE
:
10821 /* This must be a namespace alias definition. */
10822 cp_parser_declaration_statement (parser
);
10825 case RID_TRANSACTION_ATOMIC
:
10826 case RID_TRANSACTION_RELAXED
:
10827 case RID_SYNCHRONIZED
:
10828 case RID_ATOMIC_NOEXCEPT
:
10829 case RID_ATOMIC_CANCEL
:
10830 statement
= cp_parser_transaction (parser
, token
);
10832 case RID_TRANSACTION_CANCEL
:
10833 statement
= cp_parser_transaction_cancel (parser
);
10837 /* It might be a keyword like `int' that can start a
10838 declaration-statement. */
10842 else if (token
->type
== CPP_NAME
)
10844 /* If the next token is a `:', then we are looking at a
10845 labeled-statement. */
10846 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10847 if (token
->type
== CPP_COLON
)
10849 /* Looks like a labeled-statement with an ordinary label.
10850 Parse the label, and then use tail recursion to parse
10853 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10854 in_compound
= false;
10858 /* Anything that starts with a `{' must be a compound-statement. */
10859 else if (token
->type
== CPP_OPEN_BRACE
)
10860 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10861 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10862 a statement all its own. */
10863 else if (token
->type
== CPP_PRAGMA
)
10865 /* Only certain OpenMP pragmas are attached to statements, and thus
10866 are considered statements themselves. All others are not. In
10867 the context of a compound, accept the pragma as a "statement" and
10868 return so that we can check for a close brace. Otherwise we
10869 require a real statement and must go back and read one. */
10871 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10872 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10876 else if (token
->type
== CPP_EOF
)
10878 cp_parser_error (parser
, "expected statement");
10882 /* Everything else must be a declaration-statement or an
10883 expression-statement. Try for the declaration-statement
10884 first, unless we are looking at a `;', in which case we know that
10885 we have an expression-statement. */
10888 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10890 if (std_attrs
!= NULL_TREE
)
10892 /* Attributes should be parsed as part of the the
10893 declaration, so let's un-parse them. */
10894 saved_tokens
.rollback();
10895 std_attrs
= NULL_TREE
;
10898 cp_parser_parse_tentatively (parser
);
10899 /* Try to parse the declaration-statement. */
10900 cp_parser_declaration_statement (parser
);
10901 /* If that worked, we're done. */
10902 if (cp_parser_parse_definitely (parser
))
10905 /* All preceding labels have been parsed at this point. */
10906 if (loc_after_labels
!= NULL
)
10907 *loc_after_labels
= statement_location
;
10909 /* Look for an expression-statement instead. */
10910 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10912 /* Handle [[fallthrough]];. */
10913 if (attribute_fallthrough_p (std_attrs
))
10915 /* The next token after the fallthrough attribute is ';'. */
10916 if (statement
== NULL_TREE
)
10918 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10919 statement
= build_call_expr_internal_loc (statement_location
,
10921 void_type_node
, 0);
10922 finish_expr_stmt (statement
);
10925 warning_at (statement_location
, OPT_Wattributes
,
10926 "%<fallthrough%> attribute not followed by %<;%>");
10927 std_attrs
= NULL_TREE
;
10931 /* Set the line number for the statement. */
10932 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10933 SET_EXPR_LOCATION (statement
, statement_location
);
10935 /* Allow "[[fallthrough]];", but warn otherwise. */
10936 if (std_attrs
!= NULL_TREE
)
10937 warning_at (attrs_location
,
10939 "attributes at the beginning of statement are ignored");
10942 /* Append ATTR to attribute list ATTRS. */
10945 attr_chainon (tree attrs
, tree attr
)
10947 if (attrs
== error_mark_node
)
10948 return error_mark_node
;
10949 if (attr
== error_mark_node
)
10950 return error_mark_node
;
10951 return chainon (attrs
, attr
);
10954 /* Parse the label for a labeled-statement, i.e.
10957 case constant-expression :
10961 case constant-expression ... constant-expression : statement
10963 When a label is parsed without errors, the label is added to the
10964 parse tree by the finish_* functions, so this function doesn't
10965 have to return the label. */
10968 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10971 tree label
= NULL_TREE
;
10972 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10974 /* The next token should be an identifier. */
10975 token
= cp_lexer_peek_token (parser
->lexer
);
10976 if (token
->type
!= CPP_NAME
10977 && token
->type
!= CPP_KEYWORD
)
10979 cp_parser_error (parser
, "expected labeled-statement");
10983 /* Remember whether this case or a user-defined label is allowed to fall
10985 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
10987 parser
->colon_corrects_to_scope_p
= false;
10988 switch (token
->keyword
)
10992 tree expr
, expr_hi
;
10993 cp_token
*ellipsis
;
10995 /* Consume the `case' token. */
10996 cp_lexer_consume_token (parser
->lexer
);
10997 /* Parse the constant-expression. */
10998 expr
= cp_parser_constant_expression (parser
);
10999 if (check_for_bare_parameter_packs (expr
))
11000 expr
= error_mark_node
;
11002 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11003 if (ellipsis
->type
== CPP_ELLIPSIS
)
11005 /* Consume the `...' token. */
11006 cp_lexer_consume_token (parser
->lexer
);
11007 expr_hi
= cp_parser_constant_expression (parser
);
11008 if (check_for_bare_parameter_packs (expr_hi
))
11009 expr_hi
= error_mark_node
;
11011 /* We don't need to emit warnings here, as the common code
11012 will do this for us. */
11015 expr_hi
= NULL_TREE
;
11017 if (parser
->in_switch_statement_p
)
11019 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11020 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11021 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11024 error_at (token
->location
,
11025 "case label %qE not within a switch statement",
11031 /* Consume the `default' token. */
11032 cp_lexer_consume_token (parser
->lexer
);
11034 if (parser
->in_switch_statement_p
)
11036 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11037 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11038 FALLTHROUGH_LABEL_P (CASE_LABEL (l
)) = fallthrough_p
;
11041 error_at (token
->location
, "case label not within a switch statement");
11045 /* Anything else must be an ordinary label. */
11046 label
= finish_label_stmt (cp_parser_identifier (parser
));
11047 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11048 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11052 /* Require the `:' token. */
11053 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11055 /* An ordinary label may optionally be followed by attributes.
11056 However, this is only permitted if the attributes are then
11057 followed by a semicolon. This is because, for backward
11058 compatibility, when parsing
11059 lab: __attribute__ ((unused)) int i;
11060 we want the attribute to attach to "i", not "lab". */
11061 if (label
!= NULL_TREE
11062 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11065 cp_parser_parse_tentatively (parser
);
11066 attrs
= cp_parser_gnu_attributes_opt (parser
);
11067 if (attrs
== NULL_TREE
11068 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11069 cp_parser_abort_tentative_parse (parser
);
11070 else if (!cp_parser_parse_definitely (parser
))
11073 attributes
= attr_chainon (attributes
, attrs
);
11076 if (attributes
!= NULL_TREE
)
11077 cplus_decl_attributes (&label
, attributes
, 0);
11079 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11082 /* Parse an expression-statement.
11084 expression-statement:
11087 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11088 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11089 indicates whether this expression-statement is part of an
11090 expression statement. */
11093 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11095 tree statement
= NULL_TREE
;
11096 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11097 location_t loc
= token
->location
;
11099 /* There might be attribute fallthrough. */
11100 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11102 /* If the next token is a ';', then there is no expression
11104 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11106 statement
= cp_parser_expression (parser
);
11107 if (statement
== error_mark_node
11108 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11110 cp_parser_skip_to_end_of_block_or_statement (parser
);
11111 return error_mark_node
;
11115 /* Handle [[fallthrough]];. */
11116 if (attribute_fallthrough_p (attr
))
11118 /* The next token after the fallthrough attribute is ';'. */
11119 if (statement
== NULL_TREE
)
11120 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11121 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11122 void_type_node
, 0);
11124 warning_at (loc
, OPT_Wattributes
,
11125 "%<fallthrough%> attribute not followed by %<;%>");
11129 /* Allow "[[fallthrough]];", but warn otherwise. */
11130 if (attr
!= NULL_TREE
)
11131 warning_at (loc
, OPT_Wattributes
,
11132 "attributes at the beginning of statement are ignored");
11134 /* Give a helpful message for "A<T>::type t;" and the like. */
11135 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11136 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11138 if (TREE_CODE (statement
) == SCOPE_REF
)
11139 error_at (token
->location
, "need %<typename%> before %qE because "
11140 "%qT is a dependent scope",
11141 statement
, TREE_OPERAND (statement
, 0));
11142 else if (is_overloaded_fn (statement
)
11143 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11146 tree fn
= get_first_fn (statement
);
11147 error_at (token
->location
,
11148 "%<%T::%D%> names the constructor, not the type",
11149 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11153 /* Consume the final `;'. */
11154 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11156 if (in_statement_expr
11157 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11158 /* This is the final expression statement of a statement
11160 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11161 else if (statement
)
11162 statement
= finish_expr_stmt (statement
);
11167 /* Parse a compound-statement.
11169 compound-statement:
11170 { statement-seq [opt] }
11174 compound-statement:
11175 { label-declaration-seq [opt] statement-seq [opt] }
11177 label-declaration-seq:
11179 label-declaration-seq label-declaration
11181 Returns a tree representing the statement. */
11184 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11185 int bcs_flags
, bool function_body
)
11187 tree compound_stmt
;
11188 matching_braces braces
;
11190 /* Consume the `{'. */
11191 if (!braces
.require_open (parser
))
11192 return error_mark_node
;
11193 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11194 && !function_body
&& cxx_dialect
< cxx14
)
11195 pedwarn (input_location
, OPT_Wpedantic
,
11196 "compound-statement in %<constexpr%> function");
11197 /* Begin the compound-statement. */
11198 compound_stmt
= begin_compound_stmt (bcs_flags
);
11199 /* If the next keyword is `__label__' we have a label declaration. */
11200 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11201 cp_parser_label_declaration (parser
);
11202 /* Parse an (optional) statement-seq. */
11203 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11204 /* Finish the compound-statement. */
11205 finish_compound_stmt (compound_stmt
);
11206 /* Consume the `}'. */
11207 braces
.require_close (parser
);
11209 return compound_stmt
;
11212 /* Parse an (optional) statement-seq.
11216 statement-seq [opt] statement */
11219 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11221 /* Scan statements until there aren't any more. */
11224 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11226 /* If we are looking at a `}', then we have run out of
11227 statements; the same is true if we have reached the end
11228 of file, or have stumbled upon a stray '@end'. */
11229 if (token
->type
== CPP_CLOSE_BRACE
11230 || token
->type
== CPP_EOF
11231 || token
->type
== CPP_PRAGMA_EOL
11232 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11235 /* If we are in a compound statement and find 'else' then
11236 something went wrong. */
11237 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11239 if (parser
->in_statement
& IN_IF_STMT
)
11243 token
= cp_lexer_consume_token (parser
->lexer
);
11244 error_at (token
->location
, "%<else%> without a previous %<if%>");
11248 /* Parse the statement. */
11249 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11253 /* Return true if this is the C++20 version of range-based-for with
11257 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11261 /* Save tokens so that we can put them back. */
11262 cp_lexer_save_tokens (parser
->lexer
);
11264 /* There has to be an unnested ; followed by an unnested :. */
11265 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11266 /*recovering=*/false,
11268 /*consume_paren=*/false) != -1)
11271 /* We found the semicolon, eat it now. */
11272 cp_lexer_consume_token (parser
->lexer
);
11274 /* Now look for ':' that is not nested in () or {}. */
11275 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11276 /*recovering=*/false,
11278 /*consume_paren=*/false) == -1);
11281 /* Roll back the tokens we skipped. */
11282 cp_lexer_rollback_tokens (parser
->lexer
);
11287 /* Return true if we're looking at (init; cond), false otherwise. */
11290 cp_parser_init_statement_p (cp_parser
*parser
)
11292 /* Save tokens so that we can put them back. */
11293 cp_lexer_save_tokens (parser
->lexer
);
11295 /* Look for ';' that is not nested in () or {}. */
11296 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11297 /*recovering=*/false,
11299 /*consume_paren=*/false);
11301 /* Roll back the tokens we skipped. */
11302 cp_lexer_rollback_tokens (parser
->lexer
);
11307 /* Parse a selection-statement.
11309 selection-statement:
11310 if ( init-statement [opt] condition ) statement
11311 if ( init-statement [opt] condition ) statement else statement
11312 switch ( init-statement [opt] condition ) statement
11314 Returns the new IF_STMT or SWITCH_STMT.
11316 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11317 is a (possibly labeled) if statement which is not enclosed in
11318 braces and has an else clause. This is used to implement
11321 CHAIN is a vector of if-else-if conditions. This is used to implement
11322 -Wduplicated-cond. */
11325 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11330 token_indent_info guard_tinfo
;
11335 /* Peek at the next token. */
11336 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11337 guard_tinfo
= get_token_indent_info (token
);
11339 /* See what kind of keyword it is. */
11340 keyword
= token
->keyword
;
11350 if (keyword
== RID_IF
11351 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11355 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11356 if (cxx_dialect
< cxx17
&& !in_system_header_at (tok
->location
))
11357 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11358 "with -std=c++17 or -std=gnu++17");
11361 /* Look for the `('. */
11362 matching_parens parens
;
11363 if (!parens
.require_open (parser
))
11365 cp_parser_skip_to_end_of_statement (parser
);
11366 return error_mark_node
;
11369 /* Begin the selection-statement. */
11370 if (keyword
== RID_IF
)
11372 statement
= begin_if_stmt ();
11373 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11376 statement
= begin_switch_stmt ();
11378 /* Parse the optional init-statement. */
11379 if (cp_parser_init_statement_p (parser
))
11382 if (cxx_dialect
< cxx17
)
11383 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11384 "init-statement in selection statements only available "
11385 "with -std=c++17 or -std=gnu++17");
11386 cp_parser_init_statement (parser
, &decl
);
11389 /* Parse the condition. */
11390 condition
= cp_parser_condition (parser
);
11391 /* Look for the `)'. */
11392 if (!parens
.require_close (parser
))
11393 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11394 /*consume_paren=*/true);
11396 if (keyword
== RID_IF
)
11399 unsigned char in_statement
;
11401 /* Add the condition. */
11402 condition
= finish_if_stmt_cond (condition
, statement
);
11404 if (warn_duplicated_cond
)
11405 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11408 /* Parse the then-clause. */
11409 in_statement
= parser
->in_statement
;
11410 parser
->in_statement
|= IN_IF_STMT
;
11412 /* Outside a template, the non-selected branch of a constexpr
11413 if is a 'discarded statement', i.e. unevaluated. */
11414 bool was_discarded
= in_discarded_stmt
;
11415 bool discard_then
= (cx
&& !processing_template_decl
11416 && integer_zerop (condition
));
11419 in_discarded_stmt
= true;
11420 ++c_inhibit_evaluation_warnings
;
11423 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11426 parser
->in_statement
= in_statement
;
11428 finish_then_clause (statement
);
11432 THEN_CLAUSE (statement
) = NULL_TREE
;
11433 in_discarded_stmt
= was_discarded
;
11434 --c_inhibit_evaluation_warnings
;
11437 /* If the next token is `else', parse the else-clause. */
11438 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11441 bool discard_else
= (cx
&& !processing_template_decl
11442 && integer_nonzerop (condition
));
11445 in_discarded_stmt
= true;
11446 ++c_inhibit_evaluation_warnings
;
11450 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11451 /* Consume the `else' keyword. */
11452 cp_lexer_consume_token (parser
->lexer
);
11453 if (warn_duplicated_cond
)
11455 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11459 /* We've got "if (COND) else if (COND2)". Start
11460 the condition chain and add COND as the first
11462 chain
= new vec
<tree
> ();
11463 if (!CONSTANT_CLASS_P (condition
)
11464 && !TREE_SIDE_EFFECTS (condition
))
11466 /* Wrap it in a NOP_EXPR so that we can set the
11467 location of the condition. */
11468 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11470 SET_EXPR_LOCATION (e
, token
->location
);
11471 chain
->safe_push (e
);
11474 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11477 /* This is if-else without subsequent if. Zap the
11478 condition chain; we would have already warned at
11484 begin_else_clause (statement
);
11485 /* Parse the else-clause. */
11486 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11487 guard_tinfo
, chain
);
11489 finish_else_clause (statement
);
11491 /* If we are currently parsing a then-clause, then
11492 IF_P will not be NULL. We set it to true to
11493 indicate that this if statement has an else clause.
11494 This may trigger the Wparentheses warning below
11495 when we get back up to the parent if statement. */
11501 ELSE_CLAUSE (statement
) = NULL_TREE
;
11502 in_discarded_stmt
= was_discarded
;
11503 --c_inhibit_evaluation_warnings
;
11508 /* This if statement does not have an else clause. If
11509 NESTED_IF is true, then the then-clause has an if
11510 statement which does have an else clause. We warn
11511 about the potential ambiguity. */
11513 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
11514 "suggest explicit braces to avoid ambiguous"
11516 if (warn_duplicated_cond
)
11518 /* We don't need the condition chain anymore. */
11524 /* Now we're all done with the if-statement. */
11525 finish_if_stmt (statement
);
11529 bool in_switch_statement_p
;
11530 unsigned char in_statement
;
11532 /* Add the condition. */
11533 finish_switch_cond (condition
, statement
);
11535 /* Parse the body of the switch-statement. */
11536 in_switch_statement_p
= parser
->in_switch_statement_p
;
11537 in_statement
= parser
->in_statement
;
11538 parser
->in_switch_statement_p
= true;
11539 parser
->in_statement
|= IN_SWITCH_STMT
;
11540 cp_parser_implicitly_scoped_statement (parser
, if_p
,
11542 parser
->in_switch_statement_p
= in_switch_statement_p
;
11543 parser
->in_statement
= in_statement
;
11545 /* Now we're all done with the switch-statement. */
11546 finish_switch_stmt (statement
);
11554 cp_parser_error (parser
, "expected selection-statement");
11555 return error_mark_node
;
11559 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11560 If we have seen at least one decl-specifier, and the next token
11561 is not a parenthesis, then we must be looking at a declaration.
11562 (After "int (" we might be looking at a functional cast.) */
11565 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
11566 bool any_specifiers_p
)
11568 if (any_specifiers_p
11569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11570 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11571 && !cp_parser_error_occurred (parser
))
11572 cp_parser_commit_to_tentative_parse (parser
);
11575 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11576 The declarator shall not specify a function or an array. Returns
11577 TRUE if the declarator is valid, FALSE otherwise. */
11580 cp_parser_check_condition_declarator (cp_parser
* parser
,
11581 cp_declarator
*declarator
,
11584 if (declarator
== cp_error_declarator
11585 || function_declarator_p (declarator
)
11586 || declarator
->kind
== cdk_array
)
11588 if (declarator
== cp_error_declarator
)
11589 /* Already complained. */;
11590 else if (declarator
->kind
== cdk_array
)
11591 error_at (loc
, "condition declares an array");
11593 error_at (loc
, "condition declares a function");
11594 if (parser
->fully_implicit_function_template_p
)
11595 abort_fully_implicit_template (parser
);
11596 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
11597 /*or_comma=*/false,
11598 /*consume_paren=*/false);
11605 /* Parse a condition.
11609 type-specifier-seq declarator = initializer-clause
11610 type-specifier-seq declarator braced-init-list
11615 type-specifier-seq declarator asm-specification [opt]
11616 attributes [opt] = assignment-expression
11618 Returns the expression that should be tested. */
11621 cp_parser_condition (cp_parser
* parser
)
11623 cp_decl_specifier_seq type_specifiers
;
11624 const char *saved_message
;
11625 int declares_class_or_enum
;
11627 /* Try the declaration first. */
11628 cp_parser_parse_tentatively (parser
);
11629 /* New types are not allowed in the type-specifier-seq for a
11631 saved_message
= parser
->type_definition_forbidden_message
;
11632 parser
->type_definition_forbidden_message
11633 = G_("types may not be defined in conditions");
11634 /* Parse the type-specifier-seq. */
11635 cp_parser_decl_specifier_seq (parser
,
11636 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11638 &declares_class_or_enum
);
11639 /* Restore the saved message. */
11640 parser
->type_definition_forbidden_message
= saved_message
;
11642 cp_parser_maybe_commit_to_declaration (parser
,
11643 type_specifiers
.any_specifiers_p
);
11645 /* If all is well, we might be looking at a declaration. */
11646 if (!cp_parser_error_occurred (parser
))
11649 tree asm_specification
;
11651 cp_declarator
*declarator
;
11652 tree initializer
= NULL_TREE
;
11653 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11655 /* Parse the declarator. */
11656 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11657 /*ctor_dtor_or_conv_p=*/NULL
,
11658 /*parenthesized_p=*/NULL
,
11659 /*member_p=*/false,
11660 /*friend_p=*/false);
11661 /* Parse the attributes. */
11662 attributes
= cp_parser_attributes_opt (parser
);
11663 /* Parse the asm-specification. */
11664 asm_specification
= cp_parser_asm_specification_opt (parser
);
11665 /* If the next token is not an `=' or '{', then we might still be
11666 looking at an expression. For example:
11670 looks like a decl-specifier-seq and a declarator -- but then
11671 there is no `=', so this is an expression. */
11672 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11673 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11674 cp_parser_simulate_error (parser
);
11676 /* If we did see an `=' or '{', then we are looking at a declaration
11678 if (cp_parser_parse_definitely (parser
))
11681 bool non_constant_p
;
11682 int flags
= LOOKUP_ONLYCONVERTING
;
11684 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
11685 return error_mark_node
;
11687 /* Create the declaration. */
11688 decl
= start_decl (declarator
, &type_specifiers
,
11689 /*initialized_p=*/true,
11690 attributes
, /*prefix_attributes=*/NULL_TREE
,
11693 /* Parse the initializer. */
11694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11696 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11697 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11700 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
11702 /* Consume the `='. */
11703 cp_lexer_consume_token (parser
->lexer
);
11704 initializer
= cp_parser_initializer_clause (parser
,
11709 cp_parser_error (parser
, "expected initializer");
11710 initializer
= error_mark_node
;
11712 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11713 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11715 /* Process the initializer. */
11716 cp_finish_decl (decl
,
11717 initializer
, !non_constant_p
,
11722 pop_scope (pushed_scope
);
11724 return convert_from_reference (decl
);
11727 /* If we didn't even get past the declarator successfully, we are
11728 definitely not looking at a declaration. */
11730 cp_parser_abort_tentative_parse (parser
);
11732 /* Otherwise, we are looking at an expression. */
11733 return cp_parser_expression (parser
);
11736 /* Parses a for-statement or range-for-statement until the closing ')',
11740 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
11742 tree init
, scope
, decl
;
11745 /* Begin the for-statement. */
11746 scope
= begin_for_scope (&init
);
11748 /* Parse the initialization. */
11749 is_range_for
= cp_parser_init_statement (parser
, &decl
);
11752 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
);
11754 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
11758 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
11759 unsigned short unroll
)
11761 /* Normal for loop */
11762 tree condition
= NULL_TREE
;
11763 tree expression
= NULL_TREE
;
11766 stmt
= begin_for_stmt (scope
, init
);
11767 /* The init-statement has already been parsed in
11768 cp_parser_init_statement, so no work is needed here. */
11769 finish_init_stmt (stmt
);
11771 /* If there's a condition, process it. */
11772 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11773 condition
= cp_parser_condition (parser
);
11776 cp_parser_error (parser
, "missing loop condition in loop with "
11777 "%<GCC ivdep%> pragma");
11778 condition
= error_mark_node
;
11782 cp_parser_error (parser
, "missing loop condition in loop with "
11783 "%<GCC unroll%> pragma");
11784 condition
= error_mark_node
;
11786 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
11787 /* Look for the `;'. */
11788 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11790 /* If there's an expression, process it. */
11791 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11792 expression
= cp_parser_expression (parser
);
11793 finish_for_expr (expression
, stmt
);
11798 /* Tries to parse a range-based for-statement:
11801 decl-specifier-seq declarator : expression
11803 The decl-specifier-seq declarator and the `:' are already parsed by
11804 cp_parser_init_statement. If processing_template_decl it returns a
11805 newly created RANGE_FOR_STMT; if not, it is converted to a
11806 regular FOR_STMT. */
11809 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11810 bool ivdep
, unsigned short unroll
)
11812 tree stmt
, range_expr
;
11813 auto_vec
<cxx_binding
*, 16> bindings
;
11814 auto_vec
<tree
, 16> names
;
11815 tree decomp_first_name
= NULL_TREE
;
11816 unsigned int decomp_cnt
= 0;
11818 /* Get the range declaration momentarily out of the way so that
11819 the range expression doesn't clash with it. */
11820 if (range_decl
!= error_mark_node
)
11822 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
11824 tree v
= DECL_VALUE_EXPR (range_decl
);
11825 /* For decomposition declaration get all of the corresponding
11826 declarations out of the way. */
11827 if (TREE_CODE (v
) == ARRAY_REF
11828 && VAR_P (TREE_OPERAND (v
, 0))
11829 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
11831 tree d
= range_decl
;
11832 range_decl
= TREE_OPERAND (v
, 0);
11833 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
11834 decomp_first_name
= d
;
11835 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
11837 tree name
= DECL_NAME (d
);
11838 names
.safe_push (name
);
11839 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11840 IDENTIFIER_BINDING (name
)
11841 = IDENTIFIER_BINDING (name
)->previous
;
11845 if (names
.is_empty ())
11847 tree name
= DECL_NAME (range_decl
);
11848 names
.safe_push (name
);
11849 bindings
.safe_push (IDENTIFIER_BINDING (name
));
11850 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
11854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11856 bool expr_non_constant_p
;
11857 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11860 range_expr
= cp_parser_expression (parser
);
11862 /* Put the range declaration(s) back into scope. */
11863 for (unsigned int i
= 0; i
< names
.length (); i
++)
11865 cxx_binding
*binding
= bindings
[i
];
11866 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
11867 IDENTIFIER_BINDING (names
[i
]) = binding
;
11870 /* If in template, STMT is converted to a normal for-statement
11871 at instantiation. If not, it is done just ahead. */
11872 if (processing_template_decl
)
11874 if (check_for_bare_parameter_packs (range_expr
))
11875 range_expr
= error_mark_node
;
11876 stmt
= begin_range_for_stmt (scope
, init
);
11878 RANGE_FOR_IVDEP (stmt
) = 1;
11880 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
11881 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11882 if (!type_dependent_expression_p (range_expr
)
11883 /* do_auto_deduction doesn't mess with template init-lists. */
11884 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11885 do_range_for_auto_deduction (range_decl
, range_expr
);
11889 stmt
= begin_for_stmt (scope
, init
);
11890 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
11891 decomp_first_name
, decomp_cnt
, ivdep
,
11897 /* Subroutine of cp_convert_range_for: given the initializer expression,
11898 builds up the range temporary. */
11901 build_range_temp (tree range_expr
)
11903 tree range_type
, range_temp
;
11905 /* Find out the type deduced by the declaration
11906 `auto &&__range = range_expr'. */
11907 range_type
= cp_build_reference_type (make_auto (), true);
11908 range_type
= do_auto_deduction (range_type
, range_expr
,
11909 type_uses_auto (range_type
));
11911 /* Create the __range variable. */
11912 range_temp
= build_decl (input_location
, VAR_DECL
,
11913 get_identifier ("__for_range"), range_type
);
11914 TREE_USED (range_temp
) = 1;
11915 DECL_ARTIFICIAL (range_temp
) = 1;
11920 /* Used by cp_parser_range_for in template context: we aren't going to
11921 do a full conversion yet, but we still need to resolve auto in the
11922 type of the for-range-declaration if present. This is basically
11923 a shortcut version of cp_convert_range_for. */
11926 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11928 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11931 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11932 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11933 iter_type
= (cp_parser_perform_range_for_lookup
11934 (range_temp
, &begin_dummy
, &end_dummy
));
11937 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11939 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
11941 tf_warning_or_error
);
11942 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11943 iter_decl
, auto_node
);
11948 /* Converts a range-based for-statement into a normal
11949 for-statement, as per the definition.
11951 for (RANGE_DECL : RANGE_EXPR)
11954 should be equivalent to:
11957 auto &&__range = RANGE_EXPR;
11958 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11962 RANGE_DECL = *__begin;
11967 If RANGE_EXPR is an array:
11968 BEGIN_EXPR = __range
11969 END_EXPR = __range + ARRAY_SIZE(__range)
11970 Else if RANGE_EXPR has a member 'begin' or 'end':
11971 BEGIN_EXPR = __range.begin()
11972 END_EXPR = __range.end()
11974 BEGIN_EXPR = begin(__range)
11975 END_EXPR = end(__range);
11977 If __range has a member 'begin' but not 'end', or vice versa, we must
11978 still use the second alternative (it will surely fail, however).
11979 When calling begin()/end() in the third alternative we must use
11980 argument dependent lookup, but always considering 'std' as an associated
11984 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11985 tree decomp_first_name
, unsigned int decomp_cnt
,
11986 bool ivdep
, unsigned short unroll
)
11989 tree iter_type
, begin_expr
, end_expr
;
11990 tree condition
, expression
;
11992 range_expr
= mark_lvalue_use (range_expr
);
11994 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11995 /* If an error happened previously do nothing or else a lot of
11996 unhelpful errors would be issued. */
11997 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12002 if (VAR_P (range_expr
)
12003 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12004 /* Can't bind a reference to an array of runtime bound. */
12005 range_temp
= range_expr
;
12008 range_temp
= build_range_temp (range_expr
);
12009 pushdecl (range_temp
);
12010 cp_finish_decl (range_temp
, range_expr
,
12011 /*is_constant_init*/false, NULL_TREE
,
12012 LOOKUP_ONLYCONVERTING
);
12013 range_temp
= convert_from_reference (range_temp
);
12015 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12016 &begin_expr
, &end_expr
);
12019 /* The new for initialization statement. */
12020 begin
= build_decl (input_location
, VAR_DECL
,
12021 get_identifier ("__for_begin"), iter_type
);
12022 TREE_USED (begin
) = 1;
12023 DECL_ARTIFICIAL (begin
) = 1;
12025 cp_finish_decl (begin
, begin_expr
,
12026 /*is_constant_init*/false, NULL_TREE
,
12027 LOOKUP_ONLYCONVERTING
);
12029 if (cxx_dialect
>= cxx17
)
12030 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12031 end
= build_decl (input_location
, VAR_DECL
,
12032 get_identifier ("__for_end"), iter_type
);
12033 TREE_USED (end
) = 1;
12034 DECL_ARTIFICIAL (end
) = 1;
12036 cp_finish_decl (end
, end_expr
,
12037 /*is_constant_init*/false, NULL_TREE
,
12038 LOOKUP_ONLYCONVERTING
);
12040 finish_init_stmt (statement
);
12042 /* The new for condition. */
12043 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12046 NULL
, tf_warning_or_error
);
12047 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12049 /* The new increment expression. */
12050 expression
= finish_unary_op_expr (input_location
,
12051 PREINCREMENT_EXPR
, begin
,
12052 tf_warning_or_error
);
12053 finish_for_expr (expression
, statement
);
12055 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12056 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12058 /* The declaration is initialized with *__begin inside the loop body. */
12059 cp_finish_decl (range_decl
,
12060 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12061 tf_warning_or_error
),
12062 /*is_constant_init*/false, NULL_TREE
,
12063 LOOKUP_ONLYCONVERTING
);
12064 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12065 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12070 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12071 We need to solve both at the same time because the method used
12072 depends on the existence of members begin or end.
12073 Returns the type deduced for the iterator expression. */
12076 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12078 if (error_operand_p (range
))
12080 *begin
= *end
= error_mark_node
;
12081 return error_mark_node
;
12084 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12086 error ("range-based %<for%> expression of type %qT "
12087 "has incomplete type", TREE_TYPE (range
));
12088 *begin
= *end
= error_mark_node
;
12089 return error_mark_node
;
12091 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12093 /* If RANGE is an array, we will use pointer arithmetic. */
12094 *begin
= decay_conversion (range
, tf_warning_or_error
);
12095 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12097 array_type_nelts_top (TREE_TYPE (range
)),
12099 return TREE_TYPE (*begin
);
12103 /* If it is not an array, we must do a bit of magic. */
12104 tree id_begin
, id_end
;
12105 tree member_begin
, member_end
;
12107 *begin
= *end
= error_mark_node
;
12109 id_begin
= get_identifier ("begin");
12110 id_end
= get_identifier ("end");
12111 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12112 /*protect=*/2, /*want_type=*/false,
12113 tf_warning_or_error
);
12114 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12115 /*protect=*/2, /*want_type=*/false,
12116 tf_warning_or_error
);
12118 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12120 /* Use the member functions. */
12121 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12122 *end
= cp_parser_range_for_member_function (range
, id_end
);
12126 /* Use global functions with ADL. */
12127 vec
<tree
, va_gc
> *vec
;
12128 vec
= make_tree_vector ();
12130 vec_safe_push (vec
, range
);
12132 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12133 tf_warning_or_error
);
12134 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12135 tf_warning_or_error
);
12136 member_end
= perform_koenig_lookup (id_end
, vec
,
12137 tf_warning_or_error
);
12138 *end
= finish_call_expr (member_end
, &vec
, false, true,
12139 tf_warning_or_error
);
12141 release_tree_vector (vec
);
12144 /* Last common checks. */
12145 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12147 /* If one of the expressions is an error do no more checks. */
12148 *begin
= *end
= error_mark_node
;
12149 return error_mark_node
;
12151 else if (type_dependent_expression_p (*begin
)
12152 || type_dependent_expression_p (*end
))
12153 /* Can happen, when, eg, in a template context, Koenig lookup
12154 can't resolve begin/end (c++/58503). */
12158 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12159 /* The unqualified type of the __begin and __end temporaries should
12160 be the same, as required by the multiple auto declaration. */
12161 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12163 if (cxx_dialect
>= cxx17
12164 && (build_x_binary_op (input_location
, NE_EXPR
,
12165 *begin
, ERROR_MARK
,
12168 != error_mark_node
))
12169 /* P0184R0 allows __begin and __end to have different types,
12170 but make sure they are comparable so we can give a better
12173 error ("inconsistent begin/end types in range-based %<for%> "
12174 "statement: %qT and %qT",
12175 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12182 /* Helper function for cp_parser_perform_range_for_lookup.
12183 Builds a tree for RANGE.IDENTIFIER(). */
12186 cp_parser_range_for_member_function (tree range
, tree identifier
)
12189 vec
<tree
, va_gc
> *vec
;
12191 member
= finish_class_member_access_expr (range
, identifier
,
12192 false, tf_warning_or_error
);
12193 if (member
== error_mark_node
)
12194 return error_mark_node
;
12196 vec
= make_tree_vector ();
12197 res
= finish_call_expr (member
, &vec
,
12198 /*disallow_virtual=*/false,
12199 /*koenig_p=*/false,
12200 tf_warning_or_error
);
12201 release_tree_vector (vec
);
12205 /* Parse an iteration-statement.
12207 iteration-statement:
12208 while ( condition ) statement
12209 do statement while ( expression ) ;
12210 for ( init-statement condition [opt] ; expression [opt] )
12213 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12216 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12217 unsigned short unroll
)
12222 unsigned char in_statement
;
12223 token_indent_info guard_tinfo
;
12225 /* Peek at the next token. */
12226 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12228 return error_mark_node
;
12230 guard_tinfo
= get_token_indent_info (token
);
12232 /* Remember whether or not we are already within an iteration
12234 in_statement
= parser
->in_statement
;
12236 /* See what kind of keyword it is. */
12237 keyword
= token
->keyword
;
12244 /* Begin the while-statement. */
12245 statement
= begin_while_stmt ();
12246 /* Look for the `('. */
12247 matching_parens parens
;
12248 parens
.require_open (parser
);
12249 /* Parse the condition. */
12250 condition
= cp_parser_condition (parser
);
12251 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12252 /* Look for the `)'. */
12253 parens
.require_close (parser
);
12254 /* Parse the dependent statement. */
12255 parser
->in_statement
= IN_ITERATION_STMT
;
12256 bool prev
= note_iteration_stmt_body_start ();
12257 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12258 note_iteration_stmt_body_end (prev
);
12259 parser
->in_statement
= in_statement
;
12260 /* We're done with the while-statement. */
12261 finish_while_stmt (statement
);
12269 /* Begin the do-statement. */
12270 statement
= begin_do_stmt ();
12271 /* Parse the body of the do-statement. */
12272 parser
->in_statement
= IN_ITERATION_STMT
;
12273 bool prev
= note_iteration_stmt_body_start ();
12274 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12275 note_iteration_stmt_body_end (prev
);
12276 parser
->in_statement
= in_statement
;
12277 finish_do_body (statement
);
12278 /* Look for the `while' keyword. */
12279 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12280 /* Look for the `('. */
12281 matching_parens parens
;
12282 parens
.require_open (parser
);
12283 /* Parse the expression. */
12284 expression
= cp_parser_expression (parser
);
12285 /* We're done with the do-statement. */
12286 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12287 /* Look for the `)'. */
12288 parens
.require_close (parser
);
12289 /* Look for the `;'. */
12290 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12296 /* Look for the `('. */
12297 matching_parens parens
;
12298 parens
.require_open (parser
);
12300 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12302 /* Look for the `)'. */
12303 parens
.require_close (parser
);
12305 /* Parse the body of the for-statement. */
12306 parser
->in_statement
= IN_ITERATION_STMT
;
12307 bool prev
= note_iteration_stmt_body_start ();
12308 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12309 note_iteration_stmt_body_end (prev
);
12310 parser
->in_statement
= in_statement
;
12312 /* We're done with the for-statement. */
12313 finish_for_stmt (statement
);
12318 cp_parser_error (parser
, "expected iteration-statement");
12319 statement
= error_mark_node
;
12326 /* Parse a init-statement or the declarator of a range-based-for.
12327 Returns true if a range-based-for declaration is seen.
12330 expression-statement
12331 simple-declaration */
12334 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12336 /* If the next token is a `;', then we have an empty
12337 expression-statement. Grammatically, this is also a
12338 simple-declaration, but an invalid one, because it does not
12339 declare anything. Therefore, if we did not handle this case
12340 specially, we would issue an error message about an invalid
12342 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12344 bool is_range_for
= false;
12345 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12347 /* Try to parse the init-statement. */
12348 if (cp_parser_range_based_for_with_init_p (parser
))
12351 cp_parser_parse_tentatively (parser
);
12352 /* Parse the declaration. */
12353 cp_parser_simple_declaration (parser
,
12354 /*function_definition_allowed_p=*/false,
12356 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12357 if (!cp_parser_parse_definitely (parser
))
12358 /* That didn't work, try to parse it as an expression-statement. */
12359 cp_parser_expression_statement (parser
, NULL_TREE
);
12361 if (cxx_dialect
< cxx2a
)
12363 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12364 "range-based %<for%> loops with initializer only "
12365 "available with -std=c++2a or -std=gnu++2a");
12366 *decl
= error_mark_node
;
12370 /* A colon is used in range-based for. */
12371 parser
->colon_corrects_to_scope_p
= false;
12373 /* We're going to speculatively look for a declaration, falling back
12374 to an expression, if necessary. */
12375 cp_parser_parse_tentatively (parser
);
12376 /* Parse the declaration. */
12377 cp_parser_simple_declaration (parser
,
12378 /*function_definition_allowed_p=*/false,
12380 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12381 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12383 /* It is a range-for, consume the ':'. */
12384 cp_lexer_consume_token (parser
->lexer
);
12385 is_range_for
= true;
12386 if (cxx_dialect
< cxx11
)
12388 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12389 "range-based %<for%> loops only available with "
12390 "-std=c++11 or -std=gnu++11");
12391 *decl
= error_mark_node
;
12395 /* The ';' is not consumed yet because we told
12396 cp_parser_simple_declaration not to. */
12397 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12399 if (cp_parser_parse_definitely (parser
))
12400 return is_range_for
;
12401 /* If the tentative parse failed, then we shall need to look for an
12402 expression-statement. */
12404 /* If we are here, it is an expression-statement. */
12405 cp_parser_expression_statement (parser
, NULL_TREE
);
12409 /* Parse a jump-statement.
12414 return expression [opt] ;
12415 return braced-init-list ;
12421 goto * expression ;
12423 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12426 cp_parser_jump_statement (cp_parser
* parser
)
12428 tree statement
= error_mark_node
;
12431 unsigned char in_statement
;
12433 /* Peek at the next token. */
12434 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12436 return error_mark_node
;
12438 /* See what kind of keyword it is. */
12439 keyword
= token
->keyword
;
12443 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12444 switch (in_statement
)
12447 error_at (token
->location
, "break statement not within loop or switch");
12450 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12451 || in_statement
== IN_ITERATION_STMT
);
12452 statement
= finish_break_stmt ();
12453 if (in_statement
== IN_ITERATION_STMT
)
12454 break_maybe_infinite_loop ();
12457 error_at (token
->location
, "invalid exit from OpenMP structured block");
12460 error_at (token
->location
, "break statement used with OpenMP for loop");
12463 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12467 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12470 error_at (token
->location
, "continue statement not within a loop");
12472 /* Fall through. */
12473 case IN_ITERATION_STMT
:
12475 statement
= finish_continue_stmt ();
12478 error_at (token
->location
, "invalid exit from OpenMP structured block");
12481 gcc_unreachable ();
12483 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12489 bool expr_non_constant_p
;
12491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12493 cp_lexer_set_source_position (parser
->lexer
);
12494 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12495 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12497 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12498 expr
= cp_parser_expression (parser
);
12500 /* If the next token is a `;', then there is no
12503 /* Build the return-statement. */
12504 if (current_function_auto_return_pattern
&& in_discarded_stmt
)
12505 /* Don't deduce from a discarded return statement. */;
12507 statement
= finish_return_stmt (expr
);
12508 /* Look for the final `;'. */
12509 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12514 if (parser
->in_function_body
12515 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
12517 error ("%<goto%> in %<constexpr%> function");
12518 cp_function_chain
->invalid_constexpr
= true;
12521 /* Create the goto-statement. */
12522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
12524 /* Issue a warning about this use of a GNU extension. */
12525 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
12526 /* Consume the '*' token. */
12527 cp_lexer_consume_token (parser
->lexer
);
12528 /* Parse the dependent expression. */
12529 finish_goto_stmt (cp_parser_expression (parser
));
12532 finish_goto_stmt (cp_parser_identifier (parser
));
12533 /* Look for the final `;'. */
12534 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12538 cp_parser_error (parser
, "expected jump-statement");
12545 /* Parse a declaration-statement.
12547 declaration-statement:
12548 block-declaration */
12551 cp_parser_declaration_statement (cp_parser
* parser
)
12555 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12556 p
= obstack_alloc (&declarator_obstack
, 0);
12558 /* Parse the block-declaration. */
12559 cp_parser_block_declaration (parser
, /*statement_p=*/true);
12561 /* Free any declarators allocated. */
12562 obstack_free (&declarator_obstack
, p
);
12565 /* Some dependent statements (like `if (cond) statement'), are
12566 implicitly in their own scope. In other words, if the statement is
12567 a single statement (as opposed to a compound-statement), it is
12568 none-the-less treated as if it were enclosed in braces. Any
12569 declarations appearing in the dependent statement are out of scope
12570 after control passes that point. This function parses a statement,
12571 but ensures that is in its own scope, even if it is not a
12572 compound-statement.
12574 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12575 is a (possibly labeled) if statement which is not enclosed in
12576 braces and has an else clause. This is used to implement
12579 CHAIN is a vector of if-else-if conditions. This is used to implement
12582 Returns the new statement. */
12585 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
12586 const token_indent_info
&guard_tinfo
,
12590 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12591 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
12592 token_indent_info body_tinfo
12593 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12598 /* Mark if () ; with a special NOP_EXPR. */
12599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12601 cp_lexer_consume_token (parser
->lexer
);
12602 statement
= add_stmt (build_empty_stmt (body_loc
));
12604 if (guard_tinfo
.keyword
== RID_IF
12605 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
12606 warning_at (body_loc
, OPT_Wempty_body
,
12607 "suggest braces around empty body in an %<if%> statement");
12608 else if (guard_tinfo
.keyword
== RID_ELSE
)
12609 warning_at (body_loc
, OPT_Wempty_body
,
12610 "suggest braces around empty body in an %<else%> statement");
12612 /* if a compound is opened, we simply parse the statement directly. */
12613 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12614 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
12615 /* If the token is not a `{', then we must take special action. */
12618 /* Create a compound-statement. */
12619 statement
= begin_compound_stmt (0);
12620 /* Parse the dependent-statement. */
12621 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
12622 &body_loc_after_labels
);
12623 /* Finish the dummy compound-statement. */
12624 finish_compound_stmt (statement
);
12627 token_indent_info next_tinfo
12628 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12629 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12631 if (body_loc_after_labels
!= UNKNOWN_LOCATION
12632 && next_tinfo
.type
!= CPP_SEMICOLON
)
12633 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
12634 guard_tinfo
.location
, guard_tinfo
.keyword
);
12636 /* Return the statement. */
12640 /* For some dependent statements (like `while (cond) statement'), we
12641 have already created a scope. Therefore, even if the dependent
12642 statement is a compound-statement, we do not want to create another
12646 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
12647 const token_indent_info
&guard_tinfo
)
12649 /* If the token is a `{', then we must take special action. */
12650 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12652 token_indent_info body_tinfo
12653 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12654 location_t loc_after_labels
= UNKNOWN_LOCATION
;
12656 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
12657 &loc_after_labels
);
12658 token_indent_info next_tinfo
12659 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12660 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
12662 if (loc_after_labels
!= UNKNOWN_LOCATION
12663 && next_tinfo
.type
!= CPP_SEMICOLON
)
12664 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
12665 guard_tinfo
.location
,
12666 guard_tinfo
.keyword
);
12670 /* Avoid calling cp_parser_compound_statement, so that we
12671 don't create a new scope. Do everything else by hand. */
12672 matching_braces braces
;
12673 braces
.require_open (parser
);
12674 /* If the next keyword is `__label__' we have a label declaration. */
12675 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12676 cp_parser_label_declaration (parser
);
12677 /* Parse an (optional) statement-seq. */
12678 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
12679 braces
.require_close (parser
);
12683 /* Declarations [gram.dcl.dcl] */
12685 /* Parse an optional declaration-sequence.
12689 declaration-seq declaration */
12692 cp_parser_declaration_seq_opt (cp_parser
* parser
)
12698 token
= cp_lexer_peek_token (parser
->lexer
);
12700 if (token
->type
== CPP_CLOSE_BRACE
12701 || token
->type
== CPP_EOF
12702 || token
->type
== CPP_PRAGMA_EOL
)
12705 if (token
->type
== CPP_SEMICOLON
)
12707 /* A declaration consisting of a single semicolon is
12708 invalid. Allow it unless we're being pedantic. */
12709 cp_lexer_consume_token (parser
->lexer
);
12710 if (!in_system_header_at (input_location
))
12711 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
12715 /* If we're entering or exiting a region that's implicitly
12716 extern "C", modify the lang context appropriately. */
12717 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
12719 push_lang_context (lang_name_c
);
12720 parser
->implicit_extern_c
= true;
12722 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12724 pop_lang_context ();
12725 parser
->implicit_extern_c
= false;
12728 if (token
->type
== CPP_PRAGMA
)
12730 /* A top-level declaration can consist solely of a #pragma.
12731 A nested declaration cannot, so this is done here and not
12732 in cp_parser_declaration. (A #pragma at block scope is
12733 handled in cp_parser_statement.) */
12734 cp_parser_pragma (parser
, pragma_external
, NULL
);
12738 /* Parse the declaration itself. */
12739 cp_parser_declaration (parser
);
12743 /* Parse a declaration.
12747 function-definition
12748 template-declaration
12749 explicit-instantiation
12750 explicit-specialization
12751 linkage-specification
12752 namespace-definition
12760 __extension__ declaration */
12763 cp_parser_declaration (cp_parser
* parser
)
12767 int saved_pedantic
;
12769 tree attributes
= NULL_TREE
;
12771 /* Check for the `__extension__' keyword. */
12772 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12774 /* Parse the qualified declaration. */
12775 cp_parser_declaration (parser
);
12776 /* Restore the PEDANTIC flag. */
12777 pedantic
= saved_pedantic
;
12782 /* Try to figure out what kind of declaration is present. */
12783 token1
= *cp_lexer_peek_token (parser
->lexer
);
12785 if (token1
.type
!= CPP_EOF
)
12786 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12789 token2
.type
= CPP_EOF
;
12790 token2
.keyword
= RID_MAX
;
12793 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12794 p
= obstack_alloc (&declarator_obstack
, 0);
12796 /* If the next token is `extern' and the following token is a string
12797 literal, then we have a linkage specification. */
12798 if (token1
.keyword
== RID_EXTERN
12799 && cp_parser_is_pure_string_literal (&token2
))
12800 cp_parser_linkage_specification (parser
);
12801 /* If the next token is `template', then we have either a template
12802 declaration, an explicit instantiation, or an explicit
12804 else if (token1
.keyword
== RID_TEMPLATE
)
12806 /* `template <>' indicates a template specialization. */
12807 if (token2
.type
== CPP_LESS
12808 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12809 cp_parser_explicit_specialization (parser
);
12810 /* `template <' indicates a template declaration. */
12811 else if (token2
.type
== CPP_LESS
)
12812 cp_parser_template_declaration (parser
, /*member_p=*/false);
12813 /* Anything else must be an explicit instantiation. */
12815 cp_parser_explicit_instantiation (parser
);
12817 /* If the next token is `export', then we have a template
12819 else if (token1
.keyword
== RID_EXPORT
)
12820 cp_parser_template_declaration (parser
, /*member_p=*/false);
12821 /* If the next token is `extern', 'static' or 'inline' and the one
12822 after that is `template', we have a GNU extended explicit
12823 instantiation directive. */
12824 else if (cp_parser_allow_gnu_extensions_p (parser
)
12825 && (token1
.keyword
== RID_EXTERN
12826 || token1
.keyword
== RID_STATIC
12827 || token1
.keyword
== RID_INLINE
)
12828 && token2
.keyword
== RID_TEMPLATE
)
12829 cp_parser_explicit_instantiation (parser
);
12830 /* If the next token is `namespace', check for a named or unnamed
12831 namespace definition. */
12832 else if (token1
.keyword
== RID_NAMESPACE
12833 && (/* A named namespace definition. */
12834 (token2
.type
== CPP_NAME
12835 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12837 || (token2
.type
== CPP_OPEN_SQUARE
12838 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12839 == CPP_OPEN_SQUARE
)
12840 /* An unnamed namespace definition. */
12841 || token2
.type
== CPP_OPEN_BRACE
12842 || token2
.keyword
== RID_ATTRIBUTE
))
12843 cp_parser_namespace_definition (parser
);
12844 /* An inline (associated) namespace definition. */
12845 else if (token1
.keyword
== RID_INLINE
12846 && token2
.keyword
== RID_NAMESPACE
)
12847 cp_parser_namespace_definition (parser
);
12848 /* Objective-C++ declaration/definition. */
12849 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12850 cp_parser_objc_declaration (parser
, NULL_TREE
);
12851 else if (c_dialect_objc ()
12852 && token1
.keyword
== RID_ATTRIBUTE
12853 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12854 cp_parser_objc_declaration (parser
, attributes
);
12855 /* At this point we may have a template declared by a concept
12857 else if (flag_concepts
12858 && cp_parser_template_declaration_after_export (parser
,
12859 /*member_p=*/false))
12862 /* Try to parse a block-declaration, or a function-definition. */
12863 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12865 /* Free any declarators allocated. */
12866 obstack_free (&declarator_obstack
, p
);
12869 /* Parse a block-declaration.
12874 namespace-alias-definition
12881 __extension__ block-declaration
12886 static_assert-declaration
12888 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12889 part of a declaration-statement. */
12892 cp_parser_block_declaration (cp_parser
*parser
,
12896 int saved_pedantic
;
12898 /* Check for the `__extension__' keyword. */
12899 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12901 /* Parse the qualified declaration. */
12902 cp_parser_block_declaration (parser
, statement_p
);
12903 /* Restore the PEDANTIC flag. */
12904 pedantic
= saved_pedantic
;
12909 /* Peek at the next token to figure out which kind of declaration is
12911 token1
= cp_lexer_peek_token (parser
->lexer
);
12913 /* If the next keyword is `asm', we have an asm-definition. */
12914 if (token1
->keyword
== RID_ASM
)
12917 cp_parser_commit_to_tentative_parse (parser
);
12918 cp_parser_asm_definition (parser
);
12920 /* If the next keyword is `namespace', we have a
12921 namespace-alias-definition. */
12922 else if (token1
->keyword
== RID_NAMESPACE
)
12923 cp_parser_namespace_alias_definition (parser
);
12924 /* If the next keyword is `using', we have a
12925 using-declaration, a using-directive, or an alias-declaration. */
12926 else if (token1
->keyword
== RID_USING
)
12931 cp_parser_commit_to_tentative_parse (parser
);
12932 /* If the token after `using' is `namespace', then we have a
12933 using-directive. */
12934 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12935 if (token2
->keyword
== RID_NAMESPACE
)
12936 cp_parser_using_directive (parser
);
12937 /* If the second token after 'using' is '=', then we have an
12938 alias-declaration. */
12939 else if (cxx_dialect
>= cxx11
12940 && token2
->type
== CPP_NAME
12941 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12942 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12943 cp_parser_alias_declaration (parser
);
12944 /* Otherwise, it's a using-declaration. */
12946 cp_parser_using_declaration (parser
,
12947 /*access_declaration_p=*/false);
12949 /* If the next keyword is `__label__' we have a misplaced label
12951 else if (token1
->keyword
== RID_LABEL
)
12953 cp_lexer_consume_token (parser
->lexer
);
12954 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12955 cp_parser_skip_to_end_of_statement (parser
);
12956 /* If the next token is now a `;', consume it. */
12957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12958 cp_lexer_consume_token (parser
->lexer
);
12960 /* If the next token is `static_assert' we have a static assertion. */
12961 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12962 cp_parser_static_assert (parser
, /*member_p=*/false);
12963 /* Anything else must be a simple-declaration. */
12965 cp_parser_simple_declaration (parser
, !statement_p
,
12966 /*maybe_range_for_decl*/NULL
);
12969 /* Parse a simple-declaration.
12971 simple-declaration:
12972 decl-specifier-seq [opt] init-declarator-list [opt] ;
12973 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12974 brace-or-equal-initializer ;
12976 init-declarator-list:
12978 init-declarator-list , init-declarator
12980 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12981 function-definition as a simple-declaration.
12983 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12984 parsed declaration if it is an uninitialized single declarator not followed
12985 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12986 if present, will not be consumed. */
12989 cp_parser_simple_declaration (cp_parser
* parser
,
12990 bool function_definition_allowed_p
,
12991 tree
*maybe_range_for_decl
)
12993 cp_decl_specifier_seq decl_specifiers
;
12994 int declares_class_or_enum
;
12995 bool saw_declarator
;
12996 location_t comma_loc
= UNKNOWN_LOCATION
;
12997 location_t init_loc
= UNKNOWN_LOCATION
;
12999 if (maybe_range_for_decl
)
13000 *maybe_range_for_decl
= NULL_TREE
;
13002 /* Defer access checks until we know what is being declared; the
13003 checks for names appearing in the decl-specifier-seq should be
13004 done as if we were in the scope of the thing being declared. */
13005 push_deferring_access_checks (dk_deferred
);
13007 /* Parse the decl-specifier-seq. We have to keep track of whether
13008 or not the decl-specifier-seq declares a named class or
13009 enumeration type, since that is the only case in which the
13010 init-declarator-list is allowed to be empty.
13014 In a simple-declaration, the optional init-declarator-list can be
13015 omitted only when declaring a class or enumeration, that is when
13016 the decl-specifier-seq contains either a class-specifier, an
13017 elaborated-type-specifier, or an enum-specifier. */
13018 cp_parser_decl_specifier_seq (parser
,
13019 CP_PARSER_FLAGS_OPTIONAL
,
13021 &declares_class_or_enum
);
13022 /* We no longer need to defer access checks. */
13023 stop_deferring_access_checks ();
13025 /* In a block scope, a valid declaration must always have a
13026 decl-specifier-seq. By not trying to parse declarators, we can
13027 resolve the declaration/expression ambiguity more quickly. */
13028 if (!function_definition_allowed_p
13029 && !decl_specifiers
.any_specifiers_p
)
13031 cp_parser_error (parser
, "expected declaration");
13035 /* If the next two tokens are both identifiers, the code is
13036 erroneous. The usual cause of this situation is code like:
13040 where "T" should name a type -- but does not. */
13041 if (!decl_specifiers
.any_type_specifiers_p
13042 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13044 /* If parsing tentatively, we should commit; we really are
13045 looking at a declaration. */
13046 cp_parser_commit_to_tentative_parse (parser
);
13051 cp_parser_maybe_commit_to_declaration (parser
,
13052 decl_specifiers
.any_specifiers_p
);
13054 /* Look for C++17 decomposition declaration. */
13055 for (size_t n
= 1; ; n
++)
13056 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13057 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13059 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13060 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13061 && decl_specifiers
.any_specifiers_p
)
13064 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13065 maybe_range_for_decl
,
13068 /* The next token should be either a `,' or a `;'. */
13069 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13070 /* If it's a `;', we are done. */
13071 if (token
->type
== CPP_SEMICOLON
)
13073 else if (maybe_range_for_decl
)
13075 if (*maybe_range_for_decl
== NULL_TREE
)
13076 *maybe_range_for_decl
= error_mark_node
;
13079 /* Anything else is an error. */
13082 /* If we have already issued an error message we don't need
13083 to issue another one. */
13084 if ((decl
!= error_mark_node
13085 && DECL_INITIAL (decl
) != error_mark_node
)
13086 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13087 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13088 /* Skip tokens until we reach the end of the statement. */
13089 cp_parser_skip_to_end_of_statement (parser
);
13090 /* If the next token is now a `;', consume it. */
13091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13092 cp_lexer_consume_token (parser
->lexer
);
13100 bool auto_specifier_p
;
13101 /* NULL_TREE if both variable and function declaration are allowed,
13102 error_mark_node if function declaration are not allowed and
13103 a FUNCTION_DECL that should be diagnosed if it is followed by
13104 variable declarations. */
13105 tree auto_function_declaration
;
13107 last_type
= NULL_TREE
;
13109 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13110 auto_function_declaration
= NULL_TREE
;
13112 /* Keep going until we hit the `;' at the end of the simple
13114 saw_declarator
= false;
13115 while (cp_lexer_next_token_is_not (parser
->lexer
,
13119 bool function_definition_p
;
13121 tree auto_result
= NULL_TREE
;
13123 if (saw_declarator
)
13125 /* If we are processing next declarator, comma is expected */
13126 token
= cp_lexer_peek_token (parser
->lexer
);
13127 gcc_assert (token
->type
== CPP_COMMA
);
13128 cp_lexer_consume_token (parser
->lexer
);
13129 if (maybe_range_for_decl
)
13131 *maybe_range_for_decl
= error_mark_node
;
13132 if (comma_loc
== UNKNOWN_LOCATION
)
13133 comma_loc
= token
->location
;
13137 saw_declarator
= true;
13139 /* Parse the init-declarator. */
13140 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
13142 function_definition_allowed_p
,
13143 /*member_p=*/false,
13144 declares_class_or_enum
,
13145 &function_definition_p
,
13146 maybe_range_for_decl
,
13149 /* If an error occurred while parsing tentatively, exit quickly.
13150 (That usually happens when in the body of a function; each
13151 statement is treated as a declaration-statement until proven
13153 if (cp_parser_error_occurred (parser
))
13156 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13158 /* If the init-declarator-list contains more than one
13159 init-declarator, they shall all form declarations of
13161 if (auto_function_declaration
== NULL_TREE
)
13162 auto_function_declaration
13163 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13164 else if (TREE_CODE (decl
) == FUNCTION_DECL
13165 || auto_function_declaration
!= error_mark_node
)
13167 error_at (decl_specifiers
.locations
[ds_type_spec
],
13168 "non-variable %qD in declaration with more than one "
13169 "declarator with placeholder type",
13170 TREE_CODE (decl
) == FUNCTION_DECL
13171 ? decl
: auto_function_declaration
);
13172 auto_function_declaration
= error_mark_node
;
13177 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13180 && last_type
!= error_mark_node
13181 && !same_type_p (auto_result
, last_type
))
13183 /* If the list of declarators contains more than one declarator,
13184 the type of each declared variable is determined as described
13185 above. If the type deduced for the template parameter U is not
13186 the same in each deduction, the program is ill-formed. */
13187 error_at (decl_specifiers
.locations
[ds_type_spec
],
13188 "inconsistent deduction for %qT: %qT and then %qT",
13189 decl_specifiers
.type
, last_type
, auto_result
);
13190 last_type
= error_mark_node
;
13193 last_type
= auto_result
;
13196 /* Handle function definitions specially. */
13197 if (function_definition_p
)
13199 /* If the next token is a `,', then we are probably
13200 processing something like:
13204 which is erroneous. */
13205 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13207 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13208 error_at (token
->location
,
13210 " declarations and function-definitions is forbidden");
13212 /* Otherwise, we're done with the list of declarators. */
13215 pop_deferring_access_checks ();
13219 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13220 *maybe_range_for_decl
= decl
;
13221 /* The next token should be either a `,' or a `;'. */
13222 token
= cp_lexer_peek_token (parser
->lexer
);
13223 /* If it's a `,', there are more declarators to come. */
13224 if (token
->type
== CPP_COMMA
)
13225 /* will be consumed next time around */;
13226 /* If it's a `;', we are done. */
13227 else if (token
->type
== CPP_SEMICOLON
)
13229 else if (maybe_range_for_decl
)
13231 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13232 permerror (decl_specifiers
.locations
[ds_type_spec
],
13233 "types may not be defined in a for-range-declaration");
13236 /* Anything else is an error. */
13239 /* If we have already issued an error message we don't need
13240 to issue another one. */
13241 if ((decl
!= error_mark_node
13242 && DECL_INITIAL (decl
) != error_mark_node
)
13243 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13244 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13245 /* Skip tokens until we reach the end of the statement. */
13246 cp_parser_skip_to_end_of_statement (parser
);
13247 /* If the next token is now a `;', consume it. */
13248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13249 cp_lexer_consume_token (parser
->lexer
);
13252 /* After the first time around, a function-definition is not
13253 allowed -- even if it was OK at first. For example:
13258 function_definition_allowed_p
= false;
13261 /* Issue an error message if no declarators are present, and the
13262 decl-specifier-seq does not itself declare a class or
13263 enumeration: [dcl.dcl]/3. */
13264 if (!saw_declarator
)
13266 if (cp_parser_declares_only_class_p (parser
))
13268 if (!declares_class_or_enum
13269 && decl_specifiers
.type
13270 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13271 /* Ensure an error is issued anyway when finish_decltype_type,
13272 called via cp_parser_decl_specifier_seq, returns a class or
13273 an enumeration (c++/51786). */
13274 decl_specifiers
.type
= NULL_TREE
;
13275 shadow_tag (&decl_specifiers
);
13277 /* Perform any deferred access checks. */
13278 perform_deferred_access_checks (tf_warning_or_error
);
13281 /* Consume the `;'. */
13283 if (!maybe_range_for_decl
)
13284 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13285 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13287 if (init_loc
!= UNKNOWN_LOCATION
)
13288 error_at (init_loc
, "initializer in range-based %<for%> loop");
13289 if (comma_loc
!= UNKNOWN_LOCATION
)
13290 error_at (comma_loc
,
13291 "multiple declarations in range-based %<for%> loop");
13295 pop_deferring_access_checks ();
13298 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13299 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13303 cp_parser_decomposition_declaration (cp_parser
*parser
,
13304 cp_decl_specifier_seq
*decl_specifiers
,
13305 tree
*maybe_range_for_decl
,
13306 location_t
*init_loc
)
13308 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13309 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13310 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13312 /* Parse the identifier-list. */
13313 auto_vec
<cp_expr
, 10> v
;
13314 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13317 cp_expr e
= cp_parser_identifier (parser
);
13318 if (e
.get_value () == error_mark_node
)
13321 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13323 cp_lexer_consume_token (parser
->lexer
);
13326 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13327 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13329 end_loc
= UNKNOWN_LOCATION
;
13330 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13333 cp_lexer_consume_token (parser
->lexer
);
13336 cp_parser_skip_to_end_of_statement (parser
);
13337 return error_mark_node
;
13341 if (cxx_dialect
< cxx17
)
13342 pedwarn (loc
, 0, "structured bindings only available with "
13343 "-std=c++17 or -std=gnu++17");
13346 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13347 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13348 declarator
->id_loc
= loc
;
13349 if (ref_qual
!= REF_QUAL_NONE
)
13350 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13351 ref_qual
== REF_QUAL_RVALUE
,
13353 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13354 NULL_TREE
, decl_specifiers
->attributes
,
13356 tree orig_decl
= decl
;
13360 cp_decl_specifier_seq decl_specs
;
13361 clear_decl_specs (&decl_specs
);
13362 decl_specs
.type
= make_auto ();
13364 FOR_EACH_VEC_ELT (v
, i
, e
)
13367 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (), sfk_none
);
13369 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13370 declarator
->id_loc
= e
.get_location ();
13371 tree elt_pushed_scope
;
13372 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13373 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13374 if (decl2
== error_mark_node
)
13375 decl
= error_mark_node
;
13376 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13378 /* Ensure we've diagnosed redeclaration if we aren't creating
13380 gcc_assert (errorcount
);
13381 decl
= error_mark_node
;
13385 if (elt_pushed_scope
)
13386 pop_scope (elt_pushed_scope
);
13391 error_at (loc
, "empty structured binding declaration");
13392 decl
= error_mark_node
;
13395 if (maybe_range_for_decl
== NULL
13396 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13398 bool non_constant_p
= false, is_direct_init
= false;
13399 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13400 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13402 if (initializer
== NULL_TREE
13403 || (TREE_CODE (initializer
) == TREE_LIST
13404 && TREE_CHAIN (initializer
))
13406 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13407 && CONSTRUCTOR_NELTS (initializer
) != 1))
13409 error_at (loc
, "invalid initializer for structured binding "
13411 initializer
= error_mark_node
;
13414 if (decl
!= error_mark_node
)
13416 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13417 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13418 is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
);
13419 cp_finish_decomp (decl
, prev
, v
.length ());
13422 else if (decl
!= error_mark_node
)
13424 *maybe_range_for_decl
= prev
;
13425 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13426 the underlying DECL. */
13427 cp_finish_decomp (decl
, prev
, v
.length ());
13431 pop_scope (pushed_scope
);
13433 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13435 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13436 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13442 /* Parse a decl-specifier-seq.
13444 decl-specifier-seq:
13445 decl-specifier-seq [opt] decl-specifier
13446 decl-specifier attribute-specifier-seq [opt] (C++11)
13449 storage-class-specifier
13460 Concepts Extension:
13465 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13467 The parser flags FLAGS is used to control type-specifier parsing.
13469 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13472 1: one of the decl-specifiers is an elaborated-type-specifier
13473 (i.e., a type declaration)
13474 2: one of the decl-specifiers is an enum-specifier or a
13475 class-specifier (i.e., a type definition)
13480 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13481 cp_parser_flags flags
,
13482 cp_decl_specifier_seq
*decl_specs
,
13483 int* declares_class_or_enum
)
13485 bool constructor_possible_p
= !parser
->in_declarator_p
;
13486 bool found_decl_spec
= false;
13487 cp_token
*start_token
= NULL
;
13490 /* Clear DECL_SPECS. */
13491 clear_decl_specs (decl_specs
);
13493 /* Assume no class or enumeration type is declared. */
13494 *declares_class_or_enum
= 0;
13496 /* Keep reading specifiers until there are no more to read. */
13499 bool constructor_p
;
13503 /* Peek at the next token. */
13504 token
= cp_lexer_peek_token (parser
->lexer
);
13506 /* Save the first token of the decl spec list for error
13509 start_token
= token
;
13510 /* Handle attributes. */
13511 if (cp_next_tokens_can_be_attribute_p (parser
))
13513 /* Parse the attributes. */
13514 tree attrs
= cp_parser_attributes_opt (parser
);
13516 /* In a sequence of declaration specifiers, c++11 attributes
13517 appertain to the type that precede them. In that case
13520 The attribute-specifier-seq affects the type only for
13521 the declaration it appears in, not other declarations
13522 involving the same type.
13524 But for now let's force the user to position the
13525 attribute either at the beginning of the declaration or
13526 after the declarator-id, which would clearly mean that it
13527 applies to the declarator. */
13528 if (cxx11_attribute_p (attrs
))
13530 if (!found_decl_spec
)
13531 /* The c++11 attribute is at the beginning of the
13532 declaration. It appertains to the entity being
13536 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
13538 /* This is an attribute following a
13539 class-specifier. */
13540 if (decl_specs
->type_definition_p
)
13541 warn_misplaced_attr_for_class_type (token
->location
,
13547 decl_specs
->std_attributes
13548 = attr_chainon (decl_specs
->std_attributes
, attrs
);
13549 if (decl_specs
->locations
[ds_std_attribute
] == 0)
13550 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
13556 decl_specs
->attributes
13557 = attr_chainon (decl_specs
->attributes
, attrs
);
13558 if (decl_specs
->locations
[ds_attribute
] == 0)
13559 decl_specs
->locations
[ds_attribute
] = token
->location
;
13562 /* Assume we will find a decl-specifier keyword. */
13563 found_decl_spec
= true;
13564 /* If the next token is an appropriate keyword, we can simply
13565 add it to the list. */
13566 switch (token
->keyword
)
13572 if (!at_class_scope_p ())
13574 gcc_rich_location
richloc (token
->location
);
13575 richloc
.add_fixit_remove ();
13576 error_at (&richloc
, "%<friend%> used outside of class");
13577 cp_lexer_purge_token (parser
->lexer
);
13582 /* Consume the token. */
13583 cp_lexer_consume_token (parser
->lexer
);
13587 case RID_CONSTEXPR
:
13589 cp_lexer_consume_token (parser
->lexer
);
13594 cp_lexer_consume_token (parser
->lexer
);
13597 /* function-specifier:
13604 cp_parser_function_specifier_opt (parser
, decl_specs
);
13611 /* Consume the token. */
13612 cp_lexer_consume_token (parser
->lexer
);
13613 /* A constructor declarator cannot appear in a typedef. */
13614 constructor_possible_p
= false;
13615 /* The "typedef" keyword can only occur in a declaration; we
13616 may as well commit at this point. */
13617 cp_parser_commit_to_tentative_parse (parser
);
13619 if (decl_specs
->storage_class
!= sc_none
)
13620 decl_specs
->conflicting_specifiers_p
= true;
13623 /* storage-class-specifier:
13633 if (cxx_dialect
== cxx98
)
13635 /* Consume the token. */
13636 cp_lexer_consume_token (parser
->lexer
);
13638 /* Complain about `auto' as a storage specifier, if
13639 we're complaining about C++0x compatibility. */
13640 gcc_rich_location
richloc (token
->location
);
13641 richloc
.add_fixit_remove ();
13642 warning_at (&richloc
, OPT_Wc__11_compat
,
13643 "%<auto%> changes meaning in C++11; "
13644 "please remove it");
13646 /* Set the storage class anyway. */
13647 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
13651 /* C++0x auto type-specifier. */
13652 found_decl_spec
= false;
13659 /* Consume the token. */
13660 cp_lexer_consume_token (parser
->lexer
);
13661 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
13665 /* Consume the token. */
13667 cp_lexer_consume_token (parser
->lexer
);
13671 /* We did not yet find a decl-specifier yet. */
13672 found_decl_spec
= false;
13676 if (found_decl_spec
13677 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
13678 && token
->keyword
!= RID_CONSTEXPR
)
13679 error ("decl-specifier invalid in condition");
13681 if (found_decl_spec
13682 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
13683 && token
->keyword
!= RID_MUTABLE
13684 && token
->keyword
!= RID_CONSTEXPR
)
13685 error_at (token
->location
, "%qD invalid in lambda",
13686 ridpointers
[token
->keyword
]);
13689 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
13691 /* Constructors are a special case. The `S' in `S()' is not a
13692 decl-specifier; it is the beginning of the declarator. */
13694 = (!found_decl_spec
13695 && constructor_possible_p
13696 && (cp_parser_constructor_declarator_p
13697 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
13699 /* If we don't have a DECL_SPEC yet, then we must be looking at
13700 a type-specifier. */
13701 if (!found_decl_spec
&& !constructor_p
)
13703 int decl_spec_declares_class_or_enum
;
13704 bool is_cv_qualifier
;
13708 = cp_parser_type_specifier (parser
, flags
,
13710 /*is_declaration=*/true,
13711 &decl_spec_declares_class_or_enum
,
13713 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
13715 /* If this type-specifier referenced a user-defined type
13716 (a typedef, class-name, etc.), then we can't allow any
13717 more such type-specifiers henceforth.
13721 The longest sequence of decl-specifiers that could
13722 possibly be a type name is taken as the
13723 decl-specifier-seq of a declaration. The sequence shall
13724 be self-consistent as described below.
13728 As a general rule, at most one type-specifier is allowed
13729 in the complete decl-specifier-seq of a declaration. The
13730 only exceptions are the following:
13732 -- const or volatile can be combined with any other
13735 -- signed or unsigned can be combined with char, long,
13743 void g (const int Pc);
13745 Here, Pc is *not* part of the decl-specifier seq; it's
13746 the declarator. Therefore, once we see a type-specifier
13747 (other than a cv-qualifier), we forbid any additional
13748 user-defined types. We *do* still allow things like `int
13749 int' to be considered a decl-specifier-seq, and issue the
13750 error message later. */
13751 if (type_spec
&& !is_cv_qualifier
)
13752 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
13753 /* A constructor declarator cannot follow a type-specifier. */
13756 constructor_possible_p
= false;
13757 found_decl_spec
= true;
13758 if (!is_cv_qualifier
)
13759 decl_specs
->any_type_specifiers_p
= true;
13763 /* If we still do not have a DECL_SPEC, then there are no more
13764 decl-specifiers. */
13765 if (!found_decl_spec
)
13768 decl_specs
->any_specifiers_p
= true;
13769 /* After we see one decl-specifier, further decl-specifiers are
13770 always optional. */
13771 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
13774 /* Don't allow a friend specifier with a class definition. */
13775 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
13776 && (*declares_class_or_enum
& 2))
13777 error_at (decl_specs
->locations
[ds_friend
],
13778 "class definition may not be declared a friend");
13781 /* Parse an (optional) storage-class-specifier.
13783 storage-class-specifier:
13792 storage-class-specifier:
13795 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13798 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
13800 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
13803 if (cxx_dialect
!= cxx98
)
13805 /* Fall through for C++98. */
13806 gcc_fallthrough ();
13813 /* Consume the token. */
13814 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13821 /* Parse an (optional) function-specifier.
13823 function-specifier:
13828 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13829 Updates DECL_SPECS, if it is non-NULL. */
13832 cp_parser_function_specifier_opt (cp_parser
* parser
,
13833 cp_decl_specifier_seq
*decl_specs
)
13835 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13836 switch (token
->keyword
)
13839 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
13843 /* 14.5.2.3 [temp.mem]
13845 A member function template shall not be virtual. */
13846 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13847 && current_class_type
)
13848 error_at (token
->location
, "templates may not be %<virtual%>");
13850 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
13854 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
13861 /* Consume the token. */
13862 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13865 /* Parse a linkage-specification.
13867 linkage-specification:
13868 extern string-literal { declaration-seq [opt] }
13869 extern string-literal declaration */
13872 cp_parser_linkage_specification (cp_parser
* parser
)
13876 /* Look for the `extern' keyword. */
13877 cp_token
*extern_token
13878 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
13880 /* Look for the string-literal. */
13881 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
13882 linkage
= cp_parser_string_literal (parser
, false, false);
13884 /* Transform the literal into an identifier. If the literal is a
13885 wide-character string, or contains embedded NULs, then we can't
13886 handle it as the user wants. */
13887 if (strlen (TREE_STRING_POINTER (linkage
))
13888 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
13890 cp_parser_error (parser
, "invalid linkage-specification");
13891 /* Assume C++ linkage. */
13892 linkage
= lang_name_cplusplus
;
13895 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
13897 /* We're now using the new linkage. */
13898 push_lang_context (linkage
);
13900 /* Preserve the location of the the innermost linkage specification,
13901 tracking the locations of nested specifications via a local. */
13902 location_t saved_location
13903 = parser
->innermost_linkage_specification_location
;
13904 /* Construct a location ranging from the start of the "extern" to
13905 the end of the string-literal, with the caret at the start, e.g.:
13909 parser
->innermost_linkage_specification_location
13910 = make_location (extern_token
->location
,
13911 extern_token
->location
,
13912 get_finish (string_token
->location
));
13914 /* If the next token is a `{', then we're using the first
13916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13918 cp_ensure_no_omp_declare_simd (parser
);
13919 cp_ensure_no_oacc_routine (parser
);
13921 /* Consume the `{' token. */
13922 matching_braces braces
;
13923 braces
.consume_open (parser
)->location
;
13924 /* Parse the declarations. */
13925 cp_parser_declaration_seq_opt (parser
);
13926 /* Look for the closing `}'. */
13927 braces
.require_close (parser
);
13929 /* Otherwise, there's just one declaration. */
13932 bool saved_in_unbraced_linkage_specification_p
;
13934 saved_in_unbraced_linkage_specification_p
13935 = parser
->in_unbraced_linkage_specification_p
;
13936 parser
->in_unbraced_linkage_specification_p
= true;
13937 cp_parser_declaration (parser
);
13938 parser
->in_unbraced_linkage_specification_p
13939 = saved_in_unbraced_linkage_specification_p
;
13942 /* We're done with the linkage-specification. */
13943 pop_lang_context ();
13945 /* Restore location of parent linkage specification, if any. */
13946 parser
->innermost_linkage_specification_location
= saved_location
;
13949 /* Parse a static_assert-declaration.
13951 static_assert-declaration:
13952 static_assert ( constant-expression , string-literal ) ;
13953 static_assert ( constant-expression ) ; (C++17)
13955 If MEMBER_P, this static_assert is a class member. */
13958 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
13961 location_t token_loc
;
13965 /* Peek at the `static_assert' token so we can keep track of exactly
13966 where the static assertion started. */
13967 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13969 /* Look for the `static_assert' keyword. */
13970 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
13974 /* We know we are in a static assertion; commit to any tentative
13976 if (cp_parser_parsing_tentatively (parser
))
13977 cp_parser_commit_to_tentative_parse (parser
);
13979 /* Parse the `(' starting the static assertion condition. */
13980 matching_parens parens
;
13981 parens
.require_open (parser
);
13983 /* Parse the constant-expression. Allow a non-constant expression
13984 here in order to give better diagnostics in finish_static_assert. */
13986 cp_parser_constant_expression (parser
,
13987 /*allow_non_constant_p=*/true,
13988 /*non_constant_p=*/&dummy
);
13990 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13992 if (cxx_dialect
< cxx17
)
13993 pedwarn (input_location
, OPT_Wpedantic
,
13994 "static_assert without a message "
13995 "only available with -std=c++17 or -std=gnu++17");
13997 cp_lexer_consume_token (parser
->lexer
);
13998 message
= build_string (1, "");
13999 TREE_TYPE (message
) = char_array_type_node
;
14000 fix_string_type (message
);
14004 /* Parse the separating `,'. */
14005 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14007 /* Parse the string-literal message. */
14008 message
= cp_parser_string_literal (parser
,
14009 /*translate=*/false,
14012 /* A `)' completes the static assertion. */
14013 if (!parens
.require_close (parser
))
14014 cp_parser_skip_to_closing_parenthesis (parser
,
14015 /*recovering=*/true,
14016 /*or_comma=*/false,
14017 /*consume_paren=*/true);
14020 /* A semicolon terminates the declaration. */
14021 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14023 /* Get the location for the static assertion. Use that of the
14024 condition if available, otherwise, use that of the "static_assert"
14026 location_t assert_loc
= condition
.get_location ();
14027 if (assert_loc
== UNKNOWN_LOCATION
)
14028 assert_loc
= token_loc
;
14030 /* Complete the static assertion, which may mean either processing
14031 the static assert now or saving it for template instantiation. */
14032 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14035 /* Parse the expression in decltype ( expression ). */
14038 cp_parser_decltype_expr (cp_parser
*parser
,
14039 bool &id_expression_or_member_access_p
)
14041 cp_token
*id_expr_start_token
;
14044 /* Since we're going to preserve any side-effects from this parse, set up a
14045 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14046 in the expression. */
14047 tentative_firewall
firewall (parser
);
14049 /* First, try parsing an id-expression. */
14050 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14051 cp_parser_parse_tentatively (parser
);
14052 expr
= cp_parser_id_expression (parser
,
14053 /*template_keyword_p=*/false,
14054 /*check_dependency_p=*/true,
14055 /*template_p=*/NULL
,
14056 /*declarator_p=*/false,
14057 /*optional_p=*/false);
14059 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14061 bool non_integral_constant_expression_p
= false;
14062 tree id_expression
= expr
;
14064 const char *error_msg
;
14066 if (identifier_p (expr
))
14067 /* Lookup the name we got back from the id-expression. */
14068 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14069 id_expr_start_token
->location
);
14071 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14072 /* A template without args is not a complete id-expression. */
14073 expr
= error_mark_node
;
14076 && expr
!= error_mark_node
14077 && TREE_CODE (expr
) != TYPE_DECL
14078 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14079 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14080 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14082 /* Complete lookup of the id-expression. */
14083 expr
= (finish_id_expression
14084 (id_expression
, expr
, parser
->scope
, &idk
,
14085 /*integral_constant_expression_p=*/false,
14086 /*allow_non_integral_constant_expression_p=*/true,
14087 &non_integral_constant_expression_p
,
14088 /*template_p=*/false,
14090 /*address_p=*/false,
14091 /*template_arg_p=*/false,
14093 id_expr_start_token
->location
));
14095 if (expr
== error_mark_node
)
14096 /* We found an id-expression, but it was something that we
14097 should not have found. This is an error, not something
14098 we can recover from, so note that we found an
14099 id-expression and we'll recover as gracefully as
14101 id_expression_or_member_access_p
= true;
14105 && expr
!= error_mark_node
14106 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14107 /* We have an id-expression. */
14108 id_expression_or_member_access_p
= true;
14111 if (!id_expression_or_member_access_p
)
14113 /* Abort the id-expression parse. */
14114 cp_parser_abort_tentative_parse (parser
);
14116 /* Parsing tentatively, again. */
14117 cp_parser_parse_tentatively (parser
);
14119 /* Parse a class member access. */
14120 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14121 /*cast_p=*/false, /*decltype*/true,
14122 /*member_access_only_p=*/true, NULL
);
14125 && expr
!= error_mark_node
14126 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14127 /* We have an id-expression. */
14128 id_expression_or_member_access_p
= true;
14131 if (id_expression_or_member_access_p
)
14132 /* We have parsed the complete id-expression or member access. */
14133 cp_parser_parse_definitely (parser
);
14136 /* Abort our attempt to parse an id-expression or member access
14138 cp_parser_abort_tentative_parse (parser
);
14140 /* Commit to the tentative_firewall so we get syntax errors. */
14141 cp_parser_commit_to_tentative_parse (parser
);
14143 /* Parse a full expression. */
14144 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14145 /*decltype_p=*/true);
14151 /* Parse a `decltype' type. Returns the type.
14153 simple-type-specifier:
14154 decltype ( expression )
14156 decltype ( auto ) */
14159 cp_parser_decltype (cp_parser
*parser
)
14161 bool id_expression_or_member_access_p
= false;
14162 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14164 if (start_token
->type
== CPP_DECLTYPE
)
14166 /* Already parsed. */
14167 cp_lexer_consume_token (parser
->lexer
);
14168 return saved_checks_value (start_token
->u
.tree_check_value
);
14171 /* Look for the `decltype' token. */
14172 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14173 return error_mark_node
;
14175 /* Parse the opening `('. */
14176 matching_parens parens
;
14177 if (!parens
.require_open (parser
))
14178 return error_mark_node
;
14180 push_deferring_access_checks (dk_deferred
);
14182 tree expr
= NULL_TREE
;
14184 if (cxx_dialect
>= cxx14
14185 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14186 /* decltype (auto) */
14187 cp_lexer_consume_token (parser
->lexer
);
14190 /* decltype (expression) */
14192 /* Types cannot be defined in a `decltype' expression. Save away the
14193 old message and set the new one. */
14194 const char *saved_message
= parser
->type_definition_forbidden_message
;
14195 parser
->type_definition_forbidden_message
14196 = G_("types may not be defined in %<decltype%> expressions");
14198 /* The restrictions on constant-expressions do not apply inside
14199 decltype expressions. */
14200 bool saved_integral_constant_expression_p
14201 = parser
->integral_constant_expression_p
;
14202 bool saved_non_integral_constant_expression_p
14203 = parser
->non_integral_constant_expression_p
;
14204 parser
->integral_constant_expression_p
= false;
14206 /* Within a parenthesized expression, a `>' token is always
14207 the greater-than operator. */
14208 bool saved_greater_than_is_operator_p
14209 = parser
->greater_than_is_operator_p
;
14210 parser
->greater_than_is_operator_p
= true;
14212 /* Do not actually evaluate the expression. */
14213 ++cp_unevaluated_operand
;
14215 /* Do not warn about problems with the expression. */
14216 ++c_inhibit_evaluation_warnings
;
14218 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14220 /* Go back to evaluating expressions. */
14221 --cp_unevaluated_operand
;
14222 --c_inhibit_evaluation_warnings
;
14224 /* The `>' token might be the end of a template-id or
14225 template-parameter-list now. */
14226 parser
->greater_than_is_operator_p
14227 = saved_greater_than_is_operator_p
;
14229 /* Restore the old message and the integral constant expression
14231 parser
->type_definition_forbidden_message
= saved_message
;
14232 parser
->integral_constant_expression_p
14233 = saved_integral_constant_expression_p
;
14234 parser
->non_integral_constant_expression_p
14235 = saved_non_integral_constant_expression_p
;
14238 /* Parse to the closing `)'. */
14239 if (!parens
.require_close (parser
))
14241 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14242 /*consume_paren=*/true);
14243 pop_deferring_access_checks ();
14244 return error_mark_node
;
14250 expr
= make_decltype_auto ();
14251 AUTO_IS_DECLTYPE (expr
) = true;
14254 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14255 tf_warning_or_error
);
14257 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14259 start_token
->type
= CPP_DECLTYPE
;
14260 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14261 start_token
->u
.tree_check_value
->value
= expr
;
14262 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14263 start_token
->keyword
= RID_MAX
;
14264 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14266 pop_to_parent_deferring_access_checks ();
14271 /* Special member functions [gram.special] */
14273 /* Parse a conversion-function-id.
14275 conversion-function-id:
14276 operator conversion-type-id
14278 Returns an IDENTIFIER_NODE representing the operator. */
14281 cp_parser_conversion_function_id (cp_parser
* parser
)
14285 tree saved_qualifying_scope
;
14286 tree saved_object_scope
;
14287 tree pushed_scope
= NULL_TREE
;
14289 /* Look for the `operator' token. */
14290 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14291 return error_mark_node
;
14292 /* When we parse the conversion-type-id, the current scope will be
14293 reset. However, we need that information in able to look up the
14294 conversion function later, so we save it here. */
14295 saved_scope
= parser
->scope
;
14296 saved_qualifying_scope
= parser
->qualifying_scope
;
14297 saved_object_scope
= parser
->object_scope
;
14298 /* We must enter the scope of the class so that the names of
14299 entities declared within the class are available in the
14300 conversion-type-id. For example, consider:
14307 S::operator I() { ... }
14309 In order to see that `I' is a type-name in the definition, we
14310 must be in the scope of `S'. */
14312 pushed_scope
= push_scope (saved_scope
);
14313 /* Parse the conversion-type-id. */
14314 type
= cp_parser_conversion_type_id (parser
);
14315 /* Leave the scope of the class, if any. */
14317 pop_scope (pushed_scope
);
14318 /* Restore the saved scope. */
14319 parser
->scope
= saved_scope
;
14320 parser
->qualifying_scope
= saved_qualifying_scope
;
14321 parser
->object_scope
= saved_object_scope
;
14322 /* If the TYPE is invalid, indicate failure. */
14323 if (type
== error_mark_node
)
14324 return error_mark_node
;
14325 return make_conv_op_name (type
);
14328 /* Parse a conversion-type-id:
14330 conversion-type-id:
14331 type-specifier-seq conversion-declarator [opt]
14333 Returns the TYPE specified. */
14336 cp_parser_conversion_type_id (cp_parser
* parser
)
14339 cp_decl_specifier_seq type_specifiers
;
14340 cp_declarator
*declarator
;
14341 tree type_specified
;
14342 const char *saved_message
;
14344 /* Parse the attributes. */
14345 attributes
= cp_parser_attributes_opt (parser
);
14347 saved_message
= parser
->type_definition_forbidden_message
;
14348 parser
->type_definition_forbidden_message
14349 = G_("types may not be defined in a conversion-type-id");
14351 /* Parse the type-specifiers. */
14352 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
14353 /*is_trailing_return=*/false,
14356 parser
->type_definition_forbidden_message
= saved_message
;
14358 /* If that didn't work, stop. */
14359 if (type_specifiers
.type
== error_mark_node
)
14360 return error_mark_node
;
14361 /* Parse the conversion-declarator. */
14362 declarator
= cp_parser_conversion_declarator_opt (parser
);
14364 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14365 /*initialized=*/0, &attributes
);
14367 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14369 /* Don't give this error when parsing tentatively. This happens to
14370 work because we always parse this definitively once. */
14371 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14372 && type_uses_auto (type_specified
))
14374 if (cxx_dialect
< cxx14
)
14376 error ("invalid use of %<auto%> in conversion operator");
14377 return error_mark_node
;
14379 else if (template_parm_scope_p ())
14380 warning (0, "use of %<auto%> in member template "
14381 "conversion operator can never be deduced");
14384 return type_specified
;
14387 /* Parse an (optional) conversion-declarator.
14389 conversion-declarator:
14390 ptr-operator conversion-declarator [opt]
14394 static cp_declarator
*
14395 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14397 enum tree_code code
;
14398 tree class_type
, std_attributes
= NULL_TREE
;
14399 cp_cv_quals cv_quals
;
14401 /* We don't know if there's a ptr-operator next, or not. */
14402 cp_parser_parse_tentatively (parser
);
14403 /* Try the ptr-operator. */
14404 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
14406 /* If it worked, look for more conversion-declarators. */
14407 if (cp_parser_parse_definitely (parser
))
14409 cp_declarator
*declarator
;
14411 /* Parse another optional declarator. */
14412 declarator
= cp_parser_conversion_declarator_opt (parser
);
14414 declarator
= cp_parser_make_indirect_declarator
14415 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
14423 /* Parse an (optional) ctor-initializer.
14426 : mem-initializer-list */
14429 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
14431 /* If the next token is not a `:', then there is no
14432 ctor-initializer. */
14433 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14435 /* Do default initialization of any bases and members. */
14436 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14437 finish_mem_initializers (NULL_TREE
);
14441 /* Consume the `:' token. */
14442 cp_lexer_consume_token (parser
->lexer
);
14443 /* And the mem-initializer-list. */
14444 cp_parser_mem_initializer_list (parser
);
14447 /* Parse a mem-initializer-list.
14449 mem-initializer-list:
14450 mem-initializer ... [opt]
14451 mem-initializer ... [opt] , mem-initializer-list */
14454 cp_parser_mem_initializer_list (cp_parser
* parser
)
14456 tree mem_initializer_list
= NULL_TREE
;
14457 tree target_ctor
= error_mark_node
;
14458 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14460 /* Let the semantic analysis code know that we are starting the
14461 mem-initializer-list. */
14462 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
14463 error_at (token
->location
,
14464 "only constructors take member initializers");
14466 /* Loop through the list. */
14469 tree mem_initializer
;
14471 token
= cp_lexer_peek_token (parser
->lexer
);
14472 /* Parse the mem-initializer. */
14473 mem_initializer
= cp_parser_mem_initializer (parser
);
14474 /* If the next token is a `...', we're expanding member initializers. */
14475 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14477 || (mem_initializer
!= error_mark_node
14478 && check_for_bare_parameter_packs (TREE_PURPOSE
14479 (mem_initializer
))))
14481 /* Consume the `...'. */
14483 cp_lexer_consume_token (parser
->lexer
);
14485 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14486 can be expanded but members cannot. */
14487 if (mem_initializer
!= error_mark_node
14488 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
14490 error_at (token
->location
,
14491 "cannot expand initializer for member %qD",
14492 TREE_PURPOSE (mem_initializer
));
14493 mem_initializer
= error_mark_node
;
14496 /* Construct the pack expansion type. */
14497 if (mem_initializer
!= error_mark_node
)
14498 mem_initializer
= make_pack_expansion (mem_initializer
);
14500 if (target_ctor
!= error_mark_node
14501 && mem_initializer
!= error_mark_node
)
14503 error ("mem-initializer for %qD follows constructor delegation",
14504 TREE_PURPOSE (mem_initializer
));
14505 mem_initializer
= error_mark_node
;
14507 /* Look for a target constructor. */
14508 if (mem_initializer
!= error_mark_node
14509 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
14510 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
14512 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
14513 if (mem_initializer_list
)
14515 error ("constructor delegation follows mem-initializer for %qD",
14516 TREE_PURPOSE (mem_initializer_list
));
14517 mem_initializer
= error_mark_node
;
14519 target_ctor
= mem_initializer
;
14521 /* Add it to the list, unless it was erroneous. */
14522 if (mem_initializer
!= error_mark_node
)
14524 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
14525 mem_initializer_list
= mem_initializer
;
14527 /* If the next token is not a `,', we're done. */
14528 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14530 /* Consume the `,' token. */
14531 cp_lexer_consume_token (parser
->lexer
);
14534 /* Perform semantic analysis. */
14535 if (DECL_CONSTRUCTOR_P (current_function_decl
))
14536 finish_mem_initializers (mem_initializer_list
);
14539 /* Parse a mem-initializer.
14542 mem-initializer-id ( expression-list [opt] )
14543 mem-initializer-id braced-init-list
14548 ( expression-list [opt] )
14550 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14551 class) or FIELD_DECL (for a non-static data member) to initialize;
14552 the TREE_VALUE is the expression-list. An empty initialization
14553 list is represented by void_list_node. */
14556 cp_parser_mem_initializer (cp_parser
* parser
)
14558 tree mem_initializer_id
;
14559 tree expression_list
;
14561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14563 /* Find out what is being initialized. */
14564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14566 permerror (token
->location
,
14567 "anachronistic old-style base class initializer");
14568 mem_initializer_id
= NULL_TREE
;
14572 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
14573 if (mem_initializer_id
== error_mark_node
)
14574 return mem_initializer_id
;
14576 member
= expand_member_init (mem_initializer_id
);
14577 if (member
&& !DECL_P (member
))
14578 in_base_initializer
= 1;
14580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14582 bool expr_non_constant_p
;
14583 cp_lexer_set_source_position (parser
->lexer
);
14584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
14585 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
14586 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
14587 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
14591 vec
<tree
, va_gc
> *vec
;
14592 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
14594 /*allow_expansion_p=*/true,
14595 /*non_constant_p=*/NULL
);
14597 return error_mark_node
;
14598 expression_list
= build_tree_list_vec (vec
);
14599 release_tree_vector (vec
);
14602 if (expression_list
== error_mark_node
)
14603 return error_mark_node
;
14604 if (!expression_list
)
14605 expression_list
= void_type_node
;
14607 in_base_initializer
= 0;
14609 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
14612 /* Parse a mem-initializer-id.
14614 mem-initializer-id:
14615 :: [opt] nested-name-specifier [opt] class-name
14616 decltype-specifier (C++11)
14619 Returns a TYPE indicating the class to be initialized for the first
14620 production (and the second in C++11). Returns an IDENTIFIER_NODE
14621 indicating the data member to be initialized for the last production. */
14624 cp_parser_mem_initializer_id (cp_parser
* parser
)
14626 bool global_scope_p
;
14627 bool nested_name_specifier_p
;
14628 bool template_p
= false;
14631 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14633 /* `typename' is not allowed in this context ([temp.res]). */
14634 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
14636 error_at (token
->location
,
14637 "keyword %<typename%> not allowed in this context (a qualified "
14638 "member initializer is implicitly a type)");
14639 cp_lexer_consume_token (parser
->lexer
);
14641 /* Look for the optional `::' operator. */
14643 = (cp_parser_global_scope_opt (parser
,
14644 /*current_scope_valid_p=*/false)
14646 /* Look for the optional nested-name-specifier. The simplest way to
14651 The keyword `typename' is not permitted in a base-specifier or
14652 mem-initializer; in these contexts a qualified name that
14653 depends on a template-parameter is implicitly assumed to be a
14656 is to assume that we have seen the `typename' keyword at this
14658 nested_name_specifier_p
14659 = (cp_parser_nested_name_specifier_opt (parser
,
14660 /*typename_keyword_p=*/true,
14661 /*check_dependency_p=*/true,
14663 /*is_declaration=*/true)
14665 if (nested_name_specifier_p
)
14666 template_p
= cp_parser_optional_template_keyword (parser
);
14667 /* If there is a `::' operator or a nested-name-specifier, then we
14668 are definitely looking for a class-name. */
14669 if (global_scope_p
|| nested_name_specifier_p
)
14670 return cp_parser_class_name (parser
,
14671 /*typename_keyword_p=*/true,
14672 /*template_keyword_p=*/template_p
,
14674 /*check_dependency_p=*/true,
14675 /*class_head_p=*/false,
14676 /*is_declaration=*/true);
14677 /* Otherwise, we could also be looking for an ordinary identifier. */
14678 cp_parser_parse_tentatively (parser
);
14679 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
14680 /* Try a decltype-specifier. */
14681 id
= cp_parser_decltype (parser
);
14683 /* Otherwise, try a class-name. */
14684 id
= cp_parser_class_name (parser
,
14685 /*typename_keyword_p=*/true,
14686 /*template_keyword_p=*/false,
14688 /*check_dependency_p=*/true,
14689 /*class_head_p=*/false,
14690 /*is_declaration=*/true);
14691 /* If we found one, we're done. */
14692 if (cp_parser_parse_definitely (parser
))
14694 /* Otherwise, look for an ordinary identifier. */
14695 return cp_parser_identifier (parser
);
14698 /* Overloading [gram.over] */
14700 /* Parse an operator-function-id.
14702 operator-function-id:
14705 Returns an IDENTIFIER_NODE for the operator which is a
14706 human-readable spelling of the identifier, e.g., `operator +'. */
14709 cp_parser_operator_function_id (cp_parser
* parser
)
14711 /* Look for the `operator' keyword. */
14712 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14713 return error_mark_node
;
14714 /* And then the name of the operator itself. */
14715 return cp_parser_operator (parser
);
14718 /* Return an identifier node for a user-defined literal operator.
14719 The suffix identifier is chained to the operator name identifier. */
14722 cp_literal_operator_id (const char* name
)
14725 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
14726 + strlen (name
) + 10);
14727 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
14728 identifier
= get_identifier (buffer
);
14733 /* Parse an operator.
14736 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14737 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14738 || ++ -- , ->* -> () []
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 (cp_parser
* parser
)
14751 tree id
= NULL_TREE
;
14755 /* Peek at the next token. */
14756 token
= cp_lexer_peek_token (parser
->lexer
);
14758 location_t start_loc
= token
->location
;
14760 /* Figure out which operator we have. */
14761 enum tree_code op
= ERROR_MARK
;
14762 bool assop
= false;
14763 bool consumed
= false;
14764 switch (token
->type
)
14768 /* The keyword should be either `new' or `delete'. */
14769 if (token
->keyword
== RID_NEW
)
14771 else if (token
->keyword
== RID_DELETE
)
14776 /* Consume the `new' or `delete' token. */
14777 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
14779 /* Peek at the next token. */
14780 token
= cp_lexer_peek_token (parser
->lexer
);
14781 /* If it's a `[' token then this is the array variant of the
14783 if (token
->type
== CPP_OPEN_SQUARE
)
14785 /* Consume the `[' token. */
14786 cp_lexer_consume_token (parser
->lexer
);
14787 /* Look for the `]' token. */
14788 if (cp_token
*close_token
14789 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14790 end_loc
= close_token
->location
;
14791 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
14793 start_loc
= make_location (start_loc
, start_loc
, end_loc
);
14811 op
= TRUNC_DIV_EXPR
;
14815 op
= TRUNC_MOD_EXPR
;
14835 op
= TRUTH_NOT_EXPR
;
14868 op
= TRUNC_DIV_EXPR
;
14873 op
= TRUNC_MOD_EXPR
;
14899 case CPP_LSHIFT_EQ
:
14904 case CPP_RSHIFT_EQ
:
14921 case CPP_GREATER_EQ
:
14926 op
= TRUTH_ANDIF_EXPR
;
14930 op
= TRUTH_ORIF_EXPR
;
14933 case CPP_PLUS_PLUS
:
14934 op
= POSTINCREMENT_EXPR
;
14937 case CPP_MINUS_MINUS
:
14938 op
= PREDECREMENT_EXPR
;
14942 op
= COMPOUND_EXPR
;
14945 case CPP_DEREF_STAR
:
14950 op
= COMPONENT_REF
;
14953 case CPP_OPEN_PAREN
:
14955 /* Consume the `('. */
14956 matching_parens parens
;
14957 parens
.consume_open (parser
);
14958 /* Look for the matching `)'. */
14959 parens
.require_close (parser
);
14965 case CPP_OPEN_SQUARE
:
14966 /* Consume the `['. */
14967 cp_lexer_consume_token (parser
->lexer
);
14968 /* Look for the matching `]'. */
14969 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
14974 case CPP_UTF8STRING
:
14975 case CPP_UTF8STRING_USERDEF
:
14982 case CPP_STRING_USERDEF
:
14983 case CPP_WSTRING_USERDEF
:
14984 case CPP_STRING16_USERDEF
:
14985 case CPP_STRING32_USERDEF
:
14987 tree str
, string_tree
;
14990 if (cxx_dialect
== cxx98
)
14991 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
14993 /* Consume the string. */
14994 str
= cp_parser_string_literal (parser
, /*translate=*/true,
14995 /*wide_ok=*/true, /*lookup_udlit=*/false);
14996 if (str
== error_mark_node
)
14997 return error_mark_node
;
14998 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15000 string_tree
= USERDEF_LITERAL_VALUE (str
);
15001 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
15006 /* Look for the suffix identifier. */
15007 token
= cp_lexer_peek_token (parser
->lexer
);
15008 if (token
->type
== CPP_NAME
)
15009 id
= cp_parser_identifier (parser
);
15010 else if (token
->type
== CPP_KEYWORD
)
15012 error ("unexpected keyword;"
15013 " remove space between quotes and suffix identifier");
15014 return error_mark_node
;
15018 error ("expected suffix identifier");
15019 return error_mark_node
;
15022 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15023 (TREE_TYPE (TREE_TYPE (string_tree
))));
15024 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15027 error ("expected empty string after %<operator%> keyword");
15028 return error_mark_node
;
15030 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15033 error ("invalid encoding prefix in literal operator");
15034 return error_mark_node
;
15036 if (id
!= error_mark_node
)
15038 const char *name
= IDENTIFIER_POINTER (id
);
15039 id
= cp_literal_operator_id (name
);
15045 /* Anything else is an error. */
15049 /* If we have selected an identifier, we need to consume the
15051 if (op
!= ERROR_MARK
)
15053 id
= ovl_op_identifier (assop
, op
);
15055 cp_lexer_consume_token (parser
->lexer
);
15057 /* Otherwise, no valid operator name was present. */
15060 cp_parser_error (parser
, "expected operator");
15061 id
= error_mark_node
;
15064 return cp_expr (id
, start_loc
);
15067 /* Parse a template-declaration.
15069 template-declaration:
15070 export [opt] template < template-parameter-list > declaration
15072 If MEMBER_P is TRUE, this template-declaration occurs within a
15075 The grammar rule given by the standard isn't correct. What
15076 is really meant is:
15078 template-declaration:
15079 export [opt] template-parameter-list-seq
15080 decl-specifier-seq [opt] init-declarator [opt] ;
15081 export [opt] template-parameter-list-seq
15082 function-definition
15084 template-parameter-list-seq:
15085 template-parameter-list-seq [opt]
15086 template < template-parameter-list >
15088 Concept Extensions:
15090 template-parameter-list-seq:
15091 template < template-parameter-list > requires-clause [opt]
15094 requires logical-or-expression */
15097 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15099 /* Check for `export'. */
15100 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15102 /* Consume the `export' token. */
15103 cp_lexer_consume_token (parser
->lexer
);
15104 /* Warn that we do not support `export'. */
15105 warning (0, "keyword %<export%> not implemented, and will be ignored");
15108 cp_parser_template_declaration_after_export (parser
, member_p
);
15111 /* Parse a template-parameter-list.
15113 template-parameter-list:
15115 template-parameter-list , template-parameter
15117 Returns a TREE_LIST. Each node represents a template parameter.
15118 The nodes are connected via their TREE_CHAINs. */
15121 cp_parser_template_parameter_list (cp_parser
* parser
)
15123 tree parameter_list
= NULL_TREE
;
15125 begin_template_parm_list ();
15127 /* The loop below parses the template parms. We first need to know
15128 the total number of template parms to be able to compute proper
15129 canonical types of each dependent type. So after the loop, when
15130 we know the total number of template parms,
15131 end_template_parm_list computes the proper canonical types and
15132 fixes up the dependent types accordingly. */
15137 bool is_parameter_pack
;
15138 location_t parm_loc
;
15140 /* Parse the template-parameter. */
15141 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15142 parameter
= cp_parser_template_parameter (parser
,
15144 &is_parameter_pack
);
15145 /* Add it to the list. */
15146 if (parameter
!= error_mark_node
)
15147 parameter_list
= process_template_parm (parameter_list
,
15151 is_parameter_pack
);
15154 tree err_parm
= build_tree_list (parameter
, parameter
);
15155 parameter_list
= chainon (parameter_list
, err_parm
);
15158 /* If the next token is not a `,', we're done. */
15159 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15161 /* Otherwise, consume the `,' token. */
15162 cp_lexer_consume_token (parser
->lexer
);
15165 return end_template_parm_list (parameter_list
);
15168 /* Parse a introduction-list.
15171 introduced-parameter
15172 introduction-list , introduced-parameter
15174 introduced-parameter:
15175 ...[opt] identifier
15177 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15178 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15179 WILDCARD_DECL will also have DECL_NAME set and token location in
15180 DECL_SOURCE_LOCATION. */
15183 cp_parser_introduction_list (cp_parser
*parser
)
15185 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15189 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15191 cp_lexer_consume_token (parser
->lexer
);
15193 /* Build placeholder. */
15194 tree parm
= build_nt (WILDCARD_DECL
);
15195 DECL_SOURCE_LOCATION (parm
)
15196 = cp_lexer_peek_token (parser
->lexer
)->location
;
15197 DECL_NAME (parm
) = cp_parser_identifier (parser
);
15198 WILDCARD_PACK_P (parm
) = is_pack
;
15199 vec_safe_push (introduction_vec
, parm
);
15201 /* If the next token is not a `,', we're done. */
15202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15204 /* Otherwise, consume the `,' token. */
15205 cp_lexer_consume_token (parser
->lexer
);
15208 /* Convert the vec into a TREE_VEC. */
15209 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15212 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15213 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15215 release_tree_vector (introduction_vec
);
15216 return introduction_list
;
15219 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15220 is an abstract declarator. */
15222 static inline cp_declarator
*
15223 get_id_declarator (cp_declarator
*declarator
)
15225 cp_declarator
*d
= declarator
;
15226 while (d
&& d
->kind
!= cdk_id
)
15231 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15232 is an abstract declarator. */
15235 get_unqualified_id (cp_declarator
*declarator
)
15237 declarator
= get_id_declarator (declarator
);
15239 return declarator
->u
.id
.unqualified_name
;
15244 /* Returns true if DECL represents a constrained-parameter. */
15247 is_constrained_parameter (tree decl
)
15250 && TREE_CODE (decl
) == TYPE_DECL
15251 && CONSTRAINED_PARM_CONCEPT (decl
)
15252 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
15255 /* Returns true if PARM declares a constrained-parameter. */
15258 is_constrained_parameter (cp_parameter_declarator
*parm
)
15260 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15263 /* Check that the type parameter is only a declarator-id, and that its
15264 type is not cv-qualified. */
15267 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15268 cp_parameter_declarator
*parm
)
15270 if (!parm
->declarator
)
15273 if (parm
->declarator
->kind
!= cdk_id
)
15275 cp_parser_error (parser
, "invalid constrained type parameter");
15279 /* Don't allow cv-qualified type parameters. */
15280 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15281 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15283 cp_parser_error (parser
, "cv-qualified type parameter");
15290 /* Finish parsing/processing a template type parameter and checking
15291 various restrictions. */
15294 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15296 cp_parameter_declarator
* parmdecl
)
15298 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15299 return finish_template_type_parm (class_type_node
, id
);
15301 return error_mark_node
;
15305 finish_constrained_template_template_parm (tree proto
, tree id
)
15307 /* FIXME: This should probably be copied, and we may need to adjust
15308 the template parameter depths. */
15309 tree saved_parms
= current_template_parms
;
15310 begin_template_parm_list ();
15311 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15312 end_template_parm_list ();
15314 tree parm
= finish_template_template_parm (class_type_node
, id
);
15315 current_template_parms
= saved_parms
;
15320 /* Finish parsing/processing a template template parameter by borrowing
15321 the template parameter list from the prototype parameter. */
15324 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15327 cp_parameter_declarator
*parmdecl
)
15329 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15330 return error_mark_node
;
15331 return finish_constrained_template_template_parm (proto
, id
);
15334 /* Create a new non-type template parameter from the given PARM
15338 constrained_non_type_template_parm (bool *is_non_type
,
15339 cp_parameter_declarator
*parm
)
15341 *is_non_type
= true;
15342 cp_declarator
*decl
= parm
->declarator
;
15343 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15344 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15345 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15348 /* Build a constrained template parameter based on the PARMDECL
15349 declarator. The type of PARMDECL is the constrained type, which
15350 refers to the prototype template parameter that ultimately
15351 specifies the type of the declared parameter. */
15354 finish_constrained_parameter (cp_parser
*parser
,
15355 cp_parameter_declarator
*parmdecl
,
15357 bool *is_parameter_pack
)
15359 tree decl
= parmdecl
->decl_specifiers
.type
;
15360 tree id
= get_unqualified_id (parmdecl
->declarator
);
15361 tree def
= parmdecl
->default_argument
;
15362 tree proto
= DECL_INITIAL (decl
);
15364 /* A template parameter constrained by a variadic concept shall also
15365 be declared as a template parameter pack. */
15366 bool is_variadic
= template_parameter_pack_p (proto
);
15367 if (is_variadic
&& !*is_parameter_pack
)
15368 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
15370 /* Build the parameter. Return an error if the declarator was invalid. */
15372 if (TREE_CODE (proto
) == TYPE_DECL
)
15373 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
15374 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
15375 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
15378 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
15379 if (parm
== error_mark_node
)
15380 return error_mark_node
;
15382 /* Finish the parameter decl and create a node attaching the
15383 default argument and constraint. */
15384 parm
= build_tree_list (def
, parm
);
15385 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
15390 /* Returns true if the parsed type actually represents the declaration
15391 of a type template-parameter. */
15394 declares_constrained_type_template_parameter (tree type
)
15396 return (is_constrained_parameter (type
)
15397 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
15401 /* Returns true if the parsed type actually represents the declaration of
15402 a template template-parameter. */
15405 declares_constrained_template_template_parameter (tree type
)
15407 return (is_constrained_parameter (type
)
15408 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
15411 /* Parse a default argument for a type template-parameter.
15412 Note that diagnostics are handled in cp_parser_template_parameter. */
15415 cp_parser_default_type_template_argument (cp_parser
*parser
)
15417 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15419 /* Consume the `=' token. */
15420 cp_lexer_consume_token (parser
->lexer
);
15422 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15424 /* Parse the default-argument. */
15425 push_deferring_access_checks (dk_no_deferred
);
15426 tree default_argument
= cp_parser_type_id (parser
);
15427 pop_deferring_access_checks ();
15429 if (flag_concepts
&& type_uses_auto (default_argument
))
15431 error_at (token
->location
,
15432 "invalid use of %<auto%> in default template argument");
15433 return error_mark_node
;
15436 return default_argument
;
15439 /* Parse a default argument for a template template-parameter. */
15442 cp_parser_default_template_template_argument (cp_parser
*parser
)
15444 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
15448 /* Consume the `='. */
15449 cp_lexer_consume_token (parser
->lexer
);
15450 /* Parse the id-expression. */
15451 push_deferring_access_checks (dk_no_deferred
);
15452 /* save token before parsing the id-expression, for error
15454 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
15455 tree default_argument
15456 = cp_parser_id_expression (parser
,
15457 /*template_keyword_p=*/false,
15458 /*check_dependency_p=*/true,
15459 /*template_p=*/&is_template
,
15460 /*declarator_p=*/false,
15461 /*optional_p=*/false);
15462 if (TREE_CODE (default_argument
) == TYPE_DECL
)
15463 /* If the id-expression was a template-id that refers to
15464 a template-class, we already have the declaration here,
15465 so no further lookup is needed. */
15468 /* Look up the name. */
15470 = cp_parser_lookup_name (parser
, default_argument
,
15472 /*is_template=*/is_template
,
15473 /*is_namespace=*/false,
15474 /*check_dependency=*/true,
15475 /*ambiguous_decls=*/NULL
,
15477 /* See if the default argument is valid. */
15478 default_argument
= check_template_template_default_arg (default_argument
);
15479 pop_deferring_access_checks ();
15480 return default_argument
;
15483 /* Parse a template-parameter.
15485 template-parameter:
15487 parameter-declaration
15489 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15490 the parameter. The TREE_PURPOSE is the default value, if any.
15491 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15492 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15493 set to true iff this parameter is a parameter pack. */
15496 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
15497 bool *is_parameter_pack
)
15500 cp_parameter_declarator
*parameter_declarator
;
15503 /* Assume it is a type parameter or a template parameter. */
15504 *is_non_type
= false;
15505 /* Assume it not a parameter pack. */
15506 *is_parameter_pack
= false;
15507 /* Peek at the next token. */
15508 token
= cp_lexer_peek_token (parser
->lexer
);
15509 /* If it is `template', we have a type-parameter. */
15510 if (token
->keyword
== RID_TEMPLATE
)
15511 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15512 /* If it is `class' or `typename' we do not know yet whether it is a
15513 type parameter or a non-type parameter. Consider:
15515 template <typename T, typename T::X X> ...
15519 template <class C, class D*> ...
15521 Here, the first parameter is a type parameter, and the second is
15522 a non-type parameter. We can tell by looking at the token after
15523 the identifier -- if it is a `,', `=', or `>' then we have a type
15525 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
15527 /* Peek at the token after `class' or `typename'. */
15528 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
15529 /* If it's an ellipsis, we have a template type parameter
15531 if (token
->type
== CPP_ELLIPSIS
)
15532 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15533 /* If it's an identifier, skip it. */
15534 if (token
->type
== CPP_NAME
)
15535 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
15536 /* Now, see if the token looks like the end of a template
15538 if (token
->type
== CPP_COMMA
15539 || token
->type
== CPP_EQ
15540 || token
->type
== CPP_GREATER
)
15541 return cp_parser_type_parameter (parser
, is_parameter_pack
);
15544 /* Otherwise, it is a non-type parameter or a constrained parameter.
15548 When parsing a default template-argument for a non-type
15549 template-parameter, the first non-nested `>' is taken as the end
15550 of the template parameter-list rather than a greater-than
15552 parameter_declarator
15553 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
15554 /*parenthesized_p=*/NULL
);
15556 if (!parameter_declarator
)
15557 return error_mark_node
;
15559 /* If the parameter declaration is marked as a parameter pack, set
15560 *IS_PARAMETER_PACK to notify the caller. */
15561 if (parameter_declarator
->template_parameter_pack_p
)
15562 *is_parameter_pack
= true;
15564 if (parameter_declarator
->default_argument
)
15566 /* Can happen in some cases of erroneous input (c++/34892). */
15567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15568 /* Consume the `...' for better error recovery. */
15569 cp_lexer_consume_token (parser
->lexer
);
15572 // The parameter may have been constrained.
15573 if (is_constrained_parameter (parameter_declarator
))
15574 return finish_constrained_parameter (parser
,
15575 parameter_declarator
,
15577 is_parameter_pack
);
15579 // Now we're sure that the parameter is a non-type parameter.
15580 *is_non_type
= true;
15582 parm
= grokdeclarator (parameter_declarator
->declarator
,
15583 ¶meter_declarator
->decl_specifiers
,
15584 TPARM
, /*initialized=*/0,
15585 /*attrlist=*/NULL
);
15586 if (parm
== error_mark_node
)
15587 return error_mark_node
;
15589 return build_tree_list (parameter_declarator
->default_argument
, parm
);
15592 /* Parse a type-parameter.
15595 class identifier [opt]
15596 class identifier [opt] = type-id
15597 typename identifier [opt]
15598 typename identifier [opt] = type-id
15599 template < template-parameter-list > class identifier [opt]
15600 template < template-parameter-list > class identifier [opt]
15603 GNU Extension (variadic templates):
15606 class ... identifier [opt]
15607 typename ... identifier [opt]
15609 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15610 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15611 the declaration of the parameter.
15613 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15616 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
15621 /* Look for a keyword to tell us what kind of parameter this is. */
15622 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
15624 return error_mark_node
;
15626 switch (token
->keyword
)
15632 tree default_argument
;
15634 /* If the next token is an ellipsis, we have a template
15636 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15638 /* Consume the `...' token. */
15639 cp_lexer_consume_token (parser
->lexer
);
15640 maybe_warn_variadic_templates ();
15642 *is_parameter_pack
= true;
15645 /* If the next token is an identifier, then it names the
15647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15648 identifier
= cp_parser_identifier (parser
);
15650 identifier
= NULL_TREE
;
15652 /* Create the parameter. */
15653 parameter
= finish_template_type_parm (class_type_node
, identifier
);
15655 /* If the next token is an `=', we have a default argument. */
15656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15659 = cp_parser_default_type_template_argument (parser
);
15661 /* Template parameter packs cannot have default
15663 if (*is_parameter_pack
)
15666 error_at (token
->location
,
15667 "template parameter pack %qD cannot have a "
15668 "default argument", identifier
);
15670 error_at (token
->location
,
15671 "template parameter packs cannot have "
15672 "default arguments");
15673 default_argument
= NULL_TREE
;
15675 else if (check_for_bare_parameter_packs (default_argument
))
15676 default_argument
= error_mark_node
;
15679 default_argument
= NULL_TREE
;
15681 /* Create the combined representation of the parameter and the
15682 default argument. */
15683 parameter
= build_tree_list (default_argument
, parameter
);
15690 tree default_argument
;
15692 /* Look for the `<'. */
15693 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15694 /* Parse the template-parameter-list. */
15695 cp_parser_template_parameter_list (parser
);
15696 /* Look for the `>'. */
15697 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15699 // If template requirements are present, parse them.
15702 tree reqs
= get_shorthand_constraints (current_template_parms
);
15703 if (tree r
= cp_parser_requires_clause_opt (parser
))
15704 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
15705 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
15708 /* Look for the `class' or 'typename' keywords. */
15709 cp_parser_type_parameter_key (parser
);
15710 /* If the next token is an ellipsis, we have a template
15712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15714 /* Consume the `...' token. */
15715 cp_lexer_consume_token (parser
->lexer
);
15716 maybe_warn_variadic_templates ();
15718 *is_parameter_pack
= true;
15720 /* If the next token is an `=', then there is a
15721 default-argument. If the next token is a `>', we are at
15722 the end of the parameter-list. If the next token is a `,',
15723 then we are at the end of this parameter. */
15724 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
15725 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
15726 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15728 identifier
= cp_parser_identifier (parser
);
15729 /* Treat invalid names as if the parameter were nameless. */
15730 if (identifier
== error_mark_node
)
15731 identifier
= NULL_TREE
;
15734 identifier
= NULL_TREE
;
15736 /* Create the template parameter. */
15737 parameter
= finish_template_template_parm (class_type_node
,
15740 /* If the next token is an `=', then there is a
15741 default-argument. */
15742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15745 = cp_parser_default_template_template_argument (parser
);
15747 /* Template parameter packs cannot have default
15749 if (*is_parameter_pack
)
15752 error_at (token
->location
,
15753 "template parameter pack %qD cannot "
15754 "have a default argument",
15757 error_at (token
->location
, "template parameter packs cannot "
15758 "have default arguments");
15759 default_argument
= NULL_TREE
;
15763 default_argument
= NULL_TREE
;
15765 /* Create the combined representation of the parameter and the
15766 default argument. */
15767 parameter
= build_tree_list (default_argument
, parameter
);
15772 gcc_unreachable ();
15779 /* Parse a template-id.
15782 template-name < template-argument-list [opt] >
15784 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15785 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15786 returned. Otherwise, if the template-name names a function, or set
15787 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15788 names a class, returns a TYPE_DECL for the specialization.
15790 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15791 uninstantiated templates. */
15794 cp_parser_template_id (cp_parser
*parser
,
15795 bool template_keyword_p
,
15796 bool check_dependency_p
,
15797 enum tag_types tag_type
,
15798 bool is_declaration
)
15803 cp_token_position start_of_id
= 0;
15804 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
15805 bool is_identifier
;
15807 /* If the next token corresponds to a template-id, there is no need
15809 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15810 if (token
->type
== CPP_TEMPLATE_ID
)
15812 cp_lexer_consume_token (parser
->lexer
);
15813 return saved_checks_value (token
->u
.tree_check_value
);
15816 /* Avoid performing name lookup if there is no possibility of
15817 finding a template-id. */
15818 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
15819 || (token
->type
== CPP_NAME
15820 && !cp_parser_nth_token_starts_template_argument_list_p
15823 cp_parser_error (parser
, "expected template-id");
15824 return error_mark_node
;
15827 /* Remember where the template-id starts. */
15828 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
15829 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
15831 push_deferring_access_checks (dk_deferred
);
15833 /* Parse the template-name. */
15834 is_identifier
= false;
15835 templ
= cp_parser_template_name (parser
, template_keyword_p
,
15836 check_dependency_p
,
15840 if (templ
== error_mark_node
|| is_identifier
)
15842 pop_deferring_access_checks ();
15846 /* Since we're going to preserve any side-effects from this parse, set up a
15847 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15848 in the template arguments. */
15849 tentative_firewall
firewall (parser
);
15851 /* If we find the sequence `[:' after a template-name, it's probably
15852 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15853 parse correctly the argument list. */
15854 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
15855 == CPP_OPEN_SQUARE
)
15856 && next_token
->flags
& DIGRAPH
15857 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
15859 && !(next_token_2
->flags
& PREV_WHITE
))
15861 cp_parser_parse_tentatively (parser
);
15862 /* Change `:' into `::'. */
15863 next_token_2
->type
= CPP_SCOPE
;
15864 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15866 cp_lexer_consume_token (parser
->lexer
);
15868 /* Parse the arguments. */
15869 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15870 if (!cp_parser_parse_definitely (parser
))
15872 /* If we couldn't parse an argument list, then we revert our changes
15873 and return simply an error. Maybe this is not a template-id
15875 next_token_2
->type
= CPP_COLON
;
15876 cp_parser_error (parser
, "expected %<<%>");
15877 pop_deferring_access_checks ();
15878 return error_mark_node
;
15880 /* Otherwise, emit an error about the invalid digraph, but continue
15881 parsing because we got our argument list. */
15882 if (permerror (next_token
->location
,
15883 "%<<::%> cannot begin a template-argument list"))
15885 static bool hint
= false;
15886 inform (next_token
->location
,
15887 "%<<:%> is an alternate spelling for %<[%>."
15888 " Insert whitespace between %<<%> and %<::%>");
15889 if (!hint
&& !flag_permissive
)
15891 inform (next_token
->location
, "(if you use %<-fpermissive%> "
15892 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15893 "accept your code)");
15900 /* Look for the `<' that starts the template-argument-list. */
15901 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
15903 pop_deferring_access_checks ();
15904 return error_mark_node
;
15906 /* Parse the arguments. */
15907 arguments
= cp_parser_enclosed_template_argument_list (parser
);
15910 /* Set the location to be of the form:
15911 template-name < template-argument-list [opt] >
15912 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15913 with caret == start at the start of the template-name,
15914 ranging until the closing '>'. */
15915 location_t finish_loc
15916 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
15917 location_t combined_loc
15918 = make_location (token
->location
, token
->location
, finish_loc
);
15920 /* Check for concepts autos where they don't belong. We could
15921 identify types in some cases of idnetifier TEMPL, looking ahead
15922 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15923 types. We reject them in functions, but if what we have is an
15924 identifier, even with none_type we can't conclude it's NOT a
15925 type, we have to wait for template substitution. */
15926 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
15927 template_id
= error_mark_node
;
15928 /* Build a representation of the specialization. */
15929 else if (identifier_p (templ
))
15930 template_id
= build_min_nt_loc (combined_loc
,
15933 else if (DECL_TYPE_TEMPLATE_P (templ
)
15934 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
15936 bool entering_scope
;
15937 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15938 template (rather than some instantiation thereof) only if
15939 is not nested within some other construct. For example, in
15940 "template <typename T> void f(T) { A<T>::", A<T> is just an
15941 instantiation of A. */
15942 entering_scope
= (template_parm_scope_p ()
15943 && cp_lexer_next_token_is (parser
->lexer
,
15946 = finish_template_type (templ
, arguments
, entering_scope
);
15948 /* A template-like identifier may be a partial concept id. */
15949 else if (flag_concepts
15950 && (template_id
= (cp_parser_maybe_partial_concept_id
15951 (parser
, templ
, arguments
))))
15952 return template_id
;
15953 else if (variable_template_p (templ
))
15955 template_id
= lookup_template_variable (templ
, arguments
);
15956 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15957 SET_EXPR_LOCATION (template_id
, combined_loc
);
15961 /* If it's not a class-template or a template-template, it should be
15962 a function-template. */
15963 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
15964 || TREE_CODE (templ
) == OVERLOAD
15965 || BASELINK_P (templ
)));
15967 template_id
= lookup_template_function (templ
, arguments
);
15968 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
15969 SET_EXPR_LOCATION (template_id
, combined_loc
);
15972 /* If parsing tentatively, replace the sequence of tokens that makes
15973 up the template-id with a CPP_TEMPLATE_ID token. That way,
15974 should we re-parse the token stream, we will not have to repeat
15975 the effort required to do the parse, nor will we issue duplicate
15976 error messages about problems during instantiation of the
15979 /* Don't do this if we had a parse error in a declarator; re-parsing
15980 might succeed if a name changes meaning (60361). */
15981 && !(cp_parser_error_occurred (parser
)
15982 && cp_parser_parsing_tentatively (parser
)
15983 && parser
->in_declarator_p
))
15985 /* Reset the contents of the START_OF_ID token. */
15986 token
->type
= CPP_TEMPLATE_ID
;
15987 token
->location
= combined_loc
;
15989 /* We must mark the lookup as kept, so we don't throw it away on
15990 the first parse. */
15991 if (is_overloaded_fn (template_id
))
15992 lookup_keep (get_fns (template_id
), true);
15994 /* Retrieve any deferred checks. Do not pop this access checks yet
15995 so the memory will not be reclaimed during token replacing below. */
15996 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15997 token
->u
.tree_check_value
->value
= template_id
;
15998 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15999 token
->keyword
= RID_MAX
;
16001 /* Purge all subsequent tokens. */
16002 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16004 /* ??? Can we actually assume that, if template_id ==
16005 error_mark_node, we will have issued a diagnostic to the
16006 user, as opposed to simply marking the tentative parse as
16008 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16009 error_at (token
->location
, "parse error in template argument list");
16012 pop_to_parent_deferring_access_checks ();
16013 return template_id
;
16016 /* Parse a template-name.
16021 The standard should actually say:
16025 operator-function-id
16027 A defect report has been filed about this issue.
16029 A conversion-function-id cannot be a template name because they cannot
16030 be part of a template-id. In fact, looking at this code:
16032 a.operator K<int>()
16034 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16035 It is impossible to call a templated conversion-function-id with an
16036 explicit argument list, since the only allowed template parameter is
16037 the type to which it is converting.
16039 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16040 `template' keyword, in a construction like:
16044 In that case `f' is taken to be a template-name, even though there
16045 is no way of knowing for sure.
16047 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16048 name refers to a set of overloaded functions, at least one of which
16049 is a template, or an IDENTIFIER_NODE with the name of the template,
16050 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16051 names are looked up inside uninstantiated templates. */
16054 cp_parser_template_name (cp_parser
* parser
,
16055 bool template_keyword_p
,
16056 bool check_dependency_p
,
16057 bool is_declaration
,
16058 enum tag_types tag_type
,
16059 bool *is_identifier
)
16063 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16065 /* If the next token is `operator', then we have either an
16066 operator-function-id or a conversion-function-id. */
16067 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16069 /* We don't know whether we're looking at an
16070 operator-function-id or a conversion-function-id. */
16071 cp_parser_parse_tentatively (parser
);
16072 /* Try an operator-function-id. */
16073 identifier
= cp_parser_operator_function_id (parser
);
16074 /* If that didn't work, try a conversion-function-id. */
16075 if (!cp_parser_parse_definitely (parser
))
16077 cp_parser_error (parser
, "expected template-name");
16078 return error_mark_node
;
16081 /* Look for the identifier. */
16083 identifier
= cp_parser_identifier (parser
);
16085 /* If we didn't find an identifier, we don't have a template-id. */
16086 if (identifier
== error_mark_node
)
16087 return error_mark_node
;
16089 /* If the name immediately followed the `template' keyword, then it
16090 is a template-name. However, if the next token is not `<', then
16091 we do not treat it as a template-name, since it is not being used
16092 as part of a template-id. This enables us to handle constructs
16095 template <typename T> struct S { S(); };
16096 template <typename T> S<T>::S();
16098 correctly. We would treat `S' as a template -- if it were `S<T>'
16099 -- but we do not if there is no `<'. */
16101 if (processing_template_decl
16102 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16104 /* In a declaration, in a dependent context, we pretend that the
16105 "template" keyword was present in order to improve error
16106 recovery. For example, given:
16108 template <typename T> void f(T::X<int>);
16110 we want to treat "X<int>" as a template-id. */
16112 && !template_keyword_p
16113 && parser
->scope
&& TYPE_P (parser
->scope
)
16114 && check_dependency_p
16115 && dependent_scope_p (parser
->scope
)
16116 /* Do not do this for dtors (or ctors), since they never
16117 need the template keyword before their name. */
16118 && !constructor_name_p (identifier
, parser
->scope
))
16120 cp_token_position start
= 0;
16122 /* Explain what went wrong. */
16123 error_at (token
->location
, "non-template %qD used as template",
16125 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16126 parser
->scope
, identifier
);
16127 /* If parsing tentatively, find the location of the "<" token. */
16128 if (cp_parser_simulate_error (parser
))
16129 start
= cp_lexer_token_position (parser
->lexer
, true);
16130 /* Parse the template arguments so that we can issue error
16131 messages about them. */
16132 cp_lexer_consume_token (parser
->lexer
);
16133 cp_parser_enclosed_template_argument_list (parser
);
16134 /* Skip tokens until we find a good place from which to
16135 continue parsing. */
16136 cp_parser_skip_to_closing_parenthesis (parser
,
16137 /*recovering=*/true,
16139 /*consume_paren=*/false);
16140 /* If parsing tentatively, permanently remove the
16141 template argument list. That will prevent duplicate
16142 error messages from being issued about the missing
16143 "template" keyword. */
16145 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16147 *is_identifier
= true;
16148 parser
->context
->object_type
= NULL_TREE
;
16152 /* If the "template" keyword is present, then there is generally
16153 no point in doing name-lookup, so we just return IDENTIFIER.
16154 But, if the qualifying scope is non-dependent then we can
16155 (and must) do name-lookup normally. */
16156 if (template_keyword_p
)
16158 tree scope
= (parser
->scope
? parser
->scope
16159 : parser
->context
->object_type
);
16160 if (scope
&& TYPE_P (scope
)
16161 && (!CLASS_TYPE_P (scope
)
16162 || (check_dependency_p
&& dependent_type_p (scope
))))
16164 /* We're optimizing away the call to cp_parser_lookup_name, but
16165 we still need to do this. */
16166 parser
->context
->object_type
= NULL_TREE
;
16172 /* Look up the name. */
16173 decl
= cp_parser_lookup_name (parser
, identifier
,
16175 /*is_template=*/true,
16176 /*is_namespace=*/false,
16177 check_dependency_p
,
16178 /*ambiguous_decls=*/NULL
,
16181 decl
= strip_using_decl (decl
);
16183 /* If DECL is a template, then the name was a template-name. */
16184 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16186 if (TREE_DEPRECATED (decl
)
16187 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16188 warn_deprecated_use (decl
, NULL_TREE
);
16192 /* The standard does not explicitly indicate whether a name that
16193 names a set of overloaded declarations, some of which are
16194 templates, is a template-name. However, such a name should
16195 be a template-name; otherwise, there is no way to form a
16196 template-id for the overloaded templates. */
16197 bool found
= false;
16199 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16200 !found
&& iter
; ++iter
)
16201 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16206 /* The name does not name a template. */
16207 cp_parser_error (parser
, "expected template-name");
16208 return error_mark_node
;
16212 /* If DECL is dependent, and refers to a function, then just return
16213 its name; we will look it up again during template instantiation. */
16214 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
16216 tree scope
= ovl_scope (decl
);
16217 if (TYPE_P (scope
) && dependent_type_p (scope
))
16224 /* Parse a template-argument-list.
16226 template-argument-list:
16227 template-argument ... [opt]
16228 template-argument-list , template-argument ... [opt]
16230 Returns a TREE_VEC containing the arguments. */
16233 cp_parser_template_argument_list (cp_parser
* parser
)
16235 tree fixed_args
[10];
16236 unsigned n_args
= 0;
16237 unsigned alloced
= 10;
16238 tree
*arg_ary
= fixed_args
;
16240 bool saved_in_template_argument_list_p
;
16242 bool saved_non_ice_p
;
16244 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16245 parser
->in_template_argument_list_p
= true;
16246 /* Even if the template-id appears in an integral
16247 constant-expression, the contents of the argument list do
16249 saved_ice_p
= parser
->integral_constant_expression_p
;
16250 parser
->integral_constant_expression_p
= false;
16251 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16252 parser
->non_integral_constant_expression_p
= false;
16254 /* Parse the arguments. */
16260 /* Consume the comma. */
16261 cp_lexer_consume_token (parser
->lexer
);
16263 /* Parse the template-argument. */
16264 argument
= cp_parser_template_argument (parser
);
16266 /* If the next token is an ellipsis, we're expanding a template
16268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16270 if (argument
== error_mark_node
)
16272 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16273 error_at (token
->location
,
16274 "expected parameter pack before %<...%>");
16276 /* Consume the `...' token. */
16277 cp_lexer_consume_token (parser
->lexer
);
16279 /* Make the argument into a TYPE_PACK_EXPANSION or
16280 EXPR_PACK_EXPANSION. */
16281 argument
= make_pack_expansion (argument
);
16284 if (n_args
== alloced
)
16288 if (arg_ary
== fixed_args
)
16290 arg_ary
= XNEWVEC (tree
, alloced
);
16291 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16294 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16296 arg_ary
[n_args
++] = argument
;
16298 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16300 vec
= make_tree_vec (n_args
);
16303 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
16305 if (arg_ary
!= fixed_args
)
16307 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
16308 parser
->integral_constant_expression_p
= saved_ice_p
;
16309 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
16311 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
16315 /* Parse a template-argument.
16318 assignment-expression
16322 The representation is that of an assignment-expression, type-id, or
16323 id-expression -- except that the qualified id-expression is
16324 evaluated, so that the value returned is either a DECL or an
16327 Although the standard says "assignment-expression", it forbids
16328 throw-expressions or assignments in the template argument.
16329 Therefore, we use "conditional-expression" instead. */
16332 cp_parser_template_argument (cp_parser
* parser
)
16337 bool maybe_type_id
= false;
16338 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
16339 location_t loc
= 0;
16342 /* There's really no way to know what we're looking at, so we just
16343 try each alternative in order.
16347 In a template-argument, an ambiguity between a type-id and an
16348 expression is resolved to a type-id, regardless of the form of
16349 the corresponding template-parameter.
16351 Therefore, we try a type-id first. */
16352 cp_parser_parse_tentatively (parser
);
16353 argument
= cp_parser_template_type_arg (parser
);
16354 /* If there was no error parsing the type-id but the next token is a
16355 '>>', our behavior depends on which dialect of C++ we're
16356 parsing. In C++98, we probably found a typo for '> >'. But there
16357 are type-id which are also valid expressions. For instance:
16359 struct X { int operator >> (int); };
16360 template <int V> struct Foo {};
16363 Here 'X()' is a valid type-id of a function type, but the user just
16364 wanted to write the expression "X() >> 5". Thus, we remember that we
16365 found a valid type-id, but we still try to parse the argument as an
16366 expression to see what happens.
16368 In C++0x, the '>>' will be considered two separate '>'
16370 if (!cp_parser_error_occurred (parser
)
16371 && cxx_dialect
== cxx98
16372 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
16374 maybe_type_id
= true;
16375 cp_parser_abort_tentative_parse (parser
);
16379 /* If the next token isn't a `,' or a `>', then this argument wasn't
16380 really finished. This means that the argument is not a valid
16382 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16383 cp_parser_error (parser
, "expected template-argument");
16384 /* If that worked, we're done. */
16385 if (cp_parser_parse_definitely (parser
))
16388 /* We're still not sure what the argument will be. */
16389 cp_parser_parse_tentatively (parser
);
16390 /* Try a template. */
16391 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
16392 argument
= cp_parser_id_expression (parser
,
16393 /*template_keyword_p=*/false,
16394 /*check_dependency_p=*/true,
16396 /*declarator_p=*/false,
16397 /*optional_p=*/false);
16398 /* If the next token isn't a `,' or a `>', then this argument wasn't
16399 really finished. */
16400 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16401 cp_parser_error (parser
, "expected template-argument");
16402 if (!cp_parser_error_occurred (parser
))
16404 /* Figure out what is being referred to. If the id-expression
16405 was for a class template specialization, then we will have a
16406 TYPE_DECL at this point. There is no need to do name lookup
16407 at this point in that case. */
16408 if (TREE_CODE (argument
) != TYPE_DECL
)
16409 argument
= cp_parser_lookup_name (parser
, argument
,
16411 /*is_template=*/template_p
,
16412 /*is_namespace=*/false,
16413 /*check_dependency=*/true,
16414 /*ambiguous_decls=*/NULL
,
16415 argument_start_token
->location
);
16416 /* Handle a constrained-type-specifier for a non-type template
16418 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
16420 else if (TREE_CODE (argument
) != TEMPLATE_DECL
16421 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
16422 cp_parser_error (parser
, "expected template-name");
16424 if (cp_parser_parse_definitely (parser
))
16426 if (TREE_DEPRECATED (argument
))
16427 warn_deprecated_use (argument
, NULL_TREE
);
16430 /* It must be a non-type argument. In C++17 any constant-expression is
16432 if (cxx_dialect
> cxx14
)
16435 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16437 -- an integral constant-expression of integral or enumeration
16440 -- the name of a non-type template-parameter; or
16442 -- the name of an object or function with external linkage...
16444 -- the address of an object or function with external linkage...
16446 -- a pointer to member... */
16447 /* Look for a non-type template parameter. */
16448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16450 cp_parser_parse_tentatively (parser
);
16451 argument
= cp_parser_primary_expression (parser
,
16452 /*address_p=*/false,
16454 /*template_arg_p=*/true,
16456 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
16457 || !cp_parser_next_token_ends_template_argument_p (parser
))
16458 cp_parser_simulate_error (parser
);
16459 if (cp_parser_parse_definitely (parser
))
16463 /* If the next token is "&", the argument must be the address of an
16464 object or function with external linkage. */
16465 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
16468 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16469 cp_lexer_consume_token (parser
->lexer
);
16471 /* See if we might have an id-expression. */
16472 token
= cp_lexer_peek_token (parser
->lexer
);
16473 if (token
->type
== CPP_NAME
16474 || token
->keyword
== RID_OPERATOR
16475 || token
->type
== CPP_SCOPE
16476 || token
->type
== CPP_TEMPLATE_ID
16477 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
16479 cp_parser_parse_tentatively (parser
);
16480 argument
= cp_parser_primary_expression (parser
,
16483 /*template_arg_p=*/true,
16485 if (cp_parser_error_occurred (parser
)
16486 || !cp_parser_next_token_ends_template_argument_p (parser
))
16487 cp_parser_abort_tentative_parse (parser
);
16492 if (INDIRECT_REF_P (argument
))
16494 /* Strip the dereference temporarily. */
16495 gcc_assert (REFERENCE_REF_P (argument
));
16496 argument
= TREE_OPERAND (argument
, 0);
16499 /* If we're in a template, we represent a qualified-id referring
16500 to a static data member as a SCOPE_REF even if the scope isn't
16501 dependent so that we can check access control later. */
16503 if (TREE_CODE (probe
) == SCOPE_REF
)
16504 probe
= TREE_OPERAND (probe
, 1);
16507 /* A variable without external linkage might still be a
16508 valid constant-expression, so no error is issued here
16509 if the external-linkage check fails. */
16510 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
16511 cp_parser_simulate_error (parser
);
16513 else if (is_overloaded_fn (argument
))
16514 /* All overloaded functions are allowed; if the external
16515 linkage test does not pass, an error will be issued
16519 && (TREE_CODE (argument
) == OFFSET_REF
16520 || TREE_CODE (argument
) == SCOPE_REF
))
16521 /* A pointer-to-member. */
16523 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
16526 cp_parser_simulate_error (parser
);
16528 if (cp_parser_parse_definitely (parser
))
16531 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
16532 tf_warning_or_error
);
16534 argument
= convert_from_reference (argument
);
16539 /* If the argument started with "&", there are no other valid
16540 alternatives at this point. */
16543 cp_parser_error (parser
, "invalid non-type template argument");
16544 return error_mark_node
;
16548 /* If the argument wasn't successfully parsed as a type-id followed
16549 by '>>', the argument can only be a constant expression now.
16550 Otherwise, we try parsing the constant-expression tentatively,
16551 because the argument could really be a type-id. */
16553 cp_parser_parse_tentatively (parser
);
16555 if (cxx_dialect
<= cxx14
)
16556 argument
= cp_parser_constant_expression (parser
);
16559 /* With C++17 generalized non-type template arguments we need to handle
16560 lvalue constant expressions, too. */
16561 argument
= cp_parser_assignment_expression (parser
);
16562 require_potential_constant_expression (argument
);
16565 if (!maybe_type_id
)
16567 if (!cp_parser_next_token_ends_template_argument_p (parser
))
16568 cp_parser_error (parser
, "expected template-argument");
16569 if (cp_parser_parse_definitely (parser
))
16571 /* We did our best to parse the argument as a non type-id, but that
16572 was the only alternative that matched (albeit with a '>' after
16573 it). We can assume it's just a typo from the user, and a
16574 diagnostic will then be issued. */
16575 return cp_parser_template_type_arg (parser
);
16578 /* Parse an explicit-instantiation.
16580 explicit-instantiation:
16581 template declaration
16583 Although the standard says `declaration', what it really means is:
16585 explicit-instantiation:
16586 template decl-specifier-seq [opt] declarator [opt] ;
16588 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16589 supposed to be allowed. A defect report has been filed about this
16594 explicit-instantiation:
16595 storage-class-specifier template
16596 decl-specifier-seq [opt] declarator [opt] ;
16597 function-specifier template
16598 decl-specifier-seq [opt] declarator [opt] ; */
16601 cp_parser_explicit_instantiation (cp_parser
* parser
)
16603 int declares_class_or_enum
;
16604 cp_decl_specifier_seq decl_specifiers
;
16605 tree extension_specifier
= NULL_TREE
;
16607 timevar_push (TV_TEMPLATE_INST
);
16609 /* Look for an (optional) storage-class-specifier or
16610 function-specifier. */
16611 if (cp_parser_allow_gnu_extensions_p (parser
))
16613 extension_specifier
16614 = cp_parser_storage_class_specifier_opt (parser
);
16615 if (!extension_specifier
)
16616 extension_specifier
16617 = cp_parser_function_specifier_opt (parser
,
16618 /*decl_specs=*/NULL
);
16621 /* Look for the `template' keyword. */
16622 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16623 /* Let the front end know that we are processing an explicit
16625 begin_explicit_instantiation ();
16626 /* [temp.explicit] says that we are supposed to ignore access
16627 control while processing explicit instantiation directives. */
16628 push_deferring_access_checks (dk_no_check
);
16629 /* Parse a decl-specifier-seq. */
16630 cp_parser_decl_specifier_seq (parser
,
16631 CP_PARSER_FLAGS_OPTIONAL
,
16633 &declares_class_or_enum
);
16634 /* If there was exactly one decl-specifier, and it declared a class,
16635 and there's no declarator, then we have an explicit type
16637 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
16641 type
= check_tag_decl (&decl_specifiers
,
16642 /*explicit_type_instantiation_p=*/true);
16643 /* Turn access control back on for names used during
16644 template instantiation. */
16645 pop_deferring_access_checks ();
16647 do_type_instantiation (type
, extension_specifier
,
16648 /*complain=*/tf_error
);
16652 cp_declarator
*declarator
;
16655 /* Parse the declarator. */
16657 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16658 /*ctor_dtor_or_conv_p=*/NULL
,
16659 /*parenthesized_p=*/NULL
,
16660 /*member_p=*/false,
16661 /*friend_p=*/false);
16662 if (declares_class_or_enum
& 2)
16663 cp_parser_check_for_definition_in_return_type (declarator
,
16664 decl_specifiers
.type
,
16665 decl_specifiers
.locations
[ds_type_spec
]);
16666 if (declarator
!= cp_error_declarator
)
16668 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
16669 permerror (decl_specifiers
.locations
[ds_inline
],
16670 "explicit instantiation shall not use"
16671 " %<inline%> specifier");
16672 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
16673 permerror (decl_specifiers
.locations
[ds_constexpr
],
16674 "explicit instantiation shall not use"
16675 " %<constexpr%> specifier");
16677 decl
= grokdeclarator (declarator
, &decl_specifiers
,
16678 NORMAL
, 0, &decl_specifiers
.attributes
);
16679 /* Turn access control back on for names used during
16680 template instantiation. */
16681 pop_deferring_access_checks ();
16682 /* Do the explicit instantiation. */
16683 do_decl_instantiation (decl
, extension_specifier
);
16687 pop_deferring_access_checks ();
16688 /* Skip the body of the explicit instantiation. */
16689 cp_parser_skip_to_end_of_statement (parser
);
16692 /* We're done with the instantiation. */
16693 end_explicit_instantiation ();
16695 cp_parser_consume_semicolon_at_end_of_statement (parser
);
16697 timevar_pop (TV_TEMPLATE_INST
);
16700 /* Parse an explicit-specialization.
16702 explicit-specialization:
16703 template < > declaration
16705 Although the standard says `declaration', what it really means is:
16707 explicit-specialization:
16708 template <> decl-specifier [opt] init-declarator [opt] ;
16709 template <> function-definition
16710 template <> explicit-specialization
16711 template <> template-declaration */
16714 cp_parser_explicit_specialization (cp_parser
* parser
)
16716 bool need_lang_pop
;
16717 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16719 /* Look for the `template' keyword. */
16720 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
16721 /* Look for the `<'. */
16722 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16723 /* Look for the `>'. */
16724 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16725 /* We have processed another parameter list. */
16726 ++parser
->num_template_parameter_lists
;
16729 A template ... explicit specialization ... shall not have C
16731 if (current_lang_name
== lang_name_c
)
16733 error_at (token
->location
, "template specialization with C linkage");
16734 maybe_show_extern_c_location ();
16735 /* Give it C++ linkage to avoid confusing other parts of the
16737 push_lang_context (lang_name_cplusplus
);
16738 need_lang_pop
= true;
16741 need_lang_pop
= false;
16742 /* Let the front end know that we are beginning a specialization. */
16743 if (!begin_specialization ())
16745 end_specialization ();
16749 /* If the next keyword is `template', we need to figure out whether
16750 or not we're looking a template-declaration. */
16751 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
16753 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
16754 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
16755 cp_parser_template_declaration_after_export (parser
,
16756 /*member_p=*/false);
16758 cp_parser_explicit_specialization (parser
);
16761 /* Parse the dependent declaration. */
16762 cp_parser_single_declaration (parser
,
16764 /*member_p=*/false,
16765 /*explicit_specialization_p=*/true,
16766 /*friend_p=*/NULL
);
16767 /* We're done with the specialization. */
16768 end_specialization ();
16769 /* For the erroneous case of a template with C linkage, we pushed an
16770 implicit C++ linkage scope; exit that scope now. */
16772 pop_lang_context ();
16773 /* We're done with this parameter list. */
16774 --parser
->num_template_parameter_lists
;
16777 /* Parse a type-specifier.
16780 simple-type-specifier
16783 elaborated-type-specifier
16791 Returns a representation of the type-specifier. For a
16792 class-specifier, enum-specifier, or elaborated-type-specifier, a
16793 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16795 The parser flags FLAGS is used to control type-specifier parsing.
16797 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16798 in a decl-specifier-seq.
16800 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16801 class-specifier, enum-specifier, or elaborated-type-specifier, then
16802 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16803 if a type is declared; 2 if it is defined. Otherwise, it is set to
16806 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16807 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16808 is set to FALSE. */
16811 cp_parser_type_specifier (cp_parser
* parser
,
16812 cp_parser_flags flags
,
16813 cp_decl_specifier_seq
*decl_specs
,
16814 bool is_declaration
,
16815 int* declares_class_or_enum
,
16816 bool* is_cv_qualifier
)
16818 tree type_spec
= NULL_TREE
;
16821 cp_decl_spec ds
= ds_last
;
16823 /* Assume this type-specifier does not declare a new type. */
16824 if (declares_class_or_enum
)
16825 *declares_class_or_enum
= 0;
16826 /* And that it does not specify a cv-qualifier. */
16827 if (is_cv_qualifier
)
16828 *is_cv_qualifier
= false;
16829 /* Peek at the next token. */
16830 token
= cp_lexer_peek_token (parser
->lexer
);
16832 /* If we're looking at a keyword, we can use that to guide the
16833 production we choose. */
16834 keyword
= token
->keyword
;
16838 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16839 goto elaborated_type_specifier
;
16841 /* Look for the enum-specifier. */
16842 type_spec
= cp_parser_enum_specifier (parser
);
16843 /* If that worked, we're done. */
16846 if (declares_class_or_enum
)
16847 *declares_class_or_enum
= 2;
16849 cp_parser_set_decl_spec_type (decl_specs
,
16852 /*type_definition_p=*/true);
16856 goto elaborated_type_specifier
;
16858 /* Any of these indicate either a class-specifier, or an
16859 elaborated-type-specifier. */
16863 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
16864 goto elaborated_type_specifier
;
16866 /* Parse tentatively so that we can back up if we don't find a
16867 class-specifier. */
16868 cp_parser_parse_tentatively (parser
);
16869 /* Look for the class-specifier. */
16870 type_spec
= cp_parser_class_specifier (parser
);
16871 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
16872 /* If that worked, we're done. */
16873 if (cp_parser_parse_definitely (parser
))
16875 if (declares_class_or_enum
)
16876 *declares_class_or_enum
= 2;
16878 cp_parser_set_decl_spec_type (decl_specs
,
16881 /*type_definition_p=*/true);
16885 /* Fall through. */
16886 elaborated_type_specifier
:
16887 /* We're declaring (not defining) a class or enum. */
16888 if (declares_class_or_enum
)
16889 *declares_class_or_enum
= 1;
16891 /* Fall through. */
16893 /* Look for an elaborated-type-specifier. */
16895 = (cp_parser_elaborated_type_specifier
16897 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
16900 cp_parser_set_decl_spec_type (decl_specs
,
16903 /*type_definition_p=*/false);
16908 if (is_cv_qualifier
)
16909 *is_cv_qualifier
= true;
16914 if (is_cv_qualifier
)
16915 *is_cv_qualifier
= true;
16920 if (is_cv_qualifier
)
16921 *is_cv_qualifier
= true;
16925 /* The `__complex__' keyword is a GNU extension. */
16933 /* Handle simple keywords. */
16938 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
16939 decl_specs
->any_specifiers_p
= true;
16941 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
16944 /* If we do not already have a type-specifier, assume we are looking
16945 at a simple-type-specifier. */
16946 type_spec
= cp_parser_simple_type_specifier (parser
,
16950 /* If we didn't find a type-specifier, and a type-specifier was not
16951 optional in this context, issue an error message. */
16952 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16954 cp_parser_error (parser
, "expected type specifier");
16955 return error_mark_node
;
16961 /* Parse a simple-type-specifier.
16963 simple-type-specifier:
16964 :: [opt] nested-name-specifier [opt] type-name
16965 :: [opt] nested-name-specifier template template-id
16980 simple-type-specifier:
16982 decltype ( expression )
16985 __underlying_type ( type-id )
16989 nested-name-specifier(opt) template-name
16993 simple-type-specifier:
16995 __typeof__ unary-expression
16996 __typeof__ ( type-id )
16997 __typeof__ ( type-id ) { initializer-list , [opt] }
16999 Concepts Extension:
17001 simple-type-specifier:
17002 constrained-type-specifier
17004 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17005 appropriately updated. */
17008 cp_parser_simple_type_specifier (cp_parser
* parser
,
17009 cp_decl_specifier_seq
*decl_specs
,
17010 cp_parser_flags flags
)
17012 tree type
= NULL_TREE
;
17016 /* Peek at the next token. */
17017 token
= cp_lexer_peek_token (parser
->lexer
);
17019 /* If we're looking at a keyword, things are easy. */
17020 switch (token
->keyword
)
17024 decl_specs
->explicit_char_p
= true;
17025 type
= char_type_node
;
17028 type
= char16_type_node
;
17031 type
= char32_type_node
;
17034 type
= wchar_type_node
;
17037 type
= boolean_type_node
;
17040 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17041 type
= short_integer_type_node
;
17045 decl_specs
->explicit_int_p
= true;
17046 type
= integer_type_node
;
17052 idx
= token
->keyword
- RID_INT_N_0
;
17053 if (! int_n_enabled_p
[idx
])
17057 decl_specs
->explicit_intN_p
= true;
17058 decl_specs
->int_n_idx
= idx
;
17060 type
= int_n_trees
[idx
].signed_type
;
17064 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17065 type
= long_integer_type_node
;
17068 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17069 type
= integer_type_node
;
17072 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17073 type
= unsigned_type_node
;
17076 type
= float_type_node
;
17079 type
= double_type_node
;
17082 type
= void_type_node
;
17086 maybe_warn_cpp0x (CPP0X_AUTO
);
17087 if (parser
->auto_is_implicit_function_template_parm_p
)
17089 /* The 'auto' might be the placeholder return type for a function decl
17090 with trailing return type. */
17091 bool have_trailing_return_fn_decl
= false;
17093 cp_parser_parse_tentatively (parser
);
17094 cp_lexer_consume_token (parser
->lexer
);
17095 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17096 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17097 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17098 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17102 cp_lexer_consume_token (parser
->lexer
);
17103 cp_parser_skip_to_closing_parenthesis (parser
,
17104 /*recovering*/false,
17106 /*consume_paren*/true);
17110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17112 have_trailing_return_fn_decl
= true;
17116 cp_lexer_consume_token (parser
->lexer
);
17118 cp_parser_abort_tentative_parse (parser
);
17120 if (have_trailing_return_fn_decl
)
17122 type
= make_auto ();
17126 if (cxx_dialect
>= cxx14
)
17128 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17129 type
= TREE_TYPE (type
);
17132 type
= error_mark_node
;
17134 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17136 if (cxx_dialect
< cxx14
)
17137 error_at (token
->location
,
17138 "use of %<auto%> in lambda parameter declaration "
17139 "only available with "
17140 "-std=c++14 or -std=gnu++14");
17142 else if (cxx_dialect
< cxx14
)
17143 error_at (token
->location
,
17144 "use of %<auto%> in parameter declaration "
17145 "only available with "
17146 "-std=c++14 or -std=gnu++14");
17147 else if (!flag_concepts
)
17148 pedwarn (token
->location
, 0,
17149 "use of %<auto%> in parameter declaration "
17150 "only available with -fconcepts");
17153 type
= make_auto ();
17157 /* Since DR 743, decltype can either be a simple-type-specifier by
17158 itself or begin a nested-name-specifier. Parsing it will replace
17159 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17160 handling below decide what to do. */
17161 cp_parser_decltype (parser
);
17162 cp_lexer_set_token_position (parser
->lexer
, token
);
17166 /* Consume the `typeof' token. */
17167 cp_lexer_consume_token (parser
->lexer
);
17168 /* Parse the operand to `typeof'. */
17169 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17170 /* If it is not already a TYPE, take its type. */
17171 if (!TYPE_P (type
))
17172 type
= finish_typeof (type
);
17175 cp_parser_set_decl_spec_type (decl_specs
, type
,
17177 /*type_definition_p=*/false);
17181 case RID_UNDERLYING_TYPE
:
17182 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17184 cp_parser_set_decl_spec_type (decl_specs
, type
,
17186 /*type_definition_p=*/false);
17191 case RID_DIRECT_BASES
:
17192 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17194 cp_parser_set_decl_spec_type (decl_specs
, type
,
17196 /*type_definition_p=*/false);
17202 /* If token is an already-parsed decltype not followed by ::,
17203 it's a simple-type-specifier. */
17204 if (token
->type
== CPP_DECLTYPE
17205 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17207 type
= saved_checks_value (token
->u
.tree_check_value
);
17210 cp_parser_set_decl_spec_type (decl_specs
, type
,
17212 /*type_definition_p=*/false);
17213 /* Remember that we are handling a decltype in order to
17214 implement the resolution of DR 1510 when the argument
17215 isn't instantiation dependent. */
17216 decl_specs
->decltype_p
= true;
17218 cp_lexer_consume_token (parser
->lexer
);
17222 /* If the type-specifier was for a built-in type, we're done. */
17225 /* Record the type. */
17227 && (token
->keyword
!= RID_SIGNED
17228 && token
->keyword
!= RID_UNSIGNED
17229 && token
->keyword
!= RID_SHORT
17230 && token
->keyword
!= RID_LONG
))
17231 cp_parser_set_decl_spec_type (decl_specs
,
17234 /*type_definition_p=*/false);
17236 decl_specs
->any_specifiers_p
= true;
17238 /* Consume the token. */
17239 cp_lexer_consume_token (parser
->lexer
);
17241 if (type
== error_mark_node
)
17242 return error_mark_node
;
17244 /* There is no valid C++ program where a non-template type is
17245 followed by a "<". That usually indicates that the user thought
17246 that the type was a template. */
17247 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17250 return TYPE_NAME (type
);
17253 /* The type-specifier must be a user-defined type. */
17254 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17259 /* Don't gobble tokens or issue error messages if this is an
17260 optional type-specifier. */
17261 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17262 cp_parser_parse_tentatively (parser
);
17264 token
= cp_lexer_peek_token (parser
->lexer
);
17266 /* Look for the optional `::' operator. */
17268 = (cp_parser_global_scope_opt (parser
,
17269 /*current_scope_valid_p=*/false)
17271 /* Look for the nested-name specifier. */
17273 = (cp_parser_nested_name_specifier_opt (parser
,
17274 /*typename_keyword_p=*/false,
17275 /*check_dependency_p=*/true,
17277 /*is_declaration=*/false)
17279 /* If we have seen a nested-name-specifier, and the next token
17280 is `template', then we are using the template-id production. */
17282 && cp_parser_optional_template_keyword (parser
))
17284 /* Look for the template-id. */
17285 type
= cp_parser_template_id (parser
,
17286 /*template_keyword_p=*/true,
17287 /*check_dependency_p=*/true,
17289 /*is_declaration=*/false);
17290 /* If the template-id did not name a type, we are out of
17292 if (TREE_CODE (type
) != TYPE_DECL
)
17294 cp_parser_error (parser
, "expected template-id for type");
17298 /* Otherwise, look for a type-name. */
17300 type
= cp_parser_type_name (parser
);
17301 /* Keep track of all name-lookups performed in class scopes. */
17305 && TREE_CODE (type
) == TYPE_DECL
17306 && identifier_p (DECL_NAME (type
)))
17307 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
17308 /* If it didn't work out, we don't have a TYPE. */
17309 if (((flags
& CP_PARSER_FLAGS_OPTIONAL
) || cxx_dialect
>= cxx17
)
17310 && !cp_parser_parse_definitely (parser
))
17312 if (!type
&& cxx_dialect
>= cxx17
)
17314 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17315 cp_parser_parse_tentatively (parser
);
17317 cp_parser_global_scope_opt (parser
,
17318 /*current_scope_valid_p=*/false);
17319 cp_parser_nested_name_specifier_opt (parser
,
17320 /*typename_keyword_p=*/false,
17321 /*check_dependency_p=*/true,
17323 /*is_declaration=*/false);
17324 tree name
= cp_parser_identifier (parser
);
17325 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
17326 && parser
->scope
!= error_mark_node
)
17328 tree tmpl
= cp_parser_lookup_name (parser
, name
,
17330 /*is_template=*/false,
17331 /*is_namespace=*/false,
17332 /*check_dependency=*/true,
17333 /*ambiguous_decls=*/NULL
,
17335 if (tmpl
&& tmpl
!= error_mark_node
17336 && (DECL_CLASS_TEMPLATE_P (tmpl
)
17337 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
17338 type
= make_template_placeholder (tmpl
);
17341 type
= error_mark_node
;
17342 if (!cp_parser_simulate_error (parser
))
17343 cp_parser_name_lookup_error (parser
, name
, tmpl
,
17344 NLE_TYPE
, token
->location
);
17348 type
= error_mark_node
;
17350 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
17351 && !cp_parser_parse_definitely (parser
))
17354 if (type
&& decl_specs
)
17355 cp_parser_set_decl_spec_type (decl_specs
, type
,
17357 /*type_definition_p=*/false);
17360 /* If we didn't get a type-name, issue an error message. */
17361 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17363 cp_parser_error (parser
, "expected type-name");
17364 return error_mark_node
;
17367 if (type
&& type
!= error_mark_node
)
17369 /* See if TYPE is an Objective-C type, and if so, parse and
17370 accept any protocol references following it. Do this before
17371 the cp_parser_check_for_invalid_template_id() call, because
17372 Objective-C types can be followed by '<...>' which would
17373 enclose protocol names rather than template arguments, and so
17374 everything is fine. */
17375 if (c_dialect_objc () && !parser
->scope
17376 && (objc_is_id (type
) || objc_is_class_name (type
)))
17378 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17379 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
17381 /* Clobber the "unqualified" type previously entered into
17382 DECL_SPECS with the new, improved protocol-qualified version. */
17384 decl_specs
->type
= qual_type
;
17389 /* There is no valid C++ program where a non-template type is
17390 followed by a "<". That usually indicates that the user
17391 thought that the type was a template. */
17392 cp_parser_check_for_invalid_template_id (parser
, type
,
17400 /* Parse a type-name.
17406 simple-template-id [in c++0x]
17423 Returns a TYPE_DECL for the type. */
17426 cp_parser_type_name (cp_parser
* parser
)
17428 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
17433 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
17437 /* We can't know yet whether it is a class-name or not. */
17438 cp_parser_parse_tentatively (parser
);
17439 /* Try a class-name. */
17440 type_decl
= cp_parser_class_name (parser
,
17441 typename_keyword_p
,
17442 /*template_keyword_p=*/false,
17444 /*check_dependency_p=*/true,
17445 /*class_head_p=*/false,
17446 /*is_declaration=*/false);
17447 /* If it's not a class-name, keep looking. */
17448 if (!cp_parser_parse_definitely (parser
))
17450 if (cxx_dialect
< cxx11
)
17451 /* It must be a typedef-name or an enum-name. */
17452 return cp_parser_nonclass_name (parser
);
17454 cp_parser_parse_tentatively (parser
);
17455 /* It is either a simple-template-id representing an
17456 instantiation of an alias template... */
17457 type_decl
= cp_parser_template_id (parser
,
17458 /*template_keyword_p=*/false,
17459 /*check_dependency_p=*/true,
17461 /*is_declaration=*/false);
17462 /* Note that this must be an instantiation of an alias template
17463 because [temp.names]/6 says:
17465 A template-id that names an alias template specialization
17468 Whereas [temp.names]/7 says:
17470 A simple-template-id that names a class template
17471 specialization is a class-name.
17473 With concepts, this could also be a partial-concept-id that
17474 declares a non-type template parameter. */
17475 if (type_decl
!= NULL_TREE
17476 && TREE_CODE (type_decl
) == TYPE_DECL
17477 && TYPE_DECL_ALIAS_P (type_decl
))
17478 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
17479 else if (is_constrained_parameter (type_decl
))
17480 /* Don't do anything. */ ;
17482 cp_parser_simulate_error (parser
);
17484 if (!cp_parser_parse_definitely (parser
))
17485 /* ... Or a typedef-name or an enum-name. */
17486 return cp_parser_nonclass_name (parser
);
17492 /* Check if DECL and ARGS can form a constrained-type-specifier.
17493 If ARGS is non-null, we try to form a concept check of the
17494 form DECL<?, ARGS> where ? is a wildcard that matches any
17495 kind of template argument. If ARGS is NULL, then we try to
17496 form a concept check of the form DECL<?>. */
17499 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
17500 tree decl
, tree args
)
17502 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
17504 /* If we a constrained-type-specifier cannot be deduced. */
17505 if (parser
->prevent_constrained_type_specifiers
)
17508 /* A constrained type specifier can only be found in an
17509 overload set or as a reference to a template declaration.
17511 FIXME: This might be masking a bug. It's possible that
17512 that the deduction below is causing template specializations
17513 to be formed with the wildcard as an argument. */
17514 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
17517 /* Try to build a call expression that evaluates the
17518 concept. This can fail if the overload set refers
17519 only to non-templates. */
17520 tree placeholder
= build_nt (WILDCARD_DECL
);
17521 tree check
= build_concept_check (decl
, placeholder
, args
);
17522 if (check
== error_mark_node
)
17525 /* Deduce the checked constraint and the prototype parameter.
17527 FIXME: In certain cases, failure to deduce should be a
17528 diagnosable error. */
17531 if (!deduce_constrained_parameter (check
, conc
, proto
))
17534 /* In template parameter scope, this results in a constrained
17535 parameter. Return a descriptor of that parm. */
17536 if (processing_template_parmlist
)
17537 return build_constrained_parameter (conc
, proto
, args
);
17539 /* In a parameter-declaration-clause, constrained-type
17540 specifiers result in invented template parameters. */
17541 if (parser
->auto_is_implicit_function_template_parm_p
)
17543 tree x
= build_constrained_parameter (conc
, proto
, args
);
17544 return synthesize_implicit_template_parm (parser
, x
);
17548 /* Otherwise, we're in a context where the constrained
17549 type name is deduced and the constraint applies
17550 after deduction. */
17551 return make_constrained_auto (conc
, args
);
17557 /* If DECL refers to a concept, return a TYPE_DECL representing
17558 the result of using the constrained type specifier in the
17559 current context. DECL refers to a concept if
17561 - it is an overload set containing a function concept taking a single
17564 - it is a variable concept taking a single type argument. */
17567 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
17570 && (TREE_CODE (decl
) == OVERLOAD
17571 || BASELINK_P (decl
)
17572 || variable_concept_p (decl
)))
17573 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
17578 /* Check if DECL and ARGS form a partial-concept-id. If so,
17579 assign ID to the resulting constrained placeholder.
17581 Returns true if the partial-concept-id designates a placeholder
17582 and false otherwise. Note that *id is set to NULL_TREE in
17586 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
17588 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
17591 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17603 Returns a TYPE_DECL for the type. */
17606 cp_parser_nonclass_name (cp_parser
* parser
)
17611 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17612 identifier
= cp_parser_identifier (parser
);
17613 if (identifier
== error_mark_node
)
17614 return error_mark_node
;
17616 /* Look up the type-name. */
17617 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
17619 type_decl
= strip_using_decl (type_decl
);
17621 /* If we found an overload set, then it may refer to a concept-name. */
17622 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
17625 if (TREE_CODE (type_decl
) != TYPE_DECL
17626 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
17628 /* See if this is an Objective-C type. */
17629 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
17630 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
17632 type_decl
= TYPE_NAME (type
);
17635 /* Issue an error if we did not find a type-name. */
17636 if (TREE_CODE (type_decl
) != TYPE_DECL
17637 /* In Objective-C, we have the complication that class names are
17638 normally type names and start declarations (eg, the
17639 "NSObject" in "NSObject *object;"), but can be used in an
17640 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17641 is an expression. So, a classname followed by a dot is not a
17642 valid type-name. */
17643 || (objc_is_class_name (TREE_TYPE (type_decl
))
17644 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
17646 if (!cp_parser_simulate_error (parser
))
17647 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
17648 NLE_TYPE
, token
->location
);
17649 return error_mark_node
;
17651 /* Remember that the name was used in the definition of the
17652 current class so that we can check later to see if the
17653 meaning would have been different after the class was
17654 entirely defined. */
17655 else if (type_decl
!= error_mark_node
17657 maybe_note_name_used_in_class (identifier
, type_decl
);
17662 /* Parse an elaborated-type-specifier. Note that the grammar given
17663 here incorporates the resolution to DR68.
17665 elaborated-type-specifier:
17666 class-key :: [opt] nested-name-specifier [opt] identifier
17667 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17668 enum-key :: [opt] nested-name-specifier [opt] identifier
17669 typename :: [opt] nested-name-specifier identifier
17670 typename :: [opt] nested-name-specifier template [opt]
17675 elaborated-type-specifier:
17676 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17677 class-key attributes :: [opt] nested-name-specifier [opt]
17678 template [opt] template-id
17679 enum attributes :: [opt] nested-name-specifier [opt] identifier
17681 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17682 declared `friend'. If IS_DECLARATION is TRUE, then this
17683 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17684 something is being declared.
17686 Returns the TYPE specified. */
17689 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
17691 bool is_declaration
)
17693 enum tag_types tag_type
;
17695 tree type
= NULL_TREE
;
17696 tree attributes
= NULL_TREE
;
17698 cp_token
*token
= NULL
;
17700 /* See if we're looking at the `enum' keyword. */
17701 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
17703 /* Consume the `enum' token. */
17704 cp_lexer_consume_token (parser
->lexer
);
17705 /* Remember that it's an enumeration type. */
17706 tag_type
= enum_type
;
17707 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17708 enums) is used here. */
17709 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17710 if (cp_parser_is_keyword (token
, RID_CLASS
)
17711 || cp_parser_is_keyword (token
, RID_STRUCT
))
17713 gcc_rich_location
richloc (token
->location
);
17714 richloc
.add_range (input_location
, false);
17715 richloc
.add_fixit_remove ();
17716 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
17717 "a scoped enum must not use the %qD keyword",
17719 /* Consume the `struct' or `class' and parse it anyway. */
17720 cp_lexer_consume_token (parser
->lexer
);
17722 /* Parse the attributes. */
17723 attributes
= cp_parser_attributes_opt (parser
);
17725 /* Or, it might be `typename'. */
17726 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
17729 /* Consume the `typename' token. */
17730 cp_lexer_consume_token (parser
->lexer
);
17731 /* Remember that it's a `typename' type. */
17732 tag_type
= typename_type
;
17734 /* Otherwise it must be a class-key. */
17737 tag_type
= cp_parser_class_key (parser
);
17738 if (tag_type
== none_type
)
17739 return error_mark_node
;
17740 /* Parse the attributes. */
17741 attributes
= cp_parser_attributes_opt (parser
);
17744 /* Look for the `::' operator. */
17745 globalscope
= cp_parser_global_scope_opt (parser
,
17746 /*current_scope_valid_p=*/false);
17747 /* Look for the nested-name-specifier. */
17748 tree nested_name_specifier
;
17749 if (tag_type
== typename_type
&& !globalscope
)
17751 nested_name_specifier
17752 = cp_parser_nested_name_specifier (parser
,
17753 /*typename_keyword_p=*/true,
17754 /*check_dependency_p=*/true,
17757 if (!nested_name_specifier
)
17758 return error_mark_node
;
17761 /* Even though `typename' is not present, the proposed resolution
17762 to Core Issue 180 says that in `class A<T>::B', `B' should be
17763 considered a type-name, even if `A<T>' is dependent. */
17764 nested_name_specifier
17765 = cp_parser_nested_name_specifier_opt (parser
,
17766 /*typename_keyword_p=*/true,
17767 /*check_dependency_p=*/true,
17770 /* For everything but enumeration types, consider a template-id.
17771 For an enumeration type, consider only a plain identifier. */
17772 if (tag_type
!= enum_type
)
17774 bool template_p
= false;
17777 /* Allow the `template' keyword. */
17778 template_p
= cp_parser_optional_template_keyword (parser
);
17779 /* If we didn't see `template', we don't know if there's a
17780 template-id or not. */
17782 cp_parser_parse_tentatively (parser
);
17783 /* Parse the template-id. */
17784 token
= cp_lexer_peek_token (parser
->lexer
);
17785 decl
= cp_parser_template_id (parser
, template_p
,
17786 /*check_dependency_p=*/true,
17789 /* If we didn't find a template-id, look for an ordinary
17791 if (!template_p
&& !cp_parser_parse_definitely (parser
))
17793 /* We can get here when cp_parser_template_id, called by
17794 cp_parser_class_name with tag_type == none_type, succeeds
17795 and caches a BASELINK. Then, when called again here,
17796 instead of failing and returning an error_mark_node
17797 returns it (see template/typename17.C in C++11).
17798 ??? Could we diagnose this earlier? */
17799 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
17801 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
17802 type
= error_mark_node
;
17804 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17805 in effect, then we must assume that, upon instantiation, the
17806 template will correspond to a class. */
17807 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
17808 && tag_type
== typename_type
)
17809 type
= make_typename_type (parser
->scope
, decl
,
17811 /*complain=*/tf_error
);
17812 /* If the `typename' keyword is in effect and DECL is not a type
17813 decl, then type is non existent. */
17814 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
17816 else if (TREE_CODE (decl
) == TYPE_DECL
)
17818 type
= check_elaborated_type_specifier (tag_type
, decl
,
17819 /*allow_template_p=*/true);
17821 /* If the next token is a semicolon, this must be a specialization,
17822 instantiation, or friend declaration. Check the scope while we
17823 still know whether or not we had a nested-name-specifier. */
17824 if (type
!= error_mark_node
17825 && !nested_name_specifier
&& !is_friend
17826 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17827 check_unqualified_spec_or_inst (type
, token
->location
);
17829 else if (decl
== error_mark_node
)
17830 type
= error_mark_node
;
17835 token
= cp_lexer_peek_token (parser
->lexer
);
17836 identifier
= cp_parser_identifier (parser
);
17838 if (identifier
== error_mark_node
)
17840 parser
->scope
= NULL_TREE
;
17841 return error_mark_node
;
17844 /* For a `typename', we needn't call xref_tag. */
17845 if (tag_type
== typename_type
17846 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
17847 return cp_parser_make_typename_type (parser
, identifier
,
17850 /* Template parameter lists apply only if we are not within a
17851 function parameter list. */
17852 bool template_parm_lists_apply
17853 = parser
->num_template_parameter_lists
;
17854 if (template_parm_lists_apply
)
17855 for (cp_binding_level
*s
= current_binding_level
;
17856 s
&& s
->kind
!= sk_template_parms
;
17857 s
= s
->level_chain
)
17858 if (s
->kind
== sk_function_parms
)
17859 template_parm_lists_apply
= false;
17861 /* Look up a qualified name in the usual way. */
17865 tree ambiguous_decls
;
17867 decl
= cp_parser_lookup_name (parser
, identifier
,
17869 /*is_template=*/false,
17870 /*is_namespace=*/false,
17871 /*check_dependency=*/true,
17875 /* If the lookup was ambiguous, an error will already have been
17877 if (ambiguous_decls
)
17878 return error_mark_node
;
17880 /* If we are parsing friend declaration, DECL may be a
17881 TEMPLATE_DECL tree node here. However, we need to check
17882 whether this TEMPLATE_DECL results in valid code. Consider
17883 the following example:
17886 template <class T> class C {};
17889 template <class T> friend class N::C; // #1, valid code
17891 template <class T> class Y {
17892 friend class N::C; // #2, invalid code
17895 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17896 name lookup of `N::C'. We see that friend declaration must
17897 be template for the code to be valid. Note that
17898 processing_template_decl does not work here since it is
17899 always 1 for the above two cases. */
17901 decl
= (cp_parser_maybe_treat_template_as_class
17902 (decl
, /*tag_name_p=*/is_friend
17903 && template_parm_lists_apply
));
17905 if (TREE_CODE (decl
) != TYPE_DECL
)
17907 cp_parser_diagnose_invalid_type_name (parser
,
17910 return error_mark_node
;
17913 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
17915 bool allow_template
= (template_parm_lists_apply
17916 || DECL_SELF_REFERENCE_P (decl
));
17917 type
= check_elaborated_type_specifier (tag_type
, decl
,
17920 if (type
== error_mark_node
)
17921 return error_mark_node
;
17924 /* Forward declarations of nested types, such as
17929 are invalid unless all components preceding the final '::'
17930 are complete. If all enclosing types are complete, these
17931 declarations become merely pointless.
17933 Invalid forward declarations of nested types are errors
17934 caught elsewhere in parsing. Those that are pointless arrive
17937 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
17938 && !is_friend
&& !processing_explicit_instantiation
)
17939 warning (0, "declaration %qD does not declare anything", decl
);
17941 type
= TREE_TYPE (decl
);
17945 /* An elaborated-type-specifier sometimes introduces a new type and
17946 sometimes names an existing type. Normally, the rule is that it
17947 introduces a new type only if there is not an existing type of
17948 the same name already in scope. For example, given:
17951 void f() { struct S s; }
17953 the `struct S' in the body of `f' is the same `struct S' as in
17954 the global scope; the existing definition is used. However, if
17955 there were no global declaration, this would introduce a new
17956 local class named `S'.
17958 An exception to this rule applies to the following code:
17960 namespace N { struct S; }
17962 Here, the elaborated-type-specifier names a new type
17963 unconditionally; even if there is already an `S' in the
17964 containing scope this declaration names a new type.
17965 This exception only applies if the elaborated-type-specifier
17966 forms the complete declaration:
17970 A declaration consisting solely of `class-key identifier ;' is
17971 either a redeclaration of the name in the current scope or a
17972 forward declaration of the identifier as a class name. It
17973 introduces the name into the current scope.
17975 We are in this situation precisely when the next token is a `;'.
17977 An exception to the exception is that a `friend' declaration does
17978 *not* name a new type; i.e., given:
17980 struct S { friend struct T; };
17982 `T' is not a new type in the scope of `S'.
17984 Also, `new struct S' or `sizeof (struct S)' never results in the
17985 definition of a new type; a new type can only be declared in a
17986 declaration context. */
17992 /* Friends have special name lookup rules. */
17993 ts
= ts_within_enclosing_non_class
;
17994 else if (is_declaration
17995 && cp_lexer_next_token_is (parser
->lexer
,
17997 /* This is a `class-key identifier ;' */
18003 (template_parm_lists_apply
18004 && (cp_parser_next_token_starts_class_definition_p (parser
)
18005 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18006 /* An unqualified name was used to reference this type, so
18007 there were no qualifying templates. */
18008 if (template_parm_lists_apply
18009 && !cp_parser_check_template_parameters (parser
,
18010 /*num_templates=*/0,
18011 /*template_id*/false,
18013 /*declarator=*/NULL
))
18014 return error_mark_node
;
18015 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18019 if (type
== error_mark_node
)
18020 return error_mark_node
;
18022 /* Allow attributes on forward declarations of classes. */
18025 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18026 warning (OPT_Wattributes
,
18027 "attributes ignored on uninstantiated type");
18028 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18029 && ! processing_explicit_instantiation
)
18030 warning (OPT_Wattributes
,
18031 "attributes ignored on template instantiation");
18032 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18033 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18035 warning (OPT_Wattributes
,
18036 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18039 if (tag_type
!= enum_type
)
18041 /* Indicate whether this class was declared as a `class' or as a
18043 if (CLASS_TYPE_P (type
))
18044 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18045 cp_parser_check_class_key (tag_type
, type
);
18048 /* A "<" cannot follow an elaborated type specifier. If that
18049 happens, the user was probably trying to form a template-id. */
18050 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18056 /* Parse an enum-specifier.
18059 enum-head { enumerator-list [opt] }
18060 enum-head { enumerator-list , } [C++0x]
18063 enum-key identifier [opt] enum-base [opt]
18064 enum-key nested-name-specifier identifier enum-base [opt]
18069 enum struct [C++0x]
18072 : type-specifier-seq
18074 opaque-enum-specifier:
18075 enum-key identifier enum-base [opt] ;
18078 enum-key attributes[opt] identifier [opt] enum-base [opt]
18079 { enumerator-list [opt] }attributes[opt]
18080 enum-key attributes[opt] identifier [opt] enum-base [opt]
18081 { enumerator-list, }attributes[opt] [C++0x]
18083 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18084 if the token stream isn't an enum-specifier after all. */
18087 cp_parser_enum_specifier (cp_parser
* parser
)
18090 tree type
= NULL_TREE
;
18092 tree nested_name_specifier
= NULL_TREE
;
18094 bool scoped_enum_p
= false;
18095 bool has_underlying_type
= false;
18096 bool nested_being_defined
= false;
18097 bool new_value_list
= false;
18098 bool is_new_type
= false;
18099 bool is_unnamed
= false;
18100 tree underlying_type
= NULL_TREE
;
18101 cp_token
*type_start_token
= NULL
;
18102 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18104 parser
->colon_corrects_to_scope_p
= false;
18106 /* Parse tentatively so that we can back up if we don't find a
18108 cp_parser_parse_tentatively (parser
);
18110 /* Caller guarantees that the current token is 'enum', an identifier
18111 possibly follows, and the token after that is an opening brace.
18112 If we don't have an identifier, fabricate an anonymous name for
18113 the enumeration being defined. */
18114 cp_lexer_consume_token (parser
->lexer
);
18116 /* Parse the "class" or "struct", which indicates a scoped
18117 enumeration type in C++0x. */
18118 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18119 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18121 if (cxx_dialect
< cxx11
)
18122 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18124 /* Consume the `struct' or `class' token. */
18125 cp_lexer_consume_token (parser
->lexer
);
18127 scoped_enum_p
= true;
18130 attributes
= cp_parser_attributes_opt (parser
);
18132 /* Clear the qualification. */
18133 parser
->scope
= NULL_TREE
;
18134 parser
->qualifying_scope
= NULL_TREE
;
18135 parser
->object_scope
= NULL_TREE
;
18137 /* Figure out in what scope the declaration is being placed. */
18138 prev_scope
= current_scope ();
18140 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18142 push_deferring_access_checks (dk_no_check
);
18143 nested_name_specifier
18144 = cp_parser_nested_name_specifier_opt (parser
,
18145 /*typename_keyword_p=*/true,
18146 /*check_dependency_p=*/false,
18148 /*is_declaration=*/false);
18150 if (nested_name_specifier
)
18154 identifier
= cp_parser_identifier (parser
);
18155 name
= cp_parser_lookup_name (parser
, identifier
,
18157 /*is_template=*/false,
18158 /*is_namespace=*/false,
18159 /*check_dependency=*/true,
18160 /*ambiguous_decls=*/NULL
,
18162 if (name
&& name
!= error_mark_node
)
18164 type
= TREE_TYPE (name
);
18165 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18167 /* Are template enums allowed in ISO? */
18168 if (template_parm_scope_p ())
18169 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18170 "%qD is an enumeration template", name
);
18171 /* ignore a typename reference, for it will be solved by name
18176 else if (nested_name_specifier
== error_mark_node
)
18177 /* We already issued an error. */;
18180 error_at (type_start_token
->location
,
18181 "%qD does not name an enumeration in %qT",
18182 identifier
, nested_name_specifier
);
18183 nested_name_specifier
= error_mark_node
;
18188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18189 identifier
= cp_parser_identifier (parser
);
18192 identifier
= make_anon_name ();
18195 error_at (type_start_token
->location
,
18196 "unnamed scoped enum is not allowed");
18199 pop_deferring_access_checks ();
18201 /* Check for the `:' that denotes a specified underlying type in C++0x.
18202 Note that a ':' could also indicate a bitfield width, however. */
18203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18205 cp_decl_specifier_seq type_specifiers
;
18207 /* Consume the `:'. */
18208 cp_lexer_consume_token (parser
->lexer
);
18210 /* Parse the type-specifier-seq. */
18211 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18212 /*is_trailing_return=*/false,
18215 /* At this point this is surely not elaborated type specifier. */
18216 if (!cp_parser_parse_definitely (parser
))
18219 if (cxx_dialect
< cxx11
)
18220 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18222 has_underlying_type
= true;
18224 /* If that didn't work, stop. */
18225 if (type_specifiers
.type
!= error_mark_node
)
18227 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
18228 /*initialized=*/0, NULL
);
18229 if (underlying_type
== error_mark_node
18230 || check_for_bare_parameter_packs (underlying_type
))
18231 underlying_type
= NULL_TREE
;
18235 /* Look for the `{' but don't consume it yet. */
18236 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18238 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
18240 cp_parser_error (parser
, "expected %<{%>");
18241 if (has_underlying_type
)
18247 /* An opaque-enum-specifier must have a ';' here. */
18248 if ((scoped_enum_p
|| underlying_type
)
18249 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18251 cp_parser_error (parser
, "expected %<;%> or %<{%>");
18252 if (has_underlying_type
)
18260 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
18263 if (nested_name_specifier
)
18265 if (CLASS_TYPE_P (nested_name_specifier
))
18267 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
18268 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
18269 push_scope (nested_name_specifier
);
18271 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18273 push_nested_namespace (nested_name_specifier
);
18277 /* Issue an error message if type-definitions are forbidden here. */
18278 if (!cp_parser_check_type_definition (parser
))
18279 type
= error_mark_node
;
18281 /* Create the new type. We do this before consuming the opening
18282 brace so the enum will be recorded as being on the line of its
18283 tag (or the 'enum' keyword, if there is no tag). */
18284 type
= start_enum (identifier
, type
, underlying_type
,
18285 attributes
, scoped_enum_p
, &is_new_type
);
18287 /* If the next token is not '{' it is an opaque-enum-specifier or an
18288 elaborated-type-specifier. */
18289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18291 timevar_push (TV_PARSE_ENUM
);
18292 if (nested_name_specifier
18293 && nested_name_specifier
!= error_mark_node
)
18295 /* The following catches invalid code such as:
18296 enum class S<int>::E { A, B, C }; */
18297 if (!processing_specialization
18298 && CLASS_TYPE_P (nested_name_specifier
)
18299 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
18300 error_at (type_start_token
->location
, "cannot add an enumerator "
18301 "list to a template instantiation");
18303 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
18305 error_at (type_start_token
->location
,
18306 "%<%T::%E%> has not been declared",
18307 TYPE_CONTEXT (nested_name_specifier
),
18308 nested_name_specifier
);
18309 type
= error_mark_node
;
18311 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
18312 && !CLASS_TYPE_P (nested_name_specifier
))
18314 error_at (type_start_token
->location
, "nested name specifier "
18315 "%qT for enum declaration does not name a class "
18316 "or namespace", nested_name_specifier
);
18317 type
= error_mark_node
;
18319 /* If that scope does not contain the scope in which the
18320 class was originally declared, the program is invalid. */
18321 else if (prev_scope
&& !is_ancestor (prev_scope
,
18322 nested_name_specifier
))
18324 if (at_namespace_scope_p ())
18325 error_at (type_start_token
->location
,
18326 "declaration of %qD in namespace %qD which does not "
18328 type
, prev_scope
, nested_name_specifier
);
18330 error_at (type_start_token
->location
,
18331 "declaration of %qD in %qD which does not "
18333 type
, prev_scope
, nested_name_specifier
);
18334 type
= error_mark_node
;
18336 /* If that scope is the scope where the declaration is being placed
18337 the program is invalid. */
18338 else if (CLASS_TYPE_P (nested_name_specifier
)
18339 && CLASS_TYPE_P (prev_scope
)
18340 && same_type_p (nested_name_specifier
, prev_scope
))
18342 permerror (type_start_token
->location
,
18343 "extra qualification not allowed");
18344 nested_name_specifier
= NULL_TREE
;
18349 begin_scope (sk_scoped_enum
, type
);
18351 /* Consume the opening brace. */
18352 matching_braces braces
;
18353 braces
.consume_open (parser
);
18355 if (type
== error_mark_node
)
18356 ; /* Nothing to add */
18357 else if (OPAQUE_ENUM_P (type
)
18358 || (cxx_dialect
> cxx98
&& processing_specialization
))
18360 new_value_list
= true;
18361 SET_OPAQUE_ENUM_P (type
, false);
18362 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
18366 error_at (type_start_token
->location
,
18367 "multiple definition of %q#T", type
);
18368 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
18369 "previous definition here");
18370 type
= error_mark_node
;
18373 if (type
== error_mark_node
)
18374 cp_parser_skip_to_end_of_block_or_statement (parser
);
18375 /* If the next token is not '}', then there are some enumerators. */
18376 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18378 if (is_unnamed
&& !scoped_enum_p
)
18379 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18380 "ISO C++ forbids empty unnamed enum");
18383 cp_parser_enumerator_list (parser
, type
);
18385 /* Consume the final '}'. */
18386 braces
.require_close (parser
);
18390 timevar_pop (TV_PARSE_ENUM
);
18394 /* If a ';' follows, then it is an opaque-enum-specifier
18395 and additional restrictions apply. */
18396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18399 error_at (type_start_token
->location
,
18400 "opaque-enum-specifier without name");
18401 else if (nested_name_specifier
)
18402 error_at (type_start_token
->location
,
18403 "opaque-enum-specifier must use a simple identifier");
18407 /* Look for trailing attributes to apply to this enumeration, and
18408 apply them if appropriate. */
18409 if (cp_parser_allow_gnu_extensions_p (parser
))
18411 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
18412 cplus_decl_attributes (&type
,
18414 (int) ATTR_FLAG_TYPE_IN_PLACE
);
18417 /* Finish up the enumeration. */
18418 if (type
!= error_mark_node
)
18420 if (new_value_list
)
18421 finish_enum_value_list (type
);
18423 finish_enum (type
);
18426 if (nested_name_specifier
)
18428 if (CLASS_TYPE_P (nested_name_specifier
))
18430 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
18431 pop_scope (nested_name_specifier
);
18433 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
18435 pop_nested_namespace (nested_name_specifier
);
18439 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
18443 /* Parse an enumerator-list. The enumerators all have the indicated
18447 enumerator-definition
18448 enumerator-list , enumerator-definition */
18451 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
18455 /* Parse an enumerator-definition. */
18456 cp_parser_enumerator_definition (parser
, type
);
18458 /* If the next token is not a ',', we've reached the end of
18460 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
18462 /* Otherwise, consume the `,' and keep going. */
18463 cp_lexer_consume_token (parser
->lexer
);
18464 /* If the next token is a `}', there is a trailing comma. */
18465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
18467 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
18468 pedwarn (input_location
, OPT_Wpedantic
,
18469 "comma at end of enumerator list");
18475 /* Parse an enumerator-definition. The enumerator has the indicated
18478 enumerator-definition:
18480 enumerator = constant-expression
18487 enumerator-definition:
18488 enumerator attributes [opt]
18489 enumerator attributes [opt] = constant-expression */
18492 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
18498 /* Save the input location because we are interested in the location
18499 of the identifier and not the location of the explicit value. */
18500 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18502 /* Look for the identifier. */
18503 identifier
= cp_parser_identifier (parser
);
18504 if (identifier
== error_mark_node
)
18507 /* Parse any specified attributes. */
18508 tree attrs
= cp_parser_attributes_opt (parser
);
18510 /* If the next token is an '=', then there is an explicit value. */
18511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18513 /* Consume the `=' token. */
18514 cp_lexer_consume_token (parser
->lexer
);
18515 /* Parse the value. */
18516 value
= cp_parser_constant_expression (parser
);
18521 /* If we are processing a template, make sure the initializer of the
18522 enumerator doesn't contain any bare template parameter pack. */
18523 if (check_for_bare_parameter_packs (value
))
18524 value
= error_mark_node
;
18526 /* Create the enumerator. */
18527 build_enumerator (identifier
, value
, type
, attrs
, loc
);
18530 /* Parse a namespace-name.
18533 original-namespace-name
18536 Returns the NAMESPACE_DECL for the namespace. */
18539 cp_parser_namespace_name (cp_parser
* parser
)
18542 tree namespace_decl
;
18544 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18546 /* Get the name of the namespace. */
18547 identifier
= cp_parser_identifier (parser
);
18548 if (identifier
== error_mark_node
)
18549 return error_mark_node
;
18551 /* Look up the identifier in the currently active scope. Look only
18552 for namespaces, due to:
18554 [basic.lookup.udir]
18556 When looking up a namespace-name in a using-directive or alias
18557 definition, only namespace names are considered.
18561 [basic.lookup.qual]
18563 During the lookup of a name preceding the :: scope resolution
18564 operator, object, function, and enumerator names are ignored.
18566 (Note that cp_parser_qualifying_entity only calls this
18567 function if the token after the name is the scope resolution
18569 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
18571 /*is_template=*/false,
18572 /*is_namespace=*/true,
18573 /*check_dependency=*/true,
18574 /*ambiguous_decls=*/NULL
,
18576 /* If it's not a namespace, issue an error. */
18577 if (namespace_decl
== error_mark_node
18578 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
18580 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18582 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
18583 if (namespace_decl
== error_mark_node
18584 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18585 suggest_alternative_in_explicit_scope (token
->location
, identifier
,
18588 cp_parser_error (parser
, "expected namespace-name");
18589 namespace_decl
= error_mark_node
;
18592 return namespace_decl
;
18595 /* Parse a namespace-definition.
18597 namespace-definition:
18598 named-namespace-definition
18599 unnamed-namespace-definition
18601 named-namespace-definition:
18602 original-namespace-definition
18603 extension-namespace-definition
18605 original-namespace-definition:
18606 namespace identifier { namespace-body }
18608 extension-namespace-definition:
18609 namespace original-namespace-name { namespace-body }
18611 unnamed-namespace-definition:
18612 namespace { namespace-body } */
18615 cp_parser_namespace_definition (cp_parser
* parser
)
18618 int nested_definition_count
= 0;
18620 cp_ensure_no_omp_declare_simd (parser
);
18621 cp_ensure_no_oacc_routine (parser
);
18623 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
18627 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
18628 cp_lexer_consume_token (parser
->lexer
);
18631 /* Look for the `namespace' keyword. */
18633 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18635 /* Parse any specified attributes before the identifier. */
18636 tree attribs
= cp_parser_attributes_opt (parser
);
18640 identifier
= NULL_TREE
;
18642 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18644 identifier
= cp_parser_identifier (parser
);
18646 if (cp_next_tokens_can_be_std_attribute_p (parser
))
18647 pedwarn (input_location
, OPT_Wpedantic
,
18648 "standard attributes on namespaces must precede "
18649 "the namespace name");
18651 /* Parse any attributes specified after the identifier. */
18652 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
18655 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
18658 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
18659 pedwarn (input_location
, OPT_Wpedantic
,
18660 "nested namespace definitions only available with "
18661 "-std=c++17 or -std=gnu++17");
18663 /* Nested namespace names can create new namespaces (unlike
18664 other qualified-ids). */
18665 if (int count
= identifier
? push_namespace (identifier
) : 0)
18666 nested_definition_count
+= count
;
18668 cp_parser_error (parser
, "nested namespace name required");
18669 cp_lexer_consume_token (parser
->lexer
);
18672 if (nested_definition_count
&& !identifier
)
18673 cp_parser_error (parser
, "namespace name required");
18675 if (nested_definition_count
&& attribs
)
18676 error_at (token
->location
,
18677 "a nested namespace definition cannot have attributes");
18678 if (nested_definition_count
&& is_inline
)
18679 error_at (token
->location
,
18680 "a nested namespace definition cannot be inline");
18682 /* Start the namespace. */
18683 nested_definition_count
+= push_namespace (identifier
, is_inline
);
18685 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
18687 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
18689 /* Look for the `{' to validate starting the namespace. */
18690 matching_braces braces
;
18691 if (braces
.require_open (parser
))
18693 /* Parse the body of the namespace. */
18694 cp_parser_namespace_body (parser
);
18696 /* Look for the final `}'. */
18697 braces
.require_close (parser
);
18700 if (has_visibility
)
18701 pop_visibility (1);
18703 /* Pop the nested namespace definitions. */
18704 while (nested_definition_count
--)
18708 /* Parse a namespace-body.
18711 declaration-seq [opt] */
18714 cp_parser_namespace_body (cp_parser
* parser
)
18716 cp_parser_declaration_seq_opt (parser
);
18719 /* Parse a namespace-alias-definition.
18721 namespace-alias-definition:
18722 namespace identifier = qualified-namespace-specifier ; */
18725 cp_parser_namespace_alias_definition (cp_parser
* parser
)
18728 tree namespace_specifier
;
18730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18732 /* Look for the `namespace' keyword. */
18733 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18734 /* Look for the identifier. */
18735 identifier
= cp_parser_identifier (parser
);
18736 if (identifier
== error_mark_node
)
18738 /* Look for the `=' token. */
18739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
18740 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18742 error_at (token
->location
, "%<namespace%> definition is not allowed here");
18743 /* Skip the definition. */
18744 cp_lexer_consume_token (parser
->lexer
);
18745 if (cp_parser_skip_to_closing_brace (parser
))
18746 cp_lexer_consume_token (parser
->lexer
);
18749 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
18750 /* Look for the qualified-namespace-specifier. */
18751 namespace_specifier
18752 = cp_parser_qualified_namespace_specifier (parser
);
18753 /* Look for the `;' token. */
18754 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18756 /* Register the alias in the symbol table. */
18757 do_namespace_alias (identifier
, namespace_specifier
);
18760 /* Parse a qualified-namespace-specifier.
18762 qualified-namespace-specifier:
18763 :: [opt] nested-name-specifier [opt] namespace-name
18765 Returns a NAMESPACE_DECL corresponding to the specified
18769 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
18771 /* Look for the optional `::'. */
18772 cp_parser_global_scope_opt (parser
,
18773 /*current_scope_valid_p=*/false);
18775 /* Look for the optional nested-name-specifier. */
18776 cp_parser_nested_name_specifier_opt (parser
,
18777 /*typename_keyword_p=*/false,
18778 /*check_dependency_p=*/true,
18780 /*is_declaration=*/true);
18782 return cp_parser_namespace_name (parser
);
18785 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18786 access declaration.
18789 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18790 using :: unqualified-id ;
18792 access-declaration:
18798 cp_parser_using_declaration (cp_parser
* parser
,
18799 bool access_declaration_p
)
18802 bool typename_p
= false;
18803 bool global_scope_p
;
18807 int oldcount
= errorcount
;
18808 cp_token
*diag_token
= NULL
;
18810 if (access_declaration_p
)
18812 diag_token
= cp_lexer_peek_token (parser
->lexer
);
18813 cp_parser_parse_tentatively (parser
);
18817 /* Look for the `using' keyword. */
18818 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18821 /* Peek at the next token. */
18822 token
= cp_lexer_peek_token (parser
->lexer
);
18823 /* See if it's `typename'. */
18824 if (token
->keyword
== RID_TYPENAME
)
18826 /* Remember that we've seen it. */
18828 /* Consume the `typename' token. */
18829 cp_lexer_consume_token (parser
->lexer
);
18833 /* Look for the optional global scope qualification. */
18835 = (cp_parser_global_scope_opt (parser
,
18836 /*current_scope_valid_p=*/false)
18839 /* If we saw `typename', or didn't see `::', then there must be a
18840 nested-name-specifier present. */
18841 if (typename_p
|| !global_scope_p
)
18843 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
18844 /*check_dependency_p=*/true,
18846 /*is_declaration=*/true);
18847 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
18849 cp_parser_skip_to_end_of_block_or_statement (parser
);
18853 /* Otherwise, we could be in either of the two productions. In that
18854 case, treat the nested-name-specifier as optional. */
18856 qscope
= cp_parser_nested_name_specifier_opt (parser
,
18857 /*typename_keyword_p=*/false,
18858 /*check_dependency_p=*/true,
18860 /*is_declaration=*/true);
18862 qscope
= global_namespace
;
18863 else if (UNSCOPED_ENUM_P (qscope
))
18864 qscope
= CP_TYPE_CONTEXT (qscope
);
18866 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
18867 /* Something has already gone wrong; there's no need to parse
18868 further. Since an error has occurred, the return value of
18869 cp_parser_parse_definitely will be false, as required. */
18870 return cp_parser_parse_definitely (parser
);
18872 token
= cp_lexer_peek_token (parser
->lexer
);
18873 /* Parse the unqualified-id. */
18874 identifier
= cp_parser_unqualified_id (parser
,
18875 /*template_keyword_p=*/false,
18876 /*check_dependency_p=*/true,
18877 /*declarator_p=*/true,
18878 /*optional_p=*/false);
18880 if (access_declaration_p
)
18882 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
18883 cp_parser_simulate_error (parser
);
18884 if (!cp_parser_parse_definitely (parser
))
18887 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18889 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
18890 if (cxx_dialect
< cxx17
18891 && !in_system_header_at (ell
->location
))
18892 pedwarn (ell
->location
, 0,
18893 "pack expansion in using-declaration only available "
18894 "with -std=c++17 or -std=gnu++17");
18895 qscope
= make_pack_expansion (qscope
);
18898 /* The function we call to handle a using-declaration is different
18899 depending on what scope we are in. */
18900 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
18902 else if (!identifier_p (identifier
)
18903 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
18904 /* [namespace.udecl]
18906 A using declaration shall not name a template-id. */
18907 error_at (token
->location
,
18908 "a template-id may not appear in a using-declaration");
18911 if (at_class_scope_p ())
18913 /* Create the USING_DECL. */
18914 decl
= do_class_using_decl (qscope
, identifier
);
18916 if (decl
&& typename_p
)
18917 USING_DECL_TYPENAME_P (decl
) = 1;
18919 if (check_for_bare_parameter_packs (decl
))
18921 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18925 /* Add it to the list of members in this class. */
18926 finish_member_declaration (decl
);
18930 decl
= cp_parser_lookup_name_simple (parser
,
18933 if (decl
== error_mark_node
)
18934 cp_parser_name_lookup_error (parser
, identifier
,
18937 else if (check_for_bare_parameter_packs (decl
))
18939 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18942 else if (!at_namespace_scope_p ())
18943 finish_local_using_decl (decl
, qscope
, identifier
);
18945 finish_namespace_using_decl (decl
, qscope
, identifier
);
18949 if (!access_declaration_p
18950 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18952 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
18953 if (cxx_dialect
< cxx17
)
18954 pedwarn (comma
->location
, 0,
18955 "comma-separated list in using-declaration only available "
18956 "with -std=c++17 or -std=gnu++17");
18960 /* Look for the final `;'. */
18961 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18963 if (access_declaration_p
&& errorcount
== oldcount
)
18964 warning_at (diag_token
->location
, OPT_Wdeprecated
,
18965 "access declarations are deprecated "
18966 "in favour of using-declarations; "
18967 "suggestion: add the %<using%> keyword");
18972 /* Parse an alias-declaration.
18975 using identifier attribute-specifier-seq [opt] = type-id */
18978 cp_parser_alias_declaration (cp_parser
* parser
)
18980 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
18981 location_t id_location
;
18982 cp_declarator
*declarator
;
18983 cp_decl_specifier_seq decl_specs
;
18985 const char *saved_message
= NULL
;
18987 /* Look for the `using' keyword. */
18988 cp_token
*using_token
18989 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18990 if (using_token
== NULL
)
18991 return error_mark_node
;
18993 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
18994 id
= cp_parser_identifier (parser
);
18995 if (id
== error_mark_node
)
18996 return error_mark_node
;
18998 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
18999 attributes
= cp_parser_attributes_opt (parser
);
19000 if (attributes
== error_mark_node
)
19001 return error_mark_node
;
19003 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19005 if (cp_parser_error_occurred (parser
))
19006 return error_mark_node
;
19008 cp_parser_commit_to_tentative_parse (parser
);
19010 /* Now we are going to parse the type-id of the declaration. */
19015 "A type-specifier-seq shall not define a class or enumeration
19016 unless it appears in the type-id of an alias-declaration (7.1.3) that
19017 is not the declaration of a template-declaration."
19019 In other words, if we currently are in an alias template, the
19020 type-id should not define a type.
19022 So let's set parser->type_definition_forbidden_message in that
19023 case; cp_parser_check_type_definition (called by
19024 cp_parser_class_specifier) will then emit an error if a type is
19025 defined in the type-id. */
19026 if (parser
->num_template_parameter_lists
)
19028 saved_message
= parser
->type_definition_forbidden_message
;
19029 parser
->type_definition_forbidden_message
=
19030 G_("types may not be defined in alias template declarations");
19033 type
= cp_parser_type_id (parser
);
19035 /* Restore the error message if need be. */
19036 if (parser
->num_template_parameter_lists
)
19037 parser
->type_definition_forbidden_message
= saved_message
;
19039 if (type
== error_mark_node
19040 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19042 cp_parser_skip_to_end_of_block_or_statement (parser
);
19043 return error_mark_node
;
19046 /* A typedef-name can also be introduced by an alias-declaration. The
19047 identifier following the using keyword becomes a typedef-name. It has
19048 the same semantics as if it were introduced by the typedef
19049 specifier. In particular, it does not define a new type and it shall
19050 not appear in the type-id. */
19052 clear_decl_specs (&decl_specs
);
19053 decl_specs
.type
= type
;
19054 if (attributes
!= NULL_TREE
)
19056 decl_specs
.attributes
= attributes
;
19057 set_and_check_decl_spec_loc (&decl_specs
,
19061 set_and_check_decl_spec_loc (&decl_specs
,
19064 set_and_check_decl_spec_loc (&decl_specs
,
19068 if (parser
->num_template_parameter_lists
19069 && !cp_parser_check_template_parameters (parser
,
19070 /*num_templates=*/0,
19071 /*template_id*/false,
19073 /*declarator=*/NULL
))
19074 return error_mark_node
;
19076 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
19077 declarator
->id_loc
= id_location
;
19079 member_p
= at_class_scope_p ();
19081 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19082 NULL_TREE
, attributes
);
19084 decl
= start_decl (declarator
, &decl_specs
, 0,
19085 attributes
, NULL_TREE
, &pushed_scope
);
19086 if (decl
== error_mark_node
)
19089 // Attach constraints to the alias declaration.
19090 if (flag_concepts
&& current_template_parms
)
19092 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
19093 tree constr
= build_constraints (reqs
, NULL_TREE
);
19094 set_constraints (decl
, constr
);
19097 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19100 pop_scope (pushed_scope
);
19102 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19103 added into the symbol table; otherwise, return the TYPE_DECL. */
19104 if (DECL_LANG_SPECIFIC (decl
)
19105 && DECL_TEMPLATE_INFO (decl
)
19106 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19108 decl
= DECL_TI_TEMPLATE (decl
);
19110 check_member_template (decl
);
19116 /* Parse a using-directive.
19119 using namespace :: [opt] nested-name-specifier [opt]
19120 namespace-name ; */
19123 cp_parser_using_directive (cp_parser
* parser
)
19125 tree namespace_decl
;
19128 /* Look for the `using' keyword. */
19129 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19130 /* And the `namespace' keyword. */
19131 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19132 /* Look for the optional `::' operator. */
19133 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19134 /* And the optional nested-name-specifier. */
19135 cp_parser_nested_name_specifier_opt (parser
,
19136 /*typename_keyword_p=*/false,
19137 /*check_dependency_p=*/true,
19139 /*is_declaration=*/true);
19140 /* Get the namespace being used. */
19141 namespace_decl
= cp_parser_namespace_name (parser
);
19142 /* And any specified attributes. */
19143 attribs
= cp_parser_attributes_opt (parser
);
19145 /* Update the symbol table. */
19146 if (namespace_bindings_p ())
19147 finish_namespace_using_directive (namespace_decl
, attribs
);
19149 finish_local_using_directive (namespace_decl
, attribs
);
19151 /* Look for the final `;'. */
19152 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19155 /* Parse an asm-definition.
19158 asm ( string-literal ) ;
19163 asm volatile [opt] ( string-literal ) ;
19164 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19165 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19166 : asm-operand-list [opt] ) ;
19167 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19168 : asm-operand-list [opt]
19169 : asm-clobber-list [opt] ) ;
19170 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19171 : asm-clobber-list [opt]
19172 : asm-goto-list ) ; */
19175 cp_parser_asm_definition (cp_parser
* parser
)
19178 tree outputs
= NULL_TREE
;
19179 tree inputs
= NULL_TREE
;
19180 tree clobbers
= NULL_TREE
;
19181 tree labels
= NULL_TREE
;
19183 bool volatile_p
= false;
19184 bool extended_p
= false;
19185 bool invalid_inputs_p
= false;
19186 bool invalid_outputs_p
= false;
19187 bool goto_p
= false;
19188 required_token missing
= RT_NONE
;
19190 /* Look for the `asm' keyword. */
19191 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
19193 if (parser
->in_function_body
19194 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
19196 error ("%<asm%> in %<constexpr%> function");
19197 cp_function_chain
->invalid_constexpr
= true;
19200 /* See if the next token is `volatile'. */
19201 if (cp_parser_allow_gnu_extensions_p (parser
)
19202 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
19204 /* Remember that we saw the `volatile' keyword. */
19206 /* Consume the token. */
19207 cp_lexer_consume_token (parser
->lexer
);
19209 if (cp_parser_allow_gnu_extensions_p (parser
)
19210 && parser
->in_function_body
19211 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
19213 /* Remember that we saw the `goto' keyword. */
19215 /* Consume the token. */
19216 cp_lexer_consume_token (parser
->lexer
);
19218 /* Look for the opening `('. */
19219 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
19221 /* Look for the string. */
19222 string
= cp_parser_string_literal (parser
, false, false);
19223 if (string
== error_mark_node
)
19225 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19226 /*consume_paren=*/true);
19230 /* If we're allowing GNU extensions, check for the extended assembly
19231 syntax. Unfortunately, the `:' tokens need not be separated by
19232 a space in C, and so, for compatibility, we tolerate that here
19233 too. Doing that means that we have to treat the `::' operator as
19235 if (cp_parser_allow_gnu_extensions_p (parser
)
19236 && parser
->in_function_body
19237 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
19238 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
19240 bool inputs_p
= false;
19241 bool clobbers_p
= false;
19242 bool labels_p
= false;
19244 /* The extended syntax was used. */
19247 /* Look for outputs. */
19248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19250 /* Consume the `:'. */
19251 cp_lexer_consume_token (parser
->lexer
);
19252 /* Parse the output-operands. */
19253 if (cp_lexer_next_token_is_not (parser
->lexer
,
19255 && cp_lexer_next_token_is_not (parser
->lexer
,
19257 && cp_lexer_next_token_is_not (parser
->lexer
,
19261 outputs
= cp_parser_asm_operand_list (parser
);
19262 if (outputs
== error_mark_node
)
19263 invalid_outputs_p
= true;
19266 /* If the next token is `::', there are no outputs, and the
19267 next token is the beginning of the inputs. */
19268 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19269 /* The inputs are coming next. */
19272 /* Look for inputs. */
19274 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19276 /* Consume the `:' or `::'. */
19277 cp_lexer_consume_token (parser
->lexer
);
19278 /* Parse the output-operands. */
19279 if (cp_lexer_next_token_is_not (parser
->lexer
,
19281 && cp_lexer_next_token_is_not (parser
->lexer
,
19283 && cp_lexer_next_token_is_not (parser
->lexer
,
19286 inputs
= cp_parser_asm_operand_list (parser
);
19287 if (inputs
== error_mark_node
)
19288 invalid_inputs_p
= true;
19291 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19292 /* The clobbers are coming next. */
19295 /* Look for clobbers. */
19297 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19300 /* Consume the `:' or `::'. */
19301 cp_lexer_consume_token (parser
->lexer
);
19302 /* Parse the clobbers. */
19303 if (cp_lexer_next_token_is_not (parser
->lexer
,
19305 && cp_lexer_next_token_is_not (parser
->lexer
,
19307 clobbers
= cp_parser_asm_clobber_list (parser
);
19310 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19311 /* The labels are coming next. */
19314 /* Look for labels. */
19316 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
19319 /* Consume the `:' or `::'. */
19320 cp_lexer_consume_token (parser
->lexer
);
19321 /* Parse the labels. */
19322 labels
= cp_parser_asm_label_list (parser
);
19325 if (goto_p
&& !labels_p
)
19326 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
19329 missing
= RT_COLON_SCOPE
;
19331 /* Look for the closing `)'. */
19332 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
19333 missing
? missing
: RT_CLOSE_PAREN
))
19334 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
19335 /*consume_paren=*/true);
19336 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19338 if (!invalid_inputs_p
&& !invalid_outputs_p
)
19340 /* Create the ASM_EXPR. */
19341 if (parser
->in_function_body
)
19343 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
19344 inputs
, clobbers
, labels
);
19345 /* If the extended syntax was not used, mark the ASM_EXPR. */
19348 tree temp
= asm_stmt
;
19349 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
19350 temp
= TREE_OPERAND (temp
, 0);
19352 ASM_INPUT_P (temp
) = 1;
19356 symtab
->finalize_toplevel_asm (string
);
19360 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19361 type that comes from the decl-specifier-seq. */
19364 strip_declarator_types (tree type
, cp_declarator
*declarator
)
19366 for (cp_declarator
*d
= declarator
; d
;)
19376 if (TYPE_PTRMEMFUNC_P (type
))
19377 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
19378 type
= TREE_TYPE (type
);
19386 /* Declarators [gram.dcl.decl] */
19388 /* Parse an init-declarator.
19391 declarator initializer [opt]
19396 declarator asm-specification [opt] attributes [opt] initializer [opt]
19398 function-definition:
19399 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19401 decl-specifier-seq [opt] declarator function-try-block
19405 function-definition:
19406 __extension__ function-definition
19410 function-definition:
19411 decl-specifier-seq [opt] declarator function-transaction-block
19413 The DECL_SPECIFIERS apply to this declarator. Returns a
19414 representation of the entity declared. If MEMBER_P is TRUE, then
19415 this declarator appears in a class scope. The new DECL created by
19416 this declarator is returned.
19418 The CHECKS are access checks that should be performed once we know
19419 what entity is being declared (and, therefore, what classes have
19422 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19423 for a function-definition here as well. If the declarator is a
19424 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19425 be TRUE upon return. By that point, the function-definition will
19426 have been completely parsed.
19428 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19431 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19432 parsed declaration if it is an uninitialized single declarator not followed
19433 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19434 if present, will not be consumed. If returned, this declarator will be
19435 created with SD_INITIALIZED but will not call cp_finish_decl.
19437 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19438 and there is an initializer, the pointed location_t is set to the
19439 location of the '=' or `(', or '{' in C++11 token introducing the
19443 cp_parser_init_declarator (cp_parser
* parser
,
19444 cp_decl_specifier_seq
*decl_specifiers
,
19445 vec
<deferred_access_check
, va_gc
> *checks
,
19446 bool function_definition_allowed_p
,
19448 int declares_class_or_enum
,
19449 bool* function_definition_p
,
19450 tree
* maybe_range_for_decl
,
19451 location_t
* init_loc
,
19454 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
19455 *attributes_start_token
= NULL
;
19456 cp_declarator
*declarator
;
19457 tree prefix_attributes
;
19458 tree attributes
= NULL
;
19459 tree asm_specification
;
19461 tree decl
= NULL_TREE
;
19463 int is_initialized
;
19464 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19465 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19467 enum cpp_ttype initialization_kind
;
19468 bool is_direct_init
= false;
19469 bool is_non_constant_init
;
19470 int ctor_dtor_or_conv_p
;
19471 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
19472 tree pushed_scope
= NULL_TREE
;
19473 bool range_for_decl_p
= false;
19474 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19475 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
19477 /* Gather the attributes that were provided with the
19478 decl-specifiers. */
19479 prefix_attributes
= decl_specifiers
->attributes
;
19481 /* Assume that this is not the declarator for a function
19483 if (function_definition_p
)
19484 *function_definition_p
= false;
19486 /* Default arguments are only permitted for function parameters. */
19487 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
19488 parser
->default_arg_ok_p
= false;
19490 /* Defer access checks while parsing the declarator; we cannot know
19491 what names are accessible until we know what is being
19493 resume_deferring_access_checks ();
19495 token
= cp_lexer_peek_token (parser
->lexer
);
19497 /* Parse the declarator. */
19499 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
19500 &ctor_dtor_or_conv_p
,
19501 /*parenthesized_p=*/NULL
,
19502 member_p
, friend_p
);
19503 /* Gather up the deferred checks. */
19504 stop_deferring_access_checks ();
19506 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19508 /* If the DECLARATOR was erroneous, there's no need to go
19510 if (declarator
== cp_error_declarator
)
19511 return error_mark_node
;
19513 /* Check that the number of template-parameter-lists is OK. */
19514 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
19516 return error_mark_node
;
19518 if (declares_class_or_enum
& 2)
19519 cp_parser_check_for_definition_in_return_type (declarator
,
19520 decl_specifiers
->type
,
19521 decl_specifiers
->locations
[ds_type_spec
]);
19523 /* Figure out what scope the entity declared by the DECLARATOR is
19524 located in. `grokdeclarator' sometimes changes the scope, so
19525 we compute it now. */
19526 scope
= get_scope_of_declarator (declarator
);
19528 /* Perform any lookups in the declared type which were thought to be
19529 dependent, but are not in the scope of the declarator. */
19530 decl_specifiers
->type
19531 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
19533 /* If we're allowing GNU extensions, look for an
19534 asm-specification. */
19535 if (cp_parser_allow_gnu_extensions_p (parser
))
19537 /* Look for an asm-specification. */
19538 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
19539 asm_specification
= cp_parser_asm_specification_opt (parser
);
19542 asm_specification
= NULL_TREE
;
19544 /* Look for attributes. */
19545 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
19546 attributes
= cp_parser_attributes_opt (parser
);
19548 /* Peek at the next token. */
19549 token
= cp_lexer_peek_token (parser
->lexer
);
19551 bool bogus_implicit_tmpl
= false;
19553 if (function_declarator_p (declarator
))
19555 /* Handle C++17 deduction guides. */
19556 if (!decl_specifiers
->type
19557 && ctor_dtor_or_conv_p
<= 0
19558 && cxx_dialect
>= cxx17
)
19560 cp_declarator
*id
= get_id_declarator (declarator
);
19561 tree name
= id
->u
.id
.unqualified_name
;
19562 parser
->scope
= id
->u
.id
.qualifying_scope
;
19563 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
19565 && (DECL_CLASS_TEMPLATE_P (tmpl
)
19566 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
19568 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
19569 id
->u
.id
.sfk
= sfk_deduction_guide
;
19570 ctor_dtor_or_conv_p
= 1;
19574 /* Check to see if the token indicates the start of a
19575 function-definition. */
19576 if (cp_parser_token_starts_function_definition_p (token
))
19578 if (!function_definition_allowed_p
)
19580 /* If a function-definition should not appear here, issue an
19582 cp_parser_error (parser
,
19583 "a function-definition is not allowed here");
19584 return error_mark_node
;
19587 location_t func_brace_location
19588 = cp_lexer_peek_token (parser
->lexer
)->location
;
19590 /* Neither attributes nor an asm-specification are allowed
19591 on a function-definition. */
19592 if (asm_specification
)
19593 error_at (asm_spec_start_token
->location
,
19594 "an asm-specification is not allowed "
19595 "on a function-definition");
19597 error_at (attributes_start_token
->location
,
19598 "attributes are not allowed "
19599 "on a function-definition");
19600 /* This is a function-definition. */
19601 *function_definition_p
= true;
19603 /* Parse the function definition. */
19605 decl
= cp_parser_save_member_function_body (parser
,
19608 prefix_attributes
);
19611 (cp_parser_function_definition_from_specifiers_and_declarator
19612 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
19614 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
19616 /* This is where the prologue starts... */
19617 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
19618 = func_brace_location
;
19624 else if (parser
->fully_implicit_function_template_p
)
19626 /* A non-template declaration involving a function parameter list
19627 containing an implicit template parameter will be made into a
19628 template. If the resulting declaration is not going to be an
19629 actual function then finish the template scope here to prevent it.
19630 An error message will be issued once we have a decl to talk about.
19632 FIXME probably we should do type deduction rather than create an
19633 implicit template, but the standard currently doesn't allow it. */
19634 bogus_implicit_tmpl
= true;
19635 finish_fully_implicit_template (parser
, NULL_TREE
);
19640 Only in function declarations for constructors, destructors, type
19641 conversions, and deduction guides can the decl-specifier-seq be omitted.
19643 We explicitly postpone this check past the point where we handle
19644 function-definitions because we tolerate function-definitions
19645 that are missing their return types in some modes. */
19646 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
19648 cp_parser_error (parser
,
19649 "expected constructor, destructor, or type conversion");
19650 return error_mark_node
;
19653 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19654 if (token
->type
== CPP_EQ
19655 || token
->type
== CPP_OPEN_PAREN
19656 || token
->type
== CPP_OPEN_BRACE
)
19658 is_initialized
= SD_INITIALIZED
;
19659 initialization_kind
= token
->type
;
19660 if (maybe_range_for_decl
)
19661 *maybe_range_for_decl
= error_mark_node
;
19662 tmp_init_loc
= token
->location
;
19663 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
19664 *init_loc
= tmp_init_loc
;
19666 if (token
->type
== CPP_EQ
19667 && function_declarator_p (declarator
))
19669 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19670 if (t2
->keyword
== RID_DEFAULT
)
19671 is_initialized
= SD_DEFAULTED
;
19672 else if (t2
->keyword
== RID_DELETE
)
19673 is_initialized
= SD_DELETED
;
19678 /* If the init-declarator isn't initialized and isn't followed by a
19679 `,' or `;', it's not a valid init-declarator. */
19680 if (token
->type
!= CPP_COMMA
19681 && token
->type
!= CPP_SEMICOLON
)
19683 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
19684 range_for_decl_p
= true;
19687 if (!maybe_range_for_decl
)
19688 cp_parser_error (parser
, "expected initializer");
19689 return error_mark_node
;
19692 is_initialized
= SD_UNINITIALIZED
;
19693 initialization_kind
= CPP_EOF
;
19696 /* Because start_decl has side-effects, we should only call it if we
19697 know we're going ahead. By this point, we know that we cannot
19698 possibly be looking at any other construct. */
19699 cp_parser_commit_to_tentative_parse (parser
);
19701 /* Enter the newly declared entry in the symbol table. If we're
19702 processing a declaration in a class-specifier, we wait until
19703 after processing the initializer. */
19706 if (parser
->in_unbraced_linkage_specification_p
)
19707 decl_specifiers
->storage_class
= sc_extern
;
19708 decl
= start_decl (declarator
, decl_specifiers
,
19709 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
19710 attributes
, prefix_attributes
, &pushed_scope
);
19711 cp_finalize_omp_declare_simd (parser
, decl
);
19712 cp_finalize_oacc_routine (parser
, decl
, false);
19713 /* Adjust location of decl if declarator->id_loc is more appropriate:
19714 set, and decl wasn't merged with another decl, in which case its
19715 location would be different from input_location, and more accurate. */
19717 && declarator
->id_loc
!= UNKNOWN_LOCATION
19718 && DECL_SOURCE_LOCATION (decl
) == input_location
)
19719 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
19722 /* Enter the SCOPE. That way unqualified names appearing in the
19723 initializer will be looked up in SCOPE. */
19724 pushed_scope
= push_scope (scope
);
19726 /* Perform deferred access control checks, now that we know in which
19727 SCOPE the declared entity resides. */
19728 if (!member_p
&& decl
)
19730 tree saved_current_function_decl
= NULL_TREE
;
19732 /* If the entity being declared is a function, pretend that we
19733 are in its scope. If it is a `friend', it may have access to
19734 things that would not otherwise be accessible. */
19735 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19737 saved_current_function_decl
= current_function_decl
;
19738 current_function_decl
= decl
;
19741 /* Perform access checks for template parameters. */
19742 cp_parser_perform_template_parameter_access_checks (checks
);
19744 /* Perform the access control checks for the declarator and the
19745 decl-specifiers. */
19746 perform_deferred_access_checks (tf_warning_or_error
);
19748 /* Restore the saved value. */
19749 if (TREE_CODE (decl
) == FUNCTION_DECL
)
19750 current_function_decl
= saved_current_function_decl
;
19753 /* Parse the initializer. */
19754 initializer
= NULL_TREE
;
19755 is_direct_init
= false;
19756 is_non_constant_init
= true;
19757 if (is_initialized
)
19759 if (function_declarator_p (declarator
))
19761 if (initialization_kind
== CPP_EQ
)
19762 initializer
= cp_parser_pure_specifier (parser
);
19765 /* If the declaration was erroneous, we don't really
19766 know what the user intended, so just silently
19767 consume the initializer. */
19768 if (decl
!= error_mark_node
)
19769 error_at (tmp_init_loc
, "initializer provided for function");
19770 cp_parser_skip_to_closing_parenthesis (parser
,
19771 /*recovering=*/true,
19772 /*or_comma=*/false,
19773 /*consume_paren=*/true);
19778 /* We want to record the extra mangling scope for in-class
19779 initializers of class members and initializers of static data
19780 member templates. The former involves deferring
19781 parsing of the initializer until end of class as with default
19782 arguments. So right here we only handle the latter. */
19783 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19784 start_lambda_scope (decl
);
19785 initializer
= cp_parser_initializer (parser
,
19787 &is_non_constant_init
);
19788 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
19789 finish_lambda_scope ();
19790 if (initializer
== error_mark_node
)
19791 cp_parser_skip_to_end_of_statement (parser
);
19795 /* The old parser allows attributes to appear after a parenthesized
19796 initializer. Mark Mitchell proposed removing this functionality
19797 on the GCC mailing lists on 2002-08-13. This parser accepts the
19798 attributes -- but ignores them. Made a permerror in GCC 8. */
19799 if (cp_parser_allow_gnu_extensions_p (parser
)
19800 && initialization_kind
== CPP_OPEN_PAREN
19801 && cp_parser_attributes_opt (parser
)
19802 && permerror (input_location
,
19803 "attributes after parenthesized initializer ignored"))
19806 if (flag_permissive
&& !hint
)
19809 inform (input_location
,
19810 "this flexibility is deprecated and will be removed");
19814 /* And now complain about a non-function implicit template. */
19815 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
19816 error_at (DECL_SOURCE_LOCATION (decl
),
19817 "non-function %qD declared as implicit template", decl
);
19819 /* For an in-class declaration, use `grokfield' to create the
19825 pop_scope (pushed_scope
);
19826 pushed_scope
= NULL_TREE
;
19828 decl
= grokfield (declarator
, decl_specifiers
,
19829 initializer
, !is_non_constant_init
,
19830 /*asmspec=*/NULL_TREE
,
19831 attr_chainon (attributes
, prefix_attributes
));
19832 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19833 cp_parser_save_default_args (parser
, decl
);
19834 cp_finalize_omp_declare_simd (parser
, decl
);
19835 cp_finalize_oacc_routine (parser
, decl
, false);
19838 /* Finish processing the declaration. But, skip member
19840 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
19842 cp_finish_decl (decl
,
19843 initializer
, !is_non_constant_init
,
19845 /* If the initializer is in parentheses, then this is
19846 a direct-initialization, which means that an
19847 `explicit' constructor is OK. Otherwise, an
19848 `explicit' constructor cannot be used. */
19849 ((is_direct_init
|| !is_initialized
)
19850 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
19852 else if ((cxx_dialect
!= cxx98
) && friend_p
19853 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
19854 /* Core issue #226 (C++0x only): A default template-argument
19855 shall not be specified in a friend class template
19857 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
19858 /*is_partial=*/false, /*is_friend_decl=*/1);
19860 if (!friend_p
&& pushed_scope
)
19861 pop_scope (pushed_scope
);
19863 if (function_declarator_p (declarator
)
19864 && parser
->fully_implicit_function_template_p
)
19867 decl
= finish_fully_implicit_template (parser
, decl
);
19869 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
19872 if (auto_result
&& is_initialized
&& decl_specifiers
->type
19873 && type_uses_auto (decl_specifiers
->type
))
19874 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
19879 /* Parse a declarator.
19883 ptr-operator declarator
19885 abstract-declarator:
19886 ptr-operator abstract-declarator [opt]
19887 direct-abstract-declarator
19892 attributes [opt] direct-declarator
19893 attributes [opt] ptr-operator declarator
19895 abstract-declarator:
19896 attributes [opt] ptr-operator abstract-declarator [opt]
19897 attributes [opt] direct-abstract-declarator
19899 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19900 detect constructors, destructors, deduction guides, or conversion operators.
19901 It is set to -1 if the declarator is a name, and +1 if it is a
19902 function. Otherwise it is set to zero. Usually you just want to
19903 test for >0, but internally the negative value is used.
19905 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19906 a decl-specifier-seq unless it declares a constructor, destructor,
19907 or conversion. It might seem that we could check this condition in
19908 semantic analysis, rather than parsing, but that makes it difficult
19909 to handle something like `f()'. We want to notice that there are
19910 no decl-specifiers, and therefore realize that this is an
19911 expression, not a declaration.)
19913 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19914 the declarator is a direct-declarator of the form "(...)".
19916 MEMBER_P is true iff this declarator is a member-declarator.
19918 FRIEND_P is true iff this declarator is a friend. */
19920 static cp_declarator
*
19921 cp_parser_declarator (cp_parser
* parser
,
19922 cp_parser_declarator_kind dcl_kind
,
19923 int* ctor_dtor_or_conv_p
,
19924 bool* parenthesized_p
,
19925 bool member_p
, bool friend_p
)
19927 cp_declarator
*declarator
;
19928 enum tree_code code
;
19929 cp_cv_quals cv_quals
;
19931 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
19933 /* Assume this is not a constructor, destructor, or type-conversion
19935 if (ctor_dtor_or_conv_p
)
19936 *ctor_dtor_or_conv_p
= 0;
19938 if (cp_parser_allow_gnu_extensions_p (parser
))
19939 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
19941 /* Check for the ptr-operator production. */
19942 cp_parser_parse_tentatively (parser
);
19943 /* Parse the ptr-operator. */
19944 code
= cp_parser_ptr_operator (parser
,
19949 /* If that worked, then we have a ptr-operator. */
19950 if (cp_parser_parse_definitely (parser
))
19952 /* If a ptr-operator was found, then this declarator was not
19954 if (parenthesized_p
)
19955 *parenthesized_p
= true;
19956 /* The dependent declarator is optional if we are parsing an
19957 abstract-declarator. */
19958 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19959 cp_parser_parse_tentatively (parser
);
19961 /* Parse the dependent declarator. */
19962 declarator
= cp_parser_declarator (parser
, dcl_kind
,
19963 /*ctor_dtor_or_conv_p=*/NULL
,
19964 /*parenthesized_p=*/NULL
,
19965 /*member_p=*/false,
19968 /* If we are parsing an abstract-declarator, we must handle the
19969 case where the dependent declarator is absent. */
19970 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
19971 && !cp_parser_parse_definitely (parser
))
19974 declarator
= cp_parser_make_indirect_declarator
19975 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
19977 /* Everything else is a direct-declarator. */
19980 if (parenthesized_p
)
19981 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
19983 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
19984 ctor_dtor_or_conv_p
,
19985 member_p
, friend_p
);
19988 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
19989 declarator
->attributes
= gnu_attributes
;
19993 /* Parse a direct-declarator or direct-abstract-declarator.
19997 direct-declarator ( parameter-declaration-clause )
19998 cv-qualifier-seq [opt]
19999 ref-qualifier [opt]
20000 exception-specification [opt]
20001 direct-declarator [ constant-expression [opt] ]
20004 direct-abstract-declarator:
20005 direct-abstract-declarator [opt]
20006 ( parameter-declaration-clause )
20007 cv-qualifier-seq [opt]
20008 ref-qualifier [opt]
20009 exception-specification [opt]
20010 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20011 ( abstract-declarator )
20013 Returns a representation of the declarator. DCL_KIND is
20014 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20015 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20016 we are parsing a direct-declarator. It is
20017 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20018 of ambiguity we prefer an abstract declarator, as per
20019 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20020 as for cp_parser_declarator. */
20022 static cp_declarator
*
20023 cp_parser_direct_declarator (cp_parser
* parser
,
20024 cp_parser_declarator_kind dcl_kind
,
20025 int* ctor_dtor_or_conv_p
,
20026 bool member_p
, bool friend_p
)
20029 cp_declarator
*declarator
= NULL
;
20030 tree scope
= NULL_TREE
;
20031 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20032 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20034 tree pushed_scope
= NULL_TREE
;
20035 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20039 /* Peek at the next token. */
20040 token
= cp_lexer_peek_token (parser
->lexer
);
20041 if (token
->type
== CPP_OPEN_PAREN
)
20043 /* This is either a parameter-declaration-clause, or a
20044 parenthesized declarator. When we know we are parsing a
20045 named declarator, it must be a parenthesized declarator
20046 if FIRST is true. For instance, `(int)' is a
20047 parameter-declaration-clause, with an omitted
20048 direct-abstract-declarator. But `((*))', is a
20049 parenthesized abstract declarator. Finally, when T is a
20050 template parameter `(T)' is a
20051 parameter-declaration-clause, and not a parenthesized
20054 We first try and parse a parameter-declaration-clause,
20055 and then try a nested declarator (if FIRST is true).
20057 It is not an error for it not to be a
20058 parameter-declaration-clause, even when FIRST is
20064 The first is the declaration of a function while the
20065 second is the definition of a variable, including its
20068 Having seen only the parenthesis, we cannot know which of
20069 these two alternatives should be selected. Even more
20070 complex are examples like:
20075 The former is a function-declaration; the latter is a
20076 variable initialization.
20078 Thus again, we try a parameter-declaration-clause, and if
20079 that fails, we back out and return. */
20081 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20084 bool is_declarator
= false;
20088 /* In a member-declarator, the only valid interpretation
20089 of a parenthesis is the start of a
20090 parameter-declaration-clause. (It is invalid to
20091 initialize a static data member with a parenthesized
20092 initializer; only the "=" form of initialization is
20095 cp_parser_parse_tentatively (parser
);
20097 /* Consume the `('. */
20098 matching_parens parens
;
20099 parens
.consume_open (parser
);
20102 /* If this is going to be an abstract declarator, we're
20103 in a declarator and we can't have default args. */
20104 parser
->default_arg_ok_p
= false;
20105 parser
->in_declarator_p
= true;
20108 begin_scope (sk_function_parms
, NULL_TREE
);
20110 /* Parse the parameter-declaration-clause. */
20111 params
= cp_parser_parameter_declaration_clause (parser
);
20113 /* Consume the `)'. */
20114 parens
.require_close (parser
);
20116 /* If all went well, parse the cv-qualifier-seq,
20117 ref-qualifier and the exception-specification. */
20118 if (member_p
|| cp_parser_parse_definitely (parser
))
20120 cp_cv_quals cv_quals
;
20121 cp_virt_specifiers virt_specifiers
;
20122 cp_ref_qualifier ref_qual
;
20123 tree exception_specification
;
20126 bool memfn
= (member_p
|| (pushed_scope
20127 && CLASS_TYPE_P (pushed_scope
)));
20129 is_declarator
= true;
20131 if (ctor_dtor_or_conv_p
)
20132 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
20135 /* Parse the cv-qualifier-seq. */
20136 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20137 /* Parse the ref-qualifier. */
20138 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
20139 /* Parse the tx-qualifier. */
20140 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
20141 /* And the exception-specification. */
20142 exception_specification
20143 = cp_parser_exception_specification_opt (parser
);
20145 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20147 /* In here, we handle cases where attribute is used after
20148 the function declaration. For example:
20149 void func (int x) __attribute__((vector(..))); */
20150 tree gnu_attrs
= NULL_TREE
;
20151 tree requires_clause
= NULL_TREE
;
20152 late_return
= (cp_parser_late_return_type_opt
20153 (parser
, declarator
, requires_clause
,
20154 memfn
? cv_quals
: -1));
20156 /* Parse the virt-specifier-seq. */
20157 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20159 /* Create the function-declarator. */
20160 declarator
= make_call_declarator (declarator
,
20166 exception_specification
,
20169 declarator
->std_attributes
= attrs
;
20170 declarator
->attributes
= gnu_attrs
;
20171 /* Any subsequent parameter lists are to do with
20172 return type, so are not those of the declared
20174 parser
->default_arg_ok_p
= false;
20177 /* Remove the function parms from scope. */
20178 pop_bindings_and_leave_scope ();
20181 /* Repeat the main loop. */
20185 /* If this is the first, we can try a parenthesized
20189 bool saved_in_type_id_in_expr_p
;
20191 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20192 parser
->in_declarator_p
= saved_in_declarator_p
;
20194 open_paren
= token
;
20195 /* Consume the `('. */
20196 matching_parens parens
;
20197 parens
.consume_open (parser
);
20198 /* Parse the nested declarator. */
20199 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
20200 parser
->in_type_id_in_expr_p
= true;
20202 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
20203 /*parenthesized_p=*/NULL
,
20204 member_p
, friend_p
);
20205 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
20207 /* Expect a `)'. */
20208 close_paren
= cp_lexer_peek_token (parser
->lexer
);
20209 if (!parens
.require_close (parser
))
20210 declarator
= cp_error_declarator
;
20211 if (declarator
== cp_error_declarator
)
20214 goto handle_declarator
;
20216 /* Otherwise, we must be done. */
20220 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20221 && token
->type
== CPP_OPEN_SQUARE
20222 && !cp_next_tokens_can_be_attribute_p (parser
))
20224 /* Parse an array-declarator. */
20225 tree bounds
, attrs
;
20227 if (ctor_dtor_or_conv_p
)
20228 *ctor_dtor_or_conv_p
= 0;
20232 parser
->default_arg_ok_p
= false;
20233 parser
->in_declarator_p
= true;
20234 /* Consume the `['. */
20235 cp_lexer_consume_token (parser
->lexer
);
20236 /* Peek at the next token. */
20237 token
= cp_lexer_peek_token (parser
->lexer
);
20238 /* If the next token is `]', then there is no
20239 constant-expression. */
20240 if (token
->type
!= CPP_CLOSE_SQUARE
)
20242 bool non_constant_p
;
20244 = cp_parser_constant_expression (parser
,
20245 /*allow_non_constant=*/true,
20247 if (!non_constant_p
)
20249 else if (error_operand_p (bounds
))
20250 /* Already gave an error. */;
20251 else if (!parser
->in_function_body
20252 || current_binding_level
->kind
== sk_function_parms
)
20254 /* Normally, the array bound must be an integral constant
20255 expression. However, as an extension, we allow VLAs
20256 in function scopes as long as they aren't part of a
20257 parameter declaration. */
20258 cp_parser_error (parser
,
20259 "array bound is not an integer constant");
20260 bounds
= error_mark_node
;
20262 else if (processing_template_decl
20263 && !type_dependent_expression_p (bounds
))
20265 /* Remember this wasn't a constant-expression. */
20266 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
20267 TREE_SIDE_EFFECTS (bounds
) = 1;
20271 bounds
= NULL_TREE
;
20272 /* Look for the closing `]'. */
20273 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
20275 declarator
= cp_error_declarator
;
20279 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20280 declarator
= make_array_declarator (declarator
, bounds
);
20281 declarator
->std_attributes
= attrs
;
20283 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
20286 tree qualifying_scope
;
20287 tree unqualified_name
;
20289 special_function_kind sfk
;
20291 bool pack_expansion_p
= false;
20292 cp_token
*declarator_id_start_token
;
20294 /* Parse a declarator-id */
20295 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
20298 cp_parser_parse_tentatively (parser
);
20300 /* If we see an ellipsis, we should be looking at a
20302 if (token
->type
== CPP_ELLIPSIS
)
20304 /* Consume the `...' */
20305 cp_lexer_consume_token (parser
->lexer
);
20307 pack_expansion_p
= true;
20311 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
20313 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
20314 qualifying_scope
= parser
->scope
;
20319 if (!unqualified_name
&& pack_expansion_p
)
20321 /* Check whether an error occurred. */
20322 okay
= !cp_parser_error_occurred (parser
);
20324 /* We already consumed the ellipsis to mark a
20325 parameter pack, but we have no way to report it,
20326 so abort the tentative parse. We will be exiting
20327 immediately anyway. */
20328 cp_parser_abort_tentative_parse (parser
);
20331 okay
= cp_parser_parse_definitely (parser
);
20334 unqualified_name
= error_mark_node
;
20335 else if (unqualified_name
20336 && (qualifying_scope
20337 || (!identifier_p (unqualified_name
))))
20339 cp_parser_error (parser
, "expected unqualified-id");
20340 unqualified_name
= error_mark_node
;
20344 if (!unqualified_name
)
20346 if (unqualified_name
== error_mark_node
)
20348 declarator
= cp_error_declarator
;
20349 pack_expansion_p
= false;
20350 declarator
->parameter_pack_p
= false;
20354 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20356 if (qualifying_scope
&& at_namespace_scope_p ()
20357 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
20359 /* In the declaration of a member of a template class
20360 outside of the class itself, the SCOPE will sometimes
20361 be a TYPENAME_TYPE. For example, given:
20363 template <typename T>
20364 int S<T>::R::i = 3;
20366 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20367 this context, we must resolve S<T>::R to an ordinary
20368 type, rather than a typename type.
20370 The reason we normally avoid resolving TYPENAME_TYPEs
20371 is that a specialization of `S' might render
20372 `S<T>::R' not a type. However, if `S' is
20373 specialized, then this `i' will not be used, so there
20374 is no harm in resolving the types here. */
20377 /* Resolve the TYPENAME_TYPE. */
20378 type
= resolve_typename_type (qualifying_scope
,
20379 /*only_current_p=*/false);
20380 /* If that failed, the declarator is invalid. */
20381 if (TREE_CODE (type
) == TYPENAME_TYPE
)
20383 if (typedef_variant_p (type
))
20384 error_at (declarator_id_start_token
->location
,
20385 "cannot define member of dependent typedef "
20388 error_at (declarator_id_start_token
->location
,
20389 "%<%T::%E%> is not a type",
20390 TYPE_CONTEXT (qualifying_scope
),
20391 TYPE_IDENTIFIER (qualifying_scope
));
20393 qualifying_scope
= type
;
20398 if (unqualified_name
)
20402 if (qualifying_scope
20403 && CLASS_TYPE_P (qualifying_scope
))
20404 class_type
= qualifying_scope
;
20406 class_type
= current_class_type
;
20408 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
20410 tree name_type
= TREE_TYPE (unqualified_name
);
20412 if (!class_type
|| !same_type_p (name_type
, class_type
))
20414 /* We do not attempt to print the declarator
20415 here because we do not have enough
20416 information about its original syntactic
20418 cp_parser_error (parser
, "invalid declarator");
20419 declarator
= cp_error_declarator
;
20422 else if (qualifying_scope
20423 && CLASSTYPE_USE_TEMPLATE (name_type
))
20425 error_at (declarator_id_start_token
->location
,
20426 "invalid use of constructor as a template");
20427 inform (declarator_id_start_token
->location
,
20428 "use %<%T::%D%> instead of %<%T::%D%> to "
20429 "name the constructor in a qualified name",
20431 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
20432 class_type
, name_type
);
20433 declarator
= cp_error_declarator
;
20436 unqualified_name
= constructor_name (class_type
);
20441 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
20442 sfk
= sfk_destructor
;
20443 else if (identifier_p (unqualified_name
)
20444 && IDENTIFIER_CONV_OP_P (unqualified_name
))
20445 sfk
= sfk_conversion
;
20446 else if (/* There's no way to declare a constructor
20447 for an unnamed type, even if the type
20448 got a name for linkage purposes. */
20449 !TYPE_WAS_UNNAMED (class_type
)
20450 /* Handle correctly (c++/19200):
20464 friend void N::S();
20466 && (!friend_p
|| class_type
== qualifying_scope
)
20467 && constructor_name_p (unqualified_name
,
20469 sfk
= sfk_constructor
;
20470 else if (is_overloaded_fn (unqualified_name
)
20471 && DECL_CONSTRUCTOR_P (get_first_fn
20472 (unqualified_name
)))
20473 sfk
= sfk_constructor
;
20475 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
20476 *ctor_dtor_or_conv_p
= -1;
20479 declarator
= make_id_declarator (qualifying_scope
,
20482 declarator
->std_attributes
= attrs
;
20483 declarator
->id_loc
= token
->location
;
20484 declarator
->parameter_pack_p
= pack_expansion_p
;
20486 if (pack_expansion_p
)
20487 maybe_warn_variadic_templates ();
20490 handle_declarator
:;
20491 scope
= get_scope_of_declarator (declarator
);
20494 /* Any names that appear after the declarator-id for a
20495 member are looked up in the containing scope. */
20496 if (at_function_scope_p ())
20498 /* But declarations with qualified-ids can't appear in a
20500 cp_parser_error (parser
, "qualified-id in declaration");
20501 declarator
= cp_error_declarator
;
20504 pushed_scope
= push_scope (scope
);
20506 parser
->in_declarator_p
= true;
20507 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
20508 || (declarator
&& declarator
->kind
== cdk_id
))
20509 /* Default args are only allowed on function
20511 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20513 parser
->default_arg_ok_p
= false;
20522 /* For an abstract declarator, we might wind up with nothing at this
20523 point. That's an error; the declarator is not optional. */
20525 cp_parser_error (parser
, "expected declarator");
20526 else if (open_paren
)
20528 /* Record overly parenthesized declarator so we can give a
20529 diagnostic about confusing decl/expr disambiguation. */
20530 if (declarator
->kind
== cdk_array
)
20532 /* If the open and close parens are on different lines, this
20533 is probably a formatting thing, so ignore. */
20534 expanded_location open
= expand_location (open_paren
->location
);
20535 expanded_location close
= expand_location (close_paren
->location
);
20536 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
20540 declarator
->parenthesized
= open_paren
->location
;
20543 /* If we entered a scope, we must exit it now. */
20545 pop_scope (pushed_scope
);
20547 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20548 parser
->in_declarator_p
= saved_in_declarator_p
;
20553 /* Parse a ptr-operator.
20556 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20557 * cv-qualifier-seq [opt]
20559 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20560 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20565 & cv-qualifier-seq [opt]
20567 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20568 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20569 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20570 filled in with the TYPE containing the member. *CV_QUALS is
20571 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20572 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20573 Note that the tree codes returned by this function have nothing
20574 to do with the types of trees that will be eventually be created
20575 to represent the pointer or reference type being parsed. They are
20576 just constants with suggestive names. */
20577 static enum tree_code
20578 cp_parser_ptr_operator (cp_parser
* parser
,
20580 cp_cv_quals
*cv_quals
,
20583 enum tree_code code
= ERROR_MARK
;
20585 tree attrs
= NULL_TREE
;
20587 /* Assume that it's not a pointer-to-member. */
20589 /* And that there are no cv-qualifiers. */
20590 *cv_quals
= TYPE_UNQUALIFIED
;
20592 /* Peek at the next token. */
20593 token
= cp_lexer_peek_token (parser
->lexer
);
20595 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20596 if (token
->type
== CPP_MULT
)
20597 code
= INDIRECT_REF
;
20598 else if (token
->type
== CPP_AND
)
20600 else if ((cxx_dialect
!= cxx98
) &&
20601 token
->type
== CPP_AND_AND
) /* C++0x only */
20602 code
= NON_LVALUE_EXPR
;
20604 if (code
!= ERROR_MARK
)
20606 /* Consume the `*', `&' or `&&'. */
20607 cp_lexer_consume_token (parser
->lexer
);
20609 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20610 `&', if we are allowing GNU extensions. (The only qualifier
20611 that can legally appear after `&' is `restrict', but that is
20612 enforced during semantic analysis. */
20613 if (code
== INDIRECT_REF
20614 || cp_parser_allow_gnu_extensions_p (parser
))
20615 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20617 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20618 if (attributes
!= NULL
)
20619 *attributes
= attrs
;
20623 /* Try the pointer-to-member case. */
20624 cp_parser_parse_tentatively (parser
);
20625 /* Look for the optional `::' operator. */
20626 cp_parser_global_scope_opt (parser
,
20627 /*current_scope_valid_p=*/false);
20628 /* Look for the nested-name specifier. */
20629 token
= cp_lexer_peek_token (parser
->lexer
);
20630 cp_parser_nested_name_specifier (parser
,
20631 /*typename_keyword_p=*/false,
20632 /*check_dependency_p=*/true,
20634 /*is_declaration=*/false);
20635 /* If we found it, and the next token is a `*', then we are
20636 indeed looking at a pointer-to-member operator. */
20637 if (!cp_parser_error_occurred (parser
)
20638 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
20640 /* Indicate that the `*' operator was used. */
20641 code
= INDIRECT_REF
;
20643 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20644 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
20645 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
20646 error_at (token
->location
, "cannot form pointer to member of "
20647 "non-class %q#T", parser
->scope
);
20650 /* The type of which the member is a member is given by the
20652 *type
= parser
->scope
;
20653 /* The next name will not be qualified. */
20654 parser
->scope
= NULL_TREE
;
20655 parser
->qualifying_scope
= NULL_TREE
;
20656 parser
->object_scope
= NULL_TREE
;
20657 /* Look for optional c++11 attributes. */
20658 attrs
= cp_parser_std_attribute_spec_seq (parser
);
20659 if (attributes
!= NULL
)
20660 *attributes
= attrs
;
20661 /* Look for the optional cv-qualifier-seq. */
20662 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
20665 /* If that didn't work we don't have a ptr-operator. */
20666 if (!cp_parser_parse_definitely (parser
))
20667 cp_parser_error (parser
, "expected ptr-operator");
20673 /* Parse an (optional) cv-qualifier-seq.
20676 cv-qualifier cv-qualifier-seq [opt]
20687 Returns a bitmask representing the cv-qualifiers. */
20690 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
20692 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
20697 cp_cv_quals cv_qualifier
;
20699 /* Peek at the next token. */
20700 token
= cp_lexer_peek_token (parser
->lexer
);
20701 /* See if it's a cv-qualifier. */
20702 switch (token
->keyword
)
20705 cv_qualifier
= TYPE_QUAL_CONST
;
20709 cv_qualifier
= TYPE_QUAL_VOLATILE
;
20713 cv_qualifier
= TYPE_QUAL_RESTRICT
;
20717 cv_qualifier
= TYPE_UNQUALIFIED
;
20724 if (cv_quals
& cv_qualifier
)
20726 gcc_rich_location
richloc (token
->location
);
20727 richloc
.add_fixit_remove ();
20728 error_at (&richloc
, "duplicate cv-qualifier");
20729 cp_lexer_purge_token (parser
->lexer
);
20733 cp_lexer_consume_token (parser
->lexer
);
20734 cv_quals
|= cv_qualifier
;
20741 /* Parse an (optional) ref-qualifier
20747 Returns cp_ref_qualifier representing ref-qualifier. */
20749 static cp_ref_qualifier
20750 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
20752 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
20754 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20755 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
20760 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
20761 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20763 switch (token
->type
)
20766 curr_ref_qual
= REF_QUAL_LVALUE
;
20770 curr_ref_qual
= REF_QUAL_RVALUE
;
20774 curr_ref_qual
= REF_QUAL_NONE
;
20778 if (!curr_ref_qual
)
20782 error_at (token
->location
, "multiple ref-qualifiers");
20783 cp_lexer_purge_token (parser
->lexer
);
20787 ref_qual
= curr_ref_qual
;
20788 cp_lexer_consume_token (parser
->lexer
);
20795 /* Parse an optional tx-qualifier.
20799 transaction_safe_dynamic */
20802 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
20804 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20805 if (token
->type
== CPP_NAME
)
20807 tree name
= token
->u
.value
;
20808 const char *p
= IDENTIFIER_POINTER (name
);
20809 const int len
= strlen ("transaction_safe");
20810 if (!strncmp (p
, "transaction_safe", len
))
20814 || !strcmp (p
, "_dynamic"))
20816 cp_lexer_consume_token (parser
->lexer
);
20819 error ("%qE requires %<-fgnu-tm%>", name
);
20830 /* Parse an (optional) virt-specifier-seq.
20832 virt-specifier-seq:
20833 virt-specifier virt-specifier-seq [opt]
20839 Returns a bitmask representing the virt-specifiers. */
20841 static cp_virt_specifiers
20842 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
20844 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20849 cp_virt_specifiers virt_specifier
;
20851 /* Peek at the next token. */
20852 token
= cp_lexer_peek_token (parser
->lexer
);
20853 /* See if it's a virt-specifier-qualifier. */
20854 if (token
->type
!= CPP_NAME
)
20856 if (id_equal (token
->u
.value
, "override"))
20858 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20859 virt_specifier
= VIRT_SPEC_OVERRIDE
;
20861 else if (id_equal (token
->u
.value
, "final"))
20863 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
20864 virt_specifier
= VIRT_SPEC_FINAL
;
20866 else if (id_equal (token
->u
.value
, "__final"))
20868 virt_specifier
= VIRT_SPEC_FINAL
;
20873 if (virt_specifiers
& virt_specifier
)
20875 gcc_rich_location
richloc (token
->location
);
20876 richloc
.add_fixit_remove ();
20877 error_at (&richloc
, "duplicate virt-specifier");
20878 cp_lexer_purge_token (parser
->lexer
);
20882 cp_lexer_consume_token (parser
->lexer
);
20883 virt_specifiers
|= virt_specifier
;
20886 return virt_specifiers
;
20889 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20890 is in scope even though it isn't real. */
20893 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
20897 if (current_class_ptr
)
20899 /* We don't clear this between NSDMIs. Is it already what we want? */
20900 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
20901 if (DECL_P (current_class_ptr
)
20902 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
20903 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
20904 && cp_type_quals (type
) == quals
)
20908 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
20909 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20910 current_class_ptr
= NULL_TREE
;
20912 = cp_build_fold_indirect_ref (this_parm
);
20913 current_class_ptr
= this_parm
;
20916 /* Return true iff our current scope is a non-static data member
20920 parsing_nsdmi (void)
20922 /* We recognize NSDMI context by the context-less 'this' pointer set up
20923 by the function above. */
20924 if (current_class_ptr
20925 && TREE_CODE (current_class_ptr
) == PARM_DECL
20926 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
20931 /* Parse a late-specified return type, if any. This is not a separate
20932 non-terminal, but part of a function declarator, which looks like
20934 -> trailing-type-specifier-seq abstract-declarator(opt)
20936 Returns the type indicated by the type-id.
20938 In addition to this, parse any queued up #pragma omp declare simd
20939 clauses, and #pragma acc routine clauses.
20941 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20945 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
20946 tree
& requires_clause
, cp_cv_quals quals
)
20949 tree type
= NULL_TREE
;
20950 bool declare_simd_p
= (parser
->omp_declare_simd
20952 && declarator
->kind
== cdk_id
);
20954 bool oacc_routine_p
= (parser
->oacc_routine
20956 && declarator
->kind
== cdk_id
);
20958 /* Peek at the next token. */
20959 token
= cp_lexer_peek_token (parser
->lexer
);
20960 /* A late-specified return type is indicated by an initial '->'. */
20961 if (token
->type
!= CPP_DEREF
20962 && token
->keyword
!= RID_REQUIRES
20963 && !(token
->type
== CPP_NAME
20964 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
20965 && !(declare_simd_p
|| oacc_routine_p
))
20968 tree save_ccp
= current_class_ptr
;
20969 tree save_ccr
= current_class_ref
;
20972 /* DR 1207: 'this' is in scope in the trailing return type. */
20973 inject_this_parameter (current_class_type
, quals
);
20976 if (token
->type
== CPP_DEREF
)
20978 /* Consume the ->. */
20979 cp_lexer_consume_token (parser
->lexer
);
20981 type
= cp_parser_trailing_type_id (parser
);
20984 /* Function declarations may be followed by a trailing
20985 requires-clause. */
20986 requires_clause
= cp_parser_requires_clause_opt (parser
);
20988 if (declare_simd_p
)
20989 declarator
->attributes
20990 = cp_parser_late_parsing_omp_declare_simd (parser
,
20991 declarator
->attributes
);
20992 if (oacc_routine_p
)
20993 declarator
->attributes
20994 = cp_parser_late_parsing_oacc_routine (parser
,
20995 declarator
->attributes
);
20999 current_class_ptr
= save_ccp
;
21000 current_class_ref
= save_ccr
;
21006 /* Parse a declarator-id.
21010 :: [opt] nested-name-specifier [opt] type-name
21012 In the `id-expression' case, the value returned is as for
21013 cp_parser_id_expression if the id-expression was an unqualified-id.
21014 If the id-expression was a qualified-id, then a SCOPE_REF is
21015 returned. The first operand is the scope (either a NAMESPACE_DECL
21016 or TREE_TYPE), but the second is still just a representation of an
21020 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21023 /* The expression must be an id-expression. Assume that qualified
21024 names are the names of types so that:
21027 int S<T>::R::i = 3;
21029 will work; we must treat `S<T>::R' as the name of a type.
21030 Similarly, assume that qualified names are templates, where
21034 int S<T>::R<T>::i = 3;
21037 id
= cp_parser_id_expression (parser
,
21038 /*template_keyword_p=*/false,
21039 /*check_dependency_p=*/false,
21040 /*template_p=*/NULL
,
21041 /*declarator_p=*/true,
21043 if (id
&& BASELINK_P (id
))
21044 id
= BASELINK_FUNCTIONS (id
);
21048 /* Parse a type-id.
21051 type-specifier-seq abstract-declarator [opt]
21053 Returns the TYPE specified. */
21056 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
21057 bool is_trailing_return
)
21059 cp_decl_specifier_seq type_specifier_seq
;
21060 cp_declarator
*abstract_declarator
;
21062 /* Parse the type-specifier-seq. */
21063 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
21064 is_trailing_return
,
21065 &type_specifier_seq
);
21066 if (is_template_arg
&& type_specifier_seq
.type
21067 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
21068 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
21069 /* A bare template name as a template argument is a template template
21070 argument, not a placeholder, so fail parsing it as a type argument. */
21072 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
21073 cp_parser_simulate_error (parser
);
21074 return error_mark_node
;
21076 if (type_specifier_seq
.type
== error_mark_node
)
21077 return error_mark_node
;
21079 /* There might or might not be an abstract declarator. */
21080 cp_parser_parse_tentatively (parser
);
21081 /* Look for the declarator. */
21082 abstract_declarator
21083 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
21084 /*parenthesized_p=*/NULL
,
21085 /*member_p=*/false,
21086 /*friend_p=*/false);
21087 /* Check to see if there really was a declarator. */
21088 if (!cp_parser_parse_definitely (parser
))
21089 abstract_declarator
= NULL
;
21091 if (type_specifier_seq
.type
21092 /* The concepts TS allows 'auto' as a type-id. */
21093 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
21094 /* None of the valid uses of 'auto' in C++14 involve the type-id
21095 nonterminal, but it is valid in a trailing-return-type. */
21096 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
21097 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
21099 /* A type-id with type 'auto' is only ok if the abstract declarator
21100 is a function declarator with a late-specified return type.
21102 A type-id with 'auto' is also valid in a trailing-return-type
21103 in a compound-requirement. */
21104 if (abstract_declarator
21105 && abstract_declarator
->kind
== cdk_function
21106 && abstract_declarator
->u
.function
.late_return_type
)
21108 else if (parser
->in_result_type_constraint_p
)
21112 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
21113 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
21115 error_at (loc
, "missing template arguments after %qT",
21117 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
21121 error_at (loc
, "invalid use of %qT", auto_node
);
21122 return error_mark_node
;
21126 return groktypename (&type_specifier_seq
, abstract_declarator
,
21131 cp_parser_type_id (cp_parser
*parser
)
21133 return cp_parser_type_id_1 (parser
, false, false);
21137 cp_parser_template_type_arg (cp_parser
*parser
)
21140 const char *saved_message
= parser
->type_definition_forbidden_message
;
21141 parser
->type_definition_forbidden_message
21142 = G_("types may not be defined in template arguments");
21143 r
= cp_parser_type_id_1 (parser
, true, false);
21144 parser
->type_definition_forbidden_message
= saved_message
;
21145 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
21147 error ("invalid use of %<auto%> in template argument");
21148 r
= error_mark_node
;
21154 cp_parser_trailing_type_id (cp_parser
*parser
)
21156 return cp_parser_type_id_1 (parser
, false, true);
21159 /* Parse a type-specifier-seq.
21161 type-specifier-seq:
21162 type-specifier type-specifier-seq [opt]
21166 type-specifier-seq:
21167 attributes type-specifier-seq [opt]
21169 If IS_DECLARATION is true, we are at the start of a "condition" or
21170 exception-declaration, so we might be followed by a declarator-id.
21172 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21173 i.e. we've just seen "->".
21175 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21178 cp_parser_type_specifier_seq (cp_parser
* parser
,
21179 bool is_declaration
,
21180 bool is_trailing_return
,
21181 cp_decl_specifier_seq
*type_specifier_seq
)
21183 bool seen_type_specifier
= false;
21184 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
21185 cp_token
*start_token
= NULL
;
21187 /* Clear the TYPE_SPECIFIER_SEQ. */
21188 clear_decl_specs (type_specifier_seq
);
21190 /* In the context of a trailing return type, enum E { } is an
21191 elaborated-type-specifier followed by a function-body, not an
21193 if (is_trailing_return
)
21194 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
21196 /* Parse the type-specifiers and attributes. */
21199 tree type_specifier
;
21200 bool is_cv_qualifier
;
21202 /* Check for attributes first. */
21203 if (cp_next_tokens_can_be_attribute_p (parser
))
21205 type_specifier_seq
->attributes
21206 = attr_chainon (type_specifier_seq
->attributes
,
21207 cp_parser_attributes_opt (parser
));
21211 /* record the token of the beginning of the type specifier seq,
21212 for error reporting purposes*/
21214 start_token
= cp_lexer_peek_token (parser
->lexer
);
21216 /* Look for the type-specifier. */
21217 type_specifier
= cp_parser_type_specifier (parser
,
21219 type_specifier_seq
,
21220 /*is_declaration=*/false,
21223 if (!type_specifier
)
21225 /* If the first type-specifier could not be found, this is not a
21226 type-specifier-seq at all. */
21227 if (!seen_type_specifier
)
21229 /* Set in_declarator_p to avoid skipping to the semicolon. */
21230 int in_decl
= parser
->in_declarator_p
;
21231 parser
->in_declarator_p
= true;
21233 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
21234 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21235 cp_parser_error (parser
, "expected type-specifier");
21237 parser
->in_declarator_p
= in_decl
;
21239 type_specifier_seq
->type
= error_mark_node
;
21242 /* If subsequent type-specifiers could not be found, the
21243 type-specifier-seq is complete. */
21247 seen_type_specifier
= true;
21248 /* The standard says that a condition can be:
21250 type-specifier-seq declarator = assignment-expression
21257 we should treat the "S" as a declarator, not as a
21258 type-specifier. The standard doesn't say that explicitly for
21259 type-specifier-seq, but it does say that for
21260 decl-specifier-seq in an ordinary declaration. Perhaps it
21261 would be clearer just to allow a decl-specifier-seq here, and
21262 then add a semantic restriction that if any decl-specifiers
21263 that are not type-specifiers appear, the program is invalid. */
21264 if (is_declaration
&& !is_cv_qualifier
)
21265 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
21269 /* Return whether the function currently being declared has an associated
21270 template parameter list. */
21273 function_being_declared_is_template_p (cp_parser
* parser
)
21275 if (!current_template_parms
|| processing_template_parmlist
)
21278 if (parser
->implicit_template_scope
)
21281 if (at_class_scope_p ()
21282 && TYPE_BEING_DEFINED (current_class_type
))
21283 return parser
->num_template_parameter_lists
!= 0;
21285 return ((int) parser
->num_template_parameter_lists
> template_class_depth
21286 (current_class_type
));
21289 /* Parse a parameter-declaration-clause.
21291 parameter-declaration-clause:
21292 parameter-declaration-list [opt] ... [opt]
21293 parameter-declaration-list , ...
21295 Returns a representation for the parameter declarations. A return
21296 value of NULL indicates a parameter-declaration-clause consisting
21297 only of an ellipsis. */
21300 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
21306 temp_override
<bool> cleanup
21307 (parser
->auto_is_implicit_function_template_parm_p
);
21309 if (!processing_specialization
21310 && !processing_template_parmlist
21311 && !processing_explicit_instantiation
21312 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21313 actual function or a random abstract declarator. */
21314 && parser
->default_arg_ok_p
)
21315 if (!current_function_decl
21316 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
21317 parser
->auto_is_implicit_function_template_parm_p
= true;
21319 /* Peek at the next token. */
21320 token
= cp_lexer_peek_token (parser
->lexer
);
21321 /* Check for trivial parameter-declaration-clauses. */
21322 if (token
->type
== CPP_ELLIPSIS
)
21324 /* Consume the `...' token. */
21325 cp_lexer_consume_token (parser
->lexer
);
21328 else if (token
->type
== CPP_CLOSE_PAREN
)
21329 /* There are no parameters. */
21331 #ifndef NO_IMPLICIT_EXTERN_C
21332 if (in_system_header_at (input_location
)
21333 && current_class_type
== NULL
21334 && current_lang_name
== lang_name_c
)
21338 return void_list_node
;
21340 /* Check for `(void)', too, which is a special case. */
21341 else if (token
->keyword
== RID_VOID
21342 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21343 == CPP_CLOSE_PAREN
))
21345 /* Consume the `void' token. */
21346 cp_lexer_consume_token (parser
->lexer
);
21347 /* There are no parameters. */
21348 return void_list_node
;
21351 /* Parse the parameter-declaration-list. */
21352 parameters
= cp_parser_parameter_declaration_list (parser
);
21353 /* If a parse error occurred while parsing the
21354 parameter-declaration-list, then the entire
21355 parameter-declaration-clause is erroneous. */
21356 if (parameters
== error_mark_node
)
21359 /* Peek at the next token. */
21360 token
= cp_lexer_peek_token (parser
->lexer
);
21361 /* If it's a `,', the clause should terminate with an ellipsis. */
21362 if (token
->type
== CPP_COMMA
)
21364 /* Consume the `,'. */
21365 cp_lexer_consume_token (parser
->lexer
);
21366 /* Expect an ellipsis. */
21368 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
21370 /* It might also be `...' if the optional trailing `,' was
21372 else if (token
->type
== CPP_ELLIPSIS
)
21374 /* Consume the `...' token. */
21375 cp_lexer_consume_token (parser
->lexer
);
21376 /* And remember that we saw it. */
21380 ellipsis_p
= false;
21382 /* Finish the parameter list. */
21384 parameters
= chainon (parameters
, void_list_node
);
21389 /* Parse a parameter-declaration-list.
21391 parameter-declaration-list:
21392 parameter-declaration
21393 parameter-declaration-list , parameter-declaration
21395 Returns a representation of the parameter-declaration-list, as for
21396 cp_parser_parameter_declaration_clause. However, the
21397 `void_list_node' is never appended to the list. */
21400 cp_parser_parameter_declaration_list (cp_parser
* parser
)
21402 tree parameters
= NULL_TREE
;
21403 tree
*tail
= ¶meters
;
21404 bool saved_in_unbraced_linkage_specification_p
;
21407 /* The special considerations that apply to a function within an
21408 unbraced linkage specifications do not apply to the parameters
21409 to the function. */
21410 saved_in_unbraced_linkage_specification_p
21411 = parser
->in_unbraced_linkage_specification_p
;
21412 parser
->in_unbraced_linkage_specification_p
= false;
21414 /* Look for more parameters. */
21417 cp_parameter_declarator
*parameter
;
21418 tree decl
= error_mark_node
;
21419 bool parenthesized_p
= false;
21421 /* Parse the parameter. */
21423 = cp_parser_parameter_declaration (parser
,
21424 /*template_parm_p=*/false,
21427 /* We don't know yet if the enclosing context is deprecated, so wait
21428 and warn in grokparms if appropriate. */
21429 deprecated_state
= DEPRECATED_SUPPRESS
;
21433 decl
= grokdeclarator (parameter
->declarator
,
21434 ¶meter
->decl_specifiers
,
21436 parameter
->default_argument
!= NULL_TREE
,
21437 ¶meter
->decl_specifiers
.attributes
);
21438 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
21439 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
21442 deprecated_state
= DEPRECATED_NORMAL
;
21444 /* If a parse error occurred parsing the parameter declaration,
21445 then the entire parameter-declaration-list is erroneous. */
21446 if (decl
== error_mark_node
)
21448 parameters
= error_mark_node
;
21452 if (parameter
->decl_specifiers
.attributes
)
21453 cplus_decl_attributes (&decl
,
21454 parameter
->decl_specifiers
.attributes
,
21456 if (DECL_NAME (decl
))
21457 decl
= pushdecl (decl
);
21459 if (decl
!= error_mark_node
)
21461 retrofit_lang_decl (decl
);
21462 DECL_PARM_INDEX (decl
) = ++index
;
21463 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
21466 /* Add the new parameter to the list. */
21467 *tail
= build_tree_list (parameter
->default_argument
, decl
);
21468 tail
= &TREE_CHAIN (*tail
);
21470 /* Peek at the next token. */
21471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
21472 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
21473 /* These are for Objective-C++ */
21474 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
21475 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21476 /* The parameter-declaration-list is complete. */
21478 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21482 /* Peek at the next token. */
21483 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21484 /* If it's an ellipsis, then the list is complete. */
21485 if (token
->type
== CPP_ELLIPSIS
)
21487 /* Otherwise, there must be more parameters. Consume the
21489 cp_lexer_consume_token (parser
->lexer
);
21490 /* When parsing something like:
21492 int i(float f, double d)
21494 we can tell after seeing the declaration for "f" that we
21495 are not looking at an initialization of a variable "i",
21496 but rather at the declaration of a function "i".
21498 Due to the fact that the parsing of template arguments
21499 (as specified to a template-id) requires backtracking we
21500 cannot use this technique when inside a template argument
21502 if (!parser
->in_template_argument_list_p
21503 && !parser
->in_type_id_in_expr_p
21504 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21505 /* However, a parameter-declaration of the form
21506 "float(f)" (which is a valid declaration of a
21507 parameter "f") can also be interpreted as an
21508 expression (the conversion of "f" to "float"). */
21509 && !parenthesized_p
)
21510 cp_parser_commit_to_tentative_parse (parser
);
21514 cp_parser_error (parser
, "expected %<,%> or %<...%>");
21515 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21516 cp_parser_skip_to_closing_parenthesis (parser
,
21517 /*recovering=*/true,
21518 /*or_comma=*/false,
21519 /*consume_paren=*/false);
21524 parser
->in_unbraced_linkage_specification_p
21525 = saved_in_unbraced_linkage_specification_p
;
21527 /* Reset implicit_template_scope if we are about to leave the function
21528 parameter list that introduced it. Note that for out-of-line member
21529 definitions, there will be one or more class scopes before we get to
21530 the template parameter scope. */
21532 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
21533 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
21535 while (maybe_its
->kind
== sk_class
)
21536 maybe_its
= maybe_its
->level_chain
;
21537 if (maybe_its
== its
)
21539 parser
->implicit_template_parms
= 0;
21540 parser
->implicit_template_scope
= 0;
21547 /* Parse a parameter declaration.
21549 parameter-declaration:
21550 decl-specifier-seq ... [opt] declarator
21551 decl-specifier-seq declarator = assignment-expression
21552 decl-specifier-seq ... [opt] abstract-declarator [opt]
21553 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21555 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21556 declares a template parameter. (In that case, a non-nested `>'
21557 token encountered during the parsing of the assignment-expression
21558 is not interpreted as a greater-than operator.)
21560 Returns a representation of the parameter, or NULL if an error
21561 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21562 true iff the declarator is of the form "(p)". */
21564 static cp_parameter_declarator
*
21565 cp_parser_parameter_declaration (cp_parser
*parser
,
21566 bool template_parm_p
,
21567 bool *parenthesized_p
)
21569 int declares_class_or_enum
;
21570 cp_decl_specifier_seq decl_specifiers
;
21571 cp_declarator
*declarator
;
21572 tree default_argument
;
21573 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
21574 const char *saved_message
;
21575 bool template_parameter_pack_p
= false;
21577 /* In a template parameter, `>' is not an operator.
21581 When parsing a default template-argument for a non-type
21582 template-parameter, the first non-nested `>' is taken as the end
21583 of the template parameter-list rather than a greater-than
21586 /* Type definitions may not appear in parameter types. */
21587 saved_message
= parser
->type_definition_forbidden_message
;
21588 parser
->type_definition_forbidden_message
21589 = G_("types may not be defined in parameter types");
21591 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
21592 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21593 (current_template_parms
)) : 0);
21595 /* Parse the declaration-specifiers. */
21596 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21597 cp_parser_decl_specifier_seq (parser
,
21598 CP_PARSER_FLAGS_NONE
,
21600 &declares_class_or_enum
);
21602 /* Complain about missing 'typename' or other invalid type names. */
21603 if (!decl_specifiers
.any_type_specifiers_p
21604 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21605 decl_specifiers
.type
= error_mark_node
;
21607 /* If an error occurred, there's no reason to attempt to parse the
21608 rest of the declaration. */
21609 if (cp_parser_error_occurred (parser
))
21611 parser
->type_definition_forbidden_message
= saved_message
;
21615 /* Peek at the next token. */
21616 token
= cp_lexer_peek_token (parser
->lexer
);
21618 /* If the next token is a `)', `,', `=', `>', or `...', then there
21619 is no declarator. However, when variadic templates are enabled,
21620 there may be a declarator following `...'. */
21621 if (token
->type
== CPP_CLOSE_PAREN
21622 || token
->type
== CPP_COMMA
21623 || token
->type
== CPP_EQ
21624 || token
->type
== CPP_GREATER
)
21627 if (parenthesized_p
)
21628 *parenthesized_p
= false;
21630 /* Otherwise, there should be a declarator. */
21633 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21634 parser
->default_arg_ok_p
= false;
21636 /* After seeing a decl-specifier-seq, if the next token is not a
21637 "(", there is no possibility that the code is a valid
21638 expression. Therefore, if parsing tentatively, we commit at
21640 if (!parser
->in_template_argument_list_p
21641 /* In an expression context, having seen:
21645 we cannot be sure whether we are looking at a
21646 function-type (taking a "char" as a parameter) or a cast
21647 of some object of type "char" to "int". */
21648 && !parser
->in_type_id_in_expr_p
21649 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
21650 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
21651 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
21652 cp_parser_commit_to_tentative_parse (parser
);
21653 /* Parse the declarator. */
21654 declarator_token_start
= token
;
21655 declarator
= cp_parser_declarator (parser
,
21656 CP_PARSER_DECLARATOR_EITHER
,
21657 /*ctor_dtor_or_conv_p=*/NULL
,
21659 /*member_p=*/false,
21660 /*friend_p=*/false);
21661 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21662 /* After the declarator, allow more attributes. */
21663 decl_specifiers
.attributes
21664 = attr_chainon (decl_specifiers
.attributes
,
21665 cp_parser_attributes_opt (parser
));
21667 /* If the declarator is a template parameter pack, remember that and
21668 clear the flag in the declarator itself so we don't get errors
21669 from grokdeclarator. */
21670 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
21672 declarator
->parameter_pack_p
= false;
21673 template_parameter_pack_p
= true;
21677 /* If the next token is an ellipsis, and we have not seen a declarator
21678 name, and if either the type of the declarator contains parameter
21679 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21680 for, eg, abbreviated integral type names), then we actually have a
21681 parameter pack expansion expression. Otherwise, leave the ellipsis
21682 for a C-style variadic function. */
21683 token
= cp_lexer_peek_token (parser
->lexer
);
21685 /* If a function parameter pack was specified and an implicit template
21686 parameter was introduced during cp_parser_parameter_declaration,
21687 change any implicit parameters introduced into packs. */
21688 if (parser
->implicit_template_parms
21689 && (token
->type
== CPP_ELLIPSIS
21690 || (declarator
&& declarator
->parameter_pack_p
)))
21692 int latest_template_parm_idx
= TREE_VEC_LENGTH
21693 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
21695 if (latest_template_parm_idx
!= template_parm_idx
)
21696 decl_specifiers
.type
= convert_generic_types_to_packs
21697 (decl_specifiers
.type
,
21698 template_parm_idx
, latest_template_parm_idx
);
21701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21703 tree type
= decl_specifiers
.type
;
21705 if (type
&& DECL_P (type
))
21706 type
= TREE_TYPE (type
);
21709 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
21710 && (template_parm_p
|| uses_parameter_packs (type
)))
21711 || (!type
&& template_parm_p
))
21712 && declarator_can_be_parameter_pack (declarator
))
21714 /* Consume the `...'. */
21715 cp_lexer_consume_token (parser
->lexer
);
21716 maybe_warn_variadic_templates ();
21718 /* Build a pack expansion type */
21719 if (template_parm_p
)
21720 template_parameter_pack_p
= true;
21721 else if (declarator
)
21722 declarator
->parameter_pack_p
= true;
21724 decl_specifiers
.type
= make_pack_expansion (type
);
21728 /* The restriction on defining new types applies only to the type
21729 of the parameter, not to the default argument. */
21730 parser
->type_definition_forbidden_message
= saved_message
;
21732 /* If the next token is `=', then process a default argument. */
21733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21735 tree type
= decl_specifiers
.type
;
21736 token
= cp_lexer_peek_token (parser
->lexer
);
21737 /* If we are defining a class, then the tokens that make up the
21738 default argument must be saved and processed later. */
21739 if (!template_parm_p
&& at_class_scope_p ()
21740 && TYPE_BEING_DEFINED (current_class_type
)
21741 && !LAMBDA_TYPE_P (current_class_type
))
21742 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
21744 // A constrained-type-specifier may declare a type template-parameter.
21745 else if (declares_constrained_type_template_parameter (type
))
21747 = cp_parser_default_type_template_argument (parser
);
21749 // A constrained-type-specifier may declare a template-template-parameter.
21750 else if (declares_constrained_template_template_parameter (type
))
21752 = cp_parser_default_template_template_argument (parser
);
21754 /* Outside of a class definition, we can just parse the
21755 assignment-expression. */
21758 = cp_parser_default_argument (parser
, template_parm_p
);
21760 if (!parser
->default_arg_ok_p
)
21762 permerror (token
->location
,
21763 "default arguments are only "
21764 "permitted for function parameters");
21766 else if ((declarator
&& declarator
->parameter_pack_p
)
21767 || template_parameter_pack_p
21768 || (decl_specifiers
.type
21769 && PACK_EXPANSION_P (decl_specifiers
.type
)))
21771 /* Find the name of the parameter pack. */
21772 cp_declarator
*id_declarator
= declarator
;
21773 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
21774 id_declarator
= id_declarator
->declarator
;
21776 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
21777 error_at (declarator_token_start
->location
,
21779 ? G_("template parameter pack %qD "
21780 "cannot have a default argument")
21781 : G_("parameter pack %qD cannot have "
21782 "a default argument"),
21783 id_declarator
->u
.id
.unqualified_name
);
21785 error_at (declarator_token_start
->location
,
21787 ? G_("template parameter pack cannot have "
21788 "a default argument")
21789 : G_("parameter pack cannot have a "
21790 "default argument"));
21792 default_argument
= NULL_TREE
;
21796 default_argument
= NULL_TREE
;
21798 /* Generate a location for the parameter, ranging from the start of the
21799 initial token to the end of the final token (using input_location for
21800 the latter, set up by cp_lexer_set_source_position_from_token when
21803 If we have a identifier, then use it for the caret location, e.g.
21805 extern int callee (int one, int (*two)(int, int), float three);
21806 ~~~~~~^~~~~~~~~~~~~~
21808 otherwise, reuse the start location for the caret location e.g.:
21810 extern int callee (int one, int (*)(int, int), float three);
21814 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
21815 ? declarator
->id_loc
21816 : decl_spec_token_start
->location
);
21817 location_t param_loc
= make_location (caret_loc
,
21818 decl_spec_token_start
->location
,
21821 return make_parameter_declarator (&decl_specifiers
,
21825 template_parameter_pack_p
);
21828 /* Parse a default argument and return it.
21830 TEMPLATE_PARM_P is true if this is a default argument for a
21831 non-type template parameter. */
21833 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
21835 tree default_argument
= NULL_TREE
;
21836 bool saved_greater_than_is_operator_p
;
21837 bool saved_local_variables_forbidden_p
;
21838 bool non_constant_p
, is_direct_init
;
21840 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21842 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
21843 parser
->greater_than_is_operator_p
= !template_parm_p
;
21844 /* Local variable names (and the `this' keyword) may not
21845 appear in a default argument. */
21846 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
21847 parser
->local_variables_forbidden_p
= true;
21848 /* Parse the assignment-expression. */
21849 if (template_parm_p
)
21850 push_deferring_access_checks (dk_no_deferred
);
21851 tree saved_class_ptr
= NULL_TREE
;
21852 tree saved_class_ref
= NULL_TREE
;
21853 /* The "this" pointer is not valid in a default argument. */
21856 saved_class_ptr
= current_class_ptr
;
21857 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
21858 saved_class_ref
= current_class_ref
;
21859 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
21862 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
21863 /* Restore the "this" pointer. */
21866 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
21867 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
21869 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
21870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21871 if (template_parm_p
)
21872 pop_deferring_access_checks ();
21873 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
21874 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
21876 return default_argument
;
21879 /* Parse a function-body.
21882 compound_statement */
21885 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
21887 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
21888 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
21892 /* Parse a ctor-initializer-opt followed by a function-body. Return
21893 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21894 is true we are parsing a function-try-block. */
21897 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
21898 bool in_function_try_block
)
21901 const bool check_body_p
=
21902 DECL_CONSTRUCTOR_P (current_function_decl
)
21903 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
21906 /* Begin the function body. */
21907 body
= begin_function_body ();
21908 /* Parse the optional ctor-initializer. */
21909 cp_parser_ctor_initializer_opt (parser
);
21911 /* If we're parsing a constexpr constructor definition, we need
21912 to check that the constructor body is indeed empty. However,
21913 before we get to cp_parser_function_body lot of junk has been
21914 generated, so we can't just check that we have an empty block.
21915 Rather we take a snapshot of the outermost block, and check whether
21916 cp_parser_function_body changed its state. */
21919 list
= cur_stmt_list
;
21920 if (STATEMENT_LIST_TAIL (list
))
21921 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
21923 /* Parse the function-body. */
21924 cp_parser_function_body (parser
, in_function_try_block
);
21926 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
21927 /* Finish the function body. */
21928 finish_function_body (body
);
21931 /* Parse an initializer.
21934 = initializer-clause
21935 ( expression-list )
21937 Returns an expression representing the initializer. If no
21938 initializer is present, NULL_TREE is returned.
21940 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21941 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21942 set to TRUE if there is no initializer present. If there is an
21943 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21944 is set to true; otherwise it is set to false. */
21947 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
21948 bool* non_constant_p
, bool subexpression_p
)
21953 /* Peek at the next token. */
21954 token
= cp_lexer_peek_token (parser
->lexer
);
21956 /* Let our caller know whether or not this initializer was
21958 *is_direct_init
= (token
->type
!= CPP_EQ
);
21959 /* Assume that the initializer is constant. */
21960 *non_constant_p
= false;
21962 if (token
->type
== CPP_EQ
)
21964 /* Consume the `='. */
21965 cp_lexer_consume_token (parser
->lexer
);
21966 /* Parse the initializer-clause. */
21967 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
21969 else if (token
->type
== CPP_OPEN_PAREN
)
21971 vec
<tree
, va_gc
> *vec
;
21972 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
21974 /*allow_expansion_p=*/true,
21977 return error_mark_node
;
21978 init
= build_tree_list_vec (vec
);
21979 release_tree_vector (vec
);
21981 else if (token
->type
== CPP_OPEN_BRACE
)
21983 cp_lexer_set_source_position (parser
->lexer
);
21984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
21985 init
= cp_parser_braced_list (parser
, non_constant_p
);
21986 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
21990 /* Anything else is an error. */
21991 cp_parser_error (parser
, "expected initializer");
21992 init
= error_mark_node
;
21995 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
21996 init
= error_mark_node
;
22001 /* Parse an initializer-clause.
22003 initializer-clause:
22004 assignment-expression
22007 Returns an expression representing the initializer.
22009 If the `assignment-expression' production is used the value
22010 returned is simply a representation for the expression.
22012 Otherwise, calls cp_parser_braced_list. */
22015 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
22017 cp_expr initializer
;
22019 /* Assume the expression is constant. */
22020 *non_constant_p
= false;
22022 /* If it is not a `{', then we are looking at an
22023 assignment-expression. */
22024 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
22027 = cp_parser_constant_expression (parser
,
22028 /*allow_non_constant_p=*/true,
22032 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
22034 return initializer
;
22037 /* Parse a brace-enclosed initializer list.
22040 { initializer-list , [opt] }
22041 { designated-initializer-list , [opt] }
22044 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22045 the elements of the initializer-list (or NULL, if the last
22046 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22047 NULL_TREE. There is no way to detect whether or not the optional
22048 trailing `,' was provided. NON_CONSTANT_P is as for
22049 cp_parser_initializer. */
22052 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
22055 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22057 /* Consume the `{' token. */
22058 matching_braces braces
;
22059 braces
.require_open (parser
);
22060 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22061 initializer
= make_node (CONSTRUCTOR
);
22062 /* If it's not a `}', then there is a non-trivial initializer. */
22063 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
22065 /* Parse the initializer list. */
22066 CONSTRUCTOR_ELTS (initializer
)
22067 = cp_parser_initializer_list (parser
, non_constant_p
);
22068 /* A trailing `,' token is allowed. */
22069 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22070 cp_lexer_consume_token (parser
->lexer
);
22073 *non_constant_p
= false;
22074 /* Now, there should be a trailing `}'. */
22075 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22076 braces
.require_close (parser
);
22077 TREE_TYPE (initializer
) = init_list_type_node
;
22079 cp_expr
result (initializer
);
22080 /* Build a location of the form:
22083 with caret==start at the open brace, finish at the close brace. */
22084 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
22085 result
.set_location (combined_loc
);
22089 /* Consume tokens up to, and including, the next non-nested closing `]'.
22090 Returns true iff we found a closing `]'. */
22093 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
22095 unsigned square_depth
= 0;
22099 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
22101 switch (token
->type
)
22104 case CPP_PRAGMA_EOL
:
22105 /* If we've run out of tokens, then there is no closing `]'. */
22108 case CPP_OPEN_SQUARE
:
22112 case CPP_CLOSE_SQUARE
:
22113 if (!square_depth
--)
22115 cp_lexer_consume_token (parser
->lexer
);
22124 /* Consume the token. */
22125 cp_lexer_consume_token (parser
->lexer
);
22129 /* Return true if we are looking at an array-designator, false otherwise. */
22132 cp_parser_array_designator_p (cp_parser
*parser
)
22134 /* Consume the `['. */
22135 cp_lexer_consume_token (parser
->lexer
);
22137 cp_lexer_save_tokens (parser
->lexer
);
22139 /* Skip tokens until the next token is a closing square bracket.
22140 If we find the closing `]', and the next token is a `=', then
22141 we are looking at an array designator. */
22142 bool array_designator_p
22143 = (cp_parser_skip_to_closing_square_bracket (parser
)
22144 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
22146 /* Roll back the tokens we skipped. */
22147 cp_lexer_rollback_tokens (parser
->lexer
);
22149 return array_designator_p
;
22152 /* Parse an initializer-list.
22155 initializer-clause ... [opt]
22156 initializer-list , initializer-clause ... [opt]
22160 designated-initializer-list:
22161 designated-initializer-clause
22162 designated-initializer-list , designated-initializer-clause
22164 designated-initializer-clause:
22165 designator brace-or-equal-initializer
22173 designation initializer-clause ...[opt]
22174 initializer-list , designation initializer-clause ...[opt]
22179 [ constant-expression ] =
22181 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22182 for the initializer. If the INDEX of the elt is non-NULL, it is the
22183 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22184 as for cp_parser_initializer. */
22186 static vec
<constructor_elt
, va_gc
> *
22187 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
22189 vec
<constructor_elt
, va_gc
> *v
= NULL
;
22190 bool first_p
= true;
22191 tree first_designator
= NULL_TREE
;
22193 /* Assume all of the expressions are constant. */
22194 *non_constant_p
= false;
22196 /* Parse the rest of the list. */
22202 bool clause_non_constant_p
;
22203 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22205 /* Handle the C++2A syntax, '. id ='. */
22206 if ((cxx_dialect
>= cxx2a
22207 || cp_parser_allow_gnu_extensions_p (parser
))
22208 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
22209 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
22210 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
22211 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
22212 == CPP_OPEN_BRACE
)))
22214 if (cxx_dialect
< cxx2a
)
22215 pedwarn (loc
, OPT_Wpedantic
,
22216 "C++ designated initializers only available with "
22217 "-std=c++2a or -std=gnu++2a");
22218 /* Consume the `.'. */
22219 cp_lexer_consume_token (parser
->lexer
);
22220 /* Consume the identifier. */
22221 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22222 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22223 /* Consume the `='. */
22224 cp_lexer_consume_token (parser
->lexer
);
22226 /* Also, if the next token is an identifier and the following one is a
22227 colon, we are looking at the GNU designated-initializer
22229 else if (cp_parser_allow_gnu_extensions_p (parser
)
22230 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
22231 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22234 /* Warn the user that they are using an extension. */
22235 pedwarn (loc
, OPT_Wpedantic
,
22236 "ISO C++ does not allow GNU designated initializers");
22237 /* Consume the identifier. */
22238 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
22239 /* Consume the `:'. */
22240 cp_lexer_consume_token (parser
->lexer
);
22242 /* Also handle C99 array designators, '[ const ] ='. */
22243 else if (cp_parser_allow_gnu_extensions_p (parser
)
22244 && !c_dialect_objc ()
22245 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22247 /* In C++11, [ could start a lambda-introducer. */
22248 bool non_const
= false;
22250 cp_parser_parse_tentatively (parser
);
22252 if (!cp_parser_array_designator_p (parser
))
22254 cp_parser_simulate_error (parser
);
22255 designator
= NULL_TREE
;
22259 designator
= cp_parser_constant_expression (parser
, true,
22261 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22262 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
22265 if (!cp_parser_parse_definitely (parser
))
22266 designator
= NULL_TREE
;
22268 && (!require_potential_rvalue_constant_expression
22270 designator
= NULL_TREE
;
22272 /* Warn the user that they are using an extension. */
22273 pedwarn (loc
, OPT_Wpedantic
,
22274 "ISO C++ does not allow C99 designated initializers");
22277 designator
= NULL_TREE
;
22281 first_designator
= designator
;
22284 else if (cxx_dialect
>= cxx2a
22285 && first_designator
!= error_mark_node
22286 && (!first_designator
!= !designator
))
22288 error_at (loc
, "either all initializer clauses should be designated "
22289 "or none of them should be");
22290 first_designator
= error_mark_node
;
22292 else if (cxx_dialect
< cxx2a
&& !first_designator
)
22293 first_designator
= designator
;
22295 /* Parse the initializer. */
22296 initializer
= cp_parser_initializer_clause (parser
,
22297 &clause_non_constant_p
);
22298 /* If any clause is non-constant, so is the entire initializer. */
22299 if (clause_non_constant_p
)
22300 *non_constant_p
= true;
22302 /* If we have an ellipsis, this is an initializer pack
22304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22306 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
22308 /* Consume the `...'. */
22309 cp_lexer_consume_token (parser
->lexer
);
22311 if (designator
&& cxx_dialect
>= cxx2a
)
22313 "%<...%> not allowed in designated initializer list");
22315 /* Turn the initializer into an initializer expansion. */
22316 initializer
= make_pack_expansion (initializer
);
22319 /* Add it to the vector. */
22320 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
22322 /* If the next token is not a comma, we have reached the end of
22324 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22327 /* Peek at the next token. */
22328 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22329 /* If the next token is a `}', then we're still done. An
22330 initializer-clause can have a trailing `,' after the
22331 initializer-list and before the closing `}'. */
22332 if (token
->type
== CPP_CLOSE_BRACE
)
22335 /* Consume the `,' token. */
22336 cp_lexer_consume_token (parser
->lexer
);
22339 /* The same identifier shall not appear in multiple designators
22340 of a designated-initializer-list. */
22341 if (first_designator
)
22344 tree designator
, val
;
22345 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22346 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22348 if (IDENTIFIER_MARKED (designator
))
22350 error_at (EXPR_LOC_OR_LOC (val
, input_location
),
22351 "%<.%s%> designator used multiple times in "
22352 "the same initializer list",
22353 IDENTIFIER_POINTER (designator
));
22354 (*v
)[i
].index
= NULL_TREE
;
22357 IDENTIFIER_MARKED (designator
) = 1;
22359 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
22360 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
22361 IDENTIFIER_MARKED (designator
) = 0;
22367 /* Classes [gram.class] */
22369 /* Parse a class-name.
22375 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22376 to indicate that names looked up in dependent types should be
22377 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22378 keyword has been used to indicate that the name that appears next
22379 is a template. TAG_TYPE indicates the explicit tag given before
22380 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22381 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22382 is the class being defined in a class-head. If ENUM_OK is TRUE,
22383 enum-names are also accepted.
22385 Returns the TYPE_DECL representing the class. */
22388 cp_parser_class_name (cp_parser
*parser
,
22389 bool typename_keyword_p
,
22390 bool template_keyword_p
,
22391 enum tag_types tag_type
,
22392 bool check_dependency_p
,
22394 bool is_declaration
,
22401 tree identifier
= NULL_TREE
;
22403 /* All class-names start with an identifier. */
22404 token
= cp_lexer_peek_token (parser
->lexer
);
22405 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
22407 cp_parser_error (parser
, "expected class-name");
22408 return error_mark_node
;
22411 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22412 to a template-id, so we save it here. */
22413 scope
= parser
->scope
;
22414 if (scope
== error_mark_node
)
22415 return error_mark_node
;
22417 /* Any name names a type if we're following the `typename' keyword
22418 in a qualified name where the enclosing scope is type-dependent. */
22419 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
22420 && dependent_type_p (scope
));
22421 /* Handle the common case (an identifier, but not a template-id)
22423 if (token
->type
== CPP_NAME
22424 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
22426 cp_token
*identifier_token
;
22429 /* Look for the identifier. */
22430 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
22431 ambiguous_p
= identifier_token
->error_reported
;
22432 identifier
= cp_parser_identifier (parser
);
22433 /* If the next token isn't an identifier, we are certainly not
22434 looking at a class-name. */
22435 if (identifier
== error_mark_node
)
22436 decl
= error_mark_node
;
22437 /* If we know this is a type-name, there's no need to look it
22439 else if (typename_p
)
22443 tree ambiguous_decls
;
22444 /* If we already know that this lookup is ambiguous, then
22445 we've already issued an error message; there's no reason
22449 cp_parser_simulate_error (parser
);
22450 return error_mark_node
;
22452 /* If the next token is a `::', then the name must be a type
22455 [basic.lookup.qual]
22457 During the lookup for a name preceding the :: scope
22458 resolution operator, object, function, and enumerator
22459 names are ignored. */
22460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22461 tag_type
= scope_type
;
22462 /* Look up the name. */
22463 decl
= cp_parser_lookup_name (parser
, identifier
,
22465 /*is_template=*/false,
22466 /*is_namespace=*/false,
22467 check_dependency_p
,
22469 identifier_token
->location
);
22470 if (ambiguous_decls
)
22472 if (cp_parser_parsing_tentatively (parser
))
22473 cp_parser_simulate_error (parser
);
22474 return error_mark_node
;
22480 /* Try a template-id. */
22481 decl
= cp_parser_template_id (parser
, template_keyword_p
,
22482 check_dependency_p
,
22485 if (decl
== error_mark_node
)
22486 return error_mark_node
;
22489 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
22491 /* If this is a typename, create a TYPENAME_TYPE. */
22492 if (typename_p
&& decl
!= error_mark_node
)
22494 decl
= make_typename_type (scope
, decl
, typename_type
,
22495 /*complain=*/tf_error
);
22496 if (decl
!= error_mark_node
)
22497 decl
= TYPE_NAME (decl
);
22500 decl
= strip_using_decl (decl
);
22502 /* Check to see that it is really the name of a class. */
22503 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
22504 && identifier_p (TREE_OPERAND (decl
, 0))
22505 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
22506 /* Situations like this:
22508 template <typename T> struct A {
22509 typename T::template X<int>::I i;
22512 are problematic. Is `T::template X<int>' a class-name? The
22513 standard does not seem to be definitive, but there is no other
22514 valid interpretation of the following `::'. Therefore, those
22515 names are considered class-names. */
22517 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
22518 if (decl
!= error_mark_node
)
22519 decl
= TYPE_NAME (decl
);
22521 else if (TREE_CODE (decl
) != TYPE_DECL
22522 || TREE_TYPE (decl
) == error_mark_node
22523 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
22524 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
22525 /* In Objective-C 2.0, a classname followed by '.' starts a
22526 dot-syntax expression, and it's not a type-name. */
22527 || (c_dialect_objc ()
22528 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
22529 && objc_is_class_name (decl
)))
22530 decl
= error_mark_node
;
22532 if (decl
== error_mark_node
)
22533 cp_parser_error (parser
, "expected class-name");
22534 else if (identifier
&& !parser
->scope
)
22535 maybe_note_name_used_in_class (identifier
, decl
);
22540 /* Parse a class-specifier.
22543 class-head { member-specification [opt] }
22545 Returns the TREE_TYPE representing the class. */
22548 cp_parser_class_specifier_1 (cp_parser
* parser
)
22551 tree attributes
= NULL_TREE
;
22552 bool nested_name_specifier_p
;
22553 unsigned saved_num_template_parameter_lists
;
22554 bool saved_in_function_body
;
22555 unsigned char in_statement
;
22556 bool in_switch_statement_p
;
22557 bool saved_in_unbraced_linkage_specification_p
;
22558 tree old_scope
= NULL_TREE
;
22559 tree scope
= NULL_TREE
;
22560 cp_token
*closing_brace
;
22562 push_deferring_access_checks (dk_no_deferred
);
22564 /* Parse the class-head. */
22565 type
= cp_parser_class_head (parser
,
22566 &nested_name_specifier_p
);
22567 /* If the class-head was a semantic disaster, skip the entire body
22571 cp_parser_skip_to_end_of_block_or_statement (parser
);
22572 pop_deferring_access_checks ();
22573 return error_mark_node
;
22576 /* Look for the `{'. */
22577 matching_braces braces
;
22578 if (!braces
.require_open (parser
))
22580 pop_deferring_access_checks ();
22581 return error_mark_node
;
22584 cp_ensure_no_omp_declare_simd (parser
);
22585 cp_ensure_no_oacc_routine (parser
);
22587 /* Issue an error message if type-definitions are forbidden here. */
22588 cp_parser_check_type_definition (parser
);
22589 /* Remember that we are defining one more class. */
22590 ++parser
->num_classes_being_defined
;
22591 /* Inside the class, surrounding template-parameter-lists do not
22593 saved_num_template_parameter_lists
22594 = parser
->num_template_parameter_lists
;
22595 parser
->num_template_parameter_lists
= 0;
22596 /* We are not in a function body. */
22597 saved_in_function_body
= parser
->in_function_body
;
22598 parser
->in_function_body
= false;
22599 /* Or in a loop. */
22600 in_statement
= parser
->in_statement
;
22601 parser
->in_statement
= 0;
22602 /* Or in a switch. */
22603 in_switch_statement_p
= parser
->in_switch_statement_p
;
22604 parser
->in_switch_statement_p
= false;
22605 /* We are not immediately inside an extern "lang" block. */
22606 saved_in_unbraced_linkage_specification_p
22607 = parser
->in_unbraced_linkage_specification_p
;
22608 parser
->in_unbraced_linkage_specification_p
= false;
22610 // Associate constraints with the type.
22612 type
= associate_classtype_constraints (type
);
22614 /* Start the class. */
22615 if (nested_name_specifier_p
)
22617 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
22618 old_scope
= push_inner_scope (scope
);
22620 type
= begin_class_definition (type
);
22622 if (type
== error_mark_node
)
22623 /* If the type is erroneous, skip the entire body of the class. */
22624 cp_parser_skip_to_closing_brace (parser
);
22626 /* Parse the member-specification. */
22627 cp_parser_member_specification_opt (parser
);
22629 /* Look for the trailing `}'. */
22630 closing_brace
= braces
.require_close (parser
);
22631 /* Look for trailing attributes to apply to this class. */
22632 if (cp_parser_allow_gnu_extensions_p (parser
))
22633 attributes
= cp_parser_gnu_attributes_opt (parser
);
22634 if (type
!= error_mark_node
)
22635 type
= finish_struct (type
, attributes
);
22636 if (nested_name_specifier_p
)
22637 pop_inner_scope (old_scope
, scope
);
22639 /* We've finished a type definition. Check for the common syntax
22640 error of forgetting a semicolon after the definition. We need to
22641 be careful, as we can't just check for not-a-semicolon and be done
22642 with it; the user might have typed:
22644 class X { } c = ...;
22645 class X { } *p = ...;
22647 and so forth. Instead, enumerate all the possible tokens that
22648 might follow this production; if we don't see one of them, then
22649 complain and silently insert the semicolon. */
22651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22652 bool want_semicolon
= true;
22654 if (cp_next_tokens_can_be_std_attribute_p (parser
))
22655 /* Don't try to parse c++11 attributes here. As per the
22656 grammar, that should be a task for
22657 cp_parser_decl_specifier_seq. */
22658 want_semicolon
= false;
22660 switch (token
->type
)
22663 case CPP_SEMICOLON
:
22666 case CPP_OPEN_PAREN
:
22667 case CPP_CLOSE_PAREN
:
22669 want_semicolon
= false;
22672 /* While it's legal for type qualifiers and storage class
22673 specifiers to follow type definitions in the grammar, only
22674 compiler testsuites contain code like that. Assume that if
22675 we see such code, then what we're really seeing is a case
22679 const <type> var = ...;
22684 static <type> func (...) ...
22686 i.e. the qualifier or specifier applies to the next
22687 declaration. To do so, however, we need to look ahead one
22688 more token to see if *that* token is a type specifier.
22690 This code could be improved to handle:
22693 static const <type> var = ...; */
22695 if (keyword_is_decl_specifier (token
->keyword
))
22697 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22699 /* Handling user-defined types here would be nice, but very
22702 = (lookahead
->type
== CPP_KEYWORD
22703 && keyword_begins_type_specifier (lookahead
->keyword
));
22710 /* If we don't have a type, then something is very wrong and we
22711 shouldn't try to do anything clever. Likewise for not seeing the
22713 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
22715 /* Locate the closing brace. */
22716 cp_token_position prev
22717 = cp_lexer_previous_token_position (parser
->lexer
);
22718 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
22719 location_t loc
= prev_token
->location
;
22721 /* We want to suggest insertion of a ';' immediately *after* the
22722 closing brace, so, if we can, offset the location by 1 column. */
22723 location_t next_loc
= loc
;
22724 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
22725 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
22727 rich_location
richloc (line_table
, next_loc
);
22729 /* If we successfully offset the location, suggest the fix-it. */
22730 if (next_loc
!= loc
)
22731 richloc
.add_fixit_insert_before (next_loc
, ";");
22733 if (CLASSTYPE_DECLARED_CLASS (type
))
22734 error_at (&richloc
,
22735 "expected %<;%> after class definition");
22736 else if (TREE_CODE (type
) == RECORD_TYPE
)
22737 error_at (&richloc
,
22738 "expected %<;%> after struct definition");
22739 else if (TREE_CODE (type
) == UNION_TYPE
)
22740 error_at (&richloc
,
22741 "expected %<;%> after union definition");
22743 gcc_unreachable ();
22745 /* Unget one token and smash it to look as though we encountered
22746 a semicolon in the input stream. */
22747 cp_lexer_set_token_position (parser
->lexer
, prev
);
22748 token
= cp_lexer_peek_token (parser
->lexer
);
22749 token
->type
= CPP_SEMICOLON
;
22750 token
->keyword
= RID_MAX
;
22754 /* If this class is not itself within the scope of another class,
22755 then we need to parse the bodies of all of the queued function
22756 definitions. Note that the queued functions defined in a class
22757 are not always processed immediately following the
22758 class-specifier for that class. Consider:
22761 struct B { void f() { sizeof (A); } };
22764 If `f' were processed before the processing of `A' were
22765 completed, there would be no way to compute the size of `A'.
22766 Note that the nesting we are interested in here is lexical --
22767 not the semantic nesting given by TYPE_CONTEXT. In particular,
22770 struct A { struct B; };
22771 struct A::B { void f() { } };
22773 there is no need to delay the parsing of `A::B::f'. */
22774 if (--parser
->num_classes_being_defined
== 0)
22777 tree class_type
= NULL_TREE
;
22778 tree pushed_scope
= NULL_TREE
;
22780 cp_default_arg_entry
*e
;
22781 tree save_ccp
, save_ccr
;
22783 if (any_erroneous_template_args_p (type
))
22785 /* Skip default arguments, NSDMIs, etc, in order to improve
22786 error recovery (c++/71169, c++/71832). */
22787 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22788 vec_safe_truncate (unparsed_nsdmis
, 0);
22789 vec_safe_truncate (unparsed_classes
, 0);
22790 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22793 /* In a first pass, parse default arguments to the functions.
22794 Then, in a second pass, parse the bodies of the functions.
22795 This two-phased approach handles cases like:
22803 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
22806 /* If there are default arguments that have not yet been processed,
22807 take care of them now. */
22808 if (class_type
!= e
->class_type
)
22811 pop_scope (pushed_scope
);
22812 class_type
= e
->class_type
;
22813 pushed_scope
= push_scope (class_type
);
22815 /* Make sure that any template parameters are in scope. */
22816 maybe_begin_member_template_processing (decl
);
22817 /* Parse the default argument expressions. */
22818 cp_parser_late_parsing_default_args (parser
, decl
);
22819 /* Remove any template parameters from the symbol table. */
22820 maybe_end_member_template_processing ();
22822 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
22823 /* Now parse any NSDMIs. */
22824 save_ccp
= current_class_ptr
;
22825 save_ccr
= current_class_ref
;
22826 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
22828 if (class_type
!= DECL_CONTEXT (decl
))
22831 pop_scope (pushed_scope
);
22832 class_type
= DECL_CONTEXT (decl
);
22833 pushed_scope
= push_scope (class_type
);
22835 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
22836 cp_parser_late_parsing_nsdmi (parser
, decl
);
22838 vec_safe_truncate (unparsed_nsdmis
, 0);
22839 current_class_ptr
= save_ccp
;
22840 current_class_ref
= save_ccr
;
22842 pop_scope (pushed_scope
);
22844 /* Now do some post-NSDMI bookkeeping. */
22845 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
22846 after_nsdmi_defaulted_late_checks (class_type
);
22847 vec_safe_truncate (unparsed_classes
, 0);
22848 after_nsdmi_defaulted_late_checks (type
);
22850 /* Now parse the body of the functions. */
22853 /* OpenMP UDRs need to be parsed before all other functions. */
22854 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22855 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
22856 cp_parser_late_parsing_for_member (parser
, decl
);
22857 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22858 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
22859 cp_parser_late_parsing_for_member (parser
, decl
);
22862 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
22863 cp_parser_late_parsing_for_member (parser
, decl
);
22864 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
22867 vec_safe_push (unparsed_classes
, type
);
22869 /* Put back any saved access checks. */
22870 pop_deferring_access_checks ();
22872 /* Restore saved state. */
22873 parser
->in_switch_statement_p
= in_switch_statement_p
;
22874 parser
->in_statement
= in_statement
;
22875 parser
->in_function_body
= saved_in_function_body
;
22876 parser
->num_template_parameter_lists
22877 = saved_num_template_parameter_lists
;
22878 parser
->in_unbraced_linkage_specification_p
22879 = saved_in_unbraced_linkage_specification_p
;
22885 cp_parser_class_specifier (cp_parser
* parser
)
22888 timevar_push (TV_PARSE_STRUCT
);
22889 ret
= cp_parser_class_specifier_1 (parser
);
22890 timevar_pop (TV_PARSE_STRUCT
);
22894 /* Parse a class-head.
22897 class-key identifier [opt] base-clause [opt]
22898 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22899 class-key nested-name-specifier [opt] template-id
22902 class-virt-specifier:
22906 class-key attributes identifier [opt] base-clause [opt]
22907 class-key attributes nested-name-specifier identifier base-clause [opt]
22908 class-key attributes nested-name-specifier [opt] template-id
22911 Upon return BASES is initialized to the list of base classes (or
22912 NULL, if there are none) in the same form returned by
22913 cp_parser_base_clause.
22915 Returns the TYPE of the indicated class. Sets
22916 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22917 involving a nested-name-specifier was used, and FALSE otherwise.
22919 Returns error_mark_node if this is not a class-head.
22921 Returns NULL_TREE if the class-head is syntactically valid, but
22922 semantically invalid in a way that means we should skip the entire
22923 body of the class. */
22926 cp_parser_class_head (cp_parser
* parser
,
22927 bool* nested_name_specifier_p
)
22929 tree nested_name_specifier
;
22930 enum tag_types class_key
;
22931 tree id
= NULL_TREE
;
22932 tree type
= NULL_TREE
;
22935 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22936 bool template_id_p
= false;
22937 bool qualified_p
= false;
22938 bool invalid_nested_name_p
= false;
22939 bool invalid_explicit_specialization_p
= false;
22940 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22941 tree pushed_scope
= NULL_TREE
;
22942 unsigned num_templates
;
22943 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
22944 /* Assume no nested-name-specifier will be present. */
22945 *nested_name_specifier_p
= false;
22946 /* Assume no template parameter lists will be used in defining the
22949 parser
->colon_corrects_to_scope_p
= false;
22951 /* Look for the class-key. */
22952 class_key
= cp_parser_class_key (parser
);
22953 if (class_key
== none_type
)
22954 return error_mark_node
;
22956 location_t class_head_start_location
= input_location
;
22958 /* Parse the attributes. */
22959 attributes
= cp_parser_attributes_opt (parser
);
22961 /* If the next token is `::', that is invalid -- but sometimes
22962 people do try to write:
22966 Handle this gracefully by accepting the extra qualifier, and then
22967 issuing an error about it later if this really is a
22968 class-head. If it turns out just to be an elaborated type
22969 specifier, remain silent. */
22970 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
22971 qualified_p
= true;
22973 push_deferring_access_checks (dk_no_check
);
22975 /* Determine the name of the class. Begin by looking for an
22976 optional nested-name-specifier. */
22977 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
22978 nested_name_specifier
22979 = cp_parser_nested_name_specifier_opt (parser
,
22980 /*typename_keyword_p=*/false,
22981 /*check_dependency_p=*/false,
22983 /*is_declaration=*/false);
22984 /* If there was a nested-name-specifier, then there *must* be an
22987 cp_token
*bad_template_keyword
= NULL
;
22989 if (nested_name_specifier
)
22991 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
22992 /* Although the grammar says `identifier', it really means
22993 `class-name' or `template-name'. You are only allowed to
22994 define a class that has already been declared with this
22997 The proposed resolution for Core Issue 180 says that wherever
22998 you see `class T::X' you should treat `X' as a type-name.
23000 It is OK to define an inaccessible class; for example:
23002 class A { class B; };
23005 We do not know if we will see a class-name, or a
23006 template-name. We look for a class-name first, in case the
23007 class-name is a template-id; if we looked for the
23008 template-name first we would stop after the template-name. */
23009 cp_parser_parse_tentatively (parser
);
23010 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23011 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
23012 type
= cp_parser_class_name (parser
,
23013 /*typename_keyword_p=*/false,
23014 /*template_keyword_p=*/false,
23016 /*check_dependency_p=*/false,
23017 /*class_head_p=*/true,
23018 /*is_declaration=*/false);
23019 /* If that didn't work, ignore the nested-name-specifier. */
23020 if (!cp_parser_parse_definitely (parser
))
23022 invalid_nested_name_p
= true;
23023 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23024 id
= cp_parser_identifier (parser
);
23025 if (id
== error_mark_node
)
23028 /* If we could not find a corresponding TYPE, treat this
23029 declaration like an unqualified declaration. */
23030 if (type
== error_mark_node
)
23031 nested_name_specifier
= NULL_TREE
;
23032 /* Otherwise, count the number of templates used in TYPE and its
23033 containing scopes. */
23035 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
23037 /* Otherwise, the identifier is optional. */
23040 /* We don't know whether what comes next is a template-id,
23041 an identifier, or nothing at all. */
23042 cp_parser_parse_tentatively (parser
);
23043 /* Check for a template-id. */
23044 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23045 id
= cp_parser_template_id (parser
,
23046 /*template_keyword_p=*/false,
23047 /*check_dependency_p=*/true,
23049 /*is_declaration=*/true);
23050 /* If that didn't work, it could still be an identifier. */
23051 if (!cp_parser_parse_definitely (parser
))
23053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
23055 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
23056 id
= cp_parser_identifier (parser
);
23063 template_id_p
= true;
23068 pop_deferring_access_checks ();
23072 cp_parser_check_for_invalid_template_id (parser
, id
,
23074 type_start_token
->location
);
23076 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
23078 /* If it's not a `:' or a `{' then we can't really be looking at a
23079 class-head, since a class-head only appears as part of a
23080 class-specifier. We have to detect this situation before calling
23081 xref_tag, since that has irreversible side-effects. */
23082 if (!cp_parser_next_token_starts_class_definition_p (parser
))
23084 cp_parser_error (parser
, "expected %<{%> or %<:%>");
23085 type
= error_mark_node
;
23089 /* At this point, we're going ahead with the class-specifier, even
23090 if some other problem occurs. */
23091 cp_parser_commit_to_tentative_parse (parser
);
23092 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
23094 cp_parser_error (parser
,
23095 "cannot specify %<override%> for a class");
23096 type
= error_mark_node
;
23099 /* Issue the error about the overly-qualified name now. */
23102 cp_parser_error (parser
,
23103 "global qualification of class name is invalid");
23104 type
= error_mark_node
;
23107 else if (invalid_nested_name_p
)
23109 cp_parser_error (parser
,
23110 "qualified name does not name a class");
23111 type
= error_mark_node
;
23114 else if (nested_name_specifier
)
23118 if (bad_template_keyword
)
23119 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23120 keyword template shall not appear at the top level. */
23121 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
23122 "keyword %<template%> not allowed in class-head-name");
23124 /* Reject typedef-names in class heads. */
23125 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
23127 error_at (type_start_token
->location
,
23128 "invalid class name in declaration of %qD",
23134 /* Figure out in what scope the declaration is being placed. */
23135 scope
= current_scope ();
23136 /* If that scope does not contain the scope in which the
23137 class was originally declared, the program is invalid. */
23138 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
23140 if (at_namespace_scope_p ())
23141 error_at (type_start_token
->location
,
23142 "declaration of %qD in namespace %qD which does not "
23144 type
, scope
, nested_name_specifier
);
23146 error_at (type_start_token
->location
,
23147 "declaration of %qD in %qD which does not enclose %qD",
23148 type
, scope
, nested_name_specifier
);
23154 A declarator-id shall not be qualified except for the
23155 definition of a ... nested class outside of its class
23156 ... [or] the definition or explicit instantiation of a
23157 class member of a namespace outside of its namespace. */
23158 if (scope
== nested_name_specifier
)
23160 permerror (nested_name_specifier_token_start
->location
,
23161 "extra qualification not allowed");
23162 nested_name_specifier
= NULL_TREE
;
23166 /* An explicit-specialization must be preceded by "template <>". If
23167 it is not, try to recover gracefully. */
23168 if (at_namespace_scope_p ()
23169 && parser
->num_template_parameter_lists
== 0
23170 && !processing_template_parmlist
23173 /* Build a location of this form:
23174 struct typename <ARGS>
23175 ^~~~~~~~~~~~~~~~~~~~~~
23176 with caret==start at the start token, and
23177 finishing at the end of the type. */
23178 location_t reported_loc
23179 = make_location (class_head_start_location
,
23180 class_head_start_location
,
23181 get_finish (type_start_token
->location
));
23182 rich_location
richloc (line_table
, reported_loc
);
23183 richloc
.add_fixit_insert_before (class_head_start_location
,
23185 error_at (&richloc
,
23186 "an explicit specialization must be preceded by"
23187 " %<template <>%>");
23188 invalid_explicit_specialization_p
= true;
23189 /* Take the same action that would have been taken by
23190 cp_parser_explicit_specialization. */
23191 ++parser
->num_template_parameter_lists
;
23192 begin_specialization ();
23194 /* There must be no "return" statements between this point and the
23195 end of this function; set "type "to the correct return value and
23196 use "goto done;" to return. */
23197 /* Make sure that the right number of template parameters were
23199 if (!cp_parser_check_template_parameters (parser
, num_templates
,
23201 type_start_token
->location
,
23202 /*declarator=*/NULL
))
23204 /* If something went wrong, there is no point in even trying to
23205 process the class-definition. */
23210 /* Look up the type. */
23213 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
23214 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
23215 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
23217 error_at (type_start_token
->location
,
23218 "function template %qD redeclared as a class template", id
);
23219 type
= error_mark_node
;
23223 type
= TREE_TYPE (id
);
23224 type
= maybe_process_partial_specialization (type
);
23226 /* Check the scope while we still know whether or not we had a
23227 nested-name-specifier. */
23228 if (type
!= error_mark_node
)
23229 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
23231 if (nested_name_specifier
)
23232 pushed_scope
= push_scope (nested_name_specifier
);
23234 else if (nested_name_specifier
)
23240 template <typename T> struct S { struct T };
23241 template <typename T> struct S<T>::T { };
23243 we will get a TYPENAME_TYPE when processing the definition of
23244 `S::T'. We need to resolve it to the actual type before we
23245 try to define it. */
23246 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
23248 class_type
= resolve_typename_type (TREE_TYPE (type
),
23249 /*only_current_p=*/false);
23250 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
23251 type
= TYPE_NAME (class_type
);
23254 cp_parser_error (parser
, "could not resolve typename type");
23255 type
= error_mark_node
;
23259 if (maybe_process_partial_specialization (TREE_TYPE (type
))
23260 == error_mark_node
)
23266 class_type
= current_class_type
;
23267 /* Enter the scope indicated by the nested-name-specifier. */
23268 pushed_scope
= push_scope (nested_name_specifier
);
23269 /* Get the canonical version of this type. */
23270 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
23271 /* Call push_template_decl if it seems like we should be defining a
23272 template either from the template headers or the type we're
23273 defining, so that we diagnose both extra and missing headers. */
23274 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23275 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
23276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
23278 type
= push_template_decl (type
);
23279 if (type
== error_mark_node
)
23286 type
= TREE_TYPE (type
);
23287 *nested_name_specifier_p
= true;
23289 else /* The name is not a nested name. */
23291 /* If the class was unnamed, create a dummy name. */
23293 id
= make_anon_name ();
23294 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
23295 ? ts_within_enclosing_non_class
23297 type
= xref_tag (class_key
, id
, tag_scope
,
23298 parser
->num_template_parameter_lists
);
23301 /* Indicate whether this class was declared as a `class' or as a
23303 if (TREE_CODE (type
) == RECORD_TYPE
)
23304 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
23305 cp_parser_check_class_key (class_key
, type
);
23307 /* If this type was already complete, and we see another definition,
23308 that's an error. */
23309 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
23311 error_at (type_start_token
->location
, "redefinition of %q#T",
23313 inform (location_of (type
), "previous definition of %q#T",
23318 else if (type
== error_mark_node
)
23323 /* Apply attributes now, before any use of the class as a template
23324 argument in its base list. */
23325 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
23326 fixup_attribute_variants (type
);
23329 /* We will have entered the scope containing the class; the names of
23330 base classes should be looked up in that context. For example:
23332 struct A { struct B {}; struct C; };
23333 struct A::C : B {};
23337 /* Get the list of base-classes, if there is one. */
23338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23340 /* PR59482: enter the class scope so that base-specifiers are looked
23344 bases
= cp_parser_base_clause (parser
);
23345 /* PR59482: get out of the previously pushed class scope so that the
23346 subsequent pops pop the right thing. */
23353 /* If we're really defining a class, process the base classes.
23354 If they're invalid, fail. */
23355 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23356 xref_basetypes (type
, bases
);
23359 /* Leave the scope given by the nested-name-specifier. We will
23360 enter the class scope itself while processing the members. */
23362 pop_scope (pushed_scope
);
23364 if (invalid_explicit_specialization_p
)
23366 end_specialization ();
23367 --parser
->num_template_parameter_lists
;
23371 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
23372 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
23373 CLASSTYPE_FINAL (type
) = 1;
23375 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
23379 /* Parse a class-key.
23386 Returns the kind of class-key specified, or none_type to indicate
23389 static enum tag_types
23390 cp_parser_class_key (cp_parser
* parser
)
23393 enum tag_types tag_type
;
23395 /* Look for the class-key. */
23396 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
23400 /* Check to see if the TOKEN is a class-key. */
23401 tag_type
= cp_parser_token_is_class_key (token
);
23403 cp_parser_error (parser
, "expected class-key");
23407 /* Parse a type-parameter-key.
23409 type-parameter-key:
23415 cp_parser_type_parameter_key (cp_parser
* parser
)
23417 /* Look for the type-parameter-key. */
23418 enum tag_types tag_type
= none_type
;
23419 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23420 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
23422 cp_lexer_consume_token (parser
->lexer
);
23423 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
23424 /* typename is not allowed in a template template parameter
23425 by the standard until C++17. */
23426 pedwarn (token
->location
, OPT_Wpedantic
,
23427 "ISO C++ forbids typename key in template template parameter;"
23428 " use -std=c++17 or -std=gnu++17");
23431 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
23436 /* Parse an (optional) member-specification.
23438 member-specification:
23439 member-declaration member-specification [opt]
23440 access-specifier : member-specification [opt] */
23443 cp_parser_member_specification_opt (cp_parser
* parser
)
23450 /* Peek at the next token. */
23451 token
= cp_lexer_peek_token (parser
->lexer
);
23452 /* If it's a `}', or EOF then we've seen all the members. */
23453 if (token
->type
== CPP_CLOSE_BRACE
23454 || token
->type
== CPP_EOF
23455 || token
->type
== CPP_PRAGMA_EOL
)
23458 /* See if this token is a keyword. */
23459 keyword
= token
->keyword
;
23463 case RID_PROTECTED
:
23465 /* Consume the access-specifier. */
23466 cp_lexer_consume_token (parser
->lexer
);
23467 /* Remember which access-specifier is active. */
23468 current_access_specifier
= token
->u
.value
;
23469 /* Look for the `:'. */
23470 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
23474 /* Accept #pragmas at class scope. */
23475 if (token
->type
== CPP_PRAGMA
)
23477 cp_parser_pragma (parser
, pragma_member
, NULL
);
23481 /* Otherwise, the next construction must be a
23482 member-declaration. */
23483 cp_parser_member_declaration (parser
);
23488 /* Parse a member-declaration.
23490 member-declaration:
23491 decl-specifier-seq [opt] member-declarator-list [opt] ;
23492 function-definition ; [opt]
23493 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23495 template-declaration
23498 member-declarator-list:
23500 member-declarator-list , member-declarator
23503 declarator pure-specifier [opt]
23504 declarator constant-initializer [opt]
23505 identifier [opt] : constant-expression
23509 member-declaration:
23510 __extension__ member-declaration
23513 declarator attributes [opt] pure-specifier [opt]
23514 declarator attributes [opt] constant-initializer [opt]
23515 identifier [opt] attributes [opt] : constant-expression
23519 member-declaration:
23520 static_assert-declaration */
23523 cp_parser_member_declaration (cp_parser
* parser
)
23525 cp_decl_specifier_seq decl_specifiers
;
23526 tree prefix_attributes
;
23528 int declares_class_or_enum
;
23530 cp_token
*token
= NULL
;
23531 cp_token
*decl_spec_token_start
= NULL
;
23532 cp_token
*initializer_token_start
= NULL
;
23533 int saved_pedantic
;
23534 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
23536 /* Check for the `__extension__' keyword. */
23537 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
23540 cp_parser_member_declaration (parser
);
23541 /* Restore the old value of the PEDANTIC flag. */
23542 pedantic
= saved_pedantic
;
23547 /* Check for a template-declaration. */
23548 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23550 /* An explicit specialization here is an error condition, and we
23551 expect the specialization handler to detect and report this. */
23552 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
23553 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
23554 cp_parser_explicit_specialization (parser
);
23556 cp_parser_template_declaration (parser
, /*member_p=*/true);
23560 /* Check for a template introduction. */
23561 else if (cp_parser_template_declaration_after_export (parser
, true))
23564 /* Check for a using-declaration. */
23565 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23567 if (cxx_dialect
< cxx11
)
23569 /* Parse the using-declaration. */
23570 cp_parser_using_declaration (parser
,
23571 /*access_declaration_p=*/false);
23577 bool alias_decl_expected
;
23578 cp_parser_parse_tentatively (parser
);
23579 decl
= cp_parser_alias_declaration (parser
);
23580 /* Note that if we actually see the '=' token after the
23581 identifier, cp_parser_alias_declaration commits the
23582 tentative parse. In that case, we really expect an
23583 alias-declaration. Otherwise, we expect a using
23585 alias_decl_expected
=
23586 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
23587 cp_parser_parse_definitely (parser
);
23589 if (alias_decl_expected
)
23590 finish_member_declaration (decl
);
23592 cp_parser_using_declaration (parser
,
23593 /*access_declaration_p=*/false);
23598 /* Check for @defs. */
23599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
23602 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
23603 ivar
= ivar_chains
;
23607 ivar
= TREE_CHAIN (member
);
23608 TREE_CHAIN (member
) = NULL_TREE
;
23609 finish_member_declaration (member
);
23614 /* If the next token is `static_assert' we have a static assertion. */
23615 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
23617 cp_parser_static_assert (parser
, /*member_p=*/true);
23621 parser
->colon_corrects_to_scope_p
= false;
23623 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
23626 /* Parse the decl-specifier-seq. */
23627 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23628 cp_parser_decl_specifier_seq (parser
,
23629 CP_PARSER_FLAGS_OPTIONAL
,
23631 &declares_class_or_enum
);
23632 /* Check for an invalid type-name. */
23633 if (!decl_specifiers
.any_type_specifiers_p
23634 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23636 /* If there is no declarator, then the decl-specifier-seq should
23638 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23640 /* If there was no decl-specifier-seq, and the next token is a
23641 `;', then we have something like:
23647 Each member-declaration shall declare at least one member
23648 name of the class. */
23649 if (!decl_specifiers
.any_specifiers_p
)
23651 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23652 if (!in_system_header_at (token
->location
))
23654 gcc_rich_location
richloc (token
->location
);
23655 richloc
.add_fixit_remove ();
23656 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
23663 /* See if this declaration is a friend. */
23664 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23665 /* If there were decl-specifiers, check to see if there was
23666 a class-declaration. */
23667 type
= check_tag_decl (&decl_specifiers
,
23668 /*explicit_type_instantiation_p=*/false);
23669 /* Nested classes have already been added to the class, but
23670 a `friend' needs to be explicitly registered. */
23673 /* If the `friend' keyword was present, the friend must
23674 be introduced with a class-key. */
23675 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
23676 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
23677 "in C++03 a class-key must be used "
23678 "when declaring a friend");
23681 template <typename T> struct A {
23682 friend struct A<T>::B;
23685 A<T>::B will be represented by a TYPENAME_TYPE, and
23686 therefore not recognized by check_tag_decl. */
23689 type
= decl_specifiers
.type
;
23690 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
23691 type
= TREE_TYPE (type
);
23693 if (!type
|| !TYPE_P (type
))
23694 error_at (decl_spec_token_start
->location
,
23695 "friend declaration does not name a class or "
23698 make_friend_class (current_class_type
, type
,
23699 /*complain=*/true);
23701 /* If there is no TYPE, an error message will already have
23703 else if (!type
|| type
== error_mark_node
)
23705 /* An anonymous aggregate has to be handled specially; such
23706 a declaration really declares a data member (with a
23707 particular type), as opposed to a nested class. */
23708 else if (ANON_AGGR_TYPE_P (type
))
23711 if (decl_specifiers
.storage_class
!= sc_none
)
23712 error_at (decl_spec_token_start
->location
,
23713 "a storage class on an anonymous aggregate "
23714 "in class scope is not allowed");
23716 /* Remove constructors and such from TYPE, now that we
23717 know it is an anonymous aggregate. */
23718 fixup_anonymous_aggr (type
);
23719 /* And make the corresponding data member. */
23720 decl
= build_decl (decl_spec_token_start
->location
,
23721 FIELD_DECL
, NULL_TREE
, type
);
23722 /* Add it to the class. */
23723 finish_member_declaration (decl
);
23726 cp_parser_check_access_in_redeclaration
23728 decl_spec_token_start
->location
);
23733 bool assume_semicolon
= false;
23735 /* Clear attributes from the decl_specifiers but keep them
23736 around as prefix attributes that apply them to the entity
23738 prefix_attributes
= decl_specifiers
.attributes
;
23739 decl_specifiers
.attributes
= NULL_TREE
;
23741 /* See if these declarations will be friends. */
23742 friend_p
= cp_parser_friend_p (&decl_specifiers
);
23744 /* Keep going until we hit the `;' at the end of the
23746 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
23748 tree attributes
= NULL_TREE
;
23749 tree first_attribute
;
23751 bool named_bitfld
= false;
23753 /* Peek at the next token. */
23754 token
= cp_lexer_peek_token (parser
->lexer
);
23756 /* The following code wants to know early if it is a bit-field
23757 or some other declaration. Attributes can appear before
23758 the `:' token. Skip over them without consuming any tokens
23759 to peek if they are followed by `:'. */
23760 if (cp_next_tokens_can_be_attribute_p (parser
)
23761 || (token
->type
== CPP_NAME
23762 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
23763 && (named_bitfld
= true)))
23766 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
23767 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23770 /* Check for a bitfield declaration. */
23771 if (token
->type
== CPP_COLON
23772 || (token
->type
== CPP_NAME
23773 && token
== cp_lexer_peek_token (parser
->lexer
)
23774 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
23775 && (named_bitfld
= true)))
23779 tree late_attributes
= NULL_TREE
;
23782 identifier
= cp_parser_identifier (parser
);
23784 identifier
= NULL_TREE
;
23786 /* Look for attributes that apply to the bitfield. */
23787 attributes
= cp_parser_attributes_opt (parser
);
23789 /* Consume the `:' token. */
23790 cp_lexer_consume_token (parser
->lexer
);
23792 /* Get the width of the bitfield. */
23793 width
= cp_parser_constant_expression (parser
, false, NULL
,
23794 cxx_dialect
>= cxx11
);
23796 /* In C++2A and as extension for C++11 and above we allow
23797 default member initializers for bit-fields. */
23798 initializer
= NULL_TREE
;
23799 if (cxx_dialect
>= cxx11
23800 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
23801 || cp_lexer_next_token_is (parser
->lexer
,
23805 = cp_lexer_peek_token (parser
->lexer
)->location
;
23806 if (cxx_dialect
< cxx2a
23807 && !in_system_header_at (loc
)
23808 && identifier
!= NULL_TREE
)
23810 "default member initializers for bit-fields "
23811 "only available with -std=c++2a or "
23814 initializer
= cp_parser_save_nsdmi (parser
);
23815 if (identifier
== NULL_TREE
)
23817 error_at (loc
, "default member initializer for "
23818 "unnamed bit-field");
23819 initializer
= NULL_TREE
;
23824 /* Look for attributes that apply to the bitfield after
23825 the `:' token and width. This is where GCC used to
23826 parse attributes in the past, pedwarn if there is
23827 a std attribute. */
23828 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23829 pedwarn (input_location
, OPT_Wpedantic
,
23830 "ISO C++ allows bit-field attributes only "
23831 "before the %<:%> token");
23833 late_attributes
= cp_parser_attributes_opt (parser
);
23836 attributes
= attr_chainon (attributes
, late_attributes
);
23838 /* Remember which attributes are prefix attributes and
23840 first_attribute
= attributes
;
23841 /* Combine the attributes. */
23842 attributes
= attr_chainon (prefix_attributes
, attributes
);
23844 /* Create the bitfield declaration. */
23845 decl
= grokbitfield (identifier
23846 ? make_id_declarator (NULL_TREE
,
23851 width
, initializer
,
23856 cp_declarator
*declarator
;
23857 tree asm_specification
;
23858 int ctor_dtor_or_conv_p
;
23860 /* Parse the declarator. */
23862 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
23863 &ctor_dtor_or_conv_p
,
23864 /*parenthesized_p=*/NULL
,
23868 /* If something went wrong parsing the declarator, make sure
23869 that we at least consume some tokens. */
23870 if (declarator
== cp_error_declarator
)
23872 /* Skip to the end of the statement. */
23873 cp_parser_skip_to_end_of_statement (parser
);
23874 /* If the next token is not a semicolon, that is
23875 probably because we just skipped over the body of
23876 a function. So, we consume a semicolon if
23877 present, but do not issue an error message if it
23879 if (cp_lexer_next_token_is (parser
->lexer
,
23881 cp_lexer_consume_token (parser
->lexer
);
23885 if (declares_class_or_enum
& 2)
23886 cp_parser_check_for_definition_in_return_type
23887 (declarator
, decl_specifiers
.type
,
23888 decl_specifiers
.locations
[ds_type_spec
]);
23890 /* Look for an asm-specification. */
23891 asm_specification
= cp_parser_asm_specification_opt (parser
);
23892 /* Look for attributes that apply to the declaration. */
23893 attributes
= cp_parser_attributes_opt (parser
);
23894 /* Remember which attributes are prefix attributes and
23896 first_attribute
= attributes
;
23897 /* Combine the attributes. */
23898 attributes
= attr_chainon (prefix_attributes
, attributes
);
23900 /* If it's an `=', then we have a constant-initializer or a
23901 pure-specifier. It is not correct to parse the
23902 initializer before registering the member declaration
23903 since the member declaration should be in scope while
23904 its initializer is processed. However, the rest of the
23905 front end does not yet provide an interface that allows
23906 us to handle this correctly. */
23907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23911 A pure-specifier shall be used only in the declaration of
23912 a virtual function.
23914 A member-declarator can contain a constant-initializer
23915 only if it declares a static member of integral or
23918 Therefore, if the DECLARATOR is for a function, we look
23919 for a pure-specifier; otherwise, we look for a
23920 constant-initializer. When we call `grokfield', it will
23921 perform more stringent semantics checks. */
23922 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
23923 if (function_declarator_p (declarator
)
23924 || (decl_specifiers
.type
23925 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
23926 && declarator
->kind
== cdk_id
23927 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
23928 == FUNCTION_TYPE
)))
23929 initializer
= cp_parser_pure_specifier (parser
);
23930 else if (decl_specifiers
.storage_class
!= sc_static
)
23931 initializer
= cp_parser_save_nsdmi (parser
);
23932 else if (cxx_dialect
>= cxx11
)
23935 /* Don't require a constant rvalue in C++11, since we
23936 might want a reference constant. We'll enforce
23937 constancy later. */
23938 cp_lexer_consume_token (parser
->lexer
);
23939 /* Parse the initializer. */
23940 initializer
= cp_parser_initializer_clause (parser
,
23944 /* Parse the initializer. */
23945 initializer
= cp_parser_constant_initializer (parser
);
23947 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
23948 && !function_declarator_p (declarator
))
23951 if (decl_specifiers
.storage_class
!= sc_static
)
23952 initializer
= cp_parser_save_nsdmi (parser
);
23954 initializer
= cp_parser_initializer (parser
, &x
, &x
);
23956 /* Otherwise, there is no initializer. */
23958 initializer
= NULL_TREE
;
23960 /* See if we are probably looking at a function
23961 definition. We are certainly not looking at a
23962 member-declarator. Calling `grokfield' has
23963 side-effects, so we must not do it unless we are sure
23964 that we are looking at a member-declarator. */
23965 if (cp_parser_token_starts_function_definition_p
23966 (cp_lexer_peek_token (parser
->lexer
)))
23968 /* The grammar does not allow a pure-specifier to be
23969 used when a member function is defined. (It is
23970 possible that this fact is an oversight in the
23971 standard, since a pure function may be defined
23972 outside of the class-specifier. */
23973 if (initializer
&& initializer_token_start
)
23974 error_at (initializer_token_start
->location
,
23975 "pure-specifier on function-definition");
23976 decl
= cp_parser_save_member_function_body (parser
,
23980 if (parser
->fully_implicit_function_template_p
)
23981 decl
= finish_fully_implicit_template (parser
, decl
);
23982 /* If the member was not a friend, declare it here. */
23984 finish_member_declaration (decl
);
23985 /* Peek at the next token. */
23986 token
= cp_lexer_peek_token (parser
->lexer
);
23987 /* If the next token is a semicolon, consume it. */
23988 if (token
->type
== CPP_SEMICOLON
)
23990 location_t semicolon_loc
23991 = cp_lexer_consume_token (parser
->lexer
)->location
;
23992 gcc_rich_location
richloc (semicolon_loc
);
23993 richloc
.add_fixit_remove ();
23994 warning_at (&richloc
, OPT_Wextra_semi
,
23995 "extra %<;%> after in-class "
23996 "function definition");
24001 if (declarator
->kind
== cdk_function
)
24002 declarator
->id_loc
= token
->location
;
24003 /* Create the declaration. */
24004 decl
= grokfield (declarator
, &decl_specifiers
,
24005 initializer
, /*init_const_expr_p=*/true,
24006 asm_specification
, attributes
);
24007 if (parser
->fully_implicit_function_template_p
)
24010 finish_fully_implicit_template (parser
, 0);
24012 decl
= finish_fully_implicit_template (parser
, decl
);
24016 cp_finalize_omp_declare_simd (parser
, decl
);
24017 cp_finalize_oacc_routine (parser
, decl
, false);
24019 /* Reset PREFIX_ATTRIBUTES. */
24020 if (attributes
!= error_mark_node
)
24022 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
24023 attributes
= TREE_CHAIN (attributes
);
24025 TREE_CHAIN (attributes
) = NULL_TREE
;
24028 /* If there is any qualification still in effect, clear it
24029 now; we will be starting fresh with the next declarator. */
24030 parser
->scope
= NULL_TREE
;
24031 parser
->qualifying_scope
= NULL_TREE
;
24032 parser
->object_scope
= NULL_TREE
;
24033 /* If it's a `,', then there are more declarators. */
24034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24036 cp_lexer_consume_token (parser
->lexer
);
24037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24039 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24040 gcc_rich_location
richloc (token
->location
);
24041 richloc
.add_fixit_remove ();
24042 error_at (&richloc
, "stray %<,%> at end of "
24043 "member declaration");
24046 /* If the next token isn't a `;', then we have a parse error. */
24047 else if (cp_lexer_next_token_is_not (parser
->lexer
,
24050 /* The next token might be a ways away from where the
24051 actual semicolon is missing. Find the previous token
24052 and use that for our error position. */
24053 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
24054 gcc_rich_location
richloc (token
->location
);
24055 richloc
.add_fixit_insert_after (";");
24056 error_at (&richloc
, "expected %<;%> at end of "
24057 "member declaration");
24059 /* Assume that the user meant to provide a semicolon. If
24060 we were to cp_parser_skip_to_end_of_statement, we might
24061 skip to a semicolon inside a member function definition
24062 and issue nonsensical error messages. */
24063 assume_semicolon
= true;
24068 /* Add DECL to the list of members. */
24070 /* Explicitly include, eg, NSDMIs, for better error
24071 recovery (c++/58650). */
24072 || !DECL_DECLARES_FUNCTION_P (decl
))
24073 finish_member_declaration (decl
);
24075 if (TREE_CODE (decl
) == FUNCTION_DECL
)
24076 cp_parser_save_default_args (parser
, decl
);
24077 else if (TREE_CODE (decl
) == FIELD_DECL
24078 && DECL_INITIAL (decl
))
24079 /* Add DECL to the queue of NSDMI to be parsed later. */
24080 vec_safe_push (unparsed_nsdmis
, decl
);
24083 if (assume_semicolon
)
24088 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24090 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24093 /* Parse a pure-specifier.
24098 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24099 Otherwise, ERROR_MARK_NODE is returned. */
24102 cp_parser_pure_specifier (cp_parser
* parser
)
24106 /* Look for the `=' token. */
24107 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24108 return error_mark_node
;
24109 /* Look for the `0' token. */
24110 token
= cp_lexer_peek_token (parser
->lexer
);
24112 if (token
->type
== CPP_EOF
24113 || token
->type
== CPP_PRAGMA_EOL
)
24114 return error_mark_node
;
24116 cp_lexer_consume_token (parser
->lexer
);
24118 /* Accept = default or = delete in c++0x mode. */
24119 if (token
->keyword
== RID_DEFAULT
24120 || token
->keyword
== RID_DELETE
)
24122 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
24123 return token
->u
.value
;
24126 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24127 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
24129 cp_parser_error (parser
,
24130 "invalid pure specifier (only %<= 0%> is allowed)");
24131 cp_parser_skip_to_end_of_statement (parser
);
24132 return error_mark_node
;
24134 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24136 error_at (token
->location
, "templates may not be %<virtual%>");
24137 return error_mark_node
;
24140 return integer_zero_node
;
24143 /* Parse a constant-initializer.
24145 constant-initializer:
24146 = constant-expression
24148 Returns a representation of the constant-expression. */
24151 cp_parser_constant_initializer (cp_parser
* parser
)
24153 /* Look for the `=' token. */
24154 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
24155 return error_mark_node
;
24157 /* It is invalid to write:
24159 struct S { static const int i = { 7 }; };
24162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24164 cp_parser_error (parser
,
24165 "a brace-enclosed initializer is not allowed here");
24166 /* Consume the opening brace. */
24167 matching_braces braces
;
24168 braces
.consume_open (parser
);
24169 /* Skip the initializer. */
24170 cp_parser_skip_to_closing_brace (parser
);
24171 /* Look for the trailing `}'. */
24172 braces
.require_close (parser
);
24174 return error_mark_node
;
24177 return cp_parser_constant_expression (parser
);
24180 /* Derived classes [gram.class.derived] */
24182 /* Parse a base-clause.
24185 : base-specifier-list
24187 base-specifier-list:
24188 base-specifier ... [opt]
24189 base-specifier-list , base-specifier ... [opt]
24191 Returns a TREE_LIST representing the base-classes, in the order in
24192 which they were declared. The representation of each node is as
24193 described by cp_parser_base_specifier.
24195 In the case that no bases are specified, this function will return
24196 NULL_TREE, not ERROR_MARK_NODE. */
24199 cp_parser_base_clause (cp_parser
* parser
)
24201 tree bases
= NULL_TREE
;
24203 /* Look for the `:' that begins the list. */
24204 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24206 /* Scan the base-specifier-list. */
24211 bool pack_expansion_p
= false;
24213 /* Look for the base-specifier. */
24214 base
= cp_parser_base_specifier (parser
);
24215 /* Look for the (optional) ellipsis. */
24216 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24218 /* Consume the `...'. */
24219 cp_lexer_consume_token (parser
->lexer
);
24221 pack_expansion_p
= true;
24224 /* Add BASE to the front of the list. */
24225 if (base
&& base
!= error_mark_node
)
24227 if (pack_expansion_p
)
24228 /* Make this a pack expansion type. */
24229 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
24231 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
24233 TREE_CHAIN (base
) = bases
;
24237 /* Peek at the next token. */
24238 token
= cp_lexer_peek_token (parser
->lexer
);
24239 /* If it's not a comma, then the list is complete. */
24240 if (token
->type
!= CPP_COMMA
)
24242 /* Consume the `,'. */
24243 cp_lexer_consume_token (parser
->lexer
);
24246 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24247 base class had a qualified name. However, the next name that
24248 appears is certainly not qualified. */
24249 parser
->scope
= NULL_TREE
;
24250 parser
->qualifying_scope
= NULL_TREE
;
24251 parser
->object_scope
= NULL_TREE
;
24253 return nreverse (bases
);
24256 /* Parse a base-specifier.
24259 :: [opt] nested-name-specifier [opt] class-name
24260 virtual access-specifier [opt] :: [opt] nested-name-specifier
24262 access-specifier virtual [opt] :: [opt] nested-name-specifier
24265 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24266 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24267 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24268 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24271 cp_parser_base_specifier (cp_parser
* parser
)
24275 bool virtual_p
= false;
24276 bool duplicate_virtual_error_issued_p
= false;
24277 bool duplicate_access_error_issued_p
= false;
24278 bool class_scope_p
, template_p
;
24279 tree access
= access_default_node
;
24282 /* Process the optional `virtual' and `access-specifier'. */
24285 /* Peek at the next token. */
24286 token
= cp_lexer_peek_token (parser
->lexer
);
24287 /* Process `virtual'. */
24288 switch (token
->keyword
)
24291 /* If `virtual' appears more than once, issue an error. */
24292 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
24294 cp_parser_error (parser
,
24295 "%<virtual%> specified more than once in base-specifier");
24296 duplicate_virtual_error_issued_p
= true;
24301 /* Consume the `virtual' token. */
24302 cp_lexer_consume_token (parser
->lexer
);
24307 case RID_PROTECTED
:
24309 /* If more than one access specifier appears, issue an
24311 if (access
!= access_default_node
24312 && !duplicate_access_error_issued_p
)
24314 cp_parser_error (parser
,
24315 "more than one access specifier in base-specifier");
24316 duplicate_access_error_issued_p
= true;
24319 access
= ridpointers
[(int) token
->keyword
];
24321 /* Consume the access-specifier. */
24322 cp_lexer_consume_token (parser
->lexer
);
24331 /* It is not uncommon to see programs mechanically, erroneously, use
24332 the 'typename' keyword to denote (dependent) qualified types
24333 as base classes. */
24334 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24336 token
= cp_lexer_peek_token (parser
->lexer
);
24337 if (!processing_template_decl
)
24338 error_at (token
->location
,
24339 "keyword %<typename%> not allowed outside of templates");
24341 error_at (token
->location
,
24342 "keyword %<typename%> not allowed in this context "
24343 "(the base class is implicitly a type)");
24344 cp_lexer_consume_token (parser
->lexer
);
24347 /* Look for the optional `::' operator. */
24348 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
24349 /* Look for the nested-name-specifier. The simplest way to
24354 The keyword `typename' is not permitted in a base-specifier or
24355 mem-initializer; in these contexts a qualified name that
24356 depends on a template-parameter is implicitly assumed to be a
24359 is to pretend that we have seen the `typename' keyword at this
24361 cp_parser_nested_name_specifier_opt (parser
,
24362 /*typename_keyword_p=*/true,
24363 /*check_dependency_p=*/true,
24365 /*is_declaration=*/true);
24366 /* If the base class is given by a qualified name, assume that names
24367 we see are type names or templates, as appropriate. */
24368 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
24369 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
24372 && cp_lexer_next_token_is_decltype (parser
->lexer
))
24373 /* DR 950 allows decltype as a base-specifier. */
24374 type
= cp_parser_decltype (parser
);
24377 /* Otherwise, look for the class-name. */
24378 type
= cp_parser_class_name (parser
,
24382 /*check_dependency_p=*/true,
24383 /*class_head_p=*/false,
24384 /*is_declaration=*/true);
24385 type
= TREE_TYPE (type
);
24388 if (type
== error_mark_node
)
24389 return error_mark_node
;
24391 return finish_base_specifier (type
, access
, virtual_p
);
24394 /* Exception handling [gram.exception] */
24396 /* Parse an (optional) noexcept-specification.
24398 noexcept-specification:
24399 noexcept ( constant-expression ) [opt]
24401 If no noexcept-specification is present, returns NULL_TREE.
24402 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24403 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24404 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24405 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24406 in which case a boolean condition is returned instead. */
24409 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
24410 bool require_constexpr
,
24411 bool* consumed_expr
,
24415 const char *saved_message
;
24417 /* Peek at the next token. */
24418 token
= cp_lexer_peek_token (parser
->lexer
);
24420 /* Is it a noexcept-specification? */
24421 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
24424 cp_lexer_consume_token (parser
->lexer
);
24426 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
24428 matching_parens parens
;
24429 parens
.consume_open (parser
);
24431 if (require_constexpr
)
24433 /* Types may not be defined in an exception-specification. */
24434 saved_message
= parser
->type_definition_forbidden_message
;
24435 parser
->type_definition_forbidden_message
24436 = G_("types may not be defined in an exception-specification");
24438 expr
= cp_parser_constant_expression (parser
);
24440 /* Restore the saved message. */
24441 parser
->type_definition_forbidden_message
= saved_message
;
24445 expr
= cp_parser_expression (parser
);
24446 *consumed_expr
= true;
24449 parens
.require_close (parser
);
24453 expr
= boolean_true_node
;
24454 if (!require_constexpr
)
24455 *consumed_expr
= false;
24458 /* We cannot build a noexcept-spec right away because this will check
24459 that expr is a constexpr. */
24461 return build_noexcept_spec (expr
, tf_warning_or_error
);
24469 /* Parse an (optional) exception-specification.
24471 exception-specification:
24472 throw ( type-id-list [opt] )
24474 Returns a TREE_LIST representing the exception-specification. The
24475 TREE_VALUE of each node is a type. */
24478 cp_parser_exception_specification_opt (cp_parser
* parser
)
24482 const char *saved_message
;
24484 /* Peek at the next token. */
24485 token
= cp_lexer_peek_token (parser
->lexer
);
24487 /* Is it a noexcept-specification? */
24488 type_id_list
= cp_parser_noexcept_specification_opt (parser
, true, NULL
,
24490 if (type_id_list
!= NULL_TREE
)
24491 return type_id_list
;
24493 /* If it's not `throw', then there's no exception-specification. */
24494 if (!cp_parser_is_keyword (token
, RID_THROW
))
24497 location_t loc
= token
->location
;
24499 /* Consume the `throw'. */
24500 cp_lexer_consume_token (parser
->lexer
);
24502 /* Look for the `('. */
24503 matching_parens parens
;
24504 parens
.require_open (parser
);
24506 /* Peek at the next token. */
24507 token
= cp_lexer_peek_token (parser
->lexer
);
24508 /* If it's not a `)', then there is a type-id-list. */
24509 if (token
->type
!= CPP_CLOSE_PAREN
)
24511 /* Types may not be defined in an exception-specification. */
24512 saved_message
= parser
->type_definition_forbidden_message
;
24513 parser
->type_definition_forbidden_message
24514 = G_("types may not be defined in an exception-specification");
24515 /* Parse the type-id-list. */
24516 type_id_list
= cp_parser_type_id_list (parser
);
24517 /* Restore the saved message. */
24518 parser
->type_definition_forbidden_message
= saved_message
;
24520 if (cxx_dialect
>= cxx17
)
24522 error_at (loc
, "ISO C++17 does not allow dynamic exception "
24524 type_id_list
= NULL_TREE
;
24526 else if (cxx_dialect
>= cxx11
&& !in_system_header_at (loc
))
24527 warning_at (loc
, OPT_Wdeprecated
,
24528 "dynamic exception specifications are deprecated in "
24531 /* In C++17, throw() is equivalent to noexcept (true). throw()
24532 is deprecated in C++11 and above as well, but is still widely used,
24533 so don't warn about it yet. */
24534 else if (cxx_dialect
>= cxx17
)
24535 type_id_list
= noexcept_true_spec
;
24537 type_id_list
= empty_except_spec
;
24539 /* Look for the `)'. */
24540 parens
.require_close (parser
);
24542 return type_id_list
;
24545 /* Parse an (optional) type-id-list.
24549 type-id-list , type-id ... [opt]
24551 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24552 in the order that the types were presented. */
24555 cp_parser_type_id_list (cp_parser
* parser
)
24557 tree types
= NULL_TREE
;
24564 token
= cp_lexer_peek_token (parser
->lexer
);
24566 /* Get the next type-id. */
24567 type
= cp_parser_type_id (parser
);
24568 /* Check for invalid 'auto'. */
24569 if (flag_concepts
&& type_uses_auto (type
))
24571 error_at (token
->location
,
24572 "invalid use of %<auto%> in exception-specification");
24573 type
= error_mark_node
;
24575 /* Parse the optional ellipsis. */
24576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24578 /* Consume the `...'. */
24579 cp_lexer_consume_token (parser
->lexer
);
24581 /* Turn the type into a pack expansion expression. */
24582 type
= make_pack_expansion (type
);
24584 /* Add it to the list. */
24585 types
= add_exception_specifier (types
, type
, /*complain=*/1);
24586 /* Peek at the next token. */
24587 token
= cp_lexer_peek_token (parser
->lexer
);
24588 /* If it is not a `,', we are done. */
24589 if (token
->type
!= CPP_COMMA
)
24591 /* Consume the `,'. */
24592 cp_lexer_consume_token (parser
->lexer
);
24595 return nreverse (types
);
24598 /* Parse a try-block.
24601 try compound-statement handler-seq */
24604 cp_parser_try_block (cp_parser
* parser
)
24608 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
24609 if (parser
->in_function_body
24610 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
24611 error ("%<try%> in %<constexpr%> function");
24613 try_block
= begin_try_block ();
24614 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
24615 finish_try_block (try_block
);
24616 cp_parser_handler_seq (parser
);
24617 finish_handler_sequence (try_block
);
24622 /* Parse a function-try-block.
24624 function-try-block:
24625 try ctor-initializer [opt] function-body handler-seq */
24628 cp_parser_function_try_block (cp_parser
* parser
)
24630 tree compound_stmt
;
24633 /* Look for the `try' keyword. */
24634 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
24636 /* Let the rest of the front end know where we are. */
24637 try_block
= begin_function_try_block (&compound_stmt
);
24638 /* Parse the function-body. */
24639 cp_parser_ctor_initializer_opt_and_function_body
24640 (parser
, /*in_function_try_block=*/true);
24641 /* We're done with the `try' part. */
24642 finish_function_try_block (try_block
);
24643 /* Parse the handlers. */
24644 cp_parser_handler_seq (parser
);
24645 /* We're done with the handlers. */
24646 finish_function_handler_sequence (try_block
, compound_stmt
);
24649 /* Parse a handler-seq.
24652 handler handler-seq [opt] */
24655 cp_parser_handler_seq (cp_parser
* parser
)
24661 /* Parse the handler. */
24662 cp_parser_handler (parser
);
24663 /* Peek at the next token. */
24664 token
= cp_lexer_peek_token (parser
->lexer
);
24665 /* If it's not `catch' then there are no more handlers. */
24666 if (!cp_parser_is_keyword (token
, RID_CATCH
))
24671 /* Parse a handler.
24674 catch ( exception-declaration ) compound-statement */
24677 cp_parser_handler (cp_parser
* parser
)
24682 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
24683 handler
= begin_handler ();
24684 matching_parens parens
;
24685 parens
.require_open (parser
);
24686 declaration
= cp_parser_exception_declaration (parser
);
24687 finish_handler_parms (declaration
, handler
);
24688 parens
.require_close (parser
);
24689 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
24690 finish_handler (handler
);
24693 /* Parse an exception-declaration.
24695 exception-declaration:
24696 type-specifier-seq declarator
24697 type-specifier-seq abstract-declarator
24701 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24702 ellipsis variant is used. */
24705 cp_parser_exception_declaration (cp_parser
* parser
)
24707 cp_decl_specifier_seq type_specifiers
;
24708 cp_declarator
*declarator
;
24709 const char *saved_message
;
24711 /* If it's an ellipsis, it's easy to handle. */
24712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24714 /* Consume the `...' token. */
24715 cp_lexer_consume_token (parser
->lexer
);
24719 /* Types may not be defined in exception-declarations. */
24720 saved_message
= parser
->type_definition_forbidden_message
;
24721 parser
->type_definition_forbidden_message
24722 = G_("types may not be defined in exception-declarations");
24724 /* Parse the type-specifier-seq. */
24725 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
24726 /*is_trailing_return=*/false,
24728 /* If it's a `)', then there is no declarator. */
24729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
24732 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
24733 /*ctor_dtor_or_conv_p=*/NULL
,
24734 /*parenthesized_p=*/NULL
,
24735 /*member_p=*/false,
24736 /*friend_p=*/false);
24738 /* Restore the saved message. */
24739 parser
->type_definition_forbidden_message
= saved_message
;
24741 if (!type_specifiers
.any_specifiers_p
)
24742 return error_mark_node
;
24744 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
24747 /* Parse a throw-expression.
24750 throw assignment-expression [opt]
24752 Returns a THROW_EXPR representing the throw-expression. */
24755 cp_parser_throw_expression (cp_parser
* parser
)
24760 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
24761 token
= cp_lexer_peek_token (parser
->lexer
);
24762 /* Figure out whether or not there is an assignment-expression
24763 following the "throw" keyword. */
24764 if (token
->type
== CPP_COMMA
24765 || token
->type
== CPP_SEMICOLON
24766 || token
->type
== CPP_CLOSE_PAREN
24767 || token
->type
== CPP_CLOSE_SQUARE
24768 || token
->type
== CPP_CLOSE_BRACE
24769 || token
->type
== CPP_COLON
)
24770 expression
= NULL_TREE
;
24772 expression
= cp_parser_assignment_expression (parser
);
24774 return build_throw (expression
);
24777 /* GNU Extensions */
24779 /* Parse an (optional) asm-specification.
24782 asm ( string-literal )
24784 If the asm-specification is present, returns a STRING_CST
24785 corresponding to the string-literal. Otherwise, returns
24789 cp_parser_asm_specification_opt (cp_parser
* parser
)
24792 tree asm_specification
;
24794 /* Peek at the next token. */
24795 token
= cp_lexer_peek_token (parser
->lexer
);
24796 /* If the next token isn't the `asm' keyword, then there's no
24797 asm-specification. */
24798 if (!cp_parser_is_keyword (token
, RID_ASM
))
24801 /* Consume the `asm' token. */
24802 cp_lexer_consume_token (parser
->lexer
);
24803 /* Look for the `('. */
24804 matching_parens parens
;
24805 parens
.require_open (parser
);
24807 /* Look for the string-literal. */
24808 asm_specification
= cp_parser_string_literal (parser
, false, false);
24810 /* Look for the `)'. */
24811 parens
.require_close (parser
);
24813 return asm_specification
;
24816 /* Parse an asm-operand-list.
24820 asm-operand-list , asm-operand
24823 string-literal ( expression )
24824 [ string-literal ] string-literal ( expression )
24826 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24827 each node is the expression. The TREE_PURPOSE is itself a
24828 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24829 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24830 is a STRING_CST for the string literal before the parenthesis. Returns
24831 ERROR_MARK_NODE if any of the operands are invalid. */
24834 cp_parser_asm_operand_list (cp_parser
* parser
)
24836 tree asm_operands
= NULL_TREE
;
24837 bool invalid_operands
= false;
24841 tree string_literal
;
24845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24847 /* Consume the `[' token. */
24848 cp_lexer_consume_token (parser
->lexer
);
24849 /* Read the operand name. */
24850 name
= cp_parser_identifier (parser
);
24851 if (name
!= error_mark_node
)
24852 name
= build_string (IDENTIFIER_LENGTH (name
),
24853 IDENTIFIER_POINTER (name
));
24854 /* Look for the closing `]'. */
24855 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24859 /* Look for the string-literal. */
24860 string_literal
= cp_parser_string_literal (parser
, false, false);
24862 /* Look for the `('. */
24863 matching_parens parens
;
24864 parens
.require_open (parser
);
24865 /* Parse the expression. */
24866 expression
= cp_parser_expression (parser
);
24867 /* Look for the `)'. */
24868 parens
.require_close (parser
);
24870 if (name
== error_mark_node
24871 || string_literal
== error_mark_node
24872 || expression
== error_mark_node
)
24873 invalid_operands
= true;
24875 /* Add this operand to the list. */
24876 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
24879 /* If the next token is not a `,', there are no more
24881 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24883 /* Consume the `,'. */
24884 cp_lexer_consume_token (parser
->lexer
);
24887 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
24890 /* Parse an asm-clobber-list.
24894 asm-clobber-list , string-literal
24896 Returns a TREE_LIST, indicating the clobbers in the order that they
24897 appeared. The TREE_VALUE of each node is a STRING_CST. */
24900 cp_parser_asm_clobber_list (cp_parser
* parser
)
24902 tree clobbers
= NULL_TREE
;
24906 tree string_literal
;
24908 /* Look for the string literal. */
24909 string_literal
= cp_parser_string_literal (parser
, false, false);
24910 /* Add it to the list. */
24911 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
24912 /* If the next token is not a `,', then the list is
24914 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24916 /* Consume the `,' token. */
24917 cp_lexer_consume_token (parser
->lexer
);
24923 /* Parse an asm-label-list.
24927 asm-label-list , identifier
24929 Returns a TREE_LIST, indicating the labels in the order that they
24930 appeared. The TREE_VALUE of each node is a label. */
24933 cp_parser_asm_label_list (cp_parser
* parser
)
24935 tree labels
= NULL_TREE
;
24939 tree identifier
, label
, name
;
24941 /* Look for the identifier. */
24942 identifier
= cp_parser_identifier (parser
);
24943 if (!error_operand_p (identifier
))
24945 label
= lookup_label (identifier
);
24946 if (TREE_CODE (label
) == LABEL_DECL
)
24948 TREE_USED (label
) = 1;
24949 check_goto (label
);
24950 name
= build_string (IDENTIFIER_LENGTH (identifier
),
24951 IDENTIFIER_POINTER (identifier
));
24952 labels
= tree_cons (name
, label
, labels
);
24955 /* If the next token is not a `,', then the list is
24957 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24959 /* Consume the `,' token. */
24960 cp_lexer_consume_token (parser
->lexer
);
24963 return nreverse (labels
);
24966 /* Return TRUE iff the next tokens in the stream are possibly the
24967 beginning of a GNU extension attribute. */
24970 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
24972 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
24975 /* Return TRUE iff the next tokens in the stream are possibly the
24976 beginning of a standard C++-11 attribute specifier. */
24979 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
24981 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
24984 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24985 beginning of a standard C++-11 attribute specifier. */
24988 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
24990 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24992 return (cxx_dialect
>= cxx11
24993 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
24994 || (token
->type
== CPP_OPEN_SQUARE
24995 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
24996 && token
->type
== CPP_OPEN_SQUARE
)));
24999 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25000 beginning of a GNU extension attribute. */
25003 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
25005 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25007 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
25010 /* Return true iff the next tokens can be the beginning of either a
25011 GNU attribute list, or a standard C++11 attribute sequence. */
25014 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
25016 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
25017 || cp_next_tokens_can_be_std_attribute_p (parser
));
25020 /* Return true iff the next Nth tokens can be the beginning of either
25021 a GNU attribute list, or a standard C++11 attribute sequence. */
25024 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
25026 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
25027 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
25030 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25031 of GNU attributes, or return NULL. */
25034 cp_parser_attributes_opt (cp_parser
*parser
)
25036 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
25037 return cp_parser_gnu_attributes_opt (parser
);
25038 return cp_parser_std_attribute_spec_seq (parser
);
25041 /* Parse an (optional) series of attributes.
25044 attributes attribute
25047 __attribute__ (( attribute-list [opt] ))
25049 The return value is as for cp_parser_gnu_attribute_list. */
25052 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
25054 tree attributes
= NULL_TREE
;
25056 temp_override
<bool> cleanup
25057 (parser
->auto_is_implicit_function_template_parm_p
, false);
25062 tree attribute_list
;
25065 /* Peek at the next token. */
25066 token
= cp_lexer_peek_token (parser
->lexer
);
25067 /* If it's not `__attribute__', then we're done. */
25068 if (token
->keyword
!= RID_ATTRIBUTE
)
25071 /* Consume the `__attribute__' keyword. */
25072 cp_lexer_consume_token (parser
->lexer
);
25073 /* Look for the two `(' tokens. */
25074 matching_parens outer_parens
;
25075 outer_parens
.require_open (parser
);
25076 matching_parens inner_parens
;
25077 inner_parens
.require_open (parser
);
25079 /* Peek at the next token. */
25080 token
= cp_lexer_peek_token (parser
->lexer
);
25081 if (token
->type
!= CPP_CLOSE_PAREN
)
25082 /* Parse the attribute-list. */
25083 attribute_list
= cp_parser_gnu_attribute_list (parser
);
25085 /* If the next token is a `)', then there is no attribute
25087 attribute_list
= NULL
;
25089 /* Look for the two `)' tokens. */
25090 if (!inner_parens
.require_close (parser
))
25092 if (!outer_parens
.require_close (parser
))
25095 cp_parser_skip_to_end_of_statement (parser
);
25097 /* Add these new attributes to the list. */
25098 attributes
= attr_chainon (attributes
, attribute_list
);
25104 /* Parse a GNU attribute-list.
25108 attribute-list , attribute
25112 identifier ( identifier )
25113 identifier ( identifier , expression-list )
25114 identifier ( expression-list )
25116 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25117 to an attribute. The TREE_PURPOSE of each node is the identifier
25118 indicating which attribute is in use. The TREE_VALUE represents
25119 the arguments, if any. */
25122 cp_parser_gnu_attribute_list (cp_parser
* parser
)
25124 tree attribute_list
= NULL_TREE
;
25125 bool save_translate_strings_p
= parser
->translate_strings_p
;
25127 parser
->translate_strings_p
= false;
25134 /* Look for the identifier. We also allow keywords here; for
25135 example `__attribute__ ((const))' is legal. */
25136 token
= cp_lexer_peek_token (parser
->lexer
);
25137 if (token
->type
== CPP_NAME
25138 || token
->type
== CPP_KEYWORD
)
25140 tree arguments
= NULL_TREE
;
25142 /* Consume the token, but save it since we need it for the
25143 SIMD enabled function parsing. */
25144 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
25146 /* Save away the identifier that indicates which attribute
25148 identifier
= (token
->type
== CPP_KEYWORD
)
25149 /* For keywords, use the canonical spelling, not the
25150 parsed identifier. */
25151 ? ridpointers
[(int) token
->keyword
]
25152 : id_token
->u
.value
;
25154 identifier
= canonicalize_attr_name (identifier
);
25155 attribute
= build_tree_list (identifier
, NULL_TREE
);
25157 /* Peek at the next token. */
25158 token
= cp_lexer_peek_token (parser
->lexer
);
25159 /* If it's an `(', then parse the attribute arguments. */
25160 if (token
->type
== CPP_OPEN_PAREN
)
25162 vec
<tree
, va_gc
> *vec
;
25163 int attr_flag
= (attribute_takes_identifier_p (identifier
)
25164 ? id_attr
: normal_attr
);
25165 vec
= cp_parser_parenthesized_expression_list
25166 (parser
, attr_flag
, /*cast_p=*/false,
25167 /*allow_expansion_p=*/false,
25168 /*non_constant_p=*/NULL
);
25170 arguments
= error_mark_node
;
25173 arguments
= build_tree_list_vec (vec
);
25174 release_tree_vector (vec
);
25176 /* Save the arguments away. */
25177 TREE_VALUE (attribute
) = arguments
;
25180 if (arguments
!= error_mark_node
)
25182 /* Add this attribute to the list. */
25183 TREE_CHAIN (attribute
) = attribute_list
;
25184 attribute_list
= attribute
;
25187 token
= cp_lexer_peek_token (parser
->lexer
);
25189 /* Now, look for more attributes. If the next token isn't a
25190 `,', we're done. */
25191 if (token
->type
!= CPP_COMMA
)
25194 /* Consume the comma and keep going. */
25195 cp_lexer_consume_token (parser
->lexer
);
25197 parser
->translate_strings_p
= save_translate_strings_p
;
25199 /* We built up the list in reverse order. */
25200 return nreverse (attribute_list
);
25203 /* Parse a standard C++11 attribute.
25205 The returned representation is a TREE_LIST which TREE_PURPOSE is
25206 the scoped name of the attribute, and the TREE_VALUE is its
25209 Note that the scoped name of the attribute is itself a TREE_LIST
25210 which TREE_PURPOSE is the namespace of the attribute, and
25211 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25212 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25213 and which TREE_PURPOSE is directly the attribute name.
25215 Clients of the attribute code should use get_attribute_namespace
25216 and get_attribute_name to get the actual namespace and name of
25217 attributes, regardless of their being GNU or C++11 attributes.
25220 attribute-token attribute-argument-clause [opt]
25224 attribute-scoped-token
25226 attribute-scoped-token:
25227 attribute-namespace :: identifier
25229 attribute-namespace:
25232 attribute-argument-clause:
25233 ( balanced-token-seq )
25235 balanced-token-seq:
25236 balanced-token [opt]
25237 balanced-token-seq balanced-token
25240 ( balanced-token-seq )
25241 [ balanced-token-seq ]
25242 { balanced-token-seq }. */
25245 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
25247 tree attribute
, attr_id
= NULL_TREE
, arguments
;
25250 temp_override
<bool> cleanup
25251 (parser
->auto_is_implicit_function_template_parm_p
, false);
25253 /* First, parse name of the attribute, a.k.a attribute-token. */
25255 token
= cp_lexer_peek_token (parser
->lexer
);
25256 if (token
->type
== CPP_NAME
)
25257 attr_id
= token
->u
.value
;
25258 else if (token
->type
== CPP_KEYWORD
)
25259 attr_id
= ridpointers
[(int) token
->keyword
];
25260 else if (token
->flags
& NAMED_OP
)
25261 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25263 if (attr_id
== NULL_TREE
)
25266 cp_lexer_consume_token (parser
->lexer
);
25268 token
= cp_lexer_peek_token (parser
->lexer
);
25269 if (token
->type
== CPP_SCOPE
)
25271 /* We are seeing a scoped attribute token. */
25273 cp_lexer_consume_token (parser
->lexer
);
25275 error_at (token
->location
, "attribute using prefix used together "
25276 "with scoped attribute token");
25279 token
= cp_lexer_consume_token (parser
->lexer
);
25280 if (token
->type
== CPP_NAME
)
25281 attr_id
= token
->u
.value
;
25282 else if (token
->type
== CPP_KEYWORD
)
25283 attr_id
= ridpointers
[(int) token
->keyword
];
25284 else if (token
->flags
& NAMED_OP
)
25285 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
25288 error_at (token
->location
,
25289 "expected an identifier for the attribute name");
25290 return error_mark_node
;
25293 attr_id
= canonicalize_attr_name (attr_id
);
25294 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25296 token
= cp_lexer_peek_token (parser
->lexer
);
25299 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
25303 attr_id
= canonicalize_attr_name (attr_id
);
25304 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
25306 /* C++11 noreturn attribute is equivalent to GNU's. */
25307 if (is_attribute_p ("noreturn", attr_id
))
25308 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25309 /* C++14 deprecated attribute is equivalent to GNU's. */
25310 else if (is_attribute_p ("deprecated", attr_id
))
25311 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25312 /* C++17 fallthrough attribute is equivalent to GNU's. */
25313 else if (is_attribute_p ("fallthrough", attr_id
))
25314 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
25315 /* Transactional Memory TS optimize_for_synchronized attribute is
25316 equivalent to GNU transaction_callable. */
25317 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
25318 TREE_PURPOSE (attribute
)
25319 = get_identifier ("transaction_callable");
25320 /* Transactional Memory attributes are GNU attributes. */
25321 else if (tm_attr_to_mask (attr_id
))
25322 TREE_PURPOSE (attribute
) = attr_id
;
25325 /* Now parse the optional argument clause of the attribute. */
25327 if (token
->type
!= CPP_OPEN_PAREN
)
25331 vec
<tree
, va_gc
> *vec
;
25332 int attr_flag
= normal_attr
;
25334 if (attr_ns
== get_identifier ("gnu")
25335 && attribute_takes_identifier_p (attr_id
))
25336 /* A GNU attribute that takes an identifier in parameter. */
25337 attr_flag
= id_attr
;
25339 vec
= cp_parser_parenthesized_expression_list
25340 (parser
, attr_flag
, /*cast_p=*/false,
25341 /*allow_expansion_p=*/true,
25342 /*non_constant_p=*/NULL
);
25344 arguments
= error_mark_node
;
25347 arguments
= build_tree_list_vec (vec
);
25348 release_tree_vector (vec
);
25351 if (arguments
== error_mark_node
)
25352 attribute
= error_mark_node
;
25354 TREE_VALUE (attribute
) = arguments
;
25360 /* Check that the attribute ATTRIBUTE appears at most once in the
25361 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25362 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25363 isn't implemented yet in GCC. */
25366 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
25370 tree name
= get_attribute_name (attribute
);
25371 if (is_attribute_p ("noreturn", name
)
25372 && lookup_attribute ("noreturn", attributes
))
25373 error ("attribute %<noreturn%> can appear at most once "
25374 "in an attribute-list");
25375 else if (is_attribute_p ("deprecated", name
)
25376 && lookup_attribute ("deprecated", attributes
))
25377 error ("attribute %<deprecated%> can appear at most once "
25378 "in an attribute-list");
25382 /* Parse a list of standard C++-11 attributes.
25386 attribute-list , attribute[opt]
25388 attribute-list , attribute ...
25392 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
25394 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
25395 cp_token
*token
= NULL
;
25399 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
25400 if (attribute
== error_mark_node
)
25402 if (attribute
!= NULL_TREE
)
25404 cp_parser_check_std_attribute (attributes
, attribute
);
25405 TREE_CHAIN (attribute
) = attributes
;
25406 attributes
= attribute
;
25408 token
= cp_lexer_peek_token (parser
->lexer
);
25409 if (token
->type
== CPP_ELLIPSIS
)
25411 cp_lexer_consume_token (parser
->lexer
);
25412 if (attribute
== NULL_TREE
)
25413 error_at (token
->location
,
25414 "expected attribute before %<...%>");
25417 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
25418 if (pack
== error_mark_node
)
25419 return error_mark_node
;
25420 TREE_VALUE (attribute
) = pack
;
25422 token
= cp_lexer_peek_token (parser
->lexer
);
25424 if (token
->type
!= CPP_COMMA
)
25426 cp_lexer_consume_token (parser
->lexer
);
25428 attributes
= nreverse (attributes
);
25432 /* Parse a standard C++-11 attribute specifier.
25434 attribute-specifier:
25435 [ [ attribute-using-prefix [opt] attribute-list ] ]
25436 alignment-specifier
25438 attribute-using-prefix:
25439 using attribute-namespace :
25441 alignment-specifier:
25442 alignas ( type-id ... [opt] )
25443 alignas ( alignment-expression ... [opt] ). */
25446 cp_parser_std_attribute_spec (cp_parser
*parser
)
25448 tree attributes
= NULL_TREE
;
25449 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25451 if (token
->type
== CPP_OPEN_SQUARE
25452 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
25454 tree attr_ns
= NULL_TREE
;
25456 cp_lexer_consume_token (parser
->lexer
);
25457 cp_lexer_consume_token (parser
->lexer
);
25459 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25461 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
25462 if (token
->type
== CPP_NAME
)
25463 attr_ns
= token
->u
.value
;
25464 else if (token
->type
== CPP_KEYWORD
)
25465 attr_ns
= ridpointers
[(int) token
->keyword
];
25466 else if (token
->flags
& NAMED_OP
)
25467 attr_ns
= get_identifier (cpp_type2name (token
->type
,
25470 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
25472 if (cxx_dialect
< cxx17
25473 && !in_system_header_at (input_location
))
25474 pedwarn (input_location
, 0,
25475 "attribute using prefix only available "
25476 "with -std=c++17 or -std=gnu++17");
25478 cp_lexer_consume_token (parser
->lexer
);
25479 cp_lexer_consume_token (parser
->lexer
);
25480 cp_lexer_consume_token (parser
->lexer
);
25483 attr_ns
= NULL_TREE
;
25486 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
25488 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
25489 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
25490 cp_parser_skip_to_end_of_statement (parser
);
25492 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25493 when we are sure that we have actually parsed them. */
25494 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25500 /* Look for an alignment-specifier. */
25502 token
= cp_lexer_peek_token (parser
->lexer
);
25504 if (token
->type
!= CPP_KEYWORD
25505 || token
->keyword
!= RID_ALIGNAS
)
25508 cp_lexer_consume_token (parser
->lexer
);
25509 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
25511 matching_parens parens
;
25512 if (!parens
.require_open (parser
))
25513 return error_mark_node
;
25515 cp_parser_parse_tentatively (parser
);
25516 alignas_expr
= cp_parser_type_id (parser
);
25518 if (!cp_parser_parse_definitely (parser
))
25520 alignas_expr
= cp_parser_assignment_expression (parser
);
25521 if (alignas_expr
== error_mark_node
)
25522 cp_parser_skip_to_end_of_statement (parser
);
25523 if (alignas_expr
== NULL_TREE
25524 || alignas_expr
== error_mark_node
)
25525 return alignas_expr
;
25528 alignas_expr
= cxx_alignas_expr (alignas_expr
);
25529 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
25531 /* Handle alignas (pack...). */
25532 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25534 cp_lexer_consume_token (parser
->lexer
);
25535 alignas_expr
= make_pack_expansion (alignas_expr
);
25538 /* Something went wrong, so don't build the attribute. */
25539 if (alignas_expr
== error_mark_node
)
25540 return error_mark_node
;
25542 if (!parens
.require_close (parser
))
25543 return error_mark_node
;
25545 /* Build the C++-11 representation of an 'aligned'
25548 build_tree_list (build_tree_list (get_identifier ("gnu"),
25549 get_identifier ("aligned")),
25556 /* Parse a standard C++-11 attribute-specifier-seq.
25558 attribute-specifier-seq:
25559 attribute-specifier-seq [opt] attribute-specifier
25563 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
25565 tree attr_specs
= NULL_TREE
;
25566 tree attr_last
= NULL_TREE
;
25570 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
25571 if (attr_spec
== NULL_TREE
)
25573 if (attr_spec
== error_mark_node
)
25574 return error_mark_node
;
25577 TREE_CHAIN (attr_last
) = attr_spec
;
25579 attr_specs
= attr_last
= attr_spec
;
25580 attr_last
= tree_last (attr_last
);
25586 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25587 return index of the first token after balanced-token, or N on failure. */
25590 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
25593 int nparens
= 0, nbraces
= 0, nsquares
= 0;
25595 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
25598 case CPP_PRAGMA_EOL
:
25599 /* Ran out of tokens. */
25601 case CPP_OPEN_PAREN
:
25604 case CPP_OPEN_BRACE
:
25607 case CPP_OPEN_SQUARE
:
25610 case CPP_CLOSE_PAREN
:
25613 case CPP_CLOSE_BRACE
:
25616 case CPP_CLOSE_SQUARE
:
25622 while (nparens
|| nbraces
|| nsquares
);
25626 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25627 return index of the first token after the GNU attribute tokens, or N on
25631 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
25635 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
25636 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
25637 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
25640 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
25643 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
25650 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25651 next token), return index of the first token after the standard C++11
25652 attribute tokens, or N on failure. */
25655 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
25659 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
25660 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
25662 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25665 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
25669 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
25670 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
25672 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
25683 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25684 as the next token), return index of the first token after the attribute
25685 tokens, or N on failure. */
25688 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
25690 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
25691 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
25692 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
25695 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25696 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25697 current value of the PEDANTIC flag, regardless of whether or not
25698 the `__extension__' keyword is present. The caller is responsible
25699 for restoring the value of the PEDANTIC flag. */
25702 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
25704 /* Save the old value of the PEDANTIC flag. */
25705 *saved_pedantic
= pedantic
;
25707 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
25709 /* Consume the `__extension__' token. */
25710 cp_lexer_consume_token (parser
->lexer
);
25711 /* We're not being pedantic while the `__extension__' keyword is
25721 /* Parse a label declaration.
25724 __label__ label-declarator-seq ;
25726 label-declarator-seq:
25727 identifier , label-declarator-seq
25731 cp_parser_label_declaration (cp_parser
* parser
)
25733 /* Look for the `__label__' keyword. */
25734 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
25740 /* Look for an identifier. */
25741 identifier
= cp_parser_identifier (parser
);
25742 /* If we failed, stop. */
25743 if (identifier
== error_mark_node
)
25745 /* Declare it as a label. */
25746 finish_label_decl (identifier
);
25747 /* If the next token is a `;', stop. */
25748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25750 /* Look for the `,' separating the label declarations. */
25751 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
25754 /* Look for the final `;'. */
25755 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25758 // -------------------------------------------------------------------------- //
25761 // Parse a requires clause.
25763 // requires-clause:
25764 // 'requires' logical-or-expression
25766 // The required logical-or-expression must be a constant expression. Note
25767 // that we don't check that the expression is constepxr here. We defer until
25768 // we analyze constraints and then, we only check atomic constraints.
25770 cp_parser_requires_clause (cp_parser
*parser
)
25772 // Parse the requires clause so that it is not automatically folded.
25773 ++processing_template_decl
;
25774 tree expr
= cp_parser_binary_expression (parser
, false, false,
25775 PREC_NOT_OPERATOR
, NULL
);
25776 if (check_for_bare_parameter_packs (expr
))
25777 expr
= error_mark_node
;
25778 --processing_template_decl
;
25782 // Optionally parse a requires clause:
25784 cp_parser_requires_clause_opt (cp_parser
*parser
)
25786 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
25787 if (tok
->keyword
!= RID_REQUIRES
)
25789 if (!flag_concepts
&& tok
->type
== CPP_NAME
25790 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
25792 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25793 "%<requires%> only available with -fconcepts");
25794 /* Parse and discard the requires-clause. */
25795 cp_lexer_consume_token (parser
->lexer
);
25796 cp_parser_requires_clause (parser
);
25800 cp_lexer_consume_token (parser
->lexer
);
25801 return cp_parser_requires_clause (parser
);
25805 /*---------------------------------------------------------------------------
25806 Requires expressions
25807 ---------------------------------------------------------------------------*/
25809 /* Parse a requires expression
25811 requirement-expression:
25812 'requires' requirement-parameter-list [opt] requirement-body */
25814 cp_parser_requires_expression (cp_parser
*parser
)
25816 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
25817 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
25819 /* A requires-expression shall appear only within a concept
25820 definition or a requires-clause.
25822 TODO: Implement this diagnostic correctly. */
25823 if (!processing_template_decl
)
25825 error_at (loc
, "a requires expression cannot appear outside a template");
25826 cp_parser_skip_to_end_of_statement (parser
);
25827 return error_mark_node
;
25832 /* Local parameters are delared as variables within the scope
25833 of the expression. They are not visible past the end of
25834 the expression. Expressions within the requires-expression
25835 are unevaluated. */
25836 struct scope_sentinel
25840 ++cp_unevaluated_operand
;
25841 begin_scope (sk_block
, NULL_TREE
);
25846 pop_bindings_and_leave_scope ();
25847 --cp_unevaluated_operand
;
25851 /* Parse the optional parameter list. */
25852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25854 parms
= cp_parser_requirement_parameter_list (parser
);
25855 if (parms
== error_mark_node
)
25856 return error_mark_node
;
25861 /* Parse the requirement body. */
25862 reqs
= cp_parser_requirement_body (parser
);
25863 if (reqs
== error_mark_node
)
25864 return error_mark_node
;
25867 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25869 grokparms (parms
, &parms
);
25870 return finish_requires_expr (parms
, reqs
);
25873 /* Parse a parameterized requirement.
25875 requirement-parameter-list:
25876 '(' parameter-declaration-clause ')' */
25878 cp_parser_requirement_parameter_list (cp_parser
*parser
)
25880 matching_parens parens
;
25881 if (!parens
.require_open (parser
))
25882 return error_mark_node
;
25884 tree parms
= cp_parser_parameter_declaration_clause (parser
);
25886 if (!parens
.require_close (parser
))
25887 return error_mark_node
;
25892 /* Parse the body of a requirement.
25895 '{' requirement-list '}' */
25897 cp_parser_requirement_body (cp_parser
*parser
)
25899 matching_braces braces
;
25900 if (!braces
.require_open (parser
))
25901 return error_mark_node
;
25903 tree reqs
= cp_parser_requirement_list (parser
);
25905 if (!braces
.require_close (parser
))
25906 return error_mark_node
;
25911 /* Parse a list of requirements.
25915 requirement-list ';' requirement[opt] */
25917 cp_parser_requirement_list (cp_parser
*parser
)
25919 tree result
= NULL_TREE
;
25922 tree req
= cp_parser_requirement (parser
);
25923 if (req
== error_mark_node
)
25924 return error_mark_node
;
25926 result
= tree_cons (NULL_TREE
, req
, result
);
25928 /* If we see a semi-colon, consume it. */
25929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25930 cp_lexer_consume_token (parser
->lexer
);
25932 /* Stop processing at the end of the list. */
25933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
25937 /* Reverse the order of requirements so they are analyzed in
25938 declaration order. */
25939 return nreverse (result
);
25942 /* Parse a syntactic requirement or type requirement.
25946 compound-requirement
25948 nested-requirement */
25950 cp_parser_requirement (cp_parser
*parser
)
25952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25953 return cp_parser_compound_requirement (parser
);
25954 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25955 return cp_parser_type_requirement (parser
);
25956 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
25957 return cp_parser_nested_requirement (parser
);
25959 return cp_parser_simple_requirement (parser
);
25962 /* Parse a simple requirement.
25964 simple-requirement:
25967 cp_parser_simple_requirement (cp_parser
*parser
)
25969 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
25970 if (!expr
|| expr
== error_mark_node
)
25971 return error_mark_node
;
25973 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
25974 return error_mark_node
;
25976 return finish_simple_requirement (expr
);
25979 /* Parse a type requirement
25982 nested-name-specifier [opt] required-type-name ';'
25984 required-type-name:
25986 'template' [opt] simple-template-id */
25988 cp_parser_type_requirement (cp_parser
*parser
)
25990 cp_lexer_consume_token (parser
->lexer
);
25992 // Save the scope before parsing name specifiers.
25993 tree saved_scope
= parser
->scope
;
25994 tree saved_object_scope
= parser
->object_scope
;
25995 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25996 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
25997 cp_parser_nested_name_specifier_opt (parser
,
25998 /*typename_keyword_p=*/true,
25999 /*check_dependency_p=*/false,
26001 /*is_declaration=*/false);
26004 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26006 cp_lexer_consume_token (parser
->lexer
);
26007 type
= cp_parser_template_id (parser
,
26008 /*template_keyword_p=*/true,
26009 /*check_dependency=*/false,
26010 /*tag_type=*/none_type
,
26011 /*is_declaration=*/false);
26012 type
= make_typename_type (parser
->scope
, type
, typename_type
,
26013 /*complain=*/tf_error
);
26016 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
26018 if (TREE_CODE (type
) == TYPE_DECL
)
26019 type
= TREE_TYPE (type
);
26021 parser
->scope
= saved_scope
;
26022 parser
->object_scope
= saved_object_scope
;
26023 parser
->qualifying_scope
= saved_qualifying_scope
;
26025 if (type
== error_mark_node
)
26026 cp_parser_skip_to_end_of_statement (parser
);
26028 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
26029 return error_mark_node
;
26030 if (type
== error_mark_node
)
26031 return error_mark_node
;
26033 return finish_type_requirement (type
);
26036 /* Parse a compound requirement
26038 compound-requirement:
26039 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26041 cp_parser_compound_requirement (cp_parser
*parser
)
26043 /* Parse an expression enclosed in '{ }'s. */
26044 matching_braces braces
;
26045 if (!braces
.require_open (parser
))
26046 return error_mark_node
;
26048 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
26049 if (!expr
|| expr
== error_mark_node
)
26050 return error_mark_node
;
26052 if (!braces
.require_close (parser
))
26053 return error_mark_node
;
26055 /* Parse the optional noexcept. */
26056 bool noexcept_p
= false;
26057 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
26059 cp_lexer_consume_token (parser
->lexer
);
26063 /* Parse the optional trailing return type. */
26064 tree type
= NULL_TREE
;
26065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
26067 cp_lexer_consume_token (parser
->lexer
);
26068 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
26069 parser
->in_result_type_constraint_p
= true;
26070 type
= cp_parser_trailing_type_id (parser
);
26071 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
26072 if (type
== error_mark_node
)
26073 return error_mark_node
;
26076 return finish_compound_requirement (expr
, type
, noexcept_p
);
26079 /* Parse a nested requirement. This is the same as a requires clause.
26081 nested-requirement:
26084 cp_parser_nested_requirement (cp_parser
*parser
)
26086 cp_lexer_consume_token (parser
->lexer
);
26087 tree req
= cp_parser_requires_clause (parser
);
26088 if (req
== error_mark_node
)
26089 return error_mark_node
;
26090 return finish_nested_requirement (req
);
26093 /* Support Functions */
26095 /* Return the appropriate prefer_type argument for lookup_name_real based on
26096 tag_type and template_mem_access. */
26099 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
26101 /* DR 141: When looking in the current enclosing context for a template-name
26102 after -> or ., only consider class templates. */
26103 if (template_mem_access
)
26107 case none_type
: return 0; // No preference.
26108 case scope_type
: return 1; // Type or namespace.
26109 default: return 2; // Type only.
26113 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26114 NAME should have one of the representations used for an
26115 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26116 is returned. If PARSER->SCOPE is a dependent type, then a
26117 SCOPE_REF is returned.
26119 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26120 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26121 was formed. Abstractly, such entities should not be passed to this
26122 function, because they do not need to be looked up, but it is
26123 simpler to check for this special case here, rather than at the
26126 In cases not explicitly covered above, this function returns a
26127 DECL, OVERLOAD, or baselink representing the result of the lookup.
26128 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26131 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26132 (e.g., "struct") that was used. In that case bindings that do not
26133 refer to types are ignored.
26135 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26138 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26141 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26144 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26145 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26146 NULL_TREE otherwise. */
26149 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
26150 enum tag_types tag_type
,
26153 bool check_dependency
,
26154 tree
*ambiguous_decls
,
26155 location_t name_location
)
26158 tree object_type
= parser
->context
->object_type
;
26160 /* Assume that the lookup will be unambiguous. */
26161 if (ambiguous_decls
)
26162 *ambiguous_decls
= NULL_TREE
;
26164 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26165 no longer valid. Note that if we are parsing tentatively, and
26166 the parse fails, OBJECT_TYPE will be automatically restored. */
26167 parser
->context
->object_type
= NULL_TREE
;
26169 if (name
== error_mark_node
)
26170 return error_mark_node
;
26172 /* A template-id has already been resolved; there is no lookup to
26174 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
26176 if (BASELINK_P (name
))
26178 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
26179 == TEMPLATE_ID_EXPR
);
26183 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26184 it should already have been checked to make sure that the name
26185 used matches the type being destroyed. */
26186 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
26190 /* Figure out to which type this destructor applies. */
26192 type
= parser
->scope
;
26193 else if (object_type
)
26194 type
= object_type
;
26196 type
= current_class_type
;
26197 /* If that's not a class type, there is no destructor. */
26198 if (!type
|| !CLASS_TYPE_P (type
))
26199 return error_mark_node
;
26201 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
26202 lazily_declare_fn (sfk_destructor
, type
);
26204 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
26207 return error_mark_node
;
26210 /* By this point, the NAME should be an ordinary identifier. If
26211 the id-expression was a qualified name, the qualifying scope is
26212 stored in PARSER->SCOPE at this point. */
26213 gcc_assert (identifier_p (name
));
26215 /* Perform the lookup. */
26220 if (parser
->scope
== error_mark_node
)
26221 return error_mark_node
;
26223 /* If the SCOPE is dependent, the lookup must be deferred until
26224 the template is instantiated -- unless we are explicitly
26225 looking up names in uninstantiated templates. Even then, we
26226 cannot look up the name if the scope is not a class type; it
26227 might, for example, be a template type parameter. */
26228 dependent_p
= (TYPE_P (parser
->scope
)
26229 && dependent_scope_p (parser
->scope
));
26230 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
26232 /* Defer lookup. */
26233 decl
= error_mark_node
;
26236 tree pushed_scope
= NULL_TREE
;
26238 /* If PARSER->SCOPE is a dependent type, then it must be a
26239 class type, and we must not be checking dependencies;
26240 otherwise, we would have processed this lookup above. So
26241 that PARSER->SCOPE is not considered a dependent base by
26242 lookup_member, we must enter the scope here. */
26244 pushed_scope
= push_scope (parser
->scope
);
26246 /* If the PARSER->SCOPE is a template specialization, it
26247 may be instantiated during name lookup. In that case,
26248 errors may be issued. Even if we rollback the current
26249 tentative parse, those errors are valid. */
26250 decl
= lookup_qualified_name (parser
->scope
, name
,
26251 prefer_type_arg (tag_type
),
26252 /*complain=*/true);
26254 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26255 lookup result and the nested-name-specifier nominates a class C:
26256 * if the name specified after the nested-name-specifier, when
26257 looked up in C, is the injected-class-name of C (Clause 9), or
26258 * if the name specified after the nested-name-specifier is the
26259 same as the identifier or the simple-template-id's template-
26260 name in the last component of the nested-name-specifier,
26261 the name is instead considered to name the constructor of
26262 class C. [ Note: for example, the constructor is not an
26263 acceptable lookup result in an elaborated-type-specifier so
26264 the constructor would not be used in place of the
26265 injected-class-name. --end note ] Such a constructor name
26266 shall be used only in the declarator-id of a declaration that
26267 names a constructor or in a using-declaration. */
26268 if (tag_type
== none_type
26269 && DECL_SELF_REFERENCE_P (decl
)
26270 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
26271 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
26272 prefer_type_arg (tag_type
),
26273 /*complain=*/true);
26275 /* If we have a single function from a using decl, pull it out. */
26276 if (TREE_CODE (decl
) == OVERLOAD
26277 && !really_overloaded_fn (decl
))
26278 decl
= OVL_FUNCTION (decl
);
26281 pop_scope (pushed_scope
);
26284 /* If the scope is a dependent type and either we deferred lookup or
26285 we did lookup but didn't find the name, rememeber the name. */
26286 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
26287 && dependent_type_p (parser
->scope
))
26293 /* The resolution to Core Issue 180 says that `struct
26294 A::B' should be considered a type-name, even if `A'
26296 type
= make_typename_type (parser
->scope
, name
, tag_type
,
26297 /*complain=*/tf_error
);
26298 if (type
!= error_mark_node
)
26299 decl
= TYPE_NAME (type
);
26301 else if (is_template
26302 && (cp_parser_next_token_ends_template_argument_p (parser
)
26303 || cp_lexer_next_token_is (parser
->lexer
,
26305 decl
= make_unbound_class_template (parser
->scope
,
26307 /*complain=*/tf_error
);
26309 decl
= build_qualified_name (/*type=*/NULL_TREE
,
26310 parser
->scope
, name
,
26313 parser
->qualifying_scope
= parser
->scope
;
26314 parser
->object_scope
= NULL_TREE
;
26316 else if (object_type
)
26318 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26319 OBJECT_TYPE is not a class. */
26320 if (CLASS_TYPE_P (object_type
))
26321 /* If the OBJECT_TYPE is a template specialization, it may
26322 be instantiated during name lookup. In that case, errors
26323 may be issued. Even if we rollback the current tentative
26324 parse, those errors are valid. */
26325 decl
= lookup_member (object_type
,
26328 prefer_type_arg (tag_type
),
26329 tf_warning_or_error
);
26334 /* Look it up in the enclosing context. DR 141: When looking for a
26335 template-name after -> or ., only consider class templates. */
26336 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
26338 /*block_p=*/true, is_namespace
, 0);
26339 if (object_type
== unknown_type_node
)
26340 /* The object is type-dependent, so we can't look anything up; we used
26341 this to get the DR 141 behavior. */
26342 object_type
= NULL_TREE
;
26343 parser
->object_scope
= object_type
;
26344 parser
->qualifying_scope
= NULL_TREE
;
26348 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
26350 /*block_p=*/true, is_namespace
, 0);
26351 parser
->qualifying_scope
= NULL_TREE
;
26352 parser
->object_scope
= NULL_TREE
;
26355 /* If the lookup failed, let our caller know. */
26356 if (!decl
|| decl
== error_mark_node
)
26357 return error_mark_node
;
26359 /* Pull out the template from an injected-class-name (or multiple). */
26361 decl
= maybe_get_template_decl_from_type_decl (decl
);
26363 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26364 if (TREE_CODE (decl
) == TREE_LIST
)
26366 if (ambiguous_decls
)
26367 *ambiguous_decls
= decl
;
26368 /* The error message we have to print is too complicated for
26369 cp_parser_error, so we incorporate its actions directly. */
26370 if (!cp_parser_simulate_error (parser
))
26372 error_at (name_location
, "reference to %qD is ambiguous",
26374 print_candidates (decl
);
26376 return error_mark_node
;
26379 gcc_assert (DECL_P (decl
)
26380 || TREE_CODE (decl
) == OVERLOAD
26381 || TREE_CODE (decl
) == SCOPE_REF
26382 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
26383 || BASELINK_P (decl
));
26385 /* If we have resolved the name of a member declaration, check to
26386 see if the declaration is accessible. When the name resolves to
26387 set of overloaded functions, accessibility is checked when
26388 overload resolution is done.
26390 During an explicit instantiation, access is not checked at all,
26391 as per [temp.explicit]. */
26393 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
26395 maybe_record_typedef_use (decl
);
26397 return cp_expr (decl
, name_location
);
26400 /* Like cp_parser_lookup_name, but for use in the typical case where
26401 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26402 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26405 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
26407 return cp_parser_lookup_name (parser
, name
,
26409 /*is_template=*/false,
26410 /*is_namespace=*/false,
26411 /*check_dependency=*/true,
26412 /*ambiguous_decls=*/NULL
,
26416 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26417 the current context, return the TYPE_DECL. If TAG_NAME_P is
26418 true, the DECL indicates the class being defined in a class-head,
26419 or declared in an elaborated-type-specifier.
26421 Otherwise, return DECL. */
26424 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
26426 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26427 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26430 template <typename T> struct B;
26433 template <typename T> struct A::B {};
26435 Similarly, in an elaborated-type-specifier:
26437 namespace N { struct X{}; }
26440 template <typename T> friend struct N::X;
26443 However, if the DECL refers to a class type, and we are in
26444 the scope of the class, then the name lookup automatically
26445 finds the TYPE_DECL created by build_self_reference rather
26446 than a TEMPLATE_DECL. For example, in:
26448 template <class T> struct S {
26452 there is no need to handle such case. */
26454 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
26455 return DECL_TEMPLATE_RESULT (decl
);
26460 /* If too many, or too few, template-parameter lists apply to the
26461 declarator, issue an error message. Returns TRUE if all went well,
26462 and FALSE otherwise. */
26465 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
26466 cp_declarator
*declarator
,
26467 location_t declarator_location
)
26469 switch (declarator
->kind
)
26473 unsigned num_templates
= 0;
26474 tree scope
= declarator
->u
.id
.qualifying_scope
;
26475 bool template_id_p
= false;
26478 num_templates
= num_template_headers_for_class (scope
);
26479 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
26480 == TEMPLATE_ID_EXPR
)
26482 /* If the DECLARATOR has the form `X<y>' then it uses one
26483 additional level of template parameters. */
26485 template_id_p
= true;
26488 return cp_parser_check_template_parameters
26489 (parser
, num_templates
, template_id_p
, declarator_location
,
26496 case cdk_reference
:
26498 return (cp_parser_check_declarator_template_parameters
26499 (parser
, declarator
->declarator
, declarator_location
));
26506 gcc_unreachable ();
26511 /* NUM_TEMPLATES were used in the current declaration. If that is
26512 invalid, return FALSE and issue an error messages. Otherwise,
26513 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26514 declarator and we can print more accurate diagnostics. */
26517 cp_parser_check_template_parameters (cp_parser
* parser
,
26518 unsigned num_templates
,
26519 bool template_id_p
,
26520 location_t location
,
26521 cp_declarator
*declarator
)
26523 /* If there are the same number of template classes and parameter
26524 lists, that's OK. */
26525 if (parser
->num_template_parameter_lists
== num_templates
)
26527 /* If there are more, but only one more, and the name ends in an identifier,
26528 then we are declaring a primary template. That's OK too. */
26530 && parser
->num_template_parameter_lists
== num_templates
+ 1)
26532 /* If there are more template classes than parameter lists, we have
26535 template <class T> void S<T>::R<T>::f (); */
26536 if (parser
->num_template_parameter_lists
< num_templates
)
26538 if (declarator
&& !current_function_decl
)
26539 error_at (location
, "specializing member %<%T::%E%> "
26540 "requires %<template<>%> syntax",
26541 declarator
->u
.id
.qualifying_scope
,
26542 declarator
->u
.id
.unqualified_name
);
26543 else if (declarator
)
26544 error_at (location
, "invalid declaration of %<%T::%E%>",
26545 declarator
->u
.id
.qualifying_scope
,
26546 declarator
->u
.id
.unqualified_name
);
26548 error_at (location
, "too few template-parameter-lists");
26551 /* Otherwise, there are too many template parameter lists. We have
26554 template <class T> template <class U> void S::f(); */
26555 error_at (location
, "too many template-parameter-lists");
26559 /* Parse an optional `::' token indicating that the following name is
26560 from the global namespace. If so, PARSER->SCOPE is set to the
26561 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26562 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26563 Returns the new value of PARSER->SCOPE, if the `::' token is
26564 present, and NULL_TREE otherwise. */
26567 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
26571 /* Peek at the next token. */
26572 token
= cp_lexer_peek_token (parser
->lexer
);
26573 /* If we're looking at a `::' token then we're starting from the
26574 global namespace, not our current location. */
26575 if (token
->type
== CPP_SCOPE
)
26577 /* Consume the `::' token. */
26578 cp_lexer_consume_token (parser
->lexer
);
26579 /* Set the SCOPE so that we know where to start the lookup. */
26580 parser
->scope
= global_namespace
;
26581 parser
->qualifying_scope
= global_namespace
;
26582 parser
->object_scope
= NULL_TREE
;
26584 return parser
->scope
;
26586 else if (!current_scope_valid_p
)
26588 parser
->scope
= NULL_TREE
;
26589 parser
->qualifying_scope
= NULL_TREE
;
26590 parser
->object_scope
= NULL_TREE
;
26596 /* Returns TRUE if the upcoming token sequence is the start of a
26597 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26598 declarator is preceded by the `friend' specifier. */
26601 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
26603 bool constructor_p
;
26604 bool outside_class_specifier_p
;
26605 tree nested_name_specifier
;
26606 cp_token
*next_token
;
26608 /* The common case is that this is not a constructor declarator, so
26609 try to avoid doing lots of work if at all possible. It's not
26610 valid declare a constructor at function scope. */
26611 if (parser
->in_function_body
)
26613 /* And only certain tokens can begin a constructor declarator. */
26614 next_token
= cp_lexer_peek_token (parser
->lexer
);
26615 if (next_token
->type
!= CPP_NAME
26616 && next_token
->type
!= CPP_SCOPE
26617 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
26618 && next_token
->type
!= CPP_TEMPLATE_ID
)
26621 /* Parse tentatively; we are going to roll back all of the tokens
26623 cp_parser_parse_tentatively (parser
);
26624 /* Assume that we are looking at a constructor declarator. */
26625 constructor_p
= true;
26627 /* Look for the optional `::' operator. */
26628 cp_parser_global_scope_opt (parser
,
26629 /*current_scope_valid_p=*/false);
26630 /* Look for the nested-name-specifier. */
26631 nested_name_specifier
26632 = (cp_parser_nested_name_specifier_opt (parser
,
26633 /*typename_keyword_p=*/false,
26634 /*check_dependency_p=*/false,
26636 /*is_declaration=*/false));
26638 outside_class_specifier_p
= (!at_class_scope_p ()
26639 || !TYPE_BEING_DEFINED (current_class_type
)
26642 /* Outside of a class-specifier, there must be a
26643 nested-name-specifier. Except in C++17 mode, where we
26644 might be declaring a guiding declaration. */
26645 if (!nested_name_specifier
&& outside_class_specifier_p
26646 && cxx_dialect
< cxx17
)
26647 constructor_p
= false;
26648 else if (nested_name_specifier
== error_mark_node
)
26649 constructor_p
= false;
26651 /* If we have a class scope, this is easy; DR 147 says that S::S always
26652 names the constructor, and no other qualified name could. */
26653 if (constructor_p
&& nested_name_specifier
26654 && CLASS_TYPE_P (nested_name_specifier
))
26656 tree id
= cp_parser_unqualified_id (parser
,
26657 /*template_keyword_p=*/false,
26658 /*check_dependency_p=*/false,
26659 /*declarator_p=*/true,
26660 /*optional_p=*/false);
26661 if (is_overloaded_fn (id
))
26662 id
= DECL_NAME (get_first_fn (id
));
26663 if (!constructor_name_p (id
, nested_name_specifier
))
26664 constructor_p
= false;
26666 /* If we still think that this might be a constructor-declarator,
26667 look for a class-name. */
26668 else if (constructor_p
)
26672 template <typename T> struct S {
26676 we must recognize that the nested `S' names a class. */
26677 if (cxx_dialect
>= cxx17
)
26678 cp_parser_parse_tentatively (parser
);
26681 type_decl
= cp_parser_class_name (parser
,
26682 /*typename_keyword_p=*/false,
26683 /*template_keyword_p=*/false,
26685 /*check_dependency_p=*/false,
26686 /*class_head_p=*/false,
26687 /*is_declaration=*/false);
26689 if (cxx_dialect
>= cxx17
26690 && !cp_parser_parse_definitely (parser
))
26692 type_decl
= NULL_TREE
;
26693 tree tmpl
= cp_parser_template_name (parser
,
26694 /*template_keyword*/false,
26695 /*check_dependency_p*/false,
26696 /*is_declaration*/false,
26698 /*is_identifier*/NULL
);
26699 if (DECL_CLASS_TEMPLATE_P (tmpl
)
26700 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
26701 /* It's a deduction guide, return true. */;
26703 cp_parser_simulate_error (parser
);
26706 /* If there was no class-name, then this is not a constructor.
26707 Otherwise, if we are in a class-specifier and we aren't
26708 handling a friend declaration, check that its type matches
26709 current_class_type (c++/38313). Note: error_mark_node
26710 is left alone for error recovery purposes. */
26711 constructor_p
= (!cp_parser_error_occurred (parser
)
26712 && (outside_class_specifier_p
26713 || type_decl
== NULL_TREE
26714 || type_decl
== error_mark_node
26715 || same_type_p (current_class_type
,
26716 TREE_TYPE (type_decl
))));
26718 /* If we're still considering a constructor, we have to see a `(',
26719 to begin the parameter-declaration-clause, followed by either a
26720 `)', an `...', or a decl-specifier. We need to check for a
26721 type-specifier to avoid being fooled into thinking that:
26725 is a constructor. (It is actually a function named `f' that
26726 takes one parameter (of type `int') and returns a value of type
26729 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26730 constructor_p
= false;
26733 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
26734 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
26735 /* A parameter declaration begins with a decl-specifier,
26736 which is either the "attribute" keyword, a storage class
26737 specifier, or (usually) a type-specifier. */
26738 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
26741 tree pushed_scope
= NULL_TREE
;
26742 unsigned saved_num_template_parameter_lists
;
26744 /* Names appearing in the type-specifier should be looked up
26745 in the scope of the class. */
26746 if (current_class_type
)
26748 else if (type_decl
)
26750 type
= TREE_TYPE (type_decl
);
26751 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26753 type
= resolve_typename_type (type
,
26754 /*only_current_p=*/false);
26755 if (TREE_CODE (type
) == TYPENAME_TYPE
)
26757 cp_parser_abort_tentative_parse (parser
);
26761 pushed_scope
= push_scope (type
);
26764 /* Inside the constructor parameter list, surrounding
26765 template-parameter-lists do not apply. */
26766 saved_num_template_parameter_lists
26767 = parser
->num_template_parameter_lists
;
26768 parser
->num_template_parameter_lists
= 0;
26770 /* Look for the type-specifier. */
26771 cp_parser_type_specifier (parser
,
26772 CP_PARSER_FLAGS_NONE
,
26773 /*decl_specs=*/NULL
,
26774 /*is_declarator=*/true,
26775 /*declares_class_or_enum=*/NULL
,
26776 /*is_cv_qualifier=*/NULL
);
26778 parser
->num_template_parameter_lists
26779 = saved_num_template_parameter_lists
;
26781 /* Leave the scope of the class. */
26783 pop_scope (pushed_scope
);
26785 constructor_p
= !cp_parser_error_occurred (parser
);
26789 /* We did not really want to consume any tokens. */
26790 cp_parser_abort_tentative_parse (parser
);
26792 return constructor_p
;
26795 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26796 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26797 they must be performed once we are in the scope of the function.
26799 Returns the function defined. */
26802 cp_parser_function_definition_from_specifiers_and_declarator
26803 (cp_parser
* parser
,
26804 cp_decl_specifier_seq
*decl_specifiers
,
26806 const cp_declarator
*declarator
)
26811 /* Begin the function-definition. */
26812 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
26814 /* The things we're about to see are not directly qualified by any
26815 template headers we've seen thus far. */
26816 reset_specialization ();
26818 /* If there were names looked up in the decl-specifier-seq that we
26819 did not check, check them now. We must wait until we are in the
26820 scope of the function to perform the checks, since the function
26821 might be a friend. */
26822 perform_deferred_access_checks (tf_warning_or_error
);
26826 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
26827 parser
->omp_declare_simd
= NULL
;
26828 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
26829 parser
->oacc_routine
= NULL
;
26834 /* Skip the entire function. */
26835 cp_parser_skip_to_end_of_block_or_statement (parser
);
26836 fn
= error_mark_node
;
26838 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
26840 /* Seen already, skip it. An error message has already been output. */
26841 cp_parser_skip_to_end_of_block_or_statement (parser
);
26842 fn
= current_function_decl
;
26843 current_function_decl
= NULL_TREE
;
26844 /* If this is a function from a class, pop the nested class. */
26845 if (current_class_name
)
26846 pop_nested_class ();
26851 if (DECL_DECLARED_INLINE_P (current_function_decl
))
26852 tv
= TV_PARSE_INLINE
;
26854 tv
= TV_PARSE_FUNC
;
26856 fn
= cp_parser_function_definition_after_declarator (parser
,
26857 /*inline_p=*/false);
26864 /* Parse the part of a function-definition that follows the
26865 declarator. INLINE_P is TRUE iff this function is an inline
26866 function defined within a class-specifier.
26868 Returns the function defined. */
26871 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
26875 bool saved_in_unbraced_linkage_specification_p
;
26876 bool saved_in_function_body
;
26877 unsigned saved_num_template_parameter_lists
;
26879 bool fully_implicit_function_template_p
26880 = parser
->fully_implicit_function_template_p
;
26881 parser
->fully_implicit_function_template_p
= false;
26882 tree implicit_template_parms
26883 = parser
->implicit_template_parms
;
26884 parser
->implicit_template_parms
= 0;
26885 cp_binding_level
* implicit_template_scope
26886 = parser
->implicit_template_scope
;
26887 parser
->implicit_template_scope
= 0;
26889 saved_in_function_body
= parser
->in_function_body
;
26890 parser
->in_function_body
= true;
26891 /* If the next token is `return', then the code may be trying to
26892 make use of the "named return value" extension that G++ used to
26894 token
= cp_lexer_peek_token (parser
->lexer
);
26895 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
26897 /* Consume the `return' keyword. */
26898 cp_lexer_consume_token (parser
->lexer
);
26899 /* Look for the identifier that indicates what value is to be
26901 cp_parser_identifier (parser
);
26902 /* Issue an error message. */
26903 error_at (token
->location
,
26904 "named return values are no longer supported");
26905 /* Skip tokens until we reach the start of the function body. */
26908 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26909 if (token
->type
== CPP_OPEN_BRACE
26910 || token
->type
== CPP_EOF
26911 || token
->type
== CPP_PRAGMA_EOL
)
26913 cp_lexer_consume_token (parser
->lexer
);
26916 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26917 anything declared inside `f'. */
26918 saved_in_unbraced_linkage_specification_p
26919 = parser
->in_unbraced_linkage_specification_p
;
26920 parser
->in_unbraced_linkage_specification_p
= false;
26921 /* Inside the function, surrounding template-parameter-lists do not
26923 saved_num_template_parameter_lists
26924 = parser
->num_template_parameter_lists
;
26925 parser
->num_template_parameter_lists
= 0;
26927 /* If the next token is `try', `__transaction_atomic', or
26928 `__transaction_relaxed`, then we are looking at either function-try-block
26929 or function-transaction-block. Note that all of these include the
26931 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
26932 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
26933 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
26934 RID_TRANSACTION_RELAXED
))
26935 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
26936 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26937 cp_parser_function_try_block (parser
);
26939 cp_parser_ctor_initializer_opt_and_function_body
26940 (parser
, /*in_function_try_block=*/false);
26942 /* Finish the function. */
26943 fn
= finish_function (inline_p
);
26944 /* Generate code for it, if necessary. */
26945 expand_or_defer_fn (fn
);
26946 /* Restore the saved values. */
26947 parser
->in_unbraced_linkage_specification_p
26948 = saved_in_unbraced_linkage_specification_p
;
26949 parser
->num_template_parameter_lists
26950 = saved_num_template_parameter_lists
;
26951 parser
->in_function_body
= saved_in_function_body
;
26953 parser
->fully_implicit_function_template_p
26954 = fully_implicit_function_template_p
;
26955 parser
->implicit_template_parms
26956 = implicit_template_parms
;
26957 parser
->implicit_template_scope
26958 = implicit_template_scope
;
26960 if (parser
->fully_implicit_function_template_p
)
26961 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
26966 /* Parse a template-declaration body (following argument list). */
26969 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
26970 tree parameter_list
,
26973 tree decl
= NULL_TREE
;
26974 bool friend_p
= false;
26976 /* We just processed one more parameter list. */
26977 ++parser
->num_template_parameter_lists
;
26979 /* Get the deferred access checks from the parameter list. These
26980 will be checked once we know what is being declared, as for a
26981 member template the checks must be performed in the scope of the
26982 class containing the member. */
26983 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
26985 /* Tentatively parse for a new template parameter list, which can either be
26986 the template keyword or a template introduction. */
26987 if (cp_parser_template_declaration_after_export (parser
, member_p
))
26989 else if (cxx_dialect
>= cxx11
26990 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26991 decl
= cp_parser_alias_declaration (parser
);
26994 /* There are no access checks when parsing a template, as we do not
26995 know if a specialization will be a friend. */
26996 push_deferring_access_checks (dk_no_check
);
26997 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26998 decl
= cp_parser_single_declaration (parser
,
27001 /*explicit_specialization_p=*/false,
27003 pop_deferring_access_checks ();
27005 /* If this is a member template declaration, let the front
27007 if (member_p
&& !friend_p
&& decl
)
27009 if (TREE_CODE (decl
) == TYPE_DECL
)
27010 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
27012 decl
= finish_member_template_decl (decl
);
27014 else if (friend_p
&& decl
27015 && DECL_DECLARES_TYPE_P (decl
))
27016 make_friend_class (current_class_type
, TREE_TYPE (decl
),
27017 /*complain=*/true);
27019 /* We are done with the current parameter list. */
27020 --parser
->num_template_parameter_lists
;
27022 pop_deferring_access_checks ();
27025 finish_template_decl (parameter_list
);
27027 /* Check the template arguments for a literal operator template. */
27029 && DECL_DECLARES_FUNCTION_P (decl
)
27030 && UDLIT_OPER_P (DECL_NAME (decl
)))
27033 if (parameter_list
== NULL_TREE
)
27037 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
27038 if (num_parms
== 1)
27040 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
27041 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27042 if (TREE_TYPE (parm
) != char_type_node
27043 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27046 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
27048 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
27049 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
27050 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
27051 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
27052 if (parm
== error_mark_node
27053 || TREE_TYPE (parm
) != TREE_TYPE (type
)
27054 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
27062 if (cxx_dialect
>= cxx14
)
27063 error ("literal operator template %qD has invalid parameter list."
27064 " Expected non-type template argument pack <char...>"
27065 " or <typename CharT, CharT...>",
27068 error ("literal operator template %qD has invalid parameter list."
27069 " Expected non-type template argument pack <char...>",
27074 /* Register member declarations. */
27075 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
27076 finish_member_declaration (decl
);
27077 /* If DECL is a function template, we must return to parse it later.
27078 (Even though there is no definition, there might be default
27079 arguments that need handling.) */
27080 if (member_p
&& decl
27081 && DECL_DECLARES_FUNCTION_P (decl
))
27082 vec_safe_push (unparsed_funs_with_definitions
, decl
);
27085 /* Parse a template introduction header for a template-declaration. Returns
27086 false if tentative parse fails. */
27089 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
27091 cp_parser_parse_tentatively (parser
);
27093 tree saved_scope
= parser
->scope
;
27094 tree saved_object_scope
= parser
->object_scope
;
27095 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27097 /* Look for the optional `::' operator. */
27098 cp_parser_global_scope_opt (parser
,
27099 /*current_scope_valid_p=*/false);
27100 /* Look for the nested-name-specifier. */
27101 cp_parser_nested_name_specifier_opt (parser
,
27102 /*typename_keyword_p=*/false,
27103 /*check_dependency_p=*/true,
27105 /*is_declaration=*/false);
27107 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27108 tree concept_name
= cp_parser_identifier (parser
);
27110 /* Look up the concept for which we will be matching
27111 template parameters. */
27112 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
27114 parser
->scope
= saved_scope
;
27115 parser
->object_scope
= saved_object_scope
;
27116 parser
->qualifying_scope
= saved_qualifying_scope
;
27118 if (concept_name
== error_mark_node
)
27119 cp_parser_simulate_error (parser
);
27121 /* Look for opening brace for introduction. */
27122 matching_braces braces
;
27123 braces
.require_open (parser
);
27125 if (!cp_parser_parse_definitely (parser
))
27128 push_deferring_access_checks (dk_deferred
);
27130 /* Build vector of placeholder parameters and grab
27131 matching identifiers. */
27132 tree introduction_list
= cp_parser_introduction_list (parser
);
27134 /* The introduction-list shall not be empty. */
27135 int nargs
= TREE_VEC_LENGTH (introduction_list
);
27138 error ("empty introduction-list");
27142 /* Look for closing brace for introduction. */
27143 if (!braces
.require_close (parser
))
27146 if (tmpl_decl
== error_mark_node
)
27148 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
27153 /* Build and associate the constraint. */
27154 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
27155 if (parms
&& parms
!= error_mark_node
)
27157 cp_parser_template_declaration_after_parameters (parser
, parms
,
27162 error_at (token
->location
, "no matching concept for template-introduction");
27166 /* Parse a normal template-declaration following the template keyword. */
27169 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
27171 tree parameter_list
;
27172 bool need_lang_pop
;
27173 location_t location
= input_location
;
27175 /* Look for the `<' token. */
27176 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
27178 if (at_class_scope_p () && current_function_decl
)
27180 /* 14.5.2.2 [temp.mem]
27182 A local class shall not have member templates. */
27183 error_at (location
,
27184 "invalid declaration of member template in local class");
27185 cp_parser_skip_to_end_of_block_or_statement (parser
);
27190 A template ... shall not have C linkage. */
27191 if (current_lang_name
== lang_name_c
)
27193 error_at (location
, "template with C linkage");
27194 maybe_show_extern_c_location ();
27195 /* Give it C++ linkage to avoid confusing other parts of the
27197 push_lang_context (lang_name_cplusplus
);
27198 need_lang_pop
= true;
27201 need_lang_pop
= false;
27203 /* We cannot perform access checks on the template parameter
27204 declarations until we know what is being declared, just as we
27205 cannot check the decl-specifier list. */
27206 push_deferring_access_checks (dk_deferred
);
27208 /* If the next token is `>', then we have an invalid
27209 specialization. Rather than complain about an invalid template
27210 parameter, issue an error message here. */
27211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
27213 cp_parser_error (parser
, "invalid explicit specialization");
27214 begin_specialization ();
27215 parameter_list
= NULL_TREE
;
27219 /* Parse the template parameters. */
27220 parameter_list
= cp_parser_template_parameter_list (parser
);
27223 /* Look for the `>'. */
27224 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27226 /* Manage template requirements */
27229 tree reqs
= get_shorthand_constraints (current_template_parms
);
27230 if (tree r
= cp_parser_requires_clause_opt (parser
))
27231 reqs
= conjoin_constraints (reqs
, normalize_expression (r
));
27232 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
27235 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
27238 /* For the erroneous case of a template with C linkage, we pushed an
27239 implicit C++ linkage scope; exit that scope now. */
27241 pop_lang_context ();
27244 /* Parse a template-declaration, assuming that the `export' (and
27245 `extern') keywords, if present, has already been scanned. MEMBER_P
27246 is as for cp_parser_template_declaration. */
27249 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
27251 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27253 cp_lexer_consume_token (parser
->lexer
);
27254 cp_parser_explicit_template_declaration (parser
, member_p
);
27257 else if (flag_concepts
)
27258 return cp_parser_template_introduction (parser
, member_p
);
27263 /* Perform the deferred access checks from a template-parameter-list.
27264 CHECKS is a TREE_LIST of access checks, as returned by
27265 get_deferred_access_checks. */
27268 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
27270 ++processing_template_parmlist
;
27271 perform_access_checks (checks
, tf_warning_or_error
);
27272 --processing_template_parmlist
;
27275 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27276 `function-definition' sequence that follows a template header.
27277 If MEMBER_P is true, this declaration appears in a class scope.
27279 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27280 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27283 cp_parser_single_declaration (cp_parser
* parser
,
27284 vec
<deferred_access_check
, va_gc
> *checks
,
27286 bool explicit_specialization_p
,
27289 int declares_class_or_enum
;
27290 tree decl
= NULL_TREE
;
27291 cp_decl_specifier_seq decl_specifiers
;
27292 bool function_definition_p
= false;
27293 cp_token
*decl_spec_token_start
;
27295 /* This function is only used when processing a template
27297 gcc_assert (innermost_scope_kind () == sk_template_parms
27298 || innermost_scope_kind () == sk_template_spec
);
27300 /* Defer access checks until we know what is being declared. */
27301 push_deferring_access_checks (dk_deferred
);
27303 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27305 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
27306 cp_parser_decl_specifier_seq (parser
,
27307 CP_PARSER_FLAGS_OPTIONAL
,
27309 &declares_class_or_enum
);
27311 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
27313 /* There are no template typedefs. */
27314 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
27316 error_at (decl_spec_token_start
->location
,
27317 "template declaration of %<typedef%>");
27318 decl
= error_mark_node
;
27321 /* Gather up the access checks that occurred the
27322 decl-specifier-seq. */
27323 stop_deferring_access_checks ();
27325 /* Check for the declaration of a template class. */
27326 if (declares_class_or_enum
)
27328 if (cp_parser_declares_only_class_p (parser
)
27329 || (declares_class_or_enum
& 2))
27331 // If this is a declaration, but not a definition, associate
27332 // any constraints with the type declaration. Constraints
27333 // are associated with definitions in cp_parser_class_specifier.
27334 if (declares_class_or_enum
== 1)
27335 associate_classtype_constraints (decl_specifiers
.type
);
27337 decl
= shadow_tag (&decl_specifiers
);
27342 friend template <typename T> struct A<T>::B;
27345 A<T>::B will be represented by a TYPENAME_TYPE, and
27346 therefore not recognized by shadow_tag. */
27347 if (friend_p
&& *friend_p
27349 && decl_specifiers
.type
27350 && TYPE_P (decl_specifiers
.type
))
27351 decl
= decl_specifiers
.type
;
27353 if (decl
&& decl
!= error_mark_node
)
27354 decl
= TYPE_NAME (decl
);
27356 decl
= error_mark_node
;
27358 /* Perform access checks for template parameters. */
27359 cp_parser_perform_template_parameter_access_checks (checks
);
27361 /* Give a helpful diagnostic for
27362 template <class T> struct A { } a;
27363 if we aren't already recovering from an error. */
27364 if (!cp_parser_declares_only_class_p (parser
)
27367 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27368 "a class template declaration must not declare "
27370 cp_parser_skip_to_end_of_block_or_statement (parser
);
27376 /* Complain about missing 'typename' or other invalid type names. */
27377 if (!decl_specifiers
.any_type_specifiers_p
27378 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
27380 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27381 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27382 the rest of this declaration. */
27383 decl
= error_mark_node
;
27387 /* If it's not a template class, try for a template function. If
27388 the next token is a `;', then this declaration does not declare
27389 anything. But, if there were errors in the decl-specifiers, then
27390 the error might well have come from an attempted class-specifier.
27391 In that case, there's no need to warn about a missing declarator. */
27393 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
27394 || decl_specifiers
.type
!= error_mark_node
))
27396 decl
= cp_parser_init_declarator (parser
,
27399 /*function_definition_allowed_p=*/true,
27401 declares_class_or_enum
,
27402 &function_definition_p
,
27405 /* 7.1.1-1 [dcl.stc]
27407 A storage-class-specifier shall not be specified in an explicit
27408 specialization... */
27410 && explicit_specialization_p
27411 && decl_specifiers
.storage_class
!= sc_none
)
27413 error_at (decl_spec_token_start
->location
,
27414 "explicit template specialization cannot have a storage class");
27415 decl
= error_mark_node
;
27418 if (decl
&& VAR_P (decl
))
27419 check_template_variable (decl
);
27422 /* Look for a trailing `;' after the declaration. */
27423 if (!function_definition_p
27424 && (decl
== error_mark_node
27425 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
27426 cp_parser_skip_to_end_of_block_or_statement (parser
);
27429 pop_deferring_access_checks ();
27431 /* Clear any current qualification; whatever comes next is the start
27432 of something new. */
27433 parser
->scope
= NULL_TREE
;
27434 parser
->qualifying_scope
= NULL_TREE
;
27435 parser
->object_scope
= NULL_TREE
;
27440 /* Parse a cast-expression that is not the operand of a unary "&". */
27443 cp_parser_simple_cast_expression (cp_parser
*parser
)
27445 return cp_parser_cast_expression (parser
, /*address_p=*/false,
27446 /*cast_p=*/false, /*decltype*/false, NULL
);
27449 /* Parse a functional cast to TYPE. Returns an expression
27450 representing the cast. */
27453 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
27455 vec
<tree
, va_gc
> *vec
;
27456 tree expression_list
;
27460 location_t start_loc
= input_location
;
27463 type
= error_mark_node
;
27465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27467 cp_lexer_set_source_position (parser
->lexer
);
27468 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27469 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
27470 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
27471 if (TREE_CODE (type
) == TYPE_DECL
)
27472 type
= TREE_TYPE (type
);
27474 cast
= finish_compound_literal (type
, expression_list
,
27475 tf_warning_or_error
, fcl_functional
);
27476 /* Create a location of the form:
27479 with caret == start at the start of the type name,
27480 finishing at the closing brace. */
27481 location_t finish_loc
27482 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27483 location_t combined_loc
= make_location (start_loc
, start_loc
,
27485 cast
.set_location (combined_loc
);
27490 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27492 /*allow_expansion_p=*/true,
27493 /*non_constant_p=*/NULL
);
27495 expression_list
= error_mark_node
;
27498 expression_list
= build_tree_list_vec (vec
);
27499 release_tree_vector (vec
);
27502 cast
= build_functional_cast (type
, expression_list
,
27503 tf_warning_or_error
);
27504 /* [expr.const]/1: In an integral constant expression "only type
27505 conversions to integral or enumeration type can be used". */
27506 if (TREE_CODE (type
) == TYPE_DECL
)
27507 type
= TREE_TYPE (type
);
27508 if (cast
!= error_mark_node
27509 && !cast_valid_in_integral_constant_expression_p (type
)
27510 && cp_parser_non_integral_constant_expression (parser
,
27512 return error_mark_node
;
27514 /* Create a location of the form:
27517 with caret == start at the start of the type name,
27518 finishing at the closing paren. */
27519 location_t finish_loc
27520 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
27521 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
27522 cast
.set_location (combined_loc
);
27526 /* Save the tokens that make up the body of a member function defined
27527 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27528 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27529 specifiers applied to the declaration. Returns the FUNCTION_DECL
27530 for the member function. */
27533 cp_parser_save_member_function_body (cp_parser
* parser
,
27534 cp_decl_specifier_seq
*decl_specifiers
,
27535 cp_declarator
*declarator
,
27541 bool function_try_block
= false;
27543 /* Create the FUNCTION_DECL. */
27544 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
27545 cp_finalize_omp_declare_simd (parser
, fn
);
27546 cp_finalize_oacc_routine (parser
, fn
, true);
27547 /* If something went badly wrong, bail out now. */
27548 if (fn
== error_mark_node
)
27550 /* If there's a function-body, skip it. */
27551 if (cp_parser_token_starts_function_definition_p
27552 (cp_lexer_peek_token (parser
->lexer
)))
27553 cp_parser_skip_to_end_of_block_or_statement (parser
);
27554 return error_mark_node
;
27557 /* Remember it, if there default args to post process. */
27558 cp_parser_save_default_args (parser
, fn
);
27560 /* Save away the tokens that make up the body of the
27562 first
= parser
->lexer
->next_token
;
27564 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
27565 cp_lexer_consume_token (parser
->lexer
);
27566 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
27567 RID_TRANSACTION_ATOMIC
))
27569 cp_lexer_consume_token (parser
->lexer
);
27570 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
27572 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
27573 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
27574 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
27575 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
27576 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
27578 cp_lexer_consume_token (parser
->lexer
);
27579 cp_lexer_consume_token (parser
->lexer
);
27580 cp_lexer_consume_token (parser
->lexer
);
27581 cp_lexer_consume_token (parser
->lexer
);
27582 cp_lexer_consume_token (parser
->lexer
);
27585 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27586 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
27588 cp_lexer_consume_token (parser
->lexer
);
27589 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27594 /* Handle function try blocks. */
27595 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
27597 cp_lexer_consume_token (parser
->lexer
);
27598 function_try_block
= true;
27600 /* We can have braced-init-list mem-initializers before the fn body. */
27601 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27603 cp_lexer_consume_token (parser
->lexer
);
27604 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
27606 /* cache_group will stop after an un-nested { } pair, too. */
27607 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
27610 /* variadic mem-inits have ... after the ')'. */
27611 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27612 cp_lexer_consume_token (parser
->lexer
);
27615 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27616 /* Handle function try blocks. */
27617 if (function_try_block
)
27618 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
27619 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27620 last
= parser
->lexer
->next_token
;
27622 /* Save away the inline definition; we will process it when the
27623 class is complete. */
27624 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
27625 DECL_PENDING_INLINE_P (fn
) = 1;
27627 /* We need to know that this was defined in the class, so that
27628 friend templates are handled correctly. */
27629 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
27631 /* Add FN to the queue of functions to be parsed later. */
27632 vec_safe_push (unparsed_funs_with_definitions
, fn
);
27637 /* Save the tokens that make up the in-class initializer for a non-static
27638 data member. Returns a DEFAULT_ARG. */
27641 cp_parser_save_nsdmi (cp_parser
* parser
)
27643 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
27646 /* Parse a template-argument-list, as well as the trailing ">" (but
27647 not the opening "<"). See cp_parser_template_argument_list for the
27651 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
27655 tree saved_qualifying_scope
;
27656 tree saved_object_scope
;
27657 bool saved_greater_than_is_operator_p
;
27658 int saved_unevaluated_operand
;
27659 int saved_inhibit_evaluation_warnings
;
27663 When parsing a template-id, the first non-nested `>' is taken as
27664 the end of the template-argument-list rather than a greater-than
27666 saved_greater_than_is_operator_p
27667 = parser
->greater_than_is_operator_p
;
27668 parser
->greater_than_is_operator_p
= false;
27669 /* Parsing the argument list may modify SCOPE, so we save it
27671 saved_scope
= parser
->scope
;
27672 saved_qualifying_scope
= parser
->qualifying_scope
;
27673 saved_object_scope
= parser
->object_scope
;
27674 /* We need to evaluate the template arguments, even though this
27675 template-id may be nested within a "sizeof". */
27676 saved_unevaluated_operand
= cp_unevaluated_operand
;
27677 cp_unevaluated_operand
= 0;
27678 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
27679 c_inhibit_evaluation_warnings
= 0;
27680 /* Parse the template-argument-list itself. */
27681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
27682 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27683 arguments
= NULL_TREE
;
27685 arguments
= cp_parser_template_argument_list (parser
);
27686 /* Look for the `>' that ends the template-argument-list. If we find
27687 a '>>' instead, it's probably just a typo. */
27688 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
27690 if (cxx_dialect
!= cxx98
)
27692 /* In C++0x, a `>>' in a template argument list or cast
27693 expression is considered to be two separate `>'
27694 tokens. So, change the current token to a `>', but don't
27695 consume it: it will be consumed later when the outer
27696 template argument list (or cast expression) is parsed.
27697 Note that this replacement of `>' for `>>' is necessary
27698 even if we are parsing tentatively: in the tentative
27699 case, after calling
27700 cp_parser_enclosed_template_argument_list we will always
27701 throw away all of the template arguments and the first
27702 closing `>', either because the template argument list
27703 was erroneous or because we are replacing those tokens
27704 with a CPP_TEMPLATE_ID token. The second `>' (which will
27705 not have been thrown away) is needed either to close an
27706 outer template argument list or to complete a new-style
27708 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27709 token
->type
= CPP_GREATER
;
27711 else if (!saved_greater_than_is_operator_p
)
27713 /* If we're in a nested template argument list, the '>>' has
27714 to be a typo for '> >'. We emit the error message, but we
27715 continue parsing and we push a '>' as next token, so that
27716 the argument list will be parsed correctly. Note that the
27717 global source location is still on the token before the
27718 '>>', so we need to say explicitly where we want it. */
27719 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27720 gcc_rich_location
richloc (token
->location
);
27721 richloc
.add_fixit_replace ("> >");
27722 error_at (&richloc
, "%<>>%> should be %<> >%> "
27723 "within a nested template argument list");
27725 token
->type
= CPP_GREATER
;
27729 /* If this is not a nested template argument list, the '>>'
27730 is a typo for '>'. Emit an error message and continue.
27731 Same deal about the token location, but here we can get it
27732 right by consuming the '>>' before issuing the diagnostic. */
27733 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27734 error_at (token
->location
,
27735 "spurious %<>>%>, use %<>%> to terminate "
27736 "a template argument list");
27740 cp_parser_skip_to_end_of_template_parameter_list (parser
);
27741 /* The `>' token might be a greater-than operator again now. */
27742 parser
->greater_than_is_operator_p
27743 = saved_greater_than_is_operator_p
;
27744 /* Restore the SAVED_SCOPE. */
27745 parser
->scope
= saved_scope
;
27746 parser
->qualifying_scope
= saved_qualifying_scope
;
27747 parser
->object_scope
= saved_object_scope
;
27748 cp_unevaluated_operand
= saved_unevaluated_operand
;
27749 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
27754 /* MEMBER_FUNCTION is a member function, or a friend. If default
27755 arguments, or the body of the function have not yet been parsed,
27759 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
27761 timevar_push (TV_PARSE_INMETH
);
27762 /* If this member is a template, get the underlying
27764 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
27765 member_function
= DECL_TEMPLATE_RESULT (member_function
);
27767 /* There should not be any class definitions in progress at this
27768 point; the bodies of members are only parsed outside of all class
27770 gcc_assert (parser
->num_classes_being_defined
== 0);
27771 /* While we're parsing the member functions we might encounter more
27772 classes. We want to handle them right away, but we don't want
27773 them getting mixed up with functions that are currently in the
27775 push_unparsed_function_queues (parser
);
27777 /* Make sure that any template parameters are in scope. */
27778 maybe_begin_member_template_processing (member_function
);
27780 /* If the body of the function has not yet been parsed, parse it
27782 if (DECL_PENDING_INLINE_P (member_function
))
27784 tree function_scope
;
27785 cp_token_cache
*tokens
;
27787 /* The function is no longer pending; we are processing it. */
27788 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
27789 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
27790 DECL_PENDING_INLINE_P (member_function
) = 0;
27792 /* If this is a local class, enter the scope of the containing
27794 function_scope
= current_function_decl
;
27795 if (function_scope
)
27796 push_function_context ();
27798 /* Push the body of the function onto the lexer stack. */
27799 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27801 /* Let the front end know that we going to be defining this
27803 start_preparsed_function (member_function
, NULL_TREE
,
27804 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
27806 /* Don't do access checking if it is a templated function. */
27807 if (processing_template_decl
)
27808 push_deferring_access_checks (dk_no_check
);
27810 /* #pragma omp declare reduction needs special parsing. */
27811 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
27813 parser
->lexer
->in_pragma
= true;
27814 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
27815 finish_function (/*inline_p=*/true);
27816 cp_check_omp_declare_reduction (member_function
);
27819 /* Now, parse the body of the function. */
27820 cp_parser_function_definition_after_declarator (parser
,
27821 /*inline_p=*/true);
27823 if (processing_template_decl
)
27824 pop_deferring_access_checks ();
27826 /* Leave the scope of the containing function. */
27827 if (function_scope
)
27828 pop_function_context ();
27829 cp_parser_pop_lexer (parser
);
27832 /* Remove any template parameters from the symbol table. */
27833 maybe_end_member_template_processing ();
27835 /* Restore the queue. */
27836 pop_unparsed_function_queues (parser
);
27837 timevar_pop (TV_PARSE_INMETH
);
27840 /* If DECL contains any default args, remember it on the unparsed
27841 functions queue. */
27844 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
27848 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
27850 probe
= TREE_CHAIN (probe
))
27851 if (TREE_PURPOSE (probe
))
27853 cp_default_arg_entry entry
= {current_class_type
, decl
};
27854 vec_safe_push (unparsed_funs_with_default_args
, entry
);
27859 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27860 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27861 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27862 from the parameter-type-list. */
27865 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
27866 tree default_arg
, tree parmtype
)
27868 cp_token_cache
*tokens
;
27872 if (default_arg
== error_mark_node
)
27873 return error_mark_node
;
27875 /* Push the saved tokens for the default argument onto the parser's
27877 tokens
= DEFARG_TOKENS (default_arg
);
27878 cp_parser_push_lexer_for_tokens (parser
, tokens
);
27880 start_lambda_scope (decl
);
27882 /* Parse the default argument. */
27883 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
27884 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
27885 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27887 finish_lambda_scope ();
27889 if (parsed_arg
== error_mark_node
)
27890 cp_parser_skip_to_end_of_statement (parser
);
27892 if (!processing_template_decl
)
27894 /* In a non-template class, check conversions now. In a template,
27895 we'll wait and instantiate these as needed. */
27896 if (TREE_CODE (decl
) == PARM_DECL
)
27897 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
27898 tf_warning_or_error
);
27899 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
27900 parsed_arg
= error_mark_node
;
27902 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
27905 /* If the token stream has not been completely used up, then
27906 there was extra junk after the end of the default
27908 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
27910 if (TREE_CODE (decl
) == PARM_DECL
)
27911 cp_parser_error (parser
, "expected %<,%>");
27913 cp_parser_error (parser
, "expected %<;%>");
27916 /* Revert to the main lexer. */
27917 cp_parser_pop_lexer (parser
);
27922 /* FIELD is a non-static data member with an initializer which we saved for
27923 later; parse it now. */
27926 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
27930 maybe_begin_member_template_processing (field
);
27932 push_unparsed_function_queues (parser
);
27933 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
27934 DECL_INITIAL (field
),
27936 pop_unparsed_function_queues (parser
);
27938 maybe_end_member_template_processing ();
27940 DECL_INITIAL (field
) = def
;
27943 /* FN is a FUNCTION_DECL which may contains a parameter with an
27944 unparsed DEFAULT_ARG. Parse the default args now. This function
27945 assumes that the current scope is the scope in which the default
27946 argument should be processed. */
27949 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
27951 bool saved_local_variables_forbidden_p
;
27952 tree parm
, parmdecl
;
27954 /* While we're parsing the default args, we might (due to the
27955 statement expression extension) encounter more classes. We want
27956 to handle them right away, but we don't want them getting mixed
27957 up with default args that are currently in the queue. */
27958 push_unparsed_function_queues (parser
);
27960 /* Local variable names (and the `this' keyword) may not appear
27961 in a default argument. */
27962 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
27963 parser
->local_variables_forbidden_p
= true;
27965 push_defarg_context (fn
);
27967 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
27968 parmdecl
= DECL_ARGUMENTS (fn
);
27969 parm
&& parm
!= void_list_node
;
27970 parm
= TREE_CHAIN (parm
),
27971 parmdecl
= DECL_CHAIN (parmdecl
))
27973 tree default_arg
= TREE_PURPOSE (parm
);
27975 vec
<tree
, va_gc
> *insts
;
27982 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
27983 /* This can happen for a friend declaration for a function
27984 already declared with default arguments. */
27988 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
27990 TREE_VALUE (parm
));
27991 TREE_PURPOSE (parm
) = parsed_arg
;
27993 /* Update any instantiations we've already created. */
27994 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
27995 vec_safe_iterate (insts
, ix
, ©
); ix
++)
27996 TREE_PURPOSE (copy
) = parsed_arg
;
27999 pop_defarg_context ();
28001 /* Make sure no default arg is missing. */
28002 check_default_args (fn
);
28004 /* Restore the state of local_variables_forbidden_p. */
28005 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
28007 /* Restore the queue. */
28008 pop_unparsed_function_queues (parser
);
28011 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28013 sizeof ... ( identifier )
28015 where the 'sizeof' token has already been consumed. */
28018 cp_parser_sizeof_pack (cp_parser
*parser
)
28020 /* Consume the `...'. */
28021 cp_lexer_consume_token (parser
->lexer
);
28022 maybe_warn_variadic_templates ();
28024 matching_parens parens
;
28025 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
28027 parens
.consume_open (parser
);
28029 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
28030 "%<sizeof...%> argument must be surrounded by parentheses");
28032 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28033 tree name
= cp_parser_identifier (parser
);
28034 if (name
== error_mark_node
)
28035 return error_mark_node
;
28036 /* The name is not qualified. */
28037 parser
->scope
= NULL_TREE
;
28038 parser
->qualifying_scope
= NULL_TREE
;
28039 parser
->object_scope
= NULL_TREE
;
28040 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
28041 if (expr
== error_mark_node
)
28042 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
28044 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
28045 expr
= TREE_TYPE (expr
);
28046 else if (TREE_CODE (expr
) == CONST_DECL
)
28047 expr
= DECL_INITIAL (expr
);
28048 expr
= make_pack_expansion (expr
);
28049 PACK_EXPANSION_SIZEOF_P (expr
) = true;
28052 parens
.require_close (parser
);
28057 /* Parse the operand of `sizeof' (or a similar operator). Returns
28058 either a TYPE or an expression, depending on the form of the
28059 input. The KEYWORD indicates which kind of expression we have
28063 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
28065 tree expr
= NULL_TREE
;
28066 const char *saved_message
;
28068 bool saved_integral_constant_expression_p
;
28069 bool saved_non_integral_constant_expression_p
;
28071 /* If it's a `...', then we are computing the length of a parameter
28073 if (keyword
== RID_SIZEOF
28074 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28075 return cp_parser_sizeof_pack (parser
);
28077 /* Types cannot be defined in a `sizeof' expression. Save away the
28079 saved_message
= parser
->type_definition_forbidden_message
;
28080 /* And create the new one. */
28081 tmp
= concat ("types may not be defined in %<",
28082 IDENTIFIER_POINTER (ridpointers
[keyword
]),
28083 "%> expressions", NULL
);
28084 parser
->type_definition_forbidden_message
= tmp
;
28086 /* The restrictions on constant-expressions do not apply inside
28087 sizeof expressions. */
28088 saved_integral_constant_expression_p
28089 = parser
->integral_constant_expression_p
;
28090 saved_non_integral_constant_expression_p
28091 = parser
->non_integral_constant_expression_p
;
28092 parser
->integral_constant_expression_p
= false;
28094 /* Do not actually evaluate the expression. */
28095 ++cp_unevaluated_operand
;
28096 ++c_inhibit_evaluation_warnings
;
28097 /* If it's a `(', then we might be looking at the type-id
28099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28101 tree type
= NULL_TREE
;
28103 /* We can't be sure yet whether we're looking at a type-id or an
28105 cp_parser_parse_tentatively (parser
);
28107 matching_parens parens
;
28108 parens
.consume_open (parser
);
28110 /* Note: as a GNU Extension, compound literals are considered
28111 postfix-expressions as they are in C99, so they are valid
28112 arguments to sizeof. See comment in cp_parser_cast_expression
28114 if (cp_parser_compound_literal_p (parser
))
28115 cp_parser_simulate_error (parser
);
28118 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
28119 parser
->in_type_id_in_expr_p
= true;
28120 /* Look for the type-id. */
28121 type
= cp_parser_type_id (parser
);
28122 /* Look for the closing `)'. */
28123 parens
.require_close (parser
);
28124 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
28127 /* If all went well, then we're done. */
28128 if (cp_parser_parse_definitely (parser
))
28130 cp_decl_specifier_seq decl_specs
;
28132 /* Build a trivial decl-specifier-seq. */
28133 clear_decl_specs (&decl_specs
);
28134 decl_specs
.type
= type
;
28136 /* Call grokdeclarator to figure out what type this is. */
28137 expr
= grokdeclarator (NULL
,
28141 /*attrlist=*/NULL
);
28145 /* If the type-id production did not work out, then we must be
28146 looking at the unary-expression production. */
28148 expr
= cp_parser_unary_expression (parser
);
28150 /* Go back to evaluating expressions. */
28151 --cp_unevaluated_operand
;
28152 --c_inhibit_evaluation_warnings
;
28154 /* Free the message we created. */
28156 /* And restore the old one. */
28157 parser
->type_definition_forbidden_message
= saved_message
;
28158 parser
->integral_constant_expression_p
28159 = saved_integral_constant_expression_p
;
28160 parser
->non_integral_constant_expression_p
28161 = saved_non_integral_constant_expression_p
;
28166 /* If the current declaration has no declarator, return true. */
28169 cp_parser_declares_only_class_p (cp_parser
*parser
)
28171 /* If the next token is a `;' or a `,' then there is no
28173 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28174 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
28177 /* Update the DECL_SPECS to reflect the storage class indicated by
28181 cp_parser_set_storage_class (cp_parser
*parser
,
28182 cp_decl_specifier_seq
*decl_specs
,
28186 cp_storage_class storage_class
;
28188 if (parser
->in_unbraced_linkage_specification_p
)
28190 error_at (token
->location
, "invalid use of %qD in linkage specification",
28191 ridpointers
[keyword
]);
28194 else if (decl_specs
->storage_class
!= sc_none
)
28196 decl_specs
->conflicting_specifiers_p
= true;
28200 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
28201 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
28202 && decl_specs
->gnu_thread_keyword_p
)
28204 pedwarn (decl_specs
->locations
[ds_thread
], 0,
28205 "%<__thread%> before %qD", ridpointers
[keyword
]);
28211 storage_class
= sc_auto
;
28214 storage_class
= sc_register
;
28217 storage_class
= sc_static
;
28220 storage_class
= sc_extern
;
28223 storage_class
= sc_mutable
;
28226 gcc_unreachable ();
28228 decl_specs
->storage_class
= storage_class
;
28229 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
28231 /* A storage class specifier cannot be applied alongside a typedef
28232 specifier. If there is a typedef specifier present then set
28233 conflicting_specifiers_p which will trigger an error later
28234 on in grokdeclarator. */
28235 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
28236 decl_specs
->conflicting_specifiers_p
= true;
28239 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28240 is true, the type is a class or enum definition. */
28243 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
28246 bool type_definition_p
)
28248 decl_specs
->any_specifiers_p
= true;
28250 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28251 (with, for example, in "typedef int wchar_t;") we remember that
28252 this is what happened. In system headers, we ignore these
28253 declarations so that G++ can work with system headers that are not
28255 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
28256 && !type_definition_p
28257 && (type_spec
== boolean_type_node
28258 || type_spec
== char16_type_node
28259 || type_spec
== char32_type_node
28260 || type_spec
== wchar_type_node
)
28261 && (decl_specs
->type
28262 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
28263 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
28264 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
28265 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
28267 decl_specs
->redefined_builtin_type
= type_spec
;
28268 set_and_check_decl_spec_loc (decl_specs
,
28269 ds_redefined_builtin_type_spec
,
28271 if (!decl_specs
->type
)
28273 decl_specs
->type
= type_spec
;
28274 decl_specs
->type_definition_p
= false;
28275 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
28278 else if (decl_specs
->type
)
28279 decl_specs
->multiple_types_p
= true;
28282 decl_specs
->type
= type_spec
;
28283 decl_specs
->type_definition_p
= type_definition_p
;
28284 decl_specs
->redefined_builtin_type
= NULL_TREE
;
28285 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
28289 /* True iff TOKEN is the GNU keyword __thread. */
28292 token_is__thread (cp_token
*token
)
28294 gcc_assert (token
->keyword
== RID_THREAD
);
28295 return id_equal (token
->u
.value
, "__thread");
28298 /* Set the location for a declarator specifier and check if it is
28301 DECL_SPECS is the sequence of declarator specifiers onto which to
28304 DS is the single declarator specifier to set which location is to
28305 be set onto the existing sequence of declarators.
28307 LOCATION is the location for the declarator specifier to
28311 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
28312 cp_decl_spec ds
, cp_token
*token
)
28314 gcc_assert (ds
< ds_last
);
28316 if (decl_specs
== NULL
)
28319 source_location location
= token
->location
;
28321 if (decl_specs
->locations
[ds
] == 0)
28323 decl_specs
->locations
[ds
] = location
;
28324 if (ds
== ds_thread
)
28325 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
28331 if (decl_specs
->locations
[ds_long_long
] != 0)
28332 error_at (location
,
28333 "%<long long long%> is too long for GCC");
28336 decl_specs
->locations
[ds_long_long
] = location
;
28337 pedwarn_cxx98 (location
,
28339 "ISO C++ 1998 does not support %<long long%>");
28342 else if (ds
== ds_thread
)
28344 bool gnu
= token_is__thread (token
);
28345 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
28346 error_at (location
,
28347 "both %<__thread%> and %<thread_local%> specified");
28350 gcc_rich_location
richloc (location
);
28351 richloc
.add_fixit_remove ();
28352 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
28357 static const char *const decl_spec_names
[] = {
28374 gcc_rich_location
richloc (location
);
28375 richloc
.add_fixit_remove ();
28376 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
28381 /* Return true iff the declarator specifier DS is present in the
28382 sequence of declarator specifiers DECL_SPECS. */
28385 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
28388 gcc_assert (ds
< ds_last
);
28390 if (decl_specs
== NULL
)
28393 return decl_specs
->locations
[ds
] != 0;
28396 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28397 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28400 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
28402 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
28405 /* Issue an error message indicating that TOKEN_DESC was expected.
28406 If KEYWORD is true, it indicated this function is called by
28407 cp_parser_require_keword and the required token can only be
28408 a indicated keyword.
28410 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28411 within any error as the location of an "opening" token matching
28412 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28413 RT_CLOSE_PAREN). */
28416 cp_parser_required_error (cp_parser
*parser
,
28417 required_token token_desc
,
28419 location_t matching_location
)
28421 if (cp_parser_simulate_error (parser
))
28424 const char *gmsgid
= NULL
;
28425 switch (token_desc
)
28428 gmsgid
= G_("expected %<new%>");
28431 gmsgid
= G_("expected %<delete%>");
28434 gmsgid
= G_("expected %<return%>");
28437 gmsgid
= G_("expected %<while%>");
28440 gmsgid
= G_("expected %<extern%>");
28442 case RT_STATIC_ASSERT
:
28443 gmsgid
= G_("expected %<static_assert%>");
28446 gmsgid
= G_("expected %<decltype%>");
28449 gmsgid
= G_("expected %<operator%>");
28452 gmsgid
= G_("expected %<class%>");
28455 gmsgid
= G_("expected %<template%>");
28458 gmsgid
= G_("expected %<namespace%>");
28461 gmsgid
= G_("expected %<using%>");
28464 gmsgid
= G_("expected %<asm%>");
28467 gmsgid
= G_("expected %<try%>");
28470 gmsgid
= G_("expected %<catch%>");
28473 gmsgid
= G_("expected %<throw%>");
28476 gmsgid
= G_("expected %<__label__%>");
28479 gmsgid
= G_("expected %<@try%>");
28481 case RT_AT_SYNCHRONIZED
:
28482 gmsgid
= G_("expected %<@synchronized%>");
28485 gmsgid
= G_("expected %<@throw%>");
28487 case RT_TRANSACTION_ATOMIC
:
28488 gmsgid
= G_("expected %<__transaction_atomic%>");
28490 case RT_TRANSACTION_RELAXED
:
28491 gmsgid
= G_("expected %<__transaction_relaxed%>");
28497 if (!gmsgid
&& !keyword
)
28499 switch (token_desc
)
28502 gmsgid
= G_("expected %<;%>");
28504 case RT_OPEN_PAREN
:
28505 gmsgid
= G_("expected %<(%>");
28507 case RT_CLOSE_BRACE
:
28508 gmsgid
= G_("expected %<}%>");
28510 case RT_OPEN_BRACE
:
28511 gmsgid
= G_("expected %<{%>");
28513 case RT_CLOSE_SQUARE
:
28514 gmsgid
= G_("expected %<]%>");
28516 case RT_OPEN_SQUARE
:
28517 gmsgid
= G_("expected %<[%>");
28520 gmsgid
= G_("expected %<,%>");
28523 gmsgid
= G_("expected %<::%>");
28526 gmsgid
= G_("expected %<<%>");
28529 gmsgid
= G_("expected %<>%>");
28532 gmsgid
= G_("expected %<=%>");
28535 gmsgid
= G_("expected %<...%>");
28538 gmsgid
= G_("expected %<*%>");
28541 gmsgid
= G_("expected %<~%>");
28544 gmsgid
= G_("expected %<:%>");
28546 case RT_COLON_SCOPE
:
28547 gmsgid
= G_("expected %<:%> or %<::%>");
28549 case RT_CLOSE_PAREN
:
28550 gmsgid
= G_("expected %<)%>");
28552 case RT_COMMA_CLOSE_PAREN
:
28553 gmsgid
= G_("expected %<,%> or %<)%>");
28555 case RT_PRAGMA_EOL
:
28556 gmsgid
= G_("expected end of line");
28559 gmsgid
= G_("expected identifier");
28562 gmsgid
= G_("expected selection-statement");
28565 gmsgid
= G_("expected iteration-statement");
28568 gmsgid
= G_("expected jump-statement");
28571 gmsgid
= G_("expected class-key");
28573 case RT_CLASS_TYPENAME_TEMPLATE
:
28574 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
28577 gcc_unreachable ();
28582 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
28586 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28587 issue an error message indicating that TOKEN_DESC was expected.
28589 Returns the token consumed, if the token had the appropriate type.
28590 Otherwise, returns NULL.
28592 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28593 within any error as the location of an "opening" token matching
28594 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28595 RT_CLOSE_PAREN). */
28598 cp_parser_require (cp_parser
* parser
,
28599 enum cpp_ttype type
,
28600 required_token token_desc
,
28601 location_t matching_location
)
28603 if (cp_lexer_next_token_is (parser
->lexer
, type
))
28604 return cp_lexer_consume_token (parser
->lexer
);
28607 /* Output the MESSAGE -- unless we're parsing tentatively. */
28608 if (!cp_parser_simulate_error (parser
))
28609 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
28610 matching_location
);
28615 /* An error message is produced if the next token is not '>'.
28616 All further tokens are skipped until the desired token is
28617 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28620 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
28622 /* Current level of '< ... >'. */
28623 unsigned level
= 0;
28624 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28625 unsigned nesting_depth
= 0;
28627 /* Are we ready, yet? If not, issue error message. */
28628 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
28631 /* Skip tokens until the desired token is found. */
28634 /* Peek at the next token. */
28635 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28638 if (!nesting_depth
)
28643 if (cxx_dialect
== cxx98
)
28644 /* C++0x views the `>>' operator as two `>' tokens, but
28647 else if (!nesting_depth
&& level
-- == 0)
28649 /* We've hit a `>>' where the first `>' closes the
28650 template argument list, and the second `>' is
28651 spurious. Just consume the `>>' and stop; we've
28652 already produced at least one error. */
28653 cp_lexer_consume_token (parser
->lexer
);
28656 /* Fall through for C++0x, so we handle the second `>' in
28658 gcc_fallthrough ();
28661 if (!nesting_depth
&& level
-- == 0)
28663 /* We've reached the token we want, consume it and stop. */
28664 cp_lexer_consume_token (parser
->lexer
);
28669 case CPP_OPEN_PAREN
:
28670 case CPP_OPEN_SQUARE
:
28674 case CPP_CLOSE_PAREN
:
28675 case CPP_CLOSE_SQUARE
:
28676 if (nesting_depth
-- == 0)
28681 case CPP_PRAGMA_EOL
:
28682 case CPP_SEMICOLON
:
28683 case CPP_OPEN_BRACE
:
28684 case CPP_CLOSE_BRACE
:
28685 /* The '>' was probably forgotten, don't look further. */
28692 /* Consume this token. */
28693 cp_lexer_consume_token (parser
->lexer
);
28697 /* If the next token is the indicated keyword, consume it. Otherwise,
28698 issue an error message indicating that TOKEN_DESC was expected.
28700 Returns the token consumed, if the token had the appropriate type.
28701 Otherwise, returns NULL. */
28704 cp_parser_require_keyword (cp_parser
* parser
,
28706 required_token token_desc
)
28708 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
28710 if (token
&& token
->keyword
!= keyword
)
28712 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
28720 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28721 function-definition. */
28724 cp_parser_token_starts_function_definition_p (cp_token
* token
)
28726 return (/* An ordinary function-body begins with an `{'. */
28727 token
->type
== CPP_OPEN_BRACE
28728 /* A ctor-initializer begins with a `:'. */
28729 || token
->type
== CPP_COLON
28730 /* A function-try-block begins with `try'. */
28731 || token
->keyword
== RID_TRY
28732 /* A function-transaction-block begins with `__transaction_atomic'
28733 or `__transaction_relaxed'. */
28734 || token
->keyword
== RID_TRANSACTION_ATOMIC
28735 || token
->keyword
== RID_TRANSACTION_RELAXED
28736 /* The named return value extension begins with `return'. */
28737 || token
->keyword
== RID_RETURN
);
28740 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28744 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
28748 token
= cp_lexer_peek_token (parser
->lexer
);
28749 return (token
->type
== CPP_OPEN_BRACE
28750 || (token
->type
== CPP_COLON
28751 && !parser
->colon_doesnt_start_class_def_p
));
28754 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28755 C++0x) ending a template-argument. */
28758 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
28762 token
= cp_lexer_peek_token (parser
->lexer
);
28763 return (token
->type
== CPP_COMMA
28764 || token
->type
== CPP_GREATER
28765 || token
->type
== CPP_ELLIPSIS
28766 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
28769 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28770 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28773 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
28778 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
28779 if (token
->type
== CPP_LESS
)
28781 /* Check for the sequence `<::' in the original code. It would be lexed as
28782 `[:', where `[' is a digraph, and there is no whitespace before
28784 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
28787 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
28788 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
28794 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28795 or none_type otherwise. */
28797 static enum tag_types
28798 cp_parser_token_is_class_key (cp_token
* token
)
28800 switch (token
->keyword
)
28805 return record_type
;
28814 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28815 or none_type otherwise or if the token is null. */
28817 static enum tag_types
28818 cp_parser_token_is_type_parameter_key (cp_token
* token
)
28823 switch (token
->keyword
)
28828 return typename_type
;
28835 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28838 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
28840 if (type
== error_mark_node
)
28842 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
28844 if (permerror (input_location
, "%qs tag used in naming %q#T",
28845 class_key
== union_type
? "union"
28846 : class_key
== record_type
? "struct" : "class",
28848 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
28849 "%q#T was previously declared here", type
);
28853 /* Issue an error message if DECL is redeclared with different
28854 access than its original declaration [class.access.spec/3].
28855 This applies to nested classes, nested class templates and
28856 enumerations [class.mem/1]. */
28859 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
28862 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
28863 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
28866 if ((TREE_PRIVATE (decl
)
28867 != (current_access_specifier
== access_private_node
))
28868 || (TREE_PROTECTED (decl
)
28869 != (current_access_specifier
== access_protected_node
)))
28870 error_at (location
, "%qD redeclared with different access", decl
);
28873 /* Look for the `template' keyword, as a syntactic disambiguator.
28874 Return TRUE iff it is present, in which case it will be
28878 cp_parser_optional_template_keyword (cp_parser
*parser
)
28880 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28882 /* In C++98 the `template' keyword can only be used within templates;
28883 outside templates the parser can always figure out what is a
28884 template and what is not. In C++11, per the resolution of DR 468,
28885 `template' is allowed in cases where it is not strictly necessary. */
28886 if (!processing_template_decl
28887 && pedantic
&& cxx_dialect
== cxx98
)
28889 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28890 pedwarn (token
->location
, OPT_Wpedantic
,
28891 "in C++98 %<template%> (as a disambiguator) is only "
28892 "allowed within templates");
28893 /* If this part of the token stream is rescanned, the same
28894 error message would be generated. So, we purge the token
28895 from the stream. */
28896 cp_lexer_purge_token (parser
->lexer
);
28901 /* Consume the `template' keyword. */
28902 cp_lexer_consume_token (parser
->lexer
);
28909 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28910 set PARSER->SCOPE, and perform other related actions. */
28913 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
28915 struct tree_check
*check_value
;
28917 /* Get the stored value. */
28918 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
28919 /* Set the scope from the stored value. */
28920 parser
->scope
= saved_checks_value (check_value
);
28921 parser
->qualifying_scope
= check_value
->qualifying_scope
;
28922 parser
->object_scope
= NULL_TREE
;
28925 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28926 encounter the end of a block before what we were looking for. */
28929 cp_parser_cache_group (cp_parser
*parser
,
28930 enum cpp_ttype end
,
28935 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28937 /* Abort a parenthesized expression if we encounter a semicolon. */
28938 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
28939 && token
->type
== CPP_SEMICOLON
)
28941 /* If we've reached the end of the file, stop. */
28942 if (token
->type
== CPP_EOF
28943 || (end
!= CPP_PRAGMA_EOL
28944 && token
->type
== CPP_PRAGMA_EOL
))
28946 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
28947 /* We've hit the end of an enclosing block, so there's been some
28948 kind of syntax error. */
28951 /* Consume the token. */
28952 cp_lexer_consume_token (parser
->lexer
);
28953 /* See if it starts a new group. */
28954 if (token
->type
== CPP_OPEN_BRACE
)
28956 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
28957 /* In theory this should probably check end == '}', but
28958 cp_parser_save_member_function_body needs it to exit
28959 after either '}' or ')' when called with ')'. */
28963 else if (token
->type
== CPP_OPEN_PAREN
)
28965 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
28966 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
28969 else if (token
->type
== CPP_PRAGMA
)
28970 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
28971 else if (token
->type
== end
)
28976 /* Like above, for caching a default argument or NSDMI. Both of these are
28977 terminated by a non-nested comma, but it can be unclear whether or not a
28978 comma is nested in a template argument list unless we do more parsing.
28979 In order to handle this ambiguity, when we encounter a ',' after a '<'
28980 we try to parse what follows as a parameter-declaration-list (in the
28981 case of a default argument) or a member-declarator (in the case of an
28982 NSDMI). If that succeeds, then we stop caching. */
28985 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
28987 unsigned depth
= 0;
28988 int maybe_template_id
= 0;
28989 cp_token
*first_token
;
28991 tree default_argument
;
28993 /* Add tokens until we have processed the entire default
28994 argument. We add the range [first_token, token). */
28995 first_token
= cp_lexer_peek_token (parser
->lexer
);
28996 if (first_token
->type
== CPP_OPEN_BRACE
)
28998 /* For list-initialization, this is straightforward. */
28999 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29000 token
= cp_lexer_peek_token (parser
->lexer
);
29006 /* Peek at the next token. */
29007 token
= cp_lexer_peek_token (parser
->lexer
);
29008 /* What we do depends on what token we have. */
29009 switch (token
->type
)
29011 /* In valid code, a default argument must be
29012 immediately followed by a `,' `)', or `...'. */
29014 if (depth
== 0 && maybe_template_id
)
29016 /* If we've seen a '<', we might be in a
29017 template-argument-list. Until Core issue 325 is
29018 resolved, we don't know how this situation ought
29019 to be handled, so try to DTRT. We check whether
29020 what comes after the comma is a valid parameter
29021 declaration list. If it is, then the comma ends
29022 the default argument; otherwise the default
29023 argument continues. */
29024 bool error
= false;
29027 /* Set ITALP so cp_parser_parameter_declaration_list
29028 doesn't decide to commit to this parse. */
29029 bool saved_italp
= parser
->in_template_argument_list_p
;
29030 parser
->in_template_argument_list_p
= true;
29032 cp_parser_parse_tentatively (parser
);
29036 /* Parse declarators until we reach a non-comma or
29037 somthing that cannot be an initializer.
29038 Just checking whether we're looking at a single
29039 declarator is insufficient. Consider:
29040 int var = tuple<T,U>::x;
29041 The template parameter 'U' looks exactly like a
29045 int ctor_dtor_or_conv_p
;
29046 cp_lexer_consume_token (parser
->lexer
);
29047 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29048 &ctor_dtor_or_conv_p
,
29049 /*parenthesized_p=*/NULL
,
29051 /*friend_p=*/false);
29052 peek
= cp_lexer_peek_token (parser
->lexer
);
29053 if (cp_parser_error_occurred (parser
))
29056 while (peek
->type
== CPP_COMMA
);
29057 /* If we met an '=' or ';' then the original comma
29058 was the end of the NSDMI. Otherwise assume
29059 we're still in the NSDMI. */
29060 error
= (peek
->type
!= CPP_EQ
29061 && peek
->type
!= CPP_SEMICOLON
);
29065 cp_lexer_consume_token (parser
->lexer
);
29066 begin_scope (sk_function_parms
, NULL_TREE
);
29067 if (cp_parser_parameter_declaration_list (parser
)
29068 == error_mark_node
)
29070 pop_bindings_and_leave_scope ();
29072 if (!cp_parser_error_occurred (parser
) && !error
)
29074 cp_parser_abort_tentative_parse (parser
);
29076 parser
->in_template_argument_list_p
= saved_italp
;
29080 case CPP_CLOSE_PAREN
:
29082 /* If we run into a non-nested `;', `}', or `]',
29083 then the code is invalid -- but the default
29084 argument is certainly over. */
29085 case CPP_SEMICOLON
:
29086 case CPP_CLOSE_BRACE
:
29087 case CPP_CLOSE_SQUARE
:
29089 /* Handle correctly int n = sizeof ... ( p ); */
29090 && token
->type
!= CPP_ELLIPSIS
)
29092 /* Update DEPTH, if necessary. */
29093 else if (token
->type
== CPP_CLOSE_PAREN
29094 || token
->type
== CPP_CLOSE_BRACE
29095 || token
->type
== CPP_CLOSE_SQUARE
)
29099 case CPP_OPEN_PAREN
:
29100 case CPP_OPEN_SQUARE
:
29101 case CPP_OPEN_BRACE
:
29107 /* This might be the comparison operator, or it might
29108 start a template argument list. */
29109 ++maybe_template_id
;
29113 if (cxx_dialect
== cxx98
)
29115 /* Fall through for C++0x, which treats the `>>'
29116 operator like two `>' tokens in certain
29118 gcc_fallthrough ();
29123 /* This might be an operator, or it might close a
29124 template argument list. But if a previous '<'
29125 started a template argument list, this will have
29126 closed it, so we can't be in one anymore. */
29127 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
29128 if (maybe_template_id
< 0)
29129 maybe_template_id
= 0;
29133 /* If we run out of tokens, issue an error message. */
29135 case CPP_PRAGMA_EOL
:
29136 error_at (token
->location
, "file ends in default argument");
29137 return error_mark_node
;
29141 /* In these cases, we should look for template-ids.
29142 For example, if the default argument is
29143 `X<int, double>()', we need to do name lookup to
29144 figure out whether or not `X' is a template; if
29145 so, the `,' does not end the default argument.
29147 That is not yet done. */
29154 /* If we've reached the end, stop. */
29158 /* Add the token to the token block. */
29159 token
= cp_lexer_consume_token (parser
->lexer
);
29162 /* Create a DEFAULT_ARG to represent the unparsed default
29164 default_argument
= make_node (DEFAULT_ARG
);
29165 DEFARG_TOKENS (default_argument
)
29166 = cp_token_cache_new (first_token
, token
);
29167 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
29169 return default_argument
;
29172 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29175 defarg_location (tree default_argument
)
29177 cp_token_cache
*tokens
= DEFARG_TOKENS (default_argument
);
29178 location_t start
= tokens
->first
->location
;
29179 location_t end
= tokens
->last
->location
;
29180 return make_location (start
, start
, end
);
29183 /* Begin parsing tentatively. We always save tokens while parsing
29184 tentatively so that if the tentative parsing fails we can restore the
29188 cp_parser_parse_tentatively (cp_parser
* parser
)
29190 /* Enter a new parsing context. */
29191 parser
->context
= cp_parser_context_new (parser
->context
);
29192 /* Begin saving tokens. */
29193 cp_lexer_save_tokens (parser
->lexer
);
29194 /* In order to avoid repetitive access control error messages,
29195 access checks are queued up until we are no longer parsing
29197 push_deferring_access_checks (dk_deferred
);
29200 /* Commit to the currently active tentative parse. */
29203 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
29205 cp_parser_context
*context
;
29208 /* Mark all of the levels as committed. */
29209 lexer
= parser
->lexer
;
29210 for (context
= parser
->context
; context
->next
; context
= context
->next
)
29212 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29214 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29215 while (!cp_lexer_saving_tokens (lexer
))
29216 lexer
= lexer
->next
;
29217 cp_lexer_commit_tokens (lexer
);
29221 /* Commit to the topmost currently active tentative parse.
29223 Note that this function shouldn't be called when there are
29224 irreversible side-effects while in a tentative state. For
29225 example, we shouldn't create a permanent entry in the symbol
29226 table, or issue an error message that might not apply if the
29227 tentative parse is aborted. */
29230 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
29232 cp_parser_context
*context
= parser
->context
;
29233 cp_lexer
*lexer
= parser
->lexer
;
29237 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
29239 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
29241 while (!cp_lexer_saving_tokens (lexer
))
29242 lexer
= lexer
->next
;
29243 cp_lexer_commit_tokens (lexer
);
29247 /* Abort the currently active tentative parse. All consumed tokens
29248 will be rolled back, and no diagnostics will be issued. */
29251 cp_parser_abort_tentative_parse (cp_parser
* parser
)
29253 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
29254 || errorcount
> 0);
29255 cp_parser_simulate_error (parser
);
29256 /* Now, pretend that we want to see if the construct was
29257 successfully parsed. */
29258 cp_parser_parse_definitely (parser
);
29261 /* Stop parsing tentatively. If a parse error has occurred, restore the
29262 token stream. Otherwise, commit to the tokens we have consumed.
29263 Returns true if no error occurred; false otherwise. */
29266 cp_parser_parse_definitely (cp_parser
* parser
)
29268 bool error_occurred
;
29269 cp_parser_context
*context
;
29271 /* Remember whether or not an error occurred, since we are about to
29272 destroy that information. */
29273 error_occurred
= cp_parser_error_occurred (parser
);
29274 /* Remove the topmost context from the stack. */
29275 context
= parser
->context
;
29276 parser
->context
= context
->next
;
29277 /* If no parse errors occurred, commit to the tentative parse. */
29278 if (!error_occurred
)
29280 /* Commit to the tokens read tentatively, unless that was
29282 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
29283 cp_lexer_commit_tokens (parser
->lexer
);
29285 pop_to_parent_deferring_access_checks ();
29287 /* Otherwise, if errors occurred, roll back our state so that things
29288 are just as they were before we began the tentative parse. */
29291 cp_lexer_rollback_tokens (parser
->lexer
);
29292 pop_deferring_access_checks ();
29294 /* Add the context to the front of the free list. */
29295 context
->next
= cp_parser_context_free_list
;
29296 cp_parser_context_free_list
= context
;
29298 return !error_occurred
;
29301 /* Returns true if we are parsing tentatively and are not committed to
29302 this tentative parse. */
29305 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
29307 return (cp_parser_parsing_tentatively (parser
)
29308 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
29311 /* Returns nonzero iff an error has occurred during the most recent
29312 tentative parse. */
29315 cp_parser_error_occurred (cp_parser
* parser
)
29317 return (cp_parser_parsing_tentatively (parser
)
29318 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
29321 /* Returns nonzero if GNU extensions are allowed. */
29324 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
29326 return parser
->allow_gnu_extensions_p
;
29329 /* Objective-C++ Productions */
29332 /* Parse an Objective-C expression, which feeds into a primary-expression
29336 objc-message-expression
29337 objc-string-literal
29338 objc-encode-expression
29339 objc-protocol-expression
29340 objc-selector-expression
29342 Returns a tree representation of the expression. */
29345 cp_parser_objc_expression (cp_parser
* parser
)
29347 /* Try to figure out what kind of declaration is present. */
29348 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29352 case CPP_OPEN_SQUARE
:
29353 return cp_parser_objc_message_expression (parser
);
29355 case CPP_OBJC_STRING
:
29356 kwd
= cp_lexer_consume_token (parser
->lexer
);
29357 return objc_build_string_object (kwd
->u
.value
);
29360 switch (kwd
->keyword
)
29362 case RID_AT_ENCODE
:
29363 return cp_parser_objc_encode_expression (parser
);
29365 case RID_AT_PROTOCOL
:
29366 return cp_parser_objc_protocol_expression (parser
);
29368 case RID_AT_SELECTOR
:
29369 return cp_parser_objc_selector_expression (parser
);
29376 error_at (kwd
->location
,
29377 "misplaced %<@%D%> Objective-C++ construct",
29379 cp_parser_skip_to_end_of_block_or_statement (parser
);
29382 return error_mark_node
;
29385 /* Parse an Objective-C message expression.
29387 objc-message-expression:
29388 [ objc-message-receiver objc-message-args ]
29390 Returns a representation of an Objective-C message. */
29393 cp_parser_objc_message_expression (cp_parser
* parser
)
29395 tree receiver
, messageargs
;
29397 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29398 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
29399 receiver
= cp_parser_objc_message_receiver (parser
);
29400 messageargs
= cp_parser_objc_message_args (parser
);
29401 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29402 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
29404 tree result
= objc_build_message_expr (receiver
, messageargs
);
29406 /* Construct a location e.g.
29409 ranging from the '[' to the ']', with the caret at the start. */
29410 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
29411 protected_set_expr_location (result
, combined_loc
);
29416 /* Parse an objc-message-receiver.
29418 objc-message-receiver:
29420 simple-type-specifier
29422 Returns a representation of the type or expression. */
29425 cp_parser_objc_message_receiver (cp_parser
* parser
)
29429 /* An Objective-C message receiver may be either (1) a type
29430 or (2) an expression. */
29431 cp_parser_parse_tentatively (parser
);
29432 rcv
= cp_parser_expression (parser
);
29434 /* If that worked out, fine. */
29435 if (cp_parser_parse_definitely (parser
))
29438 cp_parser_parse_tentatively (parser
);
29439 rcv
= cp_parser_simple_type_specifier (parser
,
29440 /*decl_specs=*/NULL
,
29441 CP_PARSER_FLAGS_NONE
);
29443 if (cp_parser_parse_definitely (parser
))
29444 return objc_get_class_reference (rcv
);
29446 cp_parser_error (parser
, "objective-c++ message receiver expected");
29447 return error_mark_node
;
29450 /* Parse the arguments and selectors comprising an Objective-C message.
29455 objc-selector-args , objc-comma-args
29457 objc-selector-args:
29458 objc-selector [opt] : assignment-expression
29459 objc-selector-args objc-selector [opt] : assignment-expression
29462 assignment-expression
29463 objc-comma-args , assignment-expression
29465 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29466 selector arguments and TREE_VALUE containing a list of comma
29470 cp_parser_objc_message_args (cp_parser
* parser
)
29472 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
29473 bool maybe_unary_selector_p
= true;
29474 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29476 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29478 tree selector
= NULL_TREE
, arg
;
29480 if (token
->type
!= CPP_COLON
)
29481 selector
= cp_parser_objc_selector (parser
);
29483 /* Detect if we have a unary selector. */
29484 if (maybe_unary_selector_p
29485 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29486 return build_tree_list (selector
, NULL_TREE
);
29488 maybe_unary_selector_p
= false;
29489 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29490 arg
= cp_parser_assignment_expression (parser
);
29493 = chainon (sel_args
,
29494 build_tree_list (selector
, arg
));
29496 token
= cp_lexer_peek_token (parser
->lexer
);
29499 /* Handle non-selector arguments, if any. */
29500 while (token
->type
== CPP_COMMA
)
29504 cp_lexer_consume_token (parser
->lexer
);
29505 arg
= cp_parser_assignment_expression (parser
);
29508 = chainon (addl_args
,
29509 build_tree_list (NULL_TREE
, arg
));
29511 token
= cp_lexer_peek_token (parser
->lexer
);
29514 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
29516 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
29517 return build_tree_list (error_mark_node
, error_mark_node
);
29520 return build_tree_list (sel_args
, addl_args
);
29523 /* Parse an Objective-C encode expression.
29525 objc-encode-expression:
29526 @encode objc-typename
29528 Returns an encoded representation of the type argument. */
29531 cp_parser_objc_encode_expression (cp_parser
* parser
)
29535 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29537 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
29538 matching_parens parens
;
29539 parens
.require_open (parser
);
29540 token
= cp_lexer_peek_token (parser
->lexer
);
29541 type
= complete_type (cp_parser_type_id (parser
));
29542 parens
.require_close (parser
);
29546 error_at (token
->location
,
29547 "%<@encode%> must specify a type as an argument");
29548 return error_mark_node
;
29551 /* This happens if we find @encode(T) (where T is a template
29552 typename or something dependent on a template typename) when
29553 parsing a template. In that case, we can't compile it
29554 immediately, but we rather create an AT_ENCODE_EXPR which will
29555 need to be instantiated when the template is used.
29557 if (dependent_type_p (type
))
29559 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
29560 TREE_READONLY (value
) = 1;
29565 /* Build a location of the form:
29568 with caret==start at the @ token, finishing at the close paren. */
29569 location_t combined_loc
29570 = make_location (start_loc
, start_loc
,
29571 cp_lexer_previous_token (parser
->lexer
)->location
);
29573 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
29576 /* Parse an Objective-C @defs expression. */
29579 cp_parser_objc_defs_expression (cp_parser
*parser
)
29583 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
29584 matching_parens parens
;
29585 parens
.require_open (parser
);
29586 name
= cp_parser_identifier (parser
);
29587 parens
.require_close (parser
);
29589 return objc_get_class_ivars (name
);
29592 /* Parse an Objective-C protocol expression.
29594 objc-protocol-expression:
29595 @protocol ( identifier )
29597 Returns a representation of the protocol expression. */
29600 cp_parser_objc_protocol_expression (cp_parser
* parser
)
29603 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29605 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
29606 matching_parens parens
;
29607 parens
.require_open (parser
);
29608 proto
= cp_parser_identifier (parser
);
29609 parens
.require_close (parser
);
29611 /* Build a location of the form:
29614 with caret==start at the @ token, finishing at the close paren. */
29615 location_t combined_loc
29616 = make_location (start_loc
, start_loc
,
29617 cp_lexer_previous_token (parser
->lexer
)->location
);
29618 tree result
= objc_build_protocol_expr (proto
);
29619 protected_set_expr_location (result
, combined_loc
);
29623 /* Parse an Objective-C selector expression.
29625 objc-selector-expression:
29626 @selector ( objc-method-signature )
29628 objc-method-signature:
29634 objc-selector-seq objc-selector :
29636 Returns a representation of the method selector. */
29639 cp_parser_objc_selector_expression (cp_parser
* parser
)
29641 tree sel_seq
= NULL_TREE
;
29642 bool maybe_unary_selector_p
= true;
29644 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29646 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
29647 matching_parens parens
;
29648 parens
.require_open (parser
);
29649 token
= cp_lexer_peek_token (parser
->lexer
);
29651 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
29652 || token
->type
== CPP_SCOPE
)
29654 tree selector
= NULL_TREE
;
29656 if (token
->type
!= CPP_COLON
29657 || token
->type
== CPP_SCOPE
)
29658 selector
= cp_parser_objc_selector (parser
);
29660 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
29661 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
29663 /* Detect if we have a unary selector. */
29664 if (maybe_unary_selector_p
)
29666 sel_seq
= selector
;
29667 goto finish_selector
;
29671 cp_parser_error (parser
, "expected %<:%>");
29674 maybe_unary_selector_p
= false;
29675 token
= cp_lexer_consume_token (parser
->lexer
);
29677 if (token
->type
== CPP_SCOPE
)
29680 = chainon (sel_seq
,
29681 build_tree_list (selector
, NULL_TREE
));
29683 = chainon (sel_seq
,
29684 build_tree_list (NULL_TREE
, NULL_TREE
));
29688 = chainon (sel_seq
,
29689 build_tree_list (selector
, NULL_TREE
));
29691 token
= cp_lexer_peek_token (parser
->lexer
);
29695 parens
.require_close (parser
);
29698 /* Build a location of the form:
29701 with caret==start at the @ token, finishing at the close paren. */
29702 location_t combined_loc
29703 = make_location (loc
, loc
,
29704 cp_lexer_previous_token (parser
->lexer
)->location
);
29705 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
29706 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29707 protected_set_expr_location (result
, combined_loc
);
29711 /* Parse a list of identifiers.
29713 objc-identifier-list:
29715 objc-identifier-list , identifier
29717 Returns a TREE_LIST of identifier nodes. */
29720 cp_parser_objc_identifier_list (cp_parser
* parser
)
29726 identifier
= cp_parser_identifier (parser
);
29727 if (identifier
== error_mark_node
)
29728 return error_mark_node
;
29730 list
= build_tree_list (NULL_TREE
, identifier
);
29731 sep
= cp_lexer_peek_token (parser
->lexer
);
29733 while (sep
->type
== CPP_COMMA
)
29735 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29736 identifier
= cp_parser_identifier (parser
);
29737 if (identifier
== error_mark_node
)
29740 list
= chainon (list
, build_tree_list (NULL_TREE
,
29742 sep
= cp_lexer_peek_token (parser
->lexer
);
29748 /* Parse an Objective-C alias declaration.
29750 objc-alias-declaration:
29751 @compatibility_alias identifier identifier ;
29753 This function registers the alias mapping with the Objective-C front end.
29754 It returns nothing. */
29757 cp_parser_objc_alias_declaration (cp_parser
* parser
)
29761 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
29762 alias
= cp_parser_identifier (parser
);
29763 orig
= cp_parser_identifier (parser
);
29764 objc_declare_alias (alias
, orig
);
29765 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29768 /* Parse an Objective-C class forward-declaration.
29770 objc-class-declaration:
29771 @class objc-identifier-list ;
29773 The function registers the forward declarations with the Objective-C
29774 front end. It returns nothing. */
29777 cp_parser_objc_class_declaration (cp_parser
* parser
)
29779 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
29784 id
= cp_parser_identifier (parser
);
29785 if (id
== error_mark_node
)
29788 objc_declare_class (id
);
29790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29791 cp_lexer_consume_token (parser
->lexer
);
29795 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29798 /* Parse a list of Objective-C protocol references.
29800 objc-protocol-refs-opt:
29801 objc-protocol-refs [opt]
29803 objc-protocol-refs:
29804 < objc-identifier-list >
29806 Returns a TREE_LIST of identifiers, if any. */
29809 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
29811 tree protorefs
= NULL_TREE
;
29813 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
29815 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
29816 protorefs
= cp_parser_objc_identifier_list (parser
);
29817 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
29823 /* Parse a Objective-C visibility specification. */
29826 cp_parser_objc_visibility_spec (cp_parser
* parser
)
29828 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
29830 switch (vis
->keyword
)
29832 case RID_AT_PRIVATE
:
29833 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
29835 case RID_AT_PROTECTED
:
29836 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
29838 case RID_AT_PUBLIC
:
29839 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
29841 case RID_AT_PACKAGE
:
29842 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
29848 /* Eat '@private'/'@protected'/'@public'. */
29849 cp_lexer_consume_token (parser
->lexer
);
29852 /* Parse an Objective-C method type. Return 'true' if it is a class
29853 (+) method, and 'false' if it is an instance (-) method. */
29856 cp_parser_objc_method_type (cp_parser
* parser
)
29858 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
29864 /* Parse an Objective-C protocol qualifier. */
29867 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
29869 tree quals
= NULL_TREE
, node
;
29870 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29872 node
= token
->u
.value
;
29874 while (node
&& identifier_p (node
)
29875 && (node
== ridpointers
[(int) RID_IN
]
29876 || node
== ridpointers
[(int) RID_OUT
]
29877 || node
== ridpointers
[(int) RID_INOUT
]
29878 || node
== ridpointers
[(int) RID_BYCOPY
]
29879 || node
== ridpointers
[(int) RID_BYREF
]
29880 || node
== ridpointers
[(int) RID_ONEWAY
]))
29882 quals
= tree_cons (NULL_TREE
, node
, quals
);
29883 cp_lexer_consume_token (parser
->lexer
);
29884 token
= cp_lexer_peek_token (parser
->lexer
);
29885 node
= token
->u
.value
;
29891 /* Parse an Objective-C typename. */
29894 cp_parser_objc_typename (cp_parser
* parser
)
29896 tree type_name
= NULL_TREE
;
29898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29900 tree proto_quals
, cp_type
= NULL_TREE
;
29902 matching_parens parens
;
29903 parens
.consume_open (parser
); /* Eat '('. */
29904 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
29906 /* An ObjC type name may consist of just protocol qualifiers, in which
29907 case the type shall default to 'id'. */
29908 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29910 cp_type
= cp_parser_type_id (parser
);
29912 /* If the type could not be parsed, an error has already
29913 been produced. For error recovery, behave as if it had
29914 not been specified, which will use the default type
29916 if (cp_type
== error_mark_node
)
29918 cp_type
= NULL_TREE
;
29919 /* We need to skip to the closing parenthesis as
29920 cp_parser_type_id() does not seem to do it for
29922 cp_parser_skip_to_closing_parenthesis (parser
,
29923 /*recovering=*/true,
29924 /*or_comma=*/false,
29925 /*consume_paren=*/false);
29929 parens
.require_close (parser
);
29930 type_name
= build_tree_list (proto_quals
, cp_type
);
29936 /* Check to see if TYPE refers to an Objective-C selector name. */
29939 cp_parser_objc_selector_p (enum cpp_ttype type
)
29941 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
29942 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
29943 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
29944 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
29945 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
29948 /* Parse an Objective-C selector. */
29951 cp_parser_objc_selector (cp_parser
* parser
)
29953 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29955 if (!cp_parser_objc_selector_p (token
->type
))
29957 error_at (token
->location
, "invalid Objective-C++ selector name");
29958 return error_mark_node
;
29961 /* C++ operator names are allowed to appear in ObjC selectors. */
29962 switch (token
->type
)
29964 case CPP_AND_AND
: return get_identifier ("and");
29965 case CPP_AND_EQ
: return get_identifier ("and_eq");
29966 case CPP_AND
: return get_identifier ("bitand");
29967 case CPP_OR
: return get_identifier ("bitor");
29968 case CPP_COMPL
: return get_identifier ("compl");
29969 case CPP_NOT
: return get_identifier ("not");
29970 case CPP_NOT_EQ
: return get_identifier ("not_eq");
29971 case CPP_OR_OR
: return get_identifier ("or");
29972 case CPP_OR_EQ
: return get_identifier ("or_eq");
29973 case CPP_XOR
: return get_identifier ("xor");
29974 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
29975 default: return token
->u
.value
;
29979 /* Parse an Objective-C params list. */
29982 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
29984 tree params
= NULL_TREE
;
29985 bool maybe_unary_selector_p
= true;
29986 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29988 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
29990 tree selector
= NULL_TREE
, type_name
, identifier
;
29991 tree parm_attr
= NULL_TREE
;
29993 if (token
->keyword
== RID_ATTRIBUTE
)
29996 if (token
->type
!= CPP_COLON
)
29997 selector
= cp_parser_objc_selector (parser
);
29999 /* Detect if we have a unary selector. */
30000 if (maybe_unary_selector_p
30001 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
30003 params
= selector
; /* Might be followed by attributes. */
30007 maybe_unary_selector_p
= false;
30008 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30010 /* Something went quite wrong. There should be a colon
30011 here, but there is not. Stop parsing parameters. */
30014 type_name
= cp_parser_objc_typename (parser
);
30015 /* New ObjC allows attributes on parameters too. */
30016 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
30017 parm_attr
= cp_parser_attributes_opt (parser
);
30018 identifier
= cp_parser_identifier (parser
);
30022 objc_build_keyword_decl (selector
,
30027 token
= cp_lexer_peek_token (parser
->lexer
);
30030 if (params
== NULL_TREE
)
30032 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30033 return error_mark_node
;
30036 /* We allow tail attributes for the method. */
30037 if (token
->keyword
== RID_ATTRIBUTE
)
30039 *attributes
= cp_parser_attributes_opt (parser
);
30040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30041 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30043 cp_parser_error (parser
,
30044 "method attributes must be specified at the end");
30045 return error_mark_node
;
30048 if (params
== NULL_TREE
)
30050 cp_parser_error (parser
, "objective-c++ method declaration is expected");
30051 return error_mark_node
;
30056 /* Parse the non-keyword Objective-C params. */
30059 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
30062 tree params
= make_node (TREE_LIST
);
30063 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30064 *ellipsisp
= false; /* Initially, assume no ellipsis. */
30066 while (token
->type
== CPP_COMMA
)
30068 cp_parameter_declarator
*parmdecl
;
30071 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30072 token
= cp_lexer_peek_token (parser
->lexer
);
30074 if (token
->type
== CPP_ELLIPSIS
)
30076 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
30078 token
= cp_lexer_peek_token (parser
->lexer
);
30082 /* TODO: parse attributes for tail parameters. */
30083 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
30084 parm
= grokdeclarator (parmdecl
->declarator
,
30085 &parmdecl
->decl_specifiers
,
30086 PARM
, /*initialized=*/0,
30087 /*attrlist=*/NULL
);
30089 chainon (params
, build_tree_list (NULL_TREE
, parm
));
30090 token
= cp_lexer_peek_token (parser
->lexer
);
30093 /* We allow tail attributes for the method. */
30094 if (token
->keyword
== RID_ATTRIBUTE
)
30096 if (*attributes
== NULL_TREE
)
30098 *attributes
= cp_parser_attributes_opt (parser
);
30099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30100 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30104 /* We have an error, but parse the attributes, so that we can
30106 *attributes
= cp_parser_attributes_opt (parser
);
30108 cp_parser_error (parser
,
30109 "method attributes must be specified at the end");
30110 return error_mark_node
;
30116 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30119 cp_parser_objc_interstitial_code (cp_parser
* parser
)
30121 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30123 /* If the next token is `extern' and the following token is a string
30124 literal, then we have a linkage specification. */
30125 if (token
->keyword
== RID_EXTERN
30126 && cp_parser_is_pure_string_literal
30127 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
30128 cp_parser_linkage_specification (parser
);
30129 /* Handle #pragma, if any. */
30130 else if (token
->type
== CPP_PRAGMA
)
30131 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
30132 /* Allow stray semicolons. */
30133 else if (token
->type
== CPP_SEMICOLON
)
30134 cp_lexer_consume_token (parser
->lexer
);
30135 /* Mark methods as optional or required, when building protocols. */
30136 else if (token
->keyword
== RID_AT_OPTIONAL
)
30138 cp_lexer_consume_token (parser
->lexer
);
30139 objc_set_method_opt (true);
30141 else if (token
->keyword
== RID_AT_REQUIRED
)
30143 cp_lexer_consume_token (parser
->lexer
);
30144 objc_set_method_opt (false);
30146 else if (token
->keyword
== RID_NAMESPACE
)
30147 cp_parser_namespace_definition (parser
);
30148 /* Other stray characters must generate errors. */
30149 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
30151 cp_lexer_consume_token (parser
->lexer
);
30152 error ("stray %qs between Objective-C++ methods",
30153 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
30155 /* Finally, try to parse a block-declaration, or a function-definition. */
30157 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30160 /* Parse a method signature. */
30163 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
30165 tree rettype
, kwdparms
, optparms
;
30166 bool ellipsis
= false;
30167 bool is_class_method
;
30169 is_class_method
= cp_parser_objc_method_type (parser
);
30170 rettype
= cp_parser_objc_typename (parser
);
30171 *attributes
= NULL_TREE
;
30172 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
30173 if (kwdparms
== error_mark_node
)
30174 return error_mark_node
;
30175 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
30176 if (optparms
== error_mark_node
)
30177 return error_mark_node
;
30179 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
30183 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
30186 cp_lexer_save_tokens (parser
->lexer
);
30187 tattr
= cp_parser_attributes_opt (parser
);
30188 gcc_assert (tattr
) ;
30190 /* If the attributes are followed by a method introducer, this is not allowed.
30191 Dump the attributes and flag the situation. */
30192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
30193 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
30196 /* Otherwise, the attributes introduce some interstitial code, possibly so
30197 rewind to allow that check. */
30198 cp_lexer_rollback_tokens (parser
->lexer
);
30202 /* Parse an Objective-C method prototype list. */
30205 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
30207 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30209 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30211 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30213 tree attributes
, sig
;
30214 bool is_class_method
;
30215 if (token
->type
== CPP_PLUS
)
30216 is_class_method
= true;
30218 is_class_method
= false;
30219 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
30220 if (sig
== error_mark_node
)
30222 cp_parser_skip_to_end_of_block_or_statement (parser
);
30223 token
= cp_lexer_peek_token (parser
->lexer
);
30226 objc_add_method_declaration (is_class_method
, sig
, attributes
);
30227 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30229 else if (token
->keyword
== RID_AT_PROPERTY
)
30230 cp_parser_objc_at_property_declaration (parser
);
30231 else if (token
->keyword
== RID_ATTRIBUTE
30232 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30233 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30235 "prefix attributes are ignored for methods");
30237 /* Allow for interspersed non-ObjC++ code. */
30238 cp_parser_objc_interstitial_code (parser
);
30240 token
= cp_lexer_peek_token (parser
->lexer
);
30243 if (token
->type
!= CPP_EOF
)
30244 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30246 cp_parser_error (parser
, "expected %<@end%>");
30248 objc_finish_interface ();
30251 /* Parse an Objective-C method definition list. */
30254 cp_parser_objc_method_definition_list (cp_parser
* parser
)
30256 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30258 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30262 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
30265 tree sig
, attribute
;
30266 bool is_class_method
;
30267 if (token
->type
== CPP_PLUS
)
30268 is_class_method
= true;
30270 is_class_method
= false;
30271 push_deferring_access_checks (dk_deferred
);
30272 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
30273 if (sig
== error_mark_node
)
30275 cp_parser_skip_to_end_of_block_or_statement (parser
);
30276 token
= cp_lexer_peek_token (parser
->lexer
);
30279 objc_start_method_definition (is_class_method
, sig
, attribute
,
30282 /* For historical reasons, we accept an optional semicolon. */
30283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30284 cp_lexer_consume_token (parser
->lexer
);
30286 ptk
= cp_lexer_peek_token (parser
->lexer
);
30287 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
30288 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
30290 perform_deferred_access_checks (tf_warning_or_error
);
30291 stop_deferring_access_checks ();
30292 meth
= cp_parser_function_definition_after_declarator (parser
,
30294 pop_deferring_access_checks ();
30295 objc_finish_method_definition (meth
);
30298 /* The following case will be removed once @synthesize is
30299 completely implemented. */
30300 else if (token
->keyword
== RID_AT_PROPERTY
)
30301 cp_parser_objc_at_property_declaration (parser
);
30302 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
30303 cp_parser_objc_at_synthesize_declaration (parser
);
30304 else if (token
->keyword
== RID_AT_DYNAMIC
)
30305 cp_parser_objc_at_dynamic_declaration (parser
);
30306 else if (token
->keyword
== RID_ATTRIBUTE
30307 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
30308 warning_at (token
->location
, OPT_Wattributes
,
30309 "prefix attributes are ignored for methods");
30311 /* Allow for interspersed non-ObjC++ code. */
30312 cp_parser_objc_interstitial_code (parser
);
30314 token
= cp_lexer_peek_token (parser
->lexer
);
30317 if (token
->type
!= CPP_EOF
)
30318 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30320 cp_parser_error (parser
, "expected %<@end%>");
30322 objc_finish_implementation ();
30325 /* Parse Objective-C ivars. */
30328 cp_parser_objc_class_ivars (cp_parser
* parser
)
30330 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30332 if (token
->type
!= CPP_OPEN_BRACE
)
30333 return; /* No ivars specified. */
30335 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
30336 token
= cp_lexer_peek_token (parser
->lexer
);
30338 while (token
->type
!= CPP_CLOSE_BRACE
30339 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30341 cp_decl_specifier_seq declspecs
;
30342 int decl_class_or_enum_p
;
30343 tree prefix_attributes
;
30345 cp_parser_objc_visibility_spec (parser
);
30347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30350 cp_parser_decl_specifier_seq (parser
,
30351 CP_PARSER_FLAGS_OPTIONAL
,
30353 &decl_class_or_enum_p
);
30355 /* auto, register, static, extern, mutable. */
30356 if (declspecs
.storage_class
!= sc_none
)
30358 cp_parser_error (parser
, "invalid type for instance variable");
30359 declspecs
.storage_class
= sc_none
;
30362 /* thread_local. */
30363 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30365 cp_parser_error (parser
, "invalid type for instance variable");
30366 declspecs
.locations
[ds_thread
] = 0;
30370 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30372 cp_parser_error (parser
, "invalid type for instance variable");
30373 declspecs
.locations
[ds_typedef
] = 0;
30376 prefix_attributes
= declspecs
.attributes
;
30377 declspecs
.attributes
= NULL_TREE
;
30379 /* Keep going until we hit the `;' at the end of the
30381 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30383 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
30384 cp_declarator
*declarator
= NULL
;
30385 int ctor_dtor_or_conv_p
;
30387 /* Check for a (possibly unnamed) bitfield declaration. */
30388 token
= cp_lexer_peek_token (parser
->lexer
);
30389 if (token
->type
== CPP_COLON
)
30392 if (token
->type
== CPP_NAME
30393 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
30396 /* Get the name of the bitfield. */
30397 declarator
= make_id_declarator (NULL_TREE
,
30398 cp_parser_identifier (parser
),
30402 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30403 /* Get the width of the bitfield. */
30405 = cp_parser_constant_expression (parser
);
30409 /* Parse the declarator. */
30411 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30412 &ctor_dtor_or_conv_p
,
30413 /*parenthesized_p=*/NULL
,
30414 /*member_p=*/false,
30415 /*friend_p=*/false);
30418 /* Look for attributes that apply to the ivar. */
30419 attributes
= cp_parser_attributes_opt (parser
);
30420 /* Remember which attributes are prefix attributes and
30422 first_attribute
= attributes
;
30423 /* Combine the attributes. */
30424 attributes
= attr_chainon (prefix_attributes
, attributes
);
30427 /* Create the bitfield declaration. */
30428 decl
= grokbitfield (declarator
, &declspecs
,
30429 width
, NULL_TREE
, attributes
);
30431 decl
= grokfield (declarator
, &declspecs
,
30432 NULL_TREE
, /*init_const_expr_p=*/false,
30433 NULL_TREE
, attributes
);
30435 /* Add the instance variable. */
30436 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
30437 objc_add_instance_variable (decl
);
30439 /* Reset PREFIX_ATTRIBUTES. */
30440 if (attributes
!= error_mark_node
)
30442 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30443 attributes
= TREE_CHAIN (attributes
);
30445 TREE_CHAIN (attributes
) = NULL_TREE
;
30448 token
= cp_lexer_peek_token (parser
->lexer
);
30450 if (token
->type
== CPP_COMMA
)
30452 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
30458 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30459 token
= cp_lexer_peek_token (parser
->lexer
);
30462 if (token
->keyword
== RID_AT_END
)
30463 cp_parser_error (parser
, "expected %<}%>");
30465 /* Do not consume the RID_AT_END, so it will be read again as terminating
30466 the @interface of @implementation. */
30467 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
30468 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
30470 /* For historical reasons, we accept an optional semicolon. */
30471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30472 cp_lexer_consume_token (parser
->lexer
);
30475 /* Parse an Objective-C protocol declaration. */
30478 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
30480 tree proto
, protorefs
;
30483 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
30484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
30486 tok
= cp_lexer_peek_token (parser
->lexer
);
30487 error_at (tok
->location
, "identifier expected after %<@protocol%>");
30488 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30492 /* See if we have a forward declaration or a definition. */
30493 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
30495 /* Try a forward declaration first. */
30496 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
30502 id
= cp_parser_identifier (parser
);
30503 if (id
== error_mark_node
)
30506 objc_declare_protocol (id
, attributes
);
30508 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30509 cp_lexer_consume_token (parser
->lexer
);
30513 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30516 /* Ok, we got a full-fledged definition (or at least should). */
30519 proto
= cp_parser_identifier (parser
);
30520 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
30521 objc_start_protocol (proto
, protorefs
, attributes
);
30522 cp_parser_objc_method_prototype_list (parser
);
30526 /* Parse an Objective-C superclass or category. */
30529 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
30532 tree
*categ
, bool *is_class_extension
)
30534 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
30536 *super
= *categ
= NULL_TREE
;
30537 *is_class_extension
= false;
30538 if (next
->type
== CPP_COLON
)
30540 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
30541 *super
= cp_parser_identifier (parser
);
30543 else if (next
->type
== CPP_OPEN_PAREN
)
30545 matching_parens parens
;
30546 parens
.consume_open (parser
); /* Eat '('. */
30548 /* If there is no category name, and this is an @interface, we
30549 have a class extension. */
30550 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30552 *categ
= NULL_TREE
;
30553 *is_class_extension
= true;
30556 *categ
= cp_parser_identifier (parser
);
30558 parens
.require_close (parser
);
30562 /* Parse an Objective-C class interface. */
30565 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
30567 tree name
, super
, categ
, protos
;
30568 bool is_class_extension
;
30570 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
30571 name
= cp_parser_identifier (parser
);
30572 if (name
== error_mark_node
)
30574 /* It's hard to recover because even if valid @interface stuff
30575 is to follow, we can't compile it (or validate it) if we
30576 don't even know which class it refers to. Let's assume this
30577 was a stray '@interface' token in the stream and skip it.
30581 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
30582 &is_class_extension
);
30583 protos
= cp_parser_objc_protocol_refs_opt (parser
);
30585 /* We have either a class or a category on our hands. */
30586 if (categ
|| is_class_extension
)
30587 objc_start_category_interface (name
, categ
, protos
, attributes
);
30590 objc_start_class_interface (name
, super
, protos
, attributes
);
30591 /* Handle instance variable declarations, if any. */
30592 cp_parser_objc_class_ivars (parser
);
30593 objc_continue_interface ();
30596 cp_parser_objc_method_prototype_list (parser
);
30599 /* Parse an Objective-C class implementation. */
30602 cp_parser_objc_class_implementation (cp_parser
* parser
)
30604 tree name
, super
, categ
;
30605 bool is_class_extension
;
30607 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
30608 name
= cp_parser_identifier (parser
);
30609 if (name
== error_mark_node
)
30611 /* It's hard to recover because even if valid @implementation
30612 stuff is to follow, we can't compile it (or validate it) if
30613 we don't even know which class it refers to. Let's assume
30614 this was a stray '@implementation' token in the stream and
30619 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
30620 &is_class_extension
);
30622 /* We have either a class or a category on our hands. */
30624 objc_start_category_implementation (name
, categ
);
30627 objc_start_class_implementation (name
, super
);
30628 /* Handle instance variable declarations, if any. */
30629 cp_parser_objc_class_ivars (parser
);
30630 objc_continue_implementation ();
30633 cp_parser_objc_method_definition_list (parser
);
30636 /* Consume the @end token and finish off the implementation. */
30639 cp_parser_objc_end_implementation (cp_parser
* parser
)
30641 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
30642 objc_finish_implementation ();
30645 /* Parse an Objective-C declaration. */
30648 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
30650 /* Try to figure out what kind of declaration is present. */
30651 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30654 switch (kwd
->keyword
)
30659 error_at (kwd
->location
, "attributes may not be specified before"
30660 " the %<@%D%> Objective-C++ keyword",
30664 case RID_AT_IMPLEMENTATION
:
30665 warning_at (kwd
->location
, OPT_Wattributes
,
30666 "prefix attributes are ignored before %<@%D%>",
30673 switch (kwd
->keyword
)
30676 cp_parser_objc_alias_declaration (parser
);
30679 cp_parser_objc_class_declaration (parser
);
30681 case RID_AT_PROTOCOL
:
30682 cp_parser_objc_protocol_declaration (parser
, attributes
);
30684 case RID_AT_INTERFACE
:
30685 cp_parser_objc_class_interface (parser
, attributes
);
30687 case RID_AT_IMPLEMENTATION
:
30688 cp_parser_objc_class_implementation (parser
);
30691 cp_parser_objc_end_implementation (parser
);
30694 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30696 cp_parser_skip_to_end_of_block_or_statement (parser
);
30700 /* Parse an Objective-C try-catch-finally statement.
30702 objc-try-catch-finally-stmt:
30703 @try compound-statement objc-catch-clause-seq [opt]
30704 objc-finally-clause [opt]
30706 objc-catch-clause-seq:
30707 objc-catch-clause objc-catch-clause-seq [opt]
30710 @catch ( objc-exception-declaration ) compound-statement
30712 objc-finally-clause:
30713 @finally compound-statement
30715 objc-exception-declaration:
30716 parameter-declaration
30719 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30723 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30724 for C. Keep them in sync. */
30727 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
30729 location_t location
;
30732 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
30733 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30734 objc_maybe_warn_exceptions (location
);
30735 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30736 node, lest it get absorbed into the surrounding block. */
30737 stmt
= push_stmt_list ();
30738 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30739 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
30741 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
30743 cp_parameter_declarator
*parm
;
30744 tree parameter_declaration
= error_mark_node
;
30745 bool seen_open_paren
= false;
30746 matching_parens parens
;
30748 cp_lexer_consume_token (parser
->lexer
);
30749 if (parens
.require_open (parser
))
30750 seen_open_paren
= true;
30751 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30753 /* We have "@catch (...)" (where the '...' are literally
30754 what is in the code). Skip the '...'.
30755 parameter_declaration is set to NULL_TREE, and
30756 objc_being_catch_clauses() knows that that means
30758 cp_lexer_consume_token (parser
->lexer
);
30759 parameter_declaration
= NULL_TREE
;
30763 /* We have "@catch (NSException *exception)" or something
30764 like that. Parse the parameter declaration. */
30765 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
30767 parameter_declaration
= error_mark_node
;
30769 parameter_declaration
= grokdeclarator (parm
->declarator
,
30770 &parm
->decl_specifiers
,
30771 PARM
, /*initialized=*/0,
30772 /*attrlist=*/NULL
);
30774 if (seen_open_paren
)
30775 parens
.require_close (parser
);
30778 /* If there was no open parenthesis, we are recovering from
30779 an error, and we are trying to figure out what mistake
30780 the user has made. */
30782 /* If there is an immediate closing parenthesis, the user
30783 probably forgot the opening one (ie, they typed "@catch
30784 NSException *e)". Parse the closing parenthesis and keep
30786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
30787 cp_lexer_consume_token (parser
->lexer
);
30789 /* If these is no immediate closing parenthesis, the user
30790 probably doesn't know that parenthesis are required at
30791 all (ie, they typed "@catch NSException *e"). So, just
30792 forget about the closing parenthesis and keep going. */
30794 objc_begin_catch_clause (parameter_declaration
);
30795 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30796 objc_finish_catch_clause ();
30798 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
30800 cp_lexer_consume_token (parser
->lexer
);
30801 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30802 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30803 node, lest it get absorbed into the surrounding block. */
30804 stmt
= push_stmt_list ();
30805 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30806 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
30809 return objc_finish_try_stmt ();
30812 /* Parse an Objective-C synchronized statement.
30814 objc-synchronized-stmt:
30815 @synchronized ( expression ) compound-statement
30817 Returns NULL_TREE. */
30820 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
30822 location_t location
;
30825 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
30827 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30828 objc_maybe_warn_exceptions (location
);
30829 matching_parens parens
;
30830 parens
.require_open (parser
);
30831 lock
= cp_parser_expression (parser
);
30832 parens
.require_close (parser
);
30834 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30835 node, lest it get absorbed into the surrounding block. */
30836 stmt
= push_stmt_list ();
30837 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
30839 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
30842 /* Parse an Objective-C throw statement.
30845 @throw assignment-expression [opt] ;
30847 Returns a constructed '@throw' statement. */
30850 cp_parser_objc_throw_statement (cp_parser
*parser
)
30852 tree expr
= NULL_TREE
;
30853 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30855 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
30857 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30858 expr
= cp_parser_expression (parser
);
30860 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30862 return objc_build_throw_stmt (loc
, expr
);
30865 /* Parse an Objective-C statement. */
30868 cp_parser_objc_statement (cp_parser
* parser
)
30870 /* Try to figure out what kind of declaration is present. */
30871 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
30873 switch (kwd
->keyword
)
30876 return cp_parser_objc_try_catch_finally_statement (parser
);
30877 case RID_AT_SYNCHRONIZED
:
30878 return cp_parser_objc_synchronized_statement (parser
);
30880 return cp_parser_objc_throw_statement (parser
);
30882 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
30884 cp_parser_skip_to_end_of_block_or_statement (parser
);
30887 return error_mark_node
;
30890 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30891 look ahead to see if an objc keyword follows the attributes. This
30892 is to detect the use of prefix attributes on ObjC @interface and
30896 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
30898 cp_lexer_save_tokens (parser
->lexer
);
30899 *attrib
= cp_parser_attributes_opt (parser
);
30900 gcc_assert (*attrib
);
30901 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
30903 cp_lexer_commit_tokens (parser
->lexer
);
30906 cp_lexer_rollback_tokens (parser
->lexer
);
30910 /* This routine is a minimal replacement for
30911 c_parser_struct_declaration () used when parsing the list of
30912 types/names or ObjC++ properties. For example, when parsing the
30915 @property (readonly) int a, b, c;
30917 this function is responsible for parsing "int a, int b, int c" and
30918 returning the declarations as CHAIN of DECLs.
30920 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30921 similar parsing. */
30923 cp_parser_objc_struct_declaration (cp_parser
*parser
)
30925 tree decls
= NULL_TREE
;
30926 cp_decl_specifier_seq declspecs
;
30927 int decl_class_or_enum_p
;
30928 tree prefix_attributes
;
30930 cp_parser_decl_specifier_seq (parser
,
30931 CP_PARSER_FLAGS_NONE
,
30933 &decl_class_or_enum_p
);
30935 if (declspecs
.type
== error_mark_node
)
30936 return error_mark_node
;
30938 /* auto, register, static, extern, mutable. */
30939 if (declspecs
.storage_class
!= sc_none
)
30941 cp_parser_error (parser
, "invalid type for property");
30942 declspecs
.storage_class
= sc_none
;
30945 /* thread_local. */
30946 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
30948 cp_parser_error (parser
, "invalid type for property");
30949 declspecs
.locations
[ds_thread
] = 0;
30953 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
30955 cp_parser_error (parser
, "invalid type for property");
30956 declspecs
.locations
[ds_typedef
] = 0;
30959 prefix_attributes
= declspecs
.attributes
;
30960 declspecs
.attributes
= NULL_TREE
;
30962 /* Keep going until we hit the `;' at the end of the declaration. */
30963 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30965 tree attributes
, first_attribute
, decl
;
30966 cp_declarator
*declarator
;
30969 /* Parse the declarator. */
30970 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30971 NULL
, NULL
, false, false);
30973 /* Look for attributes that apply to the ivar. */
30974 attributes
= cp_parser_attributes_opt (parser
);
30975 /* Remember which attributes are prefix attributes and
30977 first_attribute
= attributes
;
30978 /* Combine the attributes. */
30979 attributes
= attr_chainon (prefix_attributes
, attributes
);
30981 decl
= grokfield (declarator
, &declspecs
,
30982 NULL_TREE
, /*init_const_expr_p=*/false,
30983 NULL_TREE
, attributes
);
30985 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
30986 return error_mark_node
;
30988 /* Reset PREFIX_ATTRIBUTES. */
30989 if (attributes
!= error_mark_node
)
30991 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
30992 attributes
= TREE_CHAIN (attributes
);
30994 TREE_CHAIN (attributes
) = NULL_TREE
;
30997 DECL_CHAIN (decl
) = decls
;
31000 token
= cp_lexer_peek_token (parser
->lexer
);
31001 if (token
->type
== CPP_COMMA
)
31003 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31012 /* Parse an Objective-C @property declaration. The syntax is:
31014 objc-property-declaration:
31015 '@property' objc-property-attributes[opt] struct-declaration ;
31017 objc-property-attributes:
31018 '(' objc-property-attribute-list ')'
31020 objc-property-attribute-list:
31021 objc-property-attribute
31022 objc-property-attribute-list, objc-property-attribute
31024 objc-property-attribute
31025 'getter' = identifier
31026 'setter' = identifier
31035 @property NSString *name;
31036 @property (readonly) id object;
31037 @property (retain, nonatomic, getter=getTheName) id name;
31038 @property int a, b, c;
31040 PS: This function is identical to
31041 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31043 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
31045 /* The following variables hold the attributes of the properties as
31046 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31047 seen. When we see an attribute, we set them to 'true' (if they
31048 are boolean properties) or to the identifier (if they have an
31049 argument, ie, for getter and setter). Note that here we only
31050 parse the list of attributes, check the syntax and accumulate the
31051 attributes that we find. objc_add_property_declaration() will
31052 then process the information. */
31053 bool property_assign
= false;
31054 bool property_copy
= false;
31055 tree property_getter_ident
= NULL_TREE
;
31056 bool property_nonatomic
= false;
31057 bool property_readonly
= false;
31058 bool property_readwrite
= false;
31059 bool property_retain
= false;
31060 tree property_setter_ident
= NULL_TREE
;
31062 /* 'properties' is the list of properties that we read. Usually a
31063 single one, but maybe more (eg, in "@property int a, b, c;" there
31068 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31070 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
31072 /* Parse the optional attribute list... */
31073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31076 matching_parens parens
;
31077 parens
.consume_open (parser
);
31081 bool syntax_error
= false;
31082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31085 if (token
->type
!= CPP_NAME
)
31087 cp_parser_error (parser
, "expected identifier");
31090 keyword
= C_RID_CODE (token
->u
.value
);
31091 cp_lexer_consume_token (parser
->lexer
);
31094 case RID_ASSIGN
: property_assign
= true; break;
31095 case RID_COPY
: property_copy
= true; break;
31096 case RID_NONATOMIC
: property_nonatomic
= true; break;
31097 case RID_READONLY
: property_readonly
= true; break;
31098 case RID_READWRITE
: property_readwrite
= true; break;
31099 case RID_RETAIN
: property_retain
= true; break;
31103 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
31105 if (keyword
== RID_GETTER
)
31106 cp_parser_error (parser
,
31107 "missing %<=%> (after %<getter%> attribute)");
31109 cp_parser_error (parser
,
31110 "missing %<=%> (after %<setter%> attribute)");
31111 syntax_error
= true;
31114 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
31115 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
31117 cp_parser_error (parser
, "expected identifier");
31118 syntax_error
= true;
31121 if (keyword
== RID_SETTER
)
31123 if (property_setter_ident
!= NULL_TREE
)
31125 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
31126 cp_lexer_consume_token (parser
->lexer
);
31129 property_setter_ident
= cp_parser_objc_selector (parser
);
31130 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31131 cp_parser_error (parser
, "setter name must terminate with %<:%>");
31133 cp_lexer_consume_token (parser
->lexer
);
31137 if (property_getter_ident
!= NULL_TREE
)
31139 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
31140 cp_lexer_consume_token (parser
->lexer
);
31143 property_getter_ident
= cp_parser_objc_selector (parser
);
31147 cp_parser_error (parser
, "unknown property attribute");
31148 syntax_error
= true;
31155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31156 cp_lexer_consume_token (parser
->lexer
);
31161 /* FIXME: "@property (setter, assign);" will generate a spurious
31162 "error: expected ‘)’ before ‘,’ token". This is because
31163 cp_parser_require, unlike the C counterpart, will produce an
31164 error even if we are in error recovery. */
31165 if (!parens
.require_close (parser
))
31167 cp_parser_skip_to_closing_parenthesis (parser
,
31168 /*recovering=*/true,
31169 /*or_comma=*/false,
31170 /*consume_paren=*/true);
31174 /* ... and the property declaration(s). */
31175 properties
= cp_parser_objc_struct_declaration (parser
);
31177 if (properties
== error_mark_node
)
31179 cp_parser_skip_to_end_of_statement (parser
);
31180 /* If the next token is now a `;', consume it. */
31181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
31182 cp_lexer_consume_token (parser
->lexer
);
31186 if (properties
== NULL_TREE
)
31187 cp_parser_error (parser
, "expected identifier");
31190 /* Comma-separated properties are chained together in
31191 reverse order; add them one by one. */
31192 properties
= nreverse (properties
);
31194 for (; properties
; properties
= TREE_CHAIN (properties
))
31195 objc_add_property_declaration (loc
, copy_node (properties
),
31196 property_readonly
, property_readwrite
,
31197 property_assign
, property_retain
,
31198 property_copy
, property_nonatomic
,
31199 property_getter_ident
, property_setter_ident
);
31202 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31205 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31207 objc-synthesize-declaration:
31208 @synthesize objc-synthesize-identifier-list ;
31210 objc-synthesize-identifier-list:
31211 objc-synthesize-identifier
31212 objc-synthesize-identifier-list, objc-synthesize-identifier
31214 objc-synthesize-identifier
31216 identifier = identifier
31219 @synthesize MyProperty;
31220 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31222 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31223 for C. Keep them in sync.
31226 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
31228 tree list
= NULL_TREE
;
31230 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31232 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
31235 tree property
, ivar
;
31236 property
= cp_parser_identifier (parser
);
31237 if (property
== error_mark_node
)
31239 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
31244 cp_lexer_consume_token (parser
->lexer
);
31245 ivar
= cp_parser_identifier (parser
);
31246 if (ivar
== error_mark_node
)
31248 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31254 list
= chainon (list
, build_tree_list (ivar
, property
));
31255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31256 cp_lexer_consume_token (parser
->lexer
);
31260 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31261 objc_add_synthesize_declaration (loc
, list
);
31264 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31266 objc-dynamic-declaration:
31267 @dynamic identifier-list ;
31270 @dynamic MyProperty;
31271 @dynamic MyProperty, AnotherProperty;
31273 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31274 for C. Keep them in sync.
31277 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
31279 tree list
= NULL_TREE
;
31281 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31283 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
31287 property
= cp_parser_identifier (parser
);
31288 if (property
== error_mark_node
)
31290 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31293 list
= chainon (list
, build_tree_list (NULL
, property
));
31294 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31295 cp_lexer_consume_token (parser
->lexer
);
31299 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31300 objc_add_dynamic_declaration (loc
, list
);
31304 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31306 /* Returns name of the next clause.
31307 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31308 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31309 returned and the token is consumed. */
31311 static pragma_omp_clause
31312 cp_parser_omp_clause_name (cp_parser
*parser
)
31314 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
31316 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31317 result
= PRAGMA_OACC_CLAUSE_AUTO
;
31318 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
31319 result
= PRAGMA_OMP_CLAUSE_IF
;
31320 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
31321 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
31322 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
31323 result
= PRAGMA_OACC_CLAUSE_DELETE
;
31324 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
31325 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
31326 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31327 result
= PRAGMA_OMP_CLAUSE_FOR
;
31328 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31330 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31331 const char *p
= IDENTIFIER_POINTER (id
);
31336 if (!strcmp ("aligned", p
))
31337 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
31338 else if (!strcmp ("async", p
))
31339 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
31342 if (!strcmp ("collapse", p
))
31343 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
31344 else if (!strcmp ("copy", p
))
31345 result
= PRAGMA_OACC_CLAUSE_COPY
;
31346 else if (!strcmp ("copyin", p
))
31347 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
31348 else if (!strcmp ("copyout", p
))
31349 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
31350 else if (!strcmp ("copyprivate", p
))
31351 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
31352 else if (!strcmp ("create", p
))
31353 result
= PRAGMA_OACC_CLAUSE_CREATE
;
31356 if (!strcmp ("defaultmap", p
))
31357 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
31358 else if (!strcmp ("depend", p
))
31359 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
31360 else if (!strcmp ("device", p
))
31361 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
31362 else if (!strcmp ("deviceptr", p
))
31363 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
31364 else if (!strcmp ("device_resident", p
))
31365 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
31366 else if (!strcmp ("dist_schedule", p
))
31367 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
31370 if (!strcmp ("final", p
))
31371 result
= PRAGMA_OMP_CLAUSE_FINAL
;
31372 else if (!strcmp ("firstprivate", p
))
31373 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
31374 else if (!strcmp ("from", p
))
31375 result
= PRAGMA_OMP_CLAUSE_FROM
;
31378 if (!strcmp ("gang", p
))
31379 result
= PRAGMA_OACC_CLAUSE_GANG
;
31380 else if (!strcmp ("grainsize", p
))
31381 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
31384 if (!strcmp ("hint", p
))
31385 result
= PRAGMA_OMP_CLAUSE_HINT
;
31386 else if (!strcmp ("host", p
))
31387 result
= PRAGMA_OACC_CLAUSE_HOST
;
31390 if (!strcmp ("inbranch", p
))
31391 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
31392 else if (!strcmp ("independent", p
))
31393 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
31394 else if (!strcmp ("is_device_ptr", p
))
31395 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
31398 if (!strcmp ("lastprivate", p
))
31399 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
31400 else if (!strcmp ("linear", p
))
31401 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
31402 else if (!strcmp ("link", p
))
31403 result
= PRAGMA_OMP_CLAUSE_LINK
;
31406 if (!strcmp ("map", p
))
31407 result
= PRAGMA_OMP_CLAUSE_MAP
;
31408 else if (!strcmp ("mergeable", p
))
31409 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
31412 if (!strcmp ("nogroup", p
))
31413 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
31414 else if (!strcmp ("notinbranch", p
))
31415 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
31416 else if (!strcmp ("nowait", p
))
31417 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
31418 else if (!strcmp ("num_gangs", p
))
31419 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
31420 else if (!strcmp ("num_tasks", p
))
31421 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
31422 else if (!strcmp ("num_teams", p
))
31423 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
31424 else if (!strcmp ("num_threads", p
))
31425 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
31426 else if (!strcmp ("num_workers", p
))
31427 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
31430 if (!strcmp ("ordered", p
))
31431 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
31434 if (!strcmp ("parallel", p
))
31435 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
31436 else if (!strcmp ("present", p
))
31437 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
31438 else if (!strcmp ("present_or_copy", p
)
31439 || !strcmp ("pcopy", p
))
31440 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
31441 else if (!strcmp ("present_or_copyin", p
)
31442 || !strcmp ("pcopyin", p
))
31443 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
31444 else if (!strcmp ("present_or_copyout", p
)
31445 || !strcmp ("pcopyout", p
))
31446 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
31447 else if (!strcmp ("present_or_create", p
)
31448 || !strcmp ("pcreate", p
))
31449 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
31450 else if (!strcmp ("priority", p
))
31451 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
31452 else if (!strcmp ("proc_bind", p
))
31453 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
31456 if (!strcmp ("reduction", p
))
31457 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
31460 if (!strcmp ("safelen", p
))
31461 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
31462 else if (!strcmp ("schedule", p
))
31463 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
31464 else if (!strcmp ("sections", p
))
31465 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
31466 else if (!strcmp ("self", p
))
31467 result
= PRAGMA_OACC_CLAUSE_SELF
;
31468 else if (!strcmp ("seq", p
))
31469 result
= PRAGMA_OACC_CLAUSE_SEQ
;
31470 else if (!strcmp ("shared", p
))
31471 result
= PRAGMA_OMP_CLAUSE_SHARED
;
31472 else if (!strcmp ("simd", p
))
31473 result
= PRAGMA_OMP_CLAUSE_SIMD
;
31474 else if (!strcmp ("simdlen", p
))
31475 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
31478 if (!strcmp ("taskgroup", p
))
31479 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
31480 else if (!strcmp ("thread_limit", p
))
31481 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
31482 else if (!strcmp ("threads", p
))
31483 result
= PRAGMA_OMP_CLAUSE_THREADS
;
31484 else if (!strcmp ("tile", p
))
31485 result
= PRAGMA_OACC_CLAUSE_TILE
;
31486 else if (!strcmp ("to", p
))
31487 result
= PRAGMA_OMP_CLAUSE_TO
;
31490 if (!strcmp ("uniform", p
))
31491 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
31492 else if (!strcmp ("untied", p
))
31493 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
31494 else if (!strcmp ("use_device", p
))
31495 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
31496 else if (!strcmp ("use_device_ptr", p
))
31497 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
31500 if (!strcmp ("vector", p
))
31501 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
31502 else if (!strcmp ("vector_length", p
))
31503 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
31506 if (!strcmp ("wait", p
))
31507 result
= PRAGMA_OACC_CLAUSE_WAIT
;
31508 else if (!strcmp ("worker", p
))
31509 result
= PRAGMA_OACC_CLAUSE_WORKER
;
31514 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
31515 cp_lexer_consume_token (parser
->lexer
);
31520 /* Validate that a clause of the given type does not already exist. */
31523 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
31524 const char *name
, location_t location
)
31528 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
31529 if (OMP_CLAUSE_CODE (c
) == code
)
31531 error_at (location
, "too many %qs clauses", name
);
31539 variable-list , identifier
31541 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31542 colon). An opening parenthesis will have been consumed by the caller.
31544 If KIND is nonzero, create the appropriate node and install the decl
31545 in OMP_CLAUSE_DECL and add the node to the head of the list.
31547 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31548 return the list created.
31550 COLON can be NULL if only closing parenthesis should end the list,
31551 or pointer to bool which will receive false if the list is terminated
31552 by closing parenthesis or true if the list is terminated by colon. */
31555 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
31556 tree list
, bool *colon
)
31559 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31562 parser
->colon_corrects_to_scope_p
= false;
31569 token
= cp_lexer_peek_token (parser
->lexer
);
31571 && current_class_ptr
31572 && cp_parser_is_keyword (token
, RID_THIS
))
31574 decl
= finish_this_expr ();
31575 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
31576 || CONVERT_EXPR_P (decl
))
31577 decl
= TREE_OPERAND (decl
, 0);
31578 cp_lexer_consume_token (parser
->lexer
);
31582 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31583 /*check_dependency_p=*/true,
31584 /*template_p=*/NULL
,
31585 /*declarator_p=*/false,
31586 /*optional_p=*/false);
31587 if (name
== error_mark_node
)
31590 if (identifier_p (name
))
31591 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31594 if (decl
== error_mark_node
)
31595 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
31598 if (decl
== error_mark_node
)
31600 else if (kind
!= 0)
31604 case OMP_CLAUSE__CACHE_
:
31605 /* The OpenACC cache directive explicitly only allows "array
31606 elements or subarrays". */
31607 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
31609 error_at (token
->location
, "expected %<[%>");
31610 decl
= error_mark_node
;
31614 case OMP_CLAUSE_MAP
:
31615 case OMP_CLAUSE_FROM
:
31616 case OMP_CLAUSE_TO
:
31617 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
31620 = cp_lexer_peek_token (parser
->lexer
)->location
;
31621 cp_id_kind idk
= CP_ID_KIND_NONE
;
31622 cp_lexer_consume_token (parser
->lexer
);
31623 decl
= convert_from_reference (decl
);
31625 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
31630 case OMP_CLAUSE_DEPEND
:
31631 case OMP_CLAUSE_REDUCTION
:
31632 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
31634 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
31636 parser
->colon_corrects_to_scope_p
= false;
31637 cp_lexer_consume_token (parser
->lexer
);
31638 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31639 low_bound
= cp_parser_expression (parser
);
31641 parser
->colon_corrects_to_scope_p
31642 = saved_colon_corrects_to_scope_p
;
31643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
31644 length
= integer_one_node
;
31647 /* Look for `:'. */
31648 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31650 if (!cp_lexer_next_token_is (parser
->lexer
,
31652 length
= cp_parser_expression (parser
);
31654 /* Look for the closing `]'. */
31655 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
31659 decl
= tree_cons (low_bound
, length
, decl
);
31666 tree u
= build_omp_clause (token
->location
, kind
);
31667 OMP_CLAUSE_DECL (u
) = decl
;
31668 OMP_CLAUSE_CHAIN (u
) = list
;
31672 list
= tree_cons (decl
, NULL_TREE
, list
);
31675 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31677 cp_lexer_consume_token (parser
->lexer
);
31681 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31683 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
31686 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31690 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31694 /* Try to resync to an unnested comma. Copied from
31695 cp_parser_parenthesized_expression_list. */
31698 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31699 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
31700 /*recovering=*/true,
31702 /*consume_paren=*/true);
31710 /* Similarly, but expect leading and trailing parenthesis. This is a very
31711 common case for omp clauses. */
31714 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
31716 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31717 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
31722 copy ( variable-list )
31723 copyin ( variable-list )
31724 copyout ( variable-list )
31725 create ( variable-list )
31726 delete ( variable-list )
31727 present ( variable-list )
31728 present_or_copy ( variable-list )
31729 pcopy ( variable-list )
31730 present_or_copyin ( variable-list )
31731 pcopyin ( variable-list )
31732 present_or_copyout ( variable-list )
31733 pcopyout ( variable-list )
31734 present_or_create ( variable-list )
31735 pcreate ( variable-list ) */
31738 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
31741 enum gomp_map_kind kind
;
31744 case PRAGMA_OACC_CLAUSE_COPY
:
31745 kind
= GOMP_MAP_FORCE_TOFROM
;
31747 case PRAGMA_OACC_CLAUSE_COPYIN
:
31748 kind
= GOMP_MAP_FORCE_TO
;
31750 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31751 kind
= GOMP_MAP_FORCE_FROM
;
31753 case PRAGMA_OACC_CLAUSE_CREATE
:
31754 kind
= GOMP_MAP_FORCE_ALLOC
;
31756 case PRAGMA_OACC_CLAUSE_DELETE
:
31757 kind
= GOMP_MAP_DELETE
;
31759 case PRAGMA_OACC_CLAUSE_DEVICE
:
31760 kind
= GOMP_MAP_FORCE_TO
;
31762 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31763 kind
= GOMP_MAP_DEVICE_RESIDENT
;
31765 case PRAGMA_OACC_CLAUSE_HOST
:
31766 case PRAGMA_OACC_CLAUSE_SELF
:
31767 kind
= GOMP_MAP_FORCE_FROM
;
31769 case PRAGMA_OACC_CLAUSE_LINK
:
31770 kind
= GOMP_MAP_LINK
;
31772 case PRAGMA_OACC_CLAUSE_PRESENT
:
31773 kind
= GOMP_MAP_FORCE_PRESENT
;
31775 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31776 kind
= GOMP_MAP_TOFROM
;
31778 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31779 kind
= GOMP_MAP_TO
;
31781 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31782 kind
= GOMP_MAP_FROM
;
31784 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31785 kind
= GOMP_MAP_ALLOC
;
31788 gcc_unreachable ();
31791 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
31793 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31794 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31800 deviceptr ( variable-list ) */
31803 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
31805 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31808 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31809 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31810 variable-list must only allow for pointer variables. */
31811 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31812 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
31814 tree v
= TREE_PURPOSE (t
);
31815 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
31816 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
31817 OMP_CLAUSE_DECL (u
) = v
;
31818 OMP_CLAUSE_CHAIN (u
) = list
;
31832 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
31833 enum omp_clause_code code
,
31834 tree list
, location_t location
)
31836 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31837 tree c
= build_omp_clause (location
, code
);
31838 OMP_CLAUSE_CHAIN (c
) = list
;
31843 num_gangs ( expression )
31844 num_workers ( expression )
31845 vector_length ( expression ) */
31848 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
31849 const char *str
, tree list
)
31851 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31853 matching_parens parens
;
31854 if (!parens
.require_open (parser
))
31857 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
31859 if (t
== error_mark_node
31860 || !parens
.require_close (parser
))
31862 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31863 /*or_comma=*/false,
31864 /*consume_paren=*/true);
31868 check_no_duplicate_clause (list
, code
, str
, loc
);
31870 tree c
= build_omp_clause (loc
, code
);
31871 OMP_CLAUSE_OPERAND (c
, 0) = t
;
31872 OMP_CLAUSE_CHAIN (c
) = list
;
31878 gang [( gang-arg-list )]
31879 worker [( [num:] int-expr )]
31880 vector [( [length:] int-expr )]
31882 where gang-arg is one of:
31887 and size-expr may be:
31894 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
31895 const char *str
, tree list
)
31897 const char *id
= "num";
31898 cp_lexer
*lexer
= parser
->lexer
;
31899 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
31900 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
31902 if (kind
== OMP_CLAUSE_VECTOR
)
31905 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
31907 matching_parens parens
;
31908 parens
.consume_open (parser
);
31912 cp_token
*next
= cp_lexer_peek_token (lexer
);
31915 /* Gang static argument. */
31916 if (kind
== OMP_CLAUSE_GANG
31917 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
31919 cp_lexer_consume_token (lexer
);
31921 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31922 goto cleanup_error
;
31925 if (ops
[idx
] != NULL
)
31927 cp_parser_error (parser
, "too many %<static%> arguments");
31928 goto cleanup_error
;
31931 /* Check for the '*' argument. */
31932 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
31933 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
31934 || cp_lexer_nth_token_is (parser
->lexer
, 2,
31937 cp_lexer_consume_token (lexer
);
31938 ops
[idx
] = integer_minus_one_node
;
31940 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31942 cp_lexer_consume_token (lexer
);
31948 /* Worker num: argument and vector length: arguments. */
31949 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
31950 && id_equal (next
->u
.value
, id
)
31951 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
31953 cp_lexer_consume_token (lexer
); /* id */
31954 cp_lexer_consume_token (lexer
); /* ':' */
31957 /* Now collect the actual argument. */
31958 if (ops
[idx
] != NULL_TREE
)
31960 cp_parser_error (parser
, "unexpected argument");
31961 goto cleanup_error
;
31964 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
31966 if (expr
== error_mark_node
)
31967 goto cleanup_error
;
31969 mark_exp_read (expr
);
31972 if (kind
== OMP_CLAUSE_GANG
31973 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
31975 cp_lexer_consume_token (lexer
);
31982 if (!parens
.require_close (parser
))
31983 goto cleanup_error
;
31986 check_no_duplicate_clause (list
, kind
, str
, loc
);
31988 c
= build_omp_clause (loc
, kind
);
31991 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
31993 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
31994 OMP_CLAUSE_CHAIN (c
) = list
;
31999 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32004 tile ( size-expr-list ) */
32007 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
32009 tree c
, expr
= error_mark_node
;
32010 tree tile
= NULL_TREE
;
32012 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32013 so, but the spec authors never considered such a case and have
32014 differing opinions on what it might mean, including 'not
32016 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
32017 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
32020 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32025 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
32028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
32029 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
32030 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
32032 cp_lexer_consume_token (parser
->lexer
);
32033 expr
= integer_zero_node
;
32036 expr
= cp_parser_constant_expression (parser
);
32038 tile
= tree_cons (NULL_TREE
, expr
, tile
);
32040 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
32042 /* Consume the trailing ')'. */
32043 cp_lexer_consume_token (parser
->lexer
);
32045 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
32046 tile
= nreverse (tile
);
32047 OMP_CLAUSE_TILE_LIST (c
) = tile
;
32048 OMP_CLAUSE_CHAIN (c
) = list
;
32053 Parse wait clause or directive parameters. */
32056 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
32058 vec
<tree
, va_gc
> *args
;
32061 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
32063 /*allow_expansion_p=*/true,
32064 /*non_constant_p=*/NULL
);
32066 if (args
== NULL
|| args
->length () == 0)
32068 cp_parser_error (parser
, "expected integer expression before ')'");
32070 release_tree_vector (args
);
32074 args_tree
= build_tree_list_vec (args
);
32076 release_tree_vector (args
);
32078 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
32080 tree targ
= TREE_VALUE (t
);
32082 if (targ
!= error_mark_node
)
32084 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
32085 error ("%<wait%> expression must be integral");
32088 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
32090 targ
= mark_rvalue_use (targ
);
32091 OMP_CLAUSE_DECL (c
) = targ
;
32092 OMP_CLAUSE_CHAIN (c
) = list
;
32102 wait ( int-expr-list ) */
32105 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
32107 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32109 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
32112 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
32118 collapse ( constant-expression ) */
32121 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
32127 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32128 matching_parens parens
;
32129 if (!parens
.require_open (parser
))
32132 num
= cp_parser_constant_expression (parser
);
32134 if (!parens
.require_close (parser
))
32135 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32136 /*or_comma=*/false,
32137 /*consume_paren=*/true);
32139 if (num
== error_mark_node
)
32141 num
= fold_non_dependent_expr (num
);
32142 if (!tree_fits_shwi_p (num
)
32143 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32144 || (n
= tree_to_shwi (num
)) <= 0
32147 error_at (loc
, "collapse argument needs positive constant integer expression");
32151 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
32152 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
32153 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
32154 OMP_CLAUSE_CHAIN (c
) = list
;
32155 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
32161 default ( none | shared )
32164 default ( none | present ) */
32167 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
32168 location_t location
, bool is_oacc
)
32170 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
32173 matching_parens parens
;
32174 if (!parens
.require_open (parser
))
32176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32178 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32179 const char *p
= IDENTIFIER_POINTER (id
);
32184 if (strcmp ("none", p
) != 0)
32186 kind
= OMP_CLAUSE_DEFAULT_NONE
;
32190 if (strcmp ("present", p
) != 0 || !is_oacc
)
32192 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
32196 if (strcmp ("shared", p
) != 0 || is_oacc
)
32198 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
32205 cp_lexer_consume_token (parser
->lexer
);
32211 cp_parser_error (parser
, "expected %<none%> or %<present%>");
32213 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
32216 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
32217 || !parens
.require_close (parser
))
32218 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32219 /*or_comma=*/false,
32220 /*consume_paren=*/true);
32222 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
32225 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
32226 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
32227 OMP_CLAUSE_CHAIN (c
) = list
;
32228 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
32234 final ( expression ) */
32237 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
32241 matching_parens parens
;
32242 if (!parens
.require_open (parser
))
32245 t
= cp_parser_condition (parser
);
32247 if (t
== error_mark_node
32248 || !parens
.require_close (parser
))
32249 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32250 /*or_comma=*/false,
32251 /*consume_paren=*/true);
32253 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
32255 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
32256 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
32257 OMP_CLAUSE_CHAIN (c
) = list
;
32266 if ( directive-name-modifier : expression )
32268 directive-name-modifier:
32269 parallel | task | taskloop | target data | target | target update
32270 | target enter data | target exit data */
32273 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
32277 enum tree_code if_modifier
= ERROR_MARK
;
32279 matching_parens parens
;
32280 if (!parens
.require_open (parser
))
32283 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32285 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32286 const char *p
= IDENTIFIER_POINTER (id
);
32289 if (strcmp ("parallel", p
) == 0)
32290 if_modifier
= OMP_PARALLEL
;
32291 else if (strcmp ("task", p
) == 0)
32292 if_modifier
= OMP_TASK
;
32293 else if (strcmp ("taskloop", p
) == 0)
32294 if_modifier
= OMP_TASKLOOP
;
32295 else if (strcmp ("target", p
) == 0)
32297 if_modifier
= OMP_TARGET
;
32298 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
32300 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
32301 p
= IDENTIFIER_POINTER (id
);
32302 if (strcmp ("data", p
) == 0)
32303 if_modifier
= OMP_TARGET_DATA
;
32304 else if (strcmp ("update", p
) == 0)
32305 if_modifier
= OMP_TARGET_UPDATE
;
32306 else if (strcmp ("enter", p
) == 0)
32307 if_modifier
= OMP_TARGET_ENTER_DATA
;
32308 else if (strcmp ("exit", p
) == 0)
32309 if_modifier
= OMP_TARGET_EXIT_DATA
;
32310 if (if_modifier
!= OMP_TARGET
)
32315 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
32316 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
32318 if_modifier
= ERROR_MARK
;
32320 if (if_modifier
== OMP_TARGET_ENTER_DATA
32321 || if_modifier
== OMP_TARGET_EXIT_DATA
)
32323 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
32325 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
32326 p
= IDENTIFIER_POINTER (id
);
32327 if (strcmp ("data", p
) == 0)
32333 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
32334 error_at (loc
, "expected %<data%>");
32335 if_modifier
= ERROR_MARK
;
32340 if (if_modifier
!= ERROR_MARK
)
32342 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
32345 cp_lexer_consume_token (parser
->lexer
);
32352 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
32353 error_at (loc
, "expected %<:%>");
32355 if_modifier
= ERROR_MARK
;
32360 t
= cp_parser_condition (parser
);
32362 if (t
== error_mark_node
32363 || !parens
.require_close (parser
))
32364 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
32369 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
32371 if (if_modifier
!= ERROR_MARK
32372 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32374 const char *p
= NULL
;
32375 switch (if_modifier
)
32377 case OMP_PARALLEL
: p
= "parallel"; break;
32378 case OMP_TASK
: p
= "task"; break;
32379 case OMP_TASKLOOP
: p
= "taskloop"; break;
32380 case OMP_TARGET_DATA
: p
= "target data"; break;
32381 case OMP_TARGET
: p
= "target"; break;
32382 case OMP_TARGET_UPDATE
: p
= "target update"; break;
32383 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
32384 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
32385 default: gcc_unreachable ();
32387 error_at (location
, "too many %<if%> clauses with %qs modifier",
32391 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
32394 error_at (location
, "too many %<if%> clauses");
32396 error_at (location
, "too many %<if%> clauses without modifier");
32399 else if (if_modifier
== ERROR_MARK
32400 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
32402 error_at (location
, "if any %<if%> clause has modifier, then all "
32403 "%<if%> clauses have to use modifier");
32408 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
32409 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
32410 OMP_CLAUSE_IF_EXPR (c
) = t
;
32411 OMP_CLAUSE_CHAIN (c
) = list
;
32420 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
32421 tree list
, location_t location
)
32425 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
32428 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
32429 OMP_CLAUSE_CHAIN (c
) = list
;
32437 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
32438 tree list
, location_t location
)
32442 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
32444 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
32445 OMP_CLAUSE_CHAIN (c
) = list
;
32450 num_threads ( expression ) */
32453 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
32454 location_t location
)
32458 matching_parens parens
;
32459 if (!parens
.require_open (parser
))
32462 t
= cp_parser_expression (parser
);
32464 if (t
== error_mark_node
32465 || !parens
.require_close (parser
))
32466 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32467 /*or_comma=*/false,
32468 /*consume_paren=*/true);
32470 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
32471 "num_threads", location
);
32473 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
32474 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
32475 OMP_CLAUSE_CHAIN (c
) = list
;
32481 num_tasks ( expression ) */
32484 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
32485 location_t location
)
32489 matching_parens parens
;
32490 if (!parens
.require_open (parser
))
32493 t
= cp_parser_expression (parser
);
32495 if (t
== error_mark_node
32496 || !parens
.require_close (parser
))
32497 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32498 /*or_comma=*/false,
32499 /*consume_paren=*/true);
32501 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
32502 "num_tasks", location
);
32504 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
32505 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
32506 OMP_CLAUSE_CHAIN (c
) = list
;
32512 grainsize ( expression ) */
32515 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
32516 location_t location
)
32520 matching_parens parens
;
32521 if (!parens
.require_open (parser
))
32524 t
= cp_parser_expression (parser
);
32526 if (t
== error_mark_node
32527 || !parens
.require_close (parser
))
32528 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32529 /*or_comma=*/false,
32530 /*consume_paren=*/true);
32532 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
32533 "grainsize", location
);
32535 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
32536 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
32537 OMP_CLAUSE_CHAIN (c
) = list
;
32543 priority ( expression ) */
32546 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
32547 location_t location
)
32551 matching_parens parens
;
32552 if (!parens
.require_open (parser
))
32555 t
= cp_parser_expression (parser
);
32557 if (t
== error_mark_node
32558 || !parens
.require_close (parser
))
32559 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32560 /*or_comma=*/false,
32561 /*consume_paren=*/true);
32563 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
32564 "priority", location
);
32566 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
32567 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
32568 OMP_CLAUSE_CHAIN (c
) = list
;
32574 hint ( expression ) */
32577 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
32578 location_t location
)
32582 matching_parens parens
;
32583 if (!parens
.require_open (parser
))
32586 t
= cp_parser_expression (parser
);
32588 if (t
== error_mark_node
32589 || !parens
.require_close (parser
))
32590 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32591 /*or_comma=*/false,
32592 /*consume_paren=*/true);
32594 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
32596 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
32597 OMP_CLAUSE_HINT_EXPR (c
) = t
;
32598 OMP_CLAUSE_CHAIN (c
) = list
;
32604 defaultmap ( tofrom : scalar ) */
32607 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
32608 location_t location
)
32613 matching_parens parens
;
32614 if (!parens
.require_open (parser
))
32617 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32619 cp_parser_error (parser
, "expected %<tofrom%>");
32622 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32623 p
= IDENTIFIER_POINTER (id
);
32624 if (strcmp (p
, "tofrom") != 0)
32626 cp_parser_error (parser
, "expected %<tofrom%>");
32629 cp_lexer_consume_token (parser
->lexer
);
32630 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32633 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32635 cp_parser_error (parser
, "expected %<scalar%>");
32638 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32639 p
= IDENTIFIER_POINTER (id
);
32640 if (strcmp (p
, "scalar") != 0)
32642 cp_parser_error (parser
, "expected %<scalar%>");
32645 cp_lexer_consume_token (parser
->lexer
);
32646 if (!parens
.require_close (parser
))
32649 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
32652 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
32653 OMP_CLAUSE_CHAIN (c
) = list
;
32657 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32658 /*or_comma=*/false,
32659 /*consume_paren=*/true);
32667 ordered ( constant-expression ) */
32670 cp_parser_omp_clause_ordered (cp_parser
*parser
,
32671 tree list
, location_t location
)
32673 tree c
, num
= NULL_TREE
;
32676 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
32677 "ordered", location
);
32679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32681 matching_parens parens
;
32682 parens
.consume_open (parser
);
32684 num
= cp_parser_constant_expression (parser
);
32686 if (!parens
.require_close (parser
))
32687 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32688 /*or_comma=*/false,
32689 /*consume_paren=*/true);
32691 if (num
== error_mark_node
)
32693 num
= fold_non_dependent_expr (num
);
32694 if (!tree_fits_shwi_p (num
)
32695 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
32696 || (n
= tree_to_shwi (num
)) <= 0
32699 error_at (location
,
32700 "ordered argument needs positive constant integer "
32706 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
32707 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
32708 OMP_CLAUSE_CHAIN (c
) = list
;
32713 reduction ( reduction-operator : variable-list )
32715 reduction-operator:
32716 One of: + * - & ^ | && ||
32720 reduction-operator:
32721 One of: + * - & ^ | && || min max
32725 reduction-operator:
32726 One of: + * - & ^ | && ||
32730 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
32732 enum tree_code code
= ERROR_MARK
;
32733 tree nlist
, c
, id
= NULL_TREE
;
32735 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32738 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32740 case CPP_PLUS
: code
= PLUS_EXPR
; break;
32741 case CPP_MULT
: code
= MULT_EXPR
; break;
32742 case CPP_MINUS
: code
= MINUS_EXPR
; break;
32743 case CPP_AND
: code
= BIT_AND_EXPR
; break;
32744 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
32745 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
32746 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
32747 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
32751 if (code
!= ERROR_MARK
)
32752 cp_lexer_consume_token (parser
->lexer
);
32755 bool saved_colon_corrects_to_scope_p
;
32756 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32757 parser
->colon_corrects_to_scope_p
= false;
32758 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
32759 /*check_dependency_p=*/true,
32760 /*template_p=*/NULL
,
32761 /*declarator_p=*/false,
32762 /*optional_p=*/false);
32763 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32764 if (identifier_p (id
))
32766 const char *p
= IDENTIFIER_POINTER (id
);
32768 if (strcmp (p
, "min") == 0)
32770 else if (strcmp (p
, "max") == 0)
32772 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
32774 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
32776 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
32778 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
32779 code
= BIT_AND_EXPR
;
32780 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
32781 code
= BIT_IOR_EXPR
;
32782 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
32783 code
= BIT_XOR_EXPR
;
32784 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
32785 code
= TRUTH_ANDIF_EXPR
;
32786 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
32787 code
= TRUTH_ORIF_EXPR
;
32788 id
= omp_reduction_id (code
, id
, NULL_TREE
);
32789 tree scope
= parser
->scope
;
32791 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
32792 parser
->scope
= NULL_TREE
;
32793 parser
->qualifying_scope
= NULL_TREE
;
32794 parser
->object_scope
= NULL_TREE
;
32798 error ("invalid reduction-identifier");
32800 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32801 /*or_comma=*/false,
32802 /*consume_paren=*/true);
32807 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32810 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
32812 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
32814 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
32815 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
32822 schedule ( schedule-kind )
32823 schedule ( schedule-kind , expression )
32826 static | dynamic | guided | runtime | auto
32829 schedule ( schedule-modifier : schedule-kind )
32830 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32838 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
32841 int modifiers
= 0, nmodifiers
= 0;
32843 matching_parens parens
;
32844 if (!parens
.require_open (parser
))
32847 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
32849 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32851 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32852 const char *p
= IDENTIFIER_POINTER (id
);
32853 if (strcmp ("simd", p
) == 0)
32854 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
32855 else if (strcmp ("monotonic", p
) == 0)
32856 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
32857 else if (strcmp ("nonmonotonic", p
) == 0)
32858 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
32861 cp_lexer_consume_token (parser
->lexer
);
32862 if (nmodifiers
++ == 0
32863 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32864 cp_lexer_consume_token (parser
->lexer
);
32867 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32874 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32875 const char *p
= IDENTIFIER_POINTER (id
);
32880 if (strcmp ("dynamic", p
) != 0)
32882 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
32886 if (strcmp ("guided", p
) != 0)
32888 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
32892 if (strcmp ("runtime", p
) != 0)
32894 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
32901 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
32902 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
32903 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
32904 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
32907 cp_lexer_consume_token (parser
->lexer
);
32909 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
32910 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32911 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32912 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
32914 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32919 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32922 cp_lexer_consume_token (parser
->lexer
);
32924 token
= cp_lexer_peek_token (parser
->lexer
);
32925 t
= cp_parser_assignment_expression (parser
);
32927 if (t
== error_mark_node
)
32929 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
32930 error_at (token
->location
, "schedule %<runtime%> does not take "
32931 "a %<chunk_size%> parameter");
32932 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
32933 error_at (token
->location
, "schedule %<auto%> does not take "
32934 "a %<chunk_size%> parameter");
32936 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
32938 if (!parens
.require_close (parser
))
32941 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32944 OMP_CLAUSE_SCHEDULE_KIND (c
)
32945 = (enum omp_clause_schedule_kind
)
32946 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
32948 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
32949 OMP_CLAUSE_CHAIN (c
) = list
;
32953 cp_parser_error (parser
, "invalid schedule kind");
32955 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32956 /*or_comma=*/false,
32957 /*consume_paren=*/true);
32965 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
32966 tree list
, location_t location
)
32970 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
32972 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
32973 OMP_CLAUSE_CHAIN (c
) = list
;
32982 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
32983 tree list
, location_t location
)
32985 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
32986 tree c
= build_omp_clause (location
, code
);
32987 OMP_CLAUSE_CHAIN (c
) = list
;
32998 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
32999 enum omp_clause_code code
,
33000 tree list
, location_t location
)
33002 tree c
= build_omp_clause (location
, code
);
33003 OMP_CLAUSE_CHAIN (c
) = list
;
33011 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
33012 tree list
, location_t location
)
33014 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
33015 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
33016 OMP_CLAUSE_CHAIN (c
) = list
;
33025 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
33026 enum omp_clause_code code
,
33027 tree list
, location_t location
)
33029 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
33030 tree c
= build_omp_clause (location
, code
);
33031 OMP_CLAUSE_CHAIN (c
) = list
;
33036 num_teams ( expression ) */
33039 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
33040 location_t location
)
33044 matching_parens parens
;
33045 if (!parens
.require_open (parser
))
33048 t
= cp_parser_expression (parser
);
33050 if (t
== error_mark_node
33051 || !parens
.require_close (parser
))
33052 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33053 /*or_comma=*/false,
33054 /*consume_paren=*/true);
33056 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
33057 "num_teams", location
);
33059 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
33060 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
33061 OMP_CLAUSE_CHAIN (c
) = list
;
33067 thread_limit ( expression ) */
33070 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
33071 location_t location
)
33075 matching_parens parens
;
33076 if (!parens
.require_open (parser
))
33079 t
= cp_parser_expression (parser
);
33081 if (t
== error_mark_node
33082 || !parens
.require_close (parser
))
33083 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33084 /*or_comma=*/false,
33085 /*consume_paren=*/true);
33087 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
33088 "thread_limit", location
);
33090 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
33091 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
33092 OMP_CLAUSE_CHAIN (c
) = list
;
33098 aligned ( variable-list )
33099 aligned ( variable-list : constant-expression ) */
33102 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
33104 tree nlist
, c
, alignment
= NULL_TREE
;
33107 matching_parens parens
;
33108 if (!parens
.require_open (parser
))
33111 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
33116 alignment
= cp_parser_constant_expression (parser
);
33118 if (!parens
.require_close (parser
))
33119 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33120 /*or_comma=*/false,
33121 /*consume_paren=*/true);
33123 if (alignment
== error_mark_node
)
33124 alignment
= NULL_TREE
;
33127 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33128 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
33134 linear ( variable-list )
33135 linear ( variable-list : expression )
33138 linear ( modifier ( variable-list ) )
33139 linear ( modifier ( variable-list ) : expression ) */
33142 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
33145 tree nlist
, c
, step
= integer_one_node
;
33147 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33149 matching_parens parens
;
33150 if (!parens
.require_open (parser
))
33153 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33155 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33156 const char *p
= IDENTIFIER_POINTER (id
);
33158 if (strcmp ("ref", p
) == 0)
33159 kind
= OMP_CLAUSE_LINEAR_REF
;
33160 else if (strcmp ("val", p
) == 0)
33161 kind
= OMP_CLAUSE_LINEAR_VAL
;
33162 else if (strcmp ("uval", p
) == 0)
33163 kind
= OMP_CLAUSE_LINEAR_UVAL
;
33164 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
33165 cp_lexer_consume_token (parser
->lexer
);
33167 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
33170 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
33171 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
33175 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
33176 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
33178 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33179 else if (!parens
.require_close (parser
))
33180 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33181 /*or_comma=*/false,
33182 /*consume_paren=*/true);
33189 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33190 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
33192 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33193 cp_parser_parse_tentatively (parser
);
33194 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
33195 /*check_dependency_p=*/true,
33196 /*template_p=*/NULL
,
33197 /*declarator_p=*/false,
33198 /*optional_p=*/false);
33199 if (step
!= error_mark_node
)
33200 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
33201 if (step
== error_mark_node
)
33204 cp_parser_abort_tentative_parse (parser
);
33206 else if (!cp_parser_parse_definitely (parser
))
33210 step
= cp_parser_expression (parser
);
33212 if (!parens
.require_close (parser
))
33213 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33214 /*or_comma=*/false,
33215 /*consume_paren=*/true);
33217 if (step
== error_mark_node
)
33221 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33223 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
33224 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
33231 safelen ( constant-expression ) */
33234 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
33235 location_t location
)
33239 matching_parens parens
;
33240 if (!parens
.require_open (parser
))
33243 t
= cp_parser_constant_expression (parser
);
33245 if (t
== error_mark_node
33246 || !parens
.require_close (parser
))
33247 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33248 /*or_comma=*/false,
33249 /*consume_paren=*/true);
33251 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
33253 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
33254 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
33255 OMP_CLAUSE_CHAIN (c
) = list
;
33261 simdlen ( constant-expression ) */
33264 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
33265 location_t location
)
33269 matching_parens parens
;
33270 if (!parens
.require_open (parser
))
33273 t
= cp_parser_constant_expression (parser
);
33275 if (t
== error_mark_node
33276 || !parens
.require_close (parser
))
33277 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33278 /*or_comma=*/false,
33279 /*consume_paren=*/true);
33281 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
33283 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
33284 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
33285 OMP_CLAUSE_CHAIN (c
) = list
;
33292 identifier [+/- integer]
33293 vec , identifier [+/- integer]
33297 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
33302 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33304 cp_parser_error (parser
, "expected identifier");
33308 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33310 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33311 tree t
, identifier
= cp_parser_identifier (parser
);
33312 tree addend
= NULL
;
33314 if (identifier
== error_mark_node
)
33315 t
= error_mark_node
;
33318 t
= cp_parser_lookup_name_simple
33319 (parser
, identifier
,
33320 cp_lexer_peek_token (parser
->lexer
)->location
);
33321 if (t
== error_mark_node
)
33322 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
33327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33329 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
33331 addend
= integer_zero_node
;
33332 goto add_to_vector
;
33334 cp_lexer_consume_token (parser
->lexer
);
33336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
33338 cp_parser_error (parser
, "expected integer");
33342 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33343 if (TREE_CODE (addend
) != INTEGER_CST
)
33345 cp_parser_error (parser
, "expected integer");
33348 cp_lexer_consume_token (parser
->lexer
);
33351 if (t
!= error_mark_node
)
33353 vec
= tree_cons (addend
, t
, vec
);
33355 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
33358 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33361 cp_lexer_consume_token (parser
->lexer
);
33364 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
33366 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
33367 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
33368 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
33369 OMP_CLAUSE_CHAIN (u
) = list
;
33376 depend ( depend-kind : variable-list )
33384 depend ( sink : vec ) */
33387 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
33390 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
33392 matching_parens parens
;
33393 if (!parens
.require_open (parser
))
33396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33398 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33399 const char *p
= IDENTIFIER_POINTER (id
);
33401 if (strcmp ("in", p
) == 0)
33402 kind
= OMP_CLAUSE_DEPEND_IN
;
33403 else if (strcmp ("inout", p
) == 0)
33404 kind
= OMP_CLAUSE_DEPEND_INOUT
;
33405 else if (strcmp ("out", p
) == 0)
33406 kind
= OMP_CLAUSE_DEPEND_OUT
;
33407 else if (strcmp ("source", p
) == 0)
33408 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
33409 else if (strcmp ("sink", p
) == 0)
33410 kind
= OMP_CLAUSE_DEPEND_SINK
;
33417 cp_lexer_consume_token (parser
->lexer
);
33419 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
33421 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
33422 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33423 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
33424 OMP_CLAUSE_CHAIN (c
) = list
;
33425 if (!parens
.require_close (parser
))
33426 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33427 /*or_comma=*/false,
33428 /*consume_paren=*/true);
33432 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33435 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
33436 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
33439 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
33442 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33443 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
33448 cp_parser_error (parser
, "invalid depend kind");
33450 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33451 /*or_comma=*/false,
33452 /*consume_paren=*/true);
33457 map ( map-kind : variable-list )
33458 map ( variable-list )
33461 alloc | to | from | tofrom
33465 alloc | to | from | tofrom | release | delete
33467 map ( always [,] map-kind: variable-list ) */
33470 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
33473 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
33474 bool always
= false;
33476 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33479 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33481 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33482 const char *p
= IDENTIFIER_POINTER (id
);
33484 if (strcmp ("always", p
) == 0)
33487 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
33489 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
33490 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
33492 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
33496 cp_lexer_consume_token (parser
->lexer
);
33498 cp_lexer_consume_token (parser
->lexer
);
33503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
33504 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33506 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33507 const char *p
= IDENTIFIER_POINTER (id
);
33509 if (strcmp ("alloc", p
) == 0)
33510 kind
= GOMP_MAP_ALLOC
;
33511 else if (strcmp ("to", p
) == 0)
33512 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
33513 else if (strcmp ("from", p
) == 0)
33514 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
33515 else if (strcmp ("tofrom", p
) == 0)
33516 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
33517 else if (strcmp ("release", p
) == 0)
33518 kind
= GOMP_MAP_RELEASE
;
33521 cp_parser_error (parser
, "invalid map kind");
33522 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33523 /*or_comma=*/false,
33524 /*consume_paren=*/true);
33527 cp_lexer_consume_token (parser
->lexer
);
33528 cp_lexer_consume_token (parser
->lexer
);
33530 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
33531 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
33533 kind
= GOMP_MAP_DELETE
;
33534 cp_lexer_consume_token (parser
->lexer
);
33535 cp_lexer_consume_token (parser
->lexer
);
33538 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
33541 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33542 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33548 device ( expression ) */
33551 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
33552 location_t location
)
33556 matching_parens parens
;
33557 if (!parens
.require_open (parser
))
33560 t
= cp_parser_expression (parser
);
33562 if (t
== error_mark_node
33563 || !parens
.require_close (parser
))
33564 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33565 /*or_comma=*/false,
33566 /*consume_paren=*/true);
33568 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
33569 "device", location
);
33571 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
33572 OMP_CLAUSE_DEVICE_ID (c
) = t
;
33573 OMP_CLAUSE_CHAIN (c
) = list
;
33579 dist_schedule ( static )
33580 dist_schedule ( static , expression ) */
33583 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
33584 location_t location
)
33588 matching_parens parens
;
33589 if (!parens
.require_open (parser
))
33592 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
33594 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
33596 cp_lexer_consume_token (parser
->lexer
);
33598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33600 cp_lexer_consume_token (parser
->lexer
);
33602 t
= cp_parser_assignment_expression (parser
);
33604 if (t
== error_mark_node
)
33606 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
33608 if (!parens
.require_close (parser
))
33611 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33614 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
33616 OMP_CLAUSE_CHAIN (c
) = list
;
33620 cp_parser_error (parser
, "invalid dist_schedule kind");
33622 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33623 /*or_comma=*/false,
33624 /*consume_paren=*/true);
33629 proc_bind ( proc-bind-kind )
33632 master | close | spread */
33635 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
33636 location_t location
)
33639 enum omp_clause_proc_bind_kind kind
;
33641 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33646 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33647 const char *p
= IDENTIFIER_POINTER (id
);
33649 if (strcmp ("master", p
) == 0)
33650 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
33651 else if (strcmp ("close", p
) == 0)
33652 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
33653 else if (strcmp ("spread", p
) == 0)
33654 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
33661 cp_lexer_consume_token (parser
->lexer
);
33662 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
33665 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
33666 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
33668 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
33669 OMP_CLAUSE_CHAIN (c
) = list
;
33673 cp_parser_error (parser
, "invalid depend kind");
33675 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33676 /*or_comma=*/false,
33677 /*consume_paren=*/true);
33682 async [( int-expr )] */
33685 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
33688 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33690 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33692 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33694 matching_parens parens
;
33695 parens
.consume_open (parser
);
33697 t
= cp_parser_expression (parser
);
33698 if (t
== error_mark_node
33699 || !parens
.require_close (parser
))
33700 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33701 /*or_comma=*/false,
33702 /*consume_paren=*/true);
33705 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
33707 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
33708 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
33709 OMP_CLAUSE_CHAIN (c
) = list
;
33715 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33716 is a bitmask in MASK. Return the list of clauses found. */
33719 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33720 const char *where
, cp_token
*pragma_tok
,
33721 bool finish_p
= true)
33723 tree clauses
= NULL
;
33726 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33729 pragma_omp_clause c_kind
;
33730 omp_clause_code code
;
33731 const char *c_name
;
33732 tree prev
= clauses
;
33734 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33735 cp_lexer_consume_token (parser
->lexer
);
33737 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
33738 c_kind
= cp_parser_omp_clause_name (parser
);
33742 case PRAGMA_OACC_CLAUSE_ASYNC
:
33743 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
33746 case PRAGMA_OACC_CLAUSE_AUTO
:
33747 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
33751 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
33752 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
33753 c_name
= "collapse";
33755 case PRAGMA_OACC_CLAUSE_COPY
:
33756 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33759 case PRAGMA_OACC_CLAUSE_COPYIN
:
33760 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33763 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33764 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33765 c_name
= "copyout";
33767 case PRAGMA_OACC_CLAUSE_CREATE
:
33768 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33771 case PRAGMA_OACC_CLAUSE_DELETE
:
33772 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33775 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33776 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
33777 c_name
= "default";
33779 case PRAGMA_OACC_CLAUSE_DEVICE
:
33780 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33783 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
33784 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
33785 c_name
= "deviceptr";
33787 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33788 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33789 c_name
= "device_resident";
33791 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
33792 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33794 c_name
= "firstprivate";
33796 case PRAGMA_OACC_CLAUSE_GANG
:
33798 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
33801 case PRAGMA_OACC_CLAUSE_HOST
:
33802 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33805 case PRAGMA_OACC_CLAUSE_IF
:
33806 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
33809 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
33810 clauses
= cp_parser_oacc_simple_clause (parser
,
33811 OMP_CLAUSE_INDEPENDENT
,
33813 c_name
= "independent";
33815 case PRAGMA_OACC_CLAUSE_LINK
:
33816 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33819 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
33820 code
= OMP_CLAUSE_NUM_GANGS
;
33821 c_name
= "num_gangs";
33822 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33825 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
33826 c_name
= "num_workers";
33827 code
= OMP_CLAUSE_NUM_WORKERS
;
33828 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33831 case PRAGMA_OACC_CLAUSE_PRESENT
:
33832 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33833 c_name
= "present";
33835 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
33836 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33837 c_name
= "present_or_copy";
33839 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
33840 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33841 c_name
= "present_or_copyin";
33843 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
33844 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33845 c_name
= "present_or_copyout";
33847 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
33848 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33849 c_name
= "present_or_create";
33851 case PRAGMA_OACC_CLAUSE_PRIVATE
:
33852 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
33854 c_name
= "private";
33856 case PRAGMA_OACC_CLAUSE_REDUCTION
:
33857 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33858 c_name
= "reduction";
33860 case PRAGMA_OACC_CLAUSE_SELF
:
33861 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
33864 case PRAGMA_OACC_CLAUSE_SEQ
:
33865 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
33869 case PRAGMA_OACC_CLAUSE_TILE
:
33870 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
33873 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
33874 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33876 c_name
= "use_device";
33878 case PRAGMA_OACC_CLAUSE_VECTOR
:
33880 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
33883 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
33884 c_name
= "vector_length";
33885 code
= OMP_CLAUSE_VECTOR_LENGTH
;
33886 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
33889 case PRAGMA_OACC_CLAUSE_WAIT
:
33890 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
33893 case PRAGMA_OACC_CLAUSE_WORKER
:
33895 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
33899 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
33905 if (((mask
>> c_kind
) & 1) == 0)
33907 /* Remove the invalid clause(s) from the list to avoid
33908 confusing the rest of the compiler. */
33910 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
33915 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33918 return finish_omp_clauses (clauses
, C_ORT_ACC
);
33923 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33924 is a bitmask in MASK. Return the list of clauses found; the result
33925 of clause default goes in *pdefault. */
33928 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
33929 const char *where
, cp_token
*pragma_tok
,
33930 bool finish_p
= true)
33932 tree clauses
= NULL
;
33934 cp_token
*token
= NULL
;
33936 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
33938 pragma_omp_clause c_kind
;
33939 const char *c_name
;
33940 tree prev
= clauses
;
33942 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33943 cp_lexer_consume_token (parser
->lexer
);
33945 token
= cp_lexer_peek_token (parser
->lexer
);
33946 c_kind
= cp_parser_omp_clause_name (parser
);
33950 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
33951 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
33953 c_name
= "collapse";
33955 case PRAGMA_OMP_CLAUSE_COPYIN
:
33956 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
33959 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
33960 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
33962 c_name
= "copyprivate";
33964 case PRAGMA_OMP_CLAUSE_DEFAULT
:
33965 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
33966 token
->location
, false);
33967 c_name
= "default";
33969 case PRAGMA_OMP_CLAUSE_FINAL
:
33970 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
33973 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
33974 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33976 c_name
= "firstprivate";
33978 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
33979 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
33981 c_name
= "grainsize";
33983 case PRAGMA_OMP_CLAUSE_HINT
:
33984 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
33988 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
33989 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
33991 c_name
= "defaultmap";
33993 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
33994 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
33996 c_name
= "use_device_ptr";
33998 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
33999 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
34001 c_name
= "is_device_ptr";
34003 case PRAGMA_OMP_CLAUSE_IF
:
34004 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
34008 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
34009 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
34011 c_name
= "lastprivate";
34013 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
34014 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
34016 c_name
= "mergeable";
34018 case PRAGMA_OMP_CLAUSE_NOWAIT
:
34019 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
34022 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
34023 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
34025 c_name
= "num_tasks";
34027 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
34028 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
34030 c_name
= "num_threads";
34032 case PRAGMA_OMP_CLAUSE_ORDERED
:
34033 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
34035 c_name
= "ordered";
34037 case PRAGMA_OMP_CLAUSE_PRIORITY
:
34038 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
34040 c_name
= "priority";
34042 case PRAGMA_OMP_CLAUSE_PRIVATE
:
34043 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
34045 c_name
= "private";
34047 case PRAGMA_OMP_CLAUSE_REDUCTION
:
34048 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
34049 c_name
= "reduction";
34051 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
34052 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
34054 c_name
= "schedule";
34056 case PRAGMA_OMP_CLAUSE_SHARED
:
34057 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
34061 case PRAGMA_OMP_CLAUSE_UNTIED
:
34062 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
34066 case PRAGMA_OMP_CLAUSE_INBRANCH
:
34067 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
34068 clauses
, token
->location
);
34069 c_name
= "inbranch";
34071 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
34072 clauses
= cp_parser_omp_clause_branch (parser
,
34073 OMP_CLAUSE_NOTINBRANCH
,
34074 clauses
, token
->location
);
34075 c_name
= "notinbranch";
34077 case PRAGMA_OMP_CLAUSE_PARALLEL
:
34078 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
34079 clauses
, token
->location
);
34080 c_name
= "parallel";
34084 error_at (token
->location
, "%qs must be the first clause of %qs",
34089 case PRAGMA_OMP_CLAUSE_FOR
:
34090 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
34091 clauses
, token
->location
);
34094 goto clause_not_first
;
34096 case PRAGMA_OMP_CLAUSE_SECTIONS
:
34097 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
34098 clauses
, token
->location
);
34099 c_name
= "sections";
34101 goto clause_not_first
;
34103 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
34104 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
34105 clauses
, token
->location
);
34106 c_name
= "taskgroup";
34108 goto clause_not_first
;
34110 case PRAGMA_OMP_CLAUSE_LINK
:
34111 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
34114 case PRAGMA_OMP_CLAUSE_TO
:
34115 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
34116 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
34119 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
34122 case PRAGMA_OMP_CLAUSE_FROM
:
34123 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
34126 case PRAGMA_OMP_CLAUSE_UNIFORM
:
34127 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
34129 c_name
= "uniform";
34131 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
34132 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
34134 c_name
= "num_teams";
34136 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
34137 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
34139 c_name
= "thread_limit";
34141 case PRAGMA_OMP_CLAUSE_ALIGNED
:
34142 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
34143 c_name
= "aligned";
34145 case PRAGMA_OMP_CLAUSE_LINEAR
:
34147 bool declare_simd
= false;
34148 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
34149 declare_simd
= true;
34150 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
34154 case PRAGMA_OMP_CLAUSE_DEPEND
:
34155 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
34159 case PRAGMA_OMP_CLAUSE_MAP
:
34160 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
34163 case PRAGMA_OMP_CLAUSE_DEVICE
:
34164 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
34168 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
34169 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
34171 c_name
= "dist_schedule";
34173 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
34174 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
34176 c_name
= "proc_bind";
34178 case PRAGMA_OMP_CLAUSE_SAFELEN
:
34179 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
34181 c_name
= "safelen";
34183 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
34184 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
34186 c_name
= "simdlen";
34188 case PRAGMA_OMP_CLAUSE_NOGROUP
:
34189 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
34191 c_name
= "nogroup";
34193 case PRAGMA_OMP_CLAUSE_THREADS
:
34195 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
34196 clauses
, token
->location
);
34197 c_name
= "threads";
34199 case PRAGMA_OMP_CLAUSE_SIMD
:
34201 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
34202 clauses
, token
->location
);
34206 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
34212 if (((mask
>> c_kind
) & 1) == 0)
34214 /* Remove the invalid clause(s) from the list to avoid
34215 confusing the rest of the compiler. */
34217 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
34221 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34224 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
34225 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
34227 return finish_omp_clauses (clauses
, C_ORT_OMP
);
34236 In practice, we're also interested in adding the statement to an
34237 outer node. So it is convenient if we work around the fact that
34238 cp_parser_statement calls add_stmt. */
34241 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
34243 unsigned save
= parser
->in_statement
;
34245 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34246 This preserves the "not within loop or switch" style error messages
34247 for nonsense cases like
34253 if (parser
->in_statement
)
34254 parser
->in_statement
= IN_OMP_BLOCK
;
34260 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
34262 parser
->in_statement
= save
;
34266 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
34268 tree stmt
= begin_omp_structured_block ();
34269 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34271 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34273 cp_parser_end_omp_structured_block (parser
, save
);
34274 return finish_omp_structured_block (stmt
);
34278 # pragma omp atomic new-line
34282 x binop= expr | x++ | ++x | x-- | --x
34284 +, *, -, /, &, ^, |, <<, >>
34286 where x is an lvalue expression with scalar type.
34289 # pragma omp atomic new-line
34292 # pragma omp atomic read new-line
34295 # pragma omp atomic write new-line
34298 # pragma omp atomic update new-line
34301 # pragma omp atomic capture new-line
34304 # pragma omp atomic capture new-line
34312 expression-stmt | x = x binop expr
34314 v = expression-stmt
34316 { v = x; update-stmt; } | { update-stmt; v = x; }
34320 expression-stmt | x = x binop expr | x = expr binop x
34324 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34326 where x and v are lvalue expressions with scalar type. */
34329 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
34331 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
34332 tree rhs1
= NULL_TREE
, orig_lhs
;
34333 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
34334 bool structured_block
= false;
34335 bool seq_cst
= false;
34337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34339 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34340 const char *p
= IDENTIFIER_POINTER (id
);
34342 if (!strcmp (p
, "seq_cst"))
34345 cp_lexer_consume_token (parser
->lexer
);
34346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34347 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34348 cp_lexer_consume_token (parser
->lexer
);
34351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34353 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34354 const char *p
= IDENTIFIER_POINTER (id
);
34356 if (!strcmp (p
, "read"))
34357 code
= OMP_ATOMIC_READ
;
34358 else if (!strcmp (p
, "write"))
34360 else if (!strcmp (p
, "update"))
34362 else if (!strcmp (p
, "capture"))
34363 code
= OMP_ATOMIC_CAPTURE_NEW
;
34367 cp_lexer_consume_token (parser
->lexer
);
34371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
34372 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
34373 cp_lexer_consume_token (parser
->lexer
);
34375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34377 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34378 const char *p
= IDENTIFIER_POINTER (id
);
34380 if (!strcmp (p
, "seq_cst"))
34383 cp_lexer_consume_token (parser
->lexer
);
34387 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34391 case OMP_ATOMIC_READ
:
34392 case NOP_EXPR
: /* atomic write */
34393 v
= cp_parser_unary_expression (parser
);
34394 if (v
== error_mark_node
)
34396 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34398 if (code
== NOP_EXPR
)
34399 lhs
= cp_parser_expression (parser
);
34401 lhs
= cp_parser_unary_expression (parser
);
34402 if (lhs
== error_mark_node
)
34404 if (code
== NOP_EXPR
)
34406 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34414 case OMP_ATOMIC_CAPTURE_NEW
:
34415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
34417 cp_lexer_consume_token (parser
->lexer
);
34418 structured_block
= true;
34422 v
= cp_parser_unary_expression (parser
);
34423 if (v
== error_mark_node
)
34425 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34433 lhs
= cp_parser_unary_expression (parser
);
34435 switch (TREE_CODE (lhs
))
34440 case POSTINCREMENT_EXPR
:
34441 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34442 code
= OMP_ATOMIC_CAPTURE_OLD
;
34444 case PREINCREMENT_EXPR
:
34445 lhs
= TREE_OPERAND (lhs
, 0);
34446 opcode
= PLUS_EXPR
;
34447 rhs
= integer_one_node
;
34450 case POSTDECREMENT_EXPR
:
34451 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
34452 code
= OMP_ATOMIC_CAPTURE_OLD
;
34454 case PREDECREMENT_EXPR
:
34455 lhs
= TREE_OPERAND (lhs
, 0);
34456 opcode
= MINUS_EXPR
;
34457 rhs
= integer_one_node
;
34460 case COMPOUND_EXPR
:
34461 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
34462 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
34463 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
34464 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
34465 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34466 (TREE_OPERAND (lhs
, 1), 0), 0)))
34468 /* Undo effects of boolean_increment for post {in,de}crement. */
34469 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
34472 if (TREE_CODE (lhs
) == MODIFY_EXPR
34473 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
34475 /* Undo effects of boolean_increment. */
34476 if (integer_onep (TREE_OPERAND (lhs
, 1)))
34478 /* This is pre or post increment. */
34479 rhs
= TREE_OPERAND (lhs
, 1);
34480 lhs
= TREE_OPERAND (lhs
, 0);
34482 if (code
== OMP_ATOMIC_CAPTURE_NEW
34483 && !structured_block
34484 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
34485 code
= OMP_ATOMIC_CAPTURE_OLD
;
34491 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34494 opcode
= MULT_EXPR
;
34497 opcode
= TRUNC_DIV_EXPR
;
34500 opcode
= PLUS_EXPR
;
34503 opcode
= MINUS_EXPR
;
34505 case CPP_LSHIFT_EQ
:
34506 opcode
= LSHIFT_EXPR
;
34508 case CPP_RSHIFT_EQ
:
34509 opcode
= RSHIFT_EXPR
;
34512 opcode
= BIT_AND_EXPR
;
34515 opcode
= BIT_IOR_EXPR
;
34518 opcode
= BIT_XOR_EXPR
;
34521 enum cp_parser_prec oprec
;
34523 cp_lexer_consume_token (parser
->lexer
);
34524 cp_parser_parse_tentatively (parser
);
34525 rhs1
= cp_parser_simple_cast_expression (parser
);
34526 if (rhs1
== error_mark_node
)
34528 cp_parser_abort_tentative_parse (parser
);
34529 cp_parser_simple_cast_expression (parser
);
34532 token
= cp_lexer_peek_token (parser
->lexer
);
34533 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
34535 cp_parser_abort_tentative_parse (parser
);
34536 cp_parser_parse_tentatively (parser
);
34537 rhs
= cp_parser_binary_expression (parser
, false, true,
34538 PREC_NOT_OPERATOR
, NULL
);
34539 if (rhs
== error_mark_node
)
34541 cp_parser_abort_tentative_parse (parser
);
34542 cp_parser_binary_expression (parser
, false, true,
34543 PREC_NOT_OPERATOR
, NULL
);
34546 switch (TREE_CODE (rhs
))
34549 case TRUNC_DIV_EXPR
:
34558 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
34560 if (cp_parser_parse_definitely (parser
))
34562 opcode
= TREE_CODE (rhs
);
34563 rhs1
= TREE_OPERAND (rhs
, 0);
34564 rhs
= TREE_OPERAND (rhs
, 1);
34574 cp_parser_abort_tentative_parse (parser
);
34575 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
34577 rhs
= cp_parser_expression (parser
);
34578 if (rhs
== error_mark_node
)
34584 cp_parser_error (parser
,
34585 "invalid form of %<#pragma omp atomic%>");
34588 if (!cp_parser_parse_definitely (parser
))
34590 switch (token
->type
)
34592 case CPP_SEMICOLON
:
34593 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34595 code
= OMP_ATOMIC_CAPTURE_OLD
;
34600 cp_lexer_consume_token (parser
->lexer
);
34603 else if (structured_block
)
34610 cp_parser_error (parser
,
34611 "invalid form of %<#pragma omp atomic%>");
34614 opcode
= MULT_EXPR
;
34617 opcode
= TRUNC_DIV_EXPR
;
34620 opcode
= PLUS_EXPR
;
34623 opcode
= MINUS_EXPR
;
34626 opcode
= LSHIFT_EXPR
;
34629 opcode
= RSHIFT_EXPR
;
34632 opcode
= BIT_AND_EXPR
;
34635 opcode
= BIT_IOR_EXPR
;
34638 opcode
= BIT_XOR_EXPR
;
34641 cp_parser_error (parser
,
34642 "invalid operator for %<#pragma omp atomic%>");
34645 oprec
= TOKEN_PRECEDENCE (token
);
34646 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
34647 if (commutative_tree_code (opcode
))
34648 oprec
= (enum cp_parser_prec
) (oprec
- 1);
34649 cp_lexer_consume_token (parser
->lexer
);
34650 rhs
= cp_parser_binary_expression (parser
, false, false,
34652 if (rhs
== error_mark_node
)
34657 cp_parser_error (parser
,
34658 "invalid operator for %<#pragma omp atomic%>");
34661 cp_lexer_consume_token (parser
->lexer
);
34663 rhs
= cp_parser_expression (parser
);
34664 if (rhs
== error_mark_node
)
34669 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
34671 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
34673 v
= cp_parser_unary_expression (parser
);
34674 if (v
== error_mark_node
)
34676 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
34678 lhs1
= cp_parser_unary_expression (parser
);
34679 if (lhs1
== error_mark_node
)
34682 if (structured_block
)
34684 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34685 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34688 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
34689 if (!structured_block
)
34690 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34694 cp_parser_skip_to_end_of_block_or_statement (parser
);
34695 if (structured_block
)
34697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34698 cp_lexer_consume_token (parser
->lexer
);
34699 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
34701 cp_parser_skip_to_end_of_block_or_statement (parser
);
34702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
34703 cp_lexer_consume_token (parser
->lexer
);
34710 # pragma omp barrier new-line */
34713 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
34715 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34716 finish_omp_barrier ();
34720 # pragma omp critical [(name)] new-line
34724 # pragma omp critical [(name) [hint(expression)]] new-line
34725 structured-block */
34727 #define OMP_CRITICAL_CLAUSE_MASK \
34728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34731 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34733 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
34735 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34737 matching_parens parens
;
34738 parens
.consume_open (parser
);
34740 name
= cp_parser_identifier (parser
);
34742 if (name
== error_mark_node
34743 || !parens
.require_close (parser
))
34744 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34745 /*or_comma=*/false,
34746 /*consume_paren=*/true);
34747 if (name
== error_mark_node
)
34750 clauses
= cp_parser_omp_all_clauses (parser
,
34751 OMP_CRITICAL_CLAUSE_MASK
,
34752 "#pragma omp critical", pragma_tok
);
34755 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34757 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
34758 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
34762 # pragma omp flush flush-vars[opt] new-line
34765 ( variable-list ) */
34768 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
34770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34771 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34772 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34774 finish_omp_flush ();
34777 /* Helper function, to parse omp for increment expression. */
34780 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
)
34782 tree cond
= cp_parser_binary_expression (parser
, false, true,
34783 PREC_NOT_OPERATOR
, NULL
);
34784 if (cond
== error_mark_node
34785 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34787 cp_parser_skip_to_end_of_statement (parser
);
34788 return error_mark_node
;
34791 switch (TREE_CODE (cond
))
34799 /* Fall through: OpenMP disallows NE_EXPR. */
34800 gcc_fallthrough ();
34802 return error_mark_node
;
34805 /* If decl is an iterator, preserve LHS and RHS of the relational
34806 expr until finish_omp_for. */
34808 && (type_dependent_expression_p (decl
)
34809 || CLASS_TYPE_P (TREE_TYPE (decl
))))
34812 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
34814 TREE_OPERAND (cond
, 0), ERROR_MARK
,
34815 TREE_OPERAND (cond
, 1), ERROR_MARK
,
34816 /*overload=*/NULL
, tf_warning_or_error
);
34819 /* Helper function, to parse omp for increment expression. */
34822 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
34824 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34830 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34832 op
= (token
->type
== CPP_PLUS_PLUS
34833 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
34834 cp_lexer_consume_token (parser
->lexer
);
34835 lhs
= cp_parser_simple_cast_expression (parser
);
34837 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34838 return error_mark_node
;
34839 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34842 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
34844 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
34845 return error_mark_node
;
34847 token
= cp_lexer_peek_token (parser
->lexer
);
34848 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
34850 op
= (token
->type
== CPP_PLUS_PLUS
34851 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
34852 cp_lexer_consume_token (parser
->lexer
);
34853 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
34856 op
= cp_parser_assignment_operator_opt (parser
);
34857 if (op
== ERROR_MARK
)
34858 return error_mark_node
;
34860 if (op
!= NOP_EXPR
)
34862 rhs
= cp_parser_assignment_expression (parser
);
34863 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
34864 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34867 lhs
= cp_parser_binary_expression (parser
, false, false,
34868 PREC_ADDITIVE_EXPRESSION
, NULL
);
34869 token
= cp_lexer_peek_token (parser
->lexer
);
34870 decl_first
= (lhs
== decl
34871 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
34874 if (token
->type
!= CPP_PLUS
34875 && token
->type
!= CPP_MINUS
)
34876 return error_mark_node
;
34880 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
34881 cp_lexer_consume_token (parser
->lexer
);
34882 rhs
= cp_parser_binary_expression (parser
, false, false,
34883 PREC_ADDITIVE_EXPRESSION
, NULL
);
34884 token
= cp_lexer_peek_token (parser
->lexer
);
34885 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
34887 if (lhs
== NULL_TREE
)
34889 if (op
== PLUS_EXPR
)
34892 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
34893 tf_warning_or_error
);
34896 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
34897 ERROR_MARK
, NULL
, tf_warning_or_error
);
34900 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
34905 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
34906 || op
== MINUS_EXPR
)
34907 return error_mark_node
;
34908 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
34911 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
34913 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
34916 /* Parse the initialization statement of an OpenMP for loop.
34918 Return true if the resulting construct should have an
34919 OMP_CLAUSE_PRIVATE added to it. */
34922 cp_parser_omp_for_loop_init (cp_parser
*parser
,
34923 tree
&this_pre_body
,
34924 vec
<tree
, va_gc
> *for_block
,
34930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34933 tree add_private_clause
= NULL_TREE
;
34935 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34939 integer-type var = lb
34940 random-access-iterator-type var = lb
34941 pointer-type var = lb
34943 cp_decl_specifier_seq type_specifiers
;
34945 /* First, try to parse as an initialized declaration. See
34946 cp_parser_condition, from whence the bulk of this is copied. */
34948 cp_parser_parse_tentatively (parser
);
34949 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
34950 /*is_trailing_return=*/false,
34952 if (cp_parser_parse_definitely (parser
))
34954 /* If parsing a type specifier seq succeeded, then this
34955 MUST be a initialized declaration. */
34956 tree asm_specification
, attributes
;
34957 cp_declarator
*declarator
;
34959 declarator
= cp_parser_declarator (parser
,
34960 CP_PARSER_DECLARATOR_NAMED
,
34961 /*ctor_dtor_or_conv_p=*/NULL
,
34962 /*parenthesized_p=*/NULL
,
34963 /*member_p=*/false,
34964 /*friend_p=*/false);
34965 attributes
= cp_parser_attributes_opt (parser
);
34966 asm_specification
= cp_parser_asm_specification_opt (parser
);
34968 if (declarator
== cp_error_declarator
)
34969 cp_parser_skip_to_end_of_statement (parser
);
34973 tree pushed_scope
, auto_node
;
34975 decl
= start_decl (declarator
, &type_specifiers
,
34976 SD_INITIALIZED
, attributes
,
34977 /*prefix_attributes=*/NULL_TREE
,
34980 auto_node
= type_uses_auto (TREE_TYPE (decl
));
34981 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34983 if (cp_lexer_next_token_is (parser
->lexer
,
34985 error ("parenthesized initialization is not allowed in "
34986 "OpenMP %<for%> loop");
34988 /* Trigger an error. */
34989 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
34991 init
= error_mark_node
;
34992 cp_parser_skip_to_end_of_statement (parser
);
34994 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
34995 || type_dependent_expression_p (decl
)
34998 bool is_direct_init
, is_non_constant_init
;
35000 init
= cp_parser_initializer (parser
,
35002 &is_non_constant_init
);
35007 = do_auto_deduction (TREE_TYPE (decl
), init
,
35010 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
35011 && !type_dependent_expression_p (decl
))
35015 cp_finish_decl (decl
, init
, !is_non_constant_init
,
35017 LOOKUP_ONLYCONVERTING
);
35019 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
35021 vec_safe_push (for_block
, this_pre_body
);
35026 init
= pop_stmt_list (this_pre_body
);
35027 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
35029 tree_stmt_iterator i
= tsi_start (init
);
35030 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35031 while (!tsi_end_p (i
))
35033 tree t
= tsi_stmt (i
);
35034 if (TREE_CODE (t
) == DECL_EXPR
35035 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
35038 vec_safe_push (for_block
, t
);
35043 if (tsi_one_before_end_p (i
))
35045 tree t
= tsi_stmt (i
);
35047 free_stmt_list (init
);
35052 this_pre_body
= NULL_TREE
;
35057 cp_lexer_consume_token (parser
->lexer
);
35058 init
= cp_parser_assignment_expression (parser
);
35061 if (TYPE_REF_P (TREE_TYPE (decl
)))
35062 init
= error_mark_node
;
35064 cp_finish_decl (decl
, NULL_TREE
,
35065 /*init_const_expr_p=*/false,
35067 LOOKUP_ONLYCONVERTING
);
35071 pop_scope (pushed_scope
);
35077 /* If parsing a type specifier sequence failed, then
35078 this MUST be a simple expression. */
35079 cp_parser_parse_tentatively (parser
);
35080 decl
= cp_parser_primary_expression (parser
, false, false,
35082 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
35083 if (!cp_parser_error_occurred (parser
)
35085 && (TREE_CODE (decl
) == COMPONENT_REF
35086 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
35088 cp_parser_abort_tentative_parse (parser
);
35089 cp_parser_parse_tentatively (parser
);
35090 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35091 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35092 /*check_dependency_p=*/true,
35093 /*template_p=*/NULL
,
35094 /*declarator_p=*/false,
35095 /*optional_p=*/false);
35096 if (name
!= error_mark_node
35097 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
35099 decl
= cp_parser_lookup_name_simple (parser
, name
,
35101 if (TREE_CODE (decl
) == FIELD_DECL
)
35102 add_private_clause
= omp_privatize_field (decl
, false);
35104 cp_parser_abort_tentative_parse (parser
);
35105 cp_parser_parse_tentatively (parser
);
35106 decl
= cp_parser_primary_expression (parser
, false, false,
35109 if (!cp_parser_error_occurred (parser
)
35112 && CLASS_TYPE_P (TREE_TYPE (decl
)))
35116 cp_parser_parse_definitely (parser
);
35117 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
35118 rhs
= cp_parser_assignment_expression (parser
);
35120 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
35123 tf_warning_or_error
));
35124 if (!add_private_clause
)
35125 add_private_clause
= decl
;
35130 cp_parser_abort_tentative_parse (parser
);
35131 init
= cp_parser_expression (parser
);
35134 if (TREE_CODE (init
) == MODIFY_EXPR
35135 || TREE_CODE (init
) == MODOP_EXPR
)
35136 real_decl
= TREE_OPERAND (init
, 0);
35140 return add_private_clause
;
35143 /* Parse the restricted form of the for statement allowed by OpenMP. */
35146 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
35147 tree
*cclauses
, bool *if_p
)
35149 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
35150 tree real_decl
, initv
, condv
, incrv
, declv
;
35151 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
35152 location_t loc_first
;
35153 bool collapse_err
= false;
35154 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
35155 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
35156 auto_vec
<tree
, 4> orig_inits
;
35157 bool tiling
= false;
35159 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
35160 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
35161 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
35162 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
35165 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
35167 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
35168 && OMP_CLAUSE_ORDERED_EXPR (cl
))
35171 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
35174 if (ordered
&& ordered
< collapse
)
35176 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
35177 "%<ordered%> clause parameter is less than %<collapse%>");
35178 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
35179 = build_int_cst (NULL_TREE
, collapse
);
35180 ordered
= collapse
;
35184 for (tree
*pc
= &clauses
; *pc
; )
35185 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
35187 error_at (OMP_CLAUSE_LOCATION (*pc
),
35188 "%<linear%> clause may not be specified together "
35189 "with %<ordered%> clause with a parameter");
35190 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35193 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35196 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
35197 count
= ordered
? ordered
: collapse
;
35199 declv
= make_tree_vec (count
);
35200 initv
= make_tree_vec (count
);
35201 condv
= make_tree_vec (count
);
35202 incrv
= make_tree_vec (count
);
35204 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
35206 for (i
= 0; i
< count
; i
++)
35208 int bracecount
= 0;
35209 tree add_private_clause
= NULL_TREE
;
35212 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35215 cp_parser_error (parser
, "for statement expected");
35218 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
35220 matching_parens parens
;
35221 if (!parens
.require_open (parser
))
35224 init
= orig_init
= decl
= real_decl
= NULL
;
35225 this_pre_body
= push_stmt_list ();
35228 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
35229 init
, orig_init
, decl
, real_decl
);
35231 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35234 this_pre_body
= pop_stmt_list (this_pre_body
);
35238 pre_body
= push_stmt_list ();
35240 add_stmt (this_pre_body
);
35241 pre_body
= pop_stmt_list (pre_body
);
35244 pre_body
= this_pre_body
;
35249 if (cclauses
!= NULL
35250 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
35251 && real_decl
!= NULL_TREE
)
35254 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
35255 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
35256 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35258 error_at (loc
, "iteration variable %qD"
35259 " should not be firstprivate", real_decl
);
35260 *c
= OMP_CLAUSE_CHAIN (*c
);
35262 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
35263 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35265 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35267 *c
= OMP_CLAUSE_CHAIN (*c
);
35268 if (code
== OMP_SIMD
)
35270 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35271 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
35275 OMP_CLAUSE_CHAIN (l
) = clauses
;
35278 add_private_clause
= NULL_TREE
;
35282 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
35283 && OMP_CLAUSE_DECL (*c
) == real_decl
)
35284 add_private_clause
= NULL_TREE
;
35285 c
= &OMP_CLAUSE_CHAIN (*c
);
35289 if (add_private_clause
)
35292 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35294 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
35295 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
35296 && OMP_CLAUSE_DECL (c
) == decl
)
35298 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
35299 && OMP_CLAUSE_DECL (c
) == decl
)
35300 error_at (loc
, "iteration variable %qD "
35301 "should not be firstprivate",
35303 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
35304 && OMP_CLAUSE_DECL (c
) == decl
)
35305 error_at (loc
, "iteration variable %qD should not be reduction",
35310 if (code
!= OMP_SIMD
)
35311 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
35312 else if (collapse
== 1)
35313 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
35315 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
35316 OMP_CLAUSE_DECL (c
) = add_private_clause
;
35317 c
= finish_omp_clauses (c
, C_ORT_OMP
);
35320 OMP_CLAUSE_CHAIN (c
) = clauses
;
35322 /* For linear, signal that we need to fill up
35323 the so far unknown linear step. */
35324 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
35325 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
35331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
35332 cond
= cp_parser_omp_for_cond (parser
, decl
);
35333 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
35336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
35338 /* If decl is an iterator, preserve the operator on decl
35339 until finish_omp_for. */
35341 && ((processing_template_decl
35342 && (TREE_TYPE (real_decl
) == NULL_TREE
35343 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
35344 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
35345 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
35347 incr
= cp_parser_expression (parser
);
35348 if (!EXPR_HAS_LOCATION (incr
))
35349 protected_set_expr_location (incr
, input_location
);
35352 if (!parens
.require_close (parser
))
35353 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35354 /*or_comma=*/false,
35355 /*consume_paren=*/true);
35357 TREE_VEC_ELT (declv
, i
) = decl
;
35358 TREE_VEC_ELT (initv
, i
) = init
;
35359 TREE_VEC_ELT (condv
, i
) = cond
;
35360 TREE_VEC_ELT (incrv
, i
) = incr
;
35363 orig_inits
.safe_grow_cleared (i
+ 1);
35364 orig_inits
[i
] = orig_init
;
35367 if (i
== count
- 1)
35370 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35371 in between the collapsed for loops to be still considered perfectly
35372 nested. Hopefully the final version clarifies this.
35373 For now handle (multiple) {'s and empty statements. */
35374 cp_parser_parse_tentatively (parser
);
35377 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35379 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
35381 cp_lexer_consume_token (parser
->lexer
);
35384 else if (bracecount
35385 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35386 cp_lexer_consume_token (parser
->lexer
);
35389 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35390 error_at (loc
, "not enough for loops to collapse");
35391 collapse_err
= true;
35392 cp_parser_abort_tentative_parse (parser
);
35400 cp_parser_parse_definitely (parser
);
35401 nbraces
+= bracecount
;
35408 /* Note that we saved the original contents of this flag when we entered
35409 the structured block, and so we don't need to re-save it here. */
35410 parser
->in_statement
= IN_OMP_FOR
;
35412 /* Note that the grammar doesn't call for a structured block here,
35413 though the loop as a whole is a structured block. */
35414 body
= push_stmt_list ();
35415 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35416 body
= pop_stmt_list (body
);
35418 if (declv
== NULL_TREE
)
35421 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
35422 body
, pre_body
, &orig_inits
, clauses
);
35426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
35428 cp_lexer_consume_token (parser
->lexer
);
35431 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
35432 cp_lexer_consume_token (parser
->lexer
);
35437 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
35438 "collapsed loops not perfectly nested");
35440 collapse_err
= true;
35441 cp_parser_statement_seq_opt (parser
, NULL
);
35442 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
35447 while (!for_block
->is_empty ())
35449 tree t
= for_block
->pop ();
35450 if (TREE_CODE (t
) == STATEMENT_LIST
)
35451 add_stmt (pop_stmt_list (t
));
35455 release_tree_vector (for_block
);
35460 /* Helper function for OpenMP parsing, split clauses and call
35461 finish_omp_clauses on each of the set of clauses afterwards. */
35464 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
35465 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
35468 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
35469 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
35471 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
35475 #pragma omp simd simd-clause[optseq] new-line
35478 #define OMP_SIMD_CLAUSE_MASK \
35479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35489 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35490 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35493 tree clauses
, sb
, ret
;
35495 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35497 strcat (p_name
, " simd");
35498 mask
|= OMP_SIMD_CLAUSE_MASK
;
35500 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35504 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
35505 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
35506 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
35507 OMP_CLAUSE_ORDERED
);
35508 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
35510 error_at (OMP_CLAUSE_LOCATION (c
),
35511 "%<ordered%> clause with parameter may not be specified "
35512 "on %qs construct", p_name
);
35513 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
35517 sb
= begin_omp_structured_block ();
35518 save
= cp_parser_begin_omp_structured_block (parser
);
35520 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
35522 cp_parser_end_omp_structured_block (parser
, save
);
35523 add_stmt (finish_omp_structured_block (sb
));
35529 #pragma omp for for-clause[optseq] new-line
35533 #pragma omp for simd for-simd-clause[optseq] new-line
35536 #define OMP_FOR_CLAUSE_MASK \
35537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35548 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
35549 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35552 tree clauses
, sb
, ret
;
35554 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35556 strcat (p_name
, " for");
35557 mask
|= OMP_FOR_CLAUSE_MASK
;
35558 /* parallel for{, simd} disallows nowait clause, but for
35559 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35560 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
35561 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35562 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35563 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35564 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
35566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35568 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35569 const char *p
= IDENTIFIER_POINTER (id
);
35571 if (strcmp (p
, "simd") == 0)
35573 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35574 if (cclauses
== NULL
)
35575 cclauses
= cclauses_buf
;
35577 cp_lexer_consume_token (parser
->lexer
);
35578 if (!flag_openmp
) /* flag_openmp_simd */
35579 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35581 sb
= begin_omp_structured_block ();
35582 save
= cp_parser_begin_omp_structured_block (parser
);
35583 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35585 cp_parser_end_omp_structured_block (parser
, save
);
35586 tree body
= finish_omp_structured_block (sb
);
35589 ret
= make_node (OMP_FOR
);
35590 TREE_TYPE (ret
) = void_type_node
;
35591 OMP_FOR_BODY (ret
) = body
;
35592 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35593 SET_EXPR_LOCATION (ret
, loc
);
35598 if (!flag_openmp
) /* flag_openmp_simd */
35600 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35604 /* Composite distribute parallel for disallows linear clause. */
35605 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35606 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
35608 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35612 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
35613 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
35616 sb
= begin_omp_structured_block ();
35617 save
= cp_parser_begin_omp_structured_block (parser
);
35619 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
35621 cp_parser_end_omp_structured_block (parser
, save
);
35622 add_stmt (finish_omp_structured_block (sb
));
35628 # pragma omp master new-line
35629 structured-block */
35632 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35634 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35635 return c_finish_omp_master (input_location
,
35636 cp_parser_omp_structured_block (parser
, if_p
));
35640 # pragma omp ordered new-line
35644 # pragma omp ordered ordered-clauses new-line
35645 structured-block */
35647 #define OMP_ORDERED_CLAUSE_MASK \
35648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35651 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35652 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35655 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
35656 enum pragma_context context
, bool *if_p
)
35658 location_t loc
= pragma_tok
->location
;
35660 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35662 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35663 const char *p
= IDENTIFIER_POINTER (id
);
35665 if (strcmp (p
, "depend") == 0)
35667 if (!flag_openmp
) /* flag_openmp_simd */
35669 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35672 if (context
== pragma_stmt
)
35674 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
35675 "%<depend%> clause may only be used in compound "
35677 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35681 = cp_parser_omp_all_clauses (parser
,
35682 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
35683 "#pragma omp ordered", pragma_tok
);
35684 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
35690 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
35691 "#pragma omp ordered", pragma_tok
);
35693 if (!flag_openmp
/* flag_openmp_simd */
35694 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
35697 c_finish_omp_ordered (loc
, clauses
,
35698 cp_parser_omp_structured_block (parser
, if_p
));
35705 { section-sequence }
35708 section-directive[opt] structured-block
35709 section-sequence section-directive structured-block */
35712 cp_parser_omp_sections_scope (cp_parser
*parser
)
35714 tree stmt
, substmt
;
35715 bool error_suppress
= false;
35718 matching_braces braces
;
35719 if (!braces
.require_open (parser
))
35722 stmt
= push_stmt_list ();
35724 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
35725 != PRAGMA_OMP_SECTION
)
35727 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35728 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35729 add_stmt (substmt
);
35734 tok
= cp_lexer_peek_token (parser
->lexer
);
35735 if (tok
->type
== CPP_CLOSE_BRACE
)
35737 if (tok
->type
== CPP_EOF
)
35740 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
35742 cp_lexer_consume_token (parser
->lexer
);
35743 cp_parser_require_pragma_eol (parser
, tok
);
35744 error_suppress
= false;
35746 else if (!error_suppress
)
35748 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
35749 error_suppress
= true;
35752 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
35753 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
35754 add_stmt (substmt
);
35756 braces
.require_close (parser
);
35758 substmt
= pop_stmt_list (stmt
);
35760 stmt
= make_node (OMP_SECTIONS
);
35761 TREE_TYPE (stmt
) = void_type_node
;
35762 OMP_SECTIONS_BODY (stmt
) = substmt
;
35769 # pragma omp sections sections-clause[optseq] newline
35772 #define OMP_SECTIONS_CLAUSE_MASK \
35773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35780 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
35781 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35786 strcat (p_name
, " sections");
35787 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
35789 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
35791 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35795 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
35796 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
35799 ret
= cp_parser_omp_sections_scope (parser
);
35801 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
35807 # pragma omp parallel parallel-clause[optseq] new-line
35809 # pragma omp parallel for parallel-for-clause[optseq] new-line
35811 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35815 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35816 structured-block */
35818 #define OMP_PARALLEL_CLAUSE_MASK \
35819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35830 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35831 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
35834 tree stmt
, clauses
, block
;
35836 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35838 strcat (p_name
, " parallel");
35839 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
35840 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35841 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
35842 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
35843 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
35845 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
35847 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35848 if (cclauses
== NULL
)
35849 cclauses
= cclauses_buf
;
35851 cp_lexer_consume_token (parser
->lexer
);
35852 if (!flag_openmp
) /* flag_openmp_simd */
35853 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35855 block
= begin_omp_parallel ();
35856 save
= cp_parser_begin_omp_structured_block (parser
);
35857 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
35859 cp_parser_end_omp_structured_block (parser
, save
);
35860 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35862 if (ret
== NULL_TREE
)
35864 OMP_PARALLEL_COMBINED (stmt
) = 1;
35867 /* When combined with distribute, parallel has to be followed by for.
35868 #pragma omp target parallel is allowed though. */
35870 && (mask
& (OMP_CLAUSE_MASK_1
35871 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
35873 error_at (loc
, "expected %<for%> after %qs", p_name
);
35874 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35877 else if (!flag_openmp
) /* flag_openmp_simd */
35879 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35882 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35884 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35885 const char *p
= IDENTIFIER_POINTER (id
);
35886 if (strcmp (p
, "sections") == 0)
35888 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35889 cclauses
= cclauses_buf
;
35891 cp_lexer_consume_token (parser
->lexer
);
35892 block
= begin_omp_parallel ();
35893 save
= cp_parser_begin_omp_structured_block (parser
);
35894 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
35895 cp_parser_end_omp_structured_block (parser
, save
);
35896 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
35898 OMP_PARALLEL_COMBINED (stmt
) = 1;
35903 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35907 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
35908 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
35911 block
= begin_omp_parallel ();
35912 save
= cp_parser_begin_omp_structured_block (parser
);
35913 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35914 cp_parser_end_omp_structured_block (parser
, save
);
35915 stmt
= finish_omp_parallel (clauses
, block
);
35920 # pragma omp single single-clause[optseq] new-line
35921 structured-block */
35923 #define OMP_SINGLE_CLAUSE_MASK \
35924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35930 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35932 tree stmt
= make_node (OMP_SINGLE
);
35933 TREE_TYPE (stmt
) = void_type_node
;
35935 OMP_SINGLE_CLAUSES (stmt
)
35936 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
35937 "#pragma omp single", pragma_tok
);
35938 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35940 return add_stmt (stmt
);
35944 # pragma omp task task-clause[optseq] new-line
35945 structured-block */
35947 #define OMP_TASK_CLAUSE_MASK \
35948 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35960 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35962 tree clauses
, block
;
35965 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
35966 "#pragma omp task", pragma_tok
);
35967 block
= begin_omp_task ();
35968 save
= cp_parser_begin_omp_structured_block (parser
);
35969 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35970 cp_parser_end_omp_structured_block (parser
, save
);
35971 return finish_omp_task (clauses
, block
);
35975 # pragma omp taskwait new-line */
35978 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
35980 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35981 finish_omp_taskwait ();
35985 # pragma omp taskyield new-line */
35988 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
35990 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35991 finish_omp_taskyield ();
35995 # pragma omp taskgroup new-line
35996 structured-block */
35999 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36001 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36002 return c_finish_omp_taskgroup (input_location
,
36003 cp_parser_omp_structured_block (parser
,
36009 # pragma omp threadprivate (variable-list) */
36012 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
36016 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
36017 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36019 finish_omp_threadprivate (vars
);
36023 # pragma omp cancel cancel-clause[optseq] new-line */
36025 #define OMP_CANCEL_CLAUSE_MASK \
36026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36033 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
36035 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
36036 "#pragma omp cancel", pragma_tok
);
36037 finish_omp_cancel (clauses
);
36041 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36043 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36050 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
36051 enum pragma_context context
)
36054 bool point_seen
= false;
36056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36058 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36059 const char *p
= IDENTIFIER_POINTER (id
);
36061 if (strcmp (p
, "point") == 0)
36063 cp_lexer_consume_token (parser
->lexer
);
36069 cp_parser_error (parser
, "expected %<point%>");
36070 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36074 if (context
!= pragma_compound
)
36076 if (context
== pragma_stmt
)
36077 error_at (pragma_tok
->location
,
36078 "%<#pragma %s%> may only be used in compound statements",
36079 "omp cancellation point");
36081 cp_parser_error (parser
, "expected declaration specifiers");
36082 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36086 clauses
= cp_parser_omp_all_clauses (parser
,
36087 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
36088 "#pragma omp cancellation point",
36090 finish_omp_cancellation_point (clauses
);
36094 #pragma omp distribute distribute-clause[optseq] new-line
36097 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36105 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
36106 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36109 tree clauses
, sb
, ret
;
36111 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36113 strcat (p_name
, " distribute");
36114 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
36116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36118 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36119 const char *p
= IDENTIFIER_POINTER (id
);
36121 bool parallel
= false;
36123 if (strcmp (p
, "simd") == 0)
36126 parallel
= strcmp (p
, "parallel") == 0;
36127 if (parallel
|| simd
)
36129 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36130 if (cclauses
== NULL
)
36131 cclauses
= cclauses_buf
;
36132 cp_lexer_consume_token (parser
->lexer
);
36133 if (!flag_openmp
) /* flag_openmp_simd */
36136 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36139 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36142 sb
= begin_omp_structured_block ();
36143 save
= cp_parser_begin_omp_structured_block (parser
);
36145 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36148 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
36150 cp_parser_end_omp_structured_block (parser
, save
);
36151 tree body
= finish_omp_structured_block (sb
);
36154 ret
= make_node (OMP_DISTRIBUTE
);
36155 TREE_TYPE (ret
) = void_type_node
;
36156 OMP_FOR_BODY (ret
) = body
;
36157 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36158 SET_EXPR_LOCATION (ret
, loc
);
36163 if (!flag_openmp
) /* flag_openmp_simd */
36165 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36169 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36173 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
36174 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
36177 sb
= begin_omp_structured_block ();
36178 save
= cp_parser_begin_omp_structured_block (parser
);
36180 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
36182 cp_parser_end_omp_structured_block (parser
, save
);
36183 add_stmt (finish_omp_structured_block (sb
));
36189 # pragma omp teams teams-clause[optseq] new-line
36190 structured-block */
36192 #define OMP_TEAMS_CLAUSE_MASK \
36193 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36202 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
36203 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36206 tree clauses
, sb
, ret
;
36208 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36210 strcat (p_name
, " teams");
36211 mask
|= OMP_TEAMS_CLAUSE_MASK
;
36213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36215 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36216 const char *p
= IDENTIFIER_POINTER (id
);
36217 if (strcmp (p
, "distribute") == 0)
36219 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36220 if (cclauses
== NULL
)
36221 cclauses
= cclauses_buf
;
36223 cp_lexer_consume_token (parser
->lexer
);
36224 if (!flag_openmp
) /* flag_openmp_simd */
36225 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36227 sb
= begin_omp_structured_block ();
36228 save
= cp_parser_begin_omp_structured_block (parser
);
36229 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
36231 cp_parser_end_omp_structured_block (parser
, save
);
36232 tree body
= finish_omp_structured_block (sb
);
36235 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36236 ret
= make_node (OMP_TEAMS
);
36237 TREE_TYPE (ret
) = void_type_node
;
36238 OMP_TEAMS_CLAUSES (ret
) = clauses
;
36239 OMP_TEAMS_BODY (ret
) = body
;
36240 OMP_TEAMS_COMBINED (ret
) = 1;
36241 SET_EXPR_LOCATION (ret
, loc
);
36242 return add_stmt (ret
);
36245 if (!flag_openmp
) /* flag_openmp_simd */
36247 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36251 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36255 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
36256 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36259 tree stmt
= make_node (OMP_TEAMS
);
36260 TREE_TYPE (stmt
) = void_type_node
;
36261 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
36262 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36263 SET_EXPR_LOCATION (stmt
, loc
);
36265 return add_stmt (stmt
);
36269 # pragma omp target data target-data-clause[optseq] new-line
36270 structured-block */
36272 #define OMP_TARGET_DATA_CLAUSE_MASK \
36273 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36279 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36282 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
36283 "#pragma omp target data", pragma_tok
);
36285 for (tree
*pc
= &clauses
; *pc
;)
36287 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36288 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36291 case GOMP_MAP_ALWAYS_TO
:
36292 case GOMP_MAP_FROM
:
36293 case GOMP_MAP_ALWAYS_FROM
:
36294 case GOMP_MAP_TOFROM
:
36295 case GOMP_MAP_ALWAYS_TOFROM
:
36296 case GOMP_MAP_ALLOC
:
36299 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36300 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36301 case GOMP_MAP_ALWAYS_POINTER
:
36305 error_at (OMP_CLAUSE_LOCATION (*pc
),
36306 "%<#pragma omp target data%> with map-type other "
36307 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36308 "on %<map%> clause");
36309 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36312 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36318 error_at (pragma_tok
->location
,
36319 "%<#pragma omp target data%> must contain at least "
36320 "one %<map%> clause");
36324 tree stmt
= make_node (OMP_TARGET_DATA
);
36325 TREE_TYPE (stmt
) = void_type_node
;
36326 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
36328 keep_next_level (true);
36329 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36331 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36332 return add_stmt (stmt
);
36336 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36337 structured-block */
36339 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36340 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36347 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36348 enum pragma_context context
)
36350 bool data_seen
= false;
36351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36353 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36354 const char *p
= IDENTIFIER_POINTER (id
);
36356 if (strcmp (p
, "data") == 0)
36358 cp_lexer_consume_token (parser
->lexer
);
36364 cp_parser_error (parser
, "expected %<data%>");
36365 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36369 if (context
== pragma_stmt
)
36371 error_at (pragma_tok
->location
,
36372 "%<#pragma %s%> may only be used in compound statements",
36373 "omp target enter data");
36374 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36379 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
36380 "#pragma omp target enter data", pragma_tok
);
36382 for (tree
*pc
= &clauses
; *pc
;)
36384 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36385 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36388 case GOMP_MAP_ALWAYS_TO
:
36389 case GOMP_MAP_ALLOC
:
36392 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36393 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36394 case GOMP_MAP_ALWAYS_POINTER
:
36398 error_at (OMP_CLAUSE_LOCATION (*pc
),
36399 "%<#pragma omp target enter data%> with map-type other "
36400 "than %<to%> or %<alloc%> on %<map%> clause");
36401 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36404 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36410 error_at (pragma_tok
->location
,
36411 "%<#pragma omp target enter data%> must contain at least "
36412 "one %<map%> clause");
36416 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
36417 TREE_TYPE (stmt
) = void_type_node
;
36418 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
36419 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36420 return add_stmt (stmt
);
36424 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36425 structured-block */
36427 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36435 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
36436 enum pragma_context context
)
36438 bool data_seen
= false;
36439 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36441 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36442 const char *p
= IDENTIFIER_POINTER (id
);
36444 if (strcmp (p
, "data") == 0)
36446 cp_lexer_consume_token (parser
->lexer
);
36452 cp_parser_error (parser
, "expected %<data%>");
36453 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36457 if (context
== pragma_stmt
)
36459 error_at (pragma_tok
->location
,
36460 "%<#pragma %s%> may only be used in compound statements",
36461 "omp target exit data");
36462 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36467 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
36468 "#pragma omp target exit data", pragma_tok
);
36470 for (tree
*pc
= &clauses
; *pc
;)
36472 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36473 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36475 case GOMP_MAP_FROM
:
36476 case GOMP_MAP_ALWAYS_FROM
:
36477 case GOMP_MAP_RELEASE
:
36478 case GOMP_MAP_DELETE
:
36481 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36482 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36483 case GOMP_MAP_ALWAYS_POINTER
:
36487 error_at (OMP_CLAUSE_LOCATION (*pc
),
36488 "%<#pragma omp target exit data%> with map-type other "
36489 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36491 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36494 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36500 error_at (pragma_tok
->location
,
36501 "%<#pragma omp target exit data%> must contain at least "
36502 "one %<map%> clause");
36506 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
36507 TREE_TYPE (stmt
) = void_type_node
;
36508 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
36509 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36510 return add_stmt (stmt
);
36514 # pragma omp target update target-update-clause[optseq] new-line */
36516 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36525 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
36526 enum pragma_context context
)
36528 if (context
== pragma_stmt
)
36530 error_at (pragma_tok
->location
,
36531 "%<#pragma %s%> may only be used in compound statements",
36532 "omp target update");
36533 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36538 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
36539 "#pragma omp target update", pragma_tok
);
36540 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
36541 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
36543 error_at (pragma_tok
->location
,
36544 "%<#pragma omp target update%> must contain at least one "
36545 "%<from%> or %<to%> clauses");
36549 tree stmt
= make_node (OMP_TARGET_UPDATE
);
36550 TREE_TYPE (stmt
) = void_type_node
;
36551 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
36552 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36558 # pragma omp target target-clause[optseq] new-line
36559 structured-block */
36561 #define OMP_TARGET_CLAUSE_MASK \
36562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36573 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
36574 enum pragma_context context
, bool *if_p
)
36576 tree
*pc
= NULL
, stmt
;
36578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36580 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36581 const char *p
= IDENTIFIER_POINTER (id
);
36582 enum tree_code ccode
= ERROR_MARK
;
36584 if (strcmp (p
, "teams") == 0)
36586 else if (strcmp (p
, "parallel") == 0)
36587 ccode
= OMP_PARALLEL
;
36588 else if (strcmp (p
, "simd") == 0)
36590 if (ccode
!= ERROR_MARK
)
36592 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
36593 char p_name
[sizeof ("#pragma omp target teams distribute "
36594 "parallel for simd")];
36596 cp_lexer_consume_token (parser
->lexer
);
36597 strcpy (p_name
, "#pragma omp target");
36598 if (!flag_openmp
) /* flag_openmp_simd */
36604 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36605 OMP_TARGET_CLAUSE_MASK
,
36609 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36610 OMP_TARGET_CLAUSE_MASK
,
36614 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36615 OMP_TARGET_CLAUSE_MASK
,
36619 gcc_unreachable ();
36621 return stmt
!= NULL_TREE
;
36623 keep_next_level (true);
36624 tree sb
= begin_omp_structured_block (), ret
;
36625 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
36629 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
36630 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36634 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
36635 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36639 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
36640 OMP_TARGET_CLAUSE_MASK
, cclauses
,
36644 gcc_unreachable ();
36646 cp_parser_end_omp_structured_block (parser
, save
);
36647 tree body
= finish_omp_structured_block (sb
);
36648 if (ret
== NULL_TREE
)
36650 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
36652 /* For combined target teams, ensure the num_teams and
36653 thread_limit clause expressions are evaluated on the host,
36654 before entering the target construct. */
36656 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
36657 c
; c
= OMP_CLAUSE_CHAIN (c
))
36658 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
36659 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
36660 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
36662 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
36663 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
36664 if (expr
== error_mark_node
)
36666 tree tmp
= TARGET_EXPR_SLOT (expr
);
36668 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
36669 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
36670 OMP_CLAUSE_FIRSTPRIVATE
);
36671 OMP_CLAUSE_DECL (tc
) = tmp
;
36672 OMP_CLAUSE_CHAIN (tc
)
36673 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36674 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
36677 tree stmt
= make_node (OMP_TARGET
);
36678 TREE_TYPE (stmt
) = void_type_node
;
36679 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
36680 OMP_TARGET_BODY (stmt
) = body
;
36681 OMP_TARGET_COMBINED (stmt
) = 1;
36682 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36684 pc
= &OMP_TARGET_CLAUSES (stmt
);
36685 goto check_clauses
;
36687 else if (!flag_openmp
) /* flag_openmp_simd */
36689 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36692 else if (strcmp (p
, "data") == 0)
36694 cp_lexer_consume_token (parser
->lexer
);
36695 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
36698 else if (strcmp (p
, "enter") == 0)
36700 cp_lexer_consume_token (parser
->lexer
);
36701 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
36704 else if (strcmp (p
, "exit") == 0)
36706 cp_lexer_consume_token (parser
->lexer
);
36707 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
36710 else if (strcmp (p
, "update") == 0)
36712 cp_lexer_consume_token (parser
->lexer
);
36713 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
36716 if (!flag_openmp
) /* flag_openmp_simd */
36718 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36722 stmt
= make_node (OMP_TARGET
);
36723 TREE_TYPE (stmt
) = void_type_node
;
36725 OMP_TARGET_CLAUSES (stmt
)
36726 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
36727 "#pragma omp target", pragma_tok
);
36728 pc
= &OMP_TARGET_CLAUSES (stmt
);
36729 keep_next_level (true);
36730 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
36732 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36738 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
36739 switch (OMP_CLAUSE_MAP_KIND (*pc
))
36742 case GOMP_MAP_ALWAYS_TO
:
36743 case GOMP_MAP_FROM
:
36744 case GOMP_MAP_ALWAYS_FROM
:
36745 case GOMP_MAP_TOFROM
:
36746 case GOMP_MAP_ALWAYS_TOFROM
:
36747 case GOMP_MAP_ALLOC
:
36748 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36749 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
36750 case GOMP_MAP_ALWAYS_POINTER
:
36753 error_at (OMP_CLAUSE_LOCATION (*pc
),
36754 "%<#pragma omp target%> with map-type other "
36755 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36756 "on %<map%> clause");
36757 *pc
= OMP_CLAUSE_CHAIN (*pc
);
36760 pc
= &OMP_CLAUSE_CHAIN (*pc
);
36766 # pragma acc cache (variable-list) new-line
36770 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
36772 tree stmt
, clauses
;
36774 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
36775 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
36777 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
36779 stmt
= make_node (OACC_CACHE
);
36780 TREE_TYPE (stmt
) = void_type_node
;
36781 OACC_CACHE_CLAUSES (stmt
) = clauses
;
36782 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36789 # pragma acc data oacc-data-clause[optseq] new-line
36790 structured-block */
36792 #define OACC_DATA_CLAUSE_MASK \
36793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36806 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36808 tree stmt
, clauses
, block
;
36811 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
36812 "#pragma acc data", pragma_tok
);
36814 block
= begin_omp_parallel ();
36815 save
= cp_parser_begin_omp_structured_block (parser
);
36816 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36817 cp_parser_end_omp_structured_block (parser
, save
);
36818 stmt
= finish_oacc_data (clauses
, block
);
36823 # pragma acc host_data <clauses> new-line
36824 structured-block */
36826 #define OACC_HOST_DATA_CLAUSE_MASK \
36827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36830 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36832 tree stmt
, clauses
, block
;
36835 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
36836 "#pragma acc host_data", pragma_tok
);
36838 block
= begin_omp_parallel ();
36839 save
= cp_parser_begin_omp_structured_block (parser
);
36840 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36841 cp_parser_end_omp_structured_block (parser
, save
);
36842 stmt
= finish_oacc_host_data (clauses
, block
);
36847 # pragma acc declare oacc-data-clause[optseq] new-line
36850 #define OACC_DECLARE_CLAUSE_MASK \
36851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36865 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
36867 tree clauses
, stmt
;
36868 bool error
= false;
36870 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
36871 "#pragma acc declare", pragma_tok
, true);
36874 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
36876 error_at (pragma_tok
->location
,
36877 "no valid clauses specified in %<#pragma acc declare%>");
36881 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
36883 location_t loc
= OMP_CLAUSE_LOCATION (t
);
36884 tree decl
= OMP_CLAUSE_DECL (t
);
36885 if (!DECL_P (decl
))
36887 error_at (loc
, "array section in %<#pragma acc declare%>");
36891 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
36892 switch (OMP_CLAUSE_MAP_KIND (t
))
36894 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
36895 case GOMP_MAP_FORCE_ALLOC
:
36896 case GOMP_MAP_FORCE_TO
:
36897 case GOMP_MAP_FORCE_DEVICEPTR
:
36898 case GOMP_MAP_DEVICE_RESIDENT
:
36901 case GOMP_MAP_LINK
:
36902 if (!global_bindings_p ()
36903 && (TREE_STATIC (decl
)
36904 || !DECL_EXTERNAL (decl
)))
36907 "%qD must be a global variable in "
36908 "%<#pragma acc declare link%>",
36916 if (global_bindings_p ())
36918 error_at (loc
, "invalid OpenACC clause at file scope");
36922 if (DECL_EXTERNAL (decl
))
36925 "invalid use of %<extern%> variable %qD "
36926 "in %<#pragma acc declare%>", decl
);
36930 else if (TREE_PUBLIC (decl
))
36933 "invalid use of %<global%> variable %qD "
36934 "in %<#pragma acc declare%>", decl
);
36941 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
36942 || lookup_attribute ("omp declare target link",
36943 DECL_ATTRIBUTES (decl
)))
36945 error_at (loc
, "variable %qD used more than once with "
36946 "%<#pragma acc declare%>", decl
);
36955 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
36956 id
= get_identifier ("omp declare target link");
36958 id
= get_identifier ("omp declare target");
36960 DECL_ATTRIBUTES (decl
)
36961 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
36962 if (global_bindings_p ())
36964 symtab_node
*node
= symtab_node::get (decl
);
36967 node
->offloadable
= 1;
36968 if (ENABLE_OFFLOADING
)
36970 g
->have_offload
= true;
36971 if (is_a
<varpool_node
*> (node
))
36972 vec_safe_push (offload_vars
, decl
);
36979 if (error
|| global_bindings_p ())
36982 stmt
= make_node (OACC_DECLARE
);
36983 TREE_TYPE (stmt
) = void_type_node
;
36984 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
36985 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
36993 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36997 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36999 LOC is the location of the #pragma token.
37002 #define OACC_ENTER_DATA_CLAUSE_MASK \
37003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37011 #define OACC_EXIT_DATA_CLAUSE_MASK \
37012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37019 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
37022 location_t loc
= pragma_tok
->location
;
37023 tree stmt
, clauses
;
37024 const char *p
= "";
37026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37027 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37029 if (strcmp (p
, "data") != 0)
37031 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
37032 enter
? "enter" : "exit");
37033 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37037 cp_lexer_consume_token (parser
->lexer
);
37040 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
37041 "#pragma acc enter data", pragma_tok
);
37043 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
37044 "#pragma acc exit data", pragma_tok
);
37046 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37048 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
37049 enter
? "enter" : "exit");
37053 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
37054 TREE_TYPE (stmt
) = void_type_node
;
37055 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
37056 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37062 # pragma acc loop oacc-loop-clause[optseq] new-line
37063 structured-block */
37065 #define OACC_LOOP_CLAUSE_MASK \
37066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37078 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
37079 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
37081 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
37083 strcat (p_name
, " loop");
37084 mask
|= OACC_LOOP_CLAUSE_MASK
;
37086 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
37090 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
37092 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
37094 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
37097 tree block
= begin_omp_structured_block ();
37098 int save
= cp_parser_begin_omp_structured_block (parser
);
37099 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
37100 cp_parser_end_omp_structured_block (parser
, save
);
37101 add_stmt (finish_omp_structured_block (block
));
37107 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37112 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37116 #define OACC_KERNELS_CLAUSE_MASK \
37117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
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_PRESENT_OR_COPY) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37158 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
37159 char *p_name
, bool *if_p
)
37161 omp_clause_mask mask
;
37162 enum tree_code code
;
37163 switch (cp_parser_pragma_kind (pragma_tok
))
37165 case PRAGMA_OACC_KERNELS
:
37166 strcat (p_name
, " kernels");
37167 mask
= OACC_KERNELS_CLAUSE_MASK
;
37168 code
= OACC_KERNELS
;
37170 case PRAGMA_OACC_PARALLEL
:
37171 strcat (p_name
, " parallel");
37172 mask
= OACC_PARALLEL_CLAUSE_MASK
;
37173 code
= OACC_PARALLEL
;
37176 gcc_unreachable ();
37179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37182 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
37183 if (strcmp (p
, "loop") == 0)
37185 cp_lexer_consume_token (parser
->lexer
);
37186 tree block
= begin_omp_parallel ();
37188 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
37190 return finish_omp_construct (code
, block
, clauses
);
37194 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
37196 tree block
= begin_omp_parallel ();
37197 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37198 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37199 cp_parser_end_omp_structured_block (parser
, save
);
37200 return finish_omp_construct (code
, block
, clauses
);
37204 # pragma acc update oacc-update-clause[optseq] new-line
37207 #define OACC_UPDATE_CLAUSE_MASK \
37208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37216 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
37218 tree stmt
, clauses
;
37220 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
37221 "#pragma acc update", pragma_tok
);
37223 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
37225 error_at (pragma_tok
->location
,
37226 "%<#pragma acc update%> must contain at least one "
37227 "%<device%> or %<host%> or %<self%> clause");
37231 stmt
= make_node (OACC_UPDATE
);
37232 TREE_TYPE (stmt
) = void_type_node
;
37233 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
37234 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
37240 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37242 LOC is the location of the #pragma token.
37245 #define OACC_WAIT_CLAUSE_MASK \
37246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37249 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
37251 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
37252 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37254 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37255 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
37257 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
37258 "#pragma acc wait", pragma_tok
);
37260 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
37261 stmt
= finish_expr_stmt (stmt
);
37267 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37269 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37278 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
37279 enum pragma_context context
)
37281 bool first_p
= parser
->omp_declare_simd
== NULL
;
37282 cp_omp_declare_simd_data data
;
37285 data
.error_seen
= false;
37286 data
.fndecl_seen
= false;
37287 data
.tokens
= vNULL
;
37288 data
.clauses
= NULL_TREE
;
37289 /* It is safe to take the address of a local variable; it will only be
37290 used while this scope is live. */
37291 parser
->omp_declare_simd
= &data
;
37294 /* Store away all pragma tokens. */
37295 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37296 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37297 cp_lexer_consume_token (parser
->lexer
);
37298 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37299 parser
->omp_declare_simd
->error_seen
= true;
37300 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37301 struct cp_token_cache
*cp
37302 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
37303 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
37307 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
37308 cp_parser_pragma (parser
, context
, NULL
);
37311 case pragma_external
:
37312 cp_parser_declaration (parser
);
37314 case pragma_member
:
37315 cp_parser_member_declaration (parser
);
37317 case pragma_objc_icode
:
37318 cp_parser_block_declaration (parser
, /*statement_p=*/false);
37321 cp_parser_declaration_statement (parser
);
37324 if (parser
->omp_declare_simd
37325 && !parser
->omp_declare_simd
->error_seen
37326 && !parser
->omp_declare_simd
->fndecl_seen
)
37327 error_at (pragma_tok
->location
,
37328 "%<#pragma omp declare simd%> not immediately followed by "
37329 "function declaration or definition");
37330 data
.tokens
.release ();
37331 parser
->omp_declare_simd
= NULL
;
37335 /* Finalize #pragma omp declare simd clauses after direct declarator has
37336 been parsed, and put that into "omp declare simd" attribute. */
37339 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
37341 struct cp_token_cache
*ce
;
37342 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
37345 if (!data
->error_seen
&& data
->fndecl_seen
)
37347 error ("%<#pragma omp declare simd%> not immediately followed by "
37348 "a single function declaration or definition");
37349 data
->error_seen
= true;
37351 if (data
->error_seen
)
37354 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
37358 cp_parser_push_lexer_for_tokens (parser
, ce
);
37359 parser
->lexer
->in_pragma
= true;
37360 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
37361 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37362 cp_lexer_consume_token (parser
->lexer
);
37363 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
37364 "#pragma omp declare simd", pragma_tok
);
37365 cp_parser_pop_lexer (parser
);
37367 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
37368 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
37369 TREE_CHAIN (c
) = attrs
;
37370 if (processing_template_decl
)
37371 ATTR_IS_DEPENDENT (c
) = 1;
37375 data
->fndecl_seen
= true;
37381 # pragma omp declare target new-line
37382 declarations and definitions
37383 # pragma omp end declare target new-line
37386 # pragma omp declare target ( extended-list ) new-line
37388 # pragma omp declare target declare-target-clauses[seq] new-line */
37390 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37395 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37397 tree clauses
= NULL_TREE
;
37398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37400 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
37401 "#pragma omp declare target", pragma_tok
);
37402 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37404 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37406 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37407 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37411 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37412 scope_chain
->omp_declare_target_attribute
++;
37415 if (scope_chain
->omp_declare_target_attribute
)
37416 error_at (pragma_tok
->location
,
37417 "%<#pragma omp declare target%> with clauses in between "
37418 "%<#pragma omp declare target%> without clauses and "
37419 "%<#pragma omp end declare target%>");
37420 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
37422 tree t
= OMP_CLAUSE_DECL (c
), id
;
37423 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
37424 tree at2
= lookup_attribute ("omp declare target link",
37425 DECL_ATTRIBUTES (t
));
37426 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
37428 id
= get_identifier ("omp declare target link");
37429 std::swap (at1
, at2
);
37432 id
= get_identifier ("omp declare target");
37435 error_at (OMP_CLAUSE_LOCATION (c
),
37436 "%qD specified both in declare target %<link%> and %<to%>"
37442 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
37443 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
37446 symtab_node
*node
= symtab_node::get (t
);
37449 node
->offloadable
= 1;
37450 if (ENABLE_OFFLOADING
)
37452 g
->have_offload
= true;
37453 if (is_a
<varpool_node
*> (node
))
37454 vec_safe_push (offload_vars
, t
);
37462 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
37464 const char *p
= "";
37465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37467 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37468 p
= IDENTIFIER_POINTER (id
);
37470 if (strcmp (p
, "declare") == 0)
37472 cp_lexer_consume_token (parser
->lexer
);
37474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37476 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37477 p
= IDENTIFIER_POINTER (id
);
37479 if (strcmp (p
, "target") == 0)
37480 cp_lexer_consume_token (parser
->lexer
);
37483 cp_parser_error (parser
, "expected %<target%>");
37484 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37490 cp_parser_error (parser
, "expected %<declare%>");
37491 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37494 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37495 if (!scope_chain
->omp_declare_target_attribute
)
37496 error_at (pragma_tok
->location
,
37497 "%<#pragma omp end declare target%> without corresponding "
37498 "%<#pragma omp declare target%>");
37500 scope_chain
->omp_declare_target_attribute
--;
37503 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37504 expression and optional initializer clause of
37505 #pragma omp declare reduction. We store the expression(s) as
37506 either 3, 6 or 7 special statements inside of the artificial function's
37507 body. The first two statements are DECL_EXPRs for the artificial
37508 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37509 expression that uses those variables.
37510 If there was any INITIALIZER clause, this is followed by further statements,
37511 the fourth and fifth statements are DECL_EXPRs for the artificial
37512 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37513 constructor variant (first token after open paren is not omp_priv),
37514 then the sixth statement is a statement with the function call expression
37515 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37516 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37517 to initialize the OMP_PRIV artificial variable and there is seventh
37518 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37521 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
37523 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
37524 gcc_assert (TYPE_REF_P (type
));
37525 type
= TREE_TYPE (type
);
37526 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
37527 DECL_ARTIFICIAL (omp_out
) = 1;
37528 pushdecl (omp_out
);
37529 add_decl_expr (omp_out
);
37530 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
37531 DECL_ARTIFICIAL (omp_in
) = 1;
37533 add_decl_expr (omp_in
);
37535 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
37537 keep_next_level (true);
37538 tree block
= begin_omp_structured_block ();
37539 combiner
= cp_parser_expression (parser
);
37540 finish_expr_stmt (combiner
);
37541 block
= finish_omp_structured_block (block
);
37544 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37547 const char *p
= "";
37548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37550 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37551 p
= IDENTIFIER_POINTER (id
);
37554 if (strcmp (p
, "initializer") == 0)
37556 cp_lexer_consume_token (parser
->lexer
);
37557 matching_parens parens
;
37558 if (!parens
.require_open (parser
))
37562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37564 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37565 p
= IDENTIFIER_POINTER (id
);
37568 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
37569 DECL_ARTIFICIAL (omp_priv
) = 1;
37570 pushdecl (omp_priv
);
37571 add_decl_expr (omp_priv
);
37572 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
37573 DECL_ARTIFICIAL (omp_orig
) = 1;
37574 pushdecl (omp_orig
);
37575 add_decl_expr (omp_orig
);
37577 keep_next_level (true);
37578 block
= begin_omp_structured_block ();
37581 if (strcmp (p
, "omp_priv") == 0)
37583 bool is_direct_init
, is_non_constant_init
;
37585 cp_lexer_consume_token (parser
->lexer
);
37586 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
37588 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37589 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
37591 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
37592 == CPP_CLOSE_PAREN
))
37594 finish_omp_structured_block (block
);
37595 error ("invalid initializer clause");
37598 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
37599 &is_non_constant_init
);
37600 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
37601 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
37605 cp_parser_parse_tentatively (parser
);
37606 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37607 /*check_dependency_p=*/true,
37608 /*template_p=*/NULL
,
37609 /*declarator_p=*/false,
37610 /*optional_p=*/false);
37611 vec
<tree
, va_gc
> *args
;
37612 if (fn_name
== error_mark_node
37613 || cp_parser_error_occurred (parser
)
37614 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
37615 || ((args
= cp_parser_parenthesized_expression_list
37616 (parser
, non_attr
, /*cast_p=*/false,
37617 /*allow_expansion_p=*/true,
37618 /*non_constant_p=*/NULL
)),
37619 cp_parser_error_occurred (parser
)))
37621 finish_omp_structured_block (block
);
37622 cp_parser_abort_tentative_parse (parser
);
37623 cp_parser_error (parser
, "expected id-expression (arguments)");
37628 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
37629 if (arg
== omp_priv
37630 || (TREE_CODE (arg
) == ADDR_EXPR
37631 && TREE_OPERAND (arg
, 0) == omp_priv
))
37633 cp_parser_abort_tentative_parse (parser
);
37634 if (arg
== NULL_TREE
)
37635 error ("one of the initializer call arguments should be %<omp_priv%>"
37636 " or %<&omp_priv%>");
37637 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
37639 finish_expr_stmt (initializer
);
37642 block
= finish_omp_structured_block (block
);
37643 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
37647 add_decl_expr (omp_orig
);
37649 if (!parens
.require_close (parser
))
37653 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
37654 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
37661 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37662 initializer-clause[opt] new-line
37664 initializer-clause:
37665 initializer (omp_priv initializer)
37666 initializer (function-name (argument-list)) */
37669 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
37670 enum pragma_context
)
37672 auto_vec
<tree
> types
;
37673 enum tree_code reduc_code
= ERROR_MARK
;
37674 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
37676 cp_token
*first_token
;
37677 cp_token_cache
*cp
;
37681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37682 p
= obstack_alloc (&declarator_obstack
, 0);
37684 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37687 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37690 reduc_code
= PLUS_EXPR
;
37693 reduc_code
= MULT_EXPR
;
37696 reduc_code
= MINUS_EXPR
;
37699 reduc_code
= BIT_AND_EXPR
;
37702 reduc_code
= BIT_XOR_EXPR
;
37705 reduc_code
= BIT_IOR_EXPR
;
37708 reduc_code
= TRUTH_ANDIF_EXPR
;
37711 reduc_code
= TRUTH_ORIF_EXPR
;
37714 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
37717 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37718 "%<|%>, %<&&%>, %<||%> or identifier");
37722 if (reduc_code
!= ERROR_MARK
)
37723 cp_lexer_consume_token (parser
->lexer
);
37725 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
37726 if (reduc_id
== error_mark_node
)
37729 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37732 /* Types may not be defined in declare reduction type list. */
37733 const char *saved_message
;
37734 saved_message
= parser
->type_definition_forbidden_message
;
37735 parser
->type_definition_forbidden_message
37736 = G_("types may not be defined in declare reduction type list");
37737 bool saved_colon_corrects_to_scope_p
;
37738 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37739 parser
->colon_corrects_to_scope_p
= false;
37740 bool saved_colon_doesnt_start_class_def_p
;
37741 saved_colon_doesnt_start_class_def_p
37742 = parser
->colon_doesnt_start_class_def_p
;
37743 parser
->colon_doesnt_start_class_def_p
= true;
37747 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37748 type
= cp_parser_type_id (parser
);
37749 if (type
== error_mark_node
)
37751 else if (ARITHMETIC_TYPE_P (type
)
37752 && (orig_reduc_id
== NULL_TREE
37753 || (TREE_CODE (type
) != COMPLEX_TYPE
37754 && (id_equal (orig_reduc_id
, "min")
37755 || id_equal (orig_reduc_id
, "max")))))
37756 error_at (loc
, "predeclared arithmetic type %qT in "
37757 "%<#pragma omp declare reduction%>", type
);
37758 else if (TREE_CODE (type
) == FUNCTION_TYPE
37759 || TREE_CODE (type
) == METHOD_TYPE
37760 || TREE_CODE (type
) == ARRAY_TYPE
)
37761 error_at (loc
, "function or array type %qT in "
37762 "%<#pragma omp declare reduction%>", type
);
37763 else if (TYPE_REF_P (type
))
37764 error_at (loc
, "reference type %qT in "
37765 "%<#pragma omp declare reduction%>", type
);
37766 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
37767 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
37768 "%<#pragma omp declare reduction%>", type
);
37770 types
.safe_push (type
);
37772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37773 cp_lexer_consume_token (parser
->lexer
);
37778 /* Restore the saved message. */
37779 parser
->type_definition_forbidden_message
= saved_message
;
37780 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37781 parser
->colon_doesnt_start_class_def_p
37782 = saved_colon_doesnt_start_class_def_p
;
37784 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
37785 || types
.is_empty ())
37788 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37792 first_token
= cp_lexer_peek_token (parser
->lexer
);
37795 FOR_EACH_VEC_ELT (types
, i
, type
)
37798 = build_function_type_list (void_type_node
,
37799 cp_build_reference_type (type
, false),
37801 tree this_reduc_id
= reduc_id
;
37802 if (!dependent_type_p (type
))
37803 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
37804 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
37805 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
37806 DECL_ARTIFICIAL (fndecl
) = 1;
37807 DECL_EXTERNAL (fndecl
) = 1;
37808 DECL_DECLARED_INLINE_P (fndecl
) = 1;
37809 DECL_IGNORED_P (fndecl
) = 1;
37810 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
37811 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
37812 DECL_ATTRIBUTES (fndecl
)
37813 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
37814 DECL_ATTRIBUTES (fndecl
));
37815 if (processing_template_decl
)
37816 fndecl
= push_template_decl (fndecl
);
37817 bool block_scope
= false;
37818 tree block
= NULL_TREE
;
37819 if (current_function_decl
)
37821 block_scope
= true;
37822 DECL_CONTEXT (fndecl
) = global_namespace
;
37823 if (!processing_template_decl
)
37826 else if (current_class_type
)
37830 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37831 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
37832 cp_lexer_consume_token (parser
->lexer
);
37833 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37835 cp
= cp_token_cache_new (first_token
,
37836 cp_lexer_peek_nth_token (parser
->lexer
,
37839 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
37840 finish_member_declaration (fndecl
);
37841 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
37842 DECL_PENDING_INLINE_P (fndecl
) = 1;
37843 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
37848 DECL_CONTEXT (fndecl
) = current_namespace
;
37852 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
37854 block
= begin_omp_structured_block ();
37857 cp_parser_push_lexer_for_tokens (parser
, cp
);
37858 parser
->lexer
->in_pragma
= true;
37860 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
37863 finish_function (/*inline_p=*/false);
37865 DECL_CONTEXT (fndecl
) = current_function_decl
;
37867 cp_parser_pop_lexer (parser
);
37871 cp_parser_pop_lexer (parser
);
37873 finish_function (/*inline_p=*/false);
37876 DECL_CONTEXT (fndecl
) = current_function_decl
;
37877 block
= finish_omp_structured_block (block
);
37878 if (TREE_CODE (block
) == BIND_EXPR
)
37879 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
37880 else if (TREE_CODE (block
) == STATEMENT_LIST
)
37881 DECL_SAVED_TREE (fndecl
) = block
;
37882 if (processing_template_decl
)
37883 add_decl_expr (fndecl
);
37885 cp_check_omp_declare_reduction (fndecl
);
37886 if (cp
== NULL
&& types
.length () > 1)
37887 cp
= cp_token_cache_new (first_token
,
37888 cp_lexer_peek_nth_token (parser
->lexer
, 2));
37889 if (errs
!= errorcount
)
37893 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37896 /* Free any declarators allocated. */
37897 obstack_free (&declarator_obstack
, p
);
37901 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37902 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37903 initializer-clause[opt] new-line
37904 #pragma omp declare target new-line */
37907 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
37908 enum pragma_context context
)
37910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37912 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37913 const char *p
= IDENTIFIER_POINTER (id
);
37915 if (strcmp (p
, "simd") == 0)
37917 cp_lexer_consume_token (parser
->lexer
);
37918 cp_parser_omp_declare_simd (parser
, pragma_tok
,
37922 cp_ensure_no_omp_declare_simd (parser
);
37923 if (strcmp (p
, "reduction") == 0)
37925 cp_lexer_consume_token (parser
->lexer
);
37926 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
37930 if (!flag_openmp
) /* flag_openmp_simd */
37932 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37935 if (strcmp (p
, "target") == 0)
37937 cp_lexer_consume_token (parser
->lexer
);
37938 cp_parser_omp_declare_target (parser
, pragma_tok
);
37942 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
37944 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37949 #pragma omp taskloop taskloop-clause[optseq] new-line
37952 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37955 #define OMP_TASKLOOP_CLAUSE_MASK \
37956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37972 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
37973 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
37976 tree clauses
, sb
, ret
;
37978 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37980 strcat (p_name
, " taskloop");
37981 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
37983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37985 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37986 const char *p
= IDENTIFIER_POINTER (id
);
37988 if (strcmp (p
, "simd") == 0)
37990 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
37991 if (cclauses
== NULL
)
37992 cclauses
= cclauses_buf
;
37994 cp_lexer_consume_token (parser
->lexer
);
37995 if (!flag_openmp
) /* flag_openmp_simd */
37996 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
37998 sb
= begin_omp_structured_block ();
37999 save
= cp_parser_begin_omp_structured_block (parser
);
38000 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38002 cp_parser_end_omp_structured_block (parser
, save
);
38003 tree body
= finish_omp_structured_block (sb
);
38006 ret
= make_node (OMP_TASKLOOP
);
38007 TREE_TYPE (ret
) = void_type_node
;
38008 OMP_FOR_BODY (ret
) = body
;
38009 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38010 SET_EXPR_LOCATION (ret
, loc
);
38015 if (!flag_openmp
) /* flag_openmp_simd */
38017 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38021 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38025 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
38026 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
38029 sb
= begin_omp_structured_block ();
38030 save
= cp_parser_begin_omp_structured_block (parser
);
38032 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
38035 cp_parser_end_omp_structured_block (parser
, save
);
38036 add_stmt (finish_omp_structured_block (sb
));
38043 # pragma acc routine oacc-routine-clause[optseq] new-line
38044 function-definition
38046 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38049 #define OACC_ROUTINE_CLAUSE_MASK \
38050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38056 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38057 component, which must resolve to a declared namespace-scope
38058 function. The clauses are either processed directly (for a named
38059 function), or defered until the immediatley following declaration
38063 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
38064 enum pragma_context context
)
38066 gcc_checking_assert (context
== pragma_external
);
38067 /* The checking for "another pragma following this one" in the "no optional
38068 '( name )'" case makes sure that we dont re-enter. */
38069 gcc_checking_assert (parser
->oacc_routine
== NULL
);
38071 cp_oacc_routine_data data
;
38072 data
.error_seen
= false;
38073 data
.fndecl_seen
= false;
38074 data
.tokens
= vNULL
;
38075 data
.clauses
= NULL_TREE
;
38076 data
.loc
= pragma_tok
->location
;
38077 /* It is safe to take the address of a local variable; it will only be
38078 used while this scope is live. */
38079 parser
->oacc_routine
= &data
;
38081 /* Look for optional '( name )'. */
38082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38084 matching_parens parens
;
38085 parens
.consume_open (parser
); /* '(' */
38087 /* We parse the name as an id-expression. If it resolves to
38088 anything other than a non-overloaded function at namespace
38089 scope, it's an error. */
38090 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38091 tree name
= cp_parser_id_expression (parser
,
38092 /*template_keyword_p=*/false,
38093 /*check_dependency_p=*/false,
38094 /*template_p=*/NULL
,
38095 /*declarator_p=*/false,
38096 /*optional_p=*/false);
38097 tree decl
= (identifier_p (name
)
38098 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
38100 if (name
!= error_mark_node
&& decl
== error_mark_node
)
38101 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
38103 if (decl
== error_mark_node
38104 || !parens
.require_close (parser
))
38106 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38107 parser
->oacc_routine
= NULL
;
38112 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38113 "#pragma acc routine",
38114 cp_lexer_peek_token (parser
->lexer
));
38116 if (decl
&& is_overloaded_fn (decl
)
38117 && (TREE_CODE (decl
) != FUNCTION_DECL
38118 || DECL_FUNCTION_TEMPLATE_P (decl
)))
38120 error_at (name_loc
,
38121 "%<#pragma acc routine%> names a set of overloads");
38122 parser
->oacc_routine
= NULL
;
38126 /* Perhaps we should use the same rule as declarations in different
38128 if (!DECL_NAMESPACE_SCOPE_P (decl
))
38130 error_at (name_loc
,
38131 "%qD does not refer to a namespace scope function", decl
);
38132 parser
->oacc_routine
= NULL
;
38136 if (TREE_CODE (decl
) != FUNCTION_DECL
)
38138 error_at (name_loc
, "%qD does not refer to a function", decl
);
38139 parser
->oacc_routine
= NULL
;
38143 cp_finalize_oacc_routine (parser
, decl
, false);
38144 parser
->oacc_routine
= NULL
;
38146 else /* No optional '( name )'. */
38148 /* Store away all pragma tokens. */
38149 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
38150 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
38151 cp_lexer_consume_token (parser
->lexer
);
38152 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38153 parser
->oacc_routine
->error_seen
= true;
38154 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38155 struct cp_token_cache
*cp
38156 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
38157 parser
->oacc_routine
->tokens
.safe_push (cp
);
38159 /* Emit a helpful diagnostic if there's another pragma following this
38161 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
38163 cp_ensure_no_oacc_routine (parser
);
38164 data
.tokens
.release ();
38165 /* ..., and then just keep going. */
38169 /* We only have to consider the pragma_external case here. */
38170 cp_parser_declaration (parser
);
38171 if (parser
->oacc_routine
38172 && !parser
->oacc_routine
->fndecl_seen
)
38173 cp_ensure_no_oacc_routine (parser
);
38175 parser
->oacc_routine
= NULL
;
38176 data
.tokens
.release ();
38180 /* Finalize #pragma acc routine clauses after direct declarator has
38184 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
38186 struct cp_token_cache
*ce
;
38187 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
38189 if (!data
->error_seen
&& data
->fndecl_seen
)
38191 error_at (data
->loc
,
38192 "%<#pragma acc routine%> not immediately followed by "
38193 "a single function declaration or definition");
38194 data
->error_seen
= true;
38196 if (data
->error_seen
)
38199 gcc_checking_assert (data
->tokens
.length () == 1);
38200 ce
= data
->tokens
[0];
38202 cp_parser_push_lexer_for_tokens (parser
, ce
);
38203 parser
->lexer
->in_pragma
= true;
38204 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
38206 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38207 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
38208 parser
->oacc_routine
->clauses
38209 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
38210 "#pragma acc routine", pragma_tok
);
38211 cp_parser_pop_lexer (parser
);
38212 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38218 /* Apply any saved OpenACC routine clauses to a just-parsed
38222 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
38224 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
38226 /* Keep going if we're in error reporting mode. */
38227 if (parser
->oacc_routine
->error_seen
38228 || fndecl
== error_mark_node
)
38231 if (parser
->oacc_routine
->fndecl_seen
)
38233 error_at (parser
->oacc_routine
->loc
,
38234 "%<#pragma acc routine%> not immediately followed by"
38235 " a single function declaration or definition");
38236 parser
->oacc_routine
= NULL
;
38239 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
38241 cp_ensure_no_oacc_routine (parser
);
38245 if (oacc_get_fn_attrib (fndecl
))
38247 error_at (parser
->oacc_routine
->loc
,
38248 "%<#pragma acc routine%> already applied to %qD", fndecl
);
38249 parser
->oacc_routine
= NULL
;
38253 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
38255 error_at (parser
->oacc_routine
->loc
,
38257 ? G_("%<#pragma acc routine%> must be applied before use")
38258 : G_("%<#pragma acc routine%> must be applied before "
38260 parser
->oacc_routine
= NULL
;
38264 /* Process the routine's dimension clauses. */
38265 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
38266 oacc_replace_fn_attrib (fndecl
, dims
);
38268 /* Add an "omp declare target" attribute. */
38269 DECL_ATTRIBUTES (fndecl
)
38270 = tree_cons (get_identifier ("omp declare target"),
38271 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
38273 /* Don't unset parser->oacc_routine here: we may still need it to
38274 diagnose wrong usage. But, remember that we've used this "#pragma acc
38276 parser
->oacc_routine
->fndecl_seen
= true;
38280 /* Main entry point to OpenMP statement pragmas. */
38283 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38286 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
38287 omp_clause_mask
mask (0);
38289 switch (cp_parser_pragma_kind (pragma_tok
))
38291 case PRAGMA_OACC_ATOMIC
:
38292 cp_parser_omp_atomic (parser
, pragma_tok
);
38294 case PRAGMA_OACC_CACHE
:
38295 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
38297 case PRAGMA_OACC_DATA
:
38298 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
38300 case PRAGMA_OACC_ENTER_DATA
:
38301 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
38303 case PRAGMA_OACC_EXIT_DATA
:
38304 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
38306 case PRAGMA_OACC_HOST_DATA
:
38307 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
38309 case PRAGMA_OACC_KERNELS
:
38310 case PRAGMA_OACC_PARALLEL
:
38311 strcpy (p_name
, "#pragma acc");
38312 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
38315 case PRAGMA_OACC_LOOP
:
38316 strcpy (p_name
, "#pragma acc");
38317 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38320 case PRAGMA_OACC_UPDATE
:
38321 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
38323 case PRAGMA_OACC_WAIT
:
38324 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
38326 case PRAGMA_OMP_ATOMIC
:
38327 cp_parser_omp_atomic (parser
, pragma_tok
);
38329 case PRAGMA_OMP_CRITICAL
:
38330 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
38332 case PRAGMA_OMP_DISTRIBUTE
:
38333 strcpy (p_name
, "#pragma omp");
38334 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
38337 case PRAGMA_OMP_FOR
:
38338 strcpy (p_name
, "#pragma omp");
38339 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
38342 case PRAGMA_OMP_MASTER
:
38343 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
38345 case PRAGMA_OMP_PARALLEL
:
38346 strcpy (p_name
, "#pragma omp");
38347 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
38350 case PRAGMA_OMP_SECTIONS
:
38351 strcpy (p_name
, "#pragma omp");
38352 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
38354 case PRAGMA_OMP_SIMD
:
38355 strcpy (p_name
, "#pragma omp");
38356 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
38359 case PRAGMA_OMP_SINGLE
:
38360 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
38362 case PRAGMA_OMP_TASK
:
38363 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
38365 case PRAGMA_OMP_TASKGROUP
:
38366 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
38368 case PRAGMA_OMP_TASKLOOP
:
38369 strcpy (p_name
, "#pragma omp");
38370 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
38373 case PRAGMA_OMP_TEAMS
:
38374 strcpy (p_name
, "#pragma omp");
38375 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
38379 gcc_unreachable ();
38382 protected_set_expr_location (stmt
, pragma_tok
->location
);
38385 /* Transactional Memory parsing routines. */
38387 /* Parse a transaction attribute.
38393 We use this instead of cp_parser_attributes_opt for transactions to avoid
38394 the pedwarn in C++98 mode. */
38397 cp_parser_txn_attribute_opt (cp_parser
*parser
)
38400 tree attr_name
, attr
= NULL
;
38402 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
38403 return cp_parser_attributes_opt (parser
);
38405 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
38407 cp_lexer_consume_token (parser
->lexer
);
38408 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
38411 token
= cp_lexer_peek_token (parser
->lexer
);
38412 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
38414 token
= cp_lexer_consume_token (parser
->lexer
);
38416 attr_name
= (token
->type
== CPP_KEYWORD
38417 /* For keywords, use the canonical spelling,
38418 not the parsed identifier. */
38419 ? ridpointers
[(int) token
->keyword
]
38421 attr
= build_tree_list (attr_name
, NULL_TREE
);
38424 cp_parser_error (parser
, "expected identifier");
38426 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38428 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
38432 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38434 transaction-statement:
38435 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38437 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38441 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
38443 unsigned char old_in
= parser
->in_transaction
;
38444 unsigned char this_in
= 1, new_in
;
38445 enum rid keyword
= token
->keyword
;
38446 tree stmt
, attrs
, noex
;
38448 cp_lexer_consume_token (parser
->lexer
);
38450 if (keyword
== RID_TRANSACTION_RELAXED
38451 || keyword
== RID_SYNCHRONIZED
)
38452 this_in
|= TM_STMT_ATTR_RELAXED
;
38455 attrs
= cp_parser_txn_attribute_opt (parser
);
38457 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38460 /* Parse a noexcept specification. */
38461 if (keyword
== RID_ATOMIC_NOEXCEPT
)
38462 noex
= boolean_true_node
;
38463 else if (keyword
== RID_ATOMIC_CANCEL
)
38465 /* cancel-and-throw is unimplemented. */
38466 sorry ("atomic_cancel");
38470 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
38472 /* Keep track if we're in the lexical scope of an outer transaction. */
38473 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
38475 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
38477 parser
->in_transaction
= new_in
;
38478 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
38479 parser
->in_transaction
= old_in
;
38481 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
38486 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38488 transaction-expression:
38489 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38490 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38494 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
38496 unsigned char old_in
= parser
->in_transaction
;
38497 unsigned char this_in
= 1;
38501 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38503 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38504 || keyword
== RID_TRANSACTION_RELAXED
);
38508 keyword
== RID_TRANSACTION_RELAXED
38509 ? G_("%<__transaction_relaxed%> without transactional memory "
38511 : G_("%<__transaction_atomic%> without transactional memory "
38512 "support enabled"));
38514 token
= cp_parser_require_keyword (parser
, keyword
,
38515 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38516 : RT_TRANSACTION_RELAXED
));
38517 gcc_assert (token
!= NULL
);
38519 if (keyword
== RID_TRANSACTION_RELAXED
)
38520 this_in
|= TM_STMT_ATTR_RELAXED
;
38522 /* Set this early. This might mean that we allow transaction_cancel in
38523 an expression that we find out later actually has to be a constexpr.
38524 However, we expect that cxx_constant_value will be able to deal with
38525 this; also, if the noexcept has no constexpr, then what we parse next
38526 really is a transaction's body. */
38527 parser
->in_transaction
= this_in
;
38529 /* Parse a noexcept specification. */
38530 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
38533 if (!noex
|| !noex_expr
38534 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
38536 matching_parens parens
;
38537 parens
.require_open (parser
);
38539 expr
= cp_parser_expression (parser
);
38540 expr
= finish_parenthesized_expr (expr
);
38542 parens
.require_close (parser
);
38546 /* The only expression that is available got parsed for the noexcept
38547 already. noexcept is true then. */
38549 noex
= boolean_true_node
;
38552 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
38553 parser
->in_transaction
= old_in
;
38555 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
38556 return error_mark_node
;
38558 return (flag_tm
? expr
: error_mark_node
);
38561 /* Parse a function-transaction-block.
38563 function-transaction-block:
38564 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38566 __transaction_atomic txn-attribute[opt] function-try-block
38567 __transaction_relaxed ctor-initializer[opt] function-body
38568 __transaction_relaxed function-try-block
38572 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
38574 unsigned char old_in
= parser
->in_transaction
;
38575 unsigned char new_in
= 1;
38576 tree compound_stmt
, stmt
, attrs
;
38579 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
38580 || keyword
== RID_TRANSACTION_RELAXED
);
38581 token
= cp_parser_require_keyword (parser
, keyword
,
38582 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
38583 : RT_TRANSACTION_RELAXED
));
38584 gcc_assert (token
!= NULL
);
38586 if (keyword
== RID_TRANSACTION_RELAXED
)
38587 new_in
|= TM_STMT_ATTR_RELAXED
;
38590 attrs
= cp_parser_txn_attribute_opt (parser
);
38592 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
38595 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
38597 parser
->in_transaction
= new_in
;
38599 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
38600 cp_parser_function_try_block (parser
);
38602 cp_parser_ctor_initializer_opt_and_function_body
38603 (parser
, /*in_function_try_block=*/false);
38605 parser
->in_transaction
= old_in
;
38607 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
38610 /* Parse a __transaction_cancel statement.
38613 __transaction_cancel txn-attribute[opt] ;
38614 __transaction_cancel txn-attribute[opt] throw-expression ;
38616 ??? Cancel and throw is not yet implemented. */
38619 cp_parser_transaction_cancel (cp_parser
*parser
)
38622 bool is_outer
= false;
38625 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
38626 RT_TRANSACTION_CANCEL
);
38627 gcc_assert (token
!= NULL
);
38629 attrs
= cp_parser_txn_attribute_opt (parser
);
38631 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
38633 /* ??? Parse cancel-and-throw here. */
38635 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38639 error_at (token
->location
, "%<__transaction_cancel%> without "
38640 "transactional memory support enabled");
38641 return error_mark_node
;
38643 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
38645 error_at (token
->location
, "%<__transaction_cancel%> within a "
38646 "%<__transaction_relaxed%>");
38647 return error_mark_node
;
38651 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
38652 && !is_tm_may_cancel_outer (current_function_decl
))
38654 error_at (token
->location
, "outer %<__transaction_cancel%> not "
38655 "within outer %<__transaction_atomic%>");
38656 error_at (token
->location
,
38657 " or a %<transaction_may_cancel_outer%> function");
38658 return error_mark_node
;
38661 else if (parser
->in_transaction
== 0)
38663 error_at (token
->location
, "%<__transaction_cancel%> not within "
38664 "%<__transaction_atomic%>");
38665 return error_mark_node
;
38668 stmt
= build_tm_abort_call (token
->location
, is_outer
);
38676 static GTY (()) cp_parser
*the_parser
;
38679 /* Special handling for the first token or line in the file. The first
38680 thing in the file might be #pragma GCC pch_preprocess, which loads a
38681 PCH file, which is a GC collection point. So we need to handle this
38682 first pragma without benefit of an existing lexer structure.
38684 Always returns one token to the caller in *FIRST_TOKEN. This is
38685 either the true first token of the file, or the first token after
38686 the initial pragma. */
38689 cp_parser_initial_pragma (cp_token
*first_token
)
38693 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38694 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
38697 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38698 if (first_token
->type
== CPP_STRING
)
38700 name
= first_token
->u
.value
;
38702 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38703 if (first_token
->type
!= CPP_PRAGMA_EOL
)
38704 error_at (first_token
->location
,
38705 "junk at end of %<#pragma GCC pch_preprocess%>");
38708 error_at (first_token
->location
, "expected string literal");
38710 /* Skip to the end of the pragma. */
38711 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
38712 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38714 /* Now actually load the PCH file. */
38716 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
38718 /* Read one more token to return to our caller. We have to do this
38719 after reading the PCH file in, since its pointers have to be
38721 cp_lexer_get_preprocessor_token (NULL
, first_token
);
38724 /* Parse a pragma GCC ivdep. */
38727 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
38729 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38733 /* Parse a pragma GCC unroll. */
38735 static unsigned short
38736 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
38738 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
38739 tree expr
= cp_parser_constant_expression (parser
);
38740 unsigned short unroll
;
38741 expr
= maybe_constant_value (expr
);
38742 HOST_WIDE_INT lunroll
= 0;
38743 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
38744 || TREE_CODE (expr
) != INTEGER_CST
38745 || (lunroll
= tree_to_shwi (expr
)) < 0
38746 || lunroll
>= USHRT_MAX
)
38748 error_at (location
, "%<#pragma GCC unroll%> requires an"
38749 " assignment-expression that evaluates to a non-negative"
38750 " integral constant less than %u", USHRT_MAX
);
38755 unroll
= (unsigned short)lunroll
;
38759 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38763 /* Normal parsing of a pragma token. Here we can (and must) use the
38767 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
38769 cp_token
*pragma_tok
;
38774 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
38775 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
38776 parser
->lexer
->in_pragma
= true;
38778 id
= cp_parser_pragma_kind (pragma_tok
);
38779 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
38780 cp_ensure_no_omp_declare_simd (parser
);
38783 case PRAGMA_GCC_PCH_PREPROCESS
:
38784 error_at (pragma_tok
->location
,
38785 "%<#pragma GCC pch_preprocess%> must be first");
38788 case PRAGMA_OMP_BARRIER
:
38791 case pragma_compound
:
38792 cp_parser_omp_barrier (parser
, pragma_tok
);
38795 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38796 "used in compound statements", "omp barrier");
38803 case PRAGMA_OMP_FLUSH
:
38806 case pragma_compound
:
38807 cp_parser_omp_flush (parser
, pragma_tok
);
38810 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
38811 "used in compound statements", "omp flush");
38818 case PRAGMA_OMP_TASKWAIT
:
38821 case pragma_compound
:
38822 cp_parser_omp_taskwait (parser
, pragma_tok
);
38825 error_at (pragma_tok
->location
,
38826 "%<#pragma %s%> may only be used in compound statements",
38834 case PRAGMA_OMP_TASKYIELD
:
38837 case pragma_compound
:
38838 cp_parser_omp_taskyield (parser
, pragma_tok
);
38841 error_at (pragma_tok
->location
,
38842 "%<#pragma %s%> may only be used in compound statements",
38850 case PRAGMA_OMP_CANCEL
:
38853 case pragma_compound
:
38854 cp_parser_omp_cancel (parser
, pragma_tok
);
38857 error_at (pragma_tok
->location
,
38858 "%<#pragma %s%> may only be used in compound statements",
38866 case PRAGMA_OMP_CANCELLATION_POINT
:
38867 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
38870 case PRAGMA_OMP_THREADPRIVATE
:
38871 cp_parser_omp_threadprivate (parser
, pragma_tok
);
38874 case PRAGMA_OMP_DECLARE
:
38875 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
38877 case PRAGMA_OACC_DECLARE
:
38878 cp_parser_oacc_declare (parser
, pragma_tok
);
38881 case PRAGMA_OACC_ENTER_DATA
:
38882 if (context
== pragma_stmt
)
38884 error_at (pragma_tok
->location
,
38885 "%<#pragma %s%> may only be used in compound statements",
38889 else if (context
!= pragma_compound
)
38891 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38894 case PRAGMA_OACC_EXIT_DATA
:
38895 if (context
== pragma_stmt
)
38897 error_at (pragma_tok
->location
,
38898 "%<#pragma %s%> may only be used in compound statements",
38902 else if (context
!= pragma_compound
)
38904 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38907 case PRAGMA_OACC_ROUTINE
:
38908 if (context
!= pragma_external
)
38910 error_at (pragma_tok
->location
,
38911 "%<#pragma acc routine%> must be at file scope");
38914 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
38917 case PRAGMA_OACC_UPDATE
:
38918 if (context
== pragma_stmt
)
38920 error_at (pragma_tok
->location
,
38921 "%<#pragma %s%> may only be used in compound statements",
38925 else if (context
!= pragma_compound
)
38927 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38930 case PRAGMA_OACC_WAIT
:
38931 if (context
== pragma_stmt
)
38933 error_at (pragma_tok
->location
,
38934 "%<#pragma %s%> may only be used in compound statements",
38938 else if (context
!= pragma_compound
)
38940 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38943 case PRAGMA_OACC_ATOMIC
:
38944 case PRAGMA_OACC_CACHE
:
38945 case PRAGMA_OACC_DATA
:
38946 case PRAGMA_OACC_HOST_DATA
:
38947 case PRAGMA_OACC_KERNELS
:
38948 case PRAGMA_OACC_PARALLEL
:
38949 case PRAGMA_OACC_LOOP
:
38950 case PRAGMA_OMP_ATOMIC
:
38951 case PRAGMA_OMP_CRITICAL
:
38952 case PRAGMA_OMP_DISTRIBUTE
:
38953 case PRAGMA_OMP_FOR
:
38954 case PRAGMA_OMP_MASTER
:
38955 case PRAGMA_OMP_PARALLEL
:
38956 case PRAGMA_OMP_SECTIONS
:
38957 case PRAGMA_OMP_SIMD
:
38958 case PRAGMA_OMP_SINGLE
:
38959 case PRAGMA_OMP_TASK
:
38960 case PRAGMA_OMP_TASKGROUP
:
38961 case PRAGMA_OMP_TASKLOOP
:
38962 case PRAGMA_OMP_TEAMS
:
38963 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38965 stmt
= push_omp_privatization_clauses (false);
38966 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
38967 pop_omp_privatization_clauses (stmt
);
38970 case PRAGMA_OMP_ORDERED
:
38971 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38973 stmt
= push_omp_privatization_clauses (false);
38974 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
38975 pop_omp_privatization_clauses (stmt
);
38978 case PRAGMA_OMP_TARGET
:
38979 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
38981 stmt
= push_omp_privatization_clauses (false);
38982 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
38983 pop_omp_privatization_clauses (stmt
);
38986 case PRAGMA_OMP_END_DECLARE_TARGET
:
38987 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
38990 case PRAGMA_OMP_SECTION
:
38991 error_at (pragma_tok
->location
,
38992 "%<#pragma omp section%> may only be used in "
38993 "%<#pragma omp sections%> construct");
38998 if (context
== pragma_external
)
39000 error_at (pragma_tok
->location
,
39001 "%<#pragma GCC ivdep%> must be inside a function");
39004 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
39005 unsigned short unroll
;
39006 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39007 if (tok
->type
== CPP_PRAGMA
39008 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
39010 tok
= cp_lexer_consume_token (parser
->lexer
);
39011 unroll
= cp_parser_pragma_unroll (parser
, tok
);
39012 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39016 if (tok
->type
!= CPP_KEYWORD
39017 || (tok
->keyword
!= RID_FOR
39018 && tok
->keyword
!= RID_WHILE
39019 && tok
->keyword
!= RID_DO
))
39021 cp_parser_error (parser
, "for, while or do statement expected");
39024 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39028 case PRAGMA_UNROLL
:
39030 if (context
== pragma_external
)
39032 error_at (pragma_tok
->location
,
39033 "%<#pragma GCC unroll%> must be inside a function");
39036 const unsigned short unroll
39037 = cp_parser_pragma_unroll (parser
, pragma_tok
);
39039 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39040 if (tok
->type
== CPP_PRAGMA
39041 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
39043 tok
= cp_lexer_consume_token (parser
->lexer
);
39044 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
39045 tok
= cp_lexer_peek_token (the_parser
->lexer
);
39049 if (tok
->type
!= CPP_KEYWORD
39050 || (tok
->keyword
!= RID_FOR
39051 && tok
->keyword
!= RID_WHILE
39052 && tok
->keyword
!= RID_DO
))
39054 cp_parser_error (parser
, "for, while or do statement expected");
39057 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
39062 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
39063 c_invoke_pragma_handler (id
);
39067 cp_parser_error (parser
, "expected declaration specifiers");
39071 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39075 /* The interface the pragma parsers have to the lexer. */
39078 pragma_lex (tree
*value
, location_t
*loc
)
39080 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
39081 enum cpp_ttype ret
= tok
->type
;
39083 *value
= tok
->u
.value
;
39085 *loc
= tok
->location
;
39087 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
39089 else if (ret
== CPP_STRING
)
39090 *value
= cp_parser_string_literal (the_parser
, false, false);
39093 if (ret
== CPP_KEYWORD
)
39095 cp_lexer_consume_token (the_parser
->lexer
);
39102 /* External interface. */
39104 /* Parse one entire translation unit. */
39107 c_parse_file (void)
39109 static bool already_called
= false;
39111 if (already_called
)
39112 fatal_error (input_location
,
39113 "inter-module optimizations not implemented for C++");
39114 already_called
= true;
39116 the_parser
= cp_parser_new ();
39117 push_deferring_access_checks (flag_access_control
39118 ? dk_no_deferred
: dk_no_check
);
39119 cp_parser_translation_unit (the_parser
);
39123 /* Create an identifier for a generic parameter type (a synthesized
39124 template parameter implied by `auto' or a concept identifier). */
39126 static GTY(()) int generic_parm_count
;
39128 make_generic_type_name ()
39131 sprintf (buf
, "auto:%d", ++generic_parm_count
);
39132 return get_identifier (buf
);
39135 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39136 (creating a new template parameter list if necessary). Returns the newly
39137 created template type parm. */
39140 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
39142 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
39144 /* Before committing to modifying any scope, if we're in an
39145 implicit template scope, and we're trying to synthesize a
39146 constrained parameter, try to find a previous parameter with
39147 the same name. This is the same-type rule for abbreviated
39148 function templates.
39150 NOTE: We can generate implicit parameters when tentatively
39151 parsing a nested name specifier, only to reject that parse
39152 later. However, matching the same template-id as part of a
39153 direct-declarator should generate an identical template
39154 parameter, so this rule will merge them. */
39155 if (parser
->implicit_template_scope
&& constr
)
39157 tree t
= parser
->implicit_template_parms
;
39160 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
39162 tree d
= TREE_VALUE (t
);
39163 if (TREE_CODE (d
) == PARM_DECL
)
39164 /* Return the TEMPLATE_PARM_INDEX. */
39165 d
= DECL_INITIAL (d
);
39168 t
= TREE_CHAIN (t
);
39172 /* We are either continuing a function template that already contains implicit
39173 template parameters, creating a new fully-implicit function template, or
39174 extending an existing explicit function template with implicit template
39177 cp_binding_level
*const entry_scope
= current_binding_level
;
39179 bool become_template
= false;
39180 cp_binding_level
*parent_scope
= 0;
39182 if (parser
->implicit_template_scope
)
39184 gcc_assert (parser
->implicit_template_parms
);
39186 current_binding_level
= parser
->implicit_template_scope
;
39190 /* Roll back to the existing template parameter scope (in the case of
39191 extending an explicit function template) or introduce a new template
39192 parameter scope ahead of the function parameter scope (or class scope
39193 in the case of out-of-line member definitions). The function scope is
39194 added back after template parameter synthesis below. */
39196 cp_binding_level
*scope
= entry_scope
;
39198 while (scope
->kind
== sk_function_parms
)
39200 parent_scope
= scope
;
39201 scope
= scope
->level_chain
;
39203 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
39205 /* If not defining a class, then any class scope is a scope level in
39206 an out-of-line member definition. In this case simply wind back
39207 beyond the first such scope to inject the template parameter list.
39208 Otherwise wind back to the class being defined. The latter can
39209 occur in class member friend declarations such as:
39215 friend void A::foo (auto);
39218 The template parameter list synthesized for the friend declaration
39219 must be injected in the scope of 'B'. This can also occur in
39220 erroneous cases such as:
39226 void B::foo (auto) {}
39229 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39230 but, nevertheless, the template parameter list synthesized for the
39231 declarator should be injected into the scope of 'A' as if the
39232 ill-formed template was specified explicitly. */
39234 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
39236 parent_scope
= scope
;
39237 scope
= scope
->level_chain
;
39241 current_binding_level
= scope
;
39243 if (scope
->kind
!= sk_template_parms
39244 || !function_being_declared_is_template_p (parser
))
39246 /* Introduce a new template parameter list for implicit template
39249 become_template
= true;
39251 parser
->implicit_template_scope
39252 = begin_scope (sk_template_parms
, NULL
);
39254 ++processing_template_decl
;
39256 parser
->fully_implicit_function_template_p
= true;
39257 ++parser
->num_template_parameter_lists
;
39261 /* Synthesize implicit template parameters at the end of the explicit
39262 template parameter list. */
39264 gcc_assert (current_template_parms
);
39266 parser
->implicit_template_scope
= scope
;
39268 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39269 parser
->implicit_template_parms
39270 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
39274 /* Synthesize a new template parameter and track the current template
39275 parameter chain with implicit_template_parms. */
39277 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
39278 tree synth_id
= make_generic_type_name ();
39279 tree synth_tmpl_parm
;
39280 bool non_type
= false;
39282 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
39284 = finish_template_type_parm (class_type_node
, synth_id
);
39285 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
39287 = finish_constrained_template_template_parm (proto
, synth_id
);
39290 synth_tmpl_parm
= copy_decl (proto
);
39291 DECL_NAME (synth_tmpl_parm
) = synth_id
;
39295 // Attach the constraint to the parm before processing.
39296 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
39297 TREE_TYPE (node
) = constr
;
39299 = process_template_parm (parser
->implicit_template_parms
,
39302 /*non_type=*/non_type
,
39303 /*param_pack=*/false);
39305 // Chain the new parameter to the list of implicit parameters.
39306 if (parser
->implicit_template_parms
)
39307 parser
->implicit_template_parms
39308 = TREE_CHAIN (parser
->implicit_template_parms
);
39310 parser
->implicit_template_parms
= new_parm
;
39312 tree new_decl
= get_local_decls ();
39314 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39315 new_decl
= DECL_INITIAL (new_decl
);
39317 /* If creating a fully implicit function template, start the new implicit
39318 template parameter list with this synthesized type, otherwise grow the
39319 current template parameter list. */
39321 if (become_template
)
39323 parent_scope
->level_chain
= current_binding_level
;
39325 tree new_parms
= make_tree_vec (1);
39326 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
39327 current_template_parms
= tree_cons (size_int (processing_template_decl
),
39328 new_parms
, current_template_parms
);
39332 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
39333 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
39334 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
39335 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
39338 // If the new parameter was constrained, we need to add that to the
39339 // constraints in the template parameter list.
39340 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
39342 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
39343 reqs
= conjoin_constraints (reqs
, req
);
39344 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
39347 current_binding_level
= entry_scope
;
39352 /* Finish the declaration of a fully implicit function template. Such a
39353 template has no explicit template parameter list so has not been through the
39354 normal template head and tail processing. synthesize_implicit_template_parm
39355 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39356 provided if the declaration is a class member such that its template
39357 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39358 form is returned. Otherwise NULL_TREE is returned. */
39361 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
39363 gcc_assert (parser
->fully_implicit_function_template_p
);
39365 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
39366 && DECL_VIRTUAL_P (member_decl_opt
))
39368 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
39369 "implicit templates may not be %<virtual%>");
39370 DECL_VIRTUAL_P (member_decl_opt
) = false;
39373 if (member_decl_opt
)
39374 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
39375 end_template_decl ();
39377 parser
->fully_implicit_function_template_p
= false;
39378 parser
->implicit_template_parms
= 0;
39379 parser
->implicit_template_scope
= 0;
39380 --parser
->num_template_parameter_lists
;
39382 return member_decl_opt
;
39385 /* Like finish_fully_implicit_template, but to be used in error
39386 recovery, rearranging scopes so that we restore the state we had
39387 before synthesize_implicit_template_parm inserted the implement
39388 template parms scope. */
39391 abort_fully_implicit_template (cp_parser
*parser
)
39393 cp_binding_level
*return_to_scope
= current_binding_level
;
39395 if (parser
->implicit_template_scope
39396 && return_to_scope
!= parser
->implicit_template_scope
)
39398 cp_binding_level
*child
= return_to_scope
;
39399 for (cp_binding_level
*scope
= child
->level_chain
;
39400 scope
!= parser
->implicit_template_scope
;
39401 scope
= child
->level_chain
)
39403 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
39404 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
39405 current_binding_level
= parser
->implicit_template_scope
;
39408 return_to_scope
= return_to_scope
->level_chain
;
39410 finish_fully_implicit_template (parser
, NULL
);
39412 gcc_assert (current_binding_level
== return_to_scope
);
39415 /* Helper function for diagnostics that have complained about things
39416 being used with 'extern "C"' linkage.
39418 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39421 maybe_show_extern_c_location (void)
39423 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
39424 inform (the_parser
->innermost_linkage_specification_location
,
39425 "%<extern \"C\"%> linkage started here");
39428 #include "gt-cp-parser.h"