2 Copyright (C) 2000-2016 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/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "stringpool.h"
29 #include "print-tree.h"
31 #include "trans-mem.h"
34 #include "c-family/c-objc.h"
36 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
42 #include "cp-cilkplus.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token
=
52 CPP_EOF
, RID_MAX
, 0, false, false, false, 0, { NULL
}
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant
{
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 /* a transaction expression */
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error
{
120 /* is not a class or namespace */
122 /* is not a class, namespace, or enumeration */
126 /* The various kinds of required token */
127 enum required_token
{
129 RT_SEMICOLON
, /* ';' */
130 RT_OPEN_PAREN
, /* '(' */
131 RT_CLOSE_BRACE
, /* '}' */
132 RT_OPEN_BRACE
, /* '{' */
133 RT_CLOSE_SQUARE
, /* ']' */
134 RT_OPEN_SQUARE
, /* '[' */
138 RT_GREATER
, /* '>' */
140 RT_ELLIPSIS
, /* '...' */
144 RT_COLON_SCOPE
, /* ':' or '::' */
145 RT_CLOSE_PAREN
, /* ')' */
146 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
147 RT_PRAGMA_EOL
, /* end of line */
148 RT_NAME
, /* identifier */
150 /* The type is CPP_KEYWORD */
152 RT_DELETE
, /* delete */
153 RT_RETURN
, /* return */
154 RT_WHILE
, /* while */
155 RT_EXTERN
, /* extern */
156 RT_STATIC_ASSERT
, /* static_assert */
157 RT_DECLTYPE
, /* decltype */
158 RT_OPERATOR
, /* operator */
159 RT_CLASS
, /* class */
160 RT_TEMPLATE
, /* template */
161 RT_NAMESPACE
, /* namespace */
162 RT_USING
, /* using */
165 RT_CATCH
, /* catch */
166 RT_THROW
, /* throw */
167 RT_LABEL
, /* __label__ */
168 RT_AT_TRY
, /* @try */
169 RT_AT_SYNCHRONIZED
, /* @synchronized */
170 RT_AT_THROW
, /* @throw */
172 RT_SELECT
, /* selection-statement */
173 RT_INTERATION
, /* iteration-statement */
174 RT_JUMP
, /* jump-statement */
175 RT_CLASS_KEY
, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
182 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
183 reverting it on destruction. */
185 class type_id_in_expr_sentinel
190 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
192 saved (parser
->in_type_id_in_expr_p
)
193 { parser
->in_type_id_in_expr_p
= set
; }
194 ~type_id_in_expr_sentinel ()
195 { parser
->in_type_id_in_expr_p
= saved
; }
200 static cp_lexer
*cp_lexer_new_main
202 static cp_lexer
*cp_lexer_new_from_tokens
203 (cp_token_cache
*tokens
);
204 static void cp_lexer_destroy
206 static int cp_lexer_saving_tokens
208 static cp_token
*cp_lexer_token_at
209 (cp_lexer
*, cp_token_position
);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer
*, cp_token
*);
212 static inline cp_token
*cp_lexer_peek_token
214 static cp_token
*cp_lexer_peek_nth_token
215 (cp_lexer
*, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer
*, enum cpp_ttype
);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer
*, enum cpp_ttype
);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer
*, enum rid
);
222 static cp_token
*cp_lexer_consume_token
224 static void cp_lexer_purge_token
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer
*, cp_token_position
);
228 static void cp_lexer_save_tokens
230 static void cp_lexer_commit_tokens
232 static void cp_lexer_rollback_tokens
234 static void cp_lexer_print_token
235 (FILE *, cp_token
*);
236 static inline bool cp_lexer_debugging_p
238 static void cp_lexer_start_debugging
239 (cp_lexer
*) ATTRIBUTE_UNUSED
;
240 static void cp_lexer_stop_debugging
241 (cp_lexer
*) ATTRIBUTE_UNUSED
;
243 static cp_token_cache
*cp_token_cache_new
244 (cp_token
*, cp_token
*);
246 static void cp_parser_initial_pragma
249 static tree cp_literal_operator_id
252 static void cp_parser_cilk_simd
253 (cp_parser
*, cp_token
*, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser
*, tree
, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser
*, tree
, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser
*, tree
, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream
;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand
;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
283 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
284 cp_token
*start_token
, unsigned num
,
285 cp_token
*curr_token
)
287 unsigned i
, nprinted
;
291 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
297 num
= buffer
->length ();
299 if (start_token
== NULL
)
300 start_token
= buffer
->address ();
302 if (start_token
> buffer
->address ())
304 cp_lexer_print_token (file
, &(*buffer
)[0]);
305 fprintf (file
, " ... ");
310 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
312 if (token
== start_token
)
319 if (token
== curr_token
)
320 fprintf (file
, "[[");
322 cp_lexer_print_token (file
, token
);
324 if (token
== curr_token
)
325 fprintf (file
, "]]");
331 case CPP_CLOSE_BRACE
:
341 if (i
== num
&& i
< buffer
->length ())
343 fprintf (file
, " ... ");
344 cp_lexer_print_token (file
, &buffer
->last ());
347 fprintf (file
, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
354 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
356 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
360 debug (vec
<cp_token
, va_gc
> &ref
)
362 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
366 debug (vec
<cp_token
, va_gc
> *ptr
)
371 fprintf (stderr
, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
379 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
383 fprintf (file
, "%s: ", desc
);
384 print_node_brief (file
, "", t
, 0);
389 /* Dump parser context C to FILE. */
392 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
394 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
396 print_node_brief (file
, "", c
->object_type
, 0);
397 fprintf (file
, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
407 cp_parser_context
*c
;
409 fprintf (file
, "Parsing context stack:\n");
410 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
412 fprintf (file
, "\t#%u: ", i
);
413 cp_debug_print_context (file
, c
);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
424 fprintf (file
, "%s: true\n", desc
);
428 /* Print an unparsed function entry UF to FILE. */
431 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
434 cp_default_arg_entry
*default_arg_fn
;
437 fprintf (file
, "\tFunctions with default args:\n");
439 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
442 fprintf (file
, "\t\tClass type: ");
443 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
444 fprintf (file
, "\t\tDeclaration: ");
445 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
446 fprintf (file
, "\n");
449 fprintf (file
, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
453 print_node_brief (file
, "", fn
, 0);
456 fprintf (file
, "\n");
458 fprintf (file
, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
462 print_node_brief (file
, "", fn
, 0);
465 fprintf (file
, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
472 cp_debug_print_unparsed_queues (FILE *file
,
473 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
476 cp_unparsed_functions_entry
*uf
;
478 fprintf (file
, "Unparsed functions\n");
479 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
481 fprintf (file
, "#%u:\n", i
);
482 cp_debug_print_unparsed_function (file
, uf
);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
493 cp_token
*next_token
, *first_token
, *start_token
;
498 next_token
= parser
->lexer
->next_token
;
499 first_token
= parser
->lexer
->buffer
->address ();
500 start_token
= (next_token
> first_token
+ window_size
/ 2)
501 ? next_token
- window_size
/ 2
503 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
512 cp_debug_parser (FILE *file
, cp_parser
*parser
)
514 const size_t window_size
= 20;
516 expanded_location eloc
;
521 fprintf (file
, "Parser state\n\n");
522 fprintf (file
, "Number of tokens: %u\n",
523 vec_safe_length (parser
->lexer
->buffer
));
524 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
525 cp_debug_print_tree_if_set (file
, "Object scope",
526 parser
->object_scope
);
527 cp_debug_print_tree_if_set (file
, "Qualifying scope",
528 parser
->qualifying_scope
);
529 cp_debug_print_context_stack (file
, parser
->context
);
530 cp_debug_print_flag (file
, "Allow GNU extensions",
531 parser
->allow_gnu_extensions_p
);
532 cp_debug_print_flag (file
, "'>' token is greater-than",
533 parser
->greater_than_is_operator_p
);
534 cp_debug_print_flag (file
, "Default args allowed in current "
535 "parameter list", parser
->default_arg_ok_p
);
536 cp_debug_print_flag (file
, "Parsing integral constant-expression",
537 parser
->integral_constant_expression_p
);
538 cp_debug_print_flag (file
, "Allow non-constant expression in current "
539 "constant-expression",
540 parser
->allow_non_integral_constant_expression_p
);
541 cp_debug_print_flag (file
, "Seen non-constant expression",
542 parser
->non_integral_constant_expression_p
);
543 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
545 parser
->local_variables_forbidden_p
);
546 cp_debug_print_flag (file
, "In unbraced linkage specification",
547 parser
->in_unbraced_linkage_specification_p
);
548 cp_debug_print_flag (file
, "Parsing a declarator",
549 parser
->in_declarator_p
);
550 cp_debug_print_flag (file
, "In template argument list",
551 parser
->in_template_argument_list_p
);
552 cp_debug_print_flag (file
, "Parsing an iteration statement",
553 parser
->in_statement
& IN_ITERATION_STMT
);
554 cp_debug_print_flag (file
, "Parsing a switch statement",
555 parser
->in_statement
& IN_SWITCH_STMT
);
556 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
557 parser
->in_statement
& IN_OMP_BLOCK
);
558 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
559 parser
->in_statement
& IN_CILK_SIMD_FOR
);
560 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
561 parser
->in_statement
& IN_OMP_FOR
);
562 cp_debug_print_flag (file
, "Parsing an if statement",
563 parser
->in_statement
& IN_IF_STMT
);
564 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
565 "context", parser
->in_type_id_in_expr_p
);
566 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
567 parser
->implicit_extern_c
);
568 cp_debug_print_flag (file
, "String expressions should be translated "
569 "to execution character set",
570 parser
->translate_strings_p
);
571 cp_debug_print_flag (file
, "Parsing function body outside of a "
572 "local class", parser
->in_function_body
);
573 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
574 parser
->colon_corrects_to_scope_p
);
575 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
576 parser
->colon_doesnt_start_class_def_p
);
577 if (parser
->type_definition_forbidden_message
)
578 fprintf (file
, "Error message for forbidden type definitions: %s\n",
579 parser
->type_definition_forbidden_message
);
580 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
581 fprintf (file
, "Number of class definitions in progress: %u\n",
582 parser
->num_classes_being_defined
);
583 fprintf (file
, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser
->num_template_parameter_lists
);
585 cp_debug_parser_tokens (file
, parser
, window_size
);
586 token
= parser
->lexer
->next_token
;
587 fprintf (file
, "Next token to parse:\n");
588 fprintf (file
, "\tToken: ");
589 cp_lexer_print_token (file
, token
);
590 eloc
= expand_location (token
->location
);
591 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
592 fprintf (file
, "\tLine: %d\n", eloc
.line
);
593 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
597 debug (cp_parser
&ref
)
599 cp_debug_parser (stderr
, &ref
);
603 debug (cp_parser
*ptr
)
608 fprintf (stderr
, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
614 cp_lexer_alloc (void)
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
623 /* Initially we are not debugging. */
624 lexer
->debugging_p
= false;
626 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
628 /* Create the buffer. */
629 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 cp_lexer_new_main (void)
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token
);
649 lexer
= cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer
->buffer
->quick_push (token
);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token
.type
!= CPP_EOF
)
657 cp_lexer_get_preprocessor_token (lexer
, &token
);
658 vec_safe_push (lexer
->buffer
, token
);
661 lexer
->last_token
= lexer
->buffer
->address ()
662 + lexer
->buffer
->length ()
664 lexer
->next_token
= lexer
->buffer
->length ()
665 ? lexer
->buffer
->address ()
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
672 gcc_assert (!lexer
->next_token
->purged_p
);
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
680 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
682 cp_token
*first
= cache
->first
;
683 cp_token
*last
= cache
->last
;
684 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
686 /* We do not own the buffer. */
687 lexer
->buffer
= NULL
;
688 lexer
->next_token
= first
== last
? &eof_token
: first
;
689 lexer
->last_token
= last
;
691 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
693 /* Initially we are not debugging. */
694 lexer
->debugging_p
= false;
696 gcc_assert (!lexer
->next_token
->purged_p
);
700 /* Frees all resources associated with LEXER. */
703 cp_lexer_destroy (cp_lexer
*lexer
)
705 vec_free (lexer
->buffer
);
706 lexer
->saved_tokens
.release ();
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
720 cp_lexer_debugging_p (cp_lexer
*lexer
)
722 if (!LEXER_DEBUGGING_ENABLED_P
)
725 return lexer
->debugging_p
;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
732 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
734 return lexer
->next_token
- previous_p
;
737 static inline cp_token
*
738 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
744 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
746 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer
*lexer
)
752 if (lexer
->next_token
== &eof_token
)
753 return lexer
->last_token
- 1;
755 return cp_lexer_token_position (lexer
, true);
758 static inline cp_token
*
759 cp_lexer_previous_token (cp_lexer
*lexer
)
761 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
763 /* Skip past purged tokens. */
766 gcc_assert (tp
!= lexer
->buffer
->address ());
770 return cp_lexer_token_at (lexer
, tp
);
773 /* nonzero if we are presently saving tokens. */
776 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
778 return lexer
->saved_tokens
.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
787 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
789 static int is_extern_c
= 0;
791 /* Get a new token from the preprocessor. */
793 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
794 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
795 token
->keyword
= RID_MAX
;
796 token
->purged_p
= false;
797 token
->error_reported
= false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c
+= pending_lang_change
;
803 pending_lang_change
= 0;
804 token
->implicit_extern_c
= is_extern_c
> 0;
806 /* Check to see if this token is a keyword. */
807 if (token
->type
== CPP_NAME
)
809 if (C_IS_RESERVED_WORD (token
->u
.value
))
811 /* Mark this token as a keyword. */
812 token
->type
= CPP_KEYWORD
;
813 /* Record which keyword. */
814 token
->keyword
= C_RID_CODE (token
->u
.value
);
818 if (warn_cxx11_compat
819 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
820 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
822 /* Warn about the C++0x keyword (but still treat it as
824 warning (OPT_Wc__11_compat
,
825 "identifier %qE is a keyword in C++11",
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
833 token
->keyword
= RID_MAX
;
836 else if (token
->type
== CPP_AT_NAME
)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token
->type
= CPP_KEYWORD
;
840 switch (C_RID_CODE (token
->u
.value
))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
850 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
851 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
852 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
853 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
854 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
855 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
856 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
857 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
858 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
863 /* Update the globals input_location and the input file stack from TOKEN. */
865 cp_lexer_set_source_position_from_token (cp_token
*token
)
867 if (token
->type
!= CPP_EOF
)
869 input_location
= token
->location
;
873 /* Update the globals input_location and the input file stack from LEXER. */
875 cp_lexer_set_source_position (cp_lexer
*lexer
)
877 cp_token
*token
= cp_lexer_peek_token (lexer
);
878 cp_lexer_set_source_position_from_token (token
);
881 /* Return a pointer to the next token in the token stream, but do not
884 static inline cp_token
*
885 cp_lexer_peek_token (cp_lexer
*lexer
)
887 if (cp_lexer_debugging_p (lexer
))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
890 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
891 putc ('\n', cp_lexer_debug_stream
);
893 return lexer
->next_token
;
896 /* Return true if the next token has the indicated TYPE. */
899 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
901 return cp_lexer_peek_token (lexer
)->type
== type
;
904 /* Return true if the next token does not have the indicated TYPE. */
907 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
909 return !cp_lexer_next_token_is (lexer
, type
);
912 /* Return true if the next token is the indicated KEYWORD. */
915 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
917 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
921 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
923 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
927 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
929 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
932 /* Return true if the next token is not the indicated KEYWORD. */
935 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
937 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
940 /* Return true if the next token is a keyword for a decl-specifier. */
943 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
947 token
= cp_lexer_peek_token (lexer
);
948 switch (token
->keyword
)
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
953 /* Storage classes. */
959 /* Elaborated type specifiers. */
965 /* Simple type specifiers. */
979 /* GNU extensions. */
982 /* C++0x extensions. */
984 case RID_UNDERLYING_TYPE
:
988 if (token
->keyword
>= RID_FIRST_INT_N
989 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
990 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
996 /* Returns TRUE iff the token T begins a decltype type. */
999 token_is_decltype (cp_token
*t
)
1001 return (t
->keyword
== RID_DECLTYPE
1002 || t
->type
== CPP_DECLTYPE
);
1005 /* Returns TRUE iff the next token begins a decltype type. */
1008 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1010 cp_token
*t
= cp_lexer_peek_token (lexer
);
1011 return token_is_decltype (t
);
1014 /* Called when processing a token with tree_check_value; perform or defer the
1015 associated checks and return the value. */
1018 saved_checks_value (struct tree_check
*check_value
)
1020 /* Perform any access checks that were deferred. */
1021 vec
<deferred_access_check
, va_gc
> *checks
;
1022 deferred_access_check
*chk
;
1023 checks
= check_value
->checks
;
1027 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1028 perform_or_defer_access_check (chk
->binfo
,
1030 chk
->diag_decl
, tf_warning_or_error
);
1032 /* Return the stored value. */
1033 return check_value
->value
;
1036 /* Return a pointer to the Nth token in the token stream. If N is 1,
1037 then this is precisely equivalent to cp_lexer_peek_token (except
1038 that it is not inline). One would like to disallow that case, but
1039 there is one case (cp_parser_nth_token_starts_template_id) where
1040 the caller passes a variable for N and it might be 1. */
1043 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1047 /* N is 1-based, not zero-based. */
1050 if (cp_lexer_debugging_p (lexer
))
1051 fprintf (cp_lexer_debug_stream
,
1052 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1055 token
= lexer
->next_token
;
1056 gcc_assert (!n
|| token
!= &eof_token
);
1060 if (token
== lexer
->last_token
)
1066 if (!token
->purged_p
)
1070 if (cp_lexer_debugging_p (lexer
))
1072 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1073 putc ('\n', cp_lexer_debug_stream
);
1079 /* Return the next token, and advance the lexer's next_token pointer
1080 to point to the next non-purged token. */
1083 cp_lexer_consume_token (cp_lexer
* lexer
)
1085 cp_token
*token
= lexer
->next_token
;
1087 gcc_assert (token
!= &eof_token
);
1088 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1092 lexer
->next_token
++;
1093 if (lexer
->next_token
== lexer
->last_token
)
1095 lexer
->next_token
= &eof_token
;
1100 while (lexer
->next_token
->purged_p
);
1102 cp_lexer_set_source_position_from_token (token
);
1104 /* Provide debugging output. */
1105 if (cp_lexer_debugging_p (lexer
))
1107 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1108 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1109 putc ('\n', cp_lexer_debug_stream
);
1115 /* Permanently remove the next token from the token stream, and
1116 advance the next_token pointer to refer to the next non-purged
1120 cp_lexer_purge_token (cp_lexer
*lexer
)
1122 cp_token
*tok
= lexer
->next_token
;
1124 gcc_assert (tok
!= &eof_token
);
1125 tok
->purged_p
= true;
1126 tok
->location
= UNKNOWN_LOCATION
;
1127 tok
->u
.value
= NULL_TREE
;
1128 tok
->keyword
= RID_MAX
;
1133 if (tok
== lexer
->last_token
)
1139 while (tok
->purged_p
);
1140 lexer
->next_token
= tok
;
1143 /* Permanently remove all tokens after TOK, up to, but not
1144 including, the token that will be returned next by
1145 cp_lexer_peek_token. */
1148 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1150 cp_token
*peek
= lexer
->next_token
;
1152 if (peek
== &eof_token
)
1153 peek
= lexer
->last_token
;
1155 gcc_assert (tok
< peek
);
1157 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1159 tok
->purged_p
= true;
1160 tok
->location
= UNKNOWN_LOCATION
;
1161 tok
->u
.value
= NULL_TREE
;
1162 tok
->keyword
= RID_MAX
;
1166 /* Begin saving tokens. All tokens consumed after this point will be
1170 cp_lexer_save_tokens (cp_lexer
* lexer
)
1172 /* Provide debugging output. */
1173 if (cp_lexer_debugging_p (lexer
))
1174 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1176 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1179 /* Commit to the portion of the token stream most recently saved. */
1182 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer
))
1186 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1188 lexer
->saved_tokens
.pop ();
1191 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1192 to the token stream. Stop saving tokens. */
1195 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer
))
1199 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1201 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1204 /* RAII wrapper around the above functions, with sanity checking. Creating
1205 a variable saves tokens, which are committed when the variable is
1206 destroyed unless they are explicitly rolled back by calling the rollback
1209 struct saved_token_sentinel
1214 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1216 len
= lexer
->saved_tokens
.length ();
1217 cp_lexer_save_tokens (lexer
);
1221 cp_lexer_rollback_tokens (lexer
);
1224 ~saved_token_sentinel()
1227 cp_lexer_commit_tokens (lexer
);
1228 gcc_assert (lexer
->saved_tokens
.length () == len
);
1232 /* Print a representation of the TOKEN on the STREAM. */
1235 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1237 /* We don't use cpp_type2name here because the parser defines
1238 a few tokens of its own. */
1239 static const char *const token_names
[] = {
1240 /* cpplib-defined token types */
1241 #define OP(e, s) #e,
1242 #define TK(e, s) #e,
1246 /* C++ parser token types - see "Manifest constants", above. */
1249 "NESTED_NAME_SPECIFIER",
1252 /* For some tokens, print the associated data. */
1253 switch (token
->type
)
1256 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1257 For example, `struct' is mapped to an INTEGER_CST. */
1258 if (!identifier_p (token
->u
.value
))
1260 /* else fall through */
1262 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1269 case CPP_UTF8STRING
:
1270 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1274 print_generic_expr (stream
, token
->u
.value
, 0);
1278 /* If we have a name for the token, print it out. Otherwise, we
1279 simply give the numeric code. */
1280 if (token
->type
< ARRAY_SIZE(token_names
))
1281 fputs (token_names
[token
->type
], stream
);
1283 fprintf (stream
, "[%d]", token
->type
);
1289 debug (cp_token
&ref
)
1291 cp_lexer_print_token (stderr
, &ref
);
1292 fprintf (stderr
, "\n");
1296 debug (cp_token
*ptr
)
1301 fprintf (stderr
, "<nil>\n");
1305 /* Start emitting debugging information. */
1308 cp_lexer_start_debugging (cp_lexer
* lexer
)
1310 if (!LEXER_DEBUGGING_ENABLED_P
)
1311 fatal_error (input_location
,
1312 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1314 lexer
->debugging_p
= true;
1315 cp_lexer_debug_stream
= stderr
;
1318 /* Stop emitting debugging information. */
1321 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1323 if (!LEXER_DEBUGGING_ENABLED_P
)
1324 fatal_error (input_location
,
1325 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1327 lexer
->debugging_p
= false;
1328 cp_lexer_debug_stream
= NULL
;
1331 /* Create a new cp_token_cache, representing a range of tokens. */
1333 static cp_token_cache
*
1334 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1336 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1337 cache
->first
= first
;
1342 /* Diagnose if #pragma omp declare simd isn't followed immediately
1343 by function declaration or definition. */
1346 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1348 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1350 error ("%<#pragma omp declare simd%> not immediately followed by "
1351 "function declaration or definition");
1352 parser
->omp_declare_simd
= NULL
;
1356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1357 and put that into "omp declare simd" attribute. */
1360 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1362 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1364 if (fndecl
== error_mark_node
)
1366 parser
->omp_declare_simd
= NULL
;
1369 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1371 cp_ensure_no_omp_declare_simd (parser
);
1377 /* Diagnose if #pragma acc routine isn't followed immediately by function
1378 declaration or definition. */
1381 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1383 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1385 tree clauses
= parser
->oacc_routine
->clauses
;
1386 location_t loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses
));
1388 error_at (loc
, "%<#pragma acc routine%> not followed by a function "
1389 "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
->attributes
= NULL_TREE
;
1448 declarator
->std_attributes
= NULL_TREE
;
1449 declarator
->declarator
= NULL
;
1450 declarator
->parameter_pack_p
= false;
1451 declarator
->id_loc
= UNKNOWN_LOCATION
;
1456 /* Make a declarator for a generalized identifier. If
1457 QUALIFYING_SCOPE is non-NULL, the identifier is
1458 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1459 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1462 static cp_declarator
*
1463 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1464 special_function_kind sfk
)
1466 cp_declarator
*declarator
;
1468 /* It is valid to write:
1470 class C { void f(); };
1474 The standard is not clear about whether `typedef const C D' is
1475 legal; as of 2002-09-15 the committee is considering that
1476 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1479 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1481 gcc_assert (identifier_p (unqualified_name
)
1482 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1483 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1485 declarator
= make_declarator (cdk_id
);
1486 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1487 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1488 declarator
->u
.id
.sfk
= sfk
;
1493 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1494 of modifiers such as const or volatile to apply to the pointer
1495 type, represented as identifiers. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1499 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1502 cp_declarator
*declarator
;
1504 declarator
= make_declarator (cdk_pointer
);
1505 declarator
->declarator
= target
;
1506 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1507 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1510 declarator
->id_loc
= target
->id_loc
;
1511 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1512 target
->parameter_pack_p
= false;
1515 declarator
->parameter_pack_p
= false;
1517 declarator
->std_attributes
= attributes
;
1522 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1523 represent the attributes that appertain to the pointer or
1527 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1528 bool rvalue_ref
, tree attributes
)
1530 cp_declarator
*declarator
;
1532 declarator
= make_declarator (cdk_reference
);
1533 declarator
->declarator
= target
;
1534 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1535 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1538 declarator
->id_loc
= target
->id_loc
;
1539 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1540 target
->parameter_pack_p
= false;
1543 declarator
->parameter_pack_p
= false;
1545 declarator
->std_attributes
= attributes
;
1550 /* Like make_pointer_declarator -- but for a pointer to a non-static
1551 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1552 appertain to the pointer or reference. */
1555 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1556 cp_declarator
*pointee
,
1559 cp_declarator
*declarator
;
1561 declarator
= make_declarator (cdk_ptrmem
);
1562 declarator
->declarator
= pointee
;
1563 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1564 declarator
->u
.pointer
.class_type
= class_type
;
1568 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1569 pointee
->parameter_pack_p
= false;
1572 declarator
->parameter_pack_p
= false;
1574 declarator
->std_attributes
= attributes
;
1579 /* Make a declarator for the function given by TARGET, with the
1580 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1581 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1582 indicates what exceptions can be thrown. */
1585 make_call_declarator (cp_declarator
*target
,
1587 cp_cv_quals cv_qualifiers
,
1588 cp_virt_specifiers virt_specifiers
,
1589 cp_ref_qualifier ref_qualifier
,
1591 tree exception_specification
,
1592 tree late_return_type
,
1593 tree requires_clause
)
1595 cp_declarator
*declarator
;
1597 declarator
= make_declarator (cdk_function
);
1598 declarator
->declarator
= target
;
1599 declarator
->u
.function
.parameters
= parms
;
1600 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1601 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1602 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1603 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1604 declarator
->u
.function
.exception_specification
= exception_specification
;
1605 declarator
->u
.function
.late_return_type
= late_return_type
;
1606 declarator
->u
.function
.requires_clause
= requires_clause
;
1609 declarator
->id_loc
= target
->id_loc
;
1610 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1611 target
->parameter_pack_p
= false;
1614 declarator
->parameter_pack_p
= false;
1619 /* Make a declarator for an array of BOUNDS elements, each of which is
1620 defined by ELEMENT. */
1623 make_array_declarator (cp_declarator
*element
, tree bounds
)
1625 cp_declarator
*declarator
;
1627 declarator
= make_declarator (cdk_array
);
1628 declarator
->declarator
= element
;
1629 declarator
->u
.array
.bounds
= bounds
;
1632 declarator
->id_loc
= element
->id_loc
;
1633 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1634 element
->parameter_pack_p
= false;
1637 declarator
->parameter_pack_p
= false;
1642 /* Determine whether the declarator we've seen so far can be a
1643 parameter pack, when followed by an ellipsis. */
1645 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1647 if (declarator
&& declarator
->parameter_pack_p
)
1648 /* We already saw an ellipsis. */
1651 /* Search for a declarator name, or any other declarator that goes
1652 after the point where the ellipsis could appear in a parameter
1653 pack. If we find any of these, then this declarator can not be
1654 made into a parameter pack. */
1656 while (declarator
&& !found
)
1658 switch ((int)declarator
->kind
)
1669 declarator
= declarator
->declarator
;
1677 cp_parameter_declarator
*no_parameters
;
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1682 cp_parameter_declarator
*
1683 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1684 cp_declarator
*declarator
,
1685 tree default_argument
,
1686 bool template_parameter_pack_p
= false)
1688 cp_parameter_declarator
*parameter
;
1690 parameter
= ((cp_parameter_declarator
*)
1691 alloc_declarator (sizeof (cp_parameter_declarator
)));
1692 parameter
->next
= NULL
;
1693 if (decl_specifiers
)
1694 parameter
->decl_specifiers
= *decl_specifiers
;
1696 clear_decl_specs (¶meter
->decl_specifiers
);
1697 parameter
->declarator
= declarator
;
1698 parameter
->default_argument
= default_argument
;
1699 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1707 function_declarator_p (const cp_declarator
*declarator
)
1711 if (declarator
->kind
== cdk_function
1712 && declarator
->declarator
->kind
== cdk_id
)
1714 if (declarator
->kind
== cdk_id
1715 || declarator
->kind
== cdk_error
)
1717 declarator
= declarator
->declarator
;
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1783 CP_PARSER_FLAGS_NONE
= 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1798 /* This type is used for parameters and variables which hold
1799 combinations of the above flags. */
1800 typedef int cp_parser_flags
;
1802 /* The different kinds of declarators we want to parse. */
1804 enum cp_parser_declarator_kind
1806 /* We want an abstract declarator. */
1807 CP_PARSER_DECLARATOR_ABSTRACT
,
1808 /* We want a named declarator. */
1809 CP_PARSER_DECLARATOR_NAMED
,
1810 /* We don't mind, but the name must be an unqualified-id. */
1811 CP_PARSER_DECLARATOR_EITHER
1814 /* The precedence values used to parse binary expressions. The minimum value
1815 of PREC must be 1, because zero is reserved to quickly discriminate
1816 binary operators from other tokens. */
1821 PREC_LOGICAL_OR_EXPRESSION
,
1822 PREC_LOGICAL_AND_EXPRESSION
,
1823 PREC_INCLUSIVE_OR_EXPRESSION
,
1824 PREC_EXCLUSIVE_OR_EXPRESSION
,
1825 PREC_AND_EXPRESSION
,
1826 PREC_EQUALITY_EXPRESSION
,
1827 PREC_RELATIONAL_EXPRESSION
,
1828 PREC_SHIFT_EXPRESSION
,
1829 PREC_ADDITIVE_EXPRESSION
,
1830 PREC_MULTIPLICATIVE_EXPRESSION
,
1832 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1835 /* A mapping from a token type to a corresponding tree node type, with a
1836 precedence value. */
1838 struct cp_parser_binary_operations_map_node
1840 /* The token type. */
1841 enum cpp_ttype token_type
;
1842 /* The corresponding tree code. */
1843 enum tree_code tree_type
;
1844 /* The precedence of this operator. */
1845 enum cp_parser_prec prec
;
1848 struct cp_parser_expression_stack_entry
1850 /* Left hand side of the binary operation we are currently
1853 /* Original tree code for left hand side, if it was a binary
1854 expression itself (used for -Wparentheses). */
1855 enum tree_code lhs_type
;
1856 /* Tree code for the binary operation we are parsing. */
1857 enum tree_code tree_type
;
1858 /* Precedence of the binary operation we are parsing. */
1859 enum cp_parser_prec prec
;
1860 /* Location of the binary operation we are parsing. */
1864 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1865 entries because precedence levels on the stack are monotonically
1867 typedef struct cp_parser_expression_stack_entry
1868 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1872 /* Constructors and destructors. */
1874 static cp_parser_context
*cp_parser_context_new
1875 (cp_parser_context
*);
1877 /* Class variables. */
1879 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1881 /* The operator-precedence table used by cp_parser_binary_expression.
1882 Transformed into an associative array (binops_by_token) by
1885 static const cp_parser_binary_operations_map_node binops
[] = {
1886 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1887 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1889 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1890 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1891 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1893 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1894 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1896 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1897 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1899 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1900 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1901 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1902 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1904 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1905 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1907 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1909 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1911 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1913 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1915 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1918 /* The same as binops, but initialized by cp_parser_new so that
1919 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1921 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1923 /* Constructors and destructors. */
1925 /* Construct a new context. The context below this one on the stack
1926 is given by NEXT. */
1928 static cp_parser_context
*
1929 cp_parser_context_new (cp_parser_context
* next
)
1931 cp_parser_context
*context
;
1933 /* Allocate the storage. */
1934 if (cp_parser_context_free_list
!= NULL
)
1936 /* Pull the first entry from the free list. */
1937 context
= cp_parser_context_free_list
;
1938 cp_parser_context_free_list
= context
->next
;
1939 memset (context
, 0, sizeof (*context
));
1942 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1944 /* No errors have occurred yet in this context. */
1945 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1946 /* If this is not the bottommost context, copy information that we
1947 need from the previous context. */
1950 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1951 expression, then we are parsing one in this context, too. */
1952 context
->object_type
= next
->object_type
;
1953 /* Thread the stack. */
1954 context
->next
= next
;
1960 /* Managing the unparsed function queues. */
1962 #define unparsed_funs_with_default_args \
1963 parser->unparsed_queues->last ().funs_with_default_args
1964 #define unparsed_funs_with_definitions \
1965 parser->unparsed_queues->last ().funs_with_definitions
1966 #define unparsed_nsdmis \
1967 parser->unparsed_queues->last ().nsdmis
1968 #define unparsed_classes \
1969 parser->unparsed_queues->last ().classes
1972 push_unparsed_function_queues (cp_parser
*parser
)
1974 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1975 vec_safe_push (parser
->unparsed_queues
, e
);
1979 pop_unparsed_function_queues (cp_parser
*parser
)
1981 release_tree_vector (unparsed_funs_with_definitions
);
1982 parser
->unparsed_queues
->pop ();
1987 /* Constructors and destructors. */
1989 static cp_parser
*cp_parser_new
1992 /* Routines to parse various constructs.
1994 Those that return `tree' will return the error_mark_node (rather
1995 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1996 Sometimes, they will return an ordinary node if error-recovery was
1997 attempted, even though a parse error occurred. So, to check
1998 whether or not a parse error occurred, you should always use
1999 cp_parser_error_occurred. If the construct is optional (indicated
2000 either by an `_opt' in the name of the function that does the
2001 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2002 the construct is not present. */
2004 /* Lexical conventions [gram.lex] */
2006 static cp_expr cp_parser_identifier
2008 static cp_expr cp_parser_string_literal
2009 (cp_parser
*, bool, bool, bool);
2010 static cp_expr cp_parser_userdef_char_literal
2012 static tree cp_parser_userdef_string_literal
2014 static cp_expr cp_parser_userdef_numeric_literal
2017 /* Basic concepts [gram.basic] */
2019 static bool cp_parser_translation_unit
2022 /* Expressions [gram.expr] */
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser
*, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser
*, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser
*, bool, bool, bool, bool);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser
*, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser
*, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser
*, tree
, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2042 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2043 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
);
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2045 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2046 static void cp_parser_pseudo_destructor_name
2047 (cp_parser
*, tree
, tree
*, tree
*);
2048 static cp_expr cp_parser_unary_expression
2049 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2052 static tree cp_parser_new_expression
2054 static vec
<tree
, va_gc
> *cp_parser_new_placement
2056 static tree cp_parser_new_type_id
2057 (cp_parser
*, tree
*);
2058 static cp_declarator
*cp_parser_new_declarator_opt
2060 static cp_declarator
*cp_parser_direct_new_declarator
2062 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2064 static tree cp_parser_delete_expression
2066 static cp_expr cp_parser_cast_expression
2067 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2068 static cp_expr cp_parser_binary_expression
2069 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2070 static tree cp_parser_question_colon_clause
2071 (cp_parser
*, cp_expr
);
2072 static cp_expr cp_parser_assignment_expression
2073 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2076 static cp_expr cp_parser_expression
2077 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079 (cp_parser
*, bool = false, bool * = NULL
);
2080 static cp_expr cp_parser_builtin_offsetof
2082 static cp_expr cp_parser_lambda_expression
2084 static void cp_parser_lambda_introducer
2085 (cp_parser
*, tree
);
2086 static bool cp_parser_lambda_declarator_opt
2087 (cp_parser
*, tree
);
2088 static void cp_parser_lambda_body
2089 (cp_parser
*, tree
);
2091 /* Statements [gram.stmt.stmt] */
2093 static void cp_parser_statement
2094 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser
*, tree
);
2097 static tree cp_parser_expression_statement
2098 (cp_parser
*, tree
);
2099 static tree cp_parser_compound_statement
2100 (cp_parser
*, tree
, int, bool);
2101 static void cp_parser_statement_seq_opt
2102 (cp_parser
*, tree
);
2103 static tree cp_parser_selection_statement
2104 (cp_parser
*, bool *, vec
<tree
> *);
2105 static tree cp_parser_condition
2107 static tree cp_parser_iteration_statement
2108 (cp_parser
*, bool *, bool);
2109 static bool cp_parser_for_init_statement
2110 (cp_parser
*, tree
*decl
);
2111 static tree cp_parser_for
2112 (cp_parser
*, bool);
2113 static tree cp_parser_c_for
2114 (cp_parser
*, tree
, tree
, bool);
2115 static tree cp_parser_range_for
2116 (cp_parser
*, tree
, tree
, tree
, bool);
2117 static void do_range_for_auto_deduction
2119 static tree cp_parser_perform_range_for_lookup
2120 (tree
, tree
*, tree
*);
2121 static tree cp_parser_range_for_member_function
2123 static tree cp_parser_jump_statement
2125 static void cp_parser_declaration_statement
2128 static tree cp_parser_implicitly_scoped_statement
2129 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2130 static void cp_parser_already_scoped_statement
2131 (cp_parser
*, bool *, const token_indent_info
&);
2133 /* Declarations [gram.dcl.dcl] */
2135 static void cp_parser_declaration_seq_opt
2137 static void cp_parser_declaration
2139 static void cp_parser_block_declaration
2140 (cp_parser
*, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser
*, bool, tree
*);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser
*, cp_decl_specifier_seq
*);
2149 static tree cp_parser_type_specifier
2150 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2154 static tree cp_parser_type_name
2155 (cp_parser
*, bool);
2156 static tree cp_parser_type_name
2158 static tree cp_parser_nonclass_name
2159 (cp_parser
* parser
);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser
*, bool, bool);
2162 static tree cp_parser_enum_specifier
2164 static void cp_parser_enumerator_list
2165 (cp_parser
*, tree
);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser
*, tree
);
2168 static tree cp_parser_namespace_name
2170 static void cp_parser_namespace_definition
2172 static void cp_parser_namespace_body
2174 static tree cp_parser_qualified_namespace_specifier
2176 static void cp_parser_namespace_alias_definition
2178 static bool cp_parser_using_declaration
2179 (cp_parser
*, bool);
2180 static void cp_parser_using_directive
2182 static tree cp_parser_alias_declaration
2184 static void cp_parser_asm_definition
2186 static void cp_parser_linkage_specification
2188 static void cp_parser_static_assert
2189 (cp_parser
*, bool);
2190 static tree cp_parser_decltype
2193 /* Declarators [gram.dcl.decl] */
2195 static tree cp_parser_init_declarator
2196 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2197 bool, bool, int, bool *, tree
*, location_t
*, tree
*);
2198 static cp_declarator
*cp_parser_declarator
2199 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2200 static cp_declarator
*cp_parser_direct_declarator
2201 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2202 static enum tree_code cp_parser_ptr_operator
2203 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2204 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2206 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2208 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2210 static tree cp_parser_tx_qualifier_opt
2212 static tree cp_parser_late_return_type_opt
2213 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2214 static tree cp_parser_declarator_id
2215 (cp_parser
*, bool);
2216 static tree cp_parser_type_id
2218 static tree cp_parser_template_type_arg
2220 static tree
cp_parser_trailing_type_id (cp_parser
*);
2221 static tree cp_parser_type_id_1
2222 (cp_parser
*, bool, bool);
2223 static void cp_parser_type_specifier_seq
2224 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2225 static tree cp_parser_parameter_declaration_clause
2227 static tree cp_parser_parameter_declaration_list
2228 (cp_parser
*, bool *);
2229 static cp_parameter_declarator
*cp_parser_parameter_declaration
2230 (cp_parser
*, bool, bool *);
2231 static tree cp_parser_default_argument
2232 (cp_parser
*, bool);
2233 static void cp_parser_function_body
2234 (cp_parser
*, bool);
2235 static tree cp_parser_initializer
2236 (cp_parser
*, bool *, bool *);
2237 static cp_expr cp_parser_initializer_clause
2238 (cp_parser
*, bool *);
2239 static cp_expr cp_parser_braced_list
2240 (cp_parser
*, bool*);
2241 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2242 (cp_parser
*, bool *);
2244 static bool cp_parser_ctor_initializer_opt_and_function_body
2245 (cp_parser
*, bool);
2247 static tree cp_parser_late_parsing_omp_declare_simd
2248 (cp_parser
*, tree
);
2250 static tree cp_parser_late_parsing_cilk_simd_fn_info
2251 (cp_parser
*, tree
);
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser
*, tree
);
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser
*, tree
);
2258 static tree finish_fully_implicit_template
2259 (cp_parser
*, tree
);
2261 /* Classes [gram.class] */
2263 static tree cp_parser_class_name
2264 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2267 static tree cp_parser_class_head
2268 (cp_parser
*, bool *);
2269 static enum tag_types cp_parser_class_key
2271 static void cp_parser_type_parameter_key
2272 (cp_parser
* parser
);
2273 static void cp_parser_member_specification_opt
2275 static void cp_parser_member_declaration
2277 static tree cp_parser_pure_specifier
2279 static tree cp_parser_constant_initializer
2282 /* Derived classes [gram.class.derived] */
2284 static tree cp_parser_base_clause
2286 static tree cp_parser_base_specifier
2289 /* Special member functions [gram.special] */
2291 static tree cp_parser_conversion_function_id
2293 static tree cp_parser_conversion_type_id
2295 static cp_declarator
*cp_parser_conversion_declarator_opt
2297 static bool cp_parser_ctor_initializer_opt
2299 static void cp_parser_mem_initializer_list
2301 static tree cp_parser_mem_initializer
2303 static tree cp_parser_mem_initializer_id
2306 /* Overloading [gram.over] */
2308 static cp_expr cp_parser_operator_function_id
2310 static cp_expr cp_parser_operator
2313 /* Templates [gram.temp] */
2315 static void cp_parser_template_declaration
2316 (cp_parser
*, bool);
2317 static tree cp_parser_template_parameter_list
2319 static tree cp_parser_template_parameter
2320 (cp_parser
*, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser
*, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser
*, bool, bool, enum tag_types
, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2327 static tree cp_parser_template_argument_list
2329 static tree cp_parser_template_argument
2331 static void cp_parser_explicit_instantiation
2333 static void cp_parser_explicit_specialization
2336 /* Exception handling [gram.exception] */
2338 static tree cp_parser_try_block
2340 static bool cp_parser_function_try_block
2342 static void cp_parser_handler_seq
2344 static void cp_parser_handler
2346 static tree cp_parser_exception_declaration
2348 static tree cp_parser_throw_expression
2350 static tree cp_parser_exception_specification_opt
2352 static tree cp_parser_type_id_list
2355 /* GNU Extensions */
2357 static tree cp_parser_asm_specification_opt
2359 static tree cp_parser_asm_operand_list
2361 static tree cp_parser_asm_clobber_list
2363 static tree cp_parser_asm_label_list
2365 static bool cp_next_tokens_can_be_attribute_p
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2369 static bool cp_next_tokens_can_be_std_attribute_p
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser
*, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser
*, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser
*, size_t);
2377 static tree cp_parser_attributes_opt
2379 static tree cp_parser_gnu_attributes_opt
2381 static tree cp_parser_gnu_attribute_list
2383 static tree cp_parser_std_attribute
2385 static tree cp_parser_std_attribute_spec
2387 static tree cp_parser_std_attribute_spec_seq
2389 static bool cp_parser_extension_opt
2390 (cp_parser
*, int *);
2391 static void cp_parser_label_declaration
2394 /* Concept Extensions */
2396 static tree cp_parser_requires_clause
2398 static tree cp_parser_requires_clause_opt
2400 static tree cp_parser_requires_expression
2402 static tree cp_parser_requirement_parameter_list
2404 static tree cp_parser_requirement_body
2406 static tree cp_parser_requirement_list
2408 static tree cp_parser_requirement
2410 static tree cp_parser_simple_requirement
2412 static tree cp_parser_compound_requirement
2414 static tree cp_parser_type_requirement
2416 static tree cp_parser_nested_requirement
2419 /* Transactional Memory Extensions */
2421 static tree cp_parser_transaction
2422 (cp_parser
*, cp_token
*);
2423 static tree cp_parser_transaction_expression
2424 (cp_parser
*, enum rid
);
2425 static bool cp_parser_function_transaction
2426 (cp_parser
*, enum rid
);
2427 static tree cp_parser_transaction_cancel
2430 enum pragma_context
{
2437 static bool cp_parser_pragma
2438 (cp_parser
*, enum pragma_context
, bool *);
2440 /* Objective-C++ Productions */
2442 static tree cp_parser_objc_message_receiver
2444 static tree cp_parser_objc_message_args
2446 static tree cp_parser_objc_message_expression
2448 static cp_expr cp_parser_objc_encode_expression
2450 static tree cp_parser_objc_defs_expression
2452 static tree cp_parser_objc_protocol_expression
2454 static tree cp_parser_objc_selector_expression
2456 static cp_expr cp_parser_objc_expression
2458 static bool cp_parser_objc_selector_p
2460 static tree cp_parser_objc_selector
2462 static tree cp_parser_objc_protocol_refs_opt
2464 static void cp_parser_objc_declaration
2465 (cp_parser
*, tree
);
2466 static tree cp_parser_objc_statement
2468 static bool cp_parser_objc_valid_prefix_attributes
2469 (cp_parser
*, tree
*);
2470 static void cp_parser_objc_at_property_declaration
2472 static void cp_parser_objc_at_synthesize_declaration
2474 static void cp_parser_objc_at_dynamic_declaration
2476 static tree cp_parser_objc_struct_declaration
2479 /* Utility Routines */
2481 static cp_expr cp_parser_lookup_name
2482 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2483 static tree cp_parser_lookup_name_simple
2484 (cp_parser
*, tree
, location_t
);
2485 static tree cp_parser_maybe_treat_template_as_class
2487 static bool cp_parser_check_declarator_template_parameters
2488 (cp_parser
*, cp_declarator
*, location_t
);
2489 static bool cp_parser_check_template_parameters
2490 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2491 static cp_expr cp_parser_simple_cast_expression
2493 static tree cp_parser_global_scope_opt
2494 (cp_parser
*, bool);
2495 static bool cp_parser_constructor_declarator_p
2496 (cp_parser
*, bool);
2497 static tree cp_parser_function_definition_from_specifiers_and_declarator
2498 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2499 static tree cp_parser_function_definition_after_declarator
2500 (cp_parser
*, bool);
2501 static bool cp_parser_template_declaration_after_export
2502 (cp_parser
*, bool);
2503 static void cp_parser_perform_template_parameter_access_checks
2504 (vec
<deferred_access_check
, va_gc
> *);
2505 static tree cp_parser_single_declaration
2506 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2507 static cp_expr cp_parser_functional_cast
2508 (cp_parser
*, tree
);
2509 static tree cp_parser_save_member_function_body
2510 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2511 static tree cp_parser_save_nsdmi
2513 static tree cp_parser_enclosed_template_argument_list
2515 static void cp_parser_save_default_args
2516 (cp_parser
*, tree
);
2517 static void cp_parser_late_parsing_for_member
2518 (cp_parser
*, tree
);
2519 static tree cp_parser_late_parse_one_default_arg
2520 (cp_parser
*, tree
, tree
, tree
);
2521 static void cp_parser_late_parsing_nsdmi
2522 (cp_parser
*, tree
);
2523 static void cp_parser_late_parsing_default_args
2524 (cp_parser
*, tree
);
2525 static tree cp_parser_sizeof_operand
2526 (cp_parser
*, enum rid
);
2527 static tree cp_parser_trait_expr
2528 (cp_parser
*, enum rid
);
2529 static bool cp_parser_declares_only_class_p
2531 static void cp_parser_set_storage_class
2532 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2533 static void cp_parser_set_decl_spec_type
2534 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2535 static void set_and_check_decl_spec_loc
2536 (cp_decl_specifier_seq
*decl_specs
,
2537 cp_decl_spec ds
, cp_token
*);
2538 static bool cp_parser_friend_p
2539 (const cp_decl_specifier_seq
*);
2540 static void cp_parser_required_error
2541 (cp_parser
*, required_token
, bool);
2542 static cp_token
*cp_parser_require
2543 (cp_parser
*, enum cpp_ttype
, required_token
);
2544 static cp_token
*cp_parser_require_keyword
2545 (cp_parser
*, enum rid
, required_token
);
2546 static bool cp_parser_token_starts_function_definition_p
2548 static bool cp_parser_next_token_starts_class_definition_p
2550 static bool cp_parser_next_token_ends_template_argument_p
2552 static bool cp_parser_nth_token_starts_template_argument_list_p
2553 (cp_parser
*, size_t);
2554 static enum tag_types cp_parser_token_is_class_key
2556 static enum tag_types cp_parser_token_is_type_parameter_key
2558 static void cp_parser_check_class_key
2559 (enum tag_types
, tree type
);
2560 static void cp_parser_check_access_in_redeclaration
2561 (tree type
, location_t location
);
2562 static bool cp_parser_optional_template_keyword
2564 static void cp_parser_pre_parsed_nested_name_specifier
2566 static bool cp_parser_cache_group
2567 (cp_parser
*, enum cpp_ttype
, unsigned);
2568 static tree cp_parser_cache_defarg
2569 (cp_parser
*parser
, bool nsdmi
);
2570 static void cp_parser_parse_tentatively
2572 static void cp_parser_commit_to_tentative_parse
2574 static void cp_parser_commit_to_topmost_tentative_parse
2576 static void cp_parser_abort_tentative_parse
2578 static bool cp_parser_parse_definitely
2580 static inline bool cp_parser_parsing_tentatively
2582 static bool cp_parser_uncommitted_to_tentative_parse_p
2584 static void cp_parser_error
2585 (cp_parser
*, const char *);
2586 static void cp_parser_name_lookup_error
2587 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2588 static bool cp_parser_simulate_error
2590 static bool cp_parser_check_type_definition
2592 static void cp_parser_check_for_definition_in_return_type
2593 (cp_declarator
*, tree
, location_t type_location
);
2594 static void cp_parser_check_for_invalid_template_id
2595 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2596 static bool cp_parser_non_integral_constant_expression
2597 (cp_parser
*, non_integral_constant
);
2598 static void cp_parser_diagnose_invalid_type_name
2599 (cp_parser
*, tree
, location_t
);
2600 static bool cp_parser_parse_and_diagnose_invalid_type_name
2602 static int cp_parser_skip_to_closing_parenthesis
2603 (cp_parser
*, bool, bool, bool);
2604 static void cp_parser_skip_to_end_of_statement
2606 static void cp_parser_consume_semicolon_at_end_of_statement
2608 static void cp_parser_skip_to_end_of_block_or_statement
2610 static bool cp_parser_skip_to_closing_brace
2612 static void cp_parser_skip_to_end_of_template_parameter_list
2614 static void cp_parser_skip_to_pragma_eol
2615 (cp_parser
*, cp_token
*);
2616 static bool cp_parser_error_occurred
2618 static bool cp_parser_allow_gnu_extensions_p
2620 static bool cp_parser_is_pure_string_literal
2622 static bool cp_parser_is_string_literal
2624 static bool cp_parser_is_keyword
2625 (cp_token
*, enum rid
);
2626 static tree cp_parser_make_typename_type
2627 (cp_parser
*, tree
, location_t location
);
2628 static cp_declarator
* cp_parser_make_indirect_declarator
2629 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2630 static bool cp_parser_compound_literal_p
2632 static bool cp_parser_array_designator_p
2634 static bool cp_parser_skip_to_closing_square_bracket
2637 /* Concept-related syntactic transformations */
2639 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2640 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2642 // -------------------------------------------------------------------------- //
2643 // Unevaluated Operand Guard
2645 // Implementation of an RAII helper for unevaluated operand parsing.
2646 cp_unevaluated::cp_unevaluated ()
2648 ++cp_unevaluated_operand
;
2649 ++c_inhibit_evaluation_warnings
;
2652 cp_unevaluated::~cp_unevaluated ()
2654 --c_inhibit_evaluation_warnings
;
2655 --cp_unevaluated_operand
;
2658 // -------------------------------------------------------------------------- //
2659 // Tentative Parsing
2661 /* Returns nonzero if we are parsing tentatively. */
2664 cp_parser_parsing_tentatively (cp_parser
* parser
)
2666 return parser
->context
->next
!= NULL
;
2669 /* Returns nonzero if TOKEN is a string literal. */
2672 cp_parser_is_pure_string_literal (cp_token
* token
)
2674 return (token
->type
== CPP_STRING
||
2675 token
->type
== CPP_STRING16
||
2676 token
->type
== CPP_STRING32
||
2677 token
->type
== CPP_WSTRING
||
2678 token
->type
== CPP_UTF8STRING
);
2681 /* Returns nonzero if TOKEN is a string literal
2682 of a user-defined string literal. */
2685 cp_parser_is_string_literal (cp_token
* token
)
2687 return (cp_parser_is_pure_string_literal (token
) ||
2688 token
->type
== CPP_STRING_USERDEF
||
2689 token
->type
== CPP_STRING16_USERDEF
||
2690 token
->type
== CPP_STRING32_USERDEF
||
2691 token
->type
== CPP_WSTRING_USERDEF
||
2692 token
->type
== CPP_UTF8STRING_USERDEF
);
2695 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2698 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2700 return token
->keyword
== keyword
;
2703 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2706 static enum pragma_kind
2707 cp_parser_pragma_kind (cp_token
*token
)
2709 if (token
->type
!= CPP_PRAGMA
)
2711 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2712 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2715 /* Helper function for cp_parser_error.
2716 Having peeked a token of kind TOK1_KIND that might signify
2717 a conflict marker, peek successor tokens to determine
2718 if we actually do have a conflict marker.
2719 Specifically, we consider a run of 7 '<', '=' or '>' characters
2720 at the start of a line as a conflict marker.
2721 These come through the lexer as three pairs and a single,
2722 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2723 If it returns true, *OUT_LOC is written to with the location/range
2727 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2728 location_t
*out_loc
)
2730 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2731 if (token2
->type
!= tok1_kind
)
2733 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2734 if (token3
->type
!= tok1_kind
)
2736 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2737 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2740 /* It must be at the start of the line. */
2741 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2742 if (LOCATION_COLUMN (start_loc
) != 1)
2745 /* We have a conflict marker. Construct a location of the form:
2748 with start == caret, finishing at the end of the marker. */
2749 location_t finish_loc
= get_finish (token4
->location
);
2750 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2755 /* If not parsing tentatively, issue a diagnostic of the form
2756 FILE:LINE: MESSAGE before TOKEN
2757 where TOKEN is the next token in the input stream. MESSAGE
2758 (specified by the caller) is usually of the form "expected
2762 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2764 if (!cp_parser_simulate_error (parser
))
2766 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2767 /* This diagnostic makes more sense if it is tagged to the line
2768 of the token we just peeked at. */
2769 cp_lexer_set_source_position_from_token (token
);
2771 if (token
->type
== CPP_PRAGMA
)
2773 error_at (token
->location
,
2774 "%<#pragma%> is not allowed here");
2775 cp_parser_skip_to_pragma_eol (parser
, token
);
2779 /* If this is actually a conflict marker, report it as such. */
2780 if (token
->type
== CPP_LSHIFT
2781 || token
->type
== CPP_RSHIFT
2782 || token
->type
== CPP_EQ_EQ
)
2785 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2787 error_at (loc
, "version control conflict marker in file");
2792 c_parse_error (gmsgid
,
2793 /* Because c_parser_error does not understand
2794 CPP_KEYWORD, keywords are treated like
2796 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2797 token
->u
.value
, token
->flags
);
2801 /* Issue an error about name-lookup failing. NAME is the
2802 IDENTIFIER_NODE DECL is the result of
2803 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2804 the thing that we hoped to find. */
2807 cp_parser_name_lookup_error (cp_parser
* parser
,
2810 name_lookup_error desired
,
2811 location_t location
)
2813 /* If name lookup completely failed, tell the user that NAME was not
2815 if (decl
== error_mark_node
)
2817 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2818 error_at (location
, "%<%E::%E%> has not been declared",
2819 parser
->scope
, name
);
2820 else if (parser
->scope
== global_namespace
)
2821 error_at (location
, "%<::%E%> has not been declared", name
);
2822 else if (parser
->object_scope
2823 && !CLASS_TYPE_P (parser
->object_scope
))
2824 error_at (location
, "request for member %qE in non-class type %qT",
2825 name
, parser
->object_scope
);
2826 else if (parser
->object_scope
)
2827 error_at (location
, "%<%T::%E%> has not been declared",
2828 parser
->object_scope
, name
);
2830 error_at (location
, "%qE has not been declared", name
);
2832 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2837 error_at (location
, "%<%E::%E%> is not a type",
2838 parser
->scope
, name
);
2841 error_at (location
, "%<%E::%E%> is not a class or namespace",
2842 parser
->scope
, name
);
2846 "%<%E::%E%> is not a class, namespace, or enumeration",
2847 parser
->scope
, name
);
2854 else if (parser
->scope
== global_namespace
)
2859 error_at (location
, "%<::%E%> is not a type", name
);
2862 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2866 "%<::%E%> is not a class, namespace, or enumeration",
2878 error_at (location
, "%qE is not a type", name
);
2881 error_at (location
, "%qE is not a class or namespace", name
);
2885 "%qE is not a class, namespace, or enumeration", name
);
2893 /* If we are parsing tentatively, remember that an error has occurred
2894 during this tentative parse. Returns true if the error was
2895 simulated; false if a message should be issued by the caller. */
2898 cp_parser_simulate_error (cp_parser
* parser
)
2900 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2902 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2908 /* This function is called when a type is defined. If type
2909 definitions are forbidden at this point, an error message is
2913 cp_parser_check_type_definition (cp_parser
* parser
)
2915 /* If types are forbidden here, issue a message. */
2916 if (parser
->type_definition_forbidden_message
)
2918 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2919 in the message need to be interpreted. */
2920 error (parser
->type_definition_forbidden_message
);
2926 /* This function is called when the DECLARATOR is processed. The TYPE
2927 was a type defined in the decl-specifiers. If it is invalid to
2928 define a type in the decl-specifiers for DECLARATOR, an error is
2929 issued. TYPE_LOCATION is the location of TYPE and is used
2930 for error reporting. */
2933 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2934 tree type
, location_t type_location
)
2936 /* [dcl.fct] forbids type definitions in return types.
2937 Unfortunately, it's not easy to know whether or not we are
2938 processing a return type until after the fact. */
2940 && (declarator
->kind
== cdk_pointer
2941 || declarator
->kind
== cdk_reference
2942 || declarator
->kind
== cdk_ptrmem
))
2943 declarator
= declarator
->declarator
;
2945 && declarator
->kind
== cdk_function
)
2947 error_at (type_location
,
2948 "new types may not be defined in a return type");
2949 inform (type_location
,
2950 "(perhaps a semicolon is missing after the definition of %qT)",
2955 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2956 "<" in any valid C++ program. If the next token is indeed "<",
2957 issue a message warning the user about what appears to be an
2958 invalid attempt to form a template-id. LOCATION is the location
2959 of the type-specifier (TYPE) */
2962 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2964 enum tag_types tag_type
,
2965 location_t location
)
2967 cp_token_position start
= 0;
2969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2972 error_at (location
, "%qT is not a template", type
);
2973 else if (identifier_p (type
))
2975 if (tag_type
!= none_type
)
2976 error_at (location
, "%qE is not a class template", type
);
2978 error_at (location
, "%qE is not a template", type
);
2981 error_at (location
, "invalid template-id");
2982 /* Remember the location of the invalid "<". */
2983 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2984 start
= cp_lexer_token_position (parser
->lexer
, true);
2985 /* Consume the "<". */
2986 cp_lexer_consume_token (parser
->lexer
);
2987 /* Parse the template arguments. */
2988 cp_parser_enclosed_template_argument_list (parser
);
2989 /* Permanently remove the invalid template arguments so that
2990 this error message is not issued again. */
2992 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2996 /* If parsing an integral constant-expression, issue an error message
2997 about the fact that THING appeared and return true. Otherwise,
2998 return false. In either case, set
2999 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3002 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3003 non_integral_constant thing
)
3005 parser
->non_integral_constant_expression_p
= true;
3006 if (parser
->integral_constant_expression_p
)
3008 if (!parser
->allow_non_integral_constant_expression_p
)
3010 const char *msg
= NULL
;
3014 error ("floating-point literal "
3015 "cannot appear in a constant-expression");
3018 error ("a cast to a type other than an integral or "
3019 "enumeration type cannot appear in a "
3020 "constant-expression");
3023 error ("%<typeid%> operator "
3024 "cannot appear in a constant-expression");
3027 error ("non-constant compound literals "
3028 "cannot appear in a constant-expression");
3031 error ("a function call "
3032 "cannot appear in a constant-expression");
3035 error ("an increment "
3036 "cannot appear in a constant-expression");
3039 error ("an decrement "
3040 "cannot appear in a constant-expression");
3043 error ("an array reference "
3044 "cannot appear in a constant-expression");
3046 case NIC_ADDR_LABEL
:
3047 error ("the address of a label "
3048 "cannot appear in a constant-expression");
3050 case NIC_OVERLOADED
:
3051 error ("calls to overloaded operators "
3052 "cannot appear in a constant-expression");
3054 case NIC_ASSIGNMENT
:
3055 error ("an assignment cannot appear in a constant-expression");
3058 error ("a comma operator "
3059 "cannot appear in a constant-expression");
3061 case NIC_CONSTRUCTOR
:
3062 error ("a call to a constructor "
3063 "cannot appear in a constant-expression");
3065 case NIC_TRANSACTION
:
3066 error ("a transaction expression "
3067 "cannot appear in a constant-expression");
3073 msg
= "__FUNCTION__";
3075 case NIC_PRETTY_FUNC
:
3076 msg
= "__PRETTY_FUNCTION__";
3096 case NIC_PREINCREMENT
:
3099 case NIC_PREDECREMENT
:
3112 error ("%qs cannot appear in a constant-expression", msg
);
3119 /* Emit a diagnostic for an invalid type name. This function commits
3120 to the current active tentative parse, if any. (Otherwise, the
3121 problematic construct might be encountered again later, resulting
3122 in duplicate error messages.) LOCATION is the location of ID. */
3125 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3126 location_t location
)
3128 tree decl
, ambiguous_decls
;
3129 cp_parser_commit_to_tentative_parse (parser
);
3130 /* Try to lookup the identifier. */
3131 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3132 /*is_template=*/false,
3133 /*is_namespace=*/false,
3134 /*check_dependency=*/true,
3135 &ambiguous_decls
, location
);
3136 if (ambiguous_decls
)
3137 /* If the lookup was ambiguous, an error will already have
3140 /* If the lookup found a template-name, it means that the user forgot
3141 to specify an argument list. Emit a useful error message. */
3142 if (DECL_TYPE_TEMPLATE_P (decl
))
3145 "invalid use of template-name %qE without an argument list",
3147 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3149 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3150 error_at (location
, "invalid use of destructor %qD as a type", id
);
3151 else if (TREE_CODE (decl
) == TYPE_DECL
)
3152 /* Something like 'unsigned A a;' */
3153 error_at (location
, "invalid combination of multiple type-specifiers");
3154 else if (!parser
->scope
)
3156 /* Issue an error message. */
3157 error_at (location
, "%qE does not name a type", id
);
3158 /* If we're in a template class, it's possible that the user was
3159 referring to a type from a base class. For example:
3161 template <typename T> struct A { typedef T X; };
3162 template <typename T> struct B : public A<T> { X x; };
3164 The user should have said "typename A<T>::X". */
3165 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3166 inform (location
, "C++11 %<constexpr%> only available with "
3167 "-std=c++11 or -std=gnu++11");
3168 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3169 inform (location
, "C++11 %<noexcept%> only available with "
3170 "-std=c++11 or -std=gnu++11");
3171 else if (cxx_dialect
< cxx11
3172 && TREE_CODE (id
) == IDENTIFIER_NODE
3173 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
3174 inform (location
, "C++11 %<thread_local%> only available with "
3175 "-std=c++11 or -std=gnu++11");
3176 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3177 inform (location
, "%<concept%> only available with -fconcepts");
3178 else if (processing_template_decl
&& current_class_type
3179 && TYPE_BINFO (current_class_type
))
3183 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3187 tree base_type
= BINFO_TYPE (b
);
3188 if (CLASS_TYPE_P (base_type
)
3189 && dependent_type_p (base_type
))
3192 /* Go from a particular instantiation of the
3193 template (which will have an empty TYPE_FIELDs),
3194 to the main version. */
3195 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3196 for (field
= TYPE_FIELDS (base_type
);
3198 field
= DECL_CHAIN (field
))
3199 if (TREE_CODE (field
) == TYPE_DECL
3200 && DECL_NAME (field
) == id
)
3203 "(perhaps %<typename %T::%E%> was intended)",
3204 BINFO_TYPE (b
), id
);
3213 /* Here we diagnose qualified-ids where the scope is actually correct,
3214 but the identifier does not resolve to a valid type name. */
3215 else if (parser
->scope
!= error_mark_node
)
3217 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3220 error_at (location_of (id
),
3221 "%qE in namespace %qE does not name a template type",
3224 error_at (location_of (id
),
3225 "%qE in namespace %qE does not name a type",
3228 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3230 else if (CLASS_TYPE_P (parser
->scope
)
3231 && constructor_name_p (id
, parser
->scope
))
3234 error_at (location
, "%<%T::%E%> names the constructor, not"
3235 " the type", parser
->scope
, id
);
3236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3237 error_at (location
, "and %qT has no template constructors",
3240 else if (TYPE_P (parser
->scope
)
3241 && dependent_scope_p (parser
->scope
))
3242 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3243 "%qT is a dependent scope",
3244 parser
->scope
, id
, parser
->scope
);
3245 else if (TYPE_P (parser
->scope
))
3247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3248 error_at (location_of (id
),
3249 "%qE in %q#T does not name a template type",
3252 error_at (location_of (id
),
3253 "%qE in %q#T does not name a type",
3256 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3263 /* Check for a common situation where a type-name should be present,
3264 but is not, and issue a sensible error message. Returns true if an
3265 invalid type-name was detected.
3267 The situation handled by this function are variable declarations of the
3268 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3269 Usually, `ID' should name a type, but if we got here it means that it
3270 does not. We try to emit the best possible error message depending on
3271 how exactly the id-expression looks like. */
3274 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3277 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3279 /* Avoid duplicate error about ambiguous lookup. */
3280 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3282 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3283 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3287 cp_parser_parse_tentatively (parser
);
3288 id
= cp_parser_id_expression (parser
,
3289 /*template_keyword_p=*/false,
3290 /*check_dependency_p=*/true,
3291 /*template_p=*/NULL
,
3292 /*declarator_p=*/true,
3293 /*optional_p=*/false);
3294 /* If the next token is a (, this is a function with no explicit return
3295 type, i.e. constructor, destructor or conversion op. */
3296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3297 || TREE_CODE (id
) == TYPE_DECL
)
3299 cp_parser_abort_tentative_parse (parser
);
3302 if (!cp_parser_parse_definitely (parser
))
3305 /* Emit a diagnostic for the invalid type. */
3306 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3308 /* If we aren't in the middle of a declarator (i.e. in a
3309 parameter-declaration-clause), skip to the end of the declaration;
3310 there's no point in trying to process it. */
3311 if (!parser
->in_declarator_p
)
3312 cp_parser_skip_to_end_of_block_or_statement (parser
);
3316 /* Consume tokens up to, and including, the next non-nested closing `)'.
3317 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3318 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3319 found an unnested token of that type. */
3322 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3327 unsigned paren_depth
= 0;
3328 unsigned brace_depth
= 0;
3329 unsigned square_depth
= 0;
3331 if (recovering
&& or_ttype
== CPP_EOF
3332 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3337 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3339 /* Have we found what we're looking for before the closing paren? */
3340 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3341 && !brace_depth
&& !paren_depth
&& !square_depth
)
3344 switch (token
->type
)
3347 case CPP_PRAGMA_EOL
:
3348 /* If we've run out of tokens, then there is no closing `)'. */
3351 /* This is good for lambda expression capture-lists. */
3352 case CPP_OPEN_SQUARE
:
3355 case CPP_CLOSE_SQUARE
:
3356 if (!square_depth
--)
3361 /* This matches the processing in skip_to_end_of_statement. */
3366 case CPP_OPEN_BRACE
:
3369 case CPP_CLOSE_BRACE
:
3374 case CPP_OPEN_PAREN
:
3379 case CPP_CLOSE_PAREN
:
3380 if (!brace_depth
&& !paren_depth
--)
3383 cp_lexer_consume_token (parser
->lexer
);
3392 /* Consume the token. */
3393 cp_lexer_consume_token (parser
->lexer
);
3397 /* Consume tokens up to, and including, the next non-nested closing `)'.
3398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3399 are doing error recovery. Returns -1 if OR_COMMA is true and we
3400 found an unnested token of that type. */
3403 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3408 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3409 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3410 ttype
, consume_paren
);
3413 /* Consume tokens until we reach the end of the current statement.
3414 Normally, that will be just before consuming a `;'. However, if a
3415 non-nested `}' comes first, then we stop before consuming that. */
3418 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3420 unsigned nesting_depth
= 0;
3422 /* Unwind generic function template scope if necessary. */
3423 if (parser
->fully_implicit_function_template_p
)
3424 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3430 switch (token
->type
)
3433 case CPP_PRAGMA_EOL
:
3434 /* If we've run out of tokens, stop. */
3438 /* If the next token is a `;', we have reached the end of the
3444 case CPP_CLOSE_BRACE
:
3445 /* If this is a non-nested '}', stop before consuming it.
3446 That way, when confronted with something like:
3450 we stop before consuming the closing '}', even though we
3451 have not yet reached a `;'. */
3452 if (nesting_depth
== 0)
3455 /* If it is the closing '}' for a block that we have
3456 scanned, stop -- but only after consuming the token.
3462 we will stop after the body of the erroneously declared
3463 function, but before consuming the following `typedef'
3465 if (--nesting_depth
== 0)
3467 cp_lexer_consume_token (parser
->lexer
);
3471 case CPP_OPEN_BRACE
:
3479 /* Consume the token. */
3480 cp_lexer_consume_token (parser
->lexer
);
3484 /* This function is called at the end of a statement or declaration.
3485 If the next token is a semicolon, it is consumed; otherwise, error
3486 recovery is attempted. */
3489 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3491 /* Look for the trailing `;'. */
3492 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3494 /* If there is additional (erroneous) input, skip to the end of
3496 cp_parser_skip_to_end_of_statement (parser
);
3497 /* If the next token is now a `;', consume it. */
3498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3499 cp_lexer_consume_token (parser
->lexer
);
3503 /* Skip tokens until we have consumed an entire block, or until we
3504 have consumed a non-nested `;'. */
3507 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3509 int nesting_depth
= 0;
3511 /* Unwind generic function template scope if necessary. */
3512 if (parser
->fully_implicit_function_template_p
)
3513 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3515 while (nesting_depth
>= 0)
3517 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3519 switch (token
->type
)
3522 case CPP_PRAGMA_EOL
:
3523 /* If we've run out of tokens, stop. */
3527 /* Stop if this is an unnested ';'. */
3532 case CPP_CLOSE_BRACE
:
3533 /* Stop if this is an unnested '}', or closes the outermost
3536 if (nesting_depth
< 0)
3542 case CPP_OPEN_BRACE
:
3551 /* Consume the token. */
3552 cp_lexer_consume_token (parser
->lexer
);
3556 /* Skip tokens until a non-nested closing curly brace is the next
3557 token, or there are no more tokens. Return true in the first case,
3561 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3563 unsigned nesting_depth
= 0;
3567 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3569 switch (token
->type
)
3572 case CPP_PRAGMA_EOL
:
3573 /* If we've run out of tokens, stop. */
3576 case CPP_CLOSE_BRACE
:
3577 /* If the next token is a non-nested `}', then we have reached
3578 the end of the current block. */
3579 if (nesting_depth
-- == 0)
3583 case CPP_OPEN_BRACE
:
3584 /* If it the next token is a `{', then we are entering a new
3585 block. Consume the entire block. */
3593 /* Consume the token. */
3594 cp_lexer_consume_token (parser
->lexer
);
3598 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3599 parameter is the PRAGMA token, allowing us to purge the entire pragma
3603 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3607 parser
->lexer
->in_pragma
= false;
3610 token
= cp_lexer_consume_token (parser
->lexer
);
3611 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3613 /* Ensure that the pragma is not parsed again. */
3614 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3617 /* Require pragma end of line, resyncing with it as necessary. The
3618 arguments are as for cp_parser_skip_to_pragma_eol. */
3621 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3623 parser
->lexer
->in_pragma
= false;
3624 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3625 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3628 /* This is a simple wrapper around make_typename_type. When the id is
3629 an unresolved identifier node, we can provide a superior diagnostic
3630 using cp_parser_diagnose_invalid_type_name. */
3633 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3634 location_t id_location
)
3637 if (identifier_p (id
))
3639 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3640 /*complain=*/tf_none
);
3641 if (result
== error_mark_node
)
3642 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3645 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3648 /* This is a wrapper around the
3649 make_{pointer,ptrmem,reference}_declarator functions that decides
3650 which one to call based on the CODE and CLASS_TYPE arguments. The
3651 CODE argument should be one of the values returned by
3652 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3653 appertain to the pointer or reference. */
3655 static cp_declarator
*
3656 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3657 cp_cv_quals cv_qualifiers
,
3658 cp_declarator
*target
,
3661 if (code
== ERROR_MARK
)
3662 return cp_error_declarator
;
3664 if (code
== INDIRECT_REF
)
3665 if (class_type
== NULL_TREE
)
3666 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3668 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3669 target
, attributes
);
3670 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3671 return make_reference_declarator (cv_qualifiers
, target
,
3673 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3674 return make_reference_declarator (cv_qualifiers
, target
,
3679 /* Create a new C++ parser. */
3682 cp_parser_new (void)
3688 /* cp_lexer_new_main is called before doing GC allocation because
3689 cp_lexer_new_main might load a PCH file. */
3690 lexer
= cp_lexer_new_main ();
3692 /* Initialize the binops_by_token so that we can get the tree
3693 directly from the token. */
3694 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3695 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3697 parser
= ggc_cleared_alloc
<cp_parser
> ();
3698 parser
->lexer
= lexer
;
3699 parser
->context
= cp_parser_context_new (NULL
);
3701 /* For now, we always accept GNU extensions. */
3702 parser
->allow_gnu_extensions_p
= 1;
3704 /* The `>' token is a greater-than operator, not the end of a
3706 parser
->greater_than_is_operator_p
= true;
3708 parser
->default_arg_ok_p
= true;
3710 /* We are not parsing a constant-expression. */
3711 parser
->integral_constant_expression_p
= false;
3712 parser
->allow_non_integral_constant_expression_p
= false;
3713 parser
->non_integral_constant_expression_p
= false;
3715 /* Local variable names are not forbidden. */
3716 parser
->local_variables_forbidden_p
= false;
3718 /* We are not processing an `extern "C"' declaration. */
3719 parser
->in_unbraced_linkage_specification_p
= false;
3721 /* We are not processing a declarator. */
3722 parser
->in_declarator_p
= false;
3724 /* We are not processing a template-argument-list. */
3725 parser
->in_template_argument_list_p
= false;
3727 /* We are not in an iteration statement. */
3728 parser
->in_statement
= 0;
3730 /* We are not in a switch statement. */
3731 parser
->in_switch_statement_p
= false;
3733 /* We are not parsing a type-id inside an expression. */
3734 parser
->in_type_id_in_expr_p
= false;
3736 /* Declarations aren't implicitly extern "C". */
3737 parser
->implicit_extern_c
= false;
3739 /* String literals should be translated to the execution character set. */
3740 parser
->translate_strings_p
= true;
3742 /* We are not parsing a function body. */
3743 parser
->in_function_body
= false;
3745 /* We can correct until told otherwise. */
3746 parser
->colon_corrects_to_scope_p
= true;
3748 /* The unparsed function queue is empty. */
3749 push_unparsed_function_queues (parser
);
3751 /* There are no classes being defined. */
3752 parser
->num_classes_being_defined
= 0;
3754 /* No template parameters apply. */
3755 parser
->num_template_parameter_lists
= 0;
3757 /* Not declaring an implicit function template. */
3758 parser
->auto_is_implicit_function_template_parm_p
= false;
3759 parser
->fully_implicit_function_template_p
= false;
3760 parser
->implicit_template_parms
= 0;
3761 parser
->implicit_template_scope
= 0;
3763 /* Active OpenACC routine clauses. */
3764 parser
->oacc_routine
= NULL
;
3766 /* Allow constrained-type-specifiers. */
3767 parser
->prevent_constrained_type_specifiers
= 0;
3772 /* Create a cp_lexer structure which will emit the tokens in CACHE
3773 and push it onto the parser's lexer stack. This is used for delayed
3774 parsing of in-class method bodies and default arguments, and should
3775 not be confused with tentative parsing. */
3777 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3779 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3780 lexer
->next
= parser
->lexer
;
3781 parser
->lexer
= lexer
;
3783 /* Move the current source position to that of the first token in the
3785 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3788 /* Pop the top lexer off the parser stack. This is never used for the
3789 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3791 cp_parser_pop_lexer (cp_parser
*parser
)
3793 cp_lexer
*lexer
= parser
->lexer
;
3794 parser
->lexer
= lexer
->next
;
3795 cp_lexer_destroy (lexer
);
3797 /* Put the current source position back where it was before this
3798 lexer was pushed. */
3799 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3802 /* Lexical conventions [gram.lex] */
3804 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3808 cp_parser_identifier (cp_parser
* parser
)
3812 /* Look for the identifier. */
3813 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3814 /* Return the value. */
3816 return cp_expr (token
->u
.value
, token
->location
);
3818 return error_mark_node
;
3821 /* Parse a sequence of adjacent string constants. Returns a
3822 TREE_STRING representing the combined, nul-terminated string
3823 constant. If TRANSLATE is true, translate the string to the
3824 execution character set. If WIDE_OK is true, a wide string is
3827 C++98 [lex.string] says that if a narrow string literal token is
3828 adjacent to a wide string literal token, the behavior is undefined.
3829 However, C99 6.4.5p4 says that this results in a wide string literal.
3830 We follow C99 here, for consistency with the C front end.
3832 This code is largely lifted from lex_string() in c-lex.c.
3834 FUTURE: ObjC++ will need to handle @-strings here. */
3836 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3837 bool lookup_udlit
= true)
3841 struct obstack str_ob
;
3842 cpp_string str
, istr
, *strs
;
3844 enum cpp_ttype type
, curr_type
;
3845 int have_suffix_p
= 0;
3847 tree suffix_id
= NULL_TREE
;
3848 bool curr_tok_is_userdef_p
= false;
3850 tok
= cp_lexer_peek_token (parser
->lexer
);
3851 if (!cp_parser_is_string_literal (tok
))
3853 cp_parser_error (parser
, "expected string-literal");
3854 return error_mark_node
;
3857 location_t loc
= tok
->location
;
3859 if (cpp_userdef_string_p (tok
->type
))
3861 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3862 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3863 curr_tok_is_userdef_p
= true;
3867 string_tree
= tok
->u
.value
;
3868 curr_type
= tok
->type
;
3872 /* Try to avoid the overhead of creating and destroying an obstack
3873 for the common case of just one string. */
3874 if (!cp_parser_is_string_literal
3875 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3877 cp_lexer_consume_token (parser
->lexer
);
3879 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3880 str
.len
= TREE_STRING_LENGTH (string_tree
);
3883 if (curr_tok_is_userdef_p
)
3885 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3887 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3890 curr_type
= tok
->type
;
3896 location_t last_tok_loc
;
3897 gcc_obstack_init (&str_ob
);
3902 last_tok_loc
= tok
->location
;
3903 cp_lexer_consume_token (parser
->lexer
);
3905 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3906 str
.len
= TREE_STRING_LENGTH (string_tree
);
3908 if (curr_tok_is_userdef_p
)
3910 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3911 if (have_suffix_p
== 0)
3913 suffix_id
= curr_suffix_id
;
3916 else if (have_suffix_p
== 1
3917 && curr_suffix_id
!= suffix_id
)
3919 error ("inconsistent user-defined literal suffixes"
3920 " %qD and %qD in string literal",
3921 suffix_id
, curr_suffix_id
);
3924 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3927 curr_type
= tok
->type
;
3929 if (type
!= curr_type
)
3931 if (type
== CPP_STRING
)
3933 else if (curr_type
!= CPP_STRING
)
3934 error_at (tok
->location
,
3935 "unsupported non-standard concatenation "
3936 "of string literals");
3939 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3941 tok
= cp_lexer_peek_token (parser
->lexer
);
3942 if (cpp_userdef_string_p (tok
->type
))
3944 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3945 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3946 curr_tok_is_userdef_p
= true;
3950 string_tree
= tok
->u
.value
;
3951 curr_type
= tok
->type
;
3952 curr_tok_is_userdef_p
= false;
3955 while (cp_parser_is_string_literal (tok
));
3957 /* A string literal built by concatenation has its caret=start at
3958 the start of the initial string, and its finish at the finish of
3959 the final string literal. */
3960 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
3962 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3965 if (type
!= CPP_STRING
&& !wide_ok
)
3967 cp_parser_error (parser
, "a wide string is invalid in this context");
3971 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3972 (parse_in
, strs
, count
, &istr
, type
))
3974 value
= build_string (istr
.len
, (const char *)istr
.text
);
3975 free (CONST_CAST (unsigned char *, istr
.text
));
3981 case CPP_UTF8STRING
:
3982 TREE_TYPE (value
) = char_array_type_node
;
3985 TREE_TYPE (value
) = char16_array_type_node
;
3988 TREE_TYPE (value
) = char32_array_type_node
;
3991 TREE_TYPE (value
) = wchar_array_type_node
;
3995 value
= fix_string_type (value
);
3999 tree literal
= build_userdef_literal (suffix_id
, value
,
4000 OT_NONE
, NULL_TREE
);
4002 value
= cp_parser_userdef_string_literal (literal
);
4008 /* cpp_interpret_string has issued an error. */
4009 value
= error_mark_node
;
4012 obstack_free (&str_ob
, 0);
4014 return cp_expr (value
, loc
);
4017 /* Look up a literal operator with the name and the exact arguments. */
4020 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4023 decl
= lookup_name (name
);
4024 if (!decl
|| !is_overloaded_fn (decl
))
4025 return error_mark_node
;
4027 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
4031 tree fn
= OVL_CURRENT (fns
);
4032 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4033 if (parmtypes
!= NULL_TREE
)
4035 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4036 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4038 tree tparm
= TREE_VALUE (parmtypes
);
4039 tree targ
= TREE_TYPE ((*args
)[ix
]);
4040 bool ptr
= TYPE_PTR_P (tparm
);
4041 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4042 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4044 || !same_type_p (TREE_TYPE (tparm
),
4049 && ix
== vec_safe_length (args
)
4050 /* May be this should be sufficient_parms_p instead,
4051 depending on how exactly should user-defined literals
4052 work in presence of default arguments on the literal
4053 operator parameters. */
4054 && parmtypes
== void_list_node
)
4059 return error_mark_node
;
4062 /* Parse a user-defined char constant. Returns a call to a user-defined
4063 literal operator taking the character as an argument. */
4066 cp_parser_userdef_char_literal (cp_parser
*parser
)
4068 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4069 tree literal
= token
->u
.value
;
4070 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4071 tree value
= USERDEF_LITERAL_VALUE (literal
);
4072 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4075 /* Build up a call to the user-defined operator */
4076 /* Lookup the name we got back from the id-expression. */
4077 vec
<tree
, va_gc
> *args
= make_tree_vector ();
4078 vec_safe_push (args
, value
);
4079 decl
= lookup_literal_operator (name
, args
);
4080 if (!decl
|| decl
== error_mark_node
)
4082 error ("unable to find character literal operator %qD with %qT argument",
4083 name
, TREE_TYPE (value
));
4084 release_tree_vector (args
);
4085 return error_mark_node
;
4087 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4088 release_tree_vector (args
);
4092 /* A subroutine of cp_parser_userdef_numeric_literal to
4093 create a char... template parameter pack from a string node. */
4096 make_char_string_pack (tree value
)
4099 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4100 const char *str
= TREE_STRING_POINTER (value
);
4101 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4102 tree argvec
= make_tree_vec (1);
4104 /* Fill in CHARVEC with all of the parameters. */
4105 charvec
= make_tree_vec (len
);
4106 for (i
= 0; i
< len
; ++i
)
4107 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
4109 /* Build the argument packs. */
4110 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4111 TREE_TYPE (argpack
) = char_type_node
;
4113 TREE_VEC_ELT (argvec
, 0) = argpack
;
4118 /* A subroutine of cp_parser_userdef_numeric_literal to
4119 create a char... template parameter pack from a string node. */
4122 make_string_pack (tree value
)
4125 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4126 const unsigned char *str
4127 = (const unsigned char *) TREE_STRING_POINTER (value
);
4128 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4129 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4130 tree argvec
= make_tree_vec (2);
4132 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4133 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4135 /* First template parm is character type. */
4136 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4138 /* Fill in CHARVEC with all of the parameters. */
4139 charvec
= make_tree_vec (len
);
4140 for (int i
= 0; i
< len
; ++i
)
4141 TREE_VEC_ELT (charvec
, i
)
4142 = double_int_to_tree (str_char_type_node
,
4143 double_int::from_buffer (str
+ i
* sz
, sz
));
4145 /* Build the argument packs. */
4146 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4147 TREE_TYPE (argpack
) = str_char_type_node
;
4149 TREE_VEC_ELT (argvec
, 1) = argpack
;
4154 /* Parse a user-defined numeric constant. returns a call to a user-defined
4155 literal operator. */
4158 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4160 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4161 tree literal
= token
->u
.value
;
4162 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4163 tree value
= USERDEF_LITERAL_VALUE (literal
);
4164 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4165 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4166 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4168 vec
<tree
, va_gc
> *args
;
4170 /* Look for a literal operator taking the exact type of numeric argument
4171 as the literal value. */
4172 args
= make_tree_vector ();
4173 vec_safe_push (args
, value
);
4174 decl
= lookup_literal_operator (name
, args
);
4175 if (decl
&& decl
!= error_mark_node
)
4177 result
= finish_call_expr (decl
, &args
, false, true,
4178 tf_warning_or_error
);
4180 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4182 warning_at (token
->location
, OPT_Woverflow
,
4183 "integer literal exceeds range of %qT type",
4184 long_long_unsigned_type_node
);
4189 warning_at (token
->location
, OPT_Woverflow
,
4190 "floating literal exceeds range of %qT type",
4191 long_double_type_node
);
4192 else if (overflow
< 0)
4193 warning_at (token
->location
, OPT_Woverflow
,
4194 "floating literal truncated to zero");
4197 release_tree_vector (args
);
4200 release_tree_vector (args
);
4202 /* If the numeric argument didn't work, look for a raw literal
4203 operator taking a const char* argument consisting of the number
4204 in string format. */
4205 args
= make_tree_vector ();
4206 vec_safe_push (args
, num_string
);
4207 decl
= lookup_literal_operator (name
, args
);
4208 if (decl
&& decl
!= error_mark_node
)
4210 result
= finish_call_expr (decl
, &args
, false, true,
4211 tf_warning_or_error
);
4212 release_tree_vector (args
);
4215 release_tree_vector (args
);
4217 /* If the raw literal didn't work, look for a non-type template
4218 function with parameter pack char.... Call the function with
4219 template parameter characters representing the number. */
4220 args
= make_tree_vector ();
4221 decl
= lookup_literal_operator (name
, args
);
4222 if (decl
&& decl
!= error_mark_node
)
4224 tree tmpl_args
= make_char_string_pack (num_string
);
4225 decl
= lookup_template_function (decl
, tmpl_args
);
4226 result
= finish_call_expr (decl
, &args
, false, true,
4227 tf_warning_or_error
);
4228 release_tree_vector (args
);
4232 release_tree_vector (args
);
4234 error ("unable to find numeric literal operator %qD", name
);
4235 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4236 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4237 "to enable more built-in suffixes");
4238 return error_mark_node
;
4241 /* Parse a user-defined string constant. Returns a call to a user-defined
4242 literal operator taking a character pointer and the length of the string
4246 cp_parser_userdef_string_literal (tree literal
)
4248 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4249 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4250 tree value
= USERDEF_LITERAL_VALUE (literal
);
4251 int len
= TREE_STRING_LENGTH (value
)
4252 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4254 vec
<tree
, va_gc
> *args
;
4256 /* Build up a call to the user-defined operator. */
4257 /* Lookup the name we got back from the id-expression. */
4258 args
= make_tree_vector ();
4259 vec_safe_push (args
, value
);
4260 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4261 decl
= lookup_literal_operator (name
, args
);
4263 if (decl
&& decl
!= error_mark_node
)
4265 result
= finish_call_expr (decl
, &args
, false, true,
4266 tf_warning_or_error
);
4267 release_tree_vector (args
);
4270 release_tree_vector (args
);
4272 /* Look for a template function with typename parameter CharT
4273 and parameter pack CharT... Call the function with
4274 template parameter characters representing the string. */
4275 args
= make_tree_vector ();
4276 decl
= lookup_literal_operator (name
, args
);
4277 if (decl
&& decl
!= error_mark_node
)
4279 tree tmpl_args
= make_string_pack (value
);
4280 decl
= lookup_template_function (decl
, tmpl_args
);
4281 result
= finish_call_expr (decl
, &args
, false, true,
4282 tf_warning_or_error
);
4283 release_tree_vector (args
);
4286 release_tree_vector (args
);
4288 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4289 name
, TREE_TYPE (value
), size_type_node
);
4290 return error_mark_node
;
4294 /* Basic concepts [gram.basic] */
4296 /* Parse a translation-unit.
4299 declaration-seq [opt]
4301 Returns TRUE if all went well. */
4304 cp_parser_translation_unit (cp_parser
* parser
)
4306 /* The address of the first non-permanent object on the declarator
4308 static void *declarator_obstack_base
;
4312 /* Create the declarator obstack, if necessary. */
4313 if (!cp_error_declarator
)
4315 gcc_obstack_init (&declarator_obstack
);
4316 /* Create the error declarator. */
4317 cp_error_declarator
= make_declarator (cdk_error
);
4318 /* Create the empty parameter list. */
4319 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4320 /* Remember where the base of the declarator obstack lies. */
4321 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4324 cp_parser_declaration_seq_opt (parser
);
4326 /* If there are no tokens left then all went well. */
4327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4329 /* Get rid of the token array; we don't need it any more. */
4330 cp_lexer_destroy (parser
->lexer
);
4331 parser
->lexer
= NULL
;
4333 /* This file might have been a context that's implicitly extern
4334 "C". If so, pop the lang context. (Only relevant for PCH.) */
4335 if (parser
->implicit_extern_c
)
4337 pop_lang_context ();
4338 parser
->implicit_extern_c
= false;
4342 finish_translation_unit ();
4348 cp_parser_error (parser
, "expected declaration");
4352 /* Make sure the declarator obstack was fully cleaned up. */
4353 gcc_assert (obstack_next_free (&declarator_obstack
)
4354 == declarator_obstack_base
);
4356 /* All went well. */
4360 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4361 decltype context. */
4363 static inline tsubst_flags_t
4364 complain_flags (bool decltype_p
)
4366 tsubst_flags_t complain
= tf_warning_or_error
;
4368 complain
|= tf_decltype
;
4372 /* We're about to parse a collection of statements. If we're currently
4373 parsing tentatively, set up a firewall so that any nested
4374 cp_parser_commit_to_tentative_parse won't affect the current context. */
4376 static cp_token_position
4377 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4379 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4382 cp_parser_parse_tentatively (parser
);
4383 cp_parser_commit_to_topmost_tentative_parse (parser
);
4384 return cp_lexer_token_position (parser
->lexer
, false);
4387 /* We've finished parsing the collection of statements. Wrap up the
4388 firewall and replace the relevant tokens with the parsed form. */
4391 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4397 /* Finish the firewall level. */
4398 cp_parser_parse_definitely (parser
);
4399 /* And remember the result of the parse for when we try again. */
4400 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4401 token
->type
= CPP_PREPARSED_EXPR
;
4402 token
->u
.value
= expr
;
4403 token
->keyword
= RID_MAX
;
4404 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4407 /* Like the above functions, but let the user modify the tokens. Used by
4408 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4409 later parses, so it makes sense to localize the effects of
4410 cp_parser_commit_to_tentative_parse. */
4412 struct tentative_firewall
4417 tentative_firewall (cp_parser
*p
): parser(p
)
4419 /* If we're currently parsing tentatively, start a committed level as a
4420 firewall and then an inner tentative parse. */
4421 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4423 cp_parser_parse_tentatively (parser
);
4424 cp_parser_commit_to_topmost_tentative_parse (parser
);
4425 cp_parser_parse_tentatively (parser
);
4429 ~tentative_firewall()
4433 /* Finish the inner tentative parse and the firewall, propagating any
4434 uncommitted error state to the outer tentative parse. */
4435 bool err
= cp_parser_error_occurred (parser
);
4436 cp_parser_parse_definitely (parser
);
4437 cp_parser_parse_definitely (parser
);
4439 cp_parser_simulate_error (parser
);
4444 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4445 enclosing parentheses. */
4448 cp_parser_statement_expr (cp_parser
*parser
)
4450 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4452 /* Consume the '('. */
4453 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4454 cp_lexer_consume_token (parser
->lexer
);
4455 /* Start the statement-expression. */
4456 tree expr
= begin_stmt_expr ();
4457 /* Parse the compound-statement. */
4458 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4460 expr
= finish_stmt_expr (expr
, false);
4461 /* Consume the ')'. */
4462 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4463 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4464 cp_parser_skip_to_end_of_statement (parser
);
4466 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4467 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4468 return cp_expr (expr
, combined_loc
);
4471 /* Expressions [gram.expr] */
4473 /* Parse a fold-operator.
4476 - * / % ^ & | = < > << >>
4477 = -= *= /= %= ^= &= |= <<= >>=
4478 == != <= >= && || , .* ->*
4480 This returns the tree code corresponding to the matched operator
4481 as an int. When the current token matches a compound assignment
4482 opertor, the resulting tree code is the negative value of the
4483 non-assignment operator. */
4486 cp_parser_fold_operator (cp_token
*token
)
4488 switch (token
->type
)
4490 case CPP_PLUS
: return PLUS_EXPR
;
4491 case CPP_MINUS
: return MINUS_EXPR
;
4492 case CPP_MULT
: return MULT_EXPR
;
4493 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4494 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4495 case CPP_XOR
: return BIT_XOR_EXPR
;
4496 case CPP_AND
: return BIT_AND_EXPR
;
4497 case CPP_OR
: return BIT_IOR_EXPR
;
4498 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4499 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4501 case CPP_EQ
: return -NOP_EXPR
;
4502 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4503 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4504 case CPP_MULT_EQ
: return -MULT_EXPR
;
4505 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4506 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4507 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4508 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4509 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4510 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4511 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4513 case CPP_EQ_EQ
: return EQ_EXPR
;
4514 case CPP_NOT_EQ
: return NE_EXPR
;
4515 case CPP_LESS
: return LT_EXPR
;
4516 case CPP_GREATER
: return GT_EXPR
;
4517 case CPP_LESS_EQ
: return LE_EXPR
;
4518 case CPP_GREATER_EQ
: return GE_EXPR
;
4520 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4521 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4523 case CPP_COMMA
: return COMPOUND_EXPR
;
4525 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4526 case CPP_DEREF_STAR
: return MEMBER_REF
;
4528 default: return ERROR_MARK
;
4532 /* Returns true if CODE indicates a binary expression, which is not allowed in
4533 the LHS of a fold-expression. More codes will need to be added to use this
4534 function in other contexts. */
4537 is_binary_op (tree_code code
)
4542 case POINTER_PLUS_EXPR
:
4545 case TRUNC_DIV_EXPR
:
4546 case TRUNC_MOD_EXPR
:
4562 case TRUTH_ANDIF_EXPR
:
4563 case TRUTH_ORIF_EXPR
:
4576 /* If the next token is a suitable fold operator, consume it and return as
4577 the function above. */
4580 cp_parser_fold_operator (cp_parser
*parser
)
4582 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4583 int code
= cp_parser_fold_operator (token
);
4584 if (code
!= ERROR_MARK
)
4585 cp_lexer_consume_token (parser
->lexer
);
4589 /* Parse a fold-expression.
4592 ( ... folding-operator cast-expression)
4593 ( cast-expression folding-operator ... )
4594 ( cast-expression folding operator ... folding-operator cast-expression)
4596 Note that the '(' and ')' are matched in primary expression. */
4599 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4606 cp_lexer_consume_token (parser
->lexer
);
4607 int op
= cp_parser_fold_operator (parser
);
4608 if (op
== ERROR_MARK
)
4610 cp_parser_error (parser
, "expected binary operator");
4611 return error_mark_node
;
4614 tree expr
= cp_parser_cast_expression (parser
, false, false,
4616 if (expr
== error_mark_node
)
4617 return error_mark_node
;
4618 return finish_left_unary_fold_expr (expr
, op
);
4621 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4622 int op
= cp_parser_fold_operator (parser
);
4623 if (op
== ERROR_MARK
)
4625 cp_parser_error (parser
, "expected binary operator");
4626 return error_mark_node
;
4629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4631 cp_parser_error (parser
, "expected ...");
4632 return error_mark_node
;
4634 cp_lexer_consume_token (parser
->lexer
);
4636 /* The operands of a fold-expression are cast-expressions, so binary or
4637 conditional expressions are not allowed. We check this here to avoid
4638 tentative parsing. */
4639 if (is_binary_op (TREE_CODE (expr1
)))
4640 error_at (location_of (expr1
),
4641 "binary expression in operand of fold-expression");
4642 else if (TREE_CODE (expr1
) == COND_EXPR
)
4643 error_at (location_of (expr1
),
4644 "conditional expression in operand of fold-expression");
4647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4648 return finish_right_unary_fold_expr (expr1
, op
);
4650 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4652 cp_parser_error (parser
, "mismatched operator in fold-expression");
4653 return error_mark_node
;
4655 cp_lexer_consume_token (parser
->lexer
);
4657 // Binary left or right fold.
4658 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4659 if (expr2
== error_mark_node
)
4660 return error_mark_node
;
4661 return finish_binary_fold_expr (expr1
, expr2
, op
);
4664 /* Parse a primary-expression.
4671 lambda-expression (C++11)
4676 ( compound-statement )
4677 __builtin_va_arg ( assignment-expression , type-id )
4678 __builtin_offsetof ( type-id , offsetof-expression )
4681 __has_nothrow_assign ( type-id )
4682 __has_nothrow_constructor ( type-id )
4683 __has_nothrow_copy ( type-id )
4684 __has_trivial_assign ( type-id )
4685 __has_trivial_constructor ( type-id )
4686 __has_trivial_copy ( type-id )
4687 __has_trivial_destructor ( type-id )
4688 __has_virtual_destructor ( type-id )
4689 __is_abstract ( type-id )
4690 __is_base_of ( type-id , type-id )
4691 __is_class ( type-id )
4692 __is_empty ( type-id )
4693 __is_enum ( type-id )
4694 __is_final ( type-id )
4695 __is_literal_type ( type-id )
4696 __is_pod ( type-id )
4697 __is_polymorphic ( type-id )
4698 __is_std_layout ( type-id )
4699 __is_trivial ( type-id )
4700 __is_union ( type-id )
4702 Objective-C++ Extension:
4710 ADDRESS_P is true iff this expression was immediately preceded by
4711 "&" and therefore might denote a pointer-to-member. CAST_P is true
4712 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4713 true iff this expression is a template argument.
4715 Returns a representation of the expression. Upon return, *IDK
4716 indicates what kind of id-expression (if any) was present. */
4719 cp_parser_primary_expression (cp_parser
*parser
,
4722 bool template_arg_p
,
4726 cp_token
*token
= NULL
;
4728 /* Assume the primary expression is not an id-expression. */
4729 *idk
= CP_ID_KIND_NONE
;
4731 /* Peek at the next token. */
4732 token
= cp_lexer_peek_token (parser
->lexer
);
4733 switch ((int) token
->type
)
4742 user-defined-literal */
4749 case CPP_PREPARSED_EXPR
:
4750 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4751 return cp_parser_userdef_numeric_literal (parser
);
4752 token
= cp_lexer_consume_token (parser
->lexer
);
4753 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4755 error_at (token
->location
,
4756 "fixed-point types not supported in C++");
4757 return error_mark_node
;
4759 /* Floating-point literals are only allowed in an integral
4760 constant expression if they are cast to an integral or
4761 enumeration type. */
4762 if (TREE_CODE (token
->u
.value
) == REAL_CST
4763 && parser
->integral_constant_expression_p
4766 /* CAST_P will be set even in invalid code like "int(2.7 +
4767 ...)". Therefore, we have to check that the next token
4768 is sure to end the cast. */
4771 cp_token
*next_token
;
4773 next_token
= cp_lexer_peek_token (parser
->lexer
);
4774 if (/* The comma at the end of an
4775 enumerator-definition. */
4776 next_token
->type
!= CPP_COMMA
4777 /* The curly brace at the end of an enum-specifier. */
4778 && next_token
->type
!= CPP_CLOSE_BRACE
4779 /* The end of a statement. */
4780 && next_token
->type
!= CPP_SEMICOLON
4781 /* The end of the cast-expression. */
4782 && next_token
->type
!= CPP_CLOSE_PAREN
4783 /* The end of an array bound. */
4784 && next_token
->type
!= CPP_CLOSE_SQUARE
4785 /* The closing ">" in a template-argument-list. */
4786 && (next_token
->type
!= CPP_GREATER
4787 || parser
->greater_than_is_operator_p
)
4788 /* C++0x only: A ">>" treated like two ">" tokens,
4789 in a template-argument-list. */
4790 && (next_token
->type
!= CPP_RSHIFT
4791 || (cxx_dialect
== cxx98
)
4792 || parser
->greater_than_is_operator_p
))
4796 /* If we are within a cast, then the constraint that the
4797 cast is to an integral or enumeration type will be
4798 checked at that point. If we are not within a cast, then
4799 this code is invalid. */
4801 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4803 return cp_expr (token
->u
.value
, token
->location
);
4805 case CPP_CHAR_USERDEF
:
4806 case CPP_CHAR16_USERDEF
:
4807 case CPP_CHAR32_USERDEF
:
4808 case CPP_WCHAR_USERDEF
:
4809 case CPP_UTF8CHAR_USERDEF
:
4810 return cp_parser_userdef_char_literal (parser
);
4816 case CPP_UTF8STRING
:
4817 case CPP_STRING_USERDEF
:
4818 case CPP_STRING16_USERDEF
:
4819 case CPP_STRING32_USERDEF
:
4820 case CPP_WSTRING_USERDEF
:
4821 case CPP_UTF8STRING_USERDEF
:
4822 /* ??? Should wide strings be allowed when parser->translate_strings_p
4823 is false (i.e. in attributes)? If not, we can kill the third
4824 argument to cp_parser_string_literal. */
4825 return cp_parser_string_literal (parser
,
4826 parser
->translate_strings_p
,
4829 case CPP_OPEN_PAREN
:
4830 /* If we see `( { ' then we are looking at the beginning of
4831 a GNU statement-expression. */
4832 if (cp_parser_allow_gnu_extensions_p (parser
)
4833 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4835 /* Statement-expressions are not allowed by the standard. */
4836 pedwarn (token
->location
, OPT_Wpedantic
,
4837 "ISO C++ forbids braced-groups within expressions");
4839 /* And they're not allowed outside of a function-body; you
4840 cannot, for example, write:
4842 int i = ({ int j = 3; j + 1; });
4844 at class or namespace scope. */
4845 if (!parser
->in_function_body
4846 || parser
->in_template_argument_list_p
)
4848 error_at (token
->location
,
4849 "statement-expressions are not allowed outside "
4850 "functions nor in template-argument lists");
4851 cp_parser_skip_to_end_of_block_or_statement (parser
);
4852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4853 cp_lexer_consume_token (parser
->lexer
);
4854 return error_mark_node
;
4857 return cp_parser_statement_expr (parser
);
4859 /* Otherwise it's a normal parenthesized expression. */
4862 bool saved_greater_than_is_operator_p
;
4864 location_t open_paren_loc
= token
->location
;
4866 /* Consume the `('. */
4867 cp_lexer_consume_token (parser
->lexer
);
4868 /* Within a parenthesized expression, a `>' token is always
4869 the greater-than operator. */
4870 saved_greater_than_is_operator_p
4871 = parser
->greater_than_is_operator_p
;
4872 parser
->greater_than_is_operator_p
= true;
4874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4875 /* Left fold expression. */
4878 /* Parse the parenthesized expression. */
4879 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4881 token
= cp_lexer_peek_token (parser
->lexer
);
4882 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
4884 expr
= cp_parser_fold_expression (parser
, expr
);
4885 if (expr
!= error_mark_node
4886 && cxx_dialect
< cxx1z
4887 && !in_system_header_at (input_location
))
4888 pedwarn (input_location
, 0, "fold-expressions only available "
4889 "with -std=c++1z or -std=gnu++1z");
4892 /* Let the front end know that this expression was
4893 enclosed in parentheses. This matters in case, for
4894 example, the expression is of the form `A::B', since
4895 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4897 expr
= finish_parenthesized_expr (expr
);
4899 /* DR 705: Wrapping an unqualified name in parentheses
4900 suppresses arg-dependent lookup. We want to pass back
4901 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4902 (c++/37862), but none of the others. */
4903 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4904 *idk
= CP_ID_KIND_NONE
;
4906 /* The `>' token might be the end of a template-id or
4907 template-parameter-list now. */
4908 parser
->greater_than_is_operator_p
4909 = saved_greater_than_is_operator_p
;
4911 /* Consume the `)'. */
4912 token
= cp_lexer_peek_token (parser
->lexer
);
4913 location_t close_paren_loc
= token
->location
;
4914 expr
.set_range (open_paren_loc
, close_paren_loc
);
4915 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
)
4916 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4917 cp_parser_skip_to_end_of_statement (parser
);
4922 case CPP_OPEN_SQUARE
:
4924 if (c_dialect_objc ())
4926 /* We might have an Objective-C++ message. */
4927 cp_parser_parse_tentatively (parser
);
4928 tree msg
= cp_parser_objc_message_expression (parser
);
4929 /* If that works out, we're done ... */
4930 if (cp_parser_parse_definitely (parser
))
4932 /* ... else, fall though to see if it's a lambda. */
4934 cp_expr lam
= cp_parser_lambda_expression (parser
);
4935 /* Don't warn about a failed tentative parse. */
4936 if (cp_parser_error_occurred (parser
))
4937 return error_mark_node
;
4938 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4942 case CPP_OBJC_STRING
:
4943 if (c_dialect_objc ())
4944 /* We have an Objective-C++ string literal. */
4945 return cp_parser_objc_expression (parser
);
4946 cp_parser_error (parser
, "expected primary-expression");
4947 return error_mark_node
;
4950 switch (token
->keyword
)
4952 /* These two are the boolean literals. */
4954 cp_lexer_consume_token (parser
->lexer
);
4955 return cp_expr (boolean_true_node
, token
->location
);
4957 cp_lexer_consume_token (parser
->lexer
);
4958 return cp_expr (boolean_false_node
, token
->location
);
4960 /* The `__null' literal. */
4962 cp_lexer_consume_token (parser
->lexer
);
4963 return cp_expr (null_node
, token
->location
);
4965 /* The `nullptr' literal. */
4967 cp_lexer_consume_token (parser
->lexer
);
4968 return cp_expr (nullptr_node
, token
->location
);
4970 /* Recognize the `this' keyword. */
4972 cp_lexer_consume_token (parser
->lexer
);
4973 if (parser
->local_variables_forbidden_p
)
4975 error_at (token
->location
,
4976 "%<this%> may not be used in this context");
4977 return error_mark_node
;
4979 /* Pointers cannot appear in constant-expressions. */
4980 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4981 return error_mark_node
;
4982 return cp_expr (finish_this_expr (), token
->location
);
4984 /* The `operator' keyword can be the beginning of an
4989 case RID_FUNCTION_NAME
:
4990 case RID_PRETTY_FUNCTION_NAME
:
4991 case RID_C99_FUNCTION_NAME
:
4993 non_integral_constant name
;
4995 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4996 __func__ are the names of variables -- but they are
4997 treated specially. Therefore, they are handled here,
4998 rather than relying on the generic id-expression logic
4999 below. Grammatically, these names are id-expressions.
5001 Consume the token. */
5002 token
= cp_lexer_consume_token (parser
->lexer
);
5004 switch (token
->keyword
)
5006 case RID_FUNCTION_NAME
:
5007 name
= NIC_FUNC_NAME
;
5009 case RID_PRETTY_FUNCTION_NAME
:
5010 name
= NIC_PRETTY_FUNC
;
5012 case RID_C99_FUNCTION_NAME
:
5013 name
= NIC_C99_FUNC
;
5019 if (cp_parser_non_integral_constant_expression (parser
, name
))
5020 return error_mark_node
;
5022 /* Look up the name. */
5023 return finish_fname (token
->u
.value
);
5030 source_location type_location
;
5031 location_t start_loc
5032 = cp_lexer_peek_token (parser
->lexer
)->location
;
5033 /* The `__builtin_va_arg' construct is used to handle
5034 `va_arg'. Consume the `__builtin_va_arg' token. */
5035 cp_lexer_consume_token (parser
->lexer
);
5036 /* Look for the opening `('. */
5037 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5038 /* Now, parse the assignment-expression. */
5039 expression
= cp_parser_assignment_expression (parser
);
5040 /* Look for the `,'. */
5041 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5042 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5043 /* Parse the type-id. */
5045 type_id_in_expr_sentinel
s (parser
);
5046 type
= cp_parser_type_id (parser
);
5048 /* Look for the closing `)'. */
5049 location_t finish_loc
5050 = cp_lexer_peek_token (parser
->lexer
)->location
;
5051 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5052 /* Using `va_arg' in a constant-expression is not
5054 if (cp_parser_non_integral_constant_expression (parser
,
5056 return error_mark_node
;
5057 /* Construct a location of the form:
5058 __builtin_va_arg (v, int)
5059 ~~~~~~~~~~~~~~~~~~~~~^~~~
5060 with the caret at the type, ranging from the start of the
5061 "__builtin_va_arg" token to the close paren. */
5062 location_t combined_loc
5063 = make_location (type_location
, start_loc
, finish_loc
);
5064 return build_x_va_arg (combined_loc
, expression
, type
);
5068 return cp_parser_builtin_offsetof (parser
);
5070 case RID_HAS_NOTHROW_ASSIGN
:
5071 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5072 case RID_HAS_NOTHROW_COPY
:
5073 case RID_HAS_TRIVIAL_ASSIGN
:
5074 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5075 case RID_HAS_TRIVIAL_COPY
:
5076 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5077 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5078 case RID_IS_ABSTRACT
:
5079 case RID_IS_BASE_OF
:
5084 case RID_IS_LITERAL_TYPE
:
5086 case RID_IS_POLYMORPHIC
:
5087 case RID_IS_SAME_AS
:
5088 case RID_IS_STD_LAYOUT
:
5089 case RID_IS_TRIVIAL
:
5090 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5091 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5092 case RID_IS_TRIVIALLY_COPYABLE
:
5094 return cp_parser_trait_expr (parser
, token
->keyword
);
5098 return cp_parser_requires_expression (parser
);
5100 /* Objective-C++ expressions. */
5102 case RID_AT_PROTOCOL
:
5103 case RID_AT_SELECTOR
:
5104 return cp_parser_objc_expression (parser
);
5107 if (parser
->in_function_body
5108 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5111 error_at (token
->location
,
5112 "a template declaration cannot appear at block scope");
5113 cp_parser_skip_to_end_of_block_or_statement (parser
);
5114 return error_mark_node
;
5117 cp_parser_error (parser
, "expected primary-expression");
5118 return error_mark_node
;
5121 /* An id-expression can start with either an identifier, a
5122 `::' as the beginning of a qualified-id, or the "operator"
5126 case CPP_TEMPLATE_ID
:
5127 case CPP_NESTED_NAME_SPECIFIER
:
5130 cp_expr id_expression
;
5132 const char *error_msg
;
5135 cp_token
*id_expr_token
;
5137 /* Parse the id-expression. */
5139 = cp_parser_id_expression (parser
,
5140 /*template_keyword_p=*/false,
5141 /*check_dependency_p=*/true,
5143 /*declarator_p=*/false,
5144 /*optional_p=*/false);
5145 if (id_expression
== error_mark_node
)
5146 return error_mark_node
;
5147 id_expr_token
= token
;
5148 token
= cp_lexer_peek_token (parser
->lexer
);
5149 done
= (token
->type
!= CPP_OPEN_SQUARE
5150 && token
->type
!= CPP_OPEN_PAREN
5151 && token
->type
!= CPP_DOT
5152 && token
->type
!= CPP_DEREF
5153 && token
->type
!= CPP_PLUS_PLUS
5154 && token
->type
!= CPP_MINUS_MINUS
);
5155 /* If we have a template-id, then no further lookup is
5156 required. If the template-id was for a template-class, we
5157 will sometimes have a TYPE_DECL at this point. */
5158 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5159 || TREE_CODE (id_expression
) == TYPE_DECL
)
5160 decl
= id_expression
;
5161 /* Look up the name. */
5164 tree ambiguous_decls
;
5166 /* If we already know that this lookup is ambiguous, then
5167 we've already issued an error message; there's no reason
5169 if (id_expr_token
->type
== CPP_NAME
5170 && id_expr_token
->error_reported
)
5172 cp_parser_simulate_error (parser
);
5173 return error_mark_node
;
5176 decl
= cp_parser_lookup_name (parser
, id_expression
,
5179 /*is_namespace=*/false,
5180 /*check_dependency=*/true,
5182 id_expr_token
->location
);
5183 /* If the lookup was ambiguous, an error will already have
5185 if (ambiguous_decls
)
5186 return error_mark_node
;
5188 /* In Objective-C++, we may have an Objective-C 2.0
5189 dot-syntax for classes here. */
5190 if (c_dialect_objc ()
5191 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5192 && TREE_CODE (decl
) == TYPE_DECL
5193 && objc_is_class_name (decl
))
5196 cp_lexer_consume_token (parser
->lexer
);
5197 component
= cp_parser_identifier (parser
);
5198 if (component
== error_mark_node
)
5199 return error_mark_node
;
5201 tree result
= objc_build_class_component_ref (id_expression
,
5203 /* Build a location of the form:
5206 with caret at the start of the component name (at
5207 input_location), ranging from the start of the id_expression
5208 to the end of the component name. */
5209 location_t combined_loc
5210 = make_location (input_location
, id_expression
.get_start (),
5211 get_finish (input_location
));
5212 protected_set_expr_location (result
, combined_loc
);
5216 /* In Objective-C++, an instance variable (ivar) may be preferred
5217 to whatever cp_parser_lookup_name() found.
5218 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5219 rest of c-family, we have to do a little extra work to preserve
5220 any location information in cp_expr "decl". Given that
5221 objc_lookup_ivar is implemented in "c-family" and "objc", we
5222 have a trip through the pure "tree" type, rather than cp_expr.
5223 Naively copying it back to "decl" would implicitly give the
5224 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5225 store an EXPR_LOCATION. Hence we only update "decl" (and
5226 hence its location_t) if we get back a different tree node. */
5227 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5229 if (decl_tree
!= decl
.get_value ())
5230 decl
= cp_expr (decl_tree
);
5232 /* If name lookup gives us a SCOPE_REF, then the
5233 qualifying scope was dependent. */
5234 if (TREE_CODE (decl
) == SCOPE_REF
)
5236 /* At this point, we do not know if DECL is a valid
5237 integral constant expression. We assume that it is
5238 in fact such an expression, so that code like:
5240 template <int N> struct A {
5244 is accepted. At template-instantiation time, we
5245 will check that B<N>::i is actually a constant. */
5248 /* Check to see if DECL is a local variable in a context
5249 where that is forbidden. */
5250 if (parser
->local_variables_forbidden_p
5251 && local_variable_p (decl
))
5253 /* It might be that we only found DECL because we are
5254 trying to be generous with pre-ISO scoping rules.
5255 For example, consider:
5259 for (int i = 0; i < 10; ++i) {}
5260 extern void f(int j = i);
5263 Here, name look up will originally find the out
5264 of scope `i'. We need to issue a warning message,
5265 but then use the global `i'. */
5266 decl
= check_for_out_of_scope_variable (decl
);
5267 if (local_variable_p (decl
))
5269 error_at (id_expr_token
->location
,
5270 "local variable %qD may not appear in this context",
5272 return error_mark_node
;
5277 decl
= (finish_id_expression
5278 (id_expression
, decl
, parser
->scope
,
5280 parser
->integral_constant_expression_p
,
5281 parser
->allow_non_integral_constant_expression_p
,
5282 &parser
->non_integral_constant_expression_p
,
5283 template_p
, done
, address_p
,
5286 id_expr_token
->location
));
5288 cp_parser_error (parser
, error_msg
);
5289 decl
.set_location (id_expr_token
->location
);
5293 /* Anything else is an error. */
5295 cp_parser_error (parser
, "expected primary-expression");
5296 return error_mark_node
;
5300 static inline cp_expr
5301 cp_parser_primary_expression (cp_parser
*parser
,
5304 bool template_arg_p
,
5307 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5308 /*decltype*/false, idk
);
5311 /* Parse an id-expression.
5318 :: [opt] nested-name-specifier template [opt] unqualified-id
5320 :: operator-function-id
5323 Return a representation of the unqualified portion of the
5324 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5325 a `::' or nested-name-specifier.
5327 Often, if the id-expression was a qualified-id, the caller will
5328 want to make a SCOPE_REF to represent the qualified-id. This
5329 function does not do this in order to avoid wastefully creating
5330 SCOPE_REFs when they are not required.
5332 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5335 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5336 uninstantiated templates.
5338 If *TEMPLATE_P is non-NULL, it is set to true iff the
5339 `template' keyword is used to explicitly indicate that the entity
5340 named is a template.
5342 If DECLARATOR_P is true, the id-expression is appearing as part of
5343 a declarator, rather than as part of an expression. */
5346 cp_parser_id_expression (cp_parser
*parser
,
5347 bool template_keyword_p
,
5348 bool check_dependency_p
,
5353 bool global_scope_p
;
5354 bool nested_name_specifier_p
;
5356 /* Assume the `template' keyword was not used. */
5358 *template_p
= template_keyword_p
;
5360 /* Look for the optional `::' operator. */
5362 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5364 /* Look for the optional nested-name-specifier. */
5365 nested_name_specifier_p
5366 = (cp_parser_nested_name_specifier_opt (parser
,
5367 /*typename_keyword_p=*/false,
5372 /* If there is a nested-name-specifier, then we are looking at
5373 the first qualified-id production. */
5374 if (nested_name_specifier_p
)
5377 tree saved_object_scope
;
5378 tree saved_qualifying_scope
;
5379 tree unqualified_id
;
5382 /* See if the next token is the `template' keyword. */
5384 template_p
= &is_template
;
5385 *template_p
= cp_parser_optional_template_keyword (parser
);
5386 /* Name lookup we do during the processing of the
5387 unqualified-id might obliterate SCOPE. */
5388 saved_scope
= parser
->scope
;
5389 saved_object_scope
= parser
->object_scope
;
5390 saved_qualifying_scope
= parser
->qualifying_scope
;
5391 /* Process the final unqualified-id. */
5392 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5395 /*optional_p=*/false);
5396 /* Restore the SAVED_SCOPE for our caller. */
5397 parser
->scope
= saved_scope
;
5398 parser
->object_scope
= saved_object_scope
;
5399 parser
->qualifying_scope
= saved_qualifying_scope
;
5401 return unqualified_id
;
5403 /* Otherwise, if we are in global scope, then we are looking at one
5404 of the other qualified-id productions. */
5405 else if (global_scope_p
)
5410 /* Peek at the next token. */
5411 token
= cp_lexer_peek_token (parser
->lexer
);
5413 /* If it's an identifier, and the next token is not a "<", then
5414 we can avoid the template-id case. This is an optimization
5415 for this common case. */
5416 if (token
->type
== CPP_NAME
5417 && !cp_parser_nth_token_starts_template_argument_list_p
5419 return cp_parser_identifier (parser
);
5421 cp_parser_parse_tentatively (parser
);
5422 /* Try a template-id. */
5423 id
= cp_parser_template_id (parser
,
5424 /*template_keyword_p=*/false,
5425 /*check_dependency_p=*/true,
5428 /* If that worked, we're done. */
5429 if (cp_parser_parse_definitely (parser
))
5432 /* Peek at the next token. (Changes in the token buffer may
5433 have invalidated the pointer obtained above.) */
5434 token
= cp_lexer_peek_token (parser
->lexer
);
5436 switch (token
->type
)
5439 return cp_parser_identifier (parser
);
5442 if (token
->keyword
== RID_OPERATOR
)
5443 return cp_parser_operator_function_id (parser
);
5447 cp_parser_error (parser
, "expected id-expression");
5448 return error_mark_node
;
5452 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5453 /*check_dependency_p=*/true,
5458 /* Parse an unqualified-id.
5462 operator-function-id
5463 conversion-function-id
5467 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5468 keyword, in a construct like `A::template ...'.
5470 Returns a representation of unqualified-id. For the `identifier'
5471 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5472 production a BIT_NOT_EXPR is returned; the operand of the
5473 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5474 other productions, see the documentation accompanying the
5475 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5476 names are looked up in uninstantiated templates. If DECLARATOR_P
5477 is true, the unqualified-id is appearing as part of a declarator,
5478 rather than as part of an expression. */
5481 cp_parser_unqualified_id (cp_parser
* parser
,
5482 bool template_keyword_p
,
5483 bool check_dependency_p
,
5489 /* Peek at the next token. */
5490 token
= cp_lexer_peek_token (parser
->lexer
);
5492 switch ((int) token
->type
)
5498 /* We don't know yet whether or not this will be a
5500 cp_parser_parse_tentatively (parser
);
5501 /* Try a template-id. */
5502 id
= cp_parser_template_id (parser
, template_keyword_p
,
5506 /* If it worked, we're done. */
5507 if (cp_parser_parse_definitely (parser
))
5509 /* Otherwise, it's an ordinary identifier. */
5510 return cp_parser_identifier (parser
);
5513 case CPP_TEMPLATE_ID
:
5514 return cp_parser_template_id (parser
, template_keyword_p
,
5522 tree qualifying_scope
;
5527 /* Consume the `~' token. */
5528 cp_lexer_consume_token (parser
->lexer
);
5529 /* Parse the class-name. The standard, as written, seems to
5532 template <typename T> struct S { ~S (); };
5533 template <typename T> S<T>::~S() {}
5535 is invalid, since `~' must be followed by a class-name, but
5536 `S<T>' is dependent, and so not known to be a class.
5537 That's not right; we need to look in uninstantiated
5538 templates. A further complication arises from:
5540 template <typename T> void f(T t) {
5544 Here, it is not possible to look up `T' in the scope of `T'
5545 itself. We must look in both the current scope, and the
5546 scope of the containing complete expression.
5548 Yet another issue is:
5557 The standard does not seem to say that the `S' in `~S'
5558 should refer to the type `S' and not the data member
5561 /* DR 244 says that we look up the name after the "~" in the
5562 same scope as we looked up the qualifying name. That idea
5563 isn't fully worked out; it's more complicated than that. */
5564 scope
= parser
->scope
;
5565 object_scope
= parser
->object_scope
;
5566 qualifying_scope
= parser
->qualifying_scope
;
5568 /* Check for invalid scopes. */
5569 if (scope
== error_mark_node
)
5571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5572 cp_lexer_consume_token (parser
->lexer
);
5573 return error_mark_node
;
5575 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5577 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5578 error_at (token
->location
,
5579 "scope %qT before %<~%> is not a class-name",
5581 cp_parser_simulate_error (parser
);
5582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5583 cp_lexer_consume_token (parser
->lexer
);
5584 return error_mark_node
;
5586 gcc_assert (!scope
|| TYPE_P (scope
));
5588 /* If the name is of the form "X::~X" it's OK even if X is a
5590 token
= cp_lexer_peek_token (parser
->lexer
);
5592 && token
->type
== CPP_NAME
5593 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5595 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5596 || (CLASS_TYPE_P (scope
)
5597 && constructor_name_p (token
->u
.value
, scope
))))
5599 cp_lexer_consume_token (parser
->lexer
);
5600 return build_nt (BIT_NOT_EXPR
, scope
);
5603 /* ~auto means the destructor of whatever the object is. */
5604 if (cp_parser_is_keyword (token
, RID_AUTO
))
5606 if (cxx_dialect
< cxx14
)
5607 pedwarn (input_location
, 0,
5608 "%<~auto%> only available with "
5609 "-std=c++14 or -std=gnu++14");
5610 cp_lexer_consume_token (parser
->lexer
);
5611 return build_nt (BIT_NOT_EXPR
, make_auto ());
5614 /* If there was an explicit qualification (S::~T), first look
5615 in the scope given by the qualification (i.e., S).
5617 Note: in the calls to cp_parser_class_name below we pass
5618 typename_type so that lookup finds the injected-class-name
5619 rather than the constructor. */
5621 type_decl
= NULL_TREE
;
5624 cp_parser_parse_tentatively (parser
);
5625 type_decl
= cp_parser_class_name (parser
,
5626 /*typename_keyword_p=*/false,
5627 /*template_keyword_p=*/false,
5629 /*check_dependency=*/false,
5630 /*class_head_p=*/false,
5632 if (cp_parser_parse_definitely (parser
))
5635 /* In "N::S::~S", look in "N" as well. */
5636 if (!done
&& scope
&& qualifying_scope
)
5638 cp_parser_parse_tentatively (parser
);
5639 parser
->scope
= qualifying_scope
;
5640 parser
->object_scope
= NULL_TREE
;
5641 parser
->qualifying_scope
= NULL_TREE
;
5643 = cp_parser_class_name (parser
,
5644 /*typename_keyword_p=*/false,
5645 /*template_keyword_p=*/false,
5647 /*check_dependency=*/false,
5648 /*class_head_p=*/false,
5650 if (cp_parser_parse_definitely (parser
))
5653 /* In "p->S::~T", look in the scope given by "*p" as well. */
5654 else if (!done
&& object_scope
)
5656 cp_parser_parse_tentatively (parser
);
5657 parser
->scope
= object_scope
;
5658 parser
->object_scope
= NULL_TREE
;
5659 parser
->qualifying_scope
= NULL_TREE
;
5661 = cp_parser_class_name (parser
,
5662 /*typename_keyword_p=*/false,
5663 /*template_keyword_p=*/false,
5665 /*check_dependency=*/false,
5666 /*class_head_p=*/false,
5668 if (cp_parser_parse_definitely (parser
))
5671 /* Look in the surrounding context. */
5674 parser
->scope
= NULL_TREE
;
5675 parser
->object_scope
= NULL_TREE
;
5676 parser
->qualifying_scope
= NULL_TREE
;
5677 if (processing_template_decl
)
5678 cp_parser_parse_tentatively (parser
);
5680 = cp_parser_class_name (parser
,
5681 /*typename_keyword_p=*/false,
5682 /*template_keyword_p=*/false,
5684 /*check_dependency=*/false,
5685 /*class_head_p=*/false,
5687 if (processing_template_decl
5688 && ! cp_parser_parse_definitely (parser
))
5690 /* We couldn't find a type with this name. If we're parsing
5691 tentatively, fail and try something else. */
5692 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5694 cp_parser_simulate_error (parser
);
5695 return error_mark_node
;
5697 /* Otherwise, accept it and check for a match at instantiation
5699 type_decl
= cp_parser_identifier (parser
);
5700 if (type_decl
!= error_mark_node
)
5701 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5705 /* If an error occurred, assume that the name of the
5706 destructor is the same as the name of the qualifying
5707 class. That allows us to keep parsing after running
5708 into ill-formed destructor names. */
5709 if (type_decl
== error_mark_node
&& scope
)
5710 return build_nt (BIT_NOT_EXPR
, scope
);
5711 else if (type_decl
== error_mark_node
)
5712 return error_mark_node
;
5714 /* Check that destructor name and scope match. */
5715 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5717 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5718 error_at (token
->location
,
5719 "declaration of %<~%T%> as member of %qT",
5721 cp_parser_simulate_error (parser
);
5722 return error_mark_node
;
5727 A typedef-name that names a class shall not be used as the
5728 identifier in the declarator for a destructor declaration. */
5730 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5731 && !DECL_SELF_REFERENCE_P (type_decl
)
5732 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5733 error_at (token
->location
,
5734 "typedef-name %qD used as destructor declarator",
5737 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5741 if (token
->keyword
== RID_OPERATOR
)
5745 /* This could be a template-id, so we try that first. */
5746 cp_parser_parse_tentatively (parser
);
5747 /* Try a template-id. */
5748 id
= cp_parser_template_id (parser
, template_keyword_p
,
5749 /*check_dependency_p=*/true,
5752 /* If that worked, we're done. */
5753 if (cp_parser_parse_definitely (parser
))
5755 /* We still don't know whether we're looking at an
5756 operator-function-id or a conversion-function-id. */
5757 cp_parser_parse_tentatively (parser
);
5758 /* Try an operator-function-id. */
5759 id
= cp_parser_operator_function_id (parser
);
5760 /* If that didn't work, try a conversion-function-id. */
5761 if (!cp_parser_parse_definitely (parser
))
5762 id
= cp_parser_conversion_function_id (parser
);
5763 else if (UDLIT_OPER_P (id
))
5766 const char *name
= UDLIT_OP_SUFFIX (id
);
5767 if (name
[0] != '_' && !in_system_header_at (input_location
)
5769 warning (0, "literal operator suffixes not preceded by %<_%>"
5770 " are reserved for future standardization");
5780 cp_parser_error (parser
, "expected unqualified-id");
5781 return error_mark_node
;
5785 /* Parse an (optional) nested-name-specifier.
5787 nested-name-specifier: [C++98]
5788 class-or-namespace-name :: nested-name-specifier [opt]
5789 class-or-namespace-name :: template nested-name-specifier [opt]
5791 nested-name-specifier: [C++0x]
5794 nested-name-specifier identifier ::
5795 nested-name-specifier template [opt] simple-template-id ::
5797 PARSER->SCOPE should be set appropriately before this function is
5798 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5799 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5802 Sets PARSER->SCOPE to the class (TYPE) or namespace
5803 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5804 it unchanged if there is no nested-name-specifier. Returns the new
5805 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5807 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5808 part of a declaration and/or decl-specifier. */
5811 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5812 bool typename_keyword_p
,
5813 bool check_dependency_p
,
5815 bool is_declaration
)
5817 bool success
= false;
5818 cp_token_position start
= 0;
5821 /* Remember where the nested-name-specifier starts. */
5822 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5824 start
= cp_lexer_token_position (parser
->lexer
, false);
5825 push_deferring_access_checks (dk_deferred
);
5832 tree saved_qualifying_scope
;
5833 bool template_keyword_p
;
5835 /* Spot cases that cannot be the beginning of a
5836 nested-name-specifier. */
5837 token
= cp_lexer_peek_token (parser
->lexer
);
5839 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5840 the already parsed nested-name-specifier. */
5841 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5843 /* Grab the nested-name-specifier and continue the loop. */
5844 cp_parser_pre_parsed_nested_name_specifier (parser
);
5845 /* If we originally encountered this nested-name-specifier
5846 with IS_DECLARATION set to false, we will not have
5847 resolved TYPENAME_TYPEs, so we must do so here. */
5849 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5851 new_scope
= resolve_typename_type (parser
->scope
,
5852 /*only_current_p=*/false);
5853 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5854 parser
->scope
= new_scope
;
5860 /* Spot cases that cannot be the beginning of a
5861 nested-name-specifier. On the second and subsequent times
5862 through the loop, we look for the `template' keyword. */
5863 if (success
&& token
->keyword
== RID_TEMPLATE
)
5865 /* A template-id can start a nested-name-specifier. */
5866 else if (token
->type
== CPP_TEMPLATE_ID
)
5868 /* DR 743: decltype can be used in a nested-name-specifier. */
5869 else if (token_is_decltype (token
))
5873 /* If the next token is not an identifier, then it is
5874 definitely not a type-name or namespace-name. */
5875 if (token
->type
!= CPP_NAME
)
5877 /* If the following token is neither a `<' (to begin a
5878 template-id), nor a `::', then we are not looking at a
5879 nested-name-specifier. */
5880 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5882 if (token
->type
== CPP_COLON
5883 && parser
->colon_corrects_to_scope_p
5884 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5886 error_at (token
->location
,
5887 "found %<:%> in nested-name-specifier, expected %<::%>");
5888 token
->type
= CPP_SCOPE
;
5891 if (token
->type
!= CPP_SCOPE
5892 && !cp_parser_nth_token_starts_template_argument_list_p
5897 /* The nested-name-specifier is optional, so we parse
5899 cp_parser_parse_tentatively (parser
);
5901 /* Look for the optional `template' keyword, if this isn't the
5902 first time through the loop. */
5904 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5906 template_keyword_p
= false;
5908 /* Save the old scope since the name lookup we are about to do
5909 might destroy it. */
5910 old_scope
= parser
->scope
;
5911 saved_qualifying_scope
= parser
->qualifying_scope
;
5912 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5913 look up names in "X<T>::I" in order to determine that "Y" is
5914 a template. So, if we have a typename at this point, we make
5915 an effort to look through it. */
5917 && !typename_keyword_p
5919 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5920 parser
->scope
= resolve_typename_type (parser
->scope
,
5921 /*only_current_p=*/false);
5922 /* Parse the qualifying entity. */
5924 = cp_parser_qualifying_entity (parser
,
5930 /* Look for the `::' token. */
5931 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5933 /* If we found what we wanted, we keep going; otherwise, we're
5935 if (!cp_parser_parse_definitely (parser
))
5937 bool error_p
= false;
5939 /* Restore the OLD_SCOPE since it was valid before the
5940 failed attempt at finding the last
5941 class-or-namespace-name. */
5942 parser
->scope
= old_scope
;
5943 parser
->qualifying_scope
= saved_qualifying_scope
;
5945 /* If the next token is a decltype, and the one after that is a
5946 `::', then the decltype has failed to resolve to a class or
5947 enumeration type. Give this error even when parsing
5948 tentatively since it can't possibly be valid--and we're going
5949 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5950 won't get another chance.*/
5951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5952 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5955 token
= cp_lexer_consume_token (parser
->lexer
);
5956 error_at (token
->location
, "decltype evaluates to %qT, "
5957 "which is not a class or enumeration type",
5958 token
->u
.tree_check_value
->value
);
5959 parser
->scope
= error_mark_node
;
5963 cp_lexer_consume_token (parser
->lexer
);
5966 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5967 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5969 /* If we have a non-type template-id followed by ::, it can't
5970 possibly be valid. */
5971 token
= cp_lexer_peek_token (parser
->lexer
);
5972 tree tid
= token
->u
.tree_check_value
->value
;
5973 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5974 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5976 tree tmpl
= NULL_TREE
;
5977 if (is_overloaded_fn (tid
))
5979 tree fns
= get_fns (tid
);
5980 if (!OVL_CHAIN (fns
))
5981 tmpl
= OVL_CURRENT (fns
);
5982 error_at (token
->location
, "function template-id %qD "
5983 "in nested-name-specifier", tid
);
5987 /* Variable template. */
5988 tmpl
= TREE_OPERAND (tid
, 0);
5989 gcc_assert (variable_template_p (tmpl
));
5990 error_at (token
->location
, "variable template-id %qD "
5991 "in nested-name-specifier", tid
);
5994 inform (DECL_SOURCE_LOCATION (tmpl
),
5995 "%qD declared here", tmpl
);
5997 parser
->scope
= error_mark_node
;
6001 cp_lexer_consume_token (parser
->lexer
);
6002 cp_lexer_consume_token (parser
->lexer
);
6006 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6008 /* If the next token is an identifier, and the one after
6009 that is a `::', then any valid interpretation would have
6010 found a class-or-namespace-name. */
6011 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6012 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6014 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6017 token
= cp_lexer_consume_token (parser
->lexer
);
6020 if (!token
->error_reported
)
6023 tree ambiguous_decls
;
6025 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6027 /*is_template=*/false,
6028 /*is_namespace=*/false,
6029 /*check_dependency=*/true,
6032 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6033 error_at (token
->location
,
6034 "%qD used without template parameters",
6036 else if (ambiguous_decls
)
6038 // cp_parser_lookup_name has the same diagnostic,
6039 // thus make sure to emit it at most once.
6040 if (cp_parser_uncommitted_to_tentative_parse_p
6043 error_at (token
->location
,
6044 "reference to %qD is ambiguous",
6046 print_candidates (ambiguous_decls
);
6048 decl
= error_mark_node
;
6052 if (cxx_dialect
!= cxx98
)
6053 cp_parser_name_lookup_error
6054 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6057 cp_parser_name_lookup_error
6058 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6062 parser
->scope
= error_mark_node
;
6064 /* Treat this as a successful nested-name-specifier
6069 If the name found is not a class-name (clause
6070 _class_) or namespace-name (_namespace.def_), the
6071 program is ill-formed. */
6074 cp_lexer_consume_token (parser
->lexer
);
6078 /* We've found one valid nested-name-specifier. */
6080 /* Name lookup always gives us a DECL. */
6081 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6082 new_scope
= TREE_TYPE (new_scope
);
6083 /* Uses of "template" must be followed by actual templates. */
6084 if (template_keyword_p
6085 && !(CLASS_TYPE_P (new_scope
)
6086 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6087 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6088 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6089 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6090 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6091 == TEMPLATE_ID_EXPR
)))
6092 permerror (input_location
, TYPE_P (new_scope
)
6093 ? G_("%qT is not a template")
6094 : G_("%qD is not a template"),
6096 /* If it is a class scope, try to complete it; we are about to
6097 be looking up names inside the class. */
6098 if (TYPE_P (new_scope
)
6099 /* Since checking types for dependency can be expensive,
6100 avoid doing it if the type is already complete. */
6101 && !COMPLETE_TYPE_P (new_scope
)
6102 /* Do not try to complete dependent types. */
6103 && !dependent_type_p (new_scope
))
6105 new_scope
= complete_type (new_scope
);
6106 /* If it is a typedef to current class, use the current
6107 class instead, as the typedef won't have any names inside
6109 if (!COMPLETE_TYPE_P (new_scope
)
6110 && currently_open_class (new_scope
))
6111 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6113 /* Make sure we look in the right scope the next time through
6115 parser
->scope
= new_scope
;
6118 /* If parsing tentatively, replace the sequence of tokens that makes
6119 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6120 token. That way, should we re-parse the token stream, we will
6121 not have to repeat the effort required to do the parse, nor will
6122 we issue duplicate error messages. */
6123 if (success
&& start
)
6127 token
= cp_lexer_token_at (parser
->lexer
, start
);
6128 /* Reset the contents of the START token. */
6129 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6130 /* Retrieve any deferred checks. Do not pop this access checks yet
6131 so the memory will not be reclaimed during token replacing below. */
6132 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6133 token
->u
.tree_check_value
->value
= parser
->scope
;
6134 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6135 token
->u
.tree_check_value
->qualifying_scope
=
6136 parser
->qualifying_scope
;
6137 token
->keyword
= RID_MAX
;
6139 /* Purge all subsequent tokens. */
6140 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6144 pop_to_parent_deferring_access_checks ();
6146 return success
? parser
->scope
: NULL_TREE
;
6149 /* Parse a nested-name-specifier. See
6150 cp_parser_nested_name_specifier_opt for details. This function
6151 behaves identically, except that it will an issue an error if no
6152 nested-name-specifier is present. */
6155 cp_parser_nested_name_specifier (cp_parser
*parser
,
6156 bool typename_keyword_p
,
6157 bool check_dependency_p
,
6159 bool is_declaration
)
6163 /* Look for the nested-name-specifier. */
6164 scope
= cp_parser_nested_name_specifier_opt (parser
,
6169 /* If it was not present, issue an error message. */
6172 cp_parser_error (parser
, "expected nested-name-specifier");
6173 parser
->scope
= NULL_TREE
;
6179 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6180 this is either a class-name or a namespace-name (which corresponds
6181 to the class-or-namespace-name production in the grammar). For
6182 C++0x, it can also be a type-name that refers to an enumeration
6183 type or a simple-template-id.
6185 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6186 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6187 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6188 TYPE_P is TRUE iff the next name should be taken as a class-name,
6189 even the same name is declared to be another entity in the same
6192 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6193 specified by the class-or-namespace-name. If neither is found the
6194 ERROR_MARK_NODE is returned. */
6197 cp_parser_qualifying_entity (cp_parser
*parser
,
6198 bool typename_keyword_p
,
6199 bool template_keyword_p
,
6200 bool check_dependency_p
,
6202 bool is_declaration
)
6205 tree saved_qualifying_scope
;
6206 tree saved_object_scope
;
6209 bool successful_parse_p
;
6211 /* DR 743: decltype can appear in a nested-name-specifier. */
6212 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6214 scope
= cp_parser_decltype (parser
);
6215 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6216 && !MAYBE_CLASS_TYPE_P (scope
))
6218 cp_parser_simulate_error (parser
);
6219 return error_mark_node
;
6221 if (TYPE_NAME (scope
))
6222 scope
= TYPE_NAME (scope
);
6226 /* Before we try to parse the class-name, we must save away the
6227 current PARSER->SCOPE since cp_parser_class_name will destroy
6229 saved_scope
= parser
->scope
;
6230 saved_qualifying_scope
= parser
->qualifying_scope
;
6231 saved_object_scope
= parser
->object_scope
;
6232 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6233 there is no need to look for a namespace-name. */
6234 only_class_p
= template_keyword_p
6235 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6237 cp_parser_parse_tentatively (parser
);
6238 scope
= cp_parser_class_name (parser
,
6241 type_p
? class_type
: none_type
,
6243 /*class_head_p=*/false,
6245 /*enum_ok=*/cxx_dialect
> cxx98
);
6246 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6247 /* If that didn't work, try for a namespace-name. */
6248 if (!only_class_p
&& !successful_parse_p
)
6250 /* Restore the saved scope. */
6251 parser
->scope
= saved_scope
;
6252 parser
->qualifying_scope
= saved_qualifying_scope
;
6253 parser
->object_scope
= saved_object_scope
;
6254 /* If we are not looking at an identifier followed by the scope
6255 resolution operator, then this is not part of a
6256 nested-name-specifier. (Note that this function is only used
6257 to parse the components of a nested-name-specifier.) */
6258 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6259 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6260 return error_mark_node
;
6261 scope
= cp_parser_namespace_name (parser
);
6267 /* Return true if we are looking at a compound-literal, false otherwise. */
6270 cp_parser_compound_literal_p (cp_parser
*parser
)
6272 /* Consume the `('. */
6273 cp_lexer_consume_token (parser
->lexer
);
6275 cp_lexer_save_tokens (parser
->lexer
);
6277 /* Skip tokens until the next token is a closing parenthesis.
6278 If we find the closing `)', and the next token is a `{', then
6279 we are looking at a compound-literal. */
6280 bool compound_literal_p
6281 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6282 /*consume_paren=*/true)
6283 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6285 /* Roll back the tokens we skipped. */
6286 cp_lexer_rollback_tokens (parser
->lexer
);
6288 return compound_literal_p
;
6291 /* Parse a postfix-expression.
6295 postfix-expression [ expression ]
6296 postfix-expression ( expression-list [opt] )
6297 simple-type-specifier ( expression-list [opt] )
6298 typename :: [opt] nested-name-specifier identifier
6299 ( expression-list [opt] )
6300 typename :: [opt] nested-name-specifier template [opt] template-id
6301 ( expression-list [opt] )
6302 postfix-expression . template [opt] id-expression
6303 postfix-expression -> template [opt] id-expression
6304 postfix-expression . pseudo-destructor-name
6305 postfix-expression -> pseudo-destructor-name
6306 postfix-expression ++
6307 postfix-expression --
6308 dynamic_cast < type-id > ( expression )
6309 static_cast < type-id > ( expression )
6310 reinterpret_cast < type-id > ( expression )
6311 const_cast < type-id > ( expression )
6312 typeid ( expression )
6318 ( type-id ) { initializer-list , [opt] }
6320 This extension is a GNU version of the C99 compound-literal
6321 construct. (The C99 grammar uses `type-name' instead of `type-id',
6322 but they are essentially the same concept.)
6324 If ADDRESS_P is true, the postfix expression is the operand of the
6325 `&' operator. CAST_P is true if this expression is the target of a
6328 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6329 class member access expressions [expr.ref].
6331 Returns a representation of the expression. */
6334 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6335 bool member_access_only_p
, bool decltype_p
,
6336 cp_id_kind
* pidk_return
)
6341 cp_id_kind idk
= CP_ID_KIND_NONE
;
6342 cp_expr postfix_expression
= NULL_TREE
;
6343 bool is_member_access
= false;
6344 int saved_in_statement
= -1;
6346 /* Peek at the next token. */
6347 token
= cp_lexer_peek_token (parser
->lexer
);
6348 loc
= token
->location
;
6349 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6351 /* Some of the productions are determined by keywords. */
6352 keyword
= token
->keyword
;
6362 const char *saved_message
;
6363 bool saved_in_type_id_in_expr_p
;
6365 /* All of these can be handled in the same way from the point
6366 of view of parsing. Begin by consuming the token
6367 identifying the cast. */
6368 cp_lexer_consume_token (parser
->lexer
);
6370 /* New types cannot be defined in the cast. */
6371 saved_message
= parser
->type_definition_forbidden_message
;
6372 parser
->type_definition_forbidden_message
6373 = G_("types may not be defined in casts");
6375 /* Look for the opening `<'. */
6376 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6377 /* Parse the type to which we are casting. */
6378 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6379 parser
->in_type_id_in_expr_p
= true;
6380 type
= cp_parser_type_id (parser
);
6381 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6382 /* Look for the closing `>'. */
6383 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6384 /* Restore the old message. */
6385 parser
->type_definition_forbidden_message
= saved_message
;
6387 bool saved_greater_than_is_operator_p
6388 = parser
->greater_than_is_operator_p
;
6389 parser
->greater_than_is_operator_p
= true;
6391 /* And the expression which is being cast. */
6392 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6393 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6394 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6396 location_t end_loc
= close_paren
?
6397 close_paren
->location
: UNKNOWN_LOCATION
;
6399 parser
->greater_than_is_operator_p
6400 = saved_greater_than_is_operator_p
;
6402 /* Only type conversions to integral or enumeration types
6403 can be used in constant-expressions. */
6404 if (!cast_valid_in_integral_constant_expression_p (type
)
6405 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6406 return error_mark_node
;
6412 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6416 = build_static_cast (type
, expression
, tf_warning_or_error
);
6420 = build_reinterpret_cast (type
, expression
,
6421 tf_warning_or_error
);
6425 = build_const_cast (type
, expression
, tf_warning_or_error
);
6431 /* Construct a location e.g. :
6432 reinterpret_cast <int *> (expr)
6433 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6434 ranging from the start of the "*_cast" token to the final closing
6435 paren, with the caret at the start. */
6436 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6437 postfix_expression
.set_location (cp_cast_loc
);
6444 const char *saved_message
;
6445 bool saved_in_type_id_in_expr_p
;
6447 /* Consume the `typeid' token. */
6448 cp_lexer_consume_token (parser
->lexer
);
6449 /* Look for the `(' token. */
6450 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6451 /* Types cannot be defined in a `typeid' expression. */
6452 saved_message
= parser
->type_definition_forbidden_message
;
6453 parser
->type_definition_forbidden_message
6454 = G_("types may not be defined in a %<typeid%> expression");
6455 /* We can't be sure yet whether we're looking at a type-id or an
6457 cp_parser_parse_tentatively (parser
);
6458 /* Try a type-id first. */
6459 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6460 parser
->in_type_id_in_expr_p
= true;
6461 type
= cp_parser_type_id (parser
);
6462 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6463 /* Look for the `)' token. Otherwise, we can't be sure that
6464 we're not looking at an expression: consider `typeid (int
6465 (3))', for example. */
6466 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6467 /* If all went well, simply lookup the type-id. */
6468 if (cp_parser_parse_definitely (parser
))
6469 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6470 /* Otherwise, fall back to the expression variant. */
6475 /* Look for an expression. */
6476 expression
= cp_parser_expression (parser
, & idk
);
6477 /* Compute its typeid. */
6478 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6479 /* Look for the `)' token. */
6480 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6482 /* Restore the saved message. */
6483 parser
->type_definition_forbidden_message
= saved_message
;
6484 /* `typeid' may not appear in an integral constant expression. */
6485 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6486 return error_mark_node
;
6493 /* The syntax permitted here is the same permitted for an
6494 elaborated-type-specifier. */
6495 ++parser
->prevent_constrained_type_specifiers
;
6496 type
= cp_parser_elaborated_type_specifier (parser
,
6497 /*is_friend=*/false,
6498 /*is_declaration=*/false);
6499 --parser
->prevent_constrained_type_specifiers
;
6500 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6504 case RID_CILK_SPAWN
:
6506 location_t cilk_spawn_loc
6507 = cp_lexer_peek_token (parser
->lexer
)->location
;
6508 cp_lexer_consume_token (parser
->lexer
);
6509 token
= cp_lexer_peek_token (parser
->lexer
);
6510 if (token
->type
== CPP_SEMICOLON
)
6512 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6514 postfix_expression
= error_mark_node
;
6517 else if (!current_function_decl
)
6519 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6520 "inside a function");
6521 postfix_expression
= error_mark_node
;
6526 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6527 saved_in_statement
= parser
->in_statement
;
6528 parser
->in_statement
|= IN_CILK_SPAWN
;
6530 cfun
->calls_cilk_spawn
= 1;
6531 postfix_expression
=
6532 cp_parser_postfix_expression (parser
, false, false,
6533 false, false, &idk
);
6536 error_at (token
->location
, "-fcilkplus must be enabled to use"
6537 " %<_Cilk_spawn%>");
6538 cfun
->calls_cilk_spawn
= 0;
6540 else if (saved_in_statement
& IN_CILK_SPAWN
)
6542 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6543 "are not permitted");
6544 postfix_expression
= error_mark_node
;
6545 cfun
->calls_cilk_spawn
= 0;
6549 location_t loc
= postfix_expression
.get_location ();
6550 postfix_expression
= build_cilk_spawn (token
->location
,
6551 postfix_expression
);
6552 /* Build a location of the form:
6555 with caret at the expr, ranging from the start of the
6556 _Cilk_spawn token to the end of the expression. */
6557 location_t combined_loc
=
6558 make_location (loc
, cilk_spawn_loc
, get_finish (loc
));
6559 postfix_expression
.set_location (combined_loc
);
6560 if (postfix_expression
!= error_mark_node
)
6561 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6562 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6567 case RID_BUILTIN_SHUFFLE
:
6569 vec
<tree
, va_gc
> *vec
;
6573 cp_lexer_consume_token (parser
->lexer
);
6574 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6575 /*cast_p=*/false, /*allow_expansion_p=*/true,
6576 /*non_constant_p=*/NULL
);
6578 return error_mark_node
;
6580 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6583 if (vec
->length () == 2)
6584 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6585 tf_warning_or_error
);
6586 else if (vec
->length () == 3)
6587 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6588 tf_warning_or_error
);
6591 error_at (loc
, "wrong number of arguments to "
6592 "%<__builtin_shuffle%>");
6593 return error_mark_node
;
6602 /* If the next thing is a simple-type-specifier, we may be
6603 looking at a functional cast. We could also be looking at
6604 an id-expression. So, we try the functional cast, and if
6605 that doesn't work we fall back to the primary-expression. */
6606 cp_parser_parse_tentatively (parser
);
6607 /* Look for the simple-type-specifier. */
6608 ++parser
->prevent_constrained_type_specifiers
;
6609 type
= cp_parser_simple_type_specifier (parser
,
6610 /*decl_specs=*/NULL
,
6611 CP_PARSER_FLAGS_NONE
);
6612 --parser
->prevent_constrained_type_specifiers
;
6613 /* Parse the cast itself. */
6614 if (!cp_parser_error_occurred (parser
))
6616 = cp_parser_functional_cast (parser
, type
);
6617 /* If that worked, we're done. */
6618 if (cp_parser_parse_definitely (parser
))
6621 /* If the functional-cast didn't work out, try a
6622 compound-literal. */
6623 if (cp_parser_allow_gnu_extensions_p (parser
)
6624 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6626 cp_expr initializer
= NULL_TREE
;
6628 cp_parser_parse_tentatively (parser
);
6630 /* Avoid calling cp_parser_type_id pointlessly, see comment
6631 in cp_parser_cast_expression about c++/29234. */
6632 if (!cp_parser_compound_literal_p (parser
))
6633 cp_parser_simulate_error (parser
);
6636 /* Parse the type. */
6637 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6638 parser
->in_type_id_in_expr_p
= true;
6639 type
= cp_parser_type_id (parser
);
6640 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6641 /* Look for the `)'. */
6642 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6645 /* If things aren't going well, there's no need to
6647 if (!cp_parser_error_occurred (parser
))
6649 bool non_constant_p
;
6650 /* Parse the brace-enclosed initializer list. */
6651 initializer
= cp_parser_braced_list (parser
,
6654 /* If that worked, we're definitely looking at a
6655 compound-literal expression. */
6656 if (cp_parser_parse_definitely (parser
))
6658 /* Warn the user that a compound literal is not
6659 allowed in standard C++. */
6660 pedwarn (input_location
, OPT_Wpedantic
,
6661 "ISO C++ forbids compound-literals");
6662 /* For simplicity, we disallow compound literals in
6663 constant-expressions. We could
6664 allow compound literals of integer type, whose
6665 initializer was a constant, in constant
6666 expressions. Permitting that usage, as a further
6667 extension, would not change the meaning of any
6668 currently accepted programs. (Of course, as
6669 compound literals are not part of ISO C++, the
6670 standard has nothing to say.) */
6671 if (cp_parser_non_integral_constant_expression (parser
,
6674 postfix_expression
= error_mark_node
;
6677 /* Form the representation of the compound-literal. */
6679 = finish_compound_literal (type
, initializer
,
6680 tf_warning_or_error
);
6681 postfix_expression
.set_location (initializer
.get_location ());
6686 /* It must be a primary-expression. */
6688 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6689 /*template_arg_p=*/false,
6696 /* Note that we don't need to worry about calling build_cplus_new on a
6697 class-valued CALL_EXPR in decltype when it isn't the end of the
6698 postfix-expression; unary_complex_lvalue will take care of that for
6701 /* Keep looping until the postfix-expression is complete. */
6704 if (idk
== CP_ID_KIND_UNQUALIFIED
6705 && identifier_p (postfix_expression
)
6706 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6707 /* It is not a Koenig lookup function call. */
6709 = unqualified_name_lookup_error (postfix_expression
);
6711 /* Peek at the next token. */
6712 token
= cp_lexer_peek_token (parser
->lexer
);
6714 switch (token
->type
)
6716 case CPP_OPEN_SQUARE
:
6717 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6719 cp_parser_error (parser
,
6720 "two consecutive %<[%> shall "
6721 "only introduce an attribute");
6722 return error_mark_node
;
6725 = cp_parser_postfix_open_square_expression (parser
,
6729 postfix_expression
.set_range (start_loc
,
6730 postfix_expression
.get_location ());
6732 idk
= CP_ID_KIND_NONE
;
6733 is_member_access
= false;
6736 case CPP_OPEN_PAREN
:
6737 /* postfix-expression ( expression-list [opt] ) */
6740 bool is_builtin_constant_p
;
6741 bool saved_integral_constant_expression_p
= false;
6742 bool saved_non_integral_constant_expression_p
= false;
6743 tsubst_flags_t complain
= complain_flags (decltype_p
);
6744 vec
<tree
, va_gc
> *args
;
6745 location_t close_paren_loc
= UNKNOWN_LOCATION
;
6747 is_member_access
= false;
6749 is_builtin_constant_p
6750 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6751 if (is_builtin_constant_p
)
6753 /* The whole point of __builtin_constant_p is to allow
6754 non-constant expressions to appear as arguments. */
6755 saved_integral_constant_expression_p
6756 = parser
->integral_constant_expression_p
;
6757 saved_non_integral_constant_expression_p
6758 = parser
->non_integral_constant_expression_p
;
6759 parser
->integral_constant_expression_p
= false;
6761 args
= (cp_parser_parenthesized_expression_list
6763 /*cast_p=*/false, /*allow_expansion_p=*/true,
6764 /*non_constant_p=*/NULL
,
6765 /*close_paren_loc=*/&close_paren_loc
));
6766 if (is_builtin_constant_p
)
6768 parser
->integral_constant_expression_p
6769 = saved_integral_constant_expression_p
;
6770 parser
->non_integral_constant_expression_p
6771 = saved_non_integral_constant_expression_p
;
6776 postfix_expression
= error_mark_node
;
6780 /* Function calls are not permitted in
6781 constant-expressions. */
6782 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6783 && cp_parser_non_integral_constant_expression (parser
,
6786 postfix_expression
= error_mark_node
;
6787 release_tree_vector (args
);
6792 if (idk
== CP_ID_KIND_UNQUALIFIED
6793 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6795 if (identifier_p (postfix_expression
))
6797 if (!args
->is_empty ())
6800 if (!any_type_dependent_arguments_p (args
))
6802 = perform_koenig_lookup (postfix_expression
, args
,
6807 = unqualified_fn_lookup_error (postfix_expression
);
6809 /* We do not perform argument-dependent lookup if
6810 normal lookup finds a non-function, in accordance
6811 with the expected resolution of DR 218. */
6812 else if (!args
->is_empty ()
6813 && is_overloaded_fn (postfix_expression
))
6815 tree fn
= get_first_fn (postfix_expression
);
6816 fn
= STRIP_TEMPLATE (fn
);
6818 /* Do not do argument dependent lookup if regular
6819 lookup finds a member function or a block-scope
6820 function declaration. [basic.lookup.argdep]/3 */
6821 if (!DECL_FUNCTION_MEMBER_P (fn
)
6822 && !DECL_LOCAL_FUNCTION_P (fn
))
6825 if (!any_type_dependent_arguments_p (args
))
6827 = perform_koenig_lookup (postfix_expression
, args
,
6833 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6834 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6835 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6836 && vec_safe_length (args
) == 3)
6838 tree arg0
= (*args
)[0];
6839 tree arg1
= (*args
)[1];
6840 tree arg2
= (*args
)[2];
6841 int literal_mask
= ((!!integer_zerop (arg1
) << 1)
6842 | (!!integer_zerop (arg2
) << 2));
6843 if (TREE_CODE (arg2
) == CONST_DECL
)
6844 arg2
= DECL_INITIAL (arg2
);
6845 warn_for_memset (input_location
, arg0
, arg2
, literal_mask
);
6848 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6850 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6851 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6853 if (processing_template_decl
6854 && (type_dependent_expression_p (instance
)
6855 || (!BASELINK_P (fn
)
6856 && TREE_CODE (fn
) != FIELD_DECL
)
6857 || type_dependent_expression_p (fn
)
6858 || any_type_dependent_arguments_p (args
)))
6861 = build_nt_call_vec (postfix_expression
, args
);
6862 release_tree_vector (args
);
6866 if (BASELINK_P (fn
))
6869 = (build_new_method_call
6870 (instance
, fn
, &args
, NULL_TREE
,
6871 (idk
== CP_ID_KIND_QUALIFIED
6872 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6879 = finish_call_expr (postfix_expression
, &args
,
6880 /*disallow_virtual=*/false,
6884 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6885 || TREE_CODE (postfix_expression
) == MEMBER_REF
6886 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6887 postfix_expression
= (build_offset_ref_call_from_tree
6888 (postfix_expression
, &args
,
6890 else if (idk
== CP_ID_KIND_QUALIFIED
)
6891 /* A call to a static class member, or a namespace-scope
6894 = finish_call_expr (postfix_expression
, &args
,
6895 /*disallow_virtual=*/true,
6899 /* All other function calls. */
6901 = finish_call_expr (postfix_expression
, &args
,
6902 /*disallow_virtual=*/false,
6906 if (close_paren_loc
!= UNKNOWN_LOCATION
)
6908 location_t combined_loc
= make_location (token
->location
,
6911 postfix_expression
.set_location (combined_loc
);
6914 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6915 idk
= CP_ID_KIND_NONE
;
6917 release_tree_vector (args
);
6923 /* postfix-expression . template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> template [opt] id-expression
6926 postfix-expression -> pseudo-destructor-name */
6928 /* Consume the `.' or `->' operator. */
6929 cp_lexer_consume_token (parser
->lexer
);
6932 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6936 is_member_access
= true;
6940 /* postfix-expression ++ */
6941 /* Consume the `++' token. */
6942 cp_lexer_consume_token (parser
->lexer
);
6943 /* Generate a representation for the complete expression. */
6945 = finish_increment_expr (postfix_expression
,
6946 POSTINCREMENT_EXPR
);
6947 /* Increments may not appear in constant-expressions. */
6948 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6949 postfix_expression
= error_mark_node
;
6950 idk
= CP_ID_KIND_NONE
;
6951 is_member_access
= false;
6954 case CPP_MINUS_MINUS
:
6955 /* postfix-expression -- */
6956 /* Consume the `--' token. */
6957 cp_lexer_consume_token (parser
->lexer
);
6958 /* Generate a representation for the complete expression. */
6960 = finish_increment_expr (postfix_expression
,
6961 POSTDECREMENT_EXPR
);
6962 /* Decrements may not appear in constant-expressions. */
6963 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6964 postfix_expression
= error_mark_node
;
6965 idk
= CP_ID_KIND_NONE
;
6966 is_member_access
= false;
6970 if (pidk_return
!= NULL
)
6971 * pidk_return
= idk
;
6972 if (member_access_only_p
)
6973 return is_member_access
6974 ? postfix_expression
6975 : cp_expr (error_mark_node
);
6977 return postfix_expression
;
6981 /* We should never get here. */
6983 return error_mark_node
;
6986 /* This function parses Cilk Plus array notations. If a normal array expr. is
6987 parsed then the array index is passed back to the caller through *INIT_INDEX
6988 and the function returns a NULL_TREE. If array notation expr. is parsed,
6989 then *INIT_INDEX is ignored by the caller and the function returns
6990 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6994 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6997 cp_token
*token
= NULL
;
6998 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6999 if (!array_value
|| array_value
== error_mark_node
)
7001 cp_parser_skip_to_end_of_statement (parser
);
7002 return error_mark_node
;
7005 array_type
= TREE_TYPE (array_value
);
7007 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
7008 parser
->colon_corrects_to_scope_p
= false;
7009 token
= cp_lexer_peek_token (parser
->lexer
);
7013 cp_parser_error (parser
, "expected %<:%> or numeral");
7014 return error_mark_node
;
7016 else if (token
->type
== CPP_COLON
)
7018 /* Consume the ':'. */
7019 cp_lexer_consume_token (parser
->lexer
);
7021 /* If we are here, then we have a case like this A[:]. */
7022 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
7024 cp_parser_error (parser
, "expected %<]%>");
7025 cp_parser_skip_to_end_of_statement (parser
);
7026 return error_mark_node
;
7028 *init_index
= NULL_TREE
;
7030 length_index
= NULL_TREE
;
7034 /* If we are here, then there are three valid possibilities:
7036 2. ARRAY [ EXP : EXP ]
7037 3. ARRAY [ EXP : EXP : EXP ] */
7039 *init_index
= cp_parser_expression (parser
);
7040 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
7042 /* This indicates that we have a normal array expression. */
7043 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7047 /* Consume the ':'. */
7048 cp_lexer_consume_token (parser
->lexer
);
7049 length_index
= cp_parser_expression (parser
);
7050 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7052 cp_lexer_consume_token (parser
->lexer
);
7053 stride
= cp_parser_expression (parser
);
7056 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
7058 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
7059 || stride
== error_mark_node
|| array_type
== error_mark_node
)
7061 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
7062 cp_lexer_consume_token (parser
->lexer
);
7063 return error_mark_node
;
7065 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7067 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
7068 length_index
, stride
, array_type
);
7072 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7073 by cp_parser_builtin_offsetof. We're looking for
7075 postfix-expression [ expression ]
7076 postfix-expression [ braced-init-list ] (C++11)
7078 FOR_OFFSETOF is set if we're being called in that context, which
7079 changes how we deal with integer constant expressions. */
7082 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7083 tree postfix_expression
,
7087 tree index
= NULL_TREE
;
7088 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7089 bool saved_greater_than_is_operator_p
;
7091 /* Consume the `[' token. */
7092 cp_lexer_consume_token (parser
->lexer
);
7094 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7095 parser
->greater_than_is_operator_p
= true;
7097 /* Parse the index expression. */
7098 /* ??? For offsetof, there is a question of what to allow here. If
7099 offsetof is not being used in an integral constant expression context,
7100 then we *could* get the right answer by computing the value at runtime.
7101 If we are in an integral constant expression context, then we might
7102 could accept any constant expression; hard to say without analysis.
7103 Rather than open the barn door too wide right away, allow only integer
7104 constant expressions here. */
7106 index
= cp_parser_constant_expression (parser
);
7109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7111 bool expr_nonconst_p
;
7112 cp_lexer_set_source_position (parser
->lexer
);
7113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7114 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7116 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
7118 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
7119 "braced list index is not allowed with array "
7121 cp_parser_skip_to_end_of_statement (parser
);
7122 return error_mark_node
;
7125 else if (flag_cilkplus
)
7127 /* Here are have these two options:
7128 ARRAY[EXP : EXP] - Array notation expr with default
7130 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7132 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
7133 postfix_expression
);
7138 index
= cp_parser_expression (parser
);
7141 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7143 /* Look for the closing `]'. */
7144 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7146 /* Build the ARRAY_REF. */
7147 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7150 /* When not doing offsetof, array references are not permitted in
7151 constant-expressions. */
7153 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7154 postfix_expression
= error_mark_node
;
7156 return postfix_expression
;
7159 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7160 by cp_parser_builtin_offsetof. We're looking for
7162 postfix-expression . template [opt] id-expression
7163 postfix-expression . pseudo-destructor-name
7164 postfix-expression -> template [opt] id-expression
7165 postfix-expression -> pseudo-destructor-name
7167 FOR_OFFSETOF is set if we're being called in that context. That sorta
7168 limits what of the above we'll actually accept, but nevermind.
7169 TOKEN_TYPE is the "." or "->" token, which will already have been
7170 removed from the stream. */
7173 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7174 enum cpp_ttype token_type
,
7175 cp_expr postfix_expression
,
7176 bool for_offsetof
, cp_id_kind
*idk
,
7177 location_t location
)
7181 bool pseudo_destructor_p
;
7182 tree scope
= NULL_TREE
;
7183 location_t start_loc
= postfix_expression
.get_start ();
7185 /* If this is a `->' operator, dereference the pointer. */
7186 if (token_type
== CPP_DEREF
)
7187 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7188 tf_warning_or_error
);
7189 /* Check to see whether or not the expression is type-dependent. */
7190 dependent_p
= type_dependent_expression_p (postfix_expression
);
7191 /* The identifier following the `->' or `.' is not qualified. */
7192 parser
->scope
= NULL_TREE
;
7193 parser
->qualifying_scope
= NULL_TREE
;
7194 parser
->object_scope
= NULL_TREE
;
7195 *idk
= CP_ID_KIND_NONE
;
7197 /* Enter the scope corresponding to the type of the object
7198 given by the POSTFIX_EXPRESSION. */
7199 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
7201 scope
= TREE_TYPE (postfix_expression
);
7202 /* According to the standard, no expression should ever have
7203 reference type. Unfortunately, we do not currently match
7204 the standard in this respect in that our internal representation
7205 of an expression may have reference type even when the standard
7206 says it does not. Therefore, we have to manually obtain the
7207 underlying type here. */
7208 scope
= non_reference (scope
);
7209 /* The type of the POSTFIX_EXPRESSION must be complete. */
7210 /* Unlike the object expression in other contexts, *this is not
7211 required to be of complete type for purposes of class member
7212 access (5.2.5) outside the member function body. */
7213 if (postfix_expression
!= current_class_ref
7214 && !(processing_template_decl
&& scope
== current_class_type
))
7215 scope
= complete_type_or_else (scope
, postfix_expression
);
7216 /* Let the name lookup machinery know that we are processing a
7217 class member access expression. */
7218 parser
->context
->object_type
= scope
;
7219 /* If something went wrong, we want to be able to discern that case,
7220 as opposed to the case where there was no SCOPE due to the type
7221 of expression being dependent. */
7223 scope
= error_mark_node
;
7224 /* If the SCOPE was erroneous, make the various semantic analysis
7225 functions exit quickly -- and without issuing additional error
7227 if (scope
== error_mark_node
)
7228 postfix_expression
= error_mark_node
;
7231 /* Tell cp_parser_lookup_name that there was an object, even though it's
7233 parser
->context
->object_type
= unknown_type_node
;
7235 /* Assume this expression is not a pseudo-destructor access. */
7236 pseudo_destructor_p
= false;
7238 /* If the SCOPE is a scalar type, then, if this is a valid program,
7239 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7240 is type dependent, it can be pseudo-destructor-name or something else.
7241 Try to parse it as pseudo-destructor-name first. */
7242 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7247 cp_parser_parse_tentatively (parser
);
7248 /* Parse the pseudo-destructor-name. */
7250 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7253 && (cp_parser_error_occurred (parser
)
7254 || !SCALAR_TYPE_P (type
)))
7255 cp_parser_abort_tentative_parse (parser
);
7256 else if (cp_parser_parse_definitely (parser
))
7258 pseudo_destructor_p
= true;
7260 = finish_pseudo_destructor_expr (postfix_expression
,
7265 if (!pseudo_destructor_p
)
7267 /* If the SCOPE is not a scalar type, we are looking at an
7268 ordinary class member access expression, rather than a
7269 pseudo-destructor-name. */
7271 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7272 /* Parse the id-expression. */
7273 name
= (cp_parser_id_expression
7275 cp_parser_optional_template_keyword (parser
),
7276 /*check_dependency_p=*/true,
7278 /*declarator_p=*/false,
7279 /*optional_p=*/false));
7280 /* In general, build a SCOPE_REF if the member name is qualified.
7281 However, if the name was not dependent and has already been
7282 resolved; there is no need to build the SCOPE_REF. For example;
7284 struct X { void f(); };
7285 template <typename T> void f(T* t) { t->X::f(); }
7287 Even though "t" is dependent, "X::f" is not and has been resolved
7288 to a BASELINK; there is no need to include scope information. */
7290 /* But we do need to remember that there was an explicit scope for
7291 virtual function calls. */
7293 *idk
= CP_ID_KIND_QUALIFIED
;
7295 /* If the name is a template-id that names a type, we will get a
7296 TYPE_DECL here. That is invalid code. */
7297 if (TREE_CODE (name
) == TYPE_DECL
)
7299 error_at (token
->location
, "invalid use of %qD", name
);
7300 postfix_expression
= error_mark_node
;
7304 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7306 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7308 error_at (token
->location
, "%<%D::%D%> is not a class member",
7309 parser
->scope
, name
);
7310 postfix_expression
= error_mark_node
;
7313 name
= build_qualified_name (/*type=*/NULL_TREE
,
7317 parser
->scope
= NULL_TREE
;
7318 parser
->qualifying_scope
= NULL_TREE
;
7319 parser
->object_scope
= NULL_TREE
;
7321 if (parser
->scope
&& name
&& BASELINK_P (name
))
7322 adjust_result_of_qualified_name_lookup
7323 (name
, parser
->scope
, scope
);
7325 = finish_class_member_access_expr (postfix_expression
, name
,
7327 tf_warning_or_error
);
7328 /* Build a location e.g.:
7331 where the caret is at the deref token, ranging from
7332 the start of postfix_expression to the end of the access expr. */
7334 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
7335 location_t combined_loc
7336 = make_location (input_location
, start_loc
, end_loc
);
7337 protected_set_expr_location (postfix_expression
, combined_loc
);
7341 /* We no longer need to look up names in the scope of the object on
7342 the left-hand side of the `.' or `->' operator. */
7343 parser
->context
->object_type
= NULL_TREE
;
7345 /* Outside of offsetof, these operators may not appear in
7346 constant-expressions. */
7348 && (cp_parser_non_integral_constant_expression
7349 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7350 postfix_expression
= error_mark_node
;
7352 return postfix_expression
;
7355 /* Parse a parenthesized expression-list.
7358 assignment-expression
7359 expression-list, assignment-expression
7364 identifier, expression-list
7366 CAST_P is true if this expression is the target of a cast.
7368 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7371 Returns a vector of trees. Each element is a representation of an
7372 assignment-expression. NULL is returned if the ( and or ) are
7373 missing. An empty, but allocated, vector is returned on no
7374 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7375 if we are parsing an attribute list for an attribute that wants a
7376 plain identifier argument, normal_attr for an attribute that wants
7377 an expression, or non_attr if we aren't parsing an attribute list. If
7378 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7379 not all of the expressions in the list were constant.
7380 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7381 will be written to with the location of the closing parenthesis. If
7382 an error occurs, it may or may not be written to. */
7384 static vec
<tree
, va_gc
> *
7385 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7386 int is_attribute_list
,
7388 bool allow_expansion_p
,
7389 bool *non_constant_p
,
7390 location_t
*close_paren_loc
)
7392 vec
<tree
, va_gc
> *expression_list
;
7393 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7394 tree identifier
= NULL_TREE
;
7395 bool saved_greater_than_is_operator_p
;
7397 /* Assume all the expressions will be constant. */
7399 *non_constant_p
= false;
7401 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7404 expression_list
= make_tree_vector ();
7406 /* Within a parenthesized expression, a `>' token is always
7407 the greater-than operator. */
7408 saved_greater_than_is_operator_p
7409 = parser
->greater_than_is_operator_p
;
7410 parser
->greater_than_is_operator_p
= true;
7412 /* Consume expressions until there are no more. */
7413 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7418 /* At the beginning of attribute lists, check to see if the
7419 next token is an identifier. */
7420 if (is_attribute_list
== id_attr
7421 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7425 /* Consume the identifier. */
7426 token
= cp_lexer_consume_token (parser
->lexer
);
7427 /* Save the identifier. */
7428 identifier
= token
->u
.value
;
7432 bool expr_non_constant_p
;
7434 /* Parse the next assignment-expression. */
7435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7437 /* A braced-init-list. */
7438 cp_lexer_set_source_position (parser
->lexer
);
7439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7440 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7441 if (non_constant_p
&& expr_non_constant_p
)
7442 *non_constant_p
= true;
7444 else if (non_constant_p
)
7446 expr
= (cp_parser_constant_expression
7447 (parser
, /*allow_non_constant_p=*/true,
7448 &expr_non_constant_p
));
7449 if (expr_non_constant_p
)
7450 *non_constant_p
= true;
7453 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7457 expr
= instantiate_non_dependent_expr (expr
);
7459 /* If we have an ellipsis, then this is an expression
7461 if (allow_expansion_p
7462 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7464 /* Consume the `...'. */
7465 cp_lexer_consume_token (parser
->lexer
);
7467 /* Build the argument pack. */
7468 expr
= make_pack_expansion (expr
);
7471 /* Add it to the list. We add error_mark_node
7472 expressions to the list, so that we can still tell if
7473 the correct form for a parenthesized expression-list
7474 is found. That gives better errors. */
7475 vec_safe_push (expression_list
, expr
);
7477 if (expr
== error_mark_node
)
7481 /* After the first item, attribute lists look the same as
7482 expression lists. */
7483 is_attribute_list
= non_attr
;
7486 /* If the next token isn't a `,', then we are done. */
7487 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7490 /* Otherwise, consume the `,' and keep going. */
7491 cp_lexer_consume_token (parser
->lexer
);
7494 if (close_paren_loc
)
7495 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7497 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7502 /* We try and resync to an unnested comma, as that will give the
7503 user better diagnostics. */
7504 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7505 /*recovering=*/true,
7507 /*consume_paren=*/true);
7512 parser
->greater_than_is_operator_p
7513 = saved_greater_than_is_operator_p
;
7518 parser
->greater_than_is_operator_p
7519 = saved_greater_than_is_operator_p
;
7522 vec_safe_insert (expression_list
, 0, identifier
);
7524 return expression_list
;
7527 /* Parse a pseudo-destructor-name.
7529 pseudo-destructor-name:
7530 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7531 :: [opt] nested-name-specifier template template-id :: ~ type-name
7532 :: [opt] nested-name-specifier [opt] ~ type-name
7534 If either of the first two productions is used, sets *SCOPE to the
7535 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7536 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7537 or ERROR_MARK_NODE if the parse fails. */
7540 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7545 bool nested_name_specifier_p
;
7548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7549 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7550 && !type_dependent_expression_p (object
))
7552 if (cxx_dialect
< cxx14
)
7553 pedwarn (input_location
, 0,
7554 "%<~auto%> only available with "
7555 "-std=c++14 or -std=gnu++14");
7556 cp_lexer_consume_token (parser
->lexer
);
7557 cp_lexer_consume_token (parser
->lexer
);
7559 *type
= TREE_TYPE (object
);
7563 /* Assume that things will not work out. */
7564 *type
= error_mark_node
;
7566 /* Look for the optional `::' operator. */
7567 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7568 /* Look for the optional nested-name-specifier. */
7569 nested_name_specifier_p
7570 = (cp_parser_nested_name_specifier_opt (parser
,
7571 /*typename_keyword_p=*/false,
7572 /*check_dependency_p=*/true,
7574 /*is_declaration=*/false)
7576 /* Now, if we saw a nested-name-specifier, we might be doing the
7577 second production. */
7578 if (nested_name_specifier_p
7579 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7581 /* Consume the `template' keyword. */
7582 cp_lexer_consume_token (parser
->lexer
);
7583 /* Parse the template-id. */
7584 cp_parser_template_id (parser
,
7585 /*template_keyword_p=*/true,
7586 /*check_dependency_p=*/false,
7588 /*is_declaration=*/true);
7589 /* Look for the `::' token. */
7590 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7592 /* If the next token is not a `~', then there might be some
7593 additional qualification. */
7594 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7596 /* At this point, we're looking for "type-name :: ~". The type-name
7597 must not be a class-name, since this is a pseudo-destructor. So,
7598 it must be either an enum-name, or a typedef-name -- both of which
7599 are just identifiers. So, we peek ahead to check that the "::"
7600 and "~" tokens are present; if they are not, then we can avoid
7601 calling type_name. */
7602 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7603 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7604 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7606 cp_parser_error (parser
, "non-scalar type");
7610 /* Look for the type-name. */
7611 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7612 if (*scope
== error_mark_node
)
7615 /* Look for the `::' token. */
7616 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7621 /* Look for the `~'. */
7622 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7624 /* Once we see the ~, this has to be a pseudo-destructor. */
7625 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7626 cp_parser_commit_to_topmost_tentative_parse (parser
);
7628 /* Look for the type-name again. We are not responsible for
7629 checking that it matches the first type-name. */
7630 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7633 /* Parse a unary-expression.
7639 unary-operator cast-expression
7640 sizeof unary-expression
7642 alignof ( type-id ) [C++0x]
7649 __extension__ cast-expression
7650 __alignof__ unary-expression
7651 __alignof__ ( type-id )
7652 alignof unary-expression [C++0x]
7653 __real__ cast-expression
7654 __imag__ cast-expression
7656 sizeof ( type-id ) { initializer-list , [opt] }
7657 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7658 __alignof__ ( type-id ) { initializer-list , [opt] }
7660 ADDRESS_P is true iff the unary-expression is appearing as the
7661 operand of the `&' operator. CAST_P is true if this expression is
7662 the target of a cast.
7664 Returns a representation of the expression. */
7667 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7668 bool address_p
, bool cast_p
, bool decltype_p
)
7671 enum tree_code unary_operator
;
7673 /* Peek at the next token. */
7674 token
= cp_lexer_peek_token (parser
->lexer
);
7675 /* Some keywords give away the kind of expression. */
7676 if (token
->type
== CPP_KEYWORD
)
7678 enum rid keyword
= token
->keyword
;
7687 location_t first_loc
;
7689 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7690 /* Consume the token. */
7691 cp_lexer_consume_token (parser
->lexer
);
7692 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7693 /* Parse the operand. */
7694 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7696 if (TYPE_P (operand
))
7697 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7700 /* ISO C++ defines alignof only with types, not with
7701 expressions. So pedwarn if alignof is used with a non-
7702 type expression. However, __alignof__ is ok. */
7703 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7704 pedwarn (token
->location
, OPT_Wpedantic
,
7705 "ISO C++ does not allow %<alignof%> "
7708 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7710 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7711 SIZEOF_EXPR with the original operand. */
7712 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7714 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7716 if (!processing_template_decl
&& TYPE_P (operand
))
7718 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7719 build1 (NOP_EXPR
, operand
,
7721 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7724 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7725 TREE_SIDE_EFFECTS (ret
) = 0;
7726 TREE_READONLY (ret
) = 1;
7728 SET_EXPR_LOCATION (ret
, first_loc
);
7734 return cp_parser_new_expression (parser
);
7737 return cp_parser_delete_expression (parser
);
7741 /* The saved value of the PEDANTIC flag. */
7745 /* Save away the PEDANTIC flag. */
7746 cp_parser_extension_opt (parser
, &saved_pedantic
);
7747 /* Parse the cast-expression. */
7748 expr
= cp_parser_simple_cast_expression (parser
);
7749 /* Restore the PEDANTIC flag. */
7750 pedantic
= saved_pedantic
;
7760 /* Consume the `__real__' or `__imag__' token. */
7761 cp_lexer_consume_token (parser
->lexer
);
7762 /* Parse the cast-expression. */
7763 expression
= cp_parser_simple_cast_expression (parser
);
7764 /* Create the complete representation. */
7765 return build_x_unary_op (token
->location
,
7766 (keyword
== RID_REALPART
7767 ? REALPART_EXPR
: IMAGPART_EXPR
),
7769 tf_warning_or_error
);
7773 case RID_TRANSACTION_ATOMIC
:
7774 case RID_TRANSACTION_RELAXED
:
7775 return cp_parser_transaction_expression (parser
, keyword
);
7780 const char *saved_message
;
7781 bool saved_integral_constant_expression_p
;
7782 bool saved_non_integral_constant_expression_p
;
7783 bool saved_greater_than_is_operator_p
;
7785 cp_lexer_consume_token (parser
->lexer
);
7786 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7788 saved_message
= parser
->type_definition_forbidden_message
;
7789 parser
->type_definition_forbidden_message
7790 = G_("types may not be defined in %<noexcept%> expressions");
7792 saved_integral_constant_expression_p
7793 = parser
->integral_constant_expression_p
;
7794 saved_non_integral_constant_expression_p
7795 = parser
->non_integral_constant_expression_p
;
7796 parser
->integral_constant_expression_p
= false;
7798 saved_greater_than_is_operator_p
7799 = parser
->greater_than_is_operator_p
;
7800 parser
->greater_than_is_operator_p
= true;
7802 ++cp_unevaluated_operand
;
7803 ++c_inhibit_evaluation_warnings
;
7804 ++cp_noexcept_operand
;
7805 expr
= cp_parser_expression (parser
);
7806 --cp_noexcept_operand
;
7807 --c_inhibit_evaluation_warnings
;
7808 --cp_unevaluated_operand
;
7810 parser
->greater_than_is_operator_p
7811 = saved_greater_than_is_operator_p
;
7813 parser
->integral_constant_expression_p
7814 = saved_integral_constant_expression_p
;
7815 parser
->non_integral_constant_expression_p
7816 = saved_non_integral_constant_expression_p
;
7818 parser
->type_definition_forbidden_message
= saved_message
;
7820 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7821 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7829 /* Look for the `:: new' and `:: delete', which also signal the
7830 beginning of a new-expression, or delete-expression,
7831 respectively. If the next token is `::', then it might be one of
7833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7837 /* See if the token after the `::' is one of the keywords in
7838 which we're interested. */
7839 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7840 /* If it's `new', we have a new-expression. */
7841 if (keyword
== RID_NEW
)
7842 return cp_parser_new_expression (parser
);
7843 /* Similarly, for `delete'. */
7844 else if (keyword
== RID_DELETE
)
7845 return cp_parser_delete_expression (parser
);
7848 /* Look for a unary operator. */
7849 unary_operator
= cp_parser_unary_operator (token
);
7850 /* The `++' and `--' operators can be handled similarly, even though
7851 they are not technically unary-operators in the grammar. */
7852 if (unary_operator
== ERROR_MARK
)
7854 if (token
->type
== CPP_PLUS_PLUS
)
7855 unary_operator
= PREINCREMENT_EXPR
;
7856 else if (token
->type
== CPP_MINUS_MINUS
)
7857 unary_operator
= PREDECREMENT_EXPR
;
7858 /* Handle the GNU address-of-label extension. */
7859 else if (cp_parser_allow_gnu_extensions_p (parser
)
7860 && token
->type
== CPP_AND_AND
)
7864 location_t start_loc
= token
->location
;
7866 /* Consume the '&&' token. */
7867 cp_lexer_consume_token (parser
->lexer
);
7868 /* Look for the identifier. */
7869 location_t finish_loc
7870 = get_finish (cp_lexer_peek_token (parser
->lexer
)->location
);
7871 identifier
= cp_parser_identifier (parser
);
7872 /* Construct a location of the form:
7875 with caret==start at the "&&", finish at the end of the label. */
7876 location_t combined_loc
7877 = make_location (start_loc
, start_loc
, finish_loc
);
7878 /* Create an expression representing the address. */
7879 expression
= finish_label_address_expr (identifier
, combined_loc
);
7880 if (cp_parser_non_integral_constant_expression (parser
,
7882 expression
= error_mark_node
;
7886 if (unary_operator
!= ERROR_MARK
)
7888 cp_expr cast_expression
;
7889 cp_expr expression
= error_mark_node
;
7890 non_integral_constant non_constant_p
= NIC_NONE
;
7891 location_t loc
= token
->location
;
7892 tsubst_flags_t complain
= complain_flags (decltype_p
);
7894 /* Consume the operator token. */
7895 token
= cp_lexer_consume_token (parser
->lexer
);
7896 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
7898 /* Parse the cast-expression. */
7900 = cp_parser_cast_expression (parser
,
7901 unary_operator
== ADDR_EXPR
,
7907 OP_TOKEN CAST_EXPRESSION
7908 ^~~~~~~~~~~~~~~~~~~~~~~~~
7909 with start==caret at the operator token, and
7910 extending to the end of the cast_expression. */
7911 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
7913 /* Now, build an appropriate representation. */
7914 switch (unary_operator
)
7917 non_constant_p
= NIC_STAR
;
7918 expression
= build_x_indirect_ref (loc
, cast_expression
,
7921 /* TODO: build_x_indirect_ref does not always honor the
7922 location, so ensure it is set. */
7923 expression
.set_location (loc
);
7927 non_constant_p
= NIC_ADDR
;
7930 expression
= build_x_unary_op (loc
, unary_operator
,
7933 /* TODO: build_x_unary_op does not always honor the location,
7934 so ensure it is set. */
7935 expression
.set_location (loc
);
7938 case PREINCREMENT_EXPR
:
7939 case PREDECREMENT_EXPR
:
7940 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7941 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7944 /* Immediately fold negation of a constant, unless the constant is 0
7945 (since -0 == 0) or it would overflow. */
7946 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
7947 && CONSTANT_CLASS_P (cast_expression
)
7948 && !integer_zerop (cast_expression
)
7949 && !TREE_OVERFLOW (cast_expression
))
7951 tree folded
= fold_build1 (unary_operator
,
7952 TREE_TYPE (cast_expression
),
7954 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
7956 expression
= cp_expr (folded
, loc
);
7961 case UNARY_PLUS_EXPR
:
7962 case TRUTH_NOT_EXPR
:
7963 expression
= finish_unary_op_expr (loc
, unary_operator
,
7964 cast_expression
, complain
);
7971 if (non_constant_p
!= NIC_NONE
7972 && cp_parser_non_integral_constant_expression (parser
,
7974 expression
= error_mark_node
;
7979 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7980 /*member_access_only_p=*/false,
7985 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7986 unary-operator, the corresponding tree code is returned. */
7988 static enum tree_code
7989 cp_parser_unary_operator (cp_token
* token
)
7991 switch (token
->type
)
7994 return INDIRECT_REF
;
8000 return UNARY_PLUS_EXPR
;
8006 return TRUTH_NOT_EXPR
;
8009 return BIT_NOT_EXPR
;
8016 /* Parse a new-expression.
8019 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8020 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8022 Returns a representation of the expression. */
8025 cp_parser_new_expression (cp_parser
* parser
)
8027 bool global_scope_p
;
8028 vec
<tree
, va_gc
> *placement
;
8030 vec
<tree
, va_gc
> *initializer
;
8031 tree nelts
= NULL_TREE
;
8034 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8036 /* Look for the optional `::' operator. */
8038 = (cp_parser_global_scope_opt (parser
,
8039 /*current_scope_valid_p=*/false)
8041 /* Look for the `new' operator. */
8042 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8043 /* There's no easy way to tell a new-placement from the
8044 `( type-id )' construct. */
8045 cp_parser_parse_tentatively (parser
);
8046 /* Look for a new-placement. */
8047 placement
= cp_parser_new_placement (parser
);
8048 /* If that didn't work out, there's no new-placement. */
8049 if (!cp_parser_parse_definitely (parser
))
8051 if (placement
!= NULL
)
8052 release_tree_vector (placement
);
8056 /* If the next token is a `(', then we have a parenthesized
8058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8061 const char *saved_message
= parser
->type_definition_forbidden_message
;
8063 /* Consume the `('. */
8064 cp_lexer_consume_token (parser
->lexer
);
8066 /* Parse the type-id. */
8067 parser
->type_definition_forbidden_message
8068 = G_("types may not be defined in a new-expression");
8070 type_id_in_expr_sentinel
s (parser
);
8071 type
= cp_parser_type_id (parser
);
8073 parser
->type_definition_forbidden_message
= saved_message
;
8075 /* Look for the closing `)'. */
8076 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8077 token
= cp_lexer_peek_token (parser
->lexer
);
8078 /* There should not be a direct-new-declarator in this production,
8079 but GCC used to allowed this, so we check and emit a sensible error
8080 message for this case. */
8081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8083 error_at (token
->location
,
8084 "array bound forbidden after parenthesized type-id");
8085 inform (token
->location
,
8086 "try removing the parentheses around the type-id");
8087 cp_parser_direct_new_declarator (parser
);
8090 /* Otherwise, there must be a new-type-id. */
8092 type
= cp_parser_new_type_id (parser
, &nelts
);
8094 /* If the next token is a `(' or '{', then we have a new-initializer. */
8095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8096 if (token
->type
== CPP_OPEN_PAREN
8097 || token
->type
== CPP_OPEN_BRACE
)
8098 initializer
= cp_parser_new_initializer (parser
);
8102 /* A new-expression may not appear in an integral constant
8104 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8105 ret
= error_mark_node
;
8106 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8107 of a new-type-id or type-id of a new-expression, the new-expression shall
8108 contain a new-initializer of the form ( assignment-expression )".
8109 Additionally, consistently with the spirit of DR 1467, we want to accept
8110 'new auto { 2 }' too. */
8111 else if (type_uses_auto (type
)
8112 && (vec_safe_length (initializer
) != 1
8113 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8114 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8116 error_at (token
->location
,
8117 "initialization of new-expression for type %<auto%> "
8118 "requires exactly one element");
8119 ret
= error_mark_node
;
8123 /* Construct a location e.g.:
8126 with caret == start at the start of the "new" token, and the end
8127 at the end of the final token we consumed. */
8128 cp_token
*end_tok
= cp_lexer_previous_token (parser
->lexer
);
8129 location_t end_loc
= get_finish (end_tok
->location
);
8130 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
8132 /* Create a representation of the new-expression. */
8133 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8134 tf_warning_or_error
);
8135 protected_set_expr_location (ret
, combined_loc
);
8138 if (placement
!= NULL
)
8139 release_tree_vector (placement
);
8140 if (initializer
!= NULL
)
8141 release_tree_vector (initializer
);
8146 /* Parse a new-placement.
8151 Returns the same representation as for an expression-list. */
8153 static vec
<tree
, va_gc
> *
8154 cp_parser_new_placement (cp_parser
* parser
)
8156 vec
<tree
, va_gc
> *expression_list
;
8158 /* Parse the expression-list. */
8159 expression_list
= (cp_parser_parenthesized_expression_list
8160 (parser
, non_attr
, /*cast_p=*/false,
8161 /*allow_expansion_p=*/true,
8162 /*non_constant_p=*/NULL
));
8164 if (expression_list
&& expression_list
->is_empty ())
8165 error ("expected expression-list or type-id");
8167 return expression_list
;
8170 /* Parse a new-type-id.
8173 type-specifier-seq new-declarator [opt]
8175 Returns the TYPE allocated. If the new-type-id indicates an array
8176 type, *NELTS is set to the number of elements in the last array
8177 bound; the TYPE will not include the last array bound. */
8180 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8182 cp_decl_specifier_seq type_specifier_seq
;
8183 cp_declarator
*new_declarator
;
8184 cp_declarator
*declarator
;
8185 cp_declarator
*outer_declarator
;
8186 const char *saved_message
;
8188 /* The type-specifier sequence must not contain type definitions.
8189 (It cannot contain declarations of new types either, but if they
8190 are not definitions we will catch that because they are not
8192 saved_message
= parser
->type_definition_forbidden_message
;
8193 parser
->type_definition_forbidden_message
8194 = G_("types may not be defined in a new-type-id");
8195 /* Parse the type-specifier-seq. */
8196 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
8197 /*is_trailing_return=*/false,
8198 &type_specifier_seq
);
8199 /* Restore the old message. */
8200 parser
->type_definition_forbidden_message
= saved_message
;
8202 if (type_specifier_seq
.type
== error_mark_node
)
8203 return error_mark_node
;
8205 /* Parse the new-declarator. */
8206 new_declarator
= cp_parser_new_declarator_opt (parser
);
8208 /* Determine the number of elements in the last array dimension, if
8211 /* Skip down to the last array dimension. */
8212 declarator
= new_declarator
;
8213 outer_declarator
= NULL
;
8214 while (declarator
&& (declarator
->kind
== cdk_pointer
8215 || declarator
->kind
== cdk_ptrmem
))
8217 outer_declarator
= declarator
;
8218 declarator
= declarator
->declarator
;
8221 && declarator
->kind
== cdk_array
8222 && declarator
->declarator
8223 && declarator
->declarator
->kind
== cdk_array
)
8225 outer_declarator
= declarator
;
8226 declarator
= declarator
->declarator
;
8229 if (declarator
&& declarator
->kind
== cdk_array
)
8231 *nelts
= declarator
->u
.array
.bounds
;
8232 if (*nelts
== error_mark_node
)
8233 *nelts
= integer_one_node
;
8235 if (outer_declarator
)
8236 outer_declarator
->declarator
= declarator
->declarator
;
8238 new_declarator
= NULL
;
8241 return groktypename (&type_specifier_seq
, new_declarator
, false);
8244 /* Parse an (optional) new-declarator.
8247 ptr-operator new-declarator [opt]
8248 direct-new-declarator
8250 Returns the declarator. */
8252 static cp_declarator
*
8253 cp_parser_new_declarator_opt (cp_parser
* parser
)
8255 enum tree_code code
;
8256 tree type
, std_attributes
= NULL_TREE
;
8257 cp_cv_quals cv_quals
;
8259 /* We don't know if there's a ptr-operator next, or not. */
8260 cp_parser_parse_tentatively (parser
);
8261 /* Look for a ptr-operator. */
8262 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8263 /* If that worked, look for more new-declarators. */
8264 if (cp_parser_parse_definitely (parser
))
8266 cp_declarator
*declarator
;
8268 /* Parse another optional declarator. */
8269 declarator
= cp_parser_new_declarator_opt (parser
);
8271 declarator
= cp_parser_make_indirect_declarator
8272 (code
, type
, cv_quals
, declarator
, std_attributes
);
8277 /* If the next token is a `[', there is a direct-new-declarator. */
8278 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8279 return cp_parser_direct_new_declarator (parser
);
8284 /* Parse a direct-new-declarator.
8286 direct-new-declarator:
8288 direct-new-declarator [constant-expression]
8292 static cp_declarator
*
8293 cp_parser_direct_new_declarator (cp_parser
* parser
)
8295 cp_declarator
*declarator
= NULL
;
8302 /* Look for the opening `['. */
8303 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8305 token
= cp_lexer_peek_token (parser
->lexer
);
8306 expression
= cp_parser_expression (parser
);
8307 /* The standard requires that the expression have integral
8308 type. DR 74 adds enumeration types. We believe that the
8309 real intent is that these expressions be handled like the
8310 expression in a `switch' condition, which also allows
8311 classes with a single conversion to integral or
8312 enumeration type. */
8313 if (!processing_template_decl
)
8316 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8321 error_at (token
->location
,
8322 "expression in new-declarator must have integral "
8323 "or enumeration type");
8324 expression
= error_mark_node
;
8328 /* Look for the closing `]'. */
8329 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8331 /* Add this bound to the declarator. */
8332 declarator
= make_array_declarator (declarator
, expression
);
8334 /* If the next token is not a `[', then there are no more
8336 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8343 /* Parse a new-initializer.
8346 ( expression-list [opt] )
8349 Returns a representation of the expression-list. */
8351 static vec
<tree
, va_gc
> *
8352 cp_parser_new_initializer (cp_parser
* parser
)
8354 vec
<tree
, va_gc
> *expression_list
;
8356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8359 bool expr_non_constant_p
;
8360 cp_lexer_set_source_position (parser
->lexer
);
8361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8362 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8363 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8364 expression_list
= make_tree_vector_single (t
);
8367 expression_list
= (cp_parser_parenthesized_expression_list
8368 (parser
, non_attr
, /*cast_p=*/false,
8369 /*allow_expansion_p=*/true,
8370 /*non_constant_p=*/NULL
));
8372 return expression_list
;
8375 /* Parse a delete-expression.
8378 :: [opt] delete cast-expression
8379 :: [opt] delete [ ] cast-expression
8381 Returns a representation of the expression. */
8384 cp_parser_delete_expression (cp_parser
* parser
)
8386 bool global_scope_p
;
8390 /* Look for the optional `::' operator. */
8392 = (cp_parser_global_scope_opt (parser
,
8393 /*current_scope_valid_p=*/false)
8395 /* Look for the `delete' keyword. */
8396 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8397 /* See if the array syntax is in use. */
8398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8400 /* Consume the `[' token. */
8401 cp_lexer_consume_token (parser
->lexer
);
8402 /* Look for the `]' token. */
8403 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8404 /* Remember that this is the `[]' construct. */
8410 /* Parse the cast-expression. */
8411 expression
= cp_parser_simple_cast_expression (parser
);
8413 /* A delete-expression may not appear in an integral constant
8415 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8416 return error_mark_node
;
8418 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8419 tf_warning_or_error
);
8422 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8423 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8427 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8429 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8430 switch (token
->type
)
8436 case CPP_CLOSE_SQUARE
:
8437 case CPP_CLOSE_PAREN
:
8438 case CPP_CLOSE_BRACE
:
8439 case CPP_OPEN_BRACE
:
8443 case CPP_DEREF_STAR
:
8451 case CPP_GREATER_EQ
:
8472 case CPP_OPEN_PAREN
:
8473 /* In ((type ()) () the last () isn't a valid cast-expression,
8474 so the whole must be parsed as postfix-expression. */
8475 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8478 case CPP_OPEN_SQUARE
:
8479 /* '[' may start a primary-expression in obj-c++ and in C++11,
8480 as a lambda-expression, eg, '(void)[]{}'. */
8481 if (cxx_dialect
>= cxx11
)
8483 return c_dialect_objc ();
8486 case CPP_MINUS_MINUS
:
8487 /* '++' and '--' may or may not start a cast-expression:
8489 struct T { void operator++(int); };
8490 void f() { (T())++; }
8503 /* Parse a cast-expression.
8507 ( type-id ) cast-expression
8509 ADDRESS_P is true iff the unary-expression is appearing as the
8510 operand of the `&' operator. CAST_P is true if this expression is
8511 the target of a cast.
8513 Returns a representation of the expression. */
8516 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8517 bool decltype_p
, cp_id_kind
* pidk
)
8519 /* If it's a `(', then we might be looking at a cast. */
8520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8522 tree type
= NULL_TREE
;
8523 cp_expr
expr (NULL_TREE
);
8524 int cast_expression
= 0;
8525 const char *saved_message
;
8527 /* There's no way to know yet whether or not this is a cast.
8528 For example, `(int (3))' is a unary-expression, while `(int)
8529 3' is a cast. So, we resort to parsing tentatively. */
8530 cp_parser_parse_tentatively (parser
);
8531 /* Types may not be defined in a cast. */
8532 saved_message
= parser
->type_definition_forbidden_message
;
8533 parser
->type_definition_forbidden_message
8534 = G_("types may not be defined in casts");
8535 /* Consume the `('. */
8536 cp_token
*open_paren
= cp_lexer_consume_token (parser
->lexer
);
8537 location_t open_paren_loc
= open_paren
->location
;
8539 /* A very tricky bit is that `(struct S) { 3 }' is a
8540 compound-literal (which we permit in C++ as an extension).
8541 But, that construct is not a cast-expression -- it is a
8542 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8543 is legal; if the compound-literal were a cast-expression,
8544 you'd need an extra set of parentheses.) But, if we parse
8545 the type-id, and it happens to be a class-specifier, then we
8546 will commit to the parse at that point, because we cannot
8547 undo the action that is done when creating a new class. So,
8548 then we cannot back up and do a postfix-expression.
8550 Another tricky case is the following (c++/29234):
8552 struct S { void operator () (); };
8559 As a type-id we parse the parenthesized S()() as a function
8560 returning a function, groktypename complains and we cannot
8561 back up in this case either.
8563 Therefore, we scan ahead to the closing `)', and check to see
8564 if the tokens after the `)' can start a cast-expression. Otherwise
8565 we are dealing with an unary-expression, a postfix-expression
8568 Yet another tricky case, in C++11, is the following (c++/54891):
8572 The issue is that usually, besides the case of lambda-expressions,
8573 the parenthesized type-id cannot be followed by '[', and, eg, we
8574 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8575 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8576 we don't commit, we try a cast-expression, then an unary-expression.
8578 Save tokens so that we can put them back. */
8579 cp_lexer_save_tokens (parser
->lexer
);
8581 /* We may be looking at a cast-expression. */
8582 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8583 /*consume_paren=*/true))
8585 = cp_parser_tokens_start_cast_expression (parser
);
8587 /* Roll back the tokens we skipped. */
8588 cp_lexer_rollback_tokens (parser
->lexer
);
8589 /* If we aren't looking at a cast-expression, simulate an error so
8590 that the call to cp_parser_error_occurred below returns true. */
8591 if (!cast_expression
)
8592 cp_parser_simulate_error (parser
);
8595 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8596 parser
->in_type_id_in_expr_p
= true;
8597 /* Look for the type-id. */
8598 type
= cp_parser_type_id (parser
);
8599 /* Look for the closing `)'. */
8600 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8601 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8604 /* Restore the saved message. */
8605 parser
->type_definition_forbidden_message
= saved_message
;
8607 /* At this point this can only be either a cast or a
8608 parenthesized ctor such as `(T ())' that looks like a cast to
8609 function returning T. */
8610 if (!cp_parser_error_occurred (parser
))
8612 /* Only commit if the cast-expression doesn't start with
8613 '++', '--', or '[' in C++11. */
8614 if (cast_expression
> 0)
8615 cp_parser_commit_to_topmost_tentative_parse (parser
);
8617 expr
= cp_parser_cast_expression (parser
,
8618 /*address_p=*/false,
8620 /*decltype_p=*/false,
8623 if (cp_parser_parse_definitely (parser
))
8625 /* Warn about old-style casts, if so requested. */
8626 if (warn_old_style_cast
8627 && !in_system_header_at (input_location
)
8628 && !VOID_TYPE_P (type
)
8629 && current_lang_name
!= lang_name_c
)
8630 warning (OPT_Wold_style_cast
, "use of old-style cast");
8632 /* Only type conversions to integral or enumeration types
8633 can be used in constant-expressions. */
8634 if (!cast_valid_in_integral_constant_expression_p (type
)
8635 && cp_parser_non_integral_constant_expression (parser
,
8637 return error_mark_node
;
8639 /* Perform the cast. */
8643 with start==caret at the open paren, extending to the
8645 location_t cast_loc
= make_location (open_paren_loc
,
8647 expr
.get_finish ());
8648 expr
= build_c_cast (cast_loc
, type
, expr
);
8653 cp_parser_abort_tentative_parse (parser
);
8656 /* If we get here, then it's not a cast, so it must be a
8657 unary-expression. */
8658 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8659 cast_p
, decltype_p
);
8662 /* Parse a binary expression of the general form:
8666 pm-expression .* cast-expression
8667 pm-expression ->* cast-expression
8669 multiplicative-expression:
8671 multiplicative-expression * pm-expression
8672 multiplicative-expression / pm-expression
8673 multiplicative-expression % pm-expression
8675 additive-expression:
8676 multiplicative-expression
8677 additive-expression + multiplicative-expression
8678 additive-expression - multiplicative-expression
8682 shift-expression << additive-expression
8683 shift-expression >> additive-expression
8685 relational-expression:
8687 relational-expression < shift-expression
8688 relational-expression > shift-expression
8689 relational-expression <= shift-expression
8690 relational-expression >= shift-expression
8694 relational-expression:
8695 relational-expression <? shift-expression
8696 relational-expression >? shift-expression
8698 equality-expression:
8699 relational-expression
8700 equality-expression == relational-expression
8701 equality-expression != relational-expression
8705 and-expression & equality-expression
8707 exclusive-or-expression:
8709 exclusive-or-expression ^ and-expression
8711 inclusive-or-expression:
8712 exclusive-or-expression
8713 inclusive-or-expression | exclusive-or-expression
8715 logical-and-expression:
8716 inclusive-or-expression
8717 logical-and-expression && inclusive-or-expression
8719 logical-or-expression:
8720 logical-and-expression
8721 logical-or-expression || logical-and-expression
8723 All these are implemented with a single function like:
8726 simple-cast-expression
8727 binary-expression <token> binary-expression
8729 CAST_P is true if this expression is the target of a cast.
8731 The binops_by_token map is used to get the tree codes for each <token> type.
8732 binary-expressions are associated according to a precedence table. */
8734 #define TOKEN_PRECEDENCE(token) \
8735 (((token->type == CPP_GREATER \
8736 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8737 && !parser->greater_than_is_operator_p) \
8738 ? PREC_NOT_OPERATOR \
8739 : binops_by_token[token->type].prec)
8742 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8743 bool no_toplevel_fold_p
,
8745 enum cp_parser_prec prec
,
8748 cp_parser_expression_stack stack
;
8749 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8750 cp_parser_expression_stack_entry current
;
8753 enum tree_code rhs_type
;
8754 enum cp_parser_prec new_prec
, lookahead_prec
;
8757 /* Parse the first expression. */
8758 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8759 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8760 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8761 cast_p
, decltype_p
, pidk
);
8762 current
.prec
= prec
;
8764 if (cp_parser_error_occurred (parser
))
8765 return error_mark_node
;
8769 /* Get an operator token. */
8770 token
= cp_lexer_peek_token (parser
->lexer
);
8772 if (warn_cxx11_compat
8773 && token
->type
== CPP_RSHIFT
8774 && !parser
->greater_than_is_operator_p
)
8776 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8777 "%<>>%> operator is treated"
8778 " as two right angle brackets in C++11"))
8779 inform (token
->location
,
8780 "suggest parentheses around %<>>%> expression");
8783 new_prec
= TOKEN_PRECEDENCE (token
);
8784 if (new_prec
!= PREC_NOT_OPERATOR
8785 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8786 /* This is a fold-expression; handle it later. */
8787 new_prec
= PREC_NOT_OPERATOR
;
8789 /* Popping an entry off the stack means we completed a subexpression:
8790 - either we found a token which is not an operator (`>' where it is not
8791 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8792 will happen repeatedly;
8793 - or, we found an operator which has lower priority. This is the case
8794 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8796 if (new_prec
<= current
.prec
)
8805 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8806 current
.loc
= token
->location
;
8808 /* We used the operator token. */
8809 cp_lexer_consume_token (parser
->lexer
);
8811 /* For "false && x" or "true || x", x will never be executed;
8812 disable warnings while evaluating it. */
8813 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8814 c_inhibit_evaluation_warnings
+=
8815 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8816 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8817 c_inhibit_evaluation_warnings
+=
8818 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8820 /* Extract another operand. It may be the RHS of this expression
8821 or the LHS of a new, higher priority expression. */
8822 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8823 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8824 rhs
= cp_parser_simple_cast_expression (parser
);
8826 /* Get another operator token. Look up its precedence to avoid
8827 building a useless (immediately popped) stack entry for common
8828 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8829 token
= cp_lexer_peek_token (parser
->lexer
);
8830 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8831 if (lookahead_prec
!= PREC_NOT_OPERATOR
8832 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8833 lookahead_prec
= PREC_NOT_OPERATOR
;
8834 if (lookahead_prec
> new_prec
)
8836 /* ... and prepare to parse the RHS of the new, higher priority
8837 expression. Since precedence levels on the stack are
8838 monotonically increasing, we do not have to care about
8843 current
.lhs_type
= rhs_type
;
8844 current
.prec
= new_prec
;
8845 new_prec
= lookahead_prec
;
8849 lookahead_prec
= new_prec
;
8850 /* If the stack is not empty, we have parsed into LHS the right side
8851 (`4' in the example above) of an expression we had suspended.
8852 We can use the information on the stack to recover the LHS (`3')
8853 from the stack together with the tree code (`MULT_EXPR'), and
8854 the precedence of the higher level subexpression
8855 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8856 which will be used to actually build the additive expression. */
8858 rhs_type
= current
.lhs_type
;
8863 /* Undo the disabling of warnings done above. */
8864 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8865 c_inhibit_evaluation_warnings
-=
8866 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8867 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8868 c_inhibit_evaluation_warnings
-=
8869 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8871 if (warn_logical_not_paren
8872 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8873 && current
.lhs_type
== TRUTH_NOT_EXPR
8874 /* Avoid warning for !!x == y. */
8875 && (TREE_CODE (current
.lhs
) != NE_EXPR
8876 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8877 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8878 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8879 /* Avoid warning for !b == y where b is boolean. */
8880 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
8881 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
8883 /* Avoid warning for !!b == y where b is boolean. */
8884 && (!DECL_P (current
.lhs
)
8885 || TREE_TYPE (current
.lhs
) == NULL_TREE
8886 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
8887 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
8888 maybe_constant_value (rhs
));
8892 location_t combined_loc
= make_location (current
.loc
,
8893 current
.lhs
.get_start (),
8896 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8897 ERROR_MARK for everything that is not a binary expression.
8898 This makes warn_about_parentheses miss some warnings that
8899 involve unary operators. For unary expressions we should
8900 pass the correct tree_code unless the unary expression was
8901 surrounded by parentheses.
8903 if (no_toplevel_fold_p
8904 && lookahead_prec
<= current
.prec
8906 current
.lhs
= build2_loc (combined_loc
,
8908 TREE_CODE_CLASS (current
.tree_type
)
8910 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8914 current
.lhs
= build_x_binary_op (combined_loc
, current
.tree_type
,
8915 current
.lhs
, current
.lhs_type
,
8916 rhs
, rhs_type
, &overload
,
8917 complain_flags (decltype_p
));
8918 /* TODO: build_x_binary_op doesn't always honor the location. */
8919 current
.lhs
.set_location (combined_loc
);
8921 current
.lhs_type
= current
.tree_type
;
8923 /* If the binary operator required the use of an overloaded operator,
8924 then this expression cannot be an integral constant-expression.
8925 An overloaded operator can be used even if both operands are
8926 otherwise permissible in an integral constant-expression if at
8927 least one of the operands is of enumeration type. */
8930 && cp_parser_non_integral_constant_expression (parser
,
8932 return error_mark_node
;
8939 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8940 bool no_toplevel_fold_p
,
8941 enum cp_parser_prec prec
,
8944 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8945 /*decltype*/false, prec
, pidk
);
8948 /* Parse the `? expression : assignment-expression' part of a
8949 conditional-expression. The LOGICAL_OR_EXPR is the
8950 logical-or-expression that started the conditional-expression.
8951 Returns a representation of the entire conditional-expression.
8953 This routine is used by cp_parser_assignment_expression.
8955 ? expression : assignment-expression
8959 ? : assignment-expression */
8962 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
8964 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
8965 cp_expr assignment_expr
;
8966 struct cp_token
*token
;
8967 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8969 /* Consume the `?' token. */
8970 cp_lexer_consume_token (parser
->lexer
);
8971 token
= cp_lexer_peek_token (parser
->lexer
);
8972 if (cp_parser_allow_gnu_extensions_p (parser
)
8973 && token
->type
== CPP_COLON
)
8975 pedwarn (token
->location
, OPT_Wpedantic
,
8976 "ISO C++ does not allow ?: with omitted middle operand");
8977 /* Implicit true clause. */
8979 c_inhibit_evaluation_warnings
+=
8980 folded_logical_or_expr
== truthvalue_true_node
;
8981 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8985 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8986 parser
->colon_corrects_to_scope_p
= false;
8987 /* Parse the expression. */
8988 c_inhibit_evaluation_warnings
+=
8989 folded_logical_or_expr
== truthvalue_false_node
;
8990 expr
= cp_parser_expression (parser
);
8991 c_inhibit_evaluation_warnings
+=
8992 ((folded_logical_or_expr
== truthvalue_true_node
)
8993 - (folded_logical_or_expr
== truthvalue_false_node
));
8994 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8997 /* The next token should be a `:'. */
8998 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8999 /* Parse the assignment-expression. */
9000 assignment_expr
= cp_parser_assignment_expression (parser
);
9001 c_inhibit_evaluation_warnings
-=
9002 folded_logical_or_expr
== truthvalue_true_node
;
9005 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9006 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9007 with the caret at the "?", ranging from the start of
9008 the logical_or_expr to the end of the assignment_expr. */
9009 loc
= make_location (loc
,
9010 logical_or_expr
.get_start (),
9011 assignment_expr
.get_finish ());
9013 /* Build the conditional-expression. */
9014 return build_x_conditional_expr (loc
, logical_or_expr
,
9017 tf_warning_or_error
);
9020 /* Parse an assignment-expression.
9022 assignment-expression:
9023 conditional-expression
9024 logical-or-expression assignment-operator assignment_expression
9027 CAST_P is true if this expression is the target of a cast.
9028 DECLTYPE_P is true if this expression is the operand of decltype.
9030 Returns a representation for the expression. */
9033 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9034 bool cast_p
, bool decltype_p
)
9038 /* If the next token is the `throw' keyword, then we're looking at
9039 a throw-expression. */
9040 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9041 expr
= cp_parser_throw_expression (parser
);
9042 /* Otherwise, it must be that we are looking at a
9043 logical-or-expression. */
9046 /* Parse the binary expressions (logical-or-expression). */
9047 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9049 PREC_NOT_OPERATOR
, pidk
);
9050 /* If the next token is a `?' then we're actually looking at a
9051 conditional-expression. */
9052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9053 return cp_parser_question_colon_clause (parser
, expr
);
9056 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9058 /* If it's an assignment-operator, we're using the second
9060 enum tree_code assignment_operator
9061 = cp_parser_assignment_operator_opt (parser
);
9062 if (assignment_operator
!= ERROR_MARK
)
9064 bool non_constant_p
;
9066 /* Parse the right-hand side of the assignment. */
9067 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9070 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9071 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9073 /* An assignment may not appear in a
9074 constant-expression. */
9075 if (cp_parser_non_integral_constant_expression (parser
,
9077 return error_mark_node
;
9078 /* Build the assignment expression. Its default
9082 is the location of the '=' token as the
9083 caret, ranging from the start of the lhs to the
9085 loc
= make_location (loc
,
9088 expr
= build_x_modify_expr (loc
, expr
,
9089 assignment_operator
,
9091 complain_flags (decltype_p
));
9092 /* TODO: build_x_modify_expr doesn't honor the location,
9093 so we must set it here. */
9094 expr
.set_location (loc
);
9102 /* Parse an (optional) assignment-operator.
9104 assignment-operator: one of
9105 = *= /= %= += -= >>= <<= &= ^= |=
9109 assignment-operator: one of
9112 If the next token is an assignment operator, the corresponding tree
9113 code is returned, and the token is consumed. For example, for
9114 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9115 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9116 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9117 operator, ERROR_MARK is returned. */
9119 static enum tree_code
9120 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9125 /* Peek at the next token. */
9126 token
= cp_lexer_peek_token (parser
->lexer
);
9128 switch (token
->type
)
9139 op
= TRUNC_DIV_EXPR
;
9143 op
= TRUNC_MOD_EXPR
;
9175 /* Nothing else is an assignment operator. */
9179 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9180 if (op
!= ERROR_MARK
9181 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9184 /* If it was an assignment operator, consume it. */
9185 if (op
!= ERROR_MARK
)
9186 cp_lexer_consume_token (parser
->lexer
);
9191 /* Parse an expression.
9194 assignment-expression
9195 expression , assignment-expression
9197 CAST_P is true if this expression is the target of a cast.
9198 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9199 except possibly parenthesized or on the RHS of a comma (N3276).
9201 Returns a representation of the expression. */
9204 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9205 bool cast_p
, bool decltype_p
)
9207 cp_expr expression
= NULL_TREE
;
9208 location_t loc
= UNKNOWN_LOCATION
;
9212 cp_expr assignment_expression
;
9214 /* Parse the next assignment-expression. */
9215 assignment_expression
9216 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9218 /* We don't create a temporary for a call that is the immediate operand
9219 of decltype or on the RHS of a comma. But when we see a comma, we
9220 need to create a temporary for a call on the LHS. */
9221 if (decltype_p
&& !processing_template_decl
9222 && TREE_CODE (assignment_expression
) == CALL_EXPR
9223 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9224 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9225 assignment_expression
9226 = build_cplus_new (TREE_TYPE (assignment_expression
),
9227 assignment_expression
, tf_warning_or_error
);
9229 /* If this is the first assignment-expression, we can just
9232 expression
= assignment_expression
;
9235 /* Create a location with caret at the comma, ranging
9236 from the start of the LHS to the end of the RHS. */
9237 loc
= make_location (loc
,
9238 expression
.get_start (),
9239 assignment_expression
.get_finish ());
9240 expression
= build_x_compound_expr (loc
, expression
,
9241 assignment_expression
,
9242 complain_flags (decltype_p
));
9243 expression
.set_location (loc
);
9245 /* If the next token is not a comma, or we're in a fold-expression, then
9246 we are done with the expression. */
9247 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9248 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9250 /* Consume the `,'. */
9251 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9252 cp_lexer_consume_token (parser
->lexer
);
9253 /* A comma operator cannot appear in a constant-expression. */
9254 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9255 expression
= error_mark_node
;
9261 /* Parse a constant-expression.
9263 constant-expression:
9264 conditional-expression
9266 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9267 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9268 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9269 is false, NON_CONSTANT_P should be NULL. */
9272 cp_parser_constant_expression (cp_parser
* parser
,
9273 bool allow_non_constant_p
,
9274 bool *non_constant_p
)
9276 bool saved_integral_constant_expression_p
;
9277 bool saved_allow_non_integral_constant_expression_p
;
9278 bool saved_non_integral_constant_expression_p
;
9281 /* It might seem that we could simply parse the
9282 conditional-expression, and then check to see if it were
9283 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9284 one that the compiler can figure out is constant, possibly after
9285 doing some simplifications or optimizations. The standard has a
9286 precise definition of constant-expression, and we must honor
9287 that, even though it is somewhat more restrictive.
9293 is not a legal declaration, because `(2, 3)' is not a
9294 constant-expression. The `,' operator is forbidden in a
9295 constant-expression. However, GCC's constant-folding machinery
9296 will fold this operation to an INTEGER_CST for `3'. */
9298 /* Save the old settings. */
9299 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
9300 saved_allow_non_integral_constant_expression_p
9301 = parser
->allow_non_integral_constant_expression_p
;
9302 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
9303 /* We are now parsing a constant-expression. */
9304 parser
->integral_constant_expression_p
= true;
9305 parser
->allow_non_integral_constant_expression_p
9306 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
9307 parser
->non_integral_constant_expression_p
= false;
9308 /* Although the grammar says "conditional-expression", we parse an
9309 "assignment-expression", which also permits "throw-expression"
9310 and the use of assignment operators. In the case that
9311 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9312 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9313 actually essential that we look for an assignment-expression.
9314 For example, cp_parser_initializer_clauses uses this function to
9315 determine whether a particular assignment-expression is in fact
9317 expression
= cp_parser_assignment_expression (parser
);
9318 /* Restore the old settings. */
9319 parser
->integral_constant_expression_p
9320 = saved_integral_constant_expression_p
;
9321 parser
->allow_non_integral_constant_expression_p
9322 = saved_allow_non_integral_constant_expression_p
;
9323 if (cxx_dialect
>= cxx11
)
9325 /* Require an rvalue constant expression here; that's what our
9326 callers expect. Reference constant expressions are handled
9327 separately in e.g. cp_parser_template_argument. */
9328 bool is_const
= potential_rvalue_constant_expression (expression
);
9329 parser
->non_integral_constant_expression_p
= !is_const
;
9330 if (!is_const
&& !allow_non_constant_p
)
9331 require_potential_rvalue_constant_expression (expression
);
9333 if (allow_non_constant_p
)
9334 *non_constant_p
= parser
->non_integral_constant_expression_p
;
9335 parser
->non_integral_constant_expression_p
9336 = saved_non_integral_constant_expression_p
;
9341 /* Parse __builtin_offsetof.
9343 offsetof-expression:
9344 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9346 offsetof-member-designator:
9348 | offsetof-member-designator "." id-expression
9349 | offsetof-member-designator "[" expression "]"
9350 | offsetof-member-designator "->" id-expression */
9353 cp_parser_builtin_offsetof (cp_parser
*parser
)
9355 int save_ice_p
, save_non_ice_p
;
9360 location_t finish_loc
;
9362 /* We're about to accept non-integral-constant things, but will
9363 definitely yield an integral constant expression. Save and
9364 restore these values around our local parsing. */
9365 save_ice_p
= parser
->integral_constant_expression_p
;
9366 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9368 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9370 /* Consume the "__builtin_offsetof" token. */
9371 cp_lexer_consume_token (parser
->lexer
);
9372 /* Consume the opening `('. */
9373 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9374 /* Parse the type-id. */
9375 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9376 type
= cp_parser_type_id (parser
);
9377 /* Look for the `,'. */
9378 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9379 token
= cp_lexer_peek_token (parser
->lexer
);
9381 /* Build the (type *)null that begins the traditional offsetof macro. */
9382 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
9383 tf_warning_or_error
);
9385 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9386 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
9387 true, &dummy
, token
->location
);
9390 token
= cp_lexer_peek_token (parser
->lexer
);
9391 switch (token
->type
)
9393 case CPP_OPEN_SQUARE
:
9394 /* offsetof-member-designator "[" expression "]" */
9395 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9400 /* offsetof-member-designator "->" identifier */
9401 expr
= grok_array_decl (token
->location
, expr
,
9402 integer_zero_node
, false);
9406 /* offsetof-member-designator "." identifier */
9407 cp_lexer_consume_token (parser
->lexer
);
9408 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9413 case CPP_CLOSE_PAREN
:
9414 /* Consume the ")" token. */
9415 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9416 cp_lexer_consume_token (parser
->lexer
);
9420 /* Error. We know the following require will fail, but
9421 that gives the proper error message. */
9422 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9423 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9424 expr
= error_mark_node
;
9430 /* Make a location of the form:
9431 __builtin_offsetof (struct s, f)
9432 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9433 with caret at the type-id, ranging from the start of the
9434 "_builtin_offsetof" token to the close paren. */
9435 loc
= make_location (loc
, start_loc
, finish_loc
);
9436 /* The result will be an INTEGER_CST, so we need to explicitly
9437 preserve the location. */
9438 expr
= cp_expr (finish_offsetof (expr
, loc
), loc
);
9441 parser
->integral_constant_expression_p
= save_ice_p
;
9442 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9447 /* Parse a trait expression.
9449 Returns a representation of the expression, the underlying type
9450 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9453 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9456 tree type1
, type2
= NULL_TREE
;
9457 bool binary
= false;
9458 bool variadic
= false;
9462 case RID_HAS_NOTHROW_ASSIGN
:
9463 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9465 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9466 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9468 case RID_HAS_NOTHROW_COPY
:
9469 kind
= CPTK_HAS_NOTHROW_COPY
;
9471 case RID_HAS_TRIVIAL_ASSIGN
:
9472 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9474 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9475 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9477 case RID_HAS_TRIVIAL_COPY
:
9478 kind
= CPTK_HAS_TRIVIAL_COPY
;
9480 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9481 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9483 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9484 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9486 case RID_IS_ABSTRACT
:
9487 kind
= CPTK_IS_ABSTRACT
;
9489 case RID_IS_BASE_OF
:
9490 kind
= CPTK_IS_BASE_OF
;
9494 kind
= CPTK_IS_CLASS
;
9497 kind
= CPTK_IS_EMPTY
;
9500 kind
= CPTK_IS_ENUM
;
9503 kind
= CPTK_IS_FINAL
;
9505 case RID_IS_LITERAL_TYPE
:
9506 kind
= CPTK_IS_LITERAL_TYPE
;
9511 case RID_IS_POLYMORPHIC
:
9512 kind
= CPTK_IS_POLYMORPHIC
;
9514 case RID_IS_SAME_AS
:
9515 kind
= CPTK_IS_SAME_AS
;
9518 case RID_IS_STD_LAYOUT
:
9519 kind
= CPTK_IS_STD_LAYOUT
;
9521 case RID_IS_TRIVIAL
:
9522 kind
= CPTK_IS_TRIVIAL
;
9524 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9525 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9528 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9529 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9532 case RID_IS_TRIVIALLY_COPYABLE
:
9533 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9536 kind
= CPTK_IS_UNION
;
9538 case RID_UNDERLYING_TYPE
:
9539 kind
= CPTK_UNDERLYING_TYPE
;
9544 case RID_DIRECT_BASES
:
9545 kind
= CPTK_DIRECT_BASES
;
9551 /* Consume the token. */
9552 cp_lexer_consume_token (parser
->lexer
);
9554 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9557 type_id_in_expr_sentinel
s (parser
);
9558 type1
= cp_parser_type_id (parser
);
9561 if (type1
== error_mark_node
)
9562 return error_mark_node
;
9566 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9569 type_id_in_expr_sentinel
s (parser
);
9570 type2
= cp_parser_type_id (parser
);
9573 if (type2
== error_mark_node
)
9574 return error_mark_node
;
9578 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9580 cp_lexer_consume_token (parser
->lexer
);
9581 tree elt
= cp_parser_type_id (parser
);
9582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9584 cp_lexer_consume_token (parser
->lexer
);
9585 elt
= make_pack_expansion (elt
);
9587 if (elt
== error_mark_node
)
9588 return error_mark_node
;
9589 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9593 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9595 /* Complete the trait expression, which may mean either processing
9596 the trait expr now or saving it for template instantiation. */
9599 case CPTK_UNDERLYING_TYPE
:
9600 return finish_underlying_type (type1
);
9602 return finish_bases (type1
, false);
9603 case CPTK_DIRECT_BASES
:
9604 return finish_bases (type1
, true);
9606 return finish_trait_expr (kind
, type1
, type2
);
9610 /* Lambdas that appear in variable initializer or default argument scope
9611 get that in their mangling, so we need to record it. We might as well
9612 use the count for function and namespace scopes as well. */
9613 static GTY(()) tree lambda_scope
;
9614 static GTY(()) int lambda_count
;
9615 struct GTY(()) tree_int
9620 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9623 start_lambda_scope (tree decl
)
9627 /* Once we're inside a function, we ignore other scopes and just push
9628 the function again so that popping works properly. */
9629 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9630 decl
= current_function_decl
;
9631 ti
.t
= lambda_scope
;
9632 ti
.i
= lambda_count
;
9633 vec_safe_push (lambda_scope_stack
, ti
);
9634 if (lambda_scope
!= decl
)
9636 /* Don't reset the count if we're still in the same function. */
9637 lambda_scope
= decl
;
9643 record_lambda_scope (tree lambda
)
9645 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9646 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9650 finish_lambda_scope (void)
9652 tree_int
*p
= &lambda_scope_stack
->last ();
9653 if (lambda_scope
!= p
->t
)
9655 lambda_scope
= p
->t
;
9656 lambda_count
= p
->i
;
9658 lambda_scope_stack
->pop ();
9661 /* Parse a lambda expression.
9664 lambda-introducer lambda-declarator [opt] compound-statement
9666 Returns a representation of the expression. */
9669 cp_parser_lambda_expression (cp_parser
* parser
)
9671 tree lambda_expr
= build_lambda_expr ();
9674 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9675 cp_token_position start
= 0;
9677 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9679 if (cp_unevaluated_operand
)
9681 if (!token
->error_reported
)
9683 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9684 "lambda-expression in unevaluated context");
9685 token
->error_reported
= true;
9689 else if (parser
->in_template_argument_list_p
)
9691 if (!token
->error_reported
)
9693 error_at (token
->location
, "lambda-expression in template-argument");
9694 token
->error_reported
= true;
9699 /* We may be in the middle of deferred access check. Disable
9701 push_deferring_access_checks (dk_no_deferred
);
9703 cp_parser_lambda_introducer (parser
, lambda_expr
);
9705 type
= begin_lambda_type (lambda_expr
);
9706 if (type
== error_mark_node
)
9707 return error_mark_node
;
9709 record_lambda_scope (lambda_expr
);
9711 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9712 determine_visibility (TYPE_NAME (type
));
9714 /* Now that we've started the type, add the capture fields for any
9715 explicit captures. */
9716 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9719 /* Inside the class, surrounding template-parameter-lists do not apply. */
9720 unsigned int saved_num_template_parameter_lists
9721 = parser
->num_template_parameter_lists
;
9722 unsigned char in_statement
= parser
->in_statement
;
9723 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9724 bool fully_implicit_function_template_p
9725 = parser
->fully_implicit_function_template_p
;
9726 tree implicit_template_parms
= parser
->implicit_template_parms
;
9727 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9728 bool auto_is_implicit_function_template_parm_p
9729 = parser
->auto_is_implicit_function_template_parm_p
;
9731 parser
->num_template_parameter_lists
= 0;
9732 parser
->in_statement
= 0;
9733 parser
->in_switch_statement_p
= false;
9734 parser
->fully_implicit_function_template_p
= false;
9735 parser
->implicit_template_parms
= 0;
9736 parser
->implicit_template_scope
= 0;
9737 parser
->auto_is_implicit_function_template_parm_p
= false;
9739 /* By virtue of defining a local class, a lambda expression has access to
9740 the private variables of enclosing classes. */
9742 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9746 if (!cp_parser_error_occurred (parser
)
9747 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9748 && cp_parser_start_tentative_firewall (parser
))
9750 cp_parser_lambda_body (parser
, lambda_expr
);
9752 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9754 if (cp_parser_skip_to_closing_brace (parser
))
9755 cp_lexer_consume_token (parser
->lexer
);
9758 /* The capture list was built up in reverse order; fix that now. */
9759 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9760 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9763 maybe_add_lambda_conv_op (type
);
9765 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9767 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9768 parser
->in_statement
= in_statement
;
9769 parser
->in_switch_statement_p
= in_switch_statement_p
;
9770 parser
->fully_implicit_function_template_p
9771 = fully_implicit_function_template_p
;
9772 parser
->implicit_template_parms
= implicit_template_parms
;
9773 parser
->implicit_template_scope
= implicit_template_scope
;
9774 parser
->auto_is_implicit_function_template_parm_p
9775 = auto_is_implicit_function_template_parm_p
;
9778 /* This field is only used during parsing of the lambda. */
9779 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9781 /* This lambda shouldn't have any proxies left at this point. */
9782 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9783 /* And now that we're done, push proxies for an enclosing lambda. */
9784 insert_pending_capture_proxies ();
9787 lambda_expr
= build_lambda_object (lambda_expr
);
9789 lambda_expr
= error_mark_node
;
9791 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9793 pop_deferring_access_checks ();
9798 /* Parse the beginning of a lambda expression.
9801 [ lambda-capture [opt] ]
9803 LAMBDA_EXPR is the current representation of the lambda expression. */
9806 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9808 /* Need commas after the first capture. */
9811 /* Eat the leading `['. */
9812 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9814 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9816 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9817 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9818 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9819 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9821 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9823 cp_lexer_consume_token (parser
->lexer
);
9827 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9829 cp_token
* capture_token
;
9831 tree capture_init_expr
;
9832 cp_id_kind idk
= CP_ID_KIND_NONE
;
9833 bool explicit_init_p
= false;
9835 enum capture_kind_type
9840 enum capture_kind_type capture_kind
= BY_COPY
;
9842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9844 error ("expected end of capture-list");
9851 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9853 /* Possibly capture `this'. */
9854 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9856 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9857 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9858 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9859 "with by-copy capture default");
9860 cp_lexer_consume_token (parser
->lexer
);
9861 add_capture (lambda_expr
,
9862 /*id=*/this_identifier
,
9863 /*initializer=*/finish_this_expr(),
9864 /*by_reference_p=*/false,
9869 /* Remember whether we want to capture as a reference or not. */
9870 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9872 capture_kind
= BY_REFERENCE
;
9873 cp_lexer_consume_token (parser
->lexer
);
9876 /* Get the identifier. */
9877 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9878 capture_id
= cp_parser_identifier (parser
);
9880 if (capture_id
== error_mark_node
)
9881 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9882 delimiters, but I modified this to stop on unnested ']' as well. It
9883 was already changed to stop on unnested '}', so the
9884 "closing_parenthesis" name is no more misleading with my change. */
9886 cp_parser_skip_to_closing_parenthesis (parser
,
9887 /*recovering=*/true,
9889 /*consume_paren=*/true);
9893 /* Find the initializer for this capture. */
9894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9895 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9896 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9898 bool direct
, non_constant
;
9899 /* An explicit initializer exists. */
9900 if (cxx_dialect
< cxx14
)
9901 pedwarn (input_location
, 0,
9902 "lambda capture initializers "
9903 "only available with -std=c++14 or -std=gnu++14");
9904 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9906 explicit_init_p
= true;
9907 if (capture_init_expr
== NULL_TREE
)
9909 error ("empty initializer for lambda init-capture");
9910 capture_init_expr
= error_mark_node
;
9915 const char* error_msg
;
9917 /* Turn the identifier into an id-expression. */
9919 = cp_parser_lookup_name_simple (parser
, capture_id
,
9920 capture_token
->location
);
9922 if (capture_init_expr
== error_mark_node
)
9924 unqualified_name_lookup_error (capture_id
);
9927 else if (DECL_P (capture_init_expr
)
9928 && (!VAR_P (capture_init_expr
)
9929 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9931 error_at (capture_token
->location
,
9932 "capture of non-variable %qD ",
9934 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9935 "%q#D declared here", capture_init_expr
);
9938 if (VAR_P (capture_init_expr
)
9939 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9941 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9942 "%qD with non-automatic storage duration",
9944 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9945 "%q#D declared here", capture_init_expr
);
9950 = finish_id_expression
9955 /*integral_constant_expression_p=*/false,
9956 /*allow_non_integral_constant_expression_p=*/false,
9957 /*non_integral_constant_expression_p=*/NULL
,
9958 /*template_p=*/false,
9960 /*address_p=*/false,
9961 /*template_arg_p=*/false,
9963 capture_token
->location
);
9965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9967 cp_lexer_consume_token (parser
->lexer
);
9968 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9971 check_for_bare_parameter_packs (capture_init_expr
);
9974 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9975 && !explicit_init_p
)
9977 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9978 && capture_kind
== BY_COPY
)
9979 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9980 "of %qD redundant with by-copy capture default",
9982 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9983 && capture_kind
== BY_REFERENCE
)
9984 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9985 "capture of %qD redundant with by-reference capture "
9986 "default", capture_id
);
9989 add_capture (lambda_expr
,
9992 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9996 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9999 /* Parse the (optional) middle of a lambda expression.
10002 < template-parameter-list [opt] >
10003 ( parameter-declaration-clause [opt] )
10004 attribute-specifier [opt]
10006 exception-specification [opt]
10007 lambda-return-type-clause [opt]
10009 LAMBDA_EXPR is the current representation of the lambda expression. */
10012 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10014 /* 5.1.1.4 of the standard says:
10015 If a lambda-expression does not include a lambda-declarator, it is as if
10016 the lambda-declarator were ().
10017 This means an empty parameter list, no attributes, and no exception
10019 tree param_list
= void_list_node
;
10020 tree attributes
= NULL_TREE
;
10021 tree exception_spec
= NULL_TREE
;
10022 tree template_param_list
= NULL_TREE
;
10023 tree tx_qual
= NULL_TREE
;
10025 /* The template-parameter-list is optional, but must begin with
10026 an opening angle if present. */
10027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10029 if (cxx_dialect
< cxx14
)
10030 pedwarn (parser
->lexer
->next_token
->location
, 0,
10031 "lambda templates are only available with "
10032 "-std=c++14 or -std=gnu++14");
10034 cp_lexer_consume_token (parser
->lexer
);
10036 template_param_list
= cp_parser_template_parameter_list (parser
);
10038 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10040 /* We just processed one more parameter list. */
10041 ++parser
->num_template_parameter_lists
;
10044 /* The parameter-declaration-clause is optional (unless
10045 template-parameter-list was given), but must begin with an
10046 opening parenthesis if present. */
10047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10049 cp_lexer_consume_token (parser
->lexer
);
10051 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10053 /* Parse parameters. */
10054 param_list
= cp_parser_parameter_declaration_clause (parser
);
10056 /* Default arguments shall not be specified in the
10057 parameter-declaration-clause of a lambda-declarator. */
10058 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10059 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
10060 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10061 "default argument specified for lambda parameter");
10063 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10065 attributes
= cp_parser_attributes_opt (parser
);
10067 /* Parse optional `mutable' keyword. */
10068 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
10070 cp_lexer_consume_token (parser
->lexer
);
10071 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10074 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10076 /* Parse optional exception specification. */
10077 exception_spec
= cp_parser_exception_specification_opt (parser
);
10079 /* Parse optional trailing return type. */
10080 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10082 cp_lexer_consume_token (parser
->lexer
);
10083 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10084 = cp_parser_trailing_type_id (parser
);
10087 /* The function parameters must be in scope all the way until after the
10088 trailing-return-type in case of decltype. */
10089 pop_bindings_and_leave_scope ();
10091 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
10092 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10094 /* Create the function call operator.
10096 Messing with declarators like this is no uglier than building up the
10097 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10100 cp_decl_specifier_seq return_type_specs
;
10101 cp_declarator
* declarator
;
10106 clear_decl_specs (&return_type_specs
);
10107 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10108 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
10110 /* Maybe we will deduce the return type later. */
10111 return_type_specs
.type
= make_auto ();
10113 p
= obstack_alloc (&declarator_obstack
, 0);
10115 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
10118 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
10119 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
10120 declarator
= make_call_declarator (declarator
, param_list
, quals
,
10121 VIRT_SPEC_UNSPECIFIED
,
10125 /*late_return_type=*/NULL_TREE
,
10126 /*requires_clause*/NULL_TREE
);
10127 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
10129 fco
= grokmethod (&return_type_specs
,
10132 if (fco
!= error_mark_node
)
10134 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
10135 DECL_ARTIFICIAL (fco
) = 1;
10136 /* Give the object parameter a different name. */
10137 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
10138 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
10139 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
10141 if (template_param_list
)
10143 fco
= finish_member_template_decl (fco
);
10144 finish_template_decl (template_param_list
);
10145 --parser
->num_template_parameter_lists
;
10147 else if (parser
->fully_implicit_function_template_p
)
10148 fco
= finish_fully_implicit_template (parser
, fco
);
10150 finish_member_declaration (fco
);
10152 obstack_free (&declarator_obstack
, p
);
10154 return (fco
!= error_mark_node
);
10158 /* Parse the body of a lambda expression, which is simply
10162 but which requires special handling.
10163 LAMBDA_EXPR is the current representation of the lambda expression. */
10166 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
10168 bool nested
= (current_function_decl
!= NULL_TREE
);
10169 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
10171 push_function_context ();
10173 /* Still increment function_depth so that we don't GC in the
10174 middle of an expression. */
10176 vec
<tree
> omp_privatization_save
;
10177 save_omp_privatization_clauses (omp_privatization_save
);
10178 /* Clear this in case we're in the middle of a default argument. */
10179 parser
->local_variables_forbidden_p
= false;
10181 /* Finish the function call operator
10183 + late_parsing_for_member
10184 + function_definition_after_declarator
10185 + ctor_initializer_opt_and_function_body */
10187 tree fco
= lambda_function (lambda_expr
);
10190 tree compound_stmt
;
10193 /* Let the front end know that we are going to be defining this
10195 start_preparsed_function (fco
,
10197 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
10199 start_lambda_scope (fco
);
10200 body
= begin_function_body ();
10202 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10205 /* Push the proxies for any explicit captures. */
10206 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
10207 cap
= TREE_CHAIN (cap
))
10208 build_capture_proxy (TREE_PURPOSE (cap
));
10210 compound_stmt
= begin_compound_stmt (0);
10212 /* 5.1.1.4 of the standard says:
10213 If a lambda-expression does not include a trailing-return-type, it
10214 is as if the trailing-return-type denotes the following type:
10215 * if the compound-statement is of the form
10216 { return attribute-specifier [opt] expression ; }
10217 the type of the returned expression after lvalue-to-rvalue
10218 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10219 (_conv.array_ 4.2), and function-to-pointer conversion
10221 * otherwise, void. */
10223 /* In a lambda that has neither a lambda-return-type-clause
10224 nor a deducible form, errors should be reported for return statements
10225 in the body. Since we used void as the placeholder return type, parsing
10226 the body as usual will give such desired behavior. */
10227 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
10228 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
10229 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
10231 tree expr
= NULL_TREE
;
10232 cp_id_kind idk
= CP_ID_KIND_NONE
;
10234 /* Parse tentatively in case there's more after the initial return
10236 cp_parser_parse_tentatively (parser
);
10238 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
10240 expr
= cp_parser_expression (parser
, &idk
);
10242 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10243 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10245 if (cp_parser_parse_definitely (parser
))
10247 if (!processing_template_decl
)
10249 tree type
= lambda_return_type (expr
);
10250 apply_deduced_return_type (fco
, type
);
10251 if (type
== error_mark_node
)
10252 expr
= error_mark_node
;
10255 /* Will get error here if type not deduced yet. */
10256 finish_return_stmt (expr
);
10264 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10265 cp_parser_label_declaration (parser
);
10266 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
10267 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10270 finish_compound_stmt (compound_stmt
);
10273 finish_function_body (body
);
10274 finish_lambda_scope ();
10276 /* Finish the function and generate code for it if necessary. */
10277 tree fn
= finish_function (/*inline*/2);
10279 /* Only expand if the call op is not a template. */
10280 if (!DECL_TEMPLATE_INFO (fco
))
10281 expand_or_defer_fn (fn
);
10284 restore_omp_privatization_clauses (omp_privatization_save
);
10285 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
10287 pop_function_context();
10292 /* Statements [gram.stmt.stmt] */
10294 /* Parse a statement.
10298 expression-statement
10300 selection-statement
10301 iteration-statement
10303 declaration-statement
10310 attribute-specifier-seq (opt) expression-statement
10311 attribute-specifier-seq (opt) compound-statement
10312 attribute-specifier-seq (opt) selection-statement
10313 attribute-specifier-seq (opt) iteration-statement
10314 attribute-specifier-seq (opt) jump-statement
10315 declaration-statement
10316 attribute-specifier-seq (opt) try-block
10323 IN_COMPOUND is true when the statement is nested inside a
10324 cp_parser_compound_statement; this matters for certain pragmas.
10326 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10327 is a (possibly labeled) if statement which is not enclosed in braces
10328 and has an else clause. This is used to implement -Wparentheses.
10330 CHAIN is a vector of if-else-if conditions. */
10333 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
10334 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
10336 tree statement
, std_attrs
= NULL_TREE
;
10338 location_t statement_location
, attrs_location
;
10343 /* There is no statement yet. */
10344 statement
= NULL_TREE
;
10346 saved_token_sentinel
saved_tokens (parser
->lexer
);
10347 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
10348 if (c_dialect_objc ())
10349 /* In obj-c++, seeing '[[' might be the either the beginning of
10350 c++11 attributes, or a nested objc-message-expression. So
10351 let's parse the c++11 attributes tentatively. */
10352 cp_parser_parse_tentatively (parser
);
10353 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10354 if (c_dialect_objc ())
10356 if (!cp_parser_parse_definitely (parser
))
10357 std_attrs
= NULL_TREE
;
10360 /* Peek at the next token. */
10361 token
= cp_lexer_peek_token (parser
->lexer
);
10362 /* Remember the location of the first token in the statement. */
10363 statement_location
= token
->location
;
10364 /* If this is a keyword, then that will often determine what kind of
10365 statement we have. */
10366 if (token
->type
== CPP_KEYWORD
)
10368 enum rid keyword
= token
->keyword
;
10374 /* Looks like a labeled-statement with a case label.
10375 Parse the label, and then use tail recursion to parse
10377 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10378 in_compound
= false;
10383 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10389 statement
= cp_parser_iteration_statement (parser
, if_p
, false);
10393 if (!flag_cilkplus
)
10395 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10396 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10397 cp_lexer_consume_token (parser
->lexer
);
10398 statement
= error_mark_node
;
10401 statement
= cp_parser_cilk_for (parser
, integer_zero_node
, if_p
);
10408 statement
= cp_parser_jump_statement (parser
);
10411 case RID_CILK_SYNC
:
10412 cp_lexer_consume_token (parser
->lexer
);
10415 tree sync_expr
= build_cilk_sync ();
10416 SET_EXPR_LOCATION (sync_expr
,
10418 statement
= finish_expr_stmt (sync_expr
);
10422 error_at (token
->location
, "-fcilkplus must be enabled to use"
10423 " %<_Cilk_sync%>");
10424 statement
= error_mark_node
;
10426 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10429 /* Objective-C++ exception-handling constructs. */
10432 case RID_AT_FINALLY
:
10433 case RID_AT_SYNCHRONIZED
:
10435 statement
= cp_parser_objc_statement (parser
);
10439 statement
= cp_parser_try_block (parser
);
10442 case RID_NAMESPACE
:
10443 /* This must be a namespace alias definition. */
10444 cp_parser_declaration_statement (parser
);
10447 case RID_TRANSACTION_ATOMIC
:
10448 case RID_TRANSACTION_RELAXED
:
10449 case RID_SYNCHRONIZED
:
10450 case RID_ATOMIC_NOEXCEPT
:
10451 case RID_ATOMIC_CANCEL
:
10452 statement
= cp_parser_transaction (parser
, token
);
10454 case RID_TRANSACTION_CANCEL
:
10455 statement
= cp_parser_transaction_cancel (parser
);
10459 /* It might be a keyword like `int' that can start a
10460 declaration-statement. */
10464 else if (token
->type
== CPP_NAME
)
10466 /* If the next token is a `:', then we are looking at a
10467 labeled-statement. */
10468 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10469 if (token
->type
== CPP_COLON
)
10471 /* Looks like a labeled-statement with an ordinary label.
10472 Parse the label, and then use tail recursion to parse
10475 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10476 in_compound
= false;
10480 /* Anything that starts with a `{' must be a compound-statement. */
10481 else if (token
->type
== CPP_OPEN_BRACE
)
10482 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10483 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10484 a statement all its own. */
10485 else if (token
->type
== CPP_PRAGMA
)
10487 /* Only certain OpenMP pragmas are attached to statements, and thus
10488 are considered statements themselves. All others are not. In
10489 the context of a compound, accept the pragma as a "statement" and
10490 return so that we can check for a close brace. Otherwise we
10491 require a real statement and must go back and read one. */
10493 cp_parser_pragma (parser
, pragma_compound
, if_p
);
10494 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
10498 else if (token
->type
== CPP_EOF
)
10500 cp_parser_error (parser
, "expected statement");
10504 /* Everything else must be a declaration-statement or an
10505 expression-statement. Try for the declaration-statement
10506 first, unless we are looking at a `;', in which case we know that
10507 we have an expression-statement. */
10510 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10512 if (std_attrs
!= NULL_TREE
)
10514 /* Attributes should be parsed as part of the the
10515 declaration, so let's un-parse them. */
10516 saved_tokens
.rollback();
10517 std_attrs
= NULL_TREE
;
10520 cp_parser_parse_tentatively (parser
);
10521 /* Try to parse the declaration-statement. */
10522 cp_parser_declaration_statement (parser
);
10523 /* If that worked, we're done. */
10524 if (cp_parser_parse_definitely (parser
))
10527 /* Look for an expression-statement instead. */
10528 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10531 /* Set the line number for the statement. */
10532 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10533 SET_EXPR_LOCATION (statement
, statement_location
);
10535 /* Note that for now, we don't do anything with c++11 statements
10536 parsed at this level. */
10537 if (std_attrs
!= NULL_TREE
)
10538 warning_at (attrs_location
,
10540 "attributes at the beginning of statement are ignored");
10543 /* Parse the label for a labeled-statement, i.e.
10546 case constant-expression :
10550 case constant-expression ... constant-expression : statement
10552 When a label is parsed without errors, the label is added to the
10553 parse tree by the finish_* functions, so this function doesn't
10554 have to return the label. */
10557 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10560 tree label
= NULL_TREE
;
10561 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10563 /* The next token should be an identifier. */
10564 token
= cp_lexer_peek_token (parser
->lexer
);
10565 if (token
->type
!= CPP_NAME
10566 && token
->type
!= CPP_KEYWORD
)
10568 cp_parser_error (parser
, "expected labeled-statement");
10572 parser
->colon_corrects_to_scope_p
= false;
10573 switch (token
->keyword
)
10577 tree expr
, expr_hi
;
10578 cp_token
*ellipsis
;
10580 /* Consume the `case' token. */
10581 cp_lexer_consume_token (parser
->lexer
);
10582 /* Parse the constant-expression. */
10583 expr
= cp_parser_constant_expression (parser
);
10584 if (check_for_bare_parameter_packs (expr
))
10585 expr
= error_mark_node
;
10587 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10588 if (ellipsis
->type
== CPP_ELLIPSIS
)
10590 /* Consume the `...' token. */
10591 cp_lexer_consume_token (parser
->lexer
);
10592 expr_hi
= cp_parser_constant_expression (parser
);
10593 if (check_for_bare_parameter_packs (expr_hi
))
10594 expr_hi
= error_mark_node
;
10596 /* We don't need to emit warnings here, as the common code
10597 will do this for us. */
10600 expr_hi
= NULL_TREE
;
10602 if (parser
->in_switch_statement_p
)
10603 finish_case_label (token
->location
, expr
, expr_hi
);
10605 error_at (token
->location
,
10606 "case label %qE not within a switch statement",
10612 /* Consume the `default' token. */
10613 cp_lexer_consume_token (parser
->lexer
);
10615 if (parser
->in_switch_statement_p
)
10616 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10618 error_at (token
->location
, "case label not within a switch statement");
10622 /* Anything else must be an ordinary label. */
10623 label
= finish_label_stmt (cp_parser_identifier (parser
));
10627 /* Require the `:' token. */
10628 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10630 /* An ordinary label may optionally be followed by attributes.
10631 However, this is only permitted if the attributes are then
10632 followed by a semicolon. This is because, for backward
10633 compatibility, when parsing
10634 lab: __attribute__ ((unused)) int i;
10635 we want the attribute to attach to "i", not "lab". */
10636 if (label
!= NULL_TREE
10637 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10640 cp_parser_parse_tentatively (parser
);
10641 attrs
= cp_parser_gnu_attributes_opt (parser
);
10642 if (attrs
== NULL_TREE
10643 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10644 cp_parser_abort_tentative_parse (parser
);
10645 else if (!cp_parser_parse_definitely (parser
))
10648 attributes
= chainon (attributes
, attrs
);
10651 if (attributes
!= NULL_TREE
)
10652 cplus_decl_attributes (&label
, attributes
, 0);
10654 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10657 /* Parse an expression-statement.
10659 expression-statement:
10662 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10663 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10664 indicates whether this expression-statement is part of an
10665 expression statement. */
10668 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10670 tree statement
= NULL_TREE
;
10671 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10673 /* If the next token is a ';', then there is no expression
10675 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10677 statement
= cp_parser_expression (parser
);
10678 if (statement
== error_mark_node
10679 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10681 cp_parser_skip_to_end_of_block_or_statement (parser
);
10682 return error_mark_node
;
10686 /* Give a helpful message for "A<T>::type t;" and the like. */
10687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10688 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10690 if (TREE_CODE (statement
) == SCOPE_REF
)
10691 error_at (token
->location
, "need %<typename%> before %qE because "
10692 "%qT is a dependent scope",
10693 statement
, TREE_OPERAND (statement
, 0));
10694 else if (is_overloaded_fn (statement
)
10695 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10698 tree fn
= get_first_fn (statement
);
10699 error_at (token
->location
,
10700 "%<%T::%D%> names the constructor, not the type",
10701 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10705 /* Consume the final `;'. */
10706 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10708 if (in_statement_expr
10709 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10710 /* This is the final expression statement of a statement
10712 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10713 else if (statement
)
10714 statement
= finish_expr_stmt (statement
);
10719 /* Parse a compound-statement.
10721 compound-statement:
10722 { statement-seq [opt] }
10726 compound-statement:
10727 { label-declaration-seq [opt] statement-seq [opt] }
10729 label-declaration-seq:
10731 label-declaration-seq label-declaration
10733 Returns a tree representing the statement. */
10736 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10737 int bcs_flags
, bool function_body
)
10739 tree compound_stmt
;
10741 /* Consume the `{'. */
10742 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10743 return error_mark_node
;
10744 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10745 && !function_body
&& cxx_dialect
< cxx14
)
10746 pedwarn (input_location
, OPT_Wpedantic
,
10747 "compound-statement in constexpr function");
10748 /* Begin the compound-statement. */
10749 compound_stmt
= begin_compound_stmt (bcs_flags
);
10750 /* If the next keyword is `__label__' we have a label declaration. */
10751 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10752 cp_parser_label_declaration (parser
);
10753 /* Parse an (optional) statement-seq. */
10754 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10755 /* Finish the compound-statement. */
10756 finish_compound_stmt (compound_stmt
);
10757 /* Consume the `}'. */
10758 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10760 return compound_stmt
;
10763 /* Parse an (optional) statement-seq.
10767 statement-seq [opt] statement */
10770 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10772 /* Scan statements until there aren't any more. */
10775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10777 /* If we are looking at a `}', then we have run out of
10778 statements; the same is true if we have reached the end
10779 of file, or have stumbled upon a stray '@end'. */
10780 if (token
->type
== CPP_CLOSE_BRACE
10781 || token
->type
== CPP_EOF
10782 || token
->type
== CPP_PRAGMA_EOL
10783 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10786 /* If we are in a compound statement and find 'else' then
10787 something went wrong. */
10788 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10790 if (parser
->in_statement
& IN_IF_STMT
)
10794 token
= cp_lexer_consume_token (parser
->lexer
);
10795 error_at (token
->location
, "%<else%> without a previous %<if%>");
10799 /* Parse the statement. */
10800 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10804 /* Parse a selection-statement.
10806 selection-statement:
10807 if ( condition ) statement
10808 if ( condition ) statement else statement
10809 switch ( condition ) statement
10811 Returns the new IF_STMT or SWITCH_STMT.
10813 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10814 is a (possibly labeled) if statement which is not enclosed in
10815 braces and has an else clause. This is used to implement
10818 CHAIN is a vector of if-else-if conditions. This is used to implement
10819 -Wduplicated-cond. */
10822 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
10827 token_indent_info guard_tinfo
;
10832 /* Peek at the next token. */
10833 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10834 guard_tinfo
= get_token_indent_info (token
);
10836 /* See what kind of keyword it is. */
10837 keyword
= token
->keyword
;
10846 /* Look for the `('. */
10847 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10849 cp_parser_skip_to_end_of_statement (parser
);
10850 return error_mark_node
;
10853 /* Begin the selection-statement. */
10854 if (keyword
== RID_IF
)
10855 statement
= begin_if_stmt ();
10857 statement
= begin_switch_stmt ();
10859 /* Parse the condition. */
10860 condition
= cp_parser_condition (parser
);
10861 /* Look for the `)'. */
10862 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10863 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10864 /*consume_paren=*/true);
10866 if (keyword
== RID_IF
)
10869 unsigned char in_statement
;
10871 /* Add the condition. */
10872 finish_if_stmt_cond (condition
, statement
);
10874 if (warn_duplicated_cond
)
10875 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
10878 /* Parse the then-clause. */
10879 in_statement
= parser
->in_statement
;
10880 parser
->in_statement
|= IN_IF_STMT
;
10881 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
10883 parser
->in_statement
= in_statement
;
10885 finish_then_clause (statement
);
10887 /* If the next token is `else', parse the else-clause. */
10888 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10892 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
10893 /* Consume the `else' keyword. */
10894 cp_lexer_consume_token (parser
->lexer
);
10895 if (warn_duplicated_cond
)
10897 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10901 /* We've got "if (COND) else if (COND2)". Start
10902 the condition chain and add COND as the first
10904 chain
= new vec
<tree
> ();
10905 if (!CONSTANT_CLASS_P (condition
)
10906 && !TREE_SIDE_EFFECTS (condition
))
10908 /* Wrap it in a NOP_EXPR so that we can set the
10909 location of the condition. */
10910 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
10912 SET_EXPR_LOCATION (e
, token
->location
);
10913 chain
->safe_push (e
);
10916 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
10919 /* This is if-else without subsequent if. Zap the
10920 condition chain; we would have already warned at
10926 begin_else_clause (statement
);
10927 /* Parse the else-clause. */
10928 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10929 guard_tinfo
, chain
);
10931 finish_else_clause (statement
);
10933 /* If we are currently parsing a then-clause, then
10934 IF_P will not be NULL. We set it to true to
10935 indicate that this if statement has an else clause.
10936 This may trigger the Wparentheses warning below
10937 when we get back up to the parent if statement. */
10943 /* This if statement does not have an else clause. If
10944 NESTED_IF is true, then the then-clause has an if
10945 statement which does have an else clause. We warn
10946 about the potential ambiguity. */
10948 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
10949 "suggest explicit braces to avoid ambiguous"
10951 if (warn_duplicated_cond
)
10953 /* We don't need the condition chain anymore. */
10959 /* Now we're all done with the if-statement. */
10960 finish_if_stmt (statement
);
10964 bool in_switch_statement_p
;
10965 unsigned char in_statement
;
10967 /* Add the condition. */
10968 finish_switch_cond (condition
, statement
);
10970 /* Parse the body of the switch-statement. */
10971 in_switch_statement_p
= parser
->in_switch_statement_p
;
10972 in_statement
= parser
->in_statement
;
10973 parser
->in_switch_statement_p
= true;
10974 parser
->in_statement
|= IN_SWITCH_STMT
;
10975 cp_parser_implicitly_scoped_statement (parser
, if_p
,
10977 parser
->in_switch_statement_p
= in_switch_statement_p
;
10978 parser
->in_statement
= in_statement
;
10980 /* Now we're all done with the switch-statement. */
10981 finish_switch_stmt (statement
);
10989 cp_parser_error (parser
, "expected selection-statement");
10990 return error_mark_node
;
10994 /* Parse a condition.
10998 type-specifier-seq declarator = initializer-clause
10999 type-specifier-seq declarator braced-init-list
11004 type-specifier-seq declarator asm-specification [opt]
11005 attributes [opt] = assignment-expression
11007 Returns the expression that should be tested. */
11010 cp_parser_condition (cp_parser
* parser
)
11012 cp_decl_specifier_seq type_specifiers
;
11013 const char *saved_message
;
11014 int declares_class_or_enum
;
11016 /* Try the declaration first. */
11017 cp_parser_parse_tentatively (parser
);
11018 /* New types are not allowed in the type-specifier-seq for a
11020 saved_message
= parser
->type_definition_forbidden_message
;
11021 parser
->type_definition_forbidden_message
11022 = G_("types may not be defined in conditions");
11023 /* Parse the type-specifier-seq. */
11024 cp_parser_decl_specifier_seq (parser
,
11025 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
11027 &declares_class_or_enum
);
11028 /* Restore the saved message. */
11029 parser
->type_definition_forbidden_message
= saved_message
;
11030 /* If all is well, we might be looking at a declaration. */
11031 if (!cp_parser_error_occurred (parser
))
11034 tree asm_specification
;
11036 cp_declarator
*declarator
;
11037 tree initializer
= NULL_TREE
;
11039 /* Parse the declarator. */
11040 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
11041 /*ctor_dtor_or_conv_p=*/NULL
,
11042 /*parenthesized_p=*/NULL
,
11043 /*member_p=*/false,
11044 /*friend_p=*/false);
11045 /* Parse the attributes. */
11046 attributes
= cp_parser_attributes_opt (parser
);
11047 /* Parse the asm-specification. */
11048 asm_specification
= cp_parser_asm_specification_opt (parser
);
11049 /* If the next token is not an `=' or '{', then we might still be
11050 looking at an expression. For example:
11054 looks like a decl-specifier-seq and a declarator -- but then
11055 there is no `=', so this is an expression. */
11056 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
11057 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11058 cp_parser_simulate_error (parser
);
11060 /* If we did see an `=' or '{', then we are looking at a declaration
11062 if (cp_parser_parse_definitely (parser
))
11065 bool non_constant_p
;
11066 bool flags
= LOOKUP_ONLYCONVERTING
;
11068 /* Create the declaration. */
11069 decl
= start_decl (declarator
, &type_specifiers
,
11070 /*initialized_p=*/true,
11071 attributes
, /*prefix_attributes=*/NULL_TREE
,
11074 /* Parse the initializer. */
11075 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11077 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
11078 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
11083 /* Consume the `='. */
11084 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
11085 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
11087 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
11088 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11090 /* Process the initializer. */
11091 cp_finish_decl (decl
,
11092 initializer
, !non_constant_p
,
11097 pop_scope (pushed_scope
);
11099 return convert_from_reference (decl
);
11102 /* If we didn't even get past the declarator successfully, we are
11103 definitely not looking at a declaration. */
11105 cp_parser_abort_tentative_parse (parser
);
11107 /* Otherwise, we are looking at an expression. */
11108 return cp_parser_expression (parser
);
11111 /* Parses a for-statement or range-for-statement until the closing ')',
11115 cp_parser_for (cp_parser
*parser
, bool ivdep
)
11117 tree init
, scope
, decl
;
11120 /* Begin the for-statement. */
11121 scope
= begin_for_scope (&init
);
11123 /* Parse the initialization. */
11124 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
11127 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
11129 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
11133 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
11135 /* Normal for loop */
11136 tree condition
= NULL_TREE
;
11137 tree expression
= NULL_TREE
;
11140 stmt
= begin_for_stmt (scope
, init
);
11141 /* The for-init-statement has already been parsed in
11142 cp_parser_for_init_statement, so no work is needed here. */
11143 finish_for_init_stmt (stmt
);
11145 /* If there's a condition, process it. */
11146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11147 condition
= cp_parser_condition (parser
);
11150 cp_parser_error (parser
, "missing loop condition in loop with "
11151 "%<GCC ivdep%> pragma");
11152 condition
= error_mark_node
;
11154 finish_for_cond (condition
, stmt
, ivdep
);
11155 /* Look for the `;'. */
11156 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11158 /* If there's an expression, process it. */
11159 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
11160 expression
= cp_parser_expression (parser
);
11161 finish_for_expr (expression
, stmt
);
11166 /* Tries to parse a range-based for-statement:
11169 decl-specifier-seq declarator : expression
11171 The decl-specifier-seq declarator and the `:' are already parsed by
11172 cp_parser_for_init_statement. If processing_template_decl it returns a
11173 newly created RANGE_FOR_STMT; if not, it is converted to a
11174 regular FOR_STMT. */
11177 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
11180 tree stmt
, range_expr
;
11182 /* Get the range declaration momentarily out of the way so that
11183 the range expression doesn't clash with it. */
11184 if (range_decl
!= error_mark_node
)
11185 pop_binding (DECL_NAME (range_decl
), range_decl
);
11187 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11189 bool expr_non_constant_p
;
11190 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11193 range_expr
= cp_parser_expression (parser
);
11195 /* Put the range declaration back into scope. */
11196 if (range_decl
!= error_mark_node
)
11197 push_binding (DECL_NAME (range_decl
), range_decl
, current_binding_level
);
11199 /* If in template, STMT is converted to a normal for-statement
11200 at instantiation. If not, it is done just ahead. */
11201 if (processing_template_decl
)
11203 if (check_for_bare_parameter_packs (range_expr
))
11204 range_expr
= error_mark_node
;
11205 stmt
= begin_range_for_stmt (scope
, init
);
11207 RANGE_FOR_IVDEP (stmt
) = 1;
11208 finish_range_for_decl (stmt
, range_decl
, range_expr
);
11209 if (!type_dependent_expression_p (range_expr
)
11210 /* do_auto_deduction doesn't mess with template init-lists. */
11211 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
11212 do_range_for_auto_deduction (range_decl
, range_expr
);
11216 stmt
= begin_for_stmt (scope
, init
);
11217 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
11222 /* Subroutine of cp_convert_range_for: given the initializer expression,
11223 builds up the range temporary. */
11226 build_range_temp (tree range_expr
)
11228 tree range_type
, range_temp
;
11230 /* Find out the type deduced by the declaration
11231 `auto &&__range = range_expr'. */
11232 range_type
= cp_build_reference_type (make_auto (), true);
11233 range_type
= do_auto_deduction (range_type
, range_expr
,
11234 type_uses_auto (range_type
));
11236 /* Create the __range variable. */
11237 range_temp
= build_decl (input_location
, VAR_DECL
,
11238 get_identifier ("__for_range"), range_type
);
11239 TREE_USED (range_temp
) = 1;
11240 DECL_ARTIFICIAL (range_temp
) = 1;
11245 /* Used by cp_parser_range_for in template context: we aren't going to
11246 do a full conversion yet, but we still need to resolve auto in the
11247 type of the for-range-declaration if present. This is basically
11248 a shortcut version of cp_convert_range_for. */
11251 do_range_for_auto_deduction (tree decl
, tree range_expr
)
11253 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
11256 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
11257 range_temp
= convert_from_reference (build_range_temp (range_expr
));
11258 iter_type
= (cp_parser_perform_range_for_lookup
11259 (range_temp
, &begin_dummy
, &end_dummy
));
11262 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
11264 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
11265 tf_warning_or_error
);
11266 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
11267 iter_decl
, auto_node
);
11272 /* Converts a range-based for-statement into a normal
11273 for-statement, as per the definition.
11275 for (RANGE_DECL : RANGE_EXPR)
11278 should be equivalent to:
11281 auto &&__range = RANGE_EXPR;
11282 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11286 RANGE_DECL = *__begin;
11291 If RANGE_EXPR is an array:
11292 BEGIN_EXPR = __range
11293 END_EXPR = __range + ARRAY_SIZE(__range)
11294 Else if RANGE_EXPR has a member 'begin' or 'end':
11295 BEGIN_EXPR = __range.begin()
11296 END_EXPR = __range.end()
11298 BEGIN_EXPR = begin(__range)
11299 END_EXPR = end(__range);
11301 If __range has a member 'begin' but not 'end', or vice versa, we must
11302 still use the second alternative (it will surely fail, however).
11303 When calling begin()/end() in the third alternative we must use
11304 argument dependent lookup, but always considering 'std' as an associated
11308 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
11312 tree iter_type
, begin_expr
, end_expr
;
11313 tree condition
, expression
;
11315 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
11316 /* If an error happened previously do nothing or else a lot of
11317 unhelpful errors would be issued. */
11318 begin_expr
= end_expr
= iter_type
= error_mark_node
;
11323 if (VAR_P (range_expr
)
11324 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
11325 /* Can't bind a reference to an array of runtime bound. */
11326 range_temp
= range_expr
;
11329 range_temp
= build_range_temp (range_expr
);
11330 pushdecl (range_temp
);
11331 cp_finish_decl (range_temp
, range_expr
,
11332 /*is_constant_init*/false, NULL_TREE
,
11333 LOOKUP_ONLYCONVERTING
);
11334 range_temp
= convert_from_reference (range_temp
);
11336 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
11337 &begin_expr
, &end_expr
);
11340 /* The new for initialization statement. */
11341 begin
= build_decl (input_location
, VAR_DECL
,
11342 get_identifier ("__for_begin"), iter_type
);
11343 TREE_USED (begin
) = 1;
11344 DECL_ARTIFICIAL (begin
) = 1;
11346 cp_finish_decl (begin
, begin_expr
,
11347 /*is_constant_init*/false, NULL_TREE
,
11348 LOOKUP_ONLYCONVERTING
);
11350 if (cxx_dialect
>= cxx1z
)
11351 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
11352 end
= build_decl (input_location
, VAR_DECL
,
11353 get_identifier ("__for_end"), iter_type
);
11354 TREE_USED (end
) = 1;
11355 DECL_ARTIFICIAL (end
) = 1;
11357 cp_finish_decl (end
, end_expr
,
11358 /*is_constant_init*/false, NULL_TREE
,
11359 LOOKUP_ONLYCONVERTING
);
11361 finish_for_init_stmt (statement
);
11363 /* The new for condition. */
11364 condition
= build_x_binary_op (input_location
, NE_EXPR
,
11367 NULL
, tf_warning_or_error
);
11368 finish_for_cond (condition
, statement
, ivdep
);
11370 /* The new increment expression. */
11371 expression
= finish_unary_op_expr (input_location
,
11372 PREINCREMENT_EXPR
, begin
,
11373 tf_warning_or_error
);
11374 finish_for_expr (expression
, statement
);
11376 /* The declaration is initialized with *__begin inside the loop body. */
11377 cp_finish_decl (range_decl
,
11378 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11379 tf_warning_or_error
),
11380 /*is_constant_init*/false, NULL_TREE
,
11381 LOOKUP_ONLYCONVERTING
);
11386 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11387 We need to solve both at the same time because the method used
11388 depends on the existence of members begin or end.
11389 Returns the type deduced for the iterator expression. */
11392 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11394 if (error_operand_p (range
))
11396 *begin
= *end
= error_mark_node
;
11397 return error_mark_node
;
11400 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11402 error ("range-based %<for%> expression of type %qT "
11403 "has incomplete type", TREE_TYPE (range
));
11404 *begin
= *end
= error_mark_node
;
11405 return error_mark_node
;
11407 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11409 /* If RANGE is an array, we will use pointer arithmetic. */
11410 *begin
= decay_conversion (range
, tf_warning_or_error
);
11411 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11413 array_type_nelts_top (TREE_TYPE (range
)),
11415 return TREE_TYPE (*begin
);
11419 /* If it is not an array, we must do a bit of magic. */
11420 tree id_begin
, id_end
;
11421 tree member_begin
, member_end
;
11423 *begin
= *end
= error_mark_node
;
11425 id_begin
= get_identifier ("begin");
11426 id_end
= get_identifier ("end");
11427 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11428 /*protect=*/2, /*want_type=*/false,
11429 tf_warning_or_error
);
11430 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11431 /*protect=*/2, /*want_type=*/false,
11432 tf_warning_or_error
);
11434 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11436 /* Use the member functions. */
11437 if (member_begin
!= NULL_TREE
)
11438 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11440 error ("range-based %<for%> expression of type %qT has an "
11441 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11443 if (member_end
!= NULL_TREE
)
11444 *end
= cp_parser_range_for_member_function (range
, id_end
);
11446 error ("range-based %<for%> expression of type %qT has a "
11447 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11451 /* Use global functions with ADL. */
11452 vec
<tree
, va_gc
> *vec
;
11453 vec
= make_tree_vector ();
11455 vec_safe_push (vec
, range
);
11457 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11458 tf_warning_or_error
);
11459 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11460 tf_warning_or_error
);
11461 member_end
= perform_koenig_lookup (id_end
, vec
,
11462 tf_warning_or_error
);
11463 *end
= finish_call_expr (member_end
, &vec
, false, true,
11464 tf_warning_or_error
);
11466 release_tree_vector (vec
);
11469 /* Last common checks. */
11470 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11472 /* If one of the expressions is an error do no more checks. */
11473 *begin
= *end
= error_mark_node
;
11474 return error_mark_node
;
11476 else if (type_dependent_expression_p (*begin
)
11477 || type_dependent_expression_p (*end
))
11478 /* Can happen, when, eg, in a template context, Koenig lookup
11479 can't resolve begin/end (c++/58503). */
11483 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11484 /* The unqualified type of the __begin and __end temporaries should
11485 be the same, as required by the multiple auto declaration. */
11486 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11488 if (cxx_dialect
>= cxx1z
11489 && (build_x_binary_op (input_location
, NE_EXPR
,
11490 *begin
, ERROR_MARK
,
11493 != error_mark_node
))
11494 /* P0184R0 allows __begin and __end to have different types,
11495 but make sure they are comparable so we can give a better
11498 error ("inconsistent begin/end types in range-based %<for%> "
11499 "statement: %qT and %qT",
11500 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11507 /* Helper function for cp_parser_perform_range_for_lookup.
11508 Builds a tree for RANGE.IDENTIFIER(). */
11511 cp_parser_range_for_member_function (tree range
, tree identifier
)
11514 vec
<tree
, va_gc
> *vec
;
11516 member
= finish_class_member_access_expr (range
, identifier
,
11517 false, tf_warning_or_error
);
11518 if (member
== error_mark_node
)
11519 return error_mark_node
;
11521 vec
= make_tree_vector ();
11522 res
= finish_call_expr (member
, &vec
,
11523 /*disallow_virtual=*/false,
11524 /*koenig_p=*/false,
11525 tf_warning_or_error
);
11526 release_tree_vector (vec
);
11530 /* Parse an iteration-statement.
11532 iteration-statement:
11533 while ( condition ) statement
11534 do statement while ( expression ) ;
11535 for ( for-init-statement condition [opt] ; expression [opt] )
11538 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11541 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
)
11546 unsigned char in_statement
;
11547 token_indent_info guard_tinfo
;
11549 /* Peek at the next token. */
11550 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11552 return error_mark_node
;
11554 guard_tinfo
= get_token_indent_info (token
);
11556 /* Remember whether or not we are already within an iteration
11558 in_statement
= parser
->in_statement
;
11560 /* See what kind of keyword it is. */
11561 keyword
= token
->keyword
;
11568 /* Begin the while-statement. */
11569 statement
= begin_while_stmt ();
11570 /* Look for the `('. */
11571 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11572 /* Parse the condition. */
11573 condition
= cp_parser_condition (parser
);
11574 finish_while_stmt_cond (condition
, statement
, ivdep
);
11575 /* Look for the `)'. */
11576 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11577 /* Parse the dependent statement. */
11578 parser
->in_statement
= IN_ITERATION_STMT
;
11579 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11580 parser
->in_statement
= in_statement
;
11581 /* We're done with the while-statement. */
11582 finish_while_stmt (statement
);
11590 /* Begin the do-statement. */
11591 statement
= begin_do_stmt ();
11592 /* Parse the body of the do-statement. */
11593 parser
->in_statement
= IN_ITERATION_STMT
;
11594 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11595 parser
->in_statement
= in_statement
;
11596 finish_do_body (statement
);
11597 /* Look for the `while' keyword. */
11598 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11599 /* Look for the `('. */
11600 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11601 /* Parse the expression. */
11602 expression
= cp_parser_expression (parser
);
11603 /* We're done with the do-statement. */
11604 finish_do_stmt (expression
, statement
, ivdep
);
11605 /* Look for the `)'. */
11606 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11607 /* Look for the `;'. */
11608 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11614 /* Look for the `('. */
11615 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11617 statement
= cp_parser_for (parser
, ivdep
);
11619 /* Look for the `)'. */
11620 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11622 /* Parse the body of the for-statement. */
11623 parser
->in_statement
= IN_ITERATION_STMT
;
11624 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
11625 parser
->in_statement
= in_statement
;
11627 /* We're done with the for-statement. */
11628 finish_for_stmt (statement
);
11633 cp_parser_error (parser
, "expected iteration-statement");
11634 statement
= error_mark_node
;
11641 /* Parse a for-init-statement or the declarator of a range-based-for.
11642 Returns true if a range-based-for declaration is seen.
11644 for-init-statement:
11645 expression-statement
11646 simple-declaration */
11649 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
11651 /* If the next token is a `;', then we have an empty
11652 expression-statement. Grammatically, this is also a
11653 simple-declaration, but an invalid one, because it does not
11654 declare anything. Therefore, if we did not handle this case
11655 specially, we would issue an error message about an invalid
11657 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11659 bool is_range_for
= false;
11660 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11662 /* A colon is used in range-based for. */
11663 parser
->colon_corrects_to_scope_p
= false;
11665 /* We're going to speculatively look for a declaration, falling back
11666 to an expression, if necessary. */
11667 cp_parser_parse_tentatively (parser
);
11668 /* Parse the declaration. */
11669 cp_parser_simple_declaration (parser
,
11670 /*function_definition_allowed_p=*/false,
11672 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11673 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11675 /* It is a range-for, consume the ':' */
11676 cp_lexer_consume_token (parser
->lexer
);
11677 is_range_for
= true;
11678 if (cxx_dialect
< cxx11
)
11680 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11681 "range-based %<for%> loops only available with "
11682 "-std=c++11 or -std=gnu++11");
11683 *decl
= error_mark_node
;
11687 /* The ';' is not consumed yet because we told
11688 cp_parser_simple_declaration not to. */
11689 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11691 if (cp_parser_parse_definitely (parser
))
11692 return is_range_for
;
11693 /* If the tentative parse failed, then we shall need to look for an
11694 expression-statement. */
11696 /* If we are here, it is an expression-statement. */
11697 cp_parser_expression_statement (parser
, NULL_TREE
);
11701 /* Parse a jump-statement.
11706 return expression [opt] ;
11707 return braced-init-list ;
11713 goto * expression ;
11715 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11718 cp_parser_jump_statement (cp_parser
* parser
)
11720 tree statement
= error_mark_node
;
11723 unsigned char in_statement
;
11725 /* Peek at the next token. */
11726 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
11728 return error_mark_node
;
11730 /* See what kind of keyword it is. */
11731 keyword
= token
->keyword
;
11735 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11736 switch (in_statement
)
11739 error_at (token
->location
, "break statement not within loop or switch");
11742 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11743 || in_statement
== IN_ITERATION_STMT
);
11744 statement
= finish_break_stmt ();
11745 if (in_statement
== IN_ITERATION_STMT
)
11746 break_maybe_infinite_loop ();
11749 error_at (token
->location
, "invalid exit from OpenMP structured block");
11752 error_at (token
->location
, "break statement used with OpenMP for loop");
11754 case IN_CILK_SIMD_FOR
:
11755 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11758 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11762 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11765 error_at (token
->location
, "continue statement not within a loop");
11767 case IN_CILK_SIMD_FOR
:
11768 error_at (token
->location
,
11769 "continue statement within %<#pragma simd%> loop body");
11770 /* Fall through. */
11771 case IN_ITERATION_STMT
:
11773 statement
= finish_continue_stmt ();
11776 error_at (token
->location
, "invalid exit from OpenMP structured block");
11779 gcc_unreachable ();
11781 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11787 bool expr_non_constant_p
;
11789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11791 cp_lexer_set_source_position (parser
->lexer
);
11792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11793 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11795 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11796 expr
= cp_parser_expression (parser
);
11798 /* If the next token is a `;', then there is no
11801 /* Build the return-statement. */
11802 statement
= finish_return_stmt (expr
);
11803 /* Look for the final `;'. */
11804 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11809 if (parser
->in_function_body
11810 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11812 error ("%<goto%> in %<constexpr%> function");
11813 cp_function_chain
->invalid_constexpr
= true;
11816 /* Create the goto-statement. */
11817 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11819 /* Issue a warning about this use of a GNU extension. */
11820 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11821 /* Consume the '*' token. */
11822 cp_lexer_consume_token (parser
->lexer
);
11823 /* Parse the dependent expression. */
11824 finish_goto_stmt (cp_parser_expression (parser
));
11827 finish_goto_stmt (cp_parser_identifier (parser
));
11828 /* Look for the final `;'. */
11829 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11833 cp_parser_error (parser
, "expected jump-statement");
11840 /* Parse a declaration-statement.
11842 declaration-statement:
11843 block-declaration */
11846 cp_parser_declaration_statement (cp_parser
* parser
)
11850 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11851 p
= obstack_alloc (&declarator_obstack
, 0);
11853 /* Parse the block-declaration. */
11854 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11856 /* Free any declarators allocated. */
11857 obstack_free (&declarator_obstack
, p
);
11860 /* Some dependent statements (like `if (cond) statement'), are
11861 implicitly in their own scope. In other words, if the statement is
11862 a single statement (as opposed to a compound-statement), it is
11863 none-the-less treated as if it were enclosed in braces. Any
11864 declarations appearing in the dependent statement are out of scope
11865 after control passes that point. This function parses a statement,
11866 but ensures that is in its own scope, even if it is not a
11867 compound-statement.
11869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11870 is a (possibly labeled) if statement which is not enclosed in
11871 braces and has an else clause. This is used to implement
11874 CHAIN is a vector of if-else-if conditions. This is used to implement
11877 Returns the new statement. */
11880 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
11881 const token_indent_info
&guard_tinfo
,
11885 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11886 token_indent_info body_tinfo
11887 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11892 /* Mark if () ; with a special NOP_EXPR. */
11893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11895 cp_lexer_consume_token (parser
->lexer
);
11896 statement
= add_stmt (build_empty_stmt (body_loc
));
11898 if (guard_tinfo
.keyword
== RID_IF
11899 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
11900 warning_at (body_loc
, OPT_Wempty_body
,
11901 "suggest braces around empty body in an %<if%> statement");
11902 else if (guard_tinfo
.keyword
== RID_ELSE
)
11903 warning_at (body_loc
, OPT_Wempty_body
,
11904 "suggest braces around empty body in an %<else%> statement");
11906 /* if a compound is opened, we simply parse the statement directly. */
11907 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11908 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11909 /* If the token is not a `{', then we must take special action. */
11912 /* Create a compound-statement. */
11913 statement
= begin_compound_stmt (0);
11914 /* Parse the dependent-statement. */
11915 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
11916 /* Finish the dummy compound-statement. */
11917 finish_compound_stmt (statement
);
11920 token_indent_info next_tinfo
11921 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11922 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11924 /* Return the statement. */
11928 /* For some dependent statements (like `while (cond) statement'), we
11929 have already created a scope. Therefore, even if the dependent
11930 statement is a compound-statement, we do not want to create another
11934 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
11935 const token_indent_info
&guard_tinfo
)
11937 /* If the token is a `{', then we must take special action. */
11938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11940 token_indent_info body_tinfo
11941 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11943 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11944 token_indent_info next_tinfo
11945 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11946 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11950 /* Avoid calling cp_parser_compound_statement, so that we
11951 don't create a new scope. Do everything else by hand. */
11952 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11953 /* If the next keyword is `__label__' we have a label declaration. */
11954 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11955 cp_parser_label_declaration (parser
);
11956 /* Parse an (optional) statement-seq. */
11957 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11958 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11962 /* Declarations [gram.dcl.dcl] */
11964 /* Parse an optional declaration-sequence.
11968 declaration-seq declaration */
11971 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11977 token
= cp_lexer_peek_token (parser
->lexer
);
11979 if (token
->type
== CPP_CLOSE_BRACE
11980 || token
->type
== CPP_EOF
11981 || token
->type
== CPP_PRAGMA_EOL
)
11984 if (token
->type
== CPP_SEMICOLON
)
11986 /* A declaration consisting of a single semicolon is
11987 invalid. Allow it unless we're being pedantic. */
11988 cp_lexer_consume_token (parser
->lexer
);
11989 if (!in_system_header_at (input_location
))
11990 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11994 /* If we're entering or exiting a region that's implicitly
11995 extern "C", modify the lang context appropriately. */
11996 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11998 push_lang_context (lang_name_c
);
11999 parser
->implicit_extern_c
= true;
12001 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
12003 pop_lang_context ();
12004 parser
->implicit_extern_c
= false;
12007 if (token
->type
== CPP_PRAGMA
)
12009 /* A top-level declaration can consist solely of a #pragma.
12010 A nested declaration cannot, so this is done here and not
12011 in cp_parser_declaration. (A #pragma at block scope is
12012 handled in cp_parser_statement.) */
12013 cp_parser_pragma (parser
, pragma_external
, NULL
);
12017 /* Parse the declaration itself. */
12018 cp_parser_declaration (parser
);
12022 /* Parse a declaration.
12026 function-definition
12027 template-declaration
12028 explicit-instantiation
12029 explicit-specialization
12030 linkage-specification
12031 namespace-definition
12036 __extension__ declaration */
12039 cp_parser_declaration (cp_parser
* parser
)
12043 int saved_pedantic
;
12045 tree attributes
= NULL_TREE
;
12047 /* Check for the `__extension__' keyword. */
12048 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12050 /* Parse the qualified declaration. */
12051 cp_parser_declaration (parser
);
12052 /* Restore the PEDANTIC flag. */
12053 pedantic
= saved_pedantic
;
12058 /* Try to figure out what kind of declaration is present. */
12059 token1
= *cp_lexer_peek_token (parser
->lexer
);
12061 if (token1
.type
!= CPP_EOF
)
12062 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
12065 token2
.type
= CPP_EOF
;
12066 token2
.keyword
= RID_MAX
;
12069 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12070 p
= obstack_alloc (&declarator_obstack
, 0);
12072 /* If the next token is `extern' and the following token is a string
12073 literal, then we have a linkage specification. */
12074 if (token1
.keyword
== RID_EXTERN
12075 && cp_parser_is_pure_string_literal (&token2
))
12076 cp_parser_linkage_specification (parser
);
12077 /* If the next token is `template', then we have either a template
12078 declaration, an explicit instantiation, or an explicit
12080 else if (token1
.keyword
== RID_TEMPLATE
)
12082 /* `template <>' indicates a template specialization. */
12083 if (token2
.type
== CPP_LESS
12084 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
12085 cp_parser_explicit_specialization (parser
);
12086 /* `template <' indicates a template declaration. */
12087 else if (token2
.type
== CPP_LESS
)
12088 cp_parser_template_declaration (parser
, /*member_p=*/false);
12089 /* Anything else must be an explicit instantiation. */
12091 cp_parser_explicit_instantiation (parser
);
12093 /* If the next token is `export', then we have a template
12095 else if (token1
.keyword
== RID_EXPORT
)
12096 cp_parser_template_declaration (parser
, /*member_p=*/false);
12097 /* If the next token is `extern', 'static' or 'inline' and the one
12098 after that is `template', we have a GNU extended explicit
12099 instantiation directive. */
12100 else if (cp_parser_allow_gnu_extensions_p (parser
)
12101 && (token1
.keyword
== RID_EXTERN
12102 || token1
.keyword
== RID_STATIC
12103 || token1
.keyword
== RID_INLINE
)
12104 && token2
.keyword
== RID_TEMPLATE
)
12105 cp_parser_explicit_instantiation (parser
);
12106 /* If the next token is `namespace', check for a named or unnamed
12107 namespace definition. */
12108 else if (token1
.keyword
== RID_NAMESPACE
12109 && (/* A named namespace definition. */
12110 (token2
.type
== CPP_NAME
12111 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12113 || (token2
.type
== CPP_OPEN_SQUARE
12114 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
12115 == CPP_OPEN_SQUARE
)
12116 /* An unnamed namespace definition. */
12117 || token2
.type
== CPP_OPEN_BRACE
12118 || token2
.keyword
== RID_ATTRIBUTE
))
12119 cp_parser_namespace_definition (parser
);
12120 /* An inline (associated) namespace definition. */
12121 else if (token1
.keyword
== RID_INLINE
12122 && token2
.keyword
== RID_NAMESPACE
)
12123 cp_parser_namespace_definition (parser
);
12124 /* Objective-C++ declaration/definition. */
12125 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
12126 cp_parser_objc_declaration (parser
, NULL_TREE
);
12127 else if (c_dialect_objc ()
12128 && token1
.keyword
== RID_ATTRIBUTE
12129 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
12130 cp_parser_objc_declaration (parser
, attributes
);
12131 /* At this point we may have a template declared by a concept
12133 else if (flag_concepts
12134 && cp_parser_template_declaration_after_export (parser
,
12135 /*member_p=*/false))
12138 /* Try to parse a block-declaration, or a function-definition. */
12139 cp_parser_block_declaration (parser
, /*statement_p=*/false);
12141 /* Free any declarators allocated. */
12142 obstack_free (&declarator_obstack
, p
);
12145 /* Parse a block-declaration.
12150 namespace-alias-definition
12157 __extension__ block-declaration
12162 static_assert-declaration
12164 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12165 part of a declaration-statement. */
12168 cp_parser_block_declaration (cp_parser
*parser
,
12172 int saved_pedantic
;
12174 /* Check for the `__extension__' keyword. */
12175 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
12177 /* Parse the qualified declaration. */
12178 cp_parser_block_declaration (parser
, statement_p
);
12179 /* Restore the PEDANTIC flag. */
12180 pedantic
= saved_pedantic
;
12185 /* Peek at the next token to figure out which kind of declaration is
12187 token1
= cp_lexer_peek_token (parser
->lexer
);
12189 /* If the next keyword is `asm', we have an asm-definition. */
12190 if (token1
->keyword
== RID_ASM
)
12193 cp_parser_commit_to_tentative_parse (parser
);
12194 cp_parser_asm_definition (parser
);
12196 /* If the next keyword is `namespace', we have a
12197 namespace-alias-definition. */
12198 else if (token1
->keyword
== RID_NAMESPACE
)
12199 cp_parser_namespace_alias_definition (parser
);
12200 /* If the next keyword is `using', we have a
12201 using-declaration, a using-directive, or an alias-declaration. */
12202 else if (token1
->keyword
== RID_USING
)
12207 cp_parser_commit_to_tentative_parse (parser
);
12208 /* If the token after `using' is `namespace', then we have a
12209 using-directive. */
12210 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
12211 if (token2
->keyword
== RID_NAMESPACE
)
12212 cp_parser_using_directive (parser
);
12213 /* If the second token after 'using' is '=', then we have an
12214 alias-declaration. */
12215 else if (cxx_dialect
>= cxx11
12216 && token2
->type
== CPP_NAME
12217 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
12218 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
12219 cp_parser_alias_declaration (parser
);
12220 /* Otherwise, it's a using-declaration. */
12222 cp_parser_using_declaration (parser
,
12223 /*access_declaration_p=*/false);
12225 /* If the next keyword is `__label__' we have a misplaced label
12227 else if (token1
->keyword
== RID_LABEL
)
12229 cp_lexer_consume_token (parser
->lexer
);
12230 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
12231 cp_parser_skip_to_end_of_statement (parser
);
12232 /* If the next token is now a `;', consume it. */
12233 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12234 cp_lexer_consume_token (parser
->lexer
);
12236 /* If the next token is `static_assert' we have a static assertion. */
12237 else if (token1
->keyword
== RID_STATIC_ASSERT
)
12238 cp_parser_static_assert (parser
, /*member_p=*/false);
12239 /* Anything else must be a simple-declaration. */
12241 cp_parser_simple_declaration (parser
, !statement_p
,
12242 /*maybe_range_for_decl*/NULL
);
12245 /* Parse a simple-declaration.
12247 simple-declaration:
12248 decl-specifier-seq [opt] init-declarator-list [opt] ;
12250 init-declarator-list:
12252 init-declarator-list , init-declarator
12254 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12255 function-definition as a simple-declaration.
12257 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12258 parsed declaration if it is an uninitialized single declarator not followed
12259 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12260 if present, will not be consumed. */
12263 cp_parser_simple_declaration (cp_parser
* parser
,
12264 bool function_definition_allowed_p
,
12265 tree
*maybe_range_for_decl
)
12267 cp_decl_specifier_seq decl_specifiers
;
12268 int declares_class_or_enum
;
12269 bool saw_declarator
;
12270 location_t comma_loc
= UNKNOWN_LOCATION
;
12271 location_t init_loc
= UNKNOWN_LOCATION
;
12273 if (maybe_range_for_decl
)
12274 *maybe_range_for_decl
= NULL_TREE
;
12276 /* Defer access checks until we know what is being declared; the
12277 checks for names appearing in the decl-specifier-seq should be
12278 done as if we were in the scope of the thing being declared. */
12279 push_deferring_access_checks (dk_deferred
);
12281 /* Parse the decl-specifier-seq. We have to keep track of whether
12282 or not the decl-specifier-seq declares a named class or
12283 enumeration type, since that is the only case in which the
12284 init-declarator-list is allowed to be empty.
12288 In a simple-declaration, the optional init-declarator-list can be
12289 omitted only when declaring a class or enumeration, that is when
12290 the decl-specifier-seq contains either a class-specifier, an
12291 elaborated-type-specifier, or an enum-specifier. */
12292 cp_parser_decl_specifier_seq (parser
,
12293 CP_PARSER_FLAGS_OPTIONAL
,
12295 &declares_class_or_enum
);
12296 /* We no longer need to defer access checks. */
12297 stop_deferring_access_checks ();
12299 /* In a block scope, a valid declaration must always have a
12300 decl-specifier-seq. By not trying to parse declarators, we can
12301 resolve the declaration/expression ambiguity more quickly. */
12302 if (!function_definition_allowed_p
12303 && !decl_specifiers
.any_specifiers_p
)
12305 cp_parser_error (parser
, "expected declaration");
12309 /* If the next two tokens are both identifiers, the code is
12310 erroneous. The usual cause of this situation is code like:
12314 where "T" should name a type -- but does not. */
12315 if (!decl_specifiers
.any_type_specifiers_p
12316 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
12318 /* If parsing tentatively, we should commit; we really are
12319 looking at a declaration. */
12320 cp_parser_commit_to_tentative_parse (parser
);
12325 /* If we have seen at least one decl-specifier, and the next token
12326 is not a parenthesis, then we must be looking at a declaration.
12327 (After "int (" we might be looking at a functional cast.) */
12328 if (decl_specifiers
.any_specifiers_p
12329 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12330 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12331 && !cp_parser_error_occurred (parser
))
12332 cp_parser_commit_to_tentative_parse (parser
);
12336 last_type
= NULL_TREE
;
12338 /* Keep going until we hit the `;' at the end of the simple
12340 saw_declarator
= false;
12341 while (cp_lexer_next_token_is_not (parser
->lexer
,
12345 bool function_definition_p
;
12347 tree auto_result
= NULL_TREE
;
12349 if (saw_declarator
)
12351 /* If we are processing next declarator, comma is expected */
12352 token
= cp_lexer_peek_token (parser
->lexer
);
12353 gcc_assert (token
->type
== CPP_COMMA
);
12354 cp_lexer_consume_token (parser
->lexer
);
12355 if (maybe_range_for_decl
)
12357 *maybe_range_for_decl
= error_mark_node
;
12358 if (comma_loc
== UNKNOWN_LOCATION
)
12359 comma_loc
= token
->location
;
12363 saw_declarator
= true;
12365 /* Parse the init-declarator. */
12366 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
12368 function_definition_allowed_p
,
12369 /*member_p=*/false,
12370 declares_class_or_enum
,
12371 &function_definition_p
,
12372 maybe_range_for_decl
,
12375 /* If an error occurred while parsing tentatively, exit quickly.
12376 (That usually happens when in the body of a function; each
12377 statement is treated as a declaration-statement until proven
12379 if (cp_parser_error_occurred (parser
))
12384 if (last_type
&& last_type
!= error_mark_node
12385 && !same_type_p (auto_result
, last_type
))
12387 /* If the list of declarators contains more than one declarator,
12388 the type of each declared variable is determined as described
12389 above. If the type deduced for the template parameter U is not
12390 the same in each deduction, the program is ill-formed. */
12391 error_at (decl_specifiers
.locations
[ds_type_spec
],
12392 "inconsistent deduction for %qT: %qT and then %qT",
12393 decl_specifiers
.type
, last_type
, auto_result
);
12394 last_type
= error_mark_node
;
12397 last_type
= auto_result
;
12400 /* Handle function definitions specially. */
12401 if (function_definition_p
)
12403 /* If the next token is a `,', then we are probably
12404 processing something like:
12408 which is erroneous. */
12409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12411 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12412 error_at (token
->location
,
12414 " declarations and function-definitions is forbidden");
12416 /* Otherwise, we're done with the list of declarators. */
12419 pop_deferring_access_checks ();
12423 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12424 *maybe_range_for_decl
= decl
;
12425 /* The next token should be either a `,' or a `;'. */
12426 token
= cp_lexer_peek_token (parser
->lexer
);
12427 /* If it's a `,', there are more declarators to come. */
12428 if (token
->type
== CPP_COMMA
)
12429 /* will be consumed next time around */;
12430 /* If it's a `;', we are done. */
12431 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12433 /* Anything else is an error. */
12436 /* If we have already issued an error message we don't need
12437 to issue another one. */
12438 if ((decl
!= error_mark_node
12439 && DECL_INITIAL (decl
) != error_mark_node
)
12440 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12441 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12442 /* Skip tokens until we reach the end of the statement. */
12443 cp_parser_skip_to_end_of_statement (parser
);
12444 /* If the next token is now a `;', consume it. */
12445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12446 cp_lexer_consume_token (parser
->lexer
);
12449 /* After the first time around, a function-definition is not
12450 allowed -- even if it was OK at first. For example:
12455 function_definition_allowed_p
= false;
12458 /* Issue an error message if no declarators are present, and the
12459 decl-specifier-seq does not itself declare a class or
12460 enumeration: [dcl.dcl]/3. */
12461 if (!saw_declarator
)
12463 if (cp_parser_declares_only_class_p (parser
))
12465 if (!declares_class_or_enum
12466 && decl_specifiers
.type
12467 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12468 /* Ensure an error is issued anyway when finish_decltype_type,
12469 called via cp_parser_decl_specifier_seq, returns a class or
12470 an enumeration (c++/51786). */
12471 decl_specifiers
.type
= NULL_TREE
;
12472 shadow_tag (&decl_specifiers
);
12474 /* Perform any deferred access checks. */
12475 perform_deferred_access_checks (tf_warning_or_error
);
12478 /* Consume the `;'. */
12479 if (!maybe_range_for_decl
)
12480 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12481 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12483 if (init_loc
!= UNKNOWN_LOCATION
)
12484 error_at (init_loc
, "initializer in range-based %<for%> loop");
12485 if (comma_loc
!= UNKNOWN_LOCATION
)
12486 error_at (comma_loc
,
12487 "multiple declarations in range-based %<for%> loop");
12491 pop_deferring_access_checks ();
12494 /* Parse a decl-specifier-seq.
12496 decl-specifier-seq:
12497 decl-specifier-seq [opt] decl-specifier
12498 decl-specifier attribute-specifier-seq [opt] (C++11)
12501 storage-class-specifier
12512 Concepts Extension:
12517 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12519 The parser flags FLAGS is used to control type-specifier parsing.
12521 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12524 1: one of the decl-specifiers is an elaborated-type-specifier
12525 (i.e., a type declaration)
12526 2: one of the decl-specifiers is an enum-specifier or a
12527 class-specifier (i.e., a type definition)
12532 cp_parser_decl_specifier_seq (cp_parser
* parser
,
12533 cp_parser_flags flags
,
12534 cp_decl_specifier_seq
*decl_specs
,
12535 int* declares_class_or_enum
)
12537 bool constructor_possible_p
= !parser
->in_declarator_p
;
12538 bool found_decl_spec
= false;
12539 cp_token
*start_token
= NULL
;
12542 /* Clear DECL_SPECS. */
12543 clear_decl_specs (decl_specs
);
12545 /* Assume no class or enumeration type is declared. */
12546 *declares_class_or_enum
= 0;
12548 /* Keep reading specifiers until there are no more to read. */
12551 bool constructor_p
;
12555 /* Peek at the next token. */
12556 token
= cp_lexer_peek_token (parser
->lexer
);
12558 /* Save the first token of the decl spec list for error
12561 start_token
= token
;
12562 /* Handle attributes. */
12563 if (cp_next_tokens_can_be_attribute_p (parser
))
12565 /* Parse the attributes. */
12566 tree attrs
= cp_parser_attributes_opt (parser
);
12568 /* In a sequence of declaration specifiers, c++11 attributes
12569 appertain to the type that precede them. In that case
12572 The attribute-specifier-seq affects the type only for
12573 the declaration it appears in, not other declarations
12574 involving the same type.
12576 But for now let's force the user to position the
12577 attribute either at the beginning of the declaration or
12578 after the declarator-id, which would clearly mean that it
12579 applies to the declarator. */
12580 if (cxx11_attribute_p (attrs
))
12582 if (!found_decl_spec
)
12583 /* The c++11 attribute is at the beginning of the
12584 declaration. It appertains to the entity being
12588 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
12590 /* This is an attribute following a
12591 class-specifier. */
12592 if (decl_specs
->type_definition_p
)
12593 warn_misplaced_attr_for_class_type (token
->location
,
12599 decl_specs
->std_attributes
12600 = chainon (decl_specs
->std_attributes
,
12602 if (decl_specs
->locations
[ds_std_attribute
] == 0)
12603 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
12609 decl_specs
->attributes
12610 = chainon (decl_specs
->attributes
,
12612 if (decl_specs
->locations
[ds_attribute
] == 0)
12613 decl_specs
->locations
[ds_attribute
] = token
->location
;
12616 /* Assume we will find a decl-specifier keyword. */
12617 found_decl_spec
= true;
12618 /* If the next token is an appropriate keyword, we can simply
12619 add it to the list. */
12620 switch (token
->keyword
)
12626 if (!at_class_scope_p ())
12628 error_at (token
->location
, "%<friend%> used outside of class");
12629 cp_lexer_purge_token (parser
->lexer
);
12634 /* Consume the token. */
12635 cp_lexer_consume_token (parser
->lexer
);
12639 case RID_CONSTEXPR
:
12641 cp_lexer_consume_token (parser
->lexer
);
12646 cp_lexer_consume_token (parser
->lexer
);
12649 /* function-specifier:
12656 cp_parser_function_specifier_opt (parser
, decl_specs
);
12663 /* Consume the token. */
12664 cp_lexer_consume_token (parser
->lexer
);
12665 /* A constructor declarator cannot appear in a typedef. */
12666 constructor_possible_p
= false;
12667 /* The "typedef" keyword can only occur in a declaration; we
12668 may as well commit at this point. */
12669 cp_parser_commit_to_tentative_parse (parser
);
12671 if (decl_specs
->storage_class
!= sc_none
)
12672 decl_specs
->conflicting_specifiers_p
= true;
12675 /* storage-class-specifier:
12685 if (cxx_dialect
== cxx98
)
12687 /* Consume the token. */
12688 cp_lexer_consume_token (parser
->lexer
);
12690 /* Complain about `auto' as a storage specifier, if
12691 we're complaining about C++0x compatibility. */
12692 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
12693 " changes meaning in C++11; please remove it");
12695 /* Set the storage class anyway. */
12696 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
12700 /* C++0x auto type-specifier. */
12701 found_decl_spec
= false;
12708 /* Consume the token. */
12709 cp_lexer_consume_token (parser
->lexer
);
12710 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
12714 /* Consume the token. */
12716 cp_lexer_consume_token (parser
->lexer
);
12720 /* We did not yet find a decl-specifier yet. */
12721 found_decl_spec
= false;
12725 if (found_decl_spec
12726 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
12727 && token
->keyword
!= RID_CONSTEXPR
)
12728 error ("decl-specifier invalid in condition");
12731 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
12733 /* Constructors are a special case. The `S' in `S()' is not a
12734 decl-specifier; it is the beginning of the declarator. */
12736 = (!found_decl_spec
12737 && constructor_possible_p
12738 && (cp_parser_constructor_declarator_p
12739 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
12741 /* If we don't have a DECL_SPEC yet, then we must be looking at
12742 a type-specifier. */
12743 if (!found_decl_spec
&& !constructor_p
)
12745 int decl_spec_declares_class_or_enum
;
12746 bool is_cv_qualifier
;
12750 = cp_parser_type_specifier (parser
, flags
,
12752 /*is_declaration=*/true,
12753 &decl_spec_declares_class_or_enum
,
12755 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
12757 /* If this type-specifier referenced a user-defined type
12758 (a typedef, class-name, etc.), then we can't allow any
12759 more such type-specifiers henceforth.
12763 The longest sequence of decl-specifiers that could
12764 possibly be a type name is taken as the
12765 decl-specifier-seq of a declaration. The sequence shall
12766 be self-consistent as described below.
12770 As a general rule, at most one type-specifier is allowed
12771 in the complete decl-specifier-seq of a declaration. The
12772 only exceptions are the following:
12774 -- const or volatile can be combined with any other
12777 -- signed or unsigned can be combined with char, long,
12785 void g (const int Pc);
12787 Here, Pc is *not* part of the decl-specifier seq; it's
12788 the declarator. Therefore, once we see a type-specifier
12789 (other than a cv-qualifier), we forbid any additional
12790 user-defined types. We *do* still allow things like `int
12791 int' to be considered a decl-specifier-seq, and issue the
12792 error message later. */
12793 if (type_spec
&& !is_cv_qualifier
)
12794 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12795 /* A constructor declarator cannot follow a type-specifier. */
12798 constructor_possible_p
= false;
12799 found_decl_spec
= true;
12800 if (!is_cv_qualifier
)
12801 decl_specs
->any_type_specifiers_p
= true;
12805 /* If we still do not have a DECL_SPEC, then there are no more
12806 decl-specifiers. */
12807 if (!found_decl_spec
)
12810 decl_specs
->any_specifiers_p
= true;
12811 /* After we see one decl-specifier, further decl-specifiers are
12812 always optional. */
12813 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12816 /* Don't allow a friend specifier with a class definition. */
12817 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12818 && (*declares_class_or_enum
& 2))
12819 error_at (decl_specs
->locations
[ds_friend
],
12820 "class definition may not be declared a friend");
12823 /* Parse an (optional) storage-class-specifier.
12825 storage-class-specifier:
12834 storage-class-specifier:
12837 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12840 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12842 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12845 if (cxx_dialect
!= cxx98
)
12847 /* Fall through for C++98. */
12854 /* Consume the token. */
12855 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12862 /* Parse an (optional) function-specifier.
12864 function-specifier:
12869 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12870 Updates DECL_SPECS, if it is non-NULL. */
12873 cp_parser_function_specifier_opt (cp_parser
* parser
,
12874 cp_decl_specifier_seq
*decl_specs
)
12876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12877 switch (token
->keyword
)
12880 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12884 /* 14.5.2.3 [temp.mem]
12886 A member function template shall not be virtual. */
12887 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12888 error_at (token
->location
, "templates may not be %<virtual%>");
12890 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12894 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12901 /* Consume the token. */
12902 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12905 /* Parse a linkage-specification.
12907 linkage-specification:
12908 extern string-literal { declaration-seq [opt] }
12909 extern string-literal declaration */
12912 cp_parser_linkage_specification (cp_parser
* parser
)
12916 /* Look for the `extern' keyword. */
12917 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12919 /* Look for the string-literal. */
12920 linkage
= cp_parser_string_literal (parser
, false, false);
12922 /* Transform the literal into an identifier. If the literal is a
12923 wide-character string, or contains embedded NULs, then we can't
12924 handle it as the user wants. */
12925 if (strlen (TREE_STRING_POINTER (linkage
))
12926 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12928 cp_parser_error (parser
, "invalid linkage-specification");
12929 /* Assume C++ linkage. */
12930 linkage
= lang_name_cplusplus
;
12933 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12935 /* We're now using the new linkage. */
12936 push_lang_context (linkage
);
12938 /* If the next token is a `{', then we're using the first
12940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12942 cp_ensure_no_omp_declare_simd (parser
);
12943 cp_ensure_no_oacc_routine (parser
);
12945 /* Consume the `{' token. */
12946 cp_lexer_consume_token (parser
->lexer
);
12947 /* Parse the declarations. */
12948 cp_parser_declaration_seq_opt (parser
);
12949 /* Look for the closing `}'. */
12950 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12952 /* Otherwise, there's just one declaration. */
12955 bool saved_in_unbraced_linkage_specification_p
;
12957 saved_in_unbraced_linkage_specification_p
12958 = parser
->in_unbraced_linkage_specification_p
;
12959 parser
->in_unbraced_linkage_specification_p
= true;
12960 cp_parser_declaration (parser
);
12961 parser
->in_unbraced_linkage_specification_p
12962 = saved_in_unbraced_linkage_specification_p
;
12965 /* We're done with the linkage-specification. */
12966 pop_lang_context ();
12969 /* Parse a static_assert-declaration.
12971 static_assert-declaration:
12972 static_assert ( constant-expression , string-literal ) ;
12973 static_assert ( constant-expression ) ; (C++1Z)
12975 If MEMBER_P, this static_assert is a class member. */
12978 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12983 location_t saved_loc
;
12986 /* Peek at the `static_assert' token so we can keep track of exactly
12987 where the static assertion started. */
12988 token
= cp_lexer_peek_token (parser
->lexer
);
12989 saved_loc
= token
->location
;
12991 /* Look for the `static_assert' keyword. */
12992 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12996 /* We know we are in a static assertion; commit to any tentative
12998 if (cp_parser_parsing_tentatively (parser
))
12999 cp_parser_commit_to_tentative_parse (parser
);
13001 /* Parse the `(' starting the static assertion condition. */
13002 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
13004 /* Parse the constant-expression. Allow a non-constant expression
13005 here in order to give better diagnostics in finish_static_assert. */
13007 cp_parser_constant_expression (parser
,
13008 /*allow_non_constant_p=*/true,
13009 /*non_constant_p=*/&dummy
);
13011 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13013 if (cxx_dialect
< cxx1z
)
13014 pedwarn (input_location
, OPT_Wpedantic
,
13015 "static_assert without a message "
13016 "only available with -std=c++1z or -std=gnu++1z");
13018 cp_lexer_consume_token (parser
->lexer
);
13019 message
= build_string (1, "");
13020 TREE_TYPE (message
) = char_array_type_node
;
13021 fix_string_type (message
);
13025 /* Parse the separating `,'. */
13026 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
13028 /* Parse the string-literal message. */
13029 message
= cp_parser_string_literal (parser
,
13030 /*translate=*/false,
13033 /* A `)' completes the static assertion. */
13034 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13035 cp_parser_skip_to_closing_parenthesis (parser
,
13036 /*recovering=*/true,
13037 /*or_comma=*/false,
13038 /*consume_paren=*/true);
13041 /* A semicolon terminates the declaration. */
13042 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13044 /* Complete the static assertion, which may mean either processing
13045 the static assert now or saving it for template instantiation. */
13046 finish_static_assert (condition
, message
, saved_loc
, member_p
);
13049 /* Parse the expression in decltype ( expression ). */
13052 cp_parser_decltype_expr (cp_parser
*parser
,
13053 bool &id_expression_or_member_access_p
)
13055 cp_token
*id_expr_start_token
;
13058 /* Since we're going to preserve any side-effects from this parse, set up a
13059 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13060 in the expression. */
13061 tentative_firewall
firewall (parser
);
13063 /* First, try parsing an id-expression. */
13064 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
13065 cp_parser_parse_tentatively (parser
);
13066 expr
= cp_parser_id_expression (parser
,
13067 /*template_keyword_p=*/false,
13068 /*check_dependency_p=*/true,
13069 /*template_p=*/NULL
,
13070 /*declarator_p=*/false,
13071 /*optional_p=*/false);
13073 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
13075 bool non_integral_constant_expression_p
= false;
13076 tree id_expression
= expr
;
13078 const char *error_msg
;
13080 if (identifier_p (expr
))
13081 /* Lookup the name we got back from the id-expression. */
13082 expr
= cp_parser_lookup_name_simple (parser
, expr
,
13083 id_expr_start_token
->location
);
13086 && expr
!= error_mark_node
13087 && TREE_CODE (expr
) != TYPE_DECL
13088 && (TREE_CODE (expr
) != BIT_NOT_EXPR
13089 || !TYPE_P (TREE_OPERAND (expr
, 0)))
13090 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13092 /* Complete lookup of the id-expression. */
13093 expr
= (finish_id_expression
13094 (id_expression
, expr
, parser
->scope
, &idk
,
13095 /*integral_constant_expression_p=*/false,
13096 /*allow_non_integral_constant_expression_p=*/true,
13097 &non_integral_constant_expression_p
,
13098 /*template_p=*/false,
13100 /*address_p=*/false,
13101 /*template_arg_p=*/false,
13103 id_expr_start_token
->location
));
13105 if (expr
== error_mark_node
)
13106 /* We found an id-expression, but it was something that we
13107 should not have found. This is an error, not something
13108 we can recover from, so note that we found an
13109 id-expression and we'll recover as gracefully as
13111 id_expression_or_member_access_p
= true;
13115 && expr
!= error_mark_node
13116 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13117 /* We have an id-expression. */
13118 id_expression_or_member_access_p
= true;
13121 if (!id_expression_or_member_access_p
)
13123 /* Abort the id-expression parse. */
13124 cp_parser_abort_tentative_parse (parser
);
13126 /* Parsing tentatively, again. */
13127 cp_parser_parse_tentatively (parser
);
13129 /* Parse a class member access. */
13130 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
13131 /*cast_p=*/false, /*decltype*/true,
13132 /*member_access_only_p=*/true, NULL
);
13135 && expr
!= error_mark_node
13136 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
13137 /* We have an id-expression. */
13138 id_expression_or_member_access_p
= true;
13141 if (id_expression_or_member_access_p
)
13142 /* We have parsed the complete id-expression or member access. */
13143 cp_parser_parse_definitely (parser
);
13146 /* Abort our attempt to parse an id-expression or member access
13148 cp_parser_abort_tentative_parse (parser
);
13150 /* Parse a full expression. */
13151 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
13152 /*decltype_p=*/true);
13158 /* Parse a `decltype' type. Returns the type.
13160 simple-type-specifier:
13161 decltype ( expression )
13163 decltype ( auto ) */
13166 cp_parser_decltype (cp_parser
*parser
)
13169 bool id_expression_or_member_access_p
= false;
13170 const char *saved_message
;
13171 bool saved_integral_constant_expression_p
;
13172 bool saved_non_integral_constant_expression_p
;
13173 bool saved_greater_than_is_operator_p
;
13174 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13176 if (start_token
->type
== CPP_DECLTYPE
)
13178 /* Already parsed. */
13179 cp_lexer_consume_token (parser
->lexer
);
13180 return saved_checks_value (start_token
->u
.tree_check_value
);
13183 /* Look for the `decltype' token. */
13184 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
13185 return error_mark_node
;
13187 /* Parse the opening `('. */
13188 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
13189 return error_mark_node
;
13191 /* decltype (auto) */
13192 if (cxx_dialect
>= cxx14
13193 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
13195 cp_lexer_consume_token (parser
->lexer
);
13196 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13197 return error_mark_node
;
13198 expr
= make_decltype_auto ();
13199 AUTO_IS_DECLTYPE (expr
) = true;
13203 /* Types cannot be defined in a `decltype' expression. Save away the
13205 saved_message
= parser
->type_definition_forbidden_message
;
13207 /* And create the new one. */
13208 parser
->type_definition_forbidden_message
13209 = G_("types may not be defined in %<decltype%> expressions");
13211 /* The restrictions on constant-expressions do not apply inside
13212 decltype expressions. */
13213 saved_integral_constant_expression_p
13214 = parser
->integral_constant_expression_p
;
13215 saved_non_integral_constant_expression_p
13216 = parser
->non_integral_constant_expression_p
;
13217 parser
->integral_constant_expression_p
= false;
13219 /* Within a parenthesized expression, a `>' token is always
13220 the greater-than operator. */
13221 saved_greater_than_is_operator_p
13222 = parser
->greater_than_is_operator_p
;
13223 parser
->greater_than_is_operator_p
= true;
13225 /* Do not actually evaluate the expression. */
13226 ++cp_unevaluated_operand
;
13228 /* Do not warn about problems with the expression. */
13229 ++c_inhibit_evaluation_warnings
;
13231 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
13233 /* Go back to evaluating expressions. */
13234 --cp_unevaluated_operand
;
13235 --c_inhibit_evaluation_warnings
;
13237 /* The `>' token might be the end of a template-id or
13238 template-parameter-list now. */
13239 parser
->greater_than_is_operator_p
13240 = saved_greater_than_is_operator_p
;
13242 /* Restore the old message and the integral constant expression
13244 parser
->type_definition_forbidden_message
= saved_message
;
13245 parser
->integral_constant_expression_p
13246 = saved_integral_constant_expression_p
;
13247 parser
->non_integral_constant_expression_p
13248 = saved_non_integral_constant_expression_p
;
13250 /* Parse to the closing `)'. */
13251 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
13253 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
13254 /*consume_paren=*/true);
13255 return error_mark_node
;
13258 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
13259 tf_warning_or_error
);
13262 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13264 start_token
->type
= CPP_DECLTYPE
;
13265 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13266 start_token
->u
.tree_check_value
->value
= expr
;
13267 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13268 start_token
->keyword
= RID_MAX
;
13269 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
13274 /* Special member functions [gram.special] */
13276 /* Parse a conversion-function-id.
13278 conversion-function-id:
13279 operator conversion-type-id
13281 Returns an IDENTIFIER_NODE representing the operator. */
13284 cp_parser_conversion_function_id (cp_parser
* parser
)
13288 tree saved_qualifying_scope
;
13289 tree saved_object_scope
;
13290 tree pushed_scope
= NULL_TREE
;
13292 /* Look for the `operator' token. */
13293 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13294 return error_mark_node
;
13295 /* When we parse the conversion-type-id, the current scope will be
13296 reset. However, we need that information in able to look up the
13297 conversion function later, so we save it here. */
13298 saved_scope
= parser
->scope
;
13299 saved_qualifying_scope
= parser
->qualifying_scope
;
13300 saved_object_scope
= parser
->object_scope
;
13301 /* We must enter the scope of the class so that the names of
13302 entities declared within the class are available in the
13303 conversion-type-id. For example, consider:
13310 S::operator I() { ... }
13312 In order to see that `I' is a type-name in the definition, we
13313 must be in the scope of `S'. */
13315 pushed_scope
= push_scope (saved_scope
);
13316 /* Parse the conversion-type-id. */
13317 type
= cp_parser_conversion_type_id (parser
);
13318 /* Leave the scope of the class, if any. */
13320 pop_scope (pushed_scope
);
13321 /* Restore the saved scope. */
13322 parser
->scope
= saved_scope
;
13323 parser
->qualifying_scope
= saved_qualifying_scope
;
13324 parser
->object_scope
= saved_object_scope
;
13325 /* If the TYPE is invalid, indicate failure. */
13326 if (type
== error_mark_node
)
13327 return error_mark_node
;
13328 return mangle_conv_op_name_for_type (type
);
13331 /* Parse a conversion-type-id:
13333 conversion-type-id:
13334 type-specifier-seq conversion-declarator [opt]
13336 Returns the TYPE specified. */
13339 cp_parser_conversion_type_id (cp_parser
* parser
)
13342 cp_decl_specifier_seq type_specifiers
;
13343 cp_declarator
*declarator
;
13344 tree type_specified
;
13345 const char *saved_message
;
13347 /* Parse the attributes. */
13348 attributes
= cp_parser_attributes_opt (parser
);
13350 saved_message
= parser
->type_definition_forbidden_message
;
13351 parser
->type_definition_forbidden_message
13352 = G_("types may not be defined in a conversion-type-id");
13354 /* Parse the type-specifiers. */
13355 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
13356 /*is_trailing_return=*/false,
13359 parser
->type_definition_forbidden_message
= saved_message
;
13361 /* If that didn't work, stop. */
13362 if (type_specifiers
.type
== error_mark_node
)
13363 return error_mark_node
;
13364 /* Parse the conversion-declarator. */
13365 declarator
= cp_parser_conversion_declarator_opt (parser
);
13367 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
13368 /*initialized=*/0, &attributes
);
13370 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
13372 /* Don't give this error when parsing tentatively. This happens to
13373 work because we always parse this definitively once. */
13374 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
13375 && type_uses_auto (type_specified
))
13377 if (cxx_dialect
< cxx14
)
13379 error ("invalid use of %<auto%> in conversion operator");
13380 return error_mark_node
;
13382 else if (template_parm_scope_p ())
13383 warning (0, "use of %<auto%> in member template "
13384 "conversion operator can never be deduced");
13387 return type_specified
;
13390 /* Parse an (optional) conversion-declarator.
13392 conversion-declarator:
13393 ptr-operator conversion-declarator [opt]
13397 static cp_declarator
*
13398 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
13400 enum tree_code code
;
13401 tree class_type
, std_attributes
= NULL_TREE
;
13402 cp_cv_quals cv_quals
;
13404 /* We don't know if there's a ptr-operator next, or not. */
13405 cp_parser_parse_tentatively (parser
);
13406 /* Try the ptr-operator. */
13407 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
13409 /* If it worked, look for more conversion-declarators. */
13410 if (cp_parser_parse_definitely (parser
))
13412 cp_declarator
*declarator
;
13414 /* Parse another optional declarator. */
13415 declarator
= cp_parser_conversion_declarator_opt (parser
);
13417 declarator
= cp_parser_make_indirect_declarator
13418 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
13426 /* Parse an (optional) ctor-initializer.
13429 : mem-initializer-list
13431 Returns TRUE iff the ctor-initializer was actually present. */
13434 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
13436 /* If the next token is not a `:', then there is no
13437 ctor-initializer. */
13438 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13440 /* Do default initialization of any bases and members. */
13441 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13442 finish_mem_initializers (NULL_TREE
);
13447 /* Consume the `:' token. */
13448 cp_lexer_consume_token (parser
->lexer
);
13449 /* And the mem-initializer-list. */
13450 cp_parser_mem_initializer_list (parser
);
13455 /* Parse a mem-initializer-list.
13457 mem-initializer-list:
13458 mem-initializer ... [opt]
13459 mem-initializer ... [opt] , mem-initializer-list */
13462 cp_parser_mem_initializer_list (cp_parser
* parser
)
13464 tree mem_initializer_list
= NULL_TREE
;
13465 tree target_ctor
= error_mark_node
;
13466 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13468 /* Let the semantic analysis code know that we are starting the
13469 mem-initializer-list. */
13470 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
13471 error_at (token
->location
,
13472 "only constructors take member initializers");
13474 /* Loop through the list. */
13477 tree mem_initializer
;
13479 token
= cp_lexer_peek_token (parser
->lexer
);
13480 /* Parse the mem-initializer. */
13481 mem_initializer
= cp_parser_mem_initializer (parser
);
13482 /* If the next token is a `...', we're expanding member initializers. */
13483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13485 /* Consume the `...'. */
13486 cp_lexer_consume_token (parser
->lexer
);
13488 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13489 can be expanded but members cannot. */
13490 if (mem_initializer
!= error_mark_node
13491 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
13493 error_at (token
->location
,
13494 "cannot expand initializer for member %<%D%>",
13495 TREE_PURPOSE (mem_initializer
));
13496 mem_initializer
= error_mark_node
;
13499 /* Construct the pack expansion type. */
13500 if (mem_initializer
!= error_mark_node
)
13501 mem_initializer
= make_pack_expansion (mem_initializer
);
13503 if (target_ctor
!= error_mark_node
13504 && mem_initializer
!= error_mark_node
)
13506 error ("mem-initializer for %qD follows constructor delegation",
13507 TREE_PURPOSE (mem_initializer
));
13508 mem_initializer
= error_mark_node
;
13510 /* Look for a target constructor. */
13511 if (mem_initializer
!= error_mark_node
13512 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
13513 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
13515 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
13516 if (mem_initializer_list
)
13518 error ("constructor delegation follows mem-initializer for %qD",
13519 TREE_PURPOSE (mem_initializer_list
));
13520 mem_initializer
= error_mark_node
;
13522 target_ctor
= mem_initializer
;
13524 /* Add it to the list, unless it was erroneous. */
13525 if (mem_initializer
!= error_mark_node
)
13527 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
13528 mem_initializer_list
= mem_initializer
;
13530 /* If the next token is not a `,', we're done. */
13531 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13533 /* Consume the `,' token. */
13534 cp_lexer_consume_token (parser
->lexer
);
13537 /* Perform semantic analysis. */
13538 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13539 finish_mem_initializers (mem_initializer_list
);
13542 /* Parse a mem-initializer.
13545 mem-initializer-id ( expression-list [opt] )
13546 mem-initializer-id braced-init-list
13551 ( expression-list [opt] )
13553 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13554 class) or FIELD_DECL (for a non-static data member) to initialize;
13555 the TREE_VALUE is the expression-list. An empty initialization
13556 list is represented by void_list_node. */
13559 cp_parser_mem_initializer (cp_parser
* parser
)
13561 tree mem_initializer_id
;
13562 tree expression_list
;
13564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13566 /* Find out what is being initialized. */
13567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13569 permerror (token
->location
,
13570 "anachronistic old-style base class initializer");
13571 mem_initializer_id
= NULL_TREE
;
13575 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
13576 if (mem_initializer_id
== error_mark_node
)
13577 return mem_initializer_id
;
13579 member
= expand_member_init (mem_initializer_id
);
13580 if (member
&& !DECL_P (member
))
13581 in_base_initializer
= 1;
13583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13585 bool expr_non_constant_p
;
13586 cp_lexer_set_source_position (parser
->lexer
);
13587 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13588 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13589 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
13590 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
13594 vec
<tree
, va_gc
> *vec
;
13595 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
13597 /*allow_expansion_p=*/true,
13598 /*non_constant_p=*/NULL
);
13600 return error_mark_node
;
13601 expression_list
= build_tree_list_vec (vec
);
13602 release_tree_vector (vec
);
13605 if (expression_list
== error_mark_node
)
13606 return error_mark_node
;
13607 if (!expression_list
)
13608 expression_list
= void_type_node
;
13610 in_base_initializer
= 0;
13612 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
13615 /* Parse a mem-initializer-id.
13617 mem-initializer-id:
13618 :: [opt] nested-name-specifier [opt] class-name
13619 decltype-specifier (C++11)
13622 Returns a TYPE indicating the class to be initialized for the first
13623 production (and the second in C++11). Returns an IDENTIFIER_NODE
13624 indicating the data member to be initialized for the last production. */
13627 cp_parser_mem_initializer_id (cp_parser
* parser
)
13629 bool global_scope_p
;
13630 bool nested_name_specifier_p
;
13631 bool template_p
= false;
13634 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13636 /* `typename' is not allowed in this context ([temp.res]). */
13637 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13639 error_at (token
->location
,
13640 "keyword %<typename%> not allowed in this context (a qualified "
13641 "member initializer is implicitly a type)");
13642 cp_lexer_consume_token (parser
->lexer
);
13644 /* Look for the optional `::' operator. */
13646 = (cp_parser_global_scope_opt (parser
,
13647 /*current_scope_valid_p=*/false)
13649 /* Look for the optional nested-name-specifier. The simplest way to
13654 The keyword `typename' is not permitted in a base-specifier or
13655 mem-initializer; in these contexts a qualified name that
13656 depends on a template-parameter is implicitly assumed to be a
13659 is to assume that we have seen the `typename' keyword at this
13661 nested_name_specifier_p
13662 = (cp_parser_nested_name_specifier_opt (parser
,
13663 /*typename_keyword_p=*/true,
13664 /*check_dependency_p=*/true,
13666 /*is_declaration=*/true)
13668 if (nested_name_specifier_p
)
13669 template_p
= cp_parser_optional_template_keyword (parser
);
13670 /* If there is a `::' operator or a nested-name-specifier, then we
13671 are definitely looking for a class-name. */
13672 if (global_scope_p
|| nested_name_specifier_p
)
13673 return cp_parser_class_name (parser
,
13674 /*typename_keyword_p=*/true,
13675 /*template_keyword_p=*/template_p
,
13677 /*check_dependency_p=*/true,
13678 /*class_head_p=*/false,
13679 /*is_declaration=*/true);
13680 /* Otherwise, we could also be looking for an ordinary identifier. */
13681 cp_parser_parse_tentatively (parser
);
13682 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
13683 /* Try a decltype-specifier. */
13684 id
= cp_parser_decltype (parser
);
13686 /* Otherwise, try a class-name. */
13687 id
= cp_parser_class_name (parser
,
13688 /*typename_keyword_p=*/true,
13689 /*template_keyword_p=*/false,
13691 /*check_dependency_p=*/true,
13692 /*class_head_p=*/false,
13693 /*is_declaration=*/true);
13694 /* If we found one, we're done. */
13695 if (cp_parser_parse_definitely (parser
))
13697 /* Otherwise, look for an ordinary identifier. */
13698 return cp_parser_identifier (parser
);
13701 /* Overloading [gram.over] */
13703 /* Parse an operator-function-id.
13705 operator-function-id:
13708 Returns an IDENTIFIER_NODE for the operator which is a
13709 human-readable spelling of the identifier, e.g., `operator +'. */
13712 cp_parser_operator_function_id (cp_parser
* parser
)
13714 /* Look for the `operator' keyword. */
13715 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13716 return error_mark_node
;
13717 /* And then the name of the operator itself. */
13718 return cp_parser_operator (parser
);
13721 /* Return an identifier node for a user-defined literal operator.
13722 The suffix identifier is chained to the operator name identifier. */
13725 cp_literal_operator_id (const char* name
)
13728 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
13729 + strlen (name
) + 10);
13730 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
13731 identifier
= get_identifier (buffer
);
13736 /* Parse an operator.
13739 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13740 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13741 || ++ -- , ->* -> () []
13748 Returns an IDENTIFIER_NODE for the operator which is a
13749 human-readable spelling of the identifier, e.g., `operator +'. */
13752 cp_parser_operator (cp_parser
* parser
)
13754 tree id
= NULL_TREE
;
13758 /* Peek at the next token. */
13759 token
= cp_lexer_peek_token (parser
->lexer
);
13761 location_t start_loc
= token
->location
;
13763 /* Figure out which operator we have. */
13764 switch (token
->type
)
13770 /* The keyword should be either `new' or `delete'. */
13771 if (token
->keyword
== RID_NEW
)
13773 else if (token
->keyword
== RID_DELETE
)
13778 /* Consume the `new' or `delete' token. */
13779 location_t end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
13781 /* Peek at the next token. */
13782 token
= cp_lexer_peek_token (parser
->lexer
);
13783 /* If it's a `[' token then this is the array variant of the
13785 if (token
->type
== CPP_OPEN_SQUARE
)
13787 /* Consume the `[' token. */
13788 cp_lexer_consume_token (parser
->lexer
);
13789 /* Look for the `]' token. */
13790 end_loc
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
13791 RT_CLOSE_SQUARE
)->location
;
13792 id
= ansi_opname (op
== NEW_EXPR
13793 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
13795 /* Otherwise, we have the non-array variant. */
13797 id
= ansi_opname (op
);
13799 location_t loc
= make_location (start_loc
, start_loc
, end_loc
);
13801 return cp_expr (id
, loc
);
13805 id
= ansi_opname (PLUS_EXPR
);
13809 id
= ansi_opname (MINUS_EXPR
);
13813 id
= ansi_opname (MULT_EXPR
);
13817 id
= ansi_opname (TRUNC_DIV_EXPR
);
13821 id
= ansi_opname (TRUNC_MOD_EXPR
);
13825 id
= ansi_opname (BIT_XOR_EXPR
);
13829 id
= ansi_opname (BIT_AND_EXPR
);
13833 id
= ansi_opname (BIT_IOR_EXPR
);
13837 id
= ansi_opname (BIT_NOT_EXPR
);
13841 id
= ansi_opname (TRUTH_NOT_EXPR
);
13845 id
= ansi_assopname (NOP_EXPR
);
13849 id
= ansi_opname (LT_EXPR
);
13853 id
= ansi_opname (GT_EXPR
);
13857 id
= ansi_assopname (PLUS_EXPR
);
13861 id
= ansi_assopname (MINUS_EXPR
);
13865 id
= ansi_assopname (MULT_EXPR
);
13869 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13873 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13877 id
= ansi_assopname (BIT_XOR_EXPR
);
13881 id
= ansi_assopname (BIT_AND_EXPR
);
13885 id
= ansi_assopname (BIT_IOR_EXPR
);
13889 id
= ansi_opname (LSHIFT_EXPR
);
13893 id
= ansi_opname (RSHIFT_EXPR
);
13896 case CPP_LSHIFT_EQ
:
13897 id
= ansi_assopname (LSHIFT_EXPR
);
13900 case CPP_RSHIFT_EQ
:
13901 id
= ansi_assopname (RSHIFT_EXPR
);
13905 id
= ansi_opname (EQ_EXPR
);
13909 id
= ansi_opname (NE_EXPR
);
13913 id
= ansi_opname (LE_EXPR
);
13916 case CPP_GREATER_EQ
:
13917 id
= ansi_opname (GE_EXPR
);
13921 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13925 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13928 case CPP_PLUS_PLUS
:
13929 id
= ansi_opname (POSTINCREMENT_EXPR
);
13932 case CPP_MINUS_MINUS
:
13933 id
= ansi_opname (PREDECREMENT_EXPR
);
13937 id
= ansi_opname (COMPOUND_EXPR
);
13940 case CPP_DEREF_STAR
:
13941 id
= ansi_opname (MEMBER_REF
);
13945 id
= ansi_opname (COMPONENT_REF
);
13948 case CPP_OPEN_PAREN
:
13949 /* Consume the `('. */
13950 cp_lexer_consume_token (parser
->lexer
);
13951 /* Look for the matching `)'. */
13952 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13953 return ansi_opname (CALL_EXPR
);
13955 case CPP_OPEN_SQUARE
:
13956 /* Consume the `['. */
13957 cp_lexer_consume_token (parser
->lexer
);
13958 /* Look for the matching `]'. */
13959 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13960 return ansi_opname (ARRAY_REF
);
13962 case CPP_UTF8STRING
:
13963 case CPP_UTF8STRING_USERDEF
:
13969 case CPP_STRING_USERDEF
:
13970 case CPP_WSTRING_USERDEF
:
13971 case CPP_STRING16_USERDEF
:
13972 case CPP_STRING32_USERDEF
:
13974 tree str
, string_tree
;
13977 if (cxx_dialect
== cxx98
)
13978 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13980 /* Consume the string. */
13981 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13982 /*wide_ok=*/true, /*lookup_udlit=*/false);
13983 if (str
== error_mark_node
)
13984 return error_mark_node
;
13985 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13987 string_tree
= USERDEF_LITERAL_VALUE (str
);
13988 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13993 /* Look for the suffix identifier. */
13994 token
= cp_lexer_peek_token (parser
->lexer
);
13995 if (token
->type
== CPP_NAME
)
13996 id
= cp_parser_identifier (parser
);
13997 else if (token
->type
== CPP_KEYWORD
)
13999 error ("unexpected keyword;"
14000 " remove space between quotes and suffix identifier");
14001 return error_mark_node
;
14005 error ("expected suffix identifier");
14006 return error_mark_node
;
14009 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14010 (TREE_TYPE (TREE_TYPE (string_tree
))));
14011 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
14014 error ("expected empty string after %<operator%> keyword");
14015 return error_mark_node
;
14017 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
14020 error ("invalid encoding prefix in literal operator");
14021 return error_mark_node
;
14023 if (id
!= error_mark_node
)
14025 const char *name
= IDENTIFIER_POINTER (id
);
14026 id
= cp_literal_operator_id (name
);
14032 /* Anything else is an error. */
14036 /* If we have selected an identifier, we need to consume the
14039 cp_lexer_consume_token (parser
->lexer
);
14040 /* Otherwise, no valid operator name was present. */
14043 cp_parser_error (parser
, "expected operator");
14044 id
= error_mark_node
;
14047 return cp_expr (id
, start_loc
);
14050 /* Parse a template-declaration.
14052 template-declaration:
14053 export [opt] template < template-parameter-list > declaration
14055 If MEMBER_P is TRUE, this template-declaration occurs within a
14058 The grammar rule given by the standard isn't correct. What
14059 is really meant is:
14061 template-declaration:
14062 export [opt] template-parameter-list-seq
14063 decl-specifier-seq [opt] init-declarator [opt] ;
14064 export [opt] template-parameter-list-seq
14065 function-definition
14067 template-parameter-list-seq:
14068 template-parameter-list-seq [opt]
14069 template < template-parameter-list >
14071 Concept Extensions:
14073 template-parameter-list-seq:
14074 template < template-parameter-list > requires-clause [opt]
14077 requires logical-or-expression */
14080 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
14082 /* Check for `export'. */
14083 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
14085 /* Consume the `export' token. */
14086 cp_lexer_consume_token (parser
->lexer
);
14087 /* Warn that we do not support `export'. */
14088 warning (0, "keyword %<export%> not implemented, and will be ignored");
14091 cp_parser_template_declaration_after_export (parser
, member_p
);
14094 /* Parse a template-parameter-list.
14096 template-parameter-list:
14098 template-parameter-list , template-parameter
14100 Returns a TREE_LIST. Each node represents a template parameter.
14101 The nodes are connected via their TREE_CHAINs. */
14104 cp_parser_template_parameter_list (cp_parser
* parser
)
14106 tree parameter_list
= NULL_TREE
;
14108 begin_template_parm_list ();
14110 /* The loop below parses the template parms. We first need to know
14111 the total number of template parms to be able to compute proper
14112 canonical types of each dependent type. So after the loop, when
14113 we know the total number of template parms,
14114 end_template_parm_list computes the proper canonical types and
14115 fixes up the dependent types accordingly. */
14120 bool is_parameter_pack
;
14121 location_t parm_loc
;
14123 /* Parse the template-parameter. */
14124 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14125 parameter
= cp_parser_template_parameter (parser
,
14127 &is_parameter_pack
);
14128 /* Add it to the list. */
14129 if (parameter
!= error_mark_node
)
14130 parameter_list
= process_template_parm (parameter_list
,
14134 is_parameter_pack
);
14137 tree err_parm
= build_tree_list (parameter
, parameter
);
14138 parameter_list
= chainon (parameter_list
, err_parm
);
14141 /* If the next token is not a `,', we're done. */
14142 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14144 /* Otherwise, consume the `,' token. */
14145 cp_lexer_consume_token (parser
->lexer
);
14148 return end_template_parm_list (parameter_list
);
14151 /* Parse a introduction-list.
14154 introduced-parameter
14155 introduction-list , introduced-parameter
14157 introduced-parameter:
14158 ...[opt] identifier
14160 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14161 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14162 WILDCARD_DECL will also have DECL_NAME set and token location in
14163 DECL_SOURCE_LOCATION. */
14166 cp_parser_introduction_list (cp_parser
*parser
)
14168 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
14172 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
14174 cp_lexer_consume_token (parser
->lexer
);
14176 /* Build placeholder. */
14177 tree parm
= build_nt (WILDCARD_DECL
);
14178 DECL_SOURCE_LOCATION (parm
)
14179 = cp_lexer_peek_token (parser
->lexer
)->location
;
14180 DECL_NAME (parm
) = cp_parser_identifier (parser
);
14181 WILDCARD_PACK_P (parm
) = is_pack
;
14182 vec_safe_push (introduction_vec
, parm
);
14184 /* If the next token is not a `,', we're done. */
14185 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14187 /* Otherwise, consume the `,' token. */
14188 cp_lexer_consume_token (parser
->lexer
);
14191 /* Convert the vec into a TREE_VEC. */
14192 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
14195 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
14196 TREE_VEC_ELT (introduction_list
, n
) = parm
;
14198 release_tree_vector (introduction_vec
);
14199 return introduction_list
;
14202 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14203 is an abstract declarator. */
14205 static inline cp_declarator
*
14206 get_id_declarator (cp_declarator
*declarator
)
14208 cp_declarator
*d
= declarator
;
14209 while (d
&& d
->kind
!= cdk_id
)
14214 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14215 is an abstract declarator. */
14218 get_unqualified_id (cp_declarator
*declarator
)
14220 declarator
= get_id_declarator (declarator
);
14222 return declarator
->u
.id
.unqualified_name
;
14227 /* Returns true if DECL represents a constrained-parameter. */
14230 is_constrained_parameter (tree decl
)
14233 && TREE_CODE (decl
) == TYPE_DECL
14234 && CONSTRAINED_PARM_CONCEPT (decl
)
14235 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
14238 /* Returns true if PARM declares a constrained-parameter. */
14241 is_constrained_parameter (cp_parameter_declarator
*parm
)
14243 return is_constrained_parameter (parm
->decl_specifiers
.type
);
14246 /* Check that the type parameter is only a declarator-id, and that its
14247 type is not cv-qualified. */
14250 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
14251 cp_parameter_declarator
*parm
)
14253 if (!parm
->declarator
)
14256 if (parm
->declarator
->kind
!= cdk_id
)
14258 cp_parser_error (parser
, "invalid constrained type parameter");
14262 /* Don't allow cv-qualified type parameters. */
14263 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
14264 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
14266 cp_parser_error (parser
, "cv-qualified type parameter");
14273 /* Finish parsing/processing a template type parameter and checking
14274 various restrictions. */
14277 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
14279 cp_parameter_declarator
* parmdecl
)
14281 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14282 return finish_template_type_parm (class_type_node
, id
);
14284 return error_mark_node
;
14288 finish_constrained_template_template_parm (tree proto
, tree id
)
14290 /* FIXME: This should probably be copied, and we may need to adjust
14291 the template parameter depths. */
14292 tree saved_parms
= current_template_parms
;
14293 begin_template_parm_list ();
14294 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
14295 end_template_parm_list ();
14297 tree parm
= finish_template_template_parm (class_type_node
, id
);
14298 current_template_parms
= saved_parms
;
14303 /* Finish parsing/processing a template template parameter by borrowing
14304 the template parameter list from the prototype parameter. */
14307 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
14310 cp_parameter_declarator
*parmdecl
)
14312 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
14313 return error_mark_node
;
14314 return finish_constrained_template_template_parm (proto
, id
);
14317 /* Create a new non-type template parameter from the given PARM
14321 constrained_non_type_template_parm (bool *is_non_type
,
14322 cp_parameter_declarator
*parm
)
14324 *is_non_type
= true;
14325 cp_declarator
*decl
= parm
->declarator
;
14326 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
14327 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
14328 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
14331 /* Build a constrained template parameter based on the PARMDECL
14332 declarator. The type of PARMDECL is the constrained type, which
14333 refers to the prototype template parameter that ultimately
14334 specifies the type of the declared parameter. */
14337 finish_constrained_parameter (cp_parser
*parser
,
14338 cp_parameter_declarator
*parmdecl
,
14340 bool *is_parameter_pack
)
14342 tree decl
= parmdecl
->decl_specifiers
.type
;
14343 tree id
= get_unqualified_id (parmdecl
->declarator
);
14344 tree def
= parmdecl
->default_argument
;
14345 tree proto
= DECL_INITIAL (decl
);
14347 /* A template parameter constrained by a variadic concept shall also
14348 be declared as a template parameter pack. */
14349 bool is_variadic
= template_parameter_pack_p (proto
);
14350 if (is_variadic
&& !*is_parameter_pack
)
14351 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
14353 /* Build the parameter. Return an error if the declarator was invalid. */
14355 if (TREE_CODE (proto
) == TYPE_DECL
)
14356 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
14357 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
14358 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
14361 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
14362 if (parm
== error_mark_node
)
14363 return error_mark_node
;
14365 /* Finish the parameter decl and create a node attaching the
14366 default argument and constraint. */
14367 parm
= build_tree_list (def
, parm
);
14368 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
14373 /* Returns true if the parsed type actually represents the declaration
14374 of a type template-parameter. */
14377 declares_constrained_type_template_parameter (tree type
)
14379 return (is_constrained_parameter (type
)
14380 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
14384 /* Returns true if the parsed type actually represents the declaration of
14385 a template template-parameter. */
14388 declares_constrained_template_template_parameter (tree type
)
14390 return (is_constrained_parameter (type
)
14391 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
14394 /* Parse a default argument for a type template-parameter.
14395 Note that diagnostics are handled in cp_parser_template_parameter. */
14398 cp_parser_default_type_template_argument (cp_parser
*parser
)
14400 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14402 /* Consume the `=' token. */
14403 cp_lexer_consume_token (parser
->lexer
);
14405 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14407 /* Parse the default-argument. */
14408 push_deferring_access_checks (dk_no_deferred
);
14409 tree default_argument
= cp_parser_type_id (parser
);
14410 pop_deferring_access_checks ();
14412 if (flag_concepts
&& type_uses_auto (default_argument
))
14414 error_at (token
->location
,
14415 "invalid use of %<auto%> in default template argument");
14416 return error_mark_node
;
14419 return default_argument
;
14422 /* Parse a default argument for a template template-parameter. */
14425 cp_parser_default_template_template_argument (cp_parser
*parser
)
14427 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
14431 /* Consume the `='. */
14432 cp_lexer_consume_token (parser
->lexer
);
14433 /* Parse the id-expression. */
14434 push_deferring_access_checks (dk_no_deferred
);
14435 /* save token before parsing the id-expression, for error
14437 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
14438 tree default_argument
14439 = cp_parser_id_expression (parser
,
14440 /*template_keyword_p=*/false,
14441 /*check_dependency_p=*/true,
14442 /*template_p=*/&is_template
,
14443 /*declarator_p=*/false,
14444 /*optional_p=*/false);
14445 if (TREE_CODE (default_argument
) == TYPE_DECL
)
14446 /* If the id-expression was a template-id that refers to
14447 a template-class, we already have the declaration here,
14448 so no further lookup is needed. */
14451 /* Look up the name. */
14453 = cp_parser_lookup_name (parser
, default_argument
,
14455 /*is_template=*/is_template
,
14456 /*is_namespace=*/false,
14457 /*check_dependency=*/true,
14458 /*ambiguous_decls=*/NULL
,
14460 /* See if the default argument is valid. */
14461 default_argument
= check_template_template_default_arg (default_argument
);
14462 pop_deferring_access_checks ();
14463 return default_argument
;
14466 /* Parse a template-parameter.
14468 template-parameter:
14470 parameter-declaration
14472 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14473 the parameter. The TREE_PURPOSE is the default value, if any.
14474 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14475 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14476 set to true iff this parameter is a parameter pack. */
14479 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
14480 bool *is_parameter_pack
)
14483 cp_parameter_declarator
*parameter_declarator
;
14486 /* Assume it is a type parameter or a template parameter. */
14487 *is_non_type
= false;
14488 /* Assume it not a parameter pack. */
14489 *is_parameter_pack
= false;
14490 /* Peek at the next token. */
14491 token
= cp_lexer_peek_token (parser
->lexer
);
14492 /* If it is `class' or `template', we have a type-parameter. */
14493 if (token
->keyword
== RID_TEMPLATE
)
14494 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14495 /* If it is `class' or `typename' we do not know yet whether it is a
14496 type parameter or a non-type parameter. Consider:
14498 template <typename T, typename T::X X> ...
14502 template <class C, class D*> ...
14504 Here, the first parameter is a type parameter, and the second is
14505 a non-type parameter. We can tell by looking at the token after
14506 the identifier -- if it is a `,', `=', or `>' then we have a type
14508 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
14510 /* Peek at the token after `class' or `typename'. */
14511 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14512 /* If it's an ellipsis, we have a template type parameter
14514 if (token
->type
== CPP_ELLIPSIS
)
14515 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14516 /* If it's an identifier, skip it. */
14517 if (token
->type
== CPP_NAME
)
14518 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
14519 /* Now, see if the token looks like the end of a template
14521 if (token
->type
== CPP_COMMA
14522 || token
->type
== CPP_EQ
14523 || token
->type
== CPP_GREATER
)
14524 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14527 /* Otherwise, it is a non-type parameter or a constrained parameter.
14531 When parsing a default template-argument for a non-type
14532 template-parameter, the first non-nested `>' is taken as the end
14533 of the template parameter-list rather than a greater-than
14535 parameter_declarator
14536 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
14537 /*parenthesized_p=*/NULL
);
14539 if (!parameter_declarator
)
14540 return error_mark_node
;
14542 /* If the parameter declaration is marked as a parameter pack, set
14543 *IS_PARAMETER_PACK to notify the caller. */
14544 if (parameter_declarator
->template_parameter_pack_p
)
14545 *is_parameter_pack
= true;
14547 if (parameter_declarator
->default_argument
)
14549 /* Can happen in some cases of erroneous input (c++/34892). */
14550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14551 /* Consume the `...' for better error recovery. */
14552 cp_lexer_consume_token (parser
->lexer
);
14555 // The parameter may have been constrained.
14556 if (is_constrained_parameter (parameter_declarator
))
14557 return finish_constrained_parameter (parser
,
14558 parameter_declarator
,
14560 is_parameter_pack
);
14562 // Now we're sure that the parameter is a non-type parameter.
14563 *is_non_type
= true;
14565 parm
= grokdeclarator (parameter_declarator
->declarator
,
14566 ¶meter_declarator
->decl_specifiers
,
14567 TPARM
, /*initialized=*/0,
14568 /*attrlist=*/NULL
);
14569 if (parm
== error_mark_node
)
14570 return error_mark_node
;
14572 return build_tree_list (parameter_declarator
->default_argument
, parm
);
14575 /* Parse a type-parameter.
14578 class identifier [opt]
14579 class identifier [opt] = type-id
14580 typename identifier [opt]
14581 typename identifier [opt] = type-id
14582 template < template-parameter-list > class identifier [opt]
14583 template < template-parameter-list > class identifier [opt]
14586 GNU Extension (variadic templates):
14589 class ... identifier [opt]
14590 typename ... identifier [opt]
14592 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14593 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14594 the declaration of the parameter.
14596 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14599 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
14604 /* Look for a keyword to tell us what kind of parameter this is. */
14605 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
14607 return error_mark_node
;
14609 switch (token
->keyword
)
14615 tree default_argument
;
14617 /* If the next token is an ellipsis, we have a template
14619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14621 /* Consume the `...' token. */
14622 cp_lexer_consume_token (parser
->lexer
);
14623 maybe_warn_variadic_templates ();
14625 *is_parameter_pack
= true;
14628 /* If the next token is an identifier, then it names the
14630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14631 identifier
= cp_parser_identifier (parser
);
14633 identifier
= NULL_TREE
;
14635 /* Create the parameter. */
14636 parameter
= finish_template_type_parm (class_type_node
, identifier
);
14638 /* If the next token is an `=', we have a default argument. */
14639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14642 = cp_parser_default_type_template_argument (parser
);
14644 /* Template parameter packs cannot have default
14646 if (*is_parameter_pack
)
14649 error_at (token
->location
,
14650 "template parameter pack %qD cannot have a "
14651 "default argument", identifier
);
14653 error_at (token
->location
,
14654 "template parameter packs cannot have "
14655 "default arguments");
14656 default_argument
= NULL_TREE
;
14658 else if (check_for_bare_parameter_packs (default_argument
))
14659 default_argument
= error_mark_node
;
14662 default_argument
= NULL_TREE
;
14664 /* Create the combined representation of the parameter and the
14665 default argument. */
14666 parameter
= build_tree_list (default_argument
, parameter
);
14673 tree default_argument
;
14675 /* Look for the `<'. */
14676 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14677 /* Parse the template-parameter-list. */
14678 cp_parser_template_parameter_list (parser
);
14679 /* Look for the `>'. */
14680 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14682 // If template requirements are present, parse them.
14683 tree reqs
= get_shorthand_constraints (current_template_parms
);
14684 if (tree r
= cp_parser_requires_clause_opt (parser
))
14685 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
14686 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
14688 /* Look for the `class' or 'typename' keywords. */
14689 cp_parser_type_parameter_key (parser
);
14690 /* If the next token is an ellipsis, we have a template
14692 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14694 /* Consume the `...' token. */
14695 cp_lexer_consume_token (parser
->lexer
);
14696 maybe_warn_variadic_templates ();
14698 *is_parameter_pack
= true;
14700 /* If the next token is an `=', then there is a
14701 default-argument. If the next token is a `>', we are at
14702 the end of the parameter-list. If the next token is a `,',
14703 then we are at the end of this parameter. */
14704 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
14705 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
14706 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14708 identifier
= cp_parser_identifier (parser
);
14709 /* Treat invalid names as if the parameter were nameless. */
14710 if (identifier
== error_mark_node
)
14711 identifier
= NULL_TREE
;
14714 identifier
= NULL_TREE
;
14716 /* Create the template parameter. */
14717 parameter
= finish_template_template_parm (class_type_node
,
14720 /* If the next token is an `=', then there is a
14721 default-argument. */
14722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14725 = cp_parser_default_template_template_argument (parser
);
14727 /* Template parameter packs cannot have default
14729 if (*is_parameter_pack
)
14732 error_at (token
->location
,
14733 "template parameter pack %qD cannot "
14734 "have a default argument",
14737 error_at (token
->location
, "template parameter packs cannot "
14738 "have default arguments");
14739 default_argument
= NULL_TREE
;
14743 default_argument
= NULL_TREE
;
14745 /* Create the combined representation of the parameter and the
14746 default argument. */
14747 parameter
= build_tree_list (default_argument
, parameter
);
14752 gcc_unreachable ();
14759 /* Parse a template-id.
14762 template-name < template-argument-list [opt] >
14764 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14765 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14766 returned. Otherwise, if the template-name names a function, or set
14767 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14768 names a class, returns a TYPE_DECL for the specialization.
14770 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14771 uninstantiated templates. */
14774 cp_parser_template_id (cp_parser
*parser
,
14775 bool template_keyword_p
,
14776 bool check_dependency_p
,
14777 enum tag_types tag_type
,
14778 bool is_declaration
)
14783 cp_token_position start_of_id
= 0;
14784 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
14785 bool is_identifier
;
14787 /* If the next token corresponds to a template-id, there is no need
14789 next_token
= cp_lexer_peek_token (parser
->lexer
);
14790 if (next_token
->type
== CPP_TEMPLATE_ID
)
14792 cp_lexer_consume_token (parser
->lexer
);
14793 return saved_checks_value (next_token
->u
.tree_check_value
);
14796 /* Avoid performing name lookup if there is no possibility of
14797 finding a template-id. */
14798 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
14799 || (next_token
->type
== CPP_NAME
14800 && !cp_parser_nth_token_starts_template_argument_list_p
14803 cp_parser_error (parser
, "expected template-id");
14804 return error_mark_node
;
14807 /* Remember where the template-id starts. */
14808 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
14809 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
14811 push_deferring_access_checks (dk_deferred
);
14813 /* Parse the template-name. */
14814 is_identifier
= false;
14815 templ
= cp_parser_template_name (parser
, template_keyword_p
,
14816 check_dependency_p
,
14820 if (templ
== error_mark_node
|| is_identifier
)
14822 pop_deferring_access_checks ();
14826 /* Since we're going to preserve any side-effects from this parse, set up a
14827 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14828 in the template arguments. */
14829 tentative_firewall
firewall (parser
);
14831 /* If we find the sequence `[:' after a template-name, it's probably
14832 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14833 parse correctly the argument list. */
14834 next_token
= cp_lexer_peek_token (parser
->lexer
);
14835 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14836 if (next_token
->type
== CPP_OPEN_SQUARE
14837 && next_token
->flags
& DIGRAPH
14838 && next_token_2
->type
== CPP_COLON
14839 && !(next_token_2
->flags
& PREV_WHITE
))
14841 cp_parser_parse_tentatively (parser
);
14842 /* Change `:' into `::'. */
14843 next_token_2
->type
= CPP_SCOPE
;
14844 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14846 cp_lexer_consume_token (parser
->lexer
);
14848 /* Parse the arguments. */
14849 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14850 if (!cp_parser_parse_definitely (parser
))
14852 /* If we couldn't parse an argument list, then we revert our changes
14853 and return simply an error. Maybe this is not a template-id
14855 next_token_2
->type
= CPP_COLON
;
14856 cp_parser_error (parser
, "expected %<<%>");
14857 pop_deferring_access_checks ();
14858 return error_mark_node
;
14860 /* Otherwise, emit an error about the invalid digraph, but continue
14861 parsing because we got our argument list. */
14862 if (permerror (next_token
->location
,
14863 "%<<::%> cannot begin a template-argument list"))
14865 static bool hint
= false;
14866 inform (next_token
->location
,
14867 "%<<:%> is an alternate spelling for %<[%>."
14868 " Insert whitespace between %<<%> and %<::%>");
14869 if (!hint
&& !flag_permissive
)
14871 inform (next_token
->location
, "(if you use %<-fpermissive%> "
14872 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14873 "accept your code)");
14880 /* Look for the `<' that starts the template-argument-list. */
14881 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
14883 pop_deferring_access_checks ();
14884 return error_mark_node
;
14886 /* Parse the arguments. */
14887 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14890 /* Build a representation of the specialization. */
14891 if (identifier_p (templ
))
14892 template_id
= build_min_nt_loc (next_token
->location
,
14895 else if (DECL_TYPE_TEMPLATE_P (templ
)
14896 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
14898 bool entering_scope
;
14899 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14900 template (rather than some instantiation thereof) only if
14901 is not nested within some other construct. For example, in
14902 "template <typename T> void f(T) { A<T>::", A<T> is just an
14903 instantiation of A. */
14904 entering_scope
= (template_parm_scope_p ()
14905 && cp_lexer_next_token_is (parser
->lexer
,
14908 = finish_template_type (templ
, arguments
, entering_scope
);
14910 /* A template-like identifier may be a partial concept id. */
14911 else if (flag_concepts
14912 && (template_id
= (cp_parser_maybe_partial_concept_id
14913 (parser
, templ
, arguments
))))
14914 return template_id
;
14915 else if (variable_template_p (templ
))
14917 template_id
= lookup_template_variable (templ
, arguments
);
14918 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14919 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14923 /* If it's not a class-template or a template-template, it should be
14924 a function-template. */
14925 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
14926 || TREE_CODE (templ
) == OVERLOAD
14927 || BASELINK_P (templ
)));
14929 template_id
= lookup_template_function (templ
, arguments
);
14930 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14931 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14934 /* If parsing tentatively, replace the sequence of tokens that makes
14935 up the template-id with a CPP_TEMPLATE_ID token. That way,
14936 should we re-parse the token stream, we will not have to repeat
14937 the effort required to do the parse, nor will we issue duplicate
14938 error messages about problems during instantiation of the
14941 /* Don't do this if we had a parse error in a declarator; re-parsing
14942 might succeed if a name changes meaning (60361). */
14943 && !(cp_parser_error_occurred (parser
)
14944 && cp_parser_parsing_tentatively (parser
)
14945 && parser
->in_declarator_p
))
14947 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
14949 /* Reset the contents of the START_OF_ID token. */
14950 token
->type
= CPP_TEMPLATE_ID
;
14952 /* Update the location to be of the form:
14953 template-name < template-argument-list [opt] >
14954 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14955 with caret == start at the start of the template-name,
14956 ranging until the closing '>'. */
14957 location_t finish_loc
14958 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
14959 location_t combined_loc
14960 = make_location (token
->location
, token
->location
, finish_loc
);
14961 token
->location
= combined_loc
;
14963 /* Retrieve any deferred checks. Do not pop this access checks yet
14964 so the memory will not be reclaimed during token replacing below. */
14965 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14966 token
->u
.tree_check_value
->value
= template_id
;
14967 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14968 token
->keyword
= RID_MAX
;
14970 /* Purge all subsequent tokens. */
14971 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
14973 /* ??? Can we actually assume that, if template_id ==
14974 error_mark_node, we will have issued a diagnostic to the
14975 user, as opposed to simply marking the tentative parse as
14977 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
14978 error_at (token
->location
, "parse error in template argument list");
14981 pop_to_parent_deferring_access_checks ();
14982 return template_id
;
14985 /* Parse a template-name.
14990 The standard should actually say:
14994 operator-function-id
14996 A defect report has been filed about this issue.
14998 A conversion-function-id cannot be a template name because they cannot
14999 be part of a template-id. In fact, looking at this code:
15001 a.operator K<int>()
15003 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15004 It is impossible to call a templated conversion-function-id with an
15005 explicit argument list, since the only allowed template parameter is
15006 the type to which it is converting.
15008 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15009 `template' keyword, in a construction like:
15013 In that case `f' is taken to be a template-name, even though there
15014 is no way of knowing for sure.
15016 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15017 name refers to a set of overloaded functions, at least one of which
15018 is a template, or an IDENTIFIER_NODE with the name of the template,
15019 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15020 names are looked up inside uninstantiated templates. */
15023 cp_parser_template_name (cp_parser
* parser
,
15024 bool template_keyword_p
,
15025 bool check_dependency_p
,
15026 bool is_declaration
,
15027 enum tag_types tag_type
,
15028 bool *is_identifier
)
15033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15035 /* If the next token is `operator', then we have either an
15036 operator-function-id or a conversion-function-id. */
15037 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
15039 /* We don't know whether we're looking at an
15040 operator-function-id or a conversion-function-id. */
15041 cp_parser_parse_tentatively (parser
);
15042 /* Try an operator-function-id. */
15043 identifier
= cp_parser_operator_function_id (parser
);
15044 /* If that didn't work, try a conversion-function-id. */
15045 if (!cp_parser_parse_definitely (parser
))
15047 cp_parser_error (parser
, "expected template-name");
15048 return error_mark_node
;
15051 /* Look for the identifier. */
15053 identifier
= cp_parser_identifier (parser
);
15055 /* If we didn't find an identifier, we don't have a template-id. */
15056 if (identifier
== error_mark_node
)
15057 return error_mark_node
;
15059 /* If the name immediately followed the `template' keyword, then it
15060 is a template-name. However, if the next token is not `<', then
15061 we do not treat it as a template-name, since it is not being used
15062 as part of a template-id. This enables us to handle constructs
15065 template <typename T> struct S { S(); };
15066 template <typename T> S<T>::S();
15068 correctly. We would treat `S' as a template -- if it were `S<T>'
15069 -- but we do not if there is no `<'. */
15071 if (processing_template_decl
15072 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
15074 /* In a declaration, in a dependent context, we pretend that the
15075 "template" keyword was present in order to improve error
15076 recovery. For example, given:
15078 template <typename T> void f(T::X<int>);
15080 we want to treat "X<int>" as a template-id. */
15082 && !template_keyword_p
15083 && parser
->scope
&& TYPE_P (parser
->scope
)
15084 && check_dependency_p
15085 && dependent_scope_p (parser
->scope
)
15086 /* Do not do this for dtors (or ctors), since they never
15087 need the template keyword before their name. */
15088 && !constructor_name_p (identifier
, parser
->scope
))
15090 cp_token_position start
= 0;
15092 /* Explain what went wrong. */
15093 error_at (token
->location
, "non-template %qD used as template",
15095 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
15096 parser
->scope
, identifier
);
15097 /* If parsing tentatively, find the location of the "<" token. */
15098 if (cp_parser_simulate_error (parser
))
15099 start
= cp_lexer_token_position (parser
->lexer
, true);
15100 /* Parse the template arguments so that we can issue error
15101 messages about them. */
15102 cp_lexer_consume_token (parser
->lexer
);
15103 cp_parser_enclosed_template_argument_list (parser
);
15104 /* Skip tokens until we find a good place from which to
15105 continue parsing. */
15106 cp_parser_skip_to_closing_parenthesis (parser
,
15107 /*recovering=*/true,
15109 /*consume_paren=*/false);
15110 /* If parsing tentatively, permanently remove the
15111 template argument list. That will prevent duplicate
15112 error messages from being issued about the missing
15113 "template" keyword. */
15115 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
15117 *is_identifier
= true;
15121 /* If the "template" keyword is present, then there is generally
15122 no point in doing name-lookup, so we just return IDENTIFIER.
15123 But, if the qualifying scope is non-dependent then we can
15124 (and must) do name-lookup normally. */
15125 if (template_keyword_p
15127 || (TYPE_P (parser
->scope
)
15128 && dependent_type_p (parser
->scope
))))
15132 /* Look up the name. */
15133 decl
= cp_parser_lookup_name (parser
, identifier
,
15135 /*is_template=*/true,
15136 /*is_namespace=*/false,
15137 check_dependency_p
,
15138 /*ambiguous_decls=*/NULL
,
15141 decl
= strip_using_decl (decl
);
15143 /* If DECL is a template, then the name was a template-name. */
15144 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
15146 if (TREE_DEPRECATED (decl
)
15147 && deprecated_state
!= DEPRECATED_SUPPRESS
)
15148 warn_deprecated_use (decl
, NULL_TREE
);
15152 tree fn
= NULL_TREE
;
15154 /* The standard does not explicitly indicate whether a name that
15155 names a set of overloaded declarations, some of which are
15156 templates, is a template-name. However, such a name should
15157 be a template-name; otherwise, there is no way to form a
15158 template-id for the overloaded templates. */
15159 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
15160 if (TREE_CODE (fns
) == OVERLOAD
)
15161 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
15162 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
15167 /* The name does not name a template. */
15168 cp_parser_error (parser
, "expected template-name");
15169 return error_mark_node
;
15173 /* If DECL is dependent, and refers to a function, then just return
15174 its name; we will look it up again during template instantiation. */
15175 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
15177 tree scope
= ovl_scope (decl
);
15178 if (TYPE_P (scope
) && dependent_type_p (scope
))
15185 /* Parse a template-argument-list.
15187 template-argument-list:
15188 template-argument ... [opt]
15189 template-argument-list , template-argument ... [opt]
15191 Returns a TREE_VEC containing the arguments. */
15194 cp_parser_template_argument_list (cp_parser
* parser
)
15196 tree fixed_args
[10];
15197 unsigned n_args
= 0;
15198 unsigned alloced
= 10;
15199 tree
*arg_ary
= fixed_args
;
15201 bool saved_in_template_argument_list_p
;
15203 bool saved_non_ice_p
;
15205 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
15206 parser
->in_template_argument_list_p
= true;
15207 /* Even if the template-id appears in an integral
15208 constant-expression, the contents of the argument list do
15210 saved_ice_p
= parser
->integral_constant_expression_p
;
15211 parser
->integral_constant_expression_p
= false;
15212 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
15213 parser
->non_integral_constant_expression_p
= false;
15215 /* Parse the arguments. */
15221 /* Consume the comma. */
15222 cp_lexer_consume_token (parser
->lexer
);
15224 /* Parse the template-argument. */
15225 argument
= cp_parser_template_argument (parser
);
15227 /* If the next token is an ellipsis, we're expanding a template
15229 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
15231 if (argument
== error_mark_node
)
15233 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15234 error_at (token
->location
,
15235 "expected parameter pack before %<...%>");
15237 /* Consume the `...' token. */
15238 cp_lexer_consume_token (parser
->lexer
);
15240 /* Make the argument into a TYPE_PACK_EXPANSION or
15241 EXPR_PACK_EXPANSION. */
15242 argument
= make_pack_expansion (argument
);
15245 if (n_args
== alloced
)
15249 if (arg_ary
== fixed_args
)
15251 arg_ary
= XNEWVEC (tree
, alloced
);
15252 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
15255 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
15257 arg_ary
[n_args
++] = argument
;
15259 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
15261 vec
= make_tree_vec (n_args
);
15264 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
15266 if (arg_ary
!= fixed_args
)
15268 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
15269 parser
->integral_constant_expression_p
= saved_ice_p
;
15270 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
15272 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
15276 /* Parse a template-argument.
15279 assignment-expression
15283 The representation is that of an assignment-expression, type-id, or
15284 id-expression -- except that the qualified id-expression is
15285 evaluated, so that the value returned is either a DECL or an
15288 Although the standard says "assignment-expression", it forbids
15289 throw-expressions or assignments in the template argument.
15290 Therefore, we use "conditional-expression" instead. */
15293 cp_parser_template_argument (cp_parser
* parser
)
15298 bool maybe_type_id
= false;
15299 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
15300 location_t loc
= 0;
15303 /* There's really no way to know what we're looking at, so we just
15304 try each alternative in order.
15308 In a template-argument, an ambiguity between a type-id and an
15309 expression is resolved to a type-id, regardless of the form of
15310 the corresponding template-parameter.
15312 Therefore, we try a type-id first. */
15313 cp_parser_parse_tentatively (parser
);
15314 argument
= cp_parser_template_type_arg (parser
);
15315 /* If there was no error parsing the type-id but the next token is a
15316 '>>', our behavior depends on which dialect of C++ we're
15317 parsing. In C++98, we probably found a typo for '> >'. But there
15318 are type-id which are also valid expressions. For instance:
15320 struct X { int operator >> (int); };
15321 template <int V> struct Foo {};
15324 Here 'X()' is a valid type-id of a function type, but the user just
15325 wanted to write the expression "X() >> 5". Thus, we remember that we
15326 found a valid type-id, but we still try to parse the argument as an
15327 expression to see what happens.
15329 In C++0x, the '>>' will be considered two separate '>'
15331 if (!cp_parser_error_occurred (parser
)
15332 && cxx_dialect
== cxx98
15333 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
15335 maybe_type_id
= true;
15336 cp_parser_abort_tentative_parse (parser
);
15340 /* If the next token isn't a `,' or a `>', then this argument wasn't
15341 really finished. This means that the argument is not a valid
15343 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15344 cp_parser_error (parser
, "expected template-argument");
15345 /* If that worked, we're done. */
15346 if (cp_parser_parse_definitely (parser
))
15349 /* We're still not sure what the argument will be. */
15350 cp_parser_parse_tentatively (parser
);
15351 /* Try a template. */
15352 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
15353 argument
= cp_parser_id_expression (parser
,
15354 /*template_keyword_p=*/false,
15355 /*check_dependency_p=*/true,
15357 /*declarator_p=*/false,
15358 /*optional_p=*/false);
15359 /* If the next token isn't a `,' or a `>', then this argument wasn't
15360 really finished. */
15361 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15362 cp_parser_error (parser
, "expected template-argument");
15363 if (!cp_parser_error_occurred (parser
))
15365 /* Figure out what is being referred to. If the id-expression
15366 was for a class template specialization, then we will have a
15367 TYPE_DECL at this point. There is no need to do name lookup
15368 at this point in that case. */
15369 if (TREE_CODE (argument
) != TYPE_DECL
)
15370 argument
= cp_parser_lookup_name (parser
, argument
,
15372 /*is_template=*/template_p
,
15373 /*is_namespace=*/false,
15374 /*check_dependency=*/true,
15375 /*ambiguous_decls=*/NULL
,
15376 argument_start_token
->location
);
15377 /* Handle a constrained-type-specifier for a non-type template
15379 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
15381 else if (TREE_CODE (argument
) != TEMPLATE_DECL
15382 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
15383 cp_parser_error (parser
, "expected template-name");
15385 if (cp_parser_parse_definitely (parser
))
15387 if (TREE_DEPRECATED (argument
))
15388 warn_deprecated_use (argument
, NULL_TREE
);
15391 /* It must be a non-type argument. In C++17 any constant-expression is
15393 if (cxx_dialect
> cxx14
)
15396 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15398 -- an integral constant-expression of integral or enumeration
15401 -- the name of a non-type template-parameter; or
15403 -- the name of an object or function with external linkage...
15405 -- the address of an object or function with external linkage...
15407 -- a pointer to member... */
15408 /* Look for a non-type template parameter. */
15409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15411 cp_parser_parse_tentatively (parser
);
15412 argument
= cp_parser_primary_expression (parser
,
15413 /*address_p=*/false,
15415 /*template_arg_p=*/true,
15417 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
15418 || !cp_parser_next_token_ends_template_argument_p (parser
))
15419 cp_parser_simulate_error (parser
);
15420 if (cp_parser_parse_definitely (parser
))
15424 /* If the next token is "&", the argument must be the address of an
15425 object or function with external linkage. */
15426 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
15429 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15430 cp_lexer_consume_token (parser
->lexer
);
15432 /* See if we might have an id-expression. */
15433 token
= cp_lexer_peek_token (parser
->lexer
);
15434 if (token
->type
== CPP_NAME
15435 || token
->keyword
== RID_OPERATOR
15436 || token
->type
== CPP_SCOPE
15437 || token
->type
== CPP_TEMPLATE_ID
15438 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
15440 cp_parser_parse_tentatively (parser
);
15441 argument
= cp_parser_primary_expression (parser
,
15444 /*template_arg_p=*/true,
15446 if (cp_parser_error_occurred (parser
)
15447 || !cp_parser_next_token_ends_template_argument_p (parser
))
15448 cp_parser_abort_tentative_parse (parser
);
15453 if (INDIRECT_REF_P (argument
))
15455 /* Strip the dereference temporarily. */
15456 gcc_assert (REFERENCE_REF_P (argument
));
15457 argument
= TREE_OPERAND (argument
, 0);
15460 /* If we're in a template, we represent a qualified-id referring
15461 to a static data member as a SCOPE_REF even if the scope isn't
15462 dependent so that we can check access control later. */
15464 if (TREE_CODE (probe
) == SCOPE_REF
)
15465 probe
= TREE_OPERAND (probe
, 1);
15468 /* A variable without external linkage might still be a
15469 valid constant-expression, so no error is issued here
15470 if the external-linkage check fails. */
15471 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
15472 cp_parser_simulate_error (parser
);
15474 else if (is_overloaded_fn (argument
))
15475 /* All overloaded functions are allowed; if the external
15476 linkage test does not pass, an error will be issued
15480 && (TREE_CODE (argument
) == OFFSET_REF
15481 || TREE_CODE (argument
) == SCOPE_REF
))
15482 /* A pointer-to-member. */
15484 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
15487 cp_parser_simulate_error (parser
);
15489 if (cp_parser_parse_definitely (parser
))
15492 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
15493 tf_warning_or_error
);
15495 argument
= convert_from_reference (argument
);
15500 /* If the argument started with "&", there are no other valid
15501 alternatives at this point. */
15504 cp_parser_error (parser
, "invalid non-type template argument");
15505 return error_mark_node
;
15509 /* If the argument wasn't successfully parsed as a type-id followed
15510 by '>>', the argument can only be a constant expression now.
15511 Otherwise, we try parsing the constant-expression tentatively,
15512 because the argument could really be a type-id. */
15514 cp_parser_parse_tentatively (parser
);
15516 if (cxx_dialect
<= cxx14
)
15517 argument
= cp_parser_constant_expression (parser
);
15520 /* With C++17 generalized non-type template arguments we need to handle
15521 lvalue constant expressions, too. */
15522 argument
= cp_parser_assignment_expression (parser
);
15523 require_potential_constant_expression (argument
);
15526 if (!maybe_type_id
)
15528 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15529 cp_parser_error (parser
, "expected template-argument");
15530 if (cp_parser_parse_definitely (parser
))
15532 /* We did our best to parse the argument as a non type-id, but that
15533 was the only alternative that matched (albeit with a '>' after
15534 it). We can assume it's just a typo from the user, and a
15535 diagnostic will then be issued. */
15536 return cp_parser_template_type_arg (parser
);
15539 /* Parse an explicit-instantiation.
15541 explicit-instantiation:
15542 template declaration
15544 Although the standard says `declaration', what it really means is:
15546 explicit-instantiation:
15547 template decl-specifier-seq [opt] declarator [opt] ;
15549 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15550 supposed to be allowed. A defect report has been filed about this
15555 explicit-instantiation:
15556 storage-class-specifier template
15557 decl-specifier-seq [opt] declarator [opt] ;
15558 function-specifier template
15559 decl-specifier-seq [opt] declarator [opt] ; */
15562 cp_parser_explicit_instantiation (cp_parser
* parser
)
15564 int declares_class_or_enum
;
15565 cp_decl_specifier_seq decl_specifiers
;
15566 tree extension_specifier
= NULL_TREE
;
15568 timevar_push (TV_TEMPLATE_INST
);
15570 /* Look for an (optional) storage-class-specifier or
15571 function-specifier. */
15572 if (cp_parser_allow_gnu_extensions_p (parser
))
15574 extension_specifier
15575 = cp_parser_storage_class_specifier_opt (parser
);
15576 if (!extension_specifier
)
15577 extension_specifier
15578 = cp_parser_function_specifier_opt (parser
,
15579 /*decl_specs=*/NULL
);
15582 /* Look for the `template' keyword. */
15583 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15584 /* Let the front end know that we are processing an explicit
15586 begin_explicit_instantiation ();
15587 /* [temp.explicit] says that we are supposed to ignore access
15588 control while processing explicit instantiation directives. */
15589 push_deferring_access_checks (dk_no_check
);
15590 /* Parse a decl-specifier-seq. */
15591 cp_parser_decl_specifier_seq (parser
,
15592 CP_PARSER_FLAGS_OPTIONAL
,
15594 &declares_class_or_enum
);
15595 /* If there was exactly one decl-specifier, and it declared a class,
15596 and there's no declarator, then we have an explicit type
15598 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
15602 type
= check_tag_decl (&decl_specifiers
,
15603 /*explicit_type_instantiation_p=*/true);
15604 /* Turn access control back on for names used during
15605 template instantiation. */
15606 pop_deferring_access_checks ();
15608 do_type_instantiation (type
, extension_specifier
,
15609 /*complain=*/tf_error
);
15613 cp_declarator
*declarator
;
15616 /* Parse the declarator. */
15618 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15619 /*ctor_dtor_or_conv_p=*/NULL
,
15620 /*parenthesized_p=*/NULL
,
15621 /*member_p=*/false,
15622 /*friend_p=*/false);
15623 if (declares_class_or_enum
& 2)
15624 cp_parser_check_for_definition_in_return_type (declarator
,
15625 decl_specifiers
.type
,
15626 decl_specifiers
.locations
[ds_type_spec
]);
15627 if (declarator
!= cp_error_declarator
)
15629 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
15630 permerror (decl_specifiers
.locations
[ds_inline
],
15631 "explicit instantiation shall not use"
15632 " %<inline%> specifier");
15633 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
15634 permerror (decl_specifiers
.locations
[ds_constexpr
],
15635 "explicit instantiation shall not use"
15636 " %<constexpr%> specifier");
15638 decl
= grokdeclarator (declarator
, &decl_specifiers
,
15639 NORMAL
, 0, &decl_specifiers
.attributes
);
15640 /* Turn access control back on for names used during
15641 template instantiation. */
15642 pop_deferring_access_checks ();
15643 /* Do the explicit instantiation. */
15644 do_decl_instantiation (decl
, extension_specifier
);
15648 pop_deferring_access_checks ();
15649 /* Skip the body of the explicit instantiation. */
15650 cp_parser_skip_to_end_of_statement (parser
);
15653 /* We're done with the instantiation. */
15654 end_explicit_instantiation ();
15656 cp_parser_consume_semicolon_at_end_of_statement (parser
);
15658 timevar_pop (TV_TEMPLATE_INST
);
15661 /* Parse an explicit-specialization.
15663 explicit-specialization:
15664 template < > declaration
15666 Although the standard says `declaration', what it really means is:
15668 explicit-specialization:
15669 template <> decl-specifier [opt] init-declarator [opt] ;
15670 template <> function-definition
15671 template <> explicit-specialization
15672 template <> template-declaration */
15675 cp_parser_explicit_specialization (cp_parser
* parser
)
15677 bool need_lang_pop
;
15678 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15680 /* Look for the `template' keyword. */
15681 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15682 /* Look for the `<'. */
15683 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15684 /* Look for the `>'. */
15685 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15686 /* We have processed another parameter list. */
15687 ++parser
->num_template_parameter_lists
;
15690 A template ... explicit specialization ... shall not have C
15692 if (current_lang_name
== lang_name_c
)
15694 error_at (token
->location
, "template specialization with C linkage");
15695 /* Give it C++ linkage to avoid confusing other parts of the
15697 push_lang_context (lang_name_cplusplus
);
15698 need_lang_pop
= true;
15701 need_lang_pop
= false;
15702 /* Let the front end know that we are beginning a specialization. */
15703 if (!begin_specialization ())
15705 end_specialization ();
15709 /* If the next keyword is `template', we need to figure out whether
15710 or not we're looking a template-declaration. */
15711 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15713 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
15714 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
15715 cp_parser_template_declaration_after_export (parser
,
15716 /*member_p=*/false);
15718 cp_parser_explicit_specialization (parser
);
15721 /* Parse the dependent declaration. */
15722 cp_parser_single_declaration (parser
,
15724 /*member_p=*/false,
15725 /*explicit_specialization_p=*/true,
15726 /*friend_p=*/NULL
);
15727 /* We're done with the specialization. */
15728 end_specialization ();
15729 /* For the erroneous case of a template with C linkage, we pushed an
15730 implicit C++ linkage scope; exit that scope now. */
15732 pop_lang_context ();
15733 /* We're done with this parameter list. */
15734 --parser
->num_template_parameter_lists
;
15737 /* Parse a type-specifier.
15740 simple-type-specifier
15743 elaborated-type-specifier
15751 Returns a representation of the type-specifier. For a
15752 class-specifier, enum-specifier, or elaborated-type-specifier, a
15753 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15755 The parser flags FLAGS is used to control type-specifier parsing.
15757 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15758 in a decl-specifier-seq.
15760 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15761 class-specifier, enum-specifier, or elaborated-type-specifier, then
15762 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15763 if a type is declared; 2 if it is defined. Otherwise, it is set to
15766 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15767 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15768 is set to FALSE. */
15771 cp_parser_type_specifier (cp_parser
* parser
,
15772 cp_parser_flags flags
,
15773 cp_decl_specifier_seq
*decl_specs
,
15774 bool is_declaration
,
15775 int* declares_class_or_enum
,
15776 bool* is_cv_qualifier
)
15778 tree type_spec
= NULL_TREE
;
15781 cp_decl_spec ds
= ds_last
;
15783 /* Assume this type-specifier does not declare a new type. */
15784 if (declares_class_or_enum
)
15785 *declares_class_or_enum
= 0;
15786 /* And that it does not specify a cv-qualifier. */
15787 if (is_cv_qualifier
)
15788 *is_cv_qualifier
= false;
15789 /* Peek at the next token. */
15790 token
= cp_lexer_peek_token (parser
->lexer
);
15792 /* If we're looking at a keyword, we can use that to guide the
15793 production we choose. */
15794 keyword
= token
->keyword
;
15798 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15799 goto elaborated_type_specifier
;
15801 /* Look for the enum-specifier. */
15802 type_spec
= cp_parser_enum_specifier (parser
);
15803 /* If that worked, we're done. */
15806 if (declares_class_or_enum
)
15807 *declares_class_or_enum
= 2;
15809 cp_parser_set_decl_spec_type (decl_specs
,
15812 /*type_definition_p=*/true);
15816 goto elaborated_type_specifier
;
15818 /* Any of these indicate either a class-specifier, or an
15819 elaborated-type-specifier. */
15823 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15824 goto elaborated_type_specifier
;
15826 /* Parse tentatively so that we can back up if we don't find a
15827 class-specifier. */
15828 cp_parser_parse_tentatively (parser
);
15829 /* Look for the class-specifier. */
15830 type_spec
= cp_parser_class_specifier (parser
);
15831 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
15832 /* If that worked, we're done. */
15833 if (cp_parser_parse_definitely (parser
))
15835 if (declares_class_or_enum
)
15836 *declares_class_or_enum
= 2;
15838 cp_parser_set_decl_spec_type (decl_specs
,
15841 /*type_definition_p=*/true);
15845 /* Fall through. */
15846 elaborated_type_specifier
:
15847 /* We're declaring (not defining) a class or enum. */
15848 if (declares_class_or_enum
)
15849 *declares_class_or_enum
= 1;
15851 /* Fall through. */
15853 /* Look for an elaborated-type-specifier. */
15855 = (cp_parser_elaborated_type_specifier
15857 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
15860 cp_parser_set_decl_spec_type (decl_specs
,
15863 /*type_definition_p=*/false);
15868 if (is_cv_qualifier
)
15869 *is_cv_qualifier
= true;
15874 if (is_cv_qualifier
)
15875 *is_cv_qualifier
= true;
15880 if (is_cv_qualifier
)
15881 *is_cv_qualifier
= true;
15885 /* The `__complex__' keyword is a GNU extension. */
15893 /* Handle simple keywords. */
15898 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
15899 decl_specs
->any_specifiers_p
= true;
15901 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15904 /* If we do not already have a type-specifier, assume we are looking
15905 at a simple-type-specifier. */
15906 type_spec
= cp_parser_simple_type_specifier (parser
,
15910 /* If we didn't find a type-specifier, and a type-specifier was not
15911 optional in this context, issue an error message. */
15912 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15914 cp_parser_error (parser
, "expected type specifier");
15915 return error_mark_node
;
15921 /* Parse a simple-type-specifier.
15923 simple-type-specifier:
15924 :: [opt] nested-name-specifier [opt] type-name
15925 :: [opt] nested-name-specifier template template-id
15940 simple-type-specifier:
15942 decltype ( expression )
15945 __underlying_type ( type-id )
15949 simple-type-specifier:
15951 __typeof__ unary-expression
15952 __typeof__ ( type-id )
15953 __typeof__ ( type-id ) { initializer-list , [opt] }
15955 Concepts Extension:
15957 simple-type-specifier:
15958 constrained-type-specifier
15960 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15961 appropriately updated. */
15964 cp_parser_simple_type_specifier (cp_parser
* parser
,
15965 cp_decl_specifier_seq
*decl_specs
,
15966 cp_parser_flags flags
)
15968 tree type
= NULL_TREE
;
15972 /* Peek at the next token. */
15973 token
= cp_lexer_peek_token (parser
->lexer
);
15975 /* If we're looking at a keyword, things are easy. */
15976 switch (token
->keyword
)
15980 decl_specs
->explicit_char_p
= true;
15981 type
= char_type_node
;
15984 type
= char16_type_node
;
15987 type
= char32_type_node
;
15990 type
= wchar_type_node
;
15993 type
= boolean_type_node
;
15996 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
15997 type
= short_integer_type_node
;
16001 decl_specs
->explicit_int_p
= true;
16002 type
= integer_type_node
;
16008 idx
= token
->keyword
- RID_INT_N_0
;
16009 if (! int_n_enabled_p
[idx
])
16013 decl_specs
->explicit_intN_p
= true;
16014 decl_specs
->int_n_idx
= idx
;
16016 type
= int_n_trees
[idx
].signed_type
;
16020 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
16021 type
= long_integer_type_node
;
16024 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
16025 type
= integer_type_node
;
16028 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
16029 type
= unsigned_type_node
;
16032 type
= float_type_node
;
16035 type
= double_type_node
;
16038 type
= void_type_node
;
16042 maybe_warn_cpp0x (CPP0X_AUTO
);
16043 if (parser
->auto_is_implicit_function_template_parm_p
)
16045 /* The 'auto' might be the placeholder return type for a function decl
16046 with trailing return type. */
16047 bool have_trailing_return_fn_decl
= false;
16049 cp_parser_parse_tentatively (parser
);
16050 cp_lexer_consume_token (parser
->lexer
);
16051 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16052 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
16053 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
16054 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
16056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
16058 cp_lexer_consume_token (parser
->lexer
);
16059 cp_parser_skip_to_closing_parenthesis (parser
,
16060 /*recovering*/false,
16062 /*consume_paren*/true);
16066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
16068 have_trailing_return_fn_decl
= true;
16072 cp_lexer_consume_token (parser
->lexer
);
16074 cp_parser_abort_tentative_parse (parser
);
16076 if (have_trailing_return_fn_decl
)
16078 type
= make_auto ();
16082 if (cxx_dialect
>= cxx14
)
16084 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
16085 type
= TREE_TYPE (type
);
16088 type
= error_mark_node
;
16090 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
16092 if (cxx_dialect
< cxx14
)
16093 error_at (token
->location
,
16094 "use of %<auto%> in lambda parameter declaration "
16095 "only available with "
16096 "-std=c++14 or -std=gnu++14");
16098 else if (cxx_dialect
< cxx14
)
16099 error_at (token
->location
,
16100 "use of %<auto%> in parameter declaration "
16101 "only available with "
16102 "-std=c++14 or -std=gnu++14");
16103 else if (!flag_concepts
)
16104 pedwarn (token
->location
, OPT_Wpedantic
,
16105 "ISO C++ forbids use of %<auto%> in parameter "
16109 type
= make_auto ();
16113 /* Since DR 743, decltype can either be a simple-type-specifier by
16114 itself or begin a nested-name-specifier. Parsing it will replace
16115 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16116 handling below decide what to do. */
16117 cp_parser_decltype (parser
);
16118 cp_lexer_set_token_position (parser
->lexer
, token
);
16122 /* Consume the `typeof' token. */
16123 cp_lexer_consume_token (parser
->lexer
);
16124 /* Parse the operand to `typeof'. */
16125 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
16126 /* If it is not already a TYPE, take its type. */
16127 if (!TYPE_P (type
))
16128 type
= finish_typeof (type
);
16131 cp_parser_set_decl_spec_type (decl_specs
, type
,
16133 /*type_definition_p=*/false);
16137 case RID_UNDERLYING_TYPE
:
16138 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
16140 cp_parser_set_decl_spec_type (decl_specs
, type
,
16142 /*type_definition_p=*/false);
16147 case RID_DIRECT_BASES
:
16148 type
= cp_parser_trait_expr (parser
, token
->keyword
);
16150 cp_parser_set_decl_spec_type (decl_specs
, type
,
16152 /*type_definition_p=*/false);
16158 /* If token is an already-parsed decltype not followed by ::,
16159 it's a simple-type-specifier. */
16160 if (token
->type
== CPP_DECLTYPE
16161 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
16163 type
= saved_checks_value (token
->u
.tree_check_value
);
16166 cp_parser_set_decl_spec_type (decl_specs
, type
,
16168 /*type_definition_p=*/false);
16169 /* Remember that we are handling a decltype in order to
16170 implement the resolution of DR 1510 when the argument
16171 isn't instantiation dependent. */
16172 decl_specs
->decltype_p
= true;
16174 cp_lexer_consume_token (parser
->lexer
);
16178 /* If the type-specifier was for a built-in type, we're done. */
16181 /* Record the type. */
16183 && (token
->keyword
!= RID_SIGNED
16184 && token
->keyword
!= RID_UNSIGNED
16185 && token
->keyword
!= RID_SHORT
16186 && token
->keyword
!= RID_LONG
))
16187 cp_parser_set_decl_spec_type (decl_specs
,
16190 /*type_definition_p=*/false);
16192 decl_specs
->any_specifiers_p
= true;
16194 /* Consume the token. */
16195 cp_lexer_consume_token (parser
->lexer
);
16197 if (type
== error_mark_node
)
16198 return error_mark_node
;
16200 /* There is no valid C++ program where a non-template type is
16201 followed by a "<". That usually indicates that the user thought
16202 that the type was a template. */
16203 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
16206 return TYPE_NAME (type
);
16209 /* The type-specifier must be a user-defined type. */
16210 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
16215 /* Don't gobble tokens or issue error messages if this is an
16216 optional type-specifier. */
16217 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
16218 cp_parser_parse_tentatively (parser
);
16220 /* Look for the optional `::' operator. */
16222 = (cp_parser_global_scope_opt (parser
,
16223 /*current_scope_valid_p=*/false)
16225 /* Look for the nested-name specifier. */
16227 = (cp_parser_nested_name_specifier_opt (parser
,
16228 /*typename_keyword_p=*/false,
16229 /*check_dependency_p=*/true,
16231 /*is_declaration=*/false)
16233 token
= cp_lexer_peek_token (parser
->lexer
);
16234 /* If we have seen a nested-name-specifier, and the next token
16235 is `template', then we are using the template-id production. */
16237 && cp_parser_optional_template_keyword (parser
))
16239 /* Look for the template-id. */
16240 type
= cp_parser_template_id (parser
,
16241 /*template_keyword_p=*/true,
16242 /*check_dependency_p=*/true,
16244 /*is_declaration=*/false);
16245 /* If the template-id did not name a type, we are out of
16247 if (TREE_CODE (type
) != TYPE_DECL
)
16249 cp_parser_error (parser
, "expected template-id for type");
16253 /* Otherwise, look for a type-name. */
16255 type
= cp_parser_type_name (parser
);
16256 /* Keep track of all name-lookups performed in class scopes. */
16260 && TREE_CODE (type
) == TYPE_DECL
16261 && identifier_p (DECL_NAME (type
)))
16262 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
16263 /* If it didn't work out, we don't have a TYPE. */
16264 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
16265 && !cp_parser_parse_definitely (parser
))
16267 if (type
&& decl_specs
)
16268 cp_parser_set_decl_spec_type (decl_specs
, type
,
16270 /*type_definition_p=*/false);
16273 /* If we didn't get a type-name, issue an error message. */
16274 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
16276 cp_parser_error (parser
, "expected type-name");
16277 return error_mark_node
;
16280 if (type
&& type
!= error_mark_node
)
16282 /* See if TYPE is an Objective-C type, and if so, parse and
16283 accept any protocol references following it. Do this before
16284 the cp_parser_check_for_invalid_template_id() call, because
16285 Objective-C types can be followed by '<...>' which would
16286 enclose protocol names rather than template arguments, and so
16287 everything is fine. */
16288 if (c_dialect_objc () && !parser
->scope
16289 && (objc_is_id (type
) || objc_is_class_name (type
)))
16291 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16292 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
16294 /* Clobber the "unqualified" type previously entered into
16295 DECL_SPECS with the new, improved protocol-qualified version. */
16297 decl_specs
->type
= qual_type
;
16302 /* There is no valid C++ program where a non-template type is
16303 followed by a "<". That usually indicates that the user
16304 thought that the type was a template. */
16305 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
16313 /* Parse a type-name.
16319 simple-template-id [in c++0x]
16336 Returns a TYPE_DECL for the type. */
16339 cp_parser_type_name (cp_parser
* parser
)
16341 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
16346 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
16350 /* We can't know yet whether it is a class-name or not. */
16351 cp_parser_parse_tentatively (parser
);
16352 /* Try a class-name. */
16353 type_decl
= cp_parser_class_name (parser
,
16354 typename_keyword_p
,
16355 /*template_keyword_p=*/false,
16357 /*check_dependency_p=*/true,
16358 /*class_head_p=*/false,
16359 /*is_declaration=*/false);
16360 /* If it's not a class-name, keep looking. */
16361 if (!cp_parser_parse_definitely (parser
))
16363 if (cxx_dialect
< cxx11
)
16364 /* It must be a typedef-name or an enum-name. */
16365 return cp_parser_nonclass_name (parser
);
16367 cp_parser_parse_tentatively (parser
);
16368 /* It is either a simple-template-id representing an
16369 instantiation of an alias template... */
16370 type_decl
= cp_parser_template_id (parser
,
16371 /*template_keyword_p=*/false,
16372 /*check_dependency_p=*/true,
16374 /*is_declaration=*/false);
16375 /* Note that this must be an instantiation of an alias template
16376 because [temp.names]/6 says:
16378 A template-id that names an alias template specialization
16381 Whereas [temp.names]/7 says:
16383 A simple-template-id that names a class template
16384 specialization is a class-name.
16386 With concepts, this could also be a partial-concept-id that
16387 declares a non-type template parameter. */
16388 if (type_decl
!= NULL_TREE
16389 && TREE_CODE (type_decl
) == TYPE_DECL
16390 && TYPE_DECL_ALIAS_P (type_decl
))
16391 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
16392 else if (is_constrained_parameter (type_decl
))
16393 /* Don't do anything. */ ;
16395 cp_parser_simulate_error (parser
);
16397 if (!cp_parser_parse_definitely (parser
))
16398 /* ... Or a typedef-name or an enum-name. */
16399 return cp_parser_nonclass_name (parser
);
16405 /* Check if DECL and ARGS can form a constrained-type-specifier.
16406 If ARGS is non-null, we try to form a concept check of the
16407 form DECL<?, ARGS> where ? is a wildcard that matches any
16408 kind of template argument. If ARGS is NULL, then we try to
16409 form a concept check of the form DECL<?>. */
16412 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
16413 tree decl
, tree args
)
16415 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
16417 /* If we a constrained-type-specifier cannot be deduced. */
16418 if (parser
->prevent_constrained_type_specifiers
)
16421 /* A constrained type specifier can only be found in an
16422 overload set or as a reference to a template declaration.
16424 FIXME: This might be masking a bug. It's possible that
16425 that the deduction below is causing template specializations
16426 to be formed with the wildcard as an argument. */
16427 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
16430 /* Try to build a call expression that evaluates the
16431 concept. This can fail if the overload set refers
16432 only to non-templates. */
16433 tree placeholder
= build_nt (WILDCARD_DECL
);
16434 tree check
= build_concept_check (decl
, placeholder
, args
);
16435 if (check
== error_mark_node
)
16438 /* Deduce the checked constraint and the prototype parameter.
16440 FIXME: In certain cases, failure to deduce should be a
16441 diagnosable error. */
16444 if (!deduce_constrained_parameter (check
, conc
, proto
))
16447 /* In template parameter scope, this results in a constrained
16448 parameter. Return a descriptor of that parm. */
16449 if (processing_template_parmlist
)
16450 return build_constrained_parameter (conc
, proto
, args
);
16452 /* In a parameter-declaration-clause, constrained-type
16453 specifiers result in invented template parameters. */
16454 if (parser
->auto_is_implicit_function_template_parm_p
)
16456 tree x
= build_constrained_parameter (conc
, proto
, args
);
16457 return synthesize_implicit_template_parm (parser
, x
);
16461 /* Otherwise, we're in a context where the constrained
16462 type name is deduced and the constraint applies
16463 after deduction. */
16464 return make_constrained_auto (conc
, args
);
16470 /* If DECL refers to a concept, return a TYPE_DECL representing
16471 the result of using the constrained type specifier in the
16472 current context. DECL refers to a concept if
16474 - it is an overload set containing a function concept taking a single
16477 - it is a variable concept taking a single type argument. */
16480 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
16483 && (TREE_CODE (decl
) == OVERLOAD
16484 || BASELINK_P (decl
)
16485 || variable_concept_p (decl
)))
16486 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
16491 /* Check if DECL and ARGS form a partial-concept-id. If so,
16492 assign ID to the resulting constrained placeholder.
16494 Returns true if the partial-concept-id designates a placeholder
16495 and false otherwise. Note that *id is set to NULL_TREE in
16499 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
16501 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
16504 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16516 Returns a TYPE_DECL for the type. */
16519 cp_parser_nonclass_name (cp_parser
* parser
)
16524 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16525 identifier
= cp_parser_identifier (parser
);
16526 if (identifier
== error_mark_node
)
16527 return error_mark_node
;
16529 /* Look up the type-name. */
16530 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
16532 type_decl
= strip_using_decl (type_decl
);
16534 /* If we found an overload set, then it may refer to a concept-name. */
16535 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
16538 if (TREE_CODE (type_decl
) != TYPE_DECL
16539 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
16541 /* See if this is an Objective-C type. */
16542 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16543 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
16545 type_decl
= TYPE_NAME (type
);
16548 /* Issue an error if we did not find a type-name. */
16549 if (TREE_CODE (type_decl
) != TYPE_DECL
16550 /* In Objective-C, we have the complication that class names are
16551 normally type names and start declarations (eg, the
16552 "NSObject" in "NSObject *object;"), but can be used in an
16553 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16554 is an expression. So, a classname followed by a dot is not a
16555 valid type-name. */
16556 || (objc_is_class_name (TREE_TYPE (type_decl
))
16557 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
16559 if (!cp_parser_simulate_error (parser
))
16560 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
16561 NLE_TYPE
, token
->location
);
16562 return error_mark_node
;
16564 /* Remember that the name was used in the definition of the
16565 current class so that we can check later to see if the
16566 meaning would have been different after the class was
16567 entirely defined. */
16568 else if (type_decl
!= error_mark_node
16570 maybe_note_name_used_in_class (identifier
, type_decl
);
16575 /* Parse an elaborated-type-specifier. Note that the grammar given
16576 here incorporates the resolution to DR68.
16578 elaborated-type-specifier:
16579 class-key :: [opt] nested-name-specifier [opt] identifier
16580 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16581 enum-key :: [opt] nested-name-specifier [opt] identifier
16582 typename :: [opt] nested-name-specifier identifier
16583 typename :: [opt] nested-name-specifier template [opt]
16588 elaborated-type-specifier:
16589 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16590 class-key attributes :: [opt] nested-name-specifier [opt]
16591 template [opt] template-id
16592 enum attributes :: [opt] nested-name-specifier [opt] identifier
16594 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16595 declared `friend'. If IS_DECLARATION is TRUE, then this
16596 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16597 something is being declared.
16599 Returns the TYPE specified. */
16602 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
16604 bool is_declaration
)
16606 enum tag_types tag_type
;
16608 tree type
= NULL_TREE
;
16609 tree attributes
= NULL_TREE
;
16611 cp_token
*token
= NULL
;
16613 /* See if we're looking at the `enum' keyword. */
16614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
16616 /* Consume the `enum' token. */
16617 cp_lexer_consume_token (parser
->lexer
);
16618 /* Remember that it's an enumeration type. */
16619 tag_type
= enum_type
;
16620 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16621 enums) is used here. */
16622 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
16623 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
16625 pedwarn (input_location
, 0, "elaborated-type-specifier "
16626 "for a scoped enum must not use the %<%D%> keyword",
16627 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
16628 /* Consume the `struct' or `class' and parse it anyway. */
16629 cp_lexer_consume_token (parser
->lexer
);
16631 /* Parse the attributes. */
16632 attributes
= cp_parser_attributes_opt (parser
);
16634 /* Or, it might be `typename'. */
16635 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16638 /* Consume the `typename' token. */
16639 cp_lexer_consume_token (parser
->lexer
);
16640 /* Remember that it's a `typename' type. */
16641 tag_type
= typename_type
;
16643 /* Otherwise it must be a class-key. */
16646 tag_type
= cp_parser_class_key (parser
);
16647 if (tag_type
== none_type
)
16648 return error_mark_node
;
16649 /* Parse the attributes. */
16650 attributes
= cp_parser_attributes_opt (parser
);
16653 /* Look for the `::' operator. */
16654 globalscope
= cp_parser_global_scope_opt (parser
,
16655 /*current_scope_valid_p=*/false);
16656 /* Look for the nested-name-specifier. */
16657 if (tag_type
== typename_type
&& !globalscope
)
16659 if (!cp_parser_nested_name_specifier (parser
,
16660 /*typename_keyword_p=*/true,
16661 /*check_dependency_p=*/true,
16664 return error_mark_node
;
16667 /* Even though `typename' is not present, the proposed resolution
16668 to Core Issue 180 says that in `class A<T>::B', `B' should be
16669 considered a type-name, even if `A<T>' is dependent. */
16670 cp_parser_nested_name_specifier_opt (parser
,
16671 /*typename_keyword_p=*/true,
16672 /*check_dependency_p=*/true,
16675 /* For everything but enumeration types, consider a template-id.
16676 For an enumeration type, consider only a plain identifier. */
16677 if (tag_type
!= enum_type
)
16679 bool template_p
= false;
16682 /* Allow the `template' keyword. */
16683 template_p
= cp_parser_optional_template_keyword (parser
);
16684 /* If we didn't see `template', we don't know if there's a
16685 template-id or not. */
16687 cp_parser_parse_tentatively (parser
);
16688 /* Parse the template-id. */
16689 token
= cp_lexer_peek_token (parser
->lexer
);
16690 decl
= cp_parser_template_id (parser
, template_p
,
16691 /*check_dependency_p=*/true,
16694 /* If we didn't find a template-id, look for an ordinary
16696 if (!template_p
&& !cp_parser_parse_definitely (parser
))
16698 /* We can get here when cp_parser_template_id, called by
16699 cp_parser_class_name with tag_type == none_type, succeeds
16700 and caches a BASELINK. Then, when called again here,
16701 instead of failing and returning an error_mark_node
16702 returns it (see template/typename17.C in C++11).
16703 ??? Could we diagnose this earlier? */
16704 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
16706 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
16707 type
= error_mark_node
;
16709 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16710 in effect, then we must assume that, upon instantiation, the
16711 template will correspond to a class. */
16712 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16713 && tag_type
== typename_type
)
16714 type
= make_typename_type (parser
->scope
, decl
,
16716 /*complain=*/tf_error
);
16717 /* If the `typename' keyword is in effect and DECL is not a type
16718 decl, then type is non existent. */
16719 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
16721 else if (TREE_CODE (decl
) == TYPE_DECL
)
16722 type
= check_elaborated_type_specifier (tag_type
, decl
,
16723 /*allow_template_p=*/true);
16724 else if (decl
== error_mark_node
)
16725 type
= error_mark_node
;
16730 token
= cp_lexer_peek_token (parser
->lexer
);
16731 identifier
= cp_parser_identifier (parser
);
16733 if (identifier
== error_mark_node
)
16735 parser
->scope
= NULL_TREE
;
16736 return error_mark_node
;
16739 /* For a `typename', we needn't call xref_tag. */
16740 if (tag_type
== typename_type
16741 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
16742 return cp_parser_make_typename_type (parser
, identifier
,
16745 /* Template parameter lists apply only if we are not within a
16746 function parameter list. */
16747 bool template_parm_lists_apply
16748 = parser
->num_template_parameter_lists
;
16749 if (template_parm_lists_apply
)
16750 for (cp_binding_level
*s
= current_binding_level
;
16751 s
&& s
->kind
!= sk_template_parms
;
16752 s
= s
->level_chain
)
16753 if (s
->kind
== sk_function_parms
)
16754 template_parm_lists_apply
= false;
16756 /* Look up a qualified name in the usual way. */
16760 tree ambiguous_decls
;
16762 decl
= cp_parser_lookup_name (parser
, identifier
,
16764 /*is_template=*/false,
16765 /*is_namespace=*/false,
16766 /*check_dependency=*/true,
16770 /* If the lookup was ambiguous, an error will already have been
16772 if (ambiguous_decls
)
16773 return error_mark_node
;
16775 /* If we are parsing friend declaration, DECL may be a
16776 TEMPLATE_DECL tree node here. However, we need to check
16777 whether this TEMPLATE_DECL results in valid code. Consider
16778 the following example:
16781 template <class T> class C {};
16784 template <class T> friend class N::C; // #1, valid code
16786 template <class T> class Y {
16787 friend class N::C; // #2, invalid code
16790 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16791 name lookup of `N::C'. We see that friend declaration must
16792 be template for the code to be valid. Note that
16793 processing_template_decl does not work here since it is
16794 always 1 for the above two cases. */
16796 decl
= (cp_parser_maybe_treat_template_as_class
16797 (decl
, /*tag_name_p=*/is_friend
16798 && template_parm_lists_apply
));
16800 if (TREE_CODE (decl
) != TYPE_DECL
)
16802 cp_parser_diagnose_invalid_type_name (parser
,
16805 return error_mark_node
;
16808 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
16810 bool allow_template
= (template_parm_lists_apply
16811 || DECL_SELF_REFERENCE_P (decl
));
16812 type
= check_elaborated_type_specifier (tag_type
, decl
,
16815 if (type
== error_mark_node
)
16816 return error_mark_node
;
16819 /* Forward declarations of nested types, such as
16824 are invalid unless all components preceding the final '::'
16825 are complete. If all enclosing types are complete, these
16826 declarations become merely pointless.
16828 Invalid forward declarations of nested types are errors
16829 caught elsewhere in parsing. Those that are pointless arrive
16832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16833 && !is_friend
&& !processing_explicit_instantiation
)
16834 warning (0, "declaration %qD does not declare anything", decl
);
16836 type
= TREE_TYPE (decl
);
16840 /* An elaborated-type-specifier sometimes introduces a new type and
16841 sometimes names an existing type. Normally, the rule is that it
16842 introduces a new type only if there is not an existing type of
16843 the same name already in scope. For example, given:
16846 void f() { struct S s; }
16848 the `struct S' in the body of `f' is the same `struct S' as in
16849 the global scope; the existing definition is used. However, if
16850 there were no global declaration, this would introduce a new
16851 local class named `S'.
16853 An exception to this rule applies to the following code:
16855 namespace N { struct S; }
16857 Here, the elaborated-type-specifier names a new type
16858 unconditionally; even if there is already an `S' in the
16859 containing scope this declaration names a new type.
16860 This exception only applies if the elaborated-type-specifier
16861 forms the complete declaration:
16865 A declaration consisting solely of `class-key identifier ;' is
16866 either a redeclaration of the name in the current scope or a
16867 forward declaration of the identifier as a class name. It
16868 introduces the name into the current scope.
16870 We are in this situation precisely when the next token is a `;'.
16872 An exception to the exception is that a `friend' declaration does
16873 *not* name a new type; i.e., given:
16875 struct S { friend struct T; };
16877 `T' is not a new type in the scope of `S'.
16879 Also, `new struct S' or `sizeof (struct S)' never results in the
16880 definition of a new type; a new type can only be declared in a
16881 declaration context. */
16887 /* Friends have special name lookup rules. */
16888 ts
= ts_within_enclosing_non_class
;
16889 else if (is_declaration
16890 && cp_lexer_next_token_is (parser
->lexer
,
16892 /* This is a `class-key identifier ;' */
16898 (template_parm_lists_apply
16899 && (cp_parser_next_token_starts_class_definition_p (parser
)
16900 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
16901 /* An unqualified name was used to reference this type, so
16902 there were no qualifying templates. */
16903 if (template_parm_lists_apply
16904 && !cp_parser_check_template_parameters (parser
,
16905 /*num_templates=*/0,
16907 /*declarator=*/NULL
))
16908 return error_mark_node
;
16909 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
16913 if (type
== error_mark_node
)
16914 return error_mark_node
;
16916 /* Allow attributes on forward declarations of classes. */
16919 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16920 warning (OPT_Wattributes
,
16921 "attributes ignored on uninstantiated type");
16922 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
16923 && ! processing_explicit_instantiation
)
16924 warning (OPT_Wattributes
,
16925 "attributes ignored on template instantiation");
16926 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
16927 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
16929 warning (OPT_Wattributes
,
16930 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16933 if (tag_type
!= enum_type
)
16935 /* Indicate whether this class was declared as a `class' or as a
16937 if (CLASS_TYPE_P (type
))
16938 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
16939 cp_parser_check_class_key (tag_type
, type
);
16942 /* A "<" cannot follow an elaborated type specifier. If that
16943 happens, the user was probably trying to form a template-id. */
16944 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
16950 /* Parse an enum-specifier.
16953 enum-head { enumerator-list [opt] }
16954 enum-head { enumerator-list , } [C++0x]
16957 enum-key identifier [opt] enum-base [opt]
16958 enum-key nested-name-specifier identifier enum-base [opt]
16963 enum struct [C++0x]
16966 : type-specifier-seq
16968 opaque-enum-specifier:
16969 enum-key identifier enum-base [opt] ;
16972 enum-key attributes[opt] identifier [opt] enum-base [opt]
16973 { enumerator-list [opt] }attributes[opt]
16974 enum-key attributes[opt] identifier [opt] enum-base [opt]
16975 { enumerator-list, }attributes[opt] [C++0x]
16977 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16978 if the token stream isn't an enum-specifier after all. */
16981 cp_parser_enum_specifier (cp_parser
* parser
)
16984 tree type
= NULL_TREE
;
16986 tree nested_name_specifier
= NULL_TREE
;
16988 bool scoped_enum_p
= false;
16989 bool has_underlying_type
= false;
16990 bool nested_being_defined
= false;
16991 bool new_value_list
= false;
16992 bool is_new_type
= false;
16993 bool is_anonymous
= false;
16994 tree underlying_type
= NULL_TREE
;
16995 cp_token
*type_start_token
= NULL
;
16996 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
16998 parser
->colon_corrects_to_scope_p
= false;
17000 /* Parse tentatively so that we can back up if we don't find a
17002 cp_parser_parse_tentatively (parser
);
17004 /* Caller guarantees that the current token is 'enum', an identifier
17005 possibly follows, and the token after that is an opening brace.
17006 If we don't have an identifier, fabricate an anonymous name for
17007 the enumeration being defined. */
17008 cp_lexer_consume_token (parser
->lexer
);
17010 /* Parse the "class" or "struct", which indicates a scoped
17011 enumeration type in C++0x. */
17012 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
17013 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
17015 if (cxx_dialect
< cxx11
)
17016 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17018 /* Consume the `struct' or `class' token. */
17019 cp_lexer_consume_token (parser
->lexer
);
17021 scoped_enum_p
= true;
17024 attributes
= cp_parser_attributes_opt (parser
);
17026 /* Clear the qualification. */
17027 parser
->scope
= NULL_TREE
;
17028 parser
->qualifying_scope
= NULL_TREE
;
17029 parser
->object_scope
= NULL_TREE
;
17031 /* Figure out in what scope the declaration is being placed. */
17032 prev_scope
= current_scope ();
17034 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
17036 push_deferring_access_checks (dk_no_check
);
17037 nested_name_specifier
17038 = cp_parser_nested_name_specifier_opt (parser
,
17039 /*typename_keyword_p=*/true,
17040 /*check_dependency_p=*/false,
17042 /*is_declaration=*/false);
17044 if (nested_name_specifier
)
17048 identifier
= cp_parser_identifier (parser
);
17049 name
= cp_parser_lookup_name (parser
, identifier
,
17051 /*is_template=*/false,
17052 /*is_namespace=*/false,
17053 /*check_dependency=*/true,
17054 /*ambiguous_decls=*/NULL
,
17056 if (name
&& name
!= error_mark_node
)
17058 type
= TREE_TYPE (name
);
17059 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17061 /* Are template enums allowed in ISO? */
17062 if (template_parm_scope_p ())
17063 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17064 "%qD is an enumeration template", name
);
17065 /* ignore a typename reference, for it will be solved by name
17070 else if (nested_name_specifier
== error_mark_node
)
17071 /* We already issued an error. */;
17074 error_at (type_start_token
->location
,
17075 "%qD does not name an enumeration in %qT",
17076 identifier
, nested_name_specifier
);
17077 nested_name_specifier
= error_mark_node
;
17082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17083 identifier
= cp_parser_identifier (parser
);
17086 identifier
= make_anon_name ();
17087 is_anonymous
= true;
17089 error_at (type_start_token
->location
,
17090 "anonymous scoped enum is not allowed");
17093 pop_deferring_access_checks ();
17095 /* Check for the `:' that denotes a specified underlying type in C++0x.
17096 Note that a ':' could also indicate a bitfield width, however. */
17097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17099 cp_decl_specifier_seq type_specifiers
;
17101 /* Consume the `:'. */
17102 cp_lexer_consume_token (parser
->lexer
);
17104 /* Parse the type-specifier-seq. */
17105 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
17106 /*is_trailing_return=*/false,
17109 /* At this point this is surely not elaborated type specifier. */
17110 if (!cp_parser_parse_definitely (parser
))
17113 if (cxx_dialect
< cxx11
)
17114 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
17116 has_underlying_type
= true;
17118 /* If that didn't work, stop. */
17119 if (type_specifiers
.type
!= error_mark_node
)
17121 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
17122 /*initialized=*/0, NULL
);
17123 if (underlying_type
== error_mark_node
17124 || check_for_bare_parameter_packs (underlying_type
))
17125 underlying_type
= NULL_TREE
;
17129 /* Look for the `{' but don't consume it yet. */
17130 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17132 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
17134 cp_parser_error (parser
, "expected %<{%>");
17135 if (has_underlying_type
)
17141 /* An opaque-enum-specifier must have a ';' here. */
17142 if ((scoped_enum_p
|| underlying_type
)
17143 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17145 cp_parser_error (parser
, "expected %<;%> or %<{%>");
17146 if (has_underlying_type
)
17154 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
17157 if (nested_name_specifier
)
17159 if (CLASS_TYPE_P (nested_name_specifier
))
17161 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
17162 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
17163 push_scope (nested_name_specifier
);
17165 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17167 push_nested_namespace (nested_name_specifier
);
17171 /* Issue an error message if type-definitions are forbidden here. */
17172 if (!cp_parser_check_type_definition (parser
))
17173 type
= error_mark_node
;
17175 /* Create the new type. We do this before consuming the opening
17176 brace so the enum will be recorded as being on the line of its
17177 tag (or the 'enum' keyword, if there is no tag). */
17178 type
= start_enum (identifier
, type
, underlying_type
,
17179 attributes
, scoped_enum_p
, &is_new_type
);
17181 /* If the next token is not '{' it is an opaque-enum-specifier or an
17182 elaborated-type-specifier. */
17183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17185 timevar_push (TV_PARSE_ENUM
);
17186 if (nested_name_specifier
17187 && nested_name_specifier
!= error_mark_node
)
17189 /* The following catches invalid code such as:
17190 enum class S<int>::E { A, B, C }; */
17191 if (!processing_specialization
17192 && CLASS_TYPE_P (nested_name_specifier
)
17193 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
17194 error_at (type_start_token
->location
, "cannot add an enumerator "
17195 "list to a template instantiation");
17197 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
17199 error_at (type_start_token
->location
,
17200 "%<%T::%E%> has not been declared",
17201 TYPE_CONTEXT (nested_name_specifier
),
17202 nested_name_specifier
);
17203 type
= error_mark_node
;
17205 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
17206 && !CLASS_TYPE_P (nested_name_specifier
))
17208 error_at (type_start_token
->location
, "nested name specifier "
17209 "%qT for enum declaration does not name a class "
17210 "or namespace", nested_name_specifier
);
17211 type
= error_mark_node
;
17213 /* If that scope does not contain the scope in which the
17214 class was originally declared, the program is invalid. */
17215 else if (prev_scope
&& !is_ancestor (prev_scope
,
17216 nested_name_specifier
))
17218 if (at_namespace_scope_p ())
17219 error_at (type_start_token
->location
,
17220 "declaration of %qD in namespace %qD which does not "
17222 type
, prev_scope
, nested_name_specifier
);
17224 error_at (type_start_token
->location
,
17225 "declaration of %qD in %qD which does not "
17227 type
, prev_scope
, nested_name_specifier
);
17228 type
= error_mark_node
;
17230 /* If that scope is the scope where the declaration is being placed
17231 the program is invalid. */
17232 else if (CLASS_TYPE_P (nested_name_specifier
)
17233 && CLASS_TYPE_P (prev_scope
)
17234 && same_type_p (nested_name_specifier
, prev_scope
))
17236 permerror (type_start_token
->location
,
17237 "extra qualification not allowed");
17238 nested_name_specifier
= NULL_TREE
;
17243 begin_scope (sk_scoped_enum
, type
);
17245 /* Consume the opening brace. */
17246 cp_lexer_consume_token (parser
->lexer
);
17248 if (type
== error_mark_node
)
17249 ; /* Nothing to add */
17250 else if (OPAQUE_ENUM_P (type
)
17251 || (cxx_dialect
> cxx98
&& processing_specialization
))
17253 new_value_list
= true;
17254 SET_OPAQUE_ENUM_P (type
, false);
17255 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
17259 error_at (type_start_token
->location
,
17260 "multiple definition of %q#T", type
);
17261 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
17262 "previous definition here");
17263 type
= error_mark_node
;
17266 if (type
== error_mark_node
)
17267 cp_parser_skip_to_end_of_block_or_statement (parser
);
17268 /* If the next token is not '}', then there are some enumerators. */
17269 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17271 if (is_anonymous
&& !scoped_enum_p
)
17272 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
17273 "ISO C++ forbids empty anonymous enum");
17276 cp_parser_enumerator_list (parser
, type
);
17278 /* Consume the final '}'. */
17279 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17283 timevar_pop (TV_PARSE_ENUM
);
17287 /* If a ';' follows, then it is an opaque-enum-specifier
17288 and additional restrictions apply. */
17289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
17292 error_at (type_start_token
->location
,
17293 "opaque-enum-specifier without name");
17294 else if (nested_name_specifier
)
17295 error_at (type_start_token
->location
,
17296 "opaque-enum-specifier must use a simple identifier");
17300 /* Look for trailing attributes to apply to this enumeration, and
17301 apply them if appropriate. */
17302 if (cp_parser_allow_gnu_extensions_p (parser
))
17304 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
17305 cplus_decl_attributes (&type
,
17307 (int) ATTR_FLAG_TYPE_IN_PLACE
);
17310 /* Finish up the enumeration. */
17311 if (type
!= error_mark_node
)
17313 if (new_value_list
)
17314 finish_enum_value_list (type
);
17316 finish_enum (type
);
17319 if (nested_name_specifier
)
17321 if (CLASS_TYPE_P (nested_name_specifier
))
17323 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
17324 pop_scope (nested_name_specifier
);
17326 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
17328 pop_nested_namespace (nested_name_specifier
);
17332 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
17336 /* Parse an enumerator-list. The enumerators all have the indicated
17340 enumerator-definition
17341 enumerator-list , enumerator-definition */
17344 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
17348 /* Parse an enumerator-definition. */
17349 cp_parser_enumerator_definition (parser
, type
);
17351 /* If the next token is not a ',', we've reached the end of
17353 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17355 /* Otherwise, consume the `,' and keep going. */
17356 cp_lexer_consume_token (parser
->lexer
);
17357 /* If the next token is a `}', there is a trailing comma. */
17358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
17360 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
17361 pedwarn (input_location
, OPT_Wpedantic
,
17362 "comma at end of enumerator list");
17368 /* Parse an enumerator-definition. The enumerator has the indicated
17371 enumerator-definition:
17373 enumerator = constant-expression
17380 enumerator-definition:
17381 enumerator attributes [opt]
17382 enumerator attributes [opt] = constant-expression */
17385 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
17391 /* Save the input location because we are interested in the location
17392 of the identifier and not the location of the explicit value. */
17393 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17395 /* Look for the identifier. */
17396 identifier
= cp_parser_identifier (parser
);
17397 if (identifier
== error_mark_node
)
17400 /* Parse any specified attributes. */
17401 tree attrs
= cp_parser_attributes_opt (parser
);
17403 /* If the next token is an '=', then there is an explicit value. */
17404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17406 /* Consume the `=' token. */
17407 cp_lexer_consume_token (parser
->lexer
);
17408 /* Parse the value. */
17409 value
= cp_parser_constant_expression (parser
);
17414 /* If we are processing a template, make sure the initializer of the
17415 enumerator doesn't contain any bare template parameter pack. */
17416 if (check_for_bare_parameter_packs (value
))
17417 value
= error_mark_node
;
17419 /* Create the enumerator. */
17420 build_enumerator (identifier
, value
, type
, attrs
, loc
);
17423 /* Parse a namespace-name.
17426 original-namespace-name
17429 Returns the NAMESPACE_DECL for the namespace. */
17432 cp_parser_namespace_name (cp_parser
* parser
)
17435 tree namespace_decl
;
17437 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17439 /* Get the name of the namespace. */
17440 identifier
= cp_parser_identifier (parser
);
17441 if (identifier
== error_mark_node
)
17442 return error_mark_node
;
17444 /* Look up the identifier in the currently active scope. Look only
17445 for namespaces, due to:
17447 [basic.lookup.udir]
17449 When looking up a namespace-name in a using-directive or alias
17450 definition, only namespace names are considered.
17454 [basic.lookup.qual]
17456 During the lookup of a name preceding the :: scope resolution
17457 operator, object, function, and enumerator names are ignored.
17459 (Note that cp_parser_qualifying_entity only calls this
17460 function if the token after the name is the scope resolution
17462 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
17464 /*is_template=*/false,
17465 /*is_namespace=*/true,
17466 /*check_dependency=*/true,
17467 /*ambiguous_decls=*/NULL
,
17469 /* If it's not a namespace, issue an error. */
17470 if (namespace_decl
== error_mark_node
17471 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
17473 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17474 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
17475 cp_parser_error (parser
, "expected namespace-name");
17476 namespace_decl
= error_mark_node
;
17479 return namespace_decl
;
17482 /* Parse a namespace-definition.
17484 namespace-definition:
17485 named-namespace-definition
17486 unnamed-namespace-definition
17488 named-namespace-definition:
17489 original-namespace-definition
17490 extension-namespace-definition
17492 original-namespace-definition:
17493 namespace identifier { namespace-body }
17495 extension-namespace-definition:
17496 namespace original-namespace-name { namespace-body }
17498 unnamed-namespace-definition:
17499 namespace { namespace-body } */
17502 cp_parser_namespace_definition (cp_parser
* parser
)
17504 tree identifier
, attribs
;
17505 bool has_visibility
;
17508 int nested_definition_count
= 0;
17510 cp_ensure_no_omp_declare_simd (parser
);
17511 cp_ensure_no_oacc_routine (parser
);
17512 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
17514 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
17516 cp_lexer_consume_token (parser
->lexer
);
17521 /* Look for the `namespace' keyword. */
17522 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17524 /* Parse any specified attributes before the identifier. */
17525 attribs
= cp_parser_attributes_opt (parser
);
17527 /* Get the name of the namespace. We do not attempt to distinguish
17528 between an original-namespace-definition and an
17529 extension-namespace-definition at this point. The semantic
17530 analysis routines are responsible for that. */
17531 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17532 identifier
= cp_parser_identifier (parser
);
17534 identifier
= NULL_TREE
;
17536 /* Parse any specified attributes after the identifier. */
17537 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
17538 if (post_ident_attribs
)
17541 attribs
= chainon (attribs
, post_ident_attribs
);
17543 attribs
= post_ident_attribs
;
17546 /* Start the namespace. */
17547 push_namespace (identifier
);
17549 /* Parse any nested namespace definition. */
17550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17553 error_at (token
->location
, "a nested namespace definition cannot have attributes");
17554 if (cxx_dialect
< cxx1z
)
17555 pedwarn (input_location
, OPT_Wpedantic
,
17556 "nested namespace definitions only available with "
17557 "-std=c++1z or -std=gnu++1z");
17559 error_at (token
->location
, "a nested namespace definition cannot be inline");
17560 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17562 cp_lexer_consume_token (parser
->lexer
);
17563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17564 identifier
= cp_parser_identifier (parser
);
17567 cp_parser_error (parser
, "nested identifier required");
17570 ++nested_definition_count
;
17571 push_namespace (identifier
);
17575 /* Look for the `{' to validate starting the namespace. */
17576 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
17578 /* "inline namespace" is equivalent to a stub namespace definition
17579 followed by a strong using directive. */
17582 tree name_space
= current_namespace
;
17583 /* Set up namespace association. */
17584 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
17585 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
17586 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
17587 /* Import the contents of the inline namespace. */
17589 do_using_directive (name_space
);
17590 push_namespace (identifier
);
17593 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
17595 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
17597 /* Parse the body of the namespace. */
17598 cp_parser_namespace_body (parser
);
17600 if (has_visibility
)
17601 pop_visibility (1);
17603 /* Finish the nested namespace definitions. */
17604 while (nested_definition_count
--)
17607 /* Finish the namespace. */
17609 /* Look for the final `}'. */
17610 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17613 /* Parse a namespace-body.
17616 declaration-seq [opt] */
17619 cp_parser_namespace_body (cp_parser
* parser
)
17621 cp_parser_declaration_seq_opt (parser
);
17624 /* Parse a namespace-alias-definition.
17626 namespace-alias-definition:
17627 namespace identifier = qualified-namespace-specifier ; */
17630 cp_parser_namespace_alias_definition (cp_parser
* parser
)
17633 tree namespace_specifier
;
17635 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17637 /* Look for the `namespace' keyword. */
17638 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17639 /* Look for the identifier. */
17640 identifier
= cp_parser_identifier (parser
);
17641 if (identifier
== error_mark_node
)
17643 /* Look for the `=' token. */
17644 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
17645 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17647 error_at (token
->location
, "%<namespace%> definition is not allowed here");
17648 /* Skip the definition. */
17649 cp_lexer_consume_token (parser
->lexer
);
17650 if (cp_parser_skip_to_closing_brace (parser
))
17651 cp_lexer_consume_token (parser
->lexer
);
17654 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17655 /* Look for the qualified-namespace-specifier. */
17656 namespace_specifier
17657 = cp_parser_qualified_namespace_specifier (parser
);
17658 /* Look for the `;' token. */
17659 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17661 /* Register the alias in the symbol table. */
17662 do_namespace_alias (identifier
, namespace_specifier
);
17665 /* Parse a qualified-namespace-specifier.
17667 qualified-namespace-specifier:
17668 :: [opt] nested-name-specifier [opt] namespace-name
17670 Returns a NAMESPACE_DECL corresponding to the specified
17674 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
17676 /* Look for the optional `::'. */
17677 cp_parser_global_scope_opt (parser
,
17678 /*current_scope_valid_p=*/false);
17680 /* Look for the optional nested-name-specifier. */
17681 cp_parser_nested_name_specifier_opt (parser
,
17682 /*typename_keyword_p=*/false,
17683 /*check_dependency_p=*/true,
17685 /*is_declaration=*/true);
17687 return cp_parser_namespace_name (parser
);
17690 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17691 access declaration.
17694 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17695 using :: unqualified-id ;
17697 access-declaration:
17703 cp_parser_using_declaration (cp_parser
* parser
,
17704 bool access_declaration_p
)
17707 bool typename_p
= false;
17708 bool global_scope_p
;
17712 int oldcount
= errorcount
;
17713 cp_token
*diag_token
= NULL
;
17715 if (access_declaration_p
)
17717 diag_token
= cp_lexer_peek_token (parser
->lexer
);
17718 cp_parser_parse_tentatively (parser
);
17722 /* Look for the `using' keyword. */
17723 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17725 /* Peek at the next token. */
17726 token
= cp_lexer_peek_token (parser
->lexer
);
17727 /* See if it's `typename'. */
17728 if (token
->keyword
== RID_TYPENAME
)
17730 /* Remember that we've seen it. */
17732 /* Consume the `typename' token. */
17733 cp_lexer_consume_token (parser
->lexer
);
17737 /* Look for the optional global scope qualification. */
17739 = (cp_parser_global_scope_opt (parser
,
17740 /*current_scope_valid_p=*/false)
17743 /* If we saw `typename', or didn't see `::', then there must be a
17744 nested-name-specifier present. */
17745 if (typename_p
|| !global_scope_p
)
17747 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
17748 /*check_dependency_p=*/true,
17750 /*is_declaration=*/true);
17751 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
17753 cp_parser_skip_to_end_of_block_or_statement (parser
);
17757 /* Otherwise, we could be in either of the two productions. In that
17758 case, treat the nested-name-specifier as optional. */
17760 qscope
= cp_parser_nested_name_specifier_opt (parser
,
17761 /*typename_keyword_p=*/false,
17762 /*check_dependency_p=*/true,
17764 /*is_declaration=*/true);
17766 qscope
= global_namespace
;
17767 else if (UNSCOPED_ENUM_P (qscope
))
17768 qscope
= CP_TYPE_CONTEXT (qscope
);
17770 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
17771 /* Something has already gone wrong; there's no need to parse
17772 further. Since an error has occurred, the return value of
17773 cp_parser_parse_definitely will be false, as required. */
17774 return cp_parser_parse_definitely (parser
);
17776 token
= cp_lexer_peek_token (parser
->lexer
);
17777 /* Parse the unqualified-id. */
17778 identifier
= cp_parser_unqualified_id (parser
,
17779 /*template_keyword_p=*/false,
17780 /*check_dependency_p=*/true,
17781 /*declarator_p=*/true,
17782 /*optional_p=*/false);
17784 if (access_declaration_p
)
17786 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17787 cp_parser_simulate_error (parser
);
17788 if (!cp_parser_parse_definitely (parser
))
17792 /* The function we call to handle a using-declaration is different
17793 depending on what scope we are in. */
17794 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
17796 else if (!identifier_p (identifier
)
17797 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
17798 /* [namespace.udecl]
17800 A using declaration shall not name a template-id. */
17801 error_at (token
->location
,
17802 "a template-id may not appear in a using-declaration");
17805 if (at_class_scope_p ())
17807 /* Create the USING_DECL. */
17808 decl
= do_class_using_decl (parser
->scope
, identifier
);
17810 if (decl
&& typename_p
)
17811 USING_DECL_TYPENAME_P (decl
) = 1;
17813 if (check_for_bare_parameter_packs (decl
))
17815 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17819 /* Add it to the list of members in this class. */
17820 finish_member_declaration (decl
);
17824 decl
= cp_parser_lookup_name_simple (parser
,
17827 if (decl
== error_mark_node
)
17828 cp_parser_name_lookup_error (parser
, identifier
,
17831 else if (check_for_bare_parameter_packs (decl
))
17833 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17836 else if (!at_namespace_scope_p ())
17837 do_local_using_decl (decl
, qscope
, identifier
);
17839 do_toplevel_using_decl (decl
, qscope
, identifier
);
17843 /* Look for the final `;'. */
17844 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17846 if (access_declaration_p
&& errorcount
== oldcount
)
17847 warning_at (diag_token
->location
, OPT_Wdeprecated
,
17848 "access declarations are deprecated "
17849 "in favour of using-declarations; "
17850 "suggestion: add the %<using%> keyword");
17855 /* Parse an alias-declaration.
17858 using identifier attribute-specifier-seq [opt] = type-id */
17861 cp_parser_alias_declaration (cp_parser
* parser
)
17863 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
17864 location_t id_location
;
17865 cp_declarator
*declarator
;
17866 cp_decl_specifier_seq decl_specs
;
17868 const char *saved_message
= NULL
;
17870 /* Look for the `using' keyword. */
17871 cp_token
*using_token
17872 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17873 if (using_token
== NULL
)
17874 return error_mark_node
;
17876 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17877 id
= cp_parser_identifier (parser
);
17878 if (id
== error_mark_node
)
17879 return error_mark_node
;
17881 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
17882 attributes
= cp_parser_attributes_opt (parser
);
17883 if (attributes
== error_mark_node
)
17884 return error_mark_node
;
17886 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17888 if (cp_parser_error_occurred (parser
))
17889 return error_mark_node
;
17891 cp_parser_commit_to_tentative_parse (parser
);
17893 /* Now we are going to parse the type-id of the declaration. */
17898 "A type-specifier-seq shall not define a class or enumeration
17899 unless it appears in the type-id of an alias-declaration (7.1.3) that
17900 is not the declaration of a template-declaration."
17902 In other words, if we currently are in an alias template, the
17903 type-id should not define a type.
17905 So let's set parser->type_definition_forbidden_message in that
17906 case; cp_parser_check_type_definition (called by
17907 cp_parser_class_specifier) will then emit an error if a type is
17908 defined in the type-id. */
17909 if (parser
->num_template_parameter_lists
)
17911 saved_message
= parser
->type_definition_forbidden_message
;
17912 parser
->type_definition_forbidden_message
=
17913 G_("types may not be defined in alias template declarations");
17916 type
= cp_parser_type_id (parser
);
17918 /* Restore the error message if need be. */
17919 if (parser
->num_template_parameter_lists
)
17920 parser
->type_definition_forbidden_message
= saved_message
;
17922 if (type
== error_mark_node
17923 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
17925 cp_parser_skip_to_end_of_block_or_statement (parser
);
17926 return error_mark_node
;
17929 /* A typedef-name can also be introduced by an alias-declaration. The
17930 identifier following the using keyword becomes a typedef-name. It has
17931 the same semantics as if it were introduced by the typedef
17932 specifier. In particular, it does not define a new type and it shall
17933 not appear in the type-id. */
17935 clear_decl_specs (&decl_specs
);
17936 decl_specs
.type
= type
;
17937 if (attributes
!= NULL_TREE
)
17939 decl_specs
.attributes
= attributes
;
17940 set_and_check_decl_spec_loc (&decl_specs
,
17944 set_and_check_decl_spec_loc (&decl_specs
,
17947 set_and_check_decl_spec_loc (&decl_specs
,
17951 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
17952 declarator
->id_loc
= id_location
;
17954 member_p
= at_class_scope_p ();
17956 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
17957 NULL_TREE
, attributes
);
17959 decl
= start_decl (declarator
, &decl_specs
, 0,
17960 attributes
, NULL_TREE
, &pushed_scope
);
17961 if (decl
== error_mark_node
)
17964 // Attach constraints to the alias declaration.
17965 if (flag_concepts
&& current_template_parms
)
17967 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
17968 tree constr
= build_constraints (reqs
, NULL_TREE
);
17969 set_constraints (decl
, constr
);
17972 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
17975 pop_scope (pushed_scope
);
17977 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17978 added into the symbol table; otherwise, return the TYPE_DECL. */
17979 if (DECL_LANG_SPECIFIC (decl
)
17980 && DECL_TEMPLATE_INFO (decl
)
17981 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
17983 decl
= DECL_TI_TEMPLATE (decl
);
17985 check_member_template (decl
);
17991 /* Parse a using-directive.
17994 using namespace :: [opt] nested-name-specifier [opt]
17995 namespace-name ; */
17998 cp_parser_using_directive (cp_parser
* parser
)
18000 tree namespace_decl
;
18003 /* Look for the `using' keyword. */
18004 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
18005 /* And the `namespace' keyword. */
18006 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
18007 /* Look for the optional `::' operator. */
18008 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
18009 /* And the optional nested-name-specifier. */
18010 cp_parser_nested_name_specifier_opt (parser
,
18011 /*typename_keyword_p=*/false,
18012 /*check_dependency_p=*/true,
18014 /*is_declaration=*/true);
18015 /* Get the namespace being used. */
18016 namespace_decl
= cp_parser_namespace_name (parser
);
18017 /* And any specified attributes. */
18018 attribs
= cp_parser_attributes_opt (parser
);
18019 /* Update the symbol table. */
18020 parse_using_directive (namespace_decl
, attribs
);
18021 /* Look for the final `;'. */
18022 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18025 /* Parse an asm-definition.
18028 asm ( string-literal ) ;
18033 asm volatile [opt] ( string-literal ) ;
18034 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18035 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18036 : asm-operand-list [opt] ) ;
18037 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18038 : asm-operand-list [opt]
18039 : asm-clobber-list [opt] ) ;
18040 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18041 : asm-clobber-list [opt]
18042 : asm-goto-list ) ; */
18045 cp_parser_asm_definition (cp_parser
* parser
)
18048 tree outputs
= NULL_TREE
;
18049 tree inputs
= NULL_TREE
;
18050 tree clobbers
= NULL_TREE
;
18051 tree labels
= NULL_TREE
;
18053 bool volatile_p
= false;
18054 bool extended_p
= false;
18055 bool invalid_inputs_p
= false;
18056 bool invalid_outputs_p
= false;
18057 bool goto_p
= false;
18058 required_token missing
= RT_NONE
;
18060 /* Look for the `asm' keyword. */
18061 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
18063 if (parser
->in_function_body
18064 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
18066 error ("%<asm%> in %<constexpr%> function");
18067 cp_function_chain
->invalid_constexpr
= true;
18070 /* See if the next token is `volatile'. */
18071 if (cp_parser_allow_gnu_extensions_p (parser
)
18072 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
18074 /* Remember that we saw the `volatile' keyword. */
18076 /* Consume the token. */
18077 cp_lexer_consume_token (parser
->lexer
);
18079 if (cp_parser_allow_gnu_extensions_p (parser
)
18080 && parser
->in_function_body
18081 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
18083 /* Remember that we saw the `goto' keyword. */
18085 /* Consume the token. */
18086 cp_lexer_consume_token (parser
->lexer
);
18088 /* Look for the opening `('. */
18089 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
18091 /* Look for the string. */
18092 string
= cp_parser_string_literal (parser
, false, false);
18093 if (string
== error_mark_node
)
18095 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18096 /*consume_paren=*/true);
18100 /* If we're allowing GNU extensions, check for the extended assembly
18101 syntax. Unfortunately, the `:' tokens need not be separated by
18102 a space in C, and so, for compatibility, we tolerate that here
18103 too. Doing that means that we have to treat the `::' operator as
18105 if (cp_parser_allow_gnu_extensions_p (parser
)
18106 && parser
->in_function_body
18107 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
18108 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
18110 bool inputs_p
= false;
18111 bool clobbers_p
= false;
18112 bool labels_p
= false;
18114 /* The extended syntax was used. */
18117 /* Look for outputs. */
18118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18120 /* Consume the `:'. */
18121 cp_lexer_consume_token (parser
->lexer
);
18122 /* Parse the output-operands. */
18123 if (cp_lexer_next_token_is_not (parser
->lexer
,
18125 && cp_lexer_next_token_is_not (parser
->lexer
,
18127 && cp_lexer_next_token_is_not (parser
->lexer
,
18131 outputs
= cp_parser_asm_operand_list (parser
);
18132 if (outputs
== error_mark_node
)
18133 invalid_outputs_p
= true;
18136 /* If the next token is `::', there are no outputs, and the
18137 next token is the beginning of the inputs. */
18138 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18139 /* The inputs are coming next. */
18142 /* Look for inputs. */
18144 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18146 /* Consume the `:' or `::'. */
18147 cp_lexer_consume_token (parser
->lexer
);
18148 /* Parse the output-operands. */
18149 if (cp_lexer_next_token_is_not (parser
->lexer
,
18151 && cp_lexer_next_token_is_not (parser
->lexer
,
18153 && cp_lexer_next_token_is_not (parser
->lexer
,
18156 inputs
= cp_parser_asm_operand_list (parser
);
18157 if (inputs
== error_mark_node
)
18158 invalid_inputs_p
= true;
18161 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18162 /* The clobbers are coming next. */
18165 /* Look for clobbers. */
18167 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18170 /* Consume the `:' or `::'. */
18171 cp_lexer_consume_token (parser
->lexer
);
18172 /* Parse the clobbers. */
18173 if (cp_lexer_next_token_is_not (parser
->lexer
,
18175 && cp_lexer_next_token_is_not (parser
->lexer
,
18177 clobbers
= cp_parser_asm_clobber_list (parser
);
18180 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
18181 /* The labels are coming next. */
18184 /* Look for labels. */
18186 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
18189 /* Consume the `:' or `::'. */
18190 cp_lexer_consume_token (parser
->lexer
);
18191 /* Parse the labels. */
18192 labels
= cp_parser_asm_label_list (parser
);
18195 if (goto_p
&& !labels_p
)
18196 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
18199 missing
= RT_COLON_SCOPE
;
18201 /* Look for the closing `)'. */
18202 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
18203 missing
? missing
: RT_CLOSE_PAREN
))
18204 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
18205 /*consume_paren=*/true);
18206 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
18208 if (!invalid_inputs_p
&& !invalid_outputs_p
)
18210 /* Create the ASM_EXPR. */
18211 if (parser
->in_function_body
)
18213 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
18214 inputs
, clobbers
, labels
);
18215 /* If the extended syntax was not used, mark the ASM_EXPR. */
18218 tree temp
= asm_stmt
;
18219 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
18220 temp
= TREE_OPERAND (temp
, 0);
18222 ASM_INPUT_P (temp
) = 1;
18226 symtab
->finalize_toplevel_asm (string
);
18230 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18231 type that comes from the decl-specifier-seq. */
18234 strip_declarator_types (tree type
, cp_declarator
*declarator
)
18236 for (cp_declarator
*d
= declarator
; d
;)
18245 if (TYPE_PTRMEMFUNC_P (type
))
18246 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
18247 type
= TREE_TYPE (type
);
18255 /* Declarators [gram.dcl.decl] */
18257 /* Parse an init-declarator.
18260 declarator initializer [opt]
18265 declarator asm-specification [opt] attributes [opt] initializer [opt]
18267 function-definition:
18268 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18270 decl-specifier-seq [opt] declarator function-try-block
18274 function-definition:
18275 __extension__ function-definition
18279 function-definition:
18280 decl-specifier-seq [opt] declarator function-transaction-block
18282 The DECL_SPECIFIERS apply to this declarator. Returns a
18283 representation of the entity declared. If MEMBER_P is TRUE, then
18284 this declarator appears in a class scope. The new DECL created by
18285 this declarator is returned.
18287 The CHECKS are access checks that should be performed once we know
18288 what entity is being declared (and, therefore, what classes have
18291 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18292 for a function-definition here as well. If the declarator is a
18293 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18294 be TRUE upon return. By that point, the function-definition will
18295 have been completely parsed.
18297 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18300 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18301 parsed declaration if it is an uninitialized single declarator not followed
18302 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18303 if present, will not be consumed. If returned, this declarator will be
18304 created with SD_INITIALIZED but will not call cp_finish_decl.
18306 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18307 and there is an initializer, the pointed location_t is set to the
18308 location of the '=' or `(', or '{' in C++11 token introducing the
18312 cp_parser_init_declarator (cp_parser
* parser
,
18313 cp_decl_specifier_seq
*decl_specifiers
,
18314 vec
<deferred_access_check
, va_gc
> *checks
,
18315 bool function_definition_allowed_p
,
18317 int declares_class_or_enum
,
18318 bool* function_definition_p
,
18319 tree
* maybe_range_for_decl
,
18320 location_t
* init_loc
,
18323 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
18324 *attributes_start_token
= NULL
;
18325 cp_declarator
*declarator
;
18326 tree prefix_attributes
;
18327 tree attributes
= NULL
;
18328 tree asm_specification
;
18330 tree decl
= NULL_TREE
;
18332 int is_initialized
;
18333 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18334 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18336 enum cpp_ttype initialization_kind
;
18337 bool is_direct_init
= false;
18338 bool is_non_constant_init
;
18339 int ctor_dtor_or_conv_p
;
18340 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
18341 tree pushed_scope
= NULL_TREE
;
18342 bool range_for_decl_p
= false;
18343 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18344 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
18346 /* Gather the attributes that were provided with the
18347 decl-specifiers. */
18348 prefix_attributes
= decl_specifiers
->attributes
;
18350 /* Assume that this is not the declarator for a function
18352 if (function_definition_p
)
18353 *function_definition_p
= false;
18355 /* Default arguments are only permitted for function parameters. */
18356 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
18357 parser
->default_arg_ok_p
= false;
18359 /* Defer access checks while parsing the declarator; we cannot know
18360 what names are accessible until we know what is being
18362 resume_deferring_access_checks ();
18364 /* Parse the declarator. */
18365 token
= cp_lexer_peek_token (parser
->lexer
);
18367 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18368 &ctor_dtor_or_conv_p
,
18369 /*parenthesized_p=*/NULL
,
18370 member_p
, friend_p
);
18371 /* Gather up the deferred checks. */
18372 stop_deferring_access_checks ();
18374 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18376 /* If the DECLARATOR was erroneous, there's no need to go
18378 if (declarator
== cp_error_declarator
)
18379 return error_mark_node
;
18381 /* Check that the number of template-parameter-lists is OK. */
18382 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
18384 return error_mark_node
;
18386 if (declares_class_or_enum
& 2)
18387 cp_parser_check_for_definition_in_return_type (declarator
,
18388 decl_specifiers
->type
,
18389 decl_specifiers
->locations
[ds_type_spec
]);
18391 /* Figure out what scope the entity declared by the DECLARATOR is
18392 located in. `grokdeclarator' sometimes changes the scope, so
18393 we compute it now. */
18394 scope
= get_scope_of_declarator (declarator
);
18396 /* Perform any lookups in the declared type which were thought to be
18397 dependent, but are not in the scope of the declarator. */
18398 decl_specifiers
->type
18399 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
18401 /* If we're allowing GNU extensions, look for an
18402 asm-specification. */
18403 if (cp_parser_allow_gnu_extensions_p (parser
))
18405 /* Look for an asm-specification. */
18406 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
18407 asm_specification
= cp_parser_asm_specification_opt (parser
);
18410 asm_specification
= NULL_TREE
;
18412 /* Look for attributes. */
18413 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
18414 attributes
= cp_parser_attributes_opt (parser
);
18416 /* Peek at the next token. */
18417 token
= cp_lexer_peek_token (parser
->lexer
);
18419 bool bogus_implicit_tmpl
= false;
18421 if (function_declarator_p (declarator
))
18423 /* Check to see if the token indicates the start of a
18424 function-definition. */
18425 if (cp_parser_token_starts_function_definition_p (token
))
18427 if (!function_definition_allowed_p
)
18429 /* If a function-definition should not appear here, issue an
18431 cp_parser_error (parser
,
18432 "a function-definition is not allowed here");
18433 return error_mark_node
;
18436 location_t func_brace_location
18437 = cp_lexer_peek_token (parser
->lexer
)->location
;
18439 /* Neither attributes nor an asm-specification are allowed
18440 on a function-definition. */
18441 if (asm_specification
)
18442 error_at (asm_spec_start_token
->location
,
18443 "an asm-specification is not allowed "
18444 "on a function-definition");
18446 error_at (attributes_start_token
->location
,
18447 "attributes are not allowed "
18448 "on a function-definition");
18449 /* This is a function-definition. */
18450 *function_definition_p
= true;
18452 /* Parse the function definition. */
18454 decl
= cp_parser_save_member_function_body (parser
,
18457 prefix_attributes
);
18460 (cp_parser_function_definition_from_specifiers_and_declarator
18461 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
18463 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
18465 /* This is where the prologue starts... */
18466 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
18467 = func_brace_location
;
18473 else if (parser
->fully_implicit_function_template_p
)
18475 /* A non-template declaration involving a function parameter list
18476 containing an implicit template parameter will be made into a
18477 template. If the resulting declaration is not going to be an
18478 actual function then finish the template scope here to prevent it.
18479 An error message will be issued once we have a decl to talk about.
18481 FIXME probably we should do type deduction rather than create an
18482 implicit template, but the standard currently doesn't allow it. */
18483 bogus_implicit_tmpl
= true;
18484 finish_fully_implicit_template (parser
, NULL_TREE
);
18489 Only in function declarations for constructors, destructors, and
18490 type conversions can the decl-specifier-seq be omitted.
18492 We explicitly postpone this check past the point where we handle
18493 function-definitions because we tolerate function-definitions
18494 that are missing their return types in some modes. */
18495 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
18497 cp_parser_error (parser
,
18498 "expected constructor, destructor, or type conversion");
18499 return error_mark_node
;
18502 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18503 if (token
->type
== CPP_EQ
18504 || token
->type
== CPP_OPEN_PAREN
18505 || token
->type
== CPP_OPEN_BRACE
)
18507 is_initialized
= SD_INITIALIZED
;
18508 initialization_kind
= token
->type
;
18509 if (maybe_range_for_decl
)
18510 *maybe_range_for_decl
= error_mark_node
;
18511 tmp_init_loc
= token
->location
;
18512 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
18513 *init_loc
= tmp_init_loc
;
18515 if (token
->type
== CPP_EQ
18516 && function_declarator_p (declarator
))
18518 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18519 if (t2
->keyword
== RID_DEFAULT
)
18520 is_initialized
= SD_DEFAULTED
;
18521 else if (t2
->keyword
== RID_DELETE
)
18522 is_initialized
= SD_DELETED
;
18527 /* If the init-declarator isn't initialized and isn't followed by a
18528 `,' or `;', it's not a valid init-declarator. */
18529 if (token
->type
!= CPP_COMMA
18530 && token
->type
!= CPP_SEMICOLON
)
18532 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
18533 range_for_decl_p
= true;
18536 if (!maybe_range_for_decl
)
18537 cp_parser_error (parser
, "expected initializer");
18538 return error_mark_node
;
18541 is_initialized
= SD_UNINITIALIZED
;
18542 initialization_kind
= CPP_EOF
;
18545 /* Because start_decl has side-effects, we should only call it if we
18546 know we're going ahead. By this point, we know that we cannot
18547 possibly be looking at any other construct. */
18548 cp_parser_commit_to_tentative_parse (parser
);
18550 /* Enter the newly declared entry in the symbol table. If we're
18551 processing a declaration in a class-specifier, we wait until
18552 after processing the initializer. */
18555 if (parser
->in_unbraced_linkage_specification_p
)
18556 decl_specifiers
->storage_class
= sc_extern
;
18557 decl
= start_decl (declarator
, decl_specifiers
,
18558 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
18559 attributes
, prefix_attributes
, &pushed_scope
);
18560 cp_finalize_omp_declare_simd (parser
, decl
);
18561 cp_finalize_oacc_routine (parser
, decl
, false);
18562 /* Adjust location of decl if declarator->id_loc is more appropriate:
18563 set, and decl wasn't merged with another decl, in which case its
18564 location would be different from input_location, and more accurate. */
18566 && declarator
->id_loc
!= UNKNOWN_LOCATION
18567 && DECL_SOURCE_LOCATION (decl
) == input_location
)
18568 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
18571 /* Enter the SCOPE. That way unqualified names appearing in the
18572 initializer will be looked up in SCOPE. */
18573 pushed_scope
= push_scope (scope
);
18575 /* Perform deferred access control checks, now that we know in which
18576 SCOPE the declared entity resides. */
18577 if (!member_p
&& decl
)
18579 tree saved_current_function_decl
= NULL_TREE
;
18581 /* If the entity being declared is a function, pretend that we
18582 are in its scope. If it is a `friend', it may have access to
18583 things that would not otherwise be accessible. */
18584 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18586 saved_current_function_decl
= current_function_decl
;
18587 current_function_decl
= decl
;
18590 /* Perform access checks for template parameters. */
18591 cp_parser_perform_template_parameter_access_checks (checks
);
18593 /* Perform the access control checks for the declarator and the
18594 decl-specifiers. */
18595 perform_deferred_access_checks (tf_warning_or_error
);
18597 /* Restore the saved value. */
18598 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18599 current_function_decl
= saved_current_function_decl
;
18602 /* Parse the initializer. */
18603 initializer
= NULL_TREE
;
18604 is_direct_init
= false;
18605 is_non_constant_init
= true;
18606 if (is_initialized
)
18608 if (function_declarator_p (declarator
))
18610 if (initialization_kind
== CPP_EQ
)
18611 initializer
= cp_parser_pure_specifier (parser
);
18614 /* If the declaration was erroneous, we don't really
18615 know what the user intended, so just silently
18616 consume the initializer. */
18617 if (decl
!= error_mark_node
)
18618 error_at (tmp_init_loc
, "initializer provided for function");
18619 cp_parser_skip_to_closing_parenthesis (parser
,
18620 /*recovering=*/true,
18621 /*or_comma=*/false,
18622 /*consume_paren=*/true);
18627 /* We want to record the extra mangling scope for in-class
18628 initializers of class members and initializers of static data
18629 member templates. The former involves deferring
18630 parsing of the initializer until end of class as with default
18631 arguments. So right here we only handle the latter. */
18632 if (!member_p
&& processing_template_decl
)
18633 start_lambda_scope (decl
);
18634 initializer
= cp_parser_initializer (parser
,
18636 &is_non_constant_init
);
18637 if (!member_p
&& processing_template_decl
)
18638 finish_lambda_scope ();
18639 if (initializer
== error_mark_node
)
18640 cp_parser_skip_to_end_of_statement (parser
);
18644 /* The old parser allows attributes to appear after a parenthesized
18645 initializer. Mark Mitchell proposed removing this functionality
18646 on the GCC mailing lists on 2002-08-13. This parser accepts the
18647 attributes -- but ignores them. */
18648 if (cp_parser_allow_gnu_extensions_p (parser
)
18649 && initialization_kind
== CPP_OPEN_PAREN
)
18650 if (cp_parser_attributes_opt (parser
))
18651 warning (OPT_Wattributes
,
18652 "attributes after parenthesized initializer ignored");
18654 /* And now complain about a non-function implicit template. */
18655 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
18656 error_at (DECL_SOURCE_LOCATION (decl
),
18657 "non-function %qD declared as implicit template", decl
);
18659 /* For an in-class declaration, use `grokfield' to create the
18665 pop_scope (pushed_scope
);
18666 pushed_scope
= NULL_TREE
;
18668 decl
= grokfield (declarator
, decl_specifiers
,
18669 initializer
, !is_non_constant_init
,
18670 /*asmspec=*/NULL_TREE
,
18671 chainon (attributes
, prefix_attributes
));
18672 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18673 cp_parser_save_default_args (parser
, decl
);
18674 cp_finalize_omp_declare_simd (parser
, decl
);
18675 cp_finalize_oacc_routine (parser
, decl
, false);
18678 /* Finish processing the declaration. But, skip member
18680 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
18682 cp_finish_decl (decl
,
18683 initializer
, !is_non_constant_init
,
18685 /* If the initializer is in parentheses, then this is
18686 a direct-initialization, which means that an
18687 `explicit' constructor is OK. Otherwise, an
18688 `explicit' constructor cannot be used. */
18689 ((is_direct_init
|| !is_initialized
)
18690 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
18692 else if ((cxx_dialect
!= cxx98
) && friend_p
18693 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18694 /* Core issue #226 (C++0x only): A default template-argument
18695 shall not be specified in a friend class template
18697 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
18698 /*is_partial=*/false, /*is_friend_decl=*/1);
18700 if (!friend_p
&& pushed_scope
)
18701 pop_scope (pushed_scope
);
18703 if (function_declarator_p (declarator
)
18704 && parser
->fully_implicit_function_template_p
)
18707 decl
= finish_fully_implicit_template (parser
, decl
);
18709 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
18712 if (auto_result
&& is_initialized
&& decl_specifiers
->type
18713 && type_uses_auto (decl_specifiers
->type
))
18714 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
18719 /* Parse a declarator.
18723 ptr-operator declarator
18725 abstract-declarator:
18726 ptr-operator abstract-declarator [opt]
18727 direct-abstract-declarator
18732 attributes [opt] direct-declarator
18733 attributes [opt] ptr-operator declarator
18735 abstract-declarator:
18736 attributes [opt] ptr-operator abstract-declarator [opt]
18737 attributes [opt] direct-abstract-declarator
18739 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18740 detect constructor, destructor or conversion operators. It is set
18741 to -1 if the declarator is a name, and +1 if it is a
18742 function. Otherwise it is set to zero. Usually you just want to
18743 test for >0, but internally the negative value is used.
18745 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18746 a decl-specifier-seq unless it declares a constructor, destructor,
18747 or conversion. It might seem that we could check this condition in
18748 semantic analysis, rather than parsing, but that makes it difficult
18749 to handle something like `f()'. We want to notice that there are
18750 no decl-specifiers, and therefore realize that this is an
18751 expression, not a declaration.)
18753 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18754 the declarator is a direct-declarator of the form "(...)".
18756 MEMBER_P is true iff this declarator is a member-declarator.
18758 FRIEND_P is true iff this declarator is a friend. */
18760 static cp_declarator
*
18761 cp_parser_declarator (cp_parser
* parser
,
18762 cp_parser_declarator_kind dcl_kind
,
18763 int* ctor_dtor_or_conv_p
,
18764 bool* parenthesized_p
,
18765 bool member_p
, bool friend_p
)
18767 cp_declarator
*declarator
;
18768 enum tree_code code
;
18769 cp_cv_quals cv_quals
;
18771 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
18773 /* Assume this is not a constructor, destructor, or type-conversion
18775 if (ctor_dtor_or_conv_p
)
18776 *ctor_dtor_or_conv_p
= 0;
18778 if (cp_parser_allow_gnu_extensions_p (parser
))
18779 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
18781 /* Check for the ptr-operator production. */
18782 cp_parser_parse_tentatively (parser
);
18783 /* Parse the ptr-operator. */
18784 code
= cp_parser_ptr_operator (parser
,
18789 /* If that worked, then we have a ptr-operator. */
18790 if (cp_parser_parse_definitely (parser
))
18792 /* If a ptr-operator was found, then this declarator was not
18794 if (parenthesized_p
)
18795 *parenthesized_p
= true;
18796 /* The dependent declarator is optional if we are parsing an
18797 abstract-declarator. */
18798 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18799 cp_parser_parse_tentatively (parser
);
18801 /* Parse the dependent declarator. */
18802 declarator
= cp_parser_declarator (parser
, dcl_kind
,
18803 /*ctor_dtor_or_conv_p=*/NULL
,
18804 /*parenthesized_p=*/NULL
,
18805 /*member_p=*/false,
18808 /* If we are parsing an abstract-declarator, we must handle the
18809 case where the dependent declarator is absent. */
18810 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
18811 && !cp_parser_parse_definitely (parser
))
18814 declarator
= cp_parser_make_indirect_declarator
18815 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
18817 /* Everything else is a direct-declarator. */
18820 if (parenthesized_p
)
18821 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
18823 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
18824 ctor_dtor_or_conv_p
,
18825 member_p
, friend_p
);
18828 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
18829 declarator
->attributes
= gnu_attributes
;
18833 /* Parse a direct-declarator or direct-abstract-declarator.
18837 direct-declarator ( parameter-declaration-clause )
18838 cv-qualifier-seq [opt]
18839 ref-qualifier [opt]
18840 exception-specification [opt]
18841 direct-declarator [ constant-expression [opt] ]
18844 direct-abstract-declarator:
18845 direct-abstract-declarator [opt]
18846 ( parameter-declaration-clause )
18847 cv-qualifier-seq [opt]
18848 ref-qualifier [opt]
18849 exception-specification [opt]
18850 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18851 ( abstract-declarator )
18853 Returns a representation of the declarator. DCL_KIND is
18854 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18855 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18856 we are parsing a direct-declarator. It is
18857 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18858 of ambiguity we prefer an abstract declarator, as per
18859 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18860 as for cp_parser_declarator. */
18862 static cp_declarator
*
18863 cp_parser_direct_declarator (cp_parser
* parser
,
18864 cp_parser_declarator_kind dcl_kind
,
18865 int* ctor_dtor_or_conv_p
,
18866 bool member_p
, bool friend_p
)
18869 cp_declarator
*declarator
= NULL
;
18870 tree scope
= NULL_TREE
;
18871 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18872 bool saved_in_declarator_p
= parser
->in_declarator_p
;
18874 tree pushed_scope
= NULL_TREE
;
18878 /* Peek at the next token. */
18879 token
= cp_lexer_peek_token (parser
->lexer
);
18880 if (token
->type
== CPP_OPEN_PAREN
)
18882 /* This is either a parameter-declaration-clause, or a
18883 parenthesized declarator. When we know we are parsing a
18884 named declarator, it must be a parenthesized declarator
18885 if FIRST is true. For instance, `(int)' is a
18886 parameter-declaration-clause, with an omitted
18887 direct-abstract-declarator. But `((*))', is a
18888 parenthesized abstract declarator. Finally, when T is a
18889 template parameter `(T)' is a
18890 parameter-declaration-clause, and not a parenthesized
18893 We first try and parse a parameter-declaration-clause,
18894 and then try a nested declarator (if FIRST is true).
18896 It is not an error for it not to be a
18897 parameter-declaration-clause, even when FIRST is
18903 The first is the declaration of a function while the
18904 second is the definition of a variable, including its
18907 Having seen only the parenthesis, we cannot know which of
18908 these two alternatives should be selected. Even more
18909 complex are examples like:
18914 The former is a function-declaration; the latter is a
18915 variable initialization.
18917 Thus again, we try a parameter-declaration-clause, and if
18918 that fails, we back out and return. */
18920 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18923 bool is_declarator
= false;
18925 /* In a member-declarator, the only valid interpretation
18926 of a parenthesis is the start of a
18927 parameter-declaration-clause. (It is invalid to
18928 initialize a static data member with a parenthesized
18929 initializer; only the "=" form of initialization is
18932 cp_parser_parse_tentatively (parser
);
18934 /* Consume the `('. */
18935 cp_lexer_consume_token (parser
->lexer
);
18938 /* If this is going to be an abstract declarator, we're
18939 in a declarator and we can't have default args. */
18940 parser
->default_arg_ok_p
= false;
18941 parser
->in_declarator_p
= true;
18944 begin_scope (sk_function_parms
, NULL_TREE
);
18946 /* Parse the parameter-declaration-clause. */
18947 params
= cp_parser_parameter_declaration_clause (parser
);
18949 /* Consume the `)'. */
18950 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18952 /* If all went well, parse the cv-qualifier-seq,
18953 ref-qualifier and the exception-specification. */
18954 if (member_p
|| cp_parser_parse_definitely (parser
))
18956 cp_cv_quals cv_quals
;
18957 cp_virt_specifiers virt_specifiers
;
18958 cp_ref_qualifier ref_qual
;
18959 tree exception_specification
;
18962 bool memfn
= (member_p
|| (pushed_scope
18963 && CLASS_TYPE_P (pushed_scope
)));
18965 is_declarator
= true;
18967 if (ctor_dtor_or_conv_p
)
18968 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
18971 /* Parse the cv-qualifier-seq. */
18972 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18973 /* Parse the ref-qualifier. */
18974 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
18975 /* Parse the tx-qualifier. */
18976 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
18977 /* And the exception-specification. */
18978 exception_specification
18979 = cp_parser_exception_specification_opt (parser
);
18981 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18983 /* In here, we handle cases where attribute is used after
18984 the function declaration. For example:
18985 void func (int x) __attribute__((vector(..))); */
18986 tree gnu_attrs
= NULL_TREE
;
18988 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
18990 cp_parser_parse_tentatively (parser
);
18991 tree attr
= cp_parser_gnu_attributes_opt (parser
);
18992 if (cp_lexer_next_token_is_not (parser
->lexer
,
18994 && cp_lexer_next_token_is_not (parser
->lexer
,
18996 cp_parser_abort_tentative_parse (parser
);
18997 else if (!cp_parser_parse_definitely (parser
))
19002 tree requires_clause
= NULL_TREE
;
19003 late_return
= (cp_parser_late_return_type_opt
19004 (parser
, declarator
, requires_clause
,
19005 memfn
? cv_quals
: -1));
19007 /* Parse the virt-specifier-seq. */
19008 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
19010 /* Create the function-declarator. */
19011 declarator
= make_call_declarator (declarator
,
19017 exception_specification
,
19020 declarator
->std_attributes
= attrs
;
19021 declarator
->attributes
= gnu_attrs
;
19022 /* Any subsequent parameter lists are to do with
19023 return type, so are not those of the declared
19025 parser
->default_arg_ok_p
= false;
19028 /* Remove the function parms from scope. */
19029 pop_bindings_and_leave_scope ();
19032 /* Repeat the main loop. */
19036 /* If this is the first, we can try a parenthesized
19040 bool saved_in_type_id_in_expr_p
;
19042 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19043 parser
->in_declarator_p
= saved_in_declarator_p
;
19045 /* Consume the `('. */
19046 cp_lexer_consume_token (parser
->lexer
);
19047 /* Parse the nested declarator. */
19048 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
19049 parser
->in_type_id_in_expr_p
= true;
19051 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
19052 /*parenthesized_p=*/NULL
,
19053 member_p
, friend_p
);
19054 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
19056 /* Expect a `)'. */
19057 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
19058 declarator
= cp_error_declarator
;
19059 if (declarator
== cp_error_declarator
)
19062 goto handle_declarator
;
19064 /* Otherwise, we must be done. */
19068 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
19069 && token
->type
== CPP_OPEN_SQUARE
19070 && !cp_next_tokens_can_be_attribute_p (parser
))
19072 /* Parse an array-declarator. */
19073 tree bounds
, attrs
;
19075 if (ctor_dtor_or_conv_p
)
19076 *ctor_dtor_or_conv_p
= 0;
19079 parser
->default_arg_ok_p
= false;
19080 parser
->in_declarator_p
= true;
19081 /* Consume the `['. */
19082 cp_lexer_consume_token (parser
->lexer
);
19083 /* Peek at the next token. */
19084 token
= cp_lexer_peek_token (parser
->lexer
);
19085 /* If the next token is `]', then there is no
19086 constant-expression. */
19087 if (token
->type
!= CPP_CLOSE_SQUARE
)
19089 bool non_constant_p
;
19091 = cp_parser_constant_expression (parser
,
19092 /*allow_non_constant=*/true,
19094 if (!non_constant_p
)
19096 else if (error_operand_p (bounds
))
19097 /* Already gave an error. */;
19098 else if (!parser
->in_function_body
19099 || current_binding_level
->kind
== sk_function_parms
)
19101 /* Normally, the array bound must be an integral constant
19102 expression. However, as an extension, we allow VLAs
19103 in function scopes as long as they aren't part of a
19104 parameter declaration. */
19105 cp_parser_error (parser
,
19106 "array bound is not an integer constant");
19107 bounds
= error_mark_node
;
19109 else if (processing_template_decl
19110 && !type_dependent_expression_p (bounds
))
19112 /* Remember this wasn't a constant-expression. */
19113 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
19114 TREE_SIDE_EFFECTS (bounds
) = 1;
19118 bounds
= NULL_TREE
;
19119 /* Look for the closing `]'. */
19120 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
19122 declarator
= cp_error_declarator
;
19126 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19127 declarator
= make_array_declarator (declarator
, bounds
);
19128 declarator
->std_attributes
= attrs
;
19130 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
19133 tree qualifying_scope
;
19134 tree unqualified_name
;
19136 special_function_kind sfk
;
19138 bool pack_expansion_p
= false;
19139 cp_token
*declarator_id_start_token
;
19141 /* Parse a declarator-id */
19142 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
19145 cp_parser_parse_tentatively (parser
);
19147 /* If we see an ellipsis, we should be looking at a
19149 if (token
->type
== CPP_ELLIPSIS
)
19151 /* Consume the `...' */
19152 cp_lexer_consume_token (parser
->lexer
);
19154 pack_expansion_p
= true;
19158 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
19160 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
19161 qualifying_scope
= parser
->scope
;
19166 if (!unqualified_name
&& pack_expansion_p
)
19168 /* Check whether an error occurred. */
19169 okay
= !cp_parser_error_occurred (parser
);
19171 /* We already consumed the ellipsis to mark a
19172 parameter pack, but we have no way to report it,
19173 so abort the tentative parse. We will be exiting
19174 immediately anyway. */
19175 cp_parser_abort_tentative_parse (parser
);
19178 okay
= cp_parser_parse_definitely (parser
);
19181 unqualified_name
= error_mark_node
;
19182 else if (unqualified_name
19183 && (qualifying_scope
19184 || (!identifier_p (unqualified_name
))))
19186 cp_parser_error (parser
, "expected unqualified-id");
19187 unqualified_name
= error_mark_node
;
19191 if (!unqualified_name
)
19193 if (unqualified_name
== error_mark_node
)
19195 declarator
= cp_error_declarator
;
19196 pack_expansion_p
= false;
19197 declarator
->parameter_pack_p
= false;
19201 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19203 if (qualifying_scope
&& at_namespace_scope_p ()
19204 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
19206 /* In the declaration of a member of a template class
19207 outside of the class itself, the SCOPE will sometimes
19208 be a TYPENAME_TYPE. For example, given:
19210 template <typename T>
19211 int S<T>::R::i = 3;
19213 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19214 this context, we must resolve S<T>::R to an ordinary
19215 type, rather than a typename type.
19217 The reason we normally avoid resolving TYPENAME_TYPEs
19218 is that a specialization of `S' might render
19219 `S<T>::R' not a type. However, if `S' is
19220 specialized, then this `i' will not be used, so there
19221 is no harm in resolving the types here. */
19224 /* Resolve the TYPENAME_TYPE. */
19225 type
= resolve_typename_type (qualifying_scope
,
19226 /*only_current_p=*/false);
19227 /* If that failed, the declarator is invalid. */
19228 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19230 if (typedef_variant_p (type
))
19231 error_at (declarator_id_start_token
->location
,
19232 "cannot define member of dependent typedef "
19235 error_at (declarator_id_start_token
->location
,
19236 "%<%T::%E%> is not a type",
19237 TYPE_CONTEXT (qualifying_scope
),
19238 TYPE_IDENTIFIER (qualifying_scope
));
19240 qualifying_scope
= type
;
19245 if (unqualified_name
)
19249 if (qualifying_scope
19250 && CLASS_TYPE_P (qualifying_scope
))
19251 class_type
= qualifying_scope
;
19253 class_type
= current_class_type
;
19255 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
19257 tree name_type
= TREE_TYPE (unqualified_name
);
19258 if (class_type
&& same_type_p (name_type
, class_type
))
19260 if (qualifying_scope
19261 && CLASSTYPE_USE_TEMPLATE (name_type
))
19263 error_at (declarator_id_start_token
->location
,
19264 "invalid use of constructor as a template");
19265 inform (declarator_id_start_token
->location
,
19266 "use %<%T::%D%> instead of %<%T::%D%> to "
19267 "name the constructor in a qualified name",
19269 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
19270 class_type
, name_type
);
19271 declarator
= cp_error_declarator
;
19275 unqualified_name
= constructor_name (class_type
);
19279 /* We do not attempt to print the declarator
19280 here because we do not have enough
19281 information about its original syntactic
19283 cp_parser_error (parser
, "invalid declarator");
19284 declarator
= cp_error_declarator
;
19291 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
19292 sfk
= sfk_destructor
;
19293 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
19294 sfk
= sfk_conversion
;
19295 else if (/* There's no way to declare a constructor
19296 for an anonymous type, even if the type
19297 got a name for linkage purposes. */
19298 !TYPE_WAS_ANONYMOUS (class_type
)
19299 /* Handle correctly (c++/19200):
19313 friend void N::S();
19316 && class_type
!= qualifying_scope
)
19317 && constructor_name_p (unqualified_name
,
19320 unqualified_name
= constructor_name (class_type
);
19321 sfk
= sfk_constructor
;
19323 else if (is_overloaded_fn (unqualified_name
)
19324 && DECL_CONSTRUCTOR_P (get_first_fn
19325 (unqualified_name
)))
19326 sfk
= sfk_constructor
;
19328 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
19329 *ctor_dtor_or_conv_p
= -1;
19332 declarator
= make_id_declarator (qualifying_scope
,
19335 declarator
->std_attributes
= attrs
;
19336 declarator
->id_loc
= token
->location
;
19337 declarator
->parameter_pack_p
= pack_expansion_p
;
19339 if (pack_expansion_p
)
19340 maybe_warn_variadic_templates ();
19343 handle_declarator
:;
19344 scope
= get_scope_of_declarator (declarator
);
19347 /* Any names that appear after the declarator-id for a
19348 member are looked up in the containing scope. */
19349 if (at_function_scope_p ())
19351 /* But declarations with qualified-ids can't appear in a
19353 cp_parser_error (parser
, "qualified-id in declaration");
19354 declarator
= cp_error_declarator
;
19357 pushed_scope
= push_scope (scope
);
19359 parser
->in_declarator_p
= true;
19360 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
19361 || (declarator
&& declarator
->kind
== cdk_id
))
19362 /* Default args are only allowed on function
19364 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19366 parser
->default_arg_ok_p
= false;
19375 /* For an abstract declarator, we might wind up with nothing at this
19376 point. That's an error; the declarator is not optional. */
19378 cp_parser_error (parser
, "expected declarator");
19380 /* If we entered a scope, we must exit it now. */
19382 pop_scope (pushed_scope
);
19384 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19385 parser
->in_declarator_p
= saved_in_declarator_p
;
19390 /* Parse a ptr-operator.
19393 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19394 * cv-qualifier-seq [opt]
19396 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19397 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19402 & cv-qualifier-seq [opt]
19404 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19405 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19406 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19407 filled in with the TYPE containing the member. *CV_QUALS is
19408 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19409 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19410 Note that the tree codes returned by this function have nothing
19411 to do with the types of trees that will be eventually be created
19412 to represent the pointer or reference type being parsed. They are
19413 just constants with suggestive names. */
19414 static enum tree_code
19415 cp_parser_ptr_operator (cp_parser
* parser
,
19417 cp_cv_quals
*cv_quals
,
19420 enum tree_code code
= ERROR_MARK
;
19422 tree attrs
= NULL_TREE
;
19424 /* Assume that it's not a pointer-to-member. */
19426 /* And that there are no cv-qualifiers. */
19427 *cv_quals
= TYPE_UNQUALIFIED
;
19429 /* Peek at the next token. */
19430 token
= cp_lexer_peek_token (parser
->lexer
);
19432 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19433 if (token
->type
== CPP_MULT
)
19434 code
= INDIRECT_REF
;
19435 else if (token
->type
== CPP_AND
)
19437 else if ((cxx_dialect
!= cxx98
) &&
19438 token
->type
== CPP_AND_AND
) /* C++0x only */
19439 code
= NON_LVALUE_EXPR
;
19441 if (code
!= ERROR_MARK
)
19443 /* Consume the `*', `&' or `&&'. */
19444 cp_lexer_consume_token (parser
->lexer
);
19446 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19447 `&', if we are allowing GNU extensions. (The only qualifier
19448 that can legally appear after `&' is `restrict', but that is
19449 enforced during semantic analysis. */
19450 if (code
== INDIRECT_REF
19451 || cp_parser_allow_gnu_extensions_p (parser
))
19452 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19454 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19455 if (attributes
!= NULL
)
19456 *attributes
= attrs
;
19460 /* Try the pointer-to-member case. */
19461 cp_parser_parse_tentatively (parser
);
19462 /* Look for the optional `::' operator. */
19463 cp_parser_global_scope_opt (parser
,
19464 /*current_scope_valid_p=*/false);
19465 /* Look for the nested-name specifier. */
19466 token
= cp_lexer_peek_token (parser
->lexer
);
19467 cp_parser_nested_name_specifier (parser
,
19468 /*typename_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19471 /*is_declaration=*/false);
19472 /* If we found it, and the next token is a `*', then we are
19473 indeed looking at a pointer-to-member operator. */
19474 if (!cp_parser_error_occurred (parser
)
19475 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
19477 /* Indicate that the `*' operator was used. */
19478 code
= INDIRECT_REF
;
19480 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19481 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
19482 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
19483 error_at (token
->location
, "cannot form pointer to member of "
19484 "non-class %q#T", parser
->scope
);
19487 /* The type of which the member is a member is given by the
19489 *type
= parser
->scope
;
19490 /* The next name will not be qualified. */
19491 parser
->scope
= NULL_TREE
;
19492 parser
->qualifying_scope
= NULL_TREE
;
19493 parser
->object_scope
= NULL_TREE
;
19494 /* Look for optional c++11 attributes. */
19495 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19496 if (attributes
!= NULL
)
19497 *attributes
= attrs
;
19498 /* Look for the optional cv-qualifier-seq. */
19499 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19502 /* If that didn't work we don't have a ptr-operator. */
19503 if (!cp_parser_parse_definitely (parser
))
19504 cp_parser_error (parser
, "expected ptr-operator");
19510 /* Parse an (optional) cv-qualifier-seq.
19513 cv-qualifier cv-qualifier-seq [opt]
19524 Returns a bitmask representing the cv-qualifiers. */
19527 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
19529 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
19534 cp_cv_quals cv_qualifier
;
19536 /* Peek at the next token. */
19537 token
= cp_lexer_peek_token (parser
->lexer
);
19538 /* See if it's a cv-qualifier. */
19539 switch (token
->keyword
)
19542 cv_qualifier
= TYPE_QUAL_CONST
;
19546 cv_qualifier
= TYPE_QUAL_VOLATILE
;
19550 cv_qualifier
= TYPE_QUAL_RESTRICT
;
19554 cv_qualifier
= TYPE_UNQUALIFIED
;
19561 if (cv_quals
& cv_qualifier
)
19563 error_at (token
->location
, "duplicate cv-qualifier");
19564 cp_lexer_purge_token (parser
->lexer
);
19568 cp_lexer_consume_token (parser
->lexer
);
19569 cv_quals
|= cv_qualifier
;
19576 /* Parse an (optional) ref-qualifier
19582 Returns cp_ref_qualifier representing ref-qualifier. */
19584 static cp_ref_qualifier
19585 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
19587 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
19589 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19590 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
19595 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
19596 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19598 switch (token
->type
)
19601 curr_ref_qual
= REF_QUAL_LVALUE
;
19605 curr_ref_qual
= REF_QUAL_RVALUE
;
19609 curr_ref_qual
= REF_QUAL_NONE
;
19613 if (!curr_ref_qual
)
19617 error_at (token
->location
, "multiple ref-qualifiers");
19618 cp_lexer_purge_token (parser
->lexer
);
19622 ref_qual
= curr_ref_qual
;
19623 cp_lexer_consume_token (parser
->lexer
);
19630 /* Parse an optional tx-qualifier.
19634 transaction_safe_dynamic */
19637 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
19639 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19640 if (token
->type
== CPP_NAME
)
19642 tree name
= token
->u
.value
;
19643 const char *p
= IDENTIFIER_POINTER (name
);
19644 const int len
= strlen ("transaction_safe");
19645 if (!strncmp (p
, "transaction_safe", len
))
19649 || !strcmp (p
, "_dynamic"))
19651 cp_lexer_consume_token (parser
->lexer
);
19654 error ("%E requires %<-fgnu-tm%>", name
);
19665 /* Parse an (optional) virt-specifier-seq.
19667 virt-specifier-seq:
19668 virt-specifier virt-specifier-seq [opt]
19674 Returns a bitmask representing the virt-specifiers. */
19676 static cp_virt_specifiers
19677 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
19679 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19684 cp_virt_specifiers virt_specifier
;
19686 /* Peek at the next token. */
19687 token
= cp_lexer_peek_token (parser
->lexer
);
19688 /* See if it's a virt-specifier-qualifier. */
19689 if (token
->type
!= CPP_NAME
)
19691 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
19693 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19694 virt_specifier
= VIRT_SPEC_OVERRIDE
;
19696 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
19698 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19699 virt_specifier
= VIRT_SPEC_FINAL
;
19701 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
19703 virt_specifier
= VIRT_SPEC_FINAL
;
19708 if (virt_specifiers
& virt_specifier
)
19710 error_at (token
->location
, "duplicate virt-specifier");
19711 cp_lexer_purge_token (parser
->lexer
);
19715 cp_lexer_consume_token (parser
->lexer
);
19716 virt_specifiers
|= virt_specifier
;
19719 return virt_specifiers
;
19722 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19723 is in scope even though it isn't real. */
19726 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
19730 if (current_class_ptr
)
19732 /* We don't clear this between NSDMIs. Is it already what we want? */
19733 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
19734 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
19735 && cp_type_quals (type
) == quals
)
19739 this_parm
= build_this_parm (ctype
, quals
);
19740 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19741 current_class_ptr
= NULL_TREE
;
19743 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
19744 current_class_ptr
= this_parm
;
19747 /* Return true iff our current scope is a non-static data member
19751 parsing_nsdmi (void)
19753 /* We recognize NSDMI context by the context-less 'this' pointer set up
19754 by the function above. */
19755 if (current_class_ptr
19756 && TREE_CODE (current_class_ptr
) == PARM_DECL
19757 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
19762 /* Parse a late-specified return type, if any. This is not a separate
19763 non-terminal, but part of a function declarator, which looks like
19765 -> trailing-type-specifier-seq abstract-declarator(opt)
19767 Returns the type indicated by the type-id.
19769 In addition to this, parse any queued up omp declare simd
19770 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19772 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19776 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
19777 tree
& requires_clause
, cp_cv_quals quals
)
19780 tree type
= NULL_TREE
;
19781 bool declare_simd_p
= (parser
->omp_declare_simd
19783 && declarator
->kind
== cdk_id
);
19785 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
19786 && declarator
&& declarator
->kind
== cdk_id
);
19788 bool oacc_routine_p
= (parser
->oacc_routine
19790 && declarator
->kind
== cdk_id
);
19792 /* Peek at the next token. */
19793 token
= cp_lexer_peek_token (parser
->lexer
);
19794 /* A late-specified return type is indicated by an initial '->'. */
19795 if (token
->type
!= CPP_DEREF
19796 && token
->keyword
!= RID_REQUIRES
19797 && !(token
->type
== CPP_NAME
19798 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
19799 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
19802 tree save_ccp
= current_class_ptr
;
19803 tree save_ccr
= current_class_ref
;
19806 /* DR 1207: 'this' is in scope in the trailing return type. */
19807 inject_this_parameter (current_class_type
, quals
);
19810 if (token
->type
== CPP_DEREF
)
19812 /* Consume the ->. */
19813 cp_lexer_consume_token (parser
->lexer
);
19815 type
= cp_parser_trailing_type_id (parser
);
19818 /* Function declarations may be followed by a trailing
19819 requires-clause. */
19820 requires_clause
= cp_parser_requires_clause_opt (parser
);
19822 if (cilk_simd_fn_vector_p
)
19823 declarator
->attributes
19824 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
19825 declarator
->attributes
);
19826 if (declare_simd_p
)
19827 declarator
->attributes
19828 = cp_parser_late_parsing_omp_declare_simd (parser
,
19829 declarator
->attributes
);
19830 if (oacc_routine_p
)
19831 declarator
->attributes
19832 = cp_parser_late_parsing_oacc_routine (parser
,
19833 declarator
->attributes
);
19837 current_class_ptr
= save_ccp
;
19838 current_class_ref
= save_ccr
;
19844 /* Parse a declarator-id.
19848 :: [opt] nested-name-specifier [opt] type-name
19850 In the `id-expression' case, the value returned is as for
19851 cp_parser_id_expression if the id-expression was an unqualified-id.
19852 If the id-expression was a qualified-id, then a SCOPE_REF is
19853 returned. The first operand is the scope (either a NAMESPACE_DECL
19854 or TREE_TYPE), but the second is still just a representation of an
19858 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
19861 /* The expression must be an id-expression. Assume that qualified
19862 names are the names of types so that:
19865 int S<T>::R::i = 3;
19867 will work; we must treat `S<T>::R' as the name of a type.
19868 Similarly, assume that qualified names are templates, where
19872 int S<T>::R<T>::i = 3;
19875 id
= cp_parser_id_expression (parser
,
19876 /*template_keyword_p=*/false,
19877 /*check_dependency_p=*/false,
19878 /*template_p=*/NULL
,
19879 /*declarator_p=*/true,
19881 if (id
&& BASELINK_P (id
))
19882 id
= BASELINK_FUNCTIONS (id
);
19886 /* Parse a type-id.
19889 type-specifier-seq abstract-declarator [opt]
19891 Returns the TYPE specified. */
19894 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
19895 bool is_trailing_return
)
19897 cp_decl_specifier_seq type_specifier_seq
;
19898 cp_declarator
*abstract_declarator
;
19900 /* Parse the type-specifier-seq. */
19901 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
19902 is_trailing_return
,
19903 &type_specifier_seq
);
19904 if (type_specifier_seq
.type
== error_mark_node
)
19905 return error_mark_node
;
19907 /* There might or might not be an abstract declarator. */
19908 cp_parser_parse_tentatively (parser
);
19909 /* Look for the declarator. */
19910 abstract_declarator
19911 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
19912 /*parenthesized_p=*/NULL
,
19913 /*member_p=*/false,
19914 /*friend_p=*/false);
19915 /* Check to see if there really was a declarator. */
19916 if (!cp_parser_parse_definitely (parser
))
19917 abstract_declarator
= NULL
;
19919 if (type_specifier_seq
.type
19920 /* The concepts TS allows 'auto' as a type-id. */
19921 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
19922 /* None of the valid uses of 'auto' in C++14 involve the type-id
19923 nonterminal, but it is valid in a trailing-return-type. */
19924 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
19925 && type_uses_auto (type_specifier_seq
.type
))
19927 /* A type-id with type 'auto' is only ok if the abstract declarator
19928 is a function declarator with a late-specified return type.
19930 A type-id with 'auto' is also valid in a trailing-return-type
19931 in a compound-requirement. */
19932 if (abstract_declarator
19933 && abstract_declarator
->kind
== cdk_function
19934 && abstract_declarator
->u
.function
.late_return_type
)
19936 else if (parser
->in_result_type_constraint_p
)
19940 error ("invalid use of %<auto%>");
19941 return error_mark_node
;
19945 return groktypename (&type_specifier_seq
, abstract_declarator
,
19950 cp_parser_type_id (cp_parser
*parser
)
19952 return cp_parser_type_id_1 (parser
, false, false);
19956 cp_parser_template_type_arg (cp_parser
*parser
)
19959 const char *saved_message
= parser
->type_definition_forbidden_message
;
19960 parser
->type_definition_forbidden_message
19961 = G_("types may not be defined in template arguments");
19962 r
= cp_parser_type_id_1 (parser
, true, false);
19963 parser
->type_definition_forbidden_message
= saved_message
;
19964 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
19966 error ("invalid use of %<auto%> in template argument");
19967 r
= error_mark_node
;
19973 cp_parser_trailing_type_id (cp_parser
*parser
)
19975 return cp_parser_type_id_1 (parser
, false, true);
19978 /* Parse a type-specifier-seq.
19980 type-specifier-seq:
19981 type-specifier type-specifier-seq [opt]
19985 type-specifier-seq:
19986 attributes type-specifier-seq [opt]
19988 If IS_DECLARATION is true, we are at the start of a "condition" or
19989 exception-declaration, so we might be followed by a declarator-id.
19991 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19992 i.e. we've just seen "->".
19994 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19997 cp_parser_type_specifier_seq (cp_parser
* parser
,
19998 bool is_declaration
,
19999 bool is_trailing_return
,
20000 cp_decl_specifier_seq
*type_specifier_seq
)
20002 bool seen_type_specifier
= false;
20003 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
20004 cp_token
*start_token
= NULL
;
20006 /* Clear the TYPE_SPECIFIER_SEQ. */
20007 clear_decl_specs (type_specifier_seq
);
20009 /* In the context of a trailing return type, enum E { } is an
20010 elaborated-type-specifier followed by a function-body, not an
20012 if (is_trailing_return
)
20013 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
20015 /* Parse the type-specifiers and attributes. */
20018 tree type_specifier
;
20019 bool is_cv_qualifier
;
20021 /* Check for attributes first. */
20022 if (cp_next_tokens_can_be_attribute_p (parser
))
20024 type_specifier_seq
->attributes
=
20025 chainon (type_specifier_seq
->attributes
,
20026 cp_parser_attributes_opt (parser
));
20030 /* record the token of the beginning of the type specifier seq,
20031 for error reporting purposes*/
20033 start_token
= cp_lexer_peek_token (parser
->lexer
);
20035 /* Look for the type-specifier. */
20036 type_specifier
= cp_parser_type_specifier (parser
,
20038 type_specifier_seq
,
20039 /*is_declaration=*/false,
20042 if (!type_specifier
)
20044 /* If the first type-specifier could not be found, this is not a
20045 type-specifier-seq at all. */
20046 if (!seen_type_specifier
)
20048 /* Set in_declarator_p to avoid skipping to the semicolon. */
20049 int in_decl
= parser
->in_declarator_p
;
20050 parser
->in_declarator_p
= true;
20052 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
20053 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20054 cp_parser_error (parser
, "expected type-specifier");
20056 parser
->in_declarator_p
= in_decl
;
20058 type_specifier_seq
->type
= error_mark_node
;
20061 /* If subsequent type-specifiers could not be found, the
20062 type-specifier-seq is complete. */
20066 seen_type_specifier
= true;
20067 /* The standard says that a condition can be:
20069 type-specifier-seq declarator = assignment-expression
20076 we should treat the "S" as a declarator, not as a
20077 type-specifier. The standard doesn't say that explicitly for
20078 type-specifier-seq, but it does say that for
20079 decl-specifier-seq in an ordinary declaration. Perhaps it
20080 would be clearer just to allow a decl-specifier-seq here, and
20081 then add a semantic restriction that if any decl-specifiers
20082 that are not type-specifiers appear, the program is invalid. */
20083 if (is_declaration
&& !is_cv_qualifier
)
20084 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
20088 /* Return whether the function currently being declared has an associated
20089 template parameter list. */
20092 function_being_declared_is_template_p (cp_parser
* parser
)
20094 if (!current_template_parms
|| processing_template_parmlist
)
20097 if (parser
->implicit_template_scope
)
20100 if (at_class_scope_p ()
20101 && TYPE_BEING_DEFINED (current_class_type
))
20102 return parser
->num_template_parameter_lists
!= 0;
20104 return ((int) parser
->num_template_parameter_lists
> template_class_depth
20105 (current_class_type
));
20108 /* Parse a parameter-declaration-clause.
20110 parameter-declaration-clause:
20111 parameter-declaration-list [opt] ... [opt]
20112 parameter-declaration-list , ...
20114 Returns a representation for the parameter declarations. A return
20115 value of NULL indicates a parameter-declaration-clause consisting
20116 only of an ellipsis. */
20119 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
20128 int auto_is_implicit_function_template_parm_p
;
20130 parser
->auto_is_implicit_function_template_parm_p
20131 = auto_is_implicit_function_template_parm_p
;
20133 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
20137 if (!processing_specialization
20138 && !processing_template_parmlist
20139 && !processing_explicit_instantiation
)
20140 if (!current_function_decl
20141 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
20142 parser
->auto_is_implicit_function_template_parm_p
= true;
20144 /* Peek at the next token. */
20145 token
= cp_lexer_peek_token (parser
->lexer
);
20146 /* Check for trivial parameter-declaration-clauses. */
20147 if (token
->type
== CPP_ELLIPSIS
)
20149 /* Consume the `...' token. */
20150 cp_lexer_consume_token (parser
->lexer
);
20153 else if (token
->type
== CPP_CLOSE_PAREN
)
20154 /* There are no parameters. */
20156 #ifndef NO_IMPLICIT_EXTERN_C
20157 if (in_system_header_at (input_location
)
20158 && current_class_type
== NULL
20159 && current_lang_name
== lang_name_c
)
20163 return void_list_node
;
20165 /* Check for `(void)', too, which is a special case. */
20166 else if (token
->keyword
== RID_VOID
20167 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20168 == CPP_CLOSE_PAREN
))
20170 /* Consume the `void' token. */
20171 cp_lexer_consume_token (parser
->lexer
);
20172 /* There are no parameters. */
20173 return void_list_node
;
20176 /* Parse the parameter-declaration-list. */
20177 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
20178 /* If a parse error occurred while parsing the
20179 parameter-declaration-list, then the entire
20180 parameter-declaration-clause is erroneous. */
20184 /* Peek at the next token. */
20185 token
= cp_lexer_peek_token (parser
->lexer
);
20186 /* If it's a `,', the clause should terminate with an ellipsis. */
20187 if (token
->type
== CPP_COMMA
)
20189 /* Consume the `,'. */
20190 cp_lexer_consume_token (parser
->lexer
);
20191 /* Expect an ellipsis. */
20193 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
20195 /* It might also be `...' if the optional trailing `,' was
20197 else if (token
->type
== CPP_ELLIPSIS
)
20199 /* Consume the `...' token. */
20200 cp_lexer_consume_token (parser
->lexer
);
20201 /* And remember that we saw it. */
20205 ellipsis_p
= false;
20207 /* Finish the parameter list. */
20209 parameters
= chainon (parameters
, void_list_node
);
20214 /* Parse a parameter-declaration-list.
20216 parameter-declaration-list:
20217 parameter-declaration
20218 parameter-declaration-list , parameter-declaration
20220 Returns a representation of the parameter-declaration-list, as for
20221 cp_parser_parameter_declaration_clause. However, the
20222 `void_list_node' is never appended to the list. Upon return,
20223 *IS_ERROR will be true iff an error occurred. */
20226 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
20228 tree parameters
= NULL_TREE
;
20229 tree
*tail
= ¶meters
;
20230 bool saved_in_unbraced_linkage_specification_p
;
20233 /* Assume all will go well. */
20235 /* The special considerations that apply to a function within an
20236 unbraced linkage specifications do not apply to the parameters
20237 to the function. */
20238 saved_in_unbraced_linkage_specification_p
20239 = parser
->in_unbraced_linkage_specification_p
;
20240 parser
->in_unbraced_linkage_specification_p
= false;
20242 /* Look for more parameters. */
20245 cp_parameter_declarator
*parameter
;
20246 tree decl
= error_mark_node
;
20247 bool parenthesized_p
= false;
20248 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
20249 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20250 (current_template_parms
)) : 0);
20252 /* Parse the parameter. */
20254 = cp_parser_parameter_declaration (parser
,
20255 /*template_parm_p=*/false,
20258 /* We don't know yet if the enclosing context is deprecated, so wait
20259 and warn in grokparms if appropriate. */
20260 deprecated_state
= DEPRECATED_SUPPRESS
;
20264 /* If a function parameter pack was specified and an implicit template
20265 parameter was introduced during cp_parser_parameter_declaration,
20266 change any implicit parameters introduced into packs. */
20267 if (parser
->implicit_template_parms
20268 && parameter
->declarator
20269 && parameter
->declarator
->parameter_pack_p
)
20271 int latest_template_parm_idx
= TREE_VEC_LENGTH
20272 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
20274 if (latest_template_parm_idx
!= template_parm_idx
)
20275 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
20276 (parameter
->decl_specifiers
.type
,
20277 template_parm_idx
, latest_template_parm_idx
);
20280 decl
= grokdeclarator (parameter
->declarator
,
20281 ¶meter
->decl_specifiers
,
20283 parameter
->default_argument
!= NULL_TREE
,
20284 ¶meter
->decl_specifiers
.attributes
);
20287 deprecated_state
= DEPRECATED_NORMAL
;
20289 /* If a parse error occurred parsing the parameter declaration,
20290 then the entire parameter-declaration-list is erroneous. */
20291 if (decl
== error_mark_node
)
20294 parameters
= error_mark_node
;
20298 if (parameter
->decl_specifiers
.attributes
)
20299 cplus_decl_attributes (&decl
,
20300 parameter
->decl_specifiers
.attributes
,
20302 if (DECL_NAME (decl
))
20303 decl
= pushdecl (decl
);
20305 if (decl
!= error_mark_node
)
20307 retrofit_lang_decl (decl
);
20308 DECL_PARM_INDEX (decl
) = ++index
;
20309 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
20312 /* Add the new parameter to the list. */
20313 *tail
= build_tree_list (parameter
->default_argument
, decl
);
20314 tail
= &TREE_CHAIN (*tail
);
20316 /* Peek at the next token. */
20317 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
20318 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
20319 /* These are for Objective-C++ */
20320 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
20321 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20322 /* The parameter-declaration-list is complete. */
20324 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20328 /* Peek at the next token. */
20329 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20330 /* If it's an ellipsis, then the list is complete. */
20331 if (token
->type
== CPP_ELLIPSIS
)
20333 /* Otherwise, there must be more parameters. Consume the
20335 cp_lexer_consume_token (parser
->lexer
);
20336 /* When parsing something like:
20338 int i(float f, double d)
20340 we can tell after seeing the declaration for "f" that we
20341 are not looking at an initialization of a variable "i",
20342 but rather at the declaration of a function "i".
20344 Due to the fact that the parsing of template arguments
20345 (as specified to a template-id) requires backtracking we
20346 cannot use this technique when inside a template argument
20348 if (!parser
->in_template_argument_list_p
20349 && !parser
->in_type_id_in_expr_p
20350 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
20351 /* However, a parameter-declaration of the form
20352 "float(f)" (which is a valid declaration of a
20353 parameter "f") can also be interpreted as an
20354 expression (the conversion of "f" to "float"). */
20355 && !parenthesized_p
)
20356 cp_parser_commit_to_tentative_parse (parser
);
20360 cp_parser_error (parser
, "expected %<,%> or %<...%>");
20361 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
20362 cp_parser_skip_to_closing_parenthesis (parser
,
20363 /*recovering=*/true,
20364 /*or_comma=*/false,
20365 /*consume_paren=*/false);
20370 parser
->in_unbraced_linkage_specification_p
20371 = saved_in_unbraced_linkage_specification_p
;
20373 /* Reset implicit_template_scope if we are about to leave the function
20374 parameter list that introduced it. Note that for out-of-line member
20375 definitions, there will be one or more class scopes before we get to
20376 the template parameter scope. */
20378 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
20379 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
20381 while (maybe_its
->kind
== sk_class
)
20382 maybe_its
= maybe_its
->level_chain
;
20383 if (maybe_its
== its
)
20385 parser
->implicit_template_parms
= 0;
20386 parser
->implicit_template_scope
= 0;
20393 /* Parse a parameter declaration.
20395 parameter-declaration:
20396 decl-specifier-seq ... [opt] declarator
20397 decl-specifier-seq declarator = assignment-expression
20398 decl-specifier-seq ... [opt] abstract-declarator [opt]
20399 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20401 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20402 declares a template parameter. (In that case, a non-nested `>'
20403 token encountered during the parsing of the assignment-expression
20404 is not interpreted as a greater-than operator.)
20406 Returns a representation of the parameter, or NULL if an error
20407 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20408 true iff the declarator is of the form "(p)". */
20410 static cp_parameter_declarator
*
20411 cp_parser_parameter_declaration (cp_parser
*parser
,
20412 bool template_parm_p
,
20413 bool *parenthesized_p
)
20415 int declares_class_or_enum
;
20416 cp_decl_specifier_seq decl_specifiers
;
20417 cp_declarator
*declarator
;
20418 tree default_argument
;
20419 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
20420 const char *saved_message
;
20421 bool template_parameter_pack_p
= false;
20423 /* In a template parameter, `>' is not an operator.
20427 When parsing a default template-argument for a non-type
20428 template-parameter, the first non-nested `>' is taken as the end
20429 of the template parameter-list rather than a greater-than
20432 /* Type definitions may not appear in parameter types. */
20433 saved_message
= parser
->type_definition_forbidden_message
;
20434 parser
->type_definition_forbidden_message
20435 = G_("types may not be defined in parameter types");
20437 /* Parse the declaration-specifiers. */
20438 cp_parser_decl_specifier_seq (parser
,
20439 CP_PARSER_FLAGS_NONE
,
20441 &declares_class_or_enum
);
20443 /* Complain about missing 'typename' or other invalid type names. */
20444 if (!decl_specifiers
.any_type_specifiers_p
20445 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20446 decl_specifiers
.type
= error_mark_node
;
20448 /* If an error occurred, there's no reason to attempt to parse the
20449 rest of the declaration. */
20450 if (cp_parser_error_occurred (parser
))
20452 parser
->type_definition_forbidden_message
= saved_message
;
20456 /* Peek at the next token. */
20457 token
= cp_lexer_peek_token (parser
->lexer
);
20459 /* If the next token is a `)', `,', `=', `>', or `...', then there
20460 is no declarator. However, when variadic templates are enabled,
20461 there may be a declarator following `...'. */
20462 if (token
->type
== CPP_CLOSE_PAREN
20463 || token
->type
== CPP_COMMA
20464 || token
->type
== CPP_EQ
20465 || token
->type
== CPP_GREATER
)
20468 if (parenthesized_p
)
20469 *parenthesized_p
= false;
20471 /* Otherwise, there should be a declarator. */
20474 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20475 parser
->default_arg_ok_p
= false;
20477 /* After seeing a decl-specifier-seq, if the next token is not a
20478 "(", there is no possibility that the code is a valid
20479 expression. Therefore, if parsing tentatively, we commit at
20481 if (!parser
->in_template_argument_list_p
20482 /* In an expression context, having seen:
20486 we cannot be sure whether we are looking at a
20487 function-type (taking a "char" as a parameter) or a cast
20488 of some object of type "char" to "int". */
20489 && !parser
->in_type_id_in_expr_p
20490 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
20491 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
20492 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
20493 cp_parser_commit_to_tentative_parse (parser
);
20494 /* Parse the declarator. */
20495 declarator_token_start
= token
;
20496 declarator
= cp_parser_declarator (parser
,
20497 CP_PARSER_DECLARATOR_EITHER
,
20498 /*ctor_dtor_or_conv_p=*/NULL
,
20500 /*member_p=*/false,
20501 /*friend_p=*/false);
20502 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20503 /* After the declarator, allow more attributes. */
20504 decl_specifiers
.attributes
20505 = chainon (decl_specifiers
.attributes
,
20506 cp_parser_attributes_opt (parser
));
20508 /* If the declarator is a template parameter pack, remember that and
20509 clear the flag in the declarator itself so we don't get errors
20510 from grokdeclarator. */
20511 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
20513 declarator
->parameter_pack_p
= false;
20514 template_parameter_pack_p
= true;
20518 /* If the next token is an ellipsis, and we have not seen a declarator
20519 name, and if either the type of the declarator contains parameter
20520 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20521 for, eg, abbreviated integral type names), then we actually have a
20522 parameter pack expansion expression. Otherwise, leave the ellipsis
20523 for a C-style variadic function. */
20524 token
= cp_lexer_peek_token (parser
->lexer
);
20525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20527 tree type
= decl_specifiers
.type
;
20529 if (type
&& DECL_P (type
))
20530 type
= TREE_TYPE (type
);
20533 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
20534 && (template_parm_p
|| uses_parameter_packs (type
)))
20535 || (!type
&& template_parm_p
))
20536 && declarator_can_be_parameter_pack (declarator
))
20538 /* Consume the `...'. */
20539 cp_lexer_consume_token (parser
->lexer
);
20540 maybe_warn_variadic_templates ();
20542 /* Build a pack expansion type */
20543 if (template_parm_p
)
20544 template_parameter_pack_p
= true;
20545 else if (declarator
)
20546 declarator
->parameter_pack_p
= true;
20548 decl_specifiers
.type
= make_pack_expansion (type
);
20552 /* The restriction on defining new types applies only to the type
20553 of the parameter, not to the default argument. */
20554 parser
->type_definition_forbidden_message
= saved_message
;
20556 /* If the next token is `=', then process a default argument. */
20557 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20559 tree type
= decl_specifiers
.type
;
20560 token
= cp_lexer_peek_token (parser
->lexer
);
20561 /* If we are defining a class, then the tokens that make up the
20562 default argument must be saved and processed later. */
20563 if (!template_parm_p
&& at_class_scope_p ()
20564 && TYPE_BEING_DEFINED (current_class_type
)
20565 && !LAMBDA_TYPE_P (current_class_type
))
20566 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
20568 // A constrained-type-specifier may declare a type template-parameter.
20569 else if (declares_constrained_type_template_parameter (type
))
20571 = cp_parser_default_type_template_argument (parser
);
20573 // A constrained-type-specifier may declare a template-template-parameter.
20574 else if (declares_constrained_template_template_parameter (type
))
20576 = cp_parser_default_template_template_argument (parser
);
20578 /* Outside of a class definition, we can just parse the
20579 assignment-expression. */
20582 = cp_parser_default_argument (parser
, template_parm_p
);
20584 if (!parser
->default_arg_ok_p
)
20586 permerror (token
->location
,
20587 "default arguments are only "
20588 "permitted for function parameters");
20590 else if ((declarator
&& declarator
->parameter_pack_p
)
20591 || template_parameter_pack_p
20592 || (decl_specifiers
.type
20593 && PACK_EXPANSION_P (decl_specifiers
.type
)))
20595 /* Find the name of the parameter pack. */
20596 cp_declarator
*id_declarator
= declarator
;
20597 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
20598 id_declarator
= id_declarator
->declarator
;
20600 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
20601 error_at (declarator_token_start
->location
,
20603 ? G_("template parameter pack %qD "
20604 "cannot have a default argument")
20605 : G_("parameter pack %qD cannot have "
20606 "a default argument"),
20607 id_declarator
->u
.id
.unqualified_name
);
20609 error_at (declarator_token_start
->location
,
20611 ? G_("template parameter pack cannot have "
20612 "a default argument")
20613 : G_("parameter pack cannot have a "
20614 "default argument"));
20616 default_argument
= NULL_TREE
;
20620 default_argument
= NULL_TREE
;
20622 return make_parameter_declarator (&decl_specifiers
,
20625 template_parameter_pack_p
);
20628 /* Parse a default argument and return it.
20630 TEMPLATE_PARM_P is true if this is a default argument for a
20631 non-type template parameter. */
20633 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
20635 tree default_argument
= NULL_TREE
;
20636 bool saved_greater_than_is_operator_p
;
20637 bool saved_local_variables_forbidden_p
;
20638 bool non_constant_p
, is_direct_init
;
20640 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20642 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
20643 parser
->greater_than_is_operator_p
= !template_parm_p
;
20644 /* Local variable names (and the `this' keyword) may not
20645 appear in a default argument. */
20646 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
20647 parser
->local_variables_forbidden_p
= true;
20648 /* Parse the assignment-expression. */
20649 if (template_parm_p
)
20650 push_deferring_access_checks (dk_no_deferred
);
20651 tree saved_class_ptr
= NULL_TREE
;
20652 tree saved_class_ref
= NULL_TREE
;
20653 /* The "this" pointer is not valid in a default argument. */
20656 saved_class_ptr
= current_class_ptr
;
20657 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
20658 saved_class_ref
= current_class_ref
;
20659 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
20662 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
20663 /* Restore the "this" pointer. */
20666 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
20667 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
20669 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
20670 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20671 if (template_parm_p
)
20672 pop_deferring_access_checks ();
20673 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
20674 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20676 return default_argument
;
20679 /* Parse a function-body.
20682 compound_statement */
20685 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
20687 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
20688 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
20692 /* Parse a ctor-initializer-opt followed by a function-body. Return
20693 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20694 is true we are parsing a function-try-block. */
20697 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
20698 bool in_function_try_block
)
20701 bool ctor_initializer_p
;
20702 const bool check_body_p
=
20703 DECL_CONSTRUCTOR_P (current_function_decl
)
20704 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
20707 /* Begin the function body. */
20708 body
= begin_function_body ();
20709 /* Parse the optional ctor-initializer. */
20710 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
20712 /* If we're parsing a constexpr constructor definition, we need
20713 to check that the constructor body is indeed empty. However,
20714 before we get to cp_parser_function_body lot of junk has been
20715 generated, so we can't just check that we have an empty block.
20716 Rather we take a snapshot of the outermost block, and check whether
20717 cp_parser_function_body changed its state. */
20720 list
= cur_stmt_list
;
20721 if (STATEMENT_LIST_TAIL (list
))
20722 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
20724 /* Parse the function-body. */
20725 cp_parser_function_body (parser
, in_function_try_block
);
20727 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
20728 /* Finish the function body. */
20729 finish_function_body (body
);
20731 return ctor_initializer_p
;
20734 /* Parse an initializer.
20737 = initializer-clause
20738 ( expression-list )
20740 Returns an expression representing the initializer. If no
20741 initializer is present, NULL_TREE is returned.
20743 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20744 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20745 set to TRUE if there is no initializer present. If there is an
20746 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20747 is set to true; otherwise it is set to false. */
20750 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
20751 bool* non_constant_p
)
20756 /* Peek at the next token. */
20757 token
= cp_lexer_peek_token (parser
->lexer
);
20759 /* Let our caller know whether or not this initializer was
20761 *is_direct_init
= (token
->type
!= CPP_EQ
);
20762 /* Assume that the initializer is constant. */
20763 *non_constant_p
= false;
20765 if (token
->type
== CPP_EQ
)
20767 /* Consume the `='. */
20768 cp_lexer_consume_token (parser
->lexer
);
20769 /* Parse the initializer-clause. */
20770 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
20772 else if (token
->type
== CPP_OPEN_PAREN
)
20774 vec
<tree
, va_gc
> *vec
;
20775 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
20777 /*allow_expansion_p=*/true,
20780 return error_mark_node
;
20781 init
= build_tree_list_vec (vec
);
20782 release_tree_vector (vec
);
20784 else if (token
->type
== CPP_OPEN_BRACE
)
20786 cp_lexer_set_source_position (parser
->lexer
);
20787 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20788 init
= cp_parser_braced_list (parser
, non_constant_p
);
20789 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
20793 /* Anything else is an error. */
20794 cp_parser_error (parser
, "expected initializer");
20795 init
= error_mark_node
;
20801 /* Parse an initializer-clause.
20803 initializer-clause:
20804 assignment-expression
20807 Returns an expression representing the initializer.
20809 If the `assignment-expression' production is used the value
20810 returned is simply a representation for the expression.
20812 Otherwise, calls cp_parser_braced_list. */
20815 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
20817 cp_expr initializer
;
20819 /* Assume the expression is constant. */
20820 *non_constant_p
= false;
20822 /* If it is not a `{', then we are looking at an
20823 assignment-expression. */
20824 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
20827 = cp_parser_constant_expression (parser
,
20828 /*allow_non_constant_p=*/true,
20832 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
20834 return initializer
;
20837 /* Parse a brace-enclosed initializer list.
20840 { initializer-list , [opt] }
20843 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20844 the elements of the initializer-list (or NULL, if the last
20845 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20846 NULL_TREE. There is no way to detect whether or not the optional
20847 trailing `,' was provided. NON_CONSTANT_P is as for
20848 cp_parser_initializer. */
20851 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
20854 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20856 /* Consume the `{' token. */
20857 cp_lexer_consume_token (parser
->lexer
);
20858 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20859 initializer
= make_node (CONSTRUCTOR
);
20860 /* If it's not a `}', then there is a non-trivial initializer. */
20861 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
20863 /* Parse the initializer list. */
20864 CONSTRUCTOR_ELTS (initializer
)
20865 = cp_parser_initializer_list (parser
, non_constant_p
);
20866 /* A trailing `,' token is allowed. */
20867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20868 cp_lexer_consume_token (parser
->lexer
);
20871 *non_constant_p
= false;
20872 /* Now, there should be a trailing `}'. */
20873 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20874 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20875 TREE_TYPE (initializer
) = init_list_type_node
;
20877 cp_expr
result (initializer
);
20878 /* Build a location of the form:
20881 with caret==start at the open brace, finish at the close brace. */
20882 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
20883 result
.set_location (combined_loc
);
20887 /* Consume tokens up to, and including, the next non-nested closing `]'.
20888 Returns true iff we found a closing `]'. */
20891 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
20893 unsigned square_depth
= 0;
20897 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
20899 switch (token
->type
)
20902 case CPP_PRAGMA_EOL
:
20903 /* If we've run out of tokens, then there is no closing `]'. */
20906 case CPP_OPEN_SQUARE
:
20910 case CPP_CLOSE_SQUARE
:
20911 if (!square_depth
--)
20913 cp_lexer_consume_token (parser
->lexer
);
20922 /* Consume the token. */
20923 cp_lexer_consume_token (parser
->lexer
);
20927 /* Return true if we are looking at an array-designator, false otherwise. */
20930 cp_parser_array_designator_p (cp_parser
*parser
)
20932 /* Consume the `['. */
20933 cp_lexer_consume_token (parser
->lexer
);
20935 cp_lexer_save_tokens (parser
->lexer
);
20937 /* Skip tokens until the next token is a closing square bracket.
20938 If we find the closing `]', and the next token is a `=', then
20939 we are looking at an array designator. */
20940 bool array_designator_p
20941 = (cp_parser_skip_to_closing_square_bracket (parser
)
20942 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
20944 /* Roll back the tokens we skipped. */
20945 cp_lexer_rollback_tokens (parser
->lexer
);
20947 return array_designator_p
;
20950 /* Parse an initializer-list.
20953 initializer-clause ... [opt]
20954 initializer-list , initializer-clause ... [opt]
20959 designation initializer-clause ...[opt]
20960 initializer-list , designation initializer-clause ...[opt]
20965 [ constant-expression ] =
20967 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20968 for the initializer. If the INDEX of the elt is non-NULL, it is the
20969 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20970 as for cp_parser_initializer. */
20972 static vec
<constructor_elt
, va_gc
> *
20973 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
20975 vec
<constructor_elt
, va_gc
> *v
= NULL
;
20977 /* Assume all of the expressions are constant. */
20978 *non_constant_p
= false;
20980 /* Parse the rest of the list. */
20986 bool clause_non_constant_p
;
20988 /* If the next token is an identifier and the following one is a
20989 colon, we are looking at the GNU designated-initializer
20991 if (cp_parser_allow_gnu_extensions_p (parser
)
20992 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
20993 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
20995 /* Warn the user that they are using an extension. */
20996 pedwarn (input_location
, OPT_Wpedantic
,
20997 "ISO C++ does not allow designated initializers");
20998 /* Consume the identifier. */
20999 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21000 /* Consume the `:'. */
21001 cp_lexer_consume_token (parser
->lexer
);
21003 /* Also handle the C99 syntax, '. id ='. */
21004 else if (cp_parser_allow_gnu_extensions_p (parser
)
21005 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
21006 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
21007 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
21009 /* Warn the user that they are using an extension. */
21010 pedwarn (input_location
, OPT_Wpedantic
,
21011 "ISO C++ does not allow C99 designated initializers");
21012 /* Consume the `.'. */
21013 cp_lexer_consume_token (parser
->lexer
);
21014 /* Consume the identifier. */
21015 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
21016 /* Consume the `='. */
21017 cp_lexer_consume_token (parser
->lexer
);
21019 /* Also handle C99 array designators, '[ const ] ='. */
21020 else if (cp_parser_allow_gnu_extensions_p (parser
)
21021 && !c_dialect_objc ()
21022 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21024 /* In C++11, [ could start a lambda-introducer. */
21025 bool non_const
= false;
21027 cp_parser_parse_tentatively (parser
);
21029 if (!cp_parser_array_designator_p (parser
))
21031 cp_parser_simulate_error (parser
);
21032 designator
= NULL_TREE
;
21036 designator
= cp_parser_constant_expression (parser
, true,
21038 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21039 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
21042 if (!cp_parser_parse_definitely (parser
))
21043 designator
= NULL_TREE
;
21044 else if (non_const
)
21045 require_potential_rvalue_constant_expression (designator
);
21048 designator
= NULL_TREE
;
21050 /* Parse the initializer. */
21051 initializer
= cp_parser_initializer_clause (parser
,
21052 &clause_non_constant_p
);
21053 /* If any clause is non-constant, so is the entire initializer. */
21054 if (clause_non_constant_p
)
21055 *non_constant_p
= true;
21057 /* If we have an ellipsis, this is an initializer pack
21059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21061 /* Consume the `...'. */
21062 cp_lexer_consume_token (parser
->lexer
);
21064 /* Turn the initializer into an initializer expansion. */
21065 initializer
= make_pack_expansion (initializer
);
21068 /* Add it to the vector. */
21069 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
21071 /* If the next token is not a comma, we have reached the end of
21073 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21076 /* Peek at the next token. */
21077 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21078 /* If the next token is a `}', then we're still done. An
21079 initializer-clause can have a trailing `,' after the
21080 initializer-list and before the closing `}'. */
21081 if (token
->type
== CPP_CLOSE_BRACE
)
21084 /* Consume the `,' token. */
21085 cp_lexer_consume_token (parser
->lexer
);
21091 /* Classes [gram.class] */
21093 /* Parse a class-name.
21099 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21100 to indicate that names looked up in dependent types should be
21101 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21102 keyword has been used to indicate that the name that appears next
21103 is a template. TAG_TYPE indicates the explicit tag given before
21104 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21105 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21106 is the class being defined in a class-head. If ENUM_OK is TRUE,
21107 enum-names are also accepted.
21109 Returns the TYPE_DECL representing the class. */
21112 cp_parser_class_name (cp_parser
*parser
,
21113 bool typename_keyword_p
,
21114 bool template_keyword_p
,
21115 enum tag_types tag_type
,
21116 bool check_dependency_p
,
21118 bool is_declaration
,
21125 tree identifier
= NULL_TREE
;
21127 /* All class-names start with an identifier. */
21128 token
= cp_lexer_peek_token (parser
->lexer
);
21129 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
21131 cp_parser_error (parser
, "expected class-name");
21132 return error_mark_node
;
21135 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21136 to a template-id, so we save it here. */
21137 scope
= parser
->scope
;
21138 if (scope
== error_mark_node
)
21139 return error_mark_node
;
21141 /* Any name names a type if we're following the `typename' keyword
21142 in a qualified name where the enclosing scope is type-dependent. */
21143 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
21144 && dependent_type_p (scope
));
21145 /* Handle the common case (an identifier, but not a template-id)
21147 if (token
->type
== CPP_NAME
21148 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
21150 cp_token
*identifier_token
;
21153 /* Look for the identifier. */
21154 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
21155 ambiguous_p
= identifier_token
->error_reported
;
21156 identifier
= cp_parser_identifier (parser
);
21157 /* If the next token isn't an identifier, we are certainly not
21158 looking at a class-name. */
21159 if (identifier
== error_mark_node
)
21160 decl
= error_mark_node
;
21161 /* If we know this is a type-name, there's no need to look it
21163 else if (typename_p
)
21167 tree ambiguous_decls
;
21168 /* If we already know that this lookup is ambiguous, then
21169 we've already issued an error message; there's no reason
21173 cp_parser_simulate_error (parser
);
21174 return error_mark_node
;
21176 /* If the next token is a `::', then the name must be a type
21179 [basic.lookup.qual]
21181 During the lookup for a name preceding the :: scope
21182 resolution operator, object, function, and enumerator
21183 names are ignored. */
21184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21185 tag_type
= typename_type
;
21186 /* Look up the name. */
21187 decl
= cp_parser_lookup_name (parser
, identifier
,
21189 /*is_template=*/false,
21190 /*is_namespace=*/false,
21191 check_dependency_p
,
21193 identifier_token
->location
);
21194 if (ambiguous_decls
)
21196 if (cp_parser_parsing_tentatively (parser
))
21197 cp_parser_simulate_error (parser
);
21198 return error_mark_node
;
21204 /* Try a template-id. */
21205 decl
= cp_parser_template_id (parser
, template_keyword_p
,
21206 check_dependency_p
,
21209 if (decl
== error_mark_node
)
21210 return error_mark_node
;
21213 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
21215 /* If this is a typename, create a TYPENAME_TYPE. */
21216 if (typename_p
&& decl
!= error_mark_node
)
21218 decl
= make_typename_type (scope
, decl
, typename_type
,
21219 /*complain=*/tf_error
);
21220 if (decl
!= error_mark_node
)
21221 decl
= TYPE_NAME (decl
);
21224 decl
= strip_using_decl (decl
);
21226 /* Check to see that it is really the name of a class. */
21227 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
21228 && identifier_p (TREE_OPERAND (decl
, 0))
21229 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21230 /* Situations like this:
21232 template <typename T> struct A {
21233 typename T::template X<int>::I i;
21236 are problematic. Is `T::template X<int>' a class-name? The
21237 standard does not seem to be definitive, but there is no other
21238 valid interpretation of the following `::'. Therefore, those
21239 names are considered class-names. */
21241 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
21242 if (decl
!= error_mark_node
)
21243 decl
= TYPE_NAME (decl
);
21245 else if (TREE_CODE (decl
) != TYPE_DECL
21246 || TREE_TYPE (decl
) == error_mark_node
21247 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
21248 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
21249 /* In Objective-C 2.0, a classname followed by '.' starts a
21250 dot-syntax expression, and it's not a type-name. */
21251 || (c_dialect_objc ()
21252 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
21253 && objc_is_class_name (decl
)))
21254 decl
= error_mark_node
;
21256 if (decl
== error_mark_node
)
21257 cp_parser_error (parser
, "expected class-name");
21258 else if (identifier
&& !parser
->scope
)
21259 maybe_note_name_used_in_class (identifier
, decl
);
21264 /* Parse a class-specifier.
21267 class-head { member-specification [opt] }
21269 Returns the TREE_TYPE representing the class. */
21272 cp_parser_class_specifier_1 (cp_parser
* parser
)
21275 tree attributes
= NULL_TREE
;
21276 bool nested_name_specifier_p
;
21277 unsigned saved_num_template_parameter_lists
;
21278 bool saved_in_function_body
;
21279 unsigned char in_statement
;
21280 bool in_switch_statement_p
;
21281 bool saved_in_unbraced_linkage_specification_p
;
21282 tree old_scope
= NULL_TREE
;
21283 tree scope
= NULL_TREE
;
21284 cp_token
*closing_brace
;
21286 push_deferring_access_checks (dk_no_deferred
);
21288 /* Parse the class-head. */
21289 type
= cp_parser_class_head (parser
,
21290 &nested_name_specifier_p
);
21291 /* If the class-head was a semantic disaster, skip the entire body
21295 cp_parser_skip_to_end_of_block_or_statement (parser
);
21296 pop_deferring_access_checks ();
21297 return error_mark_node
;
21300 /* Look for the `{'. */
21301 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
21303 pop_deferring_access_checks ();
21304 return error_mark_node
;
21307 cp_ensure_no_omp_declare_simd (parser
);
21308 cp_ensure_no_oacc_routine (parser
);
21310 /* Issue an error message if type-definitions are forbidden here. */
21311 cp_parser_check_type_definition (parser
);
21312 /* Remember that we are defining one more class. */
21313 ++parser
->num_classes_being_defined
;
21314 /* Inside the class, surrounding template-parameter-lists do not
21316 saved_num_template_parameter_lists
21317 = parser
->num_template_parameter_lists
;
21318 parser
->num_template_parameter_lists
= 0;
21319 /* We are not in a function body. */
21320 saved_in_function_body
= parser
->in_function_body
;
21321 parser
->in_function_body
= false;
21322 /* Or in a loop. */
21323 in_statement
= parser
->in_statement
;
21324 parser
->in_statement
= 0;
21325 /* Or in a switch. */
21326 in_switch_statement_p
= parser
->in_switch_statement_p
;
21327 parser
->in_switch_statement_p
= false;
21328 /* We are not immediately inside an extern "lang" block. */
21329 saved_in_unbraced_linkage_specification_p
21330 = parser
->in_unbraced_linkage_specification_p
;
21331 parser
->in_unbraced_linkage_specification_p
= false;
21333 // Associate constraints with the type.
21335 type
= associate_classtype_constraints (type
);
21337 /* Start the class. */
21338 if (nested_name_specifier_p
)
21340 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
21341 old_scope
= push_inner_scope (scope
);
21343 type
= begin_class_definition (type
);
21345 if (type
== error_mark_node
)
21346 /* If the type is erroneous, skip the entire body of the class. */
21347 cp_parser_skip_to_closing_brace (parser
);
21349 /* Parse the member-specification. */
21350 cp_parser_member_specification_opt (parser
);
21352 /* Look for the trailing `}'. */
21353 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21354 /* Look for trailing attributes to apply to this class. */
21355 if (cp_parser_allow_gnu_extensions_p (parser
))
21356 attributes
= cp_parser_gnu_attributes_opt (parser
);
21357 if (type
!= error_mark_node
)
21358 type
= finish_struct (type
, attributes
);
21359 if (nested_name_specifier_p
)
21360 pop_inner_scope (old_scope
, scope
);
21362 /* We've finished a type definition. Check for the common syntax
21363 error of forgetting a semicolon after the definition. We need to
21364 be careful, as we can't just check for not-a-semicolon and be done
21365 with it; the user might have typed:
21367 class X { } c = ...;
21368 class X { } *p = ...;
21370 and so forth. Instead, enumerate all the possible tokens that
21371 might follow this production; if we don't see one of them, then
21372 complain and silently insert the semicolon. */
21374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21375 bool want_semicolon
= true;
21377 if (cp_next_tokens_can_be_std_attribute_p (parser
))
21378 /* Don't try to parse c++11 attributes here. As per the
21379 grammar, that should be a task for
21380 cp_parser_decl_specifier_seq. */
21381 want_semicolon
= false;
21383 switch (token
->type
)
21386 case CPP_SEMICOLON
:
21389 case CPP_OPEN_PAREN
:
21390 case CPP_CLOSE_PAREN
:
21392 want_semicolon
= false;
21395 /* While it's legal for type qualifiers and storage class
21396 specifiers to follow type definitions in the grammar, only
21397 compiler testsuites contain code like that. Assume that if
21398 we see such code, then what we're really seeing is a case
21402 const <type> var = ...;
21407 static <type> func (...) ...
21409 i.e. the qualifier or specifier applies to the next
21410 declaration. To do so, however, we need to look ahead one
21411 more token to see if *that* token is a type specifier.
21413 This code could be improved to handle:
21416 static const <type> var = ...; */
21418 if (keyword_is_decl_specifier (token
->keyword
))
21420 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21422 /* Handling user-defined types here would be nice, but very
21425 = (lookahead
->type
== CPP_KEYWORD
21426 && keyword_begins_type_specifier (lookahead
->keyword
));
21433 /* If we don't have a type, then something is very wrong and we
21434 shouldn't try to do anything clever. Likewise for not seeing the
21436 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
21438 cp_token_position prev
21439 = cp_lexer_previous_token_position (parser
->lexer
);
21440 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
21441 location_t loc
= prev_token
->location
;
21443 if (CLASSTYPE_DECLARED_CLASS (type
))
21444 error_at (loc
, "expected %<;%> after class definition");
21445 else if (TREE_CODE (type
) == RECORD_TYPE
)
21446 error_at (loc
, "expected %<;%> after struct definition");
21447 else if (TREE_CODE (type
) == UNION_TYPE
)
21448 error_at (loc
, "expected %<;%> after union definition");
21450 gcc_unreachable ();
21452 /* Unget one token and smash it to look as though we encountered
21453 a semicolon in the input stream. */
21454 cp_lexer_set_token_position (parser
->lexer
, prev
);
21455 token
= cp_lexer_peek_token (parser
->lexer
);
21456 token
->type
= CPP_SEMICOLON
;
21457 token
->keyword
= RID_MAX
;
21461 /* If this class is not itself within the scope of another class,
21462 then we need to parse the bodies of all of the queued function
21463 definitions. Note that the queued functions defined in a class
21464 are not always processed immediately following the
21465 class-specifier for that class. Consider:
21468 struct B { void f() { sizeof (A); } };
21471 If `f' were processed before the processing of `A' were
21472 completed, there would be no way to compute the size of `A'.
21473 Note that the nesting we are interested in here is lexical --
21474 not the semantic nesting given by TYPE_CONTEXT. In particular,
21477 struct A { struct B; };
21478 struct A::B { void f() { } };
21480 there is no need to delay the parsing of `A::B::f'. */
21481 if (--parser
->num_classes_being_defined
== 0)
21484 tree class_type
= NULL_TREE
;
21485 tree pushed_scope
= NULL_TREE
;
21487 cp_default_arg_entry
*e
;
21488 tree save_ccp
, save_ccr
;
21490 /* In a first pass, parse default arguments to the functions.
21491 Then, in a second pass, parse the bodies of the functions.
21492 This two-phased approach handles cases like:
21500 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
21503 /* If there are default arguments that have not yet been processed,
21504 take care of them now. */
21505 if (class_type
!= e
->class_type
)
21508 pop_scope (pushed_scope
);
21509 class_type
= e
->class_type
;
21510 pushed_scope
= push_scope (class_type
);
21512 /* Make sure that any template parameters are in scope. */
21513 maybe_begin_member_template_processing (decl
);
21514 /* Parse the default argument expressions. */
21515 cp_parser_late_parsing_default_args (parser
, decl
);
21516 /* Remove any template parameters from the symbol table. */
21517 maybe_end_member_template_processing ();
21519 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
21520 /* Now parse any NSDMIs. */
21521 save_ccp
= current_class_ptr
;
21522 save_ccr
= current_class_ref
;
21523 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
21525 if (class_type
!= DECL_CONTEXT (decl
))
21528 pop_scope (pushed_scope
);
21529 class_type
= DECL_CONTEXT (decl
);
21530 pushed_scope
= push_scope (class_type
);
21532 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
21533 cp_parser_late_parsing_nsdmi (parser
, decl
);
21535 vec_safe_truncate (unparsed_nsdmis
, 0);
21536 current_class_ptr
= save_ccp
;
21537 current_class_ref
= save_ccr
;
21539 pop_scope (pushed_scope
);
21541 /* Now do some post-NSDMI bookkeeping. */
21542 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
21543 after_nsdmi_defaulted_late_checks (class_type
);
21544 vec_safe_truncate (unparsed_classes
, 0);
21545 after_nsdmi_defaulted_late_checks (type
);
21547 /* Now parse the body of the functions. */
21550 /* OpenMP UDRs need to be parsed before all other functions. */
21551 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21552 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
21553 cp_parser_late_parsing_for_member (parser
, decl
);
21554 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21555 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
21556 cp_parser_late_parsing_for_member (parser
, decl
);
21559 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21560 cp_parser_late_parsing_for_member (parser
, decl
);
21561 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
21564 vec_safe_push (unparsed_classes
, type
);
21566 /* Put back any saved access checks. */
21567 pop_deferring_access_checks ();
21569 /* Restore saved state. */
21570 parser
->in_switch_statement_p
= in_switch_statement_p
;
21571 parser
->in_statement
= in_statement
;
21572 parser
->in_function_body
= saved_in_function_body
;
21573 parser
->num_template_parameter_lists
21574 = saved_num_template_parameter_lists
;
21575 parser
->in_unbraced_linkage_specification_p
21576 = saved_in_unbraced_linkage_specification_p
;
21582 cp_parser_class_specifier (cp_parser
* parser
)
21585 timevar_push (TV_PARSE_STRUCT
);
21586 ret
= cp_parser_class_specifier_1 (parser
);
21587 timevar_pop (TV_PARSE_STRUCT
);
21591 /* Parse a class-head.
21594 class-key identifier [opt] base-clause [opt]
21595 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21596 class-key nested-name-specifier [opt] template-id
21599 class-virt-specifier:
21603 class-key attributes identifier [opt] base-clause [opt]
21604 class-key attributes nested-name-specifier identifier base-clause [opt]
21605 class-key attributes nested-name-specifier [opt] template-id
21608 Upon return BASES is initialized to the list of base classes (or
21609 NULL, if there are none) in the same form returned by
21610 cp_parser_base_clause.
21612 Returns the TYPE of the indicated class. Sets
21613 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21614 involving a nested-name-specifier was used, and FALSE otherwise.
21616 Returns error_mark_node if this is not a class-head.
21618 Returns NULL_TREE if the class-head is syntactically valid, but
21619 semantically invalid in a way that means we should skip the entire
21620 body of the class. */
21623 cp_parser_class_head (cp_parser
* parser
,
21624 bool* nested_name_specifier_p
)
21626 tree nested_name_specifier
;
21627 enum tag_types class_key
;
21628 tree id
= NULL_TREE
;
21629 tree type
= NULL_TREE
;
21632 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21633 bool template_id_p
= false;
21634 bool qualified_p
= false;
21635 bool invalid_nested_name_p
= false;
21636 bool invalid_explicit_specialization_p
= false;
21637 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
21638 tree pushed_scope
= NULL_TREE
;
21639 unsigned num_templates
;
21640 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
21641 /* Assume no nested-name-specifier will be present. */
21642 *nested_name_specifier_p
= false;
21643 /* Assume no template parameter lists will be used in defining the
21646 parser
->colon_corrects_to_scope_p
= false;
21648 /* Look for the class-key. */
21649 class_key
= cp_parser_class_key (parser
);
21650 if (class_key
== none_type
)
21651 return error_mark_node
;
21653 location_t class_head_start_location
= input_location
;
21655 /* Parse the attributes. */
21656 attributes
= cp_parser_attributes_opt (parser
);
21658 /* If the next token is `::', that is invalid -- but sometimes
21659 people do try to write:
21663 Handle this gracefully by accepting the extra qualifier, and then
21664 issuing an error about it later if this really is a
21665 class-head. If it turns out just to be an elaborated type
21666 specifier, remain silent. */
21667 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
21668 qualified_p
= true;
21670 push_deferring_access_checks (dk_no_check
);
21672 /* Determine the name of the class. Begin by looking for an
21673 optional nested-name-specifier. */
21674 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
21675 nested_name_specifier
21676 = cp_parser_nested_name_specifier_opt (parser
,
21677 /*typename_keyword_p=*/false,
21678 /*check_dependency_p=*/false,
21680 /*is_declaration=*/false);
21681 /* If there was a nested-name-specifier, then there *must* be an
21683 if (nested_name_specifier
)
21685 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21686 /* Although the grammar says `identifier', it really means
21687 `class-name' or `template-name'. You are only allowed to
21688 define a class that has already been declared with this
21691 The proposed resolution for Core Issue 180 says that wherever
21692 you see `class T::X' you should treat `X' as a type-name.
21694 It is OK to define an inaccessible class; for example:
21696 class A { class B; };
21699 We do not know if we will see a class-name, or a
21700 template-name. We look for a class-name first, in case the
21701 class-name is a template-id; if we looked for the
21702 template-name first we would stop after the template-name. */
21703 cp_parser_parse_tentatively (parser
);
21704 type
= cp_parser_class_name (parser
,
21705 /*typename_keyword_p=*/false,
21706 /*template_keyword_p=*/false,
21708 /*check_dependency_p=*/false,
21709 /*class_head_p=*/true,
21710 /*is_declaration=*/false);
21711 /* If that didn't work, ignore the nested-name-specifier. */
21712 if (!cp_parser_parse_definitely (parser
))
21714 invalid_nested_name_p
= true;
21715 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21716 id
= cp_parser_identifier (parser
);
21717 if (id
== error_mark_node
)
21720 /* If we could not find a corresponding TYPE, treat this
21721 declaration like an unqualified declaration. */
21722 if (type
== error_mark_node
)
21723 nested_name_specifier
= NULL_TREE
;
21724 /* Otherwise, count the number of templates used in TYPE and its
21725 containing scopes. */
21730 for (scope
= TREE_TYPE (type
);
21731 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
21732 scope
= get_containing_scope (scope
))
21734 && CLASS_TYPE_P (scope
)
21735 && CLASSTYPE_TEMPLATE_INFO (scope
)
21736 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
21737 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
21738 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
21742 /* Otherwise, the identifier is optional. */
21745 /* We don't know whether what comes next is a template-id,
21746 an identifier, or nothing at all. */
21747 cp_parser_parse_tentatively (parser
);
21748 /* Check for a template-id. */
21749 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21750 id
= cp_parser_template_id (parser
,
21751 /*template_keyword_p=*/false,
21752 /*check_dependency_p=*/true,
21754 /*is_declaration=*/true);
21755 /* If that didn't work, it could still be an identifier. */
21756 if (!cp_parser_parse_definitely (parser
))
21758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21760 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21761 id
= cp_parser_identifier (parser
);
21768 template_id_p
= true;
21773 pop_deferring_access_checks ();
21777 cp_parser_check_for_invalid_template_id (parser
, id
,
21779 type_start_token
->location
);
21781 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21783 /* If it's not a `:' or a `{' then we can't really be looking at a
21784 class-head, since a class-head only appears as part of a
21785 class-specifier. We have to detect this situation before calling
21786 xref_tag, since that has irreversible side-effects. */
21787 if (!cp_parser_next_token_starts_class_definition_p (parser
))
21789 cp_parser_error (parser
, "expected %<{%> or %<:%>");
21790 type
= error_mark_node
;
21794 /* At this point, we're going ahead with the class-specifier, even
21795 if some other problem occurs. */
21796 cp_parser_commit_to_tentative_parse (parser
);
21797 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
21799 cp_parser_error (parser
,
21800 "cannot specify %<override%> for a class");
21801 type
= error_mark_node
;
21804 /* Issue the error about the overly-qualified name now. */
21807 cp_parser_error (parser
,
21808 "global qualification of class name is invalid");
21809 type
= error_mark_node
;
21812 else if (invalid_nested_name_p
)
21814 cp_parser_error (parser
,
21815 "qualified name does not name a class");
21816 type
= error_mark_node
;
21819 else if (nested_name_specifier
)
21823 /* Reject typedef-names in class heads. */
21824 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
21826 error_at (type_start_token
->location
,
21827 "invalid class name in declaration of %qD",
21833 /* Figure out in what scope the declaration is being placed. */
21834 scope
= current_scope ();
21835 /* If that scope does not contain the scope in which the
21836 class was originally declared, the program is invalid. */
21837 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
21839 if (at_namespace_scope_p ())
21840 error_at (type_start_token
->location
,
21841 "declaration of %qD in namespace %qD which does not "
21843 type
, scope
, nested_name_specifier
);
21845 error_at (type_start_token
->location
,
21846 "declaration of %qD in %qD which does not enclose %qD",
21847 type
, scope
, nested_name_specifier
);
21853 A declarator-id shall not be qualified except for the
21854 definition of a ... nested class outside of its class
21855 ... [or] the definition or explicit instantiation of a
21856 class member of a namespace outside of its namespace. */
21857 if (scope
== nested_name_specifier
)
21859 permerror (nested_name_specifier_token_start
->location
,
21860 "extra qualification not allowed");
21861 nested_name_specifier
= NULL_TREE
;
21865 /* An explicit-specialization must be preceded by "template <>". If
21866 it is not, try to recover gracefully. */
21867 if (at_namespace_scope_p ()
21868 && parser
->num_template_parameter_lists
== 0
21871 /* Build a location of this form:
21872 struct typename <ARGS>
21873 ^~~~~~~~~~~~~~~~~~~~~~
21874 with caret==start at the start token, and
21875 finishing at the end of the type. */
21876 location_t reported_loc
21877 = make_location (class_head_start_location
,
21878 class_head_start_location
,
21879 get_finish (type_start_token
->location
));
21880 rich_location
richloc (line_table
, reported_loc
);
21881 richloc
.add_fixit_insert (class_head_start_location
, "template <> ");
21884 "an explicit specialization must be preceded by %<template <>%>");
21885 invalid_explicit_specialization_p
= true;
21886 /* Take the same action that would have been taken by
21887 cp_parser_explicit_specialization. */
21888 ++parser
->num_template_parameter_lists
;
21889 begin_specialization ();
21891 /* There must be no "return" statements between this point and the
21892 end of this function; set "type "to the correct return value and
21893 use "goto done;" to return. */
21894 /* Make sure that the right number of template parameters were
21896 if (!cp_parser_check_template_parameters (parser
, num_templates
,
21897 type_start_token
->location
,
21898 /*declarator=*/NULL
))
21900 /* If something went wrong, there is no point in even trying to
21901 process the class-definition. */
21906 /* Look up the type. */
21909 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
21910 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
21911 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
21913 error_at (type_start_token
->location
,
21914 "function template %qD redeclared as a class template", id
);
21915 type
= error_mark_node
;
21919 type
= TREE_TYPE (id
);
21920 type
= maybe_process_partial_specialization (type
);
21922 if (nested_name_specifier
)
21923 pushed_scope
= push_scope (nested_name_specifier
);
21925 else if (nested_name_specifier
)
21931 template <typename T> struct S { struct T };
21932 template <typename T> struct S<T>::T { };
21934 we will get a TYPENAME_TYPE when processing the definition of
21935 `S::T'. We need to resolve it to the actual type before we
21936 try to define it. */
21937 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
21939 class_type
= resolve_typename_type (TREE_TYPE (type
),
21940 /*only_current_p=*/false);
21941 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
21942 type
= TYPE_NAME (class_type
);
21945 cp_parser_error (parser
, "could not resolve typename type");
21946 type
= error_mark_node
;
21950 if (maybe_process_partial_specialization (TREE_TYPE (type
))
21951 == error_mark_node
)
21957 class_type
= current_class_type
;
21958 /* Enter the scope indicated by the nested-name-specifier. */
21959 pushed_scope
= push_scope (nested_name_specifier
);
21960 /* Get the canonical version of this type. */
21961 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
21962 /* Call push_template_decl if it seems like we should be defining a
21963 template either from the template headers or the type we're
21964 defining, so that we diagnose both extra and missing headers. */
21965 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21966 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
21967 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
21969 type
= push_template_decl (type
);
21970 if (type
== error_mark_node
)
21977 type
= TREE_TYPE (type
);
21978 *nested_name_specifier_p
= true;
21980 else /* The name is not a nested name. */
21982 /* If the class was unnamed, create a dummy name. */
21984 id
= make_anon_name ();
21985 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
21986 parser
->num_template_parameter_lists
);
21989 /* Indicate whether this class was declared as a `class' or as a
21991 if (TREE_CODE (type
) == RECORD_TYPE
)
21992 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
21993 cp_parser_check_class_key (class_key
, type
);
21995 /* If this type was already complete, and we see another definition,
21996 that's an error. */
21997 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
21999 error_at (type_start_token
->location
, "redefinition of %q#T",
22001 error_at (type_start_token
->location
, "previous definition of %q+#T",
22006 else if (type
== error_mark_node
)
22011 /* Apply attributes now, before any use of the class as a template
22012 argument in its base list. */
22013 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
22014 fixup_attribute_variants (type
);
22017 /* We will have entered the scope containing the class; the names of
22018 base classes should be looked up in that context. For example:
22020 struct A { struct B {}; struct C; };
22021 struct A::C : B {};
22025 /* Get the list of base-classes, if there is one. */
22026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
22028 /* PR59482: enter the class scope so that base-specifiers are looked
22032 bases
= cp_parser_base_clause (parser
);
22033 /* PR59482: get out of the previously pushed class scope so that the
22034 subsequent pops pop the right thing. */
22041 /* If we're really defining a class, process the base classes.
22042 If they're invalid, fail. */
22043 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
22044 && !xref_basetypes (type
, bases
))
22048 /* Leave the scope given by the nested-name-specifier. We will
22049 enter the class scope itself while processing the members. */
22051 pop_scope (pushed_scope
);
22053 if (invalid_explicit_specialization_p
)
22055 end_specialization ();
22056 --parser
->num_template_parameter_lists
;
22060 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
22061 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
22062 CLASSTYPE_FINAL (type
) = 1;
22064 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22068 /* Parse a class-key.
22075 Returns the kind of class-key specified, or none_type to indicate
22078 static enum tag_types
22079 cp_parser_class_key (cp_parser
* parser
)
22082 enum tag_types tag_type
;
22084 /* Look for the class-key. */
22085 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
22089 /* Check to see if the TOKEN is a class-key. */
22090 tag_type
= cp_parser_token_is_class_key (token
);
22092 cp_parser_error (parser
, "expected class-key");
22096 /* Parse a type-parameter-key.
22098 type-parameter-key:
22104 cp_parser_type_parameter_key (cp_parser
* parser
)
22106 /* Look for the type-parameter-key. */
22107 enum tag_types tag_type
= none_type
;
22108 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22109 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
22111 cp_lexer_consume_token (parser
->lexer
);
22112 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
22113 /* typename is not allowed in a template template parameter
22114 by the standard until C++1Z. */
22115 pedwarn (token
->location
, OPT_Wpedantic
,
22116 "ISO C++ forbids typename key in template template parameter;"
22117 " use -std=c++1z or -std=gnu++1z");
22120 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
22125 /* Parse an (optional) member-specification.
22127 member-specification:
22128 member-declaration member-specification [opt]
22129 access-specifier : member-specification [opt] */
22132 cp_parser_member_specification_opt (cp_parser
* parser
)
22139 /* Peek at the next token. */
22140 token
= cp_lexer_peek_token (parser
->lexer
);
22141 /* If it's a `}', or EOF then we've seen all the members. */
22142 if (token
->type
== CPP_CLOSE_BRACE
22143 || token
->type
== CPP_EOF
22144 || token
->type
== CPP_PRAGMA_EOL
)
22147 /* See if this token is a keyword. */
22148 keyword
= token
->keyword
;
22152 case RID_PROTECTED
:
22154 /* Consume the access-specifier. */
22155 cp_lexer_consume_token (parser
->lexer
);
22156 /* Remember which access-specifier is active. */
22157 current_access_specifier
= token
->u
.value
;
22158 /* Look for the `:'. */
22159 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22163 /* Accept #pragmas at class scope. */
22164 if (token
->type
== CPP_PRAGMA
)
22166 cp_parser_pragma (parser
, pragma_member
, NULL
);
22170 /* Otherwise, the next construction must be a
22171 member-declaration. */
22172 cp_parser_member_declaration (parser
);
22177 /* Parse a member-declaration.
22179 member-declaration:
22180 decl-specifier-seq [opt] member-declarator-list [opt] ;
22181 function-definition ; [opt]
22182 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22184 template-declaration
22187 member-declarator-list:
22189 member-declarator-list , member-declarator
22192 declarator pure-specifier [opt]
22193 declarator constant-initializer [opt]
22194 identifier [opt] : constant-expression
22198 member-declaration:
22199 __extension__ member-declaration
22202 declarator attributes [opt] pure-specifier [opt]
22203 declarator attributes [opt] constant-initializer [opt]
22204 identifier [opt] attributes [opt] : constant-expression
22208 member-declaration:
22209 static_assert-declaration */
22212 cp_parser_member_declaration (cp_parser
* parser
)
22214 cp_decl_specifier_seq decl_specifiers
;
22215 tree prefix_attributes
;
22217 int declares_class_or_enum
;
22219 cp_token
*token
= NULL
;
22220 cp_token
*decl_spec_token_start
= NULL
;
22221 cp_token
*initializer_token_start
= NULL
;
22222 int saved_pedantic
;
22223 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
22225 /* Check for the `__extension__' keyword. */
22226 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
22229 cp_parser_member_declaration (parser
);
22230 /* Restore the old value of the PEDANTIC flag. */
22231 pedantic
= saved_pedantic
;
22236 /* Check for a template-declaration. */
22237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
22239 /* An explicit specialization here is an error condition, and we
22240 expect the specialization handler to detect and report this. */
22241 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
22242 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
22243 cp_parser_explicit_specialization (parser
);
22245 cp_parser_template_declaration (parser
, /*member_p=*/true);
22249 /* Check for a template introduction. */
22250 else if (cp_parser_template_declaration_after_export (parser
, true))
22253 /* Check for a using-declaration. */
22254 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
22256 if (cxx_dialect
< cxx11
)
22258 /* Parse the using-declaration. */
22259 cp_parser_using_declaration (parser
,
22260 /*access_declaration_p=*/false);
22266 bool alias_decl_expected
;
22267 cp_parser_parse_tentatively (parser
);
22268 decl
= cp_parser_alias_declaration (parser
);
22269 /* Note that if we actually see the '=' token after the
22270 identifier, cp_parser_alias_declaration commits the
22271 tentative parse. In that case, we really expect an
22272 alias-declaration. Otherwise, we expect a using
22274 alias_decl_expected
=
22275 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
22276 cp_parser_parse_definitely (parser
);
22278 if (alias_decl_expected
)
22279 finish_member_declaration (decl
);
22281 cp_parser_using_declaration (parser
,
22282 /*access_declaration_p=*/false);
22287 /* Check for @defs. */
22288 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
22291 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
22292 ivar
= ivar_chains
;
22296 ivar
= TREE_CHAIN (member
);
22297 TREE_CHAIN (member
) = NULL_TREE
;
22298 finish_member_declaration (member
);
22303 /* If the next token is `static_assert' we have a static assertion. */
22304 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
22306 cp_parser_static_assert (parser
, /*member_p=*/true);
22310 parser
->colon_corrects_to_scope_p
= false;
22312 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
22315 /* Parse the decl-specifier-seq. */
22316 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22317 cp_parser_decl_specifier_seq (parser
,
22318 CP_PARSER_FLAGS_OPTIONAL
,
22320 &declares_class_or_enum
);
22321 /* Check for an invalid type-name. */
22322 if (!decl_specifiers
.any_type_specifiers_p
22323 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22325 /* If there is no declarator, then the decl-specifier-seq should
22327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22329 /* If there was no decl-specifier-seq, and the next token is a
22330 `;', then we have something like:
22336 Each member-declaration shall declare at least one member
22337 name of the class. */
22338 if (!decl_specifiers
.any_specifiers_p
)
22340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22341 if (!in_system_header_at (token
->location
))
22342 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
22348 /* See if this declaration is a friend. */
22349 friend_p
= cp_parser_friend_p (&decl_specifiers
);
22350 /* If there were decl-specifiers, check to see if there was
22351 a class-declaration. */
22352 type
= check_tag_decl (&decl_specifiers
,
22353 /*explicit_type_instantiation_p=*/false);
22354 /* Nested classes have already been added to the class, but
22355 a `friend' needs to be explicitly registered. */
22358 /* If the `friend' keyword was present, the friend must
22359 be introduced with a class-key. */
22360 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
22361 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
22362 "in C++03 a class-key must be used "
22363 "when declaring a friend");
22366 template <typename T> struct A {
22367 friend struct A<T>::B;
22370 A<T>::B will be represented by a TYPENAME_TYPE, and
22371 therefore not recognized by check_tag_decl. */
22374 type
= decl_specifiers
.type
;
22375 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
22376 type
= TREE_TYPE (type
);
22378 if (!type
|| !TYPE_P (type
))
22379 error_at (decl_spec_token_start
->location
,
22380 "friend declaration does not name a class or "
22383 make_friend_class (current_class_type
, type
,
22384 /*complain=*/true);
22386 /* If there is no TYPE, an error message will already have
22388 else if (!type
|| type
== error_mark_node
)
22390 /* An anonymous aggregate has to be handled specially; such
22391 a declaration really declares a data member (with a
22392 particular type), as opposed to a nested class. */
22393 else if (ANON_AGGR_TYPE_P (type
))
22396 if (decl_specifiers
.storage_class
!= sc_none
)
22397 error_at (decl_spec_token_start
->location
,
22398 "a storage class on an anonymous aggregate "
22399 "in class scope is not allowed");
22401 /* Remove constructors and such from TYPE, now that we
22402 know it is an anonymous aggregate. */
22403 fixup_anonymous_aggr (type
);
22404 /* And make the corresponding data member. */
22405 decl
= build_decl (decl_spec_token_start
->location
,
22406 FIELD_DECL
, NULL_TREE
, type
);
22407 /* Add it to the class. */
22408 finish_member_declaration (decl
);
22411 cp_parser_check_access_in_redeclaration
22413 decl_spec_token_start
->location
);
22418 bool assume_semicolon
= false;
22420 /* Clear attributes from the decl_specifiers but keep them
22421 around as prefix attributes that apply them to the entity
22423 prefix_attributes
= decl_specifiers
.attributes
;
22424 decl_specifiers
.attributes
= NULL_TREE
;
22426 /* See if these declarations will be friends. */
22427 friend_p
= cp_parser_friend_p (&decl_specifiers
);
22429 /* Keep going until we hit the `;' at the end of the
22431 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
22433 tree attributes
= NULL_TREE
;
22434 tree first_attribute
;
22436 /* Peek at the next token. */
22437 token
= cp_lexer_peek_token (parser
->lexer
);
22439 /* Check for a bitfield declaration. */
22440 if (token
->type
== CPP_COLON
22441 || (token
->type
== CPP_NAME
22442 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22448 /* Get the name of the bitfield. Note that we cannot just
22449 check TOKEN here because it may have been invalidated by
22450 the call to cp_lexer_peek_nth_token above. */
22451 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
22452 identifier
= cp_parser_identifier (parser
);
22454 identifier
= NULL_TREE
;
22456 /* Consume the `:' token. */
22457 cp_lexer_consume_token (parser
->lexer
);
22458 /* Get the width of the bitfield. */
22460 = cp_parser_constant_expression (parser
);
22462 /* Look for attributes that apply to the bitfield. */
22463 attributes
= cp_parser_attributes_opt (parser
);
22464 /* Remember which attributes are prefix attributes and
22466 first_attribute
= attributes
;
22467 /* Combine the attributes. */
22468 attributes
= chainon (prefix_attributes
, attributes
);
22470 /* Create the bitfield declaration. */
22471 decl
= grokbitfield (identifier
22472 ? make_id_declarator (NULL_TREE
,
22482 cp_declarator
*declarator
;
22484 tree asm_specification
;
22485 int ctor_dtor_or_conv_p
;
22487 /* Parse the declarator. */
22489 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
22490 &ctor_dtor_or_conv_p
,
22491 /*parenthesized_p=*/NULL
,
22495 /* If something went wrong parsing the declarator, make sure
22496 that we at least consume some tokens. */
22497 if (declarator
== cp_error_declarator
)
22499 /* Skip to the end of the statement. */
22500 cp_parser_skip_to_end_of_statement (parser
);
22501 /* If the next token is not a semicolon, that is
22502 probably because we just skipped over the body of
22503 a function. So, we consume a semicolon if
22504 present, but do not issue an error message if it
22506 if (cp_lexer_next_token_is (parser
->lexer
,
22508 cp_lexer_consume_token (parser
->lexer
);
22512 if (declares_class_or_enum
& 2)
22513 cp_parser_check_for_definition_in_return_type
22514 (declarator
, decl_specifiers
.type
,
22515 decl_specifiers
.locations
[ds_type_spec
]);
22517 /* Look for an asm-specification. */
22518 asm_specification
= cp_parser_asm_specification_opt (parser
);
22519 /* Look for attributes that apply to the declaration. */
22520 attributes
= cp_parser_attributes_opt (parser
);
22521 /* Remember which attributes are prefix attributes and
22523 first_attribute
= attributes
;
22524 /* Combine the attributes. */
22525 attributes
= chainon (prefix_attributes
, attributes
);
22527 /* If it's an `=', then we have a constant-initializer or a
22528 pure-specifier. It is not correct to parse the
22529 initializer before registering the member declaration
22530 since the member declaration should be in scope while
22531 its initializer is processed. However, the rest of the
22532 front end does not yet provide an interface that allows
22533 us to handle this correctly. */
22534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22538 A pure-specifier shall be used only in the declaration of
22539 a virtual function.
22541 A member-declarator can contain a constant-initializer
22542 only if it declares a static member of integral or
22545 Therefore, if the DECLARATOR is for a function, we look
22546 for a pure-specifier; otherwise, we look for a
22547 constant-initializer. When we call `grokfield', it will
22548 perform more stringent semantics checks. */
22549 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
22550 if (function_declarator_p (declarator
)
22551 || (decl_specifiers
.type
22552 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
22553 && declarator
->kind
== cdk_id
22554 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
22555 == FUNCTION_TYPE
)))
22556 initializer
= cp_parser_pure_specifier (parser
);
22557 else if (decl_specifiers
.storage_class
!= sc_static
)
22558 initializer
= cp_parser_save_nsdmi (parser
);
22559 else if (cxx_dialect
>= cxx11
)
22562 /* Don't require a constant rvalue in C++11, since we
22563 might want a reference constant. We'll enforce
22564 constancy later. */
22565 cp_lexer_consume_token (parser
->lexer
);
22566 /* Parse the initializer. */
22567 initializer
= cp_parser_initializer_clause (parser
,
22571 /* Parse the initializer. */
22572 initializer
= cp_parser_constant_initializer (parser
);
22574 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
22575 && !function_declarator_p (declarator
))
22578 if (decl_specifiers
.storage_class
!= sc_static
)
22579 initializer
= cp_parser_save_nsdmi (parser
);
22581 initializer
= cp_parser_initializer (parser
, &x
, &x
);
22583 /* Otherwise, there is no initializer. */
22585 initializer
= NULL_TREE
;
22587 /* See if we are probably looking at a function
22588 definition. We are certainly not looking at a
22589 member-declarator. Calling `grokfield' has
22590 side-effects, so we must not do it unless we are sure
22591 that we are looking at a member-declarator. */
22592 if (cp_parser_token_starts_function_definition_p
22593 (cp_lexer_peek_token (parser
->lexer
)))
22595 /* The grammar does not allow a pure-specifier to be
22596 used when a member function is defined. (It is
22597 possible that this fact is an oversight in the
22598 standard, since a pure function may be defined
22599 outside of the class-specifier. */
22600 if (initializer
&& initializer_token_start
)
22601 error_at (initializer_token_start
->location
,
22602 "pure-specifier on function-definition");
22603 decl
= cp_parser_save_member_function_body (parser
,
22607 if (parser
->fully_implicit_function_template_p
)
22608 decl
= finish_fully_implicit_template (parser
, decl
);
22609 /* If the member was not a friend, declare it here. */
22611 finish_member_declaration (decl
);
22612 /* Peek at the next token. */
22613 token
= cp_lexer_peek_token (parser
->lexer
);
22614 /* If the next token is a semicolon, consume it. */
22615 if (token
->type
== CPP_SEMICOLON
)
22616 cp_lexer_consume_token (parser
->lexer
);
22620 if (declarator
->kind
== cdk_function
)
22621 declarator
->id_loc
= token
->location
;
22622 /* Create the declaration. */
22623 decl
= grokfield (declarator
, &decl_specifiers
,
22624 initializer
, /*init_const_expr_p=*/true,
22625 asm_specification
, attributes
);
22626 if (parser
->fully_implicit_function_template_p
)
22629 finish_fully_implicit_template (parser
, 0);
22631 decl
= finish_fully_implicit_template (parser
, decl
);
22635 cp_finalize_omp_declare_simd (parser
, decl
);
22636 cp_finalize_oacc_routine (parser
, decl
, false);
22638 /* Reset PREFIX_ATTRIBUTES. */
22639 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22640 attributes
= TREE_CHAIN (attributes
);
22642 TREE_CHAIN (attributes
) = NULL_TREE
;
22644 /* If there is any qualification still in effect, clear it
22645 now; we will be starting fresh with the next declarator. */
22646 parser
->scope
= NULL_TREE
;
22647 parser
->qualifying_scope
= NULL_TREE
;
22648 parser
->object_scope
= NULL_TREE
;
22649 /* If it's a `,', then there are more declarators. */
22650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22652 cp_lexer_consume_token (parser
->lexer
);
22653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22655 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22656 error_at (token
->location
,
22657 "stray %<,%> at end of member declaration");
22660 /* If the next token isn't a `;', then we have a parse error. */
22661 else if (cp_lexer_next_token_is_not (parser
->lexer
,
22664 /* The next token might be a ways away from where the
22665 actual semicolon is missing. Find the previous token
22666 and use that for our error position. */
22667 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22668 error_at (token
->location
,
22669 "expected %<;%> at end of member declaration");
22671 /* Assume that the user meant to provide a semicolon. If
22672 we were to cp_parser_skip_to_end_of_statement, we might
22673 skip to a semicolon inside a member function definition
22674 and issue nonsensical error messages. */
22675 assume_semicolon
= true;
22680 /* Add DECL to the list of members. */
22682 /* Explicitly include, eg, NSDMIs, for better error
22683 recovery (c++/58650). */
22684 || !DECL_DECLARES_FUNCTION_P (decl
))
22685 finish_member_declaration (decl
);
22687 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22688 cp_parser_save_default_args (parser
, decl
);
22689 else if (TREE_CODE (decl
) == FIELD_DECL
22690 && !DECL_C_BIT_FIELD (decl
)
22691 && DECL_INITIAL (decl
))
22692 /* Add DECL to the queue of NSDMI to be parsed later. */
22693 vec_safe_push (unparsed_nsdmis
, decl
);
22696 if (assume_semicolon
)
22701 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22703 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22706 /* Parse a pure-specifier.
22711 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22712 Otherwise, ERROR_MARK_NODE is returned. */
22715 cp_parser_pure_specifier (cp_parser
* parser
)
22719 /* Look for the `=' token. */
22720 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22721 return error_mark_node
;
22722 /* Look for the `0' token. */
22723 token
= cp_lexer_peek_token (parser
->lexer
);
22725 if (token
->type
== CPP_EOF
22726 || token
->type
== CPP_PRAGMA_EOL
)
22727 return error_mark_node
;
22729 cp_lexer_consume_token (parser
->lexer
);
22731 /* Accept = default or = delete in c++0x mode. */
22732 if (token
->keyword
== RID_DEFAULT
22733 || token
->keyword
== RID_DELETE
)
22735 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
22736 return token
->u
.value
;
22739 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22740 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
22742 cp_parser_error (parser
,
22743 "invalid pure specifier (only %<= 0%> is allowed)");
22744 cp_parser_skip_to_end_of_statement (parser
);
22745 return error_mark_node
;
22747 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22749 error_at (token
->location
, "templates may not be %<virtual%>");
22750 return error_mark_node
;
22753 return integer_zero_node
;
22756 /* Parse a constant-initializer.
22758 constant-initializer:
22759 = constant-expression
22761 Returns a representation of the constant-expression. */
22764 cp_parser_constant_initializer (cp_parser
* parser
)
22766 /* Look for the `=' token. */
22767 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22768 return error_mark_node
;
22770 /* It is invalid to write:
22772 struct S { static const int i = { 7 }; };
22775 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22777 cp_parser_error (parser
,
22778 "a brace-enclosed initializer is not allowed here");
22779 /* Consume the opening brace. */
22780 cp_lexer_consume_token (parser
->lexer
);
22781 /* Skip the initializer. */
22782 cp_parser_skip_to_closing_brace (parser
);
22783 /* Look for the trailing `}'. */
22784 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22786 return error_mark_node
;
22789 return cp_parser_constant_expression (parser
);
22792 /* Derived classes [gram.class.derived] */
22794 /* Parse a base-clause.
22797 : base-specifier-list
22799 base-specifier-list:
22800 base-specifier ... [opt]
22801 base-specifier-list , base-specifier ... [opt]
22803 Returns a TREE_LIST representing the base-classes, in the order in
22804 which they were declared. The representation of each node is as
22805 described by cp_parser_base_specifier.
22807 In the case that no bases are specified, this function will return
22808 NULL_TREE, not ERROR_MARK_NODE. */
22811 cp_parser_base_clause (cp_parser
* parser
)
22813 tree bases
= NULL_TREE
;
22815 /* Look for the `:' that begins the list. */
22816 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22818 /* Scan the base-specifier-list. */
22823 bool pack_expansion_p
= false;
22825 /* Look for the base-specifier. */
22826 base
= cp_parser_base_specifier (parser
);
22827 /* Look for the (optional) ellipsis. */
22828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22830 /* Consume the `...'. */
22831 cp_lexer_consume_token (parser
->lexer
);
22833 pack_expansion_p
= true;
22836 /* Add BASE to the front of the list. */
22837 if (base
&& base
!= error_mark_node
)
22839 if (pack_expansion_p
)
22840 /* Make this a pack expansion type. */
22841 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
22843 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
22845 TREE_CHAIN (base
) = bases
;
22849 /* Peek at the next token. */
22850 token
= cp_lexer_peek_token (parser
->lexer
);
22851 /* If it's not a comma, then the list is complete. */
22852 if (token
->type
!= CPP_COMMA
)
22854 /* Consume the `,'. */
22855 cp_lexer_consume_token (parser
->lexer
);
22858 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22859 base class had a qualified name. However, the next name that
22860 appears is certainly not qualified. */
22861 parser
->scope
= NULL_TREE
;
22862 parser
->qualifying_scope
= NULL_TREE
;
22863 parser
->object_scope
= NULL_TREE
;
22865 return nreverse (bases
);
22868 /* Parse a base-specifier.
22871 :: [opt] nested-name-specifier [opt] class-name
22872 virtual access-specifier [opt] :: [opt] nested-name-specifier
22874 access-specifier virtual [opt] :: [opt] nested-name-specifier
22877 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22878 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22879 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22880 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22883 cp_parser_base_specifier (cp_parser
* parser
)
22887 bool virtual_p
= false;
22888 bool duplicate_virtual_error_issued_p
= false;
22889 bool duplicate_access_error_issued_p
= false;
22890 bool class_scope_p
, template_p
;
22891 tree access
= access_default_node
;
22894 /* Process the optional `virtual' and `access-specifier'. */
22897 /* Peek at the next token. */
22898 token
= cp_lexer_peek_token (parser
->lexer
);
22899 /* Process `virtual'. */
22900 switch (token
->keyword
)
22903 /* If `virtual' appears more than once, issue an error. */
22904 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
22906 cp_parser_error (parser
,
22907 "%<virtual%> specified more than once in base-specified");
22908 duplicate_virtual_error_issued_p
= true;
22913 /* Consume the `virtual' token. */
22914 cp_lexer_consume_token (parser
->lexer
);
22919 case RID_PROTECTED
:
22921 /* If more than one access specifier appears, issue an
22923 if (access
!= access_default_node
22924 && !duplicate_access_error_issued_p
)
22926 cp_parser_error (parser
,
22927 "more than one access specifier in base-specified");
22928 duplicate_access_error_issued_p
= true;
22931 access
= ridpointers
[(int) token
->keyword
];
22933 /* Consume the access-specifier. */
22934 cp_lexer_consume_token (parser
->lexer
);
22943 /* It is not uncommon to see programs mechanically, erroneously, use
22944 the 'typename' keyword to denote (dependent) qualified types
22945 as base classes. */
22946 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
22948 token
= cp_lexer_peek_token (parser
->lexer
);
22949 if (!processing_template_decl
)
22950 error_at (token
->location
,
22951 "keyword %<typename%> not allowed outside of templates");
22953 error_at (token
->location
,
22954 "keyword %<typename%> not allowed in this context "
22955 "(the base class is implicitly a type)");
22956 cp_lexer_consume_token (parser
->lexer
);
22959 /* Look for the optional `::' operator. */
22960 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
22961 /* Look for the nested-name-specifier. The simplest way to
22966 The keyword `typename' is not permitted in a base-specifier or
22967 mem-initializer; in these contexts a qualified name that
22968 depends on a template-parameter is implicitly assumed to be a
22971 is to pretend that we have seen the `typename' keyword at this
22973 cp_parser_nested_name_specifier_opt (parser
,
22974 /*typename_keyword_p=*/true,
22975 /*check_dependency_p=*/true,
22977 /*is_declaration=*/true);
22978 /* If the base class is given by a qualified name, assume that names
22979 we see are type names or templates, as appropriate. */
22980 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
22981 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
22984 && cp_lexer_next_token_is_decltype (parser
->lexer
))
22985 /* DR 950 allows decltype as a base-specifier. */
22986 type
= cp_parser_decltype (parser
);
22989 /* Otherwise, look for the class-name. */
22990 type
= cp_parser_class_name (parser
,
22994 /*check_dependency_p=*/true,
22995 /*class_head_p=*/false,
22996 /*is_declaration=*/true);
22997 type
= TREE_TYPE (type
);
23000 if (type
== error_mark_node
)
23001 return error_mark_node
;
23003 return finish_base_specifier (type
, access
, virtual_p
);
23006 /* Exception handling [gram.exception] */
23008 /* Parse an (optional) noexcept-specification.
23010 noexcept-specification:
23011 noexcept ( constant-expression ) [opt]
23013 If no noexcept-specification is present, returns NULL_TREE.
23014 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23015 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23016 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23017 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23018 in which case a boolean condition is returned instead. */
23021 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
23022 bool require_constexpr
,
23023 bool* consumed_expr
,
23027 const char *saved_message
;
23029 /* Peek at the next token. */
23030 token
= cp_lexer_peek_token (parser
->lexer
);
23032 /* Is it a noexcept-specification? */
23033 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
23036 cp_lexer_consume_token (parser
->lexer
);
23038 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
23040 cp_lexer_consume_token (parser
->lexer
);
23042 if (require_constexpr
)
23044 /* Types may not be defined in an exception-specification. */
23045 saved_message
= parser
->type_definition_forbidden_message
;
23046 parser
->type_definition_forbidden_message
23047 = G_("types may not be defined in an exception-specification");
23049 expr
= cp_parser_constant_expression (parser
);
23051 /* Restore the saved message. */
23052 parser
->type_definition_forbidden_message
= saved_message
;
23056 expr
= cp_parser_expression (parser
);
23057 *consumed_expr
= true;
23060 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23064 expr
= boolean_true_node
;
23065 if (!require_constexpr
)
23066 *consumed_expr
= false;
23069 /* We cannot build a noexcept-spec right away because this will check
23070 that expr is a constexpr. */
23072 return build_noexcept_spec (expr
, tf_warning_or_error
);
23080 /* Parse an (optional) exception-specification.
23082 exception-specification:
23083 throw ( type-id-list [opt] )
23085 Returns a TREE_LIST representing the exception-specification. The
23086 TREE_VALUE of each node is a type. */
23089 cp_parser_exception_specification_opt (cp_parser
* parser
)
23093 const char *saved_message
;
23095 /* Peek at the next token. */
23096 token
= cp_lexer_peek_token (parser
->lexer
);
23098 /* Is it a noexcept-specification? */
23099 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
23101 if (type_id_list
!= NULL_TREE
)
23102 return type_id_list
;
23104 /* If it's not `throw', then there's no exception-specification. */
23105 if (!cp_parser_is_keyword (token
, RID_THROW
))
23109 /* Enable this once a lot of code has transitioned to noexcept? */
23110 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
23111 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
23112 "deprecated in C++0x; use %<noexcept%> instead");
23115 /* Consume the `throw'. */
23116 cp_lexer_consume_token (parser
->lexer
);
23118 /* Look for the `('. */
23119 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23121 /* Peek at the next token. */
23122 token
= cp_lexer_peek_token (parser
->lexer
);
23123 /* If it's not a `)', then there is a type-id-list. */
23124 if (token
->type
!= CPP_CLOSE_PAREN
)
23126 /* Types may not be defined in an exception-specification. */
23127 saved_message
= parser
->type_definition_forbidden_message
;
23128 parser
->type_definition_forbidden_message
23129 = G_("types may not be defined in an exception-specification");
23130 /* Parse the type-id-list. */
23131 type_id_list
= cp_parser_type_id_list (parser
);
23132 /* Restore the saved message. */
23133 parser
->type_definition_forbidden_message
= saved_message
;
23136 type_id_list
= empty_except_spec
;
23138 /* Look for the `)'. */
23139 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23141 return type_id_list
;
23144 /* Parse an (optional) type-id-list.
23148 type-id-list , type-id ... [opt]
23150 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23151 in the order that the types were presented. */
23154 cp_parser_type_id_list (cp_parser
* parser
)
23156 tree types
= NULL_TREE
;
23163 token
= cp_lexer_peek_token (parser
->lexer
);
23165 /* Get the next type-id. */
23166 type
= cp_parser_type_id (parser
);
23167 /* Check for invalid 'auto'. */
23168 if (flag_concepts
&& type_uses_auto (type
))
23170 error_at (token
->location
,
23171 "invalid use of %<auto%> in exception-specification");
23172 type
= error_mark_node
;
23174 /* Parse the optional ellipsis. */
23175 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23177 /* Consume the `...'. */
23178 cp_lexer_consume_token (parser
->lexer
);
23180 /* Turn the type into a pack expansion expression. */
23181 type
= make_pack_expansion (type
);
23183 /* Add it to the list. */
23184 types
= add_exception_specifier (types
, type
, /*complain=*/1);
23185 /* Peek at the next token. */
23186 token
= cp_lexer_peek_token (parser
->lexer
);
23187 /* If it is not a `,', we are done. */
23188 if (token
->type
!= CPP_COMMA
)
23190 /* Consume the `,'. */
23191 cp_lexer_consume_token (parser
->lexer
);
23194 return nreverse (types
);
23197 /* Parse a try-block.
23200 try compound-statement handler-seq */
23203 cp_parser_try_block (cp_parser
* parser
)
23207 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
23208 if (parser
->in_function_body
23209 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
23210 error ("%<try%> in %<constexpr%> function");
23212 try_block
= begin_try_block ();
23213 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
23214 finish_try_block (try_block
);
23215 cp_parser_handler_seq (parser
);
23216 finish_handler_sequence (try_block
);
23221 /* Parse a function-try-block.
23223 function-try-block:
23224 try ctor-initializer [opt] function-body handler-seq */
23227 cp_parser_function_try_block (cp_parser
* parser
)
23229 tree compound_stmt
;
23231 bool ctor_initializer_p
;
23233 /* Look for the `try' keyword. */
23234 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
23236 /* Let the rest of the front end know where we are. */
23237 try_block
= begin_function_try_block (&compound_stmt
);
23238 /* Parse the function-body. */
23239 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23240 (parser
, /*in_function_try_block=*/true);
23241 /* We're done with the `try' part. */
23242 finish_function_try_block (try_block
);
23243 /* Parse the handlers. */
23244 cp_parser_handler_seq (parser
);
23245 /* We're done with the handlers. */
23246 finish_function_handler_sequence (try_block
, compound_stmt
);
23248 return ctor_initializer_p
;
23251 /* Parse a handler-seq.
23254 handler handler-seq [opt] */
23257 cp_parser_handler_seq (cp_parser
* parser
)
23263 /* Parse the handler. */
23264 cp_parser_handler (parser
);
23265 /* Peek at the next token. */
23266 token
= cp_lexer_peek_token (parser
->lexer
);
23267 /* If it's not `catch' then there are no more handlers. */
23268 if (!cp_parser_is_keyword (token
, RID_CATCH
))
23273 /* Parse a handler.
23276 catch ( exception-declaration ) compound-statement */
23279 cp_parser_handler (cp_parser
* parser
)
23284 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
23285 handler
= begin_handler ();
23286 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23287 declaration
= cp_parser_exception_declaration (parser
);
23288 finish_handler_parms (declaration
, handler
);
23289 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23290 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
23291 finish_handler (handler
);
23294 /* Parse an exception-declaration.
23296 exception-declaration:
23297 type-specifier-seq declarator
23298 type-specifier-seq abstract-declarator
23302 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23303 ellipsis variant is used. */
23306 cp_parser_exception_declaration (cp_parser
* parser
)
23308 cp_decl_specifier_seq type_specifiers
;
23309 cp_declarator
*declarator
;
23310 const char *saved_message
;
23312 /* If it's an ellipsis, it's easy to handle. */
23313 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23315 /* Consume the `...' token. */
23316 cp_lexer_consume_token (parser
->lexer
);
23320 /* Types may not be defined in exception-declarations. */
23321 saved_message
= parser
->type_definition_forbidden_message
;
23322 parser
->type_definition_forbidden_message
23323 = G_("types may not be defined in exception-declarations");
23325 /* Parse the type-specifier-seq. */
23326 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
23327 /*is_trailing_return=*/false,
23329 /* If it's a `)', then there is no declarator. */
23330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
23333 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
23334 /*ctor_dtor_or_conv_p=*/NULL
,
23335 /*parenthesized_p=*/NULL
,
23336 /*member_p=*/false,
23337 /*friend_p=*/false);
23339 /* Restore the saved message. */
23340 parser
->type_definition_forbidden_message
= saved_message
;
23342 if (!type_specifiers
.any_specifiers_p
)
23343 return error_mark_node
;
23345 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
23348 /* Parse a throw-expression.
23351 throw assignment-expression [opt]
23353 Returns a THROW_EXPR representing the throw-expression. */
23356 cp_parser_throw_expression (cp_parser
* parser
)
23361 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
23362 token
= cp_lexer_peek_token (parser
->lexer
);
23363 /* Figure out whether or not there is an assignment-expression
23364 following the "throw" keyword. */
23365 if (token
->type
== CPP_COMMA
23366 || token
->type
== CPP_SEMICOLON
23367 || token
->type
== CPP_CLOSE_PAREN
23368 || token
->type
== CPP_CLOSE_SQUARE
23369 || token
->type
== CPP_CLOSE_BRACE
23370 || token
->type
== CPP_COLON
)
23371 expression
= NULL_TREE
;
23373 expression
= cp_parser_assignment_expression (parser
);
23375 return build_throw (expression
);
23378 /* GNU Extensions */
23380 /* Parse an (optional) asm-specification.
23383 asm ( string-literal )
23385 If the asm-specification is present, returns a STRING_CST
23386 corresponding to the string-literal. Otherwise, returns
23390 cp_parser_asm_specification_opt (cp_parser
* parser
)
23393 tree asm_specification
;
23395 /* Peek at the next token. */
23396 token
= cp_lexer_peek_token (parser
->lexer
);
23397 /* If the next token isn't the `asm' keyword, then there's no
23398 asm-specification. */
23399 if (!cp_parser_is_keyword (token
, RID_ASM
))
23402 /* Consume the `asm' token. */
23403 cp_lexer_consume_token (parser
->lexer
);
23404 /* Look for the `('. */
23405 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23407 /* Look for the string-literal. */
23408 asm_specification
= cp_parser_string_literal (parser
, false, false);
23410 /* Look for the `)'. */
23411 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23413 return asm_specification
;
23416 /* Parse an asm-operand-list.
23420 asm-operand-list , asm-operand
23423 string-literal ( expression )
23424 [ string-literal ] string-literal ( expression )
23426 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23427 each node is the expression. The TREE_PURPOSE is itself a
23428 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23429 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23430 is a STRING_CST for the string literal before the parenthesis. Returns
23431 ERROR_MARK_NODE if any of the operands are invalid. */
23434 cp_parser_asm_operand_list (cp_parser
* parser
)
23436 tree asm_operands
= NULL_TREE
;
23437 bool invalid_operands
= false;
23441 tree string_literal
;
23445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23447 /* Consume the `[' token. */
23448 cp_lexer_consume_token (parser
->lexer
);
23449 /* Read the operand name. */
23450 name
= cp_parser_identifier (parser
);
23451 if (name
!= error_mark_node
)
23452 name
= build_string (IDENTIFIER_LENGTH (name
),
23453 IDENTIFIER_POINTER (name
));
23454 /* Look for the closing `]'. */
23455 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23459 /* Look for the string-literal. */
23460 string_literal
= cp_parser_string_literal (parser
, false, false);
23462 /* Look for the `('. */
23463 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23464 /* Parse the expression. */
23465 expression
= cp_parser_expression (parser
);
23466 /* Look for the `)'. */
23467 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
23469 if (name
== error_mark_node
23470 || string_literal
== error_mark_node
23471 || expression
== error_mark_node
)
23472 invalid_operands
= true;
23474 /* Add this operand to the list. */
23475 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
23478 /* If the next token is not a `,', there are no more
23480 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23482 /* Consume the `,'. */
23483 cp_lexer_consume_token (parser
->lexer
);
23486 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
23489 /* Parse an asm-clobber-list.
23493 asm-clobber-list , string-literal
23495 Returns a TREE_LIST, indicating the clobbers in the order that they
23496 appeared. The TREE_VALUE of each node is a STRING_CST. */
23499 cp_parser_asm_clobber_list (cp_parser
* parser
)
23501 tree clobbers
= NULL_TREE
;
23505 tree string_literal
;
23507 /* Look for the string literal. */
23508 string_literal
= cp_parser_string_literal (parser
, false, false);
23509 /* Add it to the list. */
23510 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
23511 /* If the next token is not a `,', then the list is
23513 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23515 /* Consume the `,' token. */
23516 cp_lexer_consume_token (parser
->lexer
);
23522 /* Parse an asm-label-list.
23526 asm-label-list , identifier
23528 Returns a TREE_LIST, indicating the labels in the order that they
23529 appeared. The TREE_VALUE of each node is a label. */
23532 cp_parser_asm_label_list (cp_parser
* parser
)
23534 tree labels
= NULL_TREE
;
23538 tree identifier
, label
, name
;
23540 /* Look for the identifier. */
23541 identifier
= cp_parser_identifier (parser
);
23542 if (!error_operand_p (identifier
))
23544 label
= lookup_label (identifier
);
23545 if (TREE_CODE (label
) == LABEL_DECL
)
23547 TREE_USED (label
) = 1;
23548 check_goto (label
);
23549 name
= build_string (IDENTIFIER_LENGTH (identifier
),
23550 IDENTIFIER_POINTER (identifier
));
23551 labels
= tree_cons (name
, label
, labels
);
23554 /* If the next token is not a `,', then the list is
23556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23558 /* Consume the `,' token. */
23559 cp_lexer_consume_token (parser
->lexer
);
23562 return nreverse (labels
);
23565 /* Return TRUE iff the next tokens in the stream are possibly the
23566 beginning of a GNU extension attribute. */
23569 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
23571 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
23574 /* Return TRUE iff the next tokens in the stream are possibly the
23575 beginning of a standard C++-11 attribute specifier. */
23578 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
23580 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
23583 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23584 beginning of a standard C++-11 attribute specifier. */
23587 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
23589 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23591 return (cxx_dialect
>= cxx11
23592 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
23593 || (token
->type
== CPP_OPEN_SQUARE
23594 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
23595 && token
->type
== CPP_OPEN_SQUARE
)));
23598 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23599 beginning of a GNU extension attribute. */
23602 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
23604 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23606 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
23609 /* Return true iff the next tokens can be the beginning of either a
23610 GNU attribute list, or a standard C++11 attribute sequence. */
23613 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
23615 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
23616 || cp_next_tokens_can_be_std_attribute_p (parser
));
23619 /* Return true iff the next Nth tokens can be the beginning of either
23620 a GNU attribute list, or a standard C++11 attribute sequence. */
23623 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
23625 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
23626 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
23629 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23630 of GNU attributes, or return NULL. */
23633 cp_parser_attributes_opt (cp_parser
*parser
)
23635 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
23636 return cp_parser_gnu_attributes_opt (parser
);
23637 return cp_parser_std_attribute_spec_seq (parser
);
23640 #define CILK_SIMD_FN_CLAUSE_MASK \
23641 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23642 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23643 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23644 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23645 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23647 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23648 vector [(<clauses>)] */
23651 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
23653 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
23654 cp_token
*token
= v_token
;
23657 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
23658 parser
->cilk_simd_fn_info
->error_seen
= false;
23659 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
23660 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
23662 int paren_scope
= 0;
23663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23665 cp_lexer_consume_token (parser
->lexer
);
23666 v_token
= cp_lexer_peek_token (parser
->lexer
);
23669 while (paren_scope
> 0)
23671 token
= cp_lexer_peek_token (parser
->lexer
);
23672 if (token
->type
== CPP_OPEN_PAREN
)
23674 else if (token
->type
== CPP_CLOSE_PAREN
)
23676 /* Do not push the last ')' */
23677 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
23678 cp_lexer_consume_token (parser
->lexer
);
23681 token
->type
= CPP_PRAGMA_EOL
;
23682 parser
->lexer
->next_token
= token
;
23683 cp_lexer_consume_token (parser
->lexer
);
23685 struct cp_token_cache
*cp
23686 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
23687 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
23690 /* Parse an (optional) series of attributes.
23693 attributes attribute
23696 __attribute__ (( attribute-list [opt] ))
23698 The return value is as for cp_parser_gnu_attribute_list. */
23701 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
23703 tree attributes
= NULL_TREE
;
23708 tree attribute_list
;
23711 /* Peek at the next token. */
23712 token
= cp_lexer_peek_token (parser
->lexer
);
23713 /* If it's not `__attribute__', then we're done. */
23714 if (token
->keyword
!= RID_ATTRIBUTE
)
23717 /* Consume the `__attribute__' keyword. */
23718 cp_lexer_consume_token (parser
->lexer
);
23719 /* Look for the two `(' tokens. */
23720 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23721 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23723 /* Peek at the next token. */
23724 token
= cp_lexer_peek_token (parser
->lexer
);
23725 if (token
->type
!= CPP_CLOSE_PAREN
)
23726 /* Parse the attribute-list. */
23727 attribute_list
= cp_parser_gnu_attribute_list (parser
);
23729 /* If the next token is a `)', then there is no attribute
23731 attribute_list
= NULL
;
23733 /* Look for the two `)' tokens. */
23734 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23736 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23739 cp_parser_skip_to_end_of_statement (parser
);
23741 /* Add these new attributes to the list. */
23742 attributes
= chainon (attributes
, attribute_list
);
23748 /* Parse a GNU attribute-list.
23752 attribute-list , attribute
23756 identifier ( identifier )
23757 identifier ( identifier , expression-list )
23758 identifier ( expression-list )
23760 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23761 to an attribute. The TREE_PURPOSE of each node is the identifier
23762 indicating which attribute is in use. The TREE_VALUE represents
23763 the arguments, if any. */
23766 cp_parser_gnu_attribute_list (cp_parser
* parser
)
23768 tree attribute_list
= NULL_TREE
;
23769 bool save_translate_strings_p
= parser
->translate_strings_p
;
23771 parser
->translate_strings_p
= false;
23778 /* Look for the identifier. We also allow keywords here; for
23779 example `__attribute__ ((const))' is legal. */
23780 token
= cp_lexer_peek_token (parser
->lexer
);
23781 if (token
->type
== CPP_NAME
23782 || token
->type
== CPP_KEYWORD
)
23784 tree arguments
= NULL_TREE
;
23786 /* Consume the token, but save it since we need it for the
23787 SIMD enabled function parsing. */
23788 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
23790 /* Save away the identifier that indicates which attribute
23792 identifier
= (token
->type
== CPP_KEYWORD
)
23793 /* For keywords, use the canonical spelling, not the
23794 parsed identifier. */
23795 ? ridpointers
[(int) token
->keyword
]
23796 : id_token
->u
.value
;
23798 attribute
= build_tree_list (identifier
, NULL_TREE
);
23800 /* Peek at the next token. */
23801 token
= cp_lexer_peek_token (parser
->lexer
);
23802 /* If it's an `(', then parse the attribute arguments. */
23803 if (token
->type
== CPP_OPEN_PAREN
)
23805 vec
<tree
, va_gc
> *vec
;
23806 int attr_flag
= (attribute_takes_identifier_p (identifier
)
23807 ? id_attr
: normal_attr
);
23808 if (is_cilkplus_vector_p (identifier
))
23810 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23814 vec
= cp_parser_parenthesized_expression_list
23815 (parser
, attr_flag
, /*cast_p=*/false,
23816 /*allow_expansion_p=*/false,
23817 /*non_constant_p=*/NULL
);
23819 arguments
= error_mark_node
;
23822 arguments
= build_tree_list_vec (vec
);
23823 release_tree_vector (vec
);
23825 /* Save the arguments away. */
23826 TREE_VALUE (attribute
) = arguments
;
23828 else if (is_cilkplus_vector_p (identifier
))
23830 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23834 if (arguments
!= error_mark_node
)
23836 /* Add this attribute to the list. */
23837 TREE_CHAIN (attribute
) = attribute_list
;
23838 attribute_list
= attribute
;
23841 token
= cp_lexer_peek_token (parser
->lexer
);
23843 /* Now, look for more attributes. If the next token isn't a
23844 `,', we're done. */
23845 if (token
->type
!= CPP_COMMA
)
23848 /* Consume the comma and keep going. */
23849 cp_lexer_consume_token (parser
->lexer
);
23851 parser
->translate_strings_p
= save_translate_strings_p
;
23853 /* We built up the list in reverse order. */
23854 return nreverse (attribute_list
);
23857 /* Parse a standard C++11 attribute.
23859 The returned representation is a TREE_LIST which TREE_PURPOSE is
23860 the scoped name of the attribute, and the TREE_VALUE is its
23863 Note that the scoped name of the attribute is itself a TREE_LIST
23864 which TREE_PURPOSE is the namespace of the attribute, and
23865 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23866 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23867 and which TREE_PURPOSE is directly the attribute name.
23869 Clients of the attribute code should use get_attribute_namespace
23870 and get_attribute_name to get the actual namespace and name of
23871 attributes, regardless of their being GNU or C++11 attributes.
23874 attribute-token attribute-argument-clause [opt]
23878 attribute-scoped-token
23880 attribute-scoped-token:
23881 attribute-namespace :: identifier
23883 attribute-namespace:
23886 attribute-argument-clause:
23887 ( balanced-token-seq )
23889 balanced-token-seq:
23890 balanced-token [opt]
23891 balanced-token-seq balanced-token
23894 ( balanced-token-seq )
23895 [ balanced-token-seq ]
23896 { balanced-token-seq }. */
23899 cp_parser_std_attribute (cp_parser
*parser
)
23901 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
23904 /* First, parse name of the attribute, a.k.a attribute-token. */
23906 token
= cp_lexer_peek_token (parser
->lexer
);
23907 if (token
->type
== CPP_NAME
)
23908 attr_id
= token
->u
.value
;
23909 else if (token
->type
== CPP_KEYWORD
)
23910 attr_id
= ridpointers
[(int) token
->keyword
];
23911 else if (token
->flags
& NAMED_OP
)
23912 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
23914 if (attr_id
== NULL_TREE
)
23917 cp_lexer_consume_token (parser
->lexer
);
23919 token
= cp_lexer_peek_token (parser
->lexer
);
23920 if (token
->type
== CPP_SCOPE
)
23922 /* We are seeing a scoped attribute token. */
23924 cp_lexer_consume_token (parser
->lexer
);
23927 token
= cp_lexer_consume_token (parser
->lexer
);
23928 if (token
->type
== CPP_NAME
)
23929 attr_id
= token
->u
.value
;
23930 else if (token
->type
== CPP_KEYWORD
)
23931 attr_id
= ridpointers
[(int) token
->keyword
];
23934 error_at (token
->location
,
23935 "expected an identifier for the attribute name");
23936 return error_mark_node
;
23938 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
23940 token
= cp_lexer_peek_token (parser
->lexer
);
23944 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
23946 /* C++11 noreturn attribute is equivalent to GNU's. */
23947 if (is_attribute_p ("noreturn", attr_id
))
23948 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23949 /* C++14 deprecated attribute is equivalent to GNU's. */
23950 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
23952 if (cxx_dialect
== cxx11
)
23953 pedwarn (token
->location
, OPT_Wpedantic
,
23954 "%<deprecated%> is a C++14 feature;"
23955 " use %<gnu::deprecated%>");
23956 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23958 /* Transactional Memory TS optimize_for_synchronized attribute is
23959 equivalent to GNU transaction_callable. */
23960 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
23961 TREE_PURPOSE (attribute
)
23962 = get_identifier ("transaction_callable");
23963 /* Transactional Memory attributes are GNU attributes. */
23964 else if (tm_attr_to_mask (attr_id
))
23965 TREE_PURPOSE (attribute
) = attr_id
;
23968 /* Now parse the optional argument clause of the attribute. */
23970 if (token
->type
!= CPP_OPEN_PAREN
)
23974 vec
<tree
, va_gc
> *vec
;
23975 int attr_flag
= normal_attr
;
23977 if (attr_ns
== get_identifier ("gnu")
23978 && attribute_takes_identifier_p (attr_id
))
23979 /* A GNU attribute that takes an identifier in parameter. */
23980 attr_flag
= id_attr
;
23982 vec
= cp_parser_parenthesized_expression_list
23983 (parser
, attr_flag
, /*cast_p=*/false,
23984 /*allow_expansion_p=*/true,
23985 /*non_constant_p=*/NULL
);
23987 arguments
= error_mark_node
;
23990 arguments
= build_tree_list_vec (vec
);
23991 release_tree_vector (vec
);
23994 if (arguments
== error_mark_node
)
23995 attribute
= error_mark_node
;
23997 TREE_VALUE (attribute
) = arguments
;
24003 /* Check that the attribute ATTRIBUTE appears at most once in the
24004 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24005 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24006 isn't implemented yet in GCC. */
24009 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
24013 tree name
= get_attribute_name (attribute
);
24014 if (is_attribute_p ("noreturn", name
)
24015 && lookup_attribute ("noreturn", attributes
))
24016 error ("attribute noreturn can appear at most once "
24017 "in an attribute-list");
24018 else if (is_attribute_p ("deprecated", name
)
24019 && lookup_attribute ("deprecated", attributes
))
24020 error ("attribute deprecated can appear at most once "
24021 "in an attribute-list");
24025 /* Parse a list of standard C++-11 attributes.
24029 attribute-list , attribute[opt]
24031 attribute-list , attribute ...
24035 cp_parser_std_attribute_list (cp_parser
*parser
)
24037 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
24038 cp_token
*token
= NULL
;
24042 attribute
= cp_parser_std_attribute (parser
);
24043 if (attribute
== error_mark_node
)
24045 if (attribute
!= NULL_TREE
)
24047 cp_parser_check_std_attribute (attributes
, attribute
);
24048 TREE_CHAIN (attribute
) = attributes
;
24049 attributes
= attribute
;
24051 token
= cp_lexer_peek_token (parser
->lexer
);
24052 if (token
->type
== CPP_ELLIPSIS
)
24054 cp_lexer_consume_token (parser
->lexer
);
24055 TREE_VALUE (attribute
)
24056 = make_pack_expansion (TREE_VALUE (attribute
));
24057 token
= cp_lexer_peek_token (parser
->lexer
);
24059 if (token
->type
!= CPP_COMMA
)
24061 cp_lexer_consume_token (parser
->lexer
);
24063 attributes
= nreverse (attributes
);
24067 /* Parse a standard C++-11 attribute specifier.
24069 attribute-specifier:
24070 [ [ attribute-list ] ]
24071 alignment-specifier
24073 alignment-specifier:
24074 alignas ( type-id ... [opt] )
24075 alignas ( alignment-expression ... [opt] ). */
24078 cp_parser_std_attribute_spec (cp_parser
*parser
)
24080 tree attributes
= NULL_TREE
;
24081 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24083 if (token
->type
== CPP_OPEN_SQUARE
24084 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
24086 cp_lexer_consume_token (parser
->lexer
);
24087 cp_lexer_consume_token (parser
->lexer
);
24089 attributes
= cp_parser_std_attribute_list (parser
);
24091 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
24092 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
24093 cp_parser_skip_to_end_of_statement (parser
);
24095 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24096 when we are sure that we have actually parsed them. */
24097 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24103 /* Look for an alignment-specifier. */
24105 token
= cp_lexer_peek_token (parser
->lexer
);
24107 if (token
->type
!= CPP_KEYWORD
24108 || token
->keyword
!= RID_ALIGNAS
)
24111 cp_lexer_consume_token (parser
->lexer
);
24112 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
24114 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
24116 cp_parser_error (parser
, "expected %<(%>");
24117 return error_mark_node
;
24120 cp_parser_parse_tentatively (parser
);
24121 alignas_expr
= cp_parser_type_id (parser
);
24123 if (!cp_parser_parse_definitely (parser
))
24125 gcc_assert (alignas_expr
== error_mark_node
24126 || alignas_expr
== NULL_TREE
);
24129 cp_parser_assignment_expression (parser
);
24130 if (alignas_expr
== error_mark_node
)
24131 cp_parser_skip_to_end_of_statement (parser
);
24132 if (alignas_expr
== NULL_TREE
24133 || alignas_expr
== error_mark_node
)
24134 return alignas_expr
;
24137 alignas_expr
= cxx_alignas_expr (alignas_expr
);
24138 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
24140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24142 cp_lexer_consume_token (parser
->lexer
);
24143 alignas_expr
= make_pack_expansion (alignas_expr
);
24146 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
24148 cp_parser_error (parser
, "expected %<)%>");
24149 return error_mark_node
;
24152 /* Build the C++-11 representation of an 'aligned'
24155 build_tree_list (build_tree_list (get_identifier ("gnu"),
24156 get_identifier ("aligned")),
24163 /* Parse a standard C++-11 attribute-specifier-seq.
24165 attribute-specifier-seq:
24166 attribute-specifier-seq [opt] attribute-specifier
24170 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
24172 tree attr_specs
= NULL_TREE
;
24173 tree attr_last
= NULL_TREE
;
24177 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
24178 if (attr_spec
== NULL_TREE
)
24180 if (attr_spec
== error_mark_node
)
24181 return error_mark_node
;
24184 TREE_CHAIN (attr_last
) = attr_spec
;
24186 attr_specs
= attr_last
= attr_spec
;
24187 attr_last
= tree_last (attr_last
);
24193 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24194 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24195 current value of the PEDANTIC flag, regardless of whether or not
24196 the `__extension__' keyword is present. The caller is responsible
24197 for restoring the value of the PEDANTIC flag. */
24200 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
24202 /* Save the old value of the PEDANTIC flag. */
24203 *saved_pedantic
= pedantic
;
24205 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
24207 /* Consume the `__extension__' token. */
24208 cp_lexer_consume_token (parser
->lexer
);
24209 /* We're not being pedantic while the `__extension__' keyword is
24219 /* Parse a label declaration.
24222 __label__ label-declarator-seq ;
24224 label-declarator-seq:
24225 identifier , label-declarator-seq
24229 cp_parser_label_declaration (cp_parser
* parser
)
24231 /* Look for the `__label__' keyword. */
24232 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
24238 /* Look for an identifier. */
24239 identifier
= cp_parser_identifier (parser
);
24240 /* If we failed, stop. */
24241 if (identifier
== error_mark_node
)
24243 /* Declare it as a label. */
24244 finish_label_decl (identifier
);
24245 /* If the next token is a `;', stop. */
24246 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24248 /* Look for the `,' separating the label declarations. */
24249 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
24252 /* Look for the final `;'. */
24253 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
24256 // -------------------------------------------------------------------------- //
24259 // Parse a requires clause.
24261 // requires-clause:
24262 // 'requires' logical-or-expression
24264 // The required logical-or-expression must be a constant expression. Note
24265 // that we don't check that the expression is constepxr here. We defer until
24266 // we analyze constraints and then, we only check atomic constraints.
24268 cp_parser_requires_clause (cp_parser
*parser
)
24270 // Parse the requires clause so that it is not automatically folded.
24271 ++processing_template_decl
;
24272 tree expr
= cp_parser_binary_expression (parser
, false, false,
24273 PREC_NOT_OPERATOR
, NULL
);
24274 if (check_for_bare_parameter_packs (expr
))
24275 expr
= error_mark_node
;
24276 --processing_template_decl
;
24280 // Optionally parse a requires clause:
24282 cp_parser_requires_clause_opt (cp_parser
*parser
)
24284 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
24285 if (tok
->keyword
!= RID_REQUIRES
)
24287 if (!flag_concepts
&& tok
->type
== CPP_NAME
24288 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
24290 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
24291 "%<requires%> only available with -fconcepts");
24292 /* Parse and discard the requires-clause. */
24293 cp_lexer_consume_token (parser
->lexer
);
24294 cp_parser_requires_clause (parser
);
24298 cp_lexer_consume_token (parser
->lexer
);
24299 return cp_parser_requires_clause (parser
);
24303 /*---------------------------------------------------------------------------
24304 Requires expressions
24305 ---------------------------------------------------------------------------*/
24307 /* Parse a requires expression
24309 requirement-expression:
24310 'requires' requirement-parameter-list [opt] requirement-body */
24312 cp_parser_requires_expression (cp_parser
*parser
)
24314 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
24315 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
24317 /* A requires-expression shall appear only within a concept
24318 definition or a requires-clause.
24320 TODO: Implement this diagnostic correctly. */
24321 if (!processing_template_decl
)
24323 error_at (loc
, "a requires expression cannot appear outside a template");
24324 cp_parser_skip_to_end_of_statement (parser
);
24325 return error_mark_node
;
24330 /* Local parameters are delared as variables within the scope
24331 of the expression. They are not visible past the end of
24332 the expression. Expressions within the requires-expression
24333 are unevaluated. */
24334 struct scope_sentinel
24338 ++cp_unevaluated_operand
;
24339 begin_scope (sk_block
, NULL_TREE
);
24344 pop_bindings_and_leave_scope ();
24345 --cp_unevaluated_operand
;
24349 /* Parse the optional parameter list. */
24350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24352 parms
= cp_parser_requirement_parameter_list (parser
);
24353 if (parms
== error_mark_node
)
24354 return error_mark_node
;
24359 /* Parse the requirement body. */
24360 reqs
= cp_parser_requirement_body (parser
);
24361 if (reqs
== error_mark_node
)
24362 return error_mark_node
;
24365 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24367 grokparms (parms
, &parms
);
24368 return finish_requires_expr (parms
, reqs
);
24371 /* Parse a parameterized requirement.
24373 requirement-parameter-list:
24374 '(' parameter-declaration-clause ')' */
24376 cp_parser_requirement_parameter_list (cp_parser
*parser
)
24378 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
24379 return error_mark_node
;
24381 tree parms
= cp_parser_parameter_declaration_clause (parser
);
24383 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
24384 return error_mark_node
;
24389 /* Parse the body of a requirement.
24392 '{' requirement-list '}' */
24394 cp_parser_requirement_body (cp_parser
*parser
)
24396 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
24397 return error_mark_node
;
24399 tree reqs
= cp_parser_requirement_list (parser
);
24401 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
24402 return error_mark_node
;
24407 /* Parse a list of requirements.
24411 requirement-list ';' requirement[opt] */
24413 cp_parser_requirement_list (cp_parser
*parser
)
24415 tree result
= NULL_TREE
;
24418 tree req
= cp_parser_requirement (parser
);
24419 if (req
== error_mark_node
)
24420 return error_mark_node
;
24422 result
= tree_cons (NULL_TREE
, req
, result
);
24424 /* If we see a semi-colon, consume it. */
24425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24426 cp_lexer_consume_token (parser
->lexer
);
24428 /* Stop processing at the end of the list. */
24429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
24433 /* Reverse the order of requirements so they are analyzed in
24434 declaration order. */
24435 return nreverse (result
);
24438 /* Parse a syntactic requirement or type requirement.
24442 compound-requirement
24444 nested-requirement */
24446 cp_parser_requirement (cp_parser
*parser
)
24448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24449 return cp_parser_compound_requirement (parser
);
24450 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
24451 return cp_parser_type_requirement (parser
);
24452 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
24453 return cp_parser_nested_requirement (parser
);
24455 return cp_parser_simple_requirement (parser
);
24458 /* Parse a simple requirement.
24460 simple-requirement:
24463 cp_parser_simple_requirement (cp_parser
*parser
)
24465 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
24466 if (!expr
|| expr
== error_mark_node
)
24467 return error_mark_node
;
24469 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
24470 return error_mark_node
;
24472 return finish_simple_requirement (expr
);
24475 /* Parse a type requirement
24478 nested-name-specifier [opt] required-type-name ';'
24480 required-type-name:
24482 'template' [opt] simple-template-id */
24484 cp_parser_type_requirement (cp_parser
*parser
)
24486 cp_lexer_consume_token (parser
->lexer
);
24488 // Save the scope before parsing name specifiers.
24489 tree saved_scope
= parser
->scope
;
24490 tree saved_object_scope
= parser
->object_scope
;
24491 tree saved_qualifying_scope
= parser
->qualifying_scope
;
24492 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
24493 cp_parser_nested_name_specifier_opt (parser
,
24494 /*typename_keyword_p=*/true,
24495 /*check_dependency_p=*/false,
24497 /*is_declaration=*/false);
24500 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24502 cp_lexer_consume_token (parser
->lexer
);
24503 type
= cp_parser_template_id (parser
,
24504 /*template_keyword_p=*/true,
24505 /*check_dependency=*/false,
24506 /*tag_type=*/none_type
,
24507 /*is_declaration=*/false);
24508 type
= make_typename_type (parser
->scope
, type
, typename_type
,
24509 /*complain=*/tf_error
);
24512 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
24514 if (TREE_CODE (type
) == TYPE_DECL
)
24515 type
= TREE_TYPE (type
);
24517 parser
->scope
= saved_scope
;
24518 parser
->object_scope
= saved_object_scope
;
24519 parser
->qualifying_scope
= saved_qualifying_scope
;
24521 if (type
== error_mark_node
)
24522 cp_parser_skip_to_end_of_statement (parser
);
24524 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
24525 return error_mark_node
;
24526 if (type
== error_mark_node
)
24527 return error_mark_node
;
24529 return finish_type_requirement (type
);
24532 /* Parse a compound requirement
24534 compound-requirement:
24535 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24537 cp_parser_compound_requirement (cp_parser
*parser
)
24539 /* Parse an expression enclosed in '{ }'s. */
24540 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
24541 return error_mark_node
;
24543 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
24544 if (!expr
|| expr
== error_mark_node
)
24545 return error_mark_node
;
24547 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
24548 return error_mark_node
;
24550 /* Parse the optional noexcept. */
24551 bool noexcept_p
= false;
24552 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
24554 cp_lexer_consume_token (parser
->lexer
);
24558 /* Parse the optional trailing return type. */
24559 tree type
= NULL_TREE
;
24560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
24562 cp_lexer_consume_token (parser
->lexer
);
24563 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
24564 parser
->in_result_type_constraint_p
= true;
24565 type
= cp_parser_trailing_type_id (parser
);
24566 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
24567 if (type
== error_mark_node
)
24568 return error_mark_node
;
24571 return finish_compound_requirement (expr
, type
, noexcept_p
);
24574 /* Parse a nested requirement. This is the same as a requires clause.
24576 nested-requirement:
24579 cp_parser_nested_requirement (cp_parser
*parser
)
24581 cp_lexer_consume_token (parser
->lexer
);
24582 tree req
= cp_parser_requires_clause (parser
);
24583 if (req
== error_mark_node
)
24584 return error_mark_node
;
24585 return finish_nested_requirement (req
);
24588 /* Support Functions */
24590 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24591 NAME should have one of the representations used for an
24592 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24593 is returned. If PARSER->SCOPE is a dependent type, then a
24594 SCOPE_REF is returned.
24596 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24597 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24598 was formed. Abstractly, such entities should not be passed to this
24599 function, because they do not need to be looked up, but it is
24600 simpler to check for this special case here, rather than at the
24603 In cases not explicitly covered above, this function returns a
24604 DECL, OVERLOAD, or baselink representing the result of the lookup.
24605 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24608 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24609 (e.g., "struct") that was used. In that case bindings that do not
24610 refer to types are ignored.
24612 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24615 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24618 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24621 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24622 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24623 NULL_TREE otherwise. */
24626 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
24627 enum tag_types tag_type
,
24630 bool check_dependency
,
24631 tree
*ambiguous_decls
,
24632 location_t name_location
)
24635 tree object_type
= parser
->context
->object_type
;
24637 /* Assume that the lookup will be unambiguous. */
24638 if (ambiguous_decls
)
24639 *ambiguous_decls
= NULL_TREE
;
24641 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24642 no longer valid. Note that if we are parsing tentatively, and
24643 the parse fails, OBJECT_TYPE will be automatically restored. */
24644 parser
->context
->object_type
= NULL_TREE
;
24646 if (name
== error_mark_node
)
24647 return error_mark_node
;
24649 /* A template-id has already been resolved; there is no lookup to
24651 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
24653 if (BASELINK_P (name
))
24655 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
24656 == TEMPLATE_ID_EXPR
);
24660 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24661 it should already have been checked to make sure that the name
24662 used matches the type being destroyed. */
24663 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
24667 /* Figure out to which type this destructor applies. */
24669 type
= parser
->scope
;
24670 else if (object_type
)
24671 type
= object_type
;
24673 type
= current_class_type
;
24674 /* If that's not a class type, there is no destructor. */
24675 if (!type
|| !CLASS_TYPE_P (type
))
24676 return error_mark_node
;
24677 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
24678 lazily_declare_fn (sfk_destructor
, type
);
24679 if (!CLASSTYPE_DESTRUCTORS (type
))
24680 return error_mark_node
;
24681 /* If it was a class type, return the destructor. */
24682 return CLASSTYPE_DESTRUCTORS (type
);
24685 /* By this point, the NAME should be an ordinary identifier. If
24686 the id-expression was a qualified name, the qualifying scope is
24687 stored in PARSER->SCOPE at this point. */
24688 gcc_assert (identifier_p (name
));
24690 /* Perform the lookup. */
24695 if (parser
->scope
== error_mark_node
)
24696 return error_mark_node
;
24698 /* If the SCOPE is dependent, the lookup must be deferred until
24699 the template is instantiated -- unless we are explicitly
24700 looking up names in uninstantiated templates. Even then, we
24701 cannot look up the name if the scope is not a class type; it
24702 might, for example, be a template type parameter. */
24703 dependent_p
= (TYPE_P (parser
->scope
)
24704 && dependent_scope_p (parser
->scope
));
24705 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
24707 /* Defer lookup. */
24708 decl
= error_mark_node
;
24711 tree pushed_scope
= NULL_TREE
;
24713 /* If PARSER->SCOPE is a dependent type, then it must be a
24714 class type, and we must not be checking dependencies;
24715 otherwise, we would have processed this lookup above. So
24716 that PARSER->SCOPE is not considered a dependent base by
24717 lookup_member, we must enter the scope here. */
24719 pushed_scope
= push_scope (parser
->scope
);
24721 /* If the PARSER->SCOPE is a template specialization, it
24722 may be instantiated during name lookup. In that case,
24723 errors may be issued. Even if we rollback the current
24724 tentative parse, those errors are valid. */
24725 decl
= lookup_qualified_name (parser
->scope
, name
,
24726 tag_type
!= none_type
,
24727 /*complain=*/true);
24729 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24730 lookup result and the nested-name-specifier nominates a class C:
24731 * if the name specified after the nested-name-specifier, when
24732 looked up in C, is the injected-class-name of C (Clause 9), or
24733 * if the name specified after the nested-name-specifier is the
24734 same as the identifier or the simple-template-id's template-
24735 name in the last component of the nested-name-specifier,
24736 the name is instead considered to name the constructor of
24737 class C. [ Note: for example, the constructor is not an
24738 acceptable lookup result in an elaborated-type-specifier so
24739 the constructor would not be used in place of the
24740 injected-class-name. --end note ] Such a constructor name
24741 shall be used only in the declarator-id of a declaration that
24742 names a constructor or in a using-declaration. */
24743 if (tag_type
== none_type
24744 && DECL_SELF_REFERENCE_P (decl
)
24745 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
24746 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
24747 tag_type
!= none_type
,
24748 /*complain=*/true);
24750 /* If we have a single function from a using decl, pull it out. */
24751 if (TREE_CODE (decl
) == OVERLOAD
24752 && !really_overloaded_fn (decl
))
24753 decl
= OVL_FUNCTION (decl
);
24756 pop_scope (pushed_scope
);
24759 /* If the scope is a dependent type and either we deferred lookup or
24760 we did lookup but didn't find the name, rememeber the name. */
24761 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
24762 && dependent_type_p (parser
->scope
))
24768 /* The resolution to Core Issue 180 says that `struct
24769 A::B' should be considered a type-name, even if `A'
24771 type
= make_typename_type (parser
->scope
, name
, tag_type
,
24772 /*complain=*/tf_error
);
24773 if (type
!= error_mark_node
)
24774 decl
= TYPE_NAME (type
);
24776 else if (is_template
24777 && (cp_parser_next_token_ends_template_argument_p (parser
)
24778 || cp_lexer_next_token_is (parser
->lexer
,
24780 decl
= make_unbound_class_template (parser
->scope
,
24782 /*complain=*/tf_error
);
24784 decl
= build_qualified_name (/*type=*/NULL_TREE
,
24785 parser
->scope
, name
,
24788 parser
->qualifying_scope
= parser
->scope
;
24789 parser
->object_scope
= NULL_TREE
;
24791 else if (object_type
)
24793 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24794 OBJECT_TYPE is not a class. */
24795 if (CLASS_TYPE_P (object_type
))
24796 /* If the OBJECT_TYPE is a template specialization, it may
24797 be instantiated during name lookup. In that case, errors
24798 may be issued. Even if we rollback the current tentative
24799 parse, those errors are valid. */
24800 decl
= lookup_member (object_type
,
24803 tag_type
!= none_type
,
24804 tf_warning_or_error
);
24810 /* Look it up in the enclosing context. */
24811 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24813 /*block_p=*/true, is_namespace
, 0);
24814 /* DR 141 says when looking for a template-name after -> or ., only
24815 consider class templates. We need to fix our handling of
24816 dependent expressions to implement that properly, but for now
24817 let's ignore namespace-scope function templates. */
24818 if (decl
&& is_template
&& !DECL_TYPE_TEMPLATE_P (decl
))
24821 if (is_overloaded_fn (d
))
24822 d
= get_first_fn (d
);
24823 if (DECL_P (d
) && !DECL_CLASS_SCOPE_P (d
))
24827 if (object_type
== unknown_type_node
)
24828 /* The object is type-dependent, so we can't look anything up; we used
24829 this to get the DR 141 behavior. */
24830 object_type
= NULL_TREE
;
24831 parser
->object_scope
= object_type
;
24832 parser
->qualifying_scope
= NULL_TREE
;
24836 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24838 /*block_p=*/true, is_namespace
, 0);
24839 parser
->qualifying_scope
= NULL_TREE
;
24840 parser
->object_scope
= NULL_TREE
;
24843 /* If the lookup failed, let our caller know. */
24844 if (!decl
|| decl
== error_mark_node
)
24845 return error_mark_node
;
24847 /* Pull out the template from an injected-class-name (or multiple). */
24849 decl
= maybe_get_template_decl_from_type_decl (decl
);
24851 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24852 if (TREE_CODE (decl
) == TREE_LIST
)
24854 if (ambiguous_decls
)
24855 *ambiguous_decls
= decl
;
24856 /* The error message we have to print is too complicated for
24857 cp_parser_error, so we incorporate its actions directly. */
24858 if (!cp_parser_simulate_error (parser
))
24860 error_at (name_location
, "reference to %qD is ambiguous",
24862 print_candidates (decl
);
24864 return error_mark_node
;
24867 gcc_assert (DECL_P (decl
)
24868 || TREE_CODE (decl
) == OVERLOAD
24869 || TREE_CODE (decl
) == SCOPE_REF
24870 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
24871 || BASELINK_P (decl
));
24873 /* If we have resolved the name of a member declaration, check to
24874 see if the declaration is accessible. When the name resolves to
24875 set of overloaded functions, accessibility is checked when
24876 overload resolution is done.
24878 During an explicit instantiation, access is not checked at all,
24879 as per [temp.explicit]. */
24881 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
24883 maybe_record_typedef_use (decl
);
24885 return cp_expr (decl
, name_location
);
24888 /* Like cp_parser_lookup_name, but for use in the typical case where
24889 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24890 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24893 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
24895 return cp_parser_lookup_name (parser
, name
,
24897 /*is_template=*/false,
24898 /*is_namespace=*/false,
24899 /*check_dependency=*/true,
24900 /*ambiguous_decls=*/NULL
,
24904 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24905 the current context, return the TYPE_DECL. If TAG_NAME_P is
24906 true, the DECL indicates the class being defined in a class-head,
24907 or declared in an elaborated-type-specifier.
24909 Otherwise, return DECL. */
24912 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
24914 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24915 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24918 template <typename T> struct B;
24921 template <typename T> struct A::B {};
24923 Similarly, in an elaborated-type-specifier:
24925 namespace N { struct X{}; }
24928 template <typename T> friend struct N::X;
24931 However, if the DECL refers to a class type, and we are in
24932 the scope of the class, then the name lookup automatically
24933 finds the TYPE_DECL created by build_self_reference rather
24934 than a TEMPLATE_DECL. For example, in:
24936 template <class T> struct S {
24940 there is no need to handle such case. */
24942 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
24943 return DECL_TEMPLATE_RESULT (decl
);
24948 /* If too many, or too few, template-parameter lists apply to the
24949 declarator, issue an error message. Returns TRUE if all went well,
24950 and FALSE otherwise. */
24953 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
24954 cp_declarator
*declarator
,
24955 location_t declarator_location
)
24957 switch (declarator
->kind
)
24961 unsigned num_templates
= 0;
24962 tree scope
= declarator
->u
.id
.qualifying_scope
;
24965 num_templates
= num_template_headers_for_class (scope
);
24966 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
24967 == TEMPLATE_ID_EXPR
)
24968 /* If the DECLARATOR has the form `X<y>' then it uses one
24969 additional level of template parameters. */
24972 return cp_parser_check_template_parameters
24973 (parser
, num_templates
, declarator_location
, declarator
);
24979 case cdk_reference
:
24981 return (cp_parser_check_declarator_template_parameters
24982 (parser
, declarator
->declarator
, declarator_location
));
24988 gcc_unreachable ();
24993 /* NUM_TEMPLATES were used in the current declaration. If that is
24994 invalid, return FALSE and issue an error messages. Otherwise,
24995 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24996 declarator and we can print more accurate diagnostics. */
24999 cp_parser_check_template_parameters (cp_parser
* parser
,
25000 unsigned num_templates
,
25001 location_t location
,
25002 cp_declarator
*declarator
)
25004 /* If there are the same number of template classes and parameter
25005 lists, that's OK. */
25006 if (parser
->num_template_parameter_lists
== num_templates
)
25008 /* If there are more, but only one more, then we are referring to a
25009 member template. That's OK too. */
25010 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
25012 /* If there are more template classes than parameter lists, we have
25015 template <class T> void S<T>::R<T>::f (); */
25016 if (parser
->num_template_parameter_lists
< num_templates
)
25018 if (declarator
&& !current_function_decl
)
25019 error_at (location
, "specializing member %<%T::%E%> "
25020 "requires %<template<>%> syntax",
25021 declarator
->u
.id
.qualifying_scope
,
25022 declarator
->u
.id
.unqualified_name
);
25023 else if (declarator
)
25024 error_at (location
, "invalid declaration of %<%T::%E%>",
25025 declarator
->u
.id
.qualifying_scope
,
25026 declarator
->u
.id
.unqualified_name
);
25028 error_at (location
, "too few template-parameter-lists");
25031 /* Otherwise, there are too many template parameter lists. We have
25034 template <class T> template <class U> void S::f(); */
25035 error_at (location
, "too many template-parameter-lists");
25039 /* Parse an optional `::' token indicating that the following name is
25040 from the global namespace. If so, PARSER->SCOPE is set to the
25041 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25042 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25043 Returns the new value of PARSER->SCOPE, if the `::' token is
25044 present, and NULL_TREE otherwise. */
25047 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
25051 /* Peek at the next token. */
25052 token
= cp_lexer_peek_token (parser
->lexer
);
25053 /* If we're looking at a `::' token then we're starting from the
25054 global namespace, not our current location. */
25055 if (token
->type
== CPP_SCOPE
)
25057 /* Consume the `::' token. */
25058 cp_lexer_consume_token (parser
->lexer
);
25059 /* Set the SCOPE so that we know where to start the lookup. */
25060 parser
->scope
= global_namespace
;
25061 parser
->qualifying_scope
= global_namespace
;
25062 parser
->object_scope
= NULL_TREE
;
25064 return parser
->scope
;
25066 else if (!current_scope_valid_p
)
25068 parser
->scope
= NULL_TREE
;
25069 parser
->qualifying_scope
= NULL_TREE
;
25070 parser
->object_scope
= NULL_TREE
;
25076 /* Returns TRUE if the upcoming token sequence is the start of a
25077 constructor declarator. If FRIEND_P is true, the declarator is
25078 preceded by the `friend' specifier. */
25081 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
25083 bool constructor_p
;
25084 bool outside_class_specifier_p
;
25085 tree nested_name_specifier
;
25086 cp_token
*next_token
;
25088 /* The common case is that this is not a constructor declarator, so
25089 try to avoid doing lots of work if at all possible. It's not
25090 valid declare a constructor at function scope. */
25091 if (parser
->in_function_body
)
25093 /* And only certain tokens can begin a constructor declarator. */
25094 next_token
= cp_lexer_peek_token (parser
->lexer
);
25095 if (next_token
->type
!= CPP_NAME
25096 && next_token
->type
!= CPP_SCOPE
25097 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
25098 && next_token
->type
!= CPP_TEMPLATE_ID
)
25101 /* Parse tentatively; we are going to roll back all of the tokens
25103 cp_parser_parse_tentatively (parser
);
25104 /* Assume that we are looking at a constructor declarator. */
25105 constructor_p
= true;
25107 /* Look for the optional `::' operator. */
25108 cp_parser_global_scope_opt (parser
,
25109 /*current_scope_valid_p=*/false);
25110 /* Look for the nested-name-specifier. */
25111 nested_name_specifier
25112 = (cp_parser_nested_name_specifier_opt (parser
,
25113 /*typename_keyword_p=*/false,
25114 /*check_dependency_p=*/false,
25116 /*is_declaration=*/false));
25118 outside_class_specifier_p
= (!at_class_scope_p ()
25119 || !TYPE_BEING_DEFINED (current_class_type
)
25122 /* Outside of a class-specifier, there must be a
25123 nested-name-specifier. */
25124 if (!nested_name_specifier
&& outside_class_specifier_p
)
25125 constructor_p
= false;
25126 else if (nested_name_specifier
== error_mark_node
)
25127 constructor_p
= false;
25129 /* If we have a class scope, this is easy; DR 147 says that S::S always
25130 names the constructor, and no other qualified name could. */
25131 if (constructor_p
&& nested_name_specifier
25132 && CLASS_TYPE_P (nested_name_specifier
))
25134 tree id
= cp_parser_unqualified_id (parser
,
25135 /*template_keyword_p=*/false,
25136 /*check_dependency_p=*/false,
25137 /*declarator_p=*/true,
25138 /*optional_p=*/false);
25139 if (is_overloaded_fn (id
))
25140 id
= DECL_NAME (get_first_fn (id
));
25141 if (!constructor_name_p (id
, nested_name_specifier
))
25142 constructor_p
= false;
25144 /* If we still think that this might be a constructor-declarator,
25145 look for a class-name. */
25146 else if (constructor_p
)
25150 template <typename T> struct S {
25154 we must recognize that the nested `S' names a class. */
25156 type_decl
= cp_parser_class_name (parser
,
25157 /*typename_keyword_p=*/false,
25158 /*template_keyword_p=*/false,
25160 /*check_dependency_p=*/false,
25161 /*class_head_p=*/false,
25162 /*is_declaration=*/false);
25163 /* If there was no class-name, then this is not a constructor.
25164 Otherwise, if we are in a class-specifier and we aren't
25165 handling a friend declaration, check that its type matches
25166 current_class_type (c++/38313). Note: error_mark_node
25167 is left alone for error recovery purposes. */
25168 constructor_p
= (!cp_parser_error_occurred (parser
)
25169 && (outside_class_specifier_p
25170 || type_decl
== error_mark_node
25171 || same_type_p (current_class_type
,
25172 TREE_TYPE (type_decl
))));
25174 /* If we're still considering a constructor, we have to see a `(',
25175 to begin the parameter-declaration-clause, followed by either a
25176 `)', an `...', or a decl-specifier. We need to check for a
25177 type-specifier to avoid being fooled into thinking that:
25181 is a constructor. (It is actually a function named `f' that
25182 takes one parameter (of type `int') and returns a value of type
25185 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
25186 constructor_p
= false;
25189 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
25190 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
25191 /* A parameter declaration begins with a decl-specifier,
25192 which is either the "attribute" keyword, a storage class
25193 specifier, or (usually) a type-specifier. */
25194 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
25197 tree pushed_scope
= NULL_TREE
;
25198 unsigned saved_num_template_parameter_lists
;
25200 /* Names appearing in the type-specifier should be looked up
25201 in the scope of the class. */
25202 if (current_class_type
)
25206 type
= TREE_TYPE (type_decl
);
25207 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25209 type
= resolve_typename_type (type
,
25210 /*only_current_p=*/false);
25211 if (TREE_CODE (type
) == TYPENAME_TYPE
)
25213 cp_parser_abort_tentative_parse (parser
);
25217 pushed_scope
= push_scope (type
);
25220 /* Inside the constructor parameter list, surrounding
25221 template-parameter-lists do not apply. */
25222 saved_num_template_parameter_lists
25223 = parser
->num_template_parameter_lists
;
25224 parser
->num_template_parameter_lists
= 0;
25226 /* Look for the type-specifier. */
25227 cp_parser_type_specifier (parser
,
25228 CP_PARSER_FLAGS_NONE
,
25229 /*decl_specs=*/NULL
,
25230 /*is_declarator=*/true,
25231 /*declares_class_or_enum=*/NULL
,
25232 /*is_cv_qualifier=*/NULL
);
25234 parser
->num_template_parameter_lists
25235 = saved_num_template_parameter_lists
;
25237 /* Leave the scope of the class. */
25239 pop_scope (pushed_scope
);
25241 constructor_p
= !cp_parser_error_occurred (parser
);
25245 /* We did not really want to consume any tokens. */
25246 cp_parser_abort_tentative_parse (parser
);
25248 return constructor_p
;
25251 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25252 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25253 they must be performed once we are in the scope of the function.
25255 Returns the function defined. */
25258 cp_parser_function_definition_from_specifiers_and_declarator
25259 (cp_parser
* parser
,
25260 cp_decl_specifier_seq
*decl_specifiers
,
25262 const cp_declarator
*declarator
)
25267 /* Begin the function-definition. */
25268 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
25270 /* The things we're about to see are not directly qualified by any
25271 template headers we've seen thus far. */
25272 reset_specialization ();
25274 /* If there were names looked up in the decl-specifier-seq that we
25275 did not check, check them now. We must wait until we are in the
25276 scope of the function to perform the checks, since the function
25277 might be a friend. */
25278 perform_deferred_access_checks (tf_warning_or_error
);
25282 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
25283 parser
->omp_declare_simd
= NULL
;
25284 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
25285 parser
->oacc_routine
= NULL
;
25290 /* Skip the entire function. */
25291 cp_parser_skip_to_end_of_block_or_statement (parser
);
25292 fn
= error_mark_node
;
25294 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
25296 /* Seen already, skip it. An error message has already been output. */
25297 cp_parser_skip_to_end_of_block_or_statement (parser
);
25298 fn
= current_function_decl
;
25299 current_function_decl
= NULL_TREE
;
25300 /* If this is a function from a class, pop the nested class. */
25301 if (current_class_name
)
25302 pop_nested_class ();
25307 if (DECL_DECLARED_INLINE_P (current_function_decl
))
25308 tv
= TV_PARSE_INLINE
;
25310 tv
= TV_PARSE_FUNC
;
25312 fn
= cp_parser_function_definition_after_declarator (parser
,
25313 /*inline_p=*/false);
25320 /* Parse the part of a function-definition that follows the
25321 declarator. INLINE_P is TRUE iff this function is an inline
25322 function defined within a class-specifier.
25324 Returns the function defined. */
25327 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
25331 bool ctor_initializer_p
= false;
25332 bool saved_in_unbraced_linkage_specification_p
;
25333 bool saved_in_function_body
;
25334 unsigned saved_num_template_parameter_lists
;
25336 bool fully_implicit_function_template_p
25337 = parser
->fully_implicit_function_template_p
;
25338 parser
->fully_implicit_function_template_p
= false;
25339 tree implicit_template_parms
25340 = parser
->implicit_template_parms
;
25341 parser
->implicit_template_parms
= 0;
25342 cp_binding_level
* implicit_template_scope
25343 = parser
->implicit_template_scope
;
25344 parser
->implicit_template_scope
= 0;
25346 saved_in_function_body
= parser
->in_function_body
;
25347 parser
->in_function_body
= true;
25348 /* If the next token is `return', then the code may be trying to
25349 make use of the "named return value" extension that G++ used to
25351 token
= cp_lexer_peek_token (parser
->lexer
);
25352 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
25354 /* Consume the `return' keyword. */
25355 cp_lexer_consume_token (parser
->lexer
);
25356 /* Look for the identifier that indicates what value is to be
25358 cp_parser_identifier (parser
);
25359 /* Issue an error message. */
25360 error_at (token
->location
,
25361 "named return values are no longer supported");
25362 /* Skip tokens until we reach the start of the function body. */
25365 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25366 if (token
->type
== CPP_OPEN_BRACE
25367 || token
->type
== CPP_EOF
25368 || token
->type
== CPP_PRAGMA_EOL
)
25370 cp_lexer_consume_token (parser
->lexer
);
25373 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25374 anything declared inside `f'. */
25375 saved_in_unbraced_linkage_specification_p
25376 = parser
->in_unbraced_linkage_specification_p
;
25377 parser
->in_unbraced_linkage_specification_p
= false;
25378 /* Inside the function, surrounding template-parameter-lists do not
25380 saved_num_template_parameter_lists
25381 = parser
->num_template_parameter_lists
;
25382 parser
->num_template_parameter_lists
= 0;
25384 start_lambda_scope (current_function_decl
);
25386 /* If the next token is `try', `__transaction_atomic', or
25387 `__transaction_relaxed`, then we are looking at either function-try-block
25388 or function-transaction-block. Note that all of these include the
25390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
25391 ctor_initializer_p
= cp_parser_function_transaction (parser
,
25392 RID_TRANSACTION_ATOMIC
);
25393 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
25394 RID_TRANSACTION_RELAXED
))
25395 ctor_initializer_p
= cp_parser_function_transaction (parser
,
25396 RID_TRANSACTION_RELAXED
);
25397 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
25398 ctor_initializer_p
= cp_parser_function_try_block (parser
);
25400 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
25401 (parser
, /*in_function_try_block=*/false);
25403 finish_lambda_scope ();
25405 /* Finish the function. */
25406 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
25407 (inline_p
? 2 : 0));
25408 /* Generate code for it, if necessary. */
25409 expand_or_defer_fn (fn
);
25410 /* Restore the saved values. */
25411 parser
->in_unbraced_linkage_specification_p
25412 = saved_in_unbraced_linkage_specification_p
;
25413 parser
->num_template_parameter_lists
25414 = saved_num_template_parameter_lists
;
25415 parser
->in_function_body
= saved_in_function_body
;
25417 parser
->fully_implicit_function_template_p
25418 = fully_implicit_function_template_p
;
25419 parser
->implicit_template_parms
25420 = implicit_template_parms
;
25421 parser
->implicit_template_scope
25422 = implicit_template_scope
;
25424 if (parser
->fully_implicit_function_template_p
)
25425 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
25430 /* Parse a template-declaration body (following argument list). */
25433 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
25434 tree parameter_list
,
25437 tree decl
= NULL_TREE
;
25438 bool friend_p
= false;
25440 /* We just processed one more parameter list. */
25441 ++parser
->num_template_parameter_lists
;
25443 /* Get the deferred access checks from the parameter list. These
25444 will be checked once we know what is being declared, as for a
25445 member template the checks must be performed in the scope of the
25446 class containing the member. */
25447 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
25449 /* Tentatively parse for a new template parameter list, which can either be
25450 the template keyword or a template introduction. */
25451 if (cp_parser_template_declaration_after_export (parser
, member_p
))
25453 else if (cxx_dialect
>= cxx11
25454 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25455 decl
= cp_parser_alias_declaration (parser
);
25458 /* There are no access checks when parsing a template, as we do not
25459 know if a specialization will be a friend. */
25460 push_deferring_access_checks (dk_no_check
);
25461 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25462 decl
= cp_parser_single_declaration (parser
,
25465 /*explicit_specialization_p=*/false,
25467 pop_deferring_access_checks ();
25469 /* If this is a member template declaration, let the front
25471 if (member_p
&& !friend_p
&& decl
)
25473 if (TREE_CODE (decl
) == TYPE_DECL
)
25474 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
25476 decl
= finish_member_template_decl (decl
);
25478 else if (friend_p
&& decl
25479 && DECL_DECLARES_TYPE_P (decl
))
25480 make_friend_class (current_class_type
, TREE_TYPE (decl
),
25481 /*complain=*/true);
25483 /* We are done with the current parameter list. */
25484 --parser
->num_template_parameter_lists
;
25486 pop_deferring_access_checks ();
25489 finish_template_decl (parameter_list
);
25491 /* Check the template arguments for a literal operator template. */
25493 && DECL_DECLARES_FUNCTION_P (decl
)
25494 && UDLIT_OPER_P (DECL_NAME (decl
)))
25497 if (parameter_list
== NULL_TREE
)
25501 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
25502 if (num_parms
== 1)
25504 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
25505 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
25506 if (TREE_TYPE (parm
) != char_type_node
25507 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
25510 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
25512 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
25513 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
25514 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
25515 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
25516 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
25517 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
25525 if (cxx_dialect
>= cxx14
)
25526 error ("literal operator template %qD has invalid parameter list."
25527 " Expected non-type template argument pack <char...>"
25528 " or <typename CharT, CharT...>",
25531 error ("literal operator template %qD has invalid parameter list."
25532 " Expected non-type template argument pack <char...>",
25537 /* Register member declarations. */
25538 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
25539 finish_member_declaration (decl
);
25540 /* If DECL is a function template, we must return to parse it later.
25541 (Even though there is no definition, there might be default
25542 arguments that need handling.) */
25543 if (member_p
&& decl
25544 && DECL_DECLARES_FUNCTION_P (decl
))
25545 vec_safe_push (unparsed_funs_with_definitions
, decl
);
25548 /* Parse a template introduction header for a template-declaration. Returns
25549 false if tentative parse fails. */
25552 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
25554 cp_parser_parse_tentatively (parser
);
25556 tree saved_scope
= parser
->scope
;
25557 tree saved_object_scope
= parser
->object_scope
;
25558 tree saved_qualifying_scope
= parser
->qualifying_scope
;
25560 /* Look for the optional `::' operator. */
25561 cp_parser_global_scope_opt (parser
,
25562 /*current_scope_valid_p=*/false);
25563 /* Look for the nested-name-specifier. */
25564 cp_parser_nested_name_specifier_opt (parser
,
25565 /*typename_keyword_p=*/false,
25566 /*check_dependency_p=*/true,
25568 /*is_declaration=*/false);
25570 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25571 tree concept_name
= cp_parser_identifier (parser
);
25573 /* Look up the concept for which we will be matching
25574 template parameters. */
25575 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
25577 parser
->scope
= saved_scope
;
25578 parser
->object_scope
= saved_object_scope
;
25579 parser
->qualifying_scope
= saved_qualifying_scope
;
25581 if (concept_name
== error_mark_node
)
25582 cp_parser_simulate_error (parser
);
25584 /* Look for opening brace for introduction. */
25585 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
25587 if (!cp_parser_parse_definitely (parser
))
25590 push_deferring_access_checks (dk_deferred
);
25592 /* Build vector of placeholder parameters and grab
25593 matching identifiers. */
25594 tree introduction_list
= cp_parser_introduction_list (parser
);
25596 /* The introduction-list shall not be empty. */
25597 int nargs
= TREE_VEC_LENGTH (introduction_list
);
25600 error ("empty introduction-list");
25604 /* Look for closing brace for introduction. */
25605 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25608 if (tmpl_decl
== error_mark_node
)
25610 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
25615 /* Build and associate the constraint. */
25616 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
25617 if (parms
&& parms
!= error_mark_node
)
25619 cp_parser_template_declaration_after_parameters (parser
, parms
,
25624 error_at (token
->location
, "no matching concept for template-introduction");
25628 /* Parse a normal template-declaration following the template keyword. */
25631 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
25633 tree parameter_list
;
25634 bool need_lang_pop
;
25635 location_t location
= input_location
;
25637 /* Look for the `<' token. */
25638 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
25640 if (at_class_scope_p () && current_function_decl
)
25642 /* 14.5.2.2 [temp.mem]
25644 A local class shall not have member templates. */
25645 error_at (location
,
25646 "invalid declaration of member template in local class");
25647 cp_parser_skip_to_end_of_block_or_statement (parser
);
25652 A template ... shall not have C linkage. */
25653 if (current_lang_name
== lang_name_c
)
25655 error_at (location
, "template with C linkage");
25656 /* Give it C++ linkage to avoid confusing other parts of the
25658 push_lang_context (lang_name_cplusplus
);
25659 need_lang_pop
= true;
25662 need_lang_pop
= false;
25664 /* We cannot perform access checks on the template parameter
25665 declarations until we know what is being declared, just as we
25666 cannot check the decl-specifier list. */
25667 push_deferring_access_checks (dk_deferred
);
25669 /* If the next token is `>', then we have an invalid
25670 specialization. Rather than complain about an invalid template
25671 parameter, issue an error message here. */
25672 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
25674 cp_parser_error (parser
, "invalid explicit specialization");
25675 begin_specialization ();
25676 parameter_list
= NULL_TREE
;
25680 /* Parse the template parameters. */
25681 parameter_list
= cp_parser_template_parameter_list (parser
);
25684 /* Look for the `>'. */
25685 cp_parser_skip_to_end_of_template_parameter_list (parser
);
25687 /* Manage template requirements */
25688 tree reqs
= get_shorthand_constraints (current_template_parms
);
25689 if (tree r
= cp_parser_requires_clause_opt (parser
))
25690 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
25691 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
25693 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
25696 /* For the erroneous case of a template with C linkage, we pushed an
25697 implicit C++ linkage scope; exit that scope now. */
25699 pop_lang_context ();
25702 /* Parse a template-declaration, assuming that the `export' (and
25703 `extern') keywords, if present, has already been scanned. MEMBER_P
25704 is as for cp_parser_template_declaration. */
25707 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
25709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25711 cp_lexer_consume_token (parser
->lexer
);
25712 cp_parser_explicit_template_declaration (parser
, member_p
);
25715 else if (flag_concepts
)
25716 return cp_parser_template_introduction (parser
, member_p
);
25721 /* Perform the deferred access checks from a template-parameter-list.
25722 CHECKS is a TREE_LIST of access checks, as returned by
25723 get_deferred_access_checks. */
25726 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
25728 ++processing_template_parmlist
;
25729 perform_access_checks (checks
, tf_warning_or_error
);
25730 --processing_template_parmlist
;
25733 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25734 `function-definition' sequence that follows a template header.
25735 If MEMBER_P is true, this declaration appears in a class scope.
25737 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25738 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25741 cp_parser_single_declaration (cp_parser
* parser
,
25742 vec
<deferred_access_check
, va_gc
> *checks
,
25744 bool explicit_specialization_p
,
25747 int declares_class_or_enum
;
25748 tree decl
= NULL_TREE
;
25749 cp_decl_specifier_seq decl_specifiers
;
25750 bool function_definition_p
= false;
25751 cp_token
*decl_spec_token_start
;
25753 /* This function is only used when processing a template
25755 gcc_assert (innermost_scope_kind () == sk_template_parms
25756 || innermost_scope_kind () == sk_template_spec
);
25758 /* Defer access checks until we know what is being declared. */
25759 push_deferring_access_checks (dk_deferred
);
25761 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25763 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25764 cp_parser_decl_specifier_seq (parser
,
25765 CP_PARSER_FLAGS_OPTIONAL
,
25767 &declares_class_or_enum
);
25769 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
25771 /* There are no template typedefs. */
25772 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
25774 error_at (decl_spec_token_start
->location
,
25775 "template declaration of %<typedef%>");
25776 decl
= error_mark_node
;
25779 /* Gather up the access checks that occurred the
25780 decl-specifier-seq. */
25781 stop_deferring_access_checks ();
25783 /* Check for the declaration of a template class. */
25784 if (declares_class_or_enum
)
25786 if (cp_parser_declares_only_class_p (parser
)
25787 || (declares_class_or_enum
& 2))
25789 // If this is a declaration, but not a definition, associate
25790 // any constraints with the type declaration. Constraints
25791 // are associated with definitions in cp_parser_class_specifier.
25792 if (declares_class_or_enum
== 1)
25793 associate_classtype_constraints (decl_specifiers
.type
);
25795 decl
= shadow_tag (&decl_specifiers
);
25800 friend template <typename T> struct A<T>::B;
25803 A<T>::B will be represented by a TYPENAME_TYPE, and
25804 therefore not recognized by shadow_tag. */
25805 if (friend_p
&& *friend_p
25807 && decl_specifiers
.type
25808 && TYPE_P (decl_specifiers
.type
))
25809 decl
= decl_specifiers
.type
;
25811 if (decl
&& decl
!= error_mark_node
)
25812 decl
= TYPE_NAME (decl
);
25814 decl
= error_mark_node
;
25816 /* Perform access checks for template parameters. */
25817 cp_parser_perform_template_parameter_access_checks (checks
);
25819 /* Give a helpful diagnostic for
25820 template <class T> struct A { } a;
25821 if we aren't already recovering from an error. */
25822 if (!cp_parser_declares_only_class_p (parser
)
25825 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
25826 "a class template declaration must not declare "
25828 cp_parser_skip_to_end_of_block_or_statement (parser
);
25834 /* Complain about missing 'typename' or other invalid type names. */
25835 if (!decl_specifiers
.any_type_specifiers_p
25836 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25838 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25839 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25840 the rest of this declaration. */
25841 decl
= error_mark_node
;
25845 /* If it's not a template class, try for a template function. If
25846 the next token is a `;', then this declaration does not declare
25847 anything. But, if there were errors in the decl-specifiers, then
25848 the error might well have come from an attempted class-specifier.
25849 In that case, there's no need to warn about a missing declarator. */
25851 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
25852 || decl_specifiers
.type
!= error_mark_node
))
25854 decl
= cp_parser_init_declarator (parser
,
25857 /*function_definition_allowed_p=*/true,
25859 declares_class_or_enum
,
25860 &function_definition_p
,
25863 /* 7.1.1-1 [dcl.stc]
25865 A storage-class-specifier shall not be specified in an explicit
25866 specialization... */
25868 && explicit_specialization_p
25869 && decl_specifiers
.storage_class
!= sc_none
)
25871 error_at (decl_spec_token_start
->location
,
25872 "explicit template specialization cannot have a storage class");
25873 decl
= error_mark_node
;
25876 if (decl
&& VAR_P (decl
))
25877 check_template_variable (decl
);
25880 /* Look for a trailing `;' after the declaration. */
25881 if (!function_definition_p
25882 && (decl
== error_mark_node
25883 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
25884 cp_parser_skip_to_end_of_block_or_statement (parser
);
25887 pop_deferring_access_checks ();
25889 /* Clear any current qualification; whatever comes next is the start
25890 of something new. */
25891 parser
->scope
= NULL_TREE
;
25892 parser
->qualifying_scope
= NULL_TREE
;
25893 parser
->object_scope
= NULL_TREE
;
25898 /* Parse a cast-expression that is not the operand of a unary "&". */
25901 cp_parser_simple_cast_expression (cp_parser
*parser
)
25903 return cp_parser_cast_expression (parser
, /*address_p=*/false,
25904 /*cast_p=*/false, /*decltype*/false, NULL
);
25907 /* Parse a functional cast to TYPE. Returns an expression
25908 representing the cast. */
25911 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
25913 vec
<tree
, va_gc
> *vec
;
25914 tree expression_list
;
25918 location_t start_loc
= input_location
;
25921 type
= error_mark_node
;
25923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25925 cp_lexer_set_source_position (parser
->lexer
);
25926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25927 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
25928 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
25929 if (TREE_CODE (type
) == TYPE_DECL
)
25930 type
= TREE_TYPE (type
);
25932 cast
= finish_compound_literal (type
, expression_list
,
25933 tf_warning_or_error
);
25934 /* Create a location of the form:
25937 with caret == start at the start of the type name,
25938 finishing at the closing brace. */
25939 location_t finish_loc
25940 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
25941 location_t combined_loc
= make_location (start_loc
, start_loc
,
25943 cast
.set_location (combined_loc
);
25948 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
25950 /*allow_expansion_p=*/true,
25951 /*non_constant_p=*/NULL
);
25953 expression_list
= error_mark_node
;
25956 expression_list
= build_tree_list_vec (vec
);
25957 release_tree_vector (vec
);
25960 cast
= build_functional_cast (type
, expression_list
,
25961 tf_warning_or_error
);
25962 /* [expr.const]/1: In an integral constant expression "only type
25963 conversions to integral or enumeration type can be used". */
25964 if (TREE_CODE (type
) == TYPE_DECL
)
25965 type
= TREE_TYPE (type
);
25966 if (cast
!= error_mark_node
25967 && !cast_valid_in_integral_constant_expression_p (type
)
25968 && cp_parser_non_integral_constant_expression (parser
,
25970 return error_mark_node
;
25972 /* Create a location of the form:
25975 with caret == start at the start of the type name,
25976 finishing at the closing paren. */
25977 location_t finish_loc
25978 = get_finish (cp_lexer_previous_token (parser
->lexer
)->location
);
25979 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
25980 cast
.set_location (combined_loc
);
25984 /* Save the tokens that make up the body of a member function defined
25985 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25986 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25987 specifiers applied to the declaration. Returns the FUNCTION_DECL
25988 for the member function. */
25991 cp_parser_save_member_function_body (cp_parser
* parser
,
25992 cp_decl_specifier_seq
*decl_specifiers
,
25993 cp_declarator
*declarator
,
26000 /* Create the FUNCTION_DECL. */
26001 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
26002 cp_finalize_omp_declare_simd (parser
, fn
);
26003 cp_finalize_oacc_routine (parser
, fn
, true);
26004 /* If something went badly wrong, bail out now. */
26005 if (fn
== error_mark_node
)
26007 /* If there's a function-body, skip it. */
26008 if (cp_parser_token_starts_function_definition_p
26009 (cp_lexer_peek_token (parser
->lexer
)))
26010 cp_parser_skip_to_end_of_block_or_statement (parser
);
26011 return error_mark_node
;
26014 /* Remember it, if there default args to post process. */
26015 cp_parser_save_default_args (parser
, fn
);
26017 /* Save away the tokens that make up the body of the
26019 first
= parser
->lexer
->next_token
;
26020 /* Handle function try blocks. */
26021 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
26022 cp_lexer_consume_token (parser
->lexer
);
26023 /* We can have braced-init-list mem-initializers before the fn body. */
26024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
26026 cp_lexer_consume_token (parser
->lexer
);
26027 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
26029 /* cache_group will stop after an un-nested { } pair, too. */
26030 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
26033 /* variadic mem-inits have ... after the ')'. */
26034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26035 cp_lexer_consume_token (parser
->lexer
);
26038 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26039 /* Handle function try blocks. */
26040 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
26041 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26042 last
= parser
->lexer
->next_token
;
26044 /* Save away the inline definition; we will process it when the
26045 class is complete. */
26046 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
26047 DECL_PENDING_INLINE_P (fn
) = 1;
26049 /* We need to know that this was defined in the class, so that
26050 friend templates are handled correctly. */
26051 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
26053 /* Add FN to the queue of functions to be parsed later. */
26054 vec_safe_push (unparsed_funs_with_definitions
, fn
);
26059 /* Save the tokens that make up the in-class initializer for a non-static
26060 data member. Returns a DEFAULT_ARG. */
26063 cp_parser_save_nsdmi (cp_parser
* parser
)
26065 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
26068 /* Parse a template-argument-list, as well as the trailing ">" (but
26069 not the opening "<"). See cp_parser_template_argument_list for the
26073 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
26077 tree saved_qualifying_scope
;
26078 tree saved_object_scope
;
26079 bool saved_greater_than_is_operator_p
;
26080 int saved_unevaluated_operand
;
26081 int saved_inhibit_evaluation_warnings
;
26085 When parsing a template-id, the first non-nested `>' is taken as
26086 the end of the template-argument-list rather than a greater-than
26088 saved_greater_than_is_operator_p
26089 = parser
->greater_than_is_operator_p
;
26090 parser
->greater_than_is_operator_p
= false;
26091 /* Parsing the argument list may modify SCOPE, so we save it
26093 saved_scope
= parser
->scope
;
26094 saved_qualifying_scope
= parser
->qualifying_scope
;
26095 saved_object_scope
= parser
->object_scope
;
26096 /* We need to evaluate the template arguments, even though this
26097 template-id may be nested within a "sizeof". */
26098 saved_unevaluated_operand
= cp_unevaluated_operand
;
26099 cp_unevaluated_operand
= 0;
26100 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
26101 c_inhibit_evaluation_warnings
= 0;
26102 /* Parse the template-argument-list itself. */
26103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
26104 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
26105 arguments
= NULL_TREE
;
26107 arguments
= cp_parser_template_argument_list (parser
);
26108 /* Look for the `>' that ends the template-argument-list. If we find
26109 a '>>' instead, it's probably just a typo. */
26110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
26112 if (cxx_dialect
!= cxx98
)
26114 /* In C++0x, a `>>' in a template argument list or cast
26115 expression is considered to be two separate `>'
26116 tokens. So, change the current token to a `>', but don't
26117 consume it: it will be consumed later when the outer
26118 template argument list (or cast expression) is parsed.
26119 Note that this replacement of `>' for `>>' is necessary
26120 even if we are parsing tentatively: in the tentative
26121 case, after calling
26122 cp_parser_enclosed_template_argument_list we will always
26123 throw away all of the template arguments and the first
26124 closing `>', either because the template argument list
26125 was erroneous or because we are replacing those tokens
26126 with a CPP_TEMPLATE_ID token. The second `>' (which will
26127 not have been thrown away) is needed either to close an
26128 outer template argument list or to complete a new-style
26130 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26131 token
->type
= CPP_GREATER
;
26133 else if (!saved_greater_than_is_operator_p
)
26135 /* If we're in a nested template argument list, the '>>' has
26136 to be a typo for '> >'. We emit the error message, but we
26137 continue parsing and we push a '>' as next token, so that
26138 the argument list will be parsed correctly. Note that the
26139 global source location is still on the token before the
26140 '>>', so we need to say explicitly where we want it. */
26141 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26142 error_at (token
->location
, "%<>>%> should be %<> >%> "
26143 "within a nested template argument list");
26145 token
->type
= CPP_GREATER
;
26149 /* If this is not a nested template argument list, the '>>'
26150 is a typo for '>'. Emit an error message and continue.
26151 Same deal about the token location, but here we can get it
26152 right by consuming the '>>' before issuing the diagnostic. */
26153 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26154 error_at (token
->location
,
26155 "spurious %<>>%>, use %<>%> to terminate "
26156 "a template argument list");
26160 cp_parser_skip_to_end_of_template_parameter_list (parser
);
26161 /* The `>' token might be a greater-than operator again now. */
26162 parser
->greater_than_is_operator_p
26163 = saved_greater_than_is_operator_p
;
26164 /* Restore the SAVED_SCOPE. */
26165 parser
->scope
= saved_scope
;
26166 parser
->qualifying_scope
= saved_qualifying_scope
;
26167 parser
->object_scope
= saved_object_scope
;
26168 cp_unevaluated_operand
= saved_unevaluated_operand
;
26169 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
26174 /* MEMBER_FUNCTION is a member function, or a friend. If default
26175 arguments, or the body of the function have not yet been parsed,
26179 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
26181 timevar_push (TV_PARSE_INMETH
);
26182 /* If this member is a template, get the underlying
26184 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
26185 member_function
= DECL_TEMPLATE_RESULT (member_function
);
26187 /* There should not be any class definitions in progress at this
26188 point; the bodies of members are only parsed outside of all class
26190 gcc_assert (parser
->num_classes_being_defined
== 0);
26191 /* While we're parsing the member functions we might encounter more
26192 classes. We want to handle them right away, but we don't want
26193 them getting mixed up with functions that are currently in the
26195 push_unparsed_function_queues (parser
);
26197 /* Make sure that any template parameters are in scope. */
26198 maybe_begin_member_template_processing (member_function
);
26200 /* If the body of the function has not yet been parsed, parse it
26202 if (DECL_PENDING_INLINE_P (member_function
))
26204 tree function_scope
;
26205 cp_token_cache
*tokens
;
26207 /* The function is no longer pending; we are processing it. */
26208 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
26209 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
26210 DECL_PENDING_INLINE_P (member_function
) = 0;
26212 /* If this is a local class, enter the scope of the containing
26214 function_scope
= current_function_decl
;
26215 if (function_scope
)
26216 push_function_context ();
26218 /* Push the body of the function onto the lexer stack. */
26219 cp_parser_push_lexer_for_tokens (parser
, tokens
);
26221 /* Let the front end know that we going to be defining this
26223 start_preparsed_function (member_function
, NULL_TREE
,
26224 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
26226 /* Don't do access checking if it is a templated function. */
26227 if (processing_template_decl
)
26228 push_deferring_access_checks (dk_no_check
);
26230 /* #pragma omp declare reduction needs special parsing. */
26231 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
26233 parser
->lexer
->in_pragma
= true;
26234 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
26235 finish_function (/*inline*/2);
26236 cp_check_omp_declare_reduction (member_function
);
26239 /* Now, parse the body of the function. */
26240 cp_parser_function_definition_after_declarator (parser
,
26241 /*inline_p=*/true);
26243 if (processing_template_decl
)
26244 pop_deferring_access_checks ();
26246 /* Leave the scope of the containing function. */
26247 if (function_scope
)
26248 pop_function_context ();
26249 cp_parser_pop_lexer (parser
);
26252 /* Remove any template parameters from the symbol table. */
26253 maybe_end_member_template_processing ();
26255 /* Restore the queue. */
26256 pop_unparsed_function_queues (parser
);
26257 timevar_pop (TV_PARSE_INMETH
);
26260 /* If DECL contains any default args, remember it on the unparsed
26261 functions queue. */
26264 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
26268 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
26270 probe
= TREE_CHAIN (probe
))
26271 if (TREE_PURPOSE (probe
))
26273 cp_default_arg_entry entry
= {current_class_type
, decl
};
26274 vec_safe_push (unparsed_funs_with_default_args
, entry
);
26279 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26280 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26281 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26282 from the parameter-type-list. */
26285 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
26286 tree default_arg
, tree parmtype
)
26288 cp_token_cache
*tokens
;
26292 if (default_arg
== error_mark_node
)
26293 return error_mark_node
;
26295 /* Push the saved tokens for the default argument onto the parser's
26297 tokens
= DEFARG_TOKENS (default_arg
);
26298 cp_parser_push_lexer_for_tokens (parser
, tokens
);
26300 start_lambda_scope (decl
);
26302 /* Parse the default argument. */
26303 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
26304 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
26305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26307 finish_lambda_scope ();
26309 if (parsed_arg
== error_mark_node
)
26310 cp_parser_skip_to_end_of_statement (parser
);
26312 if (!processing_template_decl
)
26314 /* In a non-template class, check conversions now. In a template,
26315 we'll wait and instantiate these as needed. */
26316 if (TREE_CODE (decl
) == PARM_DECL
)
26317 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
26318 tf_warning_or_error
);
26320 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
26323 /* If the token stream has not been completely used up, then
26324 there was extra junk after the end of the default
26326 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
26328 if (TREE_CODE (decl
) == PARM_DECL
)
26329 cp_parser_error (parser
, "expected %<,%>");
26331 cp_parser_error (parser
, "expected %<;%>");
26334 /* Revert to the main lexer. */
26335 cp_parser_pop_lexer (parser
);
26340 /* FIELD is a non-static data member with an initializer which we saved for
26341 later; parse it now. */
26344 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
26348 maybe_begin_member_template_processing (field
);
26350 push_unparsed_function_queues (parser
);
26351 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
26352 DECL_INITIAL (field
),
26354 pop_unparsed_function_queues (parser
);
26356 maybe_end_member_template_processing ();
26358 DECL_INITIAL (field
) = def
;
26361 /* FN is a FUNCTION_DECL which may contains a parameter with an
26362 unparsed DEFAULT_ARG. Parse the default args now. This function
26363 assumes that the current scope is the scope in which the default
26364 argument should be processed. */
26367 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
26369 bool saved_local_variables_forbidden_p
;
26370 tree parm
, parmdecl
;
26372 /* While we're parsing the default args, we might (due to the
26373 statement expression extension) encounter more classes. We want
26374 to handle them right away, but we don't want them getting mixed
26375 up with default args that are currently in the queue. */
26376 push_unparsed_function_queues (parser
);
26378 /* Local variable names (and the `this' keyword) may not appear
26379 in a default argument. */
26380 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
26381 parser
->local_variables_forbidden_p
= true;
26383 push_defarg_context (fn
);
26385 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
26386 parmdecl
= DECL_ARGUMENTS (fn
);
26387 parm
&& parm
!= void_list_node
;
26388 parm
= TREE_CHAIN (parm
),
26389 parmdecl
= DECL_CHAIN (parmdecl
))
26391 tree default_arg
= TREE_PURPOSE (parm
);
26393 vec
<tree
, va_gc
> *insts
;
26400 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
26401 /* This can happen for a friend declaration for a function
26402 already declared with default arguments. */
26406 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
26408 TREE_VALUE (parm
));
26409 if (parsed_arg
== error_mark_node
)
26414 TREE_PURPOSE (parm
) = parsed_arg
;
26416 /* Update any instantiations we've already created. */
26417 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
26418 vec_safe_iterate (insts
, ix
, ©
); ix
++)
26419 TREE_PURPOSE (copy
) = parsed_arg
;
26422 pop_defarg_context ();
26424 /* Make sure no default arg is missing. */
26425 check_default_args (fn
);
26427 /* Restore the state of local_variables_forbidden_p. */
26428 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
26430 /* Restore the queue. */
26431 pop_unparsed_function_queues (parser
);
26434 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26436 sizeof ... ( identifier )
26438 where the 'sizeof' token has already been consumed. */
26441 cp_parser_sizeof_pack (cp_parser
*parser
)
26443 /* Consume the `...'. */
26444 cp_lexer_consume_token (parser
->lexer
);
26445 maybe_warn_variadic_templates ();
26447 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
26449 cp_lexer_consume_token (parser
->lexer
);
26451 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
26452 "%<sizeof...%> argument must be surrounded by parentheses");
26454 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26455 tree name
= cp_parser_identifier (parser
);
26456 if (name
== error_mark_node
)
26457 return error_mark_node
;
26458 /* The name is not qualified. */
26459 parser
->scope
= NULL_TREE
;
26460 parser
->qualifying_scope
= NULL_TREE
;
26461 parser
->object_scope
= NULL_TREE
;
26462 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
26463 if (expr
== error_mark_node
)
26464 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
26466 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
26467 expr
= TREE_TYPE (expr
);
26468 else if (TREE_CODE (expr
) == CONST_DECL
)
26469 expr
= DECL_INITIAL (expr
);
26470 expr
= make_pack_expansion (expr
);
26471 PACK_EXPANSION_SIZEOF_P (expr
) = true;
26474 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26479 /* Parse the operand of `sizeof' (or a similar operator). Returns
26480 either a TYPE or an expression, depending on the form of the
26481 input. The KEYWORD indicates which kind of expression we have
26485 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
26487 tree expr
= NULL_TREE
;
26488 const char *saved_message
;
26490 bool saved_integral_constant_expression_p
;
26491 bool saved_non_integral_constant_expression_p
;
26493 /* If it's a `...', then we are computing the length of a parameter
26495 if (keyword
== RID_SIZEOF
26496 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26497 return cp_parser_sizeof_pack (parser
);
26499 /* Types cannot be defined in a `sizeof' expression. Save away the
26501 saved_message
= parser
->type_definition_forbidden_message
;
26502 /* And create the new one. */
26503 tmp
= concat ("types may not be defined in %<",
26504 IDENTIFIER_POINTER (ridpointers
[keyword
]),
26505 "%> expressions", NULL
);
26506 parser
->type_definition_forbidden_message
= tmp
;
26508 /* The restrictions on constant-expressions do not apply inside
26509 sizeof expressions. */
26510 saved_integral_constant_expression_p
26511 = parser
->integral_constant_expression_p
;
26512 saved_non_integral_constant_expression_p
26513 = parser
->non_integral_constant_expression_p
;
26514 parser
->integral_constant_expression_p
= false;
26516 /* Do not actually evaluate the expression. */
26517 ++cp_unevaluated_operand
;
26518 ++c_inhibit_evaluation_warnings
;
26519 /* If it's a `(', then we might be looking at the type-id
26521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26523 tree type
= NULL_TREE
;
26525 /* We can't be sure yet whether we're looking at a type-id or an
26527 cp_parser_parse_tentatively (parser
);
26528 /* Note: as a GNU Extension, compound literals are considered
26529 postfix-expressions as they are in C99, so they are valid
26530 arguments to sizeof. See comment in cp_parser_cast_expression
26532 if (cp_parser_compound_literal_p (parser
))
26533 cp_parser_simulate_error (parser
);
26536 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
26537 parser
->in_type_id_in_expr_p
= true;
26538 /* Look for the type-id. */
26539 type
= cp_parser_type_id (parser
);
26540 /* Look for the closing `)'. */
26541 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26542 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
26545 /* If all went well, then we're done. */
26546 if (cp_parser_parse_definitely (parser
))
26548 cp_decl_specifier_seq decl_specs
;
26550 /* Build a trivial decl-specifier-seq. */
26551 clear_decl_specs (&decl_specs
);
26552 decl_specs
.type
= type
;
26554 /* Call grokdeclarator to figure out what type this is. */
26555 expr
= grokdeclarator (NULL
,
26559 /*attrlist=*/NULL
);
26563 /* If the type-id production did not work out, then we must be
26564 looking at the unary-expression production. */
26566 expr
= cp_parser_unary_expression (parser
);
26568 /* Go back to evaluating expressions. */
26569 --cp_unevaluated_operand
;
26570 --c_inhibit_evaluation_warnings
;
26572 /* Free the message we created. */
26574 /* And restore the old one. */
26575 parser
->type_definition_forbidden_message
= saved_message
;
26576 parser
->integral_constant_expression_p
26577 = saved_integral_constant_expression_p
;
26578 parser
->non_integral_constant_expression_p
26579 = saved_non_integral_constant_expression_p
;
26584 /* If the current declaration has no declarator, return true. */
26587 cp_parser_declares_only_class_p (cp_parser
*parser
)
26589 /* If the next token is a `;' or a `,' then there is no
26591 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26592 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
26595 /* Update the DECL_SPECS to reflect the storage class indicated by
26599 cp_parser_set_storage_class (cp_parser
*parser
,
26600 cp_decl_specifier_seq
*decl_specs
,
26604 cp_storage_class storage_class
;
26606 if (parser
->in_unbraced_linkage_specification_p
)
26608 error_at (token
->location
, "invalid use of %qD in linkage specification",
26609 ridpointers
[keyword
]);
26612 else if (decl_specs
->storage_class
!= sc_none
)
26614 decl_specs
->conflicting_specifiers_p
= true;
26618 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
26619 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
26620 && decl_specs
->gnu_thread_keyword_p
)
26622 pedwarn (decl_specs
->locations
[ds_thread
], 0,
26623 "%<__thread%> before %qD", ridpointers
[keyword
]);
26629 storage_class
= sc_auto
;
26632 storage_class
= sc_register
;
26635 storage_class
= sc_static
;
26638 storage_class
= sc_extern
;
26641 storage_class
= sc_mutable
;
26644 gcc_unreachable ();
26646 decl_specs
->storage_class
= storage_class
;
26647 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
26649 /* A storage class specifier cannot be applied alongside a typedef
26650 specifier. If there is a typedef specifier present then set
26651 conflicting_specifiers_p which will trigger an error later
26652 on in grokdeclarator. */
26653 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
26654 decl_specs
->conflicting_specifiers_p
= true;
26657 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26658 is true, the type is a class or enum definition. */
26661 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
26664 bool type_definition_p
)
26666 decl_specs
->any_specifiers_p
= true;
26668 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26669 (with, for example, in "typedef int wchar_t;") we remember that
26670 this is what happened. In system headers, we ignore these
26671 declarations so that G++ can work with system headers that are not
26673 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
26674 && !type_definition_p
26675 && (type_spec
== boolean_type_node
26676 || type_spec
== char16_type_node
26677 || type_spec
== char32_type_node
26678 || type_spec
== wchar_type_node
)
26679 && (decl_specs
->type
26680 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
26681 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
26682 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
26683 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
26685 decl_specs
->redefined_builtin_type
= type_spec
;
26686 set_and_check_decl_spec_loc (decl_specs
,
26687 ds_redefined_builtin_type_spec
,
26689 if (!decl_specs
->type
)
26691 decl_specs
->type
= type_spec
;
26692 decl_specs
->type_definition_p
= false;
26693 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
26696 else if (decl_specs
->type
)
26697 decl_specs
->multiple_types_p
= true;
26700 decl_specs
->type
= type_spec
;
26701 decl_specs
->type_definition_p
= type_definition_p
;
26702 decl_specs
->redefined_builtin_type
= NULL_TREE
;
26703 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
26707 /* True iff TOKEN is the GNU keyword __thread. */
26710 token_is__thread (cp_token
*token
)
26712 gcc_assert (token
->keyword
== RID_THREAD
);
26713 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
26716 /* Set the location for a declarator specifier and check if it is
26719 DECL_SPECS is the sequence of declarator specifiers onto which to
26722 DS is the single declarator specifier to set which location is to
26723 be set onto the existing sequence of declarators.
26725 LOCATION is the location for the declarator specifier to
26729 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
26730 cp_decl_spec ds
, cp_token
*token
)
26732 gcc_assert (ds
< ds_last
);
26734 if (decl_specs
== NULL
)
26737 source_location location
= token
->location
;
26739 if (decl_specs
->locations
[ds
] == 0)
26741 decl_specs
->locations
[ds
] = location
;
26742 if (ds
== ds_thread
)
26743 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
26749 if (decl_specs
->locations
[ds_long_long
] != 0)
26750 error_at (location
,
26751 "%<long long long%> is too long for GCC");
26754 decl_specs
->locations
[ds_long_long
] = location
;
26755 pedwarn_cxx98 (location
,
26757 "ISO C++ 1998 does not support %<long long%>");
26760 else if (ds
== ds_thread
)
26762 bool gnu
= token_is__thread (token
);
26763 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
26764 error_at (location
,
26765 "both %<__thread%> and %<thread_local%> specified");
26767 error_at (location
, "duplicate %qD", token
->u
.value
);
26771 static const char *const decl_spec_names
[] = {
26788 error_at (location
,
26789 "duplicate %qs", decl_spec_names
[ds
]);
26794 /* Return true iff the declarator specifier DS is present in the
26795 sequence of declarator specifiers DECL_SPECS. */
26798 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
26801 gcc_assert (ds
< ds_last
);
26803 if (decl_specs
== NULL
)
26806 return decl_specs
->locations
[ds
] != 0;
26809 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26810 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26813 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
26815 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
26818 /* Issue an error message indicating that TOKEN_DESC was expected.
26819 If KEYWORD is true, it indicated this function is called by
26820 cp_parser_require_keword and the required token can only be
26821 a indicated keyword. */
26824 cp_parser_required_error (cp_parser
*parser
,
26825 required_token token_desc
,
26828 switch (token_desc
)
26831 cp_parser_error (parser
, "expected %<new%>");
26834 cp_parser_error (parser
, "expected %<delete%>");
26837 cp_parser_error (parser
, "expected %<return%>");
26840 cp_parser_error (parser
, "expected %<while%>");
26843 cp_parser_error (parser
, "expected %<extern%>");
26845 case RT_STATIC_ASSERT
:
26846 cp_parser_error (parser
, "expected %<static_assert%>");
26849 cp_parser_error (parser
, "expected %<decltype%>");
26852 cp_parser_error (parser
, "expected %<operator%>");
26855 cp_parser_error (parser
, "expected %<class%>");
26858 cp_parser_error (parser
, "expected %<template%>");
26861 cp_parser_error (parser
, "expected %<namespace%>");
26864 cp_parser_error (parser
, "expected %<using%>");
26867 cp_parser_error (parser
, "expected %<asm%>");
26870 cp_parser_error (parser
, "expected %<try%>");
26873 cp_parser_error (parser
, "expected %<catch%>");
26876 cp_parser_error (parser
, "expected %<throw%>");
26879 cp_parser_error (parser
, "expected %<__label__%>");
26882 cp_parser_error (parser
, "expected %<@try%>");
26884 case RT_AT_SYNCHRONIZED
:
26885 cp_parser_error (parser
, "expected %<@synchronized%>");
26888 cp_parser_error (parser
, "expected %<@throw%>");
26890 case RT_TRANSACTION_ATOMIC
:
26891 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
26893 case RT_TRANSACTION_RELAXED
:
26894 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
26901 switch (token_desc
)
26904 cp_parser_error (parser
, "expected %<;%>");
26906 case RT_OPEN_PAREN
:
26907 cp_parser_error (parser
, "expected %<(%>");
26909 case RT_CLOSE_BRACE
:
26910 cp_parser_error (parser
, "expected %<}%>");
26912 case RT_OPEN_BRACE
:
26913 cp_parser_error (parser
, "expected %<{%>");
26915 case RT_CLOSE_SQUARE
:
26916 cp_parser_error (parser
, "expected %<]%>");
26918 case RT_OPEN_SQUARE
:
26919 cp_parser_error (parser
, "expected %<[%>");
26922 cp_parser_error (parser
, "expected %<,%>");
26925 cp_parser_error (parser
, "expected %<::%>");
26928 cp_parser_error (parser
, "expected %<<%>");
26931 cp_parser_error (parser
, "expected %<>%>");
26934 cp_parser_error (parser
, "expected %<=%>");
26937 cp_parser_error (parser
, "expected %<...%>");
26940 cp_parser_error (parser
, "expected %<*%>");
26943 cp_parser_error (parser
, "expected %<~%>");
26946 cp_parser_error (parser
, "expected %<:%>");
26948 case RT_COLON_SCOPE
:
26949 cp_parser_error (parser
, "expected %<:%> or %<::%>");
26951 case RT_CLOSE_PAREN
:
26952 cp_parser_error (parser
, "expected %<)%>");
26954 case RT_COMMA_CLOSE_PAREN
:
26955 cp_parser_error (parser
, "expected %<,%> or %<)%>");
26957 case RT_PRAGMA_EOL
:
26958 cp_parser_error (parser
, "expected end of line");
26961 cp_parser_error (parser
, "expected identifier");
26964 cp_parser_error (parser
, "expected selection-statement");
26966 case RT_INTERATION
:
26967 cp_parser_error (parser
, "expected iteration-statement");
26970 cp_parser_error (parser
, "expected jump-statement");
26973 cp_parser_error (parser
, "expected class-key");
26975 case RT_CLASS_TYPENAME_TEMPLATE
:
26976 cp_parser_error (parser
,
26977 "expected %<class%>, %<typename%>, or %<template%>");
26980 gcc_unreachable ();
26984 gcc_unreachable ();
26989 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26990 issue an error message indicating that TOKEN_DESC was expected.
26992 Returns the token consumed, if the token had the appropriate type.
26993 Otherwise, returns NULL. */
26996 cp_parser_require (cp_parser
* parser
,
26997 enum cpp_ttype type
,
26998 required_token token_desc
)
27000 if (cp_lexer_next_token_is (parser
->lexer
, type
))
27001 return cp_lexer_consume_token (parser
->lexer
);
27004 /* Output the MESSAGE -- unless we're parsing tentatively. */
27005 if (!cp_parser_simulate_error (parser
))
27006 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
27011 /* An error message is produced if the next token is not '>'.
27012 All further tokens are skipped until the desired token is
27013 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27016 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
27018 /* Current level of '< ... >'. */
27019 unsigned level
= 0;
27020 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27021 unsigned nesting_depth
= 0;
27023 /* Are we ready, yet? If not, issue error message. */
27024 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
27027 /* Skip tokens until the desired token is found. */
27030 /* Peek at the next token. */
27031 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27034 if (!nesting_depth
)
27039 if (cxx_dialect
== cxx98
)
27040 /* C++0x views the `>>' operator as two `>' tokens, but
27043 else if (!nesting_depth
&& level
-- == 0)
27045 /* We've hit a `>>' where the first `>' closes the
27046 template argument list, and the second `>' is
27047 spurious. Just consume the `>>' and stop; we've
27048 already produced at least one error. */
27049 cp_lexer_consume_token (parser
->lexer
);
27052 /* Fall through for C++0x, so we handle the second `>' in
27056 if (!nesting_depth
&& level
-- == 0)
27058 /* We've reached the token we want, consume it and stop. */
27059 cp_lexer_consume_token (parser
->lexer
);
27064 case CPP_OPEN_PAREN
:
27065 case CPP_OPEN_SQUARE
:
27069 case CPP_CLOSE_PAREN
:
27070 case CPP_CLOSE_SQUARE
:
27071 if (nesting_depth
-- == 0)
27076 case CPP_PRAGMA_EOL
:
27077 case CPP_SEMICOLON
:
27078 case CPP_OPEN_BRACE
:
27079 case CPP_CLOSE_BRACE
:
27080 /* The '>' was probably forgotten, don't look further. */
27087 /* Consume this token. */
27088 cp_lexer_consume_token (parser
->lexer
);
27092 /* If the next token is the indicated keyword, consume it. Otherwise,
27093 issue an error message indicating that TOKEN_DESC was expected.
27095 Returns the token consumed, if the token had the appropriate type.
27096 Otherwise, returns NULL. */
27099 cp_parser_require_keyword (cp_parser
* parser
,
27101 required_token token_desc
)
27103 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
27105 if (token
&& token
->keyword
!= keyword
)
27107 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
27114 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27115 function-definition. */
27118 cp_parser_token_starts_function_definition_p (cp_token
* token
)
27120 return (/* An ordinary function-body begins with an `{'. */
27121 token
->type
== CPP_OPEN_BRACE
27122 /* A ctor-initializer begins with a `:'. */
27123 || token
->type
== CPP_COLON
27124 /* A function-try-block begins with `try'. */
27125 || token
->keyword
== RID_TRY
27126 /* A function-transaction-block begins with `__transaction_atomic'
27127 or `__transaction_relaxed'. */
27128 || token
->keyword
== RID_TRANSACTION_ATOMIC
27129 || token
->keyword
== RID_TRANSACTION_RELAXED
27130 /* The named return value extension begins with `return'. */
27131 || token
->keyword
== RID_RETURN
);
27134 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27138 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
27142 token
= cp_lexer_peek_token (parser
->lexer
);
27143 return (token
->type
== CPP_OPEN_BRACE
27144 || (token
->type
== CPP_COLON
27145 && !parser
->colon_doesnt_start_class_def_p
));
27148 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27149 C++0x) ending a template-argument. */
27152 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
27156 token
= cp_lexer_peek_token (parser
->lexer
);
27157 return (token
->type
== CPP_COMMA
27158 || token
->type
== CPP_GREATER
27159 || token
->type
== CPP_ELLIPSIS
27160 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
27163 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27164 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27167 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
27172 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
27173 if (token
->type
== CPP_LESS
)
27175 /* Check for the sequence `<::' in the original code. It would be lexed as
27176 `[:', where `[' is a digraph, and there is no whitespace before
27178 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
27181 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
27182 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
27188 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27189 or none_type otherwise. */
27191 static enum tag_types
27192 cp_parser_token_is_class_key (cp_token
* token
)
27194 switch (token
->keyword
)
27199 return record_type
;
27208 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27209 or none_type otherwise or if the token is null. */
27211 static enum tag_types
27212 cp_parser_token_is_type_parameter_key (cp_token
* token
)
27217 switch (token
->keyword
)
27222 return typename_type
;
27229 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27232 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
27234 if (type
== error_mark_node
)
27236 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
27238 if (permerror (input_location
, "%qs tag used in naming %q#T",
27239 class_key
== union_type
? "union"
27240 : class_key
== record_type
? "struct" : "class",
27242 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
27243 "%q#T was previously declared here", type
);
27247 /* Issue an error message if DECL is redeclared with different
27248 access than its original declaration [class.access.spec/3].
27249 This applies to nested classes, nested class templates and
27250 enumerations [class.mem/1]. */
27253 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
27256 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
27257 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
27260 if ((TREE_PRIVATE (decl
)
27261 != (current_access_specifier
== access_private_node
))
27262 || (TREE_PROTECTED (decl
)
27263 != (current_access_specifier
== access_protected_node
)))
27264 error_at (location
, "%qD redeclared with different access", decl
);
27267 /* Look for the `template' keyword, as a syntactic disambiguator.
27268 Return TRUE iff it is present, in which case it will be
27272 cp_parser_optional_template_keyword (cp_parser
*parser
)
27274 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27276 /* In C++98 the `template' keyword can only be used within templates;
27277 outside templates the parser can always figure out what is a
27278 template and what is not. In C++11, per the resolution of DR 468,
27279 `template' is allowed in cases where it is not strictly necessary. */
27280 if (!processing_template_decl
27281 && pedantic
&& cxx_dialect
== cxx98
)
27283 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27284 pedwarn (token
->location
, OPT_Wpedantic
,
27285 "in C++98 %<template%> (as a disambiguator) is only "
27286 "allowed within templates");
27287 /* If this part of the token stream is rescanned, the same
27288 error message would be generated. So, we purge the token
27289 from the stream. */
27290 cp_lexer_purge_token (parser
->lexer
);
27295 /* Consume the `template' keyword. */
27296 cp_lexer_consume_token (parser
->lexer
);
27303 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27304 set PARSER->SCOPE, and perform other related actions. */
27307 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
27309 struct tree_check
*check_value
;
27311 /* Get the stored value. */
27312 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
27313 /* Set the scope from the stored value. */
27314 parser
->scope
= saved_checks_value (check_value
);
27315 parser
->qualifying_scope
= check_value
->qualifying_scope
;
27316 parser
->object_scope
= NULL_TREE
;
27319 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27320 encounter the end of a block before what we were looking for. */
27323 cp_parser_cache_group (cp_parser
*parser
,
27324 enum cpp_ttype end
,
27329 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27331 /* Abort a parenthesized expression if we encounter a semicolon. */
27332 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
27333 && token
->type
== CPP_SEMICOLON
)
27335 /* If we've reached the end of the file, stop. */
27336 if (token
->type
== CPP_EOF
27337 || (end
!= CPP_PRAGMA_EOL
27338 && token
->type
== CPP_PRAGMA_EOL
))
27340 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
27341 /* We've hit the end of an enclosing block, so there's been some
27342 kind of syntax error. */
27345 /* Consume the token. */
27346 cp_lexer_consume_token (parser
->lexer
);
27347 /* See if it starts a new group. */
27348 if (token
->type
== CPP_OPEN_BRACE
)
27350 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
27351 /* In theory this should probably check end == '}', but
27352 cp_parser_save_member_function_body needs it to exit
27353 after either '}' or ')' when called with ')'. */
27357 else if (token
->type
== CPP_OPEN_PAREN
)
27359 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
27360 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
27363 else if (token
->type
== CPP_PRAGMA
)
27364 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
27365 else if (token
->type
== end
)
27370 /* Like above, for caching a default argument or NSDMI. Both of these are
27371 terminated by a non-nested comma, but it can be unclear whether or not a
27372 comma is nested in a template argument list unless we do more parsing.
27373 In order to handle this ambiguity, when we encounter a ',' after a '<'
27374 we try to parse what follows as a parameter-declaration-list (in the
27375 case of a default argument) or a member-declarator (in the case of an
27376 NSDMI). If that succeeds, then we stop caching. */
27379 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
27381 unsigned depth
= 0;
27382 int maybe_template_id
= 0;
27383 cp_token
*first_token
;
27385 tree default_argument
;
27387 /* Add tokens until we have processed the entire default
27388 argument. We add the range [first_token, token). */
27389 first_token
= cp_lexer_peek_token (parser
->lexer
);
27390 if (first_token
->type
== CPP_OPEN_BRACE
)
27392 /* For list-initialization, this is straightforward. */
27393 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
27394 token
= cp_lexer_peek_token (parser
->lexer
);
27400 /* Peek at the next token. */
27401 token
= cp_lexer_peek_token (parser
->lexer
);
27402 /* What we do depends on what token we have. */
27403 switch (token
->type
)
27405 /* In valid code, a default argument must be
27406 immediately followed by a `,' `)', or `...'. */
27408 if (depth
== 0 && maybe_template_id
)
27410 /* If we've seen a '<', we might be in a
27411 template-argument-list. Until Core issue 325 is
27412 resolved, we don't know how this situation ought
27413 to be handled, so try to DTRT. We check whether
27414 what comes after the comma is a valid parameter
27415 declaration list. If it is, then the comma ends
27416 the default argument; otherwise the default
27417 argument continues. */
27418 bool error
= false;
27421 /* Set ITALP so cp_parser_parameter_declaration_list
27422 doesn't decide to commit to this parse. */
27423 bool saved_italp
= parser
->in_template_argument_list_p
;
27424 parser
->in_template_argument_list_p
= true;
27426 cp_parser_parse_tentatively (parser
);
27430 /* Parse declarators until we reach a non-comma or
27431 somthing that cannot be an initializer.
27432 Just checking whether we're looking at a single
27433 declarator is insufficient. Consider:
27434 int var = tuple<T,U>::x;
27435 The template parameter 'U' looks exactly like a
27439 int ctor_dtor_or_conv_p
;
27440 cp_lexer_consume_token (parser
->lexer
);
27441 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27442 &ctor_dtor_or_conv_p
,
27443 /*parenthesized_p=*/NULL
,
27445 /*friend_p=*/false);
27446 peek
= cp_lexer_peek_token (parser
->lexer
);
27447 if (cp_parser_error_occurred (parser
))
27450 while (peek
->type
== CPP_COMMA
);
27451 /* If we met an '=' or ';' then the original comma
27452 was the end of the NSDMI. Otherwise assume
27453 we're still in the NSDMI. */
27454 error
= (peek
->type
!= CPP_EQ
27455 && peek
->type
!= CPP_SEMICOLON
);
27459 cp_lexer_consume_token (parser
->lexer
);
27460 begin_scope (sk_function_parms
, NULL_TREE
);
27461 cp_parser_parameter_declaration_list (parser
, &error
);
27462 pop_bindings_and_leave_scope ();
27464 if (!cp_parser_error_occurred (parser
) && !error
)
27466 cp_parser_abort_tentative_parse (parser
);
27468 parser
->in_template_argument_list_p
= saved_italp
;
27471 case CPP_CLOSE_PAREN
:
27473 /* If we run into a non-nested `;', `}', or `]',
27474 then the code is invalid -- but the default
27475 argument is certainly over. */
27476 case CPP_SEMICOLON
:
27477 case CPP_CLOSE_BRACE
:
27478 case CPP_CLOSE_SQUARE
:
27480 /* Handle correctly int n = sizeof ... ( p ); */
27481 && token
->type
!= CPP_ELLIPSIS
)
27483 /* Update DEPTH, if necessary. */
27484 else if (token
->type
== CPP_CLOSE_PAREN
27485 || token
->type
== CPP_CLOSE_BRACE
27486 || token
->type
== CPP_CLOSE_SQUARE
)
27490 case CPP_OPEN_PAREN
:
27491 case CPP_OPEN_SQUARE
:
27492 case CPP_OPEN_BRACE
:
27498 /* This might be the comparison operator, or it might
27499 start a template argument list. */
27500 ++maybe_template_id
;
27504 if (cxx_dialect
== cxx98
)
27506 /* Fall through for C++0x, which treats the `>>'
27507 operator like two `>' tokens in certain
27513 /* This might be an operator, or it might close a
27514 template argument list. But if a previous '<'
27515 started a template argument list, this will have
27516 closed it, so we can't be in one anymore. */
27517 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
27518 if (maybe_template_id
< 0)
27519 maybe_template_id
= 0;
27523 /* If we run out of tokens, issue an error message. */
27525 case CPP_PRAGMA_EOL
:
27526 error_at (token
->location
, "file ends in default argument");
27527 return error_mark_node
;
27531 /* In these cases, we should look for template-ids.
27532 For example, if the default argument is
27533 `X<int, double>()', we need to do name lookup to
27534 figure out whether or not `X' is a template; if
27535 so, the `,' does not end the default argument.
27537 That is not yet done. */
27544 /* If we've reached the end, stop. */
27548 /* Add the token to the token block. */
27549 token
= cp_lexer_consume_token (parser
->lexer
);
27552 /* Create a DEFAULT_ARG to represent the unparsed default
27554 default_argument
= make_node (DEFAULT_ARG
);
27555 DEFARG_TOKENS (default_argument
)
27556 = cp_token_cache_new (first_token
, token
);
27557 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
27559 return default_argument
;
27562 /* Begin parsing tentatively. We always save tokens while parsing
27563 tentatively so that if the tentative parsing fails we can restore the
27567 cp_parser_parse_tentatively (cp_parser
* parser
)
27569 /* Enter a new parsing context. */
27570 parser
->context
= cp_parser_context_new (parser
->context
);
27571 /* Begin saving tokens. */
27572 cp_lexer_save_tokens (parser
->lexer
);
27573 /* In order to avoid repetitive access control error messages,
27574 access checks are queued up until we are no longer parsing
27576 push_deferring_access_checks (dk_deferred
);
27579 /* Commit to the currently active tentative parse. */
27582 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
27584 cp_parser_context
*context
;
27587 /* Mark all of the levels as committed. */
27588 lexer
= parser
->lexer
;
27589 for (context
= parser
->context
; context
->next
; context
= context
->next
)
27591 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27593 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27594 while (!cp_lexer_saving_tokens (lexer
))
27595 lexer
= lexer
->next
;
27596 cp_lexer_commit_tokens (lexer
);
27600 /* Commit to the topmost currently active tentative parse.
27602 Note that this function shouldn't be called when there are
27603 irreversible side-effects while in a tentative state. For
27604 example, we shouldn't create a permanent entry in the symbol
27605 table, or issue an error message that might not apply if the
27606 tentative parse is aborted. */
27609 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
27611 cp_parser_context
*context
= parser
->context
;
27612 cp_lexer
*lexer
= parser
->lexer
;
27616 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27618 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27620 while (!cp_lexer_saving_tokens (lexer
))
27621 lexer
= lexer
->next
;
27622 cp_lexer_commit_tokens (lexer
);
27626 /* Abort the currently active tentative parse. All consumed tokens
27627 will be rolled back, and no diagnostics will be issued. */
27630 cp_parser_abort_tentative_parse (cp_parser
* parser
)
27632 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
27633 || errorcount
> 0);
27634 cp_parser_simulate_error (parser
);
27635 /* Now, pretend that we want to see if the construct was
27636 successfully parsed. */
27637 cp_parser_parse_definitely (parser
);
27640 /* Stop parsing tentatively. If a parse error has occurred, restore the
27641 token stream. Otherwise, commit to the tokens we have consumed.
27642 Returns true if no error occurred; false otherwise. */
27645 cp_parser_parse_definitely (cp_parser
* parser
)
27647 bool error_occurred
;
27648 cp_parser_context
*context
;
27650 /* Remember whether or not an error occurred, since we are about to
27651 destroy that information. */
27652 error_occurred
= cp_parser_error_occurred (parser
);
27653 /* Remove the topmost context from the stack. */
27654 context
= parser
->context
;
27655 parser
->context
= context
->next
;
27656 /* If no parse errors occurred, commit to the tentative parse. */
27657 if (!error_occurred
)
27659 /* Commit to the tokens read tentatively, unless that was
27661 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
27662 cp_lexer_commit_tokens (parser
->lexer
);
27664 pop_to_parent_deferring_access_checks ();
27666 /* Otherwise, if errors occurred, roll back our state so that things
27667 are just as they were before we began the tentative parse. */
27670 cp_lexer_rollback_tokens (parser
->lexer
);
27671 pop_deferring_access_checks ();
27673 /* Add the context to the front of the free list. */
27674 context
->next
= cp_parser_context_free_list
;
27675 cp_parser_context_free_list
= context
;
27677 return !error_occurred
;
27680 /* Returns true if we are parsing tentatively and are not committed to
27681 this tentative parse. */
27684 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
27686 return (cp_parser_parsing_tentatively (parser
)
27687 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
27690 /* Returns nonzero iff an error has occurred during the most recent
27691 tentative parse. */
27694 cp_parser_error_occurred (cp_parser
* parser
)
27696 return (cp_parser_parsing_tentatively (parser
)
27697 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
27700 /* Returns nonzero if GNU extensions are allowed. */
27703 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
27705 return parser
->allow_gnu_extensions_p
;
27708 /* Objective-C++ Productions */
27711 /* Parse an Objective-C expression, which feeds into a primary-expression
27715 objc-message-expression
27716 objc-string-literal
27717 objc-encode-expression
27718 objc-protocol-expression
27719 objc-selector-expression
27721 Returns a tree representation of the expression. */
27724 cp_parser_objc_expression (cp_parser
* parser
)
27726 /* Try to figure out what kind of declaration is present. */
27727 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27731 case CPP_OPEN_SQUARE
:
27732 return cp_parser_objc_message_expression (parser
);
27734 case CPP_OBJC_STRING
:
27735 kwd
= cp_lexer_consume_token (parser
->lexer
);
27736 return objc_build_string_object (kwd
->u
.value
);
27739 switch (kwd
->keyword
)
27741 case RID_AT_ENCODE
:
27742 return cp_parser_objc_encode_expression (parser
);
27744 case RID_AT_PROTOCOL
:
27745 return cp_parser_objc_protocol_expression (parser
);
27747 case RID_AT_SELECTOR
:
27748 return cp_parser_objc_selector_expression (parser
);
27754 error_at (kwd
->location
,
27755 "misplaced %<@%D%> Objective-C++ construct",
27757 cp_parser_skip_to_end_of_block_or_statement (parser
);
27760 return error_mark_node
;
27763 /* Parse an Objective-C message expression.
27765 objc-message-expression:
27766 [ objc-message-receiver objc-message-args ]
27768 Returns a representation of an Objective-C message. */
27771 cp_parser_objc_message_expression (cp_parser
* parser
)
27773 tree receiver
, messageargs
;
27775 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27776 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
27777 receiver
= cp_parser_objc_message_receiver (parser
);
27778 messageargs
= cp_parser_objc_message_args (parser
);
27779 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27780 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27782 tree result
= objc_build_message_expr (receiver
, messageargs
);
27784 /* Construct a location e.g.
27787 ranging from the '[' to the ']', with the caret at the start. */
27788 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
27789 protected_set_expr_location (result
, combined_loc
);
27794 /* Parse an objc-message-receiver.
27796 objc-message-receiver:
27798 simple-type-specifier
27800 Returns a representation of the type or expression. */
27803 cp_parser_objc_message_receiver (cp_parser
* parser
)
27807 /* An Objective-C message receiver may be either (1) a type
27808 or (2) an expression. */
27809 cp_parser_parse_tentatively (parser
);
27810 rcv
= cp_parser_expression (parser
);
27812 /* If that worked out, fine. */
27813 if (cp_parser_parse_definitely (parser
))
27816 cp_parser_parse_tentatively (parser
);
27817 rcv
= cp_parser_simple_type_specifier (parser
,
27818 /*decl_specs=*/NULL
,
27819 CP_PARSER_FLAGS_NONE
);
27821 if (cp_parser_parse_definitely (parser
))
27822 return objc_get_class_reference (rcv
);
27824 cp_parser_error (parser
, "objective-c++ message receiver expected");
27825 return error_mark_node
;
27828 /* Parse the arguments and selectors comprising an Objective-C message.
27833 objc-selector-args , objc-comma-args
27835 objc-selector-args:
27836 objc-selector [opt] : assignment-expression
27837 objc-selector-args objc-selector [opt] : assignment-expression
27840 assignment-expression
27841 objc-comma-args , assignment-expression
27843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27844 selector arguments and TREE_VALUE containing a list of comma
27848 cp_parser_objc_message_args (cp_parser
* parser
)
27850 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
27851 bool maybe_unary_selector_p
= true;
27852 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27854 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
27856 tree selector
= NULL_TREE
, arg
;
27858 if (token
->type
!= CPP_COLON
)
27859 selector
= cp_parser_objc_selector (parser
);
27861 /* Detect if we have a unary selector. */
27862 if (maybe_unary_selector_p
27863 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27864 return build_tree_list (selector
, NULL_TREE
);
27866 maybe_unary_selector_p
= false;
27867 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27868 arg
= cp_parser_assignment_expression (parser
);
27871 = chainon (sel_args
,
27872 build_tree_list (selector
, arg
));
27874 token
= cp_lexer_peek_token (parser
->lexer
);
27877 /* Handle non-selector arguments, if any. */
27878 while (token
->type
== CPP_COMMA
)
27882 cp_lexer_consume_token (parser
->lexer
);
27883 arg
= cp_parser_assignment_expression (parser
);
27886 = chainon (addl_args
,
27887 build_tree_list (NULL_TREE
, arg
));
27889 token
= cp_lexer_peek_token (parser
->lexer
);
27892 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
27894 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
27895 return build_tree_list (error_mark_node
, error_mark_node
);
27898 return build_tree_list (sel_args
, addl_args
);
27901 /* Parse an Objective-C encode expression.
27903 objc-encode-expression:
27904 @encode objc-typename
27906 Returns an encoded representation of the type argument. */
27909 cp_parser_objc_encode_expression (cp_parser
* parser
)
27913 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27915 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
27916 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27917 token
= cp_lexer_peek_token (parser
->lexer
);
27918 type
= complete_type (cp_parser_type_id (parser
));
27919 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27923 error_at (token
->location
,
27924 "%<@encode%> must specify a type as an argument");
27925 return error_mark_node
;
27928 /* This happens if we find @encode(T) (where T is a template
27929 typename or something dependent on a template typename) when
27930 parsing a template. In that case, we can't compile it
27931 immediately, but we rather create an AT_ENCODE_EXPR which will
27932 need to be instantiated when the template is used.
27934 if (dependent_type_p (type
))
27936 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
27937 TREE_READONLY (value
) = 1;
27942 /* Build a location of the form:
27945 with caret==start at the @ token, finishing at the close paren. */
27946 location_t combined_loc
27947 = make_location (start_loc
, start_loc
,
27948 cp_lexer_previous_token (parser
->lexer
)->location
);
27950 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
27953 /* Parse an Objective-C @defs expression. */
27956 cp_parser_objc_defs_expression (cp_parser
*parser
)
27960 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
27961 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27962 name
= cp_parser_identifier (parser
);
27963 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27965 return objc_get_class_ivars (name
);
27968 /* Parse an Objective-C protocol expression.
27970 objc-protocol-expression:
27971 @protocol ( identifier )
27973 Returns a representation of the protocol expression. */
27976 cp_parser_objc_protocol_expression (cp_parser
* parser
)
27979 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27981 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
27982 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27983 proto
= cp_parser_identifier (parser
);
27984 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27986 /* Build a location of the form:
27989 with caret==start at the @ token, finishing at the close paren. */
27990 location_t combined_loc
27991 = make_location (start_loc
, start_loc
,
27992 cp_lexer_previous_token (parser
->lexer
)->location
);
27993 tree result
= objc_build_protocol_expr (proto
);
27994 protected_set_expr_location (result
, combined_loc
);
27998 /* Parse an Objective-C selector expression.
28000 objc-selector-expression:
28001 @selector ( objc-method-signature )
28003 objc-method-signature:
28009 objc-selector-seq objc-selector :
28011 Returns a representation of the method selector. */
28014 cp_parser_objc_selector_expression (cp_parser
* parser
)
28016 tree sel_seq
= NULL_TREE
;
28017 bool maybe_unary_selector_p
= true;
28019 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28021 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
28022 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28023 token
= cp_lexer_peek_token (parser
->lexer
);
28025 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
28026 || token
->type
== CPP_SCOPE
)
28028 tree selector
= NULL_TREE
;
28030 if (token
->type
!= CPP_COLON
28031 || token
->type
== CPP_SCOPE
)
28032 selector
= cp_parser_objc_selector (parser
);
28034 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
28035 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
28037 /* Detect if we have a unary selector. */
28038 if (maybe_unary_selector_p
)
28040 sel_seq
= selector
;
28041 goto finish_selector
;
28045 cp_parser_error (parser
, "expected %<:%>");
28048 maybe_unary_selector_p
= false;
28049 token
= cp_lexer_consume_token (parser
->lexer
);
28051 if (token
->type
== CPP_SCOPE
)
28054 = chainon (sel_seq
,
28055 build_tree_list (selector
, NULL_TREE
));
28057 = chainon (sel_seq
,
28058 build_tree_list (NULL_TREE
, NULL_TREE
));
28062 = chainon (sel_seq
,
28063 build_tree_list (selector
, NULL_TREE
));
28065 token
= cp_lexer_peek_token (parser
->lexer
);
28069 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28072 /* Build a location of the form:
28075 with caret==start at the @ token, finishing at the close paren. */
28076 location_t combined_loc
28077 = make_location (loc
, loc
,
28078 cp_lexer_previous_token (parser
->lexer
)->location
);
28079 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
28080 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28081 protected_set_expr_location (result
, combined_loc
);
28085 /* Parse a list of identifiers.
28087 objc-identifier-list:
28089 objc-identifier-list , identifier
28091 Returns a TREE_LIST of identifier nodes. */
28094 cp_parser_objc_identifier_list (cp_parser
* parser
)
28100 identifier
= cp_parser_identifier (parser
);
28101 if (identifier
== error_mark_node
)
28102 return error_mark_node
;
28104 list
= build_tree_list (NULL_TREE
, identifier
);
28105 sep
= cp_lexer_peek_token (parser
->lexer
);
28107 while (sep
->type
== CPP_COMMA
)
28109 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28110 identifier
= cp_parser_identifier (parser
);
28111 if (identifier
== error_mark_node
)
28114 list
= chainon (list
, build_tree_list (NULL_TREE
,
28116 sep
= cp_lexer_peek_token (parser
->lexer
);
28122 /* Parse an Objective-C alias declaration.
28124 objc-alias-declaration:
28125 @compatibility_alias identifier identifier ;
28127 This function registers the alias mapping with the Objective-C front end.
28128 It returns nothing. */
28131 cp_parser_objc_alias_declaration (cp_parser
* parser
)
28135 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
28136 alias
= cp_parser_identifier (parser
);
28137 orig
= cp_parser_identifier (parser
);
28138 objc_declare_alias (alias
, orig
);
28139 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28142 /* Parse an Objective-C class forward-declaration.
28144 objc-class-declaration:
28145 @class objc-identifier-list ;
28147 The function registers the forward declarations with the Objective-C
28148 front end. It returns nothing. */
28151 cp_parser_objc_class_declaration (cp_parser
* parser
)
28153 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
28158 id
= cp_parser_identifier (parser
);
28159 if (id
== error_mark_node
)
28162 objc_declare_class (id
);
28164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28165 cp_lexer_consume_token (parser
->lexer
);
28169 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28172 /* Parse a list of Objective-C protocol references.
28174 objc-protocol-refs-opt:
28175 objc-protocol-refs [opt]
28177 objc-protocol-refs:
28178 < objc-identifier-list >
28180 Returns a TREE_LIST of identifiers, if any. */
28183 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
28185 tree protorefs
= NULL_TREE
;
28187 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
28189 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
28190 protorefs
= cp_parser_objc_identifier_list (parser
);
28191 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
28197 /* Parse a Objective-C visibility specification. */
28200 cp_parser_objc_visibility_spec (cp_parser
* parser
)
28202 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
28204 switch (vis
->keyword
)
28206 case RID_AT_PRIVATE
:
28207 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
28209 case RID_AT_PROTECTED
:
28210 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
28212 case RID_AT_PUBLIC
:
28213 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
28215 case RID_AT_PACKAGE
:
28216 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
28222 /* Eat '@private'/'@protected'/'@public'. */
28223 cp_lexer_consume_token (parser
->lexer
);
28226 /* Parse an Objective-C method type. Return 'true' if it is a class
28227 (+) method, and 'false' if it is an instance (-) method. */
28230 cp_parser_objc_method_type (cp_parser
* parser
)
28232 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
28238 /* Parse an Objective-C protocol qualifier. */
28241 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
28243 tree quals
= NULL_TREE
, node
;
28244 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28246 node
= token
->u
.value
;
28248 while (node
&& identifier_p (node
)
28249 && (node
== ridpointers
[(int) RID_IN
]
28250 || node
== ridpointers
[(int) RID_OUT
]
28251 || node
== ridpointers
[(int) RID_INOUT
]
28252 || node
== ridpointers
[(int) RID_BYCOPY
]
28253 || node
== ridpointers
[(int) RID_BYREF
]
28254 || node
== ridpointers
[(int) RID_ONEWAY
]))
28256 quals
= tree_cons (NULL_TREE
, node
, quals
);
28257 cp_lexer_consume_token (parser
->lexer
);
28258 token
= cp_lexer_peek_token (parser
->lexer
);
28259 node
= token
->u
.value
;
28265 /* Parse an Objective-C typename. */
28268 cp_parser_objc_typename (cp_parser
* parser
)
28270 tree type_name
= NULL_TREE
;
28272 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28274 tree proto_quals
, cp_type
= NULL_TREE
;
28276 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
28277 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
28279 /* An ObjC type name may consist of just protocol qualifiers, in which
28280 case the type shall default to 'id'. */
28281 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
28283 cp_type
= cp_parser_type_id (parser
);
28285 /* If the type could not be parsed, an error has already
28286 been produced. For error recovery, behave as if it had
28287 not been specified, which will use the default type
28289 if (cp_type
== error_mark_node
)
28291 cp_type
= NULL_TREE
;
28292 /* We need to skip to the closing parenthesis as
28293 cp_parser_type_id() does not seem to do it for
28295 cp_parser_skip_to_closing_parenthesis (parser
,
28296 /*recovering=*/true,
28297 /*or_comma=*/false,
28298 /*consume_paren=*/false);
28302 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28303 type_name
= build_tree_list (proto_quals
, cp_type
);
28309 /* Check to see if TYPE refers to an Objective-C selector name. */
28312 cp_parser_objc_selector_p (enum cpp_ttype type
)
28314 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
28315 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
28316 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
28317 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
28318 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
28321 /* Parse an Objective-C selector. */
28324 cp_parser_objc_selector (cp_parser
* parser
)
28326 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
28328 if (!cp_parser_objc_selector_p (token
->type
))
28330 error_at (token
->location
, "invalid Objective-C++ selector name");
28331 return error_mark_node
;
28334 /* C++ operator names are allowed to appear in ObjC selectors. */
28335 switch (token
->type
)
28337 case CPP_AND_AND
: return get_identifier ("and");
28338 case CPP_AND_EQ
: return get_identifier ("and_eq");
28339 case CPP_AND
: return get_identifier ("bitand");
28340 case CPP_OR
: return get_identifier ("bitor");
28341 case CPP_COMPL
: return get_identifier ("compl");
28342 case CPP_NOT
: return get_identifier ("not");
28343 case CPP_NOT_EQ
: return get_identifier ("not_eq");
28344 case CPP_OR_OR
: return get_identifier ("or");
28345 case CPP_OR_EQ
: return get_identifier ("or_eq");
28346 case CPP_XOR
: return get_identifier ("xor");
28347 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
28348 default: return token
->u
.value
;
28352 /* Parse an Objective-C params list. */
28355 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
28357 tree params
= NULL_TREE
;
28358 bool maybe_unary_selector_p
= true;
28359 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28361 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
28363 tree selector
= NULL_TREE
, type_name
, identifier
;
28364 tree parm_attr
= NULL_TREE
;
28366 if (token
->keyword
== RID_ATTRIBUTE
)
28369 if (token
->type
!= CPP_COLON
)
28370 selector
= cp_parser_objc_selector (parser
);
28372 /* Detect if we have a unary selector. */
28373 if (maybe_unary_selector_p
28374 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28376 params
= selector
; /* Might be followed by attributes. */
28380 maybe_unary_selector_p
= false;
28381 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28383 /* Something went quite wrong. There should be a colon
28384 here, but there is not. Stop parsing parameters. */
28387 type_name
= cp_parser_objc_typename (parser
);
28388 /* New ObjC allows attributes on parameters too. */
28389 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
28390 parm_attr
= cp_parser_attributes_opt (parser
);
28391 identifier
= cp_parser_identifier (parser
);
28395 objc_build_keyword_decl (selector
,
28400 token
= cp_lexer_peek_token (parser
->lexer
);
28403 if (params
== NULL_TREE
)
28405 cp_parser_error (parser
, "objective-c++ method declaration is expected");
28406 return error_mark_node
;
28409 /* We allow tail attributes for the method. */
28410 if (token
->keyword
== RID_ATTRIBUTE
)
28412 *attributes
= cp_parser_attributes_opt (parser
);
28413 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28414 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28416 cp_parser_error (parser
,
28417 "method attributes must be specified at the end");
28418 return error_mark_node
;
28421 if (params
== NULL_TREE
)
28423 cp_parser_error (parser
, "objective-c++ method declaration is expected");
28424 return error_mark_node
;
28429 /* Parse the non-keyword Objective-C params. */
28432 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
28435 tree params
= make_node (TREE_LIST
);
28436 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28437 *ellipsisp
= false; /* Initially, assume no ellipsis. */
28439 while (token
->type
== CPP_COMMA
)
28441 cp_parameter_declarator
*parmdecl
;
28444 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28445 token
= cp_lexer_peek_token (parser
->lexer
);
28447 if (token
->type
== CPP_ELLIPSIS
)
28449 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
28451 token
= cp_lexer_peek_token (parser
->lexer
);
28455 /* TODO: parse attributes for tail parameters. */
28456 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
28457 parm
= grokdeclarator (parmdecl
->declarator
,
28458 &parmdecl
->decl_specifiers
,
28459 PARM
, /*initialized=*/0,
28460 /*attrlist=*/NULL
);
28462 chainon (params
, build_tree_list (NULL_TREE
, parm
));
28463 token
= cp_lexer_peek_token (parser
->lexer
);
28466 /* We allow tail attributes for the method. */
28467 if (token
->keyword
== RID_ATTRIBUTE
)
28469 if (*attributes
== NULL_TREE
)
28471 *attributes
= cp_parser_attributes_opt (parser
);
28472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
28473 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28477 /* We have an error, but parse the attributes, so that we can
28479 *attributes
= cp_parser_attributes_opt (parser
);
28481 cp_parser_error (parser
,
28482 "method attributes must be specified at the end");
28483 return error_mark_node
;
28489 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28492 cp_parser_objc_interstitial_code (cp_parser
* parser
)
28494 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28496 /* If the next token is `extern' and the following token is a string
28497 literal, then we have a linkage specification. */
28498 if (token
->keyword
== RID_EXTERN
28499 && cp_parser_is_pure_string_literal
28500 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
28501 cp_parser_linkage_specification (parser
);
28502 /* Handle #pragma, if any. */
28503 else if (token
->type
== CPP_PRAGMA
)
28504 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
28505 /* Allow stray semicolons. */
28506 else if (token
->type
== CPP_SEMICOLON
)
28507 cp_lexer_consume_token (parser
->lexer
);
28508 /* Mark methods as optional or required, when building protocols. */
28509 else if (token
->keyword
== RID_AT_OPTIONAL
)
28511 cp_lexer_consume_token (parser
->lexer
);
28512 objc_set_method_opt (true);
28514 else if (token
->keyword
== RID_AT_REQUIRED
)
28516 cp_lexer_consume_token (parser
->lexer
);
28517 objc_set_method_opt (false);
28519 else if (token
->keyword
== RID_NAMESPACE
)
28520 cp_parser_namespace_definition (parser
);
28521 /* Other stray characters must generate errors. */
28522 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
28524 cp_lexer_consume_token (parser
->lexer
);
28525 error ("stray %qs between Objective-C++ methods",
28526 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
28528 /* Finally, try to parse a block-declaration, or a function-definition. */
28530 cp_parser_block_declaration (parser
, /*statement_p=*/false);
28533 /* Parse a method signature. */
28536 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
28538 tree rettype
, kwdparms
, optparms
;
28539 bool ellipsis
= false;
28540 bool is_class_method
;
28542 is_class_method
= cp_parser_objc_method_type (parser
);
28543 rettype
= cp_parser_objc_typename (parser
);
28544 *attributes
= NULL_TREE
;
28545 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
28546 if (kwdparms
== error_mark_node
)
28547 return error_mark_node
;
28548 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
28549 if (optparms
== error_mark_node
)
28550 return error_mark_node
;
28552 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
28556 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
28559 cp_lexer_save_tokens (parser
->lexer
);
28560 tattr
= cp_parser_attributes_opt (parser
);
28561 gcc_assert (tattr
) ;
28563 /* If the attributes are followed by a method introducer, this is not allowed.
28564 Dump the attributes and flag the situation. */
28565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
28566 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
28569 /* Otherwise, the attributes introduce some interstitial code, possibly so
28570 rewind to allow that check. */
28571 cp_lexer_rollback_tokens (parser
->lexer
);
28575 /* Parse an Objective-C method prototype list. */
28578 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
28580 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28582 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28584 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
28586 tree attributes
, sig
;
28587 bool is_class_method
;
28588 if (token
->type
== CPP_PLUS
)
28589 is_class_method
= true;
28591 is_class_method
= false;
28592 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
28593 if (sig
== error_mark_node
)
28595 cp_parser_skip_to_end_of_block_or_statement (parser
);
28596 token
= cp_lexer_peek_token (parser
->lexer
);
28599 objc_add_method_declaration (is_class_method
, sig
, attributes
);
28600 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28602 else if (token
->keyword
== RID_AT_PROPERTY
)
28603 cp_parser_objc_at_property_declaration (parser
);
28604 else if (token
->keyword
== RID_ATTRIBUTE
28605 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
28606 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28608 "prefix attributes are ignored for methods");
28610 /* Allow for interspersed non-ObjC++ code. */
28611 cp_parser_objc_interstitial_code (parser
);
28613 token
= cp_lexer_peek_token (parser
->lexer
);
28616 if (token
->type
!= CPP_EOF
)
28617 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28619 cp_parser_error (parser
, "expected %<@end%>");
28621 objc_finish_interface ();
28624 /* Parse an Objective-C method definition list. */
28627 cp_parser_objc_method_definition_list (cp_parser
* parser
)
28629 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28631 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28635 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
28638 tree sig
, attribute
;
28639 bool is_class_method
;
28640 if (token
->type
== CPP_PLUS
)
28641 is_class_method
= true;
28643 is_class_method
= false;
28644 push_deferring_access_checks (dk_deferred
);
28645 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
28646 if (sig
== error_mark_node
)
28648 cp_parser_skip_to_end_of_block_or_statement (parser
);
28649 token
= cp_lexer_peek_token (parser
->lexer
);
28652 objc_start_method_definition (is_class_method
, sig
, attribute
,
28655 /* For historical reasons, we accept an optional semicolon. */
28656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28657 cp_lexer_consume_token (parser
->lexer
);
28659 ptk
= cp_lexer_peek_token (parser
->lexer
);
28660 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
28661 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
28663 perform_deferred_access_checks (tf_warning_or_error
);
28664 stop_deferring_access_checks ();
28665 meth
= cp_parser_function_definition_after_declarator (parser
,
28667 pop_deferring_access_checks ();
28668 objc_finish_method_definition (meth
);
28671 /* The following case will be removed once @synthesize is
28672 completely implemented. */
28673 else if (token
->keyword
== RID_AT_PROPERTY
)
28674 cp_parser_objc_at_property_declaration (parser
);
28675 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
28676 cp_parser_objc_at_synthesize_declaration (parser
);
28677 else if (token
->keyword
== RID_AT_DYNAMIC
)
28678 cp_parser_objc_at_dynamic_declaration (parser
);
28679 else if (token
->keyword
== RID_ATTRIBUTE
28680 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
28681 warning_at (token
->location
, OPT_Wattributes
,
28682 "prefix attributes are ignored for methods");
28684 /* Allow for interspersed non-ObjC++ code. */
28685 cp_parser_objc_interstitial_code (parser
);
28687 token
= cp_lexer_peek_token (parser
->lexer
);
28690 if (token
->type
!= CPP_EOF
)
28691 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28693 cp_parser_error (parser
, "expected %<@end%>");
28695 objc_finish_implementation ();
28698 /* Parse Objective-C ivars. */
28701 cp_parser_objc_class_ivars (cp_parser
* parser
)
28703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28705 if (token
->type
!= CPP_OPEN_BRACE
)
28706 return; /* No ivars specified. */
28708 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
28709 token
= cp_lexer_peek_token (parser
->lexer
);
28711 while (token
->type
!= CPP_CLOSE_BRACE
28712 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28714 cp_decl_specifier_seq declspecs
;
28715 int decl_class_or_enum_p
;
28716 tree prefix_attributes
;
28718 cp_parser_objc_visibility_spec (parser
);
28720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28723 cp_parser_decl_specifier_seq (parser
,
28724 CP_PARSER_FLAGS_OPTIONAL
,
28726 &decl_class_or_enum_p
);
28728 /* auto, register, static, extern, mutable. */
28729 if (declspecs
.storage_class
!= sc_none
)
28731 cp_parser_error (parser
, "invalid type for instance variable");
28732 declspecs
.storage_class
= sc_none
;
28735 /* thread_local. */
28736 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
28738 cp_parser_error (parser
, "invalid type for instance variable");
28739 declspecs
.locations
[ds_thread
] = 0;
28743 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
28745 cp_parser_error (parser
, "invalid type for instance variable");
28746 declspecs
.locations
[ds_typedef
] = 0;
28749 prefix_attributes
= declspecs
.attributes
;
28750 declspecs
.attributes
= NULL_TREE
;
28752 /* Keep going until we hit the `;' at the end of the
28754 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28756 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
28757 cp_declarator
*declarator
= NULL
;
28758 int ctor_dtor_or_conv_p
;
28760 /* Check for a (possibly unnamed) bitfield declaration. */
28761 token
= cp_lexer_peek_token (parser
->lexer
);
28762 if (token
->type
== CPP_COLON
)
28765 if (token
->type
== CPP_NAME
28766 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
28769 /* Get the name of the bitfield. */
28770 declarator
= make_id_declarator (NULL_TREE
,
28771 cp_parser_identifier (parser
),
28775 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28776 /* Get the width of the bitfield. */
28778 = cp_parser_constant_expression (parser
);
28782 /* Parse the declarator. */
28784 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28785 &ctor_dtor_or_conv_p
,
28786 /*parenthesized_p=*/NULL
,
28787 /*member_p=*/false,
28788 /*friend_p=*/false);
28791 /* Look for attributes that apply to the ivar. */
28792 attributes
= cp_parser_attributes_opt (parser
);
28793 /* Remember which attributes are prefix attributes and
28795 first_attribute
= attributes
;
28796 /* Combine the attributes. */
28797 attributes
= chainon (prefix_attributes
, attributes
);
28800 /* Create the bitfield declaration. */
28801 decl
= grokbitfield (declarator
, &declspecs
,
28805 decl
= grokfield (declarator
, &declspecs
,
28806 NULL_TREE
, /*init_const_expr_p=*/false,
28807 NULL_TREE
, attributes
);
28809 /* Add the instance variable. */
28810 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
28811 objc_add_instance_variable (decl
);
28813 /* Reset PREFIX_ATTRIBUTES. */
28814 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
28815 attributes
= TREE_CHAIN (attributes
);
28817 TREE_CHAIN (attributes
) = NULL_TREE
;
28819 token
= cp_lexer_peek_token (parser
->lexer
);
28821 if (token
->type
== CPP_COMMA
)
28823 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28829 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28830 token
= cp_lexer_peek_token (parser
->lexer
);
28833 if (token
->keyword
== RID_AT_END
)
28834 cp_parser_error (parser
, "expected %<}%>");
28836 /* Do not consume the RID_AT_END, so it will be read again as terminating
28837 the @interface of @implementation. */
28838 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28839 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
28841 /* For historical reasons, we accept an optional semicolon. */
28842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28843 cp_lexer_consume_token (parser
->lexer
);
28846 /* Parse an Objective-C protocol declaration. */
28849 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
28851 tree proto
, protorefs
;
28854 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
28857 tok
= cp_lexer_peek_token (parser
->lexer
);
28858 error_at (tok
->location
, "identifier expected after %<@protocol%>");
28859 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28863 /* See if we have a forward declaration or a definition. */
28864 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28866 /* Try a forward declaration first. */
28867 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
28873 id
= cp_parser_identifier (parser
);
28874 if (id
== error_mark_node
)
28877 objc_declare_protocol (id
, attributes
);
28879 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28880 cp_lexer_consume_token (parser
->lexer
);
28884 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28887 /* Ok, we got a full-fledged definition (or at least should). */
28890 proto
= cp_parser_identifier (parser
);
28891 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
28892 objc_start_protocol (proto
, protorefs
, attributes
);
28893 cp_parser_objc_method_prototype_list (parser
);
28897 /* Parse an Objective-C superclass or category. */
28900 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
28903 tree
*categ
, bool *is_class_extension
)
28905 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
28907 *super
= *categ
= NULL_TREE
;
28908 *is_class_extension
= false;
28909 if (next
->type
== CPP_COLON
)
28911 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28912 *super
= cp_parser_identifier (parser
);
28914 else if (next
->type
== CPP_OPEN_PAREN
)
28916 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
28918 /* If there is no category name, and this is an @interface, we
28919 have a class extension. */
28920 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28922 *categ
= NULL_TREE
;
28923 *is_class_extension
= true;
28926 *categ
= cp_parser_identifier (parser
);
28928 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28932 /* Parse an Objective-C class interface. */
28935 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
28937 tree name
, super
, categ
, protos
;
28938 bool is_class_extension
;
28940 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
28941 name
= cp_parser_identifier (parser
);
28942 if (name
== error_mark_node
)
28944 /* It's hard to recover because even if valid @interface stuff
28945 is to follow, we can't compile it (or validate it) if we
28946 don't even know which class it refers to. Let's assume this
28947 was a stray '@interface' token in the stream and skip it.
28951 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
28952 &is_class_extension
);
28953 protos
= cp_parser_objc_protocol_refs_opt (parser
);
28955 /* We have either a class or a category on our hands. */
28956 if (categ
|| is_class_extension
)
28957 objc_start_category_interface (name
, categ
, protos
, attributes
);
28960 objc_start_class_interface (name
, super
, protos
, attributes
);
28961 /* Handle instance variable declarations, if any. */
28962 cp_parser_objc_class_ivars (parser
);
28963 objc_continue_interface ();
28966 cp_parser_objc_method_prototype_list (parser
);
28969 /* Parse an Objective-C class implementation. */
28972 cp_parser_objc_class_implementation (cp_parser
* parser
)
28974 tree name
, super
, categ
;
28975 bool is_class_extension
;
28977 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
28978 name
= cp_parser_identifier (parser
);
28979 if (name
== error_mark_node
)
28981 /* It's hard to recover because even if valid @implementation
28982 stuff is to follow, we can't compile it (or validate it) if
28983 we don't even know which class it refers to. Let's assume
28984 this was a stray '@implementation' token in the stream and
28989 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
28990 &is_class_extension
);
28992 /* We have either a class or a category on our hands. */
28994 objc_start_category_implementation (name
, categ
);
28997 objc_start_class_implementation (name
, super
);
28998 /* Handle instance variable declarations, if any. */
28999 cp_parser_objc_class_ivars (parser
);
29000 objc_continue_implementation ();
29003 cp_parser_objc_method_definition_list (parser
);
29006 /* Consume the @end token and finish off the implementation. */
29009 cp_parser_objc_end_implementation (cp_parser
* parser
)
29011 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
29012 objc_finish_implementation ();
29015 /* Parse an Objective-C declaration. */
29018 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
29020 /* Try to figure out what kind of declaration is present. */
29021 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29024 switch (kwd
->keyword
)
29029 error_at (kwd
->location
, "attributes may not be specified before"
29030 " the %<@%D%> Objective-C++ keyword",
29034 case RID_AT_IMPLEMENTATION
:
29035 warning_at (kwd
->location
, OPT_Wattributes
,
29036 "prefix attributes are ignored before %<@%D%>",
29043 switch (kwd
->keyword
)
29046 cp_parser_objc_alias_declaration (parser
);
29049 cp_parser_objc_class_declaration (parser
);
29051 case RID_AT_PROTOCOL
:
29052 cp_parser_objc_protocol_declaration (parser
, attributes
);
29054 case RID_AT_INTERFACE
:
29055 cp_parser_objc_class_interface (parser
, attributes
);
29057 case RID_AT_IMPLEMENTATION
:
29058 cp_parser_objc_class_implementation (parser
);
29061 cp_parser_objc_end_implementation (parser
);
29064 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
29066 cp_parser_skip_to_end_of_block_or_statement (parser
);
29070 /* Parse an Objective-C try-catch-finally statement.
29072 objc-try-catch-finally-stmt:
29073 @try compound-statement objc-catch-clause-seq [opt]
29074 objc-finally-clause [opt]
29076 objc-catch-clause-seq:
29077 objc-catch-clause objc-catch-clause-seq [opt]
29080 @catch ( objc-exception-declaration ) compound-statement
29082 objc-finally-clause:
29083 @finally compound-statement
29085 objc-exception-declaration:
29086 parameter-declaration
29089 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29093 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29094 for C. Keep them in sync. */
29097 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
29099 location_t location
;
29102 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
29103 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29104 objc_maybe_warn_exceptions (location
);
29105 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29106 node, lest it get absorbed into the surrounding block. */
29107 stmt
= push_stmt_list ();
29108 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
29109 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
29111 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
29113 cp_parameter_declarator
*parm
;
29114 tree parameter_declaration
= error_mark_node
;
29115 bool seen_open_paren
= false;
29117 cp_lexer_consume_token (parser
->lexer
);
29118 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29119 seen_open_paren
= true;
29120 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29122 /* We have "@catch (...)" (where the '...' are literally
29123 what is in the code). Skip the '...'.
29124 parameter_declaration is set to NULL_TREE, and
29125 objc_being_catch_clauses() knows that that means
29127 cp_lexer_consume_token (parser
->lexer
);
29128 parameter_declaration
= NULL_TREE
;
29132 /* We have "@catch (NSException *exception)" or something
29133 like that. Parse the parameter declaration. */
29134 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
29136 parameter_declaration
= error_mark_node
;
29138 parameter_declaration
= grokdeclarator (parm
->declarator
,
29139 &parm
->decl_specifiers
,
29140 PARM
, /*initialized=*/0,
29141 /*attrlist=*/NULL
);
29143 if (seen_open_paren
)
29144 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29147 /* If there was no open parenthesis, we are recovering from
29148 an error, and we are trying to figure out what mistake
29149 the user has made. */
29151 /* If there is an immediate closing parenthesis, the user
29152 probably forgot the opening one (ie, they typed "@catch
29153 NSException *e)". Parse the closing parenthesis and keep
29155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
29156 cp_lexer_consume_token (parser
->lexer
);
29158 /* If these is no immediate closing parenthesis, the user
29159 probably doesn't know that parenthesis are required at
29160 all (ie, they typed "@catch NSException *e"). So, just
29161 forget about the closing parenthesis and keep going. */
29163 objc_begin_catch_clause (parameter_declaration
);
29164 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
29165 objc_finish_catch_clause ();
29167 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
29169 cp_lexer_consume_token (parser
->lexer
);
29170 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29171 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29172 node, lest it get absorbed into the surrounding block. */
29173 stmt
= push_stmt_list ();
29174 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
29175 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
29178 return objc_finish_try_stmt ();
29181 /* Parse an Objective-C synchronized statement.
29183 objc-synchronized-stmt:
29184 @synchronized ( expression ) compound-statement
29186 Returns NULL_TREE. */
29189 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
29191 location_t location
;
29194 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
29196 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29197 objc_maybe_warn_exceptions (location
);
29198 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
29199 lock
= cp_parser_expression (parser
);
29200 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
29202 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29203 node, lest it get absorbed into the surrounding block. */
29204 stmt
= push_stmt_list ();
29205 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
29207 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
29210 /* Parse an Objective-C throw statement.
29213 @throw assignment-expression [opt] ;
29215 Returns a constructed '@throw' statement. */
29218 cp_parser_objc_throw_statement (cp_parser
*parser
)
29220 tree expr
= NULL_TREE
;
29221 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29223 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
29225 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29226 expr
= cp_parser_expression (parser
);
29228 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29230 return objc_build_throw_stmt (loc
, expr
);
29233 /* Parse an Objective-C statement. */
29236 cp_parser_objc_statement (cp_parser
* parser
)
29238 /* Try to figure out what kind of declaration is present. */
29239 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
29241 switch (kwd
->keyword
)
29244 return cp_parser_objc_try_catch_finally_statement (parser
);
29245 case RID_AT_SYNCHRONIZED
:
29246 return cp_parser_objc_synchronized_statement (parser
);
29248 return cp_parser_objc_throw_statement (parser
);
29250 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
29252 cp_parser_skip_to_end_of_block_or_statement (parser
);
29255 return error_mark_node
;
29258 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29259 look ahead to see if an objc keyword follows the attributes. This
29260 is to detect the use of prefix attributes on ObjC @interface and
29264 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
29266 cp_lexer_save_tokens (parser
->lexer
);
29267 *attrib
= cp_parser_attributes_opt (parser
);
29268 gcc_assert (*attrib
);
29269 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
29271 cp_lexer_commit_tokens (parser
->lexer
);
29274 cp_lexer_rollback_tokens (parser
->lexer
);
29278 /* This routine is a minimal replacement for
29279 c_parser_struct_declaration () used when parsing the list of
29280 types/names or ObjC++ properties. For example, when parsing the
29283 @property (readonly) int a, b, c;
29285 this function is responsible for parsing "int a, int b, int c" and
29286 returning the declarations as CHAIN of DECLs.
29288 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29289 similar parsing. */
29291 cp_parser_objc_struct_declaration (cp_parser
*parser
)
29293 tree decls
= NULL_TREE
;
29294 cp_decl_specifier_seq declspecs
;
29295 int decl_class_or_enum_p
;
29296 tree prefix_attributes
;
29298 cp_parser_decl_specifier_seq (parser
,
29299 CP_PARSER_FLAGS_NONE
,
29301 &decl_class_or_enum_p
);
29303 if (declspecs
.type
== error_mark_node
)
29304 return error_mark_node
;
29306 /* auto, register, static, extern, mutable. */
29307 if (declspecs
.storage_class
!= sc_none
)
29309 cp_parser_error (parser
, "invalid type for property");
29310 declspecs
.storage_class
= sc_none
;
29313 /* thread_local. */
29314 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
29316 cp_parser_error (parser
, "invalid type for property");
29317 declspecs
.locations
[ds_thread
] = 0;
29321 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
29323 cp_parser_error (parser
, "invalid type for property");
29324 declspecs
.locations
[ds_typedef
] = 0;
29327 prefix_attributes
= declspecs
.attributes
;
29328 declspecs
.attributes
= NULL_TREE
;
29330 /* Keep going until we hit the `;' at the end of the declaration. */
29331 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29333 tree attributes
, first_attribute
, decl
;
29334 cp_declarator
*declarator
;
29337 /* Parse the declarator. */
29338 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
29339 NULL
, NULL
, false, false);
29341 /* Look for attributes that apply to the ivar. */
29342 attributes
= cp_parser_attributes_opt (parser
);
29343 /* Remember which attributes are prefix attributes and
29345 first_attribute
= attributes
;
29346 /* Combine the attributes. */
29347 attributes
= chainon (prefix_attributes
, attributes
);
29349 decl
= grokfield (declarator
, &declspecs
,
29350 NULL_TREE
, /*init_const_expr_p=*/false,
29351 NULL_TREE
, attributes
);
29353 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
29354 return error_mark_node
;
29356 /* Reset PREFIX_ATTRIBUTES. */
29357 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
29358 attributes
= TREE_CHAIN (attributes
);
29360 TREE_CHAIN (attributes
) = NULL_TREE
;
29362 DECL_CHAIN (decl
) = decls
;
29365 token
= cp_lexer_peek_token (parser
->lexer
);
29366 if (token
->type
== CPP_COMMA
)
29368 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
29377 /* Parse an Objective-C @property declaration. The syntax is:
29379 objc-property-declaration:
29380 '@property' objc-property-attributes[opt] struct-declaration ;
29382 objc-property-attributes:
29383 '(' objc-property-attribute-list ')'
29385 objc-property-attribute-list:
29386 objc-property-attribute
29387 objc-property-attribute-list, objc-property-attribute
29389 objc-property-attribute
29390 'getter' = identifier
29391 'setter' = identifier
29400 @property NSString *name;
29401 @property (readonly) id object;
29402 @property (retain, nonatomic, getter=getTheName) id name;
29403 @property int a, b, c;
29405 PS: This function is identical to
29406 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29408 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
29410 /* The following variables hold the attributes of the properties as
29411 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29412 seen. When we see an attribute, we set them to 'true' (if they
29413 are boolean properties) or to the identifier (if they have an
29414 argument, ie, for getter and setter). Note that here we only
29415 parse the list of attributes, check the syntax and accumulate the
29416 attributes that we find. objc_add_property_declaration() will
29417 then process the information. */
29418 bool property_assign
= false;
29419 bool property_copy
= false;
29420 tree property_getter_ident
= NULL_TREE
;
29421 bool property_nonatomic
= false;
29422 bool property_readonly
= false;
29423 bool property_readwrite
= false;
29424 bool property_retain
= false;
29425 tree property_setter_ident
= NULL_TREE
;
29427 /* 'properties' is the list of properties that we read. Usually a
29428 single one, but maybe more (eg, in "@property int a, b, c;" there
29433 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29435 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
29437 /* Parse the optional attribute list... */
29438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29441 cp_lexer_consume_token (parser
->lexer
);
29445 bool syntax_error
= false;
29446 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29449 if (token
->type
!= CPP_NAME
)
29451 cp_parser_error (parser
, "expected identifier");
29454 keyword
= C_RID_CODE (token
->u
.value
);
29455 cp_lexer_consume_token (parser
->lexer
);
29458 case RID_ASSIGN
: property_assign
= true; break;
29459 case RID_COPY
: property_copy
= true; break;
29460 case RID_NONATOMIC
: property_nonatomic
= true; break;
29461 case RID_READONLY
: property_readonly
= true; break;
29462 case RID_READWRITE
: property_readwrite
= true; break;
29463 case RID_RETAIN
: property_retain
= true; break;
29467 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
29469 if (keyword
== RID_GETTER
)
29470 cp_parser_error (parser
,
29471 "missing %<=%> (after %<getter%> attribute)");
29473 cp_parser_error (parser
,
29474 "missing %<=%> (after %<setter%> attribute)");
29475 syntax_error
= true;
29478 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
29479 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
29481 cp_parser_error (parser
, "expected identifier");
29482 syntax_error
= true;
29485 if (keyword
== RID_SETTER
)
29487 if (property_setter_ident
!= NULL_TREE
)
29489 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
29490 cp_lexer_consume_token (parser
->lexer
);
29493 property_setter_ident
= cp_parser_objc_selector (parser
);
29494 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
29495 cp_parser_error (parser
, "setter name must terminate with %<:%>");
29497 cp_lexer_consume_token (parser
->lexer
);
29501 if (property_getter_ident
!= NULL_TREE
)
29503 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
29504 cp_lexer_consume_token (parser
->lexer
);
29507 property_getter_ident
= cp_parser_objc_selector (parser
);
29511 cp_parser_error (parser
, "unknown property attribute");
29512 syntax_error
= true;
29519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29520 cp_lexer_consume_token (parser
->lexer
);
29525 /* FIXME: "@property (setter, assign);" will generate a spurious
29526 "error: expected ‘)’ before ‘,’ token". This is because
29527 cp_parser_require, unlike the C counterpart, will produce an
29528 error even if we are in error recovery. */
29529 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29531 cp_parser_skip_to_closing_parenthesis (parser
,
29532 /*recovering=*/true,
29533 /*or_comma=*/false,
29534 /*consume_paren=*/true);
29538 /* ... and the property declaration(s). */
29539 properties
= cp_parser_objc_struct_declaration (parser
);
29541 if (properties
== error_mark_node
)
29543 cp_parser_skip_to_end_of_statement (parser
);
29544 /* If the next token is now a `;', consume it. */
29545 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29546 cp_lexer_consume_token (parser
->lexer
);
29550 if (properties
== NULL_TREE
)
29551 cp_parser_error (parser
, "expected identifier");
29554 /* Comma-separated properties are chained together in
29555 reverse order; add them one by one. */
29556 properties
= nreverse (properties
);
29558 for (; properties
; properties
= TREE_CHAIN (properties
))
29559 objc_add_property_declaration (loc
, copy_node (properties
),
29560 property_readonly
, property_readwrite
,
29561 property_assign
, property_retain
,
29562 property_copy
, property_nonatomic
,
29563 property_getter_ident
, property_setter_ident
);
29566 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29569 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29571 objc-synthesize-declaration:
29572 @synthesize objc-synthesize-identifier-list ;
29574 objc-synthesize-identifier-list:
29575 objc-synthesize-identifier
29576 objc-synthesize-identifier-list, objc-synthesize-identifier
29578 objc-synthesize-identifier
29580 identifier = identifier
29583 @synthesize MyProperty;
29584 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29586 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29587 for C. Keep them in sync.
29590 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
29592 tree list
= NULL_TREE
;
29594 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29596 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
29599 tree property
, ivar
;
29600 property
= cp_parser_identifier (parser
);
29601 if (property
== error_mark_node
)
29603 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
29608 cp_lexer_consume_token (parser
->lexer
);
29609 ivar
= cp_parser_identifier (parser
);
29610 if (ivar
== error_mark_node
)
29612 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29618 list
= chainon (list
, build_tree_list (ivar
, property
));
29619 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29620 cp_lexer_consume_token (parser
->lexer
);
29624 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29625 objc_add_synthesize_declaration (loc
, list
);
29628 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29630 objc-dynamic-declaration:
29631 @dynamic identifier-list ;
29634 @dynamic MyProperty;
29635 @dynamic MyProperty, AnotherProperty;
29637 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29638 for C. Keep them in sync.
29641 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
29643 tree list
= NULL_TREE
;
29645 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29647 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
29651 property
= cp_parser_identifier (parser
);
29652 if (property
== error_mark_node
)
29654 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29657 list
= chainon (list
, build_tree_list (NULL
, property
));
29658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29659 cp_lexer_consume_token (parser
->lexer
);
29663 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29664 objc_add_dynamic_declaration (loc
, list
);
29668 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29670 /* Returns name of the next clause.
29671 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29672 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29673 returned and the token is consumed. */
29675 static pragma_omp_clause
29676 cp_parser_omp_clause_name (cp_parser
*parser
)
29678 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
29680 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
29681 result
= PRAGMA_OACC_CLAUSE_AUTO
;
29682 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
29683 result
= PRAGMA_OMP_CLAUSE_IF
;
29684 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
29685 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
29686 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
29687 result
= PRAGMA_OACC_CLAUSE_DELETE
;
29688 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
29689 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
29690 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29691 result
= PRAGMA_OMP_CLAUSE_FOR
;
29692 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29694 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29695 const char *p
= IDENTIFIER_POINTER (id
);
29700 if (!strcmp ("aligned", p
))
29701 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
29702 else if (!strcmp ("async", p
))
29703 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
29706 if (!strcmp ("collapse", p
))
29707 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
29708 else if (!strcmp ("copy", p
))
29709 result
= PRAGMA_OACC_CLAUSE_COPY
;
29710 else if (!strcmp ("copyin", p
))
29711 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
29712 else if (!strcmp ("copyout", p
))
29713 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
29714 else if (!strcmp ("copyprivate", p
))
29715 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
29716 else if (!strcmp ("create", p
))
29717 result
= PRAGMA_OACC_CLAUSE_CREATE
;
29720 if (!strcmp ("defaultmap", p
))
29721 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
29722 else if (!strcmp ("depend", p
))
29723 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
29724 else if (!strcmp ("device", p
))
29725 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
29726 else if (!strcmp ("deviceptr", p
))
29727 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
29728 else if (!strcmp ("device_resident", p
))
29729 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
29730 else if (!strcmp ("dist_schedule", p
))
29731 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
29734 if (!strcmp ("final", p
))
29735 result
= PRAGMA_OMP_CLAUSE_FINAL
;
29736 else if (!strcmp ("firstprivate", p
))
29737 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
29738 else if (!strcmp ("from", p
))
29739 result
= PRAGMA_OMP_CLAUSE_FROM
;
29742 if (!strcmp ("gang", p
))
29743 result
= PRAGMA_OACC_CLAUSE_GANG
;
29744 else if (!strcmp ("grainsize", p
))
29745 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
29748 if (!strcmp ("hint", p
))
29749 result
= PRAGMA_OMP_CLAUSE_HINT
;
29750 else if (!strcmp ("host", p
))
29751 result
= PRAGMA_OACC_CLAUSE_HOST
;
29754 if (!strcmp ("inbranch", p
))
29755 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
29756 else if (!strcmp ("independent", p
))
29757 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
29758 else if (!strcmp ("is_device_ptr", p
))
29759 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
29762 if (!strcmp ("lastprivate", p
))
29763 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
29764 else if (!strcmp ("linear", p
))
29765 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
29766 else if (!strcmp ("link", p
))
29767 result
= PRAGMA_OMP_CLAUSE_LINK
;
29770 if (!strcmp ("map", p
))
29771 result
= PRAGMA_OMP_CLAUSE_MAP
;
29772 else if (!strcmp ("mergeable", p
))
29773 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
29774 else if (flag_cilkplus
&& !strcmp ("mask", p
))
29775 result
= PRAGMA_CILK_CLAUSE_MASK
;
29778 if (!strcmp ("nogroup", p
))
29779 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
29780 else if (!strcmp ("notinbranch", p
))
29781 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
29782 else if (!strcmp ("nowait", p
))
29783 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
29784 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
29785 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
29786 else if (!strcmp ("num_gangs", p
))
29787 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
29788 else if (!strcmp ("num_tasks", p
))
29789 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
29790 else if (!strcmp ("num_teams", p
))
29791 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
29792 else if (!strcmp ("num_threads", p
))
29793 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
29794 else if (!strcmp ("num_workers", p
))
29795 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
29798 if (!strcmp ("ordered", p
))
29799 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
29802 if (!strcmp ("parallel", p
))
29803 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
29804 else if (!strcmp ("present", p
))
29805 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
29806 else if (!strcmp ("present_or_copy", p
)
29807 || !strcmp ("pcopy", p
))
29808 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
29809 else if (!strcmp ("present_or_copyin", p
)
29810 || !strcmp ("pcopyin", p
))
29811 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
29812 else if (!strcmp ("present_or_copyout", p
)
29813 || !strcmp ("pcopyout", p
))
29814 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
29815 else if (!strcmp ("present_or_create", p
)
29816 || !strcmp ("pcreate", p
))
29817 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
29818 else if (!strcmp ("priority", p
))
29819 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
29820 else if (!strcmp ("proc_bind", p
))
29821 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
29824 if (!strcmp ("reduction", p
))
29825 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
29828 if (!strcmp ("safelen", p
))
29829 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
29830 else if (!strcmp ("schedule", p
))
29831 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
29832 else if (!strcmp ("sections", p
))
29833 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
29834 else if (!strcmp ("self", p
))
29835 result
= PRAGMA_OACC_CLAUSE_SELF
;
29836 else if (!strcmp ("seq", p
))
29837 result
= PRAGMA_OACC_CLAUSE_SEQ
;
29838 else if (!strcmp ("shared", p
))
29839 result
= PRAGMA_OMP_CLAUSE_SHARED
;
29840 else if (!strcmp ("simd", p
))
29841 result
= PRAGMA_OMP_CLAUSE_SIMD
;
29842 else if (!strcmp ("simdlen", p
))
29843 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
29846 if (!strcmp ("taskgroup", p
))
29847 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
29848 else if (!strcmp ("thread_limit", p
))
29849 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
29850 else if (!strcmp ("threads", p
))
29851 result
= PRAGMA_OMP_CLAUSE_THREADS
;
29852 else if (!strcmp ("tile", p
))
29853 result
= PRAGMA_OACC_CLAUSE_TILE
;
29854 else if (!strcmp ("to", p
))
29855 result
= PRAGMA_OMP_CLAUSE_TO
;
29858 if (!strcmp ("uniform", p
))
29859 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
29860 else if (!strcmp ("untied", p
))
29861 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
29862 else if (!strcmp ("use_device", p
))
29863 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
29864 else if (!strcmp ("use_device_ptr", p
))
29865 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
29868 if (!strcmp ("vector", p
))
29869 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
29870 else if (!strcmp ("vector_length", p
))
29871 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
29872 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
29873 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
29876 if (!strcmp ("wait", p
))
29877 result
= PRAGMA_OACC_CLAUSE_WAIT
;
29878 else if (!strcmp ("worker", p
))
29879 result
= PRAGMA_OACC_CLAUSE_WORKER
;
29884 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
29885 cp_lexer_consume_token (parser
->lexer
);
29890 /* Validate that a clause of the given type does not already exist. */
29893 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
29894 const char *name
, location_t location
)
29898 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29899 if (OMP_CLAUSE_CODE (c
) == code
)
29901 error_at (location
, "too many %qs clauses", name
);
29909 variable-list , identifier
29911 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29912 colon). An opening parenthesis will have been consumed by the caller.
29914 If KIND is nonzero, create the appropriate node and install the decl
29915 in OMP_CLAUSE_DECL and add the node to the head of the list.
29917 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29918 return the list created.
29920 COLON can be NULL if only closing parenthesis should end the list,
29921 or pointer to bool which will receive false if the list is terminated
29922 by closing parenthesis or true if the list is terminated by colon. */
29925 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
29926 tree list
, bool *colon
)
29929 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
29932 parser
->colon_corrects_to_scope_p
= false;
29939 token
= cp_lexer_peek_token (parser
->lexer
);
29941 && current_class_ptr
29942 && cp_parser_is_keyword (token
, RID_THIS
))
29944 decl
= finish_this_expr ();
29945 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
29946 || CONVERT_EXPR_P (decl
))
29947 decl
= TREE_OPERAND (decl
, 0);
29948 cp_lexer_consume_token (parser
->lexer
);
29952 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
29953 /*check_dependency_p=*/true,
29954 /*template_p=*/NULL
,
29955 /*declarator_p=*/false,
29956 /*optional_p=*/false);
29957 if (name
== error_mark_node
)
29960 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
29961 if (decl
== error_mark_node
)
29962 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
29965 if (decl
== error_mark_node
)
29967 else if (kind
!= 0)
29971 case OMP_CLAUSE__CACHE_
:
29972 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
29974 error_at (token
->location
, "expected %<[%>");
29975 decl
= error_mark_node
;
29979 case OMP_CLAUSE_MAP
:
29980 case OMP_CLAUSE_FROM
:
29981 case OMP_CLAUSE_TO
:
29982 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
29985 = cp_lexer_peek_token (parser
->lexer
)->location
;
29986 cp_id_kind idk
= CP_ID_KIND_NONE
;
29987 cp_lexer_consume_token (parser
->lexer
);
29989 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
29994 case OMP_CLAUSE_DEPEND
:
29995 case OMP_CLAUSE_REDUCTION
:
29996 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
29998 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
30000 parser
->colon_corrects_to_scope_p
= false;
30001 cp_lexer_consume_token (parser
->lexer
);
30002 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30003 low_bound
= cp_parser_expression (parser
);
30005 parser
->colon_corrects_to_scope_p
30006 = saved_colon_corrects_to_scope_p
;
30007 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
30008 length
= integer_one_node
;
30011 /* Look for `:'. */
30012 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30014 if (!cp_lexer_next_token_is (parser
->lexer
,
30016 length
= cp_parser_expression (parser
);
30018 /* Look for the closing `]'. */
30019 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
30023 if (kind
== OMP_CLAUSE__CACHE_
)
30025 if (TREE_CODE (low_bound
) != INTEGER_CST
30026 && !TREE_READONLY (low_bound
))
30028 error_at (token
->location
,
30029 "%qD is not a constant", low_bound
);
30030 decl
= error_mark_node
;
30033 if (TREE_CODE (length
) != INTEGER_CST
30034 && !TREE_READONLY (length
))
30036 error_at (token
->location
,
30037 "%qD is not a constant", length
);
30038 decl
= error_mark_node
;
30042 decl
= tree_cons (low_bound
, length
, decl
);
30049 tree u
= build_omp_clause (token
->location
, kind
);
30050 OMP_CLAUSE_DECL (u
) = decl
;
30051 OMP_CLAUSE_CHAIN (u
) = list
;
30055 list
= tree_cons (decl
, NULL_TREE
, list
);
30058 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
30060 cp_lexer_consume_token (parser
->lexer
);
30064 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30066 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30069 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30073 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30077 /* Try to resync to an unnested comma. Copied from
30078 cp_parser_parenthesized_expression_list. */
30081 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30082 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
30083 /*recovering=*/true,
30085 /*consume_paren=*/true);
30093 /* Similarly, but expect leading and trailing parenthesis. This is a very
30094 common case for omp clauses. */
30097 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
30099 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30100 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
30105 copy ( variable-list )
30106 copyin ( variable-list )
30107 copyout ( variable-list )
30108 create ( variable-list )
30109 delete ( variable-list )
30110 present ( variable-list )
30111 present_or_copy ( variable-list )
30112 pcopy ( variable-list )
30113 present_or_copyin ( variable-list )
30114 pcopyin ( variable-list )
30115 present_or_copyout ( variable-list )
30116 pcopyout ( variable-list )
30117 present_or_create ( variable-list )
30118 pcreate ( variable-list ) */
30121 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
30124 enum gomp_map_kind kind
;
30127 case PRAGMA_OACC_CLAUSE_COPY
:
30128 kind
= GOMP_MAP_FORCE_TOFROM
;
30130 case PRAGMA_OACC_CLAUSE_COPYIN
:
30131 kind
= GOMP_MAP_FORCE_TO
;
30133 case PRAGMA_OACC_CLAUSE_COPYOUT
:
30134 kind
= GOMP_MAP_FORCE_FROM
;
30136 case PRAGMA_OACC_CLAUSE_CREATE
:
30137 kind
= GOMP_MAP_FORCE_ALLOC
;
30139 case PRAGMA_OACC_CLAUSE_DELETE
:
30140 kind
= GOMP_MAP_DELETE
;
30142 case PRAGMA_OACC_CLAUSE_DEVICE
:
30143 kind
= GOMP_MAP_FORCE_TO
;
30145 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
30146 kind
= GOMP_MAP_DEVICE_RESIDENT
;
30148 case PRAGMA_OACC_CLAUSE_HOST
:
30149 case PRAGMA_OACC_CLAUSE_SELF
:
30150 kind
= GOMP_MAP_FORCE_FROM
;
30152 case PRAGMA_OACC_CLAUSE_LINK
:
30153 kind
= GOMP_MAP_LINK
;
30155 case PRAGMA_OACC_CLAUSE_PRESENT
:
30156 kind
= GOMP_MAP_FORCE_PRESENT
;
30158 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
30159 kind
= GOMP_MAP_TOFROM
;
30161 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
30162 kind
= GOMP_MAP_TO
;
30164 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
30165 kind
= GOMP_MAP_FROM
;
30167 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
30168 kind
= GOMP_MAP_ALLOC
;
30171 gcc_unreachable ();
30174 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
30176 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30177 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
30183 deviceptr ( variable-list ) */
30186 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
30188 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30191 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30192 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30193 variable-list must only allow for pointer variables. */
30194 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30195 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
30197 tree v
= TREE_PURPOSE (t
);
30198 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
30199 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
30200 OMP_CLAUSE_DECL (u
) = v
;
30201 OMP_CLAUSE_CHAIN (u
) = list
;
30215 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
30216 enum omp_clause_code code
,
30217 tree list
, location_t location
)
30219 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
30220 tree c
= build_omp_clause (location
, code
);
30221 OMP_CLAUSE_CHAIN (c
) = list
;
30226 num_gangs ( expression )
30227 num_workers ( expression )
30228 vector_length ( expression ) */
30231 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
30232 const char *str
, tree list
)
30234 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30236 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30239 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
30241 if (t
== error_mark_node
30242 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30244 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30245 /*or_comma=*/false,
30246 /*consume_paren=*/true);
30250 check_no_duplicate_clause (list
, code
, str
, loc
);
30252 tree c
= build_omp_clause (loc
, code
);
30253 OMP_CLAUSE_OPERAND (c
, 0) = t
;
30254 OMP_CLAUSE_CHAIN (c
) = list
;
30260 gang [( gang-arg-list )]
30261 worker [( [num:] int-expr )]
30262 vector [( [length:] int-expr )]
30264 where gang-arg is one of:
30269 and size-expr may be:
30276 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
30277 const char *str
, tree list
)
30279 const char *id
= "num";
30280 cp_lexer
*lexer
= parser
->lexer
;
30281 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
30282 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
30284 if (kind
== OMP_CLAUSE_VECTOR
)
30287 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
30289 cp_lexer_consume_token (lexer
);
30293 cp_token
*next
= cp_lexer_peek_token (lexer
);
30296 /* Gang static argument. */
30297 if (kind
== OMP_CLAUSE_GANG
30298 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
30300 cp_lexer_consume_token (lexer
);
30302 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30303 goto cleanup_error
;
30306 if (ops
[idx
] != NULL
)
30308 cp_parser_error (parser
, "too many %<static%> arguments");
30309 goto cleanup_error
;
30312 /* Check for the '*' argument. */
30313 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
30314 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
30315 || cp_lexer_nth_token_is (parser
->lexer
, 2,
30318 cp_lexer_consume_token (lexer
);
30319 ops
[idx
] = integer_minus_one_node
;
30321 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
30323 cp_lexer_consume_token (lexer
);
30329 /* Worker num: argument and vector length: arguments. */
30330 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
30331 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
30332 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
30334 cp_lexer_consume_token (lexer
); /* id */
30335 cp_lexer_consume_token (lexer
); /* ':' */
30338 /* Now collect the actual argument. */
30339 if (ops
[idx
] != NULL_TREE
)
30341 cp_parser_error (parser
, "unexpected argument");
30342 goto cleanup_error
;
30345 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
30347 if (expr
== error_mark_node
)
30348 goto cleanup_error
;
30350 mark_exp_read (expr
);
30353 if (kind
== OMP_CLAUSE_GANG
30354 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
30356 cp_lexer_consume_token (lexer
);
30363 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30364 goto cleanup_error
;
30367 check_no_duplicate_clause (list
, kind
, str
, loc
);
30369 c
= build_omp_clause (loc
, kind
);
30372 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
30374 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
30375 OMP_CLAUSE_CHAIN (c
) = list
;
30380 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
30385 tile ( size-expr-list ) */
30388 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
30390 tree c
, expr
= error_mark_node
;
30391 tree tile
= NULL_TREE
;
30393 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
30395 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
30401 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
30402 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
30404 cp_lexer_consume_token (parser
->lexer
);
30405 expr
= integer_minus_one_node
;
30408 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
30410 if (expr
== error_mark_node
)
30413 tile
= tree_cons (NULL_TREE
, expr
, tile
);
30415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30416 cp_lexer_consume_token (parser
->lexer
);
30418 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
30420 /* Consume the trailing ')'. */
30421 cp_lexer_consume_token (parser
->lexer
);
30423 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
30424 tile
= nreverse (tile
);
30425 OMP_CLAUSE_TILE_LIST (c
) = tile
;
30426 OMP_CLAUSE_CHAIN (c
) = list
;
30431 Parse wait clause or directive parameters. */
30434 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
30436 vec
<tree
, va_gc
> *args
;
30439 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
30441 /*allow_expansion_p=*/true,
30442 /*non_constant_p=*/NULL
);
30444 if (args
== NULL
|| args
->length () == 0)
30446 cp_parser_error (parser
, "expected integer expression before ')'");
30448 release_tree_vector (args
);
30452 args_tree
= build_tree_list_vec (args
);
30454 release_tree_vector (args
);
30456 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
30458 tree targ
= TREE_VALUE (t
);
30460 if (targ
!= error_mark_node
)
30462 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
30463 error ("%<wait%> expression must be integral");
30466 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
30468 mark_rvalue_use (targ
);
30469 OMP_CLAUSE_DECL (c
) = targ
;
30470 OMP_CLAUSE_CHAIN (c
) = list
;
30480 wait ( int-expr-list ) */
30483 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
30485 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
30487 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
30490 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
30496 collapse ( constant-expression ) */
30499 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
30505 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30506 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30509 num
= cp_parser_constant_expression (parser
);
30511 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30512 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30513 /*or_comma=*/false,
30514 /*consume_paren=*/true);
30516 if (num
== error_mark_node
)
30518 num
= fold_non_dependent_expr (num
);
30519 if (!tree_fits_shwi_p (num
)
30520 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
30521 || (n
= tree_to_shwi (num
)) <= 0
30524 error_at (loc
, "collapse argument needs positive constant integer expression");
30528 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
30529 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
30530 OMP_CLAUSE_CHAIN (c
) = list
;
30531 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
30537 default ( shared | none )
30543 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
30544 location_t location
, bool is_oacc
)
30546 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
30549 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30553 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30554 const char *p
= IDENTIFIER_POINTER (id
);
30559 if (strcmp ("none", p
) != 0)
30561 kind
= OMP_CLAUSE_DEFAULT_NONE
;
30565 if (strcmp ("shared", p
) != 0 || is_oacc
)
30567 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
30574 cp_lexer_consume_token (parser
->lexer
);
30580 cp_parser_error (parser
, "expected %<none%>");
30582 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
30585 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30586 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30587 /*or_comma=*/false,
30588 /*consume_paren=*/true);
30590 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
30593 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
30594 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
30595 OMP_CLAUSE_CHAIN (c
) = list
;
30596 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
30602 final ( expression ) */
30605 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
30609 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30612 t
= cp_parser_condition (parser
);
30614 if (t
== error_mark_node
30615 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30616 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30617 /*or_comma=*/false,
30618 /*consume_paren=*/true);
30620 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
30622 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
30623 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
30624 OMP_CLAUSE_CHAIN (c
) = list
;
30633 if ( directive-name-modifier : expression )
30635 directive-name-modifier:
30636 parallel | task | taskloop | target data | target | target update
30637 | target enter data | target exit data */
30640 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
30644 enum tree_code if_modifier
= ERROR_MARK
;
30646 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30649 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30651 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30652 const char *p
= IDENTIFIER_POINTER (id
);
30655 if (strcmp ("parallel", p
) == 0)
30656 if_modifier
= OMP_PARALLEL
;
30657 else if (strcmp ("task", p
) == 0)
30658 if_modifier
= OMP_TASK
;
30659 else if (strcmp ("taskloop", p
) == 0)
30660 if_modifier
= OMP_TASKLOOP
;
30661 else if (strcmp ("target", p
) == 0)
30663 if_modifier
= OMP_TARGET
;
30664 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
30666 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
30667 p
= IDENTIFIER_POINTER (id
);
30668 if (strcmp ("data", p
) == 0)
30669 if_modifier
= OMP_TARGET_DATA
;
30670 else if (strcmp ("update", p
) == 0)
30671 if_modifier
= OMP_TARGET_UPDATE
;
30672 else if (strcmp ("enter", p
) == 0)
30673 if_modifier
= OMP_TARGET_ENTER_DATA
;
30674 else if (strcmp ("exit", p
) == 0)
30675 if_modifier
= OMP_TARGET_EXIT_DATA
;
30676 if (if_modifier
!= OMP_TARGET
)
30681 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
30682 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
30684 if_modifier
= ERROR_MARK
;
30686 if (if_modifier
== OMP_TARGET_ENTER_DATA
30687 || if_modifier
== OMP_TARGET_EXIT_DATA
)
30689 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
30691 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
30692 p
= IDENTIFIER_POINTER (id
);
30693 if (strcmp ("data", p
) == 0)
30699 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
30700 error_at (loc
, "expected %<data%>");
30701 if_modifier
= ERROR_MARK
;
30706 if (if_modifier
!= ERROR_MARK
)
30708 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
30711 cp_lexer_consume_token (parser
->lexer
);
30718 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
30719 error_at (loc
, "expected %<:%>");
30721 if_modifier
= ERROR_MARK
;
30726 t
= cp_parser_condition (parser
);
30728 if (t
== error_mark_node
30729 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30730 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30731 /*or_comma=*/false,
30732 /*consume_paren=*/true);
30734 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30735 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
30737 if (if_modifier
!= ERROR_MARK
30738 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30740 const char *p
= NULL
;
30741 switch (if_modifier
)
30743 case OMP_PARALLEL
: p
= "parallel"; break;
30744 case OMP_TASK
: p
= "task"; break;
30745 case OMP_TASKLOOP
: p
= "taskloop"; break;
30746 case OMP_TARGET_DATA
: p
= "target data"; break;
30747 case OMP_TARGET
: p
= "target"; break;
30748 case OMP_TARGET_UPDATE
: p
= "target update"; break;
30749 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
30750 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
30751 default: gcc_unreachable ();
30753 error_at (location
, "too many %<if%> clauses with %qs modifier",
30757 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30760 error_at (location
, "too many %<if%> clauses");
30762 error_at (location
, "too many %<if%> clauses without modifier");
30765 else if (if_modifier
== ERROR_MARK
30766 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
30768 error_at (location
, "if any %<if%> clause has modifier, then all "
30769 "%<if%> clauses have to use modifier");
30774 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
30775 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
30776 OMP_CLAUSE_IF_EXPR (c
) = t
;
30777 OMP_CLAUSE_CHAIN (c
) = list
;
30786 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
30787 tree list
, location_t location
)
30791 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
30794 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
30795 OMP_CLAUSE_CHAIN (c
) = list
;
30803 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
30804 tree list
, location_t location
)
30808 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
30810 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
30811 OMP_CLAUSE_CHAIN (c
) = list
;
30816 num_threads ( expression ) */
30819 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
30820 location_t location
)
30824 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30827 t
= cp_parser_expression (parser
);
30829 if (t
== error_mark_node
30830 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30831 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30832 /*or_comma=*/false,
30833 /*consume_paren=*/true);
30835 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
30836 "num_threads", location
);
30838 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
30839 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
30840 OMP_CLAUSE_CHAIN (c
) = list
;
30846 num_tasks ( expression ) */
30849 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
30850 location_t location
)
30854 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30857 t
= cp_parser_expression (parser
);
30859 if (t
== error_mark_node
30860 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30861 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30862 /*or_comma=*/false,
30863 /*consume_paren=*/true);
30865 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
30866 "num_tasks", location
);
30868 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
30869 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
30870 OMP_CLAUSE_CHAIN (c
) = list
;
30876 grainsize ( expression ) */
30879 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
30880 location_t location
)
30884 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30887 t
= cp_parser_expression (parser
);
30889 if (t
== error_mark_node
30890 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30891 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30892 /*or_comma=*/false,
30893 /*consume_paren=*/true);
30895 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
30896 "grainsize", location
);
30898 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
30899 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
30900 OMP_CLAUSE_CHAIN (c
) = list
;
30906 priority ( expression ) */
30909 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
30910 location_t location
)
30914 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30917 t
= cp_parser_expression (parser
);
30919 if (t
== error_mark_node
30920 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30921 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30922 /*or_comma=*/false,
30923 /*consume_paren=*/true);
30925 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
30926 "priority", location
);
30928 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
30929 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
30930 OMP_CLAUSE_CHAIN (c
) = list
;
30936 hint ( expression ) */
30939 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
30940 location_t location
)
30944 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30947 t
= cp_parser_expression (parser
);
30949 if (t
== error_mark_node
30950 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30951 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30952 /*or_comma=*/false,
30953 /*consume_paren=*/true);
30955 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
30957 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
30958 OMP_CLAUSE_HINT_EXPR (c
) = t
;
30959 OMP_CLAUSE_CHAIN (c
) = list
;
30965 defaultmap ( tofrom : scalar ) */
30968 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
30969 location_t location
)
30974 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30977 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30979 cp_parser_error (parser
, "expected %<tofrom%>");
30982 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30983 p
= IDENTIFIER_POINTER (id
);
30984 if (strcmp (p
, "tofrom") != 0)
30986 cp_parser_error (parser
, "expected %<tofrom%>");
30989 cp_lexer_consume_token (parser
->lexer
);
30990 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30993 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30995 cp_parser_error (parser
, "expected %<scalar%>");
30998 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30999 p
= IDENTIFIER_POINTER (id
);
31000 if (strcmp (p
, "scalar") != 0)
31002 cp_parser_error (parser
, "expected %<scalar%>");
31005 cp_lexer_consume_token (parser
->lexer
);
31006 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31009 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
31012 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
31013 OMP_CLAUSE_CHAIN (c
) = list
;
31017 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31018 /*or_comma=*/false,
31019 /*consume_paren=*/true);
31027 ordered ( constant-expression ) */
31030 cp_parser_omp_clause_ordered (cp_parser
*parser
,
31031 tree list
, location_t location
)
31033 tree c
, num
= NULL_TREE
;
31036 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
31037 "ordered", location
);
31039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31041 cp_lexer_consume_token (parser
->lexer
);
31043 num
= cp_parser_constant_expression (parser
);
31045 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31046 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31047 /*or_comma=*/false,
31048 /*consume_paren=*/true);
31050 if (num
== error_mark_node
)
31052 num
= fold_non_dependent_expr (num
);
31053 if (!tree_fits_shwi_p (num
)
31054 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
31055 || (n
= tree_to_shwi (num
)) <= 0
31058 error_at (location
,
31059 "ordered argument needs positive constant integer "
31065 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
31066 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
31067 OMP_CLAUSE_CHAIN (c
) = list
;
31072 reduction ( reduction-operator : variable-list )
31074 reduction-operator:
31075 One of: + * - & ^ | && ||
31079 reduction-operator:
31080 One of: + * - & ^ | && || min max
31084 reduction-operator:
31085 One of: + * - & ^ | && ||
31089 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
31091 enum tree_code code
= ERROR_MARK
;
31092 tree nlist
, c
, id
= NULL_TREE
;
31094 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31097 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31099 case CPP_PLUS
: code
= PLUS_EXPR
; break;
31100 case CPP_MULT
: code
= MULT_EXPR
; break;
31101 case CPP_MINUS
: code
= MINUS_EXPR
; break;
31102 case CPP_AND
: code
= BIT_AND_EXPR
; break;
31103 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
31104 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
31105 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
31106 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
31110 if (code
!= ERROR_MARK
)
31111 cp_lexer_consume_token (parser
->lexer
);
31114 bool saved_colon_corrects_to_scope_p
;
31115 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31116 parser
->colon_corrects_to_scope_p
= false;
31117 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
31118 /*check_dependency_p=*/true,
31119 /*template_p=*/NULL
,
31120 /*declarator_p=*/false,
31121 /*optional_p=*/false);
31122 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31123 if (identifier_p (id
))
31125 const char *p
= IDENTIFIER_POINTER (id
);
31127 if (strcmp (p
, "min") == 0)
31129 else if (strcmp (p
, "max") == 0)
31131 else if (id
== ansi_opname (PLUS_EXPR
))
31133 else if (id
== ansi_opname (MULT_EXPR
))
31135 else if (id
== ansi_opname (MINUS_EXPR
))
31137 else if (id
== ansi_opname (BIT_AND_EXPR
))
31138 code
= BIT_AND_EXPR
;
31139 else if (id
== ansi_opname (BIT_IOR_EXPR
))
31140 code
= BIT_IOR_EXPR
;
31141 else if (id
== ansi_opname (BIT_XOR_EXPR
))
31142 code
= BIT_XOR_EXPR
;
31143 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
31144 code
= TRUTH_ANDIF_EXPR
;
31145 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
31146 code
= TRUTH_ORIF_EXPR
;
31147 id
= omp_reduction_id (code
, id
, NULL_TREE
);
31148 tree scope
= parser
->scope
;
31150 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
31151 parser
->scope
= NULL_TREE
;
31152 parser
->qualifying_scope
= NULL_TREE
;
31153 parser
->object_scope
= NULL_TREE
;
31157 error ("invalid reduction-identifier");
31159 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31160 /*or_comma=*/false,
31161 /*consume_paren=*/true);
31166 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31169 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
31171 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31173 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
31174 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
31181 schedule ( schedule-kind )
31182 schedule ( schedule-kind , expression )
31185 static | dynamic | guided | runtime | auto
31188 schedule ( schedule-modifier : schedule-kind )
31189 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31197 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
31200 int modifiers
= 0, nmodifiers
= 0;
31202 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31205 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
31207 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31209 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31210 const char *p
= IDENTIFIER_POINTER (id
);
31211 if (strcmp ("simd", p
) == 0)
31212 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
31213 else if (strcmp ("monotonic", p
) == 0)
31214 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
31215 else if (strcmp ("nonmonotonic", p
) == 0)
31216 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
31219 cp_lexer_consume_token (parser
->lexer
);
31220 if (nmodifiers
++ == 0
31221 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31222 cp_lexer_consume_token (parser
->lexer
);
31225 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31230 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31232 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31233 const char *p
= IDENTIFIER_POINTER (id
);
31238 if (strcmp ("dynamic", p
) != 0)
31240 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
31244 if (strcmp ("guided", p
) != 0)
31246 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
31250 if (strcmp ("runtime", p
) != 0)
31252 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
31259 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
31260 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
31261 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
31262 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
31265 cp_lexer_consume_token (parser
->lexer
);
31267 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
31268 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
31269 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31270 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
31272 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31277 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31280 cp_lexer_consume_token (parser
->lexer
);
31282 token
= cp_lexer_peek_token (parser
->lexer
);
31283 t
= cp_parser_assignment_expression (parser
);
31285 if (t
== error_mark_node
)
31287 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
31288 error_at (token
->location
, "schedule %<runtime%> does not take "
31289 "a %<chunk_size%> parameter");
31290 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
31291 error_at (token
->location
, "schedule %<auto%> does not take "
31292 "a %<chunk_size%> parameter");
31294 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
31296 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31299 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31302 OMP_CLAUSE_SCHEDULE_KIND (c
)
31303 = (enum omp_clause_schedule_kind
)
31304 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
31306 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
31307 OMP_CLAUSE_CHAIN (c
) = list
;
31311 cp_parser_error (parser
, "invalid schedule kind");
31313 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31314 /*or_comma=*/false,
31315 /*consume_paren=*/true);
31323 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
31324 tree list
, location_t location
)
31328 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
31330 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
31331 OMP_CLAUSE_CHAIN (c
) = list
;
31340 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
31341 tree list
, location_t location
)
31343 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31344 tree c
= build_omp_clause (location
, code
);
31345 OMP_CLAUSE_CHAIN (c
) = list
;
31356 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
31357 enum omp_clause_code code
,
31358 tree list
, location_t location
)
31360 tree c
= build_omp_clause (location
, code
);
31361 OMP_CLAUSE_CHAIN (c
) = list
;
31369 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
31370 tree list
, location_t location
)
31372 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
31373 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
31374 OMP_CLAUSE_CHAIN (c
) = list
;
31383 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
31384 enum omp_clause_code code
,
31385 tree list
, location_t location
)
31387 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
31388 tree c
= build_omp_clause (location
, code
);
31389 OMP_CLAUSE_CHAIN (c
) = list
;
31394 num_teams ( expression ) */
31397 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
31398 location_t location
)
31402 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31405 t
= cp_parser_expression (parser
);
31407 if (t
== error_mark_node
31408 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31409 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31410 /*or_comma=*/false,
31411 /*consume_paren=*/true);
31413 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
31414 "num_teams", location
);
31416 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
31417 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
31418 OMP_CLAUSE_CHAIN (c
) = list
;
31424 thread_limit ( expression ) */
31427 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
31428 location_t location
)
31432 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31435 t
= cp_parser_expression (parser
);
31437 if (t
== error_mark_node
31438 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31440 /*or_comma=*/false,
31441 /*consume_paren=*/true);
31443 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
31444 "thread_limit", location
);
31446 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
31447 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
31448 OMP_CLAUSE_CHAIN (c
) = list
;
31454 aligned ( variable-list )
31455 aligned ( variable-list : constant-expression ) */
31458 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
31460 tree nlist
, c
, alignment
= NULL_TREE
;
31463 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31466 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
31471 alignment
= cp_parser_constant_expression (parser
);
31473 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31474 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31475 /*or_comma=*/false,
31476 /*consume_paren=*/true);
31478 if (alignment
== error_mark_node
)
31479 alignment
= NULL_TREE
;
31482 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31483 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
31489 linear ( variable-list )
31490 linear ( variable-list : expression )
31493 linear ( modifier ( variable-list ) )
31494 linear ( modifier ( variable-list ) : expression ) */
31497 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
31498 bool is_cilk_simd_fn
, bool declare_simd
)
31500 tree nlist
, c
, step
= integer_one_node
;
31502 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
31504 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31507 if (!is_cilk_simd_fn
31508 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31510 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31511 const char *p
= IDENTIFIER_POINTER (id
);
31513 if (strcmp ("ref", p
) == 0)
31514 kind
= OMP_CLAUSE_LINEAR_REF
;
31515 else if (strcmp ("val", p
) == 0)
31516 kind
= OMP_CLAUSE_LINEAR_VAL
;
31517 else if (strcmp ("uval", p
) == 0)
31518 kind
= OMP_CLAUSE_LINEAR_UVAL
;
31519 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
31520 cp_lexer_consume_token (parser
->lexer
);
31522 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
31525 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
31526 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
31530 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
31531 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
31533 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31534 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31535 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31536 /*or_comma=*/false,
31537 /*consume_paren=*/true);
31544 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
31545 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
31547 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31548 cp_parser_parse_tentatively (parser
);
31549 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
31550 /*check_dependency_p=*/true,
31551 /*template_p=*/NULL
,
31552 /*declarator_p=*/false,
31553 /*optional_p=*/false);
31554 if (step
!= error_mark_node
)
31555 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
31556 if (step
== error_mark_node
)
31559 cp_parser_abort_tentative_parse (parser
);
31561 else if (!cp_parser_parse_definitely (parser
))
31565 step
= cp_parser_expression (parser
);
31567 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
31569 sorry ("using parameters for %<linear%> step is not supported yet");
31570 step
= integer_one_node
;
31572 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31573 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31574 /*or_comma=*/false,
31575 /*consume_paren=*/true);
31577 if (step
== error_mark_node
)
31581 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31583 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
31584 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
31591 safelen ( constant-expression ) */
31594 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
31595 location_t location
)
31599 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31602 t
= cp_parser_constant_expression (parser
);
31604 if (t
== error_mark_node
31605 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31606 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31607 /*or_comma=*/false,
31608 /*consume_paren=*/true);
31610 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
31612 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
31613 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
31614 OMP_CLAUSE_CHAIN (c
) = list
;
31620 simdlen ( constant-expression ) */
31623 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
31624 location_t location
)
31628 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31631 t
= cp_parser_constant_expression (parser
);
31633 if (t
== error_mark_node
31634 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31635 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31636 /*or_comma=*/false,
31637 /*consume_paren=*/true);
31639 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
31641 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
31642 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
31643 OMP_CLAUSE_CHAIN (c
) = list
;
31650 identifier [+/- integer]
31651 vec , identifier [+/- integer]
31655 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
31660 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31662 cp_parser_error (parser
, "expected identifier");
31666 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31668 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31669 tree t
, identifier
= cp_parser_identifier (parser
);
31670 tree addend
= NULL
;
31672 if (identifier
== error_mark_node
)
31673 t
= error_mark_node
;
31676 t
= cp_parser_lookup_name_simple
31677 (parser
, identifier
,
31678 cp_lexer_peek_token (parser
->lexer
)->location
);
31679 if (t
== error_mark_node
)
31680 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
31685 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31687 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
31689 addend
= integer_zero_node
;
31690 goto add_to_vector
;
31692 cp_lexer_consume_token (parser
->lexer
);
31694 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
31696 cp_parser_error (parser
, "expected integer");
31700 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31701 if (TREE_CODE (addend
) != INTEGER_CST
)
31703 cp_parser_error (parser
, "expected integer");
31706 cp_lexer_consume_token (parser
->lexer
);
31709 if (t
!= error_mark_node
)
31711 vec
= tree_cons (addend
, t
, vec
);
31713 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
31716 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31719 cp_lexer_consume_token (parser
->lexer
);
31722 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
31724 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
31725 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
31726 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
31727 OMP_CLAUSE_CHAIN (u
) = list
;
31734 depend ( depend-kind : variable-list )
31742 depend ( sink : vec ) */
31745 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
31748 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
31750 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31755 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31756 const char *p
= IDENTIFIER_POINTER (id
);
31758 if (strcmp ("in", p
) == 0)
31759 kind
= OMP_CLAUSE_DEPEND_IN
;
31760 else if (strcmp ("inout", p
) == 0)
31761 kind
= OMP_CLAUSE_DEPEND_INOUT
;
31762 else if (strcmp ("out", p
) == 0)
31763 kind
= OMP_CLAUSE_DEPEND_OUT
;
31764 else if (strcmp ("source", p
) == 0)
31765 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
31766 else if (strcmp ("sink", p
) == 0)
31767 kind
= OMP_CLAUSE_DEPEND_SINK
;
31774 cp_lexer_consume_token (parser
->lexer
);
31776 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
31778 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
31779 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31780 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
31781 OMP_CLAUSE_CHAIN (c
) = list
;
31782 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31783 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31784 /*or_comma=*/false,
31785 /*consume_paren=*/true);
31789 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31792 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
31793 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
31796 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
31799 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31800 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31805 cp_parser_error (parser
, "invalid depend kind");
31807 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31808 /*or_comma=*/false,
31809 /*consume_paren=*/true);
31814 map ( map-kind : variable-list )
31815 map ( variable-list )
31818 alloc | to | from | tofrom
31822 alloc | to | from | tofrom | release | delete
31824 map ( always [,] map-kind: variable-list ) */
31827 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
31830 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
31831 bool always
= false;
31833 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31838 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31839 const char *p
= IDENTIFIER_POINTER (id
);
31841 if (strcmp ("always", p
) == 0)
31844 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
31846 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
31847 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
31849 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
31853 cp_lexer_consume_token (parser
->lexer
);
31855 cp_lexer_consume_token (parser
->lexer
);
31860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
31861 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31863 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31864 const char *p
= IDENTIFIER_POINTER (id
);
31866 if (strcmp ("alloc", p
) == 0)
31867 kind
= GOMP_MAP_ALLOC
;
31868 else if (strcmp ("to", p
) == 0)
31869 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
31870 else if (strcmp ("from", p
) == 0)
31871 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
31872 else if (strcmp ("tofrom", p
) == 0)
31873 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
31874 else if (strcmp ("release", p
) == 0)
31875 kind
= GOMP_MAP_RELEASE
;
31878 cp_parser_error (parser
, "invalid map kind");
31879 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31880 /*or_comma=*/false,
31881 /*consume_paren=*/true);
31884 cp_lexer_consume_token (parser
->lexer
);
31885 cp_lexer_consume_token (parser
->lexer
);
31887 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
31888 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31890 kind
= GOMP_MAP_DELETE
;
31891 cp_lexer_consume_token (parser
->lexer
);
31892 cp_lexer_consume_token (parser
->lexer
);
31895 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
31898 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31899 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31905 device ( expression ) */
31908 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
31909 location_t location
)
31913 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31916 t
= cp_parser_expression (parser
);
31918 if (t
== error_mark_node
31919 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31920 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31921 /*or_comma=*/false,
31922 /*consume_paren=*/true);
31924 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
31925 "device", location
);
31927 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
31928 OMP_CLAUSE_DEVICE_ID (c
) = t
;
31929 OMP_CLAUSE_CHAIN (c
) = list
;
31935 dist_schedule ( static )
31936 dist_schedule ( static , expression ) */
31939 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
31940 location_t location
)
31944 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31947 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
31949 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
31951 cp_lexer_consume_token (parser
->lexer
);
31953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31955 cp_lexer_consume_token (parser
->lexer
);
31957 t
= cp_parser_assignment_expression (parser
);
31959 if (t
== error_mark_node
)
31961 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
31963 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31966 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31969 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
31971 OMP_CLAUSE_CHAIN (c
) = list
;
31975 cp_parser_error (parser
, "invalid dist_schedule kind");
31977 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31978 /*or_comma=*/false,
31979 /*consume_paren=*/true);
31984 proc_bind ( proc-bind-kind )
31987 master | close | spread */
31990 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
31991 location_t location
)
31994 enum omp_clause_proc_bind_kind kind
;
31996 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31999 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32001 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32002 const char *p
= IDENTIFIER_POINTER (id
);
32004 if (strcmp ("master", p
) == 0)
32005 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
32006 else if (strcmp ("close", p
) == 0)
32007 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
32008 else if (strcmp ("spread", p
) == 0)
32009 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
32016 cp_lexer_consume_token (parser
->lexer
);
32017 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
32020 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
32021 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
32023 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
32024 OMP_CLAUSE_CHAIN (c
) = list
;
32028 cp_parser_error (parser
, "invalid depend kind");
32030 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32031 /*or_comma=*/false,
32032 /*consume_paren=*/true);
32037 async [( int-expr )] */
32040 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
32043 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32045 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
32047 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32049 cp_lexer_consume_token (parser
->lexer
);
32051 t
= cp_parser_expression (parser
);
32052 if (t
== error_mark_node
32053 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32055 /*or_comma=*/false,
32056 /*consume_paren=*/true);
32059 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
32061 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
32062 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
32063 OMP_CLAUSE_CHAIN (c
) = list
;
32069 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32070 is a bitmask in MASK. Return the list of clauses found. */
32073 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
32074 const char *where
, cp_token
*pragma_tok
,
32075 bool finish_p
= true)
32077 tree clauses
= NULL
;
32080 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32083 pragma_omp_clause c_kind
;
32084 omp_clause_code code
;
32085 const char *c_name
;
32086 tree prev
= clauses
;
32088 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32089 cp_lexer_consume_token (parser
->lexer
);
32091 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
32092 c_kind
= cp_parser_omp_clause_name (parser
);
32096 case PRAGMA_OACC_CLAUSE_ASYNC
:
32097 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
32100 case PRAGMA_OACC_CLAUSE_AUTO
:
32101 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
32105 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
32106 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
32107 c_name
= "collapse";
32109 case PRAGMA_OACC_CLAUSE_COPY
:
32110 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32113 case PRAGMA_OACC_CLAUSE_COPYIN
:
32114 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32117 case PRAGMA_OACC_CLAUSE_COPYOUT
:
32118 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32119 c_name
= "copyout";
32121 case PRAGMA_OACC_CLAUSE_CREATE
:
32122 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32125 case PRAGMA_OACC_CLAUSE_DELETE
:
32126 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32129 case PRAGMA_OMP_CLAUSE_DEFAULT
:
32130 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
32131 c_name
= "default";
32133 case PRAGMA_OACC_CLAUSE_DEVICE
:
32134 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32137 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
32138 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
32139 c_name
= "deviceptr";
32141 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
32142 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32143 c_name
= "device_resident";
32145 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
32146 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
32148 c_name
= "firstprivate";
32150 case PRAGMA_OACC_CLAUSE_GANG
:
32152 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
32155 case PRAGMA_OACC_CLAUSE_HOST
:
32156 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32159 case PRAGMA_OACC_CLAUSE_IF
:
32160 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
32163 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
32164 clauses
= cp_parser_oacc_simple_clause (parser
,
32165 OMP_CLAUSE_INDEPENDENT
,
32167 c_name
= "independent";
32169 case PRAGMA_OACC_CLAUSE_LINK
:
32170 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32173 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
32174 code
= OMP_CLAUSE_NUM_GANGS
;
32175 c_name
= "num_gangs";
32176 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
32179 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
32180 c_name
= "num_workers";
32181 code
= OMP_CLAUSE_NUM_WORKERS
;
32182 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
32185 case PRAGMA_OACC_CLAUSE_PRESENT
:
32186 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32187 c_name
= "present";
32189 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
32190 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32191 c_name
= "present_or_copy";
32193 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
32194 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32195 c_name
= "present_or_copyin";
32197 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
32198 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32199 c_name
= "present_or_copyout";
32201 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
32202 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32203 c_name
= "present_or_create";
32205 case PRAGMA_OACC_CLAUSE_PRIVATE
:
32206 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
32208 c_name
= "private";
32210 case PRAGMA_OACC_CLAUSE_REDUCTION
:
32211 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
32212 c_name
= "reduction";
32214 case PRAGMA_OACC_CLAUSE_SELF
:
32215 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
32218 case PRAGMA_OACC_CLAUSE_SEQ
:
32219 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
32223 case PRAGMA_OACC_CLAUSE_TILE
:
32224 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
32227 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
32228 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
32230 c_name
= "use_device";
32232 case PRAGMA_OACC_CLAUSE_VECTOR
:
32234 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
32237 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
32238 c_name
= "vector_length";
32239 code
= OMP_CLAUSE_VECTOR_LENGTH
;
32240 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
32243 case PRAGMA_OACC_CLAUSE_WAIT
:
32244 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
32247 case PRAGMA_OACC_CLAUSE_WORKER
:
32249 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
32253 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
32259 if (((mask
>> c_kind
) & 1) == 0)
32261 /* Remove the invalid clause(s) from the list to avoid
32262 confusing the rest of the compiler. */
32264 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
32269 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32272 return finish_omp_clauses (clauses
, C_ORT_ACC
);
32277 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32278 is a bitmask in MASK. Return the list of clauses found; the result
32279 of clause default goes in *pdefault. */
32282 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
32283 const char *where
, cp_token
*pragma_tok
,
32284 bool finish_p
= true)
32286 tree clauses
= NULL
;
32288 cp_token
*token
= NULL
;
32290 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32292 pragma_omp_clause c_kind
;
32293 const char *c_name
;
32294 tree prev
= clauses
;
32296 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32297 cp_lexer_consume_token (parser
->lexer
);
32299 token
= cp_lexer_peek_token (parser
->lexer
);
32300 c_kind
= cp_parser_omp_clause_name (parser
);
32304 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
32305 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
32307 c_name
= "collapse";
32309 case PRAGMA_OMP_CLAUSE_COPYIN
:
32310 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
32313 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
32314 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
32316 c_name
= "copyprivate";
32318 case PRAGMA_OMP_CLAUSE_DEFAULT
:
32319 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
32320 token
->location
, false);
32321 c_name
= "default";
32323 case PRAGMA_OMP_CLAUSE_FINAL
:
32324 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
32327 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
32328 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
32330 c_name
= "firstprivate";
32332 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
32333 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
32335 c_name
= "grainsize";
32337 case PRAGMA_OMP_CLAUSE_HINT
:
32338 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
32342 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
32343 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
32345 c_name
= "defaultmap";
32347 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
32348 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
32350 c_name
= "use_device_ptr";
32352 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
32353 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
32355 c_name
= "is_device_ptr";
32357 case PRAGMA_OMP_CLAUSE_IF
:
32358 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
32362 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
32363 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
32365 c_name
= "lastprivate";
32367 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
32368 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
32370 c_name
= "mergeable";
32372 case PRAGMA_OMP_CLAUSE_NOWAIT
:
32373 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
32376 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
32377 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
32379 c_name
= "num_tasks";
32381 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
32382 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
32384 c_name
= "num_threads";
32386 case PRAGMA_OMP_CLAUSE_ORDERED
:
32387 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
32389 c_name
= "ordered";
32391 case PRAGMA_OMP_CLAUSE_PRIORITY
:
32392 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
32394 c_name
= "priority";
32396 case PRAGMA_OMP_CLAUSE_PRIVATE
:
32397 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
32399 c_name
= "private";
32401 case PRAGMA_OMP_CLAUSE_REDUCTION
:
32402 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
32403 c_name
= "reduction";
32405 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
32406 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
32408 c_name
= "schedule";
32410 case PRAGMA_OMP_CLAUSE_SHARED
:
32411 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
32415 case PRAGMA_OMP_CLAUSE_UNTIED
:
32416 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
32420 case PRAGMA_OMP_CLAUSE_INBRANCH
:
32421 case PRAGMA_CILK_CLAUSE_MASK
:
32422 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
32423 clauses
, token
->location
);
32424 c_name
= "inbranch";
32426 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
32427 case PRAGMA_CILK_CLAUSE_NOMASK
:
32428 clauses
= cp_parser_omp_clause_branch (parser
,
32429 OMP_CLAUSE_NOTINBRANCH
,
32430 clauses
, token
->location
);
32431 c_name
= "notinbranch";
32433 case PRAGMA_OMP_CLAUSE_PARALLEL
:
32434 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
32435 clauses
, token
->location
);
32436 c_name
= "parallel";
32440 error_at (token
->location
, "%qs must be the first clause of %qs",
32445 case PRAGMA_OMP_CLAUSE_FOR
:
32446 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
32447 clauses
, token
->location
);
32450 goto clause_not_first
;
32452 case PRAGMA_OMP_CLAUSE_SECTIONS
:
32453 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
32454 clauses
, token
->location
);
32455 c_name
= "sections";
32457 goto clause_not_first
;
32459 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
32460 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
32461 clauses
, token
->location
);
32462 c_name
= "taskgroup";
32464 goto clause_not_first
;
32466 case PRAGMA_OMP_CLAUSE_LINK
:
32467 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
32470 case PRAGMA_OMP_CLAUSE_TO
:
32471 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
32472 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
32475 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
32478 case PRAGMA_OMP_CLAUSE_FROM
:
32479 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
32482 case PRAGMA_OMP_CLAUSE_UNIFORM
:
32483 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
32485 c_name
= "uniform";
32487 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
32488 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
32490 c_name
= "num_teams";
32492 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
32493 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
32495 c_name
= "thread_limit";
32497 case PRAGMA_OMP_CLAUSE_ALIGNED
:
32498 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
32499 c_name
= "aligned";
32501 case PRAGMA_OMP_CLAUSE_LINEAR
:
32503 bool cilk_simd_fn
= false, declare_simd
= false;
32504 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
32505 cilk_simd_fn
= true;
32506 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
32507 declare_simd
= true;
32508 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
32509 cilk_simd_fn
, declare_simd
);
32513 case PRAGMA_OMP_CLAUSE_DEPEND
:
32514 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
32518 case PRAGMA_OMP_CLAUSE_MAP
:
32519 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
32522 case PRAGMA_OMP_CLAUSE_DEVICE
:
32523 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
32527 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
32528 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
32530 c_name
= "dist_schedule";
32532 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
32533 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
32535 c_name
= "proc_bind";
32537 case PRAGMA_OMP_CLAUSE_SAFELEN
:
32538 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
32540 c_name
= "safelen";
32542 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
32543 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
32545 c_name
= "simdlen";
32547 case PRAGMA_OMP_CLAUSE_NOGROUP
:
32548 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
32550 c_name
= "nogroup";
32552 case PRAGMA_OMP_CLAUSE_THREADS
:
32554 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
32555 clauses
, token
->location
);
32556 c_name
= "threads";
32558 case PRAGMA_OMP_CLAUSE_SIMD
:
32560 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
32561 clauses
, token
->location
);
32564 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
32565 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
32566 c_name
= "simdlen";
32569 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
32575 if (((mask
>> c_kind
) & 1) == 0)
32577 /* Remove the invalid clause(s) from the list to avoid
32578 confusing the rest of the compiler. */
32580 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
32584 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32585 no reason to skip to the end. */
32586 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
32587 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32590 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
32591 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
32593 return finish_omp_clauses (clauses
, C_ORT_OMP
);
32602 In practice, we're also interested in adding the statement to an
32603 outer node. So it is convenient if we work around the fact that
32604 cp_parser_statement calls add_stmt. */
32607 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
32609 unsigned save
= parser
->in_statement
;
32611 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32612 This preserves the "not within loop or switch" style error messages
32613 for nonsense cases like
32619 if (parser
->in_statement
)
32620 parser
->in_statement
= IN_OMP_BLOCK
;
32626 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
32628 parser
->in_statement
= save
;
32632 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
32634 tree stmt
= begin_omp_structured_block ();
32635 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
32637 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
32639 cp_parser_end_omp_structured_block (parser
, save
);
32640 return finish_omp_structured_block (stmt
);
32644 # pragma omp atomic new-line
32648 x binop= expr | x++ | ++x | x-- | --x
32650 +, *, -, /, &, ^, |, <<, >>
32652 where x is an lvalue expression with scalar type.
32655 # pragma omp atomic new-line
32658 # pragma omp atomic read new-line
32661 # pragma omp atomic write new-line
32664 # pragma omp atomic update new-line
32667 # pragma omp atomic capture new-line
32670 # pragma omp atomic capture new-line
32678 expression-stmt | x = x binop expr
32680 v = expression-stmt
32682 { v = x; update-stmt; } | { update-stmt; v = x; }
32686 expression-stmt | x = x binop expr | x = expr binop x
32690 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32692 where x and v are lvalue expressions with scalar type. */
32695 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
32697 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
32698 tree rhs1
= NULL_TREE
, orig_lhs
;
32699 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
32700 bool structured_block
= false;
32701 bool seq_cst
= false;
32703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32705 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32706 const char *p
= IDENTIFIER_POINTER (id
);
32708 if (!strcmp (p
, "seq_cst"))
32711 cp_lexer_consume_token (parser
->lexer
);
32712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32713 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32714 cp_lexer_consume_token (parser
->lexer
);
32717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32719 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32720 const char *p
= IDENTIFIER_POINTER (id
);
32722 if (!strcmp (p
, "read"))
32723 code
= OMP_ATOMIC_READ
;
32724 else if (!strcmp (p
, "write"))
32726 else if (!strcmp (p
, "update"))
32728 else if (!strcmp (p
, "capture"))
32729 code
= OMP_ATOMIC_CAPTURE_NEW
;
32733 cp_lexer_consume_token (parser
->lexer
);
32737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32738 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32739 cp_lexer_consume_token (parser
->lexer
);
32741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32743 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32744 const char *p
= IDENTIFIER_POINTER (id
);
32746 if (!strcmp (p
, "seq_cst"))
32749 cp_lexer_consume_token (parser
->lexer
);
32753 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32757 case OMP_ATOMIC_READ
:
32758 case NOP_EXPR
: /* atomic write */
32759 v
= cp_parser_unary_expression (parser
);
32760 if (v
== error_mark_node
)
32762 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32764 if (code
== NOP_EXPR
)
32765 lhs
= cp_parser_expression (parser
);
32767 lhs
= cp_parser_unary_expression (parser
);
32768 if (lhs
== error_mark_node
)
32770 if (code
== NOP_EXPR
)
32772 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32780 case OMP_ATOMIC_CAPTURE_NEW
:
32781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32783 cp_lexer_consume_token (parser
->lexer
);
32784 structured_block
= true;
32788 v
= cp_parser_unary_expression (parser
);
32789 if (v
== error_mark_node
)
32791 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32799 lhs
= cp_parser_unary_expression (parser
);
32801 switch (TREE_CODE (lhs
))
32806 case POSTINCREMENT_EXPR
:
32807 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32808 code
= OMP_ATOMIC_CAPTURE_OLD
;
32810 case PREINCREMENT_EXPR
:
32811 lhs
= TREE_OPERAND (lhs
, 0);
32812 opcode
= PLUS_EXPR
;
32813 rhs
= integer_one_node
;
32816 case POSTDECREMENT_EXPR
:
32817 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32818 code
= OMP_ATOMIC_CAPTURE_OLD
;
32820 case PREDECREMENT_EXPR
:
32821 lhs
= TREE_OPERAND (lhs
, 0);
32822 opcode
= MINUS_EXPR
;
32823 rhs
= integer_one_node
;
32826 case COMPOUND_EXPR
:
32827 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
32828 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
32829 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
32830 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
32831 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32832 (TREE_OPERAND (lhs
, 1), 0), 0)))
32834 /* Undo effects of boolean_increment for post {in,de}crement. */
32835 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
32838 if (TREE_CODE (lhs
) == MODIFY_EXPR
32839 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
32841 /* Undo effects of boolean_increment. */
32842 if (integer_onep (TREE_OPERAND (lhs
, 1)))
32844 /* This is pre or post increment. */
32845 rhs
= TREE_OPERAND (lhs
, 1);
32846 lhs
= TREE_OPERAND (lhs
, 0);
32848 if (code
== OMP_ATOMIC_CAPTURE_NEW
32849 && !structured_block
32850 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
32851 code
= OMP_ATOMIC_CAPTURE_OLD
;
32857 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32860 opcode
= MULT_EXPR
;
32863 opcode
= TRUNC_DIV_EXPR
;
32866 opcode
= PLUS_EXPR
;
32869 opcode
= MINUS_EXPR
;
32871 case CPP_LSHIFT_EQ
:
32872 opcode
= LSHIFT_EXPR
;
32874 case CPP_RSHIFT_EQ
:
32875 opcode
= RSHIFT_EXPR
;
32878 opcode
= BIT_AND_EXPR
;
32881 opcode
= BIT_IOR_EXPR
;
32884 opcode
= BIT_XOR_EXPR
;
32887 enum cp_parser_prec oprec
;
32889 cp_lexer_consume_token (parser
->lexer
);
32890 cp_parser_parse_tentatively (parser
);
32891 rhs1
= cp_parser_simple_cast_expression (parser
);
32892 if (rhs1
== error_mark_node
)
32894 cp_parser_abort_tentative_parse (parser
);
32895 cp_parser_simple_cast_expression (parser
);
32898 token
= cp_lexer_peek_token (parser
->lexer
);
32899 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
32901 cp_parser_abort_tentative_parse (parser
);
32902 cp_parser_parse_tentatively (parser
);
32903 rhs
= cp_parser_binary_expression (parser
, false, true,
32904 PREC_NOT_OPERATOR
, NULL
);
32905 if (rhs
== error_mark_node
)
32907 cp_parser_abort_tentative_parse (parser
);
32908 cp_parser_binary_expression (parser
, false, true,
32909 PREC_NOT_OPERATOR
, NULL
);
32912 switch (TREE_CODE (rhs
))
32915 case TRUNC_DIV_EXPR
:
32924 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
32926 if (cp_parser_parse_definitely (parser
))
32928 opcode
= TREE_CODE (rhs
);
32929 rhs1
= TREE_OPERAND (rhs
, 0);
32930 rhs
= TREE_OPERAND (rhs
, 1);
32940 cp_parser_abort_tentative_parse (parser
);
32941 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
32943 rhs
= cp_parser_expression (parser
);
32944 if (rhs
== error_mark_node
)
32950 cp_parser_error (parser
,
32951 "invalid form of %<#pragma omp atomic%>");
32954 if (!cp_parser_parse_definitely (parser
))
32956 switch (token
->type
)
32958 case CPP_SEMICOLON
:
32959 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
32961 code
= OMP_ATOMIC_CAPTURE_OLD
;
32966 cp_lexer_consume_token (parser
->lexer
);
32969 else if (structured_block
)
32976 cp_parser_error (parser
,
32977 "invalid form of %<#pragma omp atomic%>");
32980 opcode
= MULT_EXPR
;
32983 opcode
= TRUNC_DIV_EXPR
;
32986 opcode
= PLUS_EXPR
;
32989 opcode
= MINUS_EXPR
;
32992 opcode
= LSHIFT_EXPR
;
32995 opcode
= RSHIFT_EXPR
;
32998 opcode
= BIT_AND_EXPR
;
33001 opcode
= BIT_IOR_EXPR
;
33004 opcode
= BIT_XOR_EXPR
;
33007 cp_parser_error (parser
,
33008 "invalid operator for %<#pragma omp atomic%>");
33011 oprec
= TOKEN_PRECEDENCE (token
);
33012 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
33013 if (commutative_tree_code (opcode
))
33014 oprec
= (enum cp_parser_prec
) (oprec
- 1);
33015 cp_lexer_consume_token (parser
->lexer
);
33016 rhs
= cp_parser_binary_expression (parser
, false, false,
33018 if (rhs
== error_mark_node
)
33023 cp_parser_error (parser
,
33024 "invalid operator for %<#pragma omp atomic%>");
33027 cp_lexer_consume_token (parser
->lexer
);
33029 rhs
= cp_parser_expression (parser
);
33030 if (rhs
== error_mark_node
)
33035 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
33037 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
33039 v
= cp_parser_unary_expression (parser
);
33040 if (v
== error_mark_node
)
33042 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
33044 lhs1
= cp_parser_unary_expression (parser
);
33045 if (lhs1
== error_mark_node
)
33048 if (structured_block
)
33050 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33051 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33054 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
33055 if (!structured_block
)
33056 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33060 cp_parser_skip_to_end_of_block_or_statement (parser
);
33061 if (structured_block
)
33063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33064 cp_lexer_consume_token (parser
->lexer
);
33065 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
33067 cp_parser_skip_to_end_of_block_or_statement (parser
);
33068 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33069 cp_lexer_consume_token (parser
->lexer
);
33076 # pragma omp barrier new-line */
33079 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
33081 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33082 finish_omp_barrier ();
33086 # pragma omp critical [(name)] new-line
33090 # pragma omp critical [(name) [hint(expression)]] new-line
33091 structured-block */
33093 #define OMP_CRITICAL_CLAUSE_MASK \
33094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33097 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
33099 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
33101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33103 cp_lexer_consume_token (parser
->lexer
);
33105 name
= cp_parser_identifier (parser
);
33107 if (name
== error_mark_node
33108 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33109 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33110 /*or_comma=*/false,
33111 /*consume_paren=*/true);
33112 if (name
== error_mark_node
)
33115 clauses
= cp_parser_omp_all_clauses (parser
,
33116 OMP_CRITICAL_CLAUSE_MASK
,
33117 "#pragma omp critical", pragma_tok
);
33120 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33122 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
33123 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
33127 # pragma omp flush flush-vars[opt] new-line
33130 ( variable-list ) */
33133 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
33135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33136 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
33137 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33139 finish_omp_flush ();
33142 /* Helper function, to parse omp for increment expression. */
33145 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
33147 tree cond
= cp_parser_binary_expression (parser
, false, true,
33148 PREC_NOT_OPERATOR
, NULL
);
33149 if (cond
== error_mark_node
33150 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33152 cp_parser_skip_to_end_of_statement (parser
);
33153 return error_mark_node
;
33156 switch (TREE_CODE (cond
))
33164 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
33166 /* Fall through: OpenMP disallows NE_EXPR. */
33168 return error_mark_node
;
33171 /* If decl is an iterator, preserve LHS and RHS of the relational
33172 expr until finish_omp_for. */
33174 && (type_dependent_expression_p (decl
)
33175 || CLASS_TYPE_P (TREE_TYPE (decl
))))
33178 return build_x_binary_op (EXPR_LOC_OR_LOC (cond
, input_location
),
33180 TREE_OPERAND (cond
, 0), ERROR_MARK
,
33181 TREE_OPERAND (cond
, 1), ERROR_MARK
,
33182 /*overload=*/NULL
, tf_warning_or_error
);
33185 /* Helper function, to parse omp for increment expression. */
33188 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
33190 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33196 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
33198 op
= (token
->type
== CPP_PLUS_PLUS
33199 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
33200 cp_lexer_consume_token (parser
->lexer
);
33201 lhs
= cp_parser_simple_cast_expression (parser
);
33203 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
33204 return error_mark_node
;
33205 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
33208 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
33210 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
33211 return error_mark_node
;
33213 token
= cp_lexer_peek_token (parser
->lexer
);
33214 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
33216 op
= (token
->type
== CPP_PLUS_PLUS
33217 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
33218 cp_lexer_consume_token (parser
->lexer
);
33219 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
33222 op
= cp_parser_assignment_operator_opt (parser
);
33223 if (op
== ERROR_MARK
)
33224 return error_mark_node
;
33226 if (op
!= NOP_EXPR
)
33228 rhs
= cp_parser_assignment_expression (parser
);
33229 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
33230 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
33233 lhs
= cp_parser_binary_expression (parser
, false, false,
33234 PREC_ADDITIVE_EXPRESSION
, NULL
);
33235 token
= cp_lexer_peek_token (parser
->lexer
);
33236 decl_first
= (lhs
== decl
33237 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
33240 if (token
->type
!= CPP_PLUS
33241 && token
->type
!= CPP_MINUS
)
33242 return error_mark_node
;
33246 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
33247 cp_lexer_consume_token (parser
->lexer
);
33248 rhs
= cp_parser_binary_expression (parser
, false, false,
33249 PREC_ADDITIVE_EXPRESSION
, NULL
);
33250 token
= cp_lexer_peek_token (parser
->lexer
);
33251 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
33253 if (lhs
== NULL_TREE
)
33255 if (op
== PLUS_EXPR
)
33258 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
33259 tf_warning_or_error
);
33262 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
33263 ERROR_MARK
, NULL
, tf_warning_or_error
);
33266 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
33271 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
33272 || op
== MINUS_EXPR
)
33273 return error_mark_node
;
33274 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
33277 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
33279 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
33282 /* Parse the initialization statement of either an OpenMP for loop or
33283 a Cilk Plus for loop.
33285 Return true if the resulting construct should have an
33286 OMP_CLAUSE_PRIVATE added to it. */
33289 cp_parser_omp_for_loop_init (cp_parser
*parser
,
33290 enum tree_code code
,
33291 tree
&this_pre_body
,
33292 vec
<tree
, va_gc
> *for_block
,
33298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33301 tree add_private_clause
= NULL_TREE
;
33303 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33307 integer-type var = lb
33308 random-access-iterator-type var = lb
33309 pointer-type var = lb
33311 cp_decl_specifier_seq type_specifiers
;
33313 /* First, try to parse as an initialized declaration. See
33314 cp_parser_condition, from whence the bulk of this is copied. */
33316 cp_parser_parse_tentatively (parser
);
33317 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
33318 /*is_trailing_return=*/false,
33320 if (cp_parser_parse_definitely (parser
))
33322 /* If parsing a type specifier seq succeeded, then this
33323 MUST be a initialized declaration. */
33324 tree asm_specification
, attributes
;
33325 cp_declarator
*declarator
;
33327 declarator
= cp_parser_declarator (parser
,
33328 CP_PARSER_DECLARATOR_NAMED
,
33329 /*ctor_dtor_or_conv_p=*/NULL
,
33330 /*parenthesized_p=*/NULL
,
33331 /*member_p=*/false,
33332 /*friend_p=*/false);
33333 attributes
= cp_parser_attributes_opt (parser
);
33334 asm_specification
= cp_parser_asm_specification_opt (parser
);
33336 if (declarator
== cp_error_declarator
)
33337 cp_parser_skip_to_end_of_statement (parser
);
33341 tree pushed_scope
, auto_node
;
33343 decl
= start_decl (declarator
, &type_specifiers
,
33344 SD_INITIALIZED
, attributes
,
33345 /*prefix_attributes=*/NULL_TREE
,
33348 auto_node
= type_uses_auto (TREE_TYPE (decl
));
33349 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
33351 if (cp_lexer_next_token_is (parser
->lexer
,
33354 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
33355 error ("parenthesized initialization is not allowed in "
33356 "OpenMP %<for%> loop");
33358 error ("parenthesized initialization is "
33359 "not allowed in for-loop");
33362 /* Trigger an error. */
33363 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
33365 init
= error_mark_node
;
33366 cp_parser_skip_to_end_of_statement (parser
);
33368 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
33369 || type_dependent_expression_p (decl
)
33372 bool is_direct_init
, is_non_constant_init
;
33374 init
= cp_parser_initializer (parser
,
33376 &is_non_constant_init
);
33381 = do_auto_deduction (TREE_TYPE (decl
), init
,
33384 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
33385 && !type_dependent_expression_p (decl
))
33389 cp_finish_decl (decl
, init
, !is_non_constant_init
,
33391 LOOKUP_ONLYCONVERTING
);
33393 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
33395 vec_safe_push (for_block
, this_pre_body
);
33399 init
= pop_stmt_list (this_pre_body
);
33400 this_pre_body
= NULL_TREE
;
33405 cp_lexer_consume_token (parser
->lexer
);
33406 init
= cp_parser_assignment_expression (parser
);
33409 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
33410 init
= error_mark_node
;
33412 cp_finish_decl (decl
, NULL_TREE
,
33413 /*init_const_expr_p=*/false,
33415 LOOKUP_ONLYCONVERTING
);
33419 pop_scope (pushed_scope
);
33425 /* If parsing a type specifier sequence failed, then
33426 this MUST be a simple expression. */
33427 if (code
== CILK_FOR
)
33428 error ("%<_Cilk_for%> allows expression instead of declaration only "
33429 "in C, not in C++");
33430 cp_parser_parse_tentatively (parser
);
33431 decl
= cp_parser_primary_expression (parser
, false, false,
33433 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
33434 if (!cp_parser_error_occurred (parser
)
33436 && (TREE_CODE (decl
) == COMPONENT_REF
33437 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
33439 cp_parser_abort_tentative_parse (parser
);
33440 cp_parser_parse_tentatively (parser
);
33441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33442 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
33443 /*check_dependency_p=*/true,
33444 /*template_p=*/NULL
,
33445 /*declarator_p=*/false,
33446 /*optional_p=*/false);
33447 if (name
!= error_mark_node
33448 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
33450 decl
= cp_parser_lookup_name_simple (parser
, name
,
33452 if (TREE_CODE (decl
) == FIELD_DECL
)
33453 add_private_clause
= omp_privatize_field (decl
, false);
33455 cp_parser_abort_tentative_parse (parser
);
33456 cp_parser_parse_tentatively (parser
);
33457 decl
= cp_parser_primary_expression (parser
, false, false,
33460 if (!cp_parser_error_occurred (parser
)
33463 && CLASS_TYPE_P (TREE_TYPE (decl
)))
33467 cp_parser_parse_definitely (parser
);
33468 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
33469 rhs
= cp_parser_assignment_expression (parser
);
33471 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
33474 tf_warning_or_error
));
33475 if (!add_private_clause
)
33476 add_private_clause
= decl
;
33481 cp_parser_abort_tentative_parse (parser
);
33482 init
= cp_parser_expression (parser
);
33485 if (TREE_CODE (init
) == MODIFY_EXPR
33486 || TREE_CODE (init
) == MODOP_EXPR
)
33487 real_decl
= TREE_OPERAND (init
, 0);
33491 return add_private_clause
;
33494 /* Parse the restricted form of the for statement allowed by OpenMP. */
33497 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
33498 tree
*cclauses
, bool *if_p
)
33500 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
33501 tree real_decl
, initv
, condv
, incrv
, declv
;
33502 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
33503 location_t loc_first
;
33504 bool collapse_err
= false;
33505 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
33506 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
33507 auto_vec
<tree
, 4> orig_inits
;
33509 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
33510 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
33511 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
33512 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
33513 && OMP_CLAUSE_ORDERED_EXPR (cl
))
33516 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
33519 if (ordered
&& ordered
< collapse
)
33521 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
33522 "%<ordered%> clause parameter is less than %<collapse%>");
33523 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
33524 = build_int_cst (NULL_TREE
, collapse
);
33525 ordered
= collapse
;
33529 for (tree
*pc
= &clauses
; *pc
; )
33530 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
33532 error_at (OMP_CLAUSE_LOCATION (*pc
),
33533 "%<linear%> clause may not be specified together "
33534 "with %<ordered%> clause with a parameter");
33535 *pc
= OMP_CLAUSE_CHAIN (*pc
);
33538 pc
= &OMP_CLAUSE_CHAIN (*pc
);
33541 gcc_assert (collapse
>= 1 && ordered
>= 0);
33542 count
= ordered
? ordered
: collapse
;
33544 declv
= make_tree_vec (count
);
33545 initv
= make_tree_vec (count
);
33546 condv
= make_tree_vec (count
);
33547 incrv
= make_tree_vec (count
);
33549 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
33551 for (i
= 0; i
< count
; i
++)
33553 int bracecount
= 0;
33554 tree add_private_clause
= NULL_TREE
;
33557 if (code
!= CILK_FOR
33558 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33560 cp_parser_error (parser
, "for statement expected");
33563 if (code
== CILK_FOR
33564 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
33566 cp_parser_error (parser
, "_Cilk_for statement expected");
33569 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
33571 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33574 init
= orig_init
= decl
= real_decl
= NULL
;
33575 this_pre_body
= push_stmt_list ();
33578 = cp_parser_omp_for_loop_init (parser
, code
,
33579 this_pre_body
, for_block
,
33580 init
, orig_init
, decl
, real_decl
);
33582 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
33585 this_pre_body
= pop_stmt_list (this_pre_body
);
33589 pre_body
= push_stmt_list ();
33591 add_stmt (this_pre_body
);
33592 pre_body
= pop_stmt_list (pre_body
);
33595 pre_body
= this_pre_body
;
33600 if (cclauses
!= NULL
33601 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
33602 && real_decl
!= NULL_TREE
)
33605 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
33606 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
33607 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33609 error_at (loc
, "iteration variable %qD"
33610 " should not be firstprivate", real_decl
);
33611 *c
= OMP_CLAUSE_CHAIN (*c
);
33613 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
33614 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33616 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33618 *c
= OMP_CLAUSE_CHAIN (*c
);
33619 if (code
== OMP_SIMD
)
33621 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33622 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
33626 OMP_CLAUSE_CHAIN (l
) = clauses
;
33629 add_private_clause
= NULL_TREE
;
33633 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
33634 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33635 add_private_clause
= NULL_TREE
;
33636 c
= &OMP_CLAUSE_CHAIN (*c
);
33640 if (add_private_clause
)
33643 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33645 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
33646 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
33647 && OMP_CLAUSE_DECL (c
) == decl
)
33649 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
33650 && OMP_CLAUSE_DECL (c
) == decl
)
33651 error_at (loc
, "iteration variable %qD "
33652 "should not be firstprivate",
33654 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
33655 && OMP_CLAUSE_DECL (c
) == decl
)
33656 error_at (loc
, "iteration variable %qD should not be reduction",
33661 if (code
!= OMP_SIMD
)
33662 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
33663 else if (collapse
== 1)
33664 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33666 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
33667 OMP_CLAUSE_DECL (c
) = add_private_clause
;
33668 c
= finish_omp_clauses (c
, C_ORT_OMP
);
33671 OMP_CLAUSE_CHAIN (c
) = clauses
;
33673 /* For linear, signal that we need to fill up
33674 the so far unknown linear step. */
33675 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
33676 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
33682 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33683 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
33684 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
33687 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
33689 /* If decl is an iterator, preserve the operator on decl
33690 until finish_omp_for. */
33692 && ((processing_template_decl
33693 && (TREE_TYPE (real_decl
) == NULL_TREE
33694 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
33695 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
33696 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
33698 incr
= cp_parser_expression (parser
);
33699 if (!EXPR_HAS_LOCATION (incr
))
33700 protected_set_expr_location (incr
, input_location
);
33703 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33704 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33705 /*or_comma=*/false,
33706 /*consume_paren=*/true);
33708 TREE_VEC_ELT (declv
, i
) = decl
;
33709 TREE_VEC_ELT (initv
, i
) = init
;
33710 TREE_VEC_ELT (condv
, i
) = cond
;
33711 TREE_VEC_ELT (incrv
, i
) = incr
;
33714 orig_inits
.safe_grow_cleared (i
+ 1);
33715 orig_inits
[i
] = orig_init
;
33718 if (i
== count
- 1)
33721 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33722 in between the collapsed for loops to be still considered perfectly
33723 nested. Hopefully the final version clarifies this.
33724 For now handle (multiple) {'s and empty statements. */
33725 cp_parser_parse_tentatively (parser
);
33728 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33730 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33732 cp_lexer_consume_token (parser
->lexer
);
33735 else if (bracecount
33736 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33737 cp_lexer_consume_token (parser
->lexer
);
33740 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33741 error_at (loc
, "not enough collapsed for loops");
33742 collapse_err
= true;
33743 cp_parser_abort_tentative_parse (parser
);
33752 cp_parser_parse_definitely (parser
);
33753 nbraces
+= bracecount
;
33760 /* Note that we saved the original contents of this flag when we entered
33761 the structured block, and so we don't need to re-save it here. */
33762 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
33763 parser
->in_statement
= IN_CILK_SIMD_FOR
;
33765 parser
->in_statement
= IN_OMP_FOR
;
33767 /* Note that the grammar doesn't call for a structured block here,
33768 though the loop as a whole is a structured block. */
33769 body
= push_stmt_list ();
33770 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
33771 body
= pop_stmt_list (body
);
33773 if (declv
== NULL_TREE
)
33776 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
33777 body
, pre_body
, &orig_inits
, clauses
);
33781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33783 cp_lexer_consume_token (parser
->lexer
);
33786 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33787 cp_lexer_consume_token (parser
->lexer
);
33792 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33793 "collapsed loops not perfectly nested");
33795 collapse_err
= true;
33796 cp_parser_statement_seq_opt (parser
, NULL
);
33797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
33802 while (!for_block
->is_empty ())
33803 add_stmt (pop_stmt_list (for_block
->pop ()));
33804 release_tree_vector (for_block
);
33809 /* Helper function for OpenMP parsing, split clauses and call
33810 finish_omp_clauses on each of the set of clauses afterwards. */
33813 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
33814 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
33817 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
33818 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
33820 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
33824 #pragma omp simd simd-clause[optseq] new-line
33827 #define OMP_SIMD_CLAUSE_MASK \
33828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33838 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
33839 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
33842 tree clauses
, sb
, ret
;
33844 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33846 strcat (p_name
, " simd");
33847 mask
|= OMP_SIMD_CLAUSE_MASK
;
33849 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33853 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
33854 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
33855 tree c
= find_omp_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
33856 OMP_CLAUSE_ORDERED
);
33857 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
33859 error_at (OMP_CLAUSE_LOCATION (c
),
33860 "%<ordered%> clause with parameter may not be specified "
33861 "on %qs construct", p_name
);
33862 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
33866 sb
= begin_omp_structured_block ();
33867 save
= cp_parser_begin_omp_structured_block (parser
);
33869 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
33871 cp_parser_end_omp_structured_block (parser
, save
);
33872 add_stmt (finish_omp_structured_block (sb
));
33878 #pragma omp for for-clause[optseq] new-line
33882 #pragma omp for simd for-simd-clause[optseq] new-line
33885 #define OMP_FOR_CLAUSE_MASK \
33886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33897 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
33898 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
33901 tree clauses
, sb
, ret
;
33903 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33905 strcat (p_name
, " for");
33906 mask
|= OMP_FOR_CLAUSE_MASK
;
33908 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
33909 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33910 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33911 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
33913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33915 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33916 const char *p
= IDENTIFIER_POINTER (id
);
33918 if (strcmp (p
, "simd") == 0)
33920 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33921 if (cclauses
== NULL
)
33922 cclauses
= cclauses_buf
;
33924 cp_lexer_consume_token (parser
->lexer
);
33925 if (!flag_openmp
) /* flag_openmp_simd */
33926 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33928 sb
= begin_omp_structured_block ();
33929 save
= cp_parser_begin_omp_structured_block (parser
);
33930 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33932 cp_parser_end_omp_structured_block (parser
, save
);
33933 tree body
= finish_omp_structured_block (sb
);
33936 ret
= make_node (OMP_FOR
);
33937 TREE_TYPE (ret
) = void_type_node
;
33938 OMP_FOR_BODY (ret
) = body
;
33939 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33940 SET_EXPR_LOCATION (ret
, loc
);
33945 if (!flag_openmp
) /* flag_openmp_simd */
33947 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33951 /* Composite distribute parallel for disallows linear clause. */
33952 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33953 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
33955 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33959 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
33960 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33963 sb
= begin_omp_structured_block ();
33964 save
= cp_parser_begin_omp_structured_block (parser
);
33966 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
33968 cp_parser_end_omp_structured_block (parser
, save
);
33969 add_stmt (finish_omp_structured_block (sb
));
33975 # pragma omp master new-line
33976 structured-block */
33979 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
33981 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33982 return c_finish_omp_master (input_location
,
33983 cp_parser_omp_structured_block (parser
, if_p
));
33987 # pragma omp ordered new-line
33991 # pragma omp ordered ordered-clauses new-line
33992 structured-block */
33994 #define OMP_ORDERED_CLAUSE_MASK \
33995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33998 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33999 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34002 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
34003 enum pragma_context context
, bool *if_p
)
34005 location_t loc
= pragma_tok
->location
;
34007 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
34009 cp_parser_error (parser
, "expected declaration specifiers");
34010 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34016 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34017 const char *p
= IDENTIFIER_POINTER (id
);
34019 if (strcmp (p
, "depend") == 0)
34021 if (context
== pragma_stmt
)
34023 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
34024 "%<depend%> clause may only be used in compound "
34026 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34030 = cp_parser_omp_all_clauses (parser
,
34031 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
34032 "#pragma omp ordered", pragma_tok
);
34033 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
34039 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
34040 "#pragma omp ordered", pragma_tok
);
34041 c_finish_omp_ordered (loc
, clauses
,
34042 cp_parser_omp_structured_block (parser
, if_p
));
34049 { section-sequence }
34052 section-directive[opt] structured-block
34053 section-sequence section-directive structured-block */
34056 cp_parser_omp_sections_scope (cp_parser
*parser
)
34058 tree stmt
, substmt
;
34059 bool error_suppress
= false;
34062 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
34065 stmt
= push_stmt_list ();
34067 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
34068 != PRAGMA_OMP_SECTION
)
34070 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34071 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34072 add_stmt (substmt
);
34077 tok
= cp_lexer_peek_token (parser
->lexer
);
34078 if (tok
->type
== CPP_CLOSE_BRACE
)
34080 if (tok
->type
== CPP_EOF
)
34083 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
34085 cp_lexer_consume_token (parser
->lexer
);
34086 cp_parser_require_pragma_eol (parser
, tok
);
34087 error_suppress
= false;
34089 else if (!error_suppress
)
34091 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
34092 error_suppress
= true;
34095 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
34096 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
34097 add_stmt (substmt
);
34099 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
34101 substmt
= pop_stmt_list (stmt
);
34103 stmt
= make_node (OMP_SECTIONS
);
34104 TREE_TYPE (stmt
) = void_type_node
;
34105 OMP_SECTIONS_BODY (stmt
) = substmt
;
34112 # pragma omp sections sections-clause[optseq] newline
34115 #define OMP_SECTIONS_CLAUSE_MASK \
34116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34123 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
34124 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
34127 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34129 strcat (p_name
, " sections");
34130 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
34132 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
34134 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34138 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
34139 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
34142 ret
= cp_parser_omp_sections_scope (parser
);
34144 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
34150 # pragma omp parallel parallel-clause[optseq] new-line
34152 # pragma omp parallel for parallel-for-clause[optseq] new-line
34154 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34158 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34159 structured-block */
34161 #define OMP_PARALLEL_CLAUSE_MASK \
34162 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34173 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
34174 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34177 tree stmt
, clauses
, block
;
34179 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34181 strcat (p_name
, " parallel");
34182 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
34183 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34184 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
34185 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
34186 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
34188 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34190 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34191 if (cclauses
== NULL
)
34192 cclauses
= cclauses_buf
;
34194 cp_lexer_consume_token (parser
->lexer
);
34195 if (!flag_openmp
) /* flag_openmp_simd */
34196 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
34198 block
= begin_omp_parallel ();
34199 save
= cp_parser_begin_omp_structured_block (parser
);
34200 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
34202 cp_parser_end_omp_structured_block (parser
, save
);
34203 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
34205 if (ret
== NULL_TREE
)
34207 OMP_PARALLEL_COMBINED (stmt
) = 1;
34210 /* When combined with distribute, parallel has to be followed by for.
34211 #pragma omp target parallel is allowed though. */
34213 && (mask
& (OMP_CLAUSE_MASK_1
34214 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
34216 error_at (loc
, "expected %<for%> after %qs", p_name
);
34217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34220 else if (!flag_openmp
) /* flag_openmp_simd */
34222 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34225 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34227 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34228 const char *p
= IDENTIFIER_POINTER (id
);
34229 if (strcmp (p
, "sections") == 0)
34231 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34232 cclauses
= cclauses_buf
;
34234 cp_lexer_consume_token (parser
->lexer
);
34235 block
= begin_omp_parallel ();
34236 save
= cp_parser_begin_omp_structured_block (parser
);
34237 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
34238 cp_parser_end_omp_structured_block (parser
, save
);
34239 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
34241 OMP_PARALLEL_COMBINED (stmt
) = 1;
34246 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
34249 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
34250 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
34253 block
= begin_omp_parallel ();
34254 save
= cp_parser_begin_omp_structured_block (parser
);
34255 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34256 cp_parser_end_omp_structured_block (parser
, save
);
34257 stmt
= finish_omp_parallel (clauses
, block
);
34262 # pragma omp single single-clause[optseq] new-line
34263 structured-block */
34265 #define OMP_SINGLE_CLAUSE_MASK \
34266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34272 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34274 tree stmt
= make_node (OMP_SINGLE
);
34275 TREE_TYPE (stmt
) = void_type_node
;
34277 OMP_SINGLE_CLAUSES (stmt
)
34278 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
34279 "#pragma omp single", pragma_tok
);
34280 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
34282 return add_stmt (stmt
);
34286 # pragma omp task task-clause[optseq] new-line
34287 structured-block */
34289 #define OMP_TASK_CLAUSE_MASK \
34290 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34302 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34304 tree clauses
, block
;
34307 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
34308 "#pragma omp task", pragma_tok
);
34309 block
= begin_omp_task ();
34310 save
= cp_parser_begin_omp_structured_block (parser
);
34311 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
34312 cp_parser_end_omp_structured_block (parser
, save
);
34313 return finish_omp_task (clauses
, block
);
34317 # pragma omp taskwait new-line */
34320 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
34322 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34323 finish_omp_taskwait ();
34327 # pragma omp taskyield new-line */
34330 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
34332 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34333 finish_omp_taskyield ();
34337 # pragma omp taskgroup new-line
34338 structured-block */
34341 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34343 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34344 return c_finish_omp_taskgroup (input_location
,
34345 cp_parser_omp_structured_block (parser
,
34351 # pragma omp threadprivate (variable-list) */
34354 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
34358 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34359 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34361 finish_omp_threadprivate (vars
);
34365 # pragma omp cancel cancel-clause[optseq] new-line */
34367 #define OMP_CANCEL_CLAUSE_MASK \
34368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34375 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
34377 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
34378 "#pragma omp cancel", pragma_tok
);
34379 finish_omp_cancel (clauses
);
34383 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34385 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34392 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
34395 bool point_seen
= false;
34397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34399 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34400 const char *p
= IDENTIFIER_POINTER (id
);
34402 if (strcmp (p
, "point") == 0)
34404 cp_lexer_consume_token (parser
->lexer
);
34410 cp_parser_error (parser
, "expected %<point%>");
34411 cp_parser_require_pragma_eol (parser
, pragma_tok
);
34415 clauses
= cp_parser_omp_all_clauses (parser
,
34416 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
34417 "#pragma omp cancellation point",
34419 finish_omp_cancellation_point (clauses
);
34423 #pragma omp distribute distribute-clause[optseq] new-line
34426 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34434 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
34435 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34438 tree clauses
, sb
, ret
;
34440 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34442 strcat (p_name
, " distribute");
34443 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
34445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34447 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34448 const char *p
= IDENTIFIER_POINTER (id
);
34450 bool parallel
= false;
34452 if (strcmp (p
, "simd") == 0)
34455 parallel
= strcmp (p
, "parallel") == 0;
34456 if (parallel
|| simd
)
34458 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34459 if (cclauses
== NULL
)
34460 cclauses
= cclauses_buf
;
34461 cp_lexer_consume_token (parser
->lexer
);
34462 if (!flag_openmp
) /* flag_openmp_simd */
34465 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34468 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
34471 sb
= begin_omp_structured_block ();
34472 save
= cp_parser_begin_omp_structured_block (parser
);
34474 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
34477 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
34479 cp_parser_end_omp_structured_block (parser
, save
);
34480 tree body
= finish_omp_structured_block (sb
);
34483 ret
= make_node (OMP_DISTRIBUTE
);
34484 TREE_TYPE (ret
) = void_type_node
;
34485 OMP_FOR_BODY (ret
) = body
;
34486 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
34487 SET_EXPR_LOCATION (ret
, loc
);
34492 if (!flag_openmp
) /* flag_openmp_simd */
34494 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34498 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34502 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
34503 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
34506 sb
= begin_omp_structured_block ();
34507 save
= cp_parser_begin_omp_structured_block (parser
);
34509 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
34511 cp_parser_end_omp_structured_block (parser
, save
);
34512 add_stmt (finish_omp_structured_block (sb
));
34518 # pragma omp teams teams-clause[optseq] new-line
34519 structured-block */
34521 #define OMP_TEAMS_CLAUSE_MASK \
34522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34531 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
34532 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
34535 tree clauses
, sb
, ret
;
34537 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34539 strcat (p_name
, " teams");
34540 mask
|= OMP_TEAMS_CLAUSE_MASK
;
34542 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34544 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34545 const char *p
= IDENTIFIER_POINTER (id
);
34546 if (strcmp (p
, "distribute") == 0)
34548 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
34549 if (cclauses
== NULL
)
34550 cclauses
= cclauses_buf
;
34552 cp_lexer_consume_token (parser
->lexer
);
34553 if (!flag_openmp
) /* flag_openmp_simd */
34554 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
34556 sb
= begin_omp_structured_block ();
34557 save
= cp_parser_begin_omp_structured_block (parser
);
34558 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
34560 cp_parser_end_omp_structured_block (parser
, save
);
34561 tree body
= finish_omp_structured_block (sb
);
34564 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
34565 ret
= make_node (OMP_TEAMS
);
34566 TREE_TYPE (ret
) = void_type_node
;
34567 OMP_TEAMS_CLAUSES (ret
) = clauses
;
34568 OMP_TEAMS_BODY (ret
) = body
;
34569 OMP_TEAMS_COMBINED (ret
) = 1;
34570 return add_stmt (ret
);
34573 if (!flag_openmp
) /* flag_openmp_simd */
34575 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34579 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34583 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
34584 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
34587 tree stmt
= make_node (OMP_TEAMS
);
34588 TREE_TYPE (stmt
) = void_type_node
;
34589 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
34590 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
34592 return add_stmt (stmt
);
34596 # pragma omp target data target-data-clause[optseq] new-line
34597 structured-block */
34599 #define OMP_TARGET_DATA_CLAUSE_MASK \
34600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34606 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
34609 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
34610 "#pragma omp target data", pragma_tok
);
34612 for (tree
*pc
= &clauses
; *pc
;)
34614 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34615 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34618 case GOMP_MAP_ALWAYS_TO
:
34619 case GOMP_MAP_FROM
:
34620 case GOMP_MAP_ALWAYS_FROM
:
34621 case GOMP_MAP_TOFROM
:
34622 case GOMP_MAP_ALWAYS_TOFROM
:
34623 case GOMP_MAP_ALLOC
:
34626 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34627 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34628 case GOMP_MAP_ALWAYS_POINTER
:
34632 error_at (OMP_CLAUSE_LOCATION (*pc
),
34633 "%<#pragma omp target data%> with map-type other "
34634 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34635 "on %<map%> clause");
34636 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34639 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34645 error_at (pragma_tok
->location
,
34646 "%<#pragma omp target data%> must contain at least "
34647 "one %<map%> clause");
34651 tree stmt
= make_node (OMP_TARGET_DATA
);
34652 TREE_TYPE (stmt
) = void_type_node
;
34653 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
34655 keep_next_level (true);
34656 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
34658 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34659 return add_stmt (stmt
);
34663 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34664 structured-block */
34666 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34674 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34675 enum pragma_context context
)
34677 bool data_seen
= false;
34678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34680 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34681 const char *p
= IDENTIFIER_POINTER (id
);
34683 if (strcmp (p
, "data") == 0)
34685 cp_lexer_consume_token (parser
->lexer
);
34691 cp_parser_error (parser
, "expected %<data%>");
34692 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34696 if (context
== pragma_stmt
)
34698 error_at (pragma_tok
->location
,
34699 "%<#pragma omp target enter data%> may only be "
34700 "used in compound statements");
34701 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34706 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
34707 "#pragma omp target enter data", pragma_tok
);
34709 for (tree
*pc
= &clauses
; *pc
;)
34711 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34712 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34715 case GOMP_MAP_ALWAYS_TO
:
34716 case GOMP_MAP_ALLOC
:
34719 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34720 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34721 case GOMP_MAP_ALWAYS_POINTER
:
34725 error_at (OMP_CLAUSE_LOCATION (*pc
),
34726 "%<#pragma omp target enter data%> with map-type other "
34727 "than %<to%> or %<alloc%> on %<map%> clause");
34728 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34731 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34737 error_at (pragma_tok
->location
,
34738 "%<#pragma omp target enter data%> must contain at least "
34739 "one %<map%> clause");
34743 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
34744 TREE_TYPE (stmt
) = void_type_node
;
34745 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
34746 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34747 return add_stmt (stmt
);
34751 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34752 structured-block */
34754 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34762 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34763 enum pragma_context context
)
34765 bool data_seen
= false;
34766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34768 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34769 const char *p
= IDENTIFIER_POINTER (id
);
34771 if (strcmp (p
, "data") == 0)
34773 cp_lexer_consume_token (parser
->lexer
);
34779 cp_parser_error (parser
, "expected %<data%>");
34780 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34784 if (context
== pragma_stmt
)
34786 error_at (pragma_tok
->location
,
34787 "%<#pragma omp target exit data%> may only be "
34788 "used in compound statements");
34789 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34794 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
34795 "#pragma omp target exit data", pragma_tok
);
34797 for (tree
*pc
= &clauses
; *pc
;)
34799 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34800 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34802 case GOMP_MAP_FROM
:
34803 case GOMP_MAP_ALWAYS_FROM
:
34804 case GOMP_MAP_RELEASE
:
34805 case GOMP_MAP_DELETE
:
34808 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34809 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34810 case GOMP_MAP_ALWAYS_POINTER
:
34814 error_at (OMP_CLAUSE_LOCATION (*pc
),
34815 "%<#pragma omp target exit data%> with map-type other "
34816 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34818 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34821 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34827 error_at (pragma_tok
->location
,
34828 "%<#pragma omp target exit data%> must contain at least "
34829 "one %<map%> clause");
34833 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
34834 TREE_TYPE (stmt
) = void_type_node
;
34835 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
34836 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34837 return add_stmt (stmt
);
34841 # pragma omp target update target-update-clause[optseq] new-line */
34843 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34852 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
34853 enum pragma_context context
)
34855 if (context
== pragma_stmt
)
34857 error_at (pragma_tok
->location
,
34858 "%<#pragma omp target update%> may only be "
34859 "used in compound statements");
34860 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34865 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
34866 "#pragma omp target update", pragma_tok
);
34867 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
34868 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
34870 error_at (pragma_tok
->location
,
34871 "%<#pragma omp target update%> must contain at least one "
34872 "%<from%> or %<to%> clauses");
34876 tree stmt
= make_node (OMP_TARGET_UPDATE
);
34877 TREE_TYPE (stmt
) = void_type_node
;
34878 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
34879 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34885 # pragma omp target target-clause[optseq] new-line
34886 structured-block */
34888 #define OMP_TARGET_CLAUSE_MASK \
34889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34900 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
34901 enum pragma_context context
, bool *if_p
)
34903 tree
*pc
= NULL
, stmt
;
34905 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
34907 cp_parser_error (parser
, "expected declaration specifiers");
34908 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34912 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34914 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34915 const char *p
= IDENTIFIER_POINTER (id
);
34916 enum tree_code ccode
= ERROR_MARK
;
34918 if (strcmp (p
, "teams") == 0)
34920 else if (strcmp (p
, "parallel") == 0)
34921 ccode
= OMP_PARALLEL
;
34922 else if (strcmp (p
, "simd") == 0)
34924 if (ccode
!= ERROR_MARK
)
34926 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
34927 char p_name
[sizeof ("#pragma omp target teams distribute "
34928 "parallel for simd")];
34930 cp_lexer_consume_token (parser
->lexer
);
34931 strcpy (p_name
, "#pragma omp target");
34932 if (!flag_openmp
) /* flag_openmp_simd */
34938 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34939 OMP_TARGET_CLAUSE_MASK
,
34943 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34944 OMP_TARGET_CLAUSE_MASK
,
34948 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34949 OMP_TARGET_CLAUSE_MASK
,
34953 gcc_unreachable ();
34955 return stmt
!= NULL_TREE
;
34957 keep_next_level (true);
34958 tree sb
= begin_omp_structured_block (), ret
;
34959 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
34963 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34964 OMP_TARGET_CLAUSE_MASK
, cclauses
,
34968 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34969 OMP_TARGET_CLAUSE_MASK
, cclauses
,
34973 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34974 OMP_TARGET_CLAUSE_MASK
, cclauses
,
34978 gcc_unreachable ();
34980 cp_parser_end_omp_structured_block (parser
, save
);
34981 tree body
= finish_omp_structured_block (sb
);
34982 if (ret
== NULL_TREE
)
34984 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
34986 /* For combined target teams, ensure the num_teams and
34987 thread_limit clause expressions are evaluated on the host,
34988 before entering the target construct. */
34990 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
34991 c
; c
= OMP_CLAUSE_CHAIN (c
))
34992 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
34993 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
34994 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
34996 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
34997 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
34998 if (expr
== error_mark_node
)
35000 tree tmp
= TARGET_EXPR_SLOT (expr
);
35002 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
35003 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
35004 OMP_CLAUSE_FIRSTPRIVATE
);
35005 OMP_CLAUSE_DECL (tc
) = tmp
;
35006 OMP_CLAUSE_CHAIN (tc
)
35007 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35008 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
35011 tree stmt
= make_node (OMP_TARGET
);
35012 TREE_TYPE (stmt
) = void_type_node
;
35013 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
35014 OMP_TARGET_BODY (stmt
) = body
;
35015 OMP_TARGET_COMBINED (stmt
) = 1;
35017 pc
= &OMP_TARGET_CLAUSES (stmt
);
35018 goto check_clauses
;
35020 else if (!flag_openmp
) /* flag_openmp_simd */
35022 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35025 else if (strcmp (p
, "data") == 0)
35027 cp_lexer_consume_token (parser
->lexer
);
35028 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
35031 else if (strcmp (p
, "enter") == 0)
35033 cp_lexer_consume_token (parser
->lexer
);
35034 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
35037 else if (strcmp (p
, "exit") == 0)
35039 cp_lexer_consume_token (parser
->lexer
);
35040 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
35043 else if (strcmp (p
, "update") == 0)
35045 cp_lexer_consume_token (parser
->lexer
);
35046 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
35050 stmt
= make_node (OMP_TARGET
);
35051 TREE_TYPE (stmt
) = void_type_node
;
35053 OMP_TARGET_CLAUSES (stmt
)
35054 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
35055 "#pragma omp target", pragma_tok
);
35056 pc
= &OMP_TARGET_CLAUSES (stmt
);
35057 keep_next_level (true);
35058 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
35060 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35066 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
35067 switch (OMP_CLAUSE_MAP_KIND (*pc
))
35070 case GOMP_MAP_ALWAYS_TO
:
35071 case GOMP_MAP_FROM
:
35072 case GOMP_MAP_ALWAYS_FROM
:
35073 case GOMP_MAP_TOFROM
:
35074 case GOMP_MAP_ALWAYS_TOFROM
:
35075 case GOMP_MAP_ALLOC
:
35076 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
35077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
35078 case GOMP_MAP_ALWAYS_POINTER
:
35081 error_at (OMP_CLAUSE_LOCATION (*pc
),
35082 "%<#pragma omp target%> with map-type other "
35083 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35084 "on %<map%> clause");
35085 *pc
= OMP_CLAUSE_CHAIN (*pc
);
35088 pc
= &OMP_CLAUSE_CHAIN (*pc
);
35094 # pragma acc cache (variable-list) new-line
35098 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
35100 tree stmt
, clauses
;
35102 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
35103 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
35105 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
35107 stmt
= make_node (OACC_CACHE
);
35108 TREE_TYPE (stmt
) = void_type_node
;
35109 OACC_CACHE_CLAUSES (stmt
) = clauses
;
35110 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35117 # pragma acc data oacc-data-clause[optseq] new-line
35118 structured-block */
35120 #define OACC_DATA_CLAUSE_MASK \
35121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35134 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35136 tree stmt
, clauses
, block
;
35139 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
35140 "#pragma acc data", pragma_tok
);
35142 block
= begin_omp_parallel ();
35143 save
= cp_parser_begin_omp_structured_block (parser
);
35144 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35145 cp_parser_end_omp_structured_block (parser
, save
);
35146 stmt
= finish_oacc_data (clauses
, block
);
35151 # pragma acc host_data <clauses> new-line
35152 structured-block */
35154 #define OACC_HOST_DATA_CLAUSE_MASK \
35155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35158 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
35160 tree stmt
, clauses
, block
;
35163 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
35164 "#pragma acc host_data", pragma_tok
);
35166 block
= begin_omp_parallel ();
35167 save
= cp_parser_begin_omp_structured_block (parser
);
35168 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35169 cp_parser_end_omp_structured_block (parser
, save
);
35170 stmt
= finish_oacc_host_data (clauses
, block
);
35175 # pragma acc declare oacc-data-clause[optseq] new-line
35178 #define OACC_DECLARE_CLAUSE_MASK \
35179 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35193 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
35195 tree clauses
, stmt
;
35196 bool error
= false;
35198 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
35199 "#pragma acc declare", pragma_tok
, true);
35202 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
35204 error_at (pragma_tok
->location
,
35205 "no valid clauses specified in %<#pragma acc declare%>");
35209 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
35211 location_t loc
= OMP_CLAUSE_LOCATION (t
);
35212 tree decl
= OMP_CLAUSE_DECL (t
);
35213 if (!DECL_P (decl
))
35215 error_at (loc
, "array section in %<#pragma acc declare%>");
35219 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
35220 switch (OMP_CLAUSE_MAP_KIND (t
))
35222 case GOMP_MAP_FORCE_ALLOC
:
35223 case GOMP_MAP_FORCE_TO
:
35224 case GOMP_MAP_FORCE_DEVICEPTR
:
35225 case GOMP_MAP_DEVICE_RESIDENT
:
35228 case GOMP_MAP_POINTER
:
35229 /* Generated by c_finish_omp_clauses from array sections;
35230 avoid spurious diagnostics. */
35233 case GOMP_MAP_LINK
:
35234 if (!global_bindings_p ()
35235 && (TREE_STATIC (decl
)
35236 || !DECL_EXTERNAL (decl
)))
35239 "%qD must be a global variable in"
35240 "%<#pragma acc declare link%>",
35248 if (global_bindings_p ())
35250 error_at (loc
, "invalid OpenACC clause at file scope");
35254 if (DECL_EXTERNAL (decl
))
35257 "invalid use of %<extern%> variable %qD "
35258 "in %<#pragma acc declare%>", decl
);
35262 else if (TREE_PUBLIC (decl
))
35265 "invalid use of %<global%> variable %qD "
35266 "in %<#pragma acc declare%>", decl
);
35273 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
35274 || lookup_attribute ("omp declare target link",
35275 DECL_ATTRIBUTES (decl
)))
35277 error_at (loc
, "variable %qD used more than once with "
35278 "%<#pragma acc declare%>", decl
);
35287 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
35288 id
= get_identifier ("omp declare target link");
35290 id
= get_identifier ("omp declare target");
35292 DECL_ATTRIBUTES (decl
)
35293 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
35294 if (global_bindings_p ())
35296 symtab_node
*node
= symtab_node::get (decl
);
35299 node
->offloadable
= 1;
35300 if (ENABLE_OFFLOADING
)
35302 g
->have_offload
= true;
35303 if (is_a
<varpool_node
*> (node
))
35304 vec_safe_push (offload_vars
, decl
);
35311 if (error
|| global_bindings_p ())
35314 stmt
= make_node (OACC_DECLARE
);
35315 TREE_TYPE (stmt
) = void_type_node
;
35316 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
35317 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35325 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35329 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35331 LOC is the location of the #pragma token.
35334 #define OACC_ENTER_DATA_CLAUSE_MASK \
35335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35343 #define OACC_EXIT_DATA_CLAUSE_MASK \
35344 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35351 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
35354 tree stmt
, clauses
;
35356 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
35357 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35359 cp_parser_error (parser
, enter
35360 ? "expected %<data%> in %<#pragma acc enter data%>"
35361 : "expected %<data%> in %<#pragma acc exit data%>");
35362 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35367 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
35368 if (strcmp (p
, "data") != 0)
35370 cp_parser_error (parser
, "invalid pragma");
35371 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35375 cp_lexer_consume_token (parser
->lexer
);
35378 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
35379 "#pragma acc enter data", pragma_tok
);
35381 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
35382 "#pragma acc exit data", pragma_tok
);
35384 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
35386 error_at (pragma_tok
->location
,
35387 "%<#pragma acc enter data%> has no data movement clause");
35391 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
35392 TREE_TYPE (stmt
) = void_type_node
;
35393 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
35394 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35400 # pragma acc loop oacc-loop-clause[optseq] new-line
35401 structured-block */
35403 #define OACC_LOOP_CLAUSE_MASK \
35404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35416 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
35417 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
35419 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
35421 strcat (p_name
, " loop");
35422 mask
|= OACC_LOOP_CLAUSE_MASK
;
35424 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35428 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
35430 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
35432 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
35435 tree block
= begin_omp_structured_block ();
35436 int save
= cp_parser_begin_omp_structured_block (parser
);
35437 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
35438 cp_parser_end_omp_structured_block (parser
, save
);
35439 add_stmt (finish_omp_structured_block (block
));
35445 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35450 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35454 #define OACC_KERNELS_CLAUSE_MASK \
35455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35470 #define OACC_PARALLEL_CLAUSE_MASK \
35471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35493 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
35494 char *p_name
, bool *if_p
)
35496 omp_clause_mask mask
;
35497 enum tree_code code
;
35498 switch (cp_parser_pragma_kind (pragma_tok
))
35500 case PRAGMA_OACC_KERNELS
:
35501 strcat (p_name
, " kernels");
35502 mask
= OACC_KERNELS_CLAUSE_MASK
;
35503 code
= OACC_KERNELS
;
35505 case PRAGMA_OACC_PARALLEL
:
35506 strcat (p_name
, " parallel");
35507 mask
= OACC_PARALLEL_CLAUSE_MASK
;
35508 code
= OACC_PARALLEL
;
35511 gcc_unreachable ();
35514 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35517 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
35518 if (strcmp (p
, "loop") == 0)
35520 cp_lexer_consume_token (parser
->lexer
);
35521 tree block
= begin_omp_parallel ();
35523 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
,
35525 return finish_omp_construct (code
, block
, clauses
);
35529 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
35531 tree block
= begin_omp_parallel ();
35532 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
35533 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
35534 cp_parser_end_omp_structured_block (parser
, save
);
35535 return finish_omp_construct (code
, block
, clauses
);
35539 # pragma acc update oacc-update-clause[optseq] new-line
35542 #define OACC_UPDATE_CLAUSE_MASK \
35543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35551 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
35553 tree stmt
, clauses
;
35555 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
35556 "#pragma acc update", pragma_tok
);
35558 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
35560 error_at (pragma_tok
->location
,
35561 "%<#pragma acc update%> must contain at least one "
35562 "%<device%> or %<host%> or %<self%> clause");
35566 stmt
= make_node (OACC_UPDATE
);
35567 TREE_TYPE (stmt
) = void_type_node
;
35568 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
35569 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
35575 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35577 LOC is the location of the #pragma token.
35580 #define OACC_WAIT_CLAUSE_MASK \
35581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35584 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
35586 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
35587 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35589 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
35590 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
35592 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
35593 "#pragma acc wait", pragma_tok
);
35595 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
35596 stmt
= finish_expr_stmt (stmt
);
35602 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35604 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35613 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
35614 enum pragma_context context
)
35616 bool first_p
= parser
->omp_declare_simd
== NULL
;
35617 cp_omp_declare_simd_data data
;
35620 data
.error_seen
= false;
35621 data
.fndecl_seen
= false;
35622 data
.tokens
= vNULL
;
35623 parser
->omp_declare_simd
= &data
;
35625 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
35626 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
35627 cp_lexer_consume_token (parser
->lexer
);
35628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35629 parser
->omp_declare_simd
->error_seen
= true;
35630 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35631 struct cp_token_cache
*cp
35632 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
35633 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
35636 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
35637 cp_parser_pragma (parser
, context
, NULL
);
35640 case pragma_external
:
35641 cp_parser_declaration (parser
);
35643 case pragma_member
:
35644 cp_parser_member_declaration (parser
);
35646 case pragma_objc_icode
:
35647 cp_parser_block_declaration (parser
, /*statement_p=*/false);
35650 cp_parser_declaration_statement (parser
);
35653 if (parser
->omp_declare_simd
35654 && !parser
->omp_declare_simd
->error_seen
35655 && !parser
->omp_declare_simd
->fndecl_seen
)
35656 error_at (pragma_tok
->location
,
35657 "%<#pragma omp declare simd%> not immediately followed by "
35658 "function declaration or definition");
35659 data
.tokens
.release ();
35660 parser
->omp_declare_simd
= NULL
;
35664 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35665 This function is modelled similar to the late parsing of omp declare
35669 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
35671 struct cp_token_cache
*ce
;
35672 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
35675 if (parser
->omp_declare_simd
!= NULL
35676 || lookup_attribute ("simd", attrs
))
35678 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35679 "used in the same function marked as a Cilk Plus SIMD-enabled "
35681 parser
->cilk_simd_fn_info
->tokens
.release ();
35682 XDELETE (parser
->cilk_simd_fn_info
);
35683 parser
->cilk_simd_fn_info
= NULL
;
35686 if (!info
->error_seen
&& info
->fndecl_seen
)
35688 error ("vector attribute not immediately followed by a single function"
35689 " declaration or definition");
35690 info
->error_seen
= true;
35692 if (info
->error_seen
)
35695 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
35699 cp_parser_push_lexer_for_tokens (parser
, ce
);
35700 parser
->lexer
->in_pragma
= true;
35701 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
35702 "SIMD-enabled functions attribute",
35704 cp_parser_pop_lexer (parser
);
35706 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
35708 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
35709 TREE_CHAIN (c
) = attrs
;
35712 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
35713 TREE_CHAIN (c
) = attrs
;
35714 if (processing_template_decl
)
35715 ATTR_IS_DEPENDENT (c
) = 1;
35718 info
->fndecl_seen
= true;
35719 parser
->cilk_simd_fn_info
->tokens
.release ();
35720 XDELETE (parser
->cilk_simd_fn_info
);
35721 parser
->cilk_simd_fn_info
= NULL
;
35725 /* Finalize #pragma omp declare simd clauses after direct declarator has
35726 been parsed, and put that into "omp declare simd" attribute. */
35729 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
35731 struct cp_token_cache
*ce
;
35732 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
35735 if (!data
->error_seen
&& data
->fndecl_seen
)
35737 error ("%<#pragma omp declare simd%> not immediately followed by "
35738 "a single function declaration or definition");
35739 data
->error_seen
= true;
35742 if (data
->error_seen
)
35745 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
35749 cp_parser_push_lexer_for_tokens (parser
, ce
);
35750 parser
->lexer
->in_pragma
= true;
35751 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
35752 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
35753 cp_lexer_consume_token (parser
->lexer
);
35754 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
35755 "#pragma omp declare simd", pragma_tok
);
35756 cp_parser_pop_lexer (parser
);
35758 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
35759 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
35760 TREE_CHAIN (c
) = attrs
;
35761 if (processing_template_decl
)
35762 ATTR_IS_DEPENDENT (c
) = 1;
35766 data
->fndecl_seen
= true;
35772 # pragma omp declare target new-line
35773 declarations and definitions
35774 # pragma omp end declare target new-line
35777 # pragma omp declare target ( extended-list ) new-line
35779 # pragma omp declare target declare-target-clauses[seq] new-line */
35781 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35786 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
35788 tree clauses
= NULL_TREE
;
35789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35791 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
35792 "#pragma omp declare target", pragma_tok
);
35793 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35795 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35797 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
35798 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35802 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35803 scope_chain
->omp_declare_target_attribute
++;
35806 if (scope_chain
->omp_declare_target_attribute
)
35807 error_at (pragma_tok
->location
,
35808 "%<#pragma omp declare target%> with clauses in between "
35809 "%<#pragma omp declare target%> without clauses and "
35810 "%<#pragma omp end declare target%>");
35811 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35813 tree t
= OMP_CLAUSE_DECL (c
), id
;
35814 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
35815 tree at2
= lookup_attribute ("omp declare target link",
35816 DECL_ATTRIBUTES (t
));
35817 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
35819 id
= get_identifier ("omp declare target link");
35820 std::swap (at1
, at2
);
35823 id
= get_identifier ("omp declare target");
35826 error_at (OMP_CLAUSE_LOCATION (c
),
35827 "%qD specified both in declare target %<link%> and %<to%>"
35833 symtab_node
*node
= symtab_node::get (t
);
35834 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
35837 node
->offloadable
= 1;
35838 if (ENABLE_OFFLOADING
)
35840 g
->have_offload
= true;
35841 if (is_a
<varpool_node
*> (node
))
35842 vec_safe_push (offload_vars
, t
);
35850 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
35852 const char *p
= "";
35853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35855 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35856 p
= IDENTIFIER_POINTER (id
);
35858 if (strcmp (p
, "declare") == 0)
35860 cp_lexer_consume_token (parser
->lexer
);
35862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35864 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35865 p
= IDENTIFIER_POINTER (id
);
35867 if (strcmp (p
, "target") == 0)
35868 cp_lexer_consume_token (parser
->lexer
);
35871 cp_parser_error (parser
, "expected %<target%>");
35872 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35878 cp_parser_error (parser
, "expected %<declare%>");
35879 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35882 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35883 if (!scope_chain
->omp_declare_target_attribute
)
35884 error_at (pragma_tok
->location
,
35885 "%<#pragma omp end declare target%> without corresponding "
35886 "%<#pragma omp declare target%>");
35888 scope_chain
->omp_declare_target_attribute
--;
35891 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35892 expression and optional initializer clause of
35893 #pragma omp declare reduction. We store the expression(s) as
35894 either 3, 6 or 7 special statements inside of the artificial function's
35895 body. The first two statements are DECL_EXPRs for the artificial
35896 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35897 expression that uses those variables.
35898 If there was any INITIALIZER clause, this is followed by further statements,
35899 the fourth and fifth statements are DECL_EXPRs for the artificial
35900 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35901 constructor variant (first token after open paren is not omp_priv),
35902 then the sixth statement is a statement with the function call expression
35903 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35904 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35905 to initialize the OMP_PRIV artificial variable and there is seventh
35906 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35909 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
35911 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
35912 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
35913 type
= TREE_TYPE (type
);
35914 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
35915 DECL_ARTIFICIAL (omp_out
) = 1;
35916 pushdecl (omp_out
);
35917 add_decl_expr (omp_out
);
35918 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
35919 DECL_ARTIFICIAL (omp_in
) = 1;
35921 add_decl_expr (omp_in
);
35923 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
35925 keep_next_level (true);
35926 tree block
= begin_omp_structured_block ();
35927 combiner
= cp_parser_expression (parser
);
35928 finish_expr_stmt (combiner
);
35929 block
= finish_omp_structured_block (block
);
35932 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35935 const char *p
= "";
35936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35938 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35939 p
= IDENTIFIER_POINTER (id
);
35942 if (strcmp (p
, "initializer") == 0)
35944 cp_lexer_consume_token (parser
->lexer
);
35945 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35951 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35952 p
= IDENTIFIER_POINTER (id
);
35955 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
35956 DECL_ARTIFICIAL (omp_priv
) = 1;
35957 pushdecl (omp_priv
);
35958 add_decl_expr (omp_priv
);
35959 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
35960 DECL_ARTIFICIAL (omp_orig
) = 1;
35961 pushdecl (omp_orig
);
35962 add_decl_expr (omp_orig
);
35964 keep_next_level (true);
35965 block
= begin_omp_structured_block ();
35968 if (strcmp (p
, "omp_priv") == 0)
35970 bool is_direct_init
, is_non_constant_init
;
35972 cp_lexer_consume_token (parser
->lexer
);
35973 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
35975 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
35976 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
35978 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
35979 == CPP_CLOSE_PAREN
))
35981 finish_omp_structured_block (block
);
35982 error ("invalid initializer clause");
35985 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
35986 &is_non_constant_init
);
35987 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
35988 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
35992 cp_parser_parse_tentatively (parser
);
35993 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35994 /*check_dependency_p=*/true,
35995 /*template_p=*/NULL
,
35996 /*declarator_p=*/false,
35997 /*optional_p=*/false);
35998 vec
<tree
, va_gc
> *args
;
35999 if (fn_name
== error_mark_node
36000 || cp_parser_error_occurred (parser
)
36001 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
36002 || ((args
= cp_parser_parenthesized_expression_list
36003 (parser
, non_attr
, /*cast_p=*/false,
36004 /*allow_expansion_p=*/true,
36005 /*non_constant_p=*/NULL
)),
36006 cp_parser_error_occurred (parser
)))
36008 finish_omp_structured_block (block
);
36009 cp_parser_abort_tentative_parse (parser
);
36010 cp_parser_error (parser
, "expected id-expression (arguments)");
36015 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
36016 if (arg
== omp_priv
36017 || (TREE_CODE (arg
) == ADDR_EXPR
36018 && TREE_OPERAND (arg
, 0) == omp_priv
))
36020 cp_parser_abort_tentative_parse (parser
);
36021 if (arg
== NULL_TREE
)
36022 error ("one of the initializer call arguments should be %<omp_priv%>"
36023 " or %<&omp_priv%>");
36024 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
36026 finish_expr_stmt (initializer
);
36029 block
= finish_omp_structured_block (block
);
36030 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
36034 add_decl_expr (omp_orig
);
36036 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36040 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
36041 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
36047 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36048 initializer-clause[opt] new-line
36050 initializer-clause:
36051 initializer (omp_priv initializer)
36052 initializer (function-name (argument-list)) */
36055 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
36056 enum pragma_context
)
36058 auto_vec
<tree
> types
;
36059 enum tree_code reduc_code
= ERROR_MARK
;
36060 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
36062 cp_token
*first_token
;
36063 cp_token_cache
*cp
;
36067 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36068 p
= obstack_alloc (&declarator_obstack
, 0);
36070 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36073 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
36076 reduc_code
= PLUS_EXPR
;
36079 reduc_code
= MULT_EXPR
;
36082 reduc_code
= MINUS_EXPR
;
36085 reduc_code
= BIT_AND_EXPR
;
36088 reduc_code
= BIT_XOR_EXPR
;
36091 reduc_code
= BIT_IOR_EXPR
;
36094 reduc_code
= TRUTH_ANDIF_EXPR
;
36097 reduc_code
= TRUTH_ORIF_EXPR
;
36100 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
36103 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36104 "%<|%>, %<&&%>, %<||%> or identifier");
36108 if (reduc_code
!= ERROR_MARK
)
36109 cp_lexer_consume_token (parser
->lexer
);
36111 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
36112 if (reduc_id
== error_mark_node
)
36115 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36118 /* Types may not be defined in declare reduction type list. */
36119 const char *saved_message
;
36120 saved_message
= parser
->type_definition_forbidden_message
;
36121 parser
->type_definition_forbidden_message
36122 = G_("types may not be defined in declare reduction type list");
36123 bool saved_colon_corrects_to_scope_p
;
36124 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36125 parser
->colon_corrects_to_scope_p
= false;
36126 bool saved_colon_doesnt_start_class_def_p
;
36127 saved_colon_doesnt_start_class_def_p
36128 = parser
->colon_doesnt_start_class_def_p
;
36129 parser
->colon_doesnt_start_class_def_p
= true;
36133 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36134 type
= cp_parser_type_id (parser
);
36135 if (type
== error_mark_node
)
36137 else if (ARITHMETIC_TYPE_P (type
)
36138 && (orig_reduc_id
== NULL_TREE
36139 || (TREE_CODE (type
) != COMPLEX_TYPE
36140 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
36142 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
36144 error_at (loc
, "predeclared arithmetic type %qT in "
36145 "%<#pragma omp declare reduction%>", type
);
36146 else if (TREE_CODE (type
) == FUNCTION_TYPE
36147 || TREE_CODE (type
) == METHOD_TYPE
36148 || TREE_CODE (type
) == ARRAY_TYPE
)
36149 error_at (loc
, "function or array type %qT in "
36150 "%<#pragma omp declare reduction%>", type
);
36151 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
36152 error_at (loc
, "reference type %qT in "
36153 "%<#pragma omp declare reduction%>", type
);
36154 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
36155 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
36156 "%<#pragma omp declare reduction%>", type
);
36158 types
.safe_push (type
);
36160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36161 cp_lexer_consume_token (parser
->lexer
);
36166 /* Restore the saved message. */
36167 parser
->type_definition_forbidden_message
= saved_message
;
36168 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36169 parser
->colon_doesnt_start_class_def_p
36170 = saved_colon_doesnt_start_class_def_p
;
36172 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
36173 || types
.is_empty ())
36176 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36180 first_token
= cp_lexer_peek_token (parser
->lexer
);
36183 FOR_EACH_VEC_ELT (types
, i
, type
)
36186 = build_function_type_list (void_type_node
,
36187 cp_build_reference_type (type
, false),
36189 tree this_reduc_id
= reduc_id
;
36190 if (!dependent_type_p (type
))
36191 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
36192 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
36193 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
36194 DECL_ARTIFICIAL (fndecl
) = 1;
36195 DECL_EXTERNAL (fndecl
) = 1;
36196 DECL_DECLARED_INLINE_P (fndecl
) = 1;
36197 DECL_IGNORED_P (fndecl
) = 1;
36198 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
36199 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
36200 DECL_ATTRIBUTES (fndecl
)
36201 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
36202 DECL_ATTRIBUTES (fndecl
));
36203 if (processing_template_decl
)
36204 fndecl
= push_template_decl (fndecl
);
36205 bool block_scope
= false;
36206 tree block
= NULL_TREE
;
36207 if (current_function_decl
)
36209 block_scope
= true;
36210 DECL_CONTEXT (fndecl
) = global_namespace
;
36211 if (!processing_template_decl
)
36214 else if (current_class_type
)
36218 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36219 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36220 cp_lexer_consume_token (parser
->lexer
);
36221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36223 cp
= cp_token_cache_new (first_token
,
36224 cp_lexer_peek_nth_token (parser
->lexer
,
36227 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
36228 finish_member_declaration (fndecl
);
36229 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
36230 DECL_PENDING_INLINE_P (fndecl
) = 1;
36231 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
36236 DECL_CONTEXT (fndecl
) = current_namespace
;
36240 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
36242 block
= begin_omp_structured_block ();
36245 cp_parser_push_lexer_for_tokens (parser
, cp
);
36246 parser
->lexer
->in_pragma
= true;
36248 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
36251 finish_function (0);
36253 DECL_CONTEXT (fndecl
) = current_function_decl
;
36255 cp_parser_pop_lexer (parser
);
36259 cp_parser_pop_lexer (parser
);
36261 finish_function (0);
36264 DECL_CONTEXT (fndecl
) = current_function_decl
;
36265 block
= finish_omp_structured_block (block
);
36266 if (TREE_CODE (block
) == BIND_EXPR
)
36267 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
36268 else if (TREE_CODE (block
) == STATEMENT_LIST
)
36269 DECL_SAVED_TREE (fndecl
) = block
;
36270 if (processing_template_decl
)
36271 add_decl_expr (fndecl
);
36273 cp_check_omp_declare_reduction (fndecl
);
36274 if (cp
== NULL
&& types
.length () > 1)
36275 cp
= cp_token_cache_new (first_token
,
36276 cp_lexer_peek_nth_token (parser
->lexer
, 2));
36277 if (errs
!= errorcount
)
36281 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36284 /* Free any declarators allocated. */
36285 obstack_free (&declarator_obstack
, p
);
36289 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36290 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36291 initializer-clause[opt] new-line
36292 #pragma omp declare target new-line */
36295 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
36296 enum pragma_context context
)
36298 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36300 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36301 const char *p
= IDENTIFIER_POINTER (id
);
36303 if (strcmp (p
, "simd") == 0)
36305 cp_lexer_consume_token (parser
->lexer
);
36306 cp_parser_omp_declare_simd (parser
, pragma_tok
,
36310 cp_ensure_no_omp_declare_simd (parser
);
36311 if (strcmp (p
, "reduction") == 0)
36313 cp_lexer_consume_token (parser
->lexer
);
36314 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
36318 if (!flag_openmp
) /* flag_openmp_simd */
36320 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36323 if (strcmp (p
, "target") == 0)
36325 cp_lexer_consume_token (parser
->lexer
);
36326 cp_parser_omp_declare_target (parser
, pragma_tok
);
36330 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
36332 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36336 #pragma omp taskloop taskloop-clause[optseq] new-line
36339 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36342 #define OMP_TASKLOOP_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36359 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
36360 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
36363 tree clauses
, sb
, ret
;
36365 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36367 strcat (p_name
, " taskloop");
36368 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
36370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36372 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36373 const char *p
= IDENTIFIER_POINTER (id
);
36375 if (strcmp (p
, "simd") == 0)
36377 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
36378 if (cclauses
== NULL
)
36379 cclauses
= cclauses_buf
;
36381 cp_lexer_consume_token (parser
->lexer
);
36382 if (!flag_openmp
) /* flag_openmp_simd */
36383 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36385 sb
= begin_omp_structured_block ();
36386 save
= cp_parser_begin_omp_structured_block (parser
);
36387 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
36389 cp_parser_end_omp_structured_block (parser
, save
);
36390 tree body
= finish_omp_structured_block (sb
);
36393 ret
= make_node (OMP_TASKLOOP
);
36394 TREE_TYPE (ret
) = void_type_node
;
36395 OMP_FOR_BODY (ret
) = body
;
36396 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
36397 SET_EXPR_LOCATION (ret
, loc
);
36402 if (!flag_openmp
) /* flag_openmp_simd */
36404 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36408 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
36412 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
36413 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
36416 sb
= begin_omp_structured_block ();
36417 save
= cp_parser_begin_omp_structured_block (parser
);
36419 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
36422 cp_parser_end_omp_structured_block (parser
, save
);
36423 add_stmt (finish_omp_structured_block (sb
));
36430 # pragma acc routine oacc-routine-clause[optseq] new-line
36431 function-definition
36433 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36436 #define OACC_ROUTINE_CLAUSE_MASK \
36437 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36443 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36444 component, which must resolve to a declared namespace-scope
36445 function. The clauses are either processed directly (for a named
36446 function), or defered until the immediatley following declaration
36450 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
36451 enum pragma_context context
)
36453 bool first_p
= parser
->oacc_routine
== NULL
;
36454 location_t loc
= pragma_tok
->location
;
36455 cp_omp_declare_simd_data data
;
36458 data
.error_seen
= false;
36459 data
.fndecl_seen
= false;
36460 data
.tokens
= vNULL
;
36461 data
.clauses
= NULL_TREE
;
36462 parser
->oacc_routine
= &data
;
36465 tree decl
= NULL_TREE
;
36466 /* Create a dummy claue, to record location. */
36467 tree c_head
= build_omp_clause (pragma_tok
->location
, OMP_CLAUSE_SEQ
);
36469 if (context
!= pragma_external
)
36471 cp_parser_error (parser
, "%<#pragma acc routine%> not at file scope");
36472 parser
->oacc_routine
->error_seen
= true;
36473 parser
->oacc_routine
= NULL
;
36477 /* Look for optional '( name )'. */
36478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36482 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36483 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36484 cp_lexer_consume_token (parser
->lexer
);
36485 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36486 parser
->oacc_routine
->error_seen
= true;
36487 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36489 error_at (OMP_CLAUSE_LOCATION (parser
->oacc_routine
->clauses
),
36490 "%<#pragma acc routine%> not followed by a "
36491 "function declaration or definition");
36493 parser
->oacc_routine
->error_seen
= true;
36497 cp_lexer_consume_token (parser
->lexer
);
36498 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36500 /* We parse the name as an id-expression. If it resolves to
36501 anything other than a non-overloaded function at namespace
36502 scope, it's an error. */
36503 tree id
= cp_parser_id_expression (parser
,
36504 /*template_keyword_p=*/false,
36505 /*check_dependency_p=*/false,
36506 /*template_p=*/NULL
,
36507 /*declarator_p=*/false,
36508 /*optional_p=*/false);
36509 decl
= cp_parser_lookup_name_simple (parser
, id
, token
->location
);
36510 if (id
!= error_mark_node
&& decl
== error_mark_node
)
36511 cp_parser_name_lookup_error (parser
, id
, decl
, NLE_NULL
,
36514 if (decl
== error_mark_node
36515 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36517 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36518 parser
->oacc_routine
= NULL
;
36522 /* Build a chain of clauses. */
36523 parser
->lexer
->in_pragma
= true;
36524 tree clauses
= NULL_TREE
;
36525 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
36526 "#pragma acc routine",
36527 cp_lexer_peek_token
36530 /* Force clauses to be non-null, by attaching context to it. */
36531 clauses
= tree_cons (c_head
, clauses
, NULL_TREE
);
36533 if (decl
&& is_overloaded_fn (decl
)
36534 && (TREE_CODE (decl
) != FUNCTION_DECL
36535 || DECL_FUNCTION_TEMPLATE_P (decl
)))
36537 error_at (loc
, "%<#pragma acc routine%> names a set of overloads");
36538 parser
->oacc_routine
= NULL
;
36542 /* Perhaps we should use the same rule as declarations in different
36544 if (!DECL_NAMESPACE_SCOPE_P (decl
))
36546 error_at (loc
, "%<#pragma acc routine%> does not refer to a "
36547 "namespace scope function");
36548 parser
->oacc_routine
= NULL
;
36552 if (!decl
|| TREE_CODE (decl
) != FUNCTION_DECL
)
36555 "%<#pragma acc routine%> does not refer to a function");
36556 parser
->oacc_routine
= NULL
;
36560 data
.clauses
= clauses
;
36562 cp_finalize_oacc_routine (parser
, decl
, false);
36563 data
.tokens
.release ();
36564 parser
->oacc_routine
= NULL
;
36568 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
36569 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
36570 cp_lexer_consume_token (parser
->lexer
);
36571 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36572 parser
->oacc_routine
->error_seen
= true;
36573 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36575 struct cp_token_cache
*cp
36576 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
36577 parser
->oacc_routine
->tokens
.safe_push (cp
);
36580 parser
->oacc_routine
->clauses
= c_head
;
36582 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
36583 cp_parser_pragma (parser
, context
, NULL
);
36587 /* Create an empty list of clauses. */
36588 parser
->oacc_routine
->clauses
= tree_cons (c_head
, NULL_TREE
,
36590 cp_parser_declaration (parser
);
36592 if (parser
->oacc_routine
36593 && !parser
->oacc_routine
->error_seen
36594 && !parser
->oacc_routine
->fndecl_seen
)
36595 error_at (loc
, "%<#pragma acc routine%> not followed by a "
36596 "function declaration or definition");
36598 data
.tokens
.release ();
36599 parser
->oacc_routine
= NULL
;
36604 /* Finalize #pragma acc routine clauses after direct declarator has
36605 been parsed, and put that into "oacc function" attribute. */
36608 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
36610 struct cp_token_cache
*ce
;
36611 cp_omp_declare_simd_data
*data
= parser
->oacc_routine
;
36612 tree cl
, clauses
= parser
->oacc_routine
->clauses
;
36615 loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses
));
36617 if ((!data
->error_seen
&& data
->fndecl_seen
)
36618 || data
->tokens
.length () != 1)
36620 error_at (loc
, "%<#pragma acc routine%> not followed by a "
36621 "function declaration or definition");
36622 data
->error_seen
= true;
36625 if (data
->error_seen
)
36628 ce
= data
->tokens
[0];
36630 cp_parser_push_lexer_for_tokens (parser
, ce
);
36631 parser
->lexer
->in_pragma
= true;
36632 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
36634 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36635 cl
= cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
36636 "#pragma acc routine", pragma_tok
);
36637 cp_parser_pop_lexer (parser
);
36639 tree c_head
= build_omp_clause (loc
, OMP_CLAUSE_SEQ
);
36641 /* Force clauses to be non-null, by attaching context to it. */
36642 parser
->oacc_routine
->clauses
= tree_cons (c_head
, cl
, NULL_TREE
);
36644 data
->fndecl_seen
= true;
36648 /* Apply any saved OpenACC routine clauses to a just-parsed
36652 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
36654 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
36656 tree clauses
= parser
->oacc_routine
->clauses
;
36657 location_t loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses
));
36659 if (parser
->oacc_routine
->error_seen
)
36662 if (fndecl
== error_mark_node
)
36664 parser
->oacc_routine
= NULL
;
36668 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
36670 cp_ensure_no_oacc_routine (parser
);
36674 if (!fndecl
|| TREE_CODE (fndecl
) != FUNCTION_DECL
)
36677 "%<#pragma acc routine%> not followed by a function "
36678 "declaration or definition");
36679 parser
->oacc_routine
= NULL
;
36682 if (get_oacc_fn_attrib (fndecl
))
36684 error_at (loc
, "%<#pragma acc routine%> already applied to %D",
36686 parser
->oacc_routine
= NULL
;
36689 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
36691 error_at (loc
, "%<#pragma acc routine%> must be applied before %s",
36692 TREE_USED (fndecl
) ? "use" : "definition");
36693 parser
->oacc_routine
= NULL
;
36696 /* Process for function attrib */
36697 tree dims
= build_oacc_routine_dims (TREE_VALUE (clauses
));
36698 replace_oacc_fn_attrib (fndecl
, dims
);
36700 /* Add an "omp target" attribute. */
36701 DECL_ATTRIBUTES (fndecl
)
36702 = tree_cons (get_identifier ("omp declare target"),
36703 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
36707 /* Main entry point to OpenMP statement pragmas. */
36710 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
36713 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
36714 omp_clause_mask
mask (0);
36716 switch (cp_parser_pragma_kind (pragma_tok
))
36718 case PRAGMA_OACC_ATOMIC
:
36719 cp_parser_omp_atomic (parser
, pragma_tok
);
36721 case PRAGMA_OACC_CACHE
:
36722 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
36724 case PRAGMA_OACC_DATA
:
36725 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
36727 case PRAGMA_OACC_ENTER_DATA
:
36728 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
36730 case PRAGMA_OACC_EXIT_DATA
:
36731 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
36733 case PRAGMA_OACC_HOST_DATA
:
36734 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
36736 case PRAGMA_OACC_KERNELS
:
36737 case PRAGMA_OACC_PARALLEL
:
36738 strcpy (p_name
, "#pragma acc");
36739 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
,
36742 case PRAGMA_OACC_LOOP
:
36743 strcpy (p_name
, "#pragma acc");
36744 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
36747 case PRAGMA_OACC_UPDATE
:
36748 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
36750 case PRAGMA_OACC_WAIT
:
36751 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
36753 case PRAGMA_OMP_ATOMIC
:
36754 cp_parser_omp_atomic (parser
, pragma_tok
);
36756 case PRAGMA_OMP_CRITICAL
:
36757 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
36759 case PRAGMA_OMP_DISTRIBUTE
:
36760 strcpy (p_name
, "#pragma omp");
36761 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
36764 case PRAGMA_OMP_FOR
:
36765 strcpy (p_name
, "#pragma omp");
36766 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
36769 case PRAGMA_OMP_MASTER
:
36770 stmt
= cp_parser_omp_master (parser
, pragma_tok
, if_p
);
36772 case PRAGMA_OMP_PARALLEL
:
36773 strcpy (p_name
, "#pragma omp");
36774 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
36777 case PRAGMA_OMP_SECTIONS
:
36778 strcpy (p_name
, "#pragma omp");
36779 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
36781 case PRAGMA_OMP_SIMD
:
36782 strcpy (p_name
, "#pragma omp");
36783 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
36786 case PRAGMA_OMP_SINGLE
:
36787 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
36789 case PRAGMA_OMP_TASK
:
36790 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
36792 case PRAGMA_OMP_TASKGROUP
:
36793 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
36795 case PRAGMA_OMP_TASKLOOP
:
36796 strcpy (p_name
, "#pragma omp");
36797 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
36800 case PRAGMA_OMP_TEAMS
:
36801 strcpy (p_name
, "#pragma omp");
36802 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
36806 gcc_unreachable ();
36809 protected_set_expr_location (stmt
, pragma_tok
->location
);
36812 /* Transactional Memory parsing routines. */
36814 /* Parse a transaction attribute.
36820 We use this instead of cp_parser_attributes_opt for transactions to avoid
36821 the pedwarn in C++98 mode. */
36824 cp_parser_txn_attribute_opt (cp_parser
*parser
)
36827 tree attr_name
, attr
= NULL
;
36829 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
36830 return cp_parser_attributes_opt (parser
);
36832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
36834 cp_lexer_consume_token (parser
->lexer
);
36835 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
36838 token
= cp_lexer_peek_token (parser
->lexer
);
36839 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
36841 token
= cp_lexer_consume_token (parser
->lexer
);
36843 attr_name
= (token
->type
== CPP_KEYWORD
36844 /* For keywords, use the canonical spelling,
36845 not the parsed identifier. */
36846 ? ridpointers
[(int) token
->keyword
]
36848 attr
= build_tree_list (attr_name
, NULL_TREE
);
36851 cp_parser_error (parser
, "expected identifier");
36853 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
36855 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
36859 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36861 transaction-statement:
36862 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36864 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36868 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
36870 unsigned char old_in
= parser
->in_transaction
;
36871 unsigned char this_in
= 1, new_in
;
36872 enum rid keyword
= token
->keyword
;
36873 tree stmt
, attrs
, noex
;
36875 cp_lexer_consume_token (parser
->lexer
);
36877 if (keyword
== RID_TRANSACTION_RELAXED
36878 || keyword
== RID_SYNCHRONIZED
)
36879 this_in
|= TM_STMT_ATTR_RELAXED
;
36882 attrs
= cp_parser_txn_attribute_opt (parser
);
36884 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
36887 /* Parse a noexcept specification. */
36888 if (keyword
== RID_ATOMIC_NOEXCEPT
)
36889 noex
= boolean_true_node
;
36890 else if (keyword
== RID_ATOMIC_CANCEL
)
36892 /* cancel-and-throw is unimplemented. */
36893 sorry ("atomic_cancel");
36897 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
36899 /* Keep track if we're in the lexical scope of an outer transaction. */
36900 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
36902 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
36904 parser
->in_transaction
= new_in
;
36905 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
36906 parser
->in_transaction
= old_in
;
36908 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
36913 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36915 transaction-expression:
36916 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36917 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36921 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
36923 unsigned char old_in
= parser
->in_transaction
;
36924 unsigned char this_in
= 1;
36928 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36930 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
36931 || keyword
== RID_TRANSACTION_RELAXED
);
36935 keyword
== RID_TRANSACTION_RELAXED
36936 ? G_("%<__transaction_relaxed%> without transactional memory "
36938 : G_("%<__transaction_atomic%> without transactional memory "
36939 "support enabled"));
36941 token
= cp_parser_require_keyword (parser
, keyword
,
36942 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
36943 : RT_TRANSACTION_RELAXED
));
36944 gcc_assert (token
!= NULL
);
36946 if (keyword
== RID_TRANSACTION_RELAXED
)
36947 this_in
|= TM_STMT_ATTR_RELAXED
;
36949 /* Set this early. This might mean that we allow transaction_cancel in
36950 an expression that we find out later actually has to be a constexpr.
36951 However, we expect that cxx_constant_value will be able to deal with
36952 this; also, if the noexcept has no constexpr, then what we parse next
36953 really is a transaction's body. */
36954 parser
->in_transaction
= this_in
;
36956 /* Parse a noexcept specification. */
36957 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
36960 if (!noex
|| !noex_expr
36961 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36963 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
36965 expr
= cp_parser_expression (parser
);
36966 expr
= finish_parenthesized_expr (expr
);
36968 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
36972 /* The only expression that is available got parsed for the noexcept
36973 already. noexcept is true then. */
36975 noex
= boolean_true_node
;
36978 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
36979 parser
->in_transaction
= old_in
;
36981 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
36982 return error_mark_node
;
36984 return (flag_tm
? expr
: error_mark_node
);
36987 /* Parse a function-transaction-block.
36989 function-transaction-block:
36990 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36992 __transaction_atomic txn-attribute[opt] function-try-block
36993 __transaction_relaxed ctor-initializer[opt] function-body
36994 __transaction_relaxed function-try-block
36998 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
37000 unsigned char old_in
= parser
->in_transaction
;
37001 unsigned char new_in
= 1;
37002 tree compound_stmt
, stmt
, attrs
;
37003 bool ctor_initializer_p
;
37006 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
37007 || keyword
== RID_TRANSACTION_RELAXED
);
37008 token
= cp_parser_require_keyword (parser
, keyword
,
37009 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
37010 : RT_TRANSACTION_RELAXED
));
37011 gcc_assert (token
!= NULL
);
37013 if (keyword
== RID_TRANSACTION_RELAXED
)
37014 new_in
|= TM_STMT_ATTR_RELAXED
;
37017 attrs
= cp_parser_txn_attribute_opt (parser
);
37019 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
37022 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
37024 parser
->in_transaction
= new_in
;
37026 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
37027 ctor_initializer_p
= cp_parser_function_try_block (parser
);
37029 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
37030 (parser
, /*in_function_try_block=*/false);
37032 parser
->in_transaction
= old_in
;
37034 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
37036 return ctor_initializer_p
;
37039 /* Parse a __transaction_cancel statement.
37042 __transaction_cancel txn-attribute[opt] ;
37043 __transaction_cancel txn-attribute[opt] throw-expression ;
37045 ??? Cancel and throw is not yet implemented. */
37048 cp_parser_transaction_cancel (cp_parser
*parser
)
37051 bool is_outer
= false;
37054 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
37055 RT_TRANSACTION_CANCEL
);
37056 gcc_assert (token
!= NULL
);
37058 attrs
= cp_parser_txn_attribute_opt (parser
);
37060 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
37062 /* ??? Parse cancel-and-throw here. */
37064 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
37068 error_at (token
->location
, "%<__transaction_cancel%> without "
37069 "transactional memory support enabled");
37070 return error_mark_node
;
37072 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
37074 error_at (token
->location
, "%<__transaction_cancel%> within a "
37075 "%<__transaction_relaxed%>");
37076 return error_mark_node
;
37080 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
37081 && !is_tm_may_cancel_outer (current_function_decl
))
37083 error_at (token
->location
, "outer %<__transaction_cancel%> not "
37084 "within outer %<__transaction_atomic%>");
37085 error_at (token
->location
,
37086 " or a %<transaction_may_cancel_outer%> function");
37087 return error_mark_node
;
37090 else if (parser
->in_transaction
== 0)
37092 error_at (token
->location
, "%<__transaction_cancel%> not within "
37093 "%<__transaction_atomic%>");
37094 return error_mark_node
;
37097 stmt
= build_tm_abort_call (token
->location
, is_outer
);
37105 static GTY (()) cp_parser
*the_parser
;
37108 /* Special handling for the first token or line in the file. The first
37109 thing in the file might be #pragma GCC pch_preprocess, which loads a
37110 PCH file, which is a GC collection point. So we need to handle this
37111 first pragma without benefit of an existing lexer structure.
37113 Always returns one token to the caller in *FIRST_TOKEN. This is
37114 either the true first token of the file, or the first token after
37115 the initial pragma. */
37118 cp_parser_initial_pragma (cp_token
*first_token
)
37122 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37123 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
37126 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37127 if (first_token
->type
== CPP_STRING
)
37129 name
= first_token
->u
.value
;
37131 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37132 if (first_token
->type
!= CPP_PRAGMA_EOL
)
37133 error_at (first_token
->location
,
37134 "junk at end of %<#pragma GCC pch_preprocess%>");
37137 error_at (first_token
->location
, "expected string literal");
37139 /* Skip to the end of the pragma. */
37140 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
37141 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37143 /* Now actually load the PCH file. */
37145 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
37147 /* Read one more token to return to our caller. We have to do this
37148 after reading the PCH file in, since its pointers have to be
37150 cp_lexer_get_preprocessor_token (NULL
, first_token
);
37153 /* Parses the grainsize pragma for the _Cilk_for statement.
37155 #pragma cilk grainsize = <VALUE>. */
37158 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37160 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37162 tree exp
= cp_parser_binary_expression (parser
, false, false,
37163 PREC_NOT_OPERATOR
, NULL
);
37164 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37165 if (!exp
|| exp
== error_mark_node
)
37167 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
37171 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37172 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
37173 cp_parser_cilk_for (parser
, exp
, if_p
);
37175 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
37176 "%<#pragma cilk grainsize%> is not followed by "
37180 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37183 /* Normal parsing of a pragma token. Here we can (and must) use the
37187 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
37189 cp_token
*pragma_tok
;
37194 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
37195 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
37196 parser
->lexer
->in_pragma
= true;
37198 id
= cp_parser_pragma_kind (pragma_tok
);
37199 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
&& id
!= PRAGMA_OACC_ROUTINE
)
37200 cp_ensure_no_omp_declare_simd (parser
);
37203 case PRAGMA_GCC_PCH_PREPROCESS
:
37204 error_at (pragma_tok
->location
,
37205 "%<#pragma GCC pch_preprocess%> must be first");
37208 case PRAGMA_OMP_BARRIER
:
37211 case pragma_compound
:
37212 cp_parser_omp_barrier (parser
, pragma_tok
);
37215 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
37216 "used in compound statements");
37223 case PRAGMA_OMP_FLUSH
:
37226 case pragma_compound
:
37227 cp_parser_omp_flush (parser
, pragma_tok
);
37230 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
37231 "used in compound statements");
37238 case PRAGMA_OMP_TASKWAIT
:
37241 case pragma_compound
:
37242 cp_parser_omp_taskwait (parser
, pragma_tok
);
37245 error_at (pragma_tok
->location
,
37246 "%<#pragma omp taskwait%> may only be "
37247 "used in compound statements");
37254 case PRAGMA_OMP_TASKYIELD
:
37257 case pragma_compound
:
37258 cp_parser_omp_taskyield (parser
, pragma_tok
);
37261 error_at (pragma_tok
->location
,
37262 "%<#pragma omp taskyield%> may only be "
37263 "used in compound statements");
37270 case PRAGMA_OMP_CANCEL
:
37273 case pragma_compound
:
37274 cp_parser_omp_cancel (parser
, pragma_tok
);
37277 error_at (pragma_tok
->location
,
37278 "%<#pragma omp cancel%> may only be "
37279 "used in compound statements");
37286 case PRAGMA_OMP_CANCELLATION_POINT
:
37289 case pragma_compound
:
37290 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
37293 error_at (pragma_tok
->location
,
37294 "%<#pragma omp cancellation point%> may only be "
37295 "used in compound statements");
37302 case PRAGMA_OMP_THREADPRIVATE
:
37303 cp_parser_omp_threadprivate (parser
, pragma_tok
);
37306 case PRAGMA_OMP_DECLARE_REDUCTION
:
37307 cp_parser_omp_declare (parser
, pragma_tok
, context
);
37310 case PRAGMA_OACC_DECLARE
:
37311 cp_parser_oacc_declare (parser
, pragma_tok
);
37314 case PRAGMA_OACC_ROUTINE
:
37315 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
37318 case PRAGMA_OACC_ATOMIC
:
37319 case PRAGMA_OACC_CACHE
:
37320 case PRAGMA_OACC_DATA
:
37321 case PRAGMA_OACC_ENTER_DATA
:
37322 case PRAGMA_OACC_EXIT_DATA
:
37323 case PRAGMA_OACC_HOST_DATA
:
37324 case PRAGMA_OACC_KERNELS
:
37325 case PRAGMA_OACC_PARALLEL
:
37326 case PRAGMA_OACC_LOOP
:
37327 case PRAGMA_OACC_UPDATE
:
37328 case PRAGMA_OACC_WAIT
:
37329 case PRAGMA_OMP_ATOMIC
:
37330 case PRAGMA_OMP_CRITICAL
:
37331 case PRAGMA_OMP_DISTRIBUTE
:
37332 case PRAGMA_OMP_FOR
:
37333 case PRAGMA_OMP_MASTER
:
37334 case PRAGMA_OMP_PARALLEL
:
37335 case PRAGMA_OMP_SECTIONS
:
37336 case PRAGMA_OMP_SIMD
:
37337 case PRAGMA_OMP_SINGLE
:
37338 case PRAGMA_OMP_TASK
:
37339 case PRAGMA_OMP_TASKGROUP
:
37340 case PRAGMA_OMP_TASKLOOP
:
37341 case PRAGMA_OMP_TEAMS
:
37342 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
37344 stmt
= push_omp_privatization_clauses (false);
37345 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
37346 pop_omp_privatization_clauses (stmt
);
37349 case PRAGMA_OMP_ORDERED
:
37350 stmt
= push_omp_privatization_clauses (false);
37351 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
37352 pop_omp_privatization_clauses (stmt
);
37355 case PRAGMA_OMP_TARGET
:
37356 stmt
= push_omp_privatization_clauses (false);
37357 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
37358 pop_omp_privatization_clauses (stmt
);
37361 case PRAGMA_OMP_END_DECLARE_TARGET
:
37362 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
37365 case PRAGMA_OMP_SECTION
:
37366 error_at (pragma_tok
->location
,
37367 "%<#pragma omp section%> may only be used in "
37368 "%<#pragma omp sections%> construct");
37373 if (context
== pragma_external
)
37375 error_at (pragma_tok
->location
,
37376 "%<#pragma GCC ivdep%> must be inside a function");
37379 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37381 tok
= cp_lexer_peek_token (the_parser
->lexer
);
37382 if (tok
->type
!= CPP_KEYWORD
37383 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
37384 && tok
->keyword
!= RID_DO
))
37386 cp_parser_error (parser
, "for, while or do statement expected");
37389 cp_parser_iteration_statement (parser
, if_p
, true);
37393 case PRAGMA_CILK_SIMD
:
37394 if (context
== pragma_external
)
37396 error_at (pragma_tok
->location
,
37397 "%<#pragma simd%> must be inside a function");
37400 stmt
= push_omp_privatization_clauses (false);
37401 cp_parser_cilk_simd (parser
, pragma_tok
, if_p
);
37402 pop_omp_privatization_clauses (stmt
);
37405 case PRAGMA_CILK_GRAINSIZE
:
37406 if (context
== pragma_external
)
37408 error_at (pragma_tok
->location
,
37409 "%<#pragma cilk grainsize%> must be inside a function");
37413 /* Ignore the pragma if Cilk Plus is not enabled. */
37416 cp_parser_cilk_grainsize (parser
, pragma_tok
, if_p
);
37421 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
37422 "%<#pragma cilk grainsize%>");
37427 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
37428 c_invoke_pragma_handler (id
);
37432 cp_parser_error (parser
, "expected declaration specifiers");
37436 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37440 /* The interface the pragma parsers have to the lexer. */
37443 pragma_lex (tree
*value
, location_t
*loc
)
37445 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
37446 enum cpp_ttype ret
= tok
->type
;
37448 *value
= tok
->u
.value
;
37450 *loc
= tok
->location
;
37452 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
37454 else if (ret
== CPP_STRING
)
37455 *value
= cp_parser_string_literal (the_parser
, false, false);
37458 if (ret
== CPP_KEYWORD
)
37460 cp_lexer_consume_token (the_parser
->lexer
);
37467 /* External interface. */
37469 /* Parse one entire translation unit. */
37472 c_parse_file (void)
37474 static bool already_called
= false;
37476 if (already_called
)
37477 fatal_error (input_location
,
37478 "inter-module optimizations not implemented for C++");
37479 already_called
= true;
37481 the_parser
= cp_parser_new ();
37482 push_deferring_access_checks (flag_access_control
37483 ? dk_no_deferred
: dk_no_check
);
37484 cp_parser_translation_unit (the_parser
);
37488 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37489 vectorlength clause:
37491 vectorlength ( constant-expression ) */
37494 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
37497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37499 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37500 safelen clause. Thus, vectorlength is represented as OMP 4.0
37501 safelen. For SIMD-enabled function it is represented by OMP 4.0
37504 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
37507 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
37510 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37511 return error_mark_node
;
37513 expr
= cp_parser_constant_expression (parser
);
37514 expr
= maybe_constant_value (expr
);
37516 /* If expr == error_mark_node, then don't emit any errors nor
37517 create a clause. if any of the above functions returns
37518 error mark node then they would have emitted an error message. */
37519 if (expr
== error_mark_node
)
37521 else if (!TREE_TYPE (expr
)
37522 || !TREE_CONSTANT (expr
)
37523 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
37524 error_at (loc
, "vectorlength must be an integer constant");
37525 else if (TREE_CONSTANT (expr
)
37526 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
37527 error_at (loc
, "vectorlength must be a power of 2");
37533 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
37534 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
37535 OMP_CLAUSE_CHAIN (c
) = clauses
;
37540 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
37541 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
37542 OMP_CLAUSE_CHAIN (c
) = clauses
;
37547 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
37548 return error_mark_node
;
37552 /* Handles the Cilk Plus #pragma simd linear clause.
37554 linear ( simd-linear-variable-list )
37556 simd-linear-variable-list:
37557 simd-linear-variable
37558 simd-linear-variable-list , simd-linear-variable
37560 simd-linear-variable:
37562 id-expression : simd-linear-step
37565 conditional-expression */
37568 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
37570 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37572 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37574 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
37576 cp_parser_error (parser
, "expected identifier");
37577 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
37578 return error_mark_node
;
37581 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37582 parser
->colon_corrects_to_scope_p
= false;
37585 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
37588 cp_parser_error (parser
, "expected variable-name");
37589 clauses
= error_mark_node
;
37593 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
37595 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
37597 if (decl
== error_mark_node
)
37599 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
37601 clauses
= error_mark_node
;
37605 tree e
= NULL_TREE
;
37606 tree step_size
= integer_one_node
;
37608 /* If present, parse the linear step. Otherwise, assume the default
37610 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
37612 cp_lexer_consume_token (parser
->lexer
);
37614 e
= cp_parser_assignment_expression (parser
);
37615 e
= maybe_constant_value (e
);
37617 if (e
== error_mark_node
)
37619 /* If an error has occurred, then the whole pragma is
37620 considered ill-formed. Thus, no reason to keep
37622 clauses
= error_mark_node
;
37625 else if (type_dependent_expression_p (e
)
37626 || value_dependent_expression_p (e
)
37628 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
37629 && (TREE_CONSTANT (e
)
37633 cp_parser_error (parser
,
37634 "step size must be an integer constant "
37635 "expression or an integer variable");
37638 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37639 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
37640 OMP_CLAUSE_DECL (l
) = decl
;
37641 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
37642 OMP_CLAUSE_CHAIN (l
) = clauses
;
37645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37646 cp_lexer_consume_token (parser
->lexer
);
37647 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
37651 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37652 "expected %<,%> or %<)%> after %qE", decl
);
37653 clauses
= error_mark_node
;
37657 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37658 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
37662 /* Returns the name of the next clause. If the clause is not
37663 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37664 token is not consumed. Otherwise, the appropriate enum from the
37665 pragma_simd_clause is returned and the token is consumed. */
37667 static pragma_omp_clause
37668 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
37670 pragma_omp_clause clause_type
;
37671 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37673 if (token
->keyword
== RID_PRIVATE
)
37674 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
37675 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
37676 return PRAGMA_CILK_CLAUSE_NONE
;
37677 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
37678 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
37679 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
37680 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
37681 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
37682 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
37683 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
37684 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
37685 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
37686 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
37688 return PRAGMA_CILK_CLAUSE_NONE
;
37690 cp_lexer_consume_token (parser
->lexer
);
37691 return clause_type
;
37694 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37697 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
37699 tree clauses
= NULL_TREE
;
37701 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37702 && clauses
!= error_mark_node
)
37704 pragma_omp_clause c_kind
;
37705 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
37706 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
37707 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
37708 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
37709 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
37710 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
37711 /* Use the OpenMP 4.0 equivalent function. */
37712 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
37713 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
37714 /* Use the OpenMP 4.0 equivalent function. */
37715 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37717 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
37718 /* Use the OMP 4.0 equivalent function. */
37719 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
37721 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
37722 /* Use the OMP 4.0 equivalent function. */
37723 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
37726 clauses
= error_mark_node
;
37727 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
37732 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
37734 if (clauses
== error_mark_node
)
37735 return error_mark_node
;
37737 return finish_omp_clauses (clauses
, C_ORT_CILK
);
37740 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37743 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
, bool *if_p
)
37745 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
37747 if (clauses
== error_mark_node
)
37750 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
37752 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37753 "for statement expected");
37757 tree sb
= begin_omp_structured_block ();
37758 int save
= cp_parser_begin_omp_structured_block (parser
);
37759 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
, if_p
);
37761 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
37762 cp_parser_end_omp_structured_block (parser
, save
);
37763 add_stmt (finish_omp_structured_block (sb
));
37766 /* Main entry-point for parsing Cilk Plus _Cilk_for
37767 loops. The return value is error_mark_node
37768 when errors happen and CILK_FOR tree on success. */
37771 cp_parser_cilk_for (cp_parser
*parser
, tree grain
, bool *if_p
)
37773 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
37774 gcc_unreachable ();
37776 tree sb
= begin_omp_structured_block ();
37777 int save
= cp_parser_begin_omp_structured_block (parser
);
37779 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
37780 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
37781 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
37782 clauses
= finish_omp_clauses (clauses
, C_ORT_CILK
);
37784 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
, if_p
);
37786 cpp_validate_cilk_plus_loop (ret
);
37788 ret
= error_mark_node
;
37790 cp_parser_end_omp_structured_block (parser
, save
);
37791 add_stmt (finish_omp_structured_block (sb
));
37795 /* Create an identifier for a generic parameter type (a synthesized
37796 template parameter implied by `auto' or a concept identifier). */
37798 static GTY(()) int generic_parm_count
;
37800 make_generic_type_name ()
37803 sprintf (buf
, "auto:%d", ++generic_parm_count
);
37804 return get_identifier (buf
);
37807 /* Predicate that behaves as is_auto_or_concept but matches the parent
37808 node of the generic type rather than the generic type itself. This
37809 allows for type transformation in add_implicit_template_parms. */
37812 tree_type_is_auto_or_concept (const_tree t
)
37814 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
37817 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37818 (creating a new template parameter list if necessary). Returns the newly
37819 created template type parm. */
37822 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
37824 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
37826 /* Before committing to modifying any scope, if we're in an
37827 implicit template scope, and we're trying to synthesize a
37828 constrained parameter, try to find a previous parameter with
37829 the same name. This is the same-type rule for abbreviated
37830 function templates. */
37831 if (parser
->implicit_template_scope
&& constr
)
37833 tree t
= parser
->implicit_template_parms
;
37836 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
37838 tree d
= TREE_VALUE (t
);
37839 if (TREE_CODE (d
) == PARM_DECL
)
37840 /* Return the TEMPLATE_PARM_INDEX. */
37841 d
= DECL_INITIAL (d
);
37844 t
= TREE_CHAIN (t
);
37848 /* We are either continuing a function template that already contains implicit
37849 template parameters, creating a new fully-implicit function template, or
37850 extending an existing explicit function template with implicit template
37853 cp_binding_level
*const entry_scope
= current_binding_level
;
37855 bool become_template
= false;
37856 cp_binding_level
*parent_scope
= 0;
37858 if (parser
->implicit_template_scope
)
37860 gcc_assert (parser
->implicit_template_parms
);
37862 current_binding_level
= parser
->implicit_template_scope
;
37866 /* Roll back to the existing template parameter scope (in the case of
37867 extending an explicit function template) or introduce a new template
37868 parameter scope ahead of the function parameter scope (or class scope
37869 in the case of out-of-line member definitions). The function scope is
37870 added back after template parameter synthesis below. */
37872 cp_binding_level
*scope
= entry_scope
;
37874 while (scope
->kind
== sk_function_parms
)
37876 parent_scope
= scope
;
37877 scope
= scope
->level_chain
;
37879 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
37881 /* If not defining a class, then any class scope is a scope level in
37882 an out-of-line member definition. In this case simply wind back
37883 beyond the first such scope to inject the template parameter list.
37884 Otherwise wind back to the class being defined. The latter can
37885 occur in class member friend declarations such as:
37891 friend void A::foo (auto);
37894 The template parameter list synthesized for the friend declaration
37895 must be injected in the scope of 'B'. This can also occur in
37896 erroneous cases such as:
37902 void B::foo (auto) {}
37905 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37906 but, nevertheless, the template parameter list synthesized for the
37907 declarator should be injected into the scope of 'A' as if the
37908 ill-formed template was specified explicitly. */
37910 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
37912 parent_scope
= scope
;
37913 scope
= scope
->level_chain
;
37917 current_binding_level
= scope
;
37919 if (scope
->kind
!= sk_template_parms
37920 || !function_being_declared_is_template_p (parser
))
37922 /* Introduce a new template parameter list for implicit template
37925 become_template
= true;
37927 parser
->implicit_template_scope
37928 = begin_scope (sk_template_parms
, NULL
);
37930 ++processing_template_decl
;
37932 parser
->fully_implicit_function_template_p
= true;
37933 ++parser
->num_template_parameter_lists
;
37937 /* Synthesize implicit template parameters at the end of the explicit
37938 template parameter list. */
37940 gcc_assert (current_template_parms
);
37942 parser
->implicit_template_scope
= scope
;
37944 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
37945 parser
->implicit_template_parms
37946 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
37950 /* Synthesize a new template parameter and track the current template
37951 parameter chain with implicit_template_parms. */
37953 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
37954 tree synth_id
= make_generic_type_name ();
37955 tree synth_tmpl_parm
;
37956 bool non_type
= false;
37958 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
37960 = finish_template_type_parm (class_type_node
, synth_id
);
37961 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
37963 = finish_constrained_template_template_parm (proto
, synth_id
);
37966 synth_tmpl_parm
= copy_decl (proto
);
37967 DECL_NAME (synth_tmpl_parm
) = synth_id
;
37971 // Attach the constraint to the parm before processing.
37972 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
37973 TREE_TYPE (node
) = constr
;
37975 = process_template_parm (parser
->implicit_template_parms
,
37978 /*non_type=*/non_type
,
37979 /*param_pack=*/false);
37981 // Chain the new parameter to the list of implicit parameters.
37982 if (parser
->implicit_template_parms
)
37983 parser
->implicit_template_parms
37984 = TREE_CHAIN (parser
->implicit_template_parms
);
37986 parser
->implicit_template_parms
= new_parm
;
37988 tree new_decl
= getdecls ();
37990 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37991 new_decl
= DECL_INITIAL (new_decl
);
37993 /* If creating a fully implicit function template, start the new implicit
37994 template parameter list with this synthesized type, otherwise grow the
37995 current template parameter list. */
37997 if (become_template
)
37999 parent_scope
->level_chain
= current_binding_level
;
38001 tree new_parms
= make_tree_vec (1);
38002 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
38003 current_template_parms
= tree_cons (size_int (processing_template_decl
),
38004 new_parms
, current_template_parms
);
38008 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
38009 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
38010 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
38011 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
38014 // If the new parameter was constrained, we need to add that to the
38015 // constraints in the template parameter list.
38016 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
38018 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
38019 reqs
= conjoin_constraints (reqs
, req
);
38020 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
38023 current_binding_level
= entry_scope
;
38028 /* Finish the declaration of a fully implicit function template. Such a
38029 template has no explicit template parameter list so has not been through the
38030 normal template head and tail processing. synthesize_implicit_template_parm
38031 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38032 provided if the declaration is a class member such that its template
38033 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38034 form is returned. Otherwise NULL_TREE is returned. */
38037 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
38039 gcc_assert (parser
->fully_implicit_function_template_p
);
38041 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
38042 && DECL_VIRTUAL_P (member_decl_opt
))
38044 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
38045 "implicit templates may not be %<virtual%>");
38046 DECL_VIRTUAL_P (member_decl_opt
) = false;
38049 if (member_decl_opt
)
38050 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
38051 end_template_decl ();
38053 parser
->fully_implicit_function_template_p
= false;
38054 --parser
->num_template_parameter_lists
;
38056 return member_decl_opt
;
38059 #include "gt-cp-parser.h"