2 Copyright (C) 2000-2015 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"
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token
=
51 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant
{
57 /* floating-point literal */
61 /* %<__FUNCTION__%> */
63 /* %<__PRETTY_FUNCTION__%> */
71 /* %<typeid%> operator */
73 /* non-constant compound literals */
81 /* an array reference */
87 /* the address of a label */
101 /* calls to overloaded operators */
105 /* a comma operator */
107 /* a call to a constructor */
109 /* a transaction expression */
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error
{
119 /* is not a class or namespace */
121 /* is not a class, namespace, or enumeration */
125 /* The various kinds of required token */
126 enum required_token
{
128 RT_SEMICOLON
, /* ';' */
129 RT_OPEN_PAREN
, /* '(' */
130 RT_CLOSE_BRACE
, /* '}' */
131 RT_OPEN_BRACE
, /* '{' */
132 RT_CLOSE_SQUARE
, /* ']' */
133 RT_OPEN_SQUARE
, /* '[' */
137 RT_GREATER
, /* '>' */
139 RT_ELLIPSIS
, /* '...' */
143 RT_COLON_SCOPE
, /* ':' or '::' */
144 RT_CLOSE_PAREN
, /* ')' */
145 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
146 RT_PRAGMA_EOL
, /* end of line */
147 RT_NAME
, /* identifier */
149 /* The type is CPP_KEYWORD */
151 RT_DELETE
, /* delete */
152 RT_RETURN
, /* return */
153 RT_WHILE
, /* while */
154 RT_EXTERN
, /* extern */
155 RT_STATIC_ASSERT
, /* static_assert */
156 RT_DECLTYPE
, /* decltype */
157 RT_OPERATOR
, /* operator */
158 RT_CLASS
, /* class */
159 RT_TEMPLATE
, /* template */
160 RT_NAMESPACE
, /* namespace */
161 RT_USING
, /* using */
164 RT_CATCH
, /* catch */
165 RT_THROW
, /* throw */
166 RT_LABEL
, /* __label__ */
167 RT_AT_TRY
, /* @try */
168 RT_AT_SYNCHRONIZED
, /* @synchronized */
169 RT_AT_THROW
, /* @throw */
171 RT_SELECT
, /* selection-statement */
172 RT_INTERATION
, /* iteration-statement */
173 RT_JUMP
, /* jump-statement */
174 RT_CLASS_KEY
, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
183 static cp_lexer
*cp_lexer_new_main
185 static cp_lexer
*cp_lexer_new_from_tokens
186 (cp_token_cache
*tokens
);
187 static void cp_lexer_destroy
189 static int cp_lexer_saving_tokens
191 static cp_token
*cp_lexer_token_at
192 (cp_lexer
*, cp_token_position
);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer
*, cp_token
*);
195 static inline cp_token
*cp_lexer_peek_token
197 static cp_token
*cp_lexer_peek_nth_token
198 (cp_lexer
*, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer
*, enum cpp_ttype
);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer
*, enum cpp_ttype
);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer
*, enum rid
);
205 static cp_token
*cp_lexer_consume_token
207 static void cp_lexer_purge_token
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer
*, cp_token_position
);
211 static void cp_lexer_save_tokens
213 static void cp_lexer_commit_tokens
215 static void cp_lexer_rollback_tokens
217 static void cp_lexer_print_token
218 (FILE *, cp_token
*);
219 static inline bool cp_lexer_debugging_p
221 static void cp_lexer_start_debugging
222 (cp_lexer
*) ATTRIBUTE_UNUSED
;
223 static void cp_lexer_stop_debugging
224 (cp_lexer
*) ATTRIBUTE_UNUSED
;
226 static cp_token_cache
*cp_token_cache_new
227 (cp_token
*, cp_token
*);
229 static void cp_parser_initial_pragma
232 static tree cp_literal_operator_id
235 static void cp_parser_cilk_simd
236 (cp_parser
*, cp_token
*);
237 static tree cp_parser_cilk_for
239 static bool cp_parser_omp_declare_reduction_exprs
241 static tree cp_parser_cilk_simd_vectorlength
242 (cp_parser
*, tree
, bool);
243 static void cp_finalize_oacc_routine
244 (cp_parser
*, tree
, bool);
246 /* Manifest constants. */
247 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
248 #define CP_SAVED_TOKEN_STACK 5
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream
;
255 /* Nonzero if we are parsing an unevaluated operand: an operand to
256 sizeof, typeof, or alignof. */
257 int cp_unevaluated_operand
;
259 /* Dump up to NUM tokens in BUFFER to FILE starting with token
260 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
261 first token in BUFFER. If NUM is 0, dump all the tokens. If
262 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
263 highlighted by surrounding it in [[ ]]. */
266 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
267 cp_token
*start_token
, unsigned num
,
268 cp_token
*curr_token
)
270 unsigned i
, nprinted
;
274 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
280 num
= buffer
->length ();
282 if (start_token
== NULL
)
283 start_token
= buffer
->address ();
285 if (start_token
> buffer
->address ())
287 cp_lexer_print_token (file
, &(*buffer
)[0]);
288 fprintf (file
, " ... ");
293 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
295 if (token
== start_token
)
302 if (token
== curr_token
)
303 fprintf (file
, "[[");
305 cp_lexer_print_token (file
, token
);
307 if (token
== curr_token
)
308 fprintf (file
, "]]");
314 case CPP_CLOSE_BRACE
:
324 if (i
== num
&& i
< buffer
->length ())
326 fprintf (file
, " ... ");
327 cp_lexer_print_token (file
, &buffer
->last ());
330 fprintf (file
, "\n");
334 /* Dump all tokens in BUFFER to stderr. */
337 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
339 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
343 debug (vec
<cp_token
, va_gc
> &ref
)
345 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
349 debug (vec
<cp_token
, va_gc
> *ptr
)
354 fprintf (stderr
, "<nil>\n");
358 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
359 description for T. */
362 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
366 fprintf (file
, "%s: ", desc
);
367 print_node_brief (file
, "", t
, 0);
372 /* Dump parser context C to FILE. */
375 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
377 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
378 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
379 print_node_brief (file
, "", c
->object_type
, 0);
380 fprintf (file
, "}\n");
384 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
390 cp_parser_context
*c
;
392 fprintf (file
, "Parsing context stack:\n");
393 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
395 fprintf (file
, "\t#%u: ", i
);
396 cp_debug_print_context (file
, c
);
401 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
407 fprintf (file
, "%s: true\n", desc
);
411 /* Print an unparsed function entry UF to FILE. */
414 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
417 cp_default_arg_entry
*default_arg_fn
;
420 fprintf (file
, "\tFunctions with default args:\n");
422 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
425 fprintf (file
, "\t\tClass type: ");
426 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
427 fprintf (file
, "\t\tDeclaration: ");
428 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
429 fprintf (file
, "\n");
432 fprintf (file
, "\n\tFunctions with definitions that require "
433 "post-processing\n\t\t");
434 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
436 print_node_brief (file
, "", fn
, 0);
439 fprintf (file
, "\n");
441 fprintf (file
, "\n\tNon-static data members with initializers that require "
442 "post-processing\n\t\t");
443 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
445 print_node_brief (file
, "", fn
, 0);
448 fprintf (file
, "\n");
452 /* Print the stack of unparsed member functions S to FILE. */
455 cp_debug_print_unparsed_queues (FILE *file
,
456 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
459 cp_unparsed_functions_entry
*uf
;
461 fprintf (file
, "Unparsed functions\n");
462 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
464 fprintf (file
, "#%u:\n", i
);
465 cp_debug_print_unparsed_function (file
, uf
);
470 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
471 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
476 cp_token
*next_token
, *first_token
, *start_token
;
481 next_token
= parser
->lexer
->next_token
;
482 first_token
= parser
->lexer
->buffer
->address ();
483 start_token
= (next_token
> first_token
+ window_size
/ 2)
484 ? next_token
- window_size
/ 2
486 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
491 /* Dump debugging information for the given PARSER. If FILE is NULL,
492 the output is printed on stderr. */
495 cp_debug_parser (FILE *file
, cp_parser
*parser
)
497 const size_t window_size
= 20;
499 expanded_location eloc
;
504 fprintf (file
, "Parser state\n\n");
505 fprintf (file
, "Number of tokens: %u\n",
506 vec_safe_length (parser
->lexer
->buffer
));
507 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
508 cp_debug_print_tree_if_set (file
, "Object scope",
509 parser
->object_scope
);
510 cp_debug_print_tree_if_set (file
, "Qualifying scope",
511 parser
->qualifying_scope
);
512 cp_debug_print_context_stack (file
, parser
->context
);
513 cp_debug_print_flag (file
, "Allow GNU extensions",
514 parser
->allow_gnu_extensions_p
);
515 cp_debug_print_flag (file
, "'>' token is greater-than",
516 parser
->greater_than_is_operator_p
);
517 cp_debug_print_flag (file
, "Default args allowed in current "
518 "parameter list", parser
->default_arg_ok_p
);
519 cp_debug_print_flag (file
, "Parsing integral constant-expression",
520 parser
->integral_constant_expression_p
);
521 cp_debug_print_flag (file
, "Allow non-constant expression in current "
522 "constant-expression",
523 parser
->allow_non_integral_constant_expression_p
);
524 cp_debug_print_flag (file
, "Seen non-constant expression",
525 parser
->non_integral_constant_expression_p
);
526 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
528 parser
->local_variables_forbidden_p
);
529 cp_debug_print_flag (file
, "In unbraced linkage specification",
530 parser
->in_unbraced_linkage_specification_p
);
531 cp_debug_print_flag (file
, "Parsing a declarator",
532 parser
->in_declarator_p
);
533 cp_debug_print_flag (file
, "In template argument list",
534 parser
->in_template_argument_list_p
);
535 cp_debug_print_flag (file
, "Parsing an iteration statement",
536 parser
->in_statement
& IN_ITERATION_STMT
);
537 cp_debug_print_flag (file
, "Parsing a switch statement",
538 parser
->in_statement
& IN_SWITCH_STMT
);
539 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
540 parser
->in_statement
& IN_OMP_BLOCK
);
541 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
542 parser
->in_statement
& IN_CILK_SIMD_FOR
);
543 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
544 parser
->in_statement
& IN_OMP_FOR
);
545 cp_debug_print_flag (file
, "Parsing an if statement",
546 parser
->in_statement
& IN_IF_STMT
);
547 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
548 "context", parser
->in_type_id_in_expr_p
);
549 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
550 parser
->implicit_extern_c
);
551 cp_debug_print_flag (file
, "String expressions should be translated "
552 "to execution character set",
553 parser
->translate_strings_p
);
554 cp_debug_print_flag (file
, "Parsing function body outside of a "
555 "local class", parser
->in_function_body
);
556 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
557 parser
->colon_corrects_to_scope_p
);
558 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
559 parser
->colon_doesnt_start_class_def_p
);
560 if (parser
->type_definition_forbidden_message
)
561 fprintf (file
, "Error message for forbidden type definitions: %s\n",
562 parser
->type_definition_forbidden_message
);
563 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
564 fprintf (file
, "Number of class definitions in progress: %u\n",
565 parser
->num_classes_being_defined
);
566 fprintf (file
, "Number of template parameter lists for the current "
567 "declaration: %u\n", parser
->num_template_parameter_lists
);
568 cp_debug_parser_tokens (file
, parser
, window_size
);
569 token
= parser
->lexer
->next_token
;
570 fprintf (file
, "Next token to parse:\n");
571 fprintf (file
, "\tToken: ");
572 cp_lexer_print_token (file
, token
);
573 eloc
= expand_location (token
->location
);
574 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
575 fprintf (file
, "\tLine: %d\n", eloc
.line
);
576 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
580 debug (cp_parser
&ref
)
582 cp_debug_parser (stderr
, &ref
);
586 debug (cp_parser
*ptr
)
591 fprintf (stderr
, "<nil>\n");
594 /* Allocate memory for a new lexer object and return it. */
597 cp_lexer_alloc (void)
601 c_common_no_more_pch ();
603 /* Allocate the memory. */
604 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
606 /* Initially we are not debugging. */
607 lexer
->debugging_p
= false;
609 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
611 /* Create the buffer. */
612 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
618 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 cp_lexer_new_main (void)
627 /* It's possible that parsing the first pragma will load a PCH file,
628 which is a GC collection point. So we have to do that before
629 allocating any memory. */
630 cp_parser_initial_pragma (&token
);
632 lexer
= cp_lexer_alloc ();
634 /* Put the first token in the buffer. */
635 lexer
->buffer
->quick_push (token
);
637 /* Get the remaining tokens from the preprocessor. */
638 while (token
.type
!= CPP_EOF
)
640 cp_lexer_get_preprocessor_token (lexer
, &token
);
641 vec_safe_push (lexer
->buffer
, token
);
644 lexer
->last_token
= lexer
->buffer
->address ()
645 + lexer
->buffer
->length ()
647 lexer
->next_token
= lexer
->buffer
->length ()
648 ? lexer
->buffer
->address ()
651 /* Subsequent preprocessor diagnostics should use compiler
652 diagnostic functions to get the compiler source location. */
655 gcc_assert (!lexer
->next_token
->purged_p
);
659 /* Create a new lexer whose token stream is primed with the tokens in
660 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
665 cp_token
*first
= cache
->first
;
666 cp_token
*last
= cache
->last
;
667 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
669 /* We do not own the buffer. */
670 lexer
->buffer
= NULL
;
671 lexer
->next_token
= first
== last
? &eof_token
: first
;
672 lexer
->last_token
= last
;
674 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
676 /* Initially we are not debugging. */
677 lexer
->debugging_p
= false;
679 gcc_assert (!lexer
->next_token
->purged_p
);
683 /* Frees all resources associated with LEXER. */
686 cp_lexer_destroy (cp_lexer
*lexer
)
688 vec_free (lexer
->buffer
);
689 lexer
->saved_tokens
.release ();
693 /* Returns nonzero if debugging information should be output. */
696 cp_lexer_debugging_p (cp_lexer
*lexer
)
698 return lexer
->debugging_p
;
702 static inline cp_token_position
703 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
705 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
707 return lexer
->next_token
- previous_p
;
710 static inline cp_token
*
711 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
717 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
719 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
722 static inline cp_token_position
723 cp_lexer_previous_token_position (cp_lexer
*lexer
)
725 if (lexer
->next_token
== &eof_token
)
726 return lexer
->last_token
- 1;
728 return cp_lexer_token_position (lexer
, true);
731 static inline cp_token
*
732 cp_lexer_previous_token (cp_lexer
*lexer
)
734 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
736 return cp_lexer_token_at (lexer
, tp
);
739 /* nonzero if we are presently saving tokens. */
742 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
744 return lexer
->saved_tokens
.length () != 0;
747 /* Store the next token from the preprocessor in *TOKEN. Return true
748 if we reach EOF. If LEXER is NULL, assume we are handling an
749 initial #pragma pch_preprocess, and thus want the lexer to return
750 processed strings. */
753 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
755 static int is_extern_c
= 0;
757 /* Get a new token from the preprocessor. */
759 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
760 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
761 token
->keyword
= RID_MAX
;
762 token
->pragma_kind
= PRAGMA_NONE
;
763 token
->purged_p
= false;
764 token
->error_reported
= false;
766 /* On some systems, some header files are surrounded by an
767 implicit extern "C" block. Set a flag in the token if it
768 comes from such a header. */
769 is_extern_c
+= pending_lang_change
;
770 pending_lang_change
= 0;
771 token
->implicit_extern_c
= is_extern_c
> 0;
773 /* Check to see if this token is a keyword. */
774 if (token
->type
== CPP_NAME
)
776 if (C_IS_RESERVED_WORD (token
->u
.value
))
778 /* Mark this token as a keyword. */
779 token
->type
= CPP_KEYWORD
;
780 /* Record which keyword. */
781 token
->keyword
= C_RID_CODE (token
->u
.value
);
785 if (warn_cxx11_compat
786 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
787 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
789 /* Warn about the C++0x keyword (but still treat it as
791 warning (OPT_Wc__11_compat
,
792 "identifier %qE is a keyword in C++11",
795 /* Clear out the C_RID_CODE so we don't warn about this
796 particular identifier-turned-keyword again. */
797 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
800 token
->keyword
= RID_MAX
;
803 else if (token
->type
== CPP_AT_NAME
)
805 /* This only happens in Objective-C++; it must be a keyword. */
806 token
->type
= CPP_KEYWORD
;
807 switch (C_RID_CODE (token
->u
.value
))
809 /* Replace 'class' with '@class', 'private' with '@private',
810 etc. This prevents confusion with the C++ keyword
811 'class', and makes the tokens consistent with other
812 Objective-C 'AT' keywords. For example '@class' is
813 reported as RID_AT_CLASS which is consistent with
814 '@synchronized', which is reported as
817 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
818 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
819 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
820 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
821 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
822 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
823 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
824 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
825 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
828 else if (token
->type
== CPP_PRAGMA
)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token
->pragma_kind
= ((enum pragma_kind
)
832 TREE_INT_CST_LOW (token
->u
.value
));
833 token
->u
.value
= NULL_TREE
;
837 /* Update the globals input_location and the input file stack from TOKEN. */
839 cp_lexer_set_source_position_from_token (cp_token
*token
)
841 if (token
->type
!= CPP_EOF
)
843 input_location
= token
->location
;
847 /* Update the globals input_location and the input file stack from LEXER. */
849 cp_lexer_set_source_position (cp_lexer
*lexer
)
851 cp_token
*token
= cp_lexer_peek_token (lexer
);
852 cp_lexer_set_source_position_from_token (token
);
855 /* Return a pointer to the next token in the token stream, but do not
858 static inline cp_token
*
859 cp_lexer_peek_token (cp_lexer
*lexer
)
861 if (cp_lexer_debugging_p (lexer
))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
864 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
865 putc ('\n', cp_lexer_debug_stream
);
867 return lexer
->next_token
;
870 /* Return true if the next token has the indicated TYPE. */
873 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
875 return cp_lexer_peek_token (lexer
)->type
== type
;
878 /* Return true if the next token does not have the indicated TYPE. */
881 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
883 return !cp_lexer_next_token_is (lexer
, type
);
886 /* Return true if the next token is the indicated KEYWORD. */
889 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
891 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
895 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
897 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
901 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
903 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
906 /* Return true if the next token is not the indicated KEYWORD. */
909 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
911 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
914 /* Return true if the next token is a keyword for a decl-specifier. */
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
921 token
= cp_lexer_peek_token (lexer
);
922 switch (token
->keyword
)
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
927 /* Storage classes. */
933 /* Elaborated type specifiers. */
939 /* Simple type specifiers. */
953 /* GNU extensions. */
956 /* C++0x extensions. */
958 case RID_UNDERLYING_TYPE
:
962 if (token
->keyword
>= RID_FIRST_INT_N
963 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
964 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
970 /* Returns TRUE iff the token T begins a decltype type. */
973 token_is_decltype (cp_token
*t
)
975 return (t
->keyword
== RID_DECLTYPE
976 || t
->type
== CPP_DECLTYPE
);
979 /* Returns TRUE iff the next token begins a decltype type. */
982 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
984 cp_token
*t
= cp_lexer_peek_token (lexer
);
985 return token_is_decltype (t
);
988 /* Return a pointer to the Nth token in the token stream. If N is 1,
989 then this is precisely equivalent to cp_lexer_peek_token (except
990 that it is not inline). One would like to disallow that case, but
991 there is one case (cp_parser_nth_token_starts_template_id) where
992 the caller passes a variable for N and it might be 1. */
995 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
999 /* N is 1-based, not zero-based. */
1002 if (cp_lexer_debugging_p (lexer
))
1003 fprintf (cp_lexer_debug_stream
,
1004 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1007 token
= lexer
->next_token
;
1008 gcc_assert (!n
|| token
!= &eof_token
);
1012 if (token
== lexer
->last_token
)
1018 if (!token
->purged_p
)
1022 if (cp_lexer_debugging_p (lexer
))
1024 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1025 putc ('\n', cp_lexer_debug_stream
);
1031 /* Return the next token, and advance the lexer's next_token pointer
1032 to point to the next non-purged token. */
1035 cp_lexer_consume_token (cp_lexer
* lexer
)
1037 cp_token
*token
= lexer
->next_token
;
1039 gcc_assert (token
!= &eof_token
);
1040 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1044 lexer
->next_token
++;
1045 if (lexer
->next_token
== lexer
->last_token
)
1047 lexer
->next_token
= &eof_token
;
1052 while (lexer
->next_token
->purged_p
);
1054 cp_lexer_set_source_position_from_token (token
);
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer
))
1059 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1060 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1061 putc ('\n', cp_lexer_debug_stream
);
1067 /* Permanently remove the next token from the token stream, and
1068 advance the next_token pointer to refer to the next non-purged
1072 cp_lexer_purge_token (cp_lexer
*lexer
)
1074 cp_token
*tok
= lexer
->next_token
;
1076 gcc_assert (tok
!= &eof_token
);
1077 tok
->purged_p
= true;
1078 tok
->location
= UNKNOWN_LOCATION
;
1079 tok
->u
.value
= NULL_TREE
;
1080 tok
->keyword
= RID_MAX
;
1085 if (tok
== lexer
->last_token
)
1091 while (tok
->purged_p
);
1092 lexer
->next_token
= tok
;
1095 /* Permanently remove all tokens after TOK, up to, but not
1096 including, the token that will be returned next by
1097 cp_lexer_peek_token. */
1100 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1102 cp_token
*peek
= lexer
->next_token
;
1104 if (peek
== &eof_token
)
1105 peek
= lexer
->last_token
;
1107 gcc_assert (tok
< peek
);
1109 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1111 tok
->purged_p
= true;
1112 tok
->location
= UNKNOWN_LOCATION
;
1113 tok
->u
.value
= NULL_TREE
;
1114 tok
->keyword
= RID_MAX
;
1118 /* Begin saving tokens. All tokens consumed after this point will be
1122 cp_lexer_save_tokens (cp_lexer
* lexer
)
1124 /* Provide debugging output. */
1125 if (cp_lexer_debugging_p (lexer
))
1126 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1128 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1131 /* Commit to the portion of the token stream most recently saved. */
1134 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1136 /* Provide debugging output. */
1137 if (cp_lexer_debugging_p (lexer
))
1138 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1140 lexer
->saved_tokens
.pop ();
1143 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1144 to the token stream. Stop saving tokens. */
1147 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer
))
1151 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1153 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1156 /* RAII wrapper around the above functions, with sanity checking. Creating
1157 a variable saves tokens, which are committed when the variable is
1158 destroyed unless they are explicitly rolled back by calling the rollback
1161 struct saved_token_sentinel
1166 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1168 len
= lexer
->saved_tokens
.length ();
1169 cp_lexer_save_tokens (lexer
);
1173 cp_lexer_rollback_tokens (lexer
);
1176 ~saved_token_sentinel()
1179 cp_lexer_commit_tokens (lexer
);
1180 gcc_assert (lexer
->saved_tokens
.length () == len
);
1184 /* Print a representation of the TOKEN on the STREAM. */
1187 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1189 /* We don't use cpp_type2name here because the parser defines
1190 a few tokens of its own. */
1191 static const char *const token_names
[] = {
1192 /* cpplib-defined token types */
1193 #define OP(e, s) #e,
1194 #define TK(e, s) #e,
1198 /* C++ parser token types - see "Manifest constants", above. */
1201 "NESTED_NAME_SPECIFIER",
1204 /* For some tokens, print the associated data. */
1205 switch (token
->type
)
1208 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1209 For example, `struct' is mapped to an INTEGER_CST. */
1210 if (!identifier_p (token
->u
.value
))
1212 /* else fall through */
1214 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1221 case CPP_UTF8STRING
:
1222 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1226 print_generic_expr (stream
, token
->u
.value
, 0);
1230 /* If we have a name for the token, print it out. Otherwise, we
1231 simply give the numeric code. */
1232 if (token
->type
< ARRAY_SIZE(token_names
))
1233 fputs (token_names
[token
->type
], stream
);
1235 fprintf (stream
, "[%d]", token
->type
);
1241 debug (cp_token
&ref
)
1243 cp_lexer_print_token (stderr
, &ref
);
1244 fprintf (stderr
, "\n");
1248 debug (cp_token
*ptr
)
1253 fprintf (stderr
, "<nil>\n");
1257 /* Start emitting debugging information. */
1260 cp_lexer_start_debugging (cp_lexer
* lexer
)
1262 lexer
->debugging_p
= true;
1263 cp_lexer_debug_stream
= stderr
;
1266 /* Stop emitting debugging information. */
1269 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1271 lexer
->debugging_p
= false;
1272 cp_lexer_debug_stream
= NULL
;
1275 /* Create a new cp_token_cache, representing a range of tokens. */
1277 static cp_token_cache
*
1278 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1280 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1281 cache
->first
= first
;
1286 /* Diagnose if #pragma omp declare simd isn't followed immediately
1287 by function declaration or definition. */
1290 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1292 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1294 error ("%<#pragma omp declare simd%> not immediately followed by "
1295 "function declaration or definition");
1296 parser
->omp_declare_simd
= NULL
;
1300 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1301 and put that into "omp declare simd" attribute. */
1304 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1306 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1308 if (fndecl
== error_mark_node
)
1310 parser
->omp_declare_simd
= NULL
;
1313 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1315 cp_ensure_no_omp_declare_simd (parser
);
1321 /* Diagnose if #pragma acc routine isn't followed immediately by function
1322 declaration or definition. */
1325 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1327 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1329 tree clauses
= parser
->oacc_routine
->clauses
;
1330 location_t loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses
));
1332 error_at (loc
, "%<#pragma oacc routine%> not followed by function "
1333 "declaration or definition");
1334 parser
->oacc_routine
= NULL
;
1338 /* Decl-specifiers. */
1340 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1343 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1345 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1350 /* Nothing other than the parser should be creating declarators;
1351 declarators are a semi-syntactic representation of C++ entities.
1352 Other parts of the front end that need to create entities (like
1353 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1355 static cp_declarator
*make_call_declarator
1356 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1357 static cp_declarator
*make_array_declarator
1358 (cp_declarator
*, tree
);
1359 static cp_declarator
*make_pointer_declarator
1360 (cp_cv_quals
, cp_declarator
*, tree
);
1361 static cp_declarator
*make_reference_declarator
1362 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1363 static cp_declarator
*make_ptrmem_declarator
1364 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1366 /* An erroneous declarator. */
1367 static cp_declarator
*cp_error_declarator
;
1369 /* The obstack on which declarators and related data structures are
1371 static struct obstack declarator_obstack
;
1373 /* Alloc BYTES from the declarator memory pool. */
1375 static inline void *
1376 alloc_declarator (size_t bytes
)
1378 return obstack_alloc (&declarator_obstack
, bytes
);
1381 /* Allocate a declarator of the indicated KIND. Clear fields that are
1382 common to all declarators. */
1384 static cp_declarator
*
1385 make_declarator (cp_declarator_kind kind
)
1387 cp_declarator
*declarator
;
1389 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1390 declarator
->kind
= kind
;
1391 declarator
->attributes
= NULL_TREE
;
1392 declarator
->std_attributes
= NULL_TREE
;
1393 declarator
->declarator
= NULL
;
1394 declarator
->parameter_pack_p
= false;
1395 declarator
->id_loc
= UNKNOWN_LOCATION
;
1400 /* Make a declarator for a generalized identifier. If
1401 QUALIFYING_SCOPE is non-NULL, the identifier is
1402 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1403 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1406 static cp_declarator
*
1407 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1408 special_function_kind sfk
)
1410 cp_declarator
*declarator
;
1412 /* It is valid to write:
1414 class C { void f(); };
1418 The standard is not clear about whether `typedef const C D' is
1419 legal; as of 2002-09-15 the committee is considering that
1420 question. EDG 3.0 allows that syntax. Therefore, we do as
1422 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1423 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1425 gcc_assert (identifier_p (unqualified_name
)
1426 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1427 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1429 declarator
= make_declarator (cdk_id
);
1430 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1431 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1432 declarator
->u
.id
.sfk
= sfk
;
1437 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1438 of modifiers such as const or volatile to apply to the pointer
1439 type, represented as identifiers. ATTRIBUTES represent the attributes that
1440 appertain to the pointer or reference. */
1443 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1446 cp_declarator
*declarator
;
1448 declarator
= make_declarator (cdk_pointer
);
1449 declarator
->declarator
= target
;
1450 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1451 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1454 declarator
->id_loc
= target
->id_loc
;
1455 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1456 target
->parameter_pack_p
= false;
1459 declarator
->parameter_pack_p
= false;
1461 declarator
->std_attributes
= attributes
;
1466 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1467 represent the attributes that appertain to the pointer or
1471 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1472 bool rvalue_ref
, tree attributes
)
1474 cp_declarator
*declarator
;
1476 declarator
= make_declarator (cdk_reference
);
1477 declarator
->declarator
= target
;
1478 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1479 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1482 declarator
->id_loc
= target
->id_loc
;
1483 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1484 target
->parameter_pack_p
= false;
1487 declarator
->parameter_pack_p
= false;
1489 declarator
->std_attributes
= attributes
;
1494 /* Like make_pointer_declarator -- but for a pointer to a non-static
1495 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1499 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1500 cp_declarator
*pointee
,
1503 cp_declarator
*declarator
;
1505 declarator
= make_declarator (cdk_ptrmem
);
1506 declarator
->declarator
= pointee
;
1507 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1508 declarator
->u
.pointer
.class_type
= class_type
;
1512 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1513 pointee
->parameter_pack_p
= false;
1516 declarator
->parameter_pack_p
= false;
1518 declarator
->std_attributes
= attributes
;
1523 /* Make a declarator for the function given by TARGET, with the
1524 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1525 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1526 indicates what exceptions can be thrown. */
1529 make_call_declarator (cp_declarator
*target
,
1531 cp_cv_quals cv_qualifiers
,
1532 cp_virt_specifiers virt_specifiers
,
1533 cp_ref_qualifier ref_qualifier
,
1535 tree exception_specification
,
1536 tree late_return_type
,
1537 tree requires_clause
)
1539 cp_declarator
*declarator
;
1541 declarator
= make_declarator (cdk_function
);
1542 declarator
->declarator
= target
;
1543 declarator
->u
.function
.parameters
= parms
;
1544 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1545 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1546 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1547 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1548 declarator
->u
.function
.exception_specification
= exception_specification
;
1549 declarator
->u
.function
.late_return_type
= late_return_type
;
1550 declarator
->u
.function
.requires_clause
= requires_clause
;
1553 declarator
->id_loc
= target
->id_loc
;
1554 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1555 target
->parameter_pack_p
= false;
1558 declarator
->parameter_pack_p
= false;
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564 defined by ELEMENT. */
1567 make_array_declarator (cp_declarator
*element
, tree bounds
)
1569 cp_declarator
*declarator
;
1571 declarator
= make_declarator (cdk_array
);
1572 declarator
->declarator
= element
;
1573 declarator
->u
.array
.bounds
= bounds
;
1576 declarator
->id_loc
= element
->id_loc
;
1577 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1578 element
->parameter_pack_p
= false;
1581 declarator
->parameter_pack_p
= false;
1586 /* Determine whether the declarator we've seen so far can be a
1587 parameter pack, when followed by an ellipsis. */
1589 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1591 if (declarator
&& declarator
->parameter_pack_p
)
1592 /* We already saw an ellipsis. */
1595 /* Search for a declarator name, or any other declarator that goes
1596 after the point where the ellipsis could appear in a parameter
1597 pack. If we find any of these, then this declarator can not be
1598 made into a parameter pack. */
1600 while (declarator
&& !found
)
1602 switch ((int)declarator
->kind
)
1613 declarator
= declarator
->declarator
;
1621 cp_parameter_declarator
*no_parameters
;
1623 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1624 DECLARATOR and DEFAULT_ARGUMENT. */
1626 cp_parameter_declarator
*
1627 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1628 cp_declarator
*declarator
,
1629 tree default_argument
,
1630 bool template_parameter_pack_p
= false)
1632 cp_parameter_declarator
*parameter
;
1634 parameter
= ((cp_parameter_declarator
*)
1635 alloc_declarator (sizeof (cp_parameter_declarator
)));
1636 parameter
->next
= NULL
;
1637 if (decl_specifiers
)
1638 parameter
->decl_specifiers
= *decl_specifiers
;
1640 clear_decl_specs (¶meter
->decl_specifiers
);
1641 parameter
->declarator
= declarator
;
1642 parameter
->default_argument
= default_argument
;
1643 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1648 /* Returns true iff DECLARATOR is a declaration for a function. */
1651 function_declarator_p (const cp_declarator
*declarator
)
1655 if (declarator
->kind
== cdk_function
1656 && declarator
->declarator
->kind
== cdk_id
)
1658 if (declarator
->kind
== cdk_id
1659 || declarator
->kind
== cdk_error
)
1661 declarator
= declarator
->declarator
;
1671 A cp_parser parses the token stream as specified by the C++
1672 grammar. Its job is purely parsing, not semantic analysis. For
1673 example, the parser breaks the token stream into declarators,
1674 expressions, statements, and other similar syntactic constructs.
1675 It does not check that the types of the expressions on either side
1676 of an assignment-statement are compatible, or that a function is
1677 not declared with a parameter of type `void'.
1679 The parser invokes routines elsewhere in the compiler to perform
1680 semantic analysis and to build up the abstract syntax tree for the
1683 The parser (and the template instantiation code, which is, in a
1684 way, a close relative of parsing) are the only parts of the
1685 compiler that should be calling push_scope and pop_scope, or
1686 related functions. The parser (and template instantiation code)
1687 keeps track of what scope is presently active; everything else
1688 should simply honor that. (The code that generates static
1689 initializers may also need to set the scope, in order to check
1690 access control correctly when emitting the initializers.)
1695 The parser is of the standard recursive-descent variety. Upcoming
1696 tokens in the token stream are examined in order to determine which
1697 production to use when parsing a non-terminal. Some C++ constructs
1698 require arbitrary look ahead to disambiguate. For example, it is
1699 impossible, in the general case, to tell whether a statement is an
1700 expression or declaration without scanning the entire statement.
1701 Therefore, the parser is capable of "parsing tentatively." When the
1702 parser is not sure what construct comes next, it enters this mode.
1703 Then, while we attempt to parse the construct, the parser queues up
1704 error messages, rather than issuing them immediately, and saves the
1705 tokens it consumes. If the construct is parsed successfully, the
1706 parser "commits", i.e., it issues any queued error messages and
1707 the tokens that were being preserved are permanently discarded.
1708 If, however, the construct is not parsed successfully, the parser
1709 rolls back its state completely so that it can resume parsing using
1710 a different alternative.
1715 The performance of the parser could probably be improved substantially.
1716 We could often eliminate the need to parse tentatively by looking ahead
1717 a little bit. In some places, this approach might not entirely eliminate
1718 the need to parse tentatively, but it might still speed up the average
1721 /* Flags that are passed to some parsing functions. These values can
1722 be bitwise-ored together. */
1727 CP_PARSER_FLAGS_NONE
= 0x0,
1728 /* The construct is optional. If it is not present, then no error
1729 should be issued. */
1730 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1731 /* When parsing a type-specifier, treat user-defined type-names
1732 as non-type identifiers. */
1733 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1734 /* When parsing a type-specifier, do not try to parse a class-specifier
1735 or enum-specifier. */
1736 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1737 /* When parsing a decl-specifier-seq, only allow type-specifier or
1739 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1742 /* This type is used for parameters and variables which hold
1743 combinations of the above flags. */
1744 typedef int cp_parser_flags
;
1746 /* The different kinds of declarators we want to parse. */
1748 enum cp_parser_declarator_kind
1750 /* We want an abstract declarator. */
1751 CP_PARSER_DECLARATOR_ABSTRACT
,
1752 /* We want a named declarator. */
1753 CP_PARSER_DECLARATOR_NAMED
,
1754 /* We don't mind, but the name must be an unqualified-id. */
1755 CP_PARSER_DECLARATOR_EITHER
1758 /* The precedence values used to parse binary expressions. The minimum value
1759 of PREC must be 1, because zero is reserved to quickly discriminate
1760 binary operators from other tokens. */
1765 PREC_LOGICAL_OR_EXPRESSION
,
1766 PREC_LOGICAL_AND_EXPRESSION
,
1767 PREC_INCLUSIVE_OR_EXPRESSION
,
1768 PREC_EXCLUSIVE_OR_EXPRESSION
,
1769 PREC_AND_EXPRESSION
,
1770 PREC_EQUALITY_EXPRESSION
,
1771 PREC_RELATIONAL_EXPRESSION
,
1772 PREC_SHIFT_EXPRESSION
,
1773 PREC_ADDITIVE_EXPRESSION
,
1774 PREC_MULTIPLICATIVE_EXPRESSION
,
1776 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1779 /* A mapping from a token type to a corresponding tree node type, with a
1780 precedence value. */
1782 struct cp_parser_binary_operations_map_node
1784 /* The token type. */
1785 enum cpp_ttype token_type
;
1786 /* The corresponding tree code. */
1787 enum tree_code tree_type
;
1788 /* The precedence of this operator. */
1789 enum cp_parser_prec prec
;
1792 struct cp_parser_expression_stack_entry
1794 /* Left hand side of the binary operation we are currently
1797 /* Original tree code for left hand side, if it was a binary
1798 expression itself (used for -Wparentheses). */
1799 enum tree_code lhs_type
;
1800 /* Tree code for the binary operation we are parsing. */
1801 enum tree_code tree_type
;
1802 /* Precedence of the binary operation we are parsing. */
1803 enum cp_parser_prec prec
;
1804 /* Location of the binary operation we are parsing. */
1808 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1809 entries because precedence levels on the stack are monotonically
1811 typedef struct cp_parser_expression_stack_entry
1812 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1816 /* Constructors and destructors. */
1818 static cp_parser_context
*cp_parser_context_new
1819 (cp_parser_context
*);
1821 /* Class variables. */
1823 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1825 /* The operator-precedence table used by cp_parser_binary_expression.
1826 Transformed into an associative array (binops_by_token) by
1829 static const cp_parser_binary_operations_map_node binops
[] = {
1830 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1831 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1833 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1834 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1835 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1837 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1838 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1840 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1841 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1843 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1844 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1845 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1846 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1848 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1849 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1851 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1853 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1855 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1857 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1859 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1862 /* The same as binops, but initialized by cp_parser_new so that
1863 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1865 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1867 /* Constructors and destructors. */
1869 /* Construct a new context. The context below this one on the stack
1870 is given by NEXT. */
1872 static cp_parser_context
*
1873 cp_parser_context_new (cp_parser_context
* next
)
1875 cp_parser_context
*context
;
1877 /* Allocate the storage. */
1878 if (cp_parser_context_free_list
!= NULL
)
1880 /* Pull the first entry from the free list. */
1881 context
= cp_parser_context_free_list
;
1882 cp_parser_context_free_list
= context
->next
;
1883 memset (context
, 0, sizeof (*context
));
1886 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1888 /* No errors have occurred yet in this context. */
1889 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1890 /* If this is not the bottommost context, copy information that we
1891 need from the previous context. */
1894 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1895 expression, then we are parsing one in this context, too. */
1896 context
->object_type
= next
->object_type
;
1897 /* Thread the stack. */
1898 context
->next
= next
;
1904 /* Managing the unparsed function queues. */
1906 #define unparsed_funs_with_default_args \
1907 parser->unparsed_queues->last ().funs_with_default_args
1908 #define unparsed_funs_with_definitions \
1909 parser->unparsed_queues->last ().funs_with_definitions
1910 #define unparsed_nsdmis \
1911 parser->unparsed_queues->last ().nsdmis
1912 #define unparsed_classes \
1913 parser->unparsed_queues->last ().classes
1916 push_unparsed_function_queues (cp_parser
*parser
)
1918 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1919 vec_safe_push (parser
->unparsed_queues
, e
);
1923 pop_unparsed_function_queues (cp_parser
*parser
)
1925 release_tree_vector (unparsed_funs_with_definitions
);
1926 parser
->unparsed_queues
->pop ();
1931 /* Constructors and destructors. */
1933 static cp_parser
*cp_parser_new
1936 /* Routines to parse various constructs.
1938 Those that return `tree' will return the error_mark_node (rather
1939 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1940 Sometimes, they will return an ordinary node if error-recovery was
1941 attempted, even though a parse error occurred. So, to check
1942 whether or not a parse error occurred, you should always use
1943 cp_parser_error_occurred. If the construct is optional (indicated
1944 either by an `_opt' in the name of the function that does the
1945 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1946 the construct is not present. */
1948 /* Lexical conventions [gram.lex] */
1950 static tree cp_parser_identifier
1952 static tree cp_parser_string_literal
1953 (cp_parser
*, bool, bool, bool);
1954 static tree cp_parser_userdef_char_literal
1956 static tree cp_parser_userdef_string_literal
1958 static tree cp_parser_userdef_numeric_literal
1961 /* Basic concepts [gram.basic] */
1963 static bool cp_parser_translation_unit
1966 /* Expressions [gram.expr] */
1968 static tree cp_parser_primary_expression
1969 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1970 static tree cp_parser_id_expression
1971 (cp_parser
*, bool, bool, bool *, bool, bool);
1972 static tree cp_parser_unqualified_id
1973 (cp_parser
*, bool, bool, bool, bool);
1974 static tree cp_parser_nested_name_specifier_opt
1975 (cp_parser
*, bool, bool, bool, bool);
1976 static tree cp_parser_nested_name_specifier
1977 (cp_parser
*, bool, bool, bool, bool);
1978 static tree cp_parser_qualifying_entity
1979 (cp_parser
*, bool, bool, bool, bool, bool);
1980 static tree cp_parser_postfix_expression
1981 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1982 static tree cp_parser_postfix_open_square_expression
1983 (cp_parser
*, tree
, bool, bool);
1984 static tree cp_parser_postfix_dot_deref_expression
1985 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1986 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1987 (cp_parser
*, int, bool, bool, bool *);
1988 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1989 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1990 static void cp_parser_pseudo_destructor_name
1991 (cp_parser
*, tree
, tree
*, tree
*);
1992 static tree cp_parser_unary_expression
1993 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1994 static enum tree_code cp_parser_unary_operator
1996 static tree cp_parser_new_expression
1998 static vec
<tree
, va_gc
> *cp_parser_new_placement
2000 static tree cp_parser_new_type_id
2001 (cp_parser
*, tree
*);
2002 static cp_declarator
*cp_parser_new_declarator_opt
2004 static cp_declarator
*cp_parser_direct_new_declarator
2006 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2008 static tree cp_parser_delete_expression
2010 static tree cp_parser_cast_expression
2011 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2012 static tree cp_parser_binary_expression
2013 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2014 static tree cp_parser_question_colon_clause
2015 (cp_parser
*, tree
);
2016 static tree cp_parser_assignment_expression
2017 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2018 static enum tree_code cp_parser_assignment_operator_opt
2020 static tree cp_parser_expression
2021 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2022 static tree cp_parser_constant_expression
2023 (cp_parser
*, bool = false, bool * = NULL
);
2024 static tree cp_parser_builtin_offsetof
2026 static tree cp_parser_lambda_expression
2028 static void cp_parser_lambda_introducer
2029 (cp_parser
*, tree
);
2030 static bool cp_parser_lambda_declarator_opt
2031 (cp_parser
*, tree
);
2032 static void cp_parser_lambda_body
2033 (cp_parser
*, tree
);
2035 /* Statements [gram.stmt.stmt] */
2037 static void cp_parser_statement
2038 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
);
2039 static void cp_parser_label_for_labeled_statement
2040 (cp_parser
*, tree
);
2041 static tree cp_parser_expression_statement
2042 (cp_parser
*, tree
);
2043 static tree cp_parser_compound_statement
2044 (cp_parser
*, tree
, int, bool);
2045 static void cp_parser_statement_seq_opt
2046 (cp_parser
*, tree
);
2047 static tree cp_parser_selection_statement
2048 (cp_parser
*, bool *, vec
<tree
> *);
2049 static tree cp_parser_condition
2051 static tree cp_parser_iteration_statement
2052 (cp_parser
*, bool);
2053 static bool cp_parser_for_init_statement
2054 (cp_parser
*, tree
*decl
);
2055 static tree cp_parser_for
2056 (cp_parser
*, bool);
2057 static tree cp_parser_c_for
2058 (cp_parser
*, tree
, tree
, bool);
2059 static tree cp_parser_range_for
2060 (cp_parser
*, tree
, tree
, tree
, bool);
2061 static void do_range_for_auto_deduction
2063 static tree cp_parser_perform_range_for_lookup
2064 (tree
, tree
*, tree
*);
2065 static tree cp_parser_range_for_member_function
2067 static tree cp_parser_jump_statement
2069 static void cp_parser_declaration_statement
2072 static tree cp_parser_implicitly_scoped_statement
2073 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2074 static void cp_parser_already_scoped_statement
2075 (cp_parser
*, const token_indent_info
&);
2077 /* Declarations [gram.dcl.dcl] */
2079 static void cp_parser_declaration_seq_opt
2081 static void cp_parser_declaration
2083 static void cp_parser_block_declaration
2084 (cp_parser
*, bool);
2085 static void cp_parser_simple_declaration
2086 (cp_parser
*, bool, tree
*);
2087 static void cp_parser_decl_specifier_seq
2088 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2089 static tree cp_parser_storage_class_specifier_opt
2091 static tree cp_parser_function_specifier_opt
2092 (cp_parser
*, cp_decl_specifier_seq
*);
2093 static tree cp_parser_type_specifier
2094 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2096 static tree cp_parser_simple_type_specifier
2097 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2098 static tree cp_parser_type_name
2099 (cp_parser
*, bool);
2100 static tree cp_parser_type_name
2102 static tree cp_parser_nonclass_name
2103 (cp_parser
* parser
);
2104 static tree cp_parser_elaborated_type_specifier
2105 (cp_parser
*, bool, bool);
2106 static tree cp_parser_enum_specifier
2108 static void cp_parser_enumerator_list
2109 (cp_parser
*, tree
);
2110 static void cp_parser_enumerator_definition
2111 (cp_parser
*, tree
);
2112 static tree cp_parser_namespace_name
2114 static void cp_parser_namespace_definition
2116 static void cp_parser_namespace_body
2118 static tree cp_parser_qualified_namespace_specifier
2120 static void cp_parser_namespace_alias_definition
2122 static bool cp_parser_using_declaration
2123 (cp_parser
*, bool);
2124 static void cp_parser_using_directive
2126 static tree cp_parser_alias_declaration
2128 static void cp_parser_asm_definition
2130 static void cp_parser_linkage_specification
2132 static void cp_parser_static_assert
2133 (cp_parser
*, bool);
2134 static tree cp_parser_decltype
2137 /* Declarators [gram.dcl.decl] */
2139 static tree cp_parser_init_declarator
2140 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2141 bool, bool, int, bool *, tree
*, location_t
*);
2142 static cp_declarator
*cp_parser_declarator
2143 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2144 static cp_declarator
*cp_parser_direct_declarator
2145 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2146 static enum tree_code cp_parser_ptr_operator
2147 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2148 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2150 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2152 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2154 static tree cp_parser_tx_qualifier_opt
2156 static tree cp_parser_late_return_type_opt
2157 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2158 static tree cp_parser_declarator_id
2159 (cp_parser
*, bool);
2160 static tree cp_parser_type_id
2162 static tree cp_parser_template_type_arg
2164 static tree
cp_parser_trailing_type_id (cp_parser
*);
2165 static tree cp_parser_type_id_1
2166 (cp_parser
*, bool, bool);
2167 static void cp_parser_type_specifier_seq
2168 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2169 static tree cp_parser_parameter_declaration_clause
2171 static tree cp_parser_parameter_declaration_list
2172 (cp_parser
*, bool *);
2173 static cp_parameter_declarator
*cp_parser_parameter_declaration
2174 (cp_parser
*, bool, bool *);
2175 static tree cp_parser_default_argument
2176 (cp_parser
*, bool);
2177 static void cp_parser_function_body
2178 (cp_parser
*, bool);
2179 static tree cp_parser_initializer
2180 (cp_parser
*, bool *, bool *);
2181 static tree cp_parser_initializer_clause
2182 (cp_parser
*, bool *);
2183 static tree cp_parser_braced_list
2184 (cp_parser
*, bool*);
2185 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2186 (cp_parser
*, bool *);
2188 static bool cp_parser_ctor_initializer_opt_and_function_body
2189 (cp_parser
*, bool);
2191 static tree cp_parser_late_parsing_omp_declare_simd
2192 (cp_parser
*, tree
);
2194 static tree cp_parser_late_parsing_cilk_simd_fn_info
2195 (cp_parser
*, tree
);
2197 static tree cp_parser_late_parsing_oacc_routine
2198 (cp_parser
*, tree
);
2200 static tree synthesize_implicit_template_parm
2201 (cp_parser
*, tree
);
2202 static tree finish_fully_implicit_template
2203 (cp_parser
*, tree
);
2205 /* Classes [gram.class] */
2207 static tree cp_parser_class_name
2208 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2209 static tree cp_parser_class_specifier
2211 static tree cp_parser_class_head
2212 (cp_parser
*, bool *);
2213 static enum tag_types cp_parser_class_key
2215 static void cp_parser_type_parameter_key
2216 (cp_parser
* parser
);
2217 static void cp_parser_member_specification_opt
2219 static void cp_parser_member_declaration
2221 static tree cp_parser_pure_specifier
2223 static tree cp_parser_constant_initializer
2226 /* Derived classes [gram.class.derived] */
2228 static tree cp_parser_base_clause
2230 static tree cp_parser_base_specifier
2233 /* Special member functions [gram.special] */
2235 static tree cp_parser_conversion_function_id
2237 static tree cp_parser_conversion_type_id
2239 static cp_declarator
*cp_parser_conversion_declarator_opt
2241 static bool cp_parser_ctor_initializer_opt
2243 static void cp_parser_mem_initializer_list
2245 static tree cp_parser_mem_initializer
2247 static tree cp_parser_mem_initializer_id
2250 /* Overloading [gram.over] */
2252 static tree cp_parser_operator_function_id
2254 static tree cp_parser_operator
2257 /* Templates [gram.temp] */
2259 static void cp_parser_template_declaration
2260 (cp_parser
*, bool);
2261 static tree cp_parser_template_parameter_list
2263 static tree cp_parser_template_parameter
2264 (cp_parser
*, bool *, bool *);
2265 static tree cp_parser_type_parameter
2266 (cp_parser
*, bool *);
2267 static tree cp_parser_template_id
2268 (cp_parser
*, bool, bool, enum tag_types
, bool);
2269 static tree cp_parser_template_name
2270 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2271 static tree cp_parser_template_argument_list
2273 static tree cp_parser_template_argument
2275 static void cp_parser_explicit_instantiation
2277 static void cp_parser_explicit_specialization
2280 /* Exception handling [gram.exception] */
2282 static tree cp_parser_try_block
2284 static bool cp_parser_function_try_block
2286 static void cp_parser_handler_seq
2288 static void cp_parser_handler
2290 static tree cp_parser_exception_declaration
2292 static tree cp_parser_throw_expression
2294 static tree cp_parser_exception_specification_opt
2296 static tree cp_parser_type_id_list
2299 /* GNU Extensions */
2301 static tree cp_parser_asm_specification_opt
2303 static tree cp_parser_asm_operand_list
2305 static tree cp_parser_asm_clobber_list
2307 static tree cp_parser_asm_label_list
2309 static bool cp_next_tokens_can_be_attribute_p
2311 static bool cp_next_tokens_can_be_gnu_attribute_p
2313 static bool cp_next_tokens_can_be_std_attribute_p
2315 static bool cp_nth_tokens_can_be_std_attribute_p
2316 (cp_parser
*, size_t);
2317 static bool cp_nth_tokens_can_be_gnu_attribute_p
2318 (cp_parser
*, size_t);
2319 static bool cp_nth_tokens_can_be_attribute_p
2320 (cp_parser
*, size_t);
2321 static tree cp_parser_attributes_opt
2323 static tree cp_parser_gnu_attributes_opt
2325 static tree cp_parser_gnu_attribute_list
2327 static tree cp_parser_std_attribute
2329 static tree cp_parser_std_attribute_spec
2331 static tree cp_parser_std_attribute_spec_seq
2333 static bool cp_parser_extension_opt
2334 (cp_parser
*, int *);
2335 static void cp_parser_label_declaration
2338 /* Concept Extensions */
2340 static tree cp_parser_requires_clause
2342 static tree cp_parser_requires_clause_opt
2344 static tree cp_parser_requires_expression
2346 static tree cp_parser_requirement_parameter_list
2348 static tree cp_parser_requirement_body
2350 static tree cp_parser_requirement_list
2352 static tree cp_parser_requirement
2354 static tree cp_parser_simple_requirement
2356 static tree cp_parser_compound_requirement
2358 static tree cp_parser_type_requirement
2360 static tree cp_parser_nested_requirement
2363 /* Transactional Memory Extensions */
2365 static tree cp_parser_transaction
2366 (cp_parser
*, cp_token
*);
2367 static tree cp_parser_transaction_expression
2368 (cp_parser
*, enum rid
);
2369 static bool cp_parser_function_transaction
2370 (cp_parser
*, enum rid
);
2371 static tree cp_parser_transaction_cancel
2374 enum pragma_context
{
2381 static bool cp_parser_pragma
2382 (cp_parser
*, enum pragma_context
);
2384 /* Objective-C++ Productions */
2386 static tree cp_parser_objc_message_receiver
2388 static tree cp_parser_objc_message_args
2390 static tree cp_parser_objc_message_expression
2392 static tree cp_parser_objc_encode_expression
2394 static tree cp_parser_objc_defs_expression
2396 static tree cp_parser_objc_protocol_expression
2398 static tree cp_parser_objc_selector_expression
2400 static tree cp_parser_objc_expression
2402 static bool cp_parser_objc_selector_p
2404 static tree cp_parser_objc_selector
2406 static tree cp_parser_objc_protocol_refs_opt
2408 static void cp_parser_objc_declaration
2409 (cp_parser
*, tree
);
2410 static tree cp_parser_objc_statement
2412 static bool cp_parser_objc_valid_prefix_attributes
2413 (cp_parser
*, tree
*);
2414 static void cp_parser_objc_at_property_declaration
2416 static void cp_parser_objc_at_synthesize_declaration
2418 static void cp_parser_objc_at_dynamic_declaration
2420 static tree cp_parser_objc_struct_declaration
2423 /* Utility Routines */
2425 static tree cp_parser_lookup_name
2426 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2427 static tree cp_parser_lookup_name_simple
2428 (cp_parser
*, tree
, location_t
);
2429 static tree cp_parser_maybe_treat_template_as_class
2431 static bool cp_parser_check_declarator_template_parameters
2432 (cp_parser
*, cp_declarator
*, location_t
);
2433 static bool cp_parser_check_template_parameters
2434 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2435 static tree cp_parser_simple_cast_expression
2437 static tree cp_parser_global_scope_opt
2438 (cp_parser
*, bool);
2439 static bool cp_parser_constructor_declarator_p
2440 (cp_parser
*, bool);
2441 static tree cp_parser_function_definition_from_specifiers_and_declarator
2442 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2443 static tree cp_parser_function_definition_after_declarator
2444 (cp_parser
*, bool);
2445 static bool cp_parser_template_declaration_after_export
2446 (cp_parser
*, bool);
2447 static void cp_parser_perform_template_parameter_access_checks
2448 (vec
<deferred_access_check
, va_gc
> *);
2449 static tree cp_parser_single_declaration
2450 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2451 static tree cp_parser_functional_cast
2452 (cp_parser
*, tree
);
2453 static tree cp_parser_save_member_function_body
2454 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2455 static tree cp_parser_save_nsdmi
2457 static tree cp_parser_enclosed_template_argument_list
2459 static void cp_parser_save_default_args
2460 (cp_parser
*, tree
);
2461 static void cp_parser_late_parsing_for_member
2462 (cp_parser
*, tree
);
2463 static tree cp_parser_late_parse_one_default_arg
2464 (cp_parser
*, tree
, tree
, tree
);
2465 static void cp_parser_late_parsing_nsdmi
2466 (cp_parser
*, tree
);
2467 static void cp_parser_late_parsing_default_args
2468 (cp_parser
*, tree
);
2469 static tree cp_parser_sizeof_operand
2470 (cp_parser
*, enum rid
);
2471 static tree cp_parser_trait_expr
2472 (cp_parser
*, enum rid
);
2473 static bool cp_parser_declares_only_class_p
2475 static void cp_parser_set_storage_class
2476 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2477 static void cp_parser_set_decl_spec_type
2478 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2479 static void set_and_check_decl_spec_loc
2480 (cp_decl_specifier_seq
*decl_specs
,
2481 cp_decl_spec ds
, cp_token
*);
2482 static bool cp_parser_friend_p
2483 (const cp_decl_specifier_seq
*);
2484 static void cp_parser_required_error
2485 (cp_parser
*, required_token
, bool);
2486 static cp_token
*cp_parser_require
2487 (cp_parser
*, enum cpp_ttype
, required_token
);
2488 static cp_token
*cp_parser_require_keyword
2489 (cp_parser
*, enum rid
, required_token
);
2490 static bool cp_parser_token_starts_function_definition_p
2492 static bool cp_parser_next_token_starts_class_definition_p
2494 static bool cp_parser_next_token_ends_template_argument_p
2496 static bool cp_parser_nth_token_starts_template_argument_list_p
2497 (cp_parser
*, size_t);
2498 static enum tag_types cp_parser_token_is_class_key
2500 static enum tag_types cp_parser_token_is_type_parameter_key
2502 static void cp_parser_check_class_key
2503 (enum tag_types
, tree type
);
2504 static void cp_parser_check_access_in_redeclaration
2505 (tree type
, location_t location
);
2506 static bool cp_parser_optional_template_keyword
2508 static void cp_parser_pre_parsed_nested_name_specifier
2510 static bool cp_parser_cache_group
2511 (cp_parser
*, enum cpp_ttype
, unsigned);
2512 static tree cp_parser_cache_defarg
2513 (cp_parser
*parser
, bool nsdmi
);
2514 static void cp_parser_parse_tentatively
2516 static void cp_parser_commit_to_tentative_parse
2518 static void cp_parser_commit_to_topmost_tentative_parse
2520 static void cp_parser_abort_tentative_parse
2522 static bool cp_parser_parse_definitely
2524 static inline bool cp_parser_parsing_tentatively
2526 static bool cp_parser_uncommitted_to_tentative_parse_p
2528 static void cp_parser_error
2529 (cp_parser
*, const char *);
2530 static void cp_parser_name_lookup_error
2531 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2532 static bool cp_parser_simulate_error
2534 static bool cp_parser_check_type_definition
2536 static void cp_parser_check_for_definition_in_return_type
2537 (cp_declarator
*, tree
, location_t type_location
);
2538 static void cp_parser_check_for_invalid_template_id
2539 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2540 static bool cp_parser_non_integral_constant_expression
2541 (cp_parser
*, non_integral_constant
);
2542 static void cp_parser_diagnose_invalid_type_name
2543 (cp_parser
*, tree
, location_t
);
2544 static bool cp_parser_parse_and_diagnose_invalid_type_name
2546 static int cp_parser_skip_to_closing_parenthesis
2547 (cp_parser
*, bool, bool, bool);
2548 static void cp_parser_skip_to_end_of_statement
2550 static void cp_parser_consume_semicolon_at_end_of_statement
2552 static void cp_parser_skip_to_end_of_block_or_statement
2554 static bool cp_parser_skip_to_closing_brace
2556 static void cp_parser_skip_to_end_of_template_parameter_list
2558 static void cp_parser_skip_to_pragma_eol
2559 (cp_parser
*, cp_token
*);
2560 static bool cp_parser_error_occurred
2562 static bool cp_parser_allow_gnu_extensions_p
2564 static bool cp_parser_is_pure_string_literal
2566 static bool cp_parser_is_string_literal
2568 static bool cp_parser_is_keyword
2569 (cp_token
*, enum rid
);
2570 static tree cp_parser_make_typename_type
2571 (cp_parser
*, tree
, location_t location
);
2572 static cp_declarator
* cp_parser_make_indirect_declarator
2573 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2574 static bool cp_parser_compound_literal_p
2576 static bool cp_parser_array_designator_p
2578 static bool cp_parser_skip_to_closing_square_bracket
2581 /* Concept-related syntactic transformations */
2583 static tree
cp_parser_maybe_concept_name (cp_parser
*, tree
);
2584 static tree
cp_parser_maybe_partial_concept_id (cp_parser
*, tree
, tree
);
2586 // -------------------------------------------------------------------------- //
2587 // Unevaluated Operand Guard
2589 // Implementation of an RAII helper for unevaluated operand parsing.
2590 cp_unevaluated::cp_unevaluated ()
2592 ++cp_unevaluated_operand
;
2593 ++c_inhibit_evaluation_warnings
;
2596 cp_unevaluated::~cp_unevaluated ()
2598 --c_inhibit_evaluation_warnings
;
2599 --cp_unevaluated_operand
;
2602 // -------------------------------------------------------------------------- //
2603 // Tentative Parsing
2605 /* Returns nonzero if we are parsing tentatively. */
2608 cp_parser_parsing_tentatively (cp_parser
* parser
)
2610 return parser
->context
->next
!= NULL
;
2613 /* Returns nonzero if TOKEN is a string literal. */
2616 cp_parser_is_pure_string_literal (cp_token
* token
)
2618 return (token
->type
== CPP_STRING
||
2619 token
->type
== CPP_STRING16
||
2620 token
->type
== CPP_STRING32
||
2621 token
->type
== CPP_WSTRING
||
2622 token
->type
== CPP_UTF8STRING
);
2625 /* Returns nonzero if TOKEN is a string literal
2626 of a user-defined string literal. */
2629 cp_parser_is_string_literal (cp_token
* token
)
2631 return (cp_parser_is_pure_string_literal (token
) ||
2632 token
->type
== CPP_STRING_USERDEF
||
2633 token
->type
== CPP_STRING16_USERDEF
||
2634 token
->type
== CPP_STRING32_USERDEF
||
2635 token
->type
== CPP_WSTRING_USERDEF
||
2636 token
->type
== CPP_UTF8STRING_USERDEF
);
2639 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2642 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2644 return token
->keyword
== keyword
;
2647 /* If not parsing tentatively, issue a diagnostic of the form
2648 FILE:LINE: MESSAGE before TOKEN
2649 where TOKEN is the next token in the input stream. MESSAGE
2650 (specified by the caller) is usually of the form "expected
2654 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2656 if (!cp_parser_simulate_error (parser
))
2658 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2659 /* This diagnostic makes more sense if it is tagged to the line
2660 of the token we just peeked at. */
2661 cp_lexer_set_source_position_from_token (token
);
2663 if (token
->type
== CPP_PRAGMA
)
2665 error_at (token
->location
,
2666 "%<#pragma%> is not allowed here");
2667 cp_parser_skip_to_pragma_eol (parser
, token
);
2671 c_parse_error (gmsgid
,
2672 /* Because c_parser_error does not understand
2673 CPP_KEYWORD, keywords are treated like
2675 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2676 token
->u
.value
, token
->flags
);
2680 /* Issue an error about name-lookup failing. NAME is the
2681 IDENTIFIER_NODE DECL is the result of
2682 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2683 the thing that we hoped to find. */
2686 cp_parser_name_lookup_error (cp_parser
* parser
,
2689 name_lookup_error desired
,
2690 location_t location
)
2692 /* If name lookup completely failed, tell the user that NAME was not
2694 if (decl
== error_mark_node
)
2696 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2697 error_at (location
, "%<%E::%E%> has not been declared",
2698 parser
->scope
, name
);
2699 else if (parser
->scope
== global_namespace
)
2700 error_at (location
, "%<::%E%> has not been declared", name
);
2701 else if (parser
->object_scope
2702 && !CLASS_TYPE_P (parser
->object_scope
))
2703 error_at (location
, "request for member %qE in non-class type %qT",
2704 name
, parser
->object_scope
);
2705 else if (parser
->object_scope
)
2706 error_at (location
, "%<%T::%E%> has not been declared",
2707 parser
->object_scope
, name
);
2709 error_at (location
, "%qE has not been declared", name
);
2711 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2716 error_at (location
, "%<%E::%E%> is not a type",
2717 parser
->scope
, name
);
2720 error_at (location
, "%<%E::%E%> is not a class or namespace",
2721 parser
->scope
, name
);
2725 "%<%E::%E%> is not a class, namespace, or enumeration",
2726 parser
->scope
, name
);
2733 else if (parser
->scope
== global_namespace
)
2738 error_at (location
, "%<::%E%> is not a type", name
);
2741 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2745 "%<::%E%> is not a class, namespace, or enumeration",
2757 error_at (location
, "%qE is not a type", name
);
2760 error_at (location
, "%qE is not a class or namespace", name
);
2764 "%qE is not a class, namespace, or enumeration", name
);
2772 /* If we are parsing tentatively, remember that an error has occurred
2773 during this tentative parse. Returns true if the error was
2774 simulated; false if a message should be issued by the caller. */
2777 cp_parser_simulate_error (cp_parser
* parser
)
2779 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2781 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2787 /* This function is called when a type is defined. If type
2788 definitions are forbidden at this point, an error message is
2792 cp_parser_check_type_definition (cp_parser
* parser
)
2794 /* If types are forbidden here, issue a message. */
2795 if (parser
->type_definition_forbidden_message
)
2797 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2798 in the message need to be interpreted. */
2799 error (parser
->type_definition_forbidden_message
);
2805 /* This function is called when the DECLARATOR is processed. The TYPE
2806 was a type defined in the decl-specifiers. If it is invalid to
2807 define a type in the decl-specifiers for DECLARATOR, an error is
2808 issued. TYPE_LOCATION is the location of TYPE and is used
2809 for error reporting. */
2812 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2813 tree type
, location_t type_location
)
2815 /* [dcl.fct] forbids type definitions in return types.
2816 Unfortunately, it's not easy to know whether or not we are
2817 processing a return type until after the fact. */
2819 && (declarator
->kind
== cdk_pointer
2820 || declarator
->kind
== cdk_reference
2821 || declarator
->kind
== cdk_ptrmem
))
2822 declarator
= declarator
->declarator
;
2824 && declarator
->kind
== cdk_function
)
2826 error_at (type_location
,
2827 "new types may not be defined in a return type");
2828 inform (type_location
,
2829 "(perhaps a semicolon is missing after the definition of %qT)",
2834 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2835 "<" in any valid C++ program. If the next token is indeed "<",
2836 issue a message warning the user about what appears to be an
2837 invalid attempt to form a template-id. LOCATION is the location
2838 of the type-specifier (TYPE) */
2841 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2843 enum tag_types tag_type
,
2844 location_t location
)
2846 cp_token_position start
= 0;
2848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2851 error_at (location
, "%qT is not a template", type
);
2852 else if (identifier_p (type
))
2854 if (tag_type
!= none_type
)
2855 error_at (location
, "%qE is not a class template", type
);
2857 error_at (location
, "%qE is not a template", type
);
2860 error_at (location
, "invalid template-id");
2861 /* Remember the location of the invalid "<". */
2862 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2863 start
= cp_lexer_token_position (parser
->lexer
, true);
2864 /* Consume the "<". */
2865 cp_lexer_consume_token (parser
->lexer
);
2866 /* Parse the template arguments. */
2867 cp_parser_enclosed_template_argument_list (parser
);
2868 /* Permanently remove the invalid template arguments so that
2869 this error message is not issued again. */
2871 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2875 /* If parsing an integral constant-expression, issue an error message
2876 about the fact that THING appeared and return true. Otherwise,
2877 return false. In either case, set
2878 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2881 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2882 non_integral_constant thing
)
2884 parser
->non_integral_constant_expression_p
= true;
2885 if (parser
->integral_constant_expression_p
)
2887 if (!parser
->allow_non_integral_constant_expression_p
)
2889 const char *msg
= NULL
;
2893 error ("floating-point literal "
2894 "cannot appear in a constant-expression");
2897 error ("a cast to a type other than an integral or "
2898 "enumeration type cannot appear in a "
2899 "constant-expression");
2902 error ("%<typeid%> operator "
2903 "cannot appear in a constant-expression");
2906 error ("non-constant compound literals "
2907 "cannot appear in a constant-expression");
2910 error ("a function call "
2911 "cannot appear in a constant-expression");
2914 error ("an increment "
2915 "cannot appear in a constant-expression");
2918 error ("an decrement "
2919 "cannot appear in a constant-expression");
2922 error ("an array reference "
2923 "cannot appear in a constant-expression");
2925 case NIC_ADDR_LABEL
:
2926 error ("the address of a label "
2927 "cannot appear in a constant-expression");
2929 case NIC_OVERLOADED
:
2930 error ("calls to overloaded operators "
2931 "cannot appear in a constant-expression");
2933 case NIC_ASSIGNMENT
:
2934 error ("an assignment cannot appear in a constant-expression");
2937 error ("a comma operator "
2938 "cannot appear in a constant-expression");
2940 case NIC_CONSTRUCTOR
:
2941 error ("a call to a constructor "
2942 "cannot appear in a constant-expression");
2944 case NIC_TRANSACTION
:
2945 error ("a transaction expression "
2946 "cannot appear in a constant-expression");
2952 msg
= "__FUNCTION__";
2954 case NIC_PRETTY_FUNC
:
2955 msg
= "__PRETTY_FUNCTION__";
2975 case NIC_PREINCREMENT
:
2978 case NIC_PREDECREMENT
:
2991 error ("%qs cannot appear in a constant-expression", msg
);
2998 /* Emit a diagnostic for an invalid type name. This function commits
2999 to the current active tentative parse, if any. (Otherwise, the
3000 problematic construct might be encountered again later, resulting
3001 in duplicate error messages.) LOCATION is the location of ID. */
3004 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3005 location_t location
)
3007 tree decl
, ambiguous_decls
;
3008 cp_parser_commit_to_tentative_parse (parser
);
3009 /* Try to lookup the identifier. */
3010 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3011 /*is_template=*/false,
3012 /*is_namespace=*/false,
3013 /*check_dependency=*/true,
3014 &ambiguous_decls
, location
);
3015 if (ambiguous_decls
)
3016 /* If the lookup was ambiguous, an error will already have
3019 /* If the lookup found a template-name, it means that the user forgot
3020 to specify an argument list. Emit a useful error message. */
3021 if (DECL_TYPE_TEMPLATE_P (decl
))
3024 "invalid use of template-name %qE without an argument list",
3026 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3028 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3029 error_at (location
, "invalid use of destructor %qD as a type", id
);
3030 else if (TREE_CODE (decl
) == TYPE_DECL
)
3031 /* Something like 'unsigned A a;' */
3032 error_at (location
, "invalid combination of multiple type-specifiers");
3033 else if (!parser
->scope
)
3035 /* Issue an error message. */
3036 error_at (location
, "%qE does not name a type", id
);
3037 /* If we're in a template class, it's possible that the user was
3038 referring to a type from a base class. For example:
3040 template <typename T> struct A { typedef T X; };
3041 template <typename T> struct B : public A<T> { X x; };
3043 The user should have said "typename A<T>::X". */
3044 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3045 inform (location
, "C++11 %<constexpr%> only available with "
3046 "-std=c++11 or -std=gnu++11");
3047 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3048 inform (location
, "C++11 %<noexcept%> only available with "
3049 "-std=c++11 or -std=gnu++11");
3050 else if (cxx_dialect
< cxx11
3051 && TREE_CODE (id
) == IDENTIFIER_NODE
3052 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
3053 inform (location
, "C++11 %<thread_local%> only available with "
3054 "-std=c++11 or -std=gnu++11");
3055 else if (processing_template_decl
&& current_class_type
3056 && TYPE_BINFO (current_class_type
))
3060 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3064 tree base_type
= BINFO_TYPE (b
);
3065 if (CLASS_TYPE_P (base_type
)
3066 && dependent_type_p (base_type
))
3069 /* Go from a particular instantiation of the
3070 template (which will have an empty TYPE_FIELDs),
3071 to the main version. */
3072 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3073 for (field
= TYPE_FIELDS (base_type
);
3075 field
= DECL_CHAIN (field
))
3076 if (TREE_CODE (field
) == TYPE_DECL
3077 && DECL_NAME (field
) == id
)
3080 "(perhaps %<typename %T::%E%> was intended)",
3081 BINFO_TYPE (b
), id
);
3090 /* Here we diagnose qualified-ids where the scope is actually correct,
3091 but the identifier does not resolve to a valid type name. */
3092 else if (parser
->scope
!= error_mark_node
)
3094 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3096 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3097 error_at (location_of (id
),
3098 "%qE in namespace %qE does not name a template type",
3101 error_at (location_of (id
),
3102 "%qE in namespace %qE does not name a type",
3105 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3107 else if (CLASS_TYPE_P (parser
->scope
)
3108 && constructor_name_p (id
, parser
->scope
))
3111 error_at (location
, "%<%T::%E%> names the constructor, not"
3112 " the type", parser
->scope
, id
);
3113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3114 error_at (location
, "and %qT has no template constructors",
3117 else if (TYPE_P (parser
->scope
)
3118 && dependent_scope_p (parser
->scope
))
3119 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3120 "%qT is a dependent scope",
3121 parser
->scope
, id
, parser
->scope
);
3122 else if (TYPE_P (parser
->scope
))
3124 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3125 error_at (location_of (id
),
3126 "%qE in %q#T does not name a template type",
3129 error_at (location_of (id
),
3130 "%qE in %q#T does not name a type",
3133 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3140 /* Check for a common situation where a type-name should be present,
3141 but is not, and issue a sensible error message. Returns true if an
3142 invalid type-name was detected.
3144 The situation handled by this function are variable declarations of the
3145 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3146 Usually, `ID' should name a type, but if we got here it means that it
3147 does not. We try to emit the best possible error message depending on
3148 how exactly the id-expression looks like. */
3151 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3156 /* Avoid duplicate error about ambiguous lookup. */
3157 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3159 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3160 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3164 cp_parser_parse_tentatively (parser
);
3165 id
= cp_parser_id_expression (parser
,
3166 /*template_keyword_p=*/false,
3167 /*check_dependency_p=*/true,
3168 /*template_p=*/NULL
,
3169 /*declarator_p=*/true,
3170 /*optional_p=*/false);
3171 /* If the next token is a (, this is a function with no explicit return
3172 type, i.e. constructor, destructor or conversion op. */
3173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3174 || TREE_CODE (id
) == TYPE_DECL
)
3176 cp_parser_abort_tentative_parse (parser
);
3179 if (!cp_parser_parse_definitely (parser
))
3182 /* Emit a diagnostic for the invalid type. */
3183 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3185 /* If we aren't in the middle of a declarator (i.e. in a
3186 parameter-declaration-clause), skip to the end of the declaration;
3187 there's no point in trying to process it. */
3188 if (!parser
->in_declarator_p
)
3189 cp_parser_skip_to_end_of_block_or_statement (parser
);
3193 /* Consume tokens up to, and including, the next non-nested closing `)'.
3194 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3195 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3196 found an unnested token of that type. */
3199 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3204 unsigned paren_depth
= 0;
3205 unsigned brace_depth
= 0;
3206 unsigned square_depth
= 0;
3208 if (recovering
&& or_ttype
== CPP_EOF
3209 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3214 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3216 /* Have we found what we're looking for before the closing paren? */
3217 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3218 && !brace_depth
&& !paren_depth
&& !square_depth
)
3221 switch (token
->type
)
3224 case CPP_PRAGMA_EOL
:
3225 /* If we've run out of tokens, then there is no closing `)'. */
3228 /* This is good for lambda expression capture-lists. */
3229 case CPP_OPEN_SQUARE
:
3232 case CPP_CLOSE_SQUARE
:
3233 if (!square_depth
--)
3238 /* This matches the processing in skip_to_end_of_statement. */
3243 case CPP_OPEN_BRACE
:
3246 case CPP_CLOSE_BRACE
:
3251 case CPP_OPEN_PAREN
:
3256 case CPP_CLOSE_PAREN
:
3257 if (!brace_depth
&& !paren_depth
--)
3260 cp_lexer_consume_token (parser
->lexer
);
3269 /* Consume the token. */
3270 cp_lexer_consume_token (parser
->lexer
);
3274 /* Consume tokens up to, and including, the next non-nested closing `)'.
3275 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3276 are doing error recovery. Returns -1 if OR_COMMA is true and we
3277 found an unnested token of that type. */
3280 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3285 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3286 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3287 ttype
, consume_paren
);
3290 /* Consume tokens until we reach the end of the current statement.
3291 Normally, that will be just before consuming a `;'. However, if a
3292 non-nested `}' comes first, then we stop before consuming that. */
3295 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3297 unsigned nesting_depth
= 0;
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser
->fully_implicit_function_template_p
)
3301 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3305 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3307 switch (token
->type
)
3310 case CPP_PRAGMA_EOL
:
3311 /* If we've run out of tokens, stop. */
3315 /* If the next token is a `;', we have reached the end of the
3321 case CPP_CLOSE_BRACE
:
3322 /* If this is a non-nested '}', stop before consuming it.
3323 That way, when confronted with something like:
3327 we stop before consuming the closing '}', even though we
3328 have not yet reached a `;'. */
3329 if (nesting_depth
== 0)
3332 /* If it is the closing '}' for a block that we have
3333 scanned, stop -- but only after consuming the token.
3339 we will stop after the body of the erroneously declared
3340 function, but before consuming the following `typedef'
3342 if (--nesting_depth
== 0)
3344 cp_lexer_consume_token (parser
->lexer
);
3348 case CPP_OPEN_BRACE
:
3356 /* Consume the token. */
3357 cp_lexer_consume_token (parser
->lexer
);
3361 /* This function is called at the end of a statement or declaration.
3362 If the next token is a semicolon, it is consumed; otherwise, error
3363 recovery is attempted. */
3366 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3368 /* Look for the trailing `;'. */
3369 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3371 /* If there is additional (erroneous) input, skip to the end of
3373 cp_parser_skip_to_end_of_statement (parser
);
3374 /* If the next token is now a `;', consume it. */
3375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3376 cp_lexer_consume_token (parser
->lexer
);
3380 /* Skip tokens until we have consumed an entire block, or until we
3381 have consumed a non-nested `;'. */
3384 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3386 int nesting_depth
= 0;
3388 /* Unwind generic function template scope if necessary. */
3389 if (parser
->fully_implicit_function_template_p
)
3390 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3392 while (nesting_depth
>= 0)
3394 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3396 switch (token
->type
)
3399 case CPP_PRAGMA_EOL
:
3400 /* If we've run out of tokens, stop. */
3404 /* Stop if this is an unnested ';'. */
3409 case CPP_CLOSE_BRACE
:
3410 /* Stop if this is an unnested '}', or closes the outermost
3413 if (nesting_depth
< 0)
3419 case CPP_OPEN_BRACE
:
3428 /* Consume the token. */
3429 cp_lexer_consume_token (parser
->lexer
);
3433 /* Skip tokens until a non-nested closing curly brace is the next
3434 token, or there are no more tokens. Return true in the first case,
3438 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3440 unsigned nesting_depth
= 0;
3444 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3446 switch (token
->type
)
3449 case CPP_PRAGMA_EOL
:
3450 /* If we've run out of tokens, stop. */
3453 case CPP_CLOSE_BRACE
:
3454 /* If the next token is a non-nested `}', then we have reached
3455 the end of the current block. */
3456 if (nesting_depth
-- == 0)
3460 case CPP_OPEN_BRACE
:
3461 /* If it the next token is a `{', then we are entering a new
3462 block. Consume the entire block. */
3470 /* Consume the token. */
3471 cp_lexer_consume_token (parser
->lexer
);
3475 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3476 parameter is the PRAGMA token, allowing us to purge the entire pragma
3480 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3484 parser
->lexer
->in_pragma
= false;
3487 token
= cp_lexer_consume_token (parser
->lexer
);
3488 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3490 /* Ensure that the pragma is not parsed again. */
3491 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3494 /* Require pragma end of line, resyncing with it as necessary. The
3495 arguments are as for cp_parser_skip_to_pragma_eol. */
3498 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3500 parser
->lexer
->in_pragma
= false;
3501 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3502 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3505 /* This is a simple wrapper around make_typename_type. When the id is
3506 an unresolved identifier node, we can provide a superior diagnostic
3507 using cp_parser_diagnose_invalid_type_name. */
3510 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3511 location_t id_location
)
3514 if (identifier_p (id
))
3516 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3517 /*complain=*/tf_none
);
3518 if (result
== error_mark_node
)
3519 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3522 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3525 /* This is a wrapper around the
3526 make_{pointer,ptrmem,reference}_declarator functions that decides
3527 which one to call based on the CODE and CLASS_TYPE arguments. The
3528 CODE argument should be one of the values returned by
3529 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3530 appertain to the pointer or reference. */
3532 static cp_declarator
*
3533 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3534 cp_cv_quals cv_qualifiers
,
3535 cp_declarator
*target
,
3538 if (code
== ERROR_MARK
)
3539 return cp_error_declarator
;
3541 if (code
== INDIRECT_REF
)
3542 if (class_type
== NULL_TREE
)
3543 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3545 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3546 target
, attributes
);
3547 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3548 return make_reference_declarator (cv_qualifiers
, target
,
3550 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3551 return make_reference_declarator (cv_qualifiers
, target
,
3556 /* Create a new C++ parser. */
3559 cp_parser_new (void)
3565 /* cp_lexer_new_main is called before doing GC allocation because
3566 cp_lexer_new_main might load a PCH file. */
3567 lexer
= cp_lexer_new_main ();
3569 /* Initialize the binops_by_token so that we can get the tree
3570 directly from the token. */
3571 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3572 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3574 parser
= ggc_cleared_alloc
<cp_parser
> ();
3575 parser
->lexer
= lexer
;
3576 parser
->context
= cp_parser_context_new (NULL
);
3578 /* For now, we always accept GNU extensions. */
3579 parser
->allow_gnu_extensions_p
= 1;
3581 /* The `>' token is a greater-than operator, not the end of a
3583 parser
->greater_than_is_operator_p
= true;
3585 parser
->default_arg_ok_p
= true;
3587 /* We are not parsing a constant-expression. */
3588 parser
->integral_constant_expression_p
= false;
3589 parser
->allow_non_integral_constant_expression_p
= false;
3590 parser
->non_integral_constant_expression_p
= false;
3592 /* Local variable names are not forbidden. */
3593 parser
->local_variables_forbidden_p
= false;
3595 /* We are not processing an `extern "C"' declaration. */
3596 parser
->in_unbraced_linkage_specification_p
= false;
3598 /* We are not processing a declarator. */
3599 parser
->in_declarator_p
= false;
3601 /* We are not processing a template-argument-list. */
3602 parser
->in_template_argument_list_p
= false;
3604 /* We are not in an iteration statement. */
3605 parser
->in_statement
= 0;
3607 /* We are not in a switch statement. */
3608 parser
->in_switch_statement_p
= false;
3610 /* We are not parsing a type-id inside an expression. */
3611 parser
->in_type_id_in_expr_p
= false;
3613 /* Declarations aren't implicitly extern "C". */
3614 parser
->implicit_extern_c
= false;
3616 /* String literals should be translated to the execution character set. */
3617 parser
->translate_strings_p
= true;
3619 /* We are not parsing a function body. */
3620 parser
->in_function_body
= false;
3622 /* We can correct until told otherwise. */
3623 parser
->colon_corrects_to_scope_p
= true;
3625 /* The unparsed function queue is empty. */
3626 push_unparsed_function_queues (parser
);
3628 /* There are no classes being defined. */
3629 parser
->num_classes_being_defined
= 0;
3631 /* No template parameters apply. */
3632 parser
->num_template_parameter_lists
= 0;
3634 /* Not declaring an implicit function template. */
3635 parser
->auto_is_implicit_function_template_parm_p
= false;
3636 parser
->fully_implicit_function_template_p
= false;
3637 parser
->implicit_template_parms
= 0;
3638 parser
->implicit_template_scope
= 0;
3640 /* Active OpenACC routine clauses. */
3641 parser
->oacc_routine
= NULL
;
3643 /* Allow constrained-type-specifiers. */
3644 parser
->prevent_constrained_type_specifiers
= 0;
3649 /* Create a cp_lexer structure which will emit the tokens in CACHE
3650 and push it onto the parser's lexer stack. This is used for delayed
3651 parsing of in-class method bodies and default arguments, and should
3652 not be confused with tentative parsing. */
3654 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3656 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3657 lexer
->next
= parser
->lexer
;
3658 parser
->lexer
= lexer
;
3660 /* Move the current source position to that of the first token in the
3662 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3665 /* Pop the top lexer off the parser stack. This is never used for the
3666 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3668 cp_parser_pop_lexer (cp_parser
*parser
)
3670 cp_lexer
*lexer
= parser
->lexer
;
3671 parser
->lexer
= lexer
->next
;
3672 cp_lexer_destroy (lexer
);
3674 /* Put the current source position back where it was before this
3675 lexer was pushed. */
3676 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3679 /* Lexical conventions [gram.lex] */
3681 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3685 cp_parser_identifier (cp_parser
* parser
)
3689 /* Look for the identifier. */
3690 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3691 /* Return the value. */
3692 return token
? token
->u
.value
: error_mark_node
;
3695 /* Parse a sequence of adjacent string constants. Returns a
3696 TREE_STRING representing the combined, nul-terminated string
3697 constant. If TRANSLATE is true, translate the string to the
3698 execution character set. If WIDE_OK is true, a wide string is
3701 C++98 [lex.string] says that if a narrow string literal token is
3702 adjacent to a wide string literal token, the behavior is undefined.
3703 However, C99 6.4.5p4 says that this results in a wide string literal.
3704 We follow C99 here, for consistency with the C front end.
3706 This code is largely lifted from lex_string() in c-lex.c.
3708 FUTURE: ObjC++ will need to handle @-strings here. */
3710 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3711 bool lookup_udlit
= true)
3715 struct obstack str_ob
;
3716 cpp_string str
, istr
, *strs
;
3718 enum cpp_ttype type
, curr_type
;
3719 int have_suffix_p
= 0;
3721 tree suffix_id
= NULL_TREE
;
3722 bool curr_tok_is_userdef_p
= false;
3724 tok
= cp_lexer_peek_token (parser
->lexer
);
3725 if (!cp_parser_is_string_literal (tok
))
3727 cp_parser_error (parser
, "expected string-literal");
3728 return error_mark_node
;
3731 if (cpp_userdef_string_p (tok
->type
))
3733 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3734 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3735 curr_tok_is_userdef_p
= true;
3739 string_tree
= tok
->u
.value
;
3740 curr_type
= tok
->type
;
3744 /* Try to avoid the overhead of creating and destroying an obstack
3745 for the common case of just one string. */
3746 if (!cp_parser_is_string_literal
3747 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3749 cp_lexer_consume_token (parser
->lexer
);
3751 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3752 str
.len
= TREE_STRING_LENGTH (string_tree
);
3755 if (curr_tok_is_userdef_p
)
3757 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3759 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3762 curr_type
= tok
->type
;
3768 gcc_obstack_init (&str_ob
);
3773 cp_lexer_consume_token (parser
->lexer
);
3775 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3776 str
.len
= TREE_STRING_LENGTH (string_tree
);
3778 if (curr_tok_is_userdef_p
)
3780 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3781 if (have_suffix_p
== 0)
3783 suffix_id
= curr_suffix_id
;
3786 else if (have_suffix_p
== 1
3787 && curr_suffix_id
!= suffix_id
)
3789 error ("inconsistent user-defined literal suffixes"
3790 " %qD and %qD in string literal",
3791 suffix_id
, curr_suffix_id
);
3794 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3797 curr_type
= tok
->type
;
3799 if (type
!= curr_type
)
3801 if (type
== CPP_STRING
)
3803 else if (curr_type
!= CPP_STRING
)
3804 error_at (tok
->location
,
3805 "unsupported non-standard concatenation "
3806 "of string literals");
3809 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3811 tok
= cp_lexer_peek_token (parser
->lexer
);
3812 if (cpp_userdef_string_p (tok
->type
))
3814 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3815 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3816 curr_tok_is_userdef_p
= true;
3820 string_tree
= tok
->u
.value
;
3821 curr_type
= tok
->type
;
3822 curr_tok_is_userdef_p
= false;
3825 while (cp_parser_is_string_literal (tok
));
3827 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3830 if (type
!= CPP_STRING
&& !wide_ok
)
3832 cp_parser_error (parser
, "a wide string is invalid in this context");
3836 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3837 (parse_in
, strs
, count
, &istr
, type
))
3839 value
= build_string (istr
.len
, (const char *)istr
.text
);
3840 free (CONST_CAST (unsigned char *, istr
.text
));
3846 case CPP_UTF8STRING
:
3847 TREE_TYPE (value
) = char_array_type_node
;
3850 TREE_TYPE (value
) = char16_array_type_node
;
3853 TREE_TYPE (value
) = char32_array_type_node
;
3856 TREE_TYPE (value
) = wchar_array_type_node
;
3860 value
= fix_string_type (value
);
3864 tree literal
= build_userdef_literal (suffix_id
, value
,
3865 OT_NONE
, NULL_TREE
);
3867 value
= cp_parser_userdef_string_literal (literal
);
3873 /* cpp_interpret_string has issued an error. */
3874 value
= error_mark_node
;
3877 obstack_free (&str_ob
, 0);
3882 /* Look up a literal operator with the name and the exact arguments. */
3885 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3888 decl
= lookup_name (name
);
3889 if (!decl
|| !is_overloaded_fn (decl
))
3890 return error_mark_node
;
3892 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3896 tree fn
= OVL_CURRENT (fns
);
3897 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3898 if (parmtypes
!= NULL_TREE
)
3900 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3901 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3903 tree tparm
= TREE_VALUE (parmtypes
);
3904 tree targ
= TREE_TYPE ((*args
)[ix
]);
3905 bool ptr
= TYPE_PTR_P (tparm
);
3906 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3907 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3909 || !same_type_p (TREE_TYPE (tparm
),
3914 && ix
== vec_safe_length (args
)
3915 /* May be this should be sufficient_parms_p instead,
3916 depending on how exactly should user-defined literals
3917 work in presence of default arguments on the literal
3918 operator parameters. */
3919 && parmtypes
== void_list_node
)
3924 return error_mark_node
;
3927 /* Parse a user-defined char constant. Returns a call to a user-defined
3928 literal operator taking the character as an argument. */
3931 cp_parser_userdef_char_literal (cp_parser
*parser
)
3933 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3934 tree literal
= token
->u
.value
;
3935 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3936 tree value
= USERDEF_LITERAL_VALUE (literal
);
3937 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3940 /* Build up a call to the user-defined operator */
3941 /* Lookup the name we got back from the id-expression. */
3942 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3943 vec_safe_push (args
, value
);
3944 decl
= lookup_literal_operator (name
, args
);
3945 if (!decl
|| decl
== error_mark_node
)
3947 error ("unable to find character literal operator %qD with %qT argument",
3948 name
, TREE_TYPE (value
));
3949 release_tree_vector (args
);
3950 return error_mark_node
;
3952 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3953 release_tree_vector (args
);
3957 /* A subroutine of cp_parser_userdef_numeric_literal to
3958 create a char... template parameter pack from a string node. */
3961 make_char_string_pack (tree value
)
3964 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3965 const char *str
= TREE_STRING_POINTER (value
);
3966 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3967 tree argvec
= make_tree_vec (1);
3969 /* Fill in CHARVEC with all of the parameters. */
3970 charvec
= make_tree_vec (len
);
3971 for (i
= 0; i
< len
; ++i
)
3972 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3974 /* Build the argument packs. */
3975 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3976 TREE_TYPE (argpack
) = char_type_node
;
3978 TREE_VEC_ELT (argvec
, 0) = argpack
;
3983 /* A subroutine of cp_parser_userdef_numeric_literal to
3984 create a char... template parameter pack from a string node. */
3987 make_string_pack (tree value
)
3990 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3991 const unsigned char *str
3992 = (const unsigned char *) TREE_STRING_POINTER (value
);
3993 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3994 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3995 tree argvec
= make_tree_vec (2);
3997 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3998 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4000 /* First template parm is character type. */
4001 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4003 /* Fill in CHARVEC with all of the parameters. */
4004 charvec
= make_tree_vec (len
);
4005 for (int i
= 0; i
< len
; ++i
)
4006 TREE_VEC_ELT (charvec
, i
)
4007 = double_int_to_tree (str_char_type_node
,
4008 double_int::from_buffer (str
+ i
* sz
, sz
));
4010 /* Build the argument packs. */
4011 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4012 TREE_TYPE (argpack
) = str_char_type_node
;
4014 TREE_VEC_ELT (argvec
, 1) = argpack
;
4019 /* Parse a user-defined numeric constant. returns a call to a user-defined
4020 literal operator. */
4023 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4025 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4026 tree literal
= token
->u
.value
;
4027 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4028 tree value
= USERDEF_LITERAL_VALUE (literal
);
4029 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4030 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4031 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4033 vec
<tree
, va_gc
> *args
;
4035 /* Look for a literal operator taking the exact type of numeric argument
4036 as the literal value. */
4037 args
= make_tree_vector ();
4038 vec_safe_push (args
, value
);
4039 decl
= lookup_literal_operator (name
, args
);
4040 if (decl
&& decl
!= error_mark_node
)
4042 result
= finish_call_expr (decl
, &args
, false, true,
4043 tf_warning_or_error
);
4045 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4047 warning_at (token
->location
, OPT_Woverflow
,
4048 "integer literal exceeds range of %qT type",
4049 long_long_unsigned_type_node
);
4054 warning_at (token
->location
, OPT_Woverflow
,
4055 "floating literal exceeds range of %qT type",
4056 long_double_type_node
);
4057 else if (overflow
< 0)
4058 warning_at (token
->location
, OPT_Woverflow
,
4059 "floating literal truncated to zero");
4062 release_tree_vector (args
);
4065 release_tree_vector (args
);
4067 /* If the numeric argument didn't work, look for a raw literal
4068 operator taking a const char* argument consisting of the number
4069 in string format. */
4070 args
= make_tree_vector ();
4071 vec_safe_push (args
, num_string
);
4072 decl
= lookup_literal_operator (name
, args
);
4073 if (decl
&& decl
!= error_mark_node
)
4075 result
= finish_call_expr (decl
, &args
, false, true,
4076 tf_warning_or_error
);
4077 release_tree_vector (args
);
4080 release_tree_vector (args
);
4082 /* If the raw literal didn't work, look for a non-type template
4083 function with parameter pack char.... Call the function with
4084 template parameter characters representing the number. */
4085 args
= make_tree_vector ();
4086 decl
= lookup_literal_operator (name
, args
);
4087 if (decl
&& decl
!= error_mark_node
)
4089 tree tmpl_args
= make_char_string_pack (num_string
);
4090 decl
= lookup_template_function (decl
, tmpl_args
);
4091 result
= finish_call_expr (decl
, &args
, false, true,
4092 tf_warning_or_error
);
4093 release_tree_vector (args
);
4097 release_tree_vector (args
);
4099 error ("unable to find numeric literal operator %qD", name
);
4100 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4101 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4102 "to enable more built-in suffixes");
4103 return error_mark_node
;
4106 /* Parse a user-defined string constant. Returns a call to a user-defined
4107 literal operator taking a character pointer and the length of the string
4111 cp_parser_userdef_string_literal (tree literal
)
4113 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4114 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4115 tree value
= USERDEF_LITERAL_VALUE (literal
);
4116 int len
= TREE_STRING_LENGTH (value
)
4117 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4119 vec
<tree
, va_gc
> *args
;
4121 /* Build up a call to the user-defined operator. */
4122 /* Lookup the name we got back from the id-expression. */
4123 args
= make_tree_vector ();
4124 vec_safe_push (args
, value
);
4125 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4126 decl
= lookup_literal_operator (name
, args
);
4128 if (decl
&& decl
!= error_mark_node
)
4130 result
= finish_call_expr (decl
, &args
, false, true,
4131 tf_warning_or_error
);
4132 release_tree_vector (args
);
4135 release_tree_vector (args
);
4137 /* Look for a template function with typename parameter CharT
4138 and parameter pack CharT... Call the function with
4139 template parameter characters representing the string. */
4140 args
= make_tree_vector ();
4141 decl
= lookup_literal_operator (name
, args
);
4142 if (decl
&& decl
!= error_mark_node
)
4144 tree tmpl_args
= make_string_pack (value
);
4145 decl
= lookup_template_function (decl
, tmpl_args
);
4146 result
= finish_call_expr (decl
, &args
, false, true,
4147 tf_warning_or_error
);
4148 release_tree_vector (args
);
4151 release_tree_vector (args
);
4153 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4154 name
, TREE_TYPE (value
), size_type_node
);
4155 return error_mark_node
;
4159 /* Basic concepts [gram.basic] */
4161 /* Parse a translation-unit.
4164 declaration-seq [opt]
4166 Returns TRUE if all went well. */
4169 cp_parser_translation_unit (cp_parser
* parser
)
4171 /* The address of the first non-permanent object on the declarator
4173 static void *declarator_obstack_base
;
4177 /* Create the declarator obstack, if necessary. */
4178 if (!cp_error_declarator
)
4180 gcc_obstack_init (&declarator_obstack
);
4181 /* Create the error declarator. */
4182 cp_error_declarator
= make_declarator (cdk_error
);
4183 /* Create the empty parameter list. */
4184 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4185 /* Remember where the base of the declarator obstack lies. */
4186 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4189 cp_parser_declaration_seq_opt (parser
);
4191 /* If there are no tokens left then all went well. */
4192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4194 /* Get rid of the token array; we don't need it any more. */
4195 cp_lexer_destroy (parser
->lexer
);
4196 parser
->lexer
= NULL
;
4198 /* This file might have been a context that's implicitly extern
4199 "C". If so, pop the lang context. (Only relevant for PCH.) */
4200 if (parser
->implicit_extern_c
)
4202 pop_lang_context ();
4203 parser
->implicit_extern_c
= false;
4207 finish_translation_unit ();
4213 cp_parser_error (parser
, "expected declaration");
4217 /* Make sure the declarator obstack was fully cleaned up. */
4218 gcc_assert (obstack_next_free (&declarator_obstack
)
4219 == declarator_obstack_base
);
4221 /* All went well. */
4225 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4226 decltype context. */
4228 static inline tsubst_flags_t
4229 complain_flags (bool decltype_p
)
4231 tsubst_flags_t complain
= tf_warning_or_error
;
4233 complain
|= tf_decltype
;
4237 /* We're about to parse a collection of statements. If we're currently
4238 parsing tentatively, set up a firewall so that any nested
4239 cp_parser_commit_to_tentative_parse won't affect the current context. */
4241 static cp_token_position
4242 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4244 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4247 cp_parser_parse_tentatively (parser
);
4248 cp_parser_commit_to_topmost_tentative_parse (parser
);
4249 return cp_lexer_token_position (parser
->lexer
, false);
4252 /* We've finished parsing the collection of statements. Wrap up the
4253 firewall and replace the relevant tokens with the parsed form. */
4256 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4262 /* Finish the firewall level. */
4263 cp_parser_parse_definitely (parser
);
4264 /* And remember the result of the parse for when we try again. */
4265 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4266 token
->type
= CPP_PREPARSED_EXPR
;
4267 token
->u
.value
= expr
;
4268 token
->keyword
= RID_MAX
;
4269 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4272 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4273 enclosing parentheses. */
4276 cp_parser_statement_expr (cp_parser
*parser
)
4278 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4280 /* Consume the '('. */
4281 cp_lexer_consume_token (parser
->lexer
);
4282 /* Start the statement-expression. */
4283 tree expr
= begin_stmt_expr ();
4284 /* Parse the compound-statement. */
4285 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4287 expr
= finish_stmt_expr (expr
, false);
4288 /* Consume the ')'. */
4289 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4290 cp_parser_skip_to_end_of_statement (parser
);
4292 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4296 /* Expressions [gram.expr] */
4298 /* Parse a fold-operator.
4301 - * / % ^ & | = < > << >>
4302 = -= *= /= %= ^= &= |= <<= >>=
4303 == != <= >= && || , .* ->*
4305 This returns the tree code corresponding to the matched operator
4306 as an int. When the current token matches a compound assignment
4307 opertor, the resulting tree code is the negative value of the
4308 non-assignment operator. */
4311 cp_parser_fold_operator (cp_token
*token
)
4313 switch (token
->type
)
4315 case CPP_PLUS
: return PLUS_EXPR
;
4316 case CPP_MINUS
: return MINUS_EXPR
;
4317 case CPP_MULT
: return MULT_EXPR
;
4318 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4319 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4320 case CPP_XOR
: return BIT_XOR_EXPR
;
4321 case CPP_AND
: return BIT_AND_EXPR
;
4322 case CPP_OR
: return BIT_IOR_EXPR
;
4323 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4324 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4326 case CPP_EQ
: return -NOP_EXPR
;
4327 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4328 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4329 case CPP_MULT_EQ
: return -MULT_EXPR
;
4330 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4331 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4332 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4333 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4334 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4335 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4336 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4338 case CPP_EQ_EQ
: return EQ_EXPR
;
4339 case CPP_NOT_EQ
: return NE_EXPR
;
4340 case CPP_LESS
: return LT_EXPR
;
4341 case CPP_GREATER
: return GT_EXPR
;
4342 case CPP_LESS_EQ
: return LE_EXPR
;
4343 case CPP_GREATER_EQ
: return GE_EXPR
;
4345 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4346 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4348 case CPP_COMMA
: return COMPOUND_EXPR
;
4350 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4351 case CPP_DEREF_STAR
: return MEMBER_REF
;
4353 default: return ERROR_MARK
;
4357 /* Returns true if CODE indicates a binary expression, which is not allowed in
4358 the LHS of a fold-expression. More codes will need to be added to use this
4359 function in other contexts. */
4362 is_binary_op (tree_code code
)
4367 case POINTER_PLUS_EXPR
:
4370 case TRUNC_DIV_EXPR
:
4371 case TRUNC_MOD_EXPR
:
4387 case TRUTH_ANDIF_EXPR
:
4388 case TRUTH_ORIF_EXPR
:
4401 /* If the next token is a suitable fold operator, consume it and return as
4402 the function above. */
4405 cp_parser_fold_operator (cp_parser
*parser
)
4407 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4408 int code
= cp_parser_fold_operator (token
);
4409 if (code
!= ERROR_MARK
)
4410 cp_lexer_consume_token (parser
->lexer
);
4414 /* Parse a fold-expression.
4417 ( ... folding-operator cast-expression)
4418 ( cast-expression folding-operator ... )
4419 ( cast-expression folding operator ... folding-operator cast-expression)
4421 Note that the '(' and ')' are matched in primary expression. */
4424 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
4429 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4431 cp_lexer_consume_token (parser
->lexer
);
4432 int op
= cp_parser_fold_operator (parser
);
4433 if (op
== ERROR_MARK
)
4435 cp_parser_error (parser
, "expected binary operator");
4436 return error_mark_node
;
4439 tree expr
= cp_parser_cast_expression (parser
, false, false,
4441 if (expr
== error_mark_node
)
4442 return error_mark_node
;
4443 return finish_left_unary_fold_expr (expr
, op
);
4446 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
4447 int op
= cp_parser_fold_operator (parser
);
4448 if (op
== ERROR_MARK
)
4450 cp_parser_error (parser
, "expected binary operator");
4451 return error_mark_node
;
4454 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
4456 cp_parser_error (parser
, "expected ...");
4457 return error_mark_node
;
4459 cp_lexer_consume_token (parser
->lexer
);
4461 /* The operands of a fold-expression are cast-expressions, so binary or
4462 conditional expressions are not allowed. We check this here to avoid
4463 tentative parsing. */
4464 if (is_binary_op (TREE_CODE (expr1
)))
4465 error_at (location_of (expr1
),
4466 "binary expression in operand of fold-expression");
4467 else if (TREE_CODE (expr1
) == COND_EXPR
)
4468 error_at (location_of (expr1
),
4469 "conditional expression in operand of fold-expression");
4472 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4473 return finish_right_unary_fold_expr (expr1
, op
);
4475 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
4477 cp_parser_error (parser
, "mismatched operator in fold-expression");
4478 return error_mark_node
;
4480 cp_lexer_consume_token (parser
->lexer
);
4482 // Binary left or right fold.
4483 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
4484 if (expr2
== error_mark_node
)
4485 return error_mark_node
;
4486 return finish_binary_fold_expr (expr1
, expr2
, op
);
4489 /* Parse a primary-expression.
4496 lambda-expression (C++11)
4501 ( compound-statement )
4502 __builtin_va_arg ( assignment-expression , type-id )
4503 __builtin_offsetof ( type-id , offsetof-expression )
4506 __has_nothrow_assign ( type-id )
4507 __has_nothrow_constructor ( type-id )
4508 __has_nothrow_copy ( type-id )
4509 __has_trivial_assign ( type-id )
4510 __has_trivial_constructor ( type-id )
4511 __has_trivial_copy ( type-id )
4512 __has_trivial_destructor ( type-id )
4513 __has_virtual_destructor ( type-id )
4514 __is_abstract ( type-id )
4515 __is_base_of ( type-id , type-id )
4516 __is_class ( type-id )
4517 __is_empty ( type-id )
4518 __is_enum ( type-id )
4519 __is_final ( type-id )
4520 __is_literal_type ( type-id )
4521 __is_pod ( type-id )
4522 __is_polymorphic ( type-id )
4523 __is_std_layout ( type-id )
4524 __is_trivial ( type-id )
4525 __is_union ( type-id )
4527 Objective-C++ Extension:
4535 ADDRESS_P is true iff this expression was immediately preceded by
4536 "&" and therefore might denote a pointer-to-member. CAST_P is true
4537 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4538 true iff this expression is a template argument.
4540 Returns a representation of the expression. Upon return, *IDK
4541 indicates what kind of id-expression (if any) was present. */
4544 cp_parser_primary_expression (cp_parser
*parser
,
4547 bool template_arg_p
,
4551 cp_token
*token
= NULL
;
4553 /* Assume the primary expression is not an id-expression. */
4554 *idk
= CP_ID_KIND_NONE
;
4556 /* Peek at the next token. */
4557 token
= cp_lexer_peek_token (parser
->lexer
);
4558 switch ((int) token
->type
)
4567 user-defined-literal */
4574 case CPP_PREPARSED_EXPR
:
4575 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4576 return cp_parser_userdef_numeric_literal (parser
);
4577 token
= cp_lexer_consume_token (parser
->lexer
);
4578 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4580 error_at (token
->location
,
4581 "fixed-point types not supported in C++");
4582 return error_mark_node
;
4584 /* Floating-point literals are only allowed in an integral
4585 constant expression if they are cast to an integral or
4586 enumeration type. */
4587 if (TREE_CODE (token
->u
.value
) == REAL_CST
4588 && parser
->integral_constant_expression_p
4591 /* CAST_P will be set even in invalid code like "int(2.7 +
4592 ...)". Therefore, we have to check that the next token
4593 is sure to end the cast. */
4596 cp_token
*next_token
;
4598 next_token
= cp_lexer_peek_token (parser
->lexer
);
4599 if (/* The comma at the end of an
4600 enumerator-definition. */
4601 next_token
->type
!= CPP_COMMA
4602 /* The curly brace at the end of an enum-specifier. */
4603 && next_token
->type
!= CPP_CLOSE_BRACE
4604 /* The end of a statement. */
4605 && next_token
->type
!= CPP_SEMICOLON
4606 /* The end of the cast-expression. */
4607 && next_token
->type
!= CPP_CLOSE_PAREN
4608 /* The end of an array bound. */
4609 && next_token
->type
!= CPP_CLOSE_SQUARE
4610 /* The closing ">" in a template-argument-list. */
4611 && (next_token
->type
!= CPP_GREATER
4612 || parser
->greater_than_is_operator_p
)
4613 /* C++0x only: A ">>" treated like two ">" tokens,
4614 in a template-argument-list. */
4615 && (next_token
->type
!= CPP_RSHIFT
4616 || (cxx_dialect
== cxx98
)
4617 || parser
->greater_than_is_operator_p
))
4621 /* If we are within a cast, then the constraint that the
4622 cast is to an integral or enumeration type will be
4623 checked at that point. If we are not within a cast, then
4624 this code is invalid. */
4626 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4628 return token
->u
.value
;
4630 case CPP_CHAR_USERDEF
:
4631 case CPP_CHAR16_USERDEF
:
4632 case CPP_CHAR32_USERDEF
:
4633 case CPP_WCHAR_USERDEF
:
4634 case CPP_UTF8CHAR_USERDEF
:
4635 return cp_parser_userdef_char_literal (parser
);
4641 case CPP_UTF8STRING
:
4642 case CPP_STRING_USERDEF
:
4643 case CPP_STRING16_USERDEF
:
4644 case CPP_STRING32_USERDEF
:
4645 case CPP_WSTRING_USERDEF
:
4646 case CPP_UTF8STRING_USERDEF
:
4647 /* ??? Should wide strings be allowed when parser->translate_strings_p
4648 is false (i.e. in attributes)? If not, we can kill the third
4649 argument to cp_parser_string_literal. */
4650 return cp_parser_string_literal (parser
,
4651 parser
->translate_strings_p
,
4654 case CPP_OPEN_PAREN
:
4655 /* If we see `( { ' then we are looking at the beginning of
4656 a GNU statement-expression. */
4657 if (cp_parser_allow_gnu_extensions_p (parser
)
4658 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4660 /* Statement-expressions are not allowed by the standard. */
4661 pedwarn (token
->location
, OPT_Wpedantic
,
4662 "ISO C++ forbids braced-groups within expressions");
4664 /* And they're not allowed outside of a function-body; you
4665 cannot, for example, write:
4667 int i = ({ int j = 3; j + 1; });
4669 at class or namespace scope. */
4670 if (!parser
->in_function_body
4671 || parser
->in_template_argument_list_p
)
4673 error_at (token
->location
,
4674 "statement-expressions are not allowed outside "
4675 "functions nor in template-argument lists");
4676 cp_parser_skip_to_end_of_block_or_statement (parser
);
4677 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4678 cp_lexer_consume_token (parser
->lexer
);
4679 return error_mark_node
;
4682 return cp_parser_statement_expr (parser
);
4684 /* Otherwise it's a normal parenthesized expression. */
4687 bool saved_greater_than_is_operator_p
;
4689 /* Consume the `('. */
4690 cp_lexer_consume_token (parser
->lexer
);
4691 /* Within a parenthesized expression, a `>' token is always
4692 the greater-than operator. */
4693 saved_greater_than_is_operator_p
4694 = parser
->greater_than_is_operator_p
;
4695 parser
->greater_than_is_operator_p
= true;
4697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
4698 /* Left fold expression. */
4701 /* Parse the parenthesized expression. */
4702 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4704 token
= cp_lexer_peek_token (parser
->lexer
);
4705 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
4707 expr
= cp_parser_fold_expression (parser
, expr
);
4708 if (expr
!= error_mark_node
4709 && cxx_dialect
< cxx1z
4710 && !in_system_header_at (input_location
))
4711 pedwarn (input_location
, 0, "fold-expressions only available "
4712 "with -std=c++1z or -std=gnu++1z");
4715 /* Let the front end know that this expression was
4716 enclosed in parentheses. This matters in case, for
4717 example, the expression is of the form `A::B', since
4718 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4720 expr
= finish_parenthesized_expr (expr
);
4722 /* DR 705: Wrapping an unqualified name in parentheses
4723 suppresses arg-dependent lookup. We want to pass back
4724 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4725 (c++/37862), but none of the others. */
4726 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4727 *idk
= CP_ID_KIND_NONE
;
4729 /* The `>' token might be the end of a template-id or
4730 template-parameter-list now. */
4731 parser
->greater_than_is_operator_p
4732 = saved_greater_than_is_operator_p
;
4733 /* Consume the `)'. */
4734 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
)
4735 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
4736 cp_parser_skip_to_end_of_statement (parser
);
4741 case CPP_OPEN_SQUARE
:
4743 if (c_dialect_objc ())
4745 /* We might have an Objective-C++ message. */
4746 cp_parser_parse_tentatively (parser
);
4747 tree msg
= cp_parser_objc_message_expression (parser
);
4748 /* If that works out, we're done ... */
4749 if (cp_parser_parse_definitely (parser
))
4751 /* ... else, fall though to see if it's a lambda. */
4753 tree lam
= cp_parser_lambda_expression (parser
);
4754 /* Don't warn about a failed tentative parse. */
4755 if (cp_parser_error_occurred (parser
))
4756 return error_mark_node
;
4757 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4761 case CPP_OBJC_STRING
:
4762 if (c_dialect_objc ())
4763 /* We have an Objective-C++ string literal. */
4764 return cp_parser_objc_expression (parser
);
4765 cp_parser_error (parser
, "expected primary-expression");
4766 return error_mark_node
;
4769 switch (token
->keyword
)
4771 /* These two are the boolean literals. */
4773 cp_lexer_consume_token (parser
->lexer
);
4774 return boolean_true_node
;
4776 cp_lexer_consume_token (parser
->lexer
);
4777 return boolean_false_node
;
4779 /* The `__null' literal. */
4781 cp_lexer_consume_token (parser
->lexer
);
4784 /* The `nullptr' literal. */
4786 cp_lexer_consume_token (parser
->lexer
);
4787 return nullptr_node
;
4789 /* Recognize the `this' keyword. */
4791 cp_lexer_consume_token (parser
->lexer
);
4792 if (parser
->local_variables_forbidden_p
)
4794 error_at (token
->location
,
4795 "%<this%> may not be used in this context");
4796 return error_mark_node
;
4798 /* Pointers cannot appear in constant-expressions. */
4799 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4800 return error_mark_node
;
4801 return finish_this_expr ();
4803 /* The `operator' keyword can be the beginning of an
4808 case RID_FUNCTION_NAME
:
4809 case RID_PRETTY_FUNCTION_NAME
:
4810 case RID_C99_FUNCTION_NAME
:
4812 non_integral_constant name
;
4814 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4815 __func__ are the names of variables -- but they are
4816 treated specially. Therefore, they are handled here,
4817 rather than relying on the generic id-expression logic
4818 below. Grammatically, these names are id-expressions.
4820 Consume the token. */
4821 token
= cp_lexer_consume_token (parser
->lexer
);
4823 switch (token
->keyword
)
4825 case RID_FUNCTION_NAME
:
4826 name
= NIC_FUNC_NAME
;
4828 case RID_PRETTY_FUNCTION_NAME
:
4829 name
= NIC_PRETTY_FUNC
;
4831 case RID_C99_FUNCTION_NAME
:
4832 name
= NIC_C99_FUNC
;
4838 if (cp_parser_non_integral_constant_expression (parser
, name
))
4839 return error_mark_node
;
4841 /* Look up the name. */
4842 return finish_fname (token
->u
.value
);
4849 source_location type_location
;
4851 /* The `__builtin_va_arg' construct is used to handle
4852 `va_arg'. Consume the `__builtin_va_arg' token. */
4853 cp_lexer_consume_token (parser
->lexer
);
4854 /* Look for the opening `('. */
4855 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4856 /* Now, parse the assignment-expression. */
4857 expression
= cp_parser_assignment_expression (parser
);
4858 /* Look for the `,'. */
4859 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4860 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4861 /* Parse the type-id. */
4862 type
= cp_parser_type_id (parser
);
4863 /* Look for the closing `)'. */
4864 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4865 /* Using `va_arg' in a constant-expression is not
4867 if (cp_parser_non_integral_constant_expression (parser
,
4869 return error_mark_node
;
4870 return build_x_va_arg (type_location
, expression
, type
);
4874 return cp_parser_builtin_offsetof (parser
);
4876 case RID_HAS_NOTHROW_ASSIGN
:
4877 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4878 case RID_HAS_NOTHROW_COPY
:
4879 case RID_HAS_TRIVIAL_ASSIGN
:
4880 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4881 case RID_HAS_TRIVIAL_COPY
:
4882 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4883 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4884 case RID_IS_ABSTRACT
:
4885 case RID_IS_BASE_OF
:
4890 case RID_IS_LITERAL_TYPE
:
4892 case RID_IS_POLYMORPHIC
:
4893 case RID_IS_SAME_AS
:
4894 case RID_IS_STD_LAYOUT
:
4895 case RID_IS_TRIVIAL
:
4896 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4897 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4898 case RID_IS_TRIVIALLY_COPYABLE
:
4900 return cp_parser_trait_expr (parser
, token
->keyword
);
4904 return cp_parser_requires_expression (parser
);
4906 /* Objective-C++ expressions. */
4908 case RID_AT_PROTOCOL
:
4909 case RID_AT_SELECTOR
:
4910 return cp_parser_objc_expression (parser
);
4913 if (parser
->in_function_body
4914 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4917 error_at (token
->location
,
4918 "a template declaration cannot appear at block scope");
4919 cp_parser_skip_to_end_of_block_or_statement (parser
);
4920 return error_mark_node
;
4923 cp_parser_error (parser
, "expected primary-expression");
4924 return error_mark_node
;
4927 /* An id-expression can start with either an identifier, a
4928 `::' as the beginning of a qualified-id, or the "operator"
4932 case CPP_TEMPLATE_ID
:
4933 case CPP_NESTED_NAME_SPECIFIER
:
4937 const char *error_msg
;
4940 cp_token
*id_expr_token
;
4943 /* Parse the id-expression. */
4945 = cp_parser_id_expression (parser
,
4946 /*template_keyword_p=*/false,
4947 /*check_dependency_p=*/true,
4949 /*declarator_p=*/false,
4950 /*optional_p=*/false);
4951 if (id_expression
== error_mark_node
)
4952 return error_mark_node
;
4953 id_expr_token
= token
;
4954 token
= cp_lexer_peek_token (parser
->lexer
);
4955 done
= (token
->type
!= CPP_OPEN_SQUARE
4956 && token
->type
!= CPP_OPEN_PAREN
4957 && token
->type
!= CPP_DOT
4958 && token
->type
!= CPP_DEREF
4959 && token
->type
!= CPP_PLUS_PLUS
4960 && token
->type
!= CPP_MINUS_MINUS
);
4961 /* If we have a template-id, then no further lookup is
4962 required. If the template-id was for a template-class, we
4963 will sometimes have a TYPE_DECL at this point. */
4964 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4965 || TREE_CODE (id_expression
) == TYPE_DECL
)
4966 decl
= id_expression
;
4967 /* Look up the name. */
4970 tree ambiguous_decls
;
4972 /* If we already know that this lookup is ambiguous, then
4973 we've already issued an error message; there's no reason
4975 if (id_expr_token
->type
== CPP_NAME
4976 && id_expr_token
->error_reported
)
4978 cp_parser_simulate_error (parser
);
4979 return error_mark_node
;
4982 decl
= cp_parser_lookup_name (parser
, id_expression
,
4985 /*is_namespace=*/false,
4986 /*check_dependency=*/true,
4988 id_expr_token
->location
);
4989 /* If the lookup was ambiguous, an error will already have
4991 if (ambiguous_decls
)
4992 return error_mark_node
;
4994 /* In Objective-C++, we may have an Objective-C 2.0
4995 dot-syntax for classes here. */
4996 if (c_dialect_objc ()
4997 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4998 && TREE_CODE (decl
) == TYPE_DECL
4999 && objc_is_class_name (decl
))
5002 cp_lexer_consume_token (parser
->lexer
);
5003 component
= cp_parser_identifier (parser
);
5004 if (component
== error_mark_node
)
5005 return error_mark_node
;
5007 return objc_build_class_component_ref (id_expression
, component
);
5010 /* In Objective-C++, an instance variable (ivar) may be preferred
5011 to whatever cp_parser_lookup_name() found. */
5012 decl
= objc_lookup_ivar (decl
, id_expression
);
5014 /* If name lookup gives us a SCOPE_REF, then the
5015 qualifying scope was dependent. */
5016 if (TREE_CODE (decl
) == SCOPE_REF
)
5018 /* At this point, we do not know if DECL is a valid
5019 integral constant expression. We assume that it is
5020 in fact such an expression, so that code like:
5022 template <int N> struct A {
5026 is accepted. At template-instantiation time, we
5027 will check that B<N>::i is actually a constant. */
5030 /* Check to see if DECL is a local variable in a context
5031 where that is forbidden. */
5032 if (parser
->local_variables_forbidden_p
5033 && local_variable_p (decl
))
5035 /* It might be that we only found DECL because we are
5036 trying to be generous with pre-ISO scoping rules.
5037 For example, consider:
5041 for (int i = 0; i < 10; ++i) {}
5042 extern void f(int j = i);
5045 Here, name look up will originally find the out
5046 of scope `i'. We need to issue a warning message,
5047 but then use the global `i'. */
5048 decl
= check_for_out_of_scope_variable (decl
);
5049 if (local_variable_p (decl
))
5051 error_at (id_expr_token
->location
,
5052 "local variable %qD may not appear in this context",
5054 return error_mark_node
;
5059 decl
= (finish_id_expression
5060 (id_expression
, decl
, parser
->scope
,
5062 parser
->integral_constant_expression_p
,
5063 parser
->allow_non_integral_constant_expression_p
,
5064 &parser
->non_integral_constant_expression_p
,
5065 template_p
, done
, address_p
,
5068 id_expr_token
->location
));
5070 cp_parser_error (parser
, error_msg
);
5074 /* Anything else is an error. */
5076 cp_parser_error (parser
, "expected primary-expression");
5077 return error_mark_node
;
5082 cp_parser_primary_expression (cp_parser
*parser
,
5085 bool template_arg_p
,
5088 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5089 /*decltype*/false, idk
);
5092 /* Parse an id-expression.
5099 :: [opt] nested-name-specifier template [opt] unqualified-id
5101 :: operator-function-id
5104 Return a representation of the unqualified portion of the
5105 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5106 a `::' or nested-name-specifier.
5108 Often, if the id-expression was a qualified-id, the caller will
5109 want to make a SCOPE_REF to represent the qualified-id. This
5110 function does not do this in order to avoid wastefully creating
5111 SCOPE_REFs when they are not required.
5113 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5116 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5117 uninstantiated templates.
5119 If *TEMPLATE_P is non-NULL, it is set to true iff the
5120 `template' keyword is used to explicitly indicate that the entity
5121 named is a template.
5123 If DECLARATOR_P is true, the id-expression is appearing as part of
5124 a declarator, rather than as part of an expression. */
5127 cp_parser_id_expression (cp_parser
*parser
,
5128 bool template_keyword_p
,
5129 bool check_dependency_p
,
5134 bool global_scope_p
;
5135 bool nested_name_specifier_p
;
5137 /* Assume the `template' keyword was not used. */
5139 *template_p
= template_keyword_p
;
5141 /* Look for the optional `::' operator. */
5143 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
5145 /* Look for the optional nested-name-specifier. */
5146 nested_name_specifier_p
5147 = (cp_parser_nested_name_specifier_opt (parser
,
5148 /*typename_keyword_p=*/false,
5153 /* If there is a nested-name-specifier, then we are looking at
5154 the first qualified-id production. */
5155 if (nested_name_specifier_p
)
5158 tree saved_object_scope
;
5159 tree saved_qualifying_scope
;
5160 tree unqualified_id
;
5163 /* See if the next token is the `template' keyword. */
5165 template_p
= &is_template
;
5166 *template_p
= cp_parser_optional_template_keyword (parser
);
5167 /* Name lookup we do during the processing of the
5168 unqualified-id might obliterate SCOPE. */
5169 saved_scope
= parser
->scope
;
5170 saved_object_scope
= parser
->object_scope
;
5171 saved_qualifying_scope
= parser
->qualifying_scope
;
5172 /* Process the final unqualified-id. */
5173 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5176 /*optional_p=*/false);
5177 /* Restore the SAVED_SCOPE for our caller. */
5178 parser
->scope
= saved_scope
;
5179 parser
->object_scope
= saved_object_scope
;
5180 parser
->qualifying_scope
= saved_qualifying_scope
;
5182 return unqualified_id
;
5184 /* Otherwise, if we are in global scope, then we are looking at one
5185 of the other qualified-id productions. */
5186 else if (global_scope_p
)
5191 /* Peek at the next token. */
5192 token
= cp_lexer_peek_token (parser
->lexer
);
5194 /* If it's an identifier, and the next token is not a "<", then
5195 we can avoid the template-id case. This is an optimization
5196 for this common case. */
5197 if (token
->type
== CPP_NAME
5198 && !cp_parser_nth_token_starts_template_argument_list_p
5200 return cp_parser_identifier (parser
);
5202 cp_parser_parse_tentatively (parser
);
5203 /* Try a template-id. */
5204 id
= cp_parser_template_id (parser
,
5205 /*template_keyword_p=*/false,
5206 /*check_dependency_p=*/true,
5209 /* If that worked, we're done. */
5210 if (cp_parser_parse_definitely (parser
))
5213 /* Peek at the next token. (Changes in the token buffer may
5214 have invalidated the pointer obtained above.) */
5215 token
= cp_lexer_peek_token (parser
->lexer
);
5217 switch (token
->type
)
5220 return cp_parser_identifier (parser
);
5223 if (token
->keyword
== RID_OPERATOR
)
5224 return cp_parser_operator_function_id (parser
);
5228 cp_parser_error (parser
, "expected id-expression");
5229 return error_mark_node
;
5233 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5234 /*check_dependency_p=*/true,
5239 /* Parse an unqualified-id.
5243 operator-function-id
5244 conversion-function-id
5248 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5249 keyword, in a construct like `A::template ...'.
5251 Returns a representation of unqualified-id. For the `identifier'
5252 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5253 production a BIT_NOT_EXPR is returned; the operand of the
5254 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5255 other productions, see the documentation accompanying the
5256 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5257 names are looked up in uninstantiated templates. If DECLARATOR_P
5258 is true, the unqualified-id is appearing as part of a declarator,
5259 rather than as part of an expression. */
5262 cp_parser_unqualified_id (cp_parser
* parser
,
5263 bool template_keyword_p
,
5264 bool check_dependency_p
,
5270 /* Peek at the next token. */
5271 token
= cp_lexer_peek_token (parser
->lexer
);
5273 switch ((int) token
->type
)
5279 /* We don't know yet whether or not this will be a
5281 cp_parser_parse_tentatively (parser
);
5282 /* Try a template-id. */
5283 id
= cp_parser_template_id (parser
, template_keyword_p
,
5287 /* If it worked, we're done. */
5288 if (cp_parser_parse_definitely (parser
))
5290 /* Otherwise, it's an ordinary identifier. */
5291 return cp_parser_identifier (parser
);
5294 case CPP_TEMPLATE_ID
:
5295 return cp_parser_template_id (parser
, template_keyword_p
,
5303 tree qualifying_scope
;
5308 /* Consume the `~' token. */
5309 cp_lexer_consume_token (parser
->lexer
);
5310 /* Parse the class-name. The standard, as written, seems to
5313 template <typename T> struct S { ~S (); };
5314 template <typename T> S<T>::~S() {}
5316 is invalid, since `~' must be followed by a class-name, but
5317 `S<T>' is dependent, and so not known to be a class.
5318 That's not right; we need to look in uninstantiated
5319 templates. A further complication arises from:
5321 template <typename T> void f(T t) {
5325 Here, it is not possible to look up `T' in the scope of `T'
5326 itself. We must look in both the current scope, and the
5327 scope of the containing complete expression.
5329 Yet another issue is:
5338 The standard does not seem to say that the `S' in `~S'
5339 should refer to the type `S' and not the data member
5342 /* DR 244 says that we look up the name after the "~" in the
5343 same scope as we looked up the qualifying name. That idea
5344 isn't fully worked out; it's more complicated than that. */
5345 scope
= parser
->scope
;
5346 object_scope
= parser
->object_scope
;
5347 qualifying_scope
= parser
->qualifying_scope
;
5349 /* Check for invalid scopes. */
5350 if (scope
== error_mark_node
)
5352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5353 cp_lexer_consume_token (parser
->lexer
);
5354 return error_mark_node
;
5356 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5359 error_at (token
->location
,
5360 "scope %qT before %<~%> is not a class-name",
5362 cp_parser_simulate_error (parser
);
5363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5364 cp_lexer_consume_token (parser
->lexer
);
5365 return error_mark_node
;
5367 gcc_assert (!scope
|| TYPE_P (scope
));
5369 /* If the name is of the form "X::~X" it's OK even if X is a
5371 token
= cp_lexer_peek_token (parser
->lexer
);
5373 && token
->type
== CPP_NAME
5374 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5376 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5377 || (CLASS_TYPE_P (scope
)
5378 && constructor_name_p (token
->u
.value
, scope
))))
5380 cp_lexer_consume_token (parser
->lexer
);
5381 return build_nt (BIT_NOT_EXPR
, scope
);
5384 /* ~auto means the destructor of whatever the object is. */
5385 if (cp_parser_is_keyword (token
, RID_AUTO
))
5387 if (cxx_dialect
< cxx14
)
5388 pedwarn (input_location
, 0,
5389 "%<~auto%> only available with "
5390 "-std=c++14 or -std=gnu++14");
5391 cp_lexer_consume_token (parser
->lexer
);
5392 return build_nt (BIT_NOT_EXPR
, make_auto ());
5395 /* If there was an explicit qualification (S::~T), first look
5396 in the scope given by the qualification (i.e., S).
5398 Note: in the calls to cp_parser_class_name below we pass
5399 typename_type so that lookup finds the injected-class-name
5400 rather than the constructor. */
5402 type_decl
= NULL_TREE
;
5405 cp_parser_parse_tentatively (parser
);
5406 type_decl
= cp_parser_class_name (parser
,
5407 /*typename_keyword_p=*/false,
5408 /*template_keyword_p=*/false,
5410 /*check_dependency=*/false,
5411 /*class_head_p=*/false,
5413 if (cp_parser_parse_definitely (parser
))
5416 /* In "N::S::~S", look in "N" as well. */
5417 if (!done
&& scope
&& qualifying_scope
)
5419 cp_parser_parse_tentatively (parser
);
5420 parser
->scope
= qualifying_scope
;
5421 parser
->object_scope
= NULL_TREE
;
5422 parser
->qualifying_scope
= NULL_TREE
;
5424 = cp_parser_class_name (parser
,
5425 /*typename_keyword_p=*/false,
5426 /*template_keyword_p=*/false,
5428 /*check_dependency=*/false,
5429 /*class_head_p=*/false,
5431 if (cp_parser_parse_definitely (parser
))
5434 /* In "p->S::~T", look in the scope given by "*p" as well. */
5435 else if (!done
&& object_scope
)
5437 cp_parser_parse_tentatively (parser
);
5438 parser
->scope
= object_scope
;
5439 parser
->object_scope
= NULL_TREE
;
5440 parser
->qualifying_scope
= NULL_TREE
;
5442 = cp_parser_class_name (parser
,
5443 /*typename_keyword_p=*/false,
5444 /*template_keyword_p=*/false,
5446 /*check_dependency=*/false,
5447 /*class_head_p=*/false,
5449 if (cp_parser_parse_definitely (parser
))
5452 /* Look in the surrounding context. */
5455 parser
->scope
= NULL_TREE
;
5456 parser
->object_scope
= NULL_TREE
;
5457 parser
->qualifying_scope
= NULL_TREE
;
5458 if (processing_template_decl
)
5459 cp_parser_parse_tentatively (parser
);
5461 = cp_parser_class_name (parser
,
5462 /*typename_keyword_p=*/false,
5463 /*template_keyword_p=*/false,
5465 /*check_dependency=*/false,
5466 /*class_head_p=*/false,
5468 if (processing_template_decl
5469 && ! cp_parser_parse_definitely (parser
))
5471 /* We couldn't find a type with this name. If we're parsing
5472 tentatively, fail and try something else. */
5473 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5475 cp_parser_simulate_error (parser
);
5476 return error_mark_node
;
5478 /* Otherwise, accept it and check for a match at instantiation
5480 type_decl
= cp_parser_identifier (parser
);
5481 if (type_decl
!= error_mark_node
)
5482 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5486 /* If an error occurred, assume that the name of the
5487 destructor is the same as the name of the qualifying
5488 class. That allows us to keep parsing after running
5489 into ill-formed destructor names. */
5490 if (type_decl
== error_mark_node
&& scope
)
5491 return build_nt (BIT_NOT_EXPR
, scope
);
5492 else if (type_decl
== error_mark_node
)
5493 return error_mark_node
;
5495 /* Check that destructor name and scope match. */
5496 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5498 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5499 error_at (token
->location
,
5500 "declaration of %<~%T%> as member of %qT",
5502 cp_parser_simulate_error (parser
);
5503 return error_mark_node
;
5508 A typedef-name that names a class shall not be used as the
5509 identifier in the declarator for a destructor declaration. */
5511 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5512 && !DECL_SELF_REFERENCE_P (type_decl
)
5513 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5514 error_at (token
->location
,
5515 "typedef-name %qD used as destructor declarator",
5518 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5522 if (token
->keyword
== RID_OPERATOR
)
5526 /* This could be a template-id, so we try that first. */
5527 cp_parser_parse_tentatively (parser
);
5528 /* Try a template-id. */
5529 id
= cp_parser_template_id (parser
, template_keyword_p
,
5530 /*check_dependency_p=*/true,
5533 /* If that worked, we're done. */
5534 if (cp_parser_parse_definitely (parser
))
5536 /* We still don't know whether we're looking at an
5537 operator-function-id or a conversion-function-id. */
5538 cp_parser_parse_tentatively (parser
);
5539 /* Try an operator-function-id. */
5540 id
= cp_parser_operator_function_id (parser
);
5541 /* If that didn't work, try a conversion-function-id. */
5542 if (!cp_parser_parse_definitely (parser
))
5543 id
= cp_parser_conversion_function_id (parser
);
5544 else if (UDLIT_OPER_P (id
))
5547 const char *name
= UDLIT_OP_SUFFIX (id
);
5548 if (name
[0] != '_' && !in_system_header_at (input_location
)
5550 warning (0, "literal operator suffixes not preceded by %<_%>"
5551 " are reserved for future standardization");
5561 cp_parser_error (parser
, "expected unqualified-id");
5562 return error_mark_node
;
5566 /* Parse an (optional) nested-name-specifier.
5568 nested-name-specifier: [C++98]
5569 class-or-namespace-name :: nested-name-specifier [opt]
5570 class-or-namespace-name :: template nested-name-specifier [opt]
5572 nested-name-specifier: [C++0x]
5575 nested-name-specifier identifier ::
5576 nested-name-specifier template [opt] simple-template-id ::
5578 PARSER->SCOPE should be set appropriately before this function is
5579 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5580 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5583 Sets PARSER->SCOPE to the class (TYPE) or namespace
5584 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5585 it unchanged if there is no nested-name-specifier. Returns the new
5586 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5588 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5589 part of a declaration and/or decl-specifier. */
5592 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5593 bool typename_keyword_p
,
5594 bool check_dependency_p
,
5596 bool is_declaration
)
5598 bool success
= false;
5599 cp_token_position start
= 0;
5602 /* Remember where the nested-name-specifier starts. */
5603 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5605 start
= cp_lexer_token_position (parser
->lexer
, false);
5606 push_deferring_access_checks (dk_deferred
);
5613 tree saved_qualifying_scope
;
5614 bool template_keyword_p
;
5616 /* Spot cases that cannot be the beginning of a
5617 nested-name-specifier. */
5618 token
= cp_lexer_peek_token (parser
->lexer
);
5620 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5621 the already parsed nested-name-specifier. */
5622 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5624 /* Grab the nested-name-specifier and continue the loop. */
5625 cp_parser_pre_parsed_nested_name_specifier (parser
);
5626 /* If we originally encountered this nested-name-specifier
5627 with IS_DECLARATION set to false, we will not have
5628 resolved TYPENAME_TYPEs, so we must do so here. */
5630 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5632 new_scope
= resolve_typename_type (parser
->scope
,
5633 /*only_current_p=*/false);
5634 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5635 parser
->scope
= new_scope
;
5641 /* Spot cases that cannot be the beginning of a
5642 nested-name-specifier. On the second and subsequent times
5643 through the loop, we look for the `template' keyword. */
5644 if (success
&& token
->keyword
== RID_TEMPLATE
)
5646 /* A template-id can start a nested-name-specifier. */
5647 else if (token
->type
== CPP_TEMPLATE_ID
)
5649 /* DR 743: decltype can be used in a nested-name-specifier. */
5650 else if (token_is_decltype (token
))
5654 /* If the next token is not an identifier, then it is
5655 definitely not a type-name or namespace-name. */
5656 if (token
->type
!= CPP_NAME
)
5658 /* If the following token is neither a `<' (to begin a
5659 template-id), nor a `::', then we are not looking at a
5660 nested-name-specifier. */
5661 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5663 if (token
->type
== CPP_COLON
5664 && parser
->colon_corrects_to_scope_p
5665 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5667 error_at (token
->location
,
5668 "found %<:%> in nested-name-specifier, expected %<::%>");
5669 token
->type
= CPP_SCOPE
;
5672 if (token
->type
!= CPP_SCOPE
5673 && !cp_parser_nth_token_starts_template_argument_list_p
5678 /* The nested-name-specifier is optional, so we parse
5680 cp_parser_parse_tentatively (parser
);
5682 /* Look for the optional `template' keyword, if this isn't the
5683 first time through the loop. */
5685 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5687 template_keyword_p
= false;
5689 /* Save the old scope since the name lookup we are about to do
5690 might destroy it. */
5691 old_scope
= parser
->scope
;
5692 saved_qualifying_scope
= parser
->qualifying_scope
;
5693 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5694 look up names in "X<T>::I" in order to determine that "Y" is
5695 a template. So, if we have a typename at this point, we make
5696 an effort to look through it. */
5698 && !typename_keyword_p
5700 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5701 parser
->scope
= resolve_typename_type (parser
->scope
,
5702 /*only_current_p=*/false);
5703 /* Parse the qualifying entity. */
5705 = cp_parser_qualifying_entity (parser
,
5711 /* Look for the `::' token. */
5712 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5714 /* If we found what we wanted, we keep going; otherwise, we're
5716 if (!cp_parser_parse_definitely (parser
))
5718 bool error_p
= false;
5720 /* Restore the OLD_SCOPE since it was valid before the
5721 failed attempt at finding the last
5722 class-or-namespace-name. */
5723 parser
->scope
= old_scope
;
5724 parser
->qualifying_scope
= saved_qualifying_scope
;
5726 /* If the next token is a decltype, and the one after that is a
5727 `::', then the decltype has failed to resolve to a class or
5728 enumeration type. Give this error even when parsing
5729 tentatively since it can't possibly be valid--and we're going
5730 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5731 won't get another chance.*/
5732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5733 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5736 token
= cp_lexer_consume_token (parser
->lexer
);
5737 error_at (token
->location
, "decltype evaluates to %qT, "
5738 "which is not a class or enumeration type",
5740 parser
->scope
= error_mark_node
;
5744 cp_lexer_consume_token (parser
->lexer
);
5747 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5748 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5750 /* If we have a non-type template-id followed by ::, it can't
5751 possibly be valid. */
5752 token
= cp_lexer_peek_token (parser
->lexer
);
5753 tree tid
= token
->u
.tree_check_value
->value
;
5754 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5755 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5757 tree tmpl
= NULL_TREE
;
5758 if (is_overloaded_fn (tid
))
5760 tree fns
= get_fns (tid
);
5761 if (!OVL_CHAIN (fns
))
5762 tmpl
= OVL_CURRENT (fns
);
5763 error_at (token
->location
, "function template-id %qD "
5764 "in nested-name-specifier", tid
);
5768 /* Variable template. */
5769 tmpl
= TREE_OPERAND (tid
, 0);
5770 gcc_assert (variable_template_p (tmpl
));
5771 error_at (token
->location
, "variable template-id %qD "
5772 "in nested-name-specifier", tid
);
5775 inform (DECL_SOURCE_LOCATION (tmpl
),
5776 "%qD declared here", tmpl
);
5778 parser
->scope
= error_mark_node
;
5782 cp_lexer_consume_token (parser
->lexer
);
5783 cp_lexer_consume_token (parser
->lexer
);
5787 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5789 /* If the next token is an identifier, and the one after
5790 that is a `::', then any valid interpretation would have
5791 found a class-or-namespace-name. */
5792 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5793 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5795 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5798 token
= cp_lexer_consume_token (parser
->lexer
);
5801 if (!token
->error_reported
)
5804 tree ambiguous_decls
;
5806 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5808 /*is_template=*/false,
5809 /*is_namespace=*/false,
5810 /*check_dependency=*/true,
5813 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5814 error_at (token
->location
,
5815 "%qD used without template parameters",
5817 else if (ambiguous_decls
)
5819 // cp_parser_lookup_name has the same diagnostic,
5820 // thus make sure to emit it at most once.
5821 if (cp_parser_uncommitted_to_tentative_parse_p
5824 error_at (token
->location
,
5825 "reference to %qD is ambiguous",
5827 print_candidates (ambiguous_decls
);
5829 decl
= error_mark_node
;
5833 if (cxx_dialect
!= cxx98
)
5834 cp_parser_name_lookup_error
5835 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5838 cp_parser_name_lookup_error
5839 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5843 parser
->scope
= error_mark_node
;
5845 /* Treat this as a successful nested-name-specifier
5850 If the name found is not a class-name (clause
5851 _class_) or namespace-name (_namespace.def_), the
5852 program is ill-formed. */
5855 cp_lexer_consume_token (parser
->lexer
);
5859 /* We've found one valid nested-name-specifier. */
5861 /* Name lookup always gives us a DECL. */
5862 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5863 new_scope
= TREE_TYPE (new_scope
);
5864 /* Uses of "template" must be followed by actual templates. */
5865 if (template_keyword_p
5866 && !(CLASS_TYPE_P (new_scope
)
5867 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5869 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5870 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5871 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5872 == TEMPLATE_ID_EXPR
)))
5873 permerror (input_location
, TYPE_P (new_scope
)
5874 ? G_("%qT is not a template")
5875 : G_("%qD is not a template"),
5877 /* If it is a class scope, try to complete it; we are about to
5878 be looking up names inside the class. */
5879 if (TYPE_P (new_scope
)
5880 /* Since checking types for dependency can be expensive,
5881 avoid doing it if the type is already complete. */
5882 && !COMPLETE_TYPE_P (new_scope
)
5883 /* Do not try to complete dependent types. */
5884 && !dependent_type_p (new_scope
))
5886 new_scope
= complete_type (new_scope
);
5887 /* If it is a typedef to current class, use the current
5888 class instead, as the typedef won't have any names inside
5890 if (!COMPLETE_TYPE_P (new_scope
)
5891 && currently_open_class (new_scope
))
5892 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5894 /* Make sure we look in the right scope the next time through
5896 parser
->scope
= new_scope
;
5899 /* If parsing tentatively, replace the sequence of tokens that makes
5900 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5901 token. That way, should we re-parse the token stream, we will
5902 not have to repeat the effort required to do the parse, nor will
5903 we issue duplicate error messages. */
5904 if (success
&& start
)
5908 token
= cp_lexer_token_at (parser
->lexer
, start
);
5909 /* Reset the contents of the START token. */
5910 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5911 /* Retrieve any deferred checks. Do not pop this access checks yet
5912 so the memory will not be reclaimed during token replacing below. */
5913 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5914 token
->u
.tree_check_value
->value
= parser
->scope
;
5915 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5916 token
->u
.tree_check_value
->qualifying_scope
=
5917 parser
->qualifying_scope
;
5918 token
->keyword
= RID_MAX
;
5920 /* Purge all subsequent tokens. */
5921 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5925 pop_to_parent_deferring_access_checks ();
5927 return success
? parser
->scope
: NULL_TREE
;
5930 /* Parse a nested-name-specifier. See
5931 cp_parser_nested_name_specifier_opt for details. This function
5932 behaves identically, except that it will an issue an error if no
5933 nested-name-specifier is present. */
5936 cp_parser_nested_name_specifier (cp_parser
*parser
,
5937 bool typename_keyword_p
,
5938 bool check_dependency_p
,
5940 bool is_declaration
)
5944 /* Look for the nested-name-specifier. */
5945 scope
= cp_parser_nested_name_specifier_opt (parser
,
5950 /* If it was not present, issue an error message. */
5953 cp_parser_error (parser
, "expected nested-name-specifier");
5954 parser
->scope
= NULL_TREE
;
5960 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5961 this is either a class-name or a namespace-name (which corresponds
5962 to the class-or-namespace-name production in the grammar). For
5963 C++0x, it can also be a type-name that refers to an enumeration
5964 type or a simple-template-id.
5966 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5967 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5968 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5969 TYPE_P is TRUE iff the next name should be taken as a class-name,
5970 even the same name is declared to be another entity in the same
5973 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5974 specified by the class-or-namespace-name. If neither is found the
5975 ERROR_MARK_NODE is returned. */
5978 cp_parser_qualifying_entity (cp_parser
*parser
,
5979 bool typename_keyword_p
,
5980 bool template_keyword_p
,
5981 bool check_dependency_p
,
5983 bool is_declaration
)
5986 tree saved_qualifying_scope
;
5987 tree saved_object_scope
;
5990 bool successful_parse_p
;
5992 /* DR 743: decltype can appear in a nested-name-specifier. */
5993 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5995 scope
= cp_parser_decltype (parser
);
5996 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5997 && !MAYBE_CLASS_TYPE_P (scope
))
5999 cp_parser_simulate_error (parser
);
6000 return error_mark_node
;
6002 if (TYPE_NAME (scope
))
6003 scope
= TYPE_NAME (scope
);
6007 /* Before we try to parse the class-name, we must save away the
6008 current PARSER->SCOPE since cp_parser_class_name will destroy
6010 saved_scope
= parser
->scope
;
6011 saved_qualifying_scope
= parser
->qualifying_scope
;
6012 saved_object_scope
= parser
->object_scope
;
6013 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6014 there is no need to look for a namespace-name. */
6015 only_class_p
= template_keyword_p
6016 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6018 cp_parser_parse_tentatively (parser
);
6019 scope
= cp_parser_class_name (parser
,
6022 type_p
? class_type
: none_type
,
6024 /*class_head_p=*/false,
6026 /*enum_ok=*/cxx_dialect
> cxx98
);
6027 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6028 /* If that didn't work, try for a namespace-name. */
6029 if (!only_class_p
&& !successful_parse_p
)
6031 /* Restore the saved scope. */
6032 parser
->scope
= saved_scope
;
6033 parser
->qualifying_scope
= saved_qualifying_scope
;
6034 parser
->object_scope
= saved_object_scope
;
6035 /* If we are not looking at an identifier followed by the scope
6036 resolution operator, then this is not part of a
6037 nested-name-specifier. (Note that this function is only used
6038 to parse the components of a nested-name-specifier.) */
6039 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6040 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6041 return error_mark_node
;
6042 scope
= cp_parser_namespace_name (parser
);
6048 /* Return true if we are looking at a compound-literal, false otherwise. */
6051 cp_parser_compound_literal_p (cp_parser
*parser
)
6053 /* Consume the `('. */
6054 cp_lexer_consume_token (parser
->lexer
);
6056 cp_lexer_save_tokens (parser
->lexer
);
6058 /* Skip tokens until the next token is a closing parenthesis.
6059 If we find the closing `)', and the next token is a `{', then
6060 we are looking at a compound-literal. */
6061 bool compound_literal_p
6062 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6063 /*consume_paren=*/true)
6064 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6066 /* Roll back the tokens we skipped. */
6067 cp_lexer_rollback_tokens (parser
->lexer
);
6069 return compound_literal_p
;
6072 /* Parse a postfix-expression.
6076 postfix-expression [ expression ]
6077 postfix-expression ( expression-list [opt] )
6078 simple-type-specifier ( expression-list [opt] )
6079 typename :: [opt] nested-name-specifier identifier
6080 ( expression-list [opt] )
6081 typename :: [opt] nested-name-specifier template [opt] template-id
6082 ( expression-list [opt] )
6083 postfix-expression . template [opt] id-expression
6084 postfix-expression -> template [opt] id-expression
6085 postfix-expression . pseudo-destructor-name
6086 postfix-expression -> pseudo-destructor-name
6087 postfix-expression ++
6088 postfix-expression --
6089 dynamic_cast < type-id > ( expression )
6090 static_cast < type-id > ( expression )
6091 reinterpret_cast < type-id > ( expression )
6092 const_cast < type-id > ( expression )
6093 typeid ( expression )
6099 ( type-id ) { initializer-list , [opt] }
6101 This extension is a GNU version of the C99 compound-literal
6102 construct. (The C99 grammar uses `type-name' instead of `type-id',
6103 but they are essentially the same concept.)
6105 If ADDRESS_P is true, the postfix expression is the operand of the
6106 `&' operator. CAST_P is true if this expression is the target of a
6109 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6110 class member access expressions [expr.ref].
6112 Returns a representation of the expression. */
6115 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6116 bool member_access_only_p
, bool decltype_p
,
6117 cp_id_kind
* pidk_return
)
6122 cp_id_kind idk
= CP_ID_KIND_NONE
;
6123 tree postfix_expression
= NULL_TREE
;
6124 bool is_member_access
= false;
6125 int saved_in_statement
= -1;
6127 /* Peek at the next token. */
6128 token
= cp_lexer_peek_token (parser
->lexer
);
6129 loc
= token
->location
;
6130 /* Some of the productions are determined by keywords. */
6131 keyword
= token
->keyword
;
6141 const char *saved_message
;
6142 bool saved_in_type_id_in_expr_p
;
6144 /* All of these can be handled in the same way from the point
6145 of view of parsing. Begin by consuming the token
6146 identifying the cast. */
6147 cp_lexer_consume_token (parser
->lexer
);
6149 /* New types cannot be defined in the cast. */
6150 saved_message
= parser
->type_definition_forbidden_message
;
6151 parser
->type_definition_forbidden_message
6152 = G_("types may not be defined in casts");
6154 /* Look for the opening `<'. */
6155 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6156 /* Parse the type to which we are casting. */
6157 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6158 parser
->in_type_id_in_expr_p
= true;
6159 type
= cp_parser_type_id (parser
);
6160 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6161 /* Look for the closing `>'. */
6162 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6163 /* Restore the old message. */
6164 parser
->type_definition_forbidden_message
= saved_message
;
6166 bool saved_greater_than_is_operator_p
6167 = parser
->greater_than_is_operator_p
;
6168 parser
->greater_than_is_operator_p
= true;
6170 /* And the expression which is being cast. */
6171 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6172 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6173 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6175 parser
->greater_than_is_operator_p
6176 = saved_greater_than_is_operator_p
;
6178 /* Only type conversions to integral or enumeration types
6179 can be used in constant-expressions. */
6180 if (!cast_valid_in_integral_constant_expression_p (type
)
6181 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6182 return error_mark_node
;
6188 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6192 = build_static_cast (type
, expression
, tf_warning_or_error
);
6196 = build_reinterpret_cast (type
, expression
,
6197 tf_warning_or_error
);
6201 = build_const_cast (type
, expression
, tf_warning_or_error
);
6212 const char *saved_message
;
6213 bool saved_in_type_id_in_expr_p
;
6215 /* Consume the `typeid' token. */
6216 cp_lexer_consume_token (parser
->lexer
);
6217 /* Look for the `(' token. */
6218 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
6219 /* Types cannot be defined in a `typeid' expression. */
6220 saved_message
= parser
->type_definition_forbidden_message
;
6221 parser
->type_definition_forbidden_message
6222 = G_("types may not be defined in a %<typeid%> expression");
6223 /* We can't be sure yet whether we're looking at a type-id or an
6225 cp_parser_parse_tentatively (parser
);
6226 /* Try a type-id first. */
6227 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6228 parser
->in_type_id_in_expr_p
= true;
6229 type
= cp_parser_type_id (parser
);
6230 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6231 /* Look for the `)' token. Otherwise, we can't be sure that
6232 we're not looking at an expression: consider `typeid (int
6233 (3))', for example. */
6234 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6235 /* If all went well, simply lookup the type-id. */
6236 if (cp_parser_parse_definitely (parser
))
6237 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6238 /* Otherwise, fall back to the expression variant. */
6243 /* Look for an expression. */
6244 expression
= cp_parser_expression (parser
, & idk
);
6245 /* Compute its typeid. */
6246 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6247 /* Look for the `)' token. */
6248 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6250 /* Restore the saved message. */
6251 parser
->type_definition_forbidden_message
= saved_message
;
6252 /* `typeid' may not appear in an integral constant expression. */
6253 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6254 return error_mark_node
;
6261 /* The syntax permitted here is the same permitted for an
6262 elaborated-type-specifier. */
6263 ++parser
->prevent_constrained_type_specifiers
;
6264 type
= cp_parser_elaborated_type_specifier (parser
,
6265 /*is_friend=*/false,
6266 /*is_declaration=*/false);
6267 --parser
->prevent_constrained_type_specifiers
;
6268 postfix_expression
= cp_parser_functional_cast (parser
, type
);
6272 case RID_CILK_SPAWN
:
6274 cp_lexer_consume_token (parser
->lexer
);
6275 token
= cp_lexer_peek_token (parser
->lexer
);
6276 if (token
->type
== CPP_SEMICOLON
)
6278 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6280 postfix_expression
= error_mark_node
;
6283 else if (!current_function_decl
)
6285 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6286 "inside a function");
6287 postfix_expression
= error_mark_node
;
6292 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6293 saved_in_statement
= parser
->in_statement
;
6294 parser
->in_statement
|= IN_CILK_SPAWN
;
6296 cfun
->calls_cilk_spawn
= 1;
6297 postfix_expression
=
6298 cp_parser_postfix_expression (parser
, false, false,
6299 false, false, &idk
);
6302 error_at (token
->location
, "-fcilkplus must be enabled to use"
6303 " %<_Cilk_spawn%>");
6304 cfun
->calls_cilk_spawn
= 0;
6306 else if (saved_in_statement
& IN_CILK_SPAWN
)
6308 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6309 "are not permitted");
6310 postfix_expression
= error_mark_node
;
6311 cfun
->calls_cilk_spawn
= 0;
6315 postfix_expression
= build_cilk_spawn (token
->location
,
6316 postfix_expression
);
6317 if (postfix_expression
!= error_mark_node
)
6318 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6319 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6324 case RID_BUILTIN_SHUFFLE
:
6326 vec
<tree
, va_gc
> *vec
;
6330 cp_lexer_consume_token (parser
->lexer
);
6331 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6332 /*cast_p=*/false, /*allow_expansion_p=*/true,
6333 /*non_constant_p=*/NULL
);
6335 return error_mark_node
;
6337 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6340 if (vec
->length () == 2)
6341 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6342 tf_warning_or_error
);
6343 else if (vec
->length () == 3)
6344 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6345 tf_warning_or_error
);
6348 error_at (loc
, "wrong number of arguments to "
6349 "%<__builtin_shuffle%>");
6350 return error_mark_node
;
6359 /* If the next thing is a simple-type-specifier, we may be
6360 looking at a functional cast. We could also be looking at
6361 an id-expression. So, we try the functional cast, and if
6362 that doesn't work we fall back to the primary-expression. */
6363 cp_parser_parse_tentatively (parser
);
6364 /* Look for the simple-type-specifier. */
6365 ++parser
->prevent_constrained_type_specifiers
;
6366 type
= cp_parser_simple_type_specifier (parser
,
6367 /*decl_specs=*/NULL
,
6368 CP_PARSER_FLAGS_NONE
);
6369 --parser
->prevent_constrained_type_specifiers
;
6370 /* Parse the cast itself. */
6371 if (!cp_parser_error_occurred (parser
))
6373 = cp_parser_functional_cast (parser
, type
);
6374 /* If that worked, we're done. */
6375 if (cp_parser_parse_definitely (parser
))
6378 /* If the functional-cast didn't work out, try a
6379 compound-literal. */
6380 if (cp_parser_allow_gnu_extensions_p (parser
)
6381 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6383 tree initializer
= NULL_TREE
;
6385 cp_parser_parse_tentatively (parser
);
6387 /* Avoid calling cp_parser_type_id pointlessly, see comment
6388 in cp_parser_cast_expression about c++/29234. */
6389 if (!cp_parser_compound_literal_p (parser
))
6390 cp_parser_simulate_error (parser
);
6393 /* Parse the type. */
6394 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6395 parser
->in_type_id_in_expr_p
= true;
6396 type
= cp_parser_type_id (parser
);
6397 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6398 /* Look for the `)'. */
6399 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6402 /* If things aren't going well, there's no need to
6404 if (!cp_parser_error_occurred (parser
))
6406 bool non_constant_p
;
6407 /* Parse the brace-enclosed initializer list. */
6408 initializer
= cp_parser_braced_list (parser
,
6411 /* If that worked, we're definitely looking at a
6412 compound-literal expression. */
6413 if (cp_parser_parse_definitely (parser
))
6415 /* Warn the user that a compound literal is not
6416 allowed in standard C++. */
6417 pedwarn (input_location
, OPT_Wpedantic
,
6418 "ISO C++ forbids compound-literals");
6419 /* For simplicity, we disallow compound literals in
6420 constant-expressions. We could
6421 allow compound literals of integer type, whose
6422 initializer was a constant, in constant
6423 expressions. Permitting that usage, as a further
6424 extension, would not change the meaning of any
6425 currently accepted programs. (Of course, as
6426 compound literals are not part of ISO C++, the
6427 standard has nothing to say.) */
6428 if (cp_parser_non_integral_constant_expression (parser
,
6431 postfix_expression
= error_mark_node
;
6434 /* Form the representation of the compound-literal. */
6436 = finish_compound_literal (type
, initializer
,
6437 tf_warning_or_error
);
6442 /* It must be a primary-expression. */
6444 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6445 /*template_arg_p=*/false,
6452 /* Note that we don't need to worry about calling build_cplus_new on a
6453 class-valued CALL_EXPR in decltype when it isn't the end of the
6454 postfix-expression; unary_complex_lvalue will take care of that for
6457 /* Keep looping until the postfix-expression is complete. */
6460 if (idk
== CP_ID_KIND_UNQUALIFIED
6461 && identifier_p (postfix_expression
)
6462 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6463 /* It is not a Koenig lookup function call. */
6465 = unqualified_name_lookup_error (postfix_expression
);
6467 /* Peek at the next token. */
6468 token
= cp_lexer_peek_token (parser
->lexer
);
6470 switch (token
->type
)
6472 case CPP_OPEN_SQUARE
:
6473 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6475 cp_parser_error (parser
,
6476 "two consecutive %<[%> shall "
6477 "only introduce an attribute");
6478 return error_mark_node
;
6481 = cp_parser_postfix_open_square_expression (parser
,
6485 idk
= CP_ID_KIND_NONE
;
6486 is_member_access
= false;
6489 case CPP_OPEN_PAREN
:
6490 /* postfix-expression ( expression-list [opt] ) */
6493 bool is_builtin_constant_p
;
6494 bool saved_integral_constant_expression_p
= false;
6495 bool saved_non_integral_constant_expression_p
= false;
6496 tsubst_flags_t complain
= complain_flags (decltype_p
);
6497 vec
<tree
, va_gc
> *args
;
6499 is_member_access
= false;
6501 is_builtin_constant_p
6502 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6503 if (is_builtin_constant_p
)
6505 /* The whole point of __builtin_constant_p is to allow
6506 non-constant expressions to appear as arguments. */
6507 saved_integral_constant_expression_p
6508 = parser
->integral_constant_expression_p
;
6509 saved_non_integral_constant_expression_p
6510 = parser
->non_integral_constant_expression_p
;
6511 parser
->integral_constant_expression_p
= false;
6513 args
= (cp_parser_parenthesized_expression_list
6515 /*cast_p=*/false, /*allow_expansion_p=*/true,
6516 /*non_constant_p=*/NULL
));
6517 if (is_builtin_constant_p
)
6519 parser
->integral_constant_expression_p
6520 = saved_integral_constant_expression_p
;
6521 parser
->non_integral_constant_expression_p
6522 = saved_non_integral_constant_expression_p
;
6527 postfix_expression
= error_mark_node
;
6531 /* Function calls are not permitted in
6532 constant-expressions. */
6533 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6534 && cp_parser_non_integral_constant_expression (parser
,
6537 postfix_expression
= error_mark_node
;
6538 release_tree_vector (args
);
6543 if (idk
== CP_ID_KIND_UNQUALIFIED
6544 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6546 if (identifier_p (postfix_expression
))
6548 if (!args
->is_empty ())
6551 if (!any_type_dependent_arguments_p (args
))
6553 = perform_koenig_lookup (postfix_expression
, args
,
6558 = unqualified_fn_lookup_error (postfix_expression
);
6560 /* We do not perform argument-dependent lookup if
6561 normal lookup finds a non-function, in accordance
6562 with the expected resolution of DR 218. */
6563 else if (!args
->is_empty ()
6564 && is_overloaded_fn (postfix_expression
))
6566 tree fn
= get_first_fn (postfix_expression
);
6567 fn
= STRIP_TEMPLATE (fn
);
6569 /* Do not do argument dependent lookup if regular
6570 lookup finds a member function or a block-scope
6571 function declaration. [basic.lookup.argdep]/3 */
6572 if (!DECL_FUNCTION_MEMBER_P (fn
)
6573 && !DECL_LOCAL_FUNCTION_P (fn
))
6576 if (!any_type_dependent_arguments_p (args
))
6578 = perform_koenig_lookup (postfix_expression
, args
,
6584 if (warn_memset_transposed_args
)
6586 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6587 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6588 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6589 && vec_safe_length (args
) == 3
6590 && TREE_CODE ((*args
)[2]) == INTEGER_CST
6591 && integer_zerop ((*args
)[2])
6592 && !(TREE_CODE ((*args
)[1]) == INTEGER_CST
6593 && integer_zerop ((*args
)[1])))
6594 warning (OPT_Wmemset_transposed_args
,
6595 "%<memset%> used with constant zero length "
6596 "parameter; this could be due to transposed "
6600 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6602 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6603 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6605 if (processing_template_decl
6606 && (type_dependent_expression_p (instance
)
6607 || (!BASELINK_P (fn
)
6608 && TREE_CODE (fn
) != FIELD_DECL
)
6609 || type_dependent_expression_p (fn
)
6610 || any_type_dependent_arguments_p (args
)))
6613 = build_nt_call_vec (postfix_expression
, args
);
6614 release_tree_vector (args
);
6618 if (BASELINK_P (fn
))
6621 = (build_new_method_call
6622 (instance
, fn
, &args
, NULL_TREE
,
6623 (idk
== CP_ID_KIND_QUALIFIED
6624 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6631 = finish_call_expr (postfix_expression
, &args
,
6632 /*disallow_virtual=*/false,
6636 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6637 || TREE_CODE (postfix_expression
) == MEMBER_REF
6638 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6639 postfix_expression
= (build_offset_ref_call_from_tree
6640 (postfix_expression
, &args
,
6642 else if (idk
== CP_ID_KIND_QUALIFIED
)
6643 /* A call to a static class member, or a namespace-scope
6646 = finish_call_expr (postfix_expression
, &args
,
6647 /*disallow_virtual=*/true,
6651 /* All other function calls. */
6653 = finish_call_expr (postfix_expression
, &args
,
6654 /*disallow_virtual=*/false,
6658 protected_set_expr_location (postfix_expression
, token
->location
);
6660 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6661 idk
= CP_ID_KIND_NONE
;
6663 release_tree_vector (args
);
6669 /* postfix-expression . template [opt] id-expression
6670 postfix-expression . pseudo-destructor-name
6671 postfix-expression -> template [opt] id-expression
6672 postfix-expression -> pseudo-destructor-name */
6674 /* Consume the `.' or `->' operator. */
6675 cp_lexer_consume_token (parser
->lexer
);
6678 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6682 is_member_access
= true;
6686 /* postfix-expression ++ */
6687 /* Consume the `++' token. */
6688 cp_lexer_consume_token (parser
->lexer
);
6689 /* Generate a representation for the complete expression. */
6691 = finish_increment_expr (postfix_expression
,
6692 POSTINCREMENT_EXPR
);
6693 /* Increments may not appear in constant-expressions. */
6694 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6695 postfix_expression
= error_mark_node
;
6696 idk
= CP_ID_KIND_NONE
;
6697 is_member_access
= false;
6700 case CPP_MINUS_MINUS
:
6701 /* postfix-expression -- */
6702 /* Consume the `--' token. */
6703 cp_lexer_consume_token (parser
->lexer
);
6704 /* Generate a representation for the complete expression. */
6706 = finish_increment_expr (postfix_expression
,
6707 POSTDECREMENT_EXPR
);
6708 /* Decrements may not appear in constant-expressions. */
6709 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6710 postfix_expression
= error_mark_node
;
6711 idk
= CP_ID_KIND_NONE
;
6712 is_member_access
= false;
6716 if (pidk_return
!= NULL
)
6717 * pidk_return
= idk
;
6718 if (member_access_only_p
)
6719 return is_member_access
? postfix_expression
: error_mark_node
;
6721 return postfix_expression
;
6725 /* We should never get here. */
6727 return error_mark_node
;
6730 /* This function parses Cilk Plus array notations. If a normal array expr. is
6731 parsed then the array index is passed back to the caller through *INIT_INDEX
6732 and the function returns a NULL_TREE. If array notation expr. is parsed,
6733 then *INIT_INDEX is ignored by the caller and the function returns
6734 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6738 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6741 cp_token
*token
= NULL
;
6742 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6743 if (!array_value
|| array_value
== error_mark_node
)
6745 cp_parser_skip_to_end_of_statement (parser
);
6746 return error_mark_node
;
6749 array_type
= TREE_TYPE (array_value
);
6751 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6752 parser
->colon_corrects_to_scope_p
= false;
6753 token
= cp_lexer_peek_token (parser
->lexer
);
6757 cp_parser_error (parser
, "expected %<:%> or numeral");
6758 return error_mark_node
;
6760 else if (token
->type
== CPP_COLON
)
6762 /* Consume the ':'. */
6763 cp_lexer_consume_token (parser
->lexer
);
6765 /* If we are here, then we have a case like this A[:]. */
6766 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6768 cp_parser_error (parser
, "expected %<]%>");
6769 cp_parser_skip_to_end_of_statement (parser
);
6770 return error_mark_node
;
6772 *init_index
= NULL_TREE
;
6774 length_index
= NULL_TREE
;
6778 /* If we are here, then there are three valid possibilities:
6780 2. ARRAY [ EXP : EXP ]
6781 3. ARRAY [ EXP : EXP : EXP ] */
6783 *init_index
= cp_parser_expression (parser
);
6784 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6786 /* This indicates that we have a normal array expression. */
6787 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6791 /* Consume the ':'. */
6792 cp_lexer_consume_token (parser
->lexer
);
6793 length_index
= cp_parser_expression (parser
);
6794 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6796 cp_lexer_consume_token (parser
->lexer
);
6797 stride
= cp_parser_expression (parser
);
6800 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6802 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6803 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6805 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6806 cp_lexer_consume_token (parser
->lexer
);
6807 return error_mark_node
;
6809 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6811 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6812 length_index
, stride
, array_type
);
6816 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6817 by cp_parser_builtin_offsetof. We're looking for
6819 postfix-expression [ expression ]
6820 postfix-expression [ braced-init-list ] (C++11)
6822 FOR_OFFSETOF is set if we're being called in that context, which
6823 changes how we deal with integer constant expressions. */
6826 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6827 tree postfix_expression
,
6831 tree index
= NULL_TREE
;
6832 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6833 bool saved_greater_than_is_operator_p
;
6835 /* Consume the `[' token. */
6836 cp_lexer_consume_token (parser
->lexer
);
6838 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6839 parser
->greater_than_is_operator_p
= true;
6841 /* Parse the index expression. */
6842 /* ??? For offsetof, there is a question of what to allow here. If
6843 offsetof is not being used in an integral constant expression context,
6844 then we *could* get the right answer by computing the value at runtime.
6845 If we are in an integral constant expression context, then we might
6846 could accept any constant expression; hard to say without analysis.
6847 Rather than open the barn door too wide right away, allow only integer
6848 constant expressions here. */
6850 index
= cp_parser_constant_expression (parser
);
6853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6855 bool expr_nonconst_p
;
6856 cp_lexer_set_source_position (parser
->lexer
);
6857 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6858 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6860 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6862 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6863 "braced list index is not allowed with array "
6865 cp_parser_skip_to_end_of_statement (parser
);
6866 return error_mark_node
;
6869 else if (flag_cilkplus
)
6871 /* Here are have these two options:
6872 ARRAY[EXP : EXP] - Array notation expr with default
6874 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6876 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6877 postfix_expression
);
6882 index
= cp_parser_expression (parser
);
6885 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6887 /* Look for the closing `]'. */
6888 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6890 /* Build the ARRAY_REF. */
6891 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6894 /* When not doing offsetof, array references are not permitted in
6895 constant-expressions. */
6897 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6898 postfix_expression
= error_mark_node
;
6900 return postfix_expression
;
6903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6904 by cp_parser_builtin_offsetof. We're looking for
6906 postfix-expression . template [opt] id-expression
6907 postfix-expression . pseudo-destructor-name
6908 postfix-expression -> template [opt] id-expression
6909 postfix-expression -> pseudo-destructor-name
6911 FOR_OFFSETOF is set if we're being called in that context. That sorta
6912 limits what of the above we'll actually accept, but nevermind.
6913 TOKEN_TYPE is the "." or "->" token, which will already have been
6914 removed from the stream. */
6917 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6918 enum cpp_ttype token_type
,
6919 tree postfix_expression
,
6920 bool for_offsetof
, cp_id_kind
*idk
,
6921 location_t location
)
6925 bool pseudo_destructor_p
;
6926 tree scope
= NULL_TREE
;
6928 /* If this is a `->' operator, dereference the pointer. */
6929 if (token_type
== CPP_DEREF
)
6930 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6931 tf_warning_or_error
);
6932 /* Check to see whether or not the expression is type-dependent. */
6933 dependent_p
= type_dependent_expression_p (postfix_expression
);
6934 /* The identifier following the `->' or `.' is not qualified. */
6935 parser
->scope
= NULL_TREE
;
6936 parser
->qualifying_scope
= NULL_TREE
;
6937 parser
->object_scope
= NULL_TREE
;
6938 *idk
= CP_ID_KIND_NONE
;
6940 /* Enter the scope corresponding to the type of the object
6941 given by the POSTFIX_EXPRESSION. */
6942 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6944 scope
= TREE_TYPE (postfix_expression
);
6945 /* According to the standard, no expression should ever have
6946 reference type. Unfortunately, we do not currently match
6947 the standard in this respect in that our internal representation
6948 of an expression may have reference type even when the standard
6949 says it does not. Therefore, we have to manually obtain the
6950 underlying type here. */
6951 scope
= non_reference (scope
);
6952 /* The type of the POSTFIX_EXPRESSION must be complete. */
6953 if (scope
== unknown_type_node
)
6955 error_at (location
, "%qE does not have class type",
6956 postfix_expression
);
6959 /* Unlike the object expression in other contexts, *this is not
6960 required to be of complete type for purposes of class member
6961 access (5.2.5) outside the member function body. */
6962 else if (postfix_expression
!= current_class_ref
6963 && !(processing_template_decl
&& scope
== current_class_type
))
6964 scope
= complete_type_or_else (scope
, NULL_TREE
);
6965 /* Let the name lookup machinery know that we are processing a
6966 class member access expression. */
6967 parser
->context
->object_type
= scope
;
6968 /* If something went wrong, we want to be able to discern that case,
6969 as opposed to the case where there was no SCOPE due to the type
6970 of expression being dependent. */
6972 scope
= error_mark_node
;
6973 /* If the SCOPE was erroneous, make the various semantic analysis
6974 functions exit quickly -- and without issuing additional error
6976 if (scope
== error_mark_node
)
6977 postfix_expression
= error_mark_node
;
6980 /* Assume this expression is not a pseudo-destructor access. */
6981 pseudo_destructor_p
= false;
6983 /* If the SCOPE is a scalar type, then, if this is a valid program,
6984 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6985 is type dependent, it can be pseudo-destructor-name or something else.
6986 Try to parse it as pseudo-destructor-name first. */
6987 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6992 cp_parser_parse_tentatively (parser
);
6993 /* Parse the pseudo-destructor-name. */
6995 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6998 && (cp_parser_error_occurred (parser
)
6999 || !SCALAR_TYPE_P (type
)))
7000 cp_parser_abort_tentative_parse (parser
);
7001 else if (cp_parser_parse_definitely (parser
))
7003 pseudo_destructor_p
= true;
7005 = finish_pseudo_destructor_expr (postfix_expression
,
7010 if (!pseudo_destructor_p
)
7012 /* If the SCOPE is not a scalar type, we are looking at an
7013 ordinary class member access expression, rather than a
7014 pseudo-destructor-name. */
7016 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7017 /* Parse the id-expression. */
7018 name
= (cp_parser_id_expression
7020 cp_parser_optional_template_keyword (parser
),
7021 /*check_dependency_p=*/true,
7023 /*declarator_p=*/false,
7024 /*optional_p=*/false));
7025 /* In general, build a SCOPE_REF if the member name is qualified.
7026 However, if the name was not dependent and has already been
7027 resolved; there is no need to build the SCOPE_REF. For example;
7029 struct X { void f(); };
7030 template <typename T> void f(T* t) { t->X::f(); }
7032 Even though "t" is dependent, "X::f" is not and has been resolved
7033 to a BASELINK; there is no need to include scope information. */
7035 /* But we do need to remember that there was an explicit scope for
7036 virtual function calls. */
7038 *idk
= CP_ID_KIND_QUALIFIED
;
7040 /* If the name is a template-id that names a type, we will get a
7041 TYPE_DECL here. That is invalid code. */
7042 if (TREE_CODE (name
) == TYPE_DECL
)
7044 error_at (token
->location
, "invalid use of %qD", name
);
7045 postfix_expression
= error_mark_node
;
7049 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7051 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7053 error_at (token
->location
, "%<%D::%D%> is not a class member",
7054 parser
->scope
, name
);
7055 postfix_expression
= error_mark_node
;
7058 name
= build_qualified_name (/*type=*/NULL_TREE
,
7062 parser
->scope
= NULL_TREE
;
7063 parser
->qualifying_scope
= NULL_TREE
;
7064 parser
->object_scope
= NULL_TREE
;
7066 if (parser
->scope
&& name
&& BASELINK_P (name
))
7067 adjust_result_of_qualified_name_lookup
7068 (name
, parser
->scope
, scope
);
7070 = finish_class_member_access_expr (postfix_expression
, name
,
7072 tf_warning_or_error
);
7076 /* We no longer need to look up names in the scope of the object on
7077 the left-hand side of the `.' or `->' operator. */
7078 parser
->context
->object_type
= NULL_TREE
;
7080 /* Outside of offsetof, these operators may not appear in
7081 constant-expressions. */
7083 && (cp_parser_non_integral_constant_expression
7084 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7085 postfix_expression
= error_mark_node
;
7087 return postfix_expression
;
7090 /* Parse a parenthesized expression-list.
7093 assignment-expression
7094 expression-list, assignment-expression
7099 identifier, expression-list
7101 CAST_P is true if this expression is the target of a cast.
7103 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7106 Returns a vector of trees. Each element is a representation of an
7107 assignment-expression. NULL is returned if the ( and or ) are
7108 missing. An empty, but allocated, vector is returned on no
7109 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7110 if we are parsing an attribute list for an attribute that wants a
7111 plain identifier argument, normal_attr for an attribute that wants
7112 an expression, or non_attr if we aren't parsing an attribute list. If
7113 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7114 not all of the expressions in the list were constant. */
7116 static vec
<tree
, va_gc
> *
7117 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7118 int is_attribute_list
,
7120 bool allow_expansion_p
,
7121 bool *non_constant_p
)
7123 vec
<tree
, va_gc
> *expression_list
;
7124 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7125 tree identifier
= NULL_TREE
;
7126 bool saved_greater_than_is_operator_p
;
7128 /* Assume all the expressions will be constant. */
7130 *non_constant_p
= false;
7132 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
7135 expression_list
= make_tree_vector ();
7137 /* Within a parenthesized expression, a `>' token is always
7138 the greater-than operator. */
7139 saved_greater_than_is_operator_p
7140 = parser
->greater_than_is_operator_p
;
7141 parser
->greater_than_is_operator_p
= true;
7143 /* Consume expressions until there are no more. */
7144 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7149 /* At the beginning of attribute lists, check to see if the
7150 next token is an identifier. */
7151 if (is_attribute_list
== id_attr
7152 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7156 /* Consume the identifier. */
7157 token
= cp_lexer_consume_token (parser
->lexer
);
7158 /* Save the identifier. */
7159 identifier
= token
->u
.value
;
7163 bool expr_non_constant_p
;
7165 /* Parse the next assignment-expression. */
7166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7168 /* A braced-init-list. */
7169 cp_lexer_set_source_position (parser
->lexer
);
7170 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7171 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7172 if (non_constant_p
&& expr_non_constant_p
)
7173 *non_constant_p
= true;
7175 else if (non_constant_p
)
7177 expr
= (cp_parser_constant_expression
7178 (parser
, /*allow_non_constant_p=*/true,
7179 &expr_non_constant_p
));
7180 if (expr_non_constant_p
)
7181 *non_constant_p
= true;
7184 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7188 expr
= instantiate_non_dependent_expr (expr
);
7190 /* If we have an ellipsis, then this is an expression
7192 if (allow_expansion_p
7193 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7195 /* Consume the `...'. */
7196 cp_lexer_consume_token (parser
->lexer
);
7198 /* Build the argument pack. */
7199 expr
= make_pack_expansion (expr
);
7202 /* Add it to the list. We add error_mark_node
7203 expressions to the list, so that we can still tell if
7204 the correct form for a parenthesized expression-list
7205 is found. That gives better errors. */
7206 vec_safe_push (expression_list
, expr
);
7208 if (expr
== error_mark_node
)
7212 /* After the first item, attribute lists look the same as
7213 expression lists. */
7214 is_attribute_list
= non_attr
;
7217 /* If the next token isn't a `,', then we are done. */
7218 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7221 /* Otherwise, consume the `,' and keep going. */
7222 cp_lexer_consume_token (parser
->lexer
);
7225 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7230 /* We try and resync to an unnested comma, as that will give the
7231 user better diagnostics. */
7232 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7233 /*recovering=*/true,
7235 /*consume_paren=*/true);
7240 parser
->greater_than_is_operator_p
7241 = saved_greater_than_is_operator_p
;
7246 parser
->greater_than_is_operator_p
7247 = saved_greater_than_is_operator_p
;
7250 vec_safe_insert (expression_list
, 0, identifier
);
7252 return expression_list
;
7255 /* Parse a pseudo-destructor-name.
7257 pseudo-destructor-name:
7258 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7259 :: [opt] nested-name-specifier template template-id :: ~ type-name
7260 :: [opt] nested-name-specifier [opt] ~ type-name
7262 If either of the first two productions is used, sets *SCOPE to the
7263 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7264 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7265 or ERROR_MARK_NODE if the parse fails. */
7268 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7273 bool nested_name_specifier_p
;
7276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7277 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7278 && !type_dependent_expression_p (object
))
7280 if (cxx_dialect
< cxx14
)
7281 pedwarn (input_location
, 0,
7282 "%<~auto%> only available with "
7283 "-std=c++14 or -std=gnu++14");
7284 cp_lexer_consume_token (parser
->lexer
);
7285 cp_lexer_consume_token (parser
->lexer
);
7287 *type
= TREE_TYPE (object
);
7291 /* Assume that things will not work out. */
7292 *type
= error_mark_node
;
7294 /* Look for the optional `::' operator. */
7295 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7296 /* Look for the optional nested-name-specifier. */
7297 nested_name_specifier_p
7298 = (cp_parser_nested_name_specifier_opt (parser
,
7299 /*typename_keyword_p=*/false,
7300 /*check_dependency_p=*/true,
7302 /*is_declaration=*/false)
7304 /* Now, if we saw a nested-name-specifier, we might be doing the
7305 second production. */
7306 if (nested_name_specifier_p
7307 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7309 /* Consume the `template' keyword. */
7310 cp_lexer_consume_token (parser
->lexer
);
7311 /* Parse the template-id. */
7312 cp_parser_template_id (parser
,
7313 /*template_keyword_p=*/true,
7314 /*check_dependency_p=*/false,
7316 /*is_declaration=*/true);
7317 /* Look for the `::' token. */
7318 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7320 /* If the next token is not a `~', then there might be some
7321 additional qualification. */
7322 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7324 /* At this point, we're looking for "type-name :: ~". The type-name
7325 must not be a class-name, since this is a pseudo-destructor. So,
7326 it must be either an enum-name, or a typedef-name -- both of which
7327 are just identifiers. So, we peek ahead to check that the "::"
7328 and "~" tokens are present; if they are not, then we can avoid
7329 calling type_name. */
7330 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7331 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7332 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7334 cp_parser_error (parser
, "non-scalar type");
7338 /* Look for the type-name. */
7339 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7340 if (*scope
== error_mark_node
)
7343 /* Look for the `::' token. */
7344 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7349 /* Look for the `~'. */
7350 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7352 /* Once we see the ~, this has to be a pseudo-destructor. */
7353 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7354 cp_parser_commit_to_topmost_tentative_parse (parser
);
7356 /* Look for the type-name again. We are not responsible for
7357 checking that it matches the first type-name. */
7358 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7361 /* Parse a unary-expression.
7367 unary-operator cast-expression
7368 sizeof unary-expression
7370 alignof ( type-id ) [C++0x]
7377 __extension__ cast-expression
7378 __alignof__ unary-expression
7379 __alignof__ ( type-id )
7380 alignof unary-expression [C++0x]
7381 __real__ cast-expression
7382 __imag__ cast-expression
7384 sizeof ( type-id ) { initializer-list , [opt] }
7385 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7386 __alignof__ ( type-id ) { initializer-list , [opt] }
7388 ADDRESS_P is true iff the unary-expression is appearing as the
7389 operand of the `&' operator. CAST_P is true if this expression is
7390 the target of a cast.
7392 Returns a representation of the expression. */
7395 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7396 bool address_p
, bool cast_p
, bool decltype_p
)
7399 enum tree_code unary_operator
;
7401 /* Peek at the next token. */
7402 token
= cp_lexer_peek_token (parser
->lexer
);
7403 /* Some keywords give away the kind of expression. */
7404 if (token
->type
== CPP_KEYWORD
)
7406 enum rid keyword
= token
->keyword
;
7415 location_t first_loc
;
7417 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7418 /* Consume the token. */
7419 cp_lexer_consume_token (parser
->lexer
);
7420 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7421 /* Parse the operand. */
7422 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7424 if (TYPE_P (operand
))
7425 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7428 /* ISO C++ defines alignof only with types, not with
7429 expressions. So pedwarn if alignof is used with a non-
7430 type expression. However, __alignof__ is ok. */
7431 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7432 pedwarn (token
->location
, OPT_Wpedantic
,
7433 "ISO C++ does not allow %<alignof%> "
7436 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7438 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7439 SIZEOF_EXPR with the original operand. */
7440 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7442 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7444 if (!processing_template_decl
&& TYPE_P (operand
))
7446 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7447 build1 (NOP_EXPR
, operand
,
7449 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7452 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7453 TREE_SIDE_EFFECTS (ret
) = 0;
7454 TREE_READONLY (ret
) = 1;
7456 SET_EXPR_LOCATION (ret
, first_loc
);
7462 return cp_parser_new_expression (parser
);
7465 return cp_parser_delete_expression (parser
);
7469 /* The saved value of the PEDANTIC flag. */
7473 /* Save away the PEDANTIC flag. */
7474 cp_parser_extension_opt (parser
, &saved_pedantic
);
7475 /* Parse the cast-expression. */
7476 expr
= cp_parser_simple_cast_expression (parser
);
7477 /* Restore the PEDANTIC flag. */
7478 pedantic
= saved_pedantic
;
7488 /* Consume the `__real__' or `__imag__' token. */
7489 cp_lexer_consume_token (parser
->lexer
);
7490 /* Parse the cast-expression. */
7491 expression
= cp_parser_simple_cast_expression (parser
);
7492 /* Create the complete representation. */
7493 return build_x_unary_op (token
->location
,
7494 (keyword
== RID_REALPART
7495 ? REALPART_EXPR
: IMAGPART_EXPR
),
7497 tf_warning_or_error
);
7501 case RID_TRANSACTION_ATOMIC
:
7502 case RID_TRANSACTION_RELAXED
:
7503 return cp_parser_transaction_expression (parser
, keyword
);
7508 const char *saved_message
;
7509 bool saved_integral_constant_expression_p
;
7510 bool saved_non_integral_constant_expression_p
;
7511 bool saved_greater_than_is_operator_p
;
7513 cp_lexer_consume_token (parser
->lexer
);
7514 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7516 saved_message
= parser
->type_definition_forbidden_message
;
7517 parser
->type_definition_forbidden_message
7518 = G_("types may not be defined in %<noexcept%> expressions");
7520 saved_integral_constant_expression_p
7521 = parser
->integral_constant_expression_p
;
7522 saved_non_integral_constant_expression_p
7523 = parser
->non_integral_constant_expression_p
;
7524 parser
->integral_constant_expression_p
= false;
7526 saved_greater_than_is_operator_p
7527 = parser
->greater_than_is_operator_p
;
7528 parser
->greater_than_is_operator_p
= true;
7530 ++cp_unevaluated_operand
;
7531 ++c_inhibit_evaluation_warnings
;
7532 ++cp_noexcept_operand
;
7533 expr
= cp_parser_expression (parser
);
7534 --cp_noexcept_operand
;
7535 --c_inhibit_evaluation_warnings
;
7536 --cp_unevaluated_operand
;
7538 parser
->greater_than_is_operator_p
7539 = saved_greater_than_is_operator_p
;
7541 parser
->integral_constant_expression_p
7542 = saved_integral_constant_expression_p
;
7543 parser
->non_integral_constant_expression_p
7544 = saved_non_integral_constant_expression_p
;
7546 parser
->type_definition_forbidden_message
= saved_message
;
7548 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7549 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7557 /* Look for the `:: new' and `:: delete', which also signal the
7558 beginning of a new-expression, or delete-expression,
7559 respectively. If the next token is `::', then it might be one of
7561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7565 /* See if the token after the `::' is one of the keywords in
7566 which we're interested. */
7567 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7568 /* If it's `new', we have a new-expression. */
7569 if (keyword
== RID_NEW
)
7570 return cp_parser_new_expression (parser
);
7571 /* Similarly, for `delete'. */
7572 else if (keyword
== RID_DELETE
)
7573 return cp_parser_delete_expression (parser
);
7576 /* Look for a unary operator. */
7577 unary_operator
= cp_parser_unary_operator (token
);
7578 /* The `++' and `--' operators can be handled similarly, even though
7579 they are not technically unary-operators in the grammar. */
7580 if (unary_operator
== ERROR_MARK
)
7582 if (token
->type
== CPP_PLUS_PLUS
)
7583 unary_operator
= PREINCREMENT_EXPR
;
7584 else if (token
->type
== CPP_MINUS_MINUS
)
7585 unary_operator
= PREDECREMENT_EXPR
;
7586 /* Handle the GNU address-of-label extension. */
7587 else if (cp_parser_allow_gnu_extensions_p (parser
)
7588 && token
->type
== CPP_AND_AND
)
7592 location_t loc
= token
->location
;
7594 /* Consume the '&&' token. */
7595 cp_lexer_consume_token (parser
->lexer
);
7596 /* Look for the identifier. */
7597 identifier
= cp_parser_identifier (parser
);
7598 /* Create an expression representing the address. */
7599 expression
= finish_label_address_expr (identifier
, loc
);
7600 if (cp_parser_non_integral_constant_expression (parser
,
7602 expression
= error_mark_node
;
7606 if (unary_operator
!= ERROR_MARK
)
7608 tree cast_expression
;
7609 tree expression
= error_mark_node
;
7610 non_integral_constant non_constant_p
= NIC_NONE
;
7611 location_t loc
= token
->location
;
7612 tsubst_flags_t complain
= complain_flags (decltype_p
);
7614 /* Consume the operator token. */
7615 token
= cp_lexer_consume_token (parser
->lexer
);
7616 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
7618 /* Parse the cast-expression. */
7620 = cp_parser_cast_expression (parser
,
7621 unary_operator
== ADDR_EXPR
,
7625 /* Now, build an appropriate representation. */
7626 switch (unary_operator
)
7629 non_constant_p
= NIC_STAR
;
7630 expression
= build_x_indirect_ref (loc
, cast_expression
,
7636 non_constant_p
= NIC_ADDR
;
7639 expression
= build_x_unary_op (loc
, unary_operator
,
7644 case PREINCREMENT_EXPR
:
7645 case PREDECREMENT_EXPR
:
7646 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7647 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7650 /* Immediately fold negation of a constant, unless the constant is 0
7651 (since -0 == 0) or it would overflow. */
7652 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
7653 && CONSTANT_CLASS_P (cast_expression
)
7654 && !integer_zerop (cast_expression
)
7655 && !TREE_OVERFLOW (cast_expression
))
7657 tree folded
= fold_build1 (unary_operator
,
7658 TREE_TYPE (cast_expression
),
7660 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
7662 expression
= folded
;
7667 case UNARY_PLUS_EXPR
:
7668 case TRUTH_NOT_EXPR
:
7669 expression
= finish_unary_op_expr (loc
, unary_operator
,
7670 cast_expression
, complain
);
7677 if (non_constant_p
!= NIC_NONE
7678 && cp_parser_non_integral_constant_expression (parser
,
7680 expression
= error_mark_node
;
7685 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7686 /*member_access_only_p=*/false,
7691 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7692 unary-operator, the corresponding tree code is returned. */
7694 static enum tree_code
7695 cp_parser_unary_operator (cp_token
* token
)
7697 switch (token
->type
)
7700 return INDIRECT_REF
;
7706 return UNARY_PLUS_EXPR
;
7712 return TRUTH_NOT_EXPR
;
7715 return BIT_NOT_EXPR
;
7722 /* Parse a new-expression.
7725 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7726 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7728 Returns a representation of the expression. */
7731 cp_parser_new_expression (cp_parser
* parser
)
7733 bool global_scope_p
;
7734 vec
<tree
, va_gc
> *placement
;
7736 vec
<tree
, va_gc
> *initializer
;
7737 tree nelts
= NULL_TREE
;
7740 /* Look for the optional `::' operator. */
7742 = (cp_parser_global_scope_opt (parser
,
7743 /*current_scope_valid_p=*/false)
7745 /* Look for the `new' operator. */
7746 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7747 /* There's no easy way to tell a new-placement from the
7748 `( type-id )' construct. */
7749 cp_parser_parse_tentatively (parser
);
7750 /* Look for a new-placement. */
7751 placement
= cp_parser_new_placement (parser
);
7752 /* If that didn't work out, there's no new-placement. */
7753 if (!cp_parser_parse_definitely (parser
))
7755 if (placement
!= NULL
)
7756 release_tree_vector (placement
);
7760 /* If the next token is a `(', then we have a parenthesized
7762 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7765 const char *saved_message
= parser
->type_definition_forbidden_message
;
7767 /* Consume the `('. */
7768 cp_lexer_consume_token (parser
->lexer
);
7770 /* Parse the type-id. */
7771 parser
->type_definition_forbidden_message
7772 = G_("types may not be defined in a new-expression");
7773 type
= cp_parser_type_id (parser
);
7774 parser
->type_definition_forbidden_message
= saved_message
;
7776 /* Look for the closing `)'. */
7777 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7778 token
= cp_lexer_peek_token (parser
->lexer
);
7779 /* There should not be a direct-new-declarator in this production,
7780 but GCC used to allowed this, so we check and emit a sensible error
7781 message for this case. */
7782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7784 error_at (token
->location
,
7785 "array bound forbidden after parenthesized type-id");
7786 inform (token
->location
,
7787 "try removing the parentheses around the type-id");
7788 cp_parser_direct_new_declarator (parser
);
7791 /* Otherwise, there must be a new-type-id. */
7793 type
= cp_parser_new_type_id (parser
, &nelts
);
7795 /* If the next token is a `(' or '{', then we have a new-initializer. */
7796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7797 if (token
->type
== CPP_OPEN_PAREN
7798 || token
->type
== CPP_OPEN_BRACE
)
7799 initializer
= cp_parser_new_initializer (parser
);
7803 /* A new-expression may not appear in an integral constant
7805 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7806 ret
= error_mark_node
;
7807 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7808 of a new-type-id or type-id of a new-expression, the new-expression shall
7809 contain a new-initializer of the form ( assignment-expression )".
7810 Additionally, consistently with the spirit of DR 1467, we want to accept
7811 'new auto { 2 }' too. */
7812 else if (type_uses_auto (type
)
7813 && (vec_safe_length (initializer
) != 1
7814 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
7815 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
7817 error_at (token
->location
,
7818 "initialization of new-expression for type %<auto%> "
7819 "requires exactly one element");
7820 ret
= error_mark_node
;
7824 /* Create a representation of the new-expression. */
7825 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7826 tf_warning_or_error
);
7829 if (placement
!= NULL
)
7830 release_tree_vector (placement
);
7831 if (initializer
!= NULL
)
7832 release_tree_vector (initializer
);
7837 /* Parse a new-placement.
7842 Returns the same representation as for an expression-list. */
7844 static vec
<tree
, va_gc
> *
7845 cp_parser_new_placement (cp_parser
* parser
)
7847 vec
<tree
, va_gc
> *expression_list
;
7849 /* Parse the expression-list. */
7850 expression_list
= (cp_parser_parenthesized_expression_list
7851 (parser
, non_attr
, /*cast_p=*/false,
7852 /*allow_expansion_p=*/true,
7853 /*non_constant_p=*/NULL
));
7855 if (expression_list
&& expression_list
->is_empty ())
7856 error ("expected expression-list or type-id");
7858 return expression_list
;
7861 /* Parse a new-type-id.
7864 type-specifier-seq new-declarator [opt]
7866 Returns the TYPE allocated. If the new-type-id indicates an array
7867 type, *NELTS is set to the number of elements in the last array
7868 bound; the TYPE will not include the last array bound. */
7871 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7873 cp_decl_specifier_seq type_specifier_seq
;
7874 cp_declarator
*new_declarator
;
7875 cp_declarator
*declarator
;
7876 cp_declarator
*outer_declarator
;
7877 const char *saved_message
;
7879 /* The type-specifier sequence must not contain type definitions.
7880 (It cannot contain declarations of new types either, but if they
7881 are not definitions we will catch that because they are not
7883 saved_message
= parser
->type_definition_forbidden_message
;
7884 parser
->type_definition_forbidden_message
7885 = G_("types may not be defined in a new-type-id");
7886 /* Parse the type-specifier-seq. */
7887 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7888 /*is_trailing_return=*/false,
7889 &type_specifier_seq
);
7890 /* Restore the old message. */
7891 parser
->type_definition_forbidden_message
= saved_message
;
7893 if (type_specifier_seq
.type
== error_mark_node
)
7894 return error_mark_node
;
7896 /* Parse the new-declarator. */
7897 new_declarator
= cp_parser_new_declarator_opt (parser
);
7899 /* Determine the number of elements in the last array dimension, if
7902 /* Skip down to the last array dimension. */
7903 declarator
= new_declarator
;
7904 outer_declarator
= NULL
;
7905 while (declarator
&& (declarator
->kind
== cdk_pointer
7906 || declarator
->kind
== cdk_ptrmem
))
7908 outer_declarator
= declarator
;
7909 declarator
= declarator
->declarator
;
7912 && declarator
->kind
== cdk_array
7913 && declarator
->declarator
7914 && declarator
->declarator
->kind
== cdk_array
)
7916 outer_declarator
= declarator
;
7917 declarator
= declarator
->declarator
;
7920 if (declarator
&& declarator
->kind
== cdk_array
)
7922 *nelts
= declarator
->u
.array
.bounds
;
7923 if (*nelts
== error_mark_node
)
7924 *nelts
= integer_one_node
;
7926 if (outer_declarator
)
7927 outer_declarator
->declarator
= declarator
->declarator
;
7929 new_declarator
= NULL
;
7932 return groktypename (&type_specifier_seq
, new_declarator
, false);
7935 /* Parse an (optional) new-declarator.
7938 ptr-operator new-declarator [opt]
7939 direct-new-declarator
7941 Returns the declarator. */
7943 static cp_declarator
*
7944 cp_parser_new_declarator_opt (cp_parser
* parser
)
7946 enum tree_code code
;
7947 tree type
, std_attributes
= NULL_TREE
;
7948 cp_cv_quals cv_quals
;
7950 /* We don't know if there's a ptr-operator next, or not. */
7951 cp_parser_parse_tentatively (parser
);
7952 /* Look for a ptr-operator. */
7953 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7954 /* If that worked, look for more new-declarators. */
7955 if (cp_parser_parse_definitely (parser
))
7957 cp_declarator
*declarator
;
7959 /* Parse another optional declarator. */
7960 declarator
= cp_parser_new_declarator_opt (parser
);
7962 declarator
= cp_parser_make_indirect_declarator
7963 (code
, type
, cv_quals
, declarator
, std_attributes
);
7968 /* If the next token is a `[', there is a direct-new-declarator. */
7969 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7970 return cp_parser_direct_new_declarator (parser
);
7975 /* Parse a direct-new-declarator.
7977 direct-new-declarator:
7979 direct-new-declarator [constant-expression]
7983 static cp_declarator
*
7984 cp_parser_direct_new_declarator (cp_parser
* parser
)
7986 cp_declarator
*declarator
= NULL
;
7993 /* Look for the opening `['. */
7994 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7996 token
= cp_lexer_peek_token (parser
->lexer
);
7997 expression
= cp_parser_expression (parser
);
7998 /* The standard requires that the expression have integral
7999 type. DR 74 adds enumeration types. We believe that the
8000 real intent is that these expressions be handled like the
8001 expression in a `switch' condition, which also allows
8002 classes with a single conversion to integral or
8003 enumeration type. */
8004 if (!processing_template_decl
)
8007 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8012 error_at (token
->location
,
8013 "expression in new-declarator must have integral "
8014 "or enumeration type");
8015 expression
= error_mark_node
;
8019 /* Look for the closing `]'. */
8020 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8022 /* Add this bound to the declarator. */
8023 declarator
= make_array_declarator (declarator
, expression
);
8025 /* If the next token is not a `[', then there are no more
8027 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8034 /* Parse a new-initializer.
8037 ( expression-list [opt] )
8040 Returns a representation of the expression-list. */
8042 static vec
<tree
, va_gc
> *
8043 cp_parser_new_initializer (cp_parser
* parser
)
8045 vec
<tree
, va_gc
> *expression_list
;
8047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8050 bool expr_non_constant_p
;
8051 cp_lexer_set_source_position (parser
->lexer
);
8052 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8053 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8054 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8055 expression_list
= make_tree_vector_single (t
);
8058 expression_list
= (cp_parser_parenthesized_expression_list
8059 (parser
, non_attr
, /*cast_p=*/false,
8060 /*allow_expansion_p=*/true,
8061 /*non_constant_p=*/NULL
));
8063 return expression_list
;
8066 /* Parse a delete-expression.
8069 :: [opt] delete cast-expression
8070 :: [opt] delete [ ] cast-expression
8072 Returns a representation of the expression. */
8075 cp_parser_delete_expression (cp_parser
* parser
)
8077 bool global_scope_p
;
8081 /* Look for the optional `::' operator. */
8083 = (cp_parser_global_scope_opt (parser
,
8084 /*current_scope_valid_p=*/false)
8086 /* Look for the `delete' keyword. */
8087 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
8088 /* See if the array syntax is in use. */
8089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8091 /* Consume the `[' token. */
8092 cp_lexer_consume_token (parser
->lexer
);
8093 /* Look for the `]' token. */
8094 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8095 /* Remember that this is the `[]' construct. */
8101 /* Parse the cast-expression. */
8102 expression
= cp_parser_simple_cast_expression (parser
);
8104 /* A delete-expression may not appear in an integral constant
8106 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
8107 return error_mark_node
;
8109 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
8110 tf_warning_or_error
);
8113 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8114 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8118 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
8120 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8121 switch (token
->type
)
8127 case CPP_CLOSE_SQUARE
:
8128 case CPP_CLOSE_PAREN
:
8129 case CPP_CLOSE_BRACE
:
8130 case CPP_OPEN_BRACE
:
8134 case CPP_DEREF_STAR
:
8142 case CPP_GREATER_EQ
:
8163 case CPP_OPEN_PAREN
:
8164 /* In ((type ()) () the last () isn't a valid cast-expression,
8165 so the whole must be parsed as postfix-expression. */
8166 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
8169 case CPP_OPEN_SQUARE
:
8170 /* '[' may start a primary-expression in obj-c++ and in C++11,
8171 as a lambda-expression, eg, '(void)[]{}'. */
8172 if (cxx_dialect
>= cxx11
)
8174 return c_dialect_objc ();
8177 case CPP_MINUS_MINUS
:
8178 /* '++' and '--' may or may not start a cast-expression:
8180 struct T { void operator++(int); };
8181 void f() { (T())++; }
8194 /* Parse a cast-expression.
8198 ( type-id ) cast-expression
8200 ADDRESS_P is true iff the unary-expression is appearing as the
8201 operand of the `&' operator. CAST_P is true if this expression is
8202 the target of a cast.
8204 Returns a representation of the expression. */
8207 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
8208 bool decltype_p
, cp_id_kind
* pidk
)
8210 /* If it's a `(', then we might be looking at a cast. */
8211 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8213 tree type
= NULL_TREE
;
8214 tree expr
= NULL_TREE
;
8215 int cast_expression
= 0;
8216 const char *saved_message
;
8218 /* There's no way to know yet whether or not this is a cast.
8219 For example, `(int (3))' is a unary-expression, while `(int)
8220 3' is a cast. So, we resort to parsing tentatively. */
8221 cp_parser_parse_tentatively (parser
);
8222 /* Types may not be defined in a cast. */
8223 saved_message
= parser
->type_definition_forbidden_message
;
8224 parser
->type_definition_forbidden_message
8225 = G_("types may not be defined in casts");
8226 /* Consume the `('. */
8227 cp_lexer_consume_token (parser
->lexer
);
8228 /* A very tricky bit is that `(struct S) { 3 }' is a
8229 compound-literal (which we permit in C++ as an extension).
8230 But, that construct is not a cast-expression -- it is a
8231 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8232 is legal; if the compound-literal were a cast-expression,
8233 you'd need an extra set of parentheses.) But, if we parse
8234 the type-id, and it happens to be a class-specifier, then we
8235 will commit to the parse at that point, because we cannot
8236 undo the action that is done when creating a new class. So,
8237 then we cannot back up and do a postfix-expression.
8239 Another tricky case is the following (c++/29234):
8241 struct S { void operator () (); };
8248 As a type-id we parse the parenthesized S()() as a function
8249 returning a function, groktypename complains and we cannot
8250 back up in this case either.
8252 Therefore, we scan ahead to the closing `)', and check to see
8253 if the tokens after the `)' can start a cast-expression. Otherwise
8254 we are dealing with an unary-expression, a postfix-expression
8257 Yet another tricky case, in C++11, is the following (c++/54891):
8261 The issue is that usually, besides the case of lambda-expressions,
8262 the parenthesized type-id cannot be followed by '[', and, eg, we
8263 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8264 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8265 we don't commit, we try a cast-expression, then an unary-expression.
8267 Save tokens so that we can put them back. */
8268 cp_lexer_save_tokens (parser
->lexer
);
8270 /* We may be looking at a cast-expression. */
8271 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8272 /*consume_paren=*/true))
8274 = cp_parser_tokens_start_cast_expression (parser
);
8276 /* Roll back the tokens we skipped. */
8277 cp_lexer_rollback_tokens (parser
->lexer
);
8278 /* If we aren't looking at a cast-expression, simulate an error so
8279 that the call to cp_parser_error_occurred below returns true. */
8280 if (!cast_expression
)
8281 cp_parser_simulate_error (parser
);
8284 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8285 parser
->in_type_id_in_expr_p
= true;
8286 /* Look for the type-id. */
8287 type
= cp_parser_type_id (parser
);
8288 /* Look for the closing `)'. */
8289 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8290 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8293 /* Restore the saved message. */
8294 parser
->type_definition_forbidden_message
= saved_message
;
8296 /* At this point this can only be either a cast or a
8297 parenthesized ctor such as `(T ())' that looks like a cast to
8298 function returning T. */
8299 if (!cp_parser_error_occurred (parser
))
8301 /* Only commit if the cast-expression doesn't start with
8302 '++', '--', or '[' in C++11. */
8303 if (cast_expression
> 0)
8304 cp_parser_commit_to_topmost_tentative_parse (parser
);
8306 expr
= cp_parser_cast_expression (parser
,
8307 /*address_p=*/false,
8309 /*decltype_p=*/false,
8312 if (cp_parser_parse_definitely (parser
))
8314 /* Warn about old-style casts, if so requested. */
8315 if (warn_old_style_cast
8316 && !in_system_header_at (input_location
)
8317 && !VOID_TYPE_P (type
)
8318 && current_lang_name
!= lang_name_c
)
8319 warning (OPT_Wold_style_cast
, "use of old-style cast");
8321 /* Only type conversions to integral or enumeration types
8322 can be used in constant-expressions. */
8323 if (!cast_valid_in_integral_constant_expression_p (type
)
8324 && cp_parser_non_integral_constant_expression (parser
,
8326 return error_mark_node
;
8328 /* Perform the cast. */
8329 expr
= build_c_cast (input_location
, type
, expr
);
8334 cp_parser_abort_tentative_parse (parser
);
8337 /* If we get here, then it's not a cast, so it must be a
8338 unary-expression. */
8339 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8340 cast_p
, decltype_p
);
8343 /* Parse a binary expression of the general form:
8347 pm-expression .* cast-expression
8348 pm-expression ->* cast-expression
8350 multiplicative-expression:
8352 multiplicative-expression * pm-expression
8353 multiplicative-expression / pm-expression
8354 multiplicative-expression % pm-expression
8356 additive-expression:
8357 multiplicative-expression
8358 additive-expression + multiplicative-expression
8359 additive-expression - multiplicative-expression
8363 shift-expression << additive-expression
8364 shift-expression >> additive-expression
8366 relational-expression:
8368 relational-expression < shift-expression
8369 relational-expression > shift-expression
8370 relational-expression <= shift-expression
8371 relational-expression >= shift-expression
8375 relational-expression:
8376 relational-expression <? shift-expression
8377 relational-expression >? shift-expression
8379 equality-expression:
8380 relational-expression
8381 equality-expression == relational-expression
8382 equality-expression != relational-expression
8386 and-expression & equality-expression
8388 exclusive-or-expression:
8390 exclusive-or-expression ^ and-expression
8392 inclusive-or-expression:
8393 exclusive-or-expression
8394 inclusive-or-expression | exclusive-or-expression
8396 logical-and-expression:
8397 inclusive-or-expression
8398 logical-and-expression && inclusive-or-expression
8400 logical-or-expression:
8401 logical-and-expression
8402 logical-or-expression || logical-and-expression
8404 All these are implemented with a single function like:
8407 simple-cast-expression
8408 binary-expression <token> binary-expression
8410 CAST_P is true if this expression is the target of a cast.
8412 The binops_by_token map is used to get the tree codes for each <token> type.
8413 binary-expressions are associated according to a precedence table. */
8415 #define TOKEN_PRECEDENCE(token) \
8416 (((token->type == CPP_GREATER \
8417 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8418 && !parser->greater_than_is_operator_p) \
8419 ? PREC_NOT_OPERATOR \
8420 : binops_by_token[token->type].prec)
8423 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8424 bool no_toplevel_fold_p
,
8426 enum cp_parser_prec prec
,
8429 cp_parser_expression_stack stack
;
8430 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8431 cp_parser_expression_stack_entry current
;
8434 enum tree_code rhs_type
;
8435 enum cp_parser_prec new_prec
, lookahead_prec
;
8438 /* Parse the first expression. */
8439 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8440 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8441 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8442 cast_p
, decltype_p
, pidk
);
8443 current
.prec
= prec
;
8445 if (cp_parser_error_occurred (parser
))
8446 return error_mark_node
;
8450 /* Get an operator token. */
8451 token
= cp_lexer_peek_token (parser
->lexer
);
8453 if (warn_cxx11_compat
8454 && token
->type
== CPP_RSHIFT
8455 && !parser
->greater_than_is_operator_p
)
8457 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8458 "%<>>%> operator is treated"
8459 " as two right angle brackets in C++11"))
8460 inform (token
->location
,
8461 "suggest parentheses around %<>>%> expression");
8464 new_prec
= TOKEN_PRECEDENCE (token
);
8465 if (new_prec
!= PREC_NOT_OPERATOR
8466 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8467 /* This is a fold-expression; handle it later. */
8468 new_prec
= PREC_NOT_OPERATOR
;
8470 /* Popping an entry off the stack means we completed a subexpression:
8471 - either we found a token which is not an operator (`>' where it is not
8472 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8473 will happen repeatedly;
8474 - or, we found an operator which has lower priority. This is the case
8475 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8477 if (new_prec
<= current
.prec
)
8486 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8487 current
.loc
= token
->location
;
8489 /* We used the operator token. */
8490 cp_lexer_consume_token (parser
->lexer
);
8492 /* For "false && x" or "true || x", x will never be executed;
8493 disable warnings while evaluating it. */
8494 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8495 c_inhibit_evaluation_warnings
+=
8496 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8497 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8498 c_inhibit_evaluation_warnings
+=
8499 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8501 /* Extract another operand. It may be the RHS of this expression
8502 or the LHS of a new, higher priority expression. */
8503 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8504 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8505 rhs
= cp_parser_simple_cast_expression (parser
);
8507 /* Get another operator token. Look up its precedence to avoid
8508 building a useless (immediately popped) stack entry for common
8509 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8510 token
= cp_lexer_peek_token (parser
->lexer
);
8511 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8512 if (lookahead_prec
!= PREC_NOT_OPERATOR
8513 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8514 lookahead_prec
= PREC_NOT_OPERATOR
;
8515 if (lookahead_prec
> new_prec
)
8517 /* ... and prepare to parse the RHS of the new, higher priority
8518 expression. Since precedence levels on the stack are
8519 monotonically increasing, we do not have to care about
8524 current
.lhs_type
= rhs_type
;
8525 current
.prec
= new_prec
;
8526 new_prec
= lookahead_prec
;
8530 lookahead_prec
= new_prec
;
8531 /* If the stack is not empty, we have parsed into LHS the right side
8532 (`4' in the example above) of an expression we had suspended.
8533 We can use the information on the stack to recover the LHS (`3')
8534 from the stack together with the tree code (`MULT_EXPR'), and
8535 the precedence of the higher level subexpression
8536 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8537 which will be used to actually build the additive expression. */
8539 rhs_type
= current
.lhs_type
;
8544 /* Undo the disabling of warnings done above. */
8545 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8546 c_inhibit_evaluation_warnings
-=
8547 cp_fully_fold (current
.lhs
) == truthvalue_false_node
;
8548 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8549 c_inhibit_evaluation_warnings
-=
8550 cp_fully_fold (current
.lhs
) == truthvalue_true_node
;
8552 if (warn_logical_not_paren
8553 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8554 && current
.lhs_type
== TRUTH_NOT_EXPR
8555 /* Avoid warning for !!x == y. */
8556 && (TREE_CODE (current
.lhs
) != NE_EXPR
8557 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8558 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8559 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8560 /* Avoid warning for !b == y where b is boolean. */
8561 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
8562 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
8564 /* Avoid warning for !!b == y where b is boolean. */
8565 && (!DECL_P (current
.lhs
)
8566 || TREE_TYPE (current
.lhs
) == NULL_TREE
8567 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
8568 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
8569 maybe_constant_value (rhs
));
8572 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8573 ERROR_MARK for everything that is not a binary expression.
8574 This makes warn_about_parentheses miss some warnings that
8575 involve unary operators. For unary expressions we should
8576 pass the correct tree_code unless the unary expression was
8577 surrounded by parentheses.
8579 if (no_toplevel_fold_p
8580 && lookahead_prec
<= current
.prec
8582 current
.lhs
= build2 (current
.tree_type
,
8583 TREE_CODE_CLASS (current
.tree_type
)
8585 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8588 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8589 current
.lhs
, current
.lhs_type
,
8590 rhs
, rhs_type
, &overload
,
8591 complain_flags (decltype_p
));
8592 current
.lhs_type
= current
.tree_type
;
8593 protected_set_expr_location (current
.lhs
, current
.loc
);
8595 /* If the binary operator required the use of an overloaded operator,
8596 then this expression cannot be an integral constant-expression.
8597 An overloaded operator can be used even if both operands are
8598 otherwise permissible in an integral constant-expression if at
8599 least one of the operands is of enumeration type. */
8602 && cp_parser_non_integral_constant_expression (parser
,
8604 return error_mark_node
;
8611 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8612 bool no_toplevel_fold_p
,
8613 enum cp_parser_prec prec
,
8616 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8617 /*decltype*/false, prec
, pidk
);
8620 /* Parse the `? expression : assignment-expression' part of a
8621 conditional-expression. The LOGICAL_OR_EXPR is the
8622 logical-or-expression that started the conditional-expression.
8623 Returns a representation of the entire conditional-expression.
8625 This routine is used by cp_parser_assignment_expression.
8627 ? expression : assignment-expression
8631 ? : assignment-expression */
8634 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8636 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
8637 tree assignment_expr
;
8638 struct cp_token
*token
;
8639 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8641 /* Consume the `?' token. */
8642 cp_lexer_consume_token (parser
->lexer
);
8643 token
= cp_lexer_peek_token (parser
->lexer
);
8644 if (cp_parser_allow_gnu_extensions_p (parser
)
8645 && token
->type
== CPP_COLON
)
8647 pedwarn (token
->location
, OPT_Wpedantic
,
8648 "ISO C++ does not allow ?: with omitted middle operand");
8649 /* Implicit true clause. */
8651 c_inhibit_evaluation_warnings
+=
8652 folded_logical_or_expr
== truthvalue_true_node
;
8653 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8657 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8658 parser
->colon_corrects_to_scope_p
= false;
8659 /* Parse the expression. */
8660 c_inhibit_evaluation_warnings
+=
8661 folded_logical_or_expr
== truthvalue_false_node
;
8662 expr
= cp_parser_expression (parser
);
8663 c_inhibit_evaluation_warnings
+=
8664 ((folded_logical_or_expr
== truthvalue_true_node
)
8665 - (folded_logical_or_expr
== truthvalue_false_node
));
8666 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8669 /* The next token should be a `:'. */
8670 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8671 /* Parse the assignment-expression. */
8672 assignment_expr
= cp_parser_assignment_expression (parser
);
8673 c_inhibit_evaluation_warnings
-=
8674 folded_logical_or_expr
== truthvalue_true_node
;
8676 /* Build the conditional-expression. */
8677 return build_x_conditional_expr (loc
, logical_or_expr
,
8680 tf_warning_or_error
);
8683 /* Parse an assignment-expression.
8685 assignment-expression:
8686 conditional-expression
8687 logical-or-expression assignment-operator assignment_expression
8690 CAST_P is true if this expression is the target of a cast.
8691 DECLTYPE_P is true if this expression is the operand of decltype.
8693 Returns a representation for the expression. */
8696 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8697 bool cast_p
, bool decltype_p
)
8701 /* If the next token is the `throw' keyword, then we're looking at
8702 a throw-expression. */
8703 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8704 expr
= cp_parser_throw_expression (parser
);
8705 /* Otherwise, it must be that we are looking at a
8706 logical-or-expression. */
8709 /* Parse the binary expressions (logical-or-expression). */
8710 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8712 PREC_NOT_OPERATOR
, pidk
);
8713 /* If the next token is a `?' then we're actually looking at a
8714 conditional-expression. */
8715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8716 return cp_parser_question_colon_clause (parser
, expr
);
8719 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8721 /* If it's an assignment-operator, we're using the second
8723 enum tree_code assignment_operator
8724 = cp_parser_assignment_operator_opt (parser
);
8725 if (assignment_operator
!= ERROR_MARK
)
8727 bool non_constant_p
;
8728 location_t saved_input_location
;
8730 /* Parse the right-hand side of the assignment. */
8731 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8733 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8734 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8736 /* An assignment may not appear in a
8737 constant-expression. */
8738 if (cp_parser_non_integral_constant_expression (parser
,
8740 return error_mark_node
;
8741 /* Build the assignment expression. Its default
8742 location is the location of the '=' token. */
8743 saved_input_location
= input_location
;
8744 input_location
= loc
;
8745 expr
= build_x_modify_expr (loc
, expr
,
8746 assignment_operator
,
8748 complain_flags (decltype_p
));
8749 input_location
= saved_input_location
;
8757 /* Parse an (optional) assignment-operator.
8759 assignment-operator: one of
8760 = *= /= %= += -= >>= <<= &= ^= |=
8764 assignment-operator: one of
8767 If the next token is an assignment operator, the corresponding tree
8768 code is returned, and the token is consumed. For example, for
8769 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8770 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8771 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8772 operator, ERROR_MARK is returned. */
8774 static enum tree_code
8775 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8780 /* Peek at the next token. */
8781 token
= cp_lexer_peek_token (parser
->lexer
);
8783 switch (token
->type
)
8794 op
= TRUNC_DIV_EXPR
;
8798 op
= TRUNC_MOD_EXPR
;
8830 /* Nothing else is an assignment operator. */
8834 /* An operator followed by ... is a fold-expression, handled elsewhere. */
8835 if (op
!= ERROR_MARK
8836 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8839 /* If it was an assignment operator, consume it. */
8840 if (op
!= ERROR_MARK
)
8841 cp_lexer_consume_token (parser
->lexer
);
8846 /* Parse an expression.
8849 assignment-expression
8850 expression , assignment-expression
8852 CAST_P is true if this expression is the target of a cast.
8853 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8854 except possibly parenthesized or on the RHS of a comma (N3276).
8856 Returns a representation of the expression. */
8859 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8860 bool cast_p
, bool decltype_p
)
8862 tree expression
= NULL_TREE
;
8863 location_t loc
= UNKNOWN_LOCATION
;
8867 tree assignment_expression
;
8869 /* Parse the next assignment-expression. */
8870 assignment_expression
8871 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8873 /* We don't create a temporary for a call that is the immediate operand
8874 of decltype or on the RHS of a comma. But when we see a comma, we
8875 need to create a temporary for a call on the LHS. */
8876 if (decltype_p
&& !processing_template_decl
8877 && TREE_CODE (assignment_expression
) == CALL_EXPR
8878 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8879 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8880 assignment_expression
8881 = build_cplus_new (TREE_TYPE (assignment_expression
),
8882 assignment_expression
, tf_warning_or_error
);
8884 /* If this is the first assignment-expression, we can just
8887 expression
= assignment_expression
;
8889 expression
= build_x_compound_expr (loc
, expression
,
8890 assignment_expression
,
8891 complain_flags (decltype_p
));
8892 /* If the next token is not a comma, or we're in a fold-expression, then
8893 we are done with the expression. */
8894 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
8895 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
8897 /* Consume the `,'. */
8898 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8899 cp_lexer_consume_token (parser
->lexer
);
8900 /* A comma operator cannot appear in a constant-expression. */
8901 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8902 expression
= error_mark_node
;
8908 /* Parse a constant-expression.
8910 constant-expression:
8911 conditional-expression
8913 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8914 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8915 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8916 is false, NON_CONSTANT_P should be NULL. */
8919 cp_parser_constant_expression (cp_parser
* parser
,
8920 bool allow_non_constant_p
,
8921 bool *non_constant_p
)
8923 bool saved_integral_constant_expression_p
;
8924 bool saved_allow_non_integral_constant_expression_p
;
8925 bool saved_non_integral_constant_expression_p
;
8928 /* It might seem that we could simply parse the
8929 conditional-expression, and then check to see if it were
8930 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8931 one that the compiler can figure out is constant, possibly after
8932 doing some simplifications or optimizations. The standard has a
8933 precise definition of constant-expression, and we must honor
8934 that, even though it is somewhat more restrictive.
8940 is not a legal declaration, because `(2, 3)' is not a
8941 constant-expression. The `,' operator is forbidden in a
8942 constant-expression. However, GCC's constant-folding machinery
8943 will fold this operation to an INTEGER_CST for `3'. */
8945 /* Save the old settings. */
8946 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8947 saved_allow_non_integral_constant_expression_p
8948 = parser
->allow_non_integral_constant_expression_p
;
8949 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8950 /* We are now parsing a constant-expression. */
8951 parser
->integral_constant_expression_p
= true;
8952 parser
->allow_non_integral_constant_expression_p
8953 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8954 parser
->non_integral_constant_expression_p
= false;
8955 /* Although the grammar says "conditional-expression", we parse an
8956 "assignment-expression", which also permits "throw-expression"
8957 and the use of assignment operators. In the case that
8958 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8959 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8960 actually essential that we look for an assignment-expression.
8961 For example, cp_parser_initializer_clauses uses this function to
8962 determine whether a particular assignment-expression is in fact
8964 expression
= cp_parser_assignment_expression (parser
);
8965 /* Restore the old settings. */
8966 parser
->integral_constant_expression_p
8967 = saved_integral_constant_expression_p
;
8968 parser
->allow_non_integral_constant_expression_p
8969 = saved_allow_non_integral_constant_expression_p
;
8970 if (cxx_dialect
>= cxx11
)
8972 /* Require an rvalue constant expression here; that's what our
8973 callers expect. Reference constant expressions are handled
8974 separately in e.g. cp_parser_template_argument. */
8975 bool is_const
= potential_rvalue_constant_expression (expression
);
8976 parser
->non_integral_constant_expression_p
= !is_const
;
8977 if (!is_const
&& !allow_non_constant_p
)
8978 require_potential_rvalue_constant_expression (expression
);
8980 if (allow_non_constant_p
)
8981 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8982 parser
->non_integral_constant_expression_p
8983 = saved_non_integral_constant_expression_p
;
8988 /* Parse __builtin_offsetof.
8990 offsetof-expression:
8991 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8993 offsetof-member-designator:
8995 | offsetof-member-designator "." id-expression
8996 | offsetof-member-designator "[" expression "]"
8997 | offsetof-member-designator "->" id-expression */
9000 cp_parser_builtin_offsetof (cp_parser
*parser
)
9002 int save_ice_p
, save_non_ice_p
;
9007 /* We're about to accept non-integral-constant things, but will
9008 definitely yield an integral constant expression. Save and
9009 restore these values around our local parsing. */
9010 save_ice_p
= parser
->integral_constant_expression_p
;
9011 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
9013 /* Consume the "__builtin_offsetof" token. */
9014 cp_lexer_consume_token (parser
->lexer
);
9015 /* Consume the opening `('. */
9016 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9017 /* Parse the type-id. */
9018 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9019 type
= cp_parser_type_id (parser
);
9020 /* Look for the `,'. */
9021 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9022 token
= cp_lexer_peek_token (parser
->lexer
);
9024 /* Build the (type *)null that begins the traditional offsetof macro. */
9025 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
9026 tf_warning_or_error
);
9028 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9029 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
9030 true, &dummy
, token
->location
);
9033 token
= cp_lexer_peek_token (parser
->lexer
);
9034 switch (token
->type
)
9036 case CPP_OPEN_SQUARE
:
9037 /* offsetof-member-designator "[" expression "]" */
9038 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
9043 /* offsetof-member-designator "->" identifier */
9044 expr
= grok_array_decl (token
->location
, expr
,
9045 integer_zero_node
, false);
9049 /* offsetof-member-designator "." identifier */
9050 cp_lexer_consume_token (parser
->lexer
);
9051 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
9056 case CPP_CLOSE_PAREN
:
9057 /* Consume the ")" token. */
9058 cp_lexer_consume_token (parser
->lexer
);
9062 /* Error. We know the following require will fail, but
9063 that gives the proper error message. */
9064 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9065 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
9066 expr
= error_mark_node
;
9072 expr
= finish_offsetof (expr
, loc
);
9075 parser
->integral_constant_expression_p
= save_ice_p
;
9076 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
9081 /* Parse a trait expression.
9083 Returns a representation of the expression, the underlying type
9084 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9087 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
9090 tree type1
, type2
= NULL_TREE
;
9091 bool binary
= false;
9092 bool variadic
= false;
9096 case RID_HAS_NOTHROW_ASSIGN
:
9097 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
9099 case RID_HAS_NOTHROW_CONSTRUCTOR
:
9100 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
9102 case RID_HAS_NOTHROW_COPY
:
9103 kind
= CPTK_HAS_NOTHROW_COPY
;
9105 case RID_HAS_TRIVIAL_ASSIGN
:
9106 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
9108 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
9109 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
9111 case RID_HAS_TRIVIAL_COPY
:
9112 kind
= CPTK_HAS_TRIVIAL_COPY
;
9114 case RID_HAS_TRIVIAL_DESTRUCTOR
:
9115 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
9117 case RID_HAS_VIRTUAL_DESTRUCTOR
:
9118 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
9120 case RID_IS_ABSTRACT
:
9121 kind
= CPTK_IS_ABSTRACT
;
9123 case RID_IS_BASE_OF
:
9124 kind
= CPTK_IS_BASE_OF
;
9128 kind
= CPTK_IS_CLASS
;
9131 kind
= CPTK_IS_EMPTY
;
9134 kind
= CPTK_IS_ENUM
;
9137 kind
= CPTK_IS_FINAL
;
9139 case RID_IS_LITERAL_TYPE
:
9140 kind
= CPTK_IS_LITERAL_TYPE
;
9145 case RID_IS_POLYMORPHIC
:
9146 kind
= CPTK_IS_POLYMORPHIC
;
9148 case RID_IS_SAME_AS
:
9149 kind
= CPTK_IS_SAME_AS
;
9152 case RID_IS_STD_LAYOUT
:
9153 kind
= CPTK_IS_STD_LAYOUT
;
9155 case RID_IS_TRIVIAL
:
9156 kind
= CPTK_IS_TRIVIAL
;
9158 case RID_IS_TRIVIALLY_ASSIGNABLE
:
9159 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
9162 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
9163 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
9166 case RID_IS_TRIVIALLY_COPYABLE
:
9167 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
9170 kind
= CPTK_IS_UNION
;
9172 case RID_UNDERLYING_TYPE
:
9173 kind
= CPTK_UNDERLYING_TYPE
;
9178 case RID_DIRECT_BASES
:
9179 kind
= CPTK_DIRECT_BASES
;
9185 /* Consume the token. */
9186 cp_lexer_consume_token (parser
->lexer
);
9188 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9190 type1
= cp_parser_type_id (parser
);
9192 if (type1
== error_mark_node
)
9193 return error_mark_node
;
9197 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9199 type2
= cp_parser_type_id (parser
);
9201 if (type2
== error_mark_node
)
9202 return error_mark_node
;
9206 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9208 cp_lexer_consume_token (parser
->lexer
);
9209 tree elt
= cp_parser_type_id (parser
);
9210 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9212 cp_lexer_consume_token (parser
->lexer
);
9213 elt
= make_pack_expansion (elt
);
9215 if (elt
== error_mark_node
)
9216 return error_mark_node
;
9217 type2
= tree_cons (NULL_TREE
, elt
, type2
);
9221 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9223 /* Complete the trait expression, which may mean either processing
9224 the trait expr now or saving it for template instantiation. */
9227 case CPTK_UNDERLYING_TYPE
:
9228 return finish_underlying_type (type1
);
9230 return finish_bases (type1
, false);
9231 case CPTK_DIRECT_BASES
:
9232 return finish_bases (type1
, true);
9234 return finish_trait_expr (kind
, type1
, type2
);
9238 /* Lambdas that appear in variable initializer or default argument scope
9239 get that in their mangling, so we need to record it. We might as well
9240 use the count for function and namespace scopes as well. */
9241 static GTY(()) tree lambda_scope
;
9242 static GTY(()) int lambda_count
;
9243 struct GTY(()) tree_int
9248 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
9251 start_lambda_scope (tree decl
)
9255 /* Once we're inside a function, we ignore other scopes and just push
9256 the function again so that popping works properly. */
9257 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
9258 decl
= current_function_decl
;
9259 ti
.t
= lambda_scope
;
9260 ti
.i
= lambda_count
;
9261 vec_safe_push (lambda_scope_stack
, ti
);
9262 if (lambda_scope
!= decl
)
9264 /* Don't reset the count if we're still in the same function. */
9265 lambda_scope
= decl
;
9271 record_lambda_scope (tree lambda
)
9273 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
9274 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
9278 finish_lambda_scope (void)
9280 tree_int
*p
= &lambda_scope_stack
->last ();
9281 if (lambda_scope
!= p
->t
)
9283 lambda_scope
= p
->t
;
9284 lambda_count
= p
->i
;
9286 lambda_scope_stack
->pop ();
9289 /* Parse a lambda expression.
9292 lambda-introducer lambda-declarator [opt] compound-statement
9294 Returns a representation of the expression. */
9297 cp_parser_lambda_expression (cp_parser
* parser
)
9299 tree lambda_expr
= build_lambda_expr ();
9302 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9303 cp_token_position start
= 0;
9305 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9307 if (cp_unevaluated_operand
)
9309 if (!token
->error_reported
)
9311 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9312 "lambda-expression in unevaluated context");
9313 token
->error_reported
= true;
9317 else if (parser
->in_template_argument_list_p
)
9319 if (!token
->error_reported
)
9321 error_at (token
->location
, "lambda-expression in template-argument");
9322 token
->error_reported
= true;
9327 /* We may be in the middle of deferred access check. Disable
9329 push_deferring_access_checks (dk_no_deferred
);
9331 cp_parser_lambda_introducer (parser
, lambda_expr
);
9333 type
= begin_lambda_type (lambda_expr
);
9334 if (type
== error_mark_node
)
9335 return error_mark_node
;
9337 record_lambda_scope (lambda_expr
);
9339 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9340 determine_visibility (TYPE_NAME (type
));
9342 /* Now that we've started the type, add the capture fields for any
9343 explicit captures. */
9344 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9347 /* Inside the class, surrounding template-parameter-lists do not apply. */
9348 unsigned int saved_num_template_parameter_lists
9349 = parser
->num_template_parameter_lists
;
9350 unsigned char in_statement
= parser
->in_statement
;
9351 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9352 bool fully_implicit_function_template_p
9353 = parser
->fully_implicit_function_template_p
;
9354 tree implicit_template_parms
= parser
->implicit_template_parms
;
9355 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9356 bool auto_is_implicit_function_template_parm_p
9357 = parser
->auto_is_implicit_function_template_parm_p
;
9359 parser
->num_template_parameter_lists
= 0;
9360 parser
->in_statement
= 0;
9361 parser
->in_switch_statement_p
= false;
9362 parser
->fully_implicit_function_template_p
= false;
9363 parser
->implicit_template_parms
= 0;
9364 parser
->implicit_template_scope
= 0;
9365 parser
->auto_is_implicit_function_template_parm_p
= false;
9367 /* By virtue of defining a local class, a lambda expression has access to
9368 the private variables of enclosing classes. */
9370 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9374 if (!cp_parser_error_occurred (parser
)
9375 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9376 && cp_parser_start_tentative_firewall (parser
))
9378 cp_parser_lambda_body (parser
, lambda_expr
);
9380 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9382 if (cp_parser_skip_to_closing_brace (parser
))
9383 cp_lexer_consume_token (parser
->lexer
);
9386 /* The capture list was built up in reverse order; fix that now. */
9387 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9388 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9391 maybe_add_lambda_conv_op (type
);
9393 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9395 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9396 parser
->in_statement
= in_statement
;
9397 parser
->in_switch_statement_p
= in_switch_statement_p
;
9398 parser
->fully_implicit_function_template_p
9399 = fully_implicit_function_template_p
;
9400 parser
->implicit_template_parms
= implicit_template_parms
;
9401 parser
->implicit_template_scope
= implicit_template_scope
;
9402 parser
->auto_is_implicit_function_template_parm_p
9403 = auto_is_implicit_function_template_parm_p
;
9406 pop_deferring_access_checks ();
9408 /* This field is only used during parsing of the lambda. */
9409 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9411 /* This lambda shouldn't have any proxies left at this point. */
9412 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9413 /* And now that we're done, push proxies for an enclosing lambda. */
9414 insert_pending_capture_proxies ();
9417 lambda_expr
= build_lambda_object (lambda_expr
);
9419 lambda_expr
= error_mark_node
;
9421 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9426 /* Parse the beginning of a lambda expression.
9429 [ lambda-capture [opt] ]
9431 LAMBDA_EXPR is the current representation of the lambda expression. */
9434 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9436 /* Need commas after the first capture. */
9439 /* Eat the leading `['. */
9440 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9442 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9444 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9445 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9446 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9447 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9449 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9451 cp_lexer_consume_token (parser
->lexer
);
9455 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9457 cp_token
* capture_token
;
9459 tree capture_init_expr
;
9460 cp_id_kind idk
= CP_ID_KIND_NONE
;
9461 bool explicit_init_p
= false;
9463 enum capture_kind_type
9468 enum capture_kind_type capture_kind
= BY_COPY
;
9470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9472 error ("expected end of capture-list");
9479 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9481 /* Possibly capture `this'. */
9482 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9484 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9485 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9486 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9487 "with by-copy capture default");
9488 cp_lexer_consume_token (parser
->lexer
);
9489 add_capture (lambda_expr
,
9490 /*id=*/this_identifier
,
9491 /*initializer=*/finish_this_expr(),
9492 /*by_reference_p=*/false,
9497 /* Remember whether we want to capture as a reference or not. */
9498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9500 capture_kind
= BY_REFERENCE
;
9501 cp_lexer_consume_token (parser
->lexer
);
9504 /* Get the identifier. */
9505 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9506 capture_id
= cp_parser_identifier (parser
);
9508 if (capture_id
== error_mark_node
)
9509 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9510 delimiters, but I modified this to stop on unnested ']' as well. It
9511 was already changed to stop on unnested '}', so the
9512 "closing_parenthesis" name is no more misleading with my change. */
9514 cp_parser_skip_to_closing_parenthesis (parser
,
9515 /*recovering=*/true,
9517 /*consume_paren=*/true);
9521 /* Find the initializer for this capture. */
9522 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9523 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9524 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9526 bool direct
, non_constant
;
9527 /* An explicit initializer exists. */
9528 if (cxx_dialect
< cxx14
)
9529 pedwarn (input_location
, 0,
9530 "lambda capture initializers "
9531 "only available with -std=c++14 or -std=gnu++14");
9532 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9534 explicit_init_p
= true;
9535 if (capture_init_expr
== NULL_TREE
)
9537 error ("empty initializer for lambda init-capture");
9538 capture_init_expr
= error_mark_node
;
9543 const char* error_msg
;
9545 /* Turn the identifier into an id-expression. */
9547 = cp_parser_lookup_name_simple (parser
, capture_id
,
9548 capture_token
->location
);
9550 if (capture_init_expr
== error_mark_node
)
9552 unqualified_name_lookup_error (capture_id
);
9555 else if (DECL_P (capture_init_expr
)
9556 && (!VAR_P (capture_init_expr
)
9557 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9559 error_at (capture_token
->location
,
9560 "capture of non-variable %qD ",
9562 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9563 "%q#D declared here", capture_init_expr
);
9566 if (VAR_P (capture_init_expr
)
9567 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9569 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9570 "%qD with non-automatic storage duration",
9572 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
9573 "%q#D declared here", capture_init_expr
);
9578 = finish_id_expression
9583 /*integral_constant_expression_p=*/false,
9584 /*allow_non_integral_constant_expression_p=*/false,
9585 /*non_integral_constant_expression_p=*/NULL
,
9586 /*template_p=*/false,
9588 /*address_p=*/false,
9589 /*template_arg_p=*/false,
9591 capture_token
->location
);
9593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9595 cp_lexer_consume_token (parser
->lexer
);
9596 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9599 check_for_bare_parameter_packs (capture_init_expr
);
9602 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9603 && !explicit_init_p
)
9605 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9606 && capture_kind
== BY_COPY
)
9607 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9608 "of %qD redundant with by-copy capture default",
9610 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9611 && capture_kind
== BY_REFERENCE
)
9612 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9613 "capture of %qD redundant with by-reference capture "
9614 "default", capture_id
);
9617 add_capture (lambda_expr
,
9620 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9624 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9627 /* Parse the (optional) middle of a lambda expression.
9630 < template-parameter-list [opt] >
9631 ( parameter-declaration-clause [opt] )
9632 attribute-specifier [opt]
9634 exception-specification [opt]
9635 lambda-return-type-clause [opt]
9637 LAMBDA_EXPR is the current representation of the lambda expression. */
9640 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9642 /* 5.1.1.4 of the standard says:
9643 If a lambda-expression does not include a lambda-declarator, it is as if
9644 the lambda-declarator were ().
9645 This means an empty parameter list, no attributes, and no exception
9647 tree param_list
= void_list_node
;
9648 tree attributes
= NULL_TREE
;
9649 tree exception_spec
= NULL_TREE
;
9650 tree template_param_list
= NULL_TREE
;
9651 tree tx_qual
= NULL_TREE
;
9653 /* The template-parameter-list is optional, but must begin with
9654 an opening angle if present. */
9655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9657 if (cxx_dialect
< cxx14
)
9658 pedwarn (parser
->lexer
->next_token
->location
, 0,
9659 "lambda templates are only available with "
9660 "-std=c++14 or -std=gnu++14");
9662 cp_lexer_consume_token (parser
->lexer
);
9664 template_param_list
= cp_parser_template_parameter_list (parser
);
9666 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9668 /* We just processed one more parameter list. */
9669 ++parser
->num_template_parameter_lists
;
9672 /* The parameter-declaration-clause is optional (unless
9673 template-parameter-list was given), but must begin with an
9674 opening parenthesis if present. */
9675 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9677 cp_lexer_consume_token (parser
->lexer
);
9679 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9681 /* Parse parameters. */
9682 param_list
= cp_parser_parameter_declaration_clause (parser
);
9684 /* Default arguments shall not be specified in the
9685 parameter-declaration-clause of a lambda-declarator. */
9686 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9687 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9688 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9689 "default argument specified for lambda parameter");
9691 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9693 attributes
= cp_parser_attributes_opt (parser
);
9695 /* Parse optional `mutable' keyword. */
9696 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9698 cp_lexer_consume_token (parser
->lexer
);
9699 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9702 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
9704 /* Parse optional exception specification. */
9705 exception_spec
= cp_parser_exception_specification_opt (parser
);
9707 /* Parse optional trailing return type. */
9708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9710 cp_lexer_consume_token (parser
->lexer
);
9711 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9712 = cp_parser_trailing_type_id (parser
);
9715 /* The function parameters must be in scope all the way until after the
9716 trailing-return-type in case of decltype. */
9717 pop_bindings_and_leave_scope ();
9719 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9720 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9722 /* Create the function call operator.
9724 Messing with declarators like this is no uglier than building up the
9725 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9728 cp_decl_specifier_seq return_type_specs
;
9729 cp_declarator
* declarator
;
9734 clear_decl_specs (&return_type_specs
);
9735 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9736 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9738 /* Maybe we will deduce the return type later. */
9739 return_type_specs
.type
= make_auto ();
9741 p
= obstack_alloc (&declarator_obstack
, 0);
9743 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9746 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9747 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9748 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9749 VIRT_SPEC_UNSPECIFIED
,
9753 /*late_return_type=*/NULL_TREE
,
9754 /*requires_clause*/NULL_TREE
);
9755 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9757 fco
= grokmethod (&return_type_specs
,
9760 if (fco
!= error_mark_node
)
9762 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9763 DECL_ARTIFICIAL (fco
) = 1;
9764 /* Give the object parameter a different name. */
9765 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9766 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9767 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9769 if (template_param_list
)
9771 fco
= finish_member_template_decl (fco
);
9772 finish_template_decl (template_param_list
);
9773 --parser
->num_template_parameter_lists
;
9775 else if (parser
->fully_implicit_function_template_p
)
9776 fco
= finish_fully_implicit_template (parser
, fco
);
9778 finish_member_declaration (fco
);
9780 obstack_free (&declarator_obstack
, p
);
9782 return (fco
!= error_mark_node
);
9786 /* Parse the body of a lambda expression, which is simply
9790 but which requires special handling.
9791 LAMBDA_EXPR is the current representation of the lambda expression. */
9794 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9796 bool nested
= (current_function_decl
!= NULL_TREE
);
9797 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9799 push_function_context ();
9801 /* Still increment function_depth so that we don't GC in the
9802 middle of an expression. */
9804 vec
<tree
> omp_privatization_save
;
9805 save_omp_privatization_clauses (omp_privatization_save
);
9806 /* Clear this in case we're in the middle of a default argument. */
9807 parser
->local_variables_forbidden_p
= false;
9809 /* Finish the function call operator
9811 + late_parsing_for_member
9812 + function_definition_after_declarator
9813 + ctor_initializer_opt_and_function_body */
9815 tree fco
= lambda_function (lambda_expr
);
9821 /* Let the front end know that we are going to be defining this
9823 start_preparsed_function (fco
,
9825 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9827 start_lambda_scope (fco
);
9828 body
= begin_function_body ();
9830 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9833 /* Push the proxies for any explicit captures. */
9834 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9835 cap
= TREE_CHAIN (cap
))
9836 build_capture_proxy (TREE_PURPOSE (cap
));
9838 compound_stmt
= begin_compound_stmt (0);
9840 /* 5.1.1.4 of the standard says:
9841 If a lambda-expression does not include a trailing-return-type, it
9842 is as if the trailing-return-type denotes the following type:
9843 * if the compound-statement is of the form
9844 { return attribute-specifier [opt] expression ; }
9845 the type of the returned expression after lvalue-to-rvalue
9846 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9847 (_conv.array_ 4.2), and function-to-pointer conversion
9849 * otherwise, void. */
9851 /* In a lambda that has neither a lambda-return-type-clause
9852 nor a deducible form, errors should be reported for return statements
9853 in the body. Since we used void as the placeholder return type, parsing
9854 the body as usual will give such desired behavior. */
9855 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9856 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9857 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9859 tree expr
= NULL_TREE
;
9860 cp_id_kind idk
= CP_ID_KIND_NONE
;
9862 /* Parse tentatively in case there's more after the initial return
9864 cp_parser_parse_tentatively (parser
);
9866 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9868 expr
= cp_parser_expression (parser
, &idk
);
9870 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9871 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9873 if (cp_parser_parse_definitely (parser
))
9875 if (!processing_template_decl
)
9877 tree type
= lambda_return_type (expr
);
9878 apply_deduced_return_type (fco
, type
);
9879 if (type
== error_mark_node
)
9880 expr
= error_mark_node
;
9883 /* Will get error here if type not deduced yet. */
9884 finish_return_stmt (expr
);
9892 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9893 cp_parser_label_declaration (parser
);
9894 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9895 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9898 finish_compound_stmt (compound_stmt
);
9901 finish_function_body (body
);
9902 finish_lambda_scope ();
9904 /* Finish the function and generate code for it if necessary. */
9905 tree fn
= finish_function (/*inline*/2);
9907 /* Only expand if the call op is not a template. */
9908 if (!DECL_TEMPLATE_INFO (fco
))
9909 expand_or_defer_fn (fn
);
9912 restore_omp_privatization_clauses (omp_privatization_save
);
9913 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9915 pop_function_context();
9920 /* Statements [gram.stmt.stmt] */
9922 /* Parse a statement.
9926 expression-statement
9931 declaration-statement
9938 attribute-specifier-seq (opt) expression-statement
9939 attribute-specifier-seq (opt) compound-statement
9940 attribute-specifier-seq (opt) selection-statement
9941 attribute-specifier-seq (opt) iteration-statement
9942 attribute-specifier-seq (opt) jump-statement
9943 declaration-statement
9944 attribute-specifier-seq (opt) try-block
9951 IN_COMPOUND is true when the statement is nested inside a
9952 cp_parser_compound_statement; this matters for certain pragmas.
9954 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9955 is a (possibly labeled) if statement which is not enclosed in braces
9956 and has an else clause. This is used to implement -Wparentheses.
9958 CHAIN is a vector of if-else-if conditions. */
9961 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9962 bool in_compound
, bool *if_p
, vec
<tree
> *chain
)
9964 tree statement
, std_attrs
= NULL_TREE
;
9966 location_t statement_location
, attrs_location
;
9971 /* There is no statement yet. */
9972 statement
= NULL_TREE
;
9974 saved_token_sentinel
saved_tokens (parser
->lexer
);
9975 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9976 if (c_dialect_objc ())
9977 /* In obj-c++, seeing '[[' might be the either the beginning of
9978 c++11 attributes, or a nested objc-message-expression. So
9979 let's parse the c++11 attributes tentatively. */
9980 cp_parser_parse_tentatively (parser
);
9981 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9982 if (c_dialect_objc ())
9984 if (!cp_parser_parse_definitely (parser
))
9985 std_attrs
= NULL_TREE
;
9988 /* Peek at the next token. */
9989 token
= cp_lexer_peek_token (parser
->lexer
);
9990 /* Remember the location of the first token in the statement. */
9991 statement_location
= token
->location
;
9992 /* If this is a keyword, then that will often determine what kind of
9993 statement we have. */
9994 if (token
->type
== CPP_KEYWORD
)
9996 enum rid keyword
= token
->keyword
;
10002 /* Looks like a labeled-statement with a case label.
10003 Parse the label, and then use tail recursion to parse
10005 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10006 in_compound
= false;
10011 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
10017 statement
= cp_parser_iteration_statement (parser
, false);
10021 if (!flag_cilkplus
)
10023 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10024 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10025 cp_lexer_consume_token (parser
->lexer
);
10026 statement
= error_mark_node
;
10029 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
10036 statement
= cp_parser_jump_statement (parser
);
10039 case RID_CILK_SYNC
:
10040 cp_lexer_consume_token (parser
->lexer
);
10043 tree sync_expr
= build_cilk_sync ();
10044 SET_EXPR_LOCATION (sync_expr
,
10046 statement
= finish_expr_stmt (sync_expr
);
10050 error_at (token
->location
, "-fcilkplus must be enabled to use"
10051 " %<_Cilk_sync%>");
10052 statement
= error_mark_node
;
10054 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10057 /* Objective-C++ exception-handling constructs. */
10060 case RID_AT_FINALLY
:
10061 case RID_AT_SYNCHRONIZED
:
10063 statement
= cp_parser_objc_statement (parser
);
10067 statement
= cp_parser_try_block (parser
);
10070 case RID_NAMESPACE
:
10071 /* This must be a namespace alias definition. */
10072 cp_parser_declaration_statement (parser
);
10075 case RID_TRANSACTION_ATOMIC
:
10076 case RID_TRANSACTION_RELAXED
:
10077 case RID_SYNCHRONIZED
:
10078 case RID_ATOMIC_NOEXCEPT
:
10079 case RID_ATOMIC_CANCEL
:
10080 statement
= cp_parser_transaction (parser
, token
);
10082 case RID_TRANSACTION_CANCEL
:
10083 statement
= cp_parser_transaction_cancel (parser
);
10087 /* It might be a keyword like `int' that can start a
10088 declaration-statement. */
10092 else if (token
->type
== CPP_NAME
)
10094 /* If the next token is a `:', then we are looking at a
10095 labeled-statement. */
10096 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
10097 if (token
->type
== CPP_COLON
)
10099 /* Looks like a labeled-statement with an ordinary label.
10100 Parse the label, and then use tail recursion to parse
10103 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
10104 in_compound
= false;
10108 /* Anything that starts with a `{' must be a compound-statement. */
10109 else if (token
->type
== CPP_OPEN_BRACE
)
10110 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
10111 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10112 a statement all its own. */
10113 else if (token
->type
== CPP_PRAGMA
)
10115 /* Only certain OpenMP pragmas are attached to statements, and thus
10116 are considered statements themselves. All others are not. In
10117 the context of a compound, accept the pragma as a "statement" and
10118 return so that we can check for a close brace. Otherwise we
10119 require a real statement and must go back and read one. */
10121 cp_parser_pragma (parser
, pragma_compound
);
10122 else if (!cp_parser_pragma (parser
, pragma_stmt
))
10126 else if (token
->type
== CPP_EOF
)
10128 cp_parser_error (parser
, "expected statement");
10132 /* Everything else must be a declaration-statement or an
10133 expression-statement. Try for the declaration-statement
10134 first, unless we are looking at a `;', in which case we know that
10135 we have an expression-statement. */
10138 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10140 if (std_attrs
!= NULL_TREE
)
10142 /* Attributes should be parsed as part of the the
10143 declaration, so let's un-parse them. */
10144 saved_tokens
.rollback();
10145 std_attrs
= NULL_TREE
;
10148 cp_parser_parse_tentatively (parser
);
10149 /* Try to parse the declaration-statement. */
10150 cp_parser_declaration_statement (parser
);
10151 /* If that worked, we're done. */
10152 if (cp_parser_parse_definitely (parser
))
10155 /* Look for an expression-statement instead. */
10156 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
10159 /* Set the line number for the statement. */
10160 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
10161 SET_EXPR_LOCATION (statement
, statement_location
);
10163 /* Note that for now, we don't do anything with c++11 statements
10164 parsed at this level. */
10165 if (std_attrs
!= NULL_TREE
)
10166 warning_at (attrs_location
,
10168 "attributes at the beginning of statement are ignored");
10171 /* Parse the label for a labeled-statement, i.e.
10174 case constant-expression :
10178 case constant-expression ... constant-expression : statement
10180 When a label is parsed without errors, the label is added to the
10181 parse tree by the finish_* functions, so this function doesn't
10182 have to return the label. */
10185 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
10188 tree label
= NULL_TREE
;
10189 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10191 /* The next token should be an identifier. */
10192 token
= cp_lexer_peek_token (parser
->lexer
);
10193 if (token
->type
!= CPP_NAME
10194 && token
->type
!= CPP_KEYWORD
)
10196 cp_parser_error (parser
, "expected labeled-statement");
10200 parser
->colon_corrects_to_scope_p
= false;
10201 switch (token
->keyword
)
10205 tree expr
, expr_hi
;
10206 cp_token
*ellipsis
;
10208 /* Consume the `case' token. */
10209 cp_lexer_consume_token (parser
->lexer
);
10210 /* Parse the constant-expression. */
10211 expr
= cp_parser_constant_expression (parser
);
10212 if (check_for_bare_parameter_packs (expr
))
10213 expr
= error_mark_node
;
10215 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
10216 if (ellipsis
->type
== CPP_ELLIPSIS
)
10218 /* Consume the `...' token. */
10219 cp_lexer_consume_token (parser
->lexer
);
10220 expr_hi
= cp_parser_constant_expression (parser
);
10221 if (check_for_bare_parameter_packs (expr_hi
))
10222 expr_hi
= error_mark_node
;
10224 /* We don't need to emit warnings here, as the common code
10225 will do this for us. */
10228 expr_hi
= NULL_TREE
;
10230 if (parser
->in_switch_statement_p
)
10231 finish_case_label (token
->location
, expr
, expr_hi
);
10233 error_at (token
->location
,
10234 "case label %qE not within a switch statement",
10240 /* Consume the `default' token. */
10241 cp_lexer_consume_token (parser
->lexer
);
10243 if (parser
->in_switch_statement_p
)
10244 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
10246 error_at (token
->location
, "case label not within a switch statement");
10250 /* Anything else must be an ordinary label. */
10251 label
= finish_label_stmt (cp_parser_identifier (parser
));
10255 /* Require the `:' token. */
10256 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10258 /* An ordinary label may optionally be followed by attributes.
10259 However, this is only permitted if the attributes are then
10260 followed by a semicolon. This is because, for backward
10261 compatibility, when parsing
10262 lab: __attribute__ ((unused)) int i;
10263 we want the attribute to attach to "i", not "lab". */
10264 if (label
!= NULL_TREE
10265 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
10268 cp_parser_parse_tentatively (parser
);
10269 attrs
= cp_parser_gnu_attributes_opt (parser
);
10270 if (attrs
== NULL_TREE
10271 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10272 cp_parser_abort_tentative_parse (parser
);
10273 else if (!cp_parser_parse_definitely (parser
))
10276 attributes
= chainon (attributes
, attrs
);
10279 if (attributes
!= NULL_TREE
)
10280 cplus_decl_attributes (&label
, attributes
, 0);
10282 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10285 /* Parse an expression-statement.
10287 expression-statement:
10290 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10291 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10292 indicates whether this expression-statement is part of an
10293 expression statement. */
10296 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
10298 tree statement
= NULL_TREE
;
10299 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10301 /* If the next token is a ';', then there is no expression
10303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10305 statement
= cp_parser_expression (parser
);
10306 if (statement
== error_mark_node
10307 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10309 cp_parser_skip_to_end_of_block_or_statement (parser
);
10310 return error_mark_node
;
10314 /* Give a helpful message for "A<T>::type t;" and the like. */
10315 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
10316 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
10318 if (TREE_CODE (statement
) == SCOPE_REF
)
10319 error_at (token
->location
, "need %<typename%> before %qE because "
10320 "%qT is a dependent scope",
10321 statement
, TREE_OPERAND (statement
, 0));
10322 else if (is_overloaded_fn (statement
)
10323 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10326 tree fn
= get_first_fn (statement
);
10327 error_at (token
->location
,
10328 "%<%T::%D%> names the constructor, not the type",
10329 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10333 /* Consume the final `;'. */
10334 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10336 if (in_statement_expr
10337 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10338 /* This is the final expression statement of a statement
10340 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10341 else if (statement
)
10342 statement
= finish_expr_stmt (statement
);
10347 /* Parse a compound-statement.
10349 compound-statement:
10350 { statement-seq [opt] }
10354 compound-statement:
10355 { label-declaration-seq [opt] statement-seq [opt] }
10357 label-declaration-seq:
10359 label-declaration-seq label-declaration
10361 Returns a tree representing the statement. */
10364 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10365 int bcs_flags
, bool function_body
)
10367 tree compound_stmt
;
10369 /* Consume the `{'. */
10370 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10371 return error_mark_node
;
10372 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10373 && !function_body
&& cxx_dialect
< cxx14
)
10374 pedwarn (input_location
, OPT_Wpedantic
,
10375 "compound-statement in constexpr function");
10376 /* Begin the compound-statement. */
10377 compound_stmt
= begin_compound_stmt (bcs_flags
);
10378 /* If the next keyword is `__label__' we have a label declaration. */
10379 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10380 cp_parser_label_declaration (parser
);
10381 /* Parse an (optional) statement-seq. */
10382 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10383 /* Finish the compound-statement. */
10384 finish_compound_stmt (compound_stmt
);
10385 /* Consume the `}'. */
10386 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10388 return compound_stmt
;
10391 /* Parse an (optional) statement-seq.
10395 statement-seq [opt] statement */
10398 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10400 /* Scan statements until there aren't any more. */
10403 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10405 /* If we are looking at a `}', then we have run out of
10406 statements; the same is true if we have reached the end
10407 of file, or have stumbled upon a stray '@end'. */
10408 if (token
->type
== CPP_CLOSE_BRACE
10409 || token
->type
== CPP_EOF
10410 || token
->type
== CPP_PRAGMA_EOL
10411 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10414 /* If we are in a compound statement and find 'else' then
10415 something went wrong. */
10416 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10418 if (parser
->in_statement
& IN_IF_STMT
)
10422 token
= cp_lexer_consume_token (parser
->lexer
);
10423 error_at (token
->location
, "%<else%> without a previous %<if%>");
10427 /* Parse the statement. */
10428 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10432 /* Parse a selection-statement.
10434 selection-statement:
10435 if ( condition ) statement
10436 if ( condition ) statement else statement
10437 switch ( condition ) statement
10439 Returns the new IF_STMT or SWITCH_STMT.
10441 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10442 is a (possibly labeled) if statement which is not enclosed in
10443 braces and has an else clause. This is used to implement
10446 CHAIN is a vector of if-else-if conditions. This is used to implement
10447 -Wduplicated-cond. */
10450 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
10455 token_indent_info guard_tinfo
;
10460 /* Peek at the next token. */
10461 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10462 guard_tinfo
= get_token_indent_info (token
);
10464 /* See what kind of keyword it is. */
10465 keyword
= token
->keyword
;
10474 /* Look for the `('. */
10475 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10477 cp_parser_skip_to_end_of_statement (parser
);
10478 return error_mark_node
;
10481 /* Begin the selection-statement. */
10482 if (keyword
== RID_IF
)
10483 statement
= begin_if_stmt ();
10485 statement
= begin_switch_stmt ();
10487 /* Parse the condition. */
10488 condition
= cp_parser_condition (parser
);
10489 /* Look for the `)'. */
10490 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10491 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10492 /*consume_paren=*/true);
10494 if (keyword
== RID_IF
)
10497 unsigned char in_statement
;
10499 /* Add the condition. */
10500 finish_if_stmt_cond (condition
, statement
);
10502 if (warn_duplicated_cond
)
10503 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
10506 /* Parse the then-clause. */
10507 in_statement
= parser
->in_statement
;
10508 parser
->in_statement
|= IN_IF_STMT
;
10509 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
10511 parser
->in_statement
= in_statement
;
10513 finish_then_clause (statement
);
10515 /* If the next token is `else', parse the else-clause. */
10516 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10520 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
10521 /* Consume the `else' keyword. */
10522 cp_lexer_consume_token (parser
->lexer
);
10523 if (warn_duplicated_cond
)
10525 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10529 /* We've got "if (COND) else if (COND2)". Start
10530 the condition chain and add COND as the first
10532 chain
= new vec
<tree
> ();
10533 if (!CONSTANT_CLASS_P (condition
)
10534 && !TREE_SIDE_EFFECTS (condition
))
10536 /* Wrap it in a NOP_EXPR so that we can set the
10537 location of the condition. */
10538 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
10540 SET_EXPR_LOCATION (e
, token
->location
);
10541 chain
->safe_push (e
);
10544 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
10547 /* This is if-else without subsequent if. Zap the
10548 condition chain; we would have already warned at
10554 begin_else_clause (statement
);
10555 /* Parse the else-clause. */
10556 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10557 guard_tinfo
, chain
);
10559 finish_else_clause (statement
);
10561 /* If we are currently parsing a then-clause, then
10562 IF_P will not be NULL. We set it to true to
10563 indicate that this if statement has an else clause.
10564 This may trigger the Wparentheses warning below
10565 when we get back up to the parent if statement. */
10571 /* This if statement does not have an else clause. If
10572 NESTED_IF is true, then the then-clause is an if
10573 statement which does have an else clause. We warn
10574 about the potential ambiguity. */
10576 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10577 "suggest explicit braces to avoid ambiguous"
10579 if (warn_duplicated_cond
)
10581 /* We don't need the condition chain anymore. */
10587 /* Now we're all done with the if-statement. */
10588 finish_if_stmt (statement
);
10592 bool in_switch_statement_p
;
10593 unsigned char in_statement
;
10595 /* Add the condition. */
10596 finish_switch_cond (condition
, statement
);
10598 /* Parse the body of the switch-statement. */
10599 in_switch_statement_p
= parser
->in_switch_statement_p
;
10600 in_statement
= parser
->in_statement
;
10601 parser
->in_switch_statement_p
= true;
10602 parser
->in_statement
|= IN_SWITCH_STMT
;
10603 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10605 parser
->in_switch_statement_p
= in_switch_statement_p
;
10606 parser
->in_statement
= in_statement
;
10608 /* Now we're all done with the switch-statement. */
10609 finish_switch_stmt (statement
);
10617 cp_parser_error (parser
, "expected selection-statement");
10618 return error_mark_node
;
10622 /* Parse a condition.
10626 type-specifier-seq declarator = initializer-clause
10627 type-specifier-seq declarator braced-init-list
10632 type-specifier-seq declarator asm-specification [opt]
10633 attributes [opt] = assignment-expression
10635 Returns the expression that should be tested. */
10638 cp_parser_condition (cp_parser
* parser
)
10640 cp_decl_specifier_seq type_specifiers
;
10641 const char *saved_message
;
10642 int declares_class_or_enum
;
10644 /* Try the declaration first. */
10645 cp_parser_parse_tentatively (parser
);
10646 /* New types are not allowed in the type-specifier-seq for a
10648 saved_message
= parser
->type_definition_forbidden_message
;
10649 parser
->type_definition_forbidden_message
10650 = G_("types may not be defined in conditions");
10651 /* Parse the type-specifier-seq. */
10652 cp_parser_decl_specifier_seq (parser
,
10653 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10655 &declares_class_or_enum
);
10656 /* Restore the saved message. */
10657 parser
->type_definition_forbidden_message
= saved_message
;
10658 /* If all is well, we might be looking at a declaration. */
10659 if (!cp_parser_error_occurred (parser
))
10662 tree asm_specification
;
10664 cp_declarator
*declarator
;
10665 tree initializer
= NULL_TREE
;
10667 /* Parse the declarator. */
10668 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10669 /*ctor_dtor_or_conv_p=*/NULL
,
10670 /*parenthesized_p=*/NULL
,
10671 /*member_p=*/false,
10672 /*friend_p=*/false);
10673 /* Parse the attributes. */
10674 attributes
= cp_parser_attributes_opt (parser
);
10675 /* Parse the asm-specification. */
10676 asm_specification
= cp_parser_asm_specification_opt (parser
);
10677 /* If the next token is not an `=' or '{', then we might still be
10678 looking at an expression. For example:
10682 looks like a decl-specifier-seq and a declarator -- but then
10683 there is no `=', so this is an expression. */
10684 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10685 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10686 cp_parser_simulate_error (parser
);
10688 /* If we did see an `=' or '{', then we are looking at a declaration
10690 if (cp_parser_parse_definitely (parser
))
10693 bool non_constant_p
;
10694 bool flags
= LOOKUP_ONLYCONVERTING
;
10696 /* Create the declaration. */
10697 decl
= start_decl (declarator
, &type_specifiers
,
10698 /*initialized_p=*/true,
10699 attributes
, /*prefix_attributes=*/NULL_TREE
,
10702 /* Parse the initializer. */
10703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10705 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10706 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10711 /* Consume the `='. */
10712 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10713 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10715 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10716 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10718 /* Process the initializer. */
10719 cp_finish_decl (decl
,
10720 initializer
, !non_constant_p
,
10725 pop_scope (pushed_scope
);
10727 return convert_from_reference (decl
);
10730 /* If we didn't even get past the declarator successfully, we are
10731 definitely not looking at a declaration. */
10733 cp_parser_abort_tentative_parse (parser
);
10735 /* Otherwise, we are looking at an expression. */
10736 return cp_parser_expression (parser
);
10739 /* Parses a for-statement or range-for-statement until the closing ')',
10743 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10745 tree init
, scope
, decl
;
10748 /* Begin the for-statement. */
10749 scope
= begin_for_scope (&init
);
10751 /* Parse the initialization. */
10752 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10755 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10757 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10761 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10763 /* Normal for loop */
10764 tree condition
= NULL_TREE
;
10765 tree expression
= NULL_TREE
;
10768 stmt
= begin_for_stmt (scope
, init
);
10769 /* The for-init-statement has already been parsed in
10770 cp_parser_for_init_statement, so no work is needed here. */
10771 finish_for_init_stmt (stmt
);
10773 /* If there's a condition, process it. */
10774 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10775 condition
= cp_parser_condition (parser
);
10778 cp_parser_error (parser
, "missing loop condition in loop with "
10779 "%<GCC ivdep%> pragma");
10780 condition
= error_mark_node
;
10782 finish_for_cond (condition
, stmt
, ivdep
);
10783 /* Look for the `;'. */
10784 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10786 /* If there's an expression, process it. */
10787 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10788 expression
= cp_parser_expression (parser
);
10789 finish_for_expr (expression
, stmt
);
10794 /* Tries to parse a range-based for-statement:
10797 decl-specifier-seq declarator : expression
10799 The decl-specifier-seq declarator and the `:' are already parsed by
10800 cp_parser_for_init_statement. If processing_template_decl it returns a
10801 newly created RANGE_FOR_STMT; if not, it is converted to a
10802 regular FOR_STMT. */
10805 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10808 tree stmt
, range_expr
;
10810 /* Get the range declaration momentarily out of the way so that
10811 the range expression doesn't clash with it. */
10812 if (range_decl
!= error_mark_node
)
10813 pop_binding (DECL_NAME (range_decl
), range_decl
);
10815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10817 bool expr_non_constant_p
;
10818 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10821 range_expr
= cp_parser_expression (parser
);
10823 /* Put the range declaration back into scope. */
10824 if (range_decl
!= error_mark_node
)
10825 push_binding (DECL_NAME (range_decl
), range_decl
, current_binding_level
);
10827 /* If in template, STMT is converted to a normal for-statement
10828 at instantiation. If not, it is done just ahead. */
10829 if (processing_template_decl
)
10831 if (check_for_bare_parameter_packs (range_expr
))
10832 range_expr
= error_mark_node
;
10833 stmt
= begin_range_for_stmt (scope
, init
);
10835 RANGE_FOR_IVDEP (stmt
) = 1;
10836 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10837 if (!type_dependent_expression_p (range_expr
)
10838 /* do_auto_deduction doesn't mess with template init-lists. */
10839 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10840 do_range_for_auto_deduction (range_decl
, range_expr
);
10844 stmt
= begin_for_stmt (scope
, init
);
10845 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10850 /* Subroutine of cp_convert_range_for: given the initializer expression,
10851 builds up the range temporary. */
10854 build_range_temp (tree range_expr
)
10856 tree range_type
, range_temp
;
10858 /* Find out the type deduced by the declaration
10859 `auto &&__range = range_expr'. */
10860 range_type
= cp_build_reference_type (make_auto (), true);
10861 range_type
= do_auto_deduction (range_type
, range_expr
,
10862 type_uses_auto (range_type
));
10864 /* Create the __range variable. */
10865 range_temp
= build_decl (input_location
, VAR_DECL
,
10866 get_identifier ("__for_range"), range_type
);
10867 TREE_USED (range_temp
) = 1;
10868 DECL_ARTIFICIAL (range_temp
) = 1;
10873 /* Used by cp_parser_range_for in template context: we aren't going to
10874 do a full conversion yet, but we still need to resolve auto in the
10875 type of the for-range-declaration if present. This is basically
10876 a shortcut version of cp_convert_range_for. */
10879 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10881 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10884 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10885 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10886 iter_type
= (cp_parser_perform_range_for_lookup
10887 (range_temp
, &begin_dummy
, &end_dummy
));
10890 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10892 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10893 tf_warning_or_error
);
10894 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10895 iter_decl
, auto_node
);
10900 /* Converts a range-based for-statement into a normal
10901 for-statement, as per the definition.
10903 for (RANGE_DECL : RANGE_EXPR)
10906 should be equivalent to:
10909 auto &&__range = RANGE_EXPR;
10910 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10914 RANGE_DECL = *__begin;
10919 If RANGE_EXPR is an array:
10920 BEGIN_EXPR = __range
10921 END_EXPR = __range + ARRAY_SIZE(__range)
10922 Else if RANGE_EXPR has a member 'begin' or 'end':
10923 BEGIN_EXPR = __range.begin()
10924 END_EXPR = __range.end()
10926 BEGIN_EXPR = begin(__range)
10927 END_EXPR = end(__range);
10929 If __range has a member 'begin' but not 'end', or vice versa, we must
10930 still use the second alternative (it will surely fail, however).
10931 When calling begin()/end() in the third alternative we must use
10932 argument dependent lookup, but always considering 'std' as an associated
10936 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10940 tree iter_type
, begin_expr
, end_expr
;
10941 tree condition
, expression
;
10943 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10944 /* If an error happened previously do nothing or else a lot of
10945 unhelpful errors would be issued. */
10946 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10951 if (VAR_P (range_expr
)
10952 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10953 /* Can't bind a reference to an array of runtime bound. */
10954 range_temp
= range_expr
;
10957 range_temp
= build_range_temp (range_expr
);
10958 pushdecl (range_temp
);
10959 cp_finish_decl (range_temp
, range_expr
,
10960 /*is_constant_init*/false, NULL_TREE
,
10961 LOOKUP_ONLYCONVERTING
);
10962 range_temp
= convert_from_reference (range_temp
);
10964 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10965 &begin_expr
, &end_expr
);
10968 /* The new for initialization statement. */
10969 begin
= build_decl (input_location
, VAR_DECL
,
10970 get_identifier ("__for_begin"), iter_type
);
10971 TREE_USED (begin
) = 1;
10972 DECL_ARTIFICIAL (begin
) = 1;
10974 cp_finish_decl (begin
, begin_expr
,
10975 /*is_constant_init*/false, NULL_TREE
,
10976 LOOKUP_ONLYCONVERTING
);
10978 end
= build_decl (input_location
, VAR_DECL
,
10979 get_identifier ("__for_end"), iter_type
);
10980 TREE_USED (end
) = 1;
10981 DECL_ARTIFICIAL (end
) = 1;
10983 cp_finish_decl (end
, end_expr
,
10984 /*is_constant_init*/false, NULL_TREE
,
10985 LOOKUP_ONLYCONVERTING
);
10987 finish_for_init_stmt (statement
);
10989 /* The new for condition. */
10990 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10993 NULL
, tf_warning_or_error
);
10994 finish_for_cond (condition
, statement
, ivdep
);
10996 /* The new increment expression. */
10997 expression
= finish_unary_op_expr (input_location
,
10998 PREINCREMENT_EXPR
, begin
,
10999 tf_warning_or_error
);
11000 finish_for_expr (expression
, statement
);
11002 /* The declaration is initialized with *__begin inside the loop body. */
11003 cp_finish_decl (range_decl
,
11004 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
11005 tf_warning_or_error
),
11006 /*is_constant_init*/false, NULL_TREE
,
11007 LOOKUP_ONLYCONVERTING
);
11012 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11013 We need to solve both at the same time because the method used
11014 depends on the existence of members begin or end.
11015 Returns the type deduced for the iterator expression. */
11018 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
11020 if (error_operand_p (range
))
11022 *begin
= *end
= error_mark_node
;
11023 return error_mark_node
;
11026 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
11028 error ("range-based %<for%> expression of type %qT "
11029 "has incomplete type", TREE_TYPE (range
));
11030 *begin
= *end
= error_mark_node
;
11031 return error_mark_node
;
11033 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
11035 /* If RANGE is an array, we will use pointer arithmetic. */
11037 *end
= build_binary_op (input_location
, PLUS_EXPR
,
11039 array_type_nelts_top (TREE_TYPE (range
)),
11041 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
11045 /* If it is not an array, we must do a bit of magic. */
11046 tree id_begin
, id_end
;
11047 tree member_begin
, member_end
;
11049 *begin
= *end
= error_mark_node
;
11051 id_begin
= get_identifier ("begin");
11052 id_end
= get_identifier ("end");
11053 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
11054 /*protect=*/2, /*want_type=*/false,
11055 tf_warning_or_error
);
11056 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
11057 /*protect=*/2, /*want_type=*/false,
11058 tf_warning_or_error
);
11060 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
11062 /* Use the member functions. */
11063 if (member_begin
!= NULL_TREE
)
11064 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
11066 error ("range-based %<for%> expression of type %qT has an "
11067 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
11069 if (member_end
!= NULL_TREE
)
11070 *end
= cp_parser_range_for_member_function (range
, id_end
);
11072 error ("range-based %<for%> expression of type %qT has a "
11073 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
11077 /* Use global functions with ADL. */
11078 vec
<tree
, va_gc
> *vec
;
11079 vec
= make_tree_vector ();
11081 vec_safe_push (vec
, range
);
11083 member_begin
= perform_koenig_lookup (id_begin
, vec
,
11084 tf_warning_or_error
);
11085 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
11086 tf_warning_or_error
);
11087 member_end
= perform_koenig_lookup (id_end
, vec
,
11088 tf_warning_or_error
);
11089 *end
= finish_call_expr (member_end
, &vec
, false, true,
11090 tf_warning_or_error
);
11092 release_tree_vector (vec
);
11095 /* Last common checks. */
11096 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
11098 /* If one of the expressions is an error do no more checks. */
11099 *begin
= *end
= error_mark_node
;
11100 return error_mark_node
;
11102 else if (type_dependent_expression_p (*begin
)
11103 || type_dependent_expression_p (*end
))
11104 /* Can happen, when, eg, in a template context, Koenig lookup
11105 can't resolve begin/end (c++/58503). */
11109 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
11110 /* The unqualified type of the __begin and __end temporaries should
11111 be the same, as required by the multiple auto declaration. */
11112 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
11113 error ("inconsistent begin/end types in range-based %<for%> "
11114 "statement: %qT and %qT",
11115 TREE_TYPE (*begin
), TREE_TYPE (*end
));
11121 /* Helper function for cp_parser_perform_range_for_lookup.
11122 Builds a tree for RANGE.IDENTIFIER(). */
11125 cp_parser_range_for_member_function (tree range
, tree identifier
)
11128 vec
<tree
, va_gc
> *vec
;
11130 member
= finish_class_member_access_expr (range
, identifier
,
11131 false, tf_warning_or_error
);
11132 if (member
== error_mark_node
)
11133 return error_mark_node
;
11135 vec
= make_tree_vector ();
11136 res
= finish_call_expr (member
, &vec
,
11137 /*disallow_virtual=*/false,
11138 /*koenig_p=*/false,
11139 tf_warning_or_error
);
11140 release_tree_vector (vec
);
11144 /* Parse an iteration-statement.
11146 iteration-statement:
11147 while ( condition ) statement
11148 do statement while ( expression ) ;
11149 for ( for-init-statement condition [opt] ; expression [opt] )
11152 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11155 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
11160 unsigned char in_statement
;
11161 token_indent_info guard_tinfo
;
11163 /* Peek at the next token. */
11164 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
11166 return error_mark_node
;
11168 guard_tinfo
= get_token_indent_info (token
);
11170 /* Remember whether or not we are already within an iteration
11172 in_statement
= parser
->in_statement
;
11174 /* See what kind of keyword it is. */
11175 keyword
= token
->keyword
;
11182 /* Begin the while-statement. */
11183 statement
= begin_while_stmt ();
11184 /* Look for the `('. */
11185 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11186 /* Parse the condition. */
11187 condition
= cp_parser_condition (parser
);
11188 finish_while_stmt_cond (condition
, statement
, ivdep
);
11189 /* Look for the `)'. */
11190 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11191 /* Parse the dependent statement. */
11192 parser
->in_statement
= IN_ITERATION_STMT
;
11193 cp_parser_already_scoped_statement (parser
, guard_tinfo
);
11194 parser
->in_statement
= in_statement
;
11195 /* We're done with the while-statement. */
11196 finish_while_stmt (statement
);
11204 /* Begin the do-statement. */
11205 statement
= begin_do_stmt ();
11206 /* Parse the body of the do-statement. */
11207 parser
->in_statement
= IN_ITERATION_STMT
;
11208 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
11209 parser
->in_statement
= in_statement
;
11210 finish_do_body (statement
);
11211 /* Look for the `while' keyword. */
11212 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
11213 /* Look for the `('. */
11214 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11215 /* Parse the expression. */
11216 expression
= cp_parser_expression (parser
);
11217 /* We're done with the do-statement. */
11218 finish_do_stmt (expression
, statement
, ivdep
);
11219 /* Look for the `)'. */
11220 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11221 /* Look for the `;'. */
11222 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11228 /* Look for the `('. */
11229 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11231 statement
= cp_parser_for (parser
, ivdep
);
11233 /* Look for the `)'. */
11234 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
11236 /* Parse the body of the for-statement. */
11237 parser
->in_statement
= IN_ITERATION_STMT
;
11238 cp_parser_already_scoped_statement (parser
, guard_tinfo
);
11239 parser
->in_statement
= in_statement
;
11241 /* We're done with the for-statement. */
11242 finish_for_stmt (statement
);
11247 cp_parser_error (parser
, "expected iteration-statement");
11248 statement
= error_mark_node
;
11255 /* Parse a for-init-statement or the declarator of a range-based-for.
11256 Returns true if a range-based-for declaration is seen.
11258 for-init-statement:
11259 expression-statement
11260 simple-declaration */
11263 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
11265 /* If the next token is a `;', then we have an empty
11266 expression-statement. Grammatically, this is also a
11267 simple-declaration, but an invalid one, because it does not
11268 declare anything. Therefore, if we did not handle this case
11269 specially, we would issue an error message about an invalid
11271 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11273 bool is_range_for
= false;
11274 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11276 /* A colon is used in range-based for. */
11277 parser
->colon_corrects_to_scope_p
= false;
11279 /* We're going to speculatively look for a declaration, falling back
11280 to an expression, if necessary. */
11281 cp_parser_parse_tentatively (parser
);
11282 /* Parse the declaration. */
11283 cp_parser_simple_declaration (parser
,
11284 /*function_definition_allowed_p=*/false,
11286 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11287 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11289 /* It is a range-for, consume the ':' */
11290 cp_lexer_consume_token (parser
->lexer
);
11291 is_range_for
= true;
11292 if (cxx_dialect
< cxx11
)
11294 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11295 "range-based %<for%> loops only available with "
11296 "-std=c++11 or -std=gnu++11");
11297 *decl
= error_mark_node
;
11301 /* The ';' is not consumed yet because we told
11302 cp_parser_simple_declaration not to. */
11303 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11305 if (cp_parser_parse_definitely (parser
))
11306 return is_range_for
;
11307 /* If the tentative parse failed, then we shall need to look for an
11308 expression-statement. */
11310 /* If we are here, it is an expression-statement. */
11311 cp_parser_expression_statement (parser
, NULL_TREE
);
11315 /* Parse a jump-statement.
11320 return expression [opt] ;
11321 return braced-init-list ;
11327 goto * expression ;
11329 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11332 cp_parser_jump_statement (cp_parser
* parser
)
11334 tree statement
= error_mark_node
;
11337 unsigned char in_statement
;
11339 /* Peek at the next token. */
11340 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
11342 return error_mark_node
;
11344 /* See what kind of keyword it is. */
11345 keyword
= token
->keyword
;
11349 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11350 switch (in_statement
)
11353 error_at (token
->location
, "break statement not within loop or switch");
11356 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11357 || in_statement
== IN_ITERATION_STMT
);
11358 statement
= finish_break_stmt ();
11359 if (in_statement
== IN_ITERATION_STMT
)
11360 break_maybe_infinite_loop ();
11363 error_at (token
->location
, "invalid exit from OpenMP structured block");
11366 error_at (token
->location
, "break statement used with OpenMP for loop");
11368 case IN_CILK_SIMD_FOR
:
11369 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11372 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11376 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11379 error_at (token
->location
, "continue statement not within a loop");
11381 case IN_CILK_SIMD_FOR
:
11382 error_at (token
->location
,
11383 "continue statement within %<#pragma simd%> loop body");
11384 /* Fall through. */
11385 case IN_ITERATION_STMT
:
11387 statement
= finish_continue_stmt ();
11390 error_at (token
->location
, "invalid exit from OpenMP structured block");
11393 gcc_unreachable ();
11395 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11401 bool expr_non_constant_p
;
11403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11405 cp_lexer_set_source_position (parser
->lexer
);
11406 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11407 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11409 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11410 expr
= cp_parser_expression (parser
);
11412 /* If the next token is a `;', then there is no
11415 /* Build the return-statement. */
11416 statement
= finish_return_stmt (expr
);
11417 /* Look for the final `;'. */
11418 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11423 if (parser
->in_function_body
11424 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11426 error ("%<goto%> in %<constexpr%> function");
11427 cp_function_chain
->invalid_constexpr
= true;
11430 /* Create the goto-statement. */
11431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11433 /* Issue a warning about this use of a GNU extension. */
11434 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11435 /* Consume the '*' token. */
11436 cp_lexer_consume_token (parser
->lexer
);
11437 /* Parse the dependent expression. */
11438 finish_goto_stmt (cp_parser_expression (parser
));
11441 finish_goto_stmt (cp_parser_identifier (parser
));
11442 /* Look for the final `;'. */
11443 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11447 cp_parser_error (parser
, "expected jump-statement");
11454 /* Parse a declaration-statement.
11456 declaration-statement:
11457 block-declaration */
11460 cp_parser_declaration_statement (cp_parser
* parser
)
11464 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11465 p
= obstack_alloc (&declarator_obstack
, 0);
11467 /* Parse the block-declaration. */
11468 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11470 /* Free any declarators allocated. */
11471 obstack_free (&declarator_obstack
, p
);
11474 /* Some dependent statements (like `if (cond) statement'), are
11475 implicitly in their own scope. In other words, if the statement is
11476 a single statement (as opposed to a compound-statement), it is
11477 none-the-less treated as if it were enclosed in braces. Any
11478 declarations appearing in the dependent statement are out of scope
11479 after control passes that point. This function parses a statement,
11480 but ensures that is in its own scope, even if it is not a
11481 compound-statement.
11483 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11484 is a (possibly labeled) if statement which is not enclosed in
11485 braces and has an else clause. This is used to implement
11488 CHAIN is a vector of if-else-if conditions. This is used to implement
11491 Returns the new statement. */
11494 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
11495 const token_indent_info
&guard_tinfo
,
11499 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11500 token_indent_info body_tinfo
11501 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11506 /* Mark if () ; with a special NOP_EXPR. */
11507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11509 cp_lexer_consume_token (parser
->lexer
);
11510 statement
= add_stmt (build_empty_stmt (body_loc
));
11512 if (guard_tinfo
.keyword
== RID_IF
11513 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
11514 warning_at (body_loc
, OPT_Wempty_body
,
11515 "suggest braces around empty body in an %<if%> statement");
11516 else if (guard_tinfo
.keyword
== RID_ELSE
)
11517 warning_at (body_loc
, OPT_Wempty_body
,
11518 "suggest braces around empty body in an %<else%> statement");
11520 /* if a compound is opened, we simply parse the statement directly. */
11521 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11522 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11523 /* If the token is not a `{', then we must take special action. */
11526 /* Create a compound-statement. */
11527 statement
= begin_compound_stmt (0);
11528 /* Parse the dependent-statement. */
11529 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
);
11530 /* Finish the dummy compound-statement. */
11531 finish_compound_stmt (statement
);
11534 token_indent_info next_tinfo
11535 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11536 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11538 /* Return the statement. */
11542 /* For some dependent statements (like `while (cond) statement'), we
11543 have already created a scope. Therefore, even if the dependent
11544 statement is a compound-statement, we do not want to create another
11548 cp_parser_already_scoped_statement (cp_parser
* parser
,
11549 const token_indent_info
&guard_tinfo
)
11551 /* If the token is a `{', then we must take special action. */
11552 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11554 token_indent_info body_tinfo
11555 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11557 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11558 token_indent_info next_tinfo
11559 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11560 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
11564 /* Avoid calling cp_parser_compound_statement, so that we
11565 don't create a new scope. Do everything else by hand. */
11566 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11567 /* If the next keyword is `__label__' we have a label declaration. */
11568 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11569 cp_parser_label_declaration (parser
);
11570 /* Parse an (optional) statement-seq. */
11571 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11572 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11576 /* Declarations [gram.dcl.dcl] */
11578 /* Parse an optional declaration-sequence.
11582 declaration-seq declaration */
11585 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11591 token
= cp_lexer_peek_token (parser
->lexer
);
11593 if (token
->type
== CPP_CLOSE_BRACE
11594 || token
->type
== CPP_EOF
11595 || token
->type
== CPP_PRAGMA_EOL
)
11598 if (token
->type
== CPP_SEMICOLON
)
11600 /* A declaration consisting of a single semicolon is
11601 invalid. Allow it unless we're being pedantic. */
11602 cp_lexer_consume_token (parser
->lexer
);
11603 if (!in_system_header_at (input_location
))
11604 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11608 /* If we're entering or exiting a region that's implicitly
11609 extern "C", modify the lang context appropriately. */
11610 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11612 push_lang_context (lang_name_c
);
11613 parser
->implicit_extern_c
= true;
11615 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11617 pop_lang_context ();
11618 parser
->implicit_extern_c
= false;
11621 if (token
->type
== CPP_PRAGMA
)
11623 /* A top-level declaration can consist solely of a #pragma.
11624 A nested declaration cannot, so this is done here and not
11625 in cp_parser_declaration. (A #pragma at block scope is
11626 handled in cp_parser_statement.) */
11627 cp_parser_pragma (parser
, pragma_external
);
11631 /* Parse the declaration itself. */
11632 cp_parser_declaration (parser
);
11636 /* Parse a declaration.
11640 function-definition
11641 template-declaration
11642 explicit-instantiation
11643 explicit-specialization
11644 linkage-specification
11645 namespace-definition
11650 __extension__ declaration */
11653 cp_parser_declaration (cp_parser
* parser
)
11657 int saved_pedantic
;
11659 tree attributes
= NULL_TREE
;
11661 /* Check for the `__extension__' keyword. */
11662 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11664 /* Parse the qualified declaration. */
11665 cp_parser_declaration (parser
);
11666 /* Restore the PEDANTIC flag. */
11667 pedantic
= saved_pedantic
;
11672 /* Try to figure out what kind of declaration is present. */
11673 token1
= *cp_lexer_peek_token (parser
->lexer
);
11675 if (token1
.type
!= CPP_EOF
)
11676 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11679 token2
.type
= CPP_EOF
;
11680 token2
.keyword
= RID_MAX
;
11683 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11684 p
= obstack_alloc (&declarator_obstack
, 0);
11686 /* If the next token is `extern' and the following token is a string
11687 literal, then we have a linkage specification. */
11688 if (token1
.keyword
== RID_EXTERN
11689 && cp_parser_is_pure_string_literal (&token2
))
11690 cp_parser_linkage_specification (parser
);
11691 /* If the next token is `template', then we have either a template
11692 declaration, an explicit instantiation, or an explicit
11694 else if (token1
.keyword
== RID_TEMPLATE
)
11696 /* `template <>' indicates a template specialization. */
11697 if (token2
.type
== CPP_LESS
11698 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11699 cp_parser_explicit_specialization (parser
);
11700 /* `template <' indicates a template declaration. */
11701 else if (token2
.type
== CPP_LESS
)
11702 cp_parser_template_declaration (parser
, /*member_p=*/false);
11703 /* Anything else must be an explicit instantiation. */
11705 cp_parser_explicit_instantiation (parser
);
11707 /* If the next token is `export', then we have a template
11709 else if (token1
.keyword
== RID_EXPORT
)
11710 cp_parser_template_declaration (parser
, /*member_p=*/false);
11711 /* If the next token is `extern', 'static' or 'inline' and the one
11712 after that is `template', we have a GNU extended explicit
11713 instantiation directive. */
11714 else if (cp_parser_allow_gnu_extensions_p (parser
)
11715 && (token1
.keyword
== RID_EXTERN
11716 || token1
.keyword
== RID_STATIC
11717 || token1
.keyword
== RID_INLINE
)
11718 && token2
.keyword
== RID_TEMPLATE
)
11719 cp_parser_explicit_instantiation (parser
);
11720 /* If the next token is `namespace', check for a named or unnamed
11721 namespace definition. */
11722 else if (token1
.keyword
== RID_NAMESPACE
11723 && (/* A named namespace definition. */
11724 (token2
.type
== CPP_NAME
11725 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11727 || (token2
.type
== CPP_OPEN_SQUARE
11728 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11729 == CPP_OPEN_SQUARE
)
11730 /* An unnamed namespace definition. */
11731 || token2
.type
== CPP_OPEN_BRACE
11732 || token2
.keyword
== RID_ATTRIBUTE
))
11733 cp_parser_namespace_definition (parser
);
11734 /* An inline (associated) namespace definition. */
11735 else if (token1
.keyword
== RID_INLINE
11736 && token2
.keyword
== RID_NAMESPACE
)
11737 cp_parser_namespace_definition (parser
);
11738 /* Objective-C++ declaration/definition. */
11739 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11740 cp_parser_objc_declaration (parser
, NULL_TREE
);
11741 else if (c_dialect_objc ()
11742 && token1
.keyword
== RID_ATTRIBUTE
11743 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11744 cp_parser_objc_declaration (parser
, attributes
);
11745 /* At this point we may have a template declared by a concept
11747 else if (flag_concepts
11748 && cp_parser_template_declaration_after_export (parser
,
11749 /*member_p=*/false))
11752 /* Try to parse a block-declaration, or a function-definition. */
11753 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11755 /* Free any declarators allocated. */
11756 obstack_free (&declarator_obstack
, p
);
11759 /* Parse a block-declaration.
11764 namespace-alias-definition
11771 __extension__ block-declaration
11776 static_assert-declaration
11778 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11779 part of a declaration-statement. */
11782 cp_parser_block_declaration (cp_parser
*parser
,
11786 int saved_pedantic
;
11788 /* Check for the `__extension__' keyword. */
11789 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11791 /* Parse the qualified declaration. */
11792 cp_parser_block_declaration (parser
, statement_p
);
11793 /* Restore the PEDANTIC flag. */
11794 pedantic
= saved_pedantic
;
11799 /* Peek at the next token to figure out which kind of declaration is
11801 token1
= cp_lexer_peek_token (parser
->lexer
);
11803 /* If the next keyword is `asm', we have an asm-definition. */
11804 if (token1
->keyword
== RID_ASM
)
11807 cp_parser_commit_to_tentative_parse (parser
);
11808 cp_parser_asm_definition (parser
);
11810 /* If the next keyword is `namespace', we have a
11811 namespace-alias-definition. */
11812 else if (token1
->keyword
== RID_NAMESPACE
)
11813 cp_parser_namespace_alias_definition (parser
);
11814 /* If the next keyword is `using', we have a
11815 using-declaration, a using-directive, or an alias-declaration. */
11816 else if (token1
->keyword
== RID_USING
)
11821 cp_parser_commit_to_tentative_parse (parser
);
11822 /* If the token after `using' is `namespace', then we have a
11823 using-directive. */
11824 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11825 if (token2
->keyword
== RID_NAMESPACE
)
11826 cp_parser_using_directive (parser
);
11827 /* If the second token after 'using' is '=', then we have an
11828 alias-declaration. */
11829 else if (cxx_dialect
>= cxx11
11830 && token2
->type
== CPP_NAME
11831 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11832 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11833 cp_parser_alias_declaration (parser
);
11834 /* Otherwise, it's a using-declaration. */
11836 cp_parser_using_declaration (parser
,
11837 /*access_declaration_p=*/false);
11839 /* If the next keyword is `__label__' we have a misplaced label
11841 else if (token1
->keyword
== RID_LABEL
)
11843 cp_lexer_consume_token (parser
->lexer
);
11844 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11845 cp_parser_skip_to_end_of_statement (parser
);
11846 /* If the next token is now a `;', consume it. */
11847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11848 cp_lexer_consume_token (parser
->lexer
);
11850 /* If the next token is `static_assert' we have a static assertion. */
11851 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11852 cp_parser_static_assert (parser
, /*member_p=*/false);
11853 /* Anything else must be a simple-declaration. */
11855 cp_parser_simple_declaration (parser
, !statement_p
,
11856 /*maybe_range_for_decl*/NULL
);
11859 /* Parse a simple-declaration.
11861 simple-declaration:
11862 decl-specifier-seq [opt] init-declarator-list [opt] ;
11864 init-declarator-list:
11866 init-declarator-list , init-declarator
11868 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11869 function-definition as a simple-declaration.
11871 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11872 parsed declaration if it is an uninitialized single declarator not followed
11873 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11874 if present, will not be consumed. */
11877 cp_parser_simple_declaration (cp_parser
* parser
,
11878 bool function_definition_allowed_p
,
11879 tree
*maybe_range_for_decl
)
11881 cp_decl_specifier_seq decl_specifiers
;
11882 int declares_class_or_enum
;
11883 bool saw_declarator
;
11884 location_t comma_loc
= UNKNOWN_LOCATION
;
11885 location_t init_loc
= UNKNOWN_LOCATION
;
11887 if (maybe_range_for_decl
)
11888 *maybe_range_for_decl
= NULL_TREE
;
11890 /* Defer access checks until we know what is being declared; the
11891 checks for names appearing in the decl-specifier-seq should be
11892 done as if we were in the scope of the thing being declared. */
11893 push_deferring_access_checks (dk_deferred
);
11895 /* Parse the decl-specifier-seq. We have to keep track of whether
11896 or not the decl-specifier-seq declares a named class or
11897 enumeration type, since that is the only case in which the
11898 init-declarator-list is allowed to be empty.
11902 In a simple-declaration, the optional init-declarator-list can be
11903 omitted only when declaring a class or enumeration, that is when
11904 the decl-specifier-seq contains either a class-specifier, an
11905 elaborated-type-specifier, or an enum-specifier. */
11906 cp_parser_decl_specifier_seq (parser
,
11907 CP_PARSER_FLAGS_OPTIONAL
,
11909 &declares_class_or_enum
);
11910 /* We no longer need to defer access checks. */
11911 stop_deferring_access_checks ();
11913 /* In a block scope, a valid declaration must always have a
11914 decl-specifier-seq. By not trying to parse declarators, we can
11915 resolve the declaration/expression ambiguity more quickly. */
11916 if (!function_definition_allowed_p
11917 && !decl_specifiers
.any_specifiers_p
)
11919 cp_parser_error (parser
, "expected declaration");
11923 /* If the next two tokens are both identifiers, the code is
11924 erroneous. The usual cause of this situation is code like:
11928 where "T" should name a type -- but does not. */
11929 if (!decl_specifiers
.any_type_specifiers_p
11930 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11932 /* If parsing tentatively, we should commit; we really are
11933 looking at a declaration. */
11934 cp_parser_commit_to_tentative_parse (parser
);
11939 /* If we have seen at least one decl-specifier, and the next token
11940 is not a parenthesis, then we must be looking at a declaration.
11941 (After "int (" we might be looking at a functional cast.) */
11942 if (decl_specifiers
.any_specifiers_p
11943 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11944 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11945 && !cp_parser_error_occurred (parser
))
11946 cp_parser_commit_to_tentative_parse (parser
);
11948 /* Keep going until we hit the `;' at the end of the simple
11950 saw_declarator
= false;
11951 while (cp_lexer_next_token_is_not (parser
->lexer
,
11955 bool function_definition_p
;
11958 if (saw_declarator
)
11960 /* If we are processing next declarator, comma is expected */
11961 token
= cp_lexer_peek_token (parser
->lexer
);
11962 gcc_assert (token
->type
== CPP_COMMA
);
11963 cp_lexer_consume_token (parser
->lexer
);
11964 if (maybe_range_for_decl
)
11966 *maybe_range_for_decl
= error_mark_node
;
11967 if (comma_loc
== UNKNOWN_LOCATION
)
11968 comma_loc
= token
->location
;
11972 saw_declarator
= true;
11974 /* Parse the init-declarator. */
11975 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11977 function_definition_allowed_p
,
11978 /*member_p=*/false,
11979 declares_class_or_enum
,
11980 &function_definition_p
,
11981 maybe_range_for_decl
,
11983 /* If an error occurred while parsing tentatively, exit quickly.
11984 (That usually happens when in the body of a function; each
11985 statement is treated as a declaration-statement until proven
11987 if (cp_parser_error_occurred (parser
))
11989 /* Handle function definitions specially. */
11990 if (function_definition_p
)
11992 /* If the next token is a `,', then we are probably
11993 processing something like:
11997 which is erroneous. */
11998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
12000 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12001 error_at (token
->location
,
12003 " declarations and function-definitions is forbidden");
12005 /* Otherwise, we're done with the list of declarators. */
12008 pop_deferring_access_checks ();
12012 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
12013 *maybe_range_for_decl
= decl
;
12014 /* The next token should be either a `,' or a `;'. */
12015 token
= cp_lexer_peek_token (parser
->lexer
);
12016 /* If it's a `,', there are more declarators to come. */
12017 if (token
->type
== CPP_COMMA
)
12018 /* will be consumed next time around */;
12019 /* If it's a `;', we are done. */
12020 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
12022 /* Anything else is an error. */
12025 /* If we have already issued an error message we don't need
12026 to issue another one. */
12027 if ((decl
!= error_mark_node
12028 && DECL_INITIAL (decl
) != error_mark_node
)
12029 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
12030 cp_parser_error (parser
, "expected %<,%> or %<;%>");
12031 /* Skip tokens until we reach the end of the statement. */
12032 cp_parser_skip_to_end_of_statement (parser
);
12033 /* If the next token is now a `;', consume it. */
12034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
12035 cp_lexer_consume_token (parser
->lexer
);
12038 /* After the first time around, a function-definition is not
12039 allowed -- even if it was OK at first. For example:
12044 function_definition_allowed_p
= false;
12047 /* Issue an error message if no declarators are present, and the
12048 decl-specifier-seq does not itself declare a class or
12049 enumeration: [dcl.dcl]/3. */
12050 if (!saw_declarator
)
12052 if (cp_parser_declares_only_class_p (parser
))
12054 if (!declares_class_or_enum
12055 && decl_specifiers
.type
12056 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
12057 /* Ensure an error is issued anyway when finish_decltype_type,
12058 called via cp_parser_decl_specifier_seq, returns a class or
12059 an enumeration (c++/51786). */
12060 decl_specifiers
.type
= NULL_TREE
;
12061 shadow_tag (&decl_specifiers
);
12063 /* Perform any deferred access checks. */
12064 perform_deferred_access_checks (tf_warning_or_error
);
12067 /* Consume the `;'. */
12068 if (!maybe_range_for_decl
)
12069 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12070 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12072 if (init_loc
!= UNKNOWN_LOCATION
)
12073 error_at (init_loc
, "initializer in range-based %<for%> loop");
12074 if (comma_loc
!= UNKNOWN_LOCATION
)
12075 error_at (comma_loc
,
12076 "multiple declarations in range-based %<for%> loop");
12080 pop_deferring_access_checks ();
12083 /* Parse a decl-specifier-seq.
12085 decl-specifier-seq:
12086 decl-specifier-seq [opt] decl-specifier
12087 decl-specifier attribute-specifier-seq [opt] (C++11)
12090 storage-class-specifier
12101 Concepts Extension:
12106 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12108 The parser flags FLAGS is used to control type-specifier parsing.
12110 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12113 1: one of the decl-specifiers is an elaborated-type-specifier
12114 (i.e., a type declaration)
12115 2: one of the decl-specifiers is an enum-specifier or a
12116 class-specifier (i.e., a type definition)
12121 cp_parser_decl_specifier_seq (cp_parser
* parser
,
12122 cp_parser_flags flags
,
12123 cp_decl_specifier_seq
*decl_specs
,
12124 int* declares_class_or_enum
)
12126 bool constructor_possible_p
= !parser
->in_declarator_p
;
12127 bool found_decl_spec
= false;
12128 cp_token
*start_token
= NULL
;
12131 /* Clear DECL_SPECS. */
12132 clear_decl_specs (decl_specs
);
12134 /* Assume no class or enumeration type is declared. */
12135 *declares_class_or_enum
= 0;
12137 /* Keep reading specifiers until there are no more to read. */
12140 bool constructor_p
;
12144 /* Peek at the next token. */
12145 token
= cp_lexer_peek_token (parser
->lexer
);
12147 /* Save the first token of the decl spec list for error
12150 start_token
= token
;
12151 /* Handle attributes. */
12152 if (cp_next_tokens_can_be_attribute_p (parser
))
12154 /* Parse the attributes. */
12155 tree attrs
= cp_parser_attributes_opt (parser
);
12157 /* In a sequence of declaration specifiers, c++11 attributes
12158 appertain to the type that precede them. In that case
12161 The attribute-specifier-seq affects the type only for
12162 the declaration it appears in, not other declarations
12163 involving the same type.
12165 But for now let's force the user to position the
12166 attribute either at the beginning of the declaration or
12167 after the declarator-id, which would clearly mean that it
12168 applies to the declarator. */
12169 if (cxx11_attribute_p (attrs
))
12171 if (!found_decl_spec
)
12172 /* The c++11 attribute is at the beginning of the
12173 declaration. It appertains to the entity being
12177 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
12179 /* This is an attribute following a
12180 class-specifier. */
12181 if (decl_specs
->type_definition_p
)
12182 warn_misplaced_attr_for_class_type (token
->location
,
12188 decl_specs
->std_attributes
12189 = chainon (decl_specs
->std_attributes
,
12191 if (decl_specs
->locations
[ds_std_attribute
] == 0)
12192 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
12198 decl_specs
->attributes
12199 = chainon (decl_specs
->attributes
,
12201 if (decl_specs
->locations
[ds_attribute
] == 0)
12202 decl_specs
->locations
[ds_attribute
] = token
->location
;
12205 /* Assume we will find a decl-specifier keyword. */
12206 found_decl_spec
= true;
12207 /* If the next token is an appropriate keyword, we can simply
12208 add it to the list. */
12209 switch (token
->keyword
)
12215 if (!at_class_scope_p ())
12217 error_at (token
->location
, "%<friend%> used outside of class");
12218 cp_lexer_purge_token (parser
->lexer
);
12223 /* Consume the token. */
12224 cp_lexer_consume_token (parser
->lexer
);
12228 case RID_CONSTEXPR
:
12230 cp_lexer_consume_token (parser
->lexer
);
12235 cp_lexer_consume_token (parser
->lexer
);
12238 /* function-specifier:
12245 cp_parser_function_specifier_opt (parser
, decl_specs
);
12252 /* Consume the token. */
12253 cp_lexer_consume_token (parser
->lexer
);
12254 /* A constructor declarator cannot appear in a typedef. */
12255 constructor_possible_p
= false;
12256 /* The "typedef" keyword can only occur in a declaration; we
12257 may as well commit at this point. */
12258 cp_parser_commit_to_tentative_parse (parser
);
12260 if (decl_specs
->storage_class
!= sc_none
)
12261 decl_specs
->conflicting_specifiers_p
= true;
12264 /* storage-class-specifier:
12274 if (cxx_dialect
== cxx98
)
12276 /* Consume the token. */
12277 cp_lexer_consume_token (parser
->lexer
);
12279 /* Complain about `auto' as a storage specifier, if
12280 we're complaining about C++0x compatibility. */
12281 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
12282 " changes meaning in C++11; please remove it");
12284 /* Set the storage class anyway. */
12285 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
12289 /* C++0x auto type-specifier. */
12290 found_decl_spec
= false;
12297 /* Consume the token. */
12298 cp_lexer_consume_token (parser
->lexer
);
12299 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
12303 /* Consume the token. */
12305 cp_lexer_consume_token (parser
->lexer
);
12309 /* We did not yet find a decl-specifier yet. */
12310 found_decl_spec
= false;
12314 if (found_decl_spec
12315 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
12316 && token
->keyword
!= RID_CONSTEXPR
)
12317 error ("decl-specifier invalid in condition");
12320 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
12322 /* Constructors are a special case. The `S' in `S()' is not a
12323 decl-specifier; it is the beginning of the declarator. */
12325 = (!found_decl_spec
12326 && constructor_possible_p
12327 && (cp_parser_constructor_declarator_p
12328 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
12330 /* If we don't have a DECL_SPEC yet, then we must be looking at
12331 a type-specifier. */
12332 if (!found_decl_spec
&& !constructor_p
)
12334 int decl_spec_declares_class_or_enum
;
12335 bool is_cv_qualifier
;
12339 = cp_parser_type_specifier (parser
, flags
,
12341 /*is_declaration=*/true,
12342 &decl_spec_declares_class_or_enum
,
12344 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
12346 /* If this type-specifier referenced a user-defined type
12347 (a typedef, class-name, etc.), then we can't allow any
12348 more such type-specifiers henceforth.
12352 The longest sequence of decl-specifiers that could
12353 possibly be a type name is taken as the
12354 decl-specifier-seq of a declaration. The sequence shall
12355 be self-consistent as described below.
12359 As a general rule, at most one type-specifier is allowed
12360 in the complete decl-specifier-seq of a declaration. The
12361 only exceptions are the following:
12363 -- const or volatile can be combined with any other
12366 -- signed or unsigned can be combined with char, long,
12374 void g (const int Pc);
12376 Here, Pc is *not* part of the decl-specifier seq; it's
12377 the declarator. Therefore, once we see a type-specifier
12378 (other than a cv-qualifier), we forbid any additional
12379 user-defined types. We *do* still allow things like `int
12380 int' to be considered a decl-specifier-seq, and issue the
12381 error message later. */
12382 if (type_spec
&& !is_cv_qualifier
)
12383 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12384 /* A constructor declarator cannot follow a type-specifier. */
12387 constructor_possible_p
= false;
12388 found_decl_spec
= true;
12389 if (!is_cv_qualifier
)
12390 decl_specs
->any_type_specifiers_p
= true;
12394 /* If we still do not have a DECL_SPEC, then there are no more
12395 decl-specifiers. */
12396 if (!found_decl_spec
)
12399 decl_specs
->any_specifiers_p
= true;
12400 /* After we see one decl-specifier, further decl-specifiers are
12401 always optional. */
12402 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12405 /* Don't allow a friend specifier with a class definition. */
12406 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12407 && (*declares_class_or_enum
& 2))
12408 error_at (decl_specs
->locations
[ds_friend
],
12409 "class definition may not be declared a friend");
12412 /* Parse an (optional) storage-class-specifier.
12414 storage-class-specifier:
12423 storage-class-specifier:
12426 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12429 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12431 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12434 if (cxx_dialect
!= cxx98
)
12436 /* Fall through for C++98. */
12443 /* Consume the token. */
12444 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12451 /* Parse an (optional) function-specifier.
12453 function-specifier:
12458 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12459 Updates DECL_SPECS, if it is non-NULL. */
12462 cp_parser_function_specifier_opt (cp_parser
* parser
,
12463 cp_decl_specifier_seq
*decl_specs
)
12465 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12466 switch (token
->keyword
)
12469 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12473 /* 14.5.2.3 [temp.mem]
12475 A member function template shall not be virtual. */
12476 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12477 error_at (token
->location
, "templates may not be %<virtual%>");
12479 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12483 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12490 /* Consume the token. */
12491 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12494 /* Parse a linkage-specification.
12496 linkage-specification:
12497 extern string-literal { declaration-seq [opt] }
12498 extern string-literal declaration */
12501 cp_parser_linkage_specification (cp_parser
* parser
)
12505 /* Look for the `extern' keyword. */
12506 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12508 /* Look for the string-literal. */
12509 linkage
= cp_parser_string_literal (parser
, false, false);
12511 /* Transform the literal into an identifier. If the literal is a
12512 wide-character string, or contains embedded NULs, then we can't
12513 handle it as the user wants. */
12514 if (strlen (TREE_STRING_POINTER (linkage
))
12515 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12517 cp_parser_error (parser
, "invalid linkage-specification");
12518 /* Assume C++ linkage. */
12519 linkage
= lang_name_cplusplus
;
12522 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12524 /* We're now using the new linkage. */
12525 push_lang_context (linkage
);
12527 /* If the next token is a `{', then we're using the first
12529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12531 cp_ensure_no_omp_declare_simd (parser
);
12532 cp_ensure_no_oacc_routine (parser
);
12534 /* Consume the `{' token. */
12535 cp_lexer_consume_token (parser
->lexer
);
12536 /* Parse the declarations. */
12537 cp_parser_declaration_seq_opt (parser
);
12538 /* Look for the closing `}'. */
12539 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12541 /* Otherwise, there's just one declaration. */
12544 bool saved_in_unbraced_linkage_specification_p
;
12546 saved_in_unbraced_linkage_specification_p
12547 = parser
->in_unbraced_linkage_specification_p
;
12548 parser
->in_unbraced_linkage_specification_p
= true;
12549 cp_parser_declaration (parser
);
12550 parser
->in_unbraced_linkage_specification_p
12551 = saved_in_unbraced_linkage_specification_p
;
12554 /* We're done with the linkage-specification. */
12555 pop_lang_context ();
12558 /* Parse a static_assert-declaration.
12560 static_assert-declaration:
12561 static_assert ( constant-expression , string-literal ) ;
12562 static_assert ( constant-expression ) ; (C++1Z)
12564 If MEMBER_P, this static_assert is a class member. */
12567 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12572 location_t saved_loc
;
12575 /* Peek at the `static_assert' token so we can keep track of exactly
12576 where the static assertion started. */
12577 token
= cp_lexer_peek_token (parser
->lexer
);
12578 saved_loc
= token
->location
;
12580 /* Look for the `static_assert' keyword. */
12581 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12585 /* We know we are in a static assertion; commit to any tentative
12587 if (cp_parser_parsing_tentatively (parser
))
12588 cp_parser_commit_to_tentative_parse (parser
);
12590 /* Parse the `(' starting the static assertion condition. */
12591 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12593 /* Parse the constant-expression. Allow a non-constant expression
12594 here in order to give better diagnostics in finish_static_assert. */
12596 cp_parser_constant_expression (parser
,
12597 /*allow_non_constant_p=*/true,
12598 /*non_constant_p=*/&dummy
);
12600 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12602 if (cxx_dialect
< cxx1z
)
12603 pedwarn (input_location
, OPT_Wpedantic
,
12604 "static_assert without a message "
12605 "only available with -std=c++1z or -std=gnu++1z");
12607 cp_lexer_consume_token (parser
->lexer
);
12608 message
= build_string (1, "");
12609 TREE_TYPE (message
) = char_array_type_node
;
12610 fix_string_type (message
);
12614 /* Parse the separating `,'. */
12615 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12617 /* Parse the string-literal message. */
12618 message
= cp_parser_string_literal (parser
,
12619 /*translate=*/false,
12622 /* A `)' completes the static assertion. */
12623 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12624 cp_parser_skip_to_closing_parenthesis (parser
,
12625 /*recovering=*/true,
12626 /*or_comma=*/false,
12627 /*consume_paren=*/true);
12630 /* A semicolon terminates the declaration. */
12631 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12633 /* Complete the static assertion, which may mean either processing
12634 the static assert now or saving it for template instantiation. */
12635 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12638 /* Parse the expression in decltype ( expression ). */
12641 cp_parser_decltype_expr (cp_parser
*parser
,
12642 bool &id_expression_or_member_access_p
)
12644 cp_token
*id_expr_start_token
;
12647 /* First, try parsing an id-expression. */
12648 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12649 cp_parser_parse_tentatively (parser
);
12650 expr
= cp_parser_id_expression (parser
,
12651 /*template_keyword_p=*/false,
12652 /*check_dependency_p=*/true,
12653 /*template_p=*/NULL
,
12654 /*declarator_p=*/false,
12655 /*optional_p=*/false);
12657 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12659 bool non_integral_constant_expression_p
= false;
12660 tree id_expression
= expr
;
12662 const char *error_msg
;
12664 if (identifier_p (expr
))
12665 /* Lookup the name we got back from the id-expression. */
12666 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12667 id_expr_start_token
->location
);
12670 && expr
!= error_mark_node
12671 && TREE_CODE (expr
) != TYPE_DECL
12672 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12673 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12674 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12676 /* Complete lookup of the id-expression. */
12677 expr
= (finish_id_expression
12678 (id_expression
, expr
, parser
->scope
, &idk
,
12679 /*integral_constant_expression_p=*/false,
12680 /*allow_non_integral_constant_expression_p=*/true,
12681 &non_integral_constant_expression_p
,
12682 /*template_p=*/false,
12684 /*address_p=*/false,
12685 /*template_arg_p=*/false,
12687 id_expr_start_token
->location
));
12689 if (expr
== error_mark_node
)
12690 /* We found an id-expression, but it was something that we
12691 should not have found. This is an error, not something
12692 we can recover from, so note that we found an
12693 id-expression and we'll recover as gracefully as
12695 id_expression_or_member_access_p
= true;
12699 && expr
!= error_mark_node
12700 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12701 /* We have an id-expression. */
12702 id_expression_or_member_access_p
= true;
12705 if (!id_expression_or_member_access_p
)
12707 /* Abort the id-expression parse. */
12708 cp_parser_abort_tentative_parse (parser
);
12710 /* Parsing tentatively, again. */
12711 cp_parser_parse_tentatively (parser
);
12713 /* Parse a class member access. */
12714 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12715 /*cast_p=*/false, /*decltype*/true,
12716 /*member_access_only_p=*/true, NULL
);
12719 && expr
!= error_mark_node
12720 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12721 /* We have an id-expression. */
12722 id_expression_or_member_access_p
= true;
12725 if (id_expression_or_member_access_p
)
12726 /* We have parsed the complete id-expression or member access. */
12727 cp_parser_parse_definitely (parser
);
12730 /* Abort our attempt to parse an id-expression or member access
12732 cp_parser_abort_tentative_parse (parser
);
12734 /* Parse a full expression. */
12735 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12736 /*decltype_p=*/true);
12742 /* Parse a `decltype' type. Returns the type.
12744 simple-type-specifier:
12745 decltype ( expression )
12747 decltype ( auto ) */
12750 cp_parser_decltype (cp_parser
*parser
)
12753 bool id_expression_or_member_access_p
= false;
12754 const char *saved_message
;
12755 bool saved_integral_constant_expression_p
;
12756 bool saved_non_integral_constant_expression_p
;
12757 bool saved_greater_than_is_operator_p
;
12758 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12760 if (start_token
->type
== CPP_DECLTYPE
)
12762 /* Already parsed. */
12763 cp_lexer_consume_token (parser
->lexer
);
12764 return start_token
->u
.value
;
12767 /* Look for the `decltype' token. */
12768 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12769 return error_mark_node
;
12771 /* Parse the opening `('. */
12772 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12773 return error_mark_node
;
12775 /* decltype (auto) */
12776 if (cxx_dialect
>= cxx14
12777 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12779 cp_lexer_consume_token (parser
->lexer
);
12780 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12781 return error_mark_node
;
12782 expr
= make_decltype_auto ();
12783 AUTO_IS_DECLTYPE (expr
) = true;
12787 /* Types cannot be defined in a `decltype' expression. Save away the
12789 saved_message
= parser
->type_definition_forbidden_message
;
12791 /* And create the new one. */
12792 parser
->type_definition_forbidden_message
12793 = G_("types may not be defined in %<decltype%> expressions");
12795 /* The restrictions on constant-expressions do not apply inside
12796 decltype expressions. */
12797 saved_integral_constant_expression_p
12798 = parser
->integral_constant_expression_p
;
12799 saved_non_integral_constant_expression_p
12800 = parser
->non_integral_constant_expression_p
;
12801 parser
->integral_constant_expression_p
= false;
12803 /* Within a parenthesized expression, a `>' token is always
12804 the greater-than operator. */
12805 saved_greater_than_is_operator_p
12806 = parser
->greater_than_is_operator_p
;
12807 parser
->greater_than_is_operator_p
= true;
12809 /* Do not actually evaluate the expression. */
12810 ++cp_unevaluated_operand
;
12812 /* Do not warn about problems with the expression. */
12813 ++c_inhibit_evaluation_warnings
;
12815 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12817 /* Go back to evaluating expressions. */
12818 --cp_unevaluated_operand
;
12819 --c_inhibit_evaluation_warnings
;
12821 /* The `>' token might be the end of a template-id or
12822 template-parameter-list now. */
12823 parser
->greater_than_is_operator_p
12824 = saved_greater_than_is_operator_p
;
12826 /* Restore the old message and the integral constant expression
12828 parser
->type_definition_forbidden_message
= saved_message
;
12829 parser
->integral_constant_expression_p
12830 = saved_integral_constant_expression_p
;
12831 parser
->non_integral_constant_expression_p
12832 = saved_non_integral_constant_expression_p
;
12834 /* Parse to the closing `)'. */
12835 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12837 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12838 /*consume_paren=*/true);
12839 return error_mark_node
;
12842 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12843 tf_warning_or_error
);
12846 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12848 start_token
->type
= CPP_DECLTYPE
;
12849 start_token
->u
.value
= expr
;
12850 start_token
->keyword
= RID_MAX
;
12851 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12856 /* Special member functions [gram.special] */
12858 /* Parse a conversion-function-id.
12860 conversion-function-id:
12861 operator conversion-type-id
12863 Returns an IDENTIFIER_NODE representing the operator. */
12866 cp_parser_conversion_function_id (cp_parser
* parser
)
12870 tree saved_qualifying_scope
;
12871 tree saved_object_scope
;
12872 tree pushed_scope
= NULL_TREE
;
12874 /* Look for the `operator' token. */
12875 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12876 return error_mark_node
;
12877 /* When we parse the conversion-type-id, the current scope will be
12878 reset. However, we need that information in able to look up the
12879 conversion function later, so we save it here. */
12880 saved_scope
= parser
->scope
;
12881 saved_qualifying_scope
= parser
->qualifying_scope
;
12882 saved_object_scope
= parser
->object_scope
;
12883 /* We must enter the scope of the class so that the names of
12884 entities declared within the class are available in the
12885 conversion-type-id. For example, consider:
12892 S::operator I() { ... }
12894 In order to see that `I' is a type-name in the definition, we
12895 must be in the scope of `S'. */
12897 pushed_scope
= push_scope (saved_scope
);
12898 /* Parse the conversion-type-id. */
12899 type
= cp_parser_conversion_type_id (parser
);
12900 /* Leave the scope of the class, if any. */
12902 pop_scope (pushed_scope
);
12903 /* Restore the saved scope. */
12904 parser
->scope
= saved_scope
;
12905 parser
->qualifying_scope
= saved_qualifying_scope
;
12906 parser
->object_scope
= saved_object_scope
;
12907 /* If the TYPE is invalid, indicate failure. */
12908 if (type
== error_mark_node
)
12909 return error_mark_node
;
12910 return mangle_conv_op_name_for_type (type
);
12913 /* Parse a conversion-type-id:
12915 conversion-type-id:
12916 type-specifier-seq conversion-declarator [opt]
12918 Returns the TYPE specified. */
12921 cp_parser_conversion_type_id (cp_parser
* parser
)
12924 cp_decl_specifier_seq type_specifiers
;
12925 cp_declarator
*declarator
;
12926 tree type_specified
;
12927 const char *saved_message
;
12929 /* Parse the attributes. */
12930 attributes
= cp_parser_attributes_opt (parser
);
12932 saved_message
= parser
->type_definition_forbidden_message
;
12933 parser
->type_definition_forbidden_message
12934 = G_("types may not be defined in a conversion-type-id");
12936 /* Parse the type-specifiers. */
12937 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12938 /*is_trailing_return=*/false,
12941 parser
->type_definition_forbidden_message
= saved_message
;
12943 /* If that didn't work, stop. */
12944 if (type_specifiers
.type
== error_mark_node
)
12945 return error_mark_node
;
12946 /* Parse the conversion-declarator. */
12947 declarator
= cp_parser_conversion_declarator_opt (parser
);
12949 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12950 /*initialized=*/0, &attributes
);
12952 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12954 /* Don't give this error when parsing tentatively. This happens to
12955 work because we always parse this definitively once. */
12956 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12957 && type_uses_auto (type_specified
))
12959 if (cxx_dialect
< cxx14
)
12961 error ("invalid use of %<auto%> in conversion operator");
12962 return error_mark_node
;
12964 else if (template_parm_scope_p ())
12965 warning (0, "use of %<auto%> in member template "
12966 "conversion operator can never be deduced");
12969 return type_specified
;
12972 /* Parse an (optional) conversion-declarator.
12974 conversion-declarator:
12975 ptr-operator conversion-declarator [opt]
12979 static cp_declarator
*
12980 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12982 enum tree_code code
;
12983 tree class_type
, std_attributes
= NULL_TREE
;
12984 cp_cv_quals cv_quals
;
12986 /* We don't know if there's a ptr-operator next, or not. */
12987 cp_parser_parse_tentatively (parser
);
12988 /* Try the ptr-operator. */
12989 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12991 /* If it worked, look for more conversion-declarators. */
12992 if (cp_parser_parse_definitely (parser
))
12994 cp_declarator
*declarator
;
12996 /* Parse another optional declarator. */
12997 declarator
= cp_parser_conversion_declarator_opt (parser
);
12999 declarator
= cp_parser_make_indirect_declarator
13000 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
13008 /* Parse an (optional) ctor-initializer.
13011 : mem-initializer-list
13013 Returns TRUE iff the ctor-initializer was actually present. */
13016 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
13018 /* If the next token is not a `:', then there is no
13019 ctor-initializer. */
13020 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13022 /* Do default initialization of any bases and members. */
13023 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13024 finish_mem_initializers (NULL_TREE
);
13029 /* Consume the `:' token. */
13030 cp_lexer_consume_token (parser
->lexer
);
13031 /* And the mem-initializer-list. */
13032 cp_parser_mem_initializer_list (parser
);
13037 /* Parse a mem-initializer-list.
13039 mem-initializer-list:
13040 mem-initializer ... [opt]
13041 mem-initializer ... [opt] , mem-initializer-list */
13044 cp_parser_mem_initializer_list (cp_parser
* parser
)
13046 tree mem_initializer_list
= NULL_TREE
;
13047 tree target_ctor
= error_mark_node
;
13048 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13050 /* Let the semantic analysis code know that we are starting the
13051 mem-initializer-list. */
13052 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
13053 error_at (token
->location
,
13054 "only constructors take member initializers");
13056 /* Loop through the list. */
13059 tree mem_initializer
;
13061 token
= cp_lexer_peek_token (parser
->lexer
);
13062 /* Parse the mem-initializer. */
13063 mem_initializer
= cp_parser_mem_initializer (parser
);
13064 /* If the next token is a `...', we're expanding member initializers. */
13065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13067 /* Consume the `...'. */
13068 cp_lexer_consume_token (parser
->lexer
);
13070 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13071 can be expanded but members cannot. */
13072 if (mem_initializer
!= error_mark_node
13073 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
13075 error_at (token
->location
,
13076 "cannot expand initializer for member %<%D%>",
13077 TREE_PURPOSE (mem_initializer
));
13078 mem_initializer
= error_mark_node
;
13081 /* Construct the pack expansion type. */
13082 if (mem_initializer
!= error_mark_node
)
13083 mem_initializer
= make_pack_expansion (mem_initializer
);
13085 if (target_ctor
!= error_mark_node
13086 && mem_initializer
!= error_mark_node
)
13088 error ("mem-initializer for %qD follows constructor delegation",
13089 TREE_PURPOSE (mem_initializer
));
13090 mem_initializer
= error_mark_node
;
13092 /* Look for a target constructor. */
13093 if (mem_initializer
!= error_mark_node
13094 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
13095 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
13097 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
13098 if (mem_initializer_list
)
13100 error ("constructor delegation follows mem-initializer for %qD",
13101 TREE_PURPOSE (mem_initializer_list
));
13102 mem_initializer
= error_mark_node
;
13104 target_ctor
= mem_initializer
;
13106 /* Add it to the list, unless it was erroneous. */
13107 if (mem_initializer
!= error_mark_node
)
13109 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
13110 mem_initializer_list
= mem_initializer
;
13112 /* If the next token is not a `,', we're done. */
13113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13115 /* Consume the `,' token. */
13116 cp_lexer_consume_token (parser
->lexer
);
13119 /* Perform semantic analysis. */
13120 if (DECL_CONSTRUCTOR_P (current_function_decl
))
13121 finish_mem_initializers (mem_initializer_list
);
13124 /* Parse a mem-initializer.
13127 mem-initializer-id ( expression-list [opt] )
13128 mem-initializer-id braced-init-list
13133 ( expression-list [opt] )
13135 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13136 class) or FIELD_DECL (for a non-static data member) to initialize;
13137 the TREE_VALUE is the expression-list. An empty initialization
13138 list is represented by void_list_node. */
13141 cp_parser_mem_initializer (cp_parser
* parser
)
13143 tree mem_initializer_id
;
13144 tree expression_list
;
13146 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13148 /* Find out what is being initialized. */
13149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
13151 permerror (token
->location
,
13152 "anachronistic old-style base class initializer");
13153 mem_initializer_id
= NULL_TREE
;
13157 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
13158 if (mem_initializer_id
== error_mark_node
)
13159 return mem_initializer_id
;
13161 member
= expand_member_init (mem_initializer_id
);
13162 if (member
&& !DECL_P (member
))
13163 in_base_initializer
= 1;
13165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13167 bool expr_non_constant_p
;
13168 cp_lexer_set_source_position (parser
->lexer
);
13169 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13170 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13171 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
13172 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
13176 vec
<tree
, va_gc
> *vec
;
13177 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
13179 /*allow_expansion_p=*/true,
13180 /*non_constant_p=*/NULL
);
13182 return error_mark_node
;
13183 expression_list
= build_tree_list_vec (vec
);
13184 release_tree_vector (vec
);
13187 if (expression_list
== error_mark_node
)
13188 return error_mark_node
;
13189 if (!expression_list
)
13190 expression_list
= void_type_node
;
13192 in_base_initializer
= 0;
13194 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
13197 /* Parse a mem-initializer-id.
13199 mem-initializer-id:
13200 :: [opt] nested-name-specifier [opt] class-name
13201 decltype-specifier (C++11)
13204 Returns a TYPE indicating the class to be initialized for the first
13205 production (and the second in C++11). Returns an IDENTIFIER_NODE
13206 indicating the data member to be initialized for the last production. */
13209 cp_parser_mem_initializer_id (cp_parser
* parser
)
13211 bool global_scope_p
;
13212 bool nested_name_specifier_p
;
13213 bool template_p
= false;
13216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13218 /* `typename' is not allowed in this context ([temp.res]). */
13219 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
13221 error_at (token
->location
,
13222 "keyword %<typename%> not allowed in this context (a qualified "
13223 "member initializer is implicitly a type)");
13224 cp_lexer_consume_token (parser
->lexer
);
13226 /* Look for the optional `::' operator. */
13228 = (cp_parser_global_scope_opt (parser
,
13229 /*current_scope_valid_p=*/false)
13231 /* Look for the optional nested-name-specifier. The simplest way to
13236 The keyword `typename' is not permitted in a base-specifier or
13237 mem-initializer; in these contexts a qualified name that
13238 depends on a template-parameter is implicitly assumed to be a
13241 is to assume that we have seen the `typename' keyword at this
13243 nested_name_specifier_p
13244 = (cp_parser_nested_name_specifier_opt (parser
,
13245 /*typename_keyword_p=*/true,
13246 /*check_dependency_p=*/true,
13248 /*is_declaration=*/true)
13250 if (nested_name_specifier_p
)
13251 template_p
= cp_parser_optional_template_keyword (parser
);
13252 /* If there is a `::' operator or a nested-name-specifier, then we
13253 are definitely looking for a class-name. */
13254 if (global_scope_p
|| nested_name_specifier_p
)
13255 return cp_parser_class_name (parser
,
13256 /*typename_keyword_p=*/true,
13257 /*template_keyword_p=*/template_p
,
13259 /*check_dependency_p=*/true,
13260 /*class_head_p=*/false,
13261 /*is_declaration=*/true);
13262 /* Otherwise, we could also be looking for an ordinary identifier. */
13263 cp_parser_parse_tentatively (parser
);
13264 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
13265 /* Try a decltype-specifier. */
13266 id
= cp_parser_decltype (parser
);
13268 /* Otherwise, try a class-name. */
13269 id
= cp_parser_class_name (parser
,
13270 /*typename_keyword_p=*/true,
13271 /*template_keyword_p=*/false,
13273 /*check_dependency_p=*/true,
13274 /*class_head_p=*/false,
13275 /*is_declaration=*/true);
13276 /* If we found one, we're done. */
13277 if (cp_parser_parse_definitely (parser
))
13279 /* Otherwise, look for an ordinary identifier. */
13280 return cp_parser_identifier (parser
);
13283 /* Overloading [gram.over] */
13285 /* Parse an operator-function-id.
13287 operator-function-id:
13290 Returns an IDENTIFIER_NODE for the operator which is a
13291 human-readable spelling of the identifier, e.g., `operator +'. */
13294 cp_parser_operator_function_id (cp_parser
* parser
)
13296 /* Look for the `operator' keyword. */
13297 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
13298 return error_mark_node
;
13299 /* And then the name of the operator itself. */
13300 return cp_parser_operator (parser
);
13303 /* Return an identifier node for a user-defined literal operator.
13304 The suffix identifier is chained to the operator name identifier. */
13307 cp_literal_operator_id (const char* name
)
13310 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
13311 + strlen (name
) + 10);
13312 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
13313 identifier
= get_identifier (buffer
);
13318 /* Parse an operator.
13321 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13322 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13323 || ++ -- , ->* -> () []
13330 Returns an IDENTIFIER_NODE for the operator which is a
13331 human-readable spelling of the identifier, e.g., `operator +'. */
13334 cp_parser_operator (cp_parser
* parser
)
13336 tree id
= NULL_TREE
;
13340 /* Peek at the next token. */
13341 token
= cp_lexer_peek_token (parser
->lexer
);
13342 /* Figure out which operator we have. */
13343 switch (token
->type
)
13349 /* The keyword should be either `new' or `delete'. */
13350 if (token
->keyword
== RID_NEW
)
13352 else if (token
->keyword
== RID_DELETE
)
13357 /* Consume the `new' or `delete' token. */
13358 cp_lexer_consume_token (parser
->lexer
);
13360 /* Peek at the next token. */
13361 token
= cp_lexer_peek_token (parser
->lexer
);
13362 /* If it's a `[' token then this is the array variant of the
13364 if (token
->type
== CPP_OPEN_SQUARE
)
13366 /* Consume the `[' token. */
13367 cp_lexer_consume_token (parser
->lexer
);
13368 /* Look for the `]' token. */
13369 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13370 id
= ansi_opname (op
== NEW_EXPR
13371 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
13373 /* Otherwise, we have the non-array variant. */
13375 id
= ansi_opname (op
);
13381 id
= ansi_opname (PLUS_EXPR
);
13385 id
= ansi_opname (MINUS_EXPR
);
13389 id
= ansi_opname (MULT_EXPR
);
13393 id
= ansi_opname (TRUNC_DIV_EXPR
);
13397 id
= ansi_opname (TRUNC_MOD_EXPR
);
13401 id
= ansi_opname (BIT_XOR_EXPR
);
13405 id
= ansi_opname (BIT_AND_EXPR
);
13409 id
= ansi_opname (BIT_IOR_EXPR
);
13413 id
= ansi_opname (BIT_NOT_EXPR
);
13417 id
= ansi_opname (TRUTH_NOT_EXPR
);
13421 id
= ansi_assopname (NOP_EXPR
);
13425 id
= ansi_opname (LT_EXPR
);
13429 id
= ansi_opname (GT_EXPR
);
13433 id
= ansi_assopname (PLUS_EXPR
);
13437 id
= ansi_assopname (MINUS_EXPR
);
13441 id
= ansi_assopname (MULT_EXPR
);
13445 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13449 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13453 id
= ansi_assopname (BIT_XOR_EXPR
);
13457 id
= ansi_assopname (BIT_AND_EXPR
);
13461 id
= ansi_assopname (BIT_IOR_EXPR
);
13465 id
= ansi_opname (LSHIFT_EXPR
);
13469 id
= ansi_opname (RSHIFT_EXPR
);
13472 case CPP_LSHIFT_EQ
:
13473 id
= ansi_assopname (LSHIFT_EXPR
);
13476 case CPP_RSHIFT_EQ
:
13477 id
= ansi_assopname (RSHIFT_EXPR
);
13481 id
= ansi_opname (EQ_EXPR
);
13485 id
= ansi_opname (NE_EXPR
);
13489 id
= ansi_opname (LE_EXPR
);
13492 case CPP_GREATER_EQ
:
13493 id
= ansi_opname (GE_EXPR
);
13497 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13501 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13504 case CPP_PLUS_PLUS
:
13505 id
= ansi_opname (POSTINCREMENT_EXPR
);
13508 case CPP_MINUS_MINUS
:
13509 id
= ansi_opname (PREDECREMENT_EXPR
);
13513 id
= ansi_opname (COMPOUND_EXPR
);
13516 case CPP_DEREF_STAR
:
13517 id
= ansi_opname (MEMBER_REF
);
13521 id
= ansi_opname (COMPONENT_REF
);
13524 case CPP_OPEN_PAREN
:
13525 /* Consume the `('. */
13526 cp_lexer_consume_token (parser
->lexer
);
13527 /* Look for the matching `)'. */
13528 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13529 return ansi_opname (CALL_EXPR
);
13531 case CPP_OPEN_SQUARE
:
13532 /* Consume the `['. */
13533 cp_lexer_consume_token (parser
->lexer
);
13534 /* Look for the matching `]'. */
13535 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13536 return ansi_opname (ARRAY_REF
);
13538 case CPP_UTF8STRING
:
13539 case CPP_UTF8STRING_USERDEF
:
13545 case CPP_STRING_USERDEF
:
13546 case CPP_WSTRING_USERDEF
:
13547 case CPP_STRING16_USERDEF
:
13548 case CPP_STRING32_USERDEF
:
13550 tree str
, string_tree
;
13553 if (cxx_dialect
== cxx98
)
13554 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13556 /* Consume the string. */
13557 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13558 /*wide_ok=*/true, /*lookup_udlit=*/false);
13559 if (str
== error_mark_node
)
13560 return error_mark_node
;
13561 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13563 string_tree
= USERDEF_LITERAL_VALUE (str
);
13564 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13569 /* Look for the suffix identifier. */
13570 token
= cp_lexer_peek_token (parser
->lexer
);
13571 if (token
->type
== CPP_NAME
)
13572 id
= cp_parser_identifier (parser
);
13573 else if (token
->type
== CPP_KEYWORD
)
13575 error ("unexpected keyword;"
13576 " remove space between quotes and suffix identifier");
13577 return error_mark_node
;
13581 error ("expected suffix identifier");
13582 return error_mark_node
;
13585 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13586 (TREE_TYPE (TREE_TYPE (string_tree
))));
13587 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13590 error ("expected empty string after %<operator%> keyword");
13591 return error_mark_node
;
13593 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13596 error ("invalid encoding prefix in literal operator");
13597 return error_mark_node
;
13599 if (id
!= error_mark_node
)
13601 const char *name
= IDENTIFIER_POINTER (id
);
13602 id
= cp_literal_operator_id (name
);
13608 /* Anything else is an error. */
13612 /* If we have selected an identifier, we need to consume the
13615 cp_lexer_consume_token (parser
->lexer
);
13616 /* Otherwise, no valid operator name was present. */
13619 cp_parser_error (parser
, "expected operator");
13620 id
= error_mark_node
;
13626 /* Parse a template-declaration.
13628 template-declaration:
13629 export [opt] template < template-parameter-list > declaration
13631 If MEMBER_P is TRUE, this template-declaration occurs within a
13634 The grammar rule given by the standard isn't correct. What
13635 is really meant is:
13637 template-declaration:
13638 export [opt] template-parameter-list-seq
13639 decl-specifier-seq [opt] init-declarator [opt] ;
13640 export [opt] template-parameter-list-seq
13641 function-definition
13643 template-parameter-list-seq:
13644 template-parameter-list-seq [opt]
13645 template < template-parameter-list >
13647 Concept Extensions:
13649 template-parameter-list-seq:
13650 template < template-parameter-list > requires-clause [opt]
13653 requires logical-or-expression */
13656 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13658 /* Check for `export'. */
13659 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13661 /* Consume the `export' token. */
13662 cp_lexer_consume_token (parser
->lexer
);
13663 /* Warn that we do not support `export'. */
13664 warning (0, "keyword %<export%> not implemented, and will be ignored");
13667 cp_parser_template_declaration_after_export (parser
, member_p
);
13670 /* Parse a template-parameter-list.
13672 template-parameter-list:
13674 template-parameter-list , template-parameter
13676 Returns a TREE_LIST. Each node represents a template parameter.
13677 The nodes are connected via their TREE_CHAINs. */
13680 cp_parser_template_parameter_list (cp_parser
* parser
)
13682 tree parameter_list
= NULL_TREE
;
13684 begin_template_parm_list ();
13686 /* The loop below parses the template parms. We first need to know
13687 the total number of template parms to be able to compute proper
13688 canonical types of each dependent type. So after the loop, when
13689 we know the total number of template parms,
13690 end_template_parm_list computes the proper canonical types and
13691 fixes up the dependent types accordingly. */
13696 bool is_parameter_pack
;
13697 location_t parm_loc
;
13699 /* Parse the template-parameter. */
13700 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13701 parameter
= cp_parser_template_parameter (parser
,
13703 &is_parameter_pack
);
13704 /* Add it to the list. */
13705 if (parameter
!= error_mark_node
)
13706 parameter_list
= process_template_parm (parameter_list
,
13710 is_parameter_pack
);
13713 tree err_parm
= build_tree_list (parameter
, parameter
);
13714 parameter_list
= chainon (parameter_list
, err_parm
);
13717 /* If the next token is not a `,', we're done. */
13718 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13720 /* Otherwise, consume the `,' token. */
13721 cp_lexer_consume_token (parser
->lexer
);
13724 return end_template_parm_list (parameter_list
);
13727 /* Parse a introduction-list.
13730 introduced-parameter
13731 introduction-list , introduced-parameter
13733 introduced-parameter:
13734 ...[opt] identifier
13736 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13737 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13738 WILDCARD_DECL will also have DECL_NAME set and token location in
13739 DECL_SOURCE_LOCATION. */
13742 cp_parser_introduction_list (cp_parser
*parser
)
13744 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
13748 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
13750 cp_lexer_consume_token (parser
->lexer
);
13752 /* Build placeholder. */
13753 tree parm
= build_nt (WILDCARD_DECL
);
13754 DECL_SOURCE_LOCATION (parm
)
13755 = cp_lexer_peek_token (parser
->lexer
)->location
;
13756 DECL_NAME (parm
) = cp_parser_identifier (parser
);
13757 WILDCARD_PACK_P (parm
) = is_pack
;
13758 vec_safe_push (introduction_vec
, parm
);
13760 /* If the next token is not a `,', we're done. */
13761 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13763 /* Otherwise, consume the `,' token. */
13764 cp_lexer_consume_token (parser
->lexer
);
13767 /* Convert the vec into a TREE_VEC. */
13768 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
13771 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
13772 TREE_VEC_ELT (introduction_list
, n
) = parm
;
13774 release_tree_vector (introduction_vec
);
13775 return introduction_list
;
13778 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13779 is an abstract declarator. */
13781 static inline cp_declarator
*
13782 get_id_declarator (cp_declarator
*declarator
)
13784 cp_declarator
*d
= declarator
;
13785 while (d
&& d
->kind
!= cdk_id
)
13790 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13791 is an abstract declarator. */
13794 get_unqualified_id (cp_declarator
*declarator
)
13796 declarator
= get_id_declarator (declarator
);
13798 return declarator
->u
.id
.unqualified_name
;
13803 /* Returns true if DECL represents a constrained-parameter. */
13806 is_constrained_parameter (tree decl
)
13809 && TREE_CODE (decl
) == TYPE_DECL
13810 && CONSTRAINED_PARM_CONCEPT (decl
)
13811 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl
)));
13814 /* Returns true if PARM declares a constrained-parameter. */
13817 is_constrained_parameter (cp_parameter_declarator
*parm
)
13819 return is_constrained_parameter (parm
->decl_specifiers
.type
);
13822 /* Check that the type parameter is only a declarator-id, and that its
13823 type is not cv-qualified. */
13826 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
13827 cp_parameter_declarator
*parm
)
13829 if (!parm
->declarator
)
13832 if (parm
->declarator
->kind
!= cdk_id
)
13834 cp_parser_error (parser
, "invalid constrained type parameter");
13838 /* Don't allow cv-qualified type parameters. */
13839 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
13840 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
13842 cp_parser_error (parser
, "cv-qualified type parameter");
13849 /* Finish parsing/processing a template type parameter and checking
13850 various restrictions. */
13853 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
13855 cp_parameter_declarator
* parmdecl
)
13857 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
13858 return finish_template_type_parm (class_type_node
, id
);
13860 return error_mark_node
;
13864 finish_constrained_template_template_parm (tree proto
, tree id
)
13866 /* FIXME: This should probably be copied, and we may need to adjust
13867 the template parameter depths. */
13868 tree saved_parms
= current_template_parms
;
13869 begin_template_parm_list ();
13870 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
13871 end_template_parm_list ();
13873 tree parm
= finish_template_template_parm (class_type_node
, id
);
13874 current_template_parms
= saved_parms
;
13879 /* Finish parsing/processing a template template parameter by borrowing
13880 the template parameter list from the prototype parameter. */
13883 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
13886 cp_parameter_declarator
*parmdecl
)
13888 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
13889 return error_mark_node
;
13890 return finish_constrained_template_template_parm (proto
, id
);
13893 /* Create a new non-type template parameter from the given PARM
13897 constrained_non_type_template_parm (bool *is_non_type
,
13898 cp_parameter_declarator
*parm
)
13900 *is_non_type
= true;
13901 cp_declarator
*decl
= parm
->declarator
;
13902 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
13903 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
13904 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
13907 /* Build a constrained template parameter based on the PARMDECL
13908 declarator. The type of PARMDECL is the constrained type, which
13909 refers to the prototype template parameter that ultimately
13910 specifies the type of the declared parameter. */
13913 finish_constrained_parameter (cp_parser
*parser
,
13914 cp_parameter_declarator
*parmdecl
,
13916 bool *is_parameter_pack
)
13918 tree decl
= parmdecl
->decl_specifiers
.type
;
13919 tree id
= get_unqualified_id (parmdecl
->declarator
);
13920 tree def
= parmdecl
->default_argument
;
13921 tree proto
= DECL_INITIAL (decl
);
13923 /* A template parameter constrained by a variadic concept shall also
13924 be declared as a template parameter pack. */
13925 bool is_variadic
= template_parameter_pack_p (proto
);
13926 if (is_variadic
&& !*is_parameter_pack
)
13927 cp_parser_error (parser
, "variadic constraint introduced without %<...%>");
13929 /* Build the parameter. Return an error if the declarator was invalid. */
13931 if (TREE_CODE (proto
) == TYPE_DECL
)
13932 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
13933 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
13934 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
13937 parm
= constrained_non_type_template_parm (is_non_type
, parmdecl
);
13938 if (parm
== error_mark_node
)
13939 return error_mark_node
;
13941 /* Finish the parameter decl and create a node attaching the
13942 default argument and constraint. */
13943 parm
= build_tree_list (def
, parm
);
13944 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
13949 /* Returns true if the parsed type actually represents the declaration
13950 of a type template-parameter. */
13953 declares_constrained_type_template_parameter (tree type
)
13955 return (is_constrained_parameter (type
)
13956 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
13960 /* Returns true if the parsed type actually represents the declaration of
13961 a template template-parameter. */
13964 declares_constrained_template_template_parameter (tree type
)
13966 return (is_constrained_parameter (type
)
13967 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
13970 /* Parse a default argument for a type template-parameter.
13971 Note that diagnostics are handled in cp_parser_template_parameter. */
13974 cp_parser_default_type_template_argument (cp_parser
*parser
)
13976 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
13978 /* Consume the `=' token. */
13979 cp_lexer_consume_token (parser
->lexer
);
13981 /* Parse the default-argument. */
13982 push_deferring_access_checks (dk_no_deferred
);
13983 tree default_argument
= cp_parser_type_id (parser
);
13984 pop_deferring_access_checks ();
13986 return default_argument
;
13989 /* Parse a default argument for a template template-parameter. */
13992 cp_parser_default_template_template_argument (cp_parser
*parser
)
13994 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
13998 /* Consume the `='. */
13999 cp_lexer_consume_token (parser
->lexer
);
14000 /* Parse the id-expression. */
14001 push_deferring_access_checks (dk_no_deferred
);
14002 /* save token before parsing the id-expression, for error
14004 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
14005 tree default_argument
14006 = cp_parser_id_expression (parser
,
14007 /*template_keyword_p=*/false,
14008 /*check_dependency_p=*/true,
14009 /*template_p=*/&is_template
,
14010 /*declarator_p=*/false,
14011 /*optional_p=*/false);
14012 if (TREE_CODE (default_argument
) == TYPE_DECL
)
14013 /* If the id-expression was a template-id that refers to
14014 a template-class, we already have the declaration here,
14015 so no further lookup is needed. */
14018 /* Look up the name. */
14020 = cp_parser_lookup_name (parser
, default_argument
,
14022 /*is_template=*/is_template
,
14023 /*is_namespace=*/false,
14024 /*check_dependency=*/true,
14025 /*ambiguous_decls=*/NULL
,
14027 /* See if the default argument is valid. */
14028 default_argument
= check_template_template_default_arg (default_argument
);
14029 pop_deferring_access_checks ();
14030 return default_argument
;
14033 /* Parse a template-parameter.
14035 template-parameter:
14037 parameter-declaration
14039 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14040 the parameter. The TREE_PURPOSE is the default value, if any.
14041 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14042 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14043 set to true iff this parameter is a parameter pack. */
14046 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
14047 bool *is_parameter_pack
)
14050 cp_parameter_declarator
*parameter_declarator
;
14053 /* Assume it is a type parameter or a template parameter. */
14054 *is_non_type
= false;
14055 /* Assume it not a parameter pack. */
14056 *is_parameter_pack
= false;
14057 /* Peek at the next token. */
14058 token
= cp_lexer_peek_token (parser
->lexer
);
14059 /* If it is `class' or `template', we have a type-parameter. */
14060 if (token
->keyword
== RID_TEMPLATE
)
14061 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14062 /* If it is `class' or `typename' we do not know yet whether it is a
14063 type parameter or a non-type parameter. Consider:
14065 template <typename T, typename T::X X> ...
14069 template <class C, class D*> ...
14071 Here, the first parameter is a type parameter, and the second is
14072 a non-type parameter. We can tell by looking at the token after
14073 the identifier -- if it is a `,', `=', or `>' then we have a type
14075 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
14077 /* Peek at the token after `class' or `typename'. */
14078 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14079 /* If it's an ellipsis, we have a template type parameter
14081 if (token
->type
== CPP_ELLIPSIS
)
14082 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14083 /* If it's an identifier, skip it. */
14084 if (token
->type
== CPP_NAME
)
14085 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
14086 /* Now, see if the token looks like the end of a template
14088 if (token
->type
== CPP_COMMA
14089 || token
->type
== CPP_EQ
14090 || token
->type
== CPP_GREATER
)
14091 return cp_parser_type_parameter (parser
, is_parameter_pack
);
14094 /* Otherwise, it is a non-type parameter or a constrained parameter.
14098 When parsing a default template-argument for a non-type
14099 template-parameter, the first non-nested `>' is taken as the end
14100 of the template parameter-list rather than a greater-than
14102 parameter_declarator
14103 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
14104 /*parenthesized_p=*/NULL
);
14106 if (!parameter_declarator
)
14107 return error_mark_node
;
14109 /* If the parameter declaration is marked as a parameter pack, set
14110 *IS_PARAMETER_PACK to notify the caller. */
14111 if (parameter_declarator
->template_parameter_pack_p
)
14112 *is_parameter_pack
= true;
14114 if (parameter_declarator
->default_argument
)
14116 /* Can happen in some cases of erroneous input (c++/34892). */
14117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14118 /* Consume the `...' for better error recovery. */
14119 cp_lexer_consume_token (parser
->lexer
);
14122 // The parameter may have been constrained.
14123 if (is_constrained_parameter (parameter_declarator
))
14124 return finish_constrained_parameter (parser
,
14125 parameter_declarator
,
14127 is_parameter_pack
);
14129 // Now we're sure that the parameter is a non-type parameter.
14130 *is_non_type
= true;
14132 parm
= grokdeclarator (parameter_declarator
->declarator
,
14133 ¶meter_declarator
->decl_specifiers
,
14134 TPARM
, /*initialized=*/0,
14135 /*attrlist=*/NULL
);
14136 if (parm
== error_mark_node
)
14137 return error_mark_node
;
14139 return build_tree_list (parameter_declarator
->default_argument
, parm
);
14142 /* Parse a type-parameter.
14145 class identifier [opt]
14146 class identifier [opt] = type-id
14147 typename identifier [opt]
14148 typename identifier [opt] = type-id
14149 template < template-parameter-list > class identifier [opt]
14150 template < template-parameter-list > class identifier [opt]
14153 GNU Extension (variadic templates):
14156 class ... identifier [opt]
14157 typename ... identifier [opt]
14159 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14160 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14161 the declaration of the parameter.
14163 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14166 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
14171 /* Look for a keyword to tell us what kind of parameter this is. */
14172 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
14174 return error_mark_node
;
14176 switch (token
->keyword
)
14182 tree default_argument
;
14184 /* If the next token is an ellipsis, we have a template
14186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14188 /* Consume the `...' token. */
14189 cp_lexer_consume_token (parser
->lexer
);
14190 maybe_warn_variadic_templates ();
14192 *is_parameter_pack
= true;
14195 /* If the next token is an identifier, then it names the
14197 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14198 identifier
= cp_parser_identifier (parser
);
14200 identifier
= NULL_TREE
;
14202 /* Create the parameter. */
14203 parameter
= finish_template_type_parm (class_type_node
, identifier
);
14205 /* If the next token is an `=', we have a default argument. */
14206 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14209 = cp_parser_default_type_template_argument (parser
);
14211 /* Template parameter packs cannot have default
14213 if (*is_parameter_pack
)
14216 error_at (token
->location
,
14217 "template parameter pack %qD cannot have a "
14218 "default argument", identifier
);
14220 error_at (token
->location
,
14221 "template parameter packs cannot have "
14222 "default arguments");
14223 default_argument
= NULL_TREE
;
14225 else if (check_for_bare_parameter_packs (default_argument
))
14226 default_argument
= error_mark_node
;
14229 default_argument
= NULL_TREE
;
14231 /* Create the combined representation of the parameter and the
14232 default argument. */
14233 parameter
= build_tree_list (default_argument
, parameter
);
14240 tree default_argument
;
14242 /* Look for the `<'. */
14243 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14244 /* Parse the template-parameter-list. */
14245 cp_parser_template_parameter_list (parser
);
14246 /* Look for the `>'. */
14247 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14249 // If template requirements are present, parse them.
14252 tree reqs
= get_shorthand_constraints (current_template_parms
);
14253 if (tree r
= cp_parser_requires_clause_opt (parser
))
14254 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
14255 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
14258 /* Look for the `class' or 'typename' keywords. */
14259 cp_parser_type_parameter_key (parser
);
14260 /* If the next token is an ellipsis, we have a template
14262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14264 /* Consume the `...' token. */
14265 cp_lexer_consume_token (parser
->lexer
);
14266 maybe_warn_variadic_templates ();
14268 *is_parameter_pack
= true;
14270 /* If the next token is an `=', then there is a
14271 default-argument. If the next token is a `>', we are at
14272 the end of the parameter-list. If the next token is a `,',
14273 then we are at the end of this parameter. */
14274 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
14275 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
14276 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
14278 identifier
= cp_parser_identifier (parser
);
14279 /* Treat invalid names as if the parameter were nameless. */
14280 if (identifier
== error_mark_node
)
14281 identifier
= NULL_TREE
;
14284 identifier
= NULL_TREE
;
14286 /* Create the template parameter. */
14287 parameter
= finish_template_template_parm (class_type_node
,
14290 /* If the next token is an `=', then there is a
14291 default-argument. */
14292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
14295 = cp_parser_default_template_template_argument (parser
);
14297 /* Template parameter packs cannot have default
14299 if (*is_parameter_pack
)
14302 error_at (token
->location
,
14303 "template parameter pack %qD cannot "
14304 "have a default argument",
14307 error_at (token
->location
, "template parameter packs cannot "
14308 "have default arguments");
14309 default_argument
= NULL_TREE
;
14313 default_argument
= NULL_TREE
;
14315 /* Create the combined representation of the parameter and the
14316 default argument. */
14317 parameter
= build_tree_list (default_argument
, parameter
);
14322 gcc_unreachable ();
14329 /* Parse a template-id.
14332 template-name < template-argument-list [opt] >
14334 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14335 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14336 returned. Otherwise, if the template-name names a function, or set
14337 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14338 names a class, returns a TYPE_DECL for the specialization.
14340 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14341 uninstantiated templates. */
14344 cp_parser_template_id (cp_parser
*parser
,
14345 bool template_keyword_p
,
14346 bool check_dependency_p
,
14347 enum tag_types tag_type
,
14348 bool is_declaration
)
14354 cp_token_position start_of_id
= 0;
14355 deferred_access_check
*chk
;
14356 vec
<deferred_access_check
, va_gc
> *access_check
;
14357 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
14358 bool is_identifier
;
14360 /* If the next token corresponds to a template-id, there is no need
14362 next_token
= cp_lexer_peek_token (parser
->lexer
);
14363 if (next_token
->type
== CPP_TEMPLATE_ID
)
14365 struct tree_check
*check_value
;
14367 /* Get the stored value. */
14368 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
14369 /* Perform any access checks that were deferred. */
14370 access_check
= check_value
->checks
;
14373 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
14374 perform_or_defer_access_check (chk
->binfo
,
14377 tf_warning_or_error
);
14379 /* Return the stored value. */
14380 return check_value
->value
;
14383 /* Avoid performing name lookup if there is no possibility of
14384 finding a template-id. */
14385 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
14386 || (next_token
->type
== CPP_NAME
14387 && !cp_parser_nth_token_starts_template_argument_list_p
14390 cp_parser_error (parser
, "expected template-id");
14391 return error_mark_node
;
14394 /* Remember where the template-id starts. */
14395 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
14396 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
14398 push_deferring_access_checks (dk_deferred
);
14400 /* Parse the template-name. */
14401 is_identifier
= false;
14402 templ
= cp_parser_template_name (parser
, template_keyword_p
,
14403 check_dependency_p
,
14407 if (templ
== error_mark_node
|| is_identifier
)
14409 pop_deferring_access_checks ();
14413 /* If we find the sequence `[:' after a template-name, it's probably
14414 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14415 parse correctly the argument list. */
14416 next_token
= cp_lexer_peek_token (parser
->lexer
);
14417 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14418 if (next_token
->type
== CPP_OPEN_SQUARE
14419 && next_token
->flags
& DIGRAPH
14420 && next_token_2
->type
== CPP_COLON
14421 && !(next_token_2
->flags
& PREV_WHITE
))
14423 cp_parser_parse_tentatively (parser
);
14424 /* Change `:' into `::'. */
14425 next_token_2
->type
= CPP_SCOPE
;
14426 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14428 cp_lexer_consume_token (parser
->lexer
);
14430 /* Parse the arguments. */
14431 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14432 if (!cp_parser_parse_definitely (parser
))
14434 /* If we couldn't parse an argument list, then we revert our changes
14435 and return simply an error. Maybe this is not a template-id
14437 next_token_2
->type
= CPP_COLON
;
14438 cp_parser_error (parser
, "expected %<<%>");
14439 pop_deferring_access_checks ();
14440 return error_mark_node
;
14442 /* Otherwise, emit an error about the invalid digraph, but continue
14443 parsing because we got our argument list. */
14444 if (permerror (next_token
->location
,
14445 "%<<::%> cannot begin a template-argument list"))
14447 static bool hint
= false;
14448 inform (next_token
->location
,
14449 "%<<:%> is an alternate spelling for %<[%>."
14450 " Insert whitespace between %<<%> and %<::%>");
14451 if (!hint
&& !flag_permissive
)
14453 inform (next_token
->location
, "(if you use %<-fpermissive%> "
14454 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14455 "accept your code)");
14462 /* Look for the `<' that starts the template-argument-list. */
14463 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
14465 pop_deferring_access_checks ();
14466 return error_mark_node
;
14468 /* Parse the arguments. */
14469 arguments
= cp_parser_enclosed_template_argument_list (parser
);
14472 /* Build a representation of the specialization. */
14473 if (identifier_p (templ
))
14474 template_id
= build_min_nt_loc (next_token
->location
,
14477 else if (DECL_TYPE_TEMPLATE_P (templ
)
14478 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
14480 bool entering_scope
;
14481 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14482 template (rather than some instantiation thereof) only if
14483 is not nested within some other construct. For example, in
14484 "template <typename T> void f(T) { A<T>::", A<T> is just an
14485 instantiation of A. */
14486 entering_scope
= (template_parm_scope_p ()
14487 && cp_lexer_next_token_is (parser
->lexer
,
14490 = finish_template_type (templ
, arguments
, entering_scope
);
14492 /* A template-like identifier may be a partial concept id. */
14493 else if (flag_concepts
14494 && (template_id
= (cp_parser_maybe_partial_concept_id
14495 (parser
, templ
, arguments
))))
14496 return template_id
;
14497 else if (variable_template_p (templ
))
14499 template_id
= lookup_template_variable (templ
, arguments
);
14500 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14501 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14505 /* If it's not a class-template or a template-template, it should be
14506 a function-template. */
14507 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
14508 || TREE_CODE (templ
) == OVERLOAD
14509 || BASELINK_P (templ
)));
14511 template_id
= lookup_template_function (templ
, arguments
);
14512 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
14513 SET_EXPR_LOCATION (template_id
, next_token
->location
);
14516 /* If parsing tentatively, replace the sequence of tokens that makes
14517 up the template-id with a CPP_TEMPLATE_ID token. That way,
14518 should we re-parse the token stream, we will not have to repeat
14519 the effort required to do the parse, nor will we issue duplicate
14520 error messages about problems during instantiation of the
14523 /* Don't do this if we had a parse error in a declarator; re-parsing
14524 might succeed if a name changes meaning (60361). */
14525 && !(cp_parser_error_occurred (parser
)
14526 && cp_parser_parsing_tentatively (parser
)
14527 && parser
->in_declarator_p
))
14529 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
14531 /* Reset the contents of the START_OF_ID token. */
14532 token
->type
= CPP_TEMPLATE_ID
;
14533 /* Retrieve any deferred checks. Do not pop this access checks yet
14534 so the memory will not be reclaimed during token replacing below. */
14535 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14536 token
->u
.tree_check_value
->value
= template_id
;
14537 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14538 token
->keyword
= RID_MAX
;
14540 /* Purge all subsequent tokens. */
14541 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
14543 /* ??? Can we actually assume that, if template_id ==
14544 error_mark_node, we will have issued a diagnostic to the
14545 user, as opposed to simply marking the tentative parse as
14547 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
14548 error_at (token
->location
, "parse error in template argument list");
14551 pop_to_parent_deferring_access_checks ();
14552 return template_id
;
14555 /* Parse a template-name.
14560 The standard should actually say:
14564 operator-function-id
14566 A defect report has been filed about this issue.
14568 A conversion-function-id cannot be a template name because they cannot
14569 be part of a template-id. In fact, looking at this code:
14571 a.operator K<int>()
14573 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14574 It is impossible to call a templated conversion-function-id with an
14575 explicit argument list, since the only allowed template parameter is
14576 the type to which it is converting.
14578 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14579 `template' keyword, in a construction like:
14583 In that case `f' is taken to be a template-name, even though there
14584 is no way of knowing for sure.
14586 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14587 name refers to a set of overloaded functions, at least one of which
14588 is a template, or an IDENTIFIER_NODE with the name of the template,
14589 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14590 names are looked up inside uninstantiated templates. */
14593 cp_parser_template_name (cp_parser
* parser
,
14594 bool template_keyword_p
,
14595 bool check_dependency_p
,
14596 bool is_declaration
,
14597 enum tag_types tag_type
,
14598 bool *is_identifier
)
14603 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14605 /* If the next token is `operator', then we have either an
14606 operator-function-id or a conversion-function-id. */
14607 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
14609 /* We don't know whether we're looking at an
14610 operator-function-id or a conversion-function-id. */
14611 cp_parser_parse_tentatively (parser
);
14612 /* Try an operator-function-id. */
14613 identifier
= cp_parser_operator_function_id (parser
);
14614 /* If that didn't work, try a conversion-function-id. */
14615 if (!cp_parser_parse_definitely (parser
))
14617 cp_parser_error (parser
, "expected template-name");
14618 return error_mark_node
;
14621 /* Look for the identifier. */
14623 identifier
= cp_parser_identifier (parser
);
14625 /* If we didn't find an identifier, we don't have a template-id. */
14626 if (identifier
== error_mark_node
)
14627 return error_mark_node
;
14629 /* If the name immediately followed the `template' keyword, then it
14630 is a template-name. However, if the next token is not `<', then
14631 we do not treat it as a template-name, since it is not being used
14632 as part of a template-id. This enables us to handle constructs
14635 template <typename T> struct S { S(); };
14636 template <typename T> S<T>::S();
14638 correctly. We would treat `S' as a template -- if it were `S<T>'
14639 -- but we do not if there is no `<'. */
14641 if (processing_template_decl
14642 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
14644 /* In a declaration, in a dependent context, we pretend that the
14645 "template" keyword was present in order to improve error
14646 recovery. For example, given:
14648 template <typename T> void f(T::X<int>);
14650 we want to treat "X<int>" as a template-id. */
14652 && !template_keyword_p
14653 && parser
->scope
&& TYPE_P (parser
->scope
)
14654 && check_dependency_p
14655 && dependent_scope_p (parser
->scope
)
14656 /* Do not do this for dtors (or ctors), since they never
14657 need the template keyword before their name. */
14658 && !constructor_name_p (identifier
, parser
->scope
))
14660 cp_token_position start
= 0;
14662 /* Explain what went wrong. */
14663 error_at (token
->location
, "non-template %qD used as template",
14665 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
14666 parser
->scope
, identifier
);
14667 /* If parsing tentatively, find the location of the "<" token. */
14668 if (cp_parser_simulate_error (parser
))
14669 start
= cp_lexer_token_position (parser
->lexer
, true);
14670 /* Parse the template arguments so that we can issue error
14671 messages about them. */
14672 cp_lexer_consume_token (parser
->lexer
);
14673 cp_parser_enclosed_template_argument_list (parser
);
14674 /* Skip tokens until we find a good place from which to
14675 continue parsing. */
14676 cp_parser_skip_to_closing_parenthesis (parser
,
14677 /*recovering=*/true,
14679 /*consume_paren=*/false);
14680 /* If parsing tentatively, permanently remove the
14681 template argument list. That will prevent duplicate
14682 error messages from being issued about the missing
14683 "template" keyword. */
14685 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
14687 *is_identifier
= true;
14691 /* If the "template" keyword is present, then there is generally
14692 no point in doing name-lookup, so we just return IDENTIFIER.
14693 But, if the qualifying scope is non-dependent then we can
14694 (and must) do name-lookup normally. */
14695 if (template_keyword_p
14697 || (TYPE_P (parser
->scope
)
14698 && dependent_type_p (parser
->scope
))))
14702 /* Look up the name. */
14703 decl
= cp_parser_lookup_name (parser
, identifier
,
14705 /*is_template=*/true,
14706 /*is_namespace=*/false,
14707 check_dependency_p
,
14708 /*ambiguous_decls=*/NULL
,
14711 decl
= strip_using_decl (decl
);
14713 /* If DECL is a template, then the name was a template-name. */
14714 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
14716 if (TREE_DEPRECATED (decl
)
14717 && deprecated_state
!= DEPRECATED_SUPPRESS
)
14718 warn_deprecated_use (decl
, NULL_TREE
);
14722 tree fn
= NULL_TREE
;
14724 /* The standard does not explicitly indicate whether a name that
14725 names a set of overloaded declarations, some of which are
14726 templates, is a template-name. However, such a name should
14727 be a template-name; otherwise, there is no way to form a
14728 template-id for the overloaded templates. */
14729 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14730 if (TREE_CODE (fns
) == OVERLOAD
)
14731 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14732 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14737 /* The name does not name a template. */
14738 cp_parser_error (parser
, "expected template-name");
14739 return error_mark_node
;
14743 /* If DECL is dependent, and refers to a function, then just return
14744 its name; we will look it up again during template instantiation. */
14745 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14747 tree scope
= ovl_scope (decl
);
14748 if (TYPE_P (scope
) && dependent_type_p (scope
))
14755 /* Parse a template-argument-list.
14757 template-argument-list:
14758 template-argument ... [opt]
14759 template-argument-list , template-argument ... [opt]
14761 Returns a TREE_VEC containing the arguments. */
14764 cp_parser_template_argument_list (cp_parser
* parser
)
14766 tree fixed_args
[10];
14767 unsigned n_args
= 0;
14768 unsigned alloced
= 10;
14769 tree
*arg_ary
= fixed_args
;
14771 bool saved_in_template_argument_list_p
;
14773 bool saved_non_ice_p
;
14775 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14776 parser
->in_template_argument_list_p
= true;
14777 /* Even if the template-id appears in an integral
14778 constant-expression, the contents of the argument list do
14780 saved_ice_p
= parser
->integral_constant_expression_p
;
14781 parser
->integral_constant_expression_p
= false;
14782 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14783 parser
->non_integral_constant_expression_p
= false;
14785 /* Parse the arguments. */
14791 /* Consume the comma. */
14792 cp_lexer_consume_token (parser
->lexer
);
14794 /* Parse the template-argument. */
14795 argument
= cp_parser_template_argument (parser
);
14797 /* If the next token is an ellipsis, we're expanding a template
14799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14801 if (argument
== error_mark_node
)
14803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14804 error_at (token
->location
,
14805 "expected parameter pack before %<...%>");
14807 /* Consume the `...' token. */
14808 cp_lexer_consume_token (parser
->lexer
);
14810 /* Make the argument into a TYPE_PACK_EXPANSION or
14811 EXPR_PACK_EXPANSION. */
14812 argument
= make_pack_expansion (argument
);
14815 if (n_args
== alloced
)
14819 if (arg_ary
== fixed_args
)
14821 arg_ary
= XNEWVEC (tree
, alloced
);
14822 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14825 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14827 arg_ary
[n_args
++] = argument
;
14829 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14831 vec
= make_tree_vec (n_args
);
14834 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14836 if (arg_ary
!= fixed_args
)
14838 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14839 parser
->integral_constant_expression_p
= saved_ice_p
;
14840 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14846 /* Parse a template-argument.
14849 assignment-expression
14853 The representation is that of an assignment-expression, type-id, or
14854 id-expression -- except that the qualified id-expression is
14855 evaluated, so that the value returned is either a DECL or an
14858 Although the standard says "assignment-expression", it forbids
14859 throw-expressions or assignments in the template argument.
14860 Therefore, we use "conditional-expression" instead. */
14863 cp_parser_template_argument (cp_parser
* parser
)
14868 bool maybe_type_id
= false;
14869 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14870 location_t loc
= 0;
14873 /* There's really no way to know what we're looking at, so we just
14874 try each alternative in order.
14878 In a template-argument, an ambiguity between a type-id and an
14879 expression is resolved to a type-id, regardless of the form of
14880 the corresponding template-parameter.
14882 Therefore, we try a type-id first. */
14883 cp_parser_parse_tentatively (parser
);
14884 argument
= cp_parser_template_type_arg (parser
);
14885 /* If there was no error parsing the type-id but the next token is a
14886 '>>', our behavior depends on which dialect of C++ we're
14887 parsing. In C++98, we probably found a typo for '> >'. But there
14888 are type-id which are also valid expressions. For instance:
14890 struct X { int operator >> (int); };
14891 template <int V> struct Foo {};
14894 Here 'X()' is a valid type-id of a function type, but the user just
14895 wanted to write the expression "X() >> 5". Thus, we remember that we
14896 found a valid type-id, but we still try to parse the argument as an
14897 expression to see what happens.
14899 In C++0x, the '>>' will be considered two separate '>'
14901 if (!cp_parser_error_occurred (parser
)
14902 && cxx_dialect
== cxx98
14903 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14905 maybe_type_id
= true;
14906 cp_parser_abort_tentative_parse (parser
);
14910 /* If the next token isn't a `,' or a `>', then this argument wasn't
14911 really finished. This means that the argument is not a valid
14913 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14914 cp_parser_error (parser
, "expected template-argument");
14915 /* If that worked, we're done. */
14916 if (cp_parser_parse_definitely (parser
))
14919 /* We're still not sure what the argument will be. */
14920 cp_parser_parse_tentatively (parser
);
14921 /* Try a template. */
14922 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14923 argument
= cp_parser_id_expression (parser
,
14924 /*template_keyword_p=*/false,
14925 /*check_dependency_p=*/true,
14927 /*declarator_p=*/false,
14928 /*optional_p=*/false);
14929 /* If the next token isn't a `,' or a `>', then this argument wasn't
14930 really finished. */
14931 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14932 cp_parser_error (parser
, "expected template-argument");
14933 if (!cp_parser_error_occurred (parser
))
14935 /* Figure out what is being referred to. If the id-expression
14936 was for a class template specialization, then we will have a
14937 TYPE_DECL at this point. There is no need to do name lookup
14938 at this point in that case. */
14939 if (TREE_CODE (argument
) != TYPE_DECL
)
14940 argument
= cp_parser_lookup_name (parser
, argument
,
14942 /*is_template=*/template_p
,
14943 /*is_namespace=*/false,
14944 /*check_dependency=*/true,
14945 /*ambiguous_decls=*/NULL
,
14946 argument_start_token
->location
);
14947 /* Handle a constrained-type-specifier for a non-type template
14949 if (tree decl
= cp_parser_maybe_concept_name (parser
, argument
))
14951 else if (TREE_CODE (argument
) != TEMPLATE_DECL
14952 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14953 cp_parser_error (parser
, "expected template-name");
14955 if (cp_parser_parse_definitely (parser
))
14957 if (TREE_DEPRECATED (argument
))
14958 warn_deprecated_use (argument
, NULL_TREE
);
14961 /* It must be a non-type argument. In C++17 any constant-expression is
14963 if (cxx_dialect
> cxx14
)
14966 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
14968 -- an integral constant-expression of integral or enumeration
14971 -- the name of a non-type template-parameter; or
14973 -- the name of an object or function with external linkage...
14975 -- the address of an object or function with external linkage...
14977 -- a pointer to member... */
14978 /* Look for a non-type template parameter. */
14979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14981 cp_parser_parse_tentatively (parser
);
14982 argument
= cp_parser_primary_expression (parser
,
14983 /*address_p=*/false,
14985 /*template_arg_p=*/true,
14987 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14988 || !cp_parser_next_token_ends_template_argument_p (parser
))
14989 cp_parser_simulate_error (parser
);
14990 if (cp_parser_parse_definitely (parser
))
14994 /* If the next token is "&", the argument must be the address of an
14995 object or function with external linkage. */
14996 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14999 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15000 cp_lexer_consume_token (parser
->lexer
);
15002 /* See if we might have an id-expression. */
15003 token
= cp_lexer_peek_token (parser
->lexer
);
15004 if (token
->type
== CPP_NAME
15005 || token
->keyword
== RID_OPERATOR
15006 || token
->type
== CPP_SCOPE
15007 || token
->type
== CPP_TEMPLATE_ID
15008 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
15010 cp_parser_parse_tentatively (parser
);
15011 argument
= cp_parser_primary_expression (parser
,
15014 /*template_arg_p=*/true,
15016 if (cp_parser_error_occurred (parser
)
15017 || !cp_parser_next_token_ends_template_argument_p (parser
))
15018 cp_parser_abort_tentative_parse (parser
);
15023 if (INDIRECT_REF_P (argument
))
15025 /* Strip the dereference temporarily. */
15026 gcc_assert (REFERENCE_REF_P (argument
));
15027 argument
= TREE_OPERAND (argument
, 0);
15030 /* If we're in a template, we represent a qualified-id referring
15031 to a static data member as a SCOPE_REF even if the scope isn't
15032 dependent so that we can check access control later. */
15034 if (TREE_CODE (probe
) == SCOPE_REF
)
15035 probe
= TREE_OPERAND (probe
, 1);
15038 /* A variable without external linkage might still be a
15039 valid constant-expression, so no error is issued here
15040 if the external-linkage check fails. */
15041 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
15042 cp_parser_simulate_error (parser
);
15044 else if (is_overloaded_fn (argument
))
15045 /* All overloaded functions are allowed; if the external
15046 linkage test does not pass, an error will be issued
15050 && (TREE_CODE (argument
) == OFFSET_REF
15051 || TREE_CODE (argument
) == SCOPE_REF
))
15052 /* A pointer-to-member. */
15054 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
15057 cp_parser_simulate_error (parser
);
15059 if (cp_parser_parse_definitely (parser
))
15062 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
15063 tf_warning_or_error
);
15065 argument
= convert_from_reference (argument
);
15070 /* If the argument started with "&", there are no other valid
15071 alternatives at this point. */
15074 cp_parser_error (parser
, "invalid non-type template argument");
15075 return error_mark_node
;
15079 /* If the argument wasn't successfully parsed as a type-id followed
15080 by '>>', the argument can only be a constant expression now.
15081 Otherwise, we try parsing the constant-expression tentatively,
15082 because the argument could really be a type-id. */
15084 cp_parser_parse_tentatively (parser
);
15085 argument
= cp_parser_constant_expression (parser
);
15087 if (!maybe_type_id
)
15089 if (!cp_parser_next_token_ends_template_argument_p (parser
))
15090 cp_parser_error (parser
, "expected template-argument");
15091 if (cp_parser_parse_definitely (parser
))
15093 /* We did our best to parse the argument as a non type-id, but that
15094 was the only alternative that matched (albeit with a '>' after
15095 it). We can assume it's just a typo from the user, and a
15096 diagnostic will then be issued. */
15097 return cp_parser_template_type_arg (parser
);
15100 /* Parse an explicit-instantiation.
15102 explicit-instantiation:
15103 template declaration
15105 Although the standard says `declaration', what it really means is:
15107 explicit-instantiation:
15108 template decl-specifier-seq [opt] declarator [opt] ;
15110 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15111 supposed to be allowed. A defect report has been filed about this
15116 explicit-instantiation:
15117 storage-class-specifier template
15118 decl-specifier-seq [opt] declarator [opt] ;
15119 function-specifier template
15120 decl-specifier-seq [opt] declarator [opt] ; */
15123 cp_parser_explicit_instantiation (cp_parser
* parser
)
15125 int declares_class_or_enum
;
15126 cp_decl_specifier_seq decl_specifiers
;
15127 tree extension_specifier
= NULL_TREE
;
15129 timevar_push (TV_TEMPLATE_INST
);
15131 /* Look for an (optional) storage-class-specifier or
15132 function-specifier. */
15133 if (cp_parser_allow_gnu_extensions_p (parser
))
15135 extension_specifier
15136 = cp_parser_storage_class_specifier_opt (parser
);
15137 if (!extension_specifier
)
15138 extension_specifier
15139 = cp_parser_function_specifier_opt (parser
,
15140 /*decl_specs=*/NULL
);
15143 /* Look for the `template' keyword. */
15144 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15145 /* Let the front end know that we are processing an explicit
15147 begin_explicit_instantiation ();
15148 /* [temp.explicit] says that we are supposed to ignore access
15149 control while processing explicit instantiation directives. */
15150 push_deferring_access_checks (dk_no_check
);
15151 /* Parse a decl-specifier-seq. */
15152 cp_parser_decl_specifier_seq (parser
,
15153 CP_PARSER_FLAGS_OPTIONAL
,
15155 &declares_class_or_enum
);
15156 /* If there was exactly one decl-specifier, and it declared a class,
15157 and there's no declarator, then we have an explicit type
15159 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
15163 type
= check_tag_decl (&decl_specifiers
,
15164 /*explicit_type_instantiation_p=*/true);
15165 /* Turn access control back on for names used during
15166 template instantiation. */
15167 pop_deferring_access_checks ();
15169 do_type_instantiation (type
, extension_specifier
,
15170 /*complain=*/tf_error
);
15174 cp_declarator
*declarator
;
15177 /* Parse the declarator. */
15179 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
15180 /*ctor_dtor_or_conv_p=*/NULL
,
15181 /*parenthesized_p=*/NULL
,
15182 /*member_p=*/false,
15183 /*friend_p=*/false);
15184 if (declares_class_or_enum
& 2)
15185 cp_parser_check_for_definition_in_return_type (declarator
,
15186 decl_specifiers
.type
,
15187 decl_specifiers
.locations
[ds_type_spec
]);
15188 if (declarator
!= cp_error_declarator
)
15190 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
15191 permerror (decl_specifiers
.locations
[ds_inline
],
15192 "explicit instantiation shall not use"
15193 " %<inline%> specifier");
15194 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
15195 permerror (decl_specifiers
.locations
[ds_constexpr
],
15196 "explicit instantiation shall not use"
15197 " %<constexpr%> specifier");
15199 decl
= grokdeclarator (declarator
, &decl_specifiers
,
15200 NORMAL
, 0, &decl_specifiers
.attributes
);
15201 /* Turn access control back on for names used during
15202 template instantiation. */
15203 pop_deferring_access_checks ();
15204 /* Do the explicit instantiation. */
15205 do_decl_instantiation (decl
, extension_specifier
);
15209 pop_deferring_access_checks ();
15210 /* Skip the body of the explicit instantiation. */
15211 cp_parser_skip_to_end_of_statement (parser
);
15214 /* We're done with the instantiation. */
15215 end_explicit_instantiation ();
15217 cp_parser_consume_semicolon_at_end_of_statement (parser
);
15219 timevar_pop (TV_TEMPLATE_INST
);
15222 /* Parse an explicit-specialization.
15224 explicit-specialization:
15225 template < > declaration
15227 Although the standard says `declaration', what it really means is:
15229 explicit-specialization:
15230 template <> decl-specifier [opt] init-declarator [opt] ;
15231 template <> function-definition
15232 template <> explicit-specialization
15233 template <> template-declaration */
15236 cp_parser_explicit_specialization (cp_parser
* parser
)
15238 bool need_lang_pop
;
15239 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15241 /* Look for the `template' keyword. */
15242 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
15243 /* Look for the `<'. */
15244 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
15245 /* Look for the `>'. */
15246 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
15247 /* We have processed another parameter list. */
15248 ++parser
->num_template_parameter_lists
;
15251 A template ... explicit specialization ... shall not have C
15253 if (current_lang_name
== lang_name_c
)
15255 error_at (token
->location
, "template specialization with C linkage");
15256 /* Give it C++ linkage to avoid confusing other parts of the
15258 push_lang_context (lang_name_cplusplus
);
15259 need_lang_pop
= true;
15262 need_lang_pop
= false;
15263 /* Let the front end know that we are beginning a specialization. */
15264 if (!begin_specialization ())
15266 end_specialization ();
15270 /* If the next keyword is `template', we need to figure out whether
15271 or not we're looking a template-declaration. */
15272 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
15274 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
15275 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
15276 cp_parser_template_declaration_after_export (parser
,
15277 /*member_p=*/false);
15279 cp_parser_explicit_specialization (parser
);
15282 /* Parse the dependent declaration. */
15283 cp_parser_single_declaration (parser
,
15285 /*member_p=*/false,
15286 /*explicit_specialization_p=*/true,
15287 /*friend_p=*/NULL
);
15288 /* We're done with the specialization. */
15289 end_specialization ();
15290 /* For the erroneous case of a template with C linkage, we pushed an
15291 implicit C++ linkage scope; exit that scope now. */
15293 pop_lang_context ();
15294 /* We're done with this parameter list. */
15295 --parser
->num_template_parameter_lists
;
15298 /* Parse a type-specifier.
15301 simple-type-specifier
15304 elaborated-type-specifier
15312 Returns a representation of the type-specifier. For a
15313 class-specifier, enum-specifier, or elaborated-type-specifier, a
15314 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15316 The parser flags FLAGS is used to control type-specifier parsing.
15318 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15319 in a decl-specifier-seq.
15321 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15322 class-specifier, enum-specifier, or elaborated-type-specifier, then
15323 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15324 if a type is declared; 2 if it is defined. Otherwise, it is set to
15327 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15328 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15329 is set to FALSE. */
15332 cp_parser_type_specifier (cp_parser
* parser
,
15333 cp_parser_flags flags
,
15334 cp_decl_specifier_seq
*decl_specs
,
15335 bool is_declaration
,
15336 int* declares_class_or_enum
,
15337 bool* is_cv_qualifier
)
15339 tree type_spec
= NULL_TREE
;
15342 cp_decl_spec ds
= ds_last
;
15344 /* Assume this type-specifier does not declare a new type. */
15345 if (declares_class_or_enum
)
15346 *declares_class_or_enum
= 0;
15347 /* And that it does not specify a cv-qualifier. */
15348 if (is_cv_qualifier
)
15349 *is_cv_qualifier
= false;
15350 /* Peek at the next token. */
15351 token
= cp_lexer_peek_token (parser
->lexer
);
15353 /* If we're looking at a keyword, we can use that to guide the
15354 production we choose. */
15355 keyword
= token
->keyword
;
15359 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15360 goto elaborated_type_specifier
;
15362 /* Look for the enum-specifier. */
15363 type_spec
= cp_parser_enum_specifier (parser
);
15364 /* If that worked, we're done. */
15367 if (declares_class_or_enum
)
15368 *declares_class_or_enum
= 2;
15370 cp_parser_set_decl_spec_type (decl_specs
,
15373 /*type_definition_p=*/true);
15377 goto elaborated_type_specifier
;
15379 /* Any of these indicate either a class-specifier, or an
15380 elaborated-type-specifier. */
15384 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
15385 goto elaborated_type_specifier
;
15387 /* Parse tentatively so that we can back up if we don't find a
15388 class-specifier. */
15389 cp_parser_parse_tentatively (parser
);
15390 /* Look for the class-specifier. */
15391 type_spec
= cp_parser_class_specifier (parser
);
15392 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
15393 /* If that worked, we're done. */
15394 if (cp_parser_parse_definitely (parser
))
15396 if (declares_class_or_enum
)
15397 *declares_class_or_enum
= 2;
15399 cp_parser_set_decl_spec_type (decl_specs
,
15402 /*type_definition_p=*/true);
15406 /* Fall through. */
15407 elaborated_type_specifier
:
15408 /* We're declaring (not defining) a class or enum. */
15409 if (declares_class_or_enum
)
15410 *declares_class_or_enum
= 1;
15412 /* Fall through. */
15414 /* Look for an elaborated-type-specifier. */
15416 = (cp_parser_elaborated_type_specifier
15418 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
15421 cp_parser_set_decl_spec_type (decl_specs
,
15424 /*type_definition_p=*/false);
15429 if (is_cv_qualifier
)
15430 *is_cv_qualifier
= true;
15435 if (is_cv_qualifier
)
15436 *is_cv_qualifier
= true;
15441 if (is_cv_qualifier
)
15442 *is_cv_qualifier
= true;
15446 /* The `__complex__' keyword is a GNU extension. */
15454 /* Handle simple keywords. */
15459 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
15460 decl_specs
->any_specifiers_p
= true;
15462 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15465 /* If we do not already have a type-specifier, assume we are looking
15466 at a simple-type-specifier. */
15467 type_spec
= cp_parser_simple_type_specifier (parser
,
15471 /* If we didn't find a type-specifier, and a type-specifier was not
15472 optional in this context, issue an error message. */
15473 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15475 cp_parser_error (parser
, "expected type specifier");
15476 return error_mark_node
;
15482 /* Parse a simple-type-specifier.
15484 simple-type-specifier:
15485 :: [opt] nested-name-specifier [opt] type-name
15486 :: [opt] nested-name-specifier template template-id
15501 simple-type-specifier:
15503 decltype ( expression )
15506 __underlying_type ( type-id )
15510 simple-type-specifier:
15512 __typeof__ unary-expression
15513 __typeof__ ( type-id )
15514 __typeof__ ( type-id ) { initializer-list , [opt] }
15516 Concepts Extension:
15518 simple-type-specifier:
15519 constrained-type-specifier
15521 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15522 appropriately updated. */
15525 cp_parser_simple_type_specifier (cp_parser
* parser
,
15526 cp_decl_specifier_seq
*decl_specs
,
15527 cp_parser_flags flags
)
15529 tree type
= NULL_TREE
;
15533 /* Peek at the next token. */
15534 token
= cp_lexer_peek_token (parser
->lexer
);
15536 /* If we're looking at a keyword, things are easy. */
15537 switch (token
->keyword
)
15541 decl_specs
->explicit_char_p
= true;
15542 type
= char_type_node
;
15545 type
= char16_type_node
;
15548 type
= char32_type_node
;
15551 type
= wchar_type_node
;
15554 type
= boolean_type_node
;
15557 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
15558 type
= short_integer_type_node
;
15562 decl_specs
->explicit_int_p
= true;
15563 type
= integer_type_node
;
15569 idx
= token
->keyword
- RID_INT_N_0
;
15570 if (! int_n_enabled_p
[idx
])
15574 decl_specs
->explicit_intN_p
= true;
15575 decl_specs
->int_n_idx
= idx
;
15577 type
= int_n_trees
[idx
].signed_type
;
15581 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
15582 type
= long_integer_type_node
;
15585 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
15586 type
= integer_type_node
;
15589 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
15590 type
= unsigned_type_node
;
15593 type
= float_type_node
;
15596 type
= double_type_node
;
15599 type
= void_type_node
;
15603 maybe_warn_cpp0x (CPP0X_AUTO
);
15604 if (parser
->auto_is_implicit_function_template_parm_p
)
15606 /* The 'auto' might be the placeholder return type for a function decl
15607 with trailing return type. */
15608 bool have_trailing_return_fn_decl
= false;
15609 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
15612 cp_parser_parse_tentatively (parser
);
15613 cp_lexer_consume_token (parser
->lexer
);
15614 cp_lexer_consume_token (parser
->lexer
);
15615 if (cp_parser_skip_to_closing_parenthesis (parser
,
15616 /*recovering*/false,
15618 /*consume_paren*/true))
15619 have_trailing_return_fn_decl
15620 = cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
);
15621 cp_parser_abort_tentative_parse (parser
);
15624 if (have_trailing_return_fn_decl
)
15626 type
= make_auto ();
15630 if (cxx_dialect
>= cxx14
)
15632 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
15633 type
= TREE_TYPE (type
);
15636 type
= error_mark_node
;
15638 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
15640 if (cxx_dialect
< cxx14
)
15641 error_at (token
->location
,
15642 "use of %<auto%> in lambda parameter declaration "
15643 "only available with "
15644 "-std=c++14 or -std=gnu++14");
15646 else if (cxx_dialect
< cxx14
)
15647 error_at (token
->location
,
15648 "use of %<auto%> in parameter declaration "
15649 "only available with "
15650 "-std=c++14 or -std=gnu++14");
15652 pedwarn (token
->location
, OPT_Wpedantic
,
15653 "ISO C++ forbids use of %<auto%> in parameter "
15657 type
= make_auto ();
15661 /* Since DR 743, decltype can either be a simple-type-specifier by
15662 itself or begin a nested-name-specifier. Parsing it will replace
15663 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15664 handling below decide what to do. */
15665 cp_parser_decltype (parser
);
15666 cp_lexer_set_token_position (parser
->lexer
, token
);
15670 /* Consume the `typeof' token. */
15671 cp_lexer_consume_token (parser
->lexer
);
15672 /* Parse the operand to `typeof'. */
15673 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
15674 /* If it is not already a TYPE, take its type. */
15675 if (!TYPE_P (type
))
15676 type
= finish_typeof (type
);
15679 cp_parser_set_decl_spec_type (decl_specs
, type
,
15681 /*type_definition_p=*/false);
15685 case RID_UNDERLYING_TYPE
:
15686 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
15688 cp_parser_set_decl_spec_type (decl_specs
, type
,
15690 /*type_definition_p=*/false);
15695 case RID_DIRECT_BASES
:
15696 type
= cp_parser_trait_expr (parser
, token
->keyword
);
15698 cp_parser_set_decl_spec_type (decl_specs
, type
,
15700 /*type_definition_p=*/false);
15706 /* If token is an already-parsed decltype not followed by ::,
15707 it's a simple-type-specifier. */
15708 if (token
->type
== CPP_DECLTYPE
15709 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
15711 type
= token
->u
.value
;
15714 cp_parser_set_decl_spec_type (decl_specs
, type
,
15716 /*type_definition_p=*/false);
15717 /* Remember that we are handling a decltype in order to
15718 implement the resolution of DR 1510 when the argument
15719 isn't instantiation dependent. */
15720 decl_specs
->decltype_p
= true;
15722 cp_lexer_consume_token (parser
->lexer
);
15726 /* If the type-specifier was for a built-in type, we're done. */
15729 /* Record the type. */
15731 && (token
->keyword
!= RID_SIGNED
15732 && token
->keyword
!= RID_UNSIGNED
15733 && token
->keyword
!= RID_SHORT
15734 && token
->keyword
!= RID_LONG
))
15735 cp_parser_set_decl_spec_type (decl_specs
,
15738 /*type_definition_p=*/false);
15740 decl_specs
->any_specifiers_p
= true;
15742 /* Consume the token. */
15743 cp_lexer_consume_token (parser
->lexer
);
15745 if (type
== error_mark_node
)
15746 return error_mark_node
;
15748 /* There is no valid C++ program where a non-template type is
15749 followed by a "<". That usually indicates that the user thought
15750 that the type was a template. */
15751 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
15754 return TYPE_NAME (type
);
15757 /* The type-specifier must be a user-defined type. */
15758 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
15763 /* Don't gobble tokens or issue error messages if this is an
15764 optional type-specifier. */
15765 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
15766 cp_parser_parse_tentatively (parser
);
15768 /* Look for the optional `::' operator. */
15770 = (cp_parser_global_scope_opt (parser
,
15771 /*current_scope_valid_p=*/false)
15773 /* Look for the nested-name specifier. */
15775 = (cp_parser_nested_name_specifier_opt (parser
,
15776 /*typename_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15779 /*is_declaration=*/false)
15781 token
= cp_lexer_peek_token (parser
->lexer
);
15782 /* If we have seen a nested-name-specifier, and the next token
15783 is `template', then we are using the template-id production. */
15785 && cp_parser_optional_template_keyword (parser
))
15787 /* Look for the template-id. */
15788 type
= cp_parser_template_id (parser
,
15789 /*template_keyword_p=*/true,
15790 /*check_dependency_p=*/true,
15792 /*is_declaration=*/false);
15793 /* If the template-id did not name a type, we are out of
15795 if (TREE_CODE (type
) != TYPE_DECL
)
15797 cp_parser_error (parser
, "expected template-id for type");
15801 /* Otherwise, look for a type-name. */
15803 type
= cp_parser_type_name (parser
);
15804 /* Keep track of all name-lookups performed in class scopes. */
15808 && TREE_CODE (type
) == TYPE_DECL
15809 && identifier_p (DECL_NAME (type
)))
15810 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15811 /* If it didn't work out, we don't have a TYPE. */
15812 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15813 && !cp_parser_parse_definitely (parser
))
15815 if (type
&& decl_specs
)
15816 cp_parser_set_decl_spec_type (decl_specs
, type
,
15818 /*type_definition_p=*/false);
15821 /* If we didn't get a type-name, issue an error message. */
15822 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15824 cp_parser_error (parser
, "expected type-name");
15825 return error_mark_node
;
15828 if (type
&& type
!= error_mark_node
)
15830 /* See if TYPE is an Objective-C type, and if so, parse and
15831 accept any protocol references following it. Do this before
15832 the cp_parser_check_for_invalid_template_id() call, because
15833 Objective-C types can be followed by '<...>' which would
15834 enclose protocol names rather than template arguments, and so
15835 everything is fine. */
15836 if (c_dialect_objc () && !parser
->scope
15837 && (objc_is_id (type
) || objc_is_class_name (type
)))
15839 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15840 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15842 /* Clobber the "unqualified" type previously entered into
15843 DECL_SPECS with the new, improved protocol-qualified version. */
15845 decl_specs
->type
= qual_type
;
15850 /* There is no valid C++ program where a non-template type is
15851 followed by a "<". That usually indicates that the user
15852 thought that the type was a template. */
15853 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15861 /* Parse a type-name.
15867 simple-template-id [in c++0x]
15884 Returns a TYPE_DECL for the type. */
15887 cp_parser_type_name (cp_parser
* parser
)
15889 return cp_parser_type_name (parser
, /*typename_keyword_p=*/false);
15894 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
15898 /* We can't know yet whether it is a class-name or not. */
15899 cp_parser_parse_tentatively (parser
);
15900 /* Try a class-name. */
15901 type_decl
= cp_parser_class_name (parser
,
15902 typename_keyword_p
,
15903 /*template_keyword_p=*/false,
15905 /*check_dependency_p=*/true,
15906 /*class_head_p=*/false,
15907 /*is_declaration=*/false);
15908 /* If it's not a class-name, keep looking. */
15909 if (!cp_parser_parse_definitely (parser
))
15911 if (cxx_dialect
< cxx11
)
15912 /* It must be a typedef-name or an enum-name. */
15913 return cp_parser_nonclass_name (parser
);
15915 cp_parser_parse_tentatively (parser
);
15916 /* It is either a simple-template-id representing an
15917 instantiation of an alias template... */
15918 type_decl
= cp_parser_template_id (parser
,
15919 /*template_keyword_p=*/false,
15920 /*check_dependency_p=*/true,
15922 /*is_declaration=*/false);
15923 /* Note that this must be an instantiation of an alias template
15924 because [temp.names]/6 says:
15926 A template-id that names an alias template specialization
15929 Whereas [temp.names]/7 says:
15931 A simple-template-id that names a class template
15932 specialization is a class-name.
15934 With concepts, this could also be a partial-concept-id that
15935 declares a non-type template parameter. */
15936 if (type_decl
!= NULL_TREE
15937 && TREE_CODE (type_decl
) == TYPE_DECL
15938 && TYPE_DECL_ALIAS_P (type_decl
))
15939 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15940 else if (is_constrained_parameter (type_decl
))
15941 /* Don't do anything. */ ;
15943 cp_parser_simulate_error (parser
);
15945 if (!cp_parser_parse_definitely (parser
))
15946 /* ... Or a typedef-name or an enum-name. */
15947 return cp_parser_nonclass_name (parser
);
15953 /* Check if DECL and ARGS can form a constrained-type-specifier.
15954 If ARGS is non-null, we try to form a concept check of the
15955 form DECL<?, ARGS> where ? is a wildcard that matches any
15956 kind of template argument. If ARGS is NULL, then we try to
15957 form a concept check of the form DECL<?>. */
15960 cp_parser_maybe_constrained_type_specifier (cp_parser
*parser
,
15961 tree decl
, tree args
)
15963 gcc_assert (args
? TREE_CODE (args
) == TREE_VEC
: true);
15965 /* If we a constrained-type-specifier cannot be deduced. */
15966 if (parser
->prevent_constrained_type_specifiers
)
15969 /* A constrained type specifier can only be found in an
15970 overload set or as a reference to a template declaration.
15972 FIXME: This might be masking a bug. It's possible that
15973 that the deduction below is causing template specializations
15974 to be formed with the wildcard as an argument. */
15975 if (TREE_CODE (decl
) != OVERLOAD
&& TREE_CODE (decl
) != TEMPLATE_DECL
)
15978 /* Try to build a call expression that evaluates the
15979 concept. This can fail if the overload set refers
15980 only to non-templates. */
15981 tree placeholder
= build_nt (WILDCARD_DECL
);
15982 tree check
= build_concept_check (decl
, placeholder
, args
);
15983 if (check
== error_mark_node
)
15986 /* Deduce the checked constraint and the prototype parameter.
15988 FIXME: In certain cases, failure to deduce should be a
15989 diagnosable error. */
15992 if (!deduce_constrained_parameter (check
, conc
, proto
))
15995 /* In template parameter scope, this results in a constrained
15996 parameter. Return a descriptor of that parm. */
15997 if (processing_template_parmlist
)
15998 return build_constrained_parameter (conc
, proto
, args
);
16000 /* In a parameter-declaration-clause, constrained-type
16001 specifiers result in invented template parameters. */
16002 if (parser
->auto_is_implicit_function_template_parm_p
)
16004 tree x
= build_constrained_parameter (conc
, proto
, args
);
16005 return synthesize_implicit_template_parm (parser
, x
);
16009 /* Otherwise, we're in a context where the constrained
16010 type name is deduced and the constraint applies
16011 after deduction. */
16012 return make_constrained_auto (conc
, args
);
16018 /* If DECL refers to a concept, return a TYPE_DECL representing
16019 the result of using the constrained type specifier in the
16020 current context. DECL refers to a concept if
16022 - it is an overload set containing a function concept taking a single
16025 - it is a variable concept taking a single type argument. */
16028 cp_parser_maybe_concept_name (cp_parser
* parser
, tree decl
)
16031 && (TREE_CODE (decl
) == OVERLOAD
16032 || BASELINK_P (decl
)
16033 || variable_concept_p (decl
)))
16034 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, NULL_TREE
);
16039 /* Check if DECL and ARGS form a partial-concept-id. If so,
16040 assign ID to the resulting constrained placeholder.
16042 Returns true if the partial-concept-id designates a placeholder
16043 and false otherwise. Note that *id is set to NULL_TREE in
16047 cp_parser_maybe_partial_concept_id (cp_parser
*parser
, tree decl
, tree args
)
16049 return cp_parser_maybe_constrained_type_specifier (parser
, decl
, args
);
16052 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16064 Returns a TYPE_DECL for the type. */
16067 cp_parser_nonclass_name (cp_parser
* parser
)
16072 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16073 identifier
= cp_parser_identifier (parser
);
16074 if (identifier
== error_mark_node
)
16075 return error_mark_node
;
16077 /* Look up the type-name. */
16078 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
16080 type_decl
= strip_using_decl (type_decl
);
16082 /* If we found an overload set, then it may refer to a concept-name. */
16083 if (tree decl
= cp_parser_maybe_concept_name (parser
, type_decl
))
16086 if (TREE_CODE (type_decl
) != TYPE_DECL
16087 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
16089 /* See if this is an Objective-C type. */
16090 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
16091 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
16093 type_decl
= TYPE_NAME (type
);
16096 /* Issue an error if we did not find a type-name. */
16097 if (TREE_CODE (type_decl
) != TYPE_DECL
16098 /* In Objective-C, we have the complication that class names are
16099 normally type names and start declarations (eg, the
16100 "NSObject" in "NSObject *object;"), but can be used in an
16101 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16102 is an expression. So, a classname followed by a dot is not a
16103 valid type-name. */
16104 || (objc_is_class_name (TREE_TYPE (type_decl
))
16105 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
16107 if (!cp_parser_simulate_error (parser
))
16108 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
16109 NLE_TYPE
, token
->location
);
16110 return error_mark_node
;
16112 /* Remember that the name was used in the definition of the
16113 current class so that we can check later to see if the
16114 meaning would have been different after the class was
16115 entirely defined. */
16116 else if (type_decl
!= error_mark_node
16118 maybe_note_name_used_in_class (identifier
, type_decl
);
16123 /* Parse an elaborated-type-specifier. Note that the grammar given
16124 here incorporates the resolution to DR68.
16126 elaborated-type-specifier:
16127 class-key :: [opt] nested-name-specifier [opt] identifier
16128 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16129 enum-key :: [opt] nested-name-specifier [opt] identifier
16130 typename :: [opt] nested-name-specifier identifier
16131 typename :: [opt] nested-name-specifier template [opt]
16136 elaborated-type-specifier:
16137 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16138 class-key attributes :: [opt] nested-name-specifier [opt]
16139 template [opt] template-id
16140 enum attributes :: [opt] nested-name-specifier [opt] identifier
16142 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16143 declared `friend'. If IS_DECLARATION is TRUE, then this
16144 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16145 something is being declared.
16147 Returns the TYPE specified. */
16150 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
16152 bool is_declaration
)
16154 enum tag_types tag_type
;
16156 tree type
= NULL_TREE
;
16157 tree attributes
= NULL_TREE
;
16159 cp_token
*token
= NULL
;
16161 /* See if we're looking at the `enum' keyword. */
16162 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
16164 /* Consume the `enum' token. */
16165 cp_lexer_consume_token (parser
->lexer
);
16166 /* Remember that it's an enumeration type. */
16167 tag_type
= enum_type
;
16168 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16169 enums) is used here. */
16170 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
16171 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
16173 pedwarn (input_location
, 0, "elaborated-type-specifier "
16174 "for a scoped enum must not use the %<%D%> keyword",
16175 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
16176 /* Consume the `struct' or `class' and parse it anyway. */
16177 cp_lexer_consume_token (parser
->lexer
);
16179 /* Parse the attributes. */
16180 attributes
= cp_parser_attributes_opt (parser
);
16182 /* Or, it might be `typename'. */
16183 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
16186 /* Consume the `typename' token. */
16187 cp_lexer_consume_token (parser
->lexer
);
16188 /* Remember that it's a `typename' type. */
16189 tag_type
= typename_type
;
16191 /* Otherwise it must be a class-key. */
16194 tag_type
= cp_parser_class_key (parser
);
16195 if (tag_type
== none_type
)
16196 return error_mark_node
;
16197 /* Parse the attributes. */
16198 attributes
= cp_parser_attributes_opt (parser
);
16201 /* Look for the `::' operator. */
16202 globalscope
= cp_parser_global_scope_opt (parser
,
16203 /*current_scope_valid_p=*/false);
16204 /* Look for the nested-name-specifier. */
16205 if (tag_type
== typename_type
&& !globalscope
)
16207 if (!cp_parser_nested_name_specifier (parser
,
16208 /*typename_keyword_p=*/true,
16209 /*check_dependency_p=*/true,
16212 return error_mark_node
;
16215 /* Even though `typename' is not present, the proposed resolution
16216 to Core Issue 180 says that in `class A<T>::B', `B' should be
16217 considered a type-name, even if `A<T>' is dependent. */
16218 cp_parser_nested_name_specifier_opt (parser
,
16219 /*typename_keyword_p=*/true,
16220 /*check_dependency_p=*/true,
16223 /* For everything but enumeration types, consider a template-id.
16224 For an enumeration type, consider only a plain identifier. */
16225 if (tag_type
!= enum_type
)
16227 bool template_p
= false;
16230 /* Allow the `template' keyword. */
16231 template_p
= cp_parser_optional_template_keyword (parser
);
16232 /* If we didn't see `template', we don't know if there's a
16233 template-id or not. */
16235 cp_parser_parse_tentatively (parser
);
16236 /* Parse the template-id. */
16237 token
= cp_lexer_peek_token (parser
->lexer
);
16238 decl
= cp_parser_template_id (parser
, template_p
,
16239 /*check_dependency_p=*/true,
16242 /* If we didn't find a template-id, look for an ordinary
16244 if (!template_p
&& !cp_parser_parse_definitely (parser
))
16246 /* We can get here when cp_parser_template_id, called by
16247 cp_parser_class_name with tag_type == none_type, succeeds
16248 and caches a BASELINK. Then, when called again here,
16249 instead of failing and returning an error_mark_node
16250 returns it (see template/typename17.C in C++11).
16251 ??? Could we diagnose this earlier? */
16252 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
16254 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
16255 type
= error_mark_node
;
16257 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16258 in effect, then we must assume that, upon instantiation, the
16259 template will correspond to a class. */
16260 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
16261 && tag_type
== typename_type
)
16262 type
= make_typename_type (parser
->scope
, decl
,
16264 /*complain=*/tf_error
);
16265 /* If the `typename' keyword is in effect and DECL is not a type
16266 decl, then type is non existent. */
16267 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
16269 else if (TREE_CODE (decl
) == TYPE_DECL
)
16270 type
= check_elaborated_type_specifier (tag_type
, decl
,
16271 /*allow_template_p=*/true);
16272 else if (decl
== error_mark_node
)
16273 type
= error_mark_node
;
16278 token
= cp_lexer_peek_token (parser
->lexer
);
16279 identifier
= cp_parser_identifier (parser
);
16281 if (identifier
== error_mark_node
)
16283 parser
->scope
= NULL_TREE
;
16284 return error_mark_node
;
16287 /* For a `typename', we needn't call xref_tag. */
16288 if (tag_type
== typename_type
16289 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
16290 return cp_parser_make_typename_type (parser
, identifier
,
16293 /* Template parameter lists apply only if we are not within a
16294 function parameter list. */
16295 bool template_parm_lists_apply
16296 = parser
->num_template_parameter_lists
;
16297 if (template_parm_lists_apply
)
16298 for (cp_binding_level
*s
= current_binding_level
;
16299 s
&& s
->kind
!= sk_template_parms
;
16300 s
= s
->level_chain
)
16301 if (s
->kind
== sk_function_parms
)
16302 template_parm_lists_apply
= false;
16304 /* Look up a qualified name in the usual way. */
16308 tree ambiguous_decls
;
16310 decl
= cp_parser_lookup_name (parser
, identifier
,
16312 /*is_template=*/false,
16313 /*is_namespace=*/false,
16314 /*check_dependency=*/true,
16318 /* If the lookup was ambiguous, an error will already have been
16320 if (ambiguous_decls
)
16321 return error_mark_node
;
16323 /* If we are parsing friend declaration, DECL may be a
16324 TEMPLATE_DECL tree node here. However, we need to check
16325 whether this TEMPLATE_DECL results in valid code. Consider
16326 the following example:
16329 template <class T> class C {};
16332 template <class T> friend class N::C; // #1, valid code
16334 template <class T> class Y {
16335 friend class N::C; // #2, invalid code
16338 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16339 name lookup of `N::C'. We see that friend declaration must
16340 be template for the code to be valid. Note that
16341 processing_template_decl does not work here since it is
16342 always 1 for the above two cases. */
16344 decl
= (cp_parser_maybe_treat_template_as_class
16345 (decl
, /*tag_name_p=*/is_friend
16346 && template_parm_lists_apply
));
16348 if (TREE_CODE (decl
) != TYPE_DECL
)
16350 cp_parser_diagnose_invalid_type_name (parser
,
16353 return error_mark_node
;
16356 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
16358 bool allow_template
= (template_parm_lists_apply
16359 || DECL_SELF_REFERENCE_P (decl
));
16360 type
= check_elaborated_type_specifier (tag_type
, decl
,
16363 if (type
== error_mark_node
)
16364 return error_mark_node
;
16367 /* Forward declarations of nested types, such as
16372 are invalid unless all components preceding the final '::'
16373 are complete. If all enclosing types are complete, these
16374 declarations become merely pointless.
16376 Invalid forward declarations of nested types are errors
16377 caught elsewhere in parsing. Those that are pointless arrive
16380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
16381 && !is_friend
&& !processing_explicit_instantiation
)
16382 warning (0, "declaration %qD does not declare anything", decl
);
16384 type
= TREE_TYPE (decl
);
16388 /* An elaborated-type-specifier sometimes introduces a new type and
16389 sometimes names an existing type. Normally, the rule is that it
16390 introduces a new type only if there is not an existing type of
16391 the same name already in scope. For example, given:
16394 void f() { struct S s; }
16396 the `struct S' in the body of `f' is the same `struct S' as in
16397 the global scope; the existing definition is used. However, if
16398 there were no global declaration, this would introduce a new
16399 local class named `S'.
16401 An exception to this rule applies to the following code:
16403 namespace N { struct S; }
16405 Here, the elaborated-type-specifier names a new type
16406 unconditionally; even if there is already an `S' in the
16407 containing scope this declaration names a new type.
16408 This exception only applies if the elaborated-type-specifier
16409 forms the complete declaration:
16413 A declaration consisting solely of `class-key identifier ;' is
16414 either a redeclaration of the name in the current scope or a
16415 forward declaration of the identifier as a class name. It
16416 introduces the name into the current scope.
16418 We are in this situation precisely when the next token is a `;'.
16420 An exception to the exception is that a `friend' declaration does
16421 *not* name a new type; i.e., given:
16423 struct S { friend struct T; };
16425 `T' is not a new type in the scope of `S'.
16427 Also, `new struct S' or `sizeof (struct S)' never results in the
16428 definition of a new type; a new type can only be declared in a
16429 declaration context. */
16435 /* Friends have special name lookup rules. */
16436 ts
= ts_within_enclosing_non_class
;
16437 else if (is_declaration
16438 && cp_lexer_next_token_is (parser
->lexer
,
16440 /* This is a `class-key identifier ;' */
16446 (template_parm_lists_apply
16447 && (cp_parser_next_token_starts_class_definition_p (parser
)
16448 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
16449 /* An unqualified name was used to reference this type, so
16450 there were no qualifying templates. */
16451 if (template_parm_lists_apply
16452 && !cp_parser_check_template_parameters (parser
,
16453 /*num_templates=*/0,
16455 /*declarator=*/NULL
))
16456 return error_mark_node
;
16457 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
16461 if (type
== error_mark_node
)
16462 return error_mark_node
;
16464 /* Allow attributes on forward declarations of classes. */
16467 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16468 warning (OPT_Wattributes
,
16469 "attributes ignored on uninstantiated type");
16470 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
16471 && ! processing_explicit_instantiation
)
16472 warning (OPT_Wattributes
,
16473 "attributes ignored on template instantiation");
16474 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
16475 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
16477 warning (OPT_Wattributes
,
16478 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16481 if (tag_type
!= enum_type
)
16483 /* Indicate whether this class was declared as a `class' or as a
16485 if (TREE_CODE (type
) == RECORD_TYPE
)
16486 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
16487 cp_parser_check_class_key (tag_type
, type
);
16490 /* A "<" cannot follow an elaborated type specifier. If that
16491 happens, the user was probably trying to form a template-id. */
16492 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
16498 /* Parse an enum-specifier.
16501 enum-head { enumerator-list [opt] }
16502 enum-head { enumerator-list , } [C++0x]
16505 enum-key identifier [opt] enum-base [opt]
16506 enum-key nested-name-specifier identifier enum-base [opt]
16511 enum struct [C++0x]
16514 : type-specifier-seq
16516 opaque-enum-specifier:
16517 enum-key identifier enum-base [opt] ;
16520 enum-key attributes[opt] identifier [opt] enum-base [opt]
16521 { enumerator-list [opt] }attributes[opt]
16522 enum-key attributes[opt] identifier [opt] enum-base [opt]
16523 { enumerator-list, }attributes[opt] [C++0x]
16525 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16526 if the token stream isn't an enum-specifier after all. */
16529 cp_parser_enum_specifier (cp_parser
* parser
)
16532 tree type
= NULL_TREE
;
16534 tree nested_name_specifier
= NULL_TREE
;
16536 bool scoped_enum_p
= false;
16537 bool has_underlying_type
= false;
16538 bool nested_being_defined
= false;
16539 bool new_value_list
= false;
16540 bool is_new_type
= false;
16541 bool is_anonymous
= false;
16542 tree underlying_type
= NULL_TREE
;
16543 cp_token
*type_start_token
= NULL
;
16544 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
16546 parser
->colon_corrects_to_scope_p
= false;
16548 /* Parse tentatively so that we can back up if we don't find a
16550 cp_parser_parse_tentatively (parser
);
16552 /* Caller guarantees that the current token is 'enum', an identifier
16553 possibly follows, and the token after that is an opening brace.
16554 If we don't have an identifier, fabricate an anonymous name for
16555 the enumeration being defined. */
16556 cp_lexer_consume_token (parser
->lexer
);
16558 /* Parse the "class" or "struct", which indicates a scoped
16559 enumeration type in C++0x. */
16560 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
16561 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
16563 if (cxx_dialect
< cxx11
)
16564 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
16566 /* Consume the `struct' or `class' token. */
16567 cp_lexer_consume_token (parser
->lexer
);
16569 scoped_enum_p
= true;
16572 attributes
= cp_parser_attributes_opt (parser
);
16574 /* Clear the qualification. */
16575 parser
->scope
= NULL_TREE
;
16576 parser
->qualifying_scope
= NULL_TREE
;
16577 parser
->object_scope
= NULL_TREE
;
16579 /* Figure out in what scope the declaration is being placed. */
16580 prev_scope
= current_scope ();
16582 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
16584 push_deferring_access_checks (dk_no_check
);
16585 nested_name_specifier
16586 = cp_parser_nested_name_specifier_opt (parser
,
16587 /*typename_keyword_p=*/true,
16588 /*check_dependency_p=*/false,
16590 /*is_declaration=*/false);
16592 if (nested_name_specifier
)
16596 identifier
= cp_parser_identifier (parser
);
16597 name
= cp_parser_lookup_name (parser
, identifier
,
16599 /*is_template=*/false,
16600 /*is_namespace=*/false,
16601 /*check_dependency=*/true,
16602 /*ambiguous_decls=*/NULL
,
16604 if (name
&& name
!= error_mark_node
)
16606 type
= TREE_TYPE (name
);
16607 if (TREE_CODE (type
) == TYPENAME_TYPE
)
16609 /* Are template enums allowed in ISO? */
16610 if (template_parm_scope_p ())
16611 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
16612 "%qD is an enumeration template", name
);
16613 /* ignore a typename reference, for it will be solved by name
16618 else if (nested_name_specifier
== error_mark_node
)
16619 /* We already issued an error. */;
16622 error_at (type_start_token
->location
,
16623 "%qD does not name an enumeration in %qT",
16624 identifier
, nested_name_specifier
);
16625 nested_name_specifier
= error_mark_node
;
16630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16631 identifier
= cp_parser_identifier (parser
);
16634 identifier
= make_anon_name ();
16635 is_anonymous
= true;
16637 error_at (type_start_token
->location
,
16638 "anonymous scoped enum is not allowed");
16641 pop_deferring_access_checks ();
16643 /* Check for the `:' that denotes a specified underlying type in C++0x.
16644 Note that a ':' could also indicate a bitfield width, however. */
16645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16647 cp_decl_specifier_seq type_specifiers
;
16649 /* Consume the `:'. */
16650 cp_lexer_consume_token (parser
->lexer
);
16652 /* Parse the type-specifier-seq. */
16653 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
16654 /*is_trailing_return=*/false,
16657 /* At this point this is surely not elaborated type specifier. */
16658 if (!cp_parser_parse_definitely (parser
))
16661 if (cxx_dialect
< cxx11
)
16662 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
16664 has_underlying_type
= true;
16666 /* If that didn't work, stop. */
16667 if (type_specifiers
.type
!= error_mark_node
)
16669 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
16670 /*initialized=*/0, NULL
);
16671 if (underlying_type
== error_mark_node
16672 || check_for_bare_parameter_packs (underlying_type
))
16673 underlying_type
= NULL_TREE
;
16677 /* Look for the `{' but don't consume it yet. */
16678 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16680 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
16682 cp_parser_error (parser
, "expected %<{%>");
16683 if (has_underlying_type
)
16689 /* An opaque-enum-specifier must have a ';' here. */
16690 if ((scoped_enum_p
|| underlying_type
)
16691 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16693 cp_parser_error (parser
, "expected %<;%> or %<{%>");
16694 if (has_underlying_type
)
16702 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
16705 if (nested_name_specifier
)
16707 if (CLASS_TYPE_P (nested_name_specifier
))
16709 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
16710 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
16711 push_scope (nested_name_specifier
);
16713 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16715 push_nested_namespace (nested_name_specifier
);
16719 /* Issue an error message if type-definitions are forbidden here. */
16720 if (!cp_parser_check_type_definition (parser
))
16721 type
= error_mark_node
;
16723 /* Create the new type. We do this before consuming the opening
16724 brace so the enum will be recorded as being on the line of its
16725 tag (or the 'enum' keyword, if there is no tag). */
16726 type
= start_enum (identifier
, type
, underlying_type
,
16727 scoped_enum_p
, &is_new_type
);
16729 /* If the next token is not '{' it is an opaque-enum-specifier or an
16730 elaborated-type-specifier. */
16731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16733 timevar_push (TV_PARSE_ENUM
);
16734 if (nested_name_specifier
16735 && nested_name_specifier
!= error_mark_node
)
16737 /* The following catches invalid code such as:
16738 enum class S<int>::E { A, B, C }; */
16739 if (!processing_specialization
16740 && CLASS_TYPE_P (nested_name_specifier
)
16741 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
16742 error_at (type_start_token
->location
, "cannot add an enumerator "
16743 "list to a template instantiation");
16745 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
16747 error_at (type_start_token
->location
,
16748 "%<%T::%E%> has not been declared",
16749 TYPE_CONTEXT (nested_name_specifier
),
16750 nested_name_specifier
);
16751 type
= error_mark_node
;
16753 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
16754 && !CLASS_TYPE_P (nested_name_specifier
))
16756 error_at (type_start_token
->location
, "nested name specifier "
16757 "%qT for enum declaration does not name a class "
16758 "or namespace", nested_name_specifier
);
16759 type
= error_mark_node
;
16761 /* If that scope does not contain the scope in which the
16762 class was originally declared, the program is invalid. */
16763 else if (prev_scope
&& !is_ancestor (prev_scope
,
16764 nested_name_specifier
))
16766 if (at_namespace_scope_p ())
16767 error_at (type_start_token
->location
,
16768 "declaration of %qD in namespace %qD which does not "
16770 type
, prev_scope
, nested_name_specifier
);
16772 error_at (type_start_token
->location
,
16773 "declaration of %qD in %qD which does not "
16775 type
, prev_scope
, nested_name_specifier
);
16776 type
= error_mark_node
;
16781 begin_scope (sk_scoped_enum
, type
);
16783 /* Consume the opening brace. */
16784 cp_lexer_consume_token (parser
->lexer
);
16786 if (type
== error_mark_node
)
16787 ; /* Nothing to add */
16788 else if (OPAQUE_ENUM_P (type
)
16789 || (cxx_dialect
> cxx98
&& processing_specialization
))
16791 new_value_list
= true;
16792 SET_OPAQUE_ENUM_P (type
, false);
16793 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
16797 error_at (type_start_token
->location
,
16798 "multiple definition of %q#T", type
);
16799 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
16800 "previous definition here");
16801 type
= error_mark_node
;
16804 if (type
== error_mark_node
)
16805 cp_parser_skip_to_end_of_block_or_statement (parser
);
16806 /* If the next token is not '}', then there are some enumerators. */
16807 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16809 if (is_anonymous
&& !scoped_enum_p
)
16810 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
16811 "ISO C++ forbids empty anonymous enum");
16814 cp_parser_enumerator_list (parser
, type
);
16816 /* Consume the final '}'. */
16817 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16821 timevar_pop (TV_PARSE_ENUM
);
16825 /* If a ';' follows, then it is an opaque-enum-specifier
16826 and additional restrictions apply. */
16827 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16830 error_at (type_start_token
->location
,
16831 "opaque-enum-specifier without name");
16832 else if (nested_name_specifier
)
16833 error_at (type_start_token
->location
,
16834 "opaque-enum-specifier must use a simple identifier");
16838 /* Look for trailing attributes to apply to this enumeration, and
16839 apply them if appropriate. */
16840 if (cp_parser_allow_gnu_extensions_p (parser
))
16842 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
16843 trailing_attr
= chainon (trailing_attr
, attributes
);
16844 cplus_decl_attributes (&type
,
16846 (int) ATTR_FLAG_TYPE_IN_PLACE
);
16849 /* Finish up the enumeration. */
16850 if (type
!= error_mark_node
)
16852 if (new_value_list
)
16853 finish_enum_value_list (type
);
16855 finish_enum (type
);
16858 if (nested_name_specifier
)
16860 if (CLASS_TYPE_P (nested_name_specifier
))
16862 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
16863 pop_scope (nested_name_specifier
);
16865 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16867 pop_nested_namespace (nested_name_specifier
);
16871 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
16875 /* Parse an enumerator-list. The enumerators all have the indicated
16879 enumerator-definition
16880 enumerator-list , enumerator-definition */
16883 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
16887 /* Parse an enumerator-definition. */
16888 cp_parser_enumerator_definition (parser
, type
);
16890 /* If the next token is not a ',', we've reached the end of
16892 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16894 /* Otherwise, consume the `,' and keep going. */
16895 cp_lexer_consume_token (parser
->lexer
);
16896 /* If the next token is a `}', there is a trailing comma. */
16897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16899 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
16900 pedwarn (input_location
, OPT_Wpedantic
,
16901 "comma at end of enumerator list");
16907 /* Parse an enumerator-definition. The enumerator has the indicated
16910 enumerator-definition:
16912 enumerator = constant-expression
16919 enumerator-definition:
16920 enumerator attributes [opt]
16921 enumerator attributes [opt] = constant-expression */
16924 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16930 /* Save the input location because we are interested in the location
16931 of the identifier and not the location of the explicit value. */
16932 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16934 /* Look for the identifier. */
16935 identifier
= cp_parser_identifier (parser
);
16936 if (identifier
== error_mark_node
)
16939 /* Parse any specified attributes. */
16940 tree attrs
= cp_parser_attributes_opt (parser
);
16942 /* If the next token is an '=', then there is an explicit value. */
16943 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16945 /* Consume the `=' token. */
16946 cp_lexer_consume_token (parser
->lexer
);
16947 /* Parse the value. */
16948 value
= cp_parser_constant_expression (parser
);
16953 /* If we are processing a template, make sure the initializer of the
16954 enumerator doesn't contain any bare template parameter pack. */
16955 if (check_for_bare_parameter_packs (value
))
16956 value
= error_mark_node
;
16958 /* Create the enumerator. */
16959 build_enumerator (identifier
, value
, type
, attrs
, loc
);
16962 /* Parse a namespace-name.
16965 original-namespace-name
16968 Returns the NAMESPACE_DECL for the namespace. */
16971 cp_parser_namespace_name (cp_parser
* parser
)
16974 tree namespace_decl
;
16976 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16978 /* Get the name of the namespace. */
16979 identifier
= cp_parser_identifier (parser
);
16980 if (identifier
== error_mark_node
)
16981 return error_mark_node
;
16983 /* Look up the identifier in the currently active scope. Look only
16984 for namespaces, due to:
16986 [basic.lookup.udir]
16988 When looking up a namespace-name in a using-directive or alias
16989 definition, only namespace names are considered.
16993 [basic.lookup.qual]
16995 During the lookup of a name preceding the :: scope resolution
16996 operator, object, function, and enumerator names are ignored.
16998 (Note that cp_parser_qualifying_entity only calls this
16999 function if the token after the name is the scope resolution
17001 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
17003 /*is_template=*/false,
17004 /*is_namespace=*/true,
17005 /*check_dependency=*/true,
17006 /*ambiguous_decls=*/NULL
,
17008 /* If it's not a namespace, issue an error. */
17009 if (namespace_decl
== error_mark_node
17010 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
17012 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
17013 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
17014 cp_parser_error (parser
, "expected namespace-name");
17015 namespace_decl
= error_mark_node
;
17018 return namespace_decl
;
17021 /* Parse a namespace-definition.
17023 namespace-definition:
17024 named-namespace-definition
17025 unnamed-namespace-definition
17027 named-namespace-definition:
17028 original-namespace-definition
17029 extension-namespace-definition
17031 original-namespace-definition:
17032 namespace identifier { namespace-body }
17034 extension-namespace-definition:
17035 namespace original-namespace-name { namespace-body }
17037 unnamed-namespace-definition:
17038 namespace { namespace-body } */
17041 cp_parser_namespace_definition (cp_parser
* parser
)
17043 tree identifier
, attribs
;
17044 bool has_visibility
;
17047 int nested_definition_count
= 0;
17049 cp_ensure_no_omp_declare_simd (parser
);
17050 cp_ensure_no_oacc_routine (parser
);
17051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
17053 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
17055 cp_lexer_consume_token (parser
->lexer
);
17060 /* Look for the `namespace' keyword. */
17061 token
= cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17063 /* Parse any specified attributes before the identifier. */
17064 attribs
= cp_parser_attributes_opt (parser
);
17066 /* Get the name of the namespace. We do not attempt to distinguish
17067 between an original-namespace-definition and an
17068 extension-namespace-definition at this point. The semantic
17069 analysis routines are responsible for that. */
17070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17071 identifier
= cp_parser_identifier (parser
);
17073 identifier
= NULL_TREE
;
17075 /* Parse any specified attributes after the identifier. */
17076 tree post_ident_attribs
= cp_parser_attributes_opt (parser
);
17077 if (post_ident_attribs
)
17080 attribs
= chainon (attribs
, post_ident_attribs
);
17082 attribs
= post_ident_attribs
;
17085 /* Start the namespace. */
17086 push_namespace (identifier
);
17088 /* Parse any nested namespace definition. */
17089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17092 error_at (token
->location
, "a nested namespace definition cannot have attributes");
17093 if (cxx_dialect
< cxx1z
)
17094 pedwarn (input_location
, OPT_Wpedantic
,
17095 "nested namespace definitions only available with "
17096 "-std=c++1z or -std=gnu++1z");
17098 error_at (token
->location
, "a nested namespace definition cannot be inline");
17099 while (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17101 cp_lexer_consume_token (parser
->lexer
);
17102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17103 identifier
= cp_parser_identifier (parser
);
17106 cp_parser_error (parser
, "nested identifier required");
17109 ++nested_definition_count
;
17110 push_namespace (identifier
);
17114 /* Look for the `{' to validate starting the namespace. */
17115 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
17117 /* "inline namespace" is equivalent to a stub namespace definition
17118 followed by a strong using directive. */
17121 tree name_space
= current_namespace
;
17122 /* Set up namespace association. */
17123 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
17124 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
17125 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
17126 /* Import the contents of the inline namespace. */
17128 do_using_directive (name_space
);
17129 push_namespace (identifier
);
17132 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
17134 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
17136 /* Parse the body of the namespace. */
17137 cp_parser_namespace_body (parser
);
17139 if (has_visibility
)
17140 pop_visibility (1);
17142 /* Finish the nested namespace definitions. */
17143 while (nested_definition_count
--)
17146 /* Finish the namespace. */
17148 /* Look for the final `}'. */
17149 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
17152 /* Parse a namespace-body.
17155 declaration-seq [opt] */
17158 cp_parser_namespace_body (cp_parser
* parser
)
17160 cp_parser_declaration_seq_opt (parser
);
17163 /* Parse a namespace-alias-definition.
17165 namespace-alias-definition:
17166 namespace identifier = qualified-namespace-specifier ; */
17169 cp_parser_namespace_alias_definition (cp_parser
* parser
)
17172 tree namespace_specifier
;
17174 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17176 /* Look for the `namespace' keyword. */
17177 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17178 /* Look for the identifier. */
17179 identifier
= cp_parser_identifier (parser
);
17180 if (identifier
== error_mark_node
)
17182 /* Look for the `=' token. */
17183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
17184 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17186 error_at (token
->location
, "%<namespace%> definition is not allowed here");
17187 /* Skip the definition. */
17188 cp_lexer_consume_token (parser
->lexer
);
17189 if (cp_parser_skip_to_closing_brace (parser
))
17190 cp_lexer_consume_token (parser
->lexer
);
17193 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17194 /* Look for the qualified-namespace-specifier. */
17195 namespace_specifier
17196 = cp_parser_qualified_namespace_specifier (parser
);
17197 /* Look for the `;' token. */
17198 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17200 /* Register the alias in the symbol table. */
17201 do_namespace_alias (identifier
, namespace_specifier
);
17204 /* Parse a qualified-namespace-specifier.
17206 qualified-namespace-specifier:
17207 :: [opt] nested-name-specifier [opt] namespace-name
17209 Returns a NAMESPACE_DECL corresponding to the specified
17213 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
17215 /* Look for the optional `::'. */
17216 cp_parser_global_scope_opt (parser
,
17217 /*current_scope_valid_p=*/false);
17219 /* Look for the optional nested-name-specifier. */
17220 cp_parser_nested_name_specifier_opt (parser
,
17221 /*typename_keyword_p=*/false,
17222 /*check_dependency_p=*/true,
17224 /*is_declaration=*/true);
17226 return cp_parser_namespace_name (parser
);
17229 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17230 access declaration.
17233 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17234 using :: unqualified-id ;
17236 access-declaration:
17242 cp_parser_using_declaration (cp_parser
* parser
,
17243 bool access_declaration_p
)
17246 bool typename_p
= false;
17247 bool global_scope_p
;
17251 int oldcount
= errorcount
;
17252 cp_token
*diag_token
= NULL
;
17254 if (access_declaration_p
)
17256 diag_token
= cp_lexer_peek_token (parser
->lexer
);
17257 cp_parser_parse_tentatively (parser
);
17261 /* Look for the `using' keyword. */
17262 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17264 /* Peek at the next token. */
17265 token
= cp_lexer_peek_token (parser
->lexer
);
17266 /* See if it's `typename'. */
17267 if (token
->keyword
== RID_TYPENAME
)
17269 /* Remember that we've seen it. */
17271 /* Consume the `typename' token. */
17272 cp_lexer_consume_token (parser
->lexer
);
17276 /* Look for the optional global scope qualification. */
17278 = (cp_parser_global_scope_opt (parser
,
17279 /*current_scope_valid_p=*/false)
17282 /* If we saw `typename', or didn't see `::', then there must be a
17283 nested-name-specifier present. */
17284 if (typename_p
|| !global_scope_p
)
17286 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
17287 /*check_dependency_p=*/true,
17289 /*is_declaration=*/true);
17290 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
17292 cp_parser_skip_to_end_of_block_or_statement (parser
);
17296 /* Otherwise, we could be in either of the two productions. In that
17297 case, treat the nested-name-specifier as optional. */
17299 qscope
= cp_parser_nested_name_specifier_opt (parser
,
17300 /*typename_keyword_p=*/false,
17301 /*check_dependency_p=*/true,
17303 /*is_declaration=*/true);
17305 qscope
= global_namespace
;
17306 else if (UNSCOPED_ENUM_P (qscope
))
17307 qscope
= CP_TYPE_CONTEXT (qscope
);
17309 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
17310 /* Something has already gone wrong; there's no need to parse
17311 further. Since an error has occurred, the return value of
17312 cp_parser_parse_definitely will be false, as required. */
17313 return cp_parser_parse_definitely (parser
);
17315 token
= cp_lexer_peek_token (parser
->lexer
);
17316 /* Parse the unqualified-id. */
17317 identifier
= cp_parser_unqualified_id (parser
,
17318 /*template_keyword_p=*/false,
17319 /*check_dependency_p=*/true,
17320 /*declarator_p=*/true,
17321 /*optional_p=*/false);
17323 if (access_declaration_p
)
17325 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
17326 cp_parser_simulate_error (parser
);
17327 if (!cp_parser_parse_definitely (parser
))
17331 /* The function we call to handle a using-declaration is different
17332 depending on what scope we are in. */
17333 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
17335 else if (!identifier_p (identifier
)
17336 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
17337 /* [namespace.udecl]
17339 A using declaration shall not name a template-id. */
17340 error_at (token
->location
,
17341 "a template-id may not appear in a using-declaration");
17344 if (at_class_scope_p ())
17346 /* Create the USING_DECL. */
17347 decl
= do_class_using_decl (parser
->scope
, identifier
);
17349 if (decl
&& typename_p
)
17350 USING_DECL_TYPENAME_P (decl
) = 1;
17352 if (check_for_bare_parameter_packs (decl
))
17354 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17358 /* Add it to the list of members in this class. */
17359 finish_member_declaration (decl
);
17363 decl
= cp_parser_lookup_name_simple (parser
,
17366 if (decl
== error_mark_node
)
17367 cp_parser_name_lookup_error (parser
, identifier
,
17370 else if (check_for_bare_parameter_packs (decl
))
17372 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17375 else if (!at_namespace_scope_p ())
17376 do_local_using_decl (decl
, qscope
, identifier
);
17378 do_toplevel_using_decl (decl
, qscope
, identifier
);
17382 /* Look for the final `;'. */
17383 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17385 if (access_declaration_p
&& errorcount
== oldcount
)
17386 warning_at (diag_token
->location
, OPT_Wdeprecated
,
17387 "access declarations are deprecated "
17388 "in favour of using-declarations; "
17389 "suggestion: add the %<using%> keyword");
17394 /* Parse an alias-declaration.
17397 using identifier attribute-specifier-seq [opt] = type-id */
17400 cp_parser_alias_declaration (cp_parser
* parser
)
17402 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
17403 location_t id_location
;
17404 cp_declarator
*declarator
;
17405 cp_decl_specifier_seq decl_specs
;
17407 const char *saved_message
= NULL
;
17409 /* Look for the `using' keyword. */
17410 cp_token
*using_token
17411 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17412 if (using_token
== NULL
)
17413 return error_mark_node
;
17415 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
17416 id
= cp_parser_identifier (parser
);
17417 if (id
== error_mark_node
)
17418 return error_mark_node
;
17420 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
17421 attributes
= cp_parser_attributes_opt (parser
);
17422 if (attributes
== error_mark_node
)
17423 return error_mark_node
;
17425 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
17427 if (cp_parser_error_occurred (parser
))
17428 return error_mark_node
;
17430 cp_parser_commit_to_tentative_parse (parser
);
17432 /* Now we are going to parse the type-id of the declaration. */
17437 "A type-specifier-seq shall not define a class or enumeration
17438 unless it appears in the type-id of an alias-declaration (7.1.3) that
17439 is not the declaration of a template-declaration."
17441 In other words, if we currently are in an alias template, the
17442 type-id should not define a type.
17444 So let's set parser->type_definition_forbidden_message in that
17445 case; cp_parser_check_type_definition (called by
17446 cp_parser_class_specifier) will then emit an error if a type is
17447 defined in the type-id. */
17448 if (parser
->num_template_parameter_lists
)
17450 saved_message
= parser
->type_definition_forbidden_message
;
17451 parser
->type_definition_forbidden_message
=
17452 G_("types may not be defined in alias template declarations");
17455 type
= cp_parser_type_id (parser
);
17457 /* Restore the error message if need be. */
17458 if (parser
->num_template_parameter_lists
)
17459 parser
->type_definition_forbidden_message
= saved_message
;
17461 if (type
== error_mark_node
17462 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
17464 cp_parser_skip_to_end_of_block_or_statement (parser
);
17465 return error_mark_node
;
17468 /* A typedef-name can also be introduced by an alias-declaration. The
17469 identifier following the using keyword becomes a typedef-name. It has
17470 the same semantics as if it were introduced by the typedef
17471 specifier. In particular, it does not define a new type and it shall
17472 not appear in the type-id. */
17474 clear_decl_specs (&decl_specs
);
17475 decl_specs
.type
= type
;
17476 if (attributes
!= NULL_TREE
)
17478 decl_specs
.attributes
= attributes
;
17479 set_and_check_decl_spec_loc (&decl_specs
,
17483 set_and_check_decl_spec_loc (&decl_specs
,
17486 set_and_check_decl_spec_loc (&decl_specs
,
17490 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
17491 declarator
->id_loc
= id_location
;
17493 member_p
= at_class_scope_p ();
17495 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
17496 NULL_TREE
, attributes
);
17498 decl
= start_decl (declarator
, &decl_specs
, 0,
17499 attributes
, NULL_TREE
, &pushed_scope
);
17500 if (decl
== error_mark_node
)
17503 // Attach constraints to the alias declaration.
17504 if (flag_concepts
&& current_template_parms
)
17506 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
17507 tree constr
= build_constraints (reqs
, NULL_TREE
);
17508 set_constraints (decl
, constr
);
17511 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
17514 pop_scope (pushed_scope
);
17516 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17517 added into the symbol table; otherwise, return the TYPE_DECL. */
17518 if (DECL_LANG_SPECIFIC (decl
)
17519 && DECL_TEMPLATE_INFO (decl
)
17520 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
17522 decl
= DECL_TI_TEMPLATE (decl
);
17524 check_member_template (decl
);
17530 /* Parse a using-directive.
17533 using namespace :: [opt] nested-name-specifier [opt]
17534 namespace-name ; */
17537 cp_parser_using_directive (cp_parser
* parser
)
17539 tree namespace_decl
;
17542 /* Look for the `using' keyword. */
17543 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
17544 /* And the `namespace' keyword. */
17545 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
17546 /* Look for the optional `::' operator. */
17547 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
17548 /* And the optional nested-name-specifier. */
17549 cp_parser_nested_name_specifier_opt (parser
,
17550 /*typename_keyword_p=*/false,
17551 /*check_dependency_p=*/true,
17553 /*is_declaration=*/true);
17554 /* Get the namespace being used. */
17555 namespace_decl
= cp_parser_namespace_name (parser
);
17556 /* And any specified attributes. */
17557 attribs
= cp_parser_attributes_opt (parser
);
17558 /* Update the symbol table. */
17559 parse_using_directive (namespace_decl
, attribs
);
17560 /* Look for the final `;'. */
17561 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17564 /* Parse an asm-definition.
17567 asm ( string-literal ) ;
17572 asm volatile [opt] ( string-literal ) ;
17573 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17574 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17575 : asm-operand-list [opt] ) ;
17576 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17577 : asm-operand-list [opt]
17578 : asm-clobber-list [opt] ) ;
17579 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17580 : asm-clobber-list [opt]
17581 : asm-goto-list ) ; */
17584 cp_parser_asm_definition (cp_parser
* parser
)
17587 tree outputs
= NULL_TREE
;
17588 tree inputs
= NULL_TREE
;
17589 tree clobbers
= NULL_TREE
;
17590 tree labels
= NULL_TREE
;
17592 bool volatile_p
= false;
17593 bool extended_p
= false;
17594 bool invalid_inputs_p
= false;
17595 bool invalid_outputs_p
= false;
17596 bool goto_p
= false;
17597 required_token missing
= RT_NONE
;
17599 /* Look for the `asm' keyword. */
17600 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
17602 if (parser
->in_function_body
17603 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
17605 error ("%<asm%> in %<constexpr%> function");
17606 cp_function_chain
->invalid_constexpr
= true;
17609 /* See if the next token is `volatile'. */
17610 if (cp_parser_allow_gnu_extensions_p (parser
)
17611 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
17613 /* Remember that we saw the `volatile' keyword. */
17615 /* Consume the token. */
17616 cp_lexer_consume_token (parser
->lexer
);
17618 if (cp_parser_allow_gnu_extensions_p (parser
)
17619 && parser
->in_function_body
17620 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
17622 /* Remember that we saw the `goto' keyword. */
17624 /* Consume the token. */
17625 cp_lexer_consume_token (parser
->lexer
);
17627 /* Look for the opening `('. */
17628 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
17630 /* Look for the string. */
17631 string
= cp_parser_string_literal (parser
, false, false);
17632 if (string
== error_mark_node
)
17634 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
17635 /*consume_paren=*/true);
17639 /* If we're allowing GNU extensions, check for the extended assembly
17640 syntax. Unfortunately, the `:' tokens need not be separated by
17641 a space in C, and so, for compatibility, we tolerate that here
17642 too. Doing that means that we have to treat the `::' operator as
17644 if (cp_parser_allow_gnu_extensions_p (parser
)
17645 && parser
->in_function_body
17646 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
17647 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
17649 bool inputs_p
= false;
17650 bool clobbers_p
= false;
17651 bool labels_p
= false;
17653 /* The extended syntax was used. */
17656 /* Look for outputs. */
17657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17659 /* Consume the `:'. */
17660 cp_lexer_consume_token (parser
->lexer
);
17661 /* Parse the output-operands. */
17662 if (cp_lexer_next_token_is_not (parser
->lexer
,
17664 && cp_lexer_next_token_is_not (parser
->lexer
,
17666 && cp_lexer_next_token_is_not (parser
->lexer
,
17670 outputs
= cp_parser_asm_operand_list (parser
);
17671 if (outputs
== error_mark_node
)
17672 invalid_outputs_p
= true;
17675 /* If the next token is `::', there are no outputs, and the
17676 next token is the beginning of the inputs. */
17677 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17678 /* The inputs are coming next. */
17681 /* Look for inputs. */
17683 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17685 /* Consume the `:' or `::'. */
17686 cp_lexer_consume_token (parser
->lexer
);
17687 /* Parse the output-operands. */
17688 if (cp_lexer_next_token_is_not (parser
->lexer
,
17690 && cp_lexer_next_token_is_not (parser
->lexer
,
17692 && cp_lexer_next_token_is_not (parser
->lexer
,
17695 inputs
= cp_parser_asm_operand_list (parser
);
17696 if (inputs
== error_mark_node
)
17697 invalid_inputs_p
= true;
17700 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17701 /* The clobbers are coming next. */
17704 /* Look for clobbers. */
17706 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
17709 /* Consume the `:' or `::'. */
17710 cp_lexer_consume_token (parser
->lexer
);
17711 /* Parse the clobbers. */
17712 if (cp_lexer_next_token_is_not (parser
->lexer
,
17714 && cp_lexer_next_token_is_not (parser
->lexer
,
17716 clobbers
= cp_parser_asm_clobber_list (parser
);
17719 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
17720 /* The labels are coming next. */
17723 /* Look for labels. */
17725 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
17728 /* Consume the `:' or `::'. */
17729 cp_lexer_consume_token (parser
->lexer
);
17730 /* Parse the labels. */
17731 labels
= cp_parser_asm_label_list (parser
);
17734 if (goto_p
&& !labels_p
)
17735 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
17738 missing
= RT_COLON_SCOPE
;
17740 /* Look for the closing `)'. */
17741 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
17742 missing
? missing
: RT_CLOSE_PAREN
))
17743 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
17744 /*consume_paren=*/true);
17745 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
17747 if (!invalid_inputs_p
&& !invalid_outputs_p
)
17749 /* Create the ASM_EXPR. */
17750 if (parser
->in_function_body
)
17752 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
17753 inputs
, clobbers
, labels
);
17754 /* If the extended syntax was not used, mark the ASM_EXPR. */
17757 tree temp
= asm_stmt
;
17758 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
17759 temp
= TREE_OPERAND (temp
, 0);
17761 ASM_INPUT_P (temp
) = 1;
17765 symtab
->finalize_toplevel_asm (string
);
17769 /* Declarators [gram.dcl.decl] */
17771 /* Parse an init-declarator.
17774 declarator initializer [opt]
17779 declarator asm-specification [opt] attributes [opt] initializer [opt]
17781 function-definition:
17782 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17784 decl-specifier-seq [opt] declarator function-try-block
17788 function-definition:
17789 __extension__ function-definition
17793 function-definition:
17794 decl-specifier-seq [opt] declarator function-transaction-block
17796 The DECL_SPECIFIERS apply to this declarator. Returns a
17797 representation of the entity declared. If MEMBER_P is TRUE, then
17798 this declarator appears in a class scope. The new DECL created by
17799 this declarator is returned.
17801 The CHECKS are access checks that should be performed once we know
17802 what entity is being declared (and, therefore, what classes have
17805 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17806 for a function-definition here as well. If the declarator is a
17807 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17808 be TRUE upon return. By that point, the function-definition will
17809 have been completely parsed.
17811 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17814 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17815 parsed declaration if it is an uninitialized single declarator not followed
17816 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17817 if present, will not be consumed. If returned, this declarator will be
17818 created with SD_INITIALIZED but will not call cp_finish_decl.
17820 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17821 and there is an initializer, the pointed location_t is set to the
17822 location of the '=' or `(', or '{' in C++11 token introducing the
17826 cp_parser_init_declarator (cp_parser
* parser
,
17827 cp_decl_specifier_seq
*decl_specifiers
,
17828 vec
<deferred_access_check
, va_gc
> *checks
,
17829 bool function_definition_allowed_p
,
17831 int declares_class_or_enum
,
17832 bool* function_definition_p
,
17833 tree
* maybe_range_for_decl
,
17834 location_t
* init_loc
)
17836 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
17837 *attributes_start_token
= NULL
;
17838 cp_declarator
*declarator
;
17839 tree prefix_attributes
;
17840 tree attributes
= NULL
;
17841 tree asm_specification
;
17843 tree decl
= NULL_TREE
;
17845 int is_initialized
;
17846 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17847 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17849 enum cpp_ttype initialization_kind
;
17850 bool is_direct_init
= false;
17851 bool is_non_constant_init
;
17852 int ctor_dtor_or_conv_p
;
17853 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
17854 tree pushed_scope
= NULL_TREE
;
17855 bool range_for_decl_p
= false;
17856 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17857 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
17859 /* Gather the attributes that were provided with the
17860 decl-specifiers. */
17861 prefix_attributes
= decl_specifiers
->attributes
;
17863 /* Assume that this is not the declarator for a function
17865 if (function_definition_p
)
17866 *function_definition_p
= false;
17868 /* Default arguments are only permitted for function parameters. */
17869 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
17870 parser
->default_arg_ok_p
= false;
17872 /* Defer access checks while parsing the declarator; we cannot know
17873 what names are accessible until we know what is being
17875 resume_deferring_access_checks ();
17877 /* Parse the declarator. */
17878 token
= cp_lexer_peek_token (parser
->lexer
);
17880 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17881 &ctor_dtor_or_conv_p
,
17882 /*parenthesized_p=*/NULL
,
17883 member_p
, friend_p
);
17884 /* Gather up the deferred checks. */
17885 stop_deferring_access_checks ();
17887 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17889 /* If the DECLARATOR was erroneous, there's no need to go
17891 if (declarator
== cp_error_declarator
)
17892 return error_mark_node
;
17894 /* Check that the number of template-parameter-lists is OK. */
17895 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
17897 return error_mark_node
;
17899 if (declares_class_or_enum
& 2)
17900 cp_parser_check_for_definition_in_return_type (declarator
,
17901 decl_specifiers
->type
,
17902 decl_specifiers
->locations
[ds_type_spec
]);
17904 /* Figure out what scope the entity declared by the DECLARATOR is
17905 located in. `grokdeclarator' sometimes changes the scope, so
17906 we compute it now. */
17907 scope
= get_scope_of_declarator (declarator
);
17909 /* Perform any lookups in the declared type which were thought to be
17910 dependent, but are not in the scope of the declarator. */
17911 decl_specifiers
->type
17912 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
17914 /* If we're allowing GNU extensions, look for an
17915 asm-specification. */
17916 if (cp_parser_allow_gnu_extensions_p (parser
))
17918 /* Look for an asm-specification. */
17919 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
17920 asm_specification
= cp_parser_asm_specification_opt (parser
);
17923 asm_specification
= NULL_TREE
;
17925 /* Look for attributes. */
17926 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
17927 attributes
= cp_parser_attributes_opt (parser
);
17929 /* Peek at the next token. */
17930 token
= cp_lexer_peek_token (parser
->lexer
);
17932 bool bogus_implicit_tmpl
= false;
17934 if (function_declarator_p (declarator
))
17936 /* Check to see if the token indicates the start of a
17937 function-definition. */
17938 if (cp_parser_token_starts_function_definition_p (token
))
17940 if (!function_definition_allowed_p
)
17942 /* If a function-definition should not appear here, issue an
17944 cp_parser_error (parser
,
17945 "a function-definition is not allowed here");
17946 return error_mark_node
;
17949 location_t func_brace_location
17950 = cp_lexer_peek_token (parser
->lexer
)->location
;
17952 /* Neither attributes nor an asm-specification are allowed
17953 on a function-definition. */
17954 if (asm_specification
)
17955 error_at (asm_spec_start_token
->location
,
17956 "an asm-specification is not allowed "
17957 "on a function-definition");
17959 error_at (attributes_start_token
->location
,
17960 "attributes are not allowed "
17961 "on a function-definition");
17962 /* This is a function-definition. */
17963 *function_definition_p
= true;
17965 /* Parse the function definition. */
17967 decl
= cp_parser_save_member_function_body (parser
,
17970 prefix_attributes
);
17973 (cp_parser_function_definition_from_specifiers_and_declarator
17974 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
17976 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
17978 /* This is where the prologue starts... */
17979 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
17980 = func_brace_location
;
17986 else if (parser
->fully_implicit_function_template_p
)
17988 /* A non-template declaration involving a function parameter list
17989 containing an implicit template parameter will be made into a
17990 template. If the resulting declaration is not going to be an
17991 actual function then finish the template scope here to prevent it.
17992 An error message will be issued once we have a decl to talk about.
17994 FIXME probably we should do type deduction rather than create an
17995 implicit template, but the standard currently doesn't allow it. */
17996 bogus_implicit_tmpl
= true;
17997 finish_fully_implicit_template (parser
, NULL_TREE
);
18002 Only in function declarations for constructors, destructors, and
18003 type conversions can the decl-specifier-seq be omitted.
18005 We explicitly postpone this check past the point where we handle
18006 function-definitions because we tolerate function-definitions
18007 that are missing their return types in some modes. */
18008 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
18010 cp_parser_error (parser
,
18011 "expected constructor, destructor, or type conversion");
18012 return error_mark_node
;
18015 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18016 if (token
->type
== CPP_EQ
18017 || token
->type
== CPP_OPEN_PAREN
18018 || token
->type
== CPP_OPEN_BRACE
)
18020 is_initialized
= SD_INITIALIZED
;
18021 initialization_kind
= token
->type
;
18022 if (maybe_range_for_decl
)
18023 *maybe_range_for_decl
= error_mark_node
;
18024 tmp_init_loc
= token
->location
;
18025 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
18026 *init_loc
= tmp_init_loc
;
18028 if (token
->type
== CPP_EQ
18029 && function_declarator_p (declarator
))
18031 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18032 if (t2
->keyword
== RID_DEFAULT
)
18033 is_initialized
= SD_DEFAULTED
;
18034 else if (t2
->keyword
== RID_DELETE
)
18035 is_initialized
= SD_DELETED
;
18040 /* If the init-declarator isn't initialized and isn't followed by a
18041 `,' or `;', it's not a valid init-declarator. */
18042 if (token
->type
!= CPP_COMMA
18043 && token
->type
!= CPP_SEMICOLON
)
18045 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
18046 range_for_decl_p
= true;
18049 if (!maybe_range_for_decl
)
18050 cp_parser_error (parser
, "expected initializer");
18051 return error_mark_node
;
18054 is_initialized
= SD_UNINITIALIZED
;
18055 initialization_kind
= CPP_EOF
;
18058 /* Because start_decl has side-effects, we should only call it if we
18059 know we're going ahead. By this point, we know that we cannot
18060 possibly be looking at any other construct. */
18061 cp_parser_commit_to_tentative_parse (parser
);
18063 /* Enter the newly declared entry in the symbol table. If we're
18064 processing a declaration in a class-specifier, we wait until
18065 after processing the initializer. */
18068 if (parser
->in_unbraced_linkage_specification_p
)
18069 decl_specifiers
->storage_class
= sc_extern
;
18070 decl
= start_decl (declarator
, decl_specifiers
,
18071 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
18072 attributes
, prefix_attributes
, &pushed_scope
);
18073 cp_finalize_omp_declare_simd (parser
, decl
);
18074 cp_finalize_oacc_routine (parser
, decl
, false);
18075 /* Adjust location of decl if declarator->id_loc is more appropriate:
18076 set, and decl wasn't merged with another decl, in which case its
18077 location would be different from input_location, and more accurate. */
18079 && declarator
->id_loc
!= UNKNOWN_LOCATION
18080 && DECL_SOURCE_LOCATION (decl
) == input_location
)
18081 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
18084 /* Enter the SCOPE. That way unqualified names appearing in the
18085 initializer will be looked up in SCOPE. */
18086 pushed_scope
= push_scope (scope
);
18088 /* Perform deferred access control checks, now that we know in which
18089 SCOPE the declared entity resides. */
18090 if (!member_p
&& decl
)
18092 tree saved_current_function_decl
= NULL_TREE
;
18094 /* If the entity being declared is a function, pretend that we
18095 are in its scope. If it is a `friend', it may have access to
18096 things that would not otherwise be accessible. */
18097 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18099 saved_current_function_decl
= current_function_decl
;
18100 current_function_decl
= decl
;
18103 /* Perform access checks for template parameters. */
18104 cp_parser_perform_template_parameter_access_checks (checks
);
18106 /* Perform the access control checks for the declarator and the
18107 decl-specifiers. */
18108 perform_deferred_access_checks (tf_warning_or_error
);
18110 /* Restore the saved value. */
18111 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18112 current_function_decl
= saved_current_function_decl
;
18115 /* Parse the initializer. */
18116 initializer
= NULL_TREE
;
18117 is_direct_init
= false;
18118 is_non_constant_init
= true;
18119 if (is_initialized
)
18121 if (function_declarator_p (declarator
))
18123 if (initialization_kind
== CPP_EQ
)
18124 initializer
= cp_parser_pure_specifier (parser
);
18127 /* If the declaration was erroneous, we don't really
18128 know what the user intended, so just silently
18129 consume the initializer. */
18130 if (decl
!= error_mark_node
)
18131 error_at (tmp_init_loc
, "initializer provided for function");
18132 cp_parser_skip_to_closing_parenthesis (parser
,
18133 /*recovering=*/true,
18134 /*or_comma=*/false,
18135 /*consume_paren=*/true);
18140 /* We want to record the extra mangling scope for in-class
18141 initializers of class members and initializers of static data
18142 member templates. The former involves deferring
18143 parsing of the initializer until end of class as with default
18144 arguments. So right here we only handle the latter. */
18145 if (!member_p
&& processing_template_decl
)
18146 start_lambda_scope (decl
);
18147 initializer
= cp_parser_initializer (parser
,
18149 &is_non_constant_init
);
18150 if (!member_p
&& processing_template_decl
)
18151 finish_lambda_scope ();
18152 if (initializer
== error_mark_node
)
18153 cp_parser_skip_to_end_of_statement (parser
);
18157 /* The old parser allows attributes to appear after a parenthesized
18158 initializer. Mark Mitchell proposed removing this functionality
18159 on the GCC mailing lists on 2002-08-13. This parser accepts the
18160 attributes -- but ignores them. */
18161 if (cp_parser_allow_gnu_extensions_p (parser
)
18162 && initialization_kind
== CPP_OPEN_PAREN
)
18163 if (cp_parser_attributes_opt (parser
))
18164 warning (OPT_Wattributes
,
18165 "attributes after parenthesized initializer ignored");
18167 /* And now complain about a non-function implicit template. */
18168 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
18169 error_at (DECL_SOURCE_LOCATION (decl
),
18170 "non-function %qD declared as implicit template", decl
);
18172 /* For an in-class declaration, use `grokfield' to create the
18178 pop_scope (pushed_scope
);
18179 pushed_scope
= NULL_TREE
;
18181 decl
= grokfield (declarator
, decl_specifiers
,
18182 initializer
, !is_non_constant_init
,
18183 /*asmspec=*/NULL_TREE
,
18184 chainon (attributes
, prefix_attributes
));
18185 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18186 cp_parser_save_default_args (parser
, decl
);
18187 cp_finalize_omp_declare_simd (parser
, decl
);
18188 cp_finalize_oacc_routine (parser
, decl
, false);
18191 /* Finish processing the declaration. But, skip member
18193 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
18195 cp_finish_decl (decl
,
18196 initializer
, !is_non_constant_init
,
18198 /* If the initializer is in parentheses, then this is
18199 a direct-initialization, which means that an
18200 `explicit' constructor is OK. Otherwise, an
18201 `explicit' constructor cannot be used. */
18202 ((is_direct_init
|| !is_initialized
)
18203 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
18205 else if ((cxx_dialect
!= cxx98
) && friend_p
18206 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
18207 /* Core issue #226 (C++0x only): A default template-argument
18208 shall not be specified in a friend class template
18210 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
18211 /*is_partial=*/false, /*is_friend_decl=*/1);
18213 if (!friend_p
&& pushed_scope
)
18214 pop_scope (pushed_scope
);
18216 if (function_declarator_p (declarator
)
18217 && parser
->fully_implicit_function_template_p
)
18220 decl
= finish_fully_implicit_template (parser
, decl
);
18222 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
18228 /* Parse a declarator.
18232 ptr-operator declarator
18234 abstract-declarator:
18235 ptr-operator abstract-declarator [opt]
18236 direct-abstract-declarator
18241 attributes [opt] direct-declarator
18242 attributes [opt] ptr-operator declarator
18244 abstract-declarator:
18245 attributes [opt] ptr-operator abstract-declarator [opt]
18246 attributes [opt] direct-abstract-declarator
18248 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18249 detect constructor, destructor or conversion operators. It is set
18250 to -1 if the declarator is a name, and +1 if it is a
18251 function. Otherwise it is set to zero. Usually you just want to
18252 test for >0, but internally the negative value is used.
18254 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18255 a decl-specifier-seq unless it declares a constructor, destructor,
18256 or conversion. It might seem that we could check this condition in
18257 semantic analysis, rather than parsing, but that makes it difficult
18258 to handle something like `f()'. We want to notice that there are
18259 no decl-specifiers, and therefore realize that this is an
18260 expression, not a declaration.)
18262 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18263 the declarator is a direct-declarator of the form "(...)".
18265 MEMBER_P is true iff this declarator is a member-declarator.
18267 FRIEND_P is true iff this declarator is a friend. */
18269 static cp_declarator
*
18270 cp_parser_declarator (cp_parser
* parser
,
18271 cp_parser_declarator_kind dcl_kind
,
18272 int* ctor_dtor_or_conv_p
,
18273 bool* parenthesized_p
,
18274 bool member_p
, bool friend_p
)
18276 cp_declarator
*declarator
;
18277 enum tree_code code
;
18278 cp_cv_quals cv_quals
;
18280 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
18282 /* Assume this is not a constructor, destructor, or type-conversion
18284 if (ctor_dtor_or_conv_p
)
18285 *ctor_dtor_or_conv_p
= 0;
18287 if (cp_parser_allow_gnu_extensions_p (parser
))
18288 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
18290 /* Check for the ptr-operator production. */
18291 cp_parser_parse_tentatively (parser
);
18292 /* Parse the ptr-operator. */
18293 code
= cp_parser_ptr_operator (parser
,
18298 /* If that worked, then we have a ptr-operator. */
18299 if (cp_parser_parse_definitely (parser
))
18301 /* If a ptr-operator was found, then this declarator was not
18303 if (parenthesized_p
)
18304 *parenthesized_p
= true;
18305 /* The dependent declarator is optional if we are parsing an
18306 abstract-declarator. */
18307 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18308 cp_parser_parse_tentatively (parser
);
18310 /* Parse the dependent declarator. */
18311 declarator
= cp_parser_declarator (parser
, dcl_kind
,
18312 /*ctor_dtor_or_conv_p=*/NULL
,
18313 /*parenthesized_p=*/NULL
,
18314 /*member_p=*/false,
18317 /* If we are parsing an abstract-declarator, we must handle the
18318 case where the dependent declarator is absent. */
18319 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
18320 && !cp_parser_parse_definitely (parser
))
18323 declarator
= cp_parser_make_indirect_declarator
18324 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
18326 /* Everything else is a direct-declarator. */
18329 if (parenthesized_p
)
18330 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
18332 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
18333 ctor_dtor_or_conv_p
,
18334 member_p
, friend_p
);
18337 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
18338 declarator
->attributes
= gnu_attributes
;
18342 /* Parse a direct-declarator or direct-abstract-declarator.
18346 direct-declarator ( parameter-declaration-clause )
18347 cv-qualifier-seq [opt]
18348 ref-qualifier [opt]
18349 exception-specification [opt]
18350 direct-declarator [ constant-expression [opt] ]
18353 direct-abstract-declarator:
18354 direct-abstract-declarator [opt]
18355 ( parameter-declaration-clause )
18356 cv-qualifier-seq [opt]
18357 ref-qualifier [opt]
18358 exception-specification [opt]
18359 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18360 ( abstract-declarator )
18362 Returns a representation of the declarator. DCL_KIND is
18363 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18364 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18365 we are parsing a direct-declarator. It is
18366 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18367 of ambiguity we prefer an abstract declarator, as per
18368 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18369 as for cp_parser_declarator. */
18371 static cp_declarator
*
18372 cp_parser_direct_declarator (cp_parser
* parser
,
18373 cp_parser_declarator_kind dcl_kind
,
18374 int* ctor_dtor_or_conv_p
,
18375 bool member_p
, bool friend_p
)
18378 cp_declarator
*declarator
= NULL
;
18379 tree scope
= NULL_TREE
;
18380 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18381 bool saved_in_declarator_p
= parser
->in_declarator_p
;
18383 tree pushed_scope
= NULL_TREE
;
18387 /* Peek at the next token. */
18388 token
= cp_lexer_peek_token (parser
->lexer
);
18389 if (token
->type
== CPP_OPEN_PAREN
)
18391 /* This is either a parameter-declaration-clause, or a
18392 parenthesized declarator. When we know we are parsing a
18393 named declarator, it must be a parenthesized declarator
18394 if FIRST is true. For instance, `(int)' is a
18395 parameter-declaration-clause, with an omitted
18396 direct-abstract-declarator. But `((*))', is a
18397 parenthesized abstract declarator. Finally, when T is a
18398 template parameter `(T)' is a
18399 parameter-declaration-clause, and not a parenthesized
18402 We first try and parse a parameter-declaration-clause,
18403 and then try a nested declarator (if FIRST is true).
18405 It is not an error for it not to be a
18406 parameter-declaration-clause, even when FIRST is
18412 The first is the declaration of a function while the
18413 second is the definition of a variable, including its
18416 Having seen only the parenthesis, we cannot know which of
18417 these two alternatives should be selected. Even more
18418 complex are examples like:
18423 The former is a function-declaration; the latter is a
18424 variable initialization.
18426 Thus again, we try a parameter-declaration-clause, and if
18427 that fails, we back out and return. */
18429 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18432 bool is_declarator
= false;
18434 /* In a member-declarator, the only valid interpretation
18435 of a parenthesis is the start of a
18436 parameter-declaration-clause. (It is invalid to
18437 initialize a static data member with a parenthesized
18438 initializer; only the "=" form of initialization is
18441 cp_parser_parse_tentatively (parser
);
18443 /* Consume the `('. */
18444 cp_lexer_consume_token (parser
->lexer
);
18447 /* If this is going to be an abstract declarator, we're
18448 in a declarator and we can't have default args. */
18449 parser
->default_arg_ok_p
= false;
18450 parser
->in_declarator_p
= true;
18453 begin_scope (sk_function_parms
, NULL_TREE
);
18455 /* Parse the parameter-declaration-clause. */
18456 params
= cp_parser_parameter_declaration_clause (parser
);
18458 /* Consume the `)'. */
18459 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
18461 /* If all went well, parse the cv-qualifier-seq,
18462 ref-qualifier and the exception-specification. */
18463 if (member_p
|| cp_parser_parse_definitely (parser
))
18465 cp_cv_quals cv_quals
;
18466 cp_virt_specifiers virt_specifiers
;
18467 cp_ref_qualifier ref_qual
;
18468 tree exception_specification
;
18471 bool memfn
= (member_p
|| (pushed_scope
18472 && CLASS_TYPE_P (pushed_scope
)));
18474 is_declarator
= true;
18476 if (ctor_dtor_or_conv_p
)
18477 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
18480 /* Parse the cv-qualifier-seq. */
18481 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18482 /* Parse the ref-qualifier. */
18483 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
18484 /* Parse the tx-qualifier. */
18485 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
18486 /* And the exception-specification. */
18487 exception_specification
18488 = cp_parser_exception_specification_opt (parser
);
18490 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18492 /* In here, we handle cases where attribute is used after
18493 the function declaration. For example:
18494 void func (int x) __attribute__((vector(..))); */
18496 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
18498 cp_parser_parse_tentatively (parser
);
18499 tree attr
= cp_parser_gnu_attributes_opt (parser
);
18500 if (cp_lexer_next_token_is_not (parser
->lexer
,
18502 && cp_lexer_next_token_is_not (parser
->lexer
,
18504 cp_parser_abort_tentative_parse (parser
);
18505 else if (!cp_parser_parse_definitely (parser
))
18508 attrs
= chainon (attr
, attrs
);
18510 tree requires_clause
= NULL_TREE
;
18511 late_return
= (cp_parser_late_return_type_opt
18512 (parser
, declarator
, requires_clause
,
18513 memfn
? cv_quals
: -1));
18515 /* Parse the virt-specifier-seq. */
18516 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
18518 /* Create the function-declarator. */
18519 declarator
= make_call_declarator (declarator
,
18525 exception_specification
,
18528 declarator
->std_attributes
= attrs
;
18529 /* Any subsequent parameter lists are to do with
18530 return type, so are not those of the declared
18532 parser
->default_arg_ok_p
= false;
18535 /* Remove the function parms from scope. */
18536 pop_bindings_and_leave_scope ();
18539 /* Repeat the main loop. */
18543 /* If this is the first, we can try a parenthesized
18547 bool saved_in_type_id_in_expr_p
;
18549 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18550 parser
->in_declarator_p
= saved_in_declarator_p
;
18552 /* Consume the `('. */
18553 cp_lexer_consume_token (parser
->lexer
);
18554 /* Parse the nested declarator. */
18555 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
18556 parser
->in_type_id_in_expr_p
= true;
18558 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
18559 /*parenthesized_p=*/NULL
,
18560 member_p
, friend_p
);
18561 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
18563 /* Expect a `)'. */
18564 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
18565 declarator
= cp_error_declarator
;
18566 if (declarator
== cp_error_declarator
)
18569 goto handle_declarator
;
18571 /* Otherwise, we must be done. */
18575 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
18576 && token
->type
== CPP_OPEN_SQUARE
18577 && !cp_next_tokens_can_be_attribute_p (parser
))
18579 /* Parse an array-declarator. */
18580 tree bounds
, attrs
;
18582 if (ctor_dtor_or_conv_p
)
18583 *ctor_dtor_or_conv_p
= 0;
18586 parser
->default_arg_ok_p
= false;
18587 parser
->in_declarator_p
= true;
18588 /* Consume the `['. */
18589 cp_lexer_consume_token (parser
->lexer
);
18590 /* Peek at the next token. */
18591 token
= cp_lexer_peek_token (parser
->lexer
);
18592 /* If the next token is `]', then there is no
18593 constant-expression. */
18594 if (token
->type
!= CPP_CLOSE_SQUARE
)
18596 bool non_constant_p
;
18598 = cp_parser_constant_expression (parser
,
18599 /*allow_non_constant=*/true,
18601 if (!non_constant_p
)
18603 else if (error_operand_p (bounds
))
18604 /* Already gave an error. */;
18605 else if (!parser
->in_function_body
18606 || current_binding_level
->kind
== sk_function_parms
)
18608 /* Normally, the array bound must be an integral constant
18609 expression. However, as an extension, we allow VLAs
18610 in function scopes as long as they aren't part of a
18611 parameter declaration. */
18612 cp_parser_error (parser
,
18613 "array bound is not an integer constant");
18614 bounds
= error_mark_node
;
18616 else if (processing_template_decl
18617 && !type_dependent_expression_p (bounds
))
18619 /* Remember this wasn't a constant-expression. */
18620 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
18621 TREE_SIDE_EFFECTS (bounds
) = 1;
18625 bounds
= NULL_TREE
;
18626 /* Look for the closing `]'. */
18627 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
18629 declarator
= cp_error_declarator
;
18633 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18634 declarator
= make_array_declarator (declarator
, bounds
);
18635 declarator
->std_attributes
= attrs
;
18637 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
18640 tree qualifying_scope
;
18641 tree unqualified_name
;
18643 special_function_kind sfk
;
18645 bool pack_expansion_p
= false;
18646 cp_token
*declarator_id_start_token
;
18648 /* Parse a declarator-id */
18649 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
18652 cp_parser_parse_tentatively (parser
);
18654 /* If we see an ellipsis, we should be looking at a
18656 if (token
->type
== CPP_ELLIPSIS
)
18658 /* Consume the `...' */
18659 cp_lexer_consume_token (parser
->lexer
);
18661 pack_expansion_p
= true;
18665 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
18667 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
18668 qualifying_scope
= parser
->scope
;
18673 if (!unqualified_name
&& pack_expansion_p
)
18675 /* Check whether an error occurred. */
18676 okay
= !cp_parser_error_occurred (parser
);
18678 /* We already consumed the ellipsis to mark a
18679 parameter pack, but we have no way to report it,
18680 so abort the tentative parse. We will be exiting
18681 immediately anyway. */
18682 cp_parser_abort_tentative_parse (parser
);
18685 okay
= cp_parser_parse_definitely (parser
);
18688 unqualified_name
= error_mark_node
;
18689 else if (unqualified_name
18690 && (qualifying_scope
18691 || (!identifier_p (unqualified_name
))))
18693 cp_parser_error (parser
, "expected unqualified-id");
18694 unqualified_name
= error_mark_node
;
18698 if (!unqualified_name
)
18700 if (unqualified_name
== error_mark_node
)
18702 declarator
= cp_error_declarator
;
18703 pack_expansion_p
= false;
18704 declarator
->parameter_pack_p
= false;
18708 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18710 if (qualifying_scope
&& at_namespace_scope_p ()
18711 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
18713 /* In the declaration of a member of a template class
18714 outside of the class itself, the SCOPE will sometimes
18715 be a TYPENAME_TYPE. For example, given:
18717 template <typename T>
18718 int S<T>::R::i = 3;
18720 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18721 this context, we must resolve S<T>::R to an ordinary
18722 type, rather than a typename type.
18724 The reason we normally avoid resolving TYPENAME_TYPEs
18725 is that a specialization of `S' might render
18726 `S<T>::R' not a type. However, if `S' is
18727 specialized, then this `i' will not be used, so there
18728 is no harm in resolving the types here. */
18731 /* Resolve the TYPENAME_TYPE. */
18732 type
= resolve_typename_type (qualifying_scope
,
18733 /*only_current_p=*/false);
18734 /* If that failed, the declarator is invalid. */
18735 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18737 if (typedef_variant_p (type
))
18738 error_at (declarator_id_start_token
->location
,
18739 "cannot define member of dependent typedef "
18742 error_at (declarator_id_start_token
->location
,
18743 "%<%T::%E%> is not a type",
18744 TYPE_CONTEXT (qualifying_scope
),
18745 TYPE_IDENTIFIER (qualifying_scope
));
18747 qualifying_scope
= type
;
18752 if (unqualified_name
)
18756 if (qualifying_scope
18757 && CLASS_TYPE_P (qualifying_scope
))
18758 class_type
= qualifying_scope
;
18760 class_type
= current_class_type
;
18762 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
18764 tree name_type
= TREE_TYPE (unqualified_name
);
18765 if (class_type
&& same_type_p (name_type
, class_type
))
18767 if (qualifying_scope
18768 && CLASSTYPE_USE_TEMPLATE (name_type
))
18770 error_at (declarator_id_start_token
->location
,
18771 "invalid use of constructor as a template");
18772 inform (declarator_id_start_token
->location
,
18773 "use %<%T::%D%> instead of %<%T::%D%> to "
18774 "name the constructor in a qualified name",
18776 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
18777 class_type
, name_type
);
18778 declarator
= cp_error_declarator
;
18782 unqualified_name
= constructor_name (class_type
);
18786 /* We do not attempt to print the declarator
18787 here because we do not have enough
18788 information about its original syntactic
18790 cp_parser_error (parser
, "invalid declarator");
18791 declarator
= cp_error_declarator
;
18798 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
18799 sfk
= sfk_destructor
;
18800 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
18801 sfk
= sfk_conversion
;
18802 else if (/* There's no way to declare a constructor
18803 for an anonymous type, even if the type
18804 got a name for linkage purposes. */
18805 !TYPE_WAS_ANONYMOUS (class_type
)
18806 /* Handle correctly (c++/19200):
18820 friend void N::S();
18823 && class_type
!= qualifying_scope
)
18824 && constructor_name_p (unqualified_name
,
18827 unqualified_name
= constructor_name (class_type
);
18828 sfk
= sfk_constructor
;
18830 else if (is_overloaded_fn (unqualified_name
)
18831 && DECL_CONSTRUCTOR_P (get_first_fn
18832 (unqualified_name
)))
18833 sfk
= sfk_constructor
;
18835 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
18836 *ctor_dtor_or_conv_p
= -1;
18839 declarator
= make_id_declarator (qualifying_scope
,
18842 declarator
->std_attributes
= attrs
;
18843 declarator
->id_loc
= token
->location
;
18844 declarator
->parameter_pack_p
= pack_expansion_p
;
18846 if (pack_expansion_p
)
18847 maybe_warn_variadic_templates ();
18850 handle_declarator
:;
18851 scope
= get_scope_of_declarator (declarator
);
18854 /* Any names that appear after the declarator-id for a
18855 member are looked up in the containing scope. */
18856 if (at_function_scope_p ())
18858 /* But declarations with qualified-ids can't appear in a
18860 cp_parser_error (parser
, "qualified-id in declaration");
18861 declarator
= cp_error_declarator
;
18864 pushed_scope
= push_scope (scope
);
18866 parser
->in_declarator_p
= true;
18867 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
18868 || (declarator
&& declarator
->kind
== cdk_id
))
18869 /* Default args are only allowed on function
18871 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18873 parser
->default_arg_ok_p
= false;
18882 /* For an abstract declarator, we might wind up with nothing at this
18883 point. That's an error; the declarator is not optional. */
18885 cp_parser_error (parser
, "expected declarator");
18887 /* If we entered a scope, we must exit it now. */
18889 pop_scope (pushed_scope
);
18891 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18892 parser
->in_declarator_p
= saved_in_declarator_p
;
18897 /* Parse a ptr-operator.
18900 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18901 * cv-qualifier-seq [opt]
18903 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18904 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18909 & cv-qualifier-seq [opt]
18911 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18912 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18913 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18914 filled in with the TYPE containing the member. *CV_QUALS is
18915 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18916 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18917 Note that the tree codes returned by this function have nothing
18918 to do with the types of trees that will be eventually be created
18919 to represent the pointer or reference type being parsed. They are
18920 just constants with suggestive names. */
18921 static enum tree_code
18922 cp_parser_ptr_operator (cp_parser
* parser
,
18924 cp_cv_quals
*cv_quals
,
18927 enum tree_code code
= ERROR_MARK
;
18929 tree attrs
= NULL_TREE
;
18931 /* Assume that it's not a pointer-to-member. */
18933 /* And that there are no cv-qualifiers. */
18934 *cv_quals
= TYPE_UNQUALIFIED
;
18936 /* Peek at the next token. */
18937 token
= cp_lexer_peek_token (parser
->lexer
);
18939 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18940 if (token
->type
== CPP_MULT
)
18941 code
= INDIRECT_REF
;
18942 else if (token
->type
== CPP_AND
)
18944 else if ((cxx_dialect
!= cxx98
) &&
18945 token
->type
== CPP_AND_AND
) /* C++0x only */
18946 code
= NON_LVALUE_EXPR
;
18948 if (code
!= ERROR_MARK
)
18950 /* Consume the `*', `&' or `&&'. */
18951 cp_lexer_consume_token (parser
->lexer
);
18953 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18954 `&', if we are allowing GNU extensions. (The only qualifier
18955 that can legally appear after `&' is `restrict', but that is
18956 enforced during semantic analysis. */
18957 if (code
== INDIRECT_REF
18958 || cp_parser_allow_gnu_extensions_p (parser
))
18959 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18961 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18962 if (attributes
!= NULL
)
18963 *attributes
= attrs
;
18967 /* Try the pointer-to-member case. */
18968 cp_parser_parse_tentatively (parser
);
18969 /* Look for the optional `::' operator. */
18970 cp_parser_global_scope_opt (parser
,
18971 /*current_scope_valid_p=*/false);
18972 /* Look for the nested-name specifier. */
18973 token
= cp_lexer_peek_token (parser
->lexer
);
18974 cp_parser_nested_name_specifier (parser
,
18975 /*typename_keyword_p=*/false,
18976 /*check_dependency_p=*/true,
18978 /*is_declaration=*/false);
18979 /* If we found it, and the next token is a `*', then we are
18980 indeed looking at a pointer-to-member operator. */
18981 if (!cp_parser_error_occurred (parser
)
18982 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
18984 /* Indicate that the `*' operator was used. */
18985 code
= INDIRECT_REF
;
18987 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18988 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
18989 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
18990 error_at (token
->location
, "cannot form pointer to member of "
18991 "non-class %q#T", parser
->scope
);
18994 /* The type of which the member is a member is given by the
18996 *type
= parser
->scope
;
18997 /* The next name will not be qualified. */
18998 parser
->scope
= NULL_TREE
;
18999 parser
->qualifying_scope
= NULL_TREE
;
19000 parser
->object_scope
= NULL_TREE
;
19001 /* Look for optional c++11 attributes. */
19002 attrs
= cp_parser_std_attribute_spec_seq (parser
);
19003 if (attributes
!= NULL
)
19004 *attributes
= attrs
;
19005 /* Look for the optional cv-qualifier-seq. */
19006 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
19009 /* If that didn't work we don't have a ptr-operator. */
19010 if (!cp_parser_parse_definitely (parser
))
19011 cp_parser_error (parser
, "expected ptr-operator");
19017 /* Parse an (optional) cv-qualifier-seq.
19020 cv-qualifier cv-qualifier-seq [opt]
19031 Returns a bitmask representing the cv-qualifiers. */
19034 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
19036 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
19041 cp_cv_quals cv_qualifier
;
19043 /* Peek at the next token. */
19044 token
= cp_lexer_peek_token (parser
->lexer
);
19045 /* See if it's a cv-qualifier. */
19046 switch (token
->keyword
)
19049 cv_qualifier
= TYPE_QUAL_CONST
;
19053 cv_qualifier
= TYPE_QUAL_VOLATILE
;
19057 cv_qualifier
= TYPE_QUAL_RESTRICT
;
19061 cv_qualifier
= TYPE_UNQUALIFIED
;
19068 if (cv_quals
& cv_qualifier
)
19070 error_at (token
->location
, "duplicate cv-qualifier");
19071 cp_lexer_purge_token (parser
->lexer
);
19075 cp_lexer_consume_token (parser
->lexer
);
19076 cv_quals
|= cv_qualifier
;
19083 /* Parse an (optional) ref-qualifier
19089 Returns cp_ref_qualifier representing ref-qualifier. */
19091 static cp_ref_qualifier
19092 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
19094 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
19096 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19097 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
19102 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
19103 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19105 switch (token
->type
)
19108 curr_ref_qual
= REF_QUAL_LVALUE
;
19112 curr_ref_qual
= REF_QUAL_RVALUE
;
19116 curr_ref_qual
= REF_QUAL_NONE
;
19120 if (!curr_ref_qual
)
19124 error_at (token
->location
, "multiple ref-qualifiers");
19125 cp_lexer_purge_token (parser
->lexer
);
19129 ref_qual
= curr_ref_qual
;
19130 cp_lexer_consume_token (parser
->lexer
);
19137 /* Parse an optional tx-qualifier.
19141 transaction_safe_dynamic */
19144 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
19146 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19147 if (token
->type
== CPP_NAME
)
19149 tree name
= token
->u
.value
;
19150 const char *p
= IDENTIFIER_POINTER (name
);
19151 const int len
= strlen ("transaction_safe");
19152 if (!strncmp (p
, "transaction_safe", len
))
19156 || !strcmp (p
, "_dynamic"))
19158 cp_lexer_consume_token (parser
->lexer
);
19161 error ("%E requires %<-fgnu-tm%>", name
);
19172 /* Parse an (optional) virt-specifier-seq.
19174 virt-specifier-seq:
19175 virt-specifier virt-specifier-seq [opt]
19181 Returns a bitmask representing the virt-specifiers. */
19183 static cp_virt_specifiers
19184 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
19186 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
19191 cp_virt_specifiers virt_specifier
;
19193 /* Peek at the next token. */
19194 token
= cp_lexer_peek_token (parser
->lexer
);
19195 /* See if it's a virt-specifier-qualifier. */
19196 if (token
->type
!= CPP_NAME
)
19198 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
19200 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19201 virt_specifier
= VIRT_SPEC_OVERRIDE
;
19203 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
19205 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
19206 virt_specifier
= VIRT_SPEC_FINAL
;
19208 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
19210 virt_specifier
= VIRT_SPEC_FINAL
;
19215 if (virt_specifiers
& virt_specifier
)
19217 error_at (token
->location
, "duplicate virt-specifier");
19218 cp_lexer_purge_token (parser
->lexer
);
19222 cp_lexer_consume_token (parser
->lexer
);
19223 virt_specifiers
|= virt_specifier
;
19226 return virt_specifiers
;
19229 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19230 is in scope even though it isn't real. */
19233 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
19237 if (current_class_ptr
)
19239 /* We don't clear this between NSDMIs. Is it already what we want? */
19240 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
19241 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
19242 && cp_type_quals (type
) == quals
)
19246 this_parm
= build_this_parm (ctype
, quals
);
19247 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19248 current_class_ptr
= NULL_TREE
;
19250 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
19251 current_class_ptr
= this_parm
;
19254 /* Return true iff our current scope is a non-static data member
19258 parsing_nsdmi (void)
19260 /* We recognize NSDMI context by the context-less 'this' pointer set up
19261 by the function above. */
19262 if (current_class_ptr
19263 && TREE_CODE (current_class_ptr
) == PARM_DECL
19264 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
19269 /* Parse a late-specified return type, if any. This is not a separate
19270 non-terminal, but part of a function declarator, which looks like
19272 -> trailing-type-specifier-seq abstract-declarator(opt)
19274 Returns the type indicated by the type-id.
19276 In addition to this, parse any queued up omp declare simd
19277 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19279 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19283 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
19284 tree
& requires_clause
, cp_cv_quals quals
)
19287 tree type
= NULL_TREE
;
19288 bool declare_simd_p
= (parser
->omp_declare_simd
19290 && declarator
->kind
== cdk_id
);
19292 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
19293 && declarator
&& declarator
->kind
== cdk_id
);
19295 bool oacc_routine_p
= (parser
->oacc_routine
19297 && declarator
->kind
== cdk_id
);
19299 /* Peek at the next token. */
19300 token
= cp_lexer_peek_token (parser
->lexer
);
19301 /* A late-specified return type is indicated by an initial '->'. */
19302 if (token
->type
!= CPP_DEREF
19303 && token
->keyword
!= RID_REQUIRES
19304 && !(declare_simd_p
|| cilk_simd_fn_vector_p
|| oacc_routine_p
))
19307 tree save_ccp
= current_class_ptr
;
19308 tree save_ccr
= current_class_ref
;
19311 /* DR 1207: 'this' is in scope in the trailing return type. */
19312 inject_this_parameter (current_class_type
, quals
);
19315 if (token
->type
== CPP_DEREF
)
19317 /* Consume the ->. */
19318 cp_lexer_consume_token (parser
->lexer
);
19320 type
= cp_parser_trailing_type_id (parser
);
19323 /* Function declarations may be followed by a trailing
19324 requires-clause. */
19325 requires_clause
= cp_parser_requires_clause_opt (parser
);
19327 if (cilk_simd_fn_vector_p
)
19328 declarator
->std_attributes
19329 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
19330 declarator
->std_attributes
);
19331 if (declare_simd_p
)
19332 declarator
->std_attributes
19333 = cp_parser_late_parsing_omp_declare_simd (parser
,
19334 declarator
->std_attributes
);
19335 if (oacc_routine_p
)
19336 declarator
->std_attributes
19337 = cp_parser_late_parsing_oacc_routine (parser
,
19338 declarator
->std_attributes
);
19342 current_class_ptr
= save_ccp
;
19343 current_class_ref
= save_ccr
;
19349 /* Parse a declarator-id.
19353 :: [opt] nested-name-specifier [opt] type-name
19355 In the `id-expression' case, the value returned is as for
19356 cp_parser_id_expression if the id-expression was an unqualified-id.
19357 If the id-expression was a qualified-id, then a SCOPE_REF is
19358 returned. The first operand is the scope (either a NAMESPACE_DECL
19359 or TREE_TYPE), but the second is still just a representation of an
19363 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
19366 /* The expression must be an id-expression. Assume that qualified
19367 names are the names of types so that:
19370 int S<T>::R::i = 3;
19372 will work; we must treat `S<T>::R' as the name of a type.
19373 Similarly, assume that qualified names are templates, where
19377 int S<T>::R<T>::i = 3;
19380 id
= cp_parser_id_expression (parser
,
19381 /*template_keyword_p=*/false,
19382 /*check_dependency_p=*/false,
19383 /*template_p=*/NULL
,
19384 /*declarator_p=*/true,
19386 if (id
&& BASELINK_P (id
))
19387 id
= BASELINK_FUNCTIONS (id
);
19391 /* Parse a type-id.
19394 type-specifier-seq abstract-declarator [opt]
19396 Returns the TYPE specified. */
19399 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
19400 bool is_trailing_return
)
19402 cp_decl_specifier_seq type_specifier_seq
;
19403 cp_declarator
*abstract_declarator
;
19405 /* Parse the type-specifier-seq. */
19406 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
19407 is_trailing_return
,
19408 &type_specifier_seq
);
19409 if (type_specifier_seq
.type
== error_mark_node
)
19410 return error_mark_node
;
19412 /* There might or might not be an abstract declarator. */
19413 cp_parser_parse_tentatively (parser
);
19414 /* Look for the declarator. */
19415 abstract_declarator
19416 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
19417 /*parenthesized_p=*/NULL
,
19418 /*member_p=*/false,
19419 /*friend_p=*/false);
19420 /* Check to see if there really was a declarator. */
19421 if (!cp_parser_parse_definitely (parser
))
19422 abstract_declarator
= NULL
;
19424 if (type_specifier_seq
.type
19425 /* The concepts TS allows 'auto' as a type-id. */
19427 /* None of the valid uses of 'auto' in C++14 involve the type-id
19428 nonterminal, but it is valid in a trailing-return-type. */
19429 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
19430 && type_uses_auto (type_specifier_seq
.type
))
19432 /* A type-id with type 'auto' is only ok if the abstract declarator
19433 is a function declarator with a late-specified return type.
19435 A type-id with 'auto' is also valid in a trailing-return-type
19436 in a compound-requirement. */
19437 if (abstract_declarator
19438 && abstract_declarator
->kind
== cdk_function
19439 && abstract_declarator
->u
.function
.late_return_type
)
19441 else if (parser
->in_result_type_constraint_p
)
19445 error ("invalid use of %<auto%>");
19446 return error_mark_node
;
19450 return groktypename (&type_specifier_seq
, abstract_declarator
,
19455 cp_parser_type_id (cp_parser
*parser
)
19457 return cp_parser_type_id_1 (parser
, false, false);
19461 cp_parser_template_type_arg (cp_parser
*parser
)
19464 const char *saved_message
= parser
->type_definition_forbidden_message
;
19465 parser
->type_definition_forbidden_message
19466 = G_("types may not be defined in template arguments");
19467 r
= cp_parser_type_id_1 (parser
, true, false);
19468 parser
->type_definition_forbidden_message
= saved_message
;
19469 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
19471 error ("invalid use of %<auto%> in template argument");
19472 r
= error_mark_node
;
19478 cp_parser_trailing_type_id (cp_parser
*parser
)
19480 return cp_parser_type_id_1 (parser
, false, true);
19483 /* Parse a type-specifier-seq.
19485 type-specifier-seq:
19486 type-specifier type-specifier-seq [opt]
19490 type-specifier-seq:
19491 attributes type-specifier-seq [opt]
19493 If IS_DECLARATION is true, we are at the start of a "condition" or
19494 exception-declaration, so we might be followed by a declarator-id.
19496 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19497 i.e. we've just seen "->".
19499 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19502 cp_parser_type_specifier_seq (cp_parser
* parser
,
19503 bool is_declaration
,
19504 bool is_trailing_return
,
19505 cp_decl_specifier_seq
*type_specifier_seq
)
19507 bool seen_type_specifier
= false;
19508 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
19509 cp_token
*start_token
= NULL
;
19511 /* Clear the TYPE_SPECIFIER_SEQ. */
19512 clear_decl_specs (type_specifier_seq
);
19514 /* In the context of a trailing return type, enum E { } is an
19515 elaborated-type-specifier followed by a function-body, not an
19517 if (is_trailing_return
)
19518 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
19520 /* Parse the type-specifiers and attributes. */
19523 tree type_specifier
;
19524 bool is_cv_qualifier
;
19526 /* Check for attributes first. */
19527 if (cp_next_tokens_can_be_attribute_p (parser
))
19529 type_specifier_seq
->attributes
=
19530 chainon (type_specifier_seq
->attributes
,
19531 cp_parser_attributes_opt (parser
));
19535 /* record the token of the beginning of the type specifier seq,
19536 for error reporting purposes*/
19538 start_token
= cp_lexer_peek_token (parser
->lexer
);
19540 /* Look for the type-specifier. */
19541 type_specifier
= cp_parser_type_specifier (parser
,
19543 type_specifier_seq
,
19544 /*is_declaration=*/false,
19547 if (!type_specifier
)
19549 /* If the first type-specifier could not be found, this is not a
19550 type-specifier-seq at all. */
19551 if (!seen_type_specifier
)
19553 /* Set in_declarator_p to avoid skipping to the semicolon. */
19554 int in_decl
= parser
->in_declarator_p
;
19555 parser
->in_declarator_p
= true;
19557 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
19558 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19559 cp_parser_error (parser
, "expected type-specifier");
19561 parser
->in_declarator_p
= in_decl
;
19563 type_specifier_seq
->type
= error_mark_node
;
19566 /* If subsequent type-specifiers could not be found, the
19567 type-specifier-seq is complete. */
19571 seen_type_specifier
= true;
19572 /* The standard says that a condition can be:
19574 type-specifier-seq declarator = assignment-expression
19581 we should treat the "S" as a declarator, not as a
19582 type-specifier. The standard doesn't say that explicitly for
19583 type-specifier-seq, but it does say that for
19584 decl-specifier-seq in an ordinary declaration. Perhaps it
19585 would be clearer just to allow a decl-specifier-seq here, and
19586 then add a semantic restriction that if any decl-specifiers
19587 that are not type-specifiers appear, the program is invalid. */
19588 if (is_declaration
&& !is_cv_qualifier
)
19589 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
19593 /* Return whether the function currently being declared has an associated
19594 template parameter list. */
19597 function_being_declared_is_template_p (cp_parser
* parser
)
19599 if (!current_template_parms
|| processing_template_parmlist
)
19602 if (parser
->implicit_template_scope
)
19605 if (at_class_scope_p ()
19606 && TYPE_BEING_DEFINED (current_class_type
))
19607 return parser
->num_template_parameter_lists
!= 0;
19609 return ((int) parser
->num_template_parameter_lists
> template_class_depth
19610 (current_class_type
));
19613 /* Parse a parameter-declaration-clause.
19615 parameter-declaration-clause:
19616 parameter-declaration-list [opt] ... [opt]
19617 parameter-declaration-list , ...
19619 Returns a representation for the parameter declarations. A return
19620 value of NULL indicates a parameter-declaration-clause consisting
19621 only of an ellipsis. */
19624 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
19633 int auto_is_implicit_function_template_parm_p
;
19635 parser
->auto_is_implicit_function_template_parm_p
19636 = auto_is_implicit_function_template_parm_p
;
19638 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
19642 if (!processing_specialization
19643 && !processing_template_parmlist
19644 && !processing_explicit_instantiation
)
19645 if (!current_function_decl
19646 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
19647 parser
->auto_is_implicit_function_template_parm_p
= true;
19649 /* Peek at the next token. */
19650 token
= cp_lexer_peek_token (parser
->lexer
);
19651 /* Check for trivial parameter-declaration-clauses. */
19652 if (token
->type
== CPP_ELLIPSIS
)
19654 /* Consume the `...' token. */
19655 cp_lexer_consume_token (parser
->lexer
);
19658 else if (token
->type
== CPP_CLOSE_PAREN
)
19659 /* There are no parameters. */
19661 #ifndef NO_IMPLICIT_EXTERN_C
19662 if (in_system_header_at (input_location
)
19663 && current_class_type
== NULL
19664 && current_lang_name
== lang_name_c
)
19668 return void_list_node
;
19670 /* Check for `(void)', too, which is a special case. */
19671 else if (token
->keyword
== RID_VOID
19672 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
19673 == CPP_CLOSE_PAREN
))
19675 /* Consume the `void' token. */
19676 cp_lexer_consume_token (parser
->lexer
);
19677 /* There are no parameters. */
19678 return void_list_node
;
19681 /* Parse the parameter-declaration-list. */
19682 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
19683 /* If a parse error occurred while parsing the
19684 parameter-declaration-list, then the entire
19685 parameter-declaration-clause is erroneous. */
19689 /* Peek at the next token. */
19690 token
= cp_lexer_peek_token (parser
->lexer
);
19691 /* If it's a `,', the clause should terminate with an ellipsis. */
19692 if (token
->type
== CPP_COMMA
)
19694 /* Consume the `,'. */
19695 cp_lexer_consume_token (parser
->lexer
);
19696 /* Expect an ellipsis. */
19698 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
19700 /* It might also be `...' if the optional trailing `,' was
19702 else if (token
->type
== CPP_ELLIPSIS
)
19704 /* Consume the `...' token. */
19705 cp_lexer_consume_token (parser
->lexer
);
19706 /* And remember that we saw it. */
19710 ellipsis_p
= false;
19712 /* Finish the parameter list. */
19714 parameters
= chainon (parameters
, void_list_node
);
19719 /* Parse a parameter-declaration-list.
19721 parameter-declaration-list:
19722 parameter-declaration
19723 parameter-declaration-list , parameter-declaration
19725 Returns a representation of the parameter-declaration-list, as for
19726 cp_parser_parameter_declaration_clause. However, the
19727 `void_list_node' is never appended to the list. Upon return,
19728 *IS_ERROR will be true iff an error occurred. */
19731 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
19733 tree parameters
= NULL_TREE
;
19734 tree
*tail
= ¶meters
;
19735 bool saved_in_unbraced_linkage_specification_p
;
19738 /* Assume all will go well. */
19740 /* The special considerations that apply to a function within an
19741 unbraced linkage specifications do not apply to the parameters
19742 to the function. */
19743 saved_in_unbraced_linkage_specification_p
19744 = parser
->in_unbraced_linkage_specification_p
;
19745 parser
->in_unbraced_linkage_specification_p
= false;
19747 /* Look for more parameters. */
19750 cp_parameter_declarator
*parameter
;
19751 tree decl
= error_mark_node
;
19752 bool parenthesized_p
= false;
19753 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
19754 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19755 (current_template_parms
)) : 0);
19757 /* Parse the parameter. */
19759 = cp_parser_parameter_declaration (parser
,
19760 /*template_parm_p=*/false,
19763 /* We don't know yet if the enclosing context is deprecated, so wait
19764 and warn in grokparms if appropriate. */
19765 deprecated_state
= DEPRECATED_SUPPRESS
;
19769 /* If a function parameter pack was specified and an implicit template
19770 parameter was introduced during cp_parser_parameter_declaration,
19771 change any implicit parameters introduced into packs. */
19772 if (parser
->implicit_template_parms
19773 && parameter
->declarator
19774 && parameter
->declarator
->parameter_pack_p
)
19776 int latest_template_parm_idx
= TREE_VEC_LENGTH
19777 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
19779 if (latest_template_parm_idx
!= template_parm_idx
)
19780 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
19781 (parameter
->decl_specifiers
.type
,
19782 template_parm_idx
, latest_template_parm_idx
);
19785 decl
= grokdeclarator (parameter
->declarator
,
19786 ¶meter
->decl_specifiers
,
19788 parameter
->default_argument
!= NULL_TREE
,
19789 ¶meter
->decl_specifiers
.attributes
);
19792 deprecated_state
= DEPRECATED_NORMAL
;
19794 /* If a parse error occurred parsing the parameter declaration,
19795 then the entire parameter-declaration-list is erroneous. */
19796 if (decl
== error_mark_node
)
19799 parameters
= error_mark_node
;
19803 if (parameter
->decl_specifiers
.attributes
)
19804 cplus_decl_attributes (&decl
,
19805 parameter
->decl_specifiers
.attributes
,
19807 if (DECL_NAME (decl
))
19808 decl
= pushdecl (decl
);
19810 if (decl
!= error_mark_node
)
19812 retrofit_lang_decl (decl
);
19813 DECL_PARM_INDEX (decl
) = ++index
;
19814 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
19817 /* Add the new parameter to the list. */
19818 *tail
= build_tree_list (parameter
->default_argument
, decl
);
19819 tail
= &TREE_CHAIN (*tail
);
19821 /* Peek at the next token. */
19822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
19823 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
19824 /* These are for Objective-C++ */
19825 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
19826 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19827 /* The parameter-declaration-list is complete. */
19829 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19833 /* Peek at the next token. */
19834 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19835 /* If it's an ellipsis, then the list is complete. */
19836 if (token
->type
== CPP_ELLIPSIS
)
19838 /* Otherwise, there must be more parameters. Consume the
19840 cp_lexer_consume_token (parser
->lexer
);
19841 /* When parsing something like:
19843 int i(float f, double d)
19845 we can tell after seeing the declaration for "f" that we
19846 are not looking at an initialization of a variable "i",
19847 but rather at the declaration of a function "i".
19849 Due to the fact that the parsing of template arguments
19850 (as specified to a template-id) requires backtracking we
19851 cannot use this technique when inside a template argument
19853 if (!parser
->in_template_argument_list_p
19854 && !parser
->in_type_id_in_expr_p
19855 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19856 /* However, a parameter-declaration of the form
19857 "float(f)" (which is a valid declaration of a
19858 parameter "f") can also be interpreted as an
19859 expression (the conversion of "f" to "float"). */
19860 && !parenthesized_p
)
19861 cp_parser_commit_to_tentative_parse (parser
);
19865 cp_parser_error (parser
, "expected %<,%> or %<...%>");
19866 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19867 cp_parser_skip_to_closing_parenthesis (parser
,
19868 /*recovering=*/true,
19869 /*or_comma=*/false,
19870 /*consume_paren=*/false);
19875 parser
->in_unbraced_linkage_specification_p
19876 = saved_in_unbraced_linkage_specification_p
;
19878 /* Reset implicit_template_scope if we are about to leave the function
19879 parameter list that introduced it. Note that for out-of-line member
19880 definitions, there will be one or more class scopes before we get to
19881 the template parameter scope. */
19883 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
19884 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
19886 while (maybe_its
->kind
== sk_class
)
19887 maybe_its
= maybe_its
->level_chain
;
19888 if (maybe_its
== its
)
19890 parser
->implicit_template_parms
= 0;
19891 parser
->implicit_template_scope
= 0;
19898 /* Parse a parameter declaration.
19900 parameter-declaration:
19901 decl-specifier-seq ... [opt] declarator
19902 decl-specifier-seq declarator = assignment-expression
19903 decl-specifier-seq ... [opt] abstract-declarator [opt]
19904 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19906 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19907 declares a template parameter. (In that case, a non-nested `>'
19908 token encountered during the parsing of the assignment-expression
19909 is not interpreted as a greater-than operator.)
19911 Returns a representation of the parameter, or NULL if an error
19912 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19913 true iff the declarator is of the form "(p)". */
19915 static cp_parameter_declarator
*
19916 cp_parser_parameter_declaration (cp_parser
*parser
,
19917 bool template_parm_p
,
19918 bool *parenthesized_p
)
19920 int declares_class_or_enum
;
19921 cp_decl_specifier_seq decl_specifiers
;
19922 cp_declarator
*declarator
;
19923 tree default_argument
;
19924 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
19925 const char *saved_message
;
19926 bool template_parameter_pack_p
= false;
19928 /* In a template parameter, `>' is not an operator.
19932 When parsing a default template-argument for a non-type
19933 template-parameter, the first non-nested `>' is taken as the end
19934 of the template parameter-list rather than a greater-than
19937 /* Type definitions may not appear in parameter types. */
19938 saved_message
= parser
->type_definition_forbidden_message
;
19939 parser
->type_definition_forbidden_message
19940 = G_("types may not be defined in parameter types");
19942 /* Parse the declaration-specifiers. */
19943 cp_parser_decl_specifier_seq (parser
,
19944 CP_PARSER_FLAGS_NONE
,
19946 &declares_class_or_enum
);
19948 /* Complain about missing 'typename' or other invalid type names. */
19949 if (!decl_specifiers
.any_type_specifiers_p
19950 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19951 decl_specifiers
.type
= error_mark_node
;
19953 /* If an error occurred, there's no reason to attempt to parse the
19954 rest of the declaration. */
19955 if (cp_parser_error_occurred (parser
))
19957 parser
->type_definition_forbidden_message
= saved_message
;
19961 /* Peek at the next token. */
19962 token
= cp_lexer_peek_token (parser
->lexer
);
19964 /* If the next token is a `)', `,', `=', `>', or `...', then there
19965 is no declarator. However, when variadic templates are enabled,
19966 there may be a declarator following `...'. */
19967 if (token
->type
== CPP_CLOSE_PAREN
19968 || token
->type
== CPP_COMMA
19969 || token
->type
== CPP_EQ
19970 || token
->type
== CPP_GREATER
)
19973 if (parenthesized_p
)
19974 *parenthesized_p
= false;
19976 /* Otherwise, there should be a declarator. */
19979 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19980 parser
->default_arg_ok_p
= false;
19982 /* After seeing a decl-specifier-seq, if the next token is not a
19983 "(", there is no possibility that the code is a valid
19984 expression. Therefore, if parsing tentatively, we commit at
19986 if (!parser
->in_template_argument_list_p
19987 /* In an expression context, having seen:
19991 we cannot be sure whether we are looking at a
19992 function-type (taking a "char" as a parameter) or a cast
19993 of some object of type "char" to "int". */
19994 && !parser
->in_type_id_in_expr_p
19995 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19996 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19997 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
19998 cp_parser_commit_to_tentative_parse (parser
);
19999 /* Parse the declarator. */
20000 declarator_token_start
= token
;
20001 declarator
= cp_parser_declarator (parser
,
20002 CP_PARSER_DECLARATOR_EITHER
,
20003 /*ctor_dtor_or_conv_p=*/NULL
,
20005 /*member_p=*/false,
20006 /*friend_p=*/false);
20007 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20008 /* After the declarator, allow more attributes. */
20009 decl_specifiers
.attributes
20010 = chainon (decl_specifiers
.attributes
,
20011 cp_parser_attributes_opt (parser
));
20013 /* If the declarator is a template parameter pack, remember that and
20014 clear the flag in the declarator itself so we don't get errors
20015 from grokdeclarator. */
20016 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
20018 declarator
->parameter_pack_p
= false;
20019 template_parameter_pack_p
= true;
20023 /* If the next token is an ellipsis, and we have not seen a declarator
20024 name, and if either the type of the declarator contains parameter
20025 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20026 for, eg, abbreviated integral type names), then we actually have a
20027 parameter pack expansion expression. Otherwise, leave the ellipsis
20028 for a C-style variadic function. */
20029 token
= cp_lexer_peek_token (parser
->lexer
);
20030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20032 tree type
= decl_specifiers
.type
;
20034 if (type
&& DECL_P (type
))
20035 type
= TREE_TYPE (type
);
20038 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
20039 && (template_parm_p
|| uses_parameter_packs (type
)))
20040 || (!type
&& template_parm_p
))
20041 && declarator_can_be_parameter_pack (declarator
))
20043 /* Consume the `...'. */
20044 cp_lexer_consume_token (parser
->lexer
);
20045 maybe_warn_variadic_templates ();
20047 /* Build a pack expansion type */
20048 if (template_parm_p
)
20049 template_parameter_pack_p
= true;
20050 else if (declarator
)
20051 declarator
->parameter_pack_p
= true;
20053 decl_specifiers
.type
= make_pack_expansion (type
);
20057 /* The restriction on defining new types applies only to the type
20058 of the parameter, not to the default argument. */
20059 parser
->type_definition_forbidden_message
= saved_message
;
20061 /* If the next token is `=', then process a default argument. */
20062 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20064 tree type
= decl_specifiers
.type
;
20065 token
= cp_lexer_peek_token (parser
->lexer
);
20066 /* If we are defining a class, then the tokens that make up the
20067 default argument must be saved and processed later. */
20068 if (!template_parm_p
&& at_class_scope_p ()
20069 && TYPE_BEING_DEFINED (current_class_type
)
20070 && !LAMBDA_TYPE_P (current_class_type
))
20071 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
20073 // A constrained-type-specifier may declare a type template-parameter.
20074 else if (declares_constrained_type_template_parameter (type
))
20076 = cp_parser_default_type_template_argument (parser
);
20078 // A constrained-type-specifier may declare a template-template-parameter.
20079 else if (declares_constrained_template_template_parameter (type
))
20081 = cp_parser_default_template_template_argument (parser
);
20083 /* Outside of a class definition, we can just parse the
20084 assignment-expression. */
20087 = cp_parser_default_argument (parser
, template_parm_p
);
20089 if (!parser
->default_arg_ok_p
)
20091 permerror (token
->location
,
20092 "default arguments are only "
20093 "permitted for function parameters");
20095 else if ((declarator
&& declarator
->parameter_pack_p
)
20096 || template_parameter_pack_p
20097 || (decl_specifiers
.type
20098 && PACK_EXPANSION_P (decl_specifiers
.type
)))
20100 /* Find the name of the parameter pack. */
20101 cp_declarator
*id_declarator
= declarator
;
20102 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
20103 id_declarator
= id_declarator
->declarator
;
20105 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
20106 error_at (declarator_token_start
->location
,
20108 ? G_("template parameter pack %qD "
20109 "cannot have a default argument")
20110 : G_("parameter pack %qD cannot have "
20111 "a default argument"),
20112 id_declarator
->u
.id
.unqualified_name
);
20114 error_at (declarator_token_start
->location
,
20116 ? G_("template parameter pack cannot have "
20117 "a default argument")
20118 : G_("parameter pack cannot have a "
20119 "default argument"));
20121 default_argument
= NULL_TREE
;
20125 default_argument
= NULL_TREE
;
20127 return make_parameter_declarator (&decl_specifiers
,
20130 template_parameter_pack_p
);
20133 /* Parse a default argument and return it.
20135 TEMPLATE_PARM_P is true if this is a default argument for a
20136 non-type template parameter. */
20138 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
20140 tree default_argument
= NULL_TREE
;
20141 bool saved_greater_than_is_operator_p
;
20142 bool saved_local_variables_forbidden_p
;
20143 bool non_constant_p
, is_direct_init
;
20145 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20147 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
20148 parser
->greater_than_is_operator_p
= !template_parm_p
;
20149 /* Local variable names (and the `this' keyword) may not
20150 appear in a default argument. */
20151 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
20152 parser
->local_variables_forbidden_p
= true;
20153 /* Parse the assignment-expression. */
20154 if (template_parm_p
)
20155 push_deferring_access_checks (dk_no_deferred
);
20156 tree saved_class_ptr
= NULL_TREE
;
20157 tree saved_class_ref
= NULL_TREE
;
20158 /* The "this" pointer is not valid in a default argument. */
20161 saved_class_ptr
= current_class_ptr
;
20162 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
20163 saved_class_ref
= current_class_ref
;
20164 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
20167 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
20168 /* Restore the "this" pointer. */
20171 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
20172 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
20174 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
20175 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20176 if (template_parm_p
)
20177 pop_deferring_access_checks ();
20178 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
20179 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
20181 return default_argument
;
20184 /* Parse a function-body.
20187 compound_statement */
20190 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
20192 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
20193 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
20197 /* Parse a ctor-initializer-opt followed by a function-body. Return
20198 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20199 is true we are parsing a function-try-block. */
20202 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
20203 bool in_function_try_block
)
20206 bool ctor_initializer_p
;
20207 const bool check_body_p
=
20208 DECL_CONSTRUCTOR_P (current_function_decl
)
20209 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
20212 /* Begin the function body. */
20213 body
= begin_function_body ();
20214 /* Parse the optional ctor-initializer. */
20215 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
20217 /* If we're parsing a constexpr constructor definition, we need
20218 to check that the constructor body is indeed empty. However,
20219 before we get to cp_parser_function_body lot of junk has been
20220 generated, so we can't just check that we have an empty block.
20221 Rather we take a snapshot of the outermost block, and check whether
20222 cp_parser_function_body changed its state. */
20225 list
= cur_stmt_list
;
20226 if (STATEMENT_LIST_TAIL (list
))
20227 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
20229 /* Parse the function-body. */
20230 cp_parser_function_body (parser
, in_function_try_block
);
20232 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
20233 /* Finish the function body. */
20234 finish_function_body (body
);
20236 return ctor_initializer_p
;
20239 /* Parse an initializer.
20242 = initializer-clause
20243 ( expression-list )
20245 Returns an expression representing the initializer. If no
20246 initializer is present, NULL_TREE is returned.
20248 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20249 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20250 set to TRUE if there is no initializer present. If there is an
20251 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20252 is set to true; otherwise it is set to false. */
20255 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
20256 bool* non_constant_p
)
20261 /* Peek at the next token. */
20262 token
= cp_lexer_peek_token (parser
->lexer
);
20264 /* Let our caller know whether or not this initializer was
20266 *is_direct_init
= (token
->type
!= CPP_EQ
);
20267 /* Assume that the initializer is constant. */
20268 *non_constant_p
= false;
20270 if (token
->type
== CPP_EQ
)
20272 /* Consume the `='. */
20273 cp_lexer_consume_token (parser
->lexer
);
20274 /* Parse the initializer-clause. */
20275 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
20277 else if (token
->type
== CPP_OPEN_PAREN
)
20279 vec
<tree
, va_gc
> *vec
;
20280 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
20282 /*allow_expansion_p=*/true,
20285 return error_mark_node
;
20286 init
= build_tree_list_vec (vec
);
20287 release_tree_vector (vec
);
20289 else if (token
->type
== CPP_OPEN_BRACE
)
20291 cp_lexer_set_source_position (parser
->lexer
);
20292 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
20293 init
= cp_parser_braced_list (parser
, non_constant_p
);
20294 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
20298 /* Anything else is an error. */
20299 cp_parser_error (parser
, "expected initializer");
20300 init
= error_mark_node
;
20306 /* Parse an initializer-clause.
20308 initializer-clause:
20309 assignment-expression
20312 Returns an expression representing the initializer.
20314 If the `assignment-expression' production is used the value
20315 returned is simply a representation for the expression.
20317 Otherwise, calls cp_parser_braced_list. */
20320 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
20324 /* Assume the expression is constant. */
20325 *non_constant_p
= false;
20327 /* If it is not a `{', then we are looking at an
20328 assignment-expression. */
20329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
20332 = cp_parser_constant_expression (parser
,
20333 /*allow_non_constant_p=*/true,
20337 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
20339 return initializer
;
20342 /* Parse a brace-enclosed initializer list.
20345 { initializer-list , [opt] }
20348 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20349 the elements of the initializer-list (or NULL, if the last
20350 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20351 NULL_TREE. There is no way to detect whether or not the optional
20352 trailing `,' was provided. NON_CONSTANT_P is as for
20353 cp_parser_initializer. */
20356 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
20360 /* Consume the `{' token. */
20361 cp_lexer_consume_token (parser
->lexer
);
20362 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20363 initializer
= make_node (CONSTRUCTOR
);
20364 /* If it's not a `}', then there is a non-trivial initializer. */
20365 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
20367 /* Parse the initializer list. */
20368 CONSTRUCTOR_ELTS (initializer
)
20369 = cp_parser_initializer_list (parser
, non_constant_p
);
20370 /* A trailing `,' token is allowed. */
20371 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20372 cp_lexer_consume_token (parser
->lexer
);
20375 *non_constant_p
= false;
20376 /* Now, there should be a trailing `}'. */
20377 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20378 TREE_TYPE (initializer
) = init_list_type_node
;
20379 return initializer
;
20382 /* Consume tokens up to, and including, the next non-nested closing `]'.
20383 Returns true iff we found a closing `]'. */
20386 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
20388 unsigned square_depth
= 0;
20392 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
20394 switch (token
->type
)
20397 case CPP_PRAGMA_EOL
:
20398 /* If we've run out of tokens, then there is no closing `]'. */
20401 case CPP_OPEN_SQUARE
:
20405 case CPP_CLOSE_SQUARE
:
20406 if (!square_depth
--)
20408 cp_lexer_consume_token (parser
->lexer
);
20417 /* Consume the token. */
20418 cp_lexer_consume_token (parser
->lexer
);
20422 /* Return true if we are looking at an array-designator, false otherwise. */
20425 cp_parser_array_designator_p (cp_parser
*parser
)
20427 /* Consume the `['. */
20428 cp_lexer_consume_token (parser
->lexer
);
20430 cp_lexer_save_tokens (parser
->lexer
);
20432 /* Skip tokens until the next token is a closing square bracket.
20433 If we find the closing `]', and the next token is a `=', then
20434 we are looking at an array designator. */
20435 bool array_designator_p
20436 = (cp_parser_skip_to_closing_square_bracket (parser
)
20437 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
20439 /* Roll back the tokens we skipped. */
20440 cp_lexer_rollback_tokens (parser
->lexer
);
20442 return array_designator_p
;
20445 /* Parse an initializer-list.
20448 initializer-clause ... [opt]
20449 initializer-list , initializer-clause ... [opt]
20454 designation initializer-clause ...[opt]
20455 initializer-list , designation initializer-clause ...[opt]
20460 [ constant-expression ] =
20462 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20463 for the initializer. If the INDEX of the elt is non-NULL, it is the
20464 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20465 as for cp_parser_initializer. */
20467 static vec
<constructor_elt
, va_gc
> *
20468 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
20470 vec
<constructor_elt
, va_gc
> *v
= NULL
;
20472 /* Assume all of the expressions are constant. */
20473 *non_constant_p
= false;
20475 /* Parse the rest of the list. */
20481 bool clause_non_constant_p
;
20483 /* If the next token is an identifier and the following one is a
20484 colon, we are looking at the GNU designated-initializer
20486 if (cp_parser_allow_gnu_extensions_p (parser
)
20487 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
20488 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
20490 /* Warn the user that they are using an extension. */
20491 pedwarn (input_location
, OPT_Wpedantic
,
20492 "ISO C++ does not allow designated initializers");
20493 /* Consume the identifier. */
20494 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
20495 /* Consume the `:'. */
20496 cp_lexer_consume_token (parser
->lexer
);
20498 /* Also handle the C99 syntax, '. id ='. */
20499 else if (cp_parser_allow_gnu_extensions_p (parser
)
20500 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
20501 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
20502 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
20504 /* Warn the user that they are using an extension. */
20505 pedwarn (input_location
, OPT_Wpedantic
,
20506 "ISO C++ does not allow C99 designated initializers");
20507 /* Consume the `.'. */
20508 cp_lexer_consume_token (parser
->lexer
);
20509 /* Consume the identifier. */
20510 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
20511 /* Consume the `='. */
20512 cp_lexer_consume_token (parser
->lexer
);
20514 /* Also handle C99 array designators, '[ const ] ='. */
20515 else if (cp_parser_allow_gnu_extensions_p (parser
)
20516 && !c_dialect_objc ()
20517 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
20519 /* In C++11, [ could start a lambda-introducer. */
20520 bool non_const
= false;
20522 cp_parser_parse_tentatively (parser
);
20524 if (!cp_parser_array_designator_p (parser
))
20526 cp_parser_simulate_error (parser
);
20527 designator
= NULL_TREE
;
20531 designator
= cp_parser_constant_expression (parser
, true,
20533 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
20534 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20537 if (!cp_parser_parse_definitely (parser
))
20538 designator
= NULL_TREE
;
20539 else if (non_const
)
20540 require_potential_rvalue_constant_expression (designator
);
20543 designator
= NULL_TREE
;
20545 /* Parse the initializer. */
20546 initializer
= cp_parser_initializer_clause (parser
,
20547 &clause_non_constant_p
);
20548 /* If any clause is non-constant, so is the entire initializer. */
20549 if (clause_non_constant_p
)
20550 *non_constant_p
= true;
20552 /* If we have an ellipsis, this is an initializer pack
20554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20556 /* Consume the `...'. */
20557 cp_lexer_consume_token (parser
->lexer
);
20559 /* Turn the initializer into an initializer expansion. */
20560 initializer
= make_pack_expansion (initializer
);
20563 /* Add it to the vector. */
20564 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
20566 /* If the next token is not a comma, we have reached the end of
20568 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20571 /* Peek at the next token. */
20572 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20573 /* If the next token is a `}', then we're still done. An
20574 initializer-clause can have a trailing `,' after the
20575 initializer-list and before the closing `}'. */
20576 if (token
->type
== CPP_CLOSE_BRACE
)
20579 /* Consume the `,' token. */
20580 cp_lexer_consume_token (parser
->lexer
);
20586 /* Classes [gram.class] */
20588 /* Parse a class-name.
20594 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20595 to indicate that names looked up in dependent types should be
20596 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20597 keyword has been used to indicate that the name that appears next
20598 is a template. TAG_TYPE indicates the explicit tag given before
20599 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20600 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20601 is the class being defined in a class-head. If ENUM_OK is TRUE,
20602 enum-names are also accepted.
20604 Returns the TYPE_DECL representing the class. */
20607 cp_parser_class_name (cp_parser
*parser
,
20608 bool typename_keyword_p
,
20609 bool template_keyword_p
,
20610 enum tag_types tag_type
,
20611 bool check_dependency_p
,
20613 bool is_declaration
,
20620 tree identifier
= NULL_TREE
;
20622 /* All class-names start with an identifier. */
20623 token
= cp_lexer_peek_token (parser
->lexer
);
20624 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
20626 cp_parser_error (parser
, "expected class-name");
20627 return error_mark_node
;
20630 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20631 to a template-id, so we save it here. */
20632 scope
= parser
->scope
;
20633 if (scope
== error_mark_node
)
20634 return error_mark_node
;
20636 /* Any name names a type if we're following the `typename' keyword
20637 in a qualified name where the enclosing scope is type-dependent. */
20638 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
20639 && dependent_type_p (scope
));
20640 /* Handle the common case (an identifier, but not a template-id)
20642 if (token
->type
== CPP_NAME
20643 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
20645 cp_token
*identifier_token
;
20648 /* Look for the identifier. */
20649 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
20650 ambiguous_p
= identifier_token
->error_reported
;
20651 identifier
= cp_parser_identifier (parser
);
20652 /* If the next token isn't an identifier, we are certainly not
20653 looking at a class-name. */
20654 if (identifier
== error_mark_node
)
20655 decl
= error_mark_node
;
20656 /* If we know this is a type-name, there's no need to look it
20658 else if (typename_p
)
20662 tree ambiguous_decls
;
20663 /* If we already know that this lookup is ambiguous, then
20664 we've already issued an error message; there's no reason
20668 cp_parser_simulate_error (parser
);
20669 return error_mark_node
;
20671 /* If the next token is a `::', then the name must be a type
20674 [basic.lookup.qual]
20676 During the lookup for a name preceding the :: scope
20677 resolution operator, object, function, and enumerator
20678 names are ignored. */
20679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20680 tag_type
= typename_type
;
20681 /* Look up the name. */
20682 decl
= cp_parser_lookup_name (parser
, identifier
,
20684 /*is_template=*/false,
20685 /*is_namespace=*/false,
20686 check_dependency_p
,
20688 identifier_token
->location
);
20689 if (ambiguous_decls
)
20691 if (cp_parser_parsing_tentatively (parser
))
20692 cp_parser_simulate_error (parser
);
20693 return error_mark_node
;
20699 /* Try a template-id. */
20700 decl
= cp_parser_template_id (parser
, template_keyword_p
,
20701 check_dependency_p
,
20704 if (decl
== error_mark_node
)
20705 return error_mark_node
;
20708 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
20710 /* If this is a typename, create a TYPENAME_TYPE. */
20711 if (typename_p
&& decl
!= error_mark_node
)
20713 decl
= make_typename_type (scope
, decl
, typename_type
,
20714 /*complain=*/tf_error
);
20715 if (decl
!= error_mark_node
)
20716 decl
= TYPE_NAME (decl
);
20719 decl
= strip_using_decl (decl
);
20721 /* Check to see that it is really the name of a class. */
20722 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
20723 && identifier_p (TREE_OPERAND (decl
, 0))
20724 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20725 /* Situations like this:
20727 template <typename T> struct A {
20728 typename T::template X<int>::I i;
20731 are problematic. Is `T::template X<int>' a class-name? The
20732 standard does not seem to be definitive, but there is no other
20733 valid interpretation of the following `::'. Therefore, those
20734 names are considered class-names. */
20736 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
20737 if (decl
!= error_mark_node
)
20738 decl
= TYPE_NAME (decl
);
20740 else if (TREE_CODE (decl
) != TYPE_DECL
20741 || TREE_TYPE (decl
) == error_mark_node
20742 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
20743 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
20744 /* In Objective-C 2.0, a classname followed by '.' starts a
20745 dot-syntax expression, and it's not a type-name. */
20746 || (c_dialect_objc ()
20747 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
20748 && objc_is_class_name (decl
)))
20749 decl
= error_mark_node
;
20751 if (decl
== error_mark_node
)
20752 cp_parser_error (parser
, "expected class-name");
20753 else if (identifier
&& !parser
->scope
)
20754 maybe_note_name_used_in_class (identifier
, decl
);
20759 /* Parse a class-specifier.
20762 class-head { member-specification [opt] }
20764 Returns the TREE_TYPE representing the class. */
20767 cp_parser_class_specifier_1 (cp_parser
* parser
)
20770 tree attributes
= NULL_TREE
;
20771 bool nested_name_specifier_p
;
20772 unsigned saved_num_template_parameter_lists
;
20773 bool saved_in_function_body
;
20774 unsigned char in_statement
;
20775 bool in_switch_statement_p
;
20776 bool saved_in_unbraced_linkage_specification_p
;
20777 tree old_scope
= NULL_TREE
;
20778 tree scope
= NULL_TREE
;
20779 cp_token
*closing_brace
;
20781 push_deferring_access_checks (dk_no_deferred
);
20783 /* Parse the class-head. */
20784 type
= cp_parser_class_head (parser
,
20785 &nested_name_specifier_p
);
20786 /* If the class-head was a semantic disaster, skip the entire body
20790 cp_parser_skip_to_end_of_block_or_statement (parser
);
20791 pop_deferring_access_checks ();
20792 return error_mark_node
;
20795 /* Look for the `{'. */
20796 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
20798 pop_deferring_access_checks ();
20799 return error_mark_node
;
20802 cp_ensure_no_omp_declare_simd (parser
);
20803 cp_ensure_no_oacc_routine (parser
);
20805 /* Issue an error message if type-definitions are forbidden here. */
20806 cp_parser_check_type_definition (parser
);
20807 /* Remember that we are defining one more class. */
20808 ++parser
->num_classes_being_defined
;
20809 /* Inside the class, surrounding template-parameter-lists do not
20811 saved_num_template_parameter_lists
20812 = parser
->num_template_parameter_lists
;
20813 parser
->num_template_parameter_lists
= 0;
20814 /* We are not in a function body. */
20815 saved_in_function_body
= parser
->in_function_body
;
20816 parser
->in_function_body
= false;
20817 /* Or in a loop. */
20818 in_statement
= parser
->in_statement
;
20819 parser
->in_statement
= 0;
20820 /* Or in a switch. */
20821 in_switch_statement_p
= parser
->in_switch_statement_p
;
20822 parser
->in_switch_statement_p
= false;
20823 /* We are not immediately inside an extern "lang" block. */
20824 saved_in_unbraced_linkage_specification_p
20825 = parser
->in_unbraced_linkage_specification_p
;
20826 parser
->in_unbraced_linkage_specification_p
= false;
20828 // Associate constraints with the type.
20830 type
= associate_classtype_constraints (type
);
20832 /* Start the class. */
20833 if (nested_name_specifier_p
)
20835 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
20836 old_scope
= push_inner_scope (scope
);
20838 type
= begin_class_definition (type
);
20840 if (type
== error_mark_node
)
20841 /* If the type is erroneous, skip the entire body of the class. */
20842 cp_parser_skip_to_closing_brace (parser
);
20844 /* Parse the member-specification. */
20845 cp_parser_member_specification_opt (parser
);
20847 /* Look for the trailing `}'. */
20848 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
20849 /* Look for trailing attributes to apply to this class. */
20850 if (cp_parser_allow_gnu_extensions_p (parser
))
20851 attributes
= cp_parser_gnu_attributes_opt (parser
);
20852 if (type
!= error_mark_node
)
20853 type
= finish_struct (type
, attributes
);
20854 if (nested_name_specifier_p
)
20855 pop_inner_scope (old_scope
, scope
);
20857 /* We've finished a type definition. Check for the common syntax
20858 error of forgetting a semicolon after the definition. We need to
20859 be careful, as we can't just check for not-a-semicolon and be done
20860 with it; the user might have typed:
20862 class X { } c = ...;
20863 class X { } *p = ...;
20865 and so forth. Instead, enumerate all the possible tokens that
20866 might follow this production; if we don't see one of them, then
20867 complain and silently insert the semicolon. */
20869 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20870 bool want_semicolon
= true;
20872 if (cp_next_tokens_can_be_std_attribute_p (parser
))
20873 /* Don't try to parse c++11 attributes here. As per the
20874 grammar, that should be a task for
20875 cp_parser_decl_specifier_seq. */
20876 want_semicolon
= false;
20878 switch (token
->type
)
20881 case CPP_SEMICOLON
:
20884 case CPP_OPEN_PAREN
:
20885 case CPP_CLOSE_PAREN
:
20887 want_semicolon
= false;
20890 /* While it's legal for type qualifiers and storage class
20891 specifiers to follow type definitions in the grammar, only
20892 compiler testsuites contain code like that. Assume that if
20893 we see such code, then what we're really seeing is a case
20897 const <type> var = ...;
20902 static <type> func (...) ...
20904 i.e. the qualifier or specifier applies to the next
20905 declaration. To do so, however, we need to look ahead one
20906 more token to see if *that* token is a type specifier.
20908 This code could be improved to handle:
20911 static const <type> var = ...; */
20913 if (keyword_is_decl_specifier (token
->keyword
))
20915 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20917 /* Handling user-defined types here would be nice, but very
20920 = (lookahead
->type
== CPP_KEYWORD
20921 && keyword_begins_type_specifier (lookahead
->keyword
));
20928 /* If we don't have a type, then something is very wrong and we
20929 shouldn't try to do anything clever. Likewise for not seeing the
20931 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
20933 cp_token_position prev
20934 = cp_lexer_previous_token_position (parser
->lexer
);
20935 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
20936 location_t loc
= prev_token
->location
;
20938 if (CLASSTYPE_DECLARED_CLASS (type
))
20939 error_at (loc
, "expected %<;%> after class definition");
20940 else if (TREE_CODE (type
) == RECORD_TYPE
)
20941 error_at (loc
, "expected %<;%> after struct definition");
20942 else if (TREE_CODE (type
) == UNION_TYPE
)
20943 error_at (loc
, "expected %<;%> after union definition");
20945 gcc_unreachable ();
20947 /* Unget one token and smash it to look as though we encountered
20948 a semicolon in the input stream. */
20949 cp_lexer_set_token_position (parser
->lexer
, prev
);
20950 token
= cp_lexer_peek_token (parser
->lexer
);
20951 token
->type
= CPP_SEMICOLON
;
20952 token
->keyword
= RID_MAX
;
20956 /* If this class is not itself within the scope of another class,
20957 then we need to parse the bodies of all of the queued function
20958 definitions. Note that the queued functions defined in a class
20959 are not always processed immediately following the
20960 class-specifier for that class. Consider:
20963 struct B { void f() { sizeof (A); } };
20966 If `f' were processed before the processing of `A' were
20967 completed, there would be no way to compute the size of `A'.
20968 Note that the nesting we are interested in here is lexical --
20969 not the semantic nesting given by TYPE_CONTEXT. In particular,
20972 struct A { struct B; };
20973 struct A::B { void f() { } };
20975 there is no need to delay the parsing of `A::B::f'. */
20976 if (--parser
->num_classes_being_defined
== 0)
20979 tree class_type
= NULL_TREE
;
20980 tree pushed_scope
= NULL_TREE
;
20982 cp_default_arg_entry
*e
;
20983 tree save_ccp
, save_ccr
;
20985 /* In a first pass, parse default arguments to the functions.
20986 Then, in a second pass, parse the bodies of the functions.
20987 This two-phased approach handles cases like:
20995 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
20998 /* If there are default arguments that have not yet been processed,
20999 take care of them now. */
21000 if (class_type
!= e
->class_type
)
21003 pop_scope (pushed_scope
);
21004 class_type
= e
->class_type
;
21005 pushed_scope
= push_scope (class_type
);
21007 /* Make sure that any template parameters are in scope. */
21008 maybe_begin_member_template_processing (decl
);
21009 /* Parse the default argument expressions. */
21010 cp_parser_late_parsing_default_args (parser
, decl
);
21011 /* Remove any template parameters from the symbol table. */
21012 maybe_end_member_template_processing ();
21014 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
21015 /* Now parse any NSDMIs. */
21016 save_ccp
= current_class_ptr
;
21017 save_ccr
= current_class_ref
;
21018 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
21020 if (class_type
!= DECL_CONTEXT (decl
))
21023 pop_scope (pushed_scope
);
21024 class_type
= DECL_CONTEXT (decl
);
21025 pushed_scope
= push_scope (class_type
);
21027 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
21028 cp_parser_late_parsing_nsdmi (parser
, decl
);
21030 vec_safe_truncate (unparsed_nsdmis
, 0);
21031 current_class_ptr
= save_ccp
;
21032 current_class_ref
= save_ccr
;
21034 pop_scope (pushed_scope
);
21036 /* Now do some post-NSDMI bookkeeping. */
21037 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
21038 after_nsdmi_defaulted_late_checks (class_type
);
21039 vec_safe_truncate (unparsed_classes
, 0);
21040 after_nsdmi_defaulted_late_checks (type
);
21042 /* Now parse the body of the functions. */
21045 /* OpenMP UDRs need to be parsed before all other functions. */
21046 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21047 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
21048 cp_parser_late_parsing_for_member (parser
, decl
);
21049 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21050 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
21051 cp_parser_late_parsing_for_member (parser
, decl
);
21054 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
21055 cp_parser_late_parsing_for_member (parser
, decl
);
21056 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
21059 vec_safe_push (unparsed_classes
, type
);
21061 /* Put back any saved access checks. */
21062 pop_deferring_access_checks ();
21064 /* Restore saved state. */
21065 parser
->in_switch_statement_p
= in_switch_statement_p
;
21066 parser
->in_statement
= in_statement
;
21067 parser
->in_function_body
= saved_in_function_body
;
21068 parser
->num_template_parameter_lists
21069 = saved_num_template_parameter_lists
;
21070 parser
->in_unbraced_linkage_specification_p
21071 = saved_in_unbraced_linkage_specification_p
;
21077 cp_parser_class_specifier (cp_parser
* parser
)
21080 timevar_push (TV_PARSE_STRUCT
);
21081 ret
= cp_parser_class_specifier_1 (parser
);
21082 timevar_pop (TV_PARSE_STRUCT
);
21086 /* Parse a class-head.
21089 class-key identifier [opt] base-clause [opt]
21090 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21091 class-key nested-name-specifier [opt] template-id
21094 class-virt-specifier:
21098 class-key attributes identifier [opt] base-clause [opt]
21099 class-key attributes nested-name-specifier identifier base-clause [opt]
21100 class-key attributes nested-name-specifier [opt] template-id
21103 Upon return BASES is initialized to the list of base classes (or
21104 NULL, if there are none) in the same form returned by
21105 cp_parser_base_clause.
21107 Returns the TYPE of the indicated class. Sets
21108 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21109 involving a nested-name-specifier was used, and FALSE otherwise.
21111 Returns error_mark_node if this is not a class-head.
21113 Returns NULL_TREE if the class-head is syntactically valid, but
21114 semantically invalid in a way that means we should skip the entire
21115 body of the class. */
21118 cp_parser_class_head (cp_parser
* parser
,
21119 bool* nested_name_specifier_p
)
21121 tree nested_name_specifier
;
21122 enum tag_types class_key
;
21123 tree id
= NULL_TREE
;
21124 tree type
= NULL_TREE
;
21127 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21128 bool template_id_p
= false;
21129 bool qualified_p
= false;
21130 bool invalid_nested_name_p
= false;
21131 bool invalid_explicit_specialization_p
= false;
21132 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
21133 tree pushed_scope
= NULL_TREE
;
21134 unsigned num_templates
;
21135 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
21136 /* Assume no nested-name-specifier will be present. */
21137 *nested_name_specifier_p
= false;
21138 /* Assume no template parameter lists will be used in defining the
21141 parser
->colon_corrects_to_scope_p
= false;
21143 /* Look for the class-key. */
21144 class_key
= cp_parser_class_key (parser
);
21145 if (class_key
== none_type
)
21146 return error_mark_node
;
21148 /* Parse the attributes. */
21149 attributes
= cp_parser_attributes_opt (parser
);
21151 /* If the next token is `::', that is invalid -- but sometimes
21152 people do try to write:
21156 Handle this gracefully by accepting the extra qualifier, and then
21157 issuing an error about it later if this really is a
21158 class-head. If it turns out just to be an elaborated type
21159 specifier, remain silent. */
21160 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
21161 qualified_p
= true;
21163 push_deferring_access_checks (dk_no_check
);
21165 /* Determine the name of the class. Begin by looking for an
21166 optional nested-name-specifier. */
21167 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
21168 nested_name_specifier
21169 = cp_parser_nested_name_specifier_opt (parser
,
21170 /*typename_keyword_p=*/false,
21171 /*check_dependency_p=*/false,
21173 /*is_declaration=*/false);
21174 /* If there was a nested-name-specifier, then there *must* be an
21176 if (nested_name_specifier
)
21178 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21179 /* Although the grammar says `identifier', it really means
21180 `class-name' or `template-name'. You are only allowed to
21181 define a class that has already been declared with this
21184 The proposed resolution for Core Issue 180 says that wherever
21185 you see `class T::X' you should treat `X' as a type-name.
21187 It is OK to define an inaccessible class; for example:
21189 class A { class B; };
21192 We do not know if we will see a class-name, or a
21193 template-name. We look for a class-name first, in case the
21194 class-name is a template-id; if we looked for the
21195 template-name first we would stop after the template-name. */
21196 cp_parser_parse_tentatively (parser
);
21197 type
= cp_parser_class_name (parser
,
21198 /*typename_keyword_p=*/false,
21199 /*template_keyword_p=*/false,
21201 /*check_dependency_p=*/false,
21202 /*class_head_p=*/true,
21203 /*is_declaration=*/false);
21204 /* If that didn't work, ignore the nested-name-specifier. */
21205 if (!cp_parser_parse_definitely (parser
))
21207 invalid_nested_name_p
= true;
21208 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21209 id
= cp_parser_identifier (parser
);
21210 if (id
== error_mark_node
)
21213 /* If we could not find a corresponding TYPE, treat this
21214 declaration like an unqualified declaration. */
21215 if (type
== error_mark_node
)
21216 nested_name_specifier
= NULL_TREE
;
21217 /* Otherwise, count the number of templates used in TYPE and its
21218 containing scopes. */
21223 for (scope
= TREE_TYPE (type
);
21224 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
21225 scope
= get_containing_scope (scope
))
21227 && CLASS_TYPE_P (scope
)
21228 && CLASSTYPE_TEMPLATE_INFO (scope
)
21229 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
21230 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
21231 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
21235 /* Otherwise, the identifier is optional. */
21238 /* We don't know whether what comes next is a template-id,
21239 an identifier, or nothing at all. */
21240 cp_parser_parse_tentatively (parser
);
21241 /* Check for a template-id. */
21242 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21243 id
= cp_parser_template_id (parser
,
21244 /*template_keyword_p=*/false,
21245 /*check_dependency_p=*/true,
21247 /*is_declaration=*/true);
21248 /* If that didn't work, it could still be an identifier. */
21249 if (!cp_parser_parse_definitely (parser
))
21251 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
21253 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
21254 id
= cp_parser_identifier (parser
);
21261 template_id_p
= true;
21266 pop_deferring_access_checks ();
21270 cp_parser_check_for_invalid_template_id (parser
, id
,
21272 type_start_token
->location
);
21274 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21276 /* If it's not a `:' or a `{' then we can't really be looking at a
21277 class-head, since a class-head only appears as part of a
21278 class-specifier. We have to detect this situation before calling
21279 xref_tag, since that has irreversible side-effects. */
21280 if (!cp_parser_next_token_starts_class_definition_p (parser
))
21282 cp_parser_error (parser
, "expected %<{%> or %<:%>");
21283 type
= error_mark_node
;
21287 /* At this point, we're going ahead with the class-specifier, even
21288 if some other problem occurs. */
21289 cp_parser_commit_to_tentative_parse (parser
);
21290 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
21292 cp_parser_error (parser
,
21293 "cannot specify %<override%> for a class");
21294 type
= error_mark_node
;
21297 /* Issue the error about the overly-qualified name now. */
21300 cp_parser_error (parser
,
21301 "global qualification of class name is invalid");
21302 type
= error_mark_node
;
21305 else if (invalid_nested_name_p
)
21307 cp_parser_error (parser
,
21308 "qualified name does not name a class");
21309 type
= error_mark_node
;
21312 else if (nested_name_specifier
)
21316 /* Reject typedef-names in class heads. */
21317 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
21319 error_at (type_start_token
->location
,
21320 "invalid class name in declaration of %qD",
21326 /* Figure out in what scope the declaration is being placed. */
21327 scope
= current_scope ();
21328 /* If that scope does not contain the scope in which the
21329 class was originally declared, the program is invalid. */
21330 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
21332 if (at_namespace_scope_p ())
21333 error_at (type_start_token
->location
,
21334 "declaration of %qD in namespace %qD which does not "
21336 type
, scope
, nested_name_specifier
);
21338 error_at (type_start_token
->location
,
21339 "declaration of %qD in %qD which does not enclose %qD",
21340 type
, scope
, nested_name_specifier
);
21346 A declarator-id shall not be qualified except for the
21347 definition of a ... nested class outside of its class
21348 ... [or] the definition or explicit instantiation of a
21349 class member of a namespace outside of its namespace. */
21350 if (scope
== nested_name_specifier
)
21352 permerror (nested_name_specifier_token_start
->location
,
21353 "extra qualification not allowed");
21354 nested_name_specifier
= NULL_TREE
;
21358 /* An explicit-specialization must be preceded by "template <>". If
21359 it is not, try to recover gracefully. */
21360 if (at_namespace_scope_p ()
21361 && parser
->num_template_parameter_lists
== 0
21364 error_at (type_start_token
->location
,
21365 "an explicit specialization must be preceded by %<template <>%>");
21366 invalid_explicit_specialization_p
= true;
21367 /* Take the same action that would have been taken by
21368 cp_parser_explicit_specialization. */
21369 ++parser
->num_template_parameter_lists
;
21370 begin_specialization ();
21372 /* There must be no "return" statements between this point and the
21373 end of this function; set "type "to the correct return value and
21374 use "goto done;" to return. */
21375 /* Make sure that the right number of template parameters were
21377 if (!cp_parser_check_template_parameters (parser
, num_templates
,
21378 type_start_token
->location
,
21379 /*declarator=*/NULL
))
21381 /* If something went wrong, there is no point in even trying to
21382 process the class-definition. */
21387 /* Look up the type. */
21390 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
21391 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
21392 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
21394 error_at (type_start_token
->location
,
21395 "function template %qD redeclared as a class template", id
);
21396 type
= error_mark_node
;
21400 type
= TREE_TYPE (id
);
21401 type
= maybe_process_partial_specialization (type
);
21403 if (nested_name_specifier
)
21404 pushed_scope
= push_scope (nested_name_specifier
);
21406 else if (nested_name_specifier
)
21412 template <typename T> struct S { struct T };
21413 template <typename T> struct S<T>::T { };
21415 we will get a TYPENAME_TYPE when processing the definition of
21416 `S::T'. We need to resolve it to the actual type before we
21417 try to define it. */
21418 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
21420 class_type
= resolve_typename_type (TREE_TYPE (type
),
21421 /*only_current_p=*/false);
21422 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
21423 type
= TYPE_NAME (class_type
);
21426 cp_parser_error (parser
, "could not resolve typename type");
21427 type
= error_mark_node
;
21431 if (maybe_process_partial_specialization (TREE_TYPE (type
))
21432 == error_mark_node
)
21438 class_type
= current_class_type
;
21439 /* Enter the scope indicated by the nested-name-specifier. */
21440 pushed_scope
= push_scope (nested_name_specifier
);
21441 /* Get the canonical version of this type. */
21442 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
21443 /* Call push_template_decl if it seems like we should be defining a
21444 template either from the template headers or the type we're
21445 defining, so that we diagnose both extra and missing headers. */
21446 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21447 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
21448 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
21450 type
= push_template_decl (type
);
21451 if (type
== error_mark_node
)
21458 type
= TREE_TYPE (type
);
21459 *nested_name_specifier_p
= true;
21461 else /* The name is not a nested name. */
21463 /* If the class was unnamed, create a dummy name. */
21465 id
= make_anon_name ();
21466 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
21467 parser
->num_template_parameter_lists
);
21470 /* Indicate whether this class was declared as a `class' or as a
21472 if (TREE_CODE (type
) == RECORD_TYPE
)
21473 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
21474 cp_parser_check_class_key (class_key
, type
);
21476 /* If this type was already complete, and we see another definition,
21477 that's an error. */
21478 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
21480 error_at (type_start_token
->location
, "redefinition of %q#T",
21482 error_at (type_start_token
->location
, "previous definition of %q+#T",
21487 else if (type
== error_mark_node
)
21492 /* Apply attributes now, before any use of the class as a template
21493 argument in its base list. */
21494 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
21495 fixup_attribute_variants (type
);
21498 /* We will have entered the scope containing the class; the names of
21499 base classes should be looked up in that context. For example:
21501 struct A { struct B {}; struct C; };
21502 struct A::C : B {};
21506 /* Get the list of base-classes, if there is one. */
21507 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21509 /* PR59482: enter the class scope so that base-specifiers are looked
21513 bases
= cp_parser_base_clause (parser
);
21514 /* PR59482: get out of the previously pushed class scope so that the
21515 subsequent pops pop the right thing. */
21522 /* If we're really defining a class, process the base classes.
21523 If they're invalid, fail. */
21524 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
21525 && !xref_basetypes (type
, bases
))
21529 /* Leave the scope given by the nested-name-specifier. We will
21530 enter the class scope itself while processing the members. */
21532 pop_scope (pushed_scope
);
21534 if (invalid_explicit_specialization_p
)
21536 end_specialization ();
21537 --parser
->num_template_parameter_lists
;
21541 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
21542 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
21543 CLASSTYPE_FINAL (type
) = 1;
21545 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21549 /* Parse a class-key.
21556 Returns the kind of class-key specified, or none_type to indicate
21559 static enum tag_types
21560 cp_parser_class_key (cp_parser
* parser
)
21563 enum tag_types tag_type
;
21565 /* Look for the class-key. */
21566 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
21570 /* Check to see if the TOKEN is a class-key. */
21571 tag_type
= cp_parser_token_is_class_key (token
);
21573 cp_parser_error (parser
, "expected class-key");
21577 /* Parse a type-parameter-key.
21579 type-parameter-key:
21585 cp_parser_type_parameter_key (cp_parser
* parser
)
21587 /* Look for the type-parameter-key. */
21588 enum tag_types tag_type
= none_type
;
21589 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21590 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
21592 cp_lexer_consume_token (parser
->lexer
);
21593 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
21594 /* typename is not allowed in a template template parameter
21595 by the standard until C++1Z. */
21596 pedwarn (token
->location
, OPT_Wpedantic
,
21597 "ISO C++ forbids typename key in template template parameter;"
21598 " use -std=c++1z or -std=gnu++1z");
21601 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
21606 /* Parse an (optional) member-specification.
21608 member-specification:
21609 member-declaration member-specification [opt]
21610 access-specifier : member-specification [opt] */
21613 cp_parser_member_specification_opt (cp_parser
* parser
)
21620 /* Peek at the next token. */
21621 token
= cp_lexer_peek_token (parser
->lexer
);
21622 /* If it's a `}', or EOF then we've seen all the members. */
21623 if (token
->type
== CPP_CLOSE_BRACE
21624 || token
->type
== CPP_EOF
21625 || token
->type
== CPP_PRAGMA_EOL
)
21628 /* See if this token is a keyword. */
21629 keyword
= token
->keyword
;
21633 case RID_PROTECTED
:
21635 /* Consume the access-specifier. */
21636 cp_lexer_consume_token (parser
->lexer
);
21637 /* Remember which access-specifier is active. */
21638 current_access_specifier
= token
->u
.value
;
21639 /* Look for the `:'. */
21640 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21644 /* Accept #pragmas at class scope. */
21645 if (token
->type
== CPP_PRAGMA
)
21647 cp_parser_pragma (parser
, pragma_member
);
21651 /* Otherwise, the next construction must be a
21652 member-declaration. */
21653 cp_parser_member_declaration (parser
);
21658 /* Parse a member-declaration.
21660 member-declaration:
21661 decl-specifier-seq [opt] member-declarator-list [opt] ;
21662 function-definition ; [opt]
21663 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21665 template-declaration
21668 member-declarator-list:
21670 member-declarator-list , member-declarator
21673 declarator pure-specifier [opt]
21674 declarator constant-initializer [opt]
21675 identifier [opt] : constant-expression
21679 member-declaration:
21680 __extension__ member-declaration
21683 declarator attributes [opt] pure-specifier [opt]
21684 declarator attributes [opt] constant-initializer [opt]
21685 identifier [opt] attributes [opt] : constant-expression
21689 member-declaration:
21690 static_assert-declaration */
21693 cp_parser_member_declaration (cp_parser
* parser
)
21695 cp_decl_specifier_seq decl_specifiers
;
21696 tree prefix_attributes
;
21698 int declares_class_or_enum
;
21700 cp_token
*token
= NULL
;
21701 cp_token
*decl_spec_token_start
= NULL
;
21702 cp_token
*initializer_token_start
= NULL
;
21703 int saved_pedantic
;
21704 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
21706 /* Check for the `__extension__' keyword. */
21707 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
21710 cp_parser_member_declaration (parser
);
21711 /* Restore the old value of the PEDANTIC flag. */
21712 pedantic
= saved_pedantic
;
21717 /* Check for a template-declaration. */
21718 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
21720 /* An explicit specialization here is an error condition, and we
21721 expect the specialization handler to detect and report this. */
21722 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
21723 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
21724 cp_parser_explicit_specialization (parser
);
21726 cp_parser_template_declaration (parser
, /*member_p=*/true);
21730 /* Check for a template introduction. */
21731 else if (cp_parser_template_declaration_after_export (parser
, true))
21734 /* Check for a using-declaration. */
21735 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
21737 if (cxx_dialect
< cxx11
)
21739 /* Parse the using-declaration. */
21740 cp_parser_using_declaration (parser
,
21741 /*access_declaration_p=*/false);
21747 bool alias_decl_expected
;
21748 cp_parser_parse_tentatively (parser
);
21749 decl
= cp_parser_alias_declaration (parser
);
21750 /* Note that if we actually see the '=' token after the
21751 identifier, cp_parser_alias_declaration commits the
21752 tentative parse. In that case, we really expect an
21753 alias-declaration. Otherwise, we expect a using
21755 alias_decl_expected
=
21756 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
21757 cp_parser_parse_definitely (parser
);
21759 if (alias_decl_expected
)
21760 finish_member_declaration (decl
);
21762 cp_parser_using_declaration (parser
,
21763 /*access_declaration_p=*/false);
21768 /* Check for @defs. */
21769 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
21772 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
21773 ivar
= ivar_chains
;
21777 ivar
= TREE_CHAIN (member
);
21778 TREE_CHAIN (member
) = NULL_TREE
;
21779 finish_member_declaration (member
);
21784 /* If the next token is `static_assert' we have a static assertion. */
21785 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
21787 cp_parser_static_assert (parser
, /*member_p=*/true);
21791 parser
->colon_corrects_to_scope_p
= false;
21793 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
21796 /* Parse the decl-specifier-seq. */
21797 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
21798 cp_parser_decl_specifier_seq (parser
,
21799 CP_PARSER_FLAGS_OPTIONAL
,
21801 &declares_class_or_enum
);
21802 /* Check for an invalid type-name. */
21803 if (!decl_specifiers
.any_type_specifiers_p
21804 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
21806 /* If there is no declarator, then the decl-specifier-seq should
21808 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21810 /* If there was no decl-specifier-seq, and the next token is a
21811 `;', then we have something like:
21817 Each member-declaration shall declare at least one member
21818 name of the class. */
21819 if (!decl_specifiers
.any_specifiers_p
)
21821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21822 if (!in_system_header_at (token
->location
))
21823 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
21829 /* See if this declaration is a friend. */
21830 friend_p
= cp_parser_friend_p (&decl_specifiers
);
21831 /* If there were decl-specifiers, check to see if there was
21832 a class-declaration. */
21833 type
= check_tag_decl (&decl_specifiers
,
21834 /*explicit_type_instantiation_p=*/false);
21835 /* Nested classes have already been added to the class, but
21836 a `friend' needs to be explicitly registered. */
21839 /* If the `friend' keyword was present, the friend must
21840 be introduced with a class-key. */
21841 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
21842 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
21843 "in C++03 a class-key must be used "
21844 "when declaring a friend");
21847 template <typename T> struct A {
21848 friend struct A<T>::B;
21851 A<T>::B will be represented by a TYPENAME_TYPE, and
21852 therefore not recognized by check_tag_decl. */
21855 type
= decl_specifiers
.type
;
21856 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
21857 type
= TREE_TYPE (type
);
21859 if (!type
|| !TYPE_P (type
))
21860 error_at (decl_spec_token_start
->location
,
21861 "friend declaration does not name a class or "
21864 make_friend_class (current_class_type
, type
,
21865 /*complain=*/true);
21867 /* If there is no TYPE, an error message will already have
21869 else if (!type
|| type
== error_mark_node
)
21871 /* An anonymous aggregate has to be handled specially; such
21872 a declaration really declares a data member (with a
21873 particular type), as opposed to a nested class. */
21874 else if (ANON_AGGR_TYPE_P (type
))
21877 if (decl_specifiers
.storage_class
!= sc_none
)
21878 error_at (decl_spec_token_start
->location
,
21879 "a storage class on an anonymous aggregate "
21880 "in class scope is not allowed");
21882 /* Remove constructors and such from TYPE, now that we
21883 know it is an anonymous aggregate. */
21884 fixup_anonymous_aggr (type
);
21885 /* And make the corresponding data member. */
21886 decl
= build_decl (decl_spec_token_start
->location
,
21887 FIELD_DECL
, NULL_TREE
, type
);
21888 /* Add it to the class. */
21889 finish_member_declaration (decl
);
21892 cp_parser_check_access_in_redeclaration
21894 decl_spec_token_start
->location
);
21899 bool assume_semicolon
= false;
21901 /* Clear attributes from the decl_specifiers but keep them
21902 around as prefix attributes that apply them to the entity
21904 prefix_attributes
= decl_specifiers
.attributes
;
21905 decl_specifiers
.attributes
= NULL_TREE
;
21907 /* See if these declarations will be friends. */
21908 friend_p
= cp_parser_friend_p (&decl_specifiers
);
21910 /* Keep going until we hit the `;' at the end of the
21912 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
21914 tree attributes
= NULL_TREE
;
21915 tree first_attribute
;
21917 /* Peek at the next token. */
21918 token
= cp_lexer_peek_token (parser
->lexer
);
21920 /* Check for a bitfield declaration. */
21921 if (token
->type
== CPP_COLON
21922 || (token
->type
== CPP_NAME
21923 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
21929 /* Get the name of the bitfield. Note that we cannot just
21930 check TOKEN here because it may have been invalidated by
21931 the call to cp_lexer_peek_nth_token above. */
21932 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
21933 identifier
= cp_parser_identifier (parser
);
21935 identifier
= NULL_TREE
;
21937 /* Consume the `:' token. */
21938 cp_lexer_consume_token (parser
->lexer
);
21939 /* Get the width of the bitfield. */
21941 = cp_parser_constant_expression (parser
);
21943 /* Look for attributes that apply to the bitfield. */
21944 attributes
= cp_parser_attributes_opt (parser
);
21945 /* Remember which attributes are prefix attributes and
21947 first_attribute
= attributes
;
21948 /* Combine the attributes. */
21949 attributes
= chainon (prefix_attributes
, attributes
);
21951 /* Create the bitfield declaration. */
21952 decl
= grokbitfield (identifier
21953 ? make_id_declarator (NULL_TREE
,
21963 cp_declarator
*declarator
;
21965 tree asm_specification
;
21966 int ctor_dtor_or_conv_p
;
21968 /* Parse the declarator. */
21970 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21971 &ctor_dtor_or_conv_p
,
21972 /*parenthesized_p=*/NULL
,
21976 /* If something went wrong parsing the declarator, make sure
21977 that we at least consume some tokens. */
21978 if (declarator
== cp_error_declarator
)
21980 /* Skip to the end of the statement. */
21981 cp_parser_skip_to_end_of_statement (parser
);
21982 /* If the next token is not a semicolon, that is
21983 probably because we just skipped over the body of
21984 a function. So, we consume a semicolon if
21985 present, but do not issue an error message if it
21987 if (cp_lexer_next_token_is (parser
->lexer
,
21989 cp_lexer_consume_token (parser
->lexer
);
21993 if (declares_class_or_enum
& 2)
21994 cp_parser_check_for_definition_in_return_type
21995 (declarator
, decl_specifiers
.type
,
21996 decl_specifiers
.locations
[ds_type_spec
]);
21998 /* Look for an asm-specification. */
21999 asm_specification
= cp_parser_asm_specification_opt (parser
);
22000 /* Look for attributes that apply to the declaration. */
22001 attributes
= cp_parser_attributes_opt (parser
);
22002 /* Remember which attributes are prefix attributes and
22004 first_attribute
= attributes
;
22005 /* Combine the attributes. */
22006 attributes
= chainon (prefix_attributes
, attributes
);
22008 /* If it's an `=', then we have a constant-initializer or a
22009 pure-specifier. It is not correct to parse the
22010 initializer before registering the member declaration
22011 since the member declaration should be in scope while
22012 its initializer is processed. However, the rest of the
22013 front end does not yet provide an interface that allows
22014 us to handle this correctly. */
22015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22019 A pure-specifier shall be used only in the declaration of
22020 a virtual function.
22022 A member-declarator can contain a constant-initializer
22023 only if it declares a static member of integral or
22026 Therefore, if the DECLARATOR is for a function, we look
22027 for a pure-specifier; otherwise, we look for a
22028 constant-initializer. When we call `grokfield', it will
22029 perform more stringent semantics checks. */
22030 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
22031 if (function_declarator_p (declarator
)
22032 || (decl_specifiers
.type
22033 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
22034 && declarator
->kind
== cdk_id
22035 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
22036 == FUNCTION_TYPE
)))
22037 initializer
= cp_parser_pure_specifier (parser
);
22038 else if (decl_specifiers
.storage_class
!= sc_static
)
22039 initializer
= cp_parser_save_nsdmi (parser
);
22040 else if (cxx_dialect
>= cxx11
)
22043 /* Don't require a constant rvalue in C++11, since we
22044 might want a reference constant. We'll enforce
22045 constancy later. */
22046 cp_lexer_consume_token (parser
->lexer
);
22047 /* Parse the initializer. */
22048 initializer
= cp_parser_initializer_clause (parser
,
22052 /* Parse the initializer. */
22053 initializer
= cp_parser_constant_initializer (parser
);
22055 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
22056 && !function_declarator_p (declarator
))
22059 if (decl_specifiers
.storage_class
!= sc_static
)
22060 initializer
= cp_parser_save_nsdmi (parser
);
22062 initializer
= cp_parser_initializer (parser
, &x
, &x
);
22064 /* Otherwise, there is no initializer. */
22066 initializer
= NULL_TREE
;
22068 /* See if we are probably looking at a function
22069 definition. We are certainly not looking at a
22070 member-declarator. Calling `grokfield' has
22071 side-effects, so we must not do it unless we are sure
22072 that we are looking at a member-declarator. */
22073 if (cp_parser_token_starts_function_definition_p
22074 (cp_lexer_peek_token (parser
->lexer
)))
22076 /* The grammar does not allow a pure-specifier to be
22077 used when a member function is defined. (It is
22078 possible that this fact is an oversight in the
22079 standard, since a pure function may be defined
22080 outside of the class-specifier. */
22081 if (initializer
&& initializer_token_start
)
22082 error_at (initializer_token_start
->location
,
22083 "pure-specifier on function-definition");
22084 decl
= cp_parser_save_member_function_body (parser
,
22088 if (parser
->fully_implicit_function_template_p
)
22089 decl
= finish_fully_implicit_template (parser
, decl
);
22090 /* If the member was not a friend, declare it here. */
22092 finish_member_declaration (decl
);
22093 /* Peek at the next token. */
22094 token
= cp_lexer_peek_token (parser
->lexer
);
22095 /* If the next token is a semicolon, consume it. */
22096 if (token
->type
== CPP_SEMICOLON
)
22097 cp_lexer_consume_token (parser
->lexer
);
22101 if (declarator
->kind
== cdk_function
)
22102 declarator
->id_loc
= token
->location
;
22103 /* Create the declaration. */
22104 decl
= grokfield (declarator
, &decl_specifiers
,
22105 initializer
, /*init_const_expr_p=*/true,
22106 asm_specification
, attributes
);
22107 if (parser
->fully_implicit_function_template_p
)
22110 finish_fully_implicit_template (parser
, 0);
22112 decl
= finish_fully_implicit_template (parser
, decl
);
22116 cp_finalize_omp_declare_simd (parser
, decl
);
22117 cp_finalize_oacc_routine (parser
, decl
, false);
22119 /* Reset PREFIX_ATTRIBUTES. */
22120 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
22121 attributes
= TREE_CHAIN (attributes
);
22123 TREE_CHAIN (attributes
) = NULL_TREE
;
22125 /* If there is any qualification still in effect, clear it
22126 now; we will be starting fresh with the next declarator. */
22127 parser
->scope
= NULL_TREE
;
22128 parser
->qualifying_scope
= NULL_TREE
;
22129 parser
->object_scope
= NULL_TREE
;
22130 /* If it's a `,', then there are more declarators. */
22131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22133 cp_lexer_consume_token (parser
->lexer
);
22134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22136 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22137 error_at (token
->location
,
22138 "stray %<,%> at end of member declaration");
22141 /* If the next token isn't a `;', then we have a parse error. */
22142 else if (cp_lexer_next_token_is_not (parser
->lexer
,
22145 /* The next token might be a ways away from where the
22146 actual semicolon is missing. Find the previous token
22147 and use that for our error position. */
22148 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
22149 error_at (token
->location
,
22150 "expected %<;%> at end of member declaration");
22152 /* Assume that the user meant to provide a semicolon. If
22153 we were to cp_parser_skip_to_end_of_statement, we might
22154 skip to a semicolon inside a member function definition
22155 and issue nonsensical error messages. */
22156 assume_semicolon
= true;
22161 /* Add DECL to the list of members. */
22163 /* Explicitly include, eg, NSDMIs, for better error
22164 recovery (c++/58650). */
22165 || !DECL_DECLARES_FUNCTION_P (decl
))
22166 finish_member_declaration (decl
);
22168 if (TREE_CODE (decl
) == FUNCTION_DECL
)
22169 cp_parser_save_default_args (parser
, decl
);
22170 else if (TREE_CODE (decl
) == FIELD_DECL
22171 && !DECL_C_BIT_FIELD (decl
)
22172 && DECL_INITIAL (decl
))
22173 /* Add DECL to the queue of NSDMI to be parsed later. */
22174 vec_safe_push (unparsed_nsdmis
, decl
);
22177 if (assume_semicolon
)
22182 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22184 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
22187 /* Parse a pure-specifier.
22192 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22193 Otherwise, ERROR_MARK_NODE is returned. */
22196 cp_parser_pure_specifier (cp_parser
* parser
)
22200 /* Look for the `=' token. */
22201 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22202 return error_mark_node
;
22203 /* Look for the `0' token. */
22204 token
= cp_lexer_peek_token (parser
->lexer
);
22206 if (token
->type
== CPP_EOF
22207 || token
->type
== CPP_PRAGMA_EOL
)
22208 return error_mark_node
;
22210 cp_lexer_consume_token (parser
->lexer
);
22212 /* Accept = default or = delete in c++0x mode. */
22213 if (token
->keyword
== RID_DEFAULT
22214 || token
->keyword
== RID_DELETE
)
22216 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
22217 return token
->u
.value
;
22220 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22221 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
22223 cp_parser_error (parser
,
22224 "invalid pure specifier (only %<= 0%> is allowed)");
22225 cp_parser_skip_to_end_of_statement (parser
);
22226 return error_mark_node
;
22228 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22230 error_at (token
->location
, "templates may not be %<virtual%>");
22231 return error_mark_node
;
22234 return integer_zero_node
;
22237 /* Parse a constant-initializer.
22239 constant-initializer:
22240 = constant-expression
22242 Returns a representation of the constant-expression. */
22245 cp_parser_constant_initializer (cp_parser
* parser
)
22247 /* Look for the `=' token. */
22248 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
22249 return error_mark_node
;
22251 /* It is invalid to write:
22253 struct S { static const int i = { 7 }; };
22256 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22258 cp_parser_error (parser
,
22259 "a brace-enclosed initializer is not allowed here");
22260 /* Consume the opening brace. */
22261 cp_lexer_consume_token (parser
->lexer
);
22262 /* Skip the initializer. */
22263 cp_parser_skip_to_closing_brace (parser
);
22264 /* Look for the trailing `}'. */
22265 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
22267 return error_mark_node
;
22270 return cp_parser_constant_expression (parser
);
22273 /* Derived classes [gram.class.derived] */
22275 /* Parse a base-clause.
22278 : base-specifier-list
22280 base-specifier-list:
22281 base-specifier ... [opt]
22282 base-specifier-list , base-specifier ... [opt]
22284 Returns a TREE_LIST representing the base-classes, in the order in
22285 which they were declared. The representation of each node is as
22286 described by cp_parser_base_specifier.
22288 In the case that no bases are specified, this function will return
22289 NULL_TREE, not ERROR_MARK_NODE. */
22292 cp_parser_base_clause (cp_parser
* parser
)
22294 tree bases
= NULL_TREE
;
22296 /* Look for the `:' that begins the list. */
22297 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
22299 /* Scan the base-specifier-list. */
22304 bool pack_expansion_p
= false;
22306 /* Look for the base-specifier. */
22307 base
= cp_parser_base_specifier (parser
);
22308 /* Look for the (optional) ellipsis. */
22309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22311 /* Consume the `...'. */
22312 cp_lexer_consume_token (parser
->lexer
);
22314 pack_expansion_p
= true;
22317 /* Add BASE to the front of the list. */
22318 if (base
&& base
!= error_mark_node
)
22320 if (pack_expansion_p
)
22321 /* Make this a pack expansion type. */
22322 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
22324 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
22326 TREE_CHAIN (base
) = bases
;
22330 /* Peek at the next token. */
22331 token
= cp_lexer_peek_token (parser
->lexer
);
22332 /* If it's not a comma, then the list is complete. */
22333 if (token
->type
!= CPP_COMMA
)
22335 /* Consume the `,'. */
22336 cp_lexer_consume_token (parser
->lexer
);
22339 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22340 base class had a qualified name. However, the next name that
22341 appears is certainly not qualified. */
22342 parser
->scope
= NULL_TREE
;
22343 parser
->qualifying_scope
= NULL_TREE
;
22344 parser
->object_scope
= NULL_TREE
;
22346 return nreverse (bases
);
22349 /* Parse a base-specifier.
22352 :: [opt] nested-name-specifier [opt] class-name
22353 virtual access-specifier [opt] :: [opt] nested-name-specifier
22355 access-specifier virtual [opt] :: [opt] nested-name-specifier
22358 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22359 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22360 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22361 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22364 cp_parser_base_specifier (cp_parser
* parser
)
22368 bool virtual_p
= false;
22369 bool duplicate_virtual_error_issued_p
= false;
22370 bool duplicate_access_error_issued_p
= false;
22371 bool class_scope_p
, template_p
;
22372 tree access
= access_default_node
;
22375 /* Process the optional `virtual' and `access-specifier'. */
22378 /* Peek at the next token. */
22379 token
= cp_lexer_peek_token (parser
->lexer
);
22380 /* Process `virtual'. */
22381 switch (token
->keyword
)
22384 /* If `virtual' appears more than once, issue an error. */
22385 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
22387 cp_parser_error (parser
,
22388 "%<virtual%> specified more than once in base-specified");
22389 duplicate_virtual_error_issued_p
= true;
22394 /* Consume the `virtual' token. */
22395 cp_lexer_consume_token (parser
->lexer
);
22400 case RID_PROTECTED
:
22402 /* If more than one access specifier appears, issue an
22404 if (access
!= access_default_node
22405 && !duplicate_access_error_issued_p
)
22407 cp_parser_error (parser
,
22408 "more than one access specifier in base-specified");
22409 duplicate_access_error_issued_p
= true;
22412 access
= ridpointers
[(int) token
->keyword
];
22414 /* Consume the access-specifier. */
22415 cp_lexer_consume_token (parser
->lexer
);
22424 /* It is not uncommon to see programs mechanically, erroneously, use
22425 the 'typename' keyword to denote (dependent) qualified types
22426 as base classes. */
22427 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
22429 token
= cp_lexer_peek_token (parser
->lexer
);
22430 if (!processing_template_decl
)
22431 error_at (token
->location
,
22432 "keyword %<typename%> not allowed outside of templates");
22434 error_at (token
->location
,
22435 "keyword %<typename%> not allowed in this context "
22436 "(the base class is implicitly a type)");
22437 cp_lexer_consume_token (parser
->lexer
);
22440 /* Look for the optional `::' operator. */
22441 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
22442 /* Look for the nested-name-specifier. The simplest way to
22447 The keyword `typename' is not permitted in a base-specifier or
22448 mem-initializer; in these contexts a qualified name that
22449 depends on a template-parameter is implicitly assumed to be a
22452 is to pretend that we have seen the `typename' keyword at this
22454 cp_parser_nested_name_specifier_opt (parser
,
22455 /*typename_keyword_p=*/true,
22456 /*check_dependency_p=*/true,
22458 /*is_declaration=*/true);
22459 /* If the base class is given by a qualified name, assume that names
22460 we see are type names or templates, as appropriate. */
22461 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
22462 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
22465 && cp_lexer_next_token_is_decltype (parser
->lexer
))
22466 /* DR 950 allows decltype as a base-specifier. */
22467 type
= cp_parser_decltype (parser
);
22470 /* Otherwise, look for the class-name. */
22471 type
= cp_parser_class_name (parser
,
22475 /*check_dependency_p=*/true,
22476 /*class_head_p=*/false,
22477 /*is_declaration=*/true);
22478 type
= TREE_TYPE (type
);
22481 if (type
== error_mark_node
)
22482 return error_mark_node
;
22484 return finish_base_specifier (type
, access
, virtual_p
);
22487 /* Exception handling [gram.exception] */
22489 /* Parse an (optional) noexcept-specification.
22491 noexcept-specification:
22492 noexcept ( constant-expression ) [opt]
22494 If no noexcept-specification is present, returns NULL_TREE.
22495 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22496 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22497 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22498 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22499 in which case a boolean condition is returned instead. */
22502 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
22503 bool require_constexpr
,
22504 bool* consumed_expr
,
22508 const char *saved_message
;
22510 /* Peek at the next token. */
22511 token
= cp_lexer_peek_token (parser
->lexer
);
22513 /* Is it a noexcept-specification? */
22514 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
22517 cp_lexer_consume_token (parser
->lexer
);
22519 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
22521 cp_lexer_consume_token (parser
->lexer
);
22523 if (require_constexpr
)
22525 /* Types may not be defined in an exception-specification. */
22526 saved_message
= parser
->type_definition_forbidden_message
;
22527 parser
->type_definition_forbidden_message
22528 = G_("types may not be defined in an exception-specification");
22530 expr
= cp_parser_constant_expression (parser
);
22532 /* Restore the saved message. */
22533 parser
->type_definition_forbidden_message
= saved_message
;
22537 expr
= cp_parser_expression (parser
);
22538 *consumed_expr
= true;
22541 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22545 expr
= boolean_true_node
;
22546 if (!require_constexpr
)
22547 *consumed_expr
= false;
22550 /* We cannot build a noexcept-spec right away because this will check
22551 that expr is a constexpr. */
22553 return build_noexcept_spec (expr
, tf_warning_or_error
);
22561 /* Parse an (optional) exception-specification.
22563 exception-specification:
22564 throw ( type-id-list [opt] )
22566 Returns a TREE_LIST representing the exception-specification. The
22567 TREE_VALUE of each node is a type. */
22570 cp_parser_exception_specification_opt (cp_parser
* parser
)
22574 const char *saved_message
;
22576 /* Peek at the next token. */
22577 token
= cp_lexer_peek_token (parser
->lexer
);
22579 /* Is it a noexcept-specification? */
22580 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
22582 if (type_id_list
!= NULL_TREE
)
22583 return type_id_list
;
22585 /* If it's not `throw', then there's no exception-specification. */
22586 if (!cp_parser_is_keyword (token
, RID_THROW
))
22590 /* Enable this once a lot of code has transitioned to noexcept? */
22591 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
22592 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
22593 "deprecated in C++0x; use %<noexcept%> instead");
22596 /* Consume the `throw'. */
22597 cp_lexer_consume_token (parser
->lexer
);
22599 /* Look for the `('. */
22600 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22602 /* Peek at the next token. */
22603 token
= cp_lexer_peek_token (parser
->lexer
);
22604 /* If it's not a `)', then there is a type-id-list. */
22605 if (token
->type
!= CPP_CLOSE_PAREN
)
22607 /* Types may not be defined in an exception-specification. */
22608 saved_message
= parser
->type_definition_forbidden_message
;
22609 parser
->type_definition_forbidden_message
22610 = G_("types may not be defined in an exception-specification");
22611 /* Parse the type-id-list. */
22612 type_id_list
= cp_parser_type_id_list (parser
);
22613 /* Restore the saved message. */
22614 parser
->type_definition_forbidden_message
= saved_message
;
22617 type_id_list
= empty_except_spec
;
22619 /* Look for the `)'. */
22620 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22622 return type_id_list
;
22625 /* Parse an (optional) type-id-list.
22629 type-id-list , type-id ... [opt]
22631 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22632 in the order that the types were presented. */
22635 cp_parser_type_id_list (cp_parser
* parser
)
22637 tree types
= NULL_TREE
;
22644 /* Get the next type-id. */
22645 type
= cp_parser_type_id (parser
);
22646 /* Parse the optional ellipsis. */
22647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22649 /* Consume the `...'. */
22650 cp_lexer_consume_token (parser
->lexer
);
22652 /* Turn the type into a pack expansion expression. */
22653 type
= make_pack_expansion (type
);
22655 /* Add it to the list. */
22656 types
= add_exception_specifier (types
, type
, /*complain=*/1);
22657 /* Peek at the next token. */
22658 token
= cp_lexer_peek_token (parser
->lexer
);
22659 /* If it is not a `,', we are done. */
22660 if (token
->type
!= CPP_COMMA
)
22662 /* Consume the `,'. */
22663 cp_lexer_consume_token (parser
->lexer
);
22666 return nreverse (types
);
22669 /* Parse a try-block.
22672 try compound-statement handler-seq */
22675 cp_parser_try_block (cp_parser
* parser
)
22679 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
22680 if (parser
->in_function_body
22681 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
22682 error ("%<try%> in %<constexpr%> function");
22684 try_block
= begin_try_block ();
22685 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
22686 finish_try_block (try_block
);
22687 cp_parser_handler_seq (parser
);
22688 finish_handler_sequence (try_block
);
22693 /* Parse a function-try-block.
22695 function-try-block:
22696 try ctor-initializer [opt] function-body handler-seq */
22699 cp_parser_function_try_block (cp_parser
* parser
)
22701 tree compound_stmt
;
22703 bool ctor_initializer_p
;
22705 /* Look for the `try' keyword. */
22706 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
22708 /* Let the rest of the front end know where we are. */
22709 try_block
= begin_function_try_block (&compound_stmt
);
22710 /* Parse the function-body. */
22711 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
22712 (parser
, /*in_function_try_block=*/true);
22713 /* We're done with the `try' part. */
22714 finish_function_try_block (try_block
);
22715 /* Parse the handlers. */
22716 cp_parser_handler_seq (parser
);
22717 /* We're done with the handlers. */
22718 finish_function_handler_sequence (try_block
, compound_stmt
);
22720 return ctor_initializer_p
;
22723 /* Parse a handler-seq.
22726 handler handler-seq [opt] */
22729 cp_parser_handler_seq (cp_parser
* parser
)
22735 /* Parse the handler. */
22736 cp_parser_handler (parser
);
22737 /* Peek at the next token. */
22738 token
= cp_lexer_peek_token (parser
->lexer
);
22739 /* If it's not `catch' then there are no more handlers. */
22740 if (!cp_parser_is_keyword (token
, RID_CATCH
))
22745 /* Parse a handler.
22748 catch ( exception-declaration ) compound-statement */
22751 cp_parser_handler (cp_parser
* parser
)
22756 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
22757 handler
= begin_handler ();
22758 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22759 declaration
= cp_parser_exception_declaration (parser
);
22760 finish_handler_parms (declaration
, handler
);
22761 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22762 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
22763 finish_handler (handler
);
22766 /* Parse an exception-declaration.
22768 exception-declaration:
22769 type-specifier-seq declarator
22770 type-specifier-seq abstract-declarator
22774 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22775 ellipsis variant is used. */
22778 cp_parser_exception_declaration (cp_parser
* parser
)
22780 cp_decl_specifier_seq type_specifiers
;
22781 cp_declarator
*declarator
;
22782 const char *saved_message
;
22784 /* If it's an ellipsis, it's easy to handle. */
22785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22787 /* Consume the `...' token. */
22788 cp_lexer_consume_token (parser
->lexer
);
22792 /* Types may not be defined in exception-declarations. */
22793 saved_message
= parser
->type_definition_forbidden_message
;
22794 parser
->type_definition_forbidden_message
22795 = G_("types may not be defined in exception-declarations");
22797 /* Parse the type-specifier-seq. */
22798 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
22799 /*is_trailing_return=*/false,
22801 /* If it's a `)', then there is no declarator. */
22802 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
22805 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
22806 /*ctor_dtor_or_conv_p=*/NULL
,
22807 /*parenthesized_p=*/NULL
,
22808 /*member_p=*/false,
22809 /*friend_p=*/false);
22811 /* Restore the saved message. */
22812 parser
->type_definition_forbidden_message
= saved_message
;
22814 if (!type_specifiers
.any_specifiers_p
)
22815 return error_mark_node
;
22817 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
22820 /* Parse a throw-expression.
22823 throw assignment-expression [opt]
22825 Returns a THROW_EXPR representing the throw-expression. */
22828 cp_parser_throw_expression (cp_parser
* parser
)
22833 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
22834 token
= cp_lexer_peek_token (parser
->lexer
);
22835 /* Figure out whether or not there is an assignment-expression
22836 following the "throw" keyword. */
22837 if (token
->type
== CPP_COMMA
22838 || token
->type
== CPP_SEMICOLON
22839 || token
->type
== CPP_CLOSE_PAREN
22840 || token
->type
== CPP_CLOSE_SQUARE
22841 || token
->type
== CPP_CLOSE_BRACE
22842 || token
->type
== CPP_COLON
)
22843 expression
= NULL_TREE
;
22845 expression
= cp_parser_assignment_expression (parser
);
22847 return build_throw (expression
);
22850 /* GNU Extensions */
22852 /* Parse an (optional) asm-specification.
22855 asm ( string-literal )
22857 If the asm-specification is present, returns a STRING_CST
22858 corresponding to the string-literal. Otherwise, returns
22862 cp_parser_asm_specification_opt (cp_parser
* parser
)
22865 tree asm_specification
;
22867 /* Peek at the next token. */
22868 token
= cp_lexer_peek_token (parser
->lexer
);
22869 /* If the next token isn't the `asm' keyword, then there's no
22870 asm-specification. */
22871 if (!cp_parser_is_keyword (token
, RID_ASM
))
22874 /* Consume the `asm' token. */
22875 cp_lexer_consume_token (parser
->lexer
);
22876 /* Look for the `('. */
22877 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22879 /* Look for the string-literal. */
22880 asm_specification
= cp_parser_string_literal (parser
, false, false);
22882 /* Look for the `)'. */
22883 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22885 return asm_specification
;
22888 /* Parse an asm-operand-list.
22892 asm-operand-list , asm-operand
22895 string-literal ( expression )
22896 [ string-literal ] string-literal ( expression )
22898 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22899 each node is the expression. The TREE_PURPOSE is itself a
22900 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22901 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22902 is a STRING_CST for the string literal before the parenthesis. Returns
22903 ERROR_MARK_NODE if any of the operands are invalid. */
22906 cp_parser_asm_operand_list (cp_parser
* parser
)
22908 tree asm_operands
= NULL_TREE
;
22909 bool invalid_operands
= false;
22913 tree string_literal
;
22917 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
22919 /* Consume the `[' token. */
22920 cp_lexer_consume_token (parser
->lexer
);
22921 /* Read the operand name. */
22922 name
= cp_parser_identifier (parser
);
22923 if (name
!= error_mark_node
)
22924 name
= build_string (IDENTIFIER_LENGTH (name
),
22925 IDENTIFIER_POINTER (name
));
22926 /* Look for the closing `]'. */
22927 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
22931 /* Look for the string-literal. */
22932 string_literal
= cp_parser_string_literal (parser
, false, false);
22934 /* Look for the `('. */
22935 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22936 /* Parse the expression. */
22937 expression
= cp_parser_expression (parser
);
22938 /* Look for the `)'. */
22939 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
22941 if (name
== error_mark_node
22942 || string_literal
== error_mark_node
22943 || expression
== error_mark_node
)
22944 invalid_operands
= true;
22946 /* Add this operand to the list. */
22947 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
22950 /* If the next token is not a `,', there are no more
22952 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22954 /* Consume the `,'. */
22955 cp_lexer_consume_token (parser
->lexer
);
22958 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
22961 /* Parse an asm-clobber-list.
22965 asm-clobber-list , string-literal
22967 Returns a TREE_LIST, indicating the clobbers in the order that they
22968 appeared. The TREE_VALUE of each node is a STRING_CST. */
22971 cp_parser_asm_clobber_list (cp_parser
* parser
)
22973 tree clobbers
= NULL_TREE
;
22977 tree string_literal
;
22979 /* Look for the string literal. */
22980 string_literal
= cp_parser_string_literal (parser
, false, false);
22981 /* Add it to the list. */
22982 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
22983 /* If the next token is not a `,', then the list is
22985 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22987 /* Consume the `,' token. */
22988 cp_lexer_consume_token (parser
->lexer
);
22994 /* Parse an asm-label-list.
22998 asm-label-list , identifier
23000 Returns a TREE_LIST, indicating the labels in the order that they
23001 appeared. The TREE_VALUE of each node is a label. */
23004 cp_parser_asm_label_list (cp_parser
* parser
)
23006 tree labels
= NULL_TREE
;
23010 tree identifier
, label
, name
;
23012 /* Look for the identifier. */
23013 identifier
= cp_parser_identifier (parser
);
23014 if (!error_operand_p (identifier
))
23016 label
= lookup_label (identifier
);
23017 if (TREE_CODE (label
) == LABEL_DECL
)
23019 TREE_USED (label
) = 1;
23020 check_goto (label
);
23021 name
= build_string (IDENTIFIER_LENGTH (identifier
),
23022 IDENTIFIER_POINTER (identifier
));
23023 labels
= tree_cons (name
, label
, labels
);
23026 /* If the next token is not a `,', then the list is
23028 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23030 /* Consume the `,' token. */
23031 cp_lexer_consume_token (parser
->lexer
);
23034 return nreverse (labels
);
23037 /* Return TRUE iff the next tokens in the stream are possibly the
23038 beginning of a GNU extension attribute. */
23041 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
23043 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
23046 /* Return TRUE iff the next tokens in the stream are possibly the
23047 beginning of a standard C++-11 attribute specifier. */
23050 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
23052 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
23055 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23056 beginning of a standard C++-11 attribute specifier. */
23059 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
23061 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23063 return (cxx_dialect
>= cxx11
23064 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
23065 || (token
->type
== CPP_OPEN_SQUARE
23066 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
23067 && token
->type
== CPP_OPEN_SQUARE
)));
23070 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23071 beginning of a GNU extension attribute. */
23074 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
23076 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23078 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
23081 /* Return true iff the next tokens can be the beginning of either a
23082 GNU attribute list, or a standard C++11 attribute sequence. */
23085 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
23087 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
23088 || cp_next_tokens_can_be_std_attribute_p (parser
));
23091 /* Return true iff the next Nth tokens can be the beginning of either
23092 a GNU attribute list, or a standard C++11 attribute sequence. */
23095 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
23097 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
23098 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
23101 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23102 of GNU attributes, or return NULL. */
23105 cp_parser_attributes_opt (cp_parser
*parser
)
23107 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
23108 return cp_parser_gnu_attributes_opt (parser
);
23109 return cp_parser_std_attribute_spec_seq (parser
);
23112 #define CILK_SIMD_FN_CLAUSE_MASK \
23113 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23114 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23115 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23116 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23117 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23119 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23120 vector [(<clauses>)] */
23123 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
23125 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
23126 cp_token
*token
= v_token
;
23129 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
23130 parser
->cilk_simd_fn_info
->error_seen
= false;
23131 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
23132 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
23134 int paren_scope
= 0;
23135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23137 cp_lexer_consume_token (parser
->lexer
);
23138 v_token
= cp_lexer_peek_token (parser
->lexer
);
23141 while (paren_scope
> 0)
23143 token
= cp_lexer_peek_token (parser
->lexer
);
23144 if (token
->type
== CPP_OPEN_PAREN
)
23146 else if (token
->type
== CPP_CLOSE_PAREN
)
23148 /* Do not push the last ')' */
23149 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
23150 cp_lexer_consume_token (parser
->lexer
);
23153 token
->type
= CPP_PRAGMA_EOL
;
23154 parser
->lexer
->next_token
= token
;
23155 cp_lexer_consume_token (parser
->lexer
);
23157 struct cp_token_cache
*cp
23158 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
23159 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
23162 /* Parse an (optional) series of attributes.
23165 attributes attribute
23168 __attribute__ (( attribute-list [opt] ))
23170 The return value is as for cp_parser_gnu_attribute_list. */
23173 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
23175 tree attributes
= NULL_TREE
;
23180 tree attribute_list
;
23183 /* Peek at the next token. */
23184 token
= cp_lexer_peek_token (parser
->lexer
);
23185 /* If it's not `__attribute__', then we're done. */
23186 if (token
->keyword
!= RID_ATTRIBUTE
)
23189 /* Consume the `__attribute__' keyword. */
23190 cp_lexer_consume_token (parser
->lexer
);
23191 /* Look for the two `(' tokens. */
23192 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23193 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
23195 /* Peek at the next token. */
23196 token
= cp_lexer_peek_token (parser
->lexer
);
23197 if (token
->type
!= CPP_CLOSE_PAREN
)
23198 /* Parse the attribute-list. */
23199 attribute_list
= cp_parser_gnu_attribute_list (parser
);
23201 /* If the next token is a `)', then there is no attribute
23203 attribute_list
= NULL
;
23205 /* Look for the two `)' tokens. */
23206 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23208 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23211 cp_parser_skip_to_end_of_statement (parser
);
23213 /* Add these new attributes to the list. */
23214 attributes
= chainon (attributes
, attribute_list
);
23220 /* Parse a GNU attribute-list.
23224 attribute-list , attribute
23228 identifier ( identifier )
23229 identifier ( identifier , expression-list )
23230 identifier ( expression-list )
23232 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23233 to an attribute. The TREE_PURPOSE of each node is the identifier
23234 indicating which attribute is in use. The TREE_VALUE represents
23235 the arguments, if any. */
23238 cp_parser_gnu_attribute_list (cp_parser
* parser
)
23240 tree attribute_list
= NULL_TREE
;
23241 bool save_translate_strings_p
= parser
->translate_strings_p
;
23243 parser
->translate_strings_p
= false;
23250 /* Look for the identifier. We also allow keywords here; for
23251 example `__attribute__ ((const))' is legal. */
23252 token
= cp_lexer_peek_token (parser
->lexer
);
23253 if (token
->type
== CPP_NAME
23254 || token
->type
== CPP_KEYWORD
)
23256 tree arguments
= NULL_TREE
;
23258 /* Consume the token, but save it since we need it for the
23259 SIMD enabled function parsing. */
23260 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
23262 /* Save away the identifier that indicates which attribute
23264 identifier
= (token
->type
== CPP_KEYWORD
)
23265 /* For keywords, use the canonical spelling, not the
23266 parsed identifier. */
23267 ? ridpointers
[(int) token
->keyword
]
23268 : id_token
->u
.value
;
23270 attribute
= build_tree_list (identifier
, NULL_TREE
);
23272 /* Peek at the next token. */
23273 token
= cp_lexer_peek_token (parser
->lexer
);
23274 /* If it's an `(', then parse the attribute arguments. */
23275 if (token
->type
== CPP_OPEN_PAREN
)
23277 vec
<tree
, va_gc
> *vec
;
23278 int attr_flag
= (attribute_takes_identifier_p (identifier
)
23279 ? id_attr
: normal_attr
);
23280 if (is_cilkplus_vector_p (identifier
))
23282 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23286 vec
= cp_parser_parenthesized_expression_list
23287 (parser
, attr_flag
, /*cast_p=*/false,
23288 /*allow_expansion_p=*/false,
23289 /*non_constant_p=*/NULL
);
23291 arguments
= error_mark_node
;
23294 arguments
= build_tree_list_vec (vec
);
23295 release_tree_vector (vec
);
23297 /* Save the arguments away. */
23298 TREE_VALUE (attribute
) = arguments
;
23300 else if (is_cilkplus_vector_p (identifier
))
23302 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
23306 if (arguments
!= error_mark_node
)
23308 /* Add this attribute to the list. */
23309 TREE_CHAIN (attribute
) = attribute_list
;
23310 attribute_list
= attribute
;
23313 token
= cp_lexer_peek_token (parser
->lexer
);
23315 /* Now, look for more attributes. If the next token isn't a
23316 `,', we're done. */
23317 if (token
->type
!= CPP_COMMA
)
23320 /* Consume the comma and keep going. */
23321 cp_lexer_consume_token (parser
->lexer
);
23323 parser
->translate_strings_p
= save_translate_strings_p
;
23325 /* We built up the list in reverse order. */
23326 return nreverse (attribute_list
);
23329 /* Parse a standard C++11 attribute.
23331 The returned representation is a TREE_LIST which TREE_PURPOSE is
23332 the scoped name of the attribute, and the TREE_VALUE is its
23335 Note that the scoped name of the attribute is itself a TREE_LIST
23336 which TREE_PURPOSE is the namespace of the attribute, and
23337 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23338 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23339 and which TREE_PURPOSE is directly the attribute name.
23341 Clients of the attribute code should use get_attribute_namespace
23342 and get_attribute_name to get the actual namespace and name of
23343 attributes, regardless of their being GNU or C++11 attributes.
23346 attribute-token attribute-argument-clause [opt]
23350 attribute-scoped-token
23352 attribute-scoped-token:
23353 attribute-namespace :: identifier
23355 attribute-namespace:
23358 attribute-argument-clause:
23359 ( balanced-token-seq )
23361 balanced-token-seq:
23362 balanced-token [opt]
23363 balanced-token-seq balanced-token
23366 ( balanced-token-seq )
23367 [ balanced-token-seq ]
23368 { balanced-token-seq }. */
23371 cp_parser_std_attribute (cp_parser
*parser
)
23373 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
23376 /* First, parse name of the attribute, a.k.a attribute-token. */
23378 token
= cp_lexer_peek_token (parser
->lexer
);
23379 if (token
->type
== CPP_NAME
)
23380 attr_id
= token
->u
.value
;
23381 else if (token
->type
== CPP_KEYWORD
)
23382 attr_id
= ridpointers
[(int) token
->keyword
];
23383 else if (token
->flags
& NAMED_OP
)
23384 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
23386 if (attr_id
== NULL_TREE
)
23389 cp_lexer_consume_token (parser
->lexer
);
23391 token
= cp_lexer_peek_token (parser
->lexer
);
23392 if (token
->type
== CPP_SCOPE
)
23394 /* We are seeing a scoped attribute token. */
23396 cp_lexer_consume_token (parser
->lexer
);
23399 token
= cp_lexer_consume_token (parser
->lexer
);
23400 if (token
->type
== CPP_NAME
)
23401 attr_id
= token
->u
.value
;
23402 else if (token
->type
== CPP_KEYWORD
)
23403 attr_id
= ridpointers
[(int) token
->keyword
];
23406 error_at (token
->location
,
23407 "expected an identifier for the attribute name");
23408 return error_mark_node
;
23410 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
23412 token
= cp_lexer_peek_token (parser
->lexer
);
23416 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
23418 /* C++11 noreturn attribute is equivalent to GNU's. */
23419 if (is_attribute_p ("noreturn", attr_id
))
23420 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23421 /* C++14 deprecated attribute is equivalent to GNU's. */
23422 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
23424 if (cxx_dialect
== cxx11
)
23425 pedwarn (token
->location
, OPT_Wpedantic
,
23426 "%<deprecated%> is a C++14 feature;"
23427 " use %<gnu::deprecated%>");
23428 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
23430 /* Transactional Memory TS optimize_for_synchronized attribute is
23431 equivalent to GNU transaction_callable. */
23432 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
23433 TREE_PURPOSE (attribute
)
23434 = get_identifier ("transaction_callable");
23435 /* Transactional Memory attributes are GNU attributes. */
23436 else if (tm_attr_to_mask (attr_id
))
23437 TREE_PURPOSE (attribute
) = attr_id
;
23440 /* Now parse the optional argument clause of the attribute. */
23442 if (token
->type
!= CPP_OPEN_PAREN
)
23446 vec
<tree
, va_gc
> *vec
;
23447 int attr_flag
= normal_attr
;
23449 if (attr_ns
== get_identifier ("gnu")
23450 && attribute_takes_identifier_p (attr_id
))
23451 /* A GNU attribute that takes an identifier in parameter. */
23452 attr_flag
= id_attr
;
23454 vec
= cp_parser_parenthesized_expression_list
23455 (parser
, attr_flag
, /*cast_p=*/false,
23456 /*allow_expansion_p=*/true,
23457 /*non_constant_p=*/NULL
);
23459 arguments
= error_mark_node
;
23462 arguments
= build_tree_list_vec (vec
);
23463 release_tree_vector (vec
);
23466 if (arguments
== error_mark_node
)
23467 attribute
= error_mark_node
;
23469 TREE_VALUE (attribute
) = arguments
;
23475 /* Check that the attribute ATTRIBUTE appears at most once in the
23476 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23477 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23478 isn't implemented yet in GCC. */
23481 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
23485 tree name
= get_attribute_name (attribute
);
23486 if (is_attribute_p ("noreturn", name
)
23487 && lookup_attribute ("noreturn", attributes
))
23488 error ("attribute noreturn can appear at most once "
23489 "in an attribute-list");
23490 else if (is_attribute_p ("deprecated", name
)
23491 && lookup_attribute ("deprecated", attributes
))
23492 error ("attribute deprecated can appear at most once "
23493 "in an attribute-list");
23497 /* Parse a list of standard C++-11 attributes.
23501 attribute-list , attribute[opt]
23503 attribute-list , attribute ...
23507 cp_parser_std_attribute_list (cp_parser
*parser
)
23509 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
23510 cp_token
*token
= NULL
;
23514 attribute
= cp_parser_std_attribute (parser
);
23515 if (attribute
== error_mark_node
)
23517 if (attribute
!= NULL_TREE
)
23519 cp_parser_check_std_attribute (attributes
, attribute
);
23520 TREE_CHAIN (attribute
) = attributes
;
23521 attributes
= attribute
;
23523 token
= cp_lexer_peek_token (parser
->lexer
);
23524 if (token
->type
== CPP_ELLIPSIS
)
23526 cp_lexer_consume_token (parser
->lexer
);
23527 TREE_VALUE (attribute
)
23528 = make_pack_expansion (TREE_VALUE (attribute
));
23529 token
= cp_lexer_peek_token (parser
->lexer
);
23531 if (token
->type
!= CPP_COMMA
)
23533 cp_lexer_consume_token (parser
->lexer
);
23535 attributes
= nreverse (attributes
);
23539 /* Parse a standard C++-11 attribute specifier.
23541 attribute-specifier:
23542 [ [ attribute-list ] ]
23543 alignment-specifier
23545 alignment-specifier:
23546 alignas ( type-id ... [opt] )
23547 alignas ( alignment-expression ... [opt] ). */
23550 cp_parser_std_attribute_spec (cp_parser
*parser
)
23552 tree attributes
= NULL_TREE
;
23553 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23555 if (token
->type
== CPP_OPEN_SQUARE
23556 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
23558 cp_lexer_consume_token (parser
->lexer
);
23559 cp_lexer_consume_token (parser
->lexer
);
23561 attributes
= cp_parser_std_attribute_list (parser
);
23563 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
23564 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
23565 cp_parser_skip_to_end_of_statement (parser
);
23567 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23568 when we are sure that we have actually parsed them. */
23569 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
23575 /* Look for an alignment-specifier. */
23577 token
= cp_lexer_peek_token (parser
->lexer
);
23579 if (token
->type
!= CPP_KEYWORD
23580 || token
->keyword
!= RID_ALIGNAS
)
23583 cp_lexer_consume_token (parser
->lexer
);
23584 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
23586 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
23588 cp_parser_error (parser
, "expected %<(%>");
23589 return error_mark_node
;
23592 cp_parser_parse_tentatively (parser
);
23593 alignas_expr
= cp_parser_type_id (parser
);
23595 if (!cp_parser_parse_definitely (parser
))
23597 gcc_assert (alignas_expr
== error_mark_node
23598 || alignas_expr
== NULL_TREE
);
23601 cp_parser_assignment_expression (parser
);
23602 if (alignas_expr
== error_mark_node
)
23603 cp_parser_skip_to_end_of_statement (parser
);
23604 if (alignas_expr
== NULL_TREE
23605 || alignas_expr
== error_mark_node
)
23606 return alignas_expr
;
23609 alignas_expr
= cxx_alignas_expr (alignas_expr
);
23610 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
23612 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23614 cp_lexer_consume_token (parser
->lexer
);
23615 alignas_expr
= make_pack_expansion (alignas_expr
);
23618 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
23620 cp_parser_error (parser
, "expected %<)%>");
23621 return error_mark_node
;
23624 /* Build the C++-11 representation of an 'aligned'
23627 build_tree_list (build_tree_list (get_identifier ("gnu"),
23628 get_identifier ("aligned")),
23635 /* Parse a standard C++-11 attribute-specifier-seq.
23637 attribute-specifier-seq:
23638 attribute-specifier-seq [opt] attribute-specifier
23642 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
23644 tree attr_specs
= NULL
;
23648 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
23649 if (attr_spec
== NULL_TREE
)
23651 if (attr_spec
== error_mark_node
)
23652 return error_mark_node
;
23654 TREE_CHAIN (attr_spec
) = attr_specs
;
23655 attr_specs
= attr_spec
;
23658 attr_specs
= nreverse (attr_specs
);
23662 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23663 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23664 current value of the PEDANTIC flag, regardless of whether or not
23665 the `__extension__' keyword is present. The caller is responsible
23666 for restoring the value of the PEDANTIC flag. */
23669 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
23671 /* Save the old value of the PEDANTIC flag. */
23672 *saved_pedantic
= pedantic
;
23674 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
23676 /* Consume the `__extension__' token. */
23677 cp_lexer_consume_token (parser
->lexer
);
23678 /* We're not being pedantic while the `__extension__' keyword is
23688 /* Parse a label declaration.
23691 __label__ label-declarator-seq ;
23693 label-declarator-seq:
23694 identifier , label-declarator-seq
23698 cp_parser_label_declaration (cp_parser
* parser
)
23700 /* Look for the `__label__' keyword. */
23701 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
23707 /* Look for an identifier. */
23708 identifier
= cp_parser_identifier (parser
);
23709 /* If we failed, stop. */
23710 if (identifier
== error_mark_node
)
23712 /* Declare it as a label. */
23713 finish_label_decl (identifier
);
23714 /* If the next token is a `;', stop. */
23715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23717 /* Look for the `,' separating the label declarations. */
23718 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
23721 /* Look for the final `;'. */
23722 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
23725 // -------------------------------------------------------------------------- //
23728 // Parse a requires clause.
23730 // requires-clause:
23731 // 'requires' logical-or-expression
23733 // The required logical-or-expression must be a constant expression. Note
23734 // that we don't check that the expression is constepxr here. We defer until
23735 // we analyze constraints and then, we only check atomic constraints.
23737 cp_parser_requires_clause (cp_parser
*parser
)
23739 // Parse the requires clause so that it is not automatically folded.
23740 ++processing_template_decl
;
23741 tree expr
= cp_parser_binary_expression (parser
, false, false,
23742 PREC_NOT_OPERATOR
, NULL
);
23743 --processing_template_decl
;
23747 // Optionally parse a requires clause:
23749 cp_parser_requires_clause_opt (cp_parser
*parser
)
23751 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
23753 cp_lexer_consume_token (parser
->lexer
);
23754 return cp_parser_requires_clause (parser
);
23758 /*---------------------------------------------------------------------------
23759 Requires expressions
23760 ---------------------------------------------------------------------------*/
23762 /* Parse a requires expression
23764 requirement-expression:
23765 'requires' requirement-parameter-list [opt] requirement-body */
23767 cp_parser_requires_expression (cp_parser
*parser
)
23769 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
23770 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
23772 /* A requires-expression shall appear only within a concept
23773 definition or a requires-clause.
23775 TODO: Implement this diagnostic correctly. */
23776 if (!processing_template_decl
)
23778 error_at (loc
, "a requires expression cannot appear outside a template");
23779 cp_parser_skip_to_end_of_statement (parser
);
23780 return error_mark_node
;
23785 /* Local parameters are delared as variables within the scope
23786 of the expression. They are not visible past the end of
23787 the expression. Expressions within the requires-expression
23788 are unevaluated. */
23789 struct scope_sentinel
23793 ++cp_unevaluated_operand
;
23794 begin_scope (sk_block
, NULL_TREE
);
23799 pop_bindings_and_leave_scope ();
23800 --cp_unevaluated_operand
;
23804 /* Parse the optional parameter list. */
23805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
23807 parms
= cp_parser_requirement_parameter_list (parser
);
23808 if (parms
== error_mark_node
)
23809 return error_mark_node
;
23814 /* Parse the requirement body. */
23815 reqs
= cp_parser_requirement_body (parser
);
23816 if (reqs
== error_mark_node
)
23817 return error_mark_node
;
23820 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23822 grokparms (parms
, &parms
);
23823 return finish_requires_expr (parms
, reqs
);
23826 /* Parse a parameterized requirement.
23828 requirement-parameter-list:
23829 '(' parameter-declaration-clause ')' */
23831 cp_parser_requirement_parameter_list (cp_parser
*parser
)
23833 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23834 return error_mark_node
;
23836 tree parms
= cp_parser_parameter_declaration_clause (parser
);
23838 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
23839 return error_mark_node
;
23844 /* Parse the body of a requirement.
23847 '{' requirement-list '}' */
23849 cp_parser_requirement_body (cp_parser
*parser
)
23851 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
23852 return error_mark_node
;
23854 tree reqs
= cp_parser_requirement_list (parser
);
23856 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
23857 return error_mark_node
;
23862 /* Parse a list of requirements.
23866 requirement-list ';' requirement[opt] */
23868 cp_parser_requirement_list (cp_parser
*parser
)
23870 tree result
= NULL_TREE
;
23873 tree req
= cp_parser_requirement (parser
);
23874 if (req
== error_mark_node
)
23875 return error_mark_node
;
23877 result
= tree_cons (NULL_TREE
, req
, result
);
23879 /* If we see a semi-colon, consume it. */
23880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
23881 cp_lexer_consume_token (parser
->lexer
);
23883 /* Stop processing at the end of the list. */
23884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
23888 /* Reverse the order of requirements so they are analyzed in
23889 declaration order. */
23890 return nreverse (result
);
23893 /* Parse a syntactic requirement or type requirement.
23897 compound-requirement
23899 nested-requirement */
23901 cp_parser_requirement (cp_parser
*parser
)
23903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23904 return cp_parser_compound_requirement (parser
);
23905 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
23906 return cp_parser_type_requirement (parser
);
23907 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
23908 return cp_parser_nested_requirement (parser
);
23910 return cp_parser_simple_requirement (parser
);
23913 /* Parse a simple requirement.
23915 simple-requirement:
23918 cp_parser_simple_requirement (cp_parser
*parser
)
23920 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
23921 if (!expr
|| expr
== error_mark_node
)
23922 return error_mark_node
;
23924 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
23925 return error_mark_node
;
23927 return finish_simple_requirement (expr
);
23930 /* Parse a type requirement
23933 nested-name-specifier [opt] required-type-name ';'
23935 required-type-name:
23937 'template' [opt] simple-template-id */
23939 cp_parser_type_requirement (cp_parser
*parser
)
23941 cp_lexer_consume_token (parser
->lexer
);
23943 // Save the scope before parsing name specifiers.
23944 tree saved_scope
= parser
->scope
;
23945 tree saved_object_scope
= parser
->object_scope
;
23946 tree saved_qualifying_scope
= parser
->qualifying_scope
;
23947 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
23948 cp_parser_nested_name_specifier_opt (parser
,
23949 /*typename_keyword_p=*/true,
23950 /*check_dependency_p=*/false,
23952 /*is_declaration=*/false);
23955 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
23957 cp_lexer_consume_token (parser
->lexer
);
23958 type
= cp_parser_template_id (parser
,
23959 /*template_keyword_p=*/true,
23960 /*check_dependency=*/false,
23961 /*tag_type=*/none_type
,
23962 /*is_declaration=*/false);
23963 type
= make_typename_type (parser
->scope
, type
, typename_type
,
23964 /*complain=*/tf_error
);
23967 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
23969 if (TREE_CODE (type
) == TYPE_DECL
)
23970 type
= TREE_TYPE (type
);
23972 parser
->scope
= saved_scope
;
23973 parser
->object_scope
= saved_object_scope
;
23974 parser
->qualifying_scope
= saved_qualifying_scope
;
23976 if (type
== error_mark_node
)
23977 cp_parser_skip_to_end_of_statement (parser
);
23979 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
23980 return error_mark_node
;
23981 if (type
== error_mark_node
)
23982 return error_mark_node
;
23984 return finish_type_requirement (type
);
23987 /* Parse a compound requirement
23989 compound-requirement:
23990 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
23992 cp_parser_compound_requirement (cp_parser
*parser
)
23994 /* Parse an expression enclosed in '{ }'s. */
23995 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
23996 return error_mark_node
;
23998 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
23999 if (!expr
|| expr
== error_mark_node
)
24000 return error_mark_node
;
24002 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
24003 return error_mark_node
;
24005 /* Parse the optional noexcept. */
24006 bool noexcept_p
= false;
24007 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
24009 cp_lexer_consume_token (parser
->lexer
);
24013 /* Parse the optional trailing return type. */
24014 tree type
= NULL_TREE
;
24015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
24017 cp_lexer_consume_token (parser
->lexer
);
24018 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
24019 parser
->in_result_type_constraint_p
= true;
24020 type
= cp_parser_trailing_type_id (parser
);
24021 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
24022 if (type
== error_mark_node
)
24023 return error_mark_node
;
24026 return finish_compound_requirement (expr
, type
, noexcept_p
);
24029 /* Parse a nested requirement. This is the same as a requires clause.
24031 nested-requirement:
24034 cp_parser_nested_requirement (cp_parser
*parser
)
24036 cp_lexer_consume_token (parser
->lexer
);
24037 tree req
= cp_parser_requires_clause (parser
);
24038 if (req
== error_mark_node
)
24039 return error_mark_node
;
24040 return finish_nested_requirement (req
);
24043 /* Support Functions */
24045 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24046 NAME should have one of the representations used for an
24047 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24048 is returned. If PARSER->SCOPE is a dependent type, then a
24049 SCOPE_REF is returned.
24051 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24052 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24053 was formed. Abstractly, such entities should not be passed to this
24054 function, because they do not need to be looked up, but it is
24055 simpler to check for this special case here, rather than at the
24058 In cases not explicitly covered above, this function returns a
24059 DECL, OVERLOAD, or baselink representing the result of the lookup.
24060 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24063 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24064 (e.g., "struct") that was used. In that case bindings that do not
24065 refer to types are ignored.
24067 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24070 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24073 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24076 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24077 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24078 NULL_TREE otherwise. */
24081 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
24082 enum tag_types tag_type
,
24085 bool check_dependency
,
24086 tree
*ambiguous_decls
,
24087 location_t name_location
)
24090 tree object_type
= parser
->context
->object_type
;
24092 /* Assume that the lookup will be unambiguous. */
24093 if (ambiguous_decls
)
24094 *ambiguous_decls
= NULL_TREE
;
24096 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24097 no longer valid. Note that if we are parsing tentatively, and
24098 the parse fails, OBJECT_TYPE will be automatically restored. */
24099 parser
->context
->object_type
= NULL_TREE
;
24101 if (name
== error_mark_node
)
24102 return error_mark_node
;
24104 /* A template-id has already been resolved; there is no lookup to
24106 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
24108 if (BASELINK_P (name
))
24110 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
24111 == TEMPLATE_ID_EXPR
);
24115 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24116 it should already have been checked to make sure that the name
24117 used matches the type being destroyed. */
24118 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
24122 /* Figure out to which type this destructor applies. */
24124 type
= parser
->scope
;
24125 else if (object_type
)
24126 type
= object_type
;
24128 type
= current_class_type
;
24129 /* If that's not a class type, there is no destructor. */
24130 if (!type
|| !CLASS_TYPE_P (type
))
24131 return error_mark_node
;
24132 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
24133 lazily_declare_fn (sfk_destructor
, type
);
24134 if (!CLASSTYPE_DESTRUCTORS (type
))
24135 return error_mark_node
;
24136 /* If it was a class type, return the destructor. */
24137 return CLASSTYPE_DESTRUCTORS (type
);
24140 /* By this point, the NAME should be an ordinary identifier. If
24141 the id-expression was a qualified name, the qualifying scope is
24142 stored in PARSER->SCOPE at this point. */
24143 gcc_assert (identifier_p (name
));
24145 /* Perform the lookup. */
24150 if (parser
->scope
== error_mark_node
)
24151 return error_mark_node
;
24153 /* If the SCOPE is dependent, the lookup must be deferred until
24154 the template is instantiated -- unless we are explicitly
24155 looking up names in uninstantiated templates. Even then, we
24156 cannot look up the name if the scope is not a class type; it
24157 might, for example, be a template type parameter. */
24158 dependent_p
= (TYPE_P (parser
->scope
)
24159 && dependent_scope_p (parser
->scope
));
24160 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
24162 /* Defer lookup. */
24163 decl
= error_mark_node
;
24166 tree pushed_scope
= NULL_TREE
;
24168 /* If PARSER->SCOPE is a dependent type, then it must be a
24169 class type, and we must not be checking dependencies;
24170 otherwise, we would have processed this lookup above. So
24171 that PARSER->SCOPE is not considered a dependent base by
24172 lookup_member, we must enter the scope here. */
24174 pushed_scope
= push_scope (parser
->scope
);
24176 /* If the PARSER->SCOPE is a template specialization, it
24177 may be instantiated during name lookup. In that case,
24178 errors may be issued. Even if we rollback the current
24179 tentative parse, those errors are valid. */
24180 decl
= lookup_qualified_name (parser
->scope
, name
,
24181 tag_type
!= none_type
,
24182 /*complain=*/true);
24184 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24185 lookup result and the nested-name-specifier nominates a class C:
24186 * if the name specified after the nested-name-specifier, when
24187 looked up in C, is the injected-class-name of C (Clause 9), or
24188 * if the name specified after the nested-name-specifier is the
24189 same as the identifier or the simple-template-id's template-
24190 name in the last component of the nested-name-specifier,
24191 the name is instead considered to name the constructor of
24192 class C. [ Note: for example, the constructor is not an
24193 acceptable lookup result in an elaborated-type-specifier so
24194 the constructor would not be used in place of the
24195 injected-class-name. --end note ] Such a constructor name
24196 shall be used only in the declarator-id of a declaration that
24197 names a constructor or in a using-declaration. */
24198 if (tag_type
== none_type
24199 && DECL_SELF_REFERENCE_P (decl
)
24200 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
24201 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
24202 tag_type
!= none_type
,
24203 /*complain=*/true);
24205 /* If we have a single function from a using decl, pull it out. */
24206 if (TREE_CODE (decl
) == OVERLOAD
24207 && !really_overloaded_fn (decl
))
24208 decl
= OVL_FUNCTION (decl
);
24211 pop_scope (pushed_scope
);
24214 /* If the scope is a dependent type and either we deferred lookup or
24215 we did lookup but didn't find the name, rememeber the name. */
24216 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
24217 && dependent_type_p (parser
->scope
))
24223 /* The resolution to Core Issue 180 says that `struct
24224 A::B' should be considered a type-name, even if `A'
24226 type
= make_typename_type (parser
->scope
, name
, tag_type
,
24227 /*complain=*/tf_error
);
24228 if (type
!= error_mark_node
)
24229 decl
= TYPE_NAME (type
);
24231 else if (is_template
24232 && (cp_parser_next_token_ends_template_argument_p (parser
)
24233 || cp_lexer_next_token_is (parser
->lexer
,
24235 decl
= make_unbound_class_template (parser
->scope
,
24237 /*complain=*/tf_error
);
24239 decl
= build_qualified_name (/*type=*/NULL_TREE
,
24240 parser
->scope
, name
,
24243 parser
->qualifying_scope
= parser
->scope
;
24244 parser
->object_scope
= NULL_TREE
;
24246 else if (object_type
)
24248 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24249 OBJECT_TYPE is not a class. */
24250 if (CLASS_TYPE_P (object_type
))
24251 /* If the OBJECT_TYPE is a template specialization, it may
24252 be instantiated during name lookup. In that case, errors
24253 may be issued. Even if we rollback the current tentative
24254 parse, those errors are valid. */
24255 decl
= lookup_member (object_type
,
24258 tag_type
!= none_type
,
24259 tf_warning_or_error
);
24264 /* Look it up in the enclosing context. */
24265 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24267 /*block_p=*/true, is_namespace
, 0);
24268 parser
->object_scope
= object_type
;
24269 parser
->qualifying_scope
= NULL_TREE
;
24273 decl
= lookup_name_real (name
, tag_type
!= none_type
,
24275 /*block_p=*/true, is_namespace
, 0);
24276 parser
->qualifying_scope
= NULL_TREE
;
24277 parser
->object_scope
= NULL_TREE
;
24280 /* If the lookup failed, let our caller know. */
24281 if (!decl
|| decl
== error_mark_node
)
24282 return error_mark_node
;
24284 /* Pull out the template from an injected-class-name (or multiple). */
24286 decl
= maybe_get_template_decl_from_type_decl (decl
);
24288 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24289 if (TREE_CODE (decl
) == TREE_LIST
)
24291 if (ambiguous_decls
)
24292 *ambiguous_decls
= decl
;
24293 /* The error message we have to print is too complicated for
24294 cp_parser_error, so we incorporate its actions directly. */
24295 if (!cp_parser_simulate_error (parser
))
24297 error_at (name_location
, "reference to %qD is ambiguous",
24299 print_candidates (decl
);
24301 return error_mark_node
;
24304 gcc_assert (DECL_P (decl
)
24305 || TREE_CODE (decl
) == OVERLOAD
24306 || TREE_CODE (decl
) == SCOPE_REF
24307 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
24308 || BASELINK_P (decl
));
24310 /* If we have resolved the name of a member declaration, check to
24311 see if the declaration is accessible. When the name resolves to
24312 set of overloaded functions, accessibility is checked when
24313 overload resolution is done.
24315 During an explicit instantiation, access is not checked at all,
24316 as per [temp.explicit]. */
24318 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
24320 maybe_record_typedef_use (decl
);
24325 /* Like cp_parser_lookup_name, but for use in the typical case where
24326 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24327 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24330 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
24332 return cp_parser_lookup_name (parser
, name
,
24334 /*is_template=*/false,
24335 /*is_namespace=*/false,
24336 /*check_dependency=*/true,
24337 /*ambiguous_decls=*/NULL
,
24341 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24342 the current context, return the TYPE_DECL. If TAG_NAME_P is
24343 true, the DECL indicates the class being defined in a class-head,
24344 or declared in an elaborated-type-specifier.
24346 Otherwise, return DECL. */
24349 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
24351 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24352 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24355 template <typename T> struct B;
24358 template <typename T> struct A::B {};
24360 Similarly, in an elaborated-type-specifier:
24362 namespace N { struct X{}; }
24365 template <typename T> friend struct N::X;
24368 However, if the DECL refers to a class type, and we are in
24369 the scope of the class, then the name lookup automatically
24370 finds the TYPE_DECL created by build_self_reference rather
24371 than a TEMPLATE_DECL. For example, in:
24373 template <class T> struct S {
24377 there is no need to handle such case. */
24379 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
24380 return DECL_TEMPLATE_RESULT (decl
);
24385 /* If too many, or too few, template-parameter lists apply to the
24386 declarator, issue an error message. Returns TRUE if all went well,
24387 and FALSE otherwise. */
24390 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
24391 cp_declarator
*declarator
,
24392 location_t declarator_location
)
24394 switch (declarator
->kind
)
24398 unsigned num_templates
= 0;
24399 tree scope
= declarator
->u
.id
.qualifying_scope
;
24402 num_templates
= num_template_headers_for_class (scope
);
24403 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
24404 == TEMPLATE_ID_EXPR
)
24405 /* If the DECLARATOR has the form `X<y>' then it uses one
24406 additional level of template parameters. */
24409 return cp_parser_check_template_parameters
24410 (parser
, num_templates
, declarator_location
, declarator
);
24416 case cdk_reference
:
24418 return (cp_parser_check_declarator_template_parameters
24419 (parser
, declarator
->declarator
, declarator_location
));
24425 gcc_unreachable ();
24430 /* NUM_TEMPLATES were used in the current declaration. If that is
24431 invalid, return FALSE and issue an error messages. Otherwise,
24432 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24433 declarator and we can print more accurate diagnostics. */
24436 cp_parser_check_template_parameters (cp_parser
* parser
,
24437 unsigned num_templates
,
24438 location_t location
,
24439 cp_declarator
*declarator
)
24441 /* If there are the same number of template classes and parameter
24442 lists, that's OK. */
24443 if (parser
->num_template_parameter_lists
== num_templates
)
24445 /* If there are more, but only one more, then we are referring to a
24446 member template. That's OK too. */
24447 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
24449 /* If there are more template classes than parameter lists, we have
24452 template <class T> void S<T>::R<T>::f (); */
24453 if (parser
->num_template_parameter_lists
< num_templates
)
24455 if (declarator
&& !current_function_decl
)
24456 error_at (location
, "specializing member %<%T::%E%> "
24457 "requires %<template<>%> syntax",
24458 declarator
->u
.id
.qualifying_scope
,
24459 declarator
->u
.id
.unqualified_name
);
24460 else if (declarator
)
24461 error_at (location
, "invalid declaration of %<%T::%E%>",
24462 declarator
->u
.id
.qualifying_scope
,
24463 declarator
->u
.id
.unqualified_name
);
24465 error_at (location
, "too few template-parameter-lists");
24468 /* Otherwise, there are too many template parameter lists. We have
24471 template <class T> template <class U> void S::f(); */
24472 error_at (location
, "too many template-parameter-lists");
24476 /* Parse an optional `::' token indicating that the following name is
24477 from the global namespace. If so, PARSER->SCOPE is set to the
24478 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24479 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24480 Returns the new value of PARSER->SCOPE, if the `::' token is
24481 present, and NULL_TREE otherwise. */
24484 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
24488 /* Peek at the next token. */
24489 token
= cp_lexer_peek_token (parser
->lexer
);
24490 /* If we're looking at a `::' token then we're starting from the
24491 global namespace, not our current location. */
24492 if (token
->type
== CPP_SCOPE
)
24494 /* Consume the `::' token. */
24495 cp_lexer_consume_token (parser
->lexer
);
24496 /* Set the SCOPE so that we know where to start the lookup. */
24497 parser
->scope
= global_namespace
;
24498 parser
->qualifying_scope
= global_namespace
;
24499 parser
->object_scope
= NULL_TREE
;
24501 return parser
->scope
;
24503 else if (!current_scope_valid_p
)
24505 parser
->scope
= NULL_TREE
;
24506 parser
->qualifying_scope
= NULL_TREE
;
24507 parser
->object_scope
= NULL_TREE
;
24513 /* Returns TRUE if the upcoming token sequence is the start of a
24514 constructor declarator. If FRIEND_P is true, the declarator is
24515 preceded by the `friend' specifier. */
24518 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
24520 bool constructor_p
;
24521 bool outside_class_specifier_p
;
24522 tree nested_name_specifier
;
24523 cp_token
*next_token
;
24525 /* The common case is that this is not a constructor declarator, so
24526 try to avoid doing lots of work if at all possible. It's not
24527 valid declare a constructor at function scope. */
24528 if (parser
->in_function_body
)
24530 /* And only certain tokens can begin a constructor declarator. */
24531 next_token
= cp_lexer_peek_token (parser
->lexer
);
24532 if (next_token
->type
!= CPP_NAME
24533 && next_token
->type
!= CPP_SCOPE
24534 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
24535 && next_token
->type
!= CPP_TEMPLATE_ID
)
24538 /* Parse tentatively; we are going to roll back all of the tokens
24540 cp_parser_parse_tentatively (parser
);
24541 /* Assume that we are looking at a constructor declarator. */
24542 constructor_p
= true;
24544 /* Look for the optional `::' operator. */
24545 cp_parser_global_scope_opt (parser
,
24546 /*current_scope_valid_p=*/false);
24547 /* Look for the nested-name-specifier. */
24548 nested_name_specifier
24549 = (cp_parser_nested_name_specifier_opt (parser
,
24550 /*typename_keyword_p=*/false,
24551 /*check_dependency_p=*/false,
24553 /*is_declaration=*/false));
24555 outside_class_specifier_p
= (!at_class_scope_p ()
24556 || !TYPE_BEING_DEFINED (current_class_type
)
24559 /* Outside of a class-specifier, there must be a
24560 nested-name-specifier. */
24561 if (!nested_name_specifier
&& outside_class_specifier_p
)
24562 constructor_p
= false;
24563 else if (nested_name_specifier
== error_mark_node
)
24564 constructor_p
= false;
24566 /* If we have a class scope, this is easy; DR 147 says that S::S always
24567 names the constructor, and no other qualified name could. */
24568 if (constructor_p
&& nested_name_specifier
24569 && CLASS_TYPE_P (nested_name_specifier
))
24571 tree id
= cp_parser_unqualified_id (parser
,
24572 /*template_keyword_p=*/false,
24573 /*check_dependency_p=*/false,
24574 /*declarator_p=*/true,
24575 /*optional_p=*/false);
24576 if (is_overloaded_fn (id
))
24577 id
= DECL_NAME (get_first_fn (id
));
24578 if (!constructor_name_p (id
, nested_name_specifier
))
24579 constructor_p
= false;
24581 /* If we still think that this might be a constructor-declarator,
24582 look for a class-name. */
24583 else if (constructor_p
)
24587 template <typename T> struct S {
24591 we must recognize that the nested `S' names a class. */
24593 type_decl
= cp_parser_class_name (parser
,
24594 /*typename_keyword_p=*/false,
24595 /*template_keyword_p=*/false,
24597 /*check_dependency_p=*/false,
24598 /*class_head_p=*/false,
24599 /*is_declaration=*/false);
24600 /* If there was no class-name, then this is not a constructor.
24601 Otherwise, if we are in a class-specifier and we aren't
24602 handling a friend declaration, check that its type matches
24603 current_class_type (c++/38313). Note: error_mark_node
24604 is left alone for error recovery purposes. */
24605 constructor_p
= (!cp_parser_error_occurred (parser
)
24606 && (outside_class_specifier_p
24607 || type_decl
== error_mark_node
24608 || same_type_p (current_class_type
,
24609 TREE_TYPE (type_decl
))));
24611 /* If we're still considering a constructor, we have to see a `(',
24612 to begin the parameter-declaration-clause, followed by either a
24613 `)', an `...', or a decl-specifier. We need to check for a
24614 type-specifier to avoid being fooled into thinking that:
24618 is a constructor. (It is actually a function named `f' that
24619 takes one parameter (of type `int') and returns a value of type
24622 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
24623 constructor_p
= false;
24626 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
24627 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
24628 /* A parameter declaration begins with a decl-specifier,
24629 which is either the "attribute" keyword, a storage class
24630 specifier, or (usually) a type-specifier. */
24631 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
24634 tree pushed_scope
= NULL_TREE
;
24635 unsigned saved_num_template_parameter_lists
;
24637 /* Names appearing in the type-specifier should be looked up
24638 in the scope of the class. */
24639 if (current_class_type
)
24643 type
= TREE_TYPE (type_decl
);
24644 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24646 type
= resolve_typename_type (type
,
24647 /*only_current_p=*/false);
24648 if (TREE_CODE (type
) == TYPENAME_TYPE
)
24650 cp_parser_abort_tentative_parse (parser
);
24654 pushed_scope
= push_scope (type
);
24657 /* Inside the constructor parameter list, surrounding
24658 template-parameter-lists do not apply. */
24659 saved_num_template_parameter_lists
24660 = parser
->num_template_parameter_lists
;
24661 parser
->num_template_parameter_lists
= 0;
24663 /* Look for the type-specifier. */
24664 cp_parser_type_specifier (parser
,
24665 CP_PARSER_FLAGS_NONE
,
24666 /*decl_specs=*/NULL
,
24667 /*is_declarator=*/true,
24668 /*declares_class_or_enum=*/NULL
,
24669 /*is_cv_qualifier=*/NULL
);
24671 parser
->num_template_parameter_lists
24672 = saved_num_template_parameter_lists
;
24674 /* Leave the scope of the class. */
24676 pop_scope (pushed_scope
);
24678 constructor_p
= !cp_parser_error_occurred (parser
);
24682 /* We did not really want to consume any tokens. */
24683 cp_parser_abort_tentative_parse (parser
);
24685 return constructor_p
;
24688 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24689 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24690 they must be performed once we are in the scope of the function.
24692 Returns the function defined. */
24695 cp_parser_function_definition_from_specifiers_and_declarator
24696 (cp_parser
* parser
,
24697 cp_decl_specifier_seq
*decl_specifiers
,
24699 const cp_declarator
*declarator
)
24704 /* Begin the function-definition. */
24705 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
24707 /* The things we're about to see are not directly qualified by any
24708 template headers we've seen thus far. */
24709 reset_specialization ();
24711 /* If there were names looked up in the decl-specifier-seq that we
24712 did not check, check them now. We must wait until we are in the
24713 scope of the function to perform the checks, since the function
24714 might be a friend. */
24715 perform_deferred_access_checks (tf_warning_or_error
);
24719 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
24720 parser
->omp_declare_simd
= NULL
;
24721 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
24722 parser
->oacc_routine
= NULL
;
24727 /* Skip the entire function. */
24728 cp_parser_skip_to_end_of_block_or_statement (parser
);
24729 fn
= error_mark_node
;
24731 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
24733 /* Seen already, skip it. An error message has already been output. */
24734 cp_parser_skip_to_end_of_block_or_statement (parser
);
24735 fn
= current_function_decl
;
24736 current_function_decl
= NULL_TREE
;
24737 /* If this is a function from a class, pop the nested class. */
24738 if (current_class_name
)
24739 pop_nested_class ();
24744 if (DECL_DECLARED_INLINE_P (current_function_decl
))
24745 tv
= TV_PARSE_INLINE
;
24747 tv
= TV_PARSE_FUNC
;
24749 fn
= cp_parser_function_definition_after_declarator (parser
,
24750 /*inline_p=*/false);
24757 /* Parse the part of a function-definition that follows the
24758 declarator. INLINE_P is TRUE iff this function is an inline
24759 function defined within a class-specifier.
24761 Returns the function defined. */
24764 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
24768 bool ctor_initializer_p
= false;
24769 bool saved_in_unbraced_linkage_specification_p
;
24770 bool saved_in_function_body
;
24771 unsigned saved_num_template_parameter_lists
;
24773 bool fully_implicit_function_template_p
24774 = parser
->fully_implicit_function_template_p
;
24775 parser
->fully_implicit_function_template_p
= false;
24776 tree implicit_template_parms
24777 = parser
->implicit_template_parms
;
24778 parser
->implicit_template_parms
= 0;
24779 cp_binding_level
* implicit_template_scope
24780 = parser
->implicit_template_scope
;
24781 parser
->implicit_template_scope
= 0;
24783 saved_in_function_body
= parser
->in_function_body
;
24784 parser
->in_function_body
= true;
24785 /* If the next token is `return', then the code may be trying to
24786 make use of the "named return value" extension that G++ used to
24788 token
= cp_lexer_peek_token (parser
->lexer
);
24789 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
24791 /* Consume the `return' keyword. */
24792 cp_lexer_consume_token (parser
->lexer
);
24793 /* Look for the identifier that indicates what value is to be
24795 cp_parser_identifier (parser
);
24796 /* Issue an error message. */
24797 error_at (token
->location
,
24798 "named return values are no longer supported");
24799 /* Skip tokens until we reach the start of the function body. */
24802 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24803 if (token
->type
== CPP_OPEN_BRACE
24804 || token
->type
== CPP_EOF
24805 || token
->type
== CPP_PRAGMA_EOL
)
24807 cp_lexer_consume_token (parser
->lexer
);
24810 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24811 anything declared inside `f'. */
24812 saved_in_unbraced_linkage_specification_p
24813 = parser
->in_unbraced_linkage_specification_p
;
24814 parser
->in_unbraced_linkage_specification_p
= false;
24815 /* Inside the function, surrounding template-parameter-lists do not
24817 saved_num_template_parameter_lists
24818 = parser
->num_template_parameter_lists
;
24819 parser
->num_template_parameter_lists
= 0;
24821 start_lambda_scope (current_function_decl
);
24823 /* If the next token is `try', `__transaction_atomic', or
24824 `__transaction_relaxed`, then we are looking at either function-try-block
24825 or function-transaction-block. Note that all of these include the
24827 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
24828 ctor_initializer_p
= cp_parser_function_transaction (parser
,
24829 RID_TRANSACTION_ATOMIC
);
24830 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
24831 RID_TRANSACTION_RELAXED
))
24832 ctor_initializer_p
= cp_parser_function_transaction (parser
,
24833 RID_TRANSACTION_RELAXED
);
24834 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
24835 ctor_initializer_p
= cp_parser_function_try_block (parser
);
24837 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
24838 (parser
, /*in_function_try_block=*/false);
24840 finish_lambda_scope ();
24842 /* Finish the function. */
24843 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
24844 (inline_p
? 2 : 0));
24845 /* Generate code for it, if necessary. */
24846 expand_or_defer_fn (fn
);
24847 /* Restore the saved values. */
24848 parser
->in_unbraced_linkage_specification_p
24849 = saved_in_unbraced_linkage_specification_p
;
24850 parser
->num_template_parameter_lists
24851 = saved_num_template_parameter_lists
;
24852 parser
->in_function_body
= saved_in_function_body
;
24854 parser
->fully_implicit_function_template_p
24855 = fully_implicit_function_template_p
;
24856 parser
->implicit_template_parms
24857 = implicit_template_parms
;
24858 parser
->implicit_template_scope
24859 = implicit_template_scope
;
24861 if (parser
->fully_implicit_function_template_p
)
24862 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
24867 /* Parse a template-declaration body (following argument list). */
24870 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
24871 tree parameter_list
,
24874 tree decl
= NULL_TREE
;
24875 bool friend_p
= false;
24877 /* We just processed one more parameter list. */
24878 ++parser
->num_template_parameter_lists
;
24880 /* Get the deferred access checks from the parameter list. These
24881 will be checked once we know what is being declared, as for a
24882 member template the checks must be performed in the scope of the
24883 class containing the member. */
24884 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
24886 /* Tentatively parse for a new template parameter list, which can either be
24887 the template keyword or a template introduction. */
24888 if (cp_parser_template_declaration_after_export (parser
, member_p
))
24890 else if (cxx_dialect
>= cxx11
24891 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24892 decl
= cp_parser_alias_declaration (parser
);
24895 /* There are no access checks when parsing a template, as we do not
24896 know if a specialization will be a friend. */
24897 push_deferring_access_checks (dk_no_check
);
24898 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24899 decl
= cp_parser_single_declaration (parser
,
24902 /*explicit_specialization_p=*/false,
24904 pop_deferring_access_checks ();
24906 /* If this is a member template declaration, let the front
24908 if (member_p
&& !friend_p
&& decl
)
24910 if (TREE_CODE (decl
) == TYPE_DECL
)
24911 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
24913 decl
= finish_member_template_decl (decl
);
24915 else if (friend_p
&& decl
24916 && DECL_DECLARES_TYPE_P (decl
))
24917 make_friend_class (current_class_type
, TREE_TYPE (decl
),
24918 /*complain=*/true);
24920 /* We are done with the current parameter list. */
24921 --parser
->num_template_parameter_lists
;
24923 pop_deferring_access_checks ();
24926 finish_template_decl (parameter_list
);
24928 /* Check the template arguments for a literal operator template. */
24930 && DECL_DECLARES_FUNCTION_P (decl
)
24931 && UDLIT_OPER_P (DECL_NAME (decl
)))
24934 if (parameter_list
== NULL_TREE
)
24938 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
24939 if (num_parms
== 1)
24941 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
24942 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
24943 if (TREE_TYPE (parm
) != char_type_node
24944 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
24947 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
24949 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
24950 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
24951 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
24952 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
24953 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
24954 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
24962 if (cxx_dialect
>= cxx14
)
24963 error ("literal operator template %qD has invalid parameter list."
24964 " Expected non-type template argument pack <char...>"
24965 " or <typename CharT, CharT...>",
24968 error ("literal operator template %qD has invalid parameter list."
24969 " Expected non-type template argument pack <char...>",
24974 /* Register member declarations. */
24975 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
24976 finish_member_declaration (decl
);
24977 /* If DECL is a function template, we must return to parse it later.
24978 (Even though there is no definition, there might be default
24979 arguments that need handling.) */
24980 if (member_p
&& decl
24981 && DECL_DECLARES_FUNCTION_P (decl
))
24982 vec_safe_push (unparsed_funs_with_definitions
, decl
);
24985 /* Parse a template introduction header for a template-declaration. Returns
24986 false if tentative parse fails. */
24989 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
24991 cp_parser_parse_tentatively (parser
);
24993 tree saved_scope
= parser
->scope
;
24994 tree saved_object_scope
= parser
->object_scope
;
24995 tree saved_qualifying_scope
= parser
->qualifying_scope
;
24997 /* Look for the optional `::' operator. */
24998 cp_parser_global_scope_opt (parser
,
24999 /*current_scope_valid_p=*/false);
25000 /* Look for the nested-name-specifier. */
25001 cp_parser_nested_name_specifier_opt (parser
,
25002 /*typename_keyword_p=*/false,
25003 /*check_dependency_p=*/true,
25005 /*is_declaration=*/false);
25007 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25008 tree concept_name
= cp_parser_identifier (parser
);
25010 /* Look up the concept for which we will be matching
25011 template parameters. */
25012 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
25014 parser
->scope
= saved_scope
;
25015 parser
->object_scope
= saved_object_scope
;
25016 parser
->qualifying_scope
= saved_qualifying_scope
;
25018 if (concept_name
== error_mark_node
)
25019 cp_parser_simulate_error (parser
);
25021 /* Look for opening brace for introduction. */
25022 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
25024 if (!cp_parser_parse_definitely (parser
))
25027 push_deferring_access_checks (dk_deferred
);
25029 /* Build vector of placeholder parameters and grab
25030 matching identifiers. */
25031 tree introduction_list
= cp_parser_introduction_list (parser
);
25033 /* The introduction-list shall not be empty. */
25034 int nargs
= TREE_VEC_LENGTH (introduction_list
);
25037 error ("empty introduction-list");
25041 /* Look for closing brace for introduction. */
25042 if (!cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
))
25045 if (tmpl_decl
== error_mark_node
)
25047 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
25052 /* Build and associate the constraint. */
25053 tree parms
= finish_template_introduction (tmpl_decl
, introduction_list
);
25054 if (parms
&& parms
!= error_mark_node
)
25056 cp_parser_template_declaration_after_parameters (parser
, parms
,
25061 error_at (token
->location
, "no matching concept for template-introduction");
25065 /* Parse a normal template-declaration following the template keyword. */
25068 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
25070 tree parameter_list
;
25071 bool need_lang_pop
;
25072 location_t location
= input_location
;
25074 /* Look for the `<' token. */
25075 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
25077 if (at_class_scope_p () && current_function_decl
)
25079 /* 14.5.2.2 [temp.mem]
25081 A local class shall not have member templates. */
25082 error_at (location
,
25083 "invalid declaration of member template in local class");
25084 cp_parser_skip_to_end_of_block_or_statement (parser
);
25089 A template ... shall not have C linkage. */
25090 if (current_lang_name
== lang_name_c
)
25092 error_at (location
, "template with C linkage");
25093 /* Give it C++ linkage to avoid confusing other parts of the
25095 push_lang_context (lang_name_cplusplus
);
25096 need_lang_pop
= true;
25099 need_lang_pop
= false;
25101 /* We cannot perform access checks on the template parameter
25102 declarations until we know what is being declared, just as we
25103 cannot check the decl-specifier list. */
25104 push_deferring_access_checks (dk_deferred
);
25106 /* If the next token is `>', then we have an invalid
25107 specialization. Rather than complain about an invalid template
25108 parameter, issue an error message here. */
25109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
25111 cp_parser_error (parser
, "invalid explicit specialization");
25112 begin_specialization ();
25113 parameter_list
= NULL_TREE
;
25117 /* Parse the template parameters. */
25118 parameter_list
= cp_parser_template_parameter_list (parser
);
25121 /* Look for the `>'. */
25122 cp_parser_skip_to_end_of_template_parameter_list (parser
);
25124 /* Manage template requirements */
25127 tree reqs
= get_shorthand_constraints (current_template_parms
);
25128 if (tree r
= cp_parser_requires_clause_opt (parser
))
25129 reqs
= conjoin_constraints (reqs
, make_predicate_constraint (r
));
25130 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
25133 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
25136 /* For the erroneous case of a template with C linkage, we pushed an
25137 implicit C++ linkage scope; exit that scope now. */
25139 pop_lang_context ();
25142 /* Parse a template-declaration, assuming that the `export' (and
25143 `extern') keywords, if present, has already been scanned. MEMBER_P
25144 is as for cp_parser_template_declaration. */
25147 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
25149 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25151 cp_lexer_consume_token (parser
->lexer
);
25152 cp_parser_explicit_template_declaration (parser
, member_p
);
25155 else if (flag_concepts
)
25156 return cp_parser_template_introduction (parser
, member_p
);
25161 /* Perform the deferred access checks from a template-parameter-list.
25162 CHECKS is a TREE_LIST of access checks, as returned by
25163 get_deferred_access_checks. */
25166 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
25168 ++processing_template_parmlist
;
25169 perform_access_checks (checks
, tf_warning_or_error
);
25170 --processing_template_parmlist
;
25173 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25174 `function-definition' sequence that follows a template header.
25175 If MEMBER_P is true, this declaration appears in a class scope.
25177 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25178 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25181 cp_parser_single_declaration (cp_parser
* parser
,
25182 vec
<deferred_access_check
, va_gc
> *checks
,
25184 bool explicit_specialization_p
,
25187 int declares_class_or_enum
;
25188 tree decl
= NULL_TREE
;
25189 cp_decl_specifier_seq decl_specifiers
;
25190 bool function_definition_p
= false;
25191 cp_token
*decl_spec_token_start
;
25193 /* This function is only used when processing a template
25195 gcc_assert (innermost_scope_kind () == sk_template_parms
25196 || innermost_scope_kind () == sk_template_spec
);
25198 /* Defer access checks until we know what is being declared. */
25199 push_deferring_access_checks (dk_deferred
);
25201 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25203 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25204 cp_parser_decl_specifier_seq (parser
,
25205 CP_PARSER_FLAGS_OPTIONAL
,
25207 &declares_class_or_enum
);
25209 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
25211 /* There are no template typedefs. */
25212 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
25214 error_at (decl_spec_token_start
->location
,
25215 "template declaration of %<typedef%>");
25216 decl
= error_mark_node
;
25219 /* Gather up the access checks that occurred the
25220 decl-specifier-seq. */
25221 stop_deferring_access_checks ();
25223 /* Check for the declaration of a template class. */
25224 if (declares_class_or_enum
)
25226 if (cp_parser_declares_only_class_p (parser
))
25228 // If this is a declaration, but not a definition, associate
25229 // any constraints with the type declaration. Constraints
25230 // are associated with definitions in cp_parser_class_specifier.
25231 if (declares_class_or_enum
== 1)
25232 associate_classtype_constraints (decl_specifiers
.type
);
25234 decl
= shadow_tag (&decl_specifiers
);
25239 friend template <typename T> struct A<T>::B;
25242 A<T>::B will be represented by a TYPENAME_TYPE, and
25243 therefore not recognized by shadow_tag. */
25244 if (friend_p
&& *friend_p
25246 && decl_specifiers
.type
25247 && TYPE_P (decl_specifiers
.type
))
25248 decl
= decl_specifiers
.type
;
25250 if (decl
&& decl
!= error_mark_node
)
25251 decl
= TYPE_NAME (decl
);
25253 decl
= error_mark_node
;
25255 /* Perform access checks for template parameters. */
25256 cp_parser_perform_template_parameter_access_checks (checks
);
25260 /* Complain about missing 'typename' or other invalid type names. */
25261 if (!decl_specifiers
.any_type_specifiers_p
25262 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25264 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25265 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25266 the rest of this declaration. */
25267 decl
= error_mark_node
;
25271 /* If it's not a template class, try for a template function. If
25272 the next token is a `;', then this declaration does not declare
25273 anything. But, if there were errors in the decl-specifiers, then
25274 the error might well have come from an attempted class-specifier.
25275 In that case, there's no need to warn about a missing declarator. */
25277 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
25278 || decl_specifiers
.type
!= error_mark_node
))
25280 decl
= cp_parser_init_declarator (parser
,
25283 /*function_definition_allowed_p=*/true,
25285 declares_class_or_enum
,
25286 &function_definition_p
,
25289 /* 7.1.1-1 [dcl.stc]
25291 A storage-class-specifier shall not be specified in an explicit
25292 specialization... */
25294 && explicit_specialization_p
25295 && decl_specifiers
.storage_class
!= sc_none
)
25297 error_at (decl_spec_token_start
->location
,
25298 "explicit template specialization cannot have a storage class");
25299 decl
= error_mark_node
;
25302 if (decl
&& VAR_P (decl
))
25303 check_template_variable (decl
);
25306 /* Look for a trailing `;' after the declaration. */
25307 if (!function_definition_p
25308 && (decl
== error_mark_node
25309 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
25310 cp_parser_skip_to_end_of_block_or_statement (parser
);
25313 pop_deferring_access_checks ();
25315 /* Clear any current qualification; whatever comes next is the start
25316 of something new. */
25317 parser
->scope
= NULL_TREE
;
25318 parser
->qualifying_scope
= NULL_TREE
;
25319 parser
->object_scope
= NULL_TREE
;
25324 /* Parse a cast-expression that is not the operand of a unary "&". */
25327 cp_parser_simple_cast_expression (cp_parser
*parser
)
25329 return cp_parser_cast_expression (parser
, /*address_p=*/false,
25330 /*cast_p=*/false, /*decltype*/false, NULL
);
25333 /* Parse a functional cast to TYPE. Returns an expression
25334 representing the cast. */
25337 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
25339 vec
<tree
, va_gc
> *vec
;
25340 tree expression_list
;
25345 type
= error_mark_node
;
25347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25349 cp_lexer_set_source_position (parser
->lexer
);
25350 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25351 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
25352 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
25353 if (TREE_CODE (type
) == TYPE_DECL
)
25354 type
= TREE_TYPE (type
);
25355 return finish_compound_literal (type
, expression_list
,
25356 tf_warning_or_error
);
25360 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
25362 /*allow_expansion_p=*/true,
25363 /*non_constant_p=*/NULL
);
25365 expression_list
= error_mark_node
;
25368 expression_list
= build_tree_list_vec (vec
);
25369 release_tree_vector (vec
);
25372 cast
= build_functional_cast (type
, expression_list
,
25373 tf_warning_or_error
);
25374 /* [expr.const]/1: In an integral constant expression "only type
25375 conversions to integral or enumeration type can be used". */
25376 if (TREE_CODE (type
) == TYPE_DECL
)
25377 type
= TREE_TYPE (type
);
25378 if (cast
!= error_mark_node
25379 && !cast_valid_in_integral_constant_expression_p (type
)
25380 && cp_parser_non_integral_constant_expression (parser
,
25382 return error_mark_node
;
25386 /* Save the tokens that make up the body of a member function defined
25387 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25388 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25389 specifiers applied to the declaration. Returns the FUNCTION_DECL
25390 for the member function. */
25393 cp_parser_save_member_function_body (cp_parser
* parser
,
25394 cp_decl_specifier_seq
*decl_specifiers
,
25395 cp_declarator
*declarator
,
25402 /* Create the FUNCTION_DECL. */
25403 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
25404 cp_finalize_omp_declare_simd (parser
, fn
);
25405 cp_finalize_oacc_routine (parser
, fn
, true);
25406 /* If something went badly wrong, bail out now. */
25407 if (fn
== error_mark_node
)
25409 /* If there's a function-body, skip it. */
25410 if (cp_parser_token_starts_function_definition_p
25411 (cp_lexer_peek_token (parser
->lexer
)))
25412 cp_parser_skip_to_end_of_block_or_statement (parser
);
25413 return error_mark_node
;
25416 /* Remember it, if there default args to post process. */
25417 cp_parser_save_default_args (parser
, fn
);
25419 /* Save away the tokens that make up the body of the
25421 first
= parser
->lexer
->next_token
;
25422 /* Handle function try blocks. */
25423 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
25424 cp_lexer_consume_token (parser
->lexer
);
25425 /* We can have braced-init-list mem-initializers before the fn body. */
25426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
25428 cp_lexer_consume_token (parser
->lexer
);
25429 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
25431 /* cache_group will stop after an un-nested { } pair, too. */
25432 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
25435 /* variadic mem-inits have ... after the ')'. */
25436 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25437 cp_lexer_consume_token (parser
->lexer
);
25440 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25441 /* Handle function try blocks. */
25442 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
25443 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25444 last
= parser
->lexer
->next_token
;
25446 /* Save away the inline definition; we will process it when the
25447 class is complete. */
25448 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
25449 DECL_PENDING_INLINE_P (fn
) = 1;
25451 /* We need to know that this was defined in the class, so that
25452 friend templates are handled correctly. */
25453 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
25455 /* Add FN to the queue of functions to be parsed later. */
25456 vec_safe_push (unparsed_funs_with_definitions
, fn
);
25461 /* Save the tokens that make up the in-class initializer for a non-static
25462 data member. Returns a DEFAULT_ARG. */
25465 cp_parser_save_nsdmi (cp_parser
* parser
)
25467 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
25470 /* Parse a template-argument-list, as well as the trailing ">" (but
25471 not the opening "<"). See cp_parser_template_argument_list for the
25475 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
25479 tree saved_qualifying_scope
;
25480 tree saved_object_scope
;
25481 bool saved_greater_than_is_operator_p
;
25482 int saved_unevaluated_operand
;
25483 int saved_inhibit_evaluation_warnings
;
25487 When parsing a template-id, the first non-nested `>' is taken as
25488 the end of the template-argument-list rather than a greater-than
25490 saved_greater_than_is_operator_p
25491 = parser
->greater_than_is_operator_p
;
25492 parser
->greater_than_is_operator_p
= false;
25493 /* Parsing the argument list may modify SCOPE, so we save it
25495 saved_scope
= parser
->scope
;
25496 saved_qualifying_scope
= parser
->qualifying_scope
;
25497 saved_object_scope
= parser
->object_scope
;
25498 /* We need to evaluate the template arguments, even though this
25499 template-id may be nested within a "sizeof". */
25500 saved_unevaluated_operand
= cp_unevaluated_operand
;
25501 cp_unevaluated_operand
= 0;
25502 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
25503 c_inhibit_evaluation_warnings
= 0;
25504 /* Parse the template-argument-list itself. */
25505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
25506 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
25507 arguments
= NULL_TREE
;
25509 arguments
= cp_parser_template_argument_list (parser
);
25510 /* Look for the `>' that ends the template-argument-list. If we find
25511 a '>>' instead, it's probably just a typo. */
25512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
25514 if (cxx_dialect
!= cxx98
)
25516 /* In C++0x, a `>>' in a template argument list or cast
25517 expression is considered to be two separate `>'
25518 tokens. So, change the current token to a `>', but don't
25519 consume it: it will be consumed later when the outer
25520 template argument list (or cast expression) is parsed.
25521 Note that this replacement of `>' for `>>' is necessary
25522 even if we are parsing tentatively: in the tentative
25523 case, after calling
25524 cp_parser_enclosed_template_argument_list we will always
25525 throw away all of the template arguments and the first
25526 closing `>', either because the template argument list
25527 was erroneous or because we are replacing those tokens
25528 with a CPP_TEMPLATE_ID token. The second `>' (which will
25529 not have been thrown away) is needed either to close an
25530 outer template argument list or to complete a new-style
25532 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25533 token
->type
= CPP_GREATER
;
25535 else if (!saved_greater_than_is_operator_p
)
25537 /* If we're in a nested template argument list, the '>>' has
25538 to be a typo for '> >'. We emit the error message, but we
25539 continue parsing and we push a '>' as next token, so that
25540 the argument list will be parsed correctly. Note that the
25541 global source location is still on the token before the
25542 '>>', so we need to say explicitly where we want it. */
25543 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25544 error_at (token
->location
, "%<>>%> should be %<> >%> "
25545 "within a nested template argument list");
25547 token
->type
= CPP_GREATER
;
25551 /* If this is not a nested template argument list, the '>>'
25552 is a typo for '>'. Emit an error message and continue.
25553 Same deal about the token location, but here we can get it
25554 right by consuming the '>>' before issuing the diagnostic. */
25555 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
25556 error_at (token
->location
,
25557 "spurious %<>>%>, use %<>%> to terminate "
25558 "a template argument list");
25562 cp_parser_skip_to_end_of_template_parameter_list (parser
);
25563 /* The `>' token might be a greater-than operator again now. */
25564 parser
->greater_than_is_operator_p
25565 = saved_greater_than_is_operator_p
;
25566 /* Restore the SAVED_SCOPE. */
25567 parser
->scope
= saved_scope
;
25568 parser
->qualifying_scope
= saved_qualifying_scope
;
25569 parser
->object_scope
= saved_object_scope
;
25570 cp_unevaluated_operand
= saved_unevaluated_operand
;
25571 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
25576 /* MEMBER_FUNCTION is a member function, or a friend. If default
25577 arguments, or the body of the function have not yet been parsed,
25581 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
25583 timevar_push (TV_PARSE_INMETH
);
25584 /* If this member is a template, get the underlying
25586 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
25587 member_function
= DECL_TEMPLATE_RESULT (member_function
);
25589 /* There should not be any class definitions in progress at this
25590 point; the bodies of members are only parsed outside of all class
25592 gcc_assert (parser
->num_classes_being_defined
== 0);
25593 /* While we're parsing the member functions we might encounter more
25594 classes. We want to handle them right away, but we don't want
25595 them getting mixed up with functions that are currently in the
25597 push_unparsed_function_queues (parser
);
25599 /* Make sure that any template parameters are in scope. */
25600 maybe_begin_member_template_processing (member_function
);
25602 /* If the body of the function has not yet been parsed, parse it
25604 if (DECL_PENDING_INLINE_P (member_function
))
25606 tree function_scope
;
25607 cp_token_cache
*tokens
;
25609 /* The function is no longer pending; we are processing it. */
25610 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
25611 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
25612 DECL_PENDING_INLINE_P (member_function
) = 0;
25614 /* If this is a local class, enter the scope of the containing
25616 function_scope
= current_function_decl
;
25617 if (function_scope
)
25618 push_function_context ();
25620 /* Push the body of the function onto the lexer stack. */
25621 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25623 /* Let the front end know that we going to be defining this
25625 start_preparsed_function (member_function
, NULL_TREE
,
25626 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
25628 /* Don't do access checking if it is a templated function. */
25629 if (processing_template_decl
)
25630 push_deferring_access_checks (dk_no_check
);
25632 /* #pragma omp declare reduction needs special parsing. */
25633 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
25635 parser
->lexer
->in_pragma
= true;
25636 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
25637 finish_function (/*inline*/2);
25638 cp_check_omp_declare_reduction (member_function
);
25641 /* Now, parse the body of the function. */
25642 cp_parser_function_definition_after_declarator (parser
,
25643 /*inline_p=*/true);
25645 if (processing_template_decl
)
25646 pop_deferring_access_checks ();
25648 /* Leave the scope of the containing function. */
25649 if (function_scope
)
25650 pop_function_context ();
25651 cp_parser_pop_lexer (parser
);
25654 /* Remove any template parameters from the symbol table. */
25655 maybe_end_member_template_processing ();
25657 /* Restore the queue. */
25658 pop_unparsed_function_queues (parser
);
25659 timevar_pop (TV_PARSE_INMETH
);
25662 /* If DECL contains any default args, remember it on the unparsed
25663 functions queue. */
25666 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
25670 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
25672 probe
= TREE_CHAIN (probe
))
25673 if (TREE_PURPOSE (probe
))
25675 cp_default_arg_entry entry
= {current_class_type
, decl
};
25676 vec_safe_push (unparsed_funs_with_default_args
, entry
);
25681 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25682 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25683 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25684 from the parameter-type-list. */
25687 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
25688 tree default_arg
, tree parmtype
)
25690 cp_token_cache
*tokens
;
25694 if (default_arg
== error_mark_node
)
25695 return error_mark_node
;
25697 /* Push the saved tokens for the default argument onto the parser's
25699 tokens
= DEFARG_TOKENS (default_arg
);
25700 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25702 start_lambda_scope (decl
);
25704 /* Parse the default argument. */
25705 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
25706 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
25707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
25709 finish_lambda_scope ();
25711 if (parsed_arg
== error_mark_node
)
25712 cp_parser_skip_to_end_of_statement (parser
);
25714 if (!processing_template_decl
)
25716 /* In a non-template class, check conversions now. In a template,
25717 we'll wait and instantiate these as needed. */
25718 if (TREE_CODE (decl
) == PARM_DECL
)
25719 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
25720 tf_warning_or_error
);
25722 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
25725 /* If the token stream has not been completely used up, then
25726 there was extra junk after the end of the default
25728 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
25730 if (TREE_CODE (decl
) == PARM_DECL
)
25731 cp_parser_error (parser
, "expected %<,%>");
25733 cp_parser_error (parser
, "expected %<;%>");
25736 /* Revert to the main lexer. */
25737 cp_parser_pop_lexer (parser
);
25742 /* FIELD is a non-static data member with an initializer which we saved for
25743 later; parse it now. */
25746 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
25750 maybe_begin_member_template_processing (field
);
25752 push_unparsed_function_queues (parser
);
25753 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
25754 DECL_INITIAL (field
),
25756 pop_unparsed_function_queues (parser
);
25758 maybe_end_member_template_processing ();
25760 DECL_INITIAL (field
) = def
;
25763 /* FN is a FUNCTION_DECL which may contains a parameter with an
25764 unparsed DEFAULT_ARG. Parse the default args now. This function
25765 assumes that the current scope is the scope in which the default
25766 argument should be processed. */
25769 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
25771 bool saved_local_variables_forbidden_p
;
25772 tree parm
, parmdecl
;
25774 /* While we're parsing the default args, we might (due to the
25775 statement expression extension) encounter more classes. We want
25776 to handle them right away, but we don't want them getting mixed
25777 up with default args that are currently in the queue. */
25778 push_unparsed_function_queues (parser
);
25780 /* Local variable names (and the `this' keyword) may not appear
25781 in a default argument. */
25782 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
25783 parser
->local_variables_forbidden_p
= true;
25785 push_defarg_context (fn
);
25787 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
25788 parmdecl
= DECL_ARGUMENTS (fn
);
25789 parm
&& parm
!= void_list_node
;
25790 parm
= TREE_CHAIN (parm
),
25791 parmdecl
= DECL_CHAIN (parmdecl
))
25793 tree default_arg
= TREE_PURPOSE (parm
);
25795 vec
<tree
, va_gc
> *insts
;
25802 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
25803 /* This can happen for a friend declaration for a function
25804 already declared with default arguments. */
25808 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
25810 TREE_VALUE (parm
));
25811 if (parsed_arg
== error_mark_node
)
25816 TREE_PURPOSE (parm
) = parsed_arg
;
25818 /* Update any instantiations we've already created. */
25819 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
25820 vec_safe_iterate (insts
, ix
, ©
); ix
++)
25821 TREE_PURPOSE (copy
) = parsed_arg
;
25824 pop_defarg_context ();
25826 /* Make sure no default arg is missing. */
25827 check_default_args (fn
);
25829 /* Restore the state of local_variables_forbidden_p. */
25830 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
25832 /* Restore the queue. */
25833 pop_unparsed_function_queues (parser
);
25836 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25838 sizeof ... ( identifier )
25840 where the 'sizeof' token has already been consumed. */
25843 cp_parser_sizeof_pack (cp_parser
*parser
)
25845 /* Consume the `...'. */
25846 cp_lexer_consume_token (parser
->lexer
);
25847 maybe_warn_variadic_templates ();
25849 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
25851 cp_lexer_consume_token (parser
->lexer
);
25853 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
25854 "%<sizeof...%> argument must be surrounded by parentheses");
25856 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25857 tree name
= cp_parser_identifier (parser
);
25858 if (name
== error_mark_node
)
25859 return error_mark_node
;
25860 /* The name is not qualified. */
25861 parser
->scope
= NULL_TREE
;
25862 parser
->qualifying_scope
= NULL_TREE
;
25863 parser
->object_scope
= NULL_TREE
;
25864 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
25865 if (expr
== error_mark_node
)
25866 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
25868 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
25869 expr
= TREE_TYPE (expr
);
25870 else if (TREE_CODE (expr
) == CONST_DECL
)
25871 expr
= DECL_INITIAL (expr
);
25872 expr
= make_pack_expansion (expr
);
25873 PACK_EXPANSION_SIZEOF_P (expr
) = true;
25876 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25881 /* Parse the operand of `sizeof' (or a similar operator). Returns
25882 either a TYPE or an expression, depending on the form of the
25883 input. The KEYWORD indicates which kind of expression we have
25887 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
25889 tree expr
= NULL_TREE
;
25890 const char *saved_message
;
25892 bool saved_integral_constant_expression_p
;
25893 bool saved_non_integral_constant_expression_p
;
25895 /* If it's a `...', then we are computing the length of a parameter
25897 if (keyword
== RID_SIZEOF
25898 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25899 return cp_parser_sizeof_pack (parser
);
25901 /* Types cannot be defined in a `sizeof' expression. Save away the
25903 saved_message
= parser
->type_definition_forbidden_message
;
25904 /* And create the new one. */
25905 tmp
= concat ("types may not be defined in %<",
25906 IDENTIFIER_POINTER (ridpointers
[keyword
]),
25907 "%> expressions", NULL
);
25908 parser
->type_definition_forbidden_message
= tmp
;
25910 /* The restrictions on constant-expressions do not apply inside
25911 sizeof expressions. */
25912 saved_integral_constant_expression_p
25913 = parser
->integral_constant_expression_p
;
25914 saved_non_integral_constant_expression_p
25915 = parser
->non_integral_constant_expression_p
;
25916 parser
->integral_constant_expression_p
= false;
25918 /* Do not actually evaluate the expression. */
25919 ++cp_unevaluated_operand
;
25920 ++c_inhibit_evaluation_warnings
;
25921 /* If it's a `(', then we might be looking at the type-id
25923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
25925 tree type
= NULL_TREE
;
25927 /* We can't be sure yet whether we're looking at a type-id or an
25929 cp_parser_parse_tentatively (parser
);
25930 /* Note: as a GNU Extension, compound literals are considered
25931 postfix-expressions as they are in C99, so they are valid
25932 arguments to sizeof. See comment in cp_parser_cast_expression
25934 if (cp_parser_compound_literal_p (parser
))
25935 cp_parser_simulate_error (parser
);
25938 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
25939 parser
->in_type_id_in_expr_p
= true;
25940 /* Look for the type-id. */
25941 type
= cp_parser_type_id (parser
);
25942 /* Look for the closing `)'. */
25943 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25944 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
25947 /* If all went well, then we're done. */
25948 if (cp_parser_parse_definitely (parser
))
25950 cp_decl_specifier_seq decl_specs
;
25952 /* Build a trivial decl-specifier-seq. */
25953 clear_decl_specs (&decl_specs
);
25954 decl_specs
.type
= type
;
25956 /* Call grokdeclarator to figure out what type this is. */
25957 expr
= grokdeclarator (NULL
,
25961 /*attrlist=*/NULL
);
25965 /* If the type-id production did not work out, then we must be
25966 looking at the unary-expression production. */
25968 expr
= cp_parser_unary_expression (parser
);
25970 /* Go back to evaluating expressions. */
25971 --cp_unevaluated_operand
;
25972 --c_inhibit_evaluation_warnings
;
25974 /* Free the message we created. */
25976 /* And restore the old one. */
25977 parser
->type_definition_forbidden_message
= saved_message
;
25978 parser
->integral_constant_expression_p
25979 = saved_integral_constant_expression_p
;
25980 parser
->non_integral_constant_expression_p
25981 = saved_non_integral_constant_expression_p
;
25986 /* If the current declaration has no declarator, return true. */
25989 cp_parser_declares_only_class_p (cp_parser
*parser
)
25991 /* If the next token is a `;' or a `,' then there is no
25993 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
25994 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
25997 /* Update the DECL_SPECS to reflect the storage class indicated by
26001 cp_parser_set_storage_class (cp_parser
*parser
,
26002 cp_decl_specifier_seq
*decl_specs
,
26006 cp_storage_class storage_class
;
26008 if (parser
->in_unbraced_linkage_specification_p
)
26010 error_at (token
->location
, "invalid use of %qD in linkage specification",
26011 ridpointers
[keyword
]);
26014 else if (decl_specs
->storage_class
!= sc_none
)
26016 decl_specs
->conflicting_specifiers_p
= true;
26020 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
26021 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
26022 && decl_specs
->gnu_thread_keyword_p
)
26024 pedwarn (decl_specs
->locations
[ds_thread
], 0,
26025 "%<__thread%> before %qD", ridpointers
[keyword
]);
26031 storage_class
= sc_auto
;
26034 storage_class
= sc_register
;
26037 storage_class
= sc_static
;
26040 storage_class
= sc_extern
;
26043 storage_class
= sc_mutable
;
26046 gcc_unreachable ();
26048 decl_specs
->storage_class
= storage_class
;
26049 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
26051 /* A storage class specifier cannot be applied alongside a typedef
26052 specifier. If there is a typedef specifier present then set
26053 conflicting_specifiers_p which will trigger an error later
26054 on in grokdeclarator. */
26055 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
26056 decl_specs
->conflicting_specifiers_p
= true;
26059 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26060 is true, the type is a class or enum definition. */
26063 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
26066 bool type_definition_p
)
26068 decl_specs
->any_specifiers_p
= true;
26070 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26071 (with, for example, in "typedef int wchar_t;") we remember that
26072 this is what happened. In system headers, we ignore these
26073 declarations so that G++ can work with system headers that are not
26075 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
26076 && !type_definition_p
26077 && (type_spec
== boolean_type_node
26078 || type_spec
== char16_type_node
26079 || type_spec
== char32_type_node
26080 || type_spec
== wchar_type_node
)
26081 && (decl_specs
->type
26082 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
26083 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
26084 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
26085 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
26087 decl_specs
->redefined_builtin_type
= type_spec
;
26088 set_and_check_decl_spec_loc (decl_specs
,
26089 ds_redefined_builtin_type_spec
,
26091 if (!decl_specs
->type
)
26093 decl_specs
->type
= type_spec
;
26094 decl_specs
->type_definition_p
= false;
26095 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
26098 else if (decl_specs
->type
)
26099 decl_specs
->multiple_types_p
= true;
26102 decl_specs
->type
= type_spec
;
26103 decl_specs
->type_definition_p
= type_definition_p
;
26104 decl_specs
->redefined_builtin_type
= NULL_TREE
;
26105 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
26109 /* True iff TOKEN is the GNU keyword __thread. */
26112 token_is__thread (cp_token
*token
)
26114 gcc_assert (token
->keyword
== RID_THREAD
);
26115 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
26118 /* Set the location for a declarator specifier and check if it is
26121 DECL_SPECS is the sequence of declarator specifiers onto which to
26124 DS is the single declarator specifier to set which location is to
26125 be set onto the existing sequence of declarators.
26127 LOCATION is the location for the declarator specifier to
26131 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
26132 cp_decl_spec ds
, cp_token
*token
)
26134 gcc_assert (ds
< ds_last
);
26136 if (decl_specs
== NULL
)
26139 source_location location
= token
->location
;
26141 if (decl_specs
->locations
[ds
] == 0)
26143 decl_specs
->locations
[ds
] = location
;
26144 if (ds
== ds_thread
)
26145 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
26151 if (decl_specs
->locations
[ds_long_long
] != 0)
26152 error_at (location
,
26153 "%<long long long%> is too long for GCC");
26156 decl_specs
->locations
[ds_long_long
] = location
;
26157 pedwarn_cxx98 (location
,
26159 "ISO C++ 1998 does not support %<long long%>");
26162 else if (ds
== ds_thread
)
26164 bool gnu
= token_is__thread (token
);
26165 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
26166 error_at (location
,
26167 "both %<__thread%> and %<thread_local%> specified");
26169 error_at (location
, "duplicate %qD", token
->u
.value
);
26173 static const char *const decl_spec_names
[] = {
26190 error_at (location
,
26191 "duplicate %qs", decl_spec_names
[ds
]);
26196 /* Return true iff the declarator specifier DS is present in the
26197 sequence of declarator specifiers DECL_SPECS. */
26200 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
26203 gcc_assert (ds
< ds_last
);
26205 if (decl_specs
== NULL
)
26208 return decl_specs
->locations
[ds
] != 0;
26211 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26212 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26215 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
26217 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
26220 /* Issue an error message indicating that TOKEN_DESC was expected.
26221 If KEYWORD is true, it indicated this function is called by
26222 cp_parser_require_keword and the required token can only be
26223 a indicated keyword. */
26226 cp_parser_required_error (cp_parser
*parser
,
26227 required_token token_desc
,
26230 switch (token_desc
)
26233 cp_parser_error (parser
, "expected %<new%>");
26236 cp_parser_error (parser
, "expected %<delete%>");
26239 cp_parser_error (parser
, "expected %<return%>");
26242 cp_parser_error (parser
, "expected %<while%>");
26245 cp_parser_error (parser
, "expected %<extern%>");
26247 case RT_STATIC_ASSERT
:
26248 cp_parser_error (parser
, "expected %<static_assert%>");
26251 cp_parser_error (parser
, "expected %<decltype%>");
26254 cp_parser_error (parser
, "expected %<operator%>");
26257 cp_parser_error (parser
, "expected %<class%>");
26260 cp_parser_error (parser
, "expected %<template%>");
26263 cp_parser_error (parser
, "expected %<namespace%>");
26266 cp_parser_error (parser
, "expected %<using%>");
26269 cp_parser_error (parser
, "expected %<asm%>");
26272 cp_parser_error (parser
, "expected %<try%>");
26275 cp_parser_error (parser
, "expected %<catch%>");
26278 cp_parser_error (parser
, "expected %<throw%>");
26281 cp_parser_error (parser
, "expected %<__label__%>");
26284 cp_parser_error (parser
, "expected %<@try%>");
26286 case RT_AT_SYNCHRONIZED
:
26287 cp_parser_error (parser
, "expected %<@synchronized%>");
26290 cp_parser_error (parser
, "expected %<@throw%>");
26292 case RT_TRANSACTION_ATOMIC
:
26293 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
26295 case RT_TRANSACTION_RELAXED
:
26296 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
26303 switch (token_desc
)
26306 cp_parser_error (parser
, "expected %<;%>");
26308 case RT_OPEN_PAREN
:
26309 cp_parser_error (parser
, "expected %<(%>");
26311 case RT_CLOSE_BRACE
:
26312 cp_parser_error (parser
, "expected %<}%>");
26314 case RT_OPEN_BRACE
:
26315 cp_parser_error (parser
, "expected %<{%>");
26317 case RT_CLOSE_SQUARE
:
26318 cp_parser_error (parser
, "expected %<]%>");
26320 case RT_OPEN_SQUARE
:
26321 cp_parser_error (parser
, "expected %<[%>");
26324 cp_parser_error (parser
, "expected %<,%>");
26327 cp_parser_error (parser
, "expected %<::%>");
26330 cp_parser_error (parser
, "expected %<<%>");
26333 cp_parser_error (parser
, "expected %<>%>");
26336 cp_parser_error (parser
, "expected %<=%>");
26339 cp_parser_error (parser
, "expected %<...%>");
26342 cp_parser_error (parser
, "expected %<*%>");
26345 cp_parser_error (parser
, "expected %<~%>");
26348 cp_parser_error (parser
, "expected %<:%>");
26350 case RT_COLON_SCOPE
:
26351 cp_parser_error (parser
, "expected %<:%> or %<::%>");
26353 case RT_CLOSE_PAREN
:
26354 cp_parser_error (parser
, "expected %<)%>");
26356 case RT_COMMA_CLOSE_PAREN
:
26357 cp_parser_error (parser
, "expected %<,%> or %<)%>");
26359 case RT_PRAGMA_EOL
:
26360 cp_parser_error (parser
, "expected end of line");
26363 cp_parser_error (parser
, "expected identifier");
26366 cp_parser_error (parser
, "expected selection-statement");
26368 case RT_INTERATION
:
26369 cp_parser_error (parser
, "expected iteration-statement");
26372 cp_parser_error (parser
, "expected jump-statement");
26375 cp_parser_error (parser
, "expected class-key");
26377 case RT_CLASS_TYPENAME_TEMPLATE
:
26378 cp_parser_error (parser
,
26379 "expected %<class%>, %<typename%>, or %<template%>");
26382 gcc_unreachable ();
26386 gcc_unreachable ();
26391 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26392 issue an error message indicating that TOKEN_DESC was expected.
26394 Returns the token consumed, if the token had the appropriate type.
26395 Otherwise, returns NULL. */
26398 cp_parser_require (cp_parser
* parser
,
26399 enum cpp_ttype type
,
26400 required_token token_desc
)
26402 if (cp_lexer_next_token_is (parser
->lexer
, type
))
26403 return cp_lexer_consume_token (parser
->lexer
);
26406 /* Output the MESSAGE -- unless we're parsing tentatively. */
26407 if (!cp_parser_simulate_error (parser
))
26408 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
26413 /* An error message is produced if the next token is not '>'.
26414 All further tokens are skipped until the desired token is
26415 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26418 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
26420 /* Current level of '< ... >'. */
26421 unsigned level
= 0;
26422 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26423 unsigned nesting_depth
= 0;
26425 /* Are we ready, yet? If not, issue error message. */
26426 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
26429 /* Skip tokens until the desired token is found. */
26432 /* Peek at the next token. */
26433 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
26436 if (!nesting_depth
)
26441 if (cxx_dialect
== cxx98
)
26442 /* C++0x views the `>>' operator as two `>' tokens, but
26445 else if (!nesting_depth
&& level
-- == 0)
26447 /* We've hit a `>>' where the first `>' closes the
26448 template argument list, and the second `>' is
26449 spurious. Just consume the `>>' and stop; we've
26450 already produced at least one error. */
26451 cp_lexer_consume_token (parser
->lexer
);
26454 /* Fall through for C++0x, so we handle the second `>' in
26458 if (!nesting_depth
&& level
-- == 0)
26460 /* We've reached the token we want, consume it and stop. */
26461 cp_lexer_consume_token (parser
->lexer
);
26466 case CPP_OPEN_PAREN
:
26467 case CPP_OPEN_SQUARE
:
26471 case CPP_CLOSE_PAREN
:
26472 case CPP_CLOSE_SQUARE
:
26473 if (nesting_depth
-- == 0)
26478 case CPP_PRAGMA_EOL
:
26479 case CPP_SEMICOLON
:
26480 case CPP_OPEN_BRACE
:
26481 case CPP_CLOSE_BRACE
:
26482 /* The '>' was probably forgotten, don't look further. */
26489 /* Consume this token. */
26490 cp_lexer_consume_token (parser
->lexer
);
26494 /* If the next token is the indicated keyword, consume it. Otherwise,
26495 issue an error message indicating that TOKEN_DESC was expected.
26497 Returns the token consumed, if the token had the appropriate type.
26498 Otherwise, returns NULL. */
26501 cp_parser_require_keyword (cp_parser
* parser
,
26503 required_token token_desc
)
26505 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
26507 if (token
&& token
->keyword
!= keyword
)
26509 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
26516 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26517 function-definition. */
26520 cp_parser_token_starts_function_definition_p (cp_token
* token
)
26522 return (/* An ordinary function-body begins with an `{'. */
26523 token
->type
== CPP_OPEN_BRACE
26524 /* A ctor-initializer begins with a `:'. */
26525 || token
->type
== CPP_COLON
26526 /* A function-try-block begins with `try'. */
26527 || token
->keyword
== RID_TRY
26528 /* A function-transaction-block begins with `__transaction_atomic'
26529 or `__transaction_relaxed'. */
26530 || token
->keyword
== RID_TRANSACTION_ATOMIC
26531 || token
->keyword
== RID_TRANSACTION_RELAXED
26532 /* The named return value extension begins with `return'. */
26533 || token
->keyword
== RID_RETURN
);
26536 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26540 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
26544 token
= cp_lexer_peek_token (parser
->lexer
);
26545 return (token
->type
== CPP_OPEN_BRACE
26546 || (token
->type
== CPP_COLON
26547 && !parser
->colon_doesnt_start_class_def_p
));
26550 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26551 C++0x) ending a template-argument. */
26554 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
26558 token
= cp_lexer_peek_token (parser
->lexer
);
26559 return (token
->type
== CPP_COMMA
26560 || token
->type
== CPP_GREATER
26561 || token
->type
== CPP_ELLIPSIS
26562 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
26565 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26566 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26569 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
26574 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26575 if (token
->type
== CPP_LESS
)
26577 /* Check for the sequence `<::' in the original code. It would be lexed as
26578 `[:', where `[' is a digraph, and there is no whitespace before
26580 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
26583 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
26584 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
26590 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26591 or none_type otherwise. */
26593 static enum tag_types
26594 cp_parser_token_is_class_key (cp_token
* token
)
26596 switch (token
->keyword
)
26601 return record_type
;
26610 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26611 or none_type otherwise or if the token is null. */
26613 static enum tag_types
26614 cp_parser_token_is_type_parameter_key (cp_token
* token
)
26619 switch (token
->keyword
)
26624 return typename_type
;
26631 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26634 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
26636 if (type
== error_mark_node
)
26638 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
26640 if (permerror (input_location
, "%qs tag used in naming %q#T",
26641 class_key
== union_type
? "union"
26642 : class_key
== record_type
? "struct" : "class",
26644 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
26645 "%q#T was previously declared here", type
);
26649 /* Issue an error message if DECL is redeclared with different
26650 access than its original declaration [class.access.spec/3].
26651 This applies to nested classes and nested class templates.
26655 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
26657 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
26660 if ((TREE_PRIVATE (decl
)
26661 != (current_access_specifier
== access_private_node
))
26662 || (TREE_PROTECTED (decl
)
26663 != (current_access_specifier
== access_protected_node
)))
26664 error_at (location
, "%qD redeclared with different access", decl
);
26667 /* Look for the `template' keyword, as a syntactic disambiguator.
26668 Return TRUE iff it is present, in which case it will be
26672 cp_parser_optional_template_keyword (cp_parser
*parser
)
26674 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
26676 /* In C++98 the `template' keyword can only be used within templates;
26677 outside templates the parser can always figure out what is a
26678 template and what is not. In C++11, per the resolution of DR 468,
26679 `template' is allowed in cases where it is not strictly necessary. */
26680 if (!processing_template_decl
26681 && pedantic
&& cxx_dialect
== cxx98
)
26683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26684 pedwarn (token
->location
, OPT_Wpedantic
,
26685 "in C++98 %<template%> (as a disambiguator) is only "
26686 "allowed within templates");
26687 /* If this part of the token stream is rescanned, the same
26688 error message would be generated. So, we purge the token
26689 from the stream. */
26690 cp_lexer_purge_token (parser
->lexer
);
26695 /* Consume the `template' keyword. */
26696 cp_lexer_consume_token (parser
->lexer
);
26703 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26704 set PARSER->SCOPE, and perform other related actions. */
26707 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
26710 struct tree_check
*check_value
;
26711 deferred_access_check
*chk
;
26712 vec
<deferred_access_check
, va_gc
> *checks
;
26714 /* Get the stored value. */
26715 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
26716 /* Perform any access checks that were deferred. */
26717 checks
= check_value
->checks
;
26720 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
26721 perform_or_defer_access_check (chk
->binfo
,
26723 chk
->diag_decl
, tf_warning_or_error
);
26725 /* Set the scope from the stored value. */
26726 parser
->scope
= check_value
->value
;
26727 parser
->qualifying_scope
= check_value
->qualifying_scope
;
26728 parser
->object_scope
= NULL_TREE
;
26731 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26732 encounter the end of a block before what we were looking for. */
26735 cp_parser_cache_group (cp_parser
*parser
,
26736 enum cpp_ttype end
,
26741 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26743 /* Abort a parenthesized expression if we encounter a semicolon. */
26744 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
26745 && token
->type
== CPP_SEMICOLON
)
26747 /* If we've reached the end of the file, stop. */
26748 if (token
->type
== CPP_EOF
26749 || (end
!= CPP_PRAGMA_EOL
26750 && token
->type
== CPP_PRAGMA_EOL
))
26752 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
26753 /* We've hit the end of an enclosing block, so there's been some
26754 kind of syntax error. */
26757 /* Consume the token. */
26758 cp_lexer_consume_token (parser
->lexer
);
26759 /* See if it starts a new group. */
26760 if (token
->type
== CPP_OPEN_BRACE
)
26762 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
26763 /* In theory this should probably check end == '}', but
26764 cp_parser_save_member_function_body needs it to exit
26765 after either '}' or ')' when called with ')'. */
26769 else if (token
->type
== CPP_OPEN_PAREN
)
26771 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
26772 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
26775 else if (token
->type
== CPP_PRAGMA
)
26776 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
26777 else if (token
->type
== end
)
26782 /* Like above, for caching a default argument or NSDMI. Both of these are
26783 terminated by a non-nested comma, but it can be unclear whether or not a
26784 comma is nested in a template argument list unless we do more parsing.
26785 In order to handle this ambiguity, when we encounter a ',' after a '<'
26786 we try to parse what follows as a parameter-declaration-list (in the
26787 case of a default argument) or a member-declarator (in the case of an
26788 NSDMI). If that succeeds, then we stop caching. */
26791 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
26793 unsigned depth
= 0;
26794 int maybe_template_id
= 0;
26795 cp_token
*first_token
;
26797 tree default_argument
;
26799 /* Add tokens until we have processed the entire default
26800 argument. We add the range [first_token, token). */
26801 first_token
= cp_lexer_peek_token (parser
->lexer
);
26802 if (first_token
->type
== CPP_OPEN_BRACE
)
26804 /* For list-initialization, this is straightforward. */
26805 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
26806 token
= cp_lexer_peek_token (parser
->lexer
);
26812 /* Peek at the next token. */
26813 token
= cp_lexer_peek_token (parser
->lexer
);
26814 /* What we do depends on what token we have. */
26815 switch (token
->type
)
26817 /* In valid code, a default argument must be
26818 immediately followed by a `,' `)', or `...'. */
26820 if (depth
== 0 && maybe_template_id
)
26822 /* If we've seen a '<', we might be in a
26823 template-argument-list. Until Core issue 325 is
26824 resolved, we don't know how this situation ought
26825 to be handled, so try to DTRT. We check whether
26826 what comes after the comma is a valid parameter
26827 declaration list. If it is, then the comma ends
26828 the default argument; otherwise the default
26829 argument continues. */
26830 bool error
= false;
26833 /* Set ITALP so cp_parser_parameter_declaration_list
26834 doesn't decide to commit to this parse. */
26835 bool saved_italp
= parser
->in_template_argument_list_p
;
26836 parser
->in_template_argument_list_p
= true;
26838 cp_parser_parse_tentatively (parser
);
26842 /* Parse declarators until we reach a non-comma or
26843 somthing that cannot be an initializer.
26844 Just checking whether we're looking at a single
26845 declarator is insufficient. Consider:
26846 int var = tuple<T,U>::x;
26847 The template parameter 'U' looks exactly like a
26851 int ctor_dtor_or_conv_p
;
26852 cp_lexer_consume_token (parser
->lexer
);
26853 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26854 &ctor_dtor_or_conv_p
,
26855 /*parenthesized_p=*/NULL
,
26857 /*friend_p=*/false);
26858 peek
= cp_lexer_peek_token (parser
->lexer
);
26859 if (cp_parser_error_occurred (parser
))
26862 while (peek
->type
== CPP_COMMA
);
26863 /* If we met an '=' or ';' then the original comma
26864 was the end of the NSDMI. Otherwise assume
26865 we're still in the NSDMI. */
26866 error
= (peek
->type
!= CPP_EQ
26867 && peek
->type
!= CPP_SEMICOLON
);
26871 cp_lexer_consume_token (parser
->lexer
);
26872 begin_scope (sk_function_parms
, NULL_TREE
);
26873 cp_parser_parameter_declaration_list (parser
, &error
);
26874 pop_bindings_and_leave_scope ();
26876 if (!cp_parser_error_occurred (parser
) && !error
)
26878 cp_parser_abort_tentative_parse (parser
);
26880 parser
->in_template_argument_list_p
= saved_italp
;
26883 case CPP_CLOSE_PAREN
:
26885 /* If we run into a non-nested `;', `}', or `]',
26886 then the code is invalid -- but the default
26887 argument is certainly over. */
26888 case CPP_SEMICOLON
:
26889 case CPP_CLOSE_BRACE
:
26890 case CPP_CLOSE_SQUARE
:
26892 /* Handle correctly int n = sizeof ... ( p ); */
26893 && token
->type
!= CPP_ELLIPSIS
)
26895 /* Update DEPTH, if necessary. */
26896 else if (token
->type
== CPP_CLOSE_PAREN
26897 || token
->type
== CPP_CLOSE_BRACE
26898 || token
->type
== CPP_CLOSE_SQUARE
)
26902 case CPP_OPEN_PAREN
:
26903 case CPP_OPEN_SQUARE
:
26904 case CPP_OPEN_BRACE
:
26910 /* This might be the comparison operator, or it might
26911 start a template argument list. */
26912 ++maybe_template_id
;
26916 if (cxx_dialect
== cxx98
)
26918 /* Fall through for C++0x, which treats the `>>'
26919 operator like two `>' tokens in certain
26925 /* This might be an operator, or it might close a
26926 template argument list. But if a previous '<'
26927 started a template argument list, this will have
26928 closed it, so we can't be in one anymore. */
26929 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
26930 if (maybe_template_id
< 0)
26931 maybe_template_id
= 0;
26935 /* If we run out of tokens, issue an error message. */
26937 case CPP_PRAGMA_EOL
:
26938 error_at (token
->location
, "file ends in default argument");
26944 /* In these cases, we should look for template-ids.
26945 For example, if the default argument is
26946 `X<int, double>()', we need to do name lookup to
26947 figure out whether or not `X' is a template; if
26948 so, the `,' does not end the default argument.
26950 That is not yet done. */
26957 /* If we've reached the end, stop. */
26961 /* Add the token to the token block. */
26962 token
= cp_lexer_consume_token (parser
->lexer
);
26965 /* Create a DEFAULT_ARG to represent the unparsed default
26967 default_argument
= make_node (DEFAULT_ARG
);
26968 DEFARG_TOKENS (default_argument
)
26969 = cp_token_cache_new (first_token
, token
);
26970 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
26972 return default_argument
;
26975 /* Begin parsing tentatively. We always save tokens while parsing
26976 tentatively so that if the tentative parsing fails we can restore the
26980 cp_parser_parse_tentatively (cp_parser
* parser
)
26982 /* Enter a new parsing context. */
26983 parser
->context
= cp_parser_context_new (parser
->context
);
26984 /* Begin saving tokens. */
26985 cp_lexer_save_tokens (parser
->lexer
);
26986 /* In order to avoid repetitive access control error messages,
26987 access checks are queued up until we are no longer parsing
26989 push_deferring_access_checks (dk_deferred
);
26992 /* Commit to the currently active tentative parse. */
26995 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
26997 cp_parser_context
*context
;
27000 /* Mark all of the levels as committed. */
27001 lexer
= parser
->lexer
;
27002 for (context
= parser
->context
; context
->next
; context
= context
->next
)
27004 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27006 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27007 while (!cp_lexer_saving_tokens (lexer
))
27008 lexer
= lexer
->next
;
27009 cp_lexer_commit_tokens (lexer
);
27013 /* Commit to the topmost currently active tentative parse.
27015 Note that this function shouldn't be called when there are
27016 irreversible side-effects while in a tentative state. For
27017 example, we shouldn't create a permanent entry in the symbol
27018 table, or issue an error message that might not apply if the
27019 tentative parse is aborted. */
27022 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
27024 cp_parser_context
*context
= parser
->context
;
27025 cp_lexer
*lexer
= parser
->lexer
;
27029 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
27031 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
27033 while (!cp_lexer_saving_tokens (lexer
))
27034 lexer
= lexer
->next
;
27035 cp_lexer_commit_tokens (lexer
);
27039 /* Abort the currently active tentative parse. All consumed tokens
27040 will be rolled back, and no diagnostics will be issued. */
27043 cp_parser_abort_tentative_parse (cp_parser
* parser
)
27045 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
27046 || errorcount
> 0);
27047 cp_parser_simulate_error (parser
);
27048 /* Now, pretend that we want to see if the construct was
27049 successfully parsed. */
27050 cp_parser_parse_definitely (parser
);
27053 /* Stop parsing tentatively. If a parse error has occurred, restore the
27054 token stream. Otherwise, commit to the tokens we have consumed.
27055 Returns true if no error occurred; false otherwise. */
27058 cp_parser_parse_definitely (cp_parser
* parser
)
27060 bool error_occurred
;
27061 cp_parser_context
*context
;
27063 /* Remember whether or not an error occurred, since we are about to
27064 destroy that information. */
27065 error_occurred
= cp_parser_error_occurred (parser
);
27066 /* Remove the topmost context from the stack. */
27067 context
= parser
->context
;
27068 parser
->context
= context
->next
;
27069 /* If no parse errors occurred, commit to the tentative parse. */
27070 if (!error_occurred
)
27072 /* Commit to the tokens read tentatively, unless that was
27074 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
27075 cp_lexer_commit_tokens (parser
->lexer
);
27077 pop_to_parent_deferring_access_checks ();
27079 /* Otherwise, if errors occurred, roll back our state so that things
27080 are just as they were before we began the tentative parse. */
27083 cp_lexer_rollback_tokens (parser
->lexer
);
27084 pop_deferring_access_checks ();
27086 /* Add the context to the front of the free list. */
27087 context
->next
= cp_parser_context_free_list
;
27088 cp_parser_context_free_list
= context
;
27090 return !error_occurred
;
27093 /* Returns true if we are parsing tentatively and are not committed to
27094 this tentative parse. */
27097 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
27099 return (cp_parser_parsing_tentatively (parser
)
27100 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
27103 /* Returns nonzero iff an error has occurred during the most recent
27104 tentative parse. */
27107 cp_parser_error_occurred (cp_parser
* parser
)
27109 return (cp_parser_parsing_tentatively (parser
)
27110 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
27113 /* Returns nonzero if GNU extensions are allowed. */
27116 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
27118 return parser
->allow_gnu_extensions_p
;
27121 /* Objective-C++ Productions */
27124 /* Parse an Objective-C expression, which feeds into a primary-expression
27128 objc-message-expression
27129 objc-string-literal
27130 objc-encode-expression
27131 objc-protocol-expression
27132 objc-selector-expression
27134 Returns a tree representation of the expression. */
27137 cp_parser_objc_expression (cp_parser
* parser
)
27139 /* Try to figure out what kind of declaration is present. */
27140 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27144 case CPP_OPEN_SQUARE
:
27145 return cp_parser_objc_message_expression (parser
);
27147 case CPP_OBJC_STRING
:
27148 kwd
= cp_lexer_consume_token (parser
->lexer
);
27149 return objc_build_string_object (kwd
->u
.value
);
27152 switch (kwd
->keyword
)
27154 case RID_AT_ENCODE
:
27155 return cp_parser_objc_encode_expression (parser
);
27157 case RID_AT_PROTOCOL
:
27158 return cp_parser_objc_protocol_expression (parser
);
27160 case RID_AT_SELECTOR
:
27161 return cp_parser_objc_selector_expression (parser
);
27167 error_at (kwd
->location
,
27168 "misplaced %<@%D%> Objective-C++ construct",
27170 cp_parser_skip_to_end_of_block_or_statement (parser
);
27173 return error_mark_node
;
27176 /* Parse an Objective-C message expression.
27178 objc-message-expression:
27179 [ objc-message-receiver objc-message-args ]
27181 Returns a representation of an Objective-C message. */
27184 cp_parser_objc_message_expression (cp_parser
* parser
)
27186 tree receiver
, messageargs
;
27188 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
27189 receiver
= cp_parser_objc_message_receiver (parser
);
27190 messageargs
= cp_parser_objc_message_args (parser
);
27191 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27193 return objc_build_message_expr (receiver
, messageargs
);
27196 /* Parse an objc-message-receiver.
27198 objc-message-receiver:
27200 simple-type-specifier
27202 Returns a representation of the type or expression. */
27205 cp_parser_objc_message_receiver (cp_parser
* parser
)
27209 /* An Objective-C message receiver may be either (1) a type
27210 or (2) an expression. */
27211 cp_parser_parse_tentatively (parser
);
27212 rcv
= cp_parser_expression (parser
);
27214 /* If that worked out, fine. */
27215 if (cp_parser_parse_definitely (parser
))
27218 cp_parser_parse_tentatively (parser
);
27219 rcv
= cp_parser_simple_type_specifier (parser
,
27220 /*decl_specs=*/NULL
,
27221 CP_PARSER_FLAGS_NONE
);
27223 if (cp_parser_parse_definitely (parser
))
27224 return objc_get_class_reference (rcv
);
27226 cp_parser_error (parser
, "objective-c++ message receiver expected");
27227 return error_mark_node
;
27230 /* Parse the arguments and selectors comprising an Objective-C message.
27235 objc-selector-args , objc-comma-args
27237 objc-selector-args:
27238 objc-selector [opt] : assignment-expression
27239 objc-selector-args objc-selector [opt] : assignment-expression
27242 assignment-expression
27243 objc-comma-args , assignment-expression
27245 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27246 selector arguments and TREE_VALUE containing a list of comma
27250 cp_parser_objc_message_args (cp_parser
* parser
)
27252 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
27253 bool maybe_unary_selector_p
= true;
27254 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27256 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
27258 tree selector
= NULL_TREE
, arg
;
27260 if (token
->type
!= CPP_COLON
)
27261 selector
= cp_parser_objc_selector (parser
);
27263 /* Detect if we have a unary selector. */
27264 if (maybe_unary_selector_p
27265 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27266 return build_tree_list (selector
, NULL_TREE
);
27268 maybe_unary_selector_p
= false;
27269 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27270 arg
= cp_parser_assignment_expression (parser
);
27273 = chainon (sel_args
,
27274 build_tree_list (selector
, arg
));
27276 token
= cp_lexer_peek_token (parser
->lexer
);
27279 /* Handle non-selector arguments, if any. */
27280 while (token
->type
== CPP_COMMA
)
27284 cp_lexer_consume_token (parser
->lexer
);
27285 arg
= cp_parser_assignment_expression (parser
);
27288 = chainon (addl_args
,
27289 build_tree_list (NULL_TREE
, arg
));
27291 token
= cp_lexer_peek_token (parser
->lexer
);
27294 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
27296 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
27297 return build_tree_list (error_mark_node
, error_mark_node
);
27300 return build_tree_list (sel_args
, addl_args
);
27303 /* Parse an Objective-C encode expression.
27305 objc-encode-expression:
27306 @encode objc-typename
27308 Returns an encoded representation of the type argument. */
27311 cp_parser_objc_encode_expression (cp_parser
* parser
)
27316 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
27317 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27318 token
= cp_lexer_peek_token (parser
->lexer
);
27319 type
= complete_type (cp_parser_type_id (parser
));
27320 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27324 error_at (token
->location
,
27325 "%<@encode%> must specify a type as an argument");
27326 return error_mark_node
;
27329 /* This happens if we find @encode(T) (where T is a template
27330 typename or something dependent on a template typename) when
27331 parsing a template. In that case, we can't compile it
27332 immediately, but we rather create an AT_ENCODE_EXPR which will
27333 need to be instantiated when the template is used.
27335 if (dependent_type_p (type
))
27337 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
27338 TREE_READONLY (value
) = 1;
27342 return objc_build_encode_expr (type
);
27345 /* Parse an Objective-C @defs expression. */
27348 cp_parser_objc_defs_expression (cp_parser
*parser
)
27352 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
27353 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27354 name
= cp_parser_identifier (parser
);
27355 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27357 return objc_get_class_ivars (name
);
27360 /* Parse an Objective-C protocol expression.
27362 objc-protocol-expression:
27363 @protocol ( identifier )
27365 Returns a representation of the protocol expression. */
27368 cp_parser_objc_protocol_expression (cp_parser
* parser
)
27372 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
27373 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27374 proto
= cp_parser_identifier (parser
);
27375 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27377 return objc_build_protocol_expr (proto
);
27380 /* Parse an Objective-C selector expression.
27382 objc-selector-expression:
27383 @selector ( objc-method-signature )
27385 objc-method-signature:
27391 objc-selector-seq objc-selector :
27393 Returns a representation of the method selector. */
27396 cp_parser_objc_selector_expression (cp_parser
* parser
)
27398 tree sel_seq
= NULL_TREE
;
27399 bool maybe_unary_selector_p
= true;
27401 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27403 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
27404 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27405 token
= cp_lexer_peek_token (parser
->lexer
);
27407 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
27408 || token
->type
== CPP_SCOPE
)
27410 tree selector
= NULL_TREE
;
27412 if (token
->type
!= CPP_COLON
27413 || token
->type
== CPP_SCOPE
)
27414 selector
= cp_parser_objc_selector (parser
);
27416 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
27417 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
27419 /* Detect if we have a unary selector. */
27420 if (maybe_unary_selector_p
)
27422 sel_seq
= selector
;
27423 goto finish_selector
;
27427 cp_parser_error (parser
, "expected %<:%>");
27430 maybe_unary_selector_p
= false;
27431 token
= cp_lexer_consume_token (parser
->lexer
);
27433 if (token
->type
== CPP_SCOPE
)
27436 = chainon (sel_seq
,
27437 build_tree_list (selector
, NULL_TREE
));
27439 = chainon (sel_seq
,
27440 build_tree_list (NULL_TREE
, NULL_TREE
));
27444 = chainon (sel_seq
,
27445 build_tree_list (selector
, NULL_TREE
));
27447 token
= cp_lexer_peek_token (parser
->lexer
);
27451 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27453 return objc_build_selector_expr (loc
, sel_seq
);
27456 /* Parse a list of identifiers.
27458 objc-identifier-list:
27460 objc-identifier-list , identifier
27462 Returns a TREE_LIST of identifier nodes. */
27465 cp_parser_objc_identifier_list (cp_parser
* parser
)
27471 identifier
= cp_parser_identifier (parser
);
27472 if (identifier
== error_mark_node
)
27473 return error_mark_node
;
27475 list
= build_tree_list (NULL_TREE
, identifier
);
27476 sep
= cp_lexer_peek_token (parser
->lexer
);
27478 while (sep
->type
== CPP_COMMA
)
27480 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27481 identifier
= cp_parser_identifier (parser
);
27482 if (identifier
== error_mark_node
)
27485 list
= chainon (list
, build_tree_list (NULL_TREE
,
27487 sep
= cp_lexer_peek_token (parser
->lexer
);
27493 /* Parse an Objective-C alias declaration.
27495 objc-alias-declaration:
27496 @compatibility_alias identifier identifier ;
27498 This function registers the alias mapping with the Objective-C front end.
27499 It returns nothing. */
27502 cp_parser_objc_alias_declaration (cp_parser
* parser
)
27506 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
27507 alias
= cp_parser_identifier (parser
);
27508 orig
= cp_parser_identifier (parser
);
27509 objc_declare_alias (alias
, orig
);
27510 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27513 /* Parse an Objective-C class forward-declaration.
27515 objc-class-declaration:
27516 @class objc-identifier-list ;
27518 The function registers the forward declarations with the Objective-C
27519 front end. It returns nothing. */
27522 cp_parser_objc_class_declaration (cp_parser
* parser
)
27524 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
27529 id
= cp_parser_identifier (parser
);
27530 if (id
== error_mark_node
)
27533 objc_declare_class (id
);
27535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27536 cp_lexer_consume_token (parser
->lexer
);
27540 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27543 /* Parse a list of Objective-C protocol references.
27545 objc-protocol-refs-opt:
27546 objc-protocol-refs [opt]
27548 objc-protocol-refs:
27549 < objc-identifier-list >
27551 Returns a TREE_LIST of identifiers, if any. */
27554 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
27556 tree protorefs
= NULL_TREE
;
27558 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
27560 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
27561 protorefs
= cp_parser_objc_identifier_list (parser
);
27562 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
27568 /* Parse a Objective-C visibility specification. */
27571 cp_parser_objc_visibility_spec (cp_parser
* parser
)
27573 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
27575 switch (vis
->keyword
)
27577 case RID_AT_PRIVATE
:
27578 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
27580 case RID_AT_PROTECTED
:
27581 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
27583 case RID_AT_PUBLIC
:
27584 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
27586 case RID_AT_PACKAGE
:
27587 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
27593 /* Eat '@private'/'@protected'/'@public'. */
27594 cp_lexer_consume_token (parser
->lexer
);
27597 /* Parse an Objective-C method type. Return 'true' if it is a class
27598 (+) method, and 'false' if it is an instance (-) method. */
27601 cp_parser_objc_method_type (cp_parser
* parser
)
27603 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
27609 /* Parse an Objective-C protocol qualifier. */
27612 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
27614 tree quals
= NULL_TREE
, node
;
27615 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27617 node
= token
->u
.value
;
27619 while (node
&& identifier_p (node
)
27620 && (node
== ridpointers
[(int) RID_IN
]
27621 || node
== ridpointers
[(int) RID_OUT
]
27622 || node
== ridpointers
[(int) RID_INOUT
]
27623 || node
== ridpointers
[(int) RID_BYCOPY
]
27624 || node
== ridpointers
[(int) RID_BYREF
]
27625 || node
== ridpointers
[(int) RID_ONEWAY
]))
27627 quals
= tree_cons (NULL_TREE
, node
, quals
);
27628 cp_lexer_consume_token (parser
->lexer
);
27629 token
= cp_lexer_peek_token (parser
->lexer
);
27630 node
= token
->u
.value
;
27636 /* Parse an Objective-C typename. */
27639 cp_parser_objc_typename (cp_parser
* parser
)
27641 tree type_name
= NULL_TREE
;
27643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27645 tree proto_quals
, cp_type
= NULL_TREE
;
27647 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
27648 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
27650 /* An ObjC type name may consist of just protocol qualifiers, in which
27651 case the type shall default to 'id'. */
27652 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27654 cp_type
= cp_parser_type_id (parser
);
27656 /* If the type could not be parsed, an error has already
27657 been produced. For error recovery, behave as if it had
27658 not been specified, which will use the default type
27660 if (cp_type
== error_mark_node
)
27662 cp_type
= NULL_TREE
;
27663 /* We need to skip to the closing parenthesis as
27664 cp_parser_type_id() does not seem to do it for
27666 cp_parser_skip_to_closing_parenthesis (parser
,
27667 /*recovering=*/true,
27668 /*or_comma=*/false,
27669 /*consume_paren=*/false);
27673 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27674 type_name
= build_tree_list (proto_quals
, cp_type
);
27680 /* Check to see if TYPE refers to an Objective-C selector name. */
27683 cp_parser_objc_selector_p (enum cpp_ttype type
)
27685 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
27686 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
27687 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
27688 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
27689 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
27692 /* Parse an Objective-C selector. */
27695 cp_parser_objc_selector (cp_parser
* parser
)
27697 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
27699 if (!cp_parser_objc_selector_p (token
->type
))
27701 error_at (token
->location
, "invalid Objective-C++ selector name");
27702 return error_mark_node
;
27705 /* C++ operator names are allowed to appear in ObjC selectors. */
27706 switch (token
->type
)
27708 case CPP_AND_AND
: return get_identifier ("and");
27709 case CPP_AND_EQ
: return get_identifier ("and_eq");
27710 case CPP_AND
: return get_identifier ("bitand");
27711 case CPP_OR
: return get_identifier ("bitor");
27712 case CPP_COMPL
: return get_identifier ("compl");
27713 case CPP_NOT
: return get_identifier ("not");
27714 case CPP_NOT_EQ
: return get_identifier ("not_eq");
27715 case CPP_OR_OR
: return get_identifier ("or");
27716 case CPP_OR_EQ
: return get_identifier ("or_eq");
27717 case CPP_XOR
: return get_identifier ("xor");
27718 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
27719 default: return token
->u
.value
;
27723 /* Parse an Objective-C params list. */
27726 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
27728 tree params
= NULL_TREE
;
27729 bool maybe_unary_selector_p
= true;
27730 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27732 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
27734 tree selector
= NULL_TREE
, type_name
, identifier
;
27735 tree parm_attr
= NULL_TREE
;
27737 if (token
->keyword
== RID_ATTRIBUTE
)
27740 if (token
->type
!= CPP_COLON
)
27741 selector
= cp_parser_objc_selector (parser
);
27743 /* Detect if we have a unary selector. */
27744 if (maybe_unary_selector_p
27745 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27747 params
= selector
; /* Might be followed by attributes. */
27751 maybe_unary_selector_p
= false;
27752 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27754 /* Something went quite wrong. There should be a colon
27755 here, but there is not. Stop parsing parameters. */
27758 type_name
= cp_parser_objc_typename (parser
);
27759 /* New ObjC allows attributes on parameters too. */
27760 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
27761 parm_attr
= cp_parser_attributes_opt (parser
);
27762 identifier
= cp_parser_identifier (parser
);
27766 objc_build_keyword_decl (selector
,
27771 token
= cp_lexer_peek_token (parser
->lexer
);
27774 if (params
== NULL_TREE
)
27776 cp_parser_error (parser
, "objective-c++ method declaration is expected");
27777 return error_mark_node
;
27780 /* We allow tail attributes for the method. */
27781 if (token
->keyword
== RID_ATTRIBUTE
)
27783 *attributes
= cp_parser_attributes_opt (parser
);
27784 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27785 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27787 cp_parser_error (parser
,
27788 "method attributes must be specified at the end");
27789 return error_mark_node
;
27792 if (params
== NULL_TREE
)
27794 cp_parser_error (parser
, "objective-c++ method declaration is expected");
27795 return error_mark_node
;
27800 /* Parse the non-keyword Objective-C params. */
27803 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
27806 tree params
= make_node (TREE_LIST
);
27807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27808 *ellipsisp
= false; /* Initially, assume no ellipsis. */
27810 while (token
->type
== CPP_COMMA
)
27812 cp_parameter_declarator
*parmdecl
;
27815 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27816 token
= cp_lexer_peek_token (parser
->lexer
);
27818 if (token
->type
== CPP_ELLIPSIS
)
27820 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
27822 token
= cp_lexer_peek_token (parser
->lexer
);
27826 /* TODO: parse attributes for tail parameters. */
27827 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
27828 parm
= grokdeclarator (parmdecl
->declarator
,
27829 &parmdecl
->decl_specifiers
,
27830 PARM
, /*initialized=*/0,
27831 /*attrlist=*/NULL
);
27833 chainon (params
, build_tree_list (NULL_TREE
, parm
));
27834 token
= cp_lexer_peek_token (parser
->lexer
);
27837 /* We allow tail attributes for the method. */
27838 if (token
->keyword
== RID_ATTRIBUTE
)
27840 if (*attributes
== NULL_TREE
)
27842 *attributes
= cp_parser_attributes_opt (parser
);
27843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
27844 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27848 /* We have an error, but parse the attributes, so that we can
27850 *attributes
= cp_parser_attributes_opt (parser
);
27852 cp_parser_error (parser
,
27853 "method attributes must be specified at the end");
27854 return error_mark_node
;
27860 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27863 cp_parser_objc_interstitial_code (cp_parser
* parser
)
27865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27867 /* If the next token is `extern' and the following token is a string
27868 literal, then we have a linkage specification. */
27869 if (token
->keyword
== RID_EXTERN
27870 && cp_parser_is_pure_string_literal
27871 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
27872 cp_parser_linkage_specification (parser
);
27873 /* Handle #pragma, if any. */
27874 else if (token
->type
== CPP_PRAGMA
)
27875 cp_parser_pragma (parser
, pragma_objc_icode
);
27876 /* Allow stray semicolons. */
27877 else if (token
->type
== CPP_SEMICOLON
)
27878 cp_lexer_consume_token (parser
->lexer
);
27879 /* Mark methods as optional or required, when building protocols. */
27880 else if (token
->keyword
== RID_AT_OPTIONAL
)
27882 cp_lexer_consume_token (parser
->lexer
);
27883 objc_set_method_opt (true);
27885 else if (token
->keyword
== RID_AT_REQUIRED
)
27887 cp_lexer_consume_token (parser
->lexer
);
27888 objc_set_method_opt (false);
27890 else if (token
->keyword
== RID_NAMESPACE
)
27891 cp_parser_namespace_definition (parser
);
27892 /* Other stray characters must generate errors. */
27893 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
27895 cp_lexer_consume_token (parser
->lexer
);
27896 error ("stray %qs between Objective-C++ methods",
27897 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
27899 /* Finally, try to parse a block-declaration, or a function-definition. */
27901 cp_parser_block_declaration (parser
, /*statement_p=*/false);
27904 /* Parse a method signature. */
27907 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
27909 tree rettype
, kwdparms
, optparms
;
27910 bool ellipsis
= false;
27911 bool is_class_method
;
27913 is_class_method
= cp_parser_objc_method_type (parser
);
27914 rettype
= cp_parser_objc_typename (parser
);
27915 *attributes
= NULL_TREE
;
27916 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
27917 if (kwdparms
== error_mark_node
)
27918 return error_mark_node
;
27919 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
27920 if (optparms
== error_mark_node
)
27921 return error_mark_node
;
27923 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
27927 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
27930 cp_lexer_save_tokens (parser
->lexer
);
27931 tattr
= cp_parser_attributes_opt (parser
);
27932 gcc_assert (tattr
) ;
27934 /* If the attributes are followed by a method introducer, this is not allowed.
27935 Dump the attributes and flag the situation. */
27936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
27937 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
27940 /* Otherwise, the attributes introduce some interstitial code, possibly so
27941 rewind to allow that check. */
27942 cp_lexer_rollback_tokens (parser
->lexer
);
27946 /* Parse an Objective-C method prototype list. */
27949 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
27951 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27953 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
27955 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
27957 tree attributes
, sig
;
27958 bool is_class_method
;
27959 if (token
->type
== CPP_PLUS
)
27960 is_class_method
= true;
27962 is_class_method
= false;
27963 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
27964 if (sig
== error_mark_node
)
27966 cp_parser_skip_to_end_of_block_or_statement (parser
);
27967 token
= cp_lexer_peek_token (parser
->lexer
);
27970 objc_add_method_declaration (is_class_method
, sig
, attributes
);
27971 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27973 else if (token
->keyword
== RID_AT_PROPERTY
)
27974 cp_parser_objc_at_property_declaration (parser
);
27975 else if (token
->keyword
== RID_ATTRIBUTE
27976 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
27977 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27979 "prefix attributes are ignored for methods");
27981 /* Allow for interspersed non-ObjC++ code. */
27982 cp_parser_objc_interstitial_code (parser
);
27984 token
= cp_lexer_peek_token (parser
->lexer
);
27987 if (token
->type
!= CPP_EOF
)
27988 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
27990 cp_parser_error (parser
, "expected %<@end%>");
27992 objc_finish_interface ();
27995 /* Parse an Objective-C method definition list. */
27998 cp_parser_objc_method_definition_list (cp_parser
* parser
)
28000 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28002 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28006 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
28009 tree sig
, attribute
;
28010 bool is_class_method
;
28011 if (token
->type
== CPP_PLUS
)
28012 is_class_method
= true;
28014 is_class_method
= false;
28015 push_deferring_access_checks (dk_deferred
);
28016 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
28017 if (sig
== error_mark_node
)
28019 cp_parser_skip_to_end_of_block_or_statement (parser
);
28020 token
= cp_lexer_peek_token (parser
->lexer
);
28023 objc_start_method_definition (is_class_method
, sig
, attribute
,
28026 /* For historical reasons, we accept an optional semicolon. */
28027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28028 cp_lexer_consume_token (parser
->lexer
);
28030 ptk
= cp_lexer_peek_token (parser
->lexer
);
28031 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
28032 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
28034 perform_deferred_access_checks (tf_warning_or_error
);
28035 stop_deferring_access_checks ();
28036 meth
= cp_parser_function_definition_after_declarator (parser
,
28038 pop_deferring_access_checks ();
28039 objc_finish_method_definition (meth
);
28042 /* The following case will be removed once @synthesize is
28043 completely implemented. */
28044 else if (token
->keyword
== RID_AT_PROPERTY
)
28045 cp_parser_objc_at_property_declaration (parser
);
28046 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
28047 cp_parser_objc_at_synthesize_declaration (parser
);
28048 else if (token
->keyword
== RID_AT_DYNAMIC
)
28049 cp_parser_objc_at_dynamic_declaration (parser
);
28050 else if (token
->keyword
== RID_ATTRIBUTE
28051 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
28052 warning_at (token
->location
, OPT_Wattributes
,
28053 "prefix attributes are ignored for methods");
28055 /* Allow for interspersed non-ObjC++ code. */
28056 cp_parser_objc_interstitial_code (parser
);
28058 token
= cp_lexer_peek_token (parser
->lexer
);
28061 if (token
->type
!= CPP_EOF
)
28062 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28064 cp_parser_error (parser
, "expected %<@end%>");
28066 objc_finish_implementation ();
28069 /* Parse Objective-C ivars. */
28072 cp_parser_objc_class_ivars (cp_parser
* parser
)
28074 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28076 if (token
->type
!= CPP_OPEN_BRACE
)
28077 return; /* No ivars specified. */
28079 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
28080 token
= cp_lexer_peek_token (parser
->lexer
);
28082 while (token
->type
!= CPP_CLOSE_BRACE
28083 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28085 cp_decl_specifier_seq declspecs
;
28086 int decl_class_or_enum_p
;
28087 tree prefix_attributes
;
28089 cp_parser_objc_visibility_spec (parser
);
28091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
28094 cp_parser_decl_specifier_seq (parser
,
28095 CP_PARSER_FLAGS_OPTIONAL
,
28097 &decl_class_or_enum_p
);
28099 /* auto, register, static, extern, mutable. */
28100 if (declspecs
.storage_class
!= sc_none
)
28102 cp_parser_error (parser
, "invalid type for instance variable");
28103 declspecs
.storage_class
= sc_none
;
28106 /* thread_local. */
28107 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
28109 cp_parser_error (parser
, "invalid type for instance variable");
28110 declspecs
.locations
[ds_thread
] = 0;
28114 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
28116 cp_parser_error (parser
, "invalid type for instance variable");
28117 declspecs
.locations
[ds_typedef
] = 0;
28120 prefix_attributes
= declspecs
.attributes
;
28121 declspecs
.attributes
= NULL_TREE
;
28123 /* Keep going until we hit the `;' at the end of the
28125 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28127 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
28128 cp_declarator
*declarator
= NULL
;
28129 int ctor_dtor_or_conv_p
;
28131 /* Check for a (possibly unnamed) bitfield declaration. */
28132 token
= cp_lexer_peek_token (parser
->lexer
);
28133 if (token
->type
== CPP_COLON
)
28136 if (token
->type
== CPP_NAME
28137 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
28140 /* Get the name of the bitfield. */
28141 declarator
= make_id_declarator (NULL_TREE
,
28142 cp_parser_identifier (parser
),
28146 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28147 /* Get the width of the bitfield. */
28149 = cp_parser_constant_expression (parser
);
28153 /* Parse the declarator. */
28155 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28156 &ctor_dtor_or_conv_p
,
28157 /*parenthesized_p=*/NULL
,
28158 /*member_p=*/false,
28159 /*friend_p=*/false);
28162 /* Look for attributes that apply to the ivar. */
28163 attributes
= cp_parser_attributes_opt (parser
);
28164 /* Remember which attributes are prefix attributes and
28166 first_attribute
= attributes
;
28167 /* Combine the attributes. */
28168 attributes
= chainon (prefix_attributes
, attributes
);
28171 /* Create the bitfield declaration. */
28172 decl
= grokbitfield (declarator
, &declspecs
,
28176 decl
= grokfield (declarator
, &declspecs
,
28177 NULL_TREE
, /*init_const_expr_p=*/false,
28178 NULL_TREE
, attributes
);
28180 /* Add the instance variable. */
28181 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
28182 objc_add_instance_variable (decl
);
28184 /* Reset PREFIX_ATTRIBUTES. */
28185 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
28186 attributes
= TREE_CHAIN (attributes
);
28188 TREE_CHAIN (attributes
) = NULL_TREE
;
28190 token
= cp_lexer_peek_token (parser
->lexer
);
28192 if (token
->type
== CPP_COMMA
)
28194 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28200 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28201 token
= cp_lexer_peek_token (parser
->lexer
);
28204 if (token
->keyword
== RID_AT_END
)
28205 cp_parser_error (parser
, "expected %<}%>");
28207 /* Do not consume the RID_AT_END, so it will be read again as terminating
28208 the @interface of @implementation. */
28209 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
28210 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
28212 /* For historical reasons, we accept an optional semicolon. */
28213 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28214 cp_lexer_consume_token (parser
->lexer
);
28217 /* Parse an Objective-C protocol declaration. */
28220 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
28222 tree proto
, protorefs
;
28225 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
28226 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
28228 tok
= cp_lexer_peek_token (parser
->lexer
);
28229 error_at (tok
->location
, "identifier expected after %<@protocol%>");
28230 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28234 /* See if we have a forward declaration or a definition. */
28235 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28237 /* Try a forward declaration first. */
28238 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
28244 id
= cp_parser_identifier (parser
);
28245 if (id
== error_mark_node
)
28248 objc_declare_protocol (id
, attributes
);
28250 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28251 cp_lexer_consume_token (parser
->lexer
);
28255 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28258 /* Ok, we got a full-fledged definition (or at least should). */
28261 proto
= cp_parser_identifier (parser
);
28262 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
28263 objc_start_protocol (proto
, protorefs
, attributes
);
28264 cp_parser_objc_method_prototype_list (parser
);
28268 /* Parse an Objective-C superclass or category. */
28271 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
28274 tree
*categ
, bool *is_class_extension
)
28276 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
28278 *super
= *categ
= NULL_TREE
;
28279 *is_class_extension
= false;
28280 if (next
->type
== CPP_COLON
)
28282 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
28283 *super
= cp_parser_identifier (parser
);
28285 else if (next
->type
== CPP_OPEN_PAREN
)
28287 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
28289 /* If there is no category name, and this is an @interface, we
28290 have a class extension. */
28291 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28293 *categ
= NULL_TREE
;
28294 *is_class_extension
= true;
28297 *categ
= cp_parser_identifier (parser
);
28299 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28303 /* Parse an Objective-C class interface. */
28306 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
28308 tree name
, super
, categ
, protos
;
28309 bool is_class_extension
;
28311 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
28312 name
= cp_parser_identifier (parser
);
28313 if (name
== error_mark_node
)
28315 /* It's hard to recover because even if valid @interface stuff
28316 is to follow, we can't compile it (or validate it) if we
28317 don't even know which class it refers to. Let's assume this
28318 was a stray '@interface' token in the stream and skip it.
28322 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
28323 &is_class_extension
);
28324 protos
= cp_parser_objc_protocol_refs_opt (parser
);
28326 /* We have either a class or a category on our hands. */
28327 if (categ
|| is_class_extension
)
28328 objc_start_category_interface (name
, categ
, protos
, attributes
);
28331 objc_start_class_interface (name
, super
, protos
, attributes
);
28332 /* Handle instance variable declarations, if any. */
28333 cp_parser_objc_class_ivars (parser
);
28334 objc_continue_interface ();
28337 cp_parser_objc_method_prototype_list (parser
);
28340 /* Parse an Objective-C class implementation. */
28343 cp_parser_objc_class_implementation (cp_parser
* parser
)
28345 tree name
, super
, categ
;
28346 bool is_class_extension
;
28348 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
28349 name
= cp_parser_identifier (parser
);
28350 if (name
== error_mark_node
)
28352 /* It's hard to recover because even if valid @implementation
28353 stuff is to follow, we can't compile it (or validate it) if
28354 we don't even know which class it refers to. Let's assume
28355 this was a stray '@implementation' token in the stream and
28360 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
28361 &is_class_extension
);
28363 /* We have either a class or a category on our hands. */
28365 objc_start_category_implementation (name
, categ
);
28368 objc_start_class_implementation (name
, super
);
28369 /* Handle instance variable declarations, if any. */
28370 cp_parser_objc_class_ivars (parser
);
28371 objc_continue_implementation ();
28374 cp_parser_objc_method_definition_list (parser
);
28377 /* Consume the @end token and finish off the implementation. */
28380 cp_parser_objc_end_implementation (cp_parser
* parser
)
28382 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
28383 objc_finish_implementation ();
28386 /* Parse an Objective-C declaration. */
28389 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
28391 /* Try to figure out what kind of declaration is present. */
28392 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28395 switch (kwd
->keyword
)
28400 error_at (kwd
->location
, "attributes may not be specified before"
28401 " the %<@%D%> Objective-C++ keyword",
28405 case RID_AT_IMPLEMENTATION
:
28406 warning_at (kwd
->location
, OPT_Wattributes
,
28407 "prefix attributes are ignored before %<@%D%>",
28414 switch (kwd
->keyword
)
28417 cp_parser_objc_alias_declaration (parser
);
28420 cp_parser_objc_class_declaration (parser
);
28422 case RID_AT_PROTOCOL
:
28423 cp_parser_objc_protocol_declaration (parser
, attributes
);
28425 case RID_AT_INTERFACE
:
28426 cp_parser_objc_class_interface (parser
, attributes
);
28428 case RID_AT_IMPLEMENTATION
:
28429 cp_parser_objc_class_implementation (parser
);
28432 cp_parser_objc_end_implementation (parser
);
28435 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
28437 cp_parser_skip_to_end_of_block_or_statement (parser
);
28441 /* Parse an Objective-C try-catch-finally statement.
28443 objc-try-catch-finally-stmt:
28444 @try compound-statement objc-catch-clause-seq [opt]
28445 objc-finally-clause [opt]
28447 objc-catch-clause-seq:
28448 objc-catch-clause objc-catch-clause-seq [opt]
28451 @catch ( objc-exception-declaration ) compound-statement
28453 objc-finally-clause:
28454 @finally compound-statement
28456 objc-exception-declaration:
28457 parameter-declaration
28460 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28464 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28465 for C. Keep them in sync. */
28468 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
28470 location_t location
;
28473 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
28474 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28475 objc_maybe_warn_exceptions (location
);
28476 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28477 node, lest it get absorbed into the surrounding block. */
28478 stmt
= push_stmt_list ();
28479 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28480 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
28482 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
28484 cp_parameter_declarator
*parm
;
28485 tree parameter_declaration
= error_mark_node
;
28486 bool seen_open_paren
= false;
28488 cp_lexer_consume_token (parser
->lexer
);
28489 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28490 seen_open_paren
= true;
28491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28493 /* We have "@catch (...)" (where the '...' are literally
28494 what is in the code). Skip the '...'.
28495 parameter_declaration is set to NULL_TREE, and
28496 objc_being_catch_clauses() knows that that means
28498 cp_lexer_consume_token (parser
->lexer
);
28499 parameter_declaration
= NULL_TREE
;
28503 /* We have "@catch (NSException *exception)" or something
28504 like that. Parse the parameter declaration. */
28505 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
28507 parameter_declaration
= error_mark_node
;
28509 parameter_declaration
= grokdeclarator (parm
->declarator
,
28510 &parm
->decl_specifiers
,
28511 PARM
, /*initialized=*/0,
28512 /*attrlist=*/NULL
);
28514 if (seen_open_paren
)
28515 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28518 /* If there was no open parenthesis, we are recovering from
28519 an error, and we are trying to figure out what mistake
28520 the user has made. */
28522 /* If there is an immediate closing parenthesis, the user
28523 probably forgot the opening one (ie, they typed "@catch
28524 NSException *e)". Parse the closing parenthesis and keep
28526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
28527 cp_lexer_consume_token (parser
->lexer
);
28529 /* If these is no immediate closing parenthesis, the user
28530 probably doesn't know that parenthesis are required at
28531 all (ie, they typed "@catch NSException *e"). So, just
28532 forget about the closing parenthesis and keep going. */
28534 objc_begin_catch_clause (parameter_declaration
);
28535 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28536 objc_finish_catch_clause ();
28538 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
28540 cp_lexer_consume_token (parser
->lexer
);
28541 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28542 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28543 node, lest it get absorbed into the surrounding block. */
28544 stmt
= push_stmt_list ();
28545 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28546 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
28549 return objc_finish_try_stmt ();
28552 /* Parse an Objective-C synchronized statement.
28554 objc-synchronized-stmt:
28555 @synchronized ( expression ) compound-statement
28557 Returns NULL_TREE. */
28560 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
28562 location_t location
;
28565 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
28567 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28568 objc_maybe_warn_exceptions (location
);
28569 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
28570 lock
= cp_parser_expression (parser
);
28571 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
28573 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28574 node, lest it get absorbed into the surrounding block. */
28575 stmt
= push_stmt_list ();
28576 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
28578 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
28581 /* Parse an Objective-C throw statement.
28584 @throw assignment-expression [opt] ;
28586 Returns a constructed '@throw' statement. */
28589 cp_parser_objc_throw_statement (cp_parser
*parser
)
28591 tree expr
= NULL_TREE
;
28592 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28594 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
28596 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28597 expr
= cp_parser_expression (parser
);
28599 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28601 return objc_build_throw_stmt (loc
, expr
);
28604 /* Parse an Objective-C statement. */
28607 cp_parser_objc_statement (cp_parser
* parser
)
28609 /* Try to figure out what kind of declaration is present. */
28610 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
28612 switch (kwd
->keyword
)
28615 return cp_parser_objc_try_catch_finally_statement (parser
);
28616 case RID_AT_SYNCHRONIZED
:
28617 return cp_parser_objc_synchronized_statement (parser
);
28619 return cp_parser_objc_throw_statement (parser
);
28621 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
28623 cp_parser_skip_to_end_of_block_or_statement (parser
);
28626 return error_mark_node
;
28629 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28630 look ahead to see if an objc keyword follows the attributes. This
28631 is to detect the use of prefix attributes on ObjC @interface and
28635 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
28637 cp_lexer_save_tokens (parser
->lexer
);
28638 *attrib
= cp_parser_attributes_opt (parser
);
28639 gcc_assert (*attrib
);
28640 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
28642 cp_lexer_commit_tokens (parser
->lexer
);
28645 cp_lexer_rollback_tokens (parser
->lexer
);
28649 /* This routine is a minimal replacement for
28650 c_parser_struct_declaration () used when parsing the list of
28651 types/names or ObjC++ properties. For example, when parsing the
28654 @property (readonly) int a, b, c;
28656 this function is responsible for parsing "int a, int b, int c" and
28657 returning the declarations as CHAIN of DECLs.
28659 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28660 similar parsing. */
28662 cp_parser_objc_struct_declaration (cp_parser
*parser
)
28664 tree decls
= NULL_TREE
;
28665 cp_decl_specifier_seq declspecs
;
28666 int decl_class_or_enum_p
;
28667 tree prefix_attributes
;
28669 cp_parser_decl_specifier_seq (parser
,
28670 CP_PARSER_FLAGS_NONE
,
28672 &decl_class_or_enum_p
);
28674 if (declspecs
.type
== error_mark_node
)
28675 return error_mark_node
;
28677 /* auto, register, static, extern, mutable. */
28678 if (declspecs
.storage_class
!= sc_none
)
28680 cp_parser_error (parser
, "invalid type for property");
28681 declspecs
.storage_class
= sc_none
;
28684 /* thread_local. */
28685 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
28687 cp_parser_error (parser
, "invalid type for property");
28688 declspecs
.locations
[ds_thread
] = 0;
28692 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
28694 cp_parser_error (parser
, "invalid type for property");
28695 declspecs
.locations
[ds_typedef
] = 0;
28698 prefix_attributes
= declspecs
.attributes
;
28699 declspecs
.attributes
= NULL_TREE
;
28701 /* Keep going until we hit the `;' at the end of the declaration. */
28702 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
28704 tree attributes
, first_attribute
, decl
;
28705 cp_declarator
*declarator
;
28708 /* Parse the declarator. */
28709 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
28710 NULL
, NULL
, false, false);
28712 /* Look for attributes that apply to the ivar. */
28713 attributes
= cp_parser_attributes_opt (parser
);
28714 /* Remember which attributes are prefix attributes and
28716 first_attribute
= attributes
;
28717 /* Combine the attributes. */
28718 attributes
= chainon (prefix_attributes
, attributes
);
28720 decl
= grokfield (declarator
, &declspecs
,
28721 NULL_TREE
, /*init_const_expr_p=*/false,
28722 NULL_TREE
, attributes
);
28724 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
28725 return error_mark_node
;
28727 /* Reset PREFIX_ATTRIBUTES. */
28728 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
28729 attributes
= TREE_CHAIN (attributes
);
28731 TREE_CHAIN (attributes
) = NULL_TREE
;
28733 DECL_CHAIN (decl
) = decls
;
28736 token
= cp_lexer_peek_token (parser
->lexer
);
28737 if (token
->type
== CPP_COMMA
)
28739 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
28748 /* Parse an Objective-C @property declaration. The syntax is:
28750 objc-property-declaration:
28751 '@property' objc-property-attributes[opt] struct-declaration ;
28753 objc-property-attributes:
28754 '(' objc-property-attribute-list ')'
28756 objc-property-attribute-list:
28757 objc-property-attribute
28758 objc-property-attribute-list, objc-property-attribute
28760 objc-property-attribute
28761 'getter' = identifier
28762 'setter' = identifier
28771 @property NSString *name;
28772 @property (readonly) id object;
28773 @property (retain, nonatomic, getter=getTheName) id name;
28774 @property int a, b, c;
28776 PS: This function is identical to
28777 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28779 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
28781 /* The following variables hold the attributes of the properties as
28782 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28783 seen. When we see an attribute, we set them to 'true' (if they
28784 are boolean properties) or to the identifier (if they have an
28785 argument, ie, for getter and setter). Note that here we only
28786 parse the list of attributes, check the syntax and accumulate the
28787 attributes that we find. objc_add_property_declaration() will
28788 then process the information. */
28789 bool property_assign
= false;
28790 bool property_copy
= false;
28791 tree property_getter_ident
= NULL_TREE
;
28792 bool property_nonatomic
= false;
28793 bool property_readonly
= false;
28794 bool property_readwrite
= false;
28795 bool property_retain
= false;
28796 tree property_setter_ident
= NULL_TREE
;
28798 /* 'properties' is the list of properties that we read. Usually a
28799 single one, but maybe more (eg, in "@property int a, b, c;" there
28804 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28806 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
28808 /* Parse the optional attribute list... */
28809 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28812 cp_lexer_consume_token (parser
->lexer
);
28816 bool syntax_error
= false;
28817 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28820 if (token
->type
!= CPP_NAME
)
28822 cp_parser_error (parser
, "expected identifier");
28825 keyword
= C_RID_CODE (token
->u
.value
);
28826 cp_lexer_consume_token (parser
->lexer
);
28829 case RID_ASSIGN
: property_assign
= true; break;
28830 case RID_COPY
: property_copy
= true; break;
28831 case RID_NONATOMIC
: property_nonatomic
= true; break;
28832 case RID_READONLY
: property_readonly
= true; break;
28833 case RID_READWRITE
: property_readwrite
= true; break;
28834 case RID_RETAIN
: property_retain
= true; break;
28838 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
28840 if (keyword
== RID_GETTER
)
28841 cp_parser_error (parser
,
28842 "missing %<=%> (after %<getter%> attribute)");
28844 cp_parser_error (parser
,
28845 "missing %<=%> (after %<setter%> attribute)");
28846 syntax_error
= true;
28849 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
28850 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
28852 cp_parser_error (parser
, "expected identifier");
28853 syntax_error
= true;
28856 if (keyword
== RID_SETTER
)
28858 if (property_setter_ident
!= NULL_TREE
)
28860 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
28861 cp_lexer_consume_token (parser
->lexer
);
28864 property_setter_ident
= cp_parser_objc_selector (parser
);
28865 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
28866 cp_parser_error (parser
, "setter name must terminate with %<:%>");
28868 cp_lexer_consume_token (parser
->lexer
);
28872 if (property_getter_ident
!= NULL_TREE
)
28874 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
28875 cp_lexer_consume_token (parser
->lexer
);
28878 property_getter_ident
= cp_parser_objc_selector (parser
);
28882 cp_parser_error (parser
, "unknown property attribute");
28883 syntax_error
= true;
28890 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28891 cp_lexer_consume_token (parser
->lexer
);
28896 /* FIXME: "@property (setter, assign);" will generate a spurious
28897 "error: expected ‘)’ before ‘,’ token". This is because
28898 cp_parser_require, unlike the C counterpart, will produce an
28899 error even if we are in error recovery. */
28900 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28902 cp_parser_skip_to_closing_parenthesis (parser
,
28903 /*recovering=*/true,
28904 /*or_comma=*/false,
28905 /*consume_paren=*/true);
28909 /* ... and the property declaration(s). */
28910 properties
= cp_parser_objc_struct_declaration (parser
);
28912 if (properties
== error_mark_node
)
28914 cp_parser_skip_to_end_of_statement (parser
);
28915 /* If the next token is now a `;', consume it. */
28916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28917 cp_lexer_consume_token (parser
->lexer
);
28921 if (properties
== NULL_TREE
)
28922 cp_parser_error (parser
, "expected identifier");
28925 /* Comma-separated properties are chained together in
28926 reverse order; add them one by one. */
28927 properties
= nreverse (properties
);
28929 for (; properties
; properties
= TREE_CHAIN (properties
))
28930 objc_add_property_declaration (loc
, copy_node (properties
),
28931 property_readonly
, property_readwrite
,
28932 property_assign
, property_retain
,
28933 property_copy
, property_nonatomic
,
28934 property_getter_ident
, property_setter_ident
);
28937 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28940 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28942 objc-synthesize-declaration:
28943 @synthesize objc-synthesize-identifier-list ;
28945 objc-synthesize-identifier-list:
28946 objc-synthesize-identifier
28947 objc-synthesize-identifier-list, objc-synthesize-identifier
28949 objc-synthesize-identifier
28951 identifier = identifier
28954 @synthesize MyProperty;
28955 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28957 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28958 for C. Keep them in sync.
28961 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
28963 tree list
= NULL_TREE
;
28965 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28967 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
28970 tree property
, ivar
;
28971 property
= cp_parser_identifier (parser
);
28972 if (property
== error_mark_node
)
28974 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
28979 cp_lexer_consume_token (parser
->lexer
);
28980 ivar
= cp_parser_identifier (parser
);
28981 if (ivar
== error_mark_node
)
28983 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28989 list
= chainon (list
, build_tree_list (ivar
, property
));
28990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28991 cp_lexer_consume_token (parser
->lexer
);
28995 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28996 objc_add_synthesize_declaration (loc
, list
);
28999 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29001 objc-dynamic-declaration:
29002 @dynamic identifier-list ;
29005 @dynamic MyProperty;
29006 @dynamic MyProperty, AnotherProperty;
29008 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29009 for C. Keep them in sync.
29012 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
29014 tree list
= NULL_TREE
;
29016 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29018 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
29022 property
= cp_parser_identifier (parser
);
29023 if (property
== error_mark_node
)
29025 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29028 list
= chainon (list
, build_tree_list (NULL
, property
));
29029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29030 cp_lexer_consume_token (parser
->lexer
);
29034 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29035 objc_add_dynamic_declaration (loc
, list
);
29039 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29041 /* Returns name of the next clause.
29042 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29043 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29044 returned and the token is consumed. */
29046 static pragma_omp_clause
29047 cp_parser_omp_clause_name (cp_parser
*parser
)
29049 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
29051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
29052 result
= PRAGMA_OACC_CLAUSE_AUTO
;
29053 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
29054 result
= PRAGMA_OMP_CLAUSE_IF
;
29055 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
29056 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
29057 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
29058 result
= PRAGMA_OACC_CLAUSE_DELETE
;
29059 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
29060 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
29061 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29062 result
= PRAGMA_OMP_CLAUSE_FOR
;
29063 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29065 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29066 const char *p
= IDENTIFIER_POINTER (id
);
29071 if (!strcmp ("aligned", p
))
29072 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
29073 else if (!strcmp ("async", p
))
29074 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
29077 if (!strcmp ("collapse", p
))
29078 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
29079 else if (!strcmp ("copy", p
))
29080 result
= PRAGMA_OACC_CLAUSE_COPY
;
29081 else if (!strcmp ("copyin", p
))
29082 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
29083 else if (!strcmp ("copyout", p
))
29084 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
29085 else if (!strcmp ("copyprivate", p
))
29086 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
29087 else if (!strcmp ("create", p
))
29088 result
= PRAGMA_OACC_CLAUSE_CREATE
;
29091 if (!strcmp ("defaultmap", p
))
29092 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
29093 else if (!strcmp ("depend", p
))
29094 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
29095 else if (!strcmp ("device", p
))
29096 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
29097 else if (!strcmp ("deviceptr", p
))
29098 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
29099 else if (!strcmp ("device_resident", p
))
29100 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
29101 else if (!strcmp ("dist_schedule", p
))
29102 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
29105 if (!strcmp ("final", p
))
29106 result
= PRAGMA_OMP_CLAUSE_FINAL
;
29107 else if (!strcmp ("firstprivate", p
))
29108 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
29109 else if (!strcmp ("from", p
))
29110 result
= PRAGMA_OMP_CLAUSE_FROM
;
29113 if (!strcmp ("gang", p
))
29114 result
= PRAGMA_OACC_CLAUSE_GANG
;
29115 else if (!strcmp ("grainsize", p
))
29116 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
29119 if (!strcmp ("hint", p
))
29120 result
= PRAGMA_OMP_CLAUSE_HINT
;
29121 else if (!strcmp ("host", p
))
29122 result
= PRAGMA_OACC_CLAUSE_HOST
;
29125 if (!strcmp ("inbranch", p
))
29126 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
29127 else if (!strcmp ("independent", p
))
29128 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
29129 else if (!strcmp ("is_device_ptr", p
))
29130 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
29133 if (!strcmp ("lastprivate", p
))
29134 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
29135 else if (!strcmp ("linear", p
))
29136 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
29137 else if (!strcmp ("link", p
))
29138 result
= PRAGMA_OMP_CLAUSE_LINK
;
29141 if (!strcmp ("map", p
))
29142 result
= PRAGMA_OMP_CLAUSE_MAP
;
29143 else if (!strcmp ("mergeable", p
))
29144 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
29145 else if (flag_cilkplus
&& !strcmp ("mask", p
))
29146 result
= PRAGMA_CILK_CLAUSE_MASK
;
29149 if (!strcmp ("nogroup", p
))
29150 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
29151 else if (!strcmp ("notinbranch", p
))
29152 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
29153 else if (!strcmp ("nowait", p
))
29154 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
29155 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
29156 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
29157 else if (!strcmp ("num_gangs", p
))
29158 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
29159 else if (!strcmp ("num_tasks", p
))
29160 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
29161 else if (!strcmp ("num_teams", p
))
29162 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
29163 else if (!strcmp ("num_threads", p
))
29164 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
29165 else if (!strcmp ("num_workers", p
))
29166 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
29169 if (!strcmp ("ordered", p
))
29170 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
29173 if (!strcmp ("parallel", p
))
29174 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
29175 else if (!strcmp ("present", p
))
29176 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
29177 else if (!strcmp ("present_or_copy", p
)
29178 || !strcmp ("pcopy", p
))
29179 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
29180 else if (!strcmp ("present_or_copyin", p
)
29181 || !strcmp ("pcopyin", p
))
29182 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
29183 else if (!strcmp ("present_or_copyout", p
)
29184 || !strcmp ("pcopyout", p
))
29185 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
29186 else if (!strcmp ("present_or_create", p
)
29187 || !strcmp ("pcreate", p
))
29188 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
29189 else if (!strcmp ("priority", p
))
29190 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
29191 else if (!strcmp ("proc_bind", p
))
29192 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
29195 if (!strcmp ("reduction", p
))
29196 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
29199 if (!strcmp ("safelen", p
))
29200 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
29201 else if (!strcmp ("schedule", p
))
29202 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
29203 else if (!strcmp ("sections", p
))
29204 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
29205 else if (!strcmp ("self", p
))
29206 result
= PRAGMA_OACC_CLAUSE_SELF
;
29207 else if (!strcmp ("seq", p
))
29208 result
= PRAGMA_OACC_CLAUSE_SEQ
;
29209 else if (!strcmp ("shared", p
))
29210 result
= PRAGMA_OMP_CLAUSE_SHARED
;
29211 else if (!strcmp ("simd", p
))
29212 result
= PRAGMA_OMP_CLAUSE_SIMD
;
29213 else if (!strcmp ("simdlen", p
))
29214 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
29217 if (!strcmp ("taskgroup", p
))
29218 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
29219 else if (!strcmp ("thread_limit", p
))
29220 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
29221 else if (!strcmp ("threads", p
))
29222 result
= PRAGMA_OMP_CLAUSE_THREADS
;
29223 else if (!strcmp ("tile", p
))
29224 result
= PRAGMA_OACC_CLAUSE_TILE
;
29225 else if (!strcmp ("to", p
))
29226 result
= PRAGMA_OMP_CLAUSE_TO
;
29229 if (!strcmp ("uniform", p
))
29230 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
29231 else if (!strcmp ("untied", p
))
29232 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
29233 else if (!strcmp ("use_device", p
))
29234 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
29235 else if (!strcmp ("use_device_ptr", p
))
29236 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
29239 if (!strcmp ("vector", p
))
29240 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
29241 else if (!strcmp ("vector_length", p
))
29242 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
29243 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
29244 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
29247 if (!strcmp ("wait", p
))
29248 result
= PRAGMA_OACC_CLAUSE_WAIT
;
29249 else if (!strcmp ("worker", p
))
29250 result
= PRAGMA_OACC_CLAUSE_WORKER
;
29255 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
29256 cp_lexer_consume_token (parser
->lexer
);
29261 /* Validate that a clause of the given type does not already exist. */
29264 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
29265 const char *name
, location_t location
)
29269 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29270 if (OMP_CLAUSE_CODE (c
) == code
)
29272 error_at (location
, "too many %qs clauses", name
);
29280 variable-list , identifier
29282 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29283 colon). An opening parenthesis will have been consumed by the caller.
29285 If KIND is nonzero, create the appropriate node and install the decl
29286 in OMP_CLAUSE_DECL and add the node to the head of the list.
29288 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29289 return the list created.
29291 COLON can be NULL if only closing parenthesis should end the list,
29292 or pointer to bool which will receive false if the list is terminated
29293 by closing parenthesis or true if the list is terminated by colon. */
29296 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
29297 tree list
, bool *colon
)
29300 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
29303 parser
->colon_corrects_to_scope_p
= false;
29310 token
= cp_lexer_peek_token (parser
->lexer
);
29312 && current_class_ptr
29313 && cp_parser_is_keyword (token
, RID_THIS
))
29315 decl
= finish_this_expr ();
29316 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
29317 || CONVERT_EXPR_P (decl
))
29318 decl
= TREE_OPERAND (decl
, 0);
29319 cp_lexer_consume_token (parser
->lexer
);
29323 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
29324 /*check_dependency_p=*/true,
29325 /*template_p=*/NULL
,
29326 /*declarator_p=*/false,
29327 /*optional_p=*/false);
29328 if (name
== error_mark_node
)
29331 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
29332 if (decl
== error_mark_node
)
29333 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
29336 if (decl
== error_mark_node
)
29338 else if (kind
!= 0)
29342 case OMP_CLAUSE__CACHE_
:
29343 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
29345 error_at (token
->location
, "expected %<[%>");
29346 decl
= error_mark_node
;
29350 case OMP_CLAUSE_MAP
:
29351 case OMP_CLAUSE_FROM
:
29352 case OMP_CLAUSE_TO
:
29353 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
29356 = cp_lexer_peek_token (parser
->lexer
)->location
;
29357 cp_id_kind idk
= CP_ID_KIND_NONE
;
29358 cp_lexer_consume_token (parser
->lexer
);
29360 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
29365 case OMP_CLAUSE_DEPEND
:
29366 case OMP_CLAUSE_REDUCTION
:
29367 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
29369 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
29371 parser
->colon_corrects_to_scope_p
= false;
29372 cp_lexer_consume_token (parser
->lexer
);
29373 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29374 low_bound
= cp_parser_expression (parser
);
29376 parser
->colon_corrects_to_scope_p
29377 = saved_colon_corrects_to_scope_p
;
29378 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
29379 length
= integer_one_node
;
29382 /* Look for `:'. */
29383 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29385 if (!cp_lexer_next_token_is (parser
->lexer
,
29387 length
= cp_parser_expression (parser
);
29389 /* Look for the closing `]'. */
29390 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
29394 if (kind
== OMP_CLAUSE__CACHE_
)
29396 if (TREE_CODE (low_bound
) != INTEGER_CST
29397 && !TREE_READONLY (low_bound
))
29399 error_at (token
->location
,
29400 "%qD is not a constant", low_bound
);
29401 decl
= error_mark_node
;
29404 if (TREE_CODE (length
) != INTEGER_CST
29405 && !TREE_READONLY (length
))
29407 error_at (token
->location
,
29408 "%qD is not a constant", length
);
29409 decl
= error_mark_node
;
29413 decl
= tree_cons (low_bound
, length
, decl
);
29420 tree u
= build_omp_clause (token
->location
, kind
);
29421 OMP_CLAUSE_DECL (u
) = decl
;
29422 OMP_CLAUSE_CHAIN (u
) = list
;
29426 list
= tree_cons (decl
, NULL_TREE
, list
);
29429 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
29431 cp_lexer_consume_token (parser
->lexer
);
29435 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
29437 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29440 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
29444 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29448 /* Try to resync to an unnested comma. Copied from
29449 cp_parser_parenthesized_expression_list. */
29452 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
29453 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
29454 /*recovering=*/true,
29456 /*consume_paren=*/true);
29464 /* Similarly, but expect leading and trailing parenthesis. This is a very
29465 common case for omp clauses. */
29468 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
29470 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29471 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
29476 copy ( variable-list )
29477 copyin ( variable-list )
29478 copyout ( variable-list )
29479 create ( variable-list )
29480 delete ( variable-list )
29481 present ( variable-list )
29482 present_or_copy ( variable-list )
29483 pcopy ( variable-list )
29484 present_or_copyin ( variable-list )
29485 pcopyin ( variable-list )
29486 present_or_copyout ( variable-list )
29487 pcopyout ( variable-list )
29488 present_or_create ( variable-list )
29489 pcreate ( variable-list ) */
29492 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
29495 enum gomp_map_kind kind
;
29498 case PRAGMA_OACC_CLAUSE_COPY
:
29499 kind
= GOMP_MAP_FORCE_TOFROM
;
29501 case PRAGMA_OACC_CLAUSE_COPYIN
:
29502 kind
= GOMP_MAP_FORCE_TO
;
29504 case PRAGMA_OACC_CLAUSE_COPYOUT
:
29505 kind
= GOMP_MAP_FORCE_FROM
;
29507 case PRAGMA_OACC_CLAUSE_CREATE
:
29508 kind
= GOMP_MAP_FORCE_ALLOC
;
29510 case PRAGMA_OACC_CLAUSE_DELETE
:
29511 kind
= GOMP_MAP_FORCE_DEALLOC
;
29513 case PRAGMA_OACC_CLAUSE_DEVICE
:
29514 kind
= GOMP_MAP_FORCE_TO
;
29516 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
29517 kind
= GOMP_MAP_DEVICE_RESIDENT
;
29519 case PRAGMA_OACC_CLAUSE_HOST
:
29520 case PRAGMA_OACC_CLAUSE_SELF
:
29521 kind
= GOMP_MAP_FORCE_FROM
;
29523 case PRAGMA_OACC_CLAUSE_LINK
:
29524 kind
= GOMP_MAP_LINK
;
29526 case PRAGMA_OACC_CLAUSE_PRESENT
:
29527 kind
= GOMP_MAP_FORCE_PRESENT
;
29529 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
29530 kind
= GOMP_MAP_TOFROM
;
29532 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
29533 kind
= GOMP_MAP_TO
;
29535 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
29536 kind
= GOMP_MAP_FROM
;
29538 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
29539 kind
= GOMP_MAP_ALLOC
;
29542 gcc_unreachable ();
29545 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
29547 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
29548 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
29554 deviceptr ( variable-list ) */
29557 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
29559 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29562 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29563 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29564 variable-list must only allow for pointer variables. */
29565 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29566 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
29568 tree v
= TREE_PURPOSE (t
);
29570 /* FIXME diagnostics: Ideally we should keep individual
29571 locations for all the variables in the var list to make the
29572 following errors more precise. Perhaps
29573 c_parser_omp_var_list_parens should construct a list of
29574 locations to go along with the var list. */
29577 error_at (loc
, "%qD is not a variable", v
);
29578 else if (TREE_TYPE (v
) == error_mark_node
)
29580 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
29581 error_at (loc
, "%qD is not a pointer variable", v
);
29583 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
29584 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
29585 OMP_CLAUSE_DECL (u
) = v
;
29586 OMP_CLAUSE_CHAIN (u
) = list
;
29600 cp_parser_oacc_simple_clause (cp_parser
* /* parser */,
29601 enum omp_clause_code code
,
29602 tree list
, location_t location
)
29604 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
29605 tree c
= build_omp_clause (location
, code
);
29606 OMP_CLAUSE_CHAIN (c
) = list
;
29611 num_gangs ( expression )
29612 num_workers ( expression )
29613 vector_length ( expression ) */
29616 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
29617 const char *str
, tree list
)
29619 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29621 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29624 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29626 if (t
== error_mark_node
29627 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29629 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29630 /*or_comma=*/false,
29631 /*consume_paren=*/true);
29635 check_no_duplicate_clause (list
, code
, str
, loc
);
29637 tree c
= build_omp_clause (loc
, code
);
29638 OMP_CLAUSE_OPERAND (c
, 0) = t
;
29639 OMP_CLAUSE_CHAIN (c
) = list
;
29645 gang [( gang-arg-list )]
29646 worker [( [num:] int-expr )]
29647 vector [( [length:] int-expr )]
29649 where gang-arg is one of:
29654 and size-expr may be:
29661 cp_parser_oacc_shape_clause (cp_parser
*parser
, omp_clause_code kind
,
29662 const char *str
, tree list
)
29664 const char *id
= "num";
29665 cp_lexer
*lexer
= parser
->lexer
;
29666 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
29667 location_t loc
= cp_lexer_peek_token (lexer
)->location
;
29669 if (kind
== OMP_CLAUSE_VECTOR
)
29672 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
29674 cp_lexer_consume_token (lexer
);
29678 cp_token
*next
= cp_lexer_peek_token (lexer
);
29681 /* Gang static argument. */
29682 if (kind
== OMP_CLAUSE_GANG
29683 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
29685 cp_lexer_consume_token (lexer
);
29687 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29688 goto cleanup_error
;
29691 if (ops
[idx
] != NULL
)
29693 cp_parser_error (parser
, "too many %<static%> arguments");
29694 goto cleanup_error
;
29697 /* Check for the '*' argument. */
29698 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
29699 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
29700 || cp_lexer_nth_token_is (parser
->lexer
, 2,
29703 cp_lexer_consume_token (lexer
);
29704 ops
[idx
] = integer_minus_one_node
;
29706 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
29708 cp_lexer_consume_token (lexer
);
29714 /* Worker num: argument and vector length: arguments. */
29715 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
29716 && strcmp (id
, IDENTIFIER_POINTER (next
->u
.value
)) == 0
29717 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
29719 cp_lexer_consume_token (lexer
); /* id */
29720 cp_lexer_consume_token (lexer
); /* ':' */
29723 /* Now collect the actual argument. */
29724 if (ops
[idx
] != NULL_TREE
)
29726 cp_parser_error (parser
, "unexpected argument");
29727 goto cleanup_error
;
29730 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
29732 if (expr
== error_mark_node
)
29733 goto cleanup_error
;
29735 mark_exp_read (expr
);
29738 if (kind
== OMP_CLAUSE_GANG
29739 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
29741 cp_lexer_consume_token (lexer
);
29748 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29749 goto cleanup_error
;
29752 check_no_duplicate_clause (list
, kind
, str
, loc
);
29754 c
= build_omp_clause (loc
, kind
);
29757 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
29759 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
29760 OMP_CLAUSE_CHAIN (c
) = list
;
29765 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
29770 tile ( size-expr-list ) */
29773 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
29775 tree c
, expr
= error_mark_node
;
29776 tree tile
= NULL_TREE
;
29778 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
29780 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29785 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
29786 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
29787 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
29789 cp_lexer_consume_token (parser
->lexer
);
29790 expr
= integer_minus_one_node
;
29793 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
29795 if (expr
== error_mark_node
)
29798 tile
= tree_cons (NULL_TREE
, expr
, tile
);
29800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29801 cp_lexer_consume_token (parser
->lexer
);
29803 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
29805 /* Consume the trailing ')'. */
29806 cp_lexer_consume_token (parser
->lexer
);
29808 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
29809 tile
= nreverse (tile
);
29810 OMP_CLAUSE_TILE_LIST (c
) = tile
;
29811 OMP_CLAUSE_CHAIN (c
) = list
;
29816 Parse wait clause or directive parameters. */
29819 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
29821 vec
<tree
, va_gc
> *args
;
29824 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
29826 /*allow_expansion_p=*/true,
29827 /*non_constant_p=*/NULL
);
29829 if (args
== NULL
|| args
->length () == 0)
29831 cp_parser_error (parser
, "expected integer expression before ')'");
29833 release_tree_vector (args
);
29837 args_tree
= build_tree_list_vec (args
);
29839 release_tree_vector (args
);
29841 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
29843 tree targ
= TREE_VALUE (t
);
29845 if (targ
!= error_mark_node
)
29847 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
29848 error ("%<wait%> expression must be integral");
29851 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
29853 mark_rvalue_use (targ
);
29854 OMP_CLAUSE_DECL (c
) = targ
;
29855 OMP_CLAUSE_CHAIN (c
) = list
;
29865 wait ( int-expr-list ) */
29868 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
29870 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
29872 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
29875 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
29881 collapse ( constant-expression ) */
29884 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
29890 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29891 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29894 num
= cp_parser_constant_expression (parser
);
29896 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29897 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29898 /*or_comma=*/false,
29899 /*consume_paren=*/true);
29901 if (num
== error_mark_node
)
29903 num
= fold_non_dependent_expr (num
);
29904 if (!tree_fits_shwi_p (num
)
29905 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
29906 || (n
= tree_to_shwi (num
)) <= 0
29909 error_at (loc
, "collapse argument needs positive constant integer expression");
29913 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
29914 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
29915 OMP_CLAUSE_CHAIN (c
) = list
;
29916 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
29922 default ( shared | none )
29928 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
29929 location_t location
, bool is_oacc
)
29931 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
29934 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29936 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29938 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29939 const char *p
= IDENTIFIER_POINTER (id
);
29944 if (strcmp ("none", p
) != 0)
29946 kind
= OMP_CLAUSE_DEFAULT_NONE
;
29950 if (strcmp ("shared", p
) != 0 || is_oacc
)
29952 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
29959 cp_lexer_consume_token (parser
->lexer
);
29965 cp_parser_error (parser
, "expected %<none%>");
29967 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
29970 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29971 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29972 /*or_comma=*/false,
29973 /*consume_paren=*/true);
29975 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
29978 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
29979 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
29980 OMP_CLAUSE_CHAIN (c
) = list
;
29981 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
29987 final ( expression ) */
29990 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
29994 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29997 t
= cp_parser_condition (parser
);
29999 if (t
== error_mark_node
30000 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30001 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30002 /*or_comma=*/false,
30003 /*consume_paren=*/true);
30005 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
30007 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
30008 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
30009 OMP_CLAUSE_CHAIN (c
) = list
;
30018 if ( directive-name-modifier : expression )
30020 directive-name-modifier:
30021 parallel | task | taskloop | target data | target | target update
30022 | target enter data | target exit data */
30025 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
30029 enum tree_code if_modifier
= ERROR_MARK
;
30031 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30034 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30036 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30037 const char *p
= IDENTIFIER_POINTER (id
);
30040 if (strcmp ("parallel", p
) == 0)
30041 if_modifier
= OMP_PARALLEL
;
30042 else if (strcmp ("task", p
) == 0)
30043 if_modifier
= OMP_TASK
;
30044 else if (strcmp ("taskloop", p
) == 0)
30045 if_modifier
= OMP_TASKLOOP
;
30046 else if (strcmp ("target", p
) == 0)
30048 if_modifier
= OMP_TARGET
;
30049 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
30051 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
30052 p
= IDENTIFIER_POINTER (id
);
30053 if (strcmp ("data", p
) == 0)
30054 if_modifier
= OMP_TARGET_DATA
;
30055 else if (strcmp ("update", p
) == 0)
30056 if_modifier
= OMP_TARGET_UPDATE
;
30057 else if (strcmp ("enter", p
) == 0)
30058 if_modifier
= OMP_TARGET_ENTER_DATA
;
30059 else if (strcmp ("exit", p
) == 0)
30060 if_modifier
= OMP_TARGET_EXIT_DATA
;
30061 if (if_modifier
!= OMP_TARGET
)
30066 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
30067 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
30069 if_modifier
= ERROR_MARK
;
30071 if (if_modifier
== OMP_TARGET_ENTER_DATA
30072 || if_modifier
== OMP_TARGET_EXIT_DATA
)
30074 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
30076 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
30077 p
= IDENTIFIER_POINTER (id
);
30078 if (strcmp ("data", p
) == 0)
30084 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
30085 error_at (loc
, "expected %<data%>");
30086 if_modifier
= ERROR_MARK
;
30091 if (if_modifier
!= ERROR_MARK
)
30093 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
30096 cp_lexer_consume_token (parser
->lexer
);
30103 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
30104 error_at (loc
, "expected %<:%>");
30106 if_modifier
= ERROR_MARK
;
30111 t
= cp_parser_condition (parser
);
30113 if (t
== error_mark_node
30114 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30115 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30116 /*or_comma=*/false,
30117 /*consume_paren=*/true);
30119 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30120 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
30122 if (if_modifier
!= ERROR_MARK
30123 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30125 const char *p
= NULL
;
30126 switch (if_modifier
)
30128 case OMP_PARALLEL
: p
= "parallel"; break;
30129 case OMP_TASK
: p
= "task"; break;
30130 case OMP_TASKLOOP
: p
= "taskloop"; break;
30131 case OMP_TARGET_DATA
: p
= "target data"; break;
30132 case OMP_TARGET
: p
= "target"; break;
30133 case OMP_TARGET_UPDATE
: p
= "target update"; break;
30134 case OMP_TARGET_ENTER_DATA
: p
= "enter data"; break;
30135 case OMP_TARGET_EXIT_DATA
: p
= "exit data"; break;
30136 default: gcc_unreachable ();
30138 error_at (location
, "too many %<if%> clauses with %qs modifier",
30142 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
30145 error_at (location
, "too many %<if%> clauses");
30147 error_at (location
, "too many %<if%> clauses without modifier");
30150 else if (if_modifier
== ERROR_MARK
30151 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
30153 error_at (location
, "if any %<if%> clause has modifier, then all "
30154 "%<if%> clauses have to use modifier");
30159 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
30160 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
30161 OMP_CLAUSE_IF_EXPR (c
) = t
;
30162 OMP_CLAUSE_CHAIN (c
) = list
;
30171 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
30172 tree list
, location_t location
)
30176 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
30179 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
30180 OMP_CLAUSE_CHAIN (c
) = list
;
30188 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
30189 tree list
, location_t location
)
30193 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
30195 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
30196 OMP_CLAUSE_CHAIN (c
) = list
;
30201 num_threads ( expression ) */
30204 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
30205 location_t location
)
30209 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30212 t
= cp_parser_expression (parser
);
30214 if (t
== error_mark_node
30215 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30216 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30217 /*or_comma=*/false,
30218 /*consume_paren=*/true);
30220 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
30221 "num_threads", location
);
30223 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
30224 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
30225 OMP_CLAUSE_CHAIN (c
) = list
;
30231 num_tasks ( expression ) */
30234 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
30235 location_t location
)
30239 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30242 t
= cp_parser_expression (parser
);
30244 if (t
== error_mark_node
30245 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30246 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30247 /*or_comma=*/false,
30248 /*consume_paren=*/true);
30250 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
30251 "num_tasks", location
);
30253 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
30254 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
30255 OMP_CLAUSE_CHAIN (c
) = list
;
30261 grainsize ( expression ) */
30264 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
30265 location_t location
)
30269 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30272 t
= cp_parser_expression (parser
);
30274 if (t
== error_mark_node
30275 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30276 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30277 /*or_comma=*/false,
30278 /*consume_paren=*/true);
30280 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
30281 "grainsize", location
);
30283 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
30284 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
30285 OMP_CLAUSE_CHAIN (c
) = list
;
30291 priority ( expression ) */
30294 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
30295 location_t location
)
30299 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30302 t
= cp_parser_expression (parser
);
30304 if (t
== error_mark_node
30305 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30306 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30307 /*or_comma=*/false,
30308 /*consume_paren=*/true);
30310 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
30311 "priority", location
);
30313 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
30314 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
30315 OMP_CLAUSE_CHAIN (c
) = list
;
30321 hint ( expression ) */
30324 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
,
30325 location_t location
)
30329 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30332 t
= cp_parser_expression (parser
);
30334 if (t
== error_mark_node
30335 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30336 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30337 /*or_comma=*/false,
30338 /*consume_paren=*/true);
30340 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
30342 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
30343 OMP_CLAUSE_HINT_EXPR (c
) = t
;
30344 OMP_CLAUSE_CHAIN (c
) = list
;
30350 defaultmap ( tofrom : scalar ) */
30353 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
30354 location_t location
)
30359 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30362 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30364 cp_parser_error (parser
, "expected %<tofrom%>");
30367 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30368 p
= IDENTIFIER_POINTER (id
);
30369 if (strcmp (p
, "tofrom") != 0)
30371 cp_parser_error (parser
, "expected %<tofrom%>");
30374 cp_lexer_consume_token (parser
->lexer
);
30375 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30378 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30380 cp_parser_error (parser
, "expected %<scalar%>");
30383 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30384 p
= IDENTIFIER_POINTER (id
);
30385 if (strcmp (p
, "scalar") != 0)
30387 cp_parser_error (parser
, "expected %<scalar%>");
30390 cp_lexer_consume_token (parser
->lexer
);
30391 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30394 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULTMAP
, "defaultmap",
30397 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
30398 OMP_CLAUSE_CHAIN (c
) = list
;
30402 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30403 /*or_comma=*/false,
30404 /*consume_paren=*/true);
30412 ordered ( constant-expression ) */
30415 cp_parser_omp_clause_ordered (cp_parser
*parser
,
30416 tree list
, location_t location
)
30418 tree c
, num
= NULL_TREE
;
30421 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
30422 "ordered", location
);
30424 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30426 cp_lexer_consume_token (parser
->lexer
);
30428 num
= cp_parser_constant_expression (parser
);
30430 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30431 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30432 /*or_comma=*/false,
30433 /*consume_paren=*/true);
30435 if (num
== error_mark_node
)
30437 num
= fold_non_dependent_expr (num
);
30438 if (!tree_fits_shwi_p (num
)
30439 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
30440 || (n
= tree_to_shwi (num
)) <= 0
30443 error_at (location
,
30444 "ordered argument needs positive constant integer "
30450 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
30451 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
30452 OMP_CLAUSE_CHAIN (c
) = list
;
30457 reduction ( reduction-operator : variable-list )
30459 reduction-operator:
30460 One of: + * - & ^ | && ||
30464 reduction-operator:
30465 One of: + * - & ^ | && || min max
30469 reduction-operator:
30470 One of: + * - & ^ | && ||
30474 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
30476 enum tree_code code
= ERROR_MARK
;
30477 tree nlist
, c
, id
= NULL_TREE
;
30479 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30482 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30484 case CPP_PLUS
: code
= PLUS_EXPR
; break;
30485 case CPP_MULT
: code
= MULT_EXPR
; break;
30486 case CPP_MINUS
: code
= MINUS_EXPR
; break;
30487 case CPP_AND
: code
= BIT_AND_EXPR
; break;
30488 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
30489 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
30490 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
30491 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
30495 if (code
!= ERROR_MARK
)
30496 cp_lexer_consume_token (parser
->lexer
);
30499 bool saved_colon_corrects_to_scope_p
;
30500 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
30501 parser
->colon_corrects_to_scope_p
= false;
30502 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
30503 /*check_dependency_p=*/true,
30504 /*template_p=*/NULL
,
30505 /*declarator_p=*/false,
30506 /*optional_p=*/false);
30507 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
30508 if (identifier_p (id
))
30510 const char *p
= IDENTIFIER_POINTER (id
);
30512 if (strcmp (p
, "min") == 0)
30514 else if (strcmp (p
, "max") == 0)
30516 else if (id
== ansi_opname (PLUS_EXPR
))
30518 else if (id
== ansi_opname (MULT_EXPR
))
30520 else if (id
== ansi_opname (MINUS_EXPR
))
30522 else if (id
== ansi_opname (BIT_AND_EXPR
))
30523 code
= BIT_AND_EXPR
;
30524 else if (id
== ansi_opname (BIT_IOR_EXPR
))
30525 code
= BIT_IOR_EXPR
;
30526 else if (id
== ansi_opname (BIT_XOR_EXPR
))
30527 code
= BIT_XOR_EXPR
;
30528 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
30529 code
= TRUTH_ANDIF_EXPR
;
30530 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
30531 code
= TRUTH_ORIF_EXPR
;
30532 id
= omp_reduction_id (code
, id
, NULL_TREE
);
30533 tree scope
= parser
->scope
;
30535 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
30536 parser
->scope
= NULL_TREE
;
30537 parser
->qualifying_scope
= NULL_TREE
;
30538 parser
->object_scope
= NULL_TREE
;
30542 error ("invalid reduction-identifier");
30544 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30545 /*or_comma=*/false,
30546 /*consume_paren=*/true);
30551 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
30554 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
30556 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30558 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
30559 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
30566 schedule ( schedule-kind )
30567 schedule ( schedule-kind , expression )
30570 static | dynamic | guided | runtime | auto
30573 schedule ( schedule-modifier : schedule-kind )
30574 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
30582 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
30585 int modifiers
= 0, nmodifiers
= 0;
30587 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30590 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
30592 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30594 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30595 const char *p
= IDENTIFIER_POINTER (id
);
30596 if (strcmp ("simd", p
) == 0)
30597 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
30598 else if (strcmp ("monotonic", p
) == 0)
30599 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
30600 else if (strcmp ("nonmonotonic", p
) == 0)
30601 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
30604 cp_lexer_consume_token (parser
->lexer
);
30605 if (nmodifiers
++ == 0
30606 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30607 cp_lexer_consume_token (parser
->lexer
);
30610 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30615 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30617 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30618 const char *p
= IDENTIFIER_POINTER (id
);
30623 if (strcmp ("dynamic", p
) != 0)
30625 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
30629 if (strcmp ("guided", p
) != 0)
30631 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
30635 if (strcmp ("runtime", p
) != 0)
30637 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
30644 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
30645 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
30646 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
30647 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
30650 cp_lexer_consume_token (parser
->lexer
);
30652 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
30653 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
30654 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
30655 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
30657 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
30662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
30665 cp_lexer_consume_token (parser
->lexer
);
30667 token
= cp_lexer_peek_token (parser
->lexer
);
30668 t
= cp_parser_assignment_expression (parser
);
30670 if (t
== error_mark_node
)
30672 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
30673 error_at (token
->location
, "schedule %<runtime%> does not take "
30674 "a %<chunk_size%> parameter");
30675 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
30676 error_at (token
->location
, "schedule %<auto%> does not take "
30677 "a %<chunk_size%> parameter");
30679 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
30681 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30684 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
30687 OMP_CLAUSE_SCHEDULE_KIND (c
)
30688 = (enum omp_clause_schedule_kind
)
30689 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
30691 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
30692 OMP_CLAUSE_CHAIN (c
) = list
;
30696 cp_parser_error (parser
, "invalid schedule kind");
30698 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30699 /*or_comma=*/false,
30700 /*consume_paren=*/true);
30708 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
30709 tree list
, location_t location
)
30713 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
30715 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
30716 OMP_CLAUSE_CHAIN (c
) = list
;
30725 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
30726 tree list
, location_t location
)
30728 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
30729 tree c
= build_omp_clause (location
, code
);
30730 OMP_CLAUSE_CHAIN (c
) = list
;
30741 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
30742 enum omp_clause_code code
,
30743 tree list
, location_t location
)
30745 tree c
= build_omp_clause (location
, code
);
30746 OMP_CLAUSE_CHAIN (c
) = list
;
30754 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
30755 tree list
, location_t location
)
30757 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
30758 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
30759 OMP_CLAUSE_CHAIN (c
) = list
;
30768 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
30769 enum omp_clause_code code
,
30770 tree list
, location_t location
)
30772 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
30773 tree c
= build_omp_clause (location
, code
);
30774 OMP_CLAUSE_CHAIN (c
) = list
;
30779 num_teams ( expression ) */
30782 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
30783 location_t location
)
30787 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30790 t
= cp_parser_expression (parser
);
30792 if (t
== error_mark_node
30793 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30794 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30795 /*or_comma=*/false,
30796 /*consume_paren=*/true);
30798 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
30799 "num_teams", location
);
30801 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
30802 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
30803 OMP_CLAUSE_CHAIN (c
) = list
;
30809 thread_limit ( expression ) */
30812 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
30813 location_t location
)
30817 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30820 t
= cp_parser_expression (parser
);
30822 if (t
== error_mark_node
30823 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30824 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30825 /*or_comma=*/false,
30826 /*consume_paren=*/true);
30828 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
30829 "thread_limit", location
);
30831 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
30832 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
30833 OMP_CLAUSE_CHAIN (c
) = list
;
30839 aligned ( variable-list )
30840 aligned ( variable-list : constant-expression ) */
30843 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
30845 tree nlist
, c
, alignment
= NULL_TREE
;
30848 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30851 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
30856 alignment
= cp_parser_constant_expression (parser
);
30858 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30859 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30860 /*or_comma=*/false,
30861 /*consume_paren=*/true);
30863 if (alignment
== error_mark_node
)
30864 alignment
= NULL_TREE
;
30867 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30868 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
30874 linear ( variable-list )
30875 linear ( variable-list : expression )
30878 linear ( modifier ( variable-list ) )
30879 linear ( modifier ( variable-list ) : expression ) */
30882 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
30883 bool is_cilk_simd_fn
, bool declare_simd
)
30885 tree nlist
, c
, step
= integer_one_node
;
30887 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
30889 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30892 if (!is_cilk_simd_fn
30893 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30895 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30896 const char *p
= IDENTIFIER_POINTER (id
);
30898 if (strcmp ("ref", p
) == 0)
30899 kind
= OMP_CLAUSE_LINEAR_REF
;
30900 else if (strcmp ("val", p
) == 0)
30901 kind
= OMP_CLAUSE_LINEAR_VAL
;
30902 else if (strcmp ("uval", p
) == 0)
30903 kind
= OMP_CLAUSE_LINEAR_UVAL
;
30904 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
30905 cp_lexer_consume_token (parser
->lexer
);
30907 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
30910 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
30911 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
30915 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
30916 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
30918 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
30919 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30920 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30921 /*or_comma=*/false,
30922 /*consume_paren=*/true);
30929 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
30930 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
30932 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30933 cp_parser_parse_tentatively (parser
);
30934 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
30935 /*check_dependency_p=*/true,
30936 /*template_p=*/NULL
,
30937 /*declarator_p=*/false,
30938 /*optional_p=*/false);
30939 if (step
!= error_mark_node
)
30940 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
30941 if (step
== error_mark_node
)
30944 cp_parser_abort_tentative_parse (parser
);
30946 else if (!cp_parser_parse_definitely (parser
))
30950 step
= cp_parser_expression (parser
);
30952 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
30954 sorry ("using parameters for %<linear%> step is not supported yet");
30955 step
= integer_one_node
;
30957 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30958 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30959 /*or_comma=*/false,
30960 /*consume_paren=*/true);
30962 if (step
== error_mark_node
)
30966 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
30968 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
30969 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
30976 safelen ( constant-expression ) */
30979 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
30980 location_t location
)
30984 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30987 t
= cp_parser_constant_expression (parser
);
30989 if (t
== error_mark_node
30990 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30991 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30992 /*or_comma=*/false,
30993 /*consume_paren=*/true);
30995 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
30997 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
30998 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
30999 OMP_CLAUSE_CHAIN (c
) = list
;
31005 simdlen ( constant-expression ) */
31008 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
31009 location_t location
)
31013 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31016 t
= cp_parser_constant_expression (parser
);
31018 if (t
== error_mark_node
31019 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31020 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31021 /*or_comma=*/false,
31022 /*consume_paren=*/true);
31024 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
31026 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
31027 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
31028 OMP_CLAUSE_CHAIN (c
) = list
;
31035 identifier [+/- integer]
31036 vec , identifier [+/- integer]
31040 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
31045 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31047 cp_parser_error (parser
, "expected identifier");
31051 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31053 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31054 tree t
, identifier
= cp_parser_identifier (parser
);
31055 tree addend
= NULL
;
31057 if (identifier
== error_mark_node
)
31058 t
= error_mark_node
;
31061 t
= cp_parser_lookup_name_simple
31062 (parser
, identifier
,
31063 cp_lexer_peek_token (parser
->lexer
)->location
);
31064 if (t
== error_mark_node
)
31065 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
31070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31072 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
31074 addend
= integer_zero_node
;
31075 goto add_to_vector
;
31077 cp_lexer_consume_token (parser
->lexer
);
31079 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
31081 cp_parser_error (parser
, "expected integer");
31085 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31086 if (TREE_CODE (addend
) != INTEGER_CST
)
31088 cp_parser_error (parser
, "expected integer");
31091 cp_lexer_consume_token (parser
->lexer
);
31094 if (t
!= error_mark_node
)
31096 vec
= tree_cons (addend
, t
, vec
);
31098 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
31101 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
31104 cp_lexer_consume_token (parser
->lexer
);
31107 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
31109 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
31110 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
31111 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
31112 OMP_CLAUSE_CHAIN (u
) = list
;
31119 depend ( depend-kind : variable-list )
31127 depend ( sink : vec ) */
31130 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
31133 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
31135 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31140 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31141 const char *p
= IDENTIFIER_POINTER (id
);
31143 if (strcmp ("in", p
) == 0)
31144 kind
= OMP_CLAUSE_DEPEND_IN
;
31145 else if (strcmp ("inout", p
) == 0)
31146 kind
= OMP_CLAUSE_DEPEND_INOUT
;
31147 else if (strcmp ("out", p
) == 0)
31148 kind
= OMP_CLAUSE_DEPEND_OUT
;
31149 else if (strcmp ("source", p
) == 0)
31150 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
31151 else if (strcmp ("sink", p
) == 0)
31152 kind
= OMP_CLAUSE_DEPEND_SINK
;
31159 cp_lexer_consume_token (parser
->lexer
);
31161 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
31163 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
31164 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31165 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
31166 OMP_CLAUSE_CHAIN (c
) = list
;
31167 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31168 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31169 /*or_comma=*/false,
31170 /*consume_paren=*/true);
31174 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31177 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
31178 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
31181 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
31184 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31185 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
31190 cp_parser_error (parser
, "invalid depend kind");
31192 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31193 /*or_comma=*/false,
31194 /*consume_paren=*/true);
31199 map ( map-kind : variable-list )
31200 map ( variable-list )
31203 alloc | to | from | tofrom
31207 alloc | to | from | tofrom | release | delete
31209 map ( always [,] map-kind: variable-list ) */
31212 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
31215 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
31216 bool always
= false;
31218 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31223 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31224 const char *p
= IDENTIFIER_POINTER (id
);
31226 if (strcmp ("always", p
) == 0)
31229 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
31231 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
31232 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
31234 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
31238 cp_lexer_consume_token (parser
->lexer
);
31240 cp_lexer_consume_token (parser
->lexer
);
31245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
31246 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31248 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31249 const char *p
= IDENTIFIER_POINTER (id
);
31251 if (strcmp ("alloc", p
) == 0)
31252 kind
= GOMP_MAP_ALLOC
;
31253 else if (strcmp ("to", p
) == 0)
31254 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
31255 else if (strcmp ("from", p
) == 0)
31256 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
31257 else if (strcmp ("tofrom", p
) == 0)
31258 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
31259 else if (strcmp ("release", p
) == 0)
31260 kind
= GOMP_MAP_RELEASE
;
31263 cp_parser_error (parser
, "invalid map kind");
31264 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31265 /*or_comma=*/false,
31266 /*consume_paren=*/true);
31269 cp_lexer_consume_token (parser
->lexer
);
31270 cp_lexer_consume_token (parser
->lexer
);
31272 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
31273 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
31275 kind
= GOMP_MAP_DELETE
;
31276 cp_lexer_consume_token (parser
->lexer
);
31277 cp_lexer_consume_token (parser
->lexer
);
31280 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
31283 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
31284 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
31290 device ( expression ) */
31293 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
31294 location_t location
)
31298 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31301 t
= cp_parser_expression (parser
);
31303 if (t
== error_mark_node
31304 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31305 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31306 /*or_comma=*/false,
31307 /*consume_paren=*/true);
31309 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
31310 "device", location
);
31312 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
31313 OMP_CLAUSE_DEVICE_ID (c
) = t
;
31314 OMP_CLAUSE_CHAIN (c
) = list
;
31320 dist_schedule ( static )
31321 dist_schedule ( static , expression ) */
31324 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
31325 location_t location
)
31329 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31332 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
31334 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
31336 cp_lexer_consume_token (parser
->lexer
);
31338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31340 cp_lexer_consume_token (parser
->lexer
);
31342 t
= cp_parser_assignment_expression (parser
);
31344 if (t
== error_mark_node
)
31346 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
31348 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31351 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31354 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
31356 OMP_CLAUSE_CHAIN (c
) = list
;
31360 cp_parser_error (parser
, "invalid dist_schedule kind");
31362 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31363 /*or_comma=*/false,
31364 /*consume_paren=*/true);
31369 proc_bind ( proc-bind-kind )
31372 master | close | spread */
31375 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
31376 location_t location
)
31379 enum omp_clause_proc_bind_kind kind
;
31381 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31386 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31387 const char *p
= IDENTIFIER_POINTER (id
);
31389 if (strcmp ("master", p
) == 0)
31390 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
31391 else if (strcmp ("close", p
) == 0)
31392 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
31393 else if (strcmp ("spread", p
) == 0)
31394 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
31401 cp_lexer_consume_token (parser
->lexer
);
31402 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
31405 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
31406 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
31408 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
31409 OMP_CLAUSE_CHAIN (c
) = list
;
31413 cp_parser_error (parser
, "invalid depend kind");
31415 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
31416 /*or_comma=*/false,
31417 /*consume_paren=*/true);
31422 async [( int-expr )] */
31425 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
31428 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31430 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
31432 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31434 cp_lexer_consume_token (parser
->lexer
);
31436 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);
31444 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
31446 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
31447 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
31448 OMP_CLAUSE_CHAIN (c
) = list
;
31454 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31455 is a bitmask in MASK. Return the list of clauses found. */
31458 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
31459 const char *where
, cp_token
*pragma_tok
,
31460 bool finish_p
= true)
31462 tree clauses
= NULL
;
31465 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31468 pragma_omp_clause c_kind
;
31469 omp_clause_code code
;
31470 const char *c_name
;
31471 tree prev
= clauses
;
31473 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31474 cp_lexer_consume_token (parser
->lexer
);
31476 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
31477 c_kind
= cp_parser_omp_clause_name (parser
);
31481 case PRAGMA_OACC_CLAUSE_ASYNC
:
31482 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
31485 case PRAGMA_OACC_CLAUSE_AUTO
:
31486 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_AUTO
,
31490 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
31491 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
31492 c_name
= "collapse";
31494 case PRAGMA_OACC_CLAUSE_COPY
:
31495 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31498 case PRAGMA_OACC_CLAUSE_COPYIN
:
31499 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31502 case PRAGMA_OACC_CLAUSE_COPYOUT
:
31503 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31504 c_name
= "copyout";
31506 case PRAGMA_OACC_CLAUSE_CREATE
:
31507 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31510 case PRAGMA_OACC_CLAUSE_DELETE
:
31511 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31514 case PRAGMA_OMP_CLAUSE_DEFAULT
:
31515 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
31516 c_name
= "default";
31518 case PRAGMA_OACC_CLAUSE_DEVICE
:
31519 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31522 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
31523 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
31524 c_name
= "deviceptr";
31526 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
31527 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31528 c_name
= "device_resident";
31530 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
31531 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31533 c_name
= "firstprivate";
31535 case PRAGMA_OACC_CLAUSE_GANG
:
31537 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_GANG
,
31540 case PRAGMA_OACC_CLAUSE_HOST
:
31541 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31544 case PRAGMA_OACC_CLAUSE_IF
:
31545 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
31548 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
31549 clauses
= cp_parser_oacc_simple_clause (parser
,
31550 OMP_CLAUSE_INDEPENDENT
,
31552 c_name
= "independent";
31554 case PRAGMA_OACC_CLAUSE_LINK
:
31555 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31558 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
31559 code
= OMP_CLAUSE_NUM_GANGS
;
31560 c_name
= "num_gangs";
31561 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31564 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
31565 c_name
= "num_workers";
31566 code
= OMP_CLAUSE_NUM_WORKERS
;
31567 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31570 case PRAGMA_OACC_CLAUSE_PRESENT
:
31571 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31572 c_name
= "present";
31574 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
31575 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31576 c_name
= "present_or_copy";
31578 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
31579 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31580 c_name
= "present_or_copyin";
31582 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
31583 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31584 c_name
= "present_or_copyout";
31586 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
31587 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31588 c_name
= "present_or_create";
31590 case PRAGMA_OACC_CLAUSE_PRIVATE
:
31591 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
31593 c_name
= "private";
31595 case PRAGMA_OACC_CLAUSE_REDUCTION
:
31596 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31597 c_name
= "reduction";
31599 case PRAGMA_OACC_CLAUSE_SELF
:
31600 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
31603 case PRAGMA_OACC_CLAUSE_SEQ
:
31604 clauses
= cp_parser_oacc_simple_clause (parser
, OMP_CLAUSE_SEQ
,
31608 case PRAGMA_OACC_CLAUSE_TILE
:
31609 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
31612 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
31613 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE
,
31615 c_name
= "use_device";
31617 case PRAGMA_OACC_CLAUSE_VECTOR
:
31619 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_VECTOR
,
31622 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
31623 c_name
= "vector_length";
31624 code
= OMP_CLAUSE_VECTOR_LENGTH
;
31625 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
31628 case PRAGMA_OACC_CLAUSE_WAIT
:
31629 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
31632 case PRAGMA_OACC_CLAUSE_WORKER
:
31634 clauses
= cp_parser_oacc_shape_clause (parser
, OMP_CLAUSE_WORKER
,
31638 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
31644 if (((mask
>> c_kind
) & 1) == 0)
31646 /* Remove the invalid clause(s) from the list to avoid
31647 confusing the rest of the compiler. */
31649 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
31654 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31657 return finish_omp_clauses (clauses
, false);
31662 /* Parse all OpenMP clauses. The set clauses allowed by the directive
31663 is a bitmask in MASK. Return the list of clauses found; the result
31664 of clause default goes in *pdefault. */
31667 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
31668 const char *where
, cp_token
*pragma_tok
,
31669 bool finish_p
= true)
31671 tree clauses
= NULL
;
31673 cp_token
*token
= NULL
;
31675 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31677 pragma_omp_clause c_kind
;
31678 const char *c_name
;
31679 tree prev
= clauses
;
31681 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31682 cp_lexer_consume_token (parser
->lexer
);
31684 token
= cp_lexer_peek_token (parser
->lexer
);
31685 c_kind
= cp_parser_omp_clause_name (parser
);
31689 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
31690 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
31692 c_name
= "collapse";
31694 case PRAGMA_OMP_CLAUSE_COPYIN
:
31695 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
31698 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
31699 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
31701 c_name
= "copyprivate";
31703 case PRAGMA_OMP_CLAUSE_DEFAULT
:
31704 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
31705 token
->location
, false);
31706 c_name
= "default";
31708 case PRAGMA_OMP_CLAUSE_FINAL
:
31709 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
31712 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
31713 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
31715 c_name
= "firstprivate";
31717 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
31718 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
31720 c_name
= "grainsize";
31722 case PRAGMA_OMP_CLAUSE_HINT
:
31723 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
31727 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
31728 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
31730 c_name
= "defaultmap";
31732 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
31733 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
31735 c_name
= "use_device_ptr";
31737 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
31738 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
31740 c_name
= "is_device_ptr";
31742 case PRAGMA_OMP_CLAUSE_IF
:
31743 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
31747 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
31748 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
31750 c_name
= "lastprivate";
31752 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
31753 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
31755 c_name
= "mergeable";
31757 case PRAGMA_OMP_CLAUSE_NOWAIT
:
31758 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
31761 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
31762 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
31764 c_name
= "num_tasks";
31766 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
31767 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
31769 c_name
= "num_threads";
31771 case PRAGMA_OMP_CLAUSE_ORDERED
:
31772 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
31774 c_name
= "ordered";
31776 case PRAGMA_OMP_CLAUSE_PRIORITY
:
31777 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
31779 c_name
= "priority";
31781 case PRAGMA_OMP_CLAUSE_PRIVATE
:
31782 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
31784 c_name
= "private";
31786 case PRAGMA_OMP_CLAUSE_REDUCTION
:
31787 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
31788 c_name
= "reduction";
31790 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
31791 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
31793 c_name
= "schedule";
31795 case PRAGMA_OMP_CLAUSE_SHARED
:
31796 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
31800 case PRAGMA_OMP_CLAUSE_UNTIED
:
31801 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
31805 case PRAGMA_OMP_CLAUSE_INBRANCH
:
31806 case PRAGMA_CILK_CLAUSE_MASK
:
31807 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
31808 clauses
, token
->location
);
31809 c_name
= "inbranch";
31811 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
31812 case PRAGMA_CILK_CLAUSE_NOMASK
:
31813 clauses
= cp_parser_omp_clause_branch (parser
,
31814 OMP_CLAUSE_NOTINBRANCH
,
31815 clauses
, token
->location
);
31816 c_name
= "notinbranch";
31818 case PRAGMA_OMP_CLAUSE_PARALLEL
:
31819 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
31820 clauses
, token
->location
);
31821 c_name
= "parallel";
31825 error_at (token
->location
, "%qs must be the first clause of %qs",
31830 case PRAGMA_OMP_CLAUSE_FOR
:
31831 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
31832 clauses
, token
->location
);
31835 goto clause_not_first
;
31837 case PRAGMA_OMP_CLAUSE_SECTIONS
:
31838 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
31839 clauses
, token
->location
);
31840 c_name
= "sections";
31842 goto clause_not_first
;
31844 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
31845 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
31846 clauses
, token
->location
);
31847 c_name
= "taskgroup";
31849 goto clause_not_first
;
31851 case PRAGMA_OMP_CLAUSE_LINK
:
31852 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
31855 case PRAGMA_OMP_CLAUSE_TO
:
31856 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
31857 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
31860 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
31863 case PRAGMA_OMP_CLAUSE_FROM
:
31864 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
31867 case PRAGMA_OMP_CLAUSE_UNIFORM
:
31868 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
31870 c_name
= "uniform";
31872 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
31873 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
31875 c_name
= "num_teams";
31877 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
31878 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
31880 c_name
= "thread_limit";
31882 case PRAGMA_OMP_CLAUSE_ALIGNED
:
31883 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
31884 c_name
= "aligned";
31886 case PRAGMA_OMP_CLAUSE_LINEAR
:
31888 bool cilk_simd_fn
= false, declare_simd
= false;
31889 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
31890 cilk_simd_fn
= true;
31891 else if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
31892 declare_simd
= true;
31893 clauses
= cp_parser_omp_clause_linear (parser
, clauses
,
31894 cilk_simd_fn
, declare_simd
);
31898 case PRAGMA_OMP_CLAUSE_DEPEND
:
31899 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
31903 case PRAGMA_OMP_CLAUSE_MAP
:
31904 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
31907 case PRAGMA_OMP_CLAUSE_DEVICE
:
31908 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
31912 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
31913 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
31915 c_name
= "dist_schedule";
31917 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
31918 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
31920 c_name
= "proc_bind";
31922 case PRAGMA_OMP_CLAUSE_SAFELEN
:
31923 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
31925 c_name
= "safelen";
31927 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
31928 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
31930 c_name
= "simdlen";
31932 case PRAGMA_OMP_CLAUSE_NOGROUP
:
31933 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
31935 c_name
= "nogroup";
31937 case PRAGMA_OMP_CLAUSE_THREADS
:
31939 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
31940 clauses
, token
->location
);
31941 c_name
= "threads";
31943 case PRAGMA_OMP_CLAUSE_SIMD
:
31945 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
31946 clauses
, token
->location
);
31949 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
31950 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
31951 c_name
= "simdlen";
31954 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
31960 if (((mask
>> c_kind
) & 1) == 0)
31962 /* Remove the invalid clause(s) from the list to avoid
31963 confusing the rest of the compiler. */
31965 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
31969 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
31970 no reason to skip to the end. */
31971 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
31972 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31975 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
31976 return finish_omp_clauses (clauses
, false, true);
31978 return finish_omp_clauses (clauses
, true);
31987 In practice, we're also interested in adding the statement to an
31988 outer node. So it is convenient if we work around the fact that
31989 cp_parser_statement calls add_stmt. */
31992 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
31994 unsigned save
= parser
->in_statement
;
31996 /* Only move the values to IN_OMP_BLOCK if they weren't false.
31997 This preserves the "not within loop or switch" style error messages
31998 for nonsense cases like
32004 if (parser
->in_statement
)
32005 parser
->in_statement
= IN_OMP_BLOCK
;
32011 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
32013 parser
->in_statement
= save
;
32017 cp_parser_omp_structured_block (cp_parser
*parser
)
32019 tree stmt
= begin_omp_structured_block ();
32020 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
32022 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
32024 cp_parser_end_omp_structured_block (parser
, save
);
32025 return finish_omp_structured_block (stmt
);
32029 # pragma omp atomic new-line
32033 x binop= expr | x++ | ++x | x-- | --x
32035 +, *, -, /, &, ^, |, <<, >>
32037 where x is an lvalue expression with scalar type.
32040 # pragma omp atomic new-line
32043 # pragma omp atomic read new-line
32046 # pragma omp atomic write new-line
32049 # pragma omp atomic update new-line
32052 # pragma omp atomic capture new-line
32055 # pragma omp atomic capture new-line
32063 expression-stmt | x = x binop expr
32065 v = expression-stmt
32067 { v = x; update-stmt; } | { update-stmt; v = x; }
32071 expression-stmt | x = x binop expr | x = expr binop x
32075 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32077 where x and v are lvalue expressions with scalar type. */
32080 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
32082 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
32083 tree rhs1
= NULL_TREE
, orig_lhs
;
32084 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
32085 bool structured_block
= false;
32086 bool seq_cst
= false;
32088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32090 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32091 const char *p
= IDENTIFIER_POINTER (id
);
32093 if (!strcmp (p
, "seq_cst"))
32096 cp_lexer_consume_token (parser
->lexer
);
32097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32098 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32099 cp_lexer_consume_token (parser
->lexer
);
32102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32104 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32105 const char *p
= IDENTIFIER_POINTER (id
);
32107 if (!strcmp (p
, "read"))
32108 code
= OMP_ATOMIC_READ
;
32109 else if (!strcmp (p
, "write"))
32111 else if (!strcmp (p
, "update"))
32113 else if (!strcmp (p
, "capture"))
32114 code
= OMP_ATOMIC_CAPTURE_NEW
;
32118 cp_lexer_consume_token (parser
->lexer
);
32122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
32123 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
32124 cp_lexer_consume_token (parser
->lexer
);
32126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32128 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32129 const char *p
= IDENTIFIER_POINTER (id
);
32131 if (!strcmp (p
, "seq_cst"))
32134 cp_lexer_consume_token (parser
->lexer
);
32138 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32142 case OMP_ATOMIC_READ
:
32143 case NOP_EXPR
: /* atomic write */
32144 v
= cp_parser_unary_expression (parser
);
32145 if (v
== error_mark_node
)
32147 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32149 if (code
== NOP_EXPR
)
32150 lhs
= cp_parser_expression (parser
);
32152 lhs
= cp_parser_unary_expression (parser
);
32153 if (lhs
== error_mark_node
)
32155 if (code
== NOP_EXPR
)
32157 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32165 case OMP_ATOMIC_CAPTURE_NEW
:
32166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32168 cp_lexer_consume_token (parser
->lexer
);
32169 structured_block
= true;
32173 v
= cp_parser_unary_expression (parser
);
32174 if (v
== error_mark_node
)
32176 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32184 lhs
= cp_parser_unary_expression (parser
);
32186 switch (TREE_CODE (lhs
))
32191 case POSTINCREMENT_EXPR
:
32192 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32193 code
= OMP_ATOMIC_CAPTURE_OLD
;
32195 case PREINCREMENT_EXPR
:
32196 lhs
= TREE_OPERAND (lhs
, 0);
32197 opcode
= PLUS_EXPR
;
32198 rhs
= integer_one_node
;
32201 case POSTDECREMENT_EXPR
:
32202 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
32203 code
= OMP_ATOMIC_CAPTURE_OLD
;
32205 case PREDECREMENT_EXPR
:
32206 lhs
= TREE_OPERAND (lhs
, 0);
32207 opcode
= MINUS_EXPR
;
32208 rhs
= integer_one_node
;
32211 case COMPOUND_EXPR
:
32212 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
32213 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
32214 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
32215 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
32216 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32217 (TREE_OPERAND (lhs
, 1), 0), 0)))
32219 /* Undo effects of boolean_increment for post {in,de}crement. */
32220 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
32223 if (TREE_CODE (lhs
) == MODIFY_EXPR
32224 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
32226 /* Undo effects of boolean_increment. */
32227 if (integer_onep (TREE_OPERAND (lhs
, 1)))
32229 /* This is pre or post increment. */
32230 rhs
= TREE_OPERAND (lhs
, 1);
32231 lhs
= TREE_OPERAND (lhs
, 0);
32233 if (code
== OMP_ATOMIC_CAPTURE_NEW
32234 && !structured_block
32235 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
32236 code
= OMP_ATOMIC_CAPTURE_OLD
;
32242 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32245 opcode
= MULT_EXPR
;
32248 opcode
= TRUNC_DIV_EXPR
;
32251 opcode
= PLUS_EXPR
;
32254 opcode
= MINUS_EXPR
;
32256 case CPP_LSHIFT_EQ
:
32257 opcode
= LSHIFT_EXPR
;
32259 case CPP_RSHIFT_EQ
:
32260 opcode
= RSHIFT_EXPR
;
32263 opcode
= BIT_AND_EXPR
;
32266 opcode
= BIT_IOR_EXPR
;
32269 opcode
= BIT_XOR_EXPR
;
32272 enum cp_parser_prec oprec
;
32274 cp_lexer_consume_token (parser
->lexer
);
32275 cp_parser_parse_tentatively (parser
);
32276 rhs1
= cp_parser_simple_cast_expression (parser
);
32277 if (rhs1
== error_mark_node
)
32279 cp_parser_abort_tentative_parse (parser
);
32280 cp_parser_simple_cast_expression (parser
);
32283 token
= cp_lexer_peek_token (parser
->lexer
);
32284 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
32286 cp_parser_abort_tentative_parse (parser
);
32287 cp_parser_parse_tentatively (parser
);
32288 rhs
= cp_parser_binary_expression (parser
, false, true,
32289 PREC_NOT_OPERATOR
, NULL
);
32290 if (rhs
== error_mark_node
)
32292 cp_parser_abort_tentative_parse (parser
);
32293 cp_parser_binary_expression (parser
, false, true,
32294 PREC_NOT_OPERATOR
, NULL
);
32297 switch (TREE_CODE (rhs
))
32300 case TRUNC_DIV_EXPR
:
32309 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
32311 if (cp_parser_parse_definitely (parser
))
32313 opcode
= TREE_CODE (rhs
);
32314 rhs1
= TREE_OPERAND (rhs
, 0);
32315 rhs
= TREE_OPERAND (rhs
, 1);
32325 cp_parser_abort_tentative_parse (parser
);
32326 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
32328 rhs
= cp_parser_expression (parser
);
32329 if (rhs
== error_mark_node
)
32335 cp_parser_error (parser
,
32336 "invalid form of %<#pragma omp atomic%>");
32339 if (!cp_parser_parse_definitely (parser
))
32341 switch (token
->type
)
32343 case CPP_SEMICOLON
:
32344 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
32346 code
= OMP_ATOMIC_CAPTURE_OLD
;
32351 cp_lexer_consume_token (parser
->lexer
);
32354 else if (structured_block
)
32361 cp_parser_error (parser
,
32362 "invalid form of %<#pragma omp atomic%>");
32365 opcode
= MULT_EXPR
;
32368 opcode
= TRUNC_DIV_EXPR
;
32371 opcode
= PLUS_EXPR
;
32374 opcode
= MINUS_EXPR
;
32377 opcode
= LSHIFT_EXPR
;
32380 opcode
= RSHIFT_EXPR
;
32383 opcode
= BIT_AND_EXPR
;
32386 opcode
= BIT_IOR_EXPR
;
32389 opcode
= BIT_XOR_EXPR
;
32392 cp_parser_error (parser
,
32393 "invalid operator for %<#pragma omp atomic%>");
32396 oprec
= TOKEN_PRECEDENCE (token
);
32397 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
32398 if (commutative_tree_code (opcode
))
32399 oprec
= (enum cp_parser_prec
) (oprec
- 1);
32400 cp_lexer_consume_token (parser
->lexer
);
32401 rhs
= cp_parser_binary_expression (parser
, false, false,
32403 if (rhs
== error_mark_node
)
32408 cp_parser_error (parser
,
32409 "invalid operator for %<#pragma omp atomic%>");
32412 cp_lexer_consume_token (parser
->lexer
);
32414 rhs
= cp_parser_expression (parser
);
32415 if (rhs
== error_mark_node
)
32420 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
32422 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
32424 v
= cp_parser_unary_expression (parser
);
32425 if (v
== error_mark_node
)
32427 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32429 lhs1
= cp_parser_unary_expression (parser
);
32430 if (lhs1
== error_mark_node
)
32433 if (structured_block
)
32435 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32436 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
32439 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
32440 if (!structured_block
)
32441 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32445 cp_parser_skip_to_end_of_block_or_statement (parser
);
32446 if (structured_block
)
32448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
32449 cp_lexer_consume_token (parser
->lexer
);
32450 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
32452 cp_parser_skip_to_end_of_block_or_statement (parser
);
32453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
32454 cp_lexer_consume_token (parser
->lexer
);
32461 # pragma omp barrier new-line */
32464 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
32466 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32467 finish_omp_barrier ();
32471 # pragma omp critical [(name)] new-line
32475 # pragma omp critical [(name) [hint(expression)]] new-line
32476 structured-block */
32478 #define OMP_CRITICAL_CLAUSE_MASK \
32479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
32482 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
32484 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
32486 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32488 cp_lexer_consume_token (parser
->lexer
);
32490 name
= cp_parser_identifier (parser
);
32492 if (name
== error_mark_node
32493 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32494 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
32495 /*or_comma=*/false,
32496 /*consume_paren=*/true);
32497 if (name
== error_mark_node
)
32500 clauses
= cp_parser_omp_all_clauses (parser
,
32501 OMP_CRITICAL_CLAUSE_MASK
,
32502 "#pragma omp critical", pragma_tok
);
32505 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32507 stmt
= cp_parser_omp_structured_block (parser
);
32508 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
32512 # pragma omp flush flush-vars[opt] new-line
32515 ( variable-list ) */
32518 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
32520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32521 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
32522 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32524 finish_omp_flush ();
32527 /* Helper function, to parse omp for increment expression. */
32530 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
32532 tree cond
= cp_parser_binary_expression (parser
, false, true,
32533 PREC_NOT_OPERATOR
, NULL
);
32534 if (cond
== error_mark_node
32535 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
32537 cp_parser_skip_to_end_of_statement (parser
);
32538 return error_mark_node
;
32541 switch (TREE_CODE (cond
))
32549 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
32551 /* Fall through: OpenMP disallows NE_EXPR. */
32553 return error_mark_node
;
32556 /* If decl is an iterator, preserve LHS and RHS of the relational
32557 expr until finish_omp_for. */
32559 && (type_dependent_expression_p (decl
)
32560 || CLASS_TYPE_P (TREE_TYPE (decl
))))
32563 return build_x_binary_op (input_location
, TREE_CODE (cond
),
32564 TREE_OPERAND (cond
, 0), ERROR_MARK
,
32565 TREE_OPERAND (cond
, 1), ERROR_MARK
,
32566 /*overload=*/NULL
, tf_warning_or_error
);
32569 /* Helper function, to parse omp for increment expression. */
32572 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
32574 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32580 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
32582 op
= (token
->type
== CPP_PLUS_PLUS
32583 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
32584 cp_lexer_consume_token (parser
->lexer
);
32585 lhs
= cp_parser_simple_cast_expression (parser
);
32587 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
32588 return error_mark_node
;
32589 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
32592 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
32594 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
32595 return error_mark_node
;
32597 token
= cp_lexer_peek_token (parser
->lexer
);
32598 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
32600 op
= (token
->type
== CPP_PLUS_PLUS
32601 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
32602 cp_lexer_consume_token (parser
->lexer
);
32603 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
32606 op
= cp_parser_assignment_operator_opt (parser
);
32607 if (op
== ERROR_MARK
)
32608 return error_mark_node
;
32610 if (op
!= NOP_EXPR
)
32612 rhs
= cp_parser_assignment_expression (parser
);
32613 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
32614 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
32617 lhs
= cp_parser_binary_expression (parser
, false, false,
32618 PREC_ADDITIVE_EXPRESSION
, NULL
);
32619 token
= cp_lexer_peek_token (parser
->lexer
);
32620 decl_first
= (lhs
== decl
32621 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
32624 if (token
->type
!= CPP_PLUS
32625 && token
->type
!= CPP_MINUS
)
32626 return error_mark_node
;
32630 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
32631 cp_lexer_consume_token (parser
->lexer
);
32632 rhs
= cp_parser_binary_expression (parser
, false, false,
32633 PREC_ADDITIVE_EXPRESSION
, NULL
);
32634 token
= cp_lexer_peek_token (parser
->lexer
);
32635 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
32637 if (lhs
== NULL_TREE
)
32639 if (op
== PLUS_EXPR
)
32642 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
32643 tf_warning_or_error
);
32646 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
32647 ERROR_MARK
, NULL
, tf_warning_or_error
);
32650 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
32655 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
32656 || op
== MINUS_EXPR
)
32657 return error_mark_node
;
32658 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
32661 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
32663 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
32666 /* Parse the initialization statement of either an OpenMP for loop or
32667 a Cilk Plus for loop.
32669 Return true if the resulting construct should have an
32670 OMP_CLAUSE_PRIVATE added to it. */
32673 cp_parser_omp_for_loop_init (cp_parser
*parser
,
32674 enum tree_code code
,
32675 tree
&this_pre_body
,
32676 vec
<tree
, va_gc
> *for_block
,
32682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32685 tree add_private_clause
= NULL_TREE
;
32687 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
32691 integer-type var = lb
32692 random-access-iterator-type var = lb
32693 pointer-type var = lb
32695 cp_decl_specifier_seq type_specifiers
;
32697 /* First, try to parse as an initialized declaration. See
32698 cp_parser_condition, from whence the bulk of this is copied. */
32700 cp_parser_parse_tentatively (parser
);
32701 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
32702 /*is_trailing_return=*/false,
32704 if (cp_parser_parse_definitely (parser
))
32706 /* If parsing a type specifier seq succeeded, then this
32707 MUST be a initialized declaration. */
32708 tree asm_specification
, attributes
;
32709 cp_declarator
*declarator
;
32711 declarator
= cp_parser_declarator (parser
,
32712 CP_PARSER_DECLARATOR_NAMED
,
32713 /*ctor_dtor_or_conv_p=*/NULL
,
32714 /*parenthesized_p=*/NULL
,
32715 /*member_p=*/false,
32716 /*friend_p=*/false);
32717 attributes
= cp_parser_attributes_opt (parser
);
32718 asm_specification
= cp_parser_asm_specification_opt (parser
);
32720 if (declarator
== cp_error_declarator
)
32721 cp_parser_skip_to_end_of_statement (parser
);
32725 tree pushed_scope
, auto_node
;
32727 decl
= start_decl (declarator
, &type_specifiers
,
32728 SD_INITIALIZED
, attributes
,
32729 /*prefix_attributes=*/NULL_TREE
,
32732 auto_node
= type_uses_auto (TREE_TYPE (decl
));
32733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
32735 if (cp_lexer_next_token_is (parser
->lexer
,
32738 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
32739 error ("parenthesized initialization is not allowed in "
32740 "OpenMP %<for%> loop");
32742 error ("parenthesized initialization is "
32743 "not allowed in for-loop");
32746 /* Trigger an error. */
32747 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
32749 init
= error_mark_node
;
32750 cp_parser_skip_to_end_of_statement (parser
);
32752 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
32753 || type_dependent_expression_p (decl
)
32756 bool is_direct_init
, is_non_constant_init
;
32758 init
= cp_parser_initializer (parser
,
32760 &is_non_constant_init
);
32765 = do_auto_deduction (TREE_TYPE (decl
), init
,
32768 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
32769 && !type_dependent_expression_p (decl
))
32773 cp_finish_decl (decl
, init
, !is_non_constant_init
,
32775 LOOKUP_ONLYCONVERTING
);
32777 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
32779 vec_safe_push (for_block
, this_pre_body
);
32783 init
= pop_stmt_list (this_pre_body
);
32784 this_pre_body
= NULL_TREE
;
32789 cp_lexer_consume_token (parser
->lexer
);
32790 init
= cp_parser_assignment_expression (parser
);
32793 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
32794 init
= error_mark_node
;
32796 cp_finish_decl (decl
, NULL_TREE
,
32797 /*init_const_expr_p=*/false,
32799 LOOKUP_ONLYCONVERTING
);
32803 pop_scope (pushed_scope
);
32809 /* If parsing a type specifier sequence failed, then
32810 this MUST be a simple expression. */
32811 if (code
== CILK_FOR
)
32812 error ("%<_Cilk_for%> allows expression instead of declaration only "
32813 "in C, not in C++");
32814 cp_parser_parse_tentatively (parser
);
32815 decl
= cp_parser_primary_expression (parser
, false, false,
32817 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
32818 if (!cp_parser_error_occurred (parser
)
32820 && (TREE_CODE (decl
) == COMPONENT_REF
32821 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
32823 cp_parser_abort_tentative_parse (parser
);
32824 cp_parser_parse_tentatively (parser
);
32825 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32826 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32827 /*check_dependency_p=*/true,
32828 /*template_p=*/NULL
,
32829 /*declarator_p=*/false,
32830 /*optional_p=*/false);
32831 if (name
!= error_mark_node
32832 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
32834 decl
= cp_parser_lookup_name_simple (parser
, name
,
32836 if (TREE_CODE (decl
) == FIELD_DECL
)
32837 add_private_clause
= omp_privatize_field (decl
, false);
32839 cp_parser_abort_tentative_parse (parser
);
32840 cp_parser_parse_tentatively (parser
);
32841 decl
= cp_parser_primary_expression (parser
, false, false,
32844 if (!cp_parser_error_occurred (parser
)
32847 && CLASS_TYPE_P (TREE_TYPE (decl
)))
32851 cp_parser_parse_definitely (parser
);
32852 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
32853 rhs
= cp_parser_assignment_expression (parser
);
32855 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
32858 tf_warning_or_error
));
32859 if (!add_private_clause
)
32860 add_private_clause
= decl
;
32865 cp_parser_abort_tentative_parse (parser
);
32866 init
= cp_parser_expression (parser
);
32869 if (TREE_CODE (init
) == MODIFY_EXPR
32870 || TREE_CODE (init
) == MODOP_EXPR
)
32871 real_decl
= TREE_OPERAND (init
, 0);
32875 return add_private_clause
;
32878 /* Parse the restricted form of the for statement allowed by OpenMP. */
32881 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
32884 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
32885 tree real_decl
, initv
, condv
, incrv
, declv
;
32886 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
32887 location_t loc_first
;
32888 bool collapse_err
= false;
32889 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
32890 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
32891 auto_vec
<tree
, 4> orig_inits
;
32893 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
32894 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
32895 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
32896 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
32897 && OMP_CLAUSE_ORDERED_EXPR (cl
))
32900 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
32903 if (ordered
&& ordered
< collapse
)
32905 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
32906 "%<ordered%> clause parameter is less than %<collapse%>");
32907 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
32908 = build_int_cst (NULL_TREE
, collapse
);
32909 ordered
= collapse
;
32913 for (tree
*pc
= &clauses
; *pc
; )
32914 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
32916 error_at (OMP_CLAUSE_LOCATION (*pc
),
32917 "%<linear%> clause may not be specified together "
32918 "with %<ordered%> clause with a parameter");
32919 *pc
= OMP_CLAUSE_CHAIN (*pc
);
32922 pc
= &OMP_CLAUSE_CHAIN (*pc
);
32925 gcc_assert (collapse
>= 1 && ordered
>= 0);
32926 count
= ordered
? ordered
: collapse
;
32928 declv
= make_tree_vec (count
);
32929 initv
= make_tree_vec (count
);
32930 condv
= make_tree_vec (count
);
32931 incrv
= make_tree_vec (count
);
32933 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
32935 for (i
= 0; i
< count
; i
++)
32937 int bracecount
= 0;
32938 tree add_private_clause
= NULL_TREE
;
32941 if (code
!= CILK_FOR
32942 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
32944 cp_parser_error (parser
, "for statement expected");
32947 if (code
== CILK_FOR
32948 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32950 cp_parser_error (parser
, "_Cilk_for statement expected");
32953 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
32955 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32958 init
= orig_init
= decl
= real_decl
= NULL
;
32959 this_pre_body
= push_stmt_list ();
32962 = cp_parser_omp_for_loop_init (parser
, code
,
32963 this_pre_body
, for_block
,
32964 init
, orig_init
, decl
, real_decl
);
32966 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32969 this_pre_body
= pop_stmt_list (this_pre_body
);
32973 pre_body
= push_stmt_list ();
32975 add_stmt (this_pre_body
);
32976 pre_body
= pop_stmt_list (pre_body
);
32979 pre_body
= this_pre_body
;
32984 if (cclauses
!= NULL
32985 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
32986 && real_decl
!= NULL_TREE
)
32989 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
32990 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
32991 && OMP_CLAUSE_DECL (*c
) == real_decl
)
32993 error_at (loc
, "iteration variable %qD"
32994 " should not be firstprivate", real_decl
);
32995 *c
= OMP_CLAUSE_CHAIN (*c
);
32997 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
32998 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33000 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33002 *c
= OMP_CLAUSE_CHAIN (*c
);
33003 if (code
== OMP_SIMD
)
33005 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33006 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
33010 OMP_CLAUSE_CHAIN (l
) = clauses
;
33013 add_private_clause
= NULL_TREE
;
33017 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
33018 && OMP_CLAUSE_DECL (*c
) == real_decl
)
33019 add_private_clause
= NULL_TREE
;
33020 c
= &OMP_CLAUSE_CHAIN (*c
);
33024 if (add_private_clause
)
33027 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
33029 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
33030 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
33031 && OMP_CLAUSE_DECL (c
) == decl
)
33033 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
33034 && OMP_CLAUSE_DECL (c
) == decl
)
33035 error_at (loc
, "iteration variable %qD "
33036 "should not be firstprivate",
33038 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
33039 && OMP_CLAUSE_DECL (c
) == decl
)
33040 error_at (loc
, "iteration variable %qD should not be reduction",
33045 if (code
!= OMP_SIMD
)
33046 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
33047 else if (collapse
== 1)
33048 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33050 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
33051 OMP_CLAUSE_DECL (c
) = add_private_clause
;
33052 c
= finish_omp_clauses (c
, true);
33055 OMP_CLAUSE_CHAIN (c
) = clauses
;
33057 /* For linear, signal that we need to fill up
33058 the so far unknown linear step. */
33059 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
33060 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
33066 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33067 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
33068 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
33071 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
33073 /* If decl is an iterator, preserve the operator on decl
33074 until finish_omp_for. */
33076 && ((processing_template_decl
33077 && (TREE_TYPE (real_decl
) == NULL_TREE
33078 || !POINTER_TYPE_P (TREE_TYPE (real_decl
))))
33079 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
33080 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
33082 incr
= cp_parser_expression (parser
);
33083 if (!EXPR_HAS_LOCATION (incr
))
33084 protected_set_expr_location (incr
, input_location
);
33087 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33088 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33092 TREE_VEC_ELT (declv
, i
) = decl
;
33093 TREE_VEC_ELT (initv
, i
) = init
;
33094 TREE_VEC_ELT (condv
, i
) = cond
;
33095 TREE_VEC_ELT (incrv
, i
) = incr
;
33098 orig_inits
.safe_grow_cleared (i
+ 1);
33099 orig_inits
[i
] = orig_init
;
33102 if (i
== count
- 1)
33105 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33106 in between the collapsed for loops to be still considered perfectly
33107 nested. Hopefully the final version clarifies this.
33108 For now handle (multiple) {'s and empty statements. */
33109 cp_parser_parse_tentatively (parser
);
33112 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33114 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33116 cp_lexer_consume_token (parser
->lexer
);
33119 else if (bracecount
33120 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33121 cp_lexer_consume_token (parser
->lexer
);
33124 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33125 error_at (loc
, "not enough collapsed for loops");
33126 collapse_err
= true;
33127 cp_parser_abort_tentative_parse (parser
);
33136 cp_parser_parse_definitely (parser
);
33137 nbraces
+= bracecount
;
33141 /* Note that we saved the original contents of this flag when we entered
33142 the structured block, and so we don't need to re-save it here. */
33143 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
33144 parser
->in_statement
= IN_CILK_SIMD_FOR
;
33146 parser
->in_statement
= IN_OMP_FOR
;
33148 /* Note that the grammar doesn't call for a structured block here,
33149 though the loop as a whole is a structured block. */
33150 body
= push_stmt_list ();
33151 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33152 body
= pop_stmt_list (body
);
33154 if (declv
== NULL_TREE
)
33157 ret
= finish_omp_for (loc_first
, code
, declv
, NULL
, initv
, condv
, incrv
,
33158 body
, pre_body
, &orig_inits
, clauses
);
33162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33164 cp_lexer_consume_token (parser
->lexer
);
33167 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33168 cp_lexer_consume_token (parser
->lexer
);
33173 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33174 "collapsed loops not perfectly nested");
33176 collapse_err
= true;
33177 cp_parser_statement_seq_opt (parser
, NULL
);
33178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
33183 while (!for_block
->is_empty ())
33184 add_stmt (pop_stmt_list (for_block
->pop ()));
33185 release_tree_vector (for_block
);
33190 /* Helper function for OpenMP parsing, split clauses and call
33191 finish_omp_clauses on each of the set of clauses afterwards. */
33194 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
33195 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
33198 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
33199 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
33201 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], true);
33205 #pragma omp simd simd-clause[optseq] new-line
33208 #define OMP_SIMD_CLAUSE_MASK \
33209 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33219 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
33220 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33222 tree clauses
, sb
, ret
;
33224 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33226 strcat (p_name
, " simd");
33227 mask
|= OMP_SIMD_CLAUSE_MASK
;
33229 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33233 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
33234 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
33235 tree c
= find_omp_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
33236 OMP_CLAUSE_ORDERED
);
33237 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
33239 error_at (OMP_CLAUSE_LOCATION (c
),
33240 "%<ordered%> clause with parameter may not be specified "
33241 "on %qs construct", p_name
);
33242 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
33246 sb
= begin_omp_structured_block ();
33247 save
= cp_parser_begin_omp_structured_block (parser
);
33249 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
33251 cp_parser_end_omp_structured_block (parser
, save
);
33252 add_stmt (finish_omp_structured_block (sb
));
33258 #pragma omp for for-clause[optseq] new-line
33262 #pragma omp for simd for-simd-clause[optseq] new-line
33265 #define OMP_FOR_CLAUSE_MASK \
33266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33277 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
33278 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33280 tree clauses
, sb
, ret
;
33282 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33284 strcat (p_name
, " for");
33285 mask
|= OMP_FOR_CLAUSE_MASK
;
33287 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
33288 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33289 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33290 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
33292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33294 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33295 const char *p
= IDENTIFIER_POINTER (id
);
33297 if (strcmp (p
, "simd") == 0)
33299 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33300 if (cclauses
== NULL
)
33301 cclauses
= cclauses_buf
;
33303 cp_lexer_consume_token (parser
->lexer
);
33304 if (!flag_openmp
) /* flag_openmp_simd */
33305 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33307 sb
= begin_omp_structured_block ();
33308 save
= cp_parser_begin_omp_structured_block (parser
);
33309 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33311 cp_parser_end_omp_structured_block (parser
, save
);
33312 tree body
= finish_omp_structured_block (sb
);
33315 ret
= make_node (OMP_FOR
);
33316 TREE_TYPE (ret
) = void_type_node
;
33317 OMP_FOR_BODY (ret
) = body
;
33318 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33319 SET_EXPR_LOCATION (ret
, loc
);
33324 if (!flag_openmp
) /* flag_openmp_simd */
33326 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33330 /* Composite distribute parallel for disallows linear clause. */
33331 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33332 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
33334 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33338 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
33339 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
33342 sb
= begin_omp_structured_block ();
33343 save
= cp_parser_begin_omp_structured_block (parser
);
33345 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
33347 cp_parser_end_omp_structured_block (parser
, save
);
33348 add_stmt (finish_omp_structured_block (sb
));
33354 # pragma omp master new-line
33355 structured-block */
33358 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
33360 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33361 return c_finish_omp_master (input_location
,
33362 cp_parser_omp_structured_block (parser
));
33366 # pragma omp ordered new-line
33370 # pragma omp ordered ordered-clauses new-line
33371 structured-block */
33373 #define OMP_ORDERED_CLAUSE_MASK \
33374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33377 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33378 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33381 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
33382 enum pragma_context context
)
33384 location_t loc
= pragma_tok
->location
;
33386 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
33388 cp_parser_error (parser
, "expected declaration specifiers");
33389 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33393 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33395 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33396 const char *p
= IDENTIFIER_POINTER (id
);
33398 if (strcmp (p
, "depend") == 0)
33400 if (context
== pragma_stmt
)
33402 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
33403 "%<depend%> clause may only be used in compound "
33405 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33409 = cp_parser_omp_all_clauses (parser
,
33410 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
33411 "#pragma omp ordered", pragma_tok
);
33412 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
33418 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
33419 "#pragma omp ordered", pragma_tok
);
33420 c_finish_omp_ordered (loc
, clauses
,
33421 cp_parser_omp_structured_block (parser
));
33428 { section-sequence }
33431 section-directive[opt] structured-block
33432 section-sequence section-directive structured-block */
33435 cp_parser_omp_sections_scope (cp_parser
*parser
)
33437 tree stmt
, substmt
;
33438 bool error_suppress
= false;
33441 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
33444 stmt
= push_stmt_list ();
33446 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
33448 substmt
= cp_parser_omp_structured_block (parser
);
33449 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
33450 add_stmt (substmt
);
33455 tok
= cp_lexer_peek_token (parser
->lexer
);
33456 if (tok
->type
== CPP_CLOSE_BRACE
)
33458 if (tok
->type
== CPP_EOF
)
33461 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
33463 cp_lexer_consume_token (parser
->lexer
);
33464 cp_parser_require_pragma_eol (parser
, tok
);
33465 error_suppress
= false;
33467 else if (!error_suppress
)
33469 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
33470 error_suppress
= true;
33473 substmt
= cp_parser_omp_structured_block (parser
);
33474 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
33475 add_stmt (substmt
);
33477 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
33479 substmt
= pop_stmt_list (stmt
);
33481 stmt
= make_node (OMP_SECTIONS
);
33482 TREE_TYPE (stmt
) = void_type_node
;
33483 OMP_SECTIONS_BODY (stmt
) = substmt
;
33490 # pragma omp sections sections-clause[optseq] newline
33493 #define OMP_SECTIONS_CLAUSE_MASK \
33494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33501 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
33502 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33505 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33507 strcat (p_name
, " sections");
33508 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
33510 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
33512 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33516 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
33517 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
33520 ret
= cp_parser_omp_sections_scope (parser
);
33522 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
33528 # pragma omp parallel parallel-clause[optseq] new-line
33530 # pragma omp parallel for parallel-for-clause[optseq] new-line
33532 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
33536 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
33537 structured-block */
33539 #define OMP_PARALLEL_CLAUSE_MASK \
33540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
33546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
33548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
33551 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
33552 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33554 tree stmt
, clauses
, block
;
33556 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33558 strcat (p_name
, " parallel");
33559 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
33560 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
33561 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
33562 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
33563 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
33565 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33567 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33568 if (cclauses
== NULL
)
33569 cclauses
= cclauses_buf
;
33571 cp_lexer_consume_token (parser
->lexer
);
33572 if (!flag_openmp
) /* flag_openmp_simd */
33573 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33574 block
= begin_omp_parallel ();
33575 save
= cp_parser_begin_omp_structured_block (parser
);
33576 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33577 cp_parser_end_omp_structured_block (parser
, save
);
33578 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
33580 if (ret
== NULL_TREE
)
33582 OMP_PARALLEL_COMBINED (stmt
) = 1;
33585 /* When combined with distribute, parallel has to be followed by for.
33586 #pragma omp target parallel is allowed though. */
33588 && (mask
& (OMP_CLAUSE_MASK_1
33589 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
33591 error_at (loc
, "expected %<for%> after %qs", p_name
);
33592 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33595 else if (!flag_openmp
) /* flag_openmp_simd */
33597 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33600 else if (cclauses
== NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33602 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33603 const char *p
= IDENTIFIER_POINTER (id
);
33604 if (strcmp (p
, "sections") == 0)
33606 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33607 cclauses
= cclauses_buf
;
33609 cp_lexer_consume_token (parser
->lexer
);
33610 block
= begin_omp_parallel ();
33611 save
= cp_parser_begin_omp_structured_block (parser
);
33612 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
33613 cp_parser_end_omp_structured_block (parser
, save
);
33614 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
33616 OMP_PARALLEL_COMBINED (stmt
) = 1;
33621 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
33624 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
33625 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
33628 block
= begin_omp_parallel ();
33629 save
= cp_parser_begin_omp_structured_block (parser
);
33630 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33631 cp_parser_end_omp_structured_block (parser
, save
);
33632 stmt
= finish_omp_parallel (clauses
, block
);
33637 # pragma omp single single-clause[optseq] new-line
33638 structured-block */
33640 #define OMP_SINGLE_CLAUSE_MASK \
33641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
33644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33647 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
33649 tree stmt
= make_node (OMP_SINGLE
);
33650 TREE_TYPE (stmt
) = void_type_node
;
33652 OMP_SINGLE_CLAUSES (stmt
)
33653 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
33654 "#pragma omp single", pragma_tok
);
33655 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
33657 return add_stmt (stmt
);
33661 # pragma omp task task-clause[optseq] new-line
33662 structured-block */
33664 #define OMP_TASK_CLAUSE_MASK \
33665 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
33674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33677 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
33679 tree clauses
, block
;
33682 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
33683 "#pragma omp task", pragma_tok
);
33684 block
= begin_omp_task ();
33685 save
= cp_parser_begin_omp_structured_block (parser
);
33686 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
33687 cp_parser_end_omp_structured_block (parser
, save
);
33688 return finish_omp_task (clauses
, block
);
33692 # pragma omp taskwait new-line */
33695 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
33697 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33698 finish_omp_taskwait ();
33702 # pragma omp taskyield new-line */
33705 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
33707 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33708 finish_omp_taskyield ();
33712 # pragma omp taskgroup new-line
33713 structured-block */
33716 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
33718 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33719 return c_finish_omp_taskgroup (input_location
,
33720 cp_parser_omp_structured_block (parser
));
33725 # pragma omp threadprivate (variable-list) */
33728 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
33732 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
33733 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33735 finish_omp_threadprivate (vars
);
33739 # pragma omp cancel cancel-clause[optseq] new-line */
33741 #define OMP_CANCEL_CLAUSE_MASK \
33742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
33746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
33749 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
33751 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
33752 "#pragma omp cancel", pragma_tok
);
33753 finish_omp_cancel (clauses
);
33757 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
33759 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
33760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
33766 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
33769 bool point_seen
= false;
33771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33773 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33774 const char *p
= IDENTIFIER_POINTER (id
);
33776 if (strcmp (p
, "point") == 0)
33778 cp_lexer_consume_token (parser
->lexer
);
33784 cp_parser_error (parser
, "expected %<point%>");
33785 cp_parser_require_pragma_eol (parser
, pragma_tok
);
33789 clauses
= cp_parser_omp_all_clauses (parser
,
33790 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
33791 "#pragma omp cancellation point",
33793 finish_omp_cancellation_point (clauses
);
33797 #pragma omp distribute distribute-clause[optseq] new-line
33800 #define OMP_DISTRIBUTE_CLAUSE_MASK \
33801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
33805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33808 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
33809 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33811 tree clauses
, sb
, ret
;
33813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33815 strcat (p_name
, " distribute");
33816 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
33818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33820 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33821 const char *p
= IDENTIFIER_POINTER (id
);
33823 bool parallel
= false;
33825 if (strcmp (p
, "simd") == 0)
33828 parallel
= strcmp (p
, "parallel") == 0;
33829 if (parallel
|| simd
)
33831 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
33832 if (cclauses
== NULL
)
33833 cclauses
= cclauses_buf
;
33834 cp_lexer_consume_token (parser
->lexer
);
33835 if (!flag_openmp
) /* flag_openmp_simd */
33838 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33841 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
33844 sb
= begin_omp_structured_block ();
33845 save
= cp_parser_begin_omp_structured_block (parser
);
33847 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
33850 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
33852 cp_parser_end_omp_structured_block (parser
, save
);
33853 tree body
= finish_omp_structured_block (sb
);
33856 ret
= make_node (OMP_DISTRIBUTE
);
33857 TREE_TYPE (ret
) = void_type_node
;
33858 OMP_FOR_BODY (ret
) = body
;
33859 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
33860 SET_EXPR_LOCATION (ret
, loc
);
33865 if (!flag_openmp
) /* flag_openmp_simd */
33867 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33871 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33875 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
33876 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
33879 sb
= begin_omp_structured_block ();
33880 save
= cp_parser_begin_omp_structured_block (parser
);
33882 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
33884 cp_parser_end_omp_structured_block (parser
, save
);
33885 add_stmt (finish_omp_structured_block (sb
));
33891 # pragma omp teams teams-clause[optseq] new-line
33892 structured-block */
33894 #define OMP_TEAMS_CLAUSE_MASK \
33895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
33900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
33901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
33904 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
33905 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
33907 tree clauses
, sb
, ret
;
33909 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33911 strcat (p_name
, " teams");
33912 mask
|= OMP_TEAMS_CLAUSE_MASK
;
33914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33916 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33917 const char *p
= IDENTIFIER_POINTER (id
);
33918 if (strcmp (p
, "distribute") == 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_distribute (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_distribute (parser
, pragma_tok
, p_name
, mask
,
33932 cp_parser_end_omp_structured_block (parser
, save
);
33933 tree body
= finish_omp_structured_block (sb
);
33936 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
33937 ret
= make_node (OMP_TEAMS
);
33938 TREE_TYPE (ret
) = void_type_node
;
33939 OMP_TEAMS_CLAUSES (ret
) = clauses
;
33940 OMP_TEAMS_BODY (ret
) = body
;
33941 OMP_TEAMS_COMBINED (ret
) = 1;
33942 return add_stmt (ret
);
33945 if (!flag_openmp
) /* flag_openmp_simd */
33947 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33951 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
33955 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
33956 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
33959 tree stmt
= make_node (OMP_TEAMS
);
33960 TREE_TYPE (stmt
) = void_type_node
;
33961 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
33962 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
33964 return add_stmt (stmt
);
33968 # pragma omp target data target-data-clause[optseq] new-line
33969 structured-block */
33971 #define OMP_TARGET_DATA_CLAUSE_MASK \
33972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
33973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
33974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
33978 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
33981 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
33982 "#pragma omp target data", pragma_tok
);
33984 for (tree
*pc
= &clauses
; *pc
;)
33986 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
33987 switch (OMP_CLAUSE_MAP_KIND (*pc
))
33990 case GOMP_MAP_ALWAYS_TO
:
33991 case GOMP_MAP_FROM
:
33992 case GOMP_MAP_ALWAYS_FROM
:
33993 case GOMP_MAP_TOFROM
:
33994 case GOMP_MAP_ALWAYS_TOFROM
:
33995 case GOMP_MAP_ALLOC
:
33998 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
33999 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34000 case GOMP_MAP_ALWAYS_POINTER
:
34004 error_at (OMP_CLAUSE_LOCATION (*pc
),
34005 "%<#pragma omp target data%> with map-type other "
34006 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34007 "on %<map%> clause");
34008 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34011 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34017 error_at (pragma_tok
->location
,
34018 "%<#pragma omp target data%> must contain at least "
34019 "one %<map%> clause");
34023 tree stmt
= make_node (OMP_TARGET_DATA
);
34024 TREE_TYPE (stmt
) = void_type_node
;
34025 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
34027 keep_next_level (true);
34028 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
34030 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34031 return add_stmt (stmt
);
34035 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34036 structured-block */
34038 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34046 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34047 enum pragma_context context
)
34049 bool data_seen
= false;
34050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34052 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34053 const char *p
= IDENTIFIER_POINTER (id
);
34055 if (strcmp (p
, "data") == 0)
34057 cp_lexer_consume_token (parser
->lexer
);
34063 cp_parser_error (parser
, "expected %<data%>");
34064 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34068 if (context
== pragma_stmt
)
34070 error_at (pragma_tok
->location
,
34071 "%<#pragma omp target enter data%> may only be "
34072 "used in compound statements");
34073 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34078 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
34079 "#pragma omp target enter data", pragma_tok
);
34081 for (tree
*pc
= &clauses
; *pc
;)
34083 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34084 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34087 case GOMP_MAP_ALWAYS_TO
:
34088 case GOMP_MAP_ALLOC
:
34091 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34092 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34093 case GOMP_MAP_ALWAYS_POINTER
:
34097 error_at (OMP_CLAUSE_LOCATION (*pc
),
34098 "%<#pragma omp target enter data%> with map-type other "
34099 "than %<to%> or %<alloc%> on %<map%> clause");
34100 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34103 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34109 error_at (pragma_tok
->location
,
34110 "%<#pragma omp target enter data%> must contain at least "
34111 "one %<map%> clause");
34115 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
34116 TREE_TYPE (stmt
) = void_type_node
;
34117 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
34118 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34119 return add_stmt (stmt
);
34123 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34124 structured-block */
34126 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34134 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34135 enum pragma_context context
)
34137 bool data_seen
= false;
34138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34140 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34141 const char *p
= IDENTIFIER_POINTER (id
);
34143 if (strcmp (p
, "data") == 0)
34145 cp_lexer_consume_token (parser
->lexer
);
34151 cp_parser_error (parser
, "expected %<data%>");
34152 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34156 if (context
== pragma_stmt
)
34158 error_at (pragma_tok
->location
,
34159 "%<#pragma omp target exit data%> may only be "
34160 "used in compound statements");
34161 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34166 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
34167 "#pragma omp target exit data", pragma_tok
);
34169 for (tree
*pc
= &clauses
; *pc
;)
34171 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34172 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34174 case GOMP_MAP_FROM
:
34175 case GOMP_MAP_ALWAYS_FROM
:
34176 case GOMP_MAP_RELEASE
:
34177 case GOMP_MAP_DELETE
:
34180 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34181 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34182 case GOMP_MAP_ALWAYS_POINTER
:
34186 error_at (OMP_CLAUSE_LOCATION (*pc
),
34187 "%<#pragma omp target exit data%> with map-type other "
34188 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34190 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34193 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34199 error_at (pragma_tok
->location
,
34200 "%<#pragma omp target exit data%> must contain at least "
34201 "one %<map%> clause");
34205 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
34206 TREE_TYPE (stmt
) = void_type_node
;
34207 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
34208 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34209 return add_stmt (stmt
);
34213 # pragma omp target update target-update-clause[optseq] new-line */
34215 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34224 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
34225 enum pragma_context context
)
34227 if (context
== pragma_stmt
)
34229 error_at (pragma_tok
->location
,
34230 "%<#pragma omp target update%> may only be "
34231 "used in compound statements");
34232 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34237 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
34238 "#pragma omp target update", pragma_tok
);
34239 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
34240 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
34242 error_at (pragma_tok
->location
,
34243 "%<#pragma omp target update%> must contain at least one "
34244 "%<from%> or %<to%> clauses");
34248 tree stmt
= make_node (OMP_TARGET_UPDATE
);
34249 TREE_TYPE (stmt
) = void_type_node
;
34250 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
34251 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34257 # pragma omp target target-clause[optseq] new-line
34258 structured-block */
34260 #define OMP_TARGET_CLAUSE_MASK \
34261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
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_DEFAULTMAP) \
34269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34272 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
34273 enum pragma_context context
)
34275 tree
*pc
= NULL
, stmt
;
34277 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
34279 cp_parser_error (parser
, "expected declaration specifiers");
34280 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34284 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34286 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34287 const char *p
= IDENTIFIER_POINTER (id
);
34288 enum tree_code ccode
= ERROR_MARK
;
34290 if (strcmp (p
, "teams") == 0)
34292 else if (strcmp (p
, "parallel") == 0)
34293 ccode
= OMP_PARALLEL
;
34294 else if (strcmp (p
, "simd") == 0)
34296 if (ccode
!= ERROR_MARK
)
34298 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
34299 char p_name
[sizeof ("#pragma omp target teams distribute "
34300 "parallel for simd")];
34302 cp_lexer_consume_token (parser
->lexer
);
34303 strcpy (p_name
, "#pragma omp target");
34304 if (!flag_openmp
) /* flag_openmp_simd */
34310 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34311 OMP_TARGET_CLAUSE_MASK
,
34315 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34316 OMP_TARGET_CLAUSE_MASK
,
34320 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34321 OMP_TARGET_CLAUSE_MASK
,
34325 gcc_unreachable ();
34327 return stmt
!= NULL_TREE
;
34329 keep_next_level (true);
34330 tree sb
= begin_omp_structured_block (), ret
;
34331 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
34335 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
34336 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34339 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
34340 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34343 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
34344 OMP_TARGET_CLAUSE_MASK
, cclauses
);
34347 gcc_unreachable ();
34349 cp_parser_end_omp_structured_block (parser
, save
);
34350 tree body
= finish_omp_structured_block (sb
);
34351 if (ret
== NULL_TREE
)
34353 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
34355 /* For combined target teams, ensure the num_teams and
34356 thread_limit clause expressions are evaluated on the host,
34357 before entering the target construct. */
34359 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
34360 c
; c
= OMP_CLAUSE_CHAIN (c
))
34361 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
34362 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
34363 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
34365 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
34366 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
34367 if (expr
== error_mark_node
)
34369 tree tmp
= TARGET_EXPR_SLOT (expr
);
34371 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
34372 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
34373 OMP_CLAUSE_FIRSTPRIVATE
);
34374 OMP_CLAUSE_DECL (tc
) = tmp
;
34375 OMP_CLAUSE_CHAIN (tc
)
34376 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
34377 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
34380 tree stmt
= make_node (OMP_TARGET
);
34381 TREE_TYPE (stmt
) = void_type_node
;
34382 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
34383 OMP_TARGET_BODY (stmt
) = body
;
34384 OMP_TARGET_COMBINED (stmt
) = 1;
34386 pc
= &OMP_TARGET_CLAUSES (stmt
);
34387 goto check_clauses
;
34389 else if (!flag_openmp
) /* flag_openmp_simd */
34391 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34394 else if (strcmp (p
, "data") == 0)
34396 cp_lexer_consume_token (parser
->lexer
);
34397 cp_parser_omp_target_data (parser
, pragma_tok
);
34400 else if (strcmp (p
, "enter") == 0)
34402 cp_lexer_consume_token (parser
->lexer
);
34403 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
34406 else if (strcmp (p
, "exit") == 0)
34408 cp_lexer_consume_token (parser
->lexer
);
34409 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
34412 else if (strcmp (p
, "update") == 0)
34414 cp_lexer_consume_token (parser
->lexer
);
34415 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
34419 stmt
= make_node (OMP_TARGET
);
34420 TREE_TYPE (stmt
) = void_type_node
;
34422 OMP_TARGET_CLAUSES (stmt
)
34423 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
34424 "#pragma omp target", pragma_tok
);
34425 pc
= &OMP_TARGET_CLAUSES (stmt
);
34426 keep_next_level (true);
34427 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
34429 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34435 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
34436 switch (OMP_CLAUSE_MAP_KIND (*pc
))
34439 case GOMP_MAP_ALWAYS_TO
:
34440 case GOMP_MAP_FROM
:
34441 case GOMP_MAP_ALWAYS_FROM
:
34442 case GOMP_MAP_TOFROM
:
34443 case GOMP_MAP_ALWAYS_TOFROM
:
34444 case GOMP_MAP_ALLOC
:
34445 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
34446 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
34447 case GOMP_MAP_ALWAYS_POINTER
:
34450 error_at (OMP_CLAUSE_LOCATION (*pc
),
34451 "%<#pragma omp target%> with map-type other "
34452 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34453 "on %<map%> clause");
34454 *pc
= OMP_CLAUSE_CHAIN (*pc
);
34457 pc
= &OMP_CLAUSE_CHAIN (*pc
);
34463 # pragma acc cache (variable-list) new-line
34467 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
34469 tree stmt
, clauses
;
34471 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
34472 clauses
= finish_omp_clauses (clauses
, false);
34474 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
34476 stmt
= make_node (OACC_CACHE
);
34477 TREE_TYPE (stmt
) = void_type_node
;
34478 OACC_CACHE_CLAUSES (stmt
) = clauses
;
34479 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34486 # pragma acc data oacc-data-clause[optseq] new-line
34487 structured-block */
34489 #define OACC_DATA_CLAUSE_MASK \
34490 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34503 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
34505 tree stmt
, clauses
, block
;
34508 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
34509 "#pragma acc data", pragma_tok
);
34511 block
= begin_omp_parallel ();
34512 save
= cp_parser_begin_omp_structured_block (parser
);
34513 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
34514 cp_parser_end_omp_structured_block (parser
, save
);
34515 stmt
= finish_oacc_data (clauses
, block
);
34520 # pragma acc host_data <clauses> new-line
34521 structured-block */
34523 #define OACC_HOST_DATA_CLAUSE_MASK \
34524 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
34527 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
)
34529 tree stmt
, clauses
, block
;
34532 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
34533 "#pragma acc host_data", pragma_tok
);
34535 block
= begin_omp_parallel ();
34536 save
= cp_parser_begin_omp_structured_block (parser
);
34537 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
34538 cp_parser_end_omp_structured_block (parser
, save
);
34539 stmt
= finish_oacc_host_data (clauses
, block
);
34544 # pragma acc declare oacc-data-clause[optseq] new-line
34547 #define OACC_DECLARE_CLAUSE_MASK \
34548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
34554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
34555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34562 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
34564 tree clauses
, stmt
;
34565 bool error
= false;
34567 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
34568 "#pragma acc declare", pragma_tok
, true);
34571 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
34573 error_at (pragma_tok
->location
,
34574 "no valid clauses specified in %<#pragma acc declare%>");
34578 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
34580 location_t loc
= OMP_CLAUSE_LOCATION (t
);
34581 tree decl
= OMP_CLAUSE_DECL (t
);
34582 if (!DECL_P (decl
))
34584 error_at (loc
, "array section in %<#pragma acc declare%>");
34588 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
34589 switch (OMP_CLAUSE_MAP_KIND (t
))
34591 case GOMP_MAP_FORCE_ALLOC
:
34592 case GOMP_MAP_FORCE_TO
:
34593 case GOMP_MAP_FORCE_DEVICEPTR
:
34594 case GOMP_MAP_DEVICE_RESIDENT
:
34597 case GOMP_MAP_POINTER
:
34598 /* Generated by c_finish_omp_clauses from array sections;
34599 avoid spurious diagnostics. */
34602 case GOMP_MAP_LINK
:
34603 if (!global_bindings_p ()
34604 && (TREE_STATIC (decl
)
34605 || !DECL_EXTERNAL (decl
)))
34608 "%qD must be a global variable in"
34609 "%<#pragma acc declare link%>",
34617 if (global_bindings_p ())
34619 error_at (loc
, "invalid OpenACC clause at file scope");
34623 if (DECL_EXTERNAL (decl
))
34626 "invalid use of %<extern%> variable %qD "
34627 "in %<#pragma acc declare%>", decl
);
34631 else if (TREE_PUBLIC (decl
))
34634 "invalid use of %<global%> variable %qD "
34635 "in %<#pragma acc declare%>", decl
);
34642 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
34643 || lookup_attribute ("omp declare target link",
34644 DECL_ATTRIBUTES (decl
)))
34646 error_at (loc
, "variable %qD used more than once with "
34647 "%<#pragma acc declare%>", decl
);
34656 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
34657 id
= get_identifier ("omp declare target link");
34659 id
= get_identifier ("omp declare target");
34661 DECL_ATTRIBUTES (decl
)
34662 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
34663 if (global_bindings_p ())
34665 symtab_node
*node
= symtab_node::get (decl
);
34668 node
->offloadable
= 1;
34669 if (ENABLE_OFFLOADING
)
34671 g
->have_offload
= true;
34672 if (is_a
<varpool_node
*> (node
))
34674 vec_safe_push (offload_vars
, decl
);
34675 node
->force_output
= 1;
34683 if (error
|| global_bindings_p ())
34686 stmt
= make_node (OACC_DECLARE
);
34687 TREE_TYPE (stmt
) = void_type_node
;
34688 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
34689 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34697 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
34701 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
34703 LOC is the location of the #pragma token.
34706 #define OACC_ENTER_DATA_CLAUSE_MASK \
34707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34715 #define OACC_EXIT_DATA_CLAUSE_MASK \
34716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
34720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34723 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
34726 tree stmt
, clauses
;
34728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
34729 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34731 cp_parser_error (parser
, enter
34732 ? "expected %<data%> in %<#pragma acc enter data%>"
34733 : "expected %<data%> in %<#pragma acc exit data%>");
34734 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34739 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
34740 if (strcmp (p
, "data") != 0)
34742 cp_parser_error (parser
, "invalid pragma");
34743 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
34747 cp_lexer_consume_token (parser
->lexer
);
34750 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
34751 "#pragma acc enter data", pragma_tok
);
34753 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
34754 "#pragma acc exit data", pragma_tok
);
34756 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
34758 error_at (pragma_tok
->location
,
34759 "%<#pragma acc enter data%> has no data movement clause");
34763 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
34764 TREE_TYPE (stmt
) = void_type_node
;
34765 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
34766 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34772 # pragma acc loop oacc-loop-clause[optseq] new-line
34773 structured-block */
34775 #define OACC_LOOP_CLAUSE_MASK \
34776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
34777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
34780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
34781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
34782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
34783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
34784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
34785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
34788 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
34789 omp_clause_mask mask
, tree
*cclauses
)
34791 strcat (p_name
, " loop");
34792 mask
|= OACC_LOOP_CLAUSE_MASK
;
34794 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
34798 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
);
34800 finish_omp_clauses (*cclauses
, false);
34802 finish_omp_clauses (clauses
, false);
34805 tree block
= begin_omp_structured_block ();
34806 int save
= cp_parser_begin_omp_structured_block (parser
);
34807 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
34808 cp_parser_end_omp_structured_block (parser
, save
);
34809 add_stmt (finish_omp_structured_block (block
));
34815 # pragma acc kernels oacc-kernels-clause[optseq] new-line
34820 # pragma acc parallel oacc-parallel-clause[optseq] new-line
34824 #define OACC_KERNELS_CLAUSE_MASK \
34825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34840 #define OACC_PARALLEL_CLAUSE_MASK \
34841 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
34849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
34851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
34852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
34860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34863 cp_parser_oacc_kernels_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
34866 omp_clause_mask mask
;
34867 enum tree_code code
;
34868 switch (pragma_tok
->pragma_kind
)
34870 case PRAGMA_OACC_KERNELS
:
34871 strcat (p_name
, " kernels");
34872 mask
= OACC_KERNELS_CLAUSE_MASK
;
34873 code
= OACC_KERNELS
;
34875 case PRAGMA_OACC_PARALLEL
:
34876 strcat (p_name
, " parallel");
34877 mask
= OACC_PARALLEL_CLAUSE_MASK
;
34878 code
= OACC_PARALLEL
;
34881 gcc_unreachable ();
34884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34887 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
34888 if (strcmp (p
, "loop") == 0)
34890 cp_lexer_consume_token (parser
->lexer
);
34891 mask
|= OACC_LOOP_CLAUSE_MASK
;
34893 tree block
= begin_omp_parallel ();
34895 cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, &clauses
);
34896 return finish_omp_construct (code
, block
, clauses
);
34900 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
34902 tree block
= begin_omp_parallel ();
34903 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
34904 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
34905 cp_parser_end_omp_structured_block (parser
, save
);
34906 return finish_omp_construct (code
, block
, clauses
);
34910 # pragma acc update oacc-update-clause[optseq] new-line
34913 #define OACC_UPDATE_CLAUSE_MASK \
34914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
34916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
34917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
34919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
34922 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
34924 tree stmt
, clauses
;
34926 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
34927 "#pragma acc update", pragma_tok
);
34929 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
34931 error_at (pragma_tok
->location
,
34932 "%<#pragma acc update%> must contain at least one "
34933 "%<device%> or %<host%> or %<self%> clause");
34937 stmt
= make_node (OACC_UPDATE
);
34938 TREE_TYPE (stmt
) = void_type_node
;
34939 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
34940 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
34946 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
34948 LOC is the location of the #pragma token.
34951 #define OACC_WAIT_CLAUSE_MASK \
34952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
34955 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
34957 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
34958 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34960 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34961 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
34963 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
34964 "#pragma acc wait", pragma_tok
);
34966 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
34972 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
34974 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
34975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
34979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
34980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
34983 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
34984 enum pragma_context context
)
34986 bool first_p
= parser
->omp_declare_simd
== NULL
;
34987 cp_omp_declare_simd_data data
;
34990 data
.error_seen
= false;
34991 data
.fndecl_seen
= false;
34992 data
.tokens
= vNULL
;
34993 parser
->omp_declare_simd
= &data
;
34995 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
34996 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
34997 cp_lexer_consume_token (parser
->lexer
);
34998 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
34999 parser
->omp_declare_simd
->error_seen
= true;
35000 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35001 struct cp_token_cache
*cp
35002 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
35003 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
35006 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
35007 cp_parser_pragma (parser
, context
);
35010 case pragma_external
:
35011 cp_parser_declaration (parser
);
35013 case pragma_member
:
35014 cp_parser_member_declaration (parser
);
35016 case pragma_objc_icode
:
35017 cp_parser_block_declaration (parser
, /*statement_p=*/false);
35020 cp_parser_declaration_statement (parser
);
35023 if (parser
->omp_declare_simd
35024 && !parser
->omp_declare_simd
->error_seen
35025 && !parser
->omp_declare_simd
->fndecl_seen
)
35026 error_at (pragma_tok
->location
,
35027 "%<#pragma omp declare simd%> not immediately followed by "
35028 "function declaration or definition");
35029 data
.tokens
.release ();
35030 parser
->omp_declare_simd
= NULL
;
35034 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35035 This function is modelled similar to the late parsing of omp declare
35039 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
35041 struct cp_token_cache
*ce
;
35042 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
35045 if (parser
->omp_declare_simd
!= NULL
35046 || lookup_attribute ("simd", attrs
))
35048 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35049 "used in the same function marked as a Cilk Plus SIMD-enabled "
35051 parser
->cilk_simd_fn_info
->tokens
.release ();
35052 XDELETE (parser
->cilk_simd_fn_info
);
35053 parser
->cilk_simd_fn_info
= NULL
;
35056 if (!info
->error_seen
&& info
->fndecl_seen
)
35058 error ("vector attribute not immediately followed by a single function"
35059 " declaration or definition");
35060 info
->error_seen
= true;
35062 if (info
->error_seen
)
35065 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
35069 cp_parser_push_lexer_for_tokens (parser
, ce
);
35070 parser
->lexer
->in_pragma
= true;
35071 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
35072 "SIMD-enabled functions attribute",
35074 cp_parser_pop_lexer (parser
);
35076 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
35078 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
35079 TREE_CHAIN (c
) = attrs
;
35082 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
35083 TREE_CHAIN (c
) = attrs
;
35084 if (processing_template_decl
)
35085 ATTR_IS_DEPENDENT (c
) = 1;
35088 info
->fndecl_seen
= true;
35089 parser
->cilk_simd_fn_info
->tokens
.release ();
35090 XDELETE (parser
->cilk_simd_fn_info
);
35091 parser
->cilk_simd_fn_info
= NULL
;
35095 /* Finalize #pragma omp declare simd clauses after direct declarator has
35096 been parsed, and put that into "omp declare simd" attribute. */
35099 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
35101 struct cp_token_cache
*ce
;
35102 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
35105 if (!data
->error_seen
&& data
->fndecl_seen
)
35107 error ("%<#pragma omp declare simd%> not immediately followed by "
35108 "a single function declaration or definition");
35109 data
->error_seen
= true;
35112 if (data
->error_seen
)
35115 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
35119 cp_parser_push_lexer_for_tokens (parser
, ce
);
35120 parser
->lexer
->in_pragma
= true;
35121 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
35122 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
35123 cp_lexer_consume_token (parser
->lexer
);
35124 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
35125 "#pragma omp declare simd", pragma_tok
);
35126 cp_parser_pop_lexer (parser
);
35128 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
35129 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
35130 TREE_CHAIN (c
) = attrs
;
35131 if (processing_template_decl
)
35132 ATTR_IS_DEPENDENT (c
) = 1;
35136 data
->fndecl_seen
= true;
35142 # pragma omp declare target new-line
35143 declarations and definitions
35144 # pragma omp end declare target new-line
35147 # pragma omp declare target ( extended-list ) new-line
35149 # pragma omp declare target declare-target-clauses[seq] new-line */
35151 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35152 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35156 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
35158 tree clauses
= NULL_TREE
;
35159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35161 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
35162 "#pragma omp declare target", pragma_tok
);
35163 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35165 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
35167 clauses
= finish_omp_clauses (clauses
, true);
35168 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35172 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35173 scope_chain
->omp_declare_target_attribute
++;
35176 if (scope_chain
->omp_declare_target_attribute
)
35177 error_at (pragma_tok
->location
,
35178 "%<#pragma omp declare target%> with clauses in between "
35179 "%<#pragma omp declare target%> without clauses and "
35180 "%<#pragma omp end declare target%>");
35181 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35183 tree t
= OMP_CLAUSE_DECL (c
), id
;
35184 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
35185 tree at2
= lookup_attribute ("omp declare target link",
35186 DECL_ATTRIBUTES (t
));
35187 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
35189 id
= get_identifier ("omp declare target link");
35190 std::swap (at1
, at2
);
35193 id
= get_identifier ("omp declare target");
35196 error_at (OMP_CLAUSE_LOCATION (c
),
35197 "%qD specified both in declare target %<link%> and %<to%>"
35203 symtab_node
*node
= symtab_node::get (t
);
35204 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
35207 node
->offloadable
= 1;
35208 if (ENABLE_OFFLOADING
)
35210 g
->have_offload
= true;
35211 if (is_a
<varpool_node
*> (node
))
35213 vec_safe_push (offload_vars
, t
);
35214 node
->force_output
= 1;
35223 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
35225 const char *p
= "";
35226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35228 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35229 p
= IDENTIFIER_POINTER (id
);
35231 if (strcmp (p
, "declare") == 0)
35233 cp_lexer_consume_token (parser
->lexer
);
35235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35237 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35238 p
= IDENTIFIER_POINTER (id
);
35240 if (strcmp (p
, "target") == 0)
35241 cp_lexer_consume_token (parser
->lexer
);
35244 cp_parser_error (parser
, "expected %<target%>");
35245 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35251 cp_parser_error (parser
, "expected %<declare%>");
35252 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35255 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35256 if (!scope_chain
->omp_declare_target_attribute
)
35257 error_at (pragma_tok
->location
,
35258 "%<#pragma omp end declare target%> without corresponding "
35259 "%<#pragma omp declare target%>");
35261 scope_chain
->omp_declare_target_attribute
--;
35264 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35265 expression and optional initializer clause of
35266 #pragma omp declare reduction. We store the expression(s) as
35267 either 3, 6 or 7 special statements inside of the artificial function's
35268 body. The first two statements are DECL_EXPRs for the artificial
35269 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35270 expression that uses those variables.
35271 If there was any INITIALIZER clause, this is followed by further statements,
35272 the fourth and fifth statements are DECL_EXPRs for the artificial
35273 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35274 constructor variant (first token after open paren is not omp_priv),
35275 then the sixth statement is a statement with the function call expression
35276 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35277 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35278 to initialize the OMP_PRIV artificial variable and there is seventh
35279 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35282 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
35284 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
35285 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
35286 type
= TREE_TYPE (type
);
35287 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
35288 DECL_ARTIFICIAL (omp_out
) = 1;
35289 pushdecl (omp_out
);
35290 add_decl_expr (omp_out
);
35291 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
35292 DECL_ARTIFICIAL (omp_in
) = 1;
35294 add_decl_expr (omp_in
);
35296 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
35298 keep_next_level (true);
35299 tree block
= begin_omp_structured_block ();
35300 combiner
= cp_parser_expression (parser
);
35301 finish_expr_stmt (combiner
);
35302 block
= finish_omp_structured_block (block
);
35305 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35308 const char *p
= "";
35309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35311 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35312 p
= IDENTIFIER_POINTER (id
);
35315 if (strcmp (p
, "initializer") == 0)
35317 cp_lexer_consume_token (parser
->lexer
);
35318 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35324 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35325 p
= IDENTIFIER_POINTER (id
);
35328 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
35329 DECL_ARTIFICIAL (omp_priv
) = 1;
35330 pushdecl (omp_priv
);
35331 add_decl_expr (omp_priv
);
35332 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
35333 DECL_ARTIFICIAL (omp_orig
) = 1;
35334 pushdecl (omp_orig
);
35335 add_decl_expr (omp_orig
);
35337 keep_next_level (true);
35338 block
= begin_omp_structured_block ();
35341 if (strcmp (p
, "omp_priv") == 0)
35343 bool is_direct_init
, is_non_constant_init
;
35345 cp_lexer_consume_token (parser
->lexer
);
35346 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
35348 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
35349 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
35351 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
35352 == CPP_CLOSE_PAREN
))
35354 finish_omp_structured_block (block
);
35355 error ("invalid initializer clause");
35358 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
35359 &is_non_constant_init
);
35360 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
35361 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
35365 cp_parser_parse_tentatively (parser
);
35366 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35367 /*check_dependency_p=*/true,
35368 /*template_p=*/NULL
,
35369 /*declarator_p=*/false,
35370 /*optional_p=*/false);
35371 vec
<tree
, va_gc
> *args
;
35372 if (fn_name
== error_mark_node
35373 || cp_parser_error_occurred (parser
)
35374 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
35375 || ((args
= cp_parser_parenthesized_expression_list
35376 (parser
, non_attr
, /*cast_p=*/false,
35377 /*allow_expansion_p=*/true,
35378 /*non_constant_p=*/NULL
)),
35379 cp_parser_error_occurred (parser
)))
35381 finish_omp_structured_block (block
);
35382 cp_parser_abort_tentative_parse (parser
);
35383 cp_parser_error (parser
, "expected id-expression (arguments)");
35388 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
35389 if (arg
== omp_priv
35390 || (TREE_CODE (arg
) == ADDR_EXPR
35391 && TREE_OPERAND (arg
, 0) == omp_priv
))
35393 cp_parser_abort_tentative_parse (parser
);
35394 if (arg
== NULL_TREE
)
35395 error ("one of the initializer call arguments should be %<omp_priv%>"
35396 " or %<&omp_priv%>");
35397 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
35399 finish_expr_stmt (initializer
);
35402 block
= finish_omp_structured_block (block
);
35403 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
35407 add_decl_expr (omp_orig
);
35409 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35413 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
35414 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
35420 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35421 initializer-clause[opt] new-line
35423 initializer-clause:
35424 initializer (omp_priv initializer)
35425 initializer (function-name (argument-list)) */
35428 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
35429 enum pragma_context
)
35431 auto_vec
<tree
> types
;
35432 enum tree_code reduc_code
= ERROR_MARK
;
35433 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
35435 cp_token
*first_token
;
35436 cp_token_cache
*cp
;
35440 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35441 p
= obstack_alloc (&declarator_obstack
, 0);
35443 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35446 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35449 reduc_code
= PLUS_EXPR
;
35452 reduc_code
= MULT_EXPR
;
35455 reduc_code
= MINUS_EXPR
;
35458 reduc_code
= BIT_AND_EXPR
;
35461 reduc_code
= BIT_XOR_EXPR
;
35464 reduc_code
= BIT_IOR_EXPR
;
35467 reduc_code
= TRUTH_ANDIF_EXPR
;
35470 reduc_code
= TRUTH_ORIF_EXPR
;
35473 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
35476 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
35477 "%<|%>, %<&&%>, %<||%> or identifier");
35481 if (reduc_code
!= ERROR_MARK
)
35482 cp_lexer_consume_token (parser
->lexer
);
35484 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
35485 if (reduc_id
== error_mark_node
)
35488 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35491 /* Types may not be defined in declare reduction type list. */
35492 const char *saved_message
;
35493 saved_message
= parser
->type_definition_forbidden_message
;
35494 parser
->type_definition_forbidden_message
35495 = G_("types may not be defined in declare reduction type list");
35496 bool saved_colon_corrects_to_scope_p
;
35497 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
35498 parser
->colon_corrects_to_scope_p
= false;
35499 bool saved_colon_doesnt_start_class_def_p
;
35500 saved_colon_doesnt_start_class_def_p
35501 = parser
->colon_doesnt_start_class_def_p
;
35502 parser
->colon_doesnt_start_class_def_p
= true;
35506 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35507 type
= cp_parser_type_id (parser
);
35508 if (type
== error_mark_node
)
35510 else if (ARITHMETIC_TYPE_P (type
)
35511 && (orig_reduc_id
== NULL_TREE
35512 || (TREE_CODE (type
) != COMPLEX_TYPE
35513 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
35515 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
35517 error_at (loc
, "predeclared arithmetic type %qT in "
35518 "%<#pragma omp declare reduction%>", type
);
35519 else if (TREE_CODE (type
) == FUNCTION_TYPE
35520 || TREE_CODE (type
) == METHOD_TYPE
35521 || TREE_CODE (type
) == ARRAY_TYPE
)
35522 error_at (loc
, "function or array type %qT in "
35523 "%<#pragma omp declare reduction%>", type
);
35524 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
35525 error_at (loc
, "reference type %qT in "
35526 "%<#pragma omp declare reduction%>", type
);
35527 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
35528 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
35529 "%<#pragma omp declare reduction%>", type
);
35531 types
.safe_push (type
);
35533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35534 cp_lexer_consume_token (parser
->lexer
);
35539 /* Restore the saved message. */
35540 parser
->type_definition_forbidden_message
= saved_message
;
35541 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35542 parser
->colon_doesnt_start_class_def_p
35543 = saved_colon_doesnt_start_class_def_p
;
35545 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
35546 || types
.is_empty ())
35549 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35553 first_token
= cp_lexer_peek_token (parser
->lexer
);
35556 FOR_EACH_VEC_ELT (types
, i
, type
)
35559 = build_function_type_list (void_type_node
,
35560 cp_build_reference_type (type
, false),
35562 tree this_reduc_id
= reduc_id
;
35563 if (!dependent_type_p (type
))
35564 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
35565 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
35566 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
35567 DECL_ARTIFICIAL (fndecl
) = 1;
35568 DECL_EXTERNAL (fndecl
) = 1;
35569 DECL_DECLARED_INLINE_P (fndecl
) = 1;
35570 DECL_IGNORED_P (fndecl
) = 1;
35571 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
35572 DECL_ATTRIBUTES (fndecl
)
35573 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
35574 DECL_ATTRIBUTES (fndecl
));
35575 if (processing_template_decl
)
35576 fndecl
= push_template_decl (fndecl
);
35577 bool block_scope
= false;
35578 tree block
= NULL_TREE
;
35579 if (current_function_decl
)
35581 block_scope
= true;
35582 DECL_CONTEXT (fndecl
) = global_namespace
;
35583 if (!processing_template_decl
)
35586 else if (current_class_type
)
35590 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
35591 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
35592 cp_lexer_consume_token (parser
->lexer
);
35593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35595 cp
= cp_token_cache_new (first_token
,
35596 cp_lexer_peek_nth_token (parser
->lexer
,
35599 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
35600 finish_member_declaration (fndecl
);
35601 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
35602 DECL_PENDING_INLINE_P (fndecl
) = 1;
35603 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
35608 DECL_CONTEXT (fndecl
) = current_namespace
;
35612 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
35614 block
= begin_omp_structured_block ();
35617 cp_parser_push_lexer_for_tokens (parser
, cp
);
35618 parser
->lexer
->in_pragma
= true;
35620 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
35623 finish_function (0);
35625 DECL_CONTEXT (fndecl
) = current_function_decl
;
35627 cp_parser_pop_lexer (parser
);
35631 cp_parser_pop_lexer (parser
);
35633 finish_function (0);
35636 DECL_CONTEXT (fndecl
) = current_function_decl
;
35637 block
= finish_omp_structured_block (block
);
35638 if (TREE_CODE (block
) == BIND_EXPR
)
35639 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
35640 else if (TREE_CODE (block
) == STATEMENT_LIST
)
35641 DECL_SAVED_TREE (fndecl
) = block
;
35642 if (processing_template_decl
)
35643 add_decl_expr (fndecl
);
35645 cp_check_omp_declare_reduction (fndecl
);
35646 if (cp
== NULL
&& types
.length () > 1)
35647 cp
= cp_token_cache_new (first_token
,
35648 cp_lexer_peek_nth_token (parser
->lexer
, 2));
35649 if (errs
!= errorcount
)
35653 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35656 /* Free any declarators allocated. */
35657 obstack_free (&declarator_obstack
, p
);
35661 #pragma omp declare simd declare-simd-clauses[optseq] new-line
35662 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35663 initializer-clause[opt] new-line
35664 #pragma omp declare target new-line */
35667 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
35668 enum pragma_context context
)
35670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35672 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35673 const char *p
= IDENTIFIER_POINTER (id
);
35675 if (strcmp (p
, "simd") == 0)
35677 cp_lexer_consume_token (parser
->lexer
);
35678 cp_parser_omp_declare_simd (parser
, pragma_tok
,
35682 cp_ensure_no_omp_declare_simd (parser
);
35683 if (strcmp (p
, "reduction") == 0)
35685 cp_lexer_consume_token (parser
->lexer
);
35686 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
35690 if (!flag_openmp
) /* flag_openmp_simd */
35692 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35695 if (strcmp (p
, "target") == 0)
35697 cp_lexer_consume_token (parser
->lexer
);
35698 cp_parser_omp_declare_target (parser
, pragma_tok
);
35702 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
35704 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35708 #pragma omp taskloop taskloop-clause[optseq] new-line
35711 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
35714 #define OMP_TASKLOOP_CLAUSE_MASK \
35715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
35721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
35722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
35723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
35728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35731 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
35732 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
35734 tree clauses
, sb
, ret
;
35736 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35738 strcat (p_name
, " taskloop");
35739 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
35741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35743 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35744 const char *p
= IDENTIFIER_POINTER (id
);
35746 if (strcmp (p
, "simd") == 0)
35748 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
35749 if (cclauses
== NULL
)
35750 cclauses
= cclauses_buf
;
35752 cp_lexer_consume_token (parser
->lexer
);
35753 if (!flag_openmp
) /* flag_openmp_simd */
35754 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35756 sb
= begin_omp_structured_block ();
35757 save
= cp_parser_begin_omp_structured_block (parser
);
35758 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
35760 cp_parser_end_omp_structured_block (parser
, save
);
35761 tree body
= finish_omp_structured_block (sb
);
35764 ret
= make_node (OMP_TASKLOOP
);
35765 TREE_TYPE (ret
) = void_type_node
;
35766 OMP_FOR_BODY (ret
) = body
;
35767 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
35768 SET_EXPR_LOCATION (ret
, loc
);
35773 if (!flag_openmp
) /* flag_openmp_simd */
35775 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35779 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
35783 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
35784 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
35787 sb
= begin_omp_structured_block ();
35788 save
= cp_parser_begin_omp_structured_block (parser
);
35790 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
);
35792 cp_parser_end_omp_structured_block (parser
, save
);
35793 add_stmt (finish_omp_structured_block (sb
));
35800 # pragma acc routine oacc-routine-clause[optseq] new-line
35801 function-definition
35803 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
35806 #define OACC_ROUTINE_CLAUSE_MASK \
35807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
35813 /* Parse the OpenACC routine pragma. This has an optional '( name )'
35814 component, which must resolve to a declared namespace-scope
35815 function. The clauses are either processed directly (for a named
35816 function), or defered until the immediatley following declaration
35820 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
35821 enum pragma_context context
)
35823 bool first_p
= parser
->oacc_routine
== NULL
;
35824 location_t loc
= pragma_tok
->location
;
35825 cp_omp_declare_simd_data data
;
35828 data
.error_seen
= false;
35829 data
.fndecl_seen
= false;
35830 data
.tokens
= vNULL
;
35831 data
.clauses
= NULL_TREE
;
35832 parser
->oacc_routine
= &data
;
35835 tree decl
= NULL_TREE
;
35836 /* Create a dummy claue, to record location. */
35837 tree c_head
= build_omp_clause (pragma_tok
->location
, OMP_CLAUSE_SEQ
);
35839 if (context
!= pragma_external
)
35841 cp_parser_error (parser
, "%<#pragma acc routine%> not at file scope");
35842 parser
->oacc_routine
->error_seen
= true;
35843 parser
->oacc_routine
= NULL
;
35847 /* Look for optional '( name )'. */
35848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35852 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
35853 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
35854 cp_lexer_consume_token (parser
->lexer
);
35855 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35856 parser
->oacc_routine
->error_seen
= true;
35857 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35859 error_at (OMP_CLAUSE_LOCATION (parser
->oacc_routine
->clauses
),
35860 "%<#pragma oacc routine%> not followed by a single "
35861 "function declaration or definition");
35863 parser
->oacc_routine
->error_seen
= true;
35867 cp_lexer_consume_token (parser
->lexer
);
35868 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35870 /* We parse the name as an id-expression. If it resolves to
35871 anything other than a non-overloaded function at namespace
35872 scope, it's an error. */
35873 tree id
= cp_parser_id_expression (parser
,
35874 /*template_keyword_p=*/false,
35875 /*check_dependency_p=*/false,
35876 /*template_p=*/NULL
,
35877 /*declarator_p=*/false,
35878 /*optional_p=*/false);
35879 decl
= cp_parser_lookup_name_simple (parser
, id
, token
->location
);
35880 if (id
!= error_mark_node
&& decl
== error_mark_node
)
35881 cp_parser_name_lookup_error (parser
, id
, decl
, NLE_NULL
,
35884 if (decl
== error_mark_node
35885 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35887 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
35888 parser
->oacc_routine
= NULL
;
35892 /* Build a chain of clauses. */
35893 parser
->lexer
->in_pragma
= true;
35894 tree clauses
= NULL_TREE
;
35895 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
35896 "#pragma acc routine",
35897 cp_lexer_peek_token
35900 /* Force clauses to be non-null, by attaching context to it. */
35901 clauses
= tree_cons (c_head
, clauses
, NULL_TREE
);
35903 if (decl
&& is_overloaded_fn (decl
)
35904 && (TREE_CODE (decl
) != FUNCTION_DECL
35905 || DECL_FUNCTION_TEMPLATE_P (decl
)))
35907 error_at (loc
, "%<#pragma acc routine%> names a set of overloads");
35908 parser
->oacc_routine
= NULL
;
35912 /* Perhaps we should use the same rule as declarations in different
35914 if (!DECL_NAMESPACE_SCOPE_P (decl
))
35916 error_at (loc
, "%<#pragma acc routine%> does not refer to a "
35917 "namespace scope function");
35918 parser
->oacc_routine
= NULL
;
35922 if (!decl
|| TREE_CODE (decl
) != FUNCTION_DECL
)
35925 "%<#pragma acc routine%> does not refer to a function");
35926 parser
->oacc_routine
= NULL
;
35930 data
.clauses
= clauses
;
35932 cp_finalize_oacc_routine (parser
, decl
, false);
35933 data
.tokens
.release ();
35934 parser
->oacc_routine
= NULL
;
35938 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
35939 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
35940 cp_lexer_consume_token (parser
->lexer
);
35941 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
35942 parser
->oacc_routine
->error_seen
= true;
35943 cp_parser_require_pragma_eol (parser
, pragma_tok
);
35945 struct cp_token_cache
*cp
35946 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
35947 parser
->oacc_routine
->tokens
.safe_push (cp
);
35950 parser
->oacc_routine
->clauses
= c_head
;
35952 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
35953 cp_parser_pragma (parser
, context
);
35957 /* Create an empty list of clauses. */
35958 parser
->oacc_routine
->clauses
= tree_cons (c_head
, NULL_TREE
,
35960 cp_parser_declaration (parser
);
35962 if (parser
->oacc_routine
35963 && !parser
->oacc_routine
->error_seen
35964 && !parser
->oacc_routine
->fndecl_seen
)
35965 error_at (loc
, "%<#pragma acc routine%> not followed by "
35966 "function declaration or definition");
35968 data
.tokens
.release ();
35969 parser
->oacc_routine
= NULL
;
35974 /* Finalize #pragma acc routine clauses after direct declarator has
35975 been parsed, and put that into "oacc routine" attribute. */
35978 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
35980 struct cp_token_cache
*ce
;
35981 cp_omp_declare_simd_data
*data
= parser
->oacc_routine
;
35982 tree cl
, clauses
= parser
->oacc_routine
->clauses
;
35985 loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses
));
35987 if ((!data
->error_seen
&& data
->fndecl_seen
)
35988 || data
->tokens
.length () != 1)
35990 error_at (loc
, "%<#pragma oacc routine%> not followed by a single "
35991 "function declaration or definition");
35992 data
->error_seen
= true;
35995 if (data
->error_seen
)
35998 ce
= data
->tokens
[0];
36000 cp_parser_push_lexer_for_tokens (parser
, ce
);
36001 parser
->lexer
->in_pragma
= true;
36002 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
36004 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36005 cl
= cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
36006 "#pragma oacc routine", pragma_tok
);
36007 cp_parser_pop_lexer (parser
);
36009 tree c_head
= build_omp_clause (loc
, OMP_CLAUSE_SEQ
);
36011 /* Force clauses to be non-null, by attaching context to it. */
36012 parser
->oacc_routine
->clauses
= tree_cons (c_head
, cl
, NULL_TREE
);
36014 data
->fndecl_seen
= true;
36018 /* Apply any saved OpenACC routine clauses to a just-parsed
36022 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
36024 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
36026 tree clauses
= parser
->oacc_routine
->clauses
;
36027 location_t loc
= OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses
));
36029 if (parser
->oacc_routine
->error_seen
)
36032 if (fndecl
== error_mark_node
)
36034 parser
->oacc_routine
= NULL
;
36038 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
36040 cp_ensure_no_oacc_routine (parser
);
36044 if (!fndecl
|| TREE_CODE (fndecl
) != FUNCTION_DECL
)
36047 "%<#pragma acc routine%> not followed by single function");
36048 parser
->oacc_routine
= NULL
;
36051 if (get_oacc_fn_attrib (fndecl
))
36053 error_at (loc
, "%<#pragma acc routine%> already applied to %D",
36055 parser
->oacc_routine
= NULL
;
36058 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
36060 error_at (loc
, "%<#pragma acc routine%> must be applied before %s",
36061 TREE_USED (fndecl
) ? "use" : "definition");
36062 parser
->oacc_routine
= NULL
;
36065 /* Process for function attrib */
36066 tree dims
= build_oacc_routine_dims (TREE_VALUE (clauses
));
36067 replace_oacc_fn_attrib (fndecl
, dims
);
36069 /* Add an "omp target" attribute. */
36070 DECL_ATTRIBUTES (fndecl
)
36071 = tree_cons (get_identifier ("omp declare target"),
36072 NULL_TREE
, DECL_ATTRIBUTES (fndecl
));
36076 /* Main entry point to OpenMP statement pragmas. */
36079 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
36082 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
36083 omp_clause_mask
mask (0);
36085 switch (pragma_tok
->pragma_kind
)
36087 case PRAGMA_OACC_ATOMIC
:
36088 cp_parser_omp_atomic (parser
, pragma_tok
);
36090 case PRAGMA_OACC_CACHE
:
36091 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
36093 case PRAGMA_OACC_DATA
:
36094 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
36096 case PRAGMA_OACC_ENTER_DATA
:
36097 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
36099 case PRAGMA_OACC_EXIT_DATA
:
36100 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
36102 case PRAGMA_OACC_HOST_DATA
:
36103 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
);
36105 case PRAGMA_OACC_KERNELS
:
36106 case PRAGMA_OACC_PARALLEL
:
36107 strcpy (p_name
, "#pragma acc");
36108 stmt
= cp_parser_oacc_kernels_parallel (parser
, pragma_tok
, p_name
);
36110 case PRAGMA_OACC_LOOP
:
36111 strcpy (p_name
, "#pragma acc");
36112 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
);
36114 case PRAGMA_OACC_UPDATE
:
36115 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
36117 case PRAGMA_OACC_WAIT
:
36118 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
36120 case PRAGMA_OMP_ATOMIC
:
36121 cp_parser_omp_atomic (parser
, pragma_tok
);
36123 case PRAGMA_OMP_CRITICAL
:
36124 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
36126 case PRAGMA_OMP_DISTRIBUTE
:
36127 strcpy (p_name
, "#pragma omp");
36128 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
36130 case PRAGMA_OMP_FOR
:
36131 strcpy (p_name
, "#pragma omp");
36132 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
36134 case PRAGMA_OMP_MASTER
:
36135 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
36137 case PRAGMA_OMP_PARALLEL
:
36138 strcpy (p_name
, "#pragma omp");
36139 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
36141 case PRAGMA_OMP_SECTIONS
:
36142 strcpy (p_name
, "#pragma omp");
36143 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
36145 case PRAGMA_OMP_SIMD
:
36146 strcpy (p_name
, "#pragma omp");
36147 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
36149 case PRAGMA_OMP_SINGLE
:
36150 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
36152 case PRAGMA_OMP_TASK
:
36153 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
36155 case PRAGMA_OMP_TASKGROUP
:
36156 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
36158 case PRAGMA_OMP_TASKLOOP
:
36159 strcpy (p_name
, "#pragma omp");
36160 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
);
36162 case PRAGMA_OMP_TEAMS
:
36163 strcpy (p_name
, "#pragma omp");
36164 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
36167 gcc_unreachable ();
36170 protected_set_expr_location (stmt
, pragma_tok
->location
);
36173 /* Transactional Memory parsing routines. */
36175 /* Parse a transaction attribute.
36181 We use this instead of cp_parser_attributes_opt for transactions to avoid
36182 the pedwarn in C++98 mode. */
36185 cp_parser_txn_attribute_opt (cp_parser
*parser
)
36188 tree attr_name
, attr
= NULL
;
36190 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
36191 return cp_parser_attributes_opt (parser
);
36193 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
36195 cp_lexer_consume_token (parser
->lexer
);
36196 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
36199 token
= cp_lexer_peek_token (parser
->lexer
);
36200 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
36202 token
= cp_lexer_consume_token (parser
->lexer
);
36204 attr_name
= (token
->type
== CPP_KEYWORD
36205 /* For keywords, use the canonical spelling,
36206 not the parsed identifier. */
36207 ? ridpointers
[(int) token
->keyword
]
36209 attr
= build_tree_list (attr_name
, NULL_TREE
);
36212 cp_parser_error (parser
, "expected identifier");
36214 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
36216 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
36220 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36222 transaction-statement:
36223 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36225 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36229 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
36231 unsigned char old_in
= parser
->in_transaction
;
36232 unsigned char this_in
= 1, new_in
;
36233 enum rid keyword
= token
->keyword
;
36234 tree stmt
, attrs
, noex
;
36236 cp_lexer_consume_token (parser
->lexer
);
36238 if (keyword
== RID_TRANSACTION_RELAXED
36239 || keyword
== RID_SYNCHRONIZED
)
36240 this_in
|= TM_STMT_ATTR_RELAXED
;
36243 attrs
= cp_parser_txn_attribute_opt (parser
);
36245 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
36248 /* Parse a noexcept specification. */
36249 if (keyword
== RID_ATOMIC_NOEXCEPT
)
36250 noex
= boolean_true_node
;
36251 else if (keyword
== RID_ATOMIC_CANCEL
)
36253 /* cancel-and-throw is unimplemented. */
36254 sorry ("atomic_cancel");
36258 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
36260 /* Keep track if we're in the lexical scope of an outer transaction. */
36261 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
36263 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
36265 parser
->in_transaction
= new_in
;
36266 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
36267 parser
->in_transaction
= old_in
;
36269 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
36274 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36276 transaction-expression:
36277 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36278 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36282 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
36284 unsigned char old_in
= parser
->in_transaction
;
36285 unsigned char this_in
= 1;
36290 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
36291 || keyword
== RID_TRANSACTION_RELAXED
);
36294 error (keyword
== RID_TRANSACTION_RELAXED
36295 ? G_("%<__transaction_relaxed%> without transactional memory "
36297 : G_("%<__transaction_atomic%> without transactional memory "
36298 "support enabled"));
36300 token
= cp_parser_require_keyword (parser
, keyword
,
36301 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
36302 : RT_TRANSACTION_RELAXED
));
36303 gcc_assert (token
!= NULL
);
36305 if (keyword
== RID_TRANSACTION_RELAXED
)
36306 this_in
|= TM_STMT_ATTR_RELAXED
;
36308 /* Set this early. This might mean that we allow transaction_cancel in
36309 an expression that we find out later actually has to be a constexpr.
36310 However, we expect that cxx_constant_value will be able to deal with
36311 this; also, if the noexcept has no constexpr, then what we parse next
36312 really is a transaction's body. */
36313 parser
->in_transaction
= this_in
;
36315 /* Parse a noexcept specification. */
36316 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
36319 if (!noex
|| !noex_expr
36320 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36322 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
36324 expr
= cp_parser_expression (parser
);
36325 expr
= finish_parenthesized_expr (expr
);
36327 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
36331 /* The only expression that is available got parsed for the noexcept
36332 already. noexcept is true then. */
36334 noex
= boolean_true_node
;
36337 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
36338 parser
->in_transaction
= old_in
;
36340 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
36341 return error_mark_node
;
36343 return (flag_tm
? expr
: error_mark_node
);
36346 /* Parse a function-transaction-block.
36348 function-transaction-block:
36349 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36351 __transaction_atomic txn-attribute[opt] function-try-block
36352 __transaction_relaxed ctor-initializer[opt] function-body
36353 __transaction_relaxed function-try-block
36357 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
36359 unsigned char old_in
= parser
->in_transaction
;
36360 unsigned char new_in
= 1;
36361 tree compound_stmt
, stmt
, attrs
;
36362 bool ctor_initializer_p
;
36365 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
36366 || keyword
== RID_TRANSACTION_RELAXED
);
36367 token
= cp_parser_require_keyword (parser
, keyword
,
36368 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
36369 : RT_TRANSACTION_RELAXED
));
36370 gcc_assert (token
!= NULL
);
36372 if (keyword
== RID_TRANSACTION_RELAXED
)
36373 new_in
|= TM_STMT_ATTR_RELAXED
;
36376 attrs
= cp_parser_txn_attribute_opt (parser
);
36378 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
36381 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
36383 parser
->in_transaction
= new_in
;
36385 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
36386 ctor_initializer_p
= cp_parser_function_try_block (parser
);
36388 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
36389 (parser
, /*in_function_try_block=*/false);
36391 parser
->in_transaction
= old_in
;
36393 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
36395 return ctor_initializer_p
;
36398 /* Parse a __transaction_cancel statement.
36401 __transaction_cancel txn-attribute[opt] ;
36402 __transaction_cancel txn-attribute[opt] throw-expression ;
36404 ??? Cancel and throw is not yet implemented. */
36407 cp_parser_transaction_cancel (cp_parser
*parser
)
36410 bool is_outer
= false;
36413 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
36414 RT_TRANSACTION_CANCEL
);
36415 gcc_assert (token
!= NULL
);
36417 attrs
= cp_parser_txn_attribute_opt (parser
);
36419 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
36421 /* ??? Parse cancel-and-throw here. */
36423 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
36427 error_at (token
->location
, "%<__transaction_cancel%> without "
36428 "transactional memory support enabled");
36429 return error_mark_node
;
36431 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
36433 error_at (token
->location
, "%<__transaction_cancel%> within a "
36434 "%<__transaction_relaxed%>");
36435 return error_mark_node
;
36439 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
36440 && !is_tm_may_cancel_outer (current_function_decl
))
36442 error_at (token
->location
, "outer %<__transaction_cancel%> not "
36443 "within outer %<__transaction_atomic%>");
36444 error_at (token
->location
,
36445 " or a %<transaction_may_cancel_outer%> function");
36446 return error_mark_node
;
36449 else if (parser
->in_transaction
== 0)
36451 error_at (token
->location
, "%<__transaction_cancel%> not within "
36452 "%<__transaction_atomic%>");
36453 return error_mark_node
;
36456 stmt
= build_tm_abort_call (token
->location
, is_outer
);
36464 static GTY (()) cp_parser
*the_parser
;
36467 /* Special handling for the first token or line in the file. The first
36468 thing in the file might be #pragma GCC pch_preprocess, which loads a
36469 PCH file, which is a GC collection point. So we need to handle this
36470 first pragma without benefit of an existing lexer structure.
36472 Always returns one token to the caller in *FIRST_TOKEN. This is
36473 either the true first token of the file, or the first token after
36474 the initial pragma. */
36477 cp_parser_initial_pragma (cp_token
*first_token
)
36481 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36482 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
36485 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36486 if (first_token
->type
== CPP_STRING
)
36488 name
= first_token
->u
.value
;
36490 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36491 if (first_token
->type
!= CPP_PRAGMA_EOL
)
36492 error_at (first_token
->location
,
36493 "junk at end of %<#pragma GCC pch_preprocess%>");
36496 error_at (first_token
->location
, "expected string literal");
36498 /* Skip to the end of the pragma. */
36499 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
36500 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36502 /* Now actually load the PCH file. */
36504 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
36506 /* Read one more token to return to our caller. We have to do this
36507 after reading the PCH file in, since its pointers have to be
36509 cp_lexer_get_preprocessor_token (NULL
, first_token
);
36512 /* Parses the grainsize pragma for the _Cilk_for statement.
36514 #pragma cilk grainsize = <VALUE>. */
36517 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
36519 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36521 tree exp
= cp_parser_binary_expression (parser
, false, false,
36522 PREC_NOT_OPERATOR
, NULL
);
36523 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36524 if (!exp
|| exp
== error_mark_node
)
36526 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
36530 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
36531 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
36532 cp_parser_cilk_for (parser
, exp
);
36534 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
36535 "%<#pragma cilk grainsize%> is not followed by "
36539 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36542 /* Normal parsing of a pragma token. Here we can (and must) use the
36546 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
36548 cp_token
*pragma_tok
;
36553 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
36554 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
36555 parser
->lexer
->in_pragma
= true;
36557 id
= pragma_tok
->pragma_kind
;
36558 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
&& id
!= PRAGMA_OACC_ROUTINE
)
36559 cp_ensure_no_omp_declare_simd (parser
);
36562 case PRAGMA_GCC_PCH_PREPROCESS
:
36563 error_at (pragma_tok
->location
,
36564 "%<#pragma GCC pch_preprocess%> must be first");
36567 case PRAGMA_OMP_BARRIER
:
36570 case pragma_compound
:
36571 cp_parser_omp_barrier (parser
, pragma_tok
);
36574 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
36575 "used in compound statements");
36582 case PRAGMA_OMP_FLUSH
:
36585 case pragma_compound
:
36586 cp_parser_omp_flush (parser
, pragma_tok
);
36589 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
36590 "used in compound statements");
36597 case PRAGMA_OMP_TASKWAIT
:
36600 case pragma_compound
:
36601 cp_parser_omp_taskwait (parser
, pragma_tok
);
36604 error_at (pragma_tok
->location
,
36605 "%<#pragma omp taskwait%> may only be "
36606 "used in compound statements");
36613 case PRAGMA_OMP_TASKYIELD
:
36616 case pragma_compound
:
36617 cp_parser_omp_taskyield (parser
, pragma_tok
);
36620 error_at (pragma_tok
->location
,
36621 "%<#pragma omp taskyield%> may only be "
36622 "used in compound statements");
36629 case PRAGMA_OMP_CANCEL
:
36632 case pragma_compound
:
36633 cp_parser_omp_cancel (parser
, pragma_tok
);
36636 error_at (pragma_tok
->location
,
36637 "%<#pragma omp cancel%> may only be "
36638 "used in compound statements");
36645 case PRAGMA_OMP_CANCELLATION_POINT
:
36648 case pragma_compound
:
36649 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
36652 error_at (pragma_tok
->location
,
36653 "%<#pragma omp cancellation point%> may only be "
36654 "used in compound statements");
36661 case PRAGMA_OMP_THREADPRIVATE
:
36662 cp_parser_omp_threadprivate (parser
, pragma_tok
);
36665 case PRAGMA_OMP_DECLARE_REDUCTION
:
36666 cp_parser_omp_declare (parser
, pragma_tok
, context
);
36669 case PRAGMA_OACC_DECLARE
:
36670 cp_parser_oacc_declare (parser
, pragma_tok
);
36673 case PRAGMA_OACC_ROUTINE
:
36674 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
36677 case PRAGMA_OACC_ATOMIC
:
36678 case PRAGMA_OACC_CACHE
:
36679 case PRAGMA_OACC_DATA
:
36680 case PRAGMA_OACC_ENTER_DATA
:
36681 case PRAGMA_OACC_EXIT_DATA
:
36682 case PRAGMA_OACC_HOST_DATA
:
36683 case PRAGMA_OACC_KERNELS
:
36684 case PRAGMA_OACC_PARALLEL
:
36685 case PRAGMA_OACC_LOOP
:
36686 case PRAGMA_OACC_UPDATE
:
36687 case PRAGMA_OACC_WAIT
:
36688 case PRAGMA_OMP_ATOMIC
:
36689 case PRAGMA_OMP_CRITICAL
:
36690 case PRAGMA_OMP_DISTRIBUTE
:
36691 case PRAGMA_OMP_FOR
:
36692 case PRAGMA_OMP_MASTER
:
36693 case PRAGMA_OMP_PARALLEL
:
36694 case PRAGMA_OMP_SECTIONS
:
36695 case PRAGMA_OMP_SIMD
:
36696 case PRAGMA_OMP_SINGLE
:
36697 case PRAGMA_OMP_TASK
:
36698 case PRAGMA_OMP_TASKGROUP
:
36699 case PRAGMA_OMP_TASKLOOP
:
36700 case PRAGMA_OMP_TEAMS
:
36701 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
36703 stmt
= push_omp_privatization_clauses (false);
36704 cp_parser_omp_construct (parser
, pragma_tok
);
36705 pop_omp_privatization_clauses (stmt
);
36708 case PRAGMA_OMP_ORDERED
:
36709 stmt
= push_omp_privatization_clauses (false);
36710 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
);
36711 pop_omp_privatization_clauses (stmt
);
36714 case PRAGMA_OMP_TARGET
:
36715 stmt
= push_omp_privatization_clauses (false);
36716 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
);
36717 pop_omp_privatization_clauses (stmt
);
36720 case PRAGMA_OMP_END_DECLARE_TARGET
:
36721 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
36724 case PRAGMA_OMP_SECTION
:
36725 error_at (pragma_tok
->location
,
36726 "%<#pragma omp section%> may only be used in "
36727 "%<#pragma omp sections%> construct");
36732 if (context
== pragma_external
)
36734 error_at (pragma_tok
->location
,
36735 "%<#pragma GCC ivdep%> must be inside a function");
36738 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36740 tok
= cp_lexer_peek_token (the_parser
->lexer
);
36741 if (tok
->type
!= CPP_KEYWORD
36742 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
36743 && tok
->keyword
!= RID_DO
))
36745 cp_parser_error (parser
, "for, while or do statement expected");
36748 cp_parser_iteration_statement (parser
, true);
36752 case PRAGMA_CILK_SIMD
:
36753 if (context
== pragma_external
)
36755 error_at (pragma_tok
->location
,
36756 "%<#pragma simd%> must be inside a function");
36759 stmt
= push_omp_privatization_clauses (false);
36760 cp_parser_cilk_simd (parser
, pragma_tok
);
36761 pop_omp_privatization_clauses (stmt
);
36764 case PRAGMA_CILK_GRAINSIZE
:
36765 if (context
== pragma_external
)
36767 error_at (pragma_tok
->location
,
36768 "%<#pragma cilk grainsize%> must be inside a function");
36772 /* Ignore the pragma if Cilk Plus is not enabled. */
36775 cp_parser_cilk_grainsize (parser
, pragma_tok
);
36780 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
36781 "%<#pragma cilk grainsize%>");
36786 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
36787 c_invoke_pragma_handler (id
);
36791 cp_parser_error (parser
, "expected declaration specifiers");
36795 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36799 /* The interface the pragma parsers have to the lexer. */
36802 pragma_lex (tree
*value
, location_t
*loc
)
36804 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
36805 enum cpp_ttype ret
= tok
->type
;
36807 *value
= tok
->u
.value
;
36809 *loc
= tok
->location
;
36811 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
36813 else if (ret
== CPP_STRING
)
36814 *value
= cp_parser_string_literal (the_parser
, false, false);
36817 if (ret
== CPP_KEYWORD
)
36819 cp_lexer_consume_token (the_parser
->lexer
);
36826 /* External interface. */
36828 /* Parse one entire translation unit. */
36831 c_parse_file (void)
36833 static bool already_called
= false;
36835 if (already_called
)
36836 fatal_error (input_location
,
36837 "inter-module optimizations not implemented for C++");
36838 already_called
= true;
36840 the_parser
= cp_parser_new ();
36841 push_deferring_access_checks (flag_access_control
36842 ? dk_no_deferred
: dk_no_check
);
36843 cp_parser_translation_unit (the_parser
);
36847 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
36848 vectorlength clause:
36850 vectorlength ( constant-expression ) */
36853 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
36856 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36858 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
36859 safelen clause. Thus, vectorlength is represented as OMP 4.0
36860 safelen. For SIMD-enabled function it is represented by OMP 4.0
36863 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
36866 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
36869 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36870 return error_mark_node
;
36872 expr
= cp_parser_constant_expression (parser
);
36873 expr
= maybe_constant_value (expr
);
36875 /* If expr == error_mark_node, then don't emit any errors nor
36876 create a clause. if any of the above functions returns
36877 error mark node then they would have emitted an error message. */
36878 if (expr
== error_mark_node
)
36880 else if (!TREE_TYPE (expr
)
36881 || !TREE_CONSTANT (expr
)
36882 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
36883 error_at (loc
, "vectorlength must be an integer constant");
36884 else if (TREE_CONSTANT (expr
)
36885 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
36886 error_at (loc
, "vectorlength must be a power of 2");
36892 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
36893 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
36894 OMP_CLAUSE_CHAIN (c
) = clauses
;
36899 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
36900 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
36901 OMP_CLAUSE_CHAIN (c
) = clauses
;
36906 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
36907 return error_mark_node
;
36911 /* Handles the Cilk Plus #pragma simd linear clause.
36913 linear ( simd-linear-variable-list )
36915 simd-linear-variable-list:
36916 simd-linear-variable
36917 simd-linear-variable-list , simd-linear-variable
36919 simd-linear-variable:
36921 id-expression : simd-linear-step
36924 conditional-expression */
36927 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
36929 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36931 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36933 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36935 cp_parser_error (parser
, "expected identifier");
36936 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
36937 return error_mark_node
;
36940 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36941 parser
->colon_corrects_to_scope_p
= false;
36944 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36947 cp_parser_error (parser
, "expected variable-name");
36948 clauses
= error_mark_node
;
36952 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
36954 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
36956 if (decl
== error_mark_node
)
36958 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
36960 clauses
= error_mark_node
;
36964 tree e
= NULL_TREE
;
36965 tree step_size
= integer_one_node
;
36967 /* If present, parse the linear step. Otherwise, assume the default
36969 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
36971 cp_lexer_consume_token (parser
->lexer
);
36973 e
= cp_parser_assignment_expression (parser
);
36974 e
= maybe_constant_value (e
);
36976 if (e
== error_mark_node
)
36978 /* If an error has occurred, then the whole pragma is
36979 considered ill-formed. Thus, no reason to keep
36981 clauses
= error_mark_node
;
36984 else if (type_dependent_expression_p (e
)
36985 || value_dependent_expression_p (e
)
36987 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
36988 && (TREE_CONSTANT (e
)
36992 cp_parser_error (parser
,
36993 "step size must be an integer constant "
36994 "expression or an integer variable");
36997 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
36998 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
36999 OMP_CLAUSE_DECL (l
) = decl
;
37000 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
37001 OMP_CLAUSE_CHAIN (l
) = clauses
;
37004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37005 cp_lexer_consume_token (parser
->lexer
);
37006 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
37010 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37011 "expected %<,%> or %<)%> after %qE", decl
);
37012 clauses
= error_mark_node
;
37016 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37017 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
37021 /* Returns the name of the next clause. If the clause is not
37022 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37023 token is not consumed. Otherwise, the appropriate enum from the
37024 pragma_simd_clause is returned and the token is consumed. */
37026 static pragma_omp_clause
37027 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
37029 pragma_omp_clause clause_type
;
37030 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37032 if (token
->keyword
== RID_PRIVATE
)
37033 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
37034 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
37035 return PRAGMA_CILK_CLAUSE_NONE
;
37036 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
37037 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
37038 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
37039 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
37040 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
37041 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
37042 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
37043 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
37044 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
37045 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
37047 return PRAGMA_CILK_CLAUSE_NONE
;
37049 cp_lexer_consume_token (parser
->lexer
);
37050 return clause_type
;
37053 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37056 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
37058 tree clauses
= NULL_TREE
;
37060 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
37061 && clauses
!= error_mark_node
)
37063 pragma_omp_clause c_kind
;
37064 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
37065 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
37066 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
37067 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
37068 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
37069 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
37070 /* Use the OpenMP 4.0 equivalent function. */
37071 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
37072 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
37073 /* Use the OpenMP 4.0 equivalent function. */
37074 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37076 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
37077 /* Use the OMP 4.0 equivalent function. */
37078 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
37080 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
37081 /* Use the OMP 4.0 equivalent function. */
37082 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
37085 clauses
= error_mark_node
;
37086 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
37091 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
37093 if (clauses
== error_mark_node
)
37094 return error_mark_node
;
37096 return c_finish_cilk_clauses (clauses
);
37099 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37102 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
37104 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
37106 if (clauses
== error_mark_node
)
37109 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
37111 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37112 "for statement expected");
37116 tree sb
= begin_omp_structured_block ();
37117 int save
= cp_parser_begin_omp_structured_block (parser
);
37118 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
37120 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
37121 cp_parser_end_omp_structured_block (parser
, save
);
37122 add_stmt (finish_omp_structured_block (sb
));
37125 /* Main entry-point for parsing Cilk Plus _Cilk_for
37126 loops. The return value is error_mark_node
37127 when errors happen and CILK_FOR tree on success. */
37130 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
37132 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
37133 gcc_unreachable ();
37135 tree sb
= begin_omp_structured_block ();
37136 int save
= cp_parser_begin_omp_structured_block (parser
);
37138 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
37139 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
37140 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
37141 clauses
= finish_omp_clauses (clauses
, false);
37143 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
37145 cpp_validate_cilk_plus_loop (ret
);
37147 ret
= error_mark_node
;
37149 cp_parser_end_omp_structured_block (parser
, save
);
37150 add_stmt (finish_omp_structured_block (sb
));
37154 /* Create an identifier for a generic parameter type (a synthesized
37155 template parameter implied by `auto' or a concept identifier). */
37157 static GTY(()) int generic_parm_count
;
37159 make_generic_type_name ()
37162 sprintf (buf
, "auto:%d", ++generic_parm_count
);
37163 return get_identifier (buf
);
37166 /* Predicate that behaves as is_auto_or_concept but matches the parent
37167 node of the generic type rather than the generic type itself. This
37168 allows for type transformation in add_implicit_template_parms. */
37171 tree_type_is_auto_or_concept (const_tree t
)
37173 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
37176 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37177 (creating a new template parameter list if necessary). Returns the newly
37178 created template type parm. */
37181 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
37183 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
37185 /* Before committing to modifying any scope, if we're in an
37186 implicit template scope, and we're trying to synthesize a
37187 constrained parameter, try to find a previous parameter with
37188 the same name. This is the same-type rule for abbreviated
37189 function templates. */
37190 if (parser
->implicit_template_scope
&& constr
)
37192 tree t
= parser
->implicit_template_parms
;
37195 if (equivalent_placeholder_constraints (TREE_TYPE (t
), constr
))
37197 tree d
= TREE_VALUE (t
);
37198 if (TREE_CODE (d
) == PARM_DECL
)
37199 /* Return the TEMPLATE_PARM_INDEX. */
37200 d
= DECL_INITIAL (d
);
37203 t
= TREE_CHAIN (t
);
37207 /* We are either continuing a function template that already contains implicit
37208 template parameters, creating a new fully-implicit function template, or
37209 extending an existing explicit function template with implicit template
37212 cp_binding_level
*const entry_scope
= current_binding_level
;
37214 bool become_template
= false;
37215 cp_binding_level
*parent_scope
= 0;
37217 if (parser
->implicit_template_scope
)
37219 gcc_assert (parser
->implicit_template_parms
);
37221 current_binding_level
= parser
->implicit_template_scope
;
37225 /* Roll back to the existing template parameter scope (in the case of
37226 extending an explicit function template) or introduce a new template
37227 parameter scope ahead of the function parameter scope (or class scope
37228 in the case of out-of-line member definitions). The function scope is
37229 added back after template parameter synthesis below. */
37231 cp_binding_level
*scope
= entry_scope
;
37233 while (scope
->kind
== sk_function_parms
)
37235 parent_scope
= scope
;
37236 scope
= scope
->level_chain
;
37238 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
37240 /* If not defining a class, then any class scope is a scope level in
37241 an out-of-line member definition. In this case simply wind back
37242 beyond the first such scope to inject the template parameter list.
37243 Otherwise wind back to the class being defined. The latter can
37244 occur in class member friend declarations such as:
37250 friend void A::foo (auto);
37253 The template parameter list synthesized for the friend declaration
37254 must be injected in the scope of 'B'. This can also occur in
37255 erroneous cases such as:
37261 void B::foo (auto) {}
37264 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37265 but, nevertheless, the template parameter list synthesized for the
37266 declarator should be injected into the scope of 'A' as if the
37267 ill-formed template was specified explicitly. */
37269 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
37271 parent_scope
= scope
;
37272 scope
= scope
->level_chain
;
37276 current_binding_level
= scope
;
37278 if (scope
->kind
!= sk_template_parms
37279 || !function_being_declared_is_template_p (parser
))
37281 /* Introduce a new template parameter list for implicit template
37284 become_template
= true;
37286 parser
->implicit_template_scope
37287 = begin_scope (sk_template_parms
, NULL
);
37289 ++processing_template_decl
;
37291 parser
->fully_implicit_function_template_p
= true;
37292 ++parser
->num_template_parameter_lists
;
37296 /* Synthesize implicit template parameters at the end of the explicit
37297 template parameter list. */
37299 gcc_assert (current_template_parms
);
37301 parser
->implicit_template_scope
= scope
;
37303 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
37304 parser
->implicit_template_parms
37305 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
37309 /* Synthesize a new template parameter and track the current template
37310 parameter chain with implicit_template_parms. */
37312 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
37313 tree synth_id
= make_generic_type_name ();
37314 tree synth_tmpl_parm
;
37315 bool non_type
= false;
37317 if (proto
== NULL_TREE
|| TREE_CODE (proto
) == TYPE_DECL
)
37319 = finish_template_type_parm (class_type_node
, synth_id
);
37320 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
37322 = finish_constrained_template_template_parm (proto
, synth_id
);
37325 synth_tmpl_parm
= copy_decl (proto
);
37326 DECL_NAME (synth_tmpl_parm
) = synth_id
;
37330 // Attach the constraint to the parm before processing.
37331 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
37332 TREE_TYPE (node
) = constr
;
37334 = process_template_parm (parser
->implicit_template_parms
,
37337 /*non_type=*/non_type
,
37338 /*param_pack=*/false);
37340 // Chain the new parameter to the list of implicit parameters.
37341 if (parser
->implicit_template_parms
)
37342 parser
->implicit_template_parms
37343 = TREE_CHAIN (parser
->implicit_template_parms
);
37345 parser
->implicit_template_parms
= new_parm
;
37347 tree new_decl
= getdecls ();
37349 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37350 new_decl
= DECL_INITIAL (new_decl
);
37352 /* If creating a fully implicit function template, start the new implicit
37353 template parameter list with this synthesized type, otherwise grow the
37354 current template parameter list. */
37356 if (become_template
)
37358 parent_scope
->level_chain
= current_binding_level
;
37360 tree new_parms
= make_tree_vec (1);
37361 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
37362 current_template_parms
= tree_cons (size_int (processing_template_decl
),
37363 new_parms
, current_template_parms
);
37367 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
37368 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
37369 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
37370 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
37373 // If the new parameter was constrained, we need to add that to the
37374 // constraints in the template parameter list.
37375 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
37377 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
37378 reqs
= conjoin_constraints (reqs
, req
);
37379 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
37382 current_binding_level
= entry_scope
;
37387 /* Finish the declaration of a fully implicit function template. Such a
37388 template has no explicit template parameter list so has not been through the
37389 normal template head and tail processing. synthesize_implicit_template_parm
37390 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37391 provided if the declaration is a class member such that its template
37392 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37393 form is returned. Otherwise NULL_TREE is returned. */
37396 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
37398 gcc_assert (parser
->fully_implicit_function_template_p
);
37400 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
37401 && DECL_VIRTUAL_P (member_decl_opt
))
37403 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
37404 "implicit templates may not be %<virtual%>");
37405 DECL_VIRTUAL_P (member_decl_opt
) = false;
37408 if (member_decl_opt
)
37409 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
37410 end_template_decl ();
37412 parser
->fully_implicit_function_template_p
= false;
37413 --parser
->num_template_parameter_lists
;
37415 return member_decl_opt
;
37418 #include "gt-cp-parser.h"