2 Copyright (C) 2000-2014 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"
28 #include "print-tree.h"
29 #include "stringpool.h"
31 #include "trans-mem.h"
34 #include "c-family/c-pragma.h"
37 #include "diagnostic-core.h"
41 #include "plugin-api.h"
46 #include "hard-reg-set.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
54 #include "tree-pretty-print.h"
56 #include "type-utils.h"
58 #include "gomp-constants.h"
63 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
64 and c-lex.c) and the C++ parser. */
66 static cp_token eof_token
=
68 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
71 /* The various kinds of non integral constant we encounter. */
72 typedef enum non_integral_constant
{
74 /* floating-point literal */
78 /* %<__FUNCTION__%> */
80 /* %<__PRETTY_FUNCTION__%> */
88 /* %<typeid%> operator */
90 /* non-constant compound literals */
98 /* an array reference */
104 /* the address of a label */
118 /* calls to overloaded operators */
122 /* a comma operator */
124 /* a call to a constructor */
126 /* a transaction expression */
128 } non_integral_constant
;
130 /* The various kinds of errors about name-lookup failing. */
131 typedef enum name_lookup_error
{
136 /* is not a class or namespace */
138 /* is not a class, namespace, or enumeration */
142 /* The various kinds of required token */
143 typedef enum required_token
{
145 RT_SEMICOLON
, /* ';' */
146 RT_OPEN_PAREN
, /* '(' */
147 RT_CLOSE_BRACE
, /* '}' */
148 RT_OPEN_BRACE
, /* '{' */
149 RT_CLOSE_SQUARE
, /* ']' */
150 RT_OPEN_SQUARE
, /* '[' */
154 RT_GREATER
, /* '>' */
156 RT_ELLIPSIS
, /* '...' */
160 RT_COLON_SCOPE
, /* ':' or '::' */
161 RT_CLOSE_PAREN
, /* ')' */
162 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
163 RT_PRAGMA_EOL
, /* end of line */
164 RT_NAME
, /* identifier */
166 /* The type is CPP_KEYWORD */
168 RT_DELETE
, /* delete */
169 RT_RETURN
, /* return */
170 RT_WHILE
, /* while */
171 RT_EXTERN
, /* extern */
172 RT_STATIC_ASSERT
, /* static_assert */
173 RT_DECLTYPE
, /* decltype */
174 RT_OPERATOR
, /* operator */
175 RT_CLASS
, /* class */
176 RT_TEMPLATE
, /* template */
177 RT_NAMESPACE
, /* namespace */
178 RT_USING
, /* using */
181 RT_CATCH
, /* catch */
182 RT_THROW
, /* throw */
183 RT_LABEL
, /* __label__ */
184 RT_AT_TRY
, /* @try */
185 RT_AT_SYNCHRONIZED
, /* @synchronized */
186 RT_AT_THROW
, /* @throw */
188 RT_SELECT
, /* selection-statement */
189 RT_INTERATION
, /* iteration-statement */
190 RT_JUMP
, /* jump-statement */
191 RT_CLASS_KEY
, /* class-key */
192 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
193 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
194 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
195 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
200 static cp_lexer
*cp_lexer_new_main
202 static cp_lexer
*cp_lexer_new_from_tokens
203 (cp_token_cache
*tokens
);
204 static void cp_lexer_destroy
206 static int cp_lexer_saving_tokens
208 static cp_token
*cp_lexer_token_at
209 (cp_lexer
*, cp_token_position
);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer
*, cp_token
*);
212 static inline cp_token
*cp_lexer_peek_token
214 static cp_token
*cp_lexer_peek_nth_token
215 (cp_lexer
*, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer
*, enum cpp_ttype
);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer
*, enum cpp_ttype
);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer
*, enum rid
);
222 static cp_token
*cp_lexer_consume_token
224 static void cp_lexer_purge_token
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer
*, cp_token_position
);
228 static void cp_lexer_save_tokens
230 static void cp_lexer_commit_tokens
232 static void cp_lexer_rollback_tokens
234 static void cp_lexer_print_token
235 (FILE *, cp_token
*);
236 static inline bool cp_lexer_debugging_p
238 static void cp_lexer_start_debugging
239 (cp_lexer
*) ATTRIBUTE_UNUSED
;
240 static void cp_lexer_stop_debugging
241 (cp_lexer
*) ATTRIBUTE_UNUSED
;
243 static cp_token_cache
*cp_token_cache_new
244 (cp_token
*, cp_token
*);
246 static void cp_parser_initial_pragma
249 static tree cp_literal_operator_id
252 static void cp_parser_cilk_simd
253 (cp_parser
*, cp_token
*);
254 static tree cp_parser_cilk_for
256 static bool cp_parser_omp_declare_reduction_exprs
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser
*, tree
, bool);
261 /* Manifest constants. */
262 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
263 #define CP_SAVED_TOKEN_STACK 5
267 /* The stream to which debugging output should be written. */
268 static FILE *cp_lexer_debug_stream
;
270 /* Nonzero if we are parsing an unevaluated operand: an operand to
271 sizeof, typeof, or alignof. */
272 int cp_unevaluated_operand
;
274 /* Dump up to NUM tokens in BUFFER to FILE starting with token
275 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
276 first token in BUFFER. If NUM is 0, dump all the tokens. If
277 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
278 highlighted by surrounding it in [[ ]]. */
281 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
282 cp_token
*start_token
, unsigned num
,
283 cp_token
*curr_token
)
285 unsigned i
, nprinted
;
289 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
295 num
= buffer
->length ();
297 if (start_token
== NULL
)
298 start_token
= buffer
->address ();
300 if (start_token
> buffer
->address ())
302 cp_lexer_print_token (file
, &(*buffer
)[0]);
303 fprintf (file
, " ... ");
308 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
310 if (token
== start_token
)
317 if (token
== curr_token
)
318 fprintf (file
, "[[");
320 cp_lexer_print_token (file
, token
);
322 if (token
== curr_token
)
323 fprintf (file
, "]]");
329 case CPP_CLOSE_BRACE
:
339 if (i
== num
&& i
< buffer
->length ())
341 fprintf (file
, " ... ");
342 cp_lexer_print_token (file
, &buffer
->last ());
345 fprintf (file
, "\n");
349 /* Dump all tokens in BUFFER to stderr. */
352 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
354 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
358 debug (vec
<cp_token
, va_gc
> &ref
)
360 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
364 debug (vec
<cp_token
, va_gc
> *ptr
)
369 fprintf (stderr
, "<nil>\n");
373 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
374 description for T. */
377 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
381 fprintf (file
, "%s: ", desc
);
382 print_node_brief (file
, "", t
, 0);
387 /* Dump parser context C to FILE. */
390 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
392 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
393 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
394 print_node_brief (file
, "", c
->object_type
, 0);
395 fprintf (file
, "}\n");
399 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
405 cp_parser_context
*c
;
407 fprintf (file
, "Parsing context stack:\n");
408 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
410 fprintf (file
, "\t#%u: ", i
);
411 cp_debug_print_context (file
, c
);
416 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
422 fprintf (file
, "%s: true\n", desc
);
426 /* Print an unparsed function entry UF to FILE. */
429 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
432 cp_default_arg_entry
*default_arg_fn
;
435 fprintf (file
, "\tFunctions with default args:\n");
437 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
440 fprintf (file
, "\t\tClass type: ");
441 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
442 fprintf (file
, "\t\tDeclaration: ");
443 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
444 fprintf (file
, "\n");
447 fprintf (file
, "\n\tFunctions with definitions that require "
448 "post-processing\n\t\t");
449 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
451 print_node_brief (file
, "", fn
, 0);
454 fprintf (file
, "\n");
456 fprintf (file
, "\n\tNon-static data members with initializers that require "
457 "post-processing\n\t\t");
458 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
460 print_node_brief (file
, "", fn
, 0);
463 fprintf (file
, "\n");
467 /* Print the stack of unparsed member functions S to FILE. */
470 cp_debug_print_unparsed_queues (FILE *file
,
471 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
474 cp_unparsed_functions_entry
*uf
;
476 fprintf (file
, "Unparsed functions\n");
477 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
479 fprintf (file
, "#%u:\n", i
);
480 cp_debug_print_unparsed_function (file
, uf
);
485 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
486 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
491 cp_token
*next_token
, *first_token
, *start_token
;
496 next_token
= parser
->lexer
->next_token
;
497 first_token
= parser
->lexer
->buffer
->address ();
498 start_token
= (next_token
> first_token
+ window_size
/ 2)
499 ? next_token
- window_size
/ 2
501 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
506 /* Dump debugging information for the given PARSER. If FILE is NULL,
507 the output is printed on stderr. */
510 cp_debug_parser (FILE *file
, cp_parser
*parser
)
512 const size_t window_size
= 20;
514 expanded_location eloc
;
519 fprintf (file
, "Parser state\n\n");
520 fprintf (file
, "Number of tokens: %u\n",
521 vec_safe_length (parser
->lexer
->buffer
));
522 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
523 cp_debug_print_tree_if_set (file
, "Object scope",
524 parser
->object_scope
);
525 cp_debug_print_tree_if_set (file
, "Qualifying scope",
526 parser
->qualifying_scope
);
527 cp_debug_print_context_stack (file
, parser
->context
);
528 cp_debug_print_flag (file
, "Allow GNU extensions",
529 parser
->allow_gnu_extensions_p
);
530 cp_debug_print_flag (file
, "'>' token is greater-than",
531 parser
->greater_than_is_operator_p
);
532 cp_debug_print_flag (file
, "Default args allowed in current "
533 "parameter list", parser
->default_arg_ok_p
);
534 cp_debug_print_flag (file
, "Parsing integral constant-expression",
535 parser
->integral_constant_expression_p
);
536 cp_debug_print_flag (file
, "Allow non-constant expression in current "
537 "constant-expression",
538 parser
->allow_non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Seen non-constant expression",
540 parser
->non_integral_constant_expression_p
);
541 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
543 parser
->local_variables_forbidden_p
);
544 cp_debug_print_flag (file
, "In unbraced linkage specification",
545 parser
->in_unbraced_linkage_specification_p
);
546 cp_debug_print_flag (file
, "Parsing a declarator",
547 parser
->in_declarator_p
);
548 cp_debug_print_flag (file
, "In template argument list",
549 parser
->in_template_argument_list_p
);
550 cp_debug_print_flag (file
, "Parsing an iteration statement",
551 parser
->in_statement
& IN_ITERATION_STMT
);
552 cp_debug_print_flag (file
, "Parsing a switch statement",
553 parser
->in_statement
& IN_SWITCH_STMT
);
554 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
555 parser
->in_statement
& IN_OMP_BLOCK
);
556 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
557 parser
->in_statement
& IN_CILK_SIMD_FOR
);
558 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
559 parser
->in_statement
& IN_OMP_FOR
);
560 cp_debug_print_flag (file
, "Parsing an if statement",
561 parser
->in_statement
& IN_IF_STMT
);
562 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
563 "context", parser
->in_type_id_in_expr_p
);
564 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
565 parser
->implicit_extern_c
);
566 cp_debug_print_flag (file
, "String expressions should be translated "
567 "to execution character set",
568 parser
->translate_strings_p
);
569 cp_debug_print_flag (file
, "Parsing function body outside of a "
570 "local class", parser
->in_function_body
);
571 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
572 parser
->colon_corrects_to_scope_p
);
573 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
574 parser
->colon_doesnt_start_class_def_p
);
575 if (parser
->type_definition_forbidden_message
)
576 fprintf (file
, "Error message for forbidden type definitions: %s\n",
577 parser
->type_definition_forbidden_message
);
578 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
579 fprintf (file
, "Number of class definitions in progress: %u\n",
580 parser
->num_classes_being_defined
);
581 fprintf (file
, "Number of template parameter lists for the current "
582 "declaration: %u\n", parser
->num_template_parameter_lists
);
583 cp_debug_parser_tokens (file
, parser
, window_size
);
584 token
= parser
->lexer
->next_token
;
585 fprintf (file
, "Next token to parse:\n");
586 fprintf (file
, "\tToken: ");
587 cp_lexer_print_token (file
, token
);
588 eloc
= expand_location (token
->location
);
589 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
590 fprintf (file
, "\tLine: %d\n", eloc
.line
);
591 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
595 debug (cp_parser
&ref
)
597 cp_debug_parser (stderr
, &ref
);
601 debug (cp_parser
*ptr
)
606 fprintf (stderr
, "<nil>\n");
609 /* Allocate memory for a new lexer object and return it. */
612 cp_lexer_alloc (void)
616 c_common_no_more_pch ();
618 /* Allocate the memory. */
619 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
621 /* Initially we are not debugging. */
622 lexer
->debugging_p
= false;
624 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
626 /* Create the buffer. */
627 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
633 /* Create a new main C++ lexer, the lexer that gets tokens from the
637 cp_lexer_new_main (void)
642 /* It's possible that parsing the first pragma will load a PCH file,
643 which is a GC collection point. So we have to do that before
644 allocating any memory. */
645 cp_parser_initial_pragma (&token
);
647 lexer
= cp_lexer_alloc ();
649 /* Put the first token in the buffer. */
650 lexer
->buffer
->quick_push (token
);
652 /* Get the remaining tokens from the preprocessor. */
653 while (token
.type
!= CPP_EOF
)
655 cp_lexer_get_preprocessor_token (lexer
, &token
);
656 vec_safe_push (lexer
->buffer
, token
);
659 lexer
->last_token
= lexer
->buffer
->address ()
660 + lexer
->buffer
->length ()
662 lexer
->next_token
= lexer
->buffer
->length ()
663 ? lexer
->buffer
->address ()
666 /* Subsequent preprocessor diagnostics should use compiler
667 diagnostic functions to get the compiler source location. */
670 gcc_assert (!lexer
->next_token
->purged_p
);
674 /* Create a new lexer whose token stream is primed with the tokens in
675 CACHE. When these tokens are exhausted, no new tokens will be read. */
678 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
680 cp_token
*first
= cache
->first
;
681 cp_token
*last
= cache
->last
;
682 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
684 /* We do not own the buffer. */
685 lexer
->buffer
= NULL
;
686 lexer
->next_token
= first
== last
? &eof_token
: first
;
687 lexer
->last_token
= last
;
689 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
691 /* Initially we are not debugging. */
692 lexer
->debugging_p
= false;
694 gcc_assert (!lexer
->next_token
->purged_p
);
698 /* Frees all resources associated with LEXER. */
701 cp_lexer_destroy (cp_lexer
*lexer
)
703 vec_free (lexer
->buffer
);
704 lexer
->saved_tokens
.release ();
708 /* Returns nonzero if debugging information should be output. */
711 cp_lexer_debugging_p (cp_lexer
*lexer
)
713 return lexer
->debugging_p
;
717 static inline cp_token_position
718 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
720 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
722 return lexer
->next_token
- previous_p
;
725 static inline cp_token
*
726 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
732 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
734 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
737 static inline cp_token_position
738 cp_lexer_previous_token_position (cp_lexer
*lexer
)
740 if (lexer
->next_token
== &eof_token
)
741 return lexer
->last_token
- 1;
743 return cp_lexer_token_position (lexer
, true);
746 static inline cp_token
*
747 cp_lexer_previous_token (cp_lexer
*lexer
)
749 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
751 return cp_lexer_token_at (lexer
, tp
);
754 /* nonzero if we are presently saving tokens. */
757 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
759 return lexer
->saved_tokens
.length () != 0;
762 /* Store the next token from the preprocessor in *TOKEN. Return true
763 if we reach EOF. If LEXER is NULL, assume we are handling an
764 initial #pragma pch_preprocess, and thus want the lexer to return
765 processed strings. */
768 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
770 static int is_extern_c
= 0;
772 /* Get a new token from the preprocessor. */
774 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
775 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
776 token
->keyword
= RID_MAX
;
777 token
->pragma_kind
= PRAGMA_NONE
;
778 token
->purged_p
= false;
779 token
->error_reported
= false;
781 /* On some systems, some header files are surrounded by an
782 implicit extern "C" block. Set a flag in the token if it
783 comes from such a header. */
784 is_extern_c
+= pending_lang_change
;
785 pending_lang_change
= 0;
786 token
->implicit_extern_c
= is_extern_c
> 0;
788 /* Check to see if this token is a keyword. */
789 if (token
->type
== CPP_NAME
)
791 if (C_IS_RESERVED_WORD (token
->u
.value
))
793 /* Mark this token as a keyword. */
794 token
->type
= CPP_KEYWORD
;
795 /* Record which keyword. */
796 token
->keyword
= C_RID_CODE (token
->u
.value
);
800 if (warn_cxx0x_compat
801 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
802 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
804 /* Warn about the C++0x keyword (but still treat it as
806 warning (OPT_Wc__0x_compat
,
807 "identifier %qE is a keyword in C++11",
810 /* Clear out the C_RID_CODE so we don't warn about this
811 particular identifier-turned-keyword again. */
812 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
815 token
->keyword
= RID_MAX
;
818 else if (token
->type
== CPP_AT_NAME
)
820 /* This only happens in Objective-C++; it must be a keyword. */
821 token
->type
= CPP_KEYWORD
;
822 switch (C_RID_CODE (token
->u
.value
))
824 /* Replace 'class' with '@class', 'private' with '@private',
825 etc. This prevents confusion with the C++ keyword
826 'class', and makes the tokens consistent with other
827 Objective-C 'AT' keywords. For example '@class' is
828 reported as RID_AT_CLASS which is consistent with
829 '@synchronized', which is reported as
832 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
833 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
834 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
835 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
836 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
837 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
838 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
839 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
842 else if (token
->type
== CPP_PRAGMA
)
844 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
845 token
->pragma_kind
= ((enum pragma_kind
)
846 TREE_INT_CST_LOW (token
->u
.value
));
847 token
->u
.value
= NULL_TREE
;
851 /* Update the globals input_location and the input file stack from TOKEN. */
853 cp_lexer_set_source_position_from_token (cp_token
*token
)
855 if (token
->type
!= CPP_EOF
)
857 input_location
= token
->location
;
861 /* Update the globals input_location and the input file stack from LEXER. */
863 cp_lexer_set_source_position (cp_lexer
*lexer
)
865 cp_token
*token
= cp_lexer_peek_token (lexer
);
866 cp_lexer_set_source_position_from_token (token
);
869 /* Return a pointer to the next token in the token stream, but do not
872 static inline cp_token
*
873 cp_lexer_peek_token (cp_lexer
*lexer
)
875 if (cp_lexer_debugging_p (lexer
))
877 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
878 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
879 putc ('\n', cp_lexer_debug_stream
);
881 return lexer
->next_token
;
884 /* Return true if the next token has the indicated TYPE. */
887 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
889 return cp_lexer_peek_token (lexer
)->type
== type
;
892 /* Return true if the next token does not have the indicated TYPE. */
895 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
897 return !cp_lexer_next_token_is (lexer
, type
);
900 /* Return true if the next token is the indicated KEYWORD. */
903 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
905 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
909 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
911 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
915 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
917 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
920 /* Return true if the next token is not the indicated KEYWORD. */
923 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
925 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
928 /* Return true if the next token is a keyword for a decl-specifier. */
931 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
935 token
= cp_lexer_peek_token (lexer
);
936 switch (token
->keyword
)
938 /* auto specifier: storage-class-specifier in C++,
939 simple-type-specifier in C++0x. */
941 /* Storage classes. */
947 /* Elaborated type specifiers. */
953 /* Simple type specifiers. */
967 /* GNU extensions. */
970 /* C++0x extensions. */
972 case RID_UNDERLYING_TYPE
:
976 if (token
->keyword
>= RID_FIRST_INT_N
977 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
978 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
984 /* Returns TRUE iff the token T begins a decltype type. */
987 token_is_decltype (cp_token
*t
)
989 return (t
->keyword
== RID_DECLTYPE
990 || t
->type
== CPP_DECLTYPE
);
993 /* Returns TRUE iff the next token begins a decltype type. */
996 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
998 cp_token
*t
= cp_lexer_peek_token (lexer
);
999 return token_is_decltype (t
);
1002 /* Return a pointer to the Nth token in the token stream. If N is 1,
1003 then this is precisely equivalent to cp_lexer_peek_token (except
1004 that it is not inline). One would like to disallow that case, but
1005 there is one case (cp_parser_nth_token_starts_template_id) where
1006 the caller passes a variable for N and it might be 1. */
1009 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1013 /* N is 1-based, not zero-based. */
1016 if (cp_lexer_debugging_p (lexer
))
1017 fprintf (cp_lexer_debug_stream
,
1018 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1021 token
= lexer
->next_token
;
1022 gcc_assert (!n
|| token
!= &eof_token
);
1026 if (token
== lexer
->last_token
)
1032 if (!token
->purged_p
)
1036 if (cp_lexer_debugging_p (lexer
))
1038 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1039 putc ('\n', cp_lexer_debug_stream
);
1045 /* Return the next token, and advance the lexer's next_token pointer
1046 to point to the next non-purged token. */
1049 cp_lexer_consume_token (cp_lexer
* lexer
)
1051 cp_token
*token
= lexer
->next_token
;
1053 gcc_assert (token
!= &eof_token
);
1054 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1058 lexer
->next_token
++;
1059 if (lexer
->next_token
== lexer
->last_token
)
1061 lexer
->next_token
= &eof_token
;
1066 while (lexer
->next_token
->purged_p
);
1068 cp_lexer_set_source_position_from_token (token
);
1070 /* Provide debugging output. */
1071 if (cp_lexer_debugging_p (lexer
))
1073 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1074 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1075 putc ('\n', cp_lexer_debug_stream
);
1081 /* Permanently remove the next token from the token stream, and
1082 advance the next_token pointer to refer to the next non-purged
1086 cp_lexer_purge_token (cp_lexer
*lexer
)
1088 cp_token
*tok
= lexer
->next_token
;
1090 gcc_assert (tok
!= &eof_token
);
1091 tok
->purged_p
= true;
1092 tok
->location
= UNKNOWN_LOCATION
;
1093 tok
->u
.value
= NULL_TREE
;
1094 tok
->keyword
= RID_MAX
;
1099 if (tok
== lexer
->last_token
)
1105 while (tok
->purged_p
);
1106 lexer
->next_token
= tok
;
1109 /* Permanently remove all tokens after TOK, up to, but not
1110 including, the token that will be returned next by
1111 cp_lexer_peek_token. */
1114 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1116 cp_token
*peek
= lexer
->next_token
;
1118 if (peek
== &eof_token
)
1119 peek
= lexer
->last_token
;
1121 gcc_assert (tok
< peek
);
1123 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1125 tok
->purged_p
= true;
1126 tok
->location
= UNKNOWN_LOCATION
;
1127 tok
->u
.value
= NULL_TREE
;
1128 tok
->keyword
= RID_MAX
;
1132 /* Begin saving tokens. All tokens consumed after this point will be
1136 cp_lexer_save_tokens (cp_lexer
* lexer
)
1138 /* Provide debugging output. */
1139 if (cp_lexer_debugging_p (lexer
))
1140 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1142 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1145 /* Commit to the portion of the token stream most recently saved. */
1148 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1150 /* Provide debugging output. */
1151 if (cp_lexer_debugging_p (lexer
))
1152 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1154 lexer
->saved_tokens
.pop ();
1157 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1158 to the token stream. Stop saving tokens. */
1161 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1163 /* Provide debugging output. */
1164 if (cp_lexer_debugging_p (lexer
))
1165 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1167 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1170 /* RAII wrapper around the above functions, with sanity checking. Creating
1171 a variable saves tokens, which are committed when the variable is
1172 destroyed unless they are explicitly rolled back by calling the rollback
1175 struct saved_token_sentinel
1180 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1182 len
= lexer
->saved_tokens
.length ();
1183 cp_lexer_save_tokens (lexer
);
1187 cp_lexer_rollback_tokens (lexer
);
1190 ~saved_token_sentinel()
1193 cp_lexer_commit_tokens (lexer
);
1194 gcc_assert (lexer
->saved_tokens
.length () == len
);
1198 /* Print a representation of the TOKEN on the STREAM. */
1201 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1203 /* We don't use cpp_type2name here because the parser defines
1204 a few tokens of its own. */
1205 static const char *const token_names
[] = {
1206 /* cpplib-defined token types */
1207 #define OP(e, s) #e,
1208 #define TK(e, s) #e,
1212 /* C++ parser token types - see "Manifest constants", above. */
1215 "NESTED_NAME_SPECIFIER",
1218 /* For some tokens, print the associated data. */
1219 switch (token
->type
)
1222 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1223 For example, `struct' is mapped to an INTEGER_CST. */
1224 if (!identifier_p (token
->u
.value
))
1226 /* else fall through */
1228 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1235 case CPP_UTF8STRING
:
1236 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1240 print_generic_expr (stream
, token
->u
.value
, 0);
1244 /* If we have a name for the token, print it out. Otherwise, we
1245 simply give the numeric code. */
1246 if (token
->type
< ARRAY_SIZE(token_names
))
1247 fputs (token_names
[token
->type
], stream
);
1249 fprintf (stream
, "[%d]", token
->type
);
1255 debug (cp_token
&ref
)
1257 cp_lexer_print_token (stderr
, &ref
);
1258 fprintf (stderr
, "\n");
1262 debug (cp_token
*ptr
)
1267 fprintf (stderr
, "<nil>\n");
1271 /* Start emitting debugging information. */
1274 cp_lexer_start_debugging (cp_lexer
* lexer
)
1276 lexer
->debugging_p
= true;
1277 cp_lexer_debug_stream
= stderr
;
1280 /* Stop emitting debugging information. */
1283 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1285 lexer
->debugging_p
= false;
1286 cp_lexer_debug_stream
= NULL
;
1289 /* Create a new cp_token_cache, representing a range of tokens. */
1291 static cp_token_cache
*
1292 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1294 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1295 cache
->first
= first
;
1300 /* Diagnose if #pragma omp declare simd isn't followed immediately
1301 by function declaration or definition. */
1304 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1306 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1308 error ("%<#pragma omp declare simd%> not immediately followed by "
1309 "function declaration or definition");
1310 parser
->omp_declare_simd
= NULL
;
1314 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1315 and put that into "omp declare simd" attribute. */
1318 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1320 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1322 if (fndecl
== error_mark_node
)
1324 parser
->omp_declare_simd
= NULL
;
1327 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1329 cp_ensure_no_omp_declare_simd (parser
);
1335 /* Decl-specifiers. */
1337 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1340 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1342 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1347 /* Nothing other than the parser should be creating declarators;
1348 declarators are a semi-syntactic representation of C++ entities.
1349 Other parts of the front end that need to create entities (like
1350 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1352 static cp_declarator
*make_call_declarator
1353 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1354 static cp_declarator
*make_array_declarator
1355 (cp_declarator
*, tree
);
1356 static cp_declarator
*make_pointer_declarator
1357 (cp_cv_quals
, cp_declarator
*, tree
);
1358 static cp_declarator
*make_reference_declarator
1359 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1360 static cp_parameter_declarator
*make_parameter_declarator
1361 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1362 static cp_declarator
*make_ptrmem_declarator
1363 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1365 /* An erroneous declarator. */
1366 static cp_declarator
*cp_error_declarator
;
1368 /* The obstack on which declarators and related data structures are
1370 static struct obstack declarator_obstack
;
1372 /* Alloc BYTES from the declarator memory pool. */
1374 static inline void *
1375 alloc_declarator (size_t bytes
)
1377 return obstack_alloc (&declarator_obstack
, bytes
);
1380 /* Allocate a declarator of the indicated KIND. Clear fields that are
1381 common to all declarators. */
1383 static cp_declarator
*
1384 make_declarator (cp_declarator_kind kind
)
1386 cp_declarator
*declarator
;
1388 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1389 declarator
->kind
= kind
;
1390 declarator
->attributes
= NULL_TREE
;
1391 declarator
->std_attributes
= NULL_TREE
;
1392 declarator
->declarator
= NULL
;
1393 declarator
->parameter_pack_p
= false;
1394 declarator
->id_loc
= UNKNOWN_LOCATION
;
1399 /* Make a declarator for a generalized identifier. If
1400 QUALIFYING_SCOPE is non-NULL, the identifier is
1401 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1402 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1405 static cp_declarator
*
1406 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1407 special_function_kind sfk
)
1409 cp_declarator
*declarator
;
1411 /* It is valid to write:
1413 class C { void f(); };
1417 The standard is not clear about whether `typedef const C D' is
1418 legal; as of 2002-09-15 the committee is considering that
1419 question. EDG 3.0 allows that syntax. Therefore, we do as
1421 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1422 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1424 gcc_assert (identifier_p (unqualified_name
)
1425 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1426 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1428 declarator
= make_declarator (cdk_id
);
1429 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1430 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1431 declarator
->u
.id
.sfk
= sfk
;
1436 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1437 of modifiers such as const or volatile to apply to the pointer
1438 type, represented as identifiers. ATTRIBUTES represent the attributes that
1439 appertain to the pointer or reference. */
1442 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1445 cp_declarator
*declarator
;
1447 declarator
= make_declarator (cdk_pointer
);
1448 declarator
->declarator
= target
;
1449 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1450 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1453 declarator
->id_loc
= target
->id_loc
;
1454 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1455 target
->parameter_pack_p
= false;
1458 declarator
->parameter_pack_p
= false;
1460 declarator
->std_attributes
= attributes
;
1465 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1466 represent the attributes that appertain to the pointer or
1470 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1471 bool rvalue_ref
, tree attributes
)
1473 cp_declarator
*declarator
;
1475 declarator
= make_declarator (cdk_reference
);
1476 declarator
->declarator
= target
;
1477 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1478 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1481 declarator
->id_loc
= target
->id_loc
;
1482 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1483 target
->parameter_pack_p
= false;
1486 declarator
->parameter_pack_p
= false;
1488 declarator
->std_attributes
= attributes
;
1493 /* Like make_pointer_declarator -- but for a pointer to a non-static
1494 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1498 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1499 cp_declarator
*pointee
,
1502 cp_declarator
*declarator
;
1504 declarator
= make_declarator (cdk_ptrmem
);
1505 declarator
->declarator
= pointee
;
1506 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1507 declarator
->u
.pointer
.class_type
= class_type
;
1511 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1512 pointee
->parameter_pack_p
= false;
1515 declarator
->parameter_pack_p
= false;
1517 declarator
->std_attributes
= attributes
;
1522 /* Make a declarator for the function given by TARGET, with the
1523 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1524 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1525 indicates what exceptions can be thrown. */
1528 make_call_declarator (cp_declarator
*target
,
1530 cp_cv_quals cv_qualifiers
,
1531 cp_virt_specifiers virt_specifiers
,
1532 cp_ref_qualifier ref_qualifier
,
1533 tree exception_specification
,
1534 tree late_return_type
)
1536 cp_declarator
*declarator
;
1538 declarator
= make_declarator (cdk_function
);
1539 declarator
->declarator
= target
;
1540 declarator
->u
.function
.parameters
= parms
;
1541 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1542 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1543 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1544 declarator
->u
.function
.exception_specification
= exception_specification
;
1545 declarator
->u
.function
.late_return_type
= late_return_type
;
1548 declarator
->id_loc
= target
->id_loc
;
1549 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1550 target
->parameter_pack_p
= false;
1553 declarator
->parameter_pack_p
= false;
1558 /* Make a declarator for an array of BOUNDS elements, each of which is
1559 defined by ELEMENT. */
1562 make_array_declarator (cp_declarator
*element
, tree bounds
)
1564 cp_declarator
*declarator
;
1566 declarator
= make_declarator (cdk_array
);
1567 declarator
->declarator
= element
;
1568 declarator
->u
.array
.bounds
= bounds
;
1571 declarator
->id_loc
= element
->id_loc
;
1572 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1573 element
->parameter_pack_p
= false;
1576 declarator
->parameter_pack_p
= false;
1581 /* Determine whether the declarator we've seen so far can be a
1582 parameter pack, when followed by an ellipsis. */
1584 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1586 /* Search for a declarator name, or any other declarator that goes
1587 after the point where the ellipsis could appear in a parameter
1588 pack. If we find any of these, then this declarator can not be
1589 made into a parameter pack. */
1591 while (declarator
&& !found
)
1593 switch ((int)declarator
->kind
)
1604 declarator
= declarator
->declarator
;
1612 cp_parameter_declarator
*no_parameters
;
1614 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1615 DECLARATOR and DEFAULT_ARGUMENT. */
1617 cp_parameter_declarator
*
1618 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1619 cp_declarator
*declarator
,
1620 tree default_argument
)
1622 cp_parameter_declarator
*parameter
;
1624 parameter
= ((cp_parameter_declarator
*)
1625 alloc_declarator (sizeof (cp_parameter_declarator
)));
1626 parameter
->next
= NULL
;
1627 if (decl_specifiers
)
1628 parameter
->decl_specifiers
= *decl_specifiers
;
1630 clear_decl_specs (¶meter
->decl_specifiers
);
1631 parameter
->declarator
= declarator
;
1632 parameter
->default_argument
= default_argument
;
1633 parameter
->ellipsis_p
= false;
1638 /* Returns true iff DECLARATOR is a declaration for a function. */
1641 function_declarator_p (const cp_declarator
*declarator
)
1645 if (declarator
->kind
== cdk_function
1646 && declarator
->declarator
->kind
== cdk_id
)
1648 if (declarator
->kind
== cdk_id
1649 || declarator
->kind
== cdk_error
)
1651 declarator
= declarator
->declarator
;
1661 A cp_parser parses the token stream as specified by the C++
1662 grammar. Its job is purely parsing, not semantic analysis. For
1663 example, the parser breaks the token stream into declarators,
1664 expressions, statements, and other similar syntactic constructs.
1665 It does not check that the types of the expressions on either side
1666 of an assignment-statement are compatible, or that a function is
1667 not declared with a parameter of type `void'.
1669 The parser invokes routines elsewhere in the compiler to perform
1670 semantic analysis and to build up the abstract syntax tree for the
1673 The parser (and the template instantiation code, which is, in a
1674 way, a close relative of parsing) are the only parts of the
1675 compiler that should be calling push_scope and pop_scope, or
1676 related functions. The parser (and template instantiation code)
1677 keeps track of what scope is presently active; everything else
1678 should simply honor that. (The code that generates static
1679 initializers may also need to set the scope, in order to check
1680 access control correctly when emitting the initializers.)
1685 The parser is of the standard recursive-descent variety. Upcoming
1686 tokens in the token stream are examined in order to determine which
1687 production to use when parsing a non-terminal. Some C++ constructs
1688 require arbitrary look ahead to disambiguate. For example, it is
1689 impossible, in the general case, to tell whether a statement is an
1690 expression or declaration without scanning the entire statement.
1691 Therefore, the parser is capable of "parsing tentatively." When the
1692 parser is not sure what construct comes next, it enters this mode.
1693 Then, while we attempt to parse the construct, the parser queues up
1694 error messages, rather than issuing them immediately, and saves the
1695 tokens it consumes. If the construct is parsed successfully, the
1696 parser "commits", i.e., it issues any queued error messages and
1697 the tokens that were being preserved are permanently discarded.
1698 If, however, the construct is not parsed successfully, the parser
1699 rolls back its state completely so that it can resume parsing using
1700 a different alternative.
1705 The performance of the parser could probably be improved substantially.
1706 We could often eliminate the need to parse tentatively by looking ahead
1707 a little bit. In some places, this approach might not entirely eliminate
1708 the need to parse tentatively, but it might still speed up the average
1711 /* Flags that are passed to some parsing functions. These values can
1712 be bitwise-ored together. */
1717 CP_PARSER_FLAGS_NONE
= 0x0,
1718 /* The construct is optional. If it is not present, then no error
1719 should be issued. */
1720 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1721 /* When parsing a type-specifier, treat user-defined type-names
1722 as non-type identifiers. */
1723 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1724 /* When parsing a type-specifier, do not try to parse a class-specifier
1725 or enum-specifier. */
1726 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1727 /* When parsing a decl-specifier-seq, only allow type-specifier or
1729 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1732 /* This type is used for parameters and variables which hold
1733 combinations of the above flags. */
1734 typedef int cp_parser_flags
;
1736 /* The different kinds of declarators we want to parse. */
1738 typedef enum cp_parser_declarator_kind
1740 /* We want an abstract declarator. */
1741 CP_PARSER_DECLARATOR_ABSTRACT
,
1742 /* We want a named declarator. */
1743 CP_PARSER_DECLARATOR_NAMED
,
1744 /* We don't mind, but the name must be an unqualified-id. */
1745 CP_PARSER_DECLARATOR_EITHER
1746 } cp_parser_declarator_kind
;
1748 /* The precedence values used to parse binary expressions. The minimum value
1749 of PREC must be 1, because zero is reserved to quickly discriminate
1750 binary operators from other tokens. */
1755 PREC_LOGICAL_OR_EXPRESSION
,
1756 PREC_LOGICAL_AND_EXPRESSION
,
1757 PREC_INCLUSIVE_OR_EXPRESSION
,
1758 PREC_EXCLUSIVE_OR_EXPRESSION
,
1759 PREC_AND_EXPRESSION
,
1760 PREC_EQUALITY_EXPRESSION
,
1761 PREC_RELATIONAL_EXPRESSION
,
1762 PREC_SHIFT_EXPRESSION
,
1763 PREC_ADDITIVE_EXPRESSION
,
1764 PREC_MULTIPLICATIVE_EXPRESSION
,
1766 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1769 /* A mapping from a token type to a corresponding tree node type, with a
1770 precedence value. */
1772 typedef struct cp_parser_binary_operations_map_node
1774 /* The token type. */
1775 enum cpp_ttype token_type
;
1776 /* The corresponding tree code. */
1777 enum tree_code tree_type
;
1778 /* The precedence of this operator. */
1779 enum cp_parser_prec prec
;
1780 } cp_parser_binary_operations_map_node
;
1782 typedef struct cp_parser_expression_stack_entry
1784 /* Left hand side of the binary operation we are currently
1787 /* Original tree code for left hand side, if it was a binary
1788 expression itself (used for -Wparentheses). */
1789 enum tree_code lhs_type
;
1790 /* Tree code for the binary operation we are parsing. */
1791 enum tree_code tree_type
;
1792 /* Precedence of the binary operation we are parsing. */
1793 enum cp_parser_prec prec
;
1794 /* Location of the binary operation we are parsing. */
1796 } cp_parser_expression_stack_entry
;
1798 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1799 entries because precedence levels on the stack are monotonically
1801 typedef struct cp_parser_expression_stack_entry
1802 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1806 /* Constructors and destructors. */
1808 static cp_parser_context
*cp_parser_context_new
1809 (cp_parser_context
*);
1811 /* Class variables. */
1813 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1815 /* The operator-precedence table used by cp_parser_binary_expression.
1816 Transformed into an associative array (binops_by_token) by
1819 static const cp_parser_binary_operations_map_node binops
[] = {
1820 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1821 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1823 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1824 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1825 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1827 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1828 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1830 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1831 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1833 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1834 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1835 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1836 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1838 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1839 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1841 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1843 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1845 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1847 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1849 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1852 /* The same as binops, but initialized by cp_parser_new so that
1853 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1855 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1857 /* Constructors and destructors. */
1859 /* Construct a new context. The context below this one on the stack
1860 is given by NEXT. */
1862 static cp_parser_context
*
1863 cp_parser_context_new (cp_parser_context
* next
)
1865 cp_parser_context
*context
;
1867 /* Allocate the storage. */
1868 if (cp_parser_context_free_list
!= NULL
)
1870 /* Pull the first entry from the free list. */
1871 context
= cp_parser_context_free_list
;
1872 cp_parser_context_free_list
= context
->next
;
1873 memset (context
, 0, sizeof (*context
));
1876 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1878 /* No errors have occurred yet in this context. */
1879 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1880 /* If this is not the bottommost context, copy information that we
1881 need from the previous context. */
1884 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1885 expression, then we are parsing one in this context, too. */
1886 context
->object_type
= next
->object_type
;
1887 /* Thread the stack. */
1888 context
->next
= next
;
1894 /* Managing the unparsed function queues. */
1896 #define unparsed_funs_with_default_args \
1897 parser->unparsed_queues->last ().funs_with_default_args
1898 #define unparsed_funs_with_definitions \
1899 parser->unparsed_queues->last ().funs_with_definitions
1900 #define unparsed_nsdmis \
1901 parser->unparsed_queues->last ().nsdmis
1902 #define unparsed_classes \
1903 parser->unparsed_queues->last ().classes
1906 push_unparsed_function_queues (cp_parser
*parser
)
1908 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1909 vec_safe_push (parser
->unparsed_queues
, e
);
1913 pop_unparsed_function_queues (cp_parser
*parser
)
1915 release_tree_vector (unparsed_funs_with_definitions
);
1916 parser
->unparsed_queues
->pop ();
1921 /* Constructors and destructors. */
1923 static cp_parser
*cp_parser_new
1926 /* Routines to parse various constructs.
1928 Those that return `tree' will return the error_mark_node (rather
1929 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1930 Sometimes, they will return an ordinary node if error-recovery was
1931 attempted, even though a parse error occurred. So, to check
1932 whether or not a parse error occurred, you should always use
1933 cp_parser_error_occurred. If the construct is optional (indicated
1934 either by an `_opt' in the name of the function that does the
1935 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1936 the construct is not present. */
1938 /* Lexical conventions [gram.lex] */
1940 static tree cp_parser_identifier
1942 static tree cp_parser_string_literal
1943 (cp_parser
*, bool, bool, bool);
1944 static tree cp_parser_userdef_char_literal
1946 static tree cp_parser_userdef_string_literal
1948 static tree cp_parser_userdef_numeric_literal
1951 /* Basic concepts [gram.basic] */
1953 static bool cp_parser_translation_unit
1956 /* Expressions [gram.expr] */
1958 static tree cp_parser_primary_expression
1959 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1960 static tree cp_parser_id_expression
1961 (cp_parser
*, bool, bool, bool *, bool, bool);
1962 static tree cp_parser_unqualified_id
1963 (cp_parser
*, bool, bool, bool, bool);
1964 static tree cp_parser_nested_name_specifier_opt
1965 (cp_parser
*, bool, bool, bool, bool);
1966 static tree cp_parser_nested_name_specifier
1967 (cp_parser
*, bool, bool, bool, bool);
1968 static tree cp_parser_qualifying_entity
1969 (cp_parser
*, bool, bool, bool, bool, bool);
1970 static tree cp_parser_postfix_expression
1971 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1972 static tree cp_parser_postfix_open_square_expression
1973 (cp_parser
*, tree
, bool, bool);
1974 static tree cp_parser_postfix_dot_deref_expression
1975 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1976 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1977 (cp_parser
*, int, bool, bool, bool *, bool = false);
1978 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1979 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1980 static void cp_parser_pseudo_destructor_name
1981 (cp_parser
*, tree
, tree
*, tree
*);
1982 static tree cp_parser_unary_expression
1983 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1984 static enum tree_code cp_parser_unary_operator
1986 static tree cp_parser_new_expression
1988 static vec
<tree
, va_gc
> *cp_parser_new_placement
1990 static tree cp_parser_new_type_id
1991 (cp_parser
*, tree
*);
1992 static cp_declarator
*cp_parser_new_declarator_opt
1994 static cp_declarator
*cp_parser_direct_new_declarator
1996 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1998 static tree cp_parser_delete_expression
2000 static tree cp_parser_cast_expression
2001 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2002 static tree cp_parser_binary_expression
2003 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2004 static tree cp_parser_question_colon_clause
2005 (cp_parser
*, tree
);
2006 static tree cp_parser_assignment_expression
2007 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2008 static enum tree_code cp_parser_assignment_operator_opt
2010 static tree cp_parser_expression
2011 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2012 static tree cp_parser_constant_expression
2013 (cp_parser
*, bool = false, bool * = NULL
);
2014 static tree cp_parser_builtin_offsetof
2016 static tree cp_parser_lambda_expression
2018 static void cp_parser_lambda_introducer
2019 (cp_parser
*, tree
);
2020 static bool cp_parser_lambda_declarator_opt
2021 (cp_parser
*, tree
);
2022 static void cp_parser_lambda_body
2023 (cp_parser
*, tree
);
2025 /* Statements [gram.stmt.stmt] */
2027 static void cp_parser_statement
2028 (cp_parser
*, tree
, bool, bool *);
2029 static void cp_parser_label_for_labeled_statement
2030 (cp_parser
*, tree
);
2031 static tree cp_parser_expression_statement
2032 (cp_parser
*, tree
);
2033 static tree cp_parser_compound_statement
2034 (cp_parser
*, tree
, bool, bool);
2035 static void cp_parser_statement_seq_opt
2036 (cp_parser
*, tree
);
2037 static tree cp_parser_selection_statement
2038 (cp_parser
*, bool *);
2039 static tree cp_parser_condition
2041 static tree cp_parser_iteration_statement
2042 (cp_parser
*, bool);
2043 static bool cp_parser_for_init_statement
2044 (cp_parser
*, tree
*decl
);
2045 static tree cp_parser_for
2046 (cp_parser
*, bool);
2047 static tree cp_parser_c_for
2048 (cp_parser
*, tree
, tree
, bool);
2049 static tree cp_parser_range_for
2050 (cp_parser
*, tree
, tree
, tree
, bool);
2051 static void do_range_for_auto_deduction
2053 static tree cp_parser_perform_range_for_lookup
2054 (tree
, tree
*, tree
*);
2055 static tree cp_parser_range_for_member_function
2057 static tree cp_parser_jump_statement
2059 static void cp_parser_declaration_statement
2062 static tree cp_parser_implicitly_scoped_statement
2063 (cp_parser
*, bool *);
2064 static void cp_parser_already_scoped_statement
2067 /* Declarations [gram.dcl.dcl] */
2069 static void cp_parser_declaration_seq_opt
2071 static void cp_parser_declaration
2073 static void cp_parser_block_declaration
2074 (cp_parser
*, bool);
2075 static void cp_parser_simple_declaration
2076 (cp_parser
*, bool, tree
*);
2077 static void cp_parser_decl_specifier_seq
2078 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2079 static tree cp_parser_storage_class_specifier_opt
2081 static tree cp_parser_function_specifier_opt
2082 (cp_parser
*, cp_decl_specifier_seq
*);
2083 static tree cp_parser_type_specifier
2084 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2086 static tree cp_parser_simple_type_specifier
2087 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2088 static tree cp_parser_type_name
2090 static tree cp_parser_nonclass_name
2091 (cp_parser
* parser
);
2092 static tree cp_parser_elaborated_type_specifier
2093 (cp_parser
*, bool, bool);
2094 static tree cp_parser_enum_specifier
2096 static void cp_parser_enumerator_list
2097 (cp_parser
*, tree
);
2098 static void cp_parser_enumerator_definition
2099 (cp_parser
*, tree
);
2100 static tree cp_parser_namespace_name
2102 static void cp_parser_namespace_definition
2104 static void cp_parser_namespace_body
2106 static tree cp_parser_qualified_namespace_specifier
2108 static void cp_parser_namespace_alias_definition
2110 static bool cp_parser_using_declaration
2111 (cp_parser
*, bool);
2112 static void cp_parser_using_directive
2114 static tree cp_parser_alias_declaration
2116 static void cp_parser_asm_definition
2118 static void cp_parser_linkage_specification
2120 static void cp_parser_static_assert
2121 (cp_parser
*, bool);
2122 static tree cp_parser_decltype
2125 /* Declarators [gram.dcl.decl] */
2127 static tree cp_parser_init_declarator
2128 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2129 static cp_declarator
*cp_parser_declarator
2130 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2131 static cp_declarator
*cp_parser_direct_declarator
2132 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2133 static enum tree_code cp_parser_ptr_operator
2134 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2135 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2137 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2139 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2141 static tree cp_parser_late_return_type_opt
2142 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2143 static tree cp_parser_declarator_id
2144 (cp_parser
*, bool);
2145 static tree cp_parser_type_id
2147 static tree cp_parser_template_type_arg
2149 static tree
cp_parser_trailing_type_id (cp_parser
*);
2150 static tree cp_parser_type_id_1
2151 (cp_parser
*, bool, bool);
2152 static void cp_parser_type_specifier_seq
2153 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2154 static tree cp_parser_parameter_declaration_clause
2156 static tree cp_parser_parameter_declaration_list
2157 (cp_parser
*, bool *);
2158 static cp_parameter_declarator
*cp_parser_parameter_declaration
2159 (cp_parser
*, bool, bool *);
2160 static tree cp_parser_default_argument
2161 (cp_parser
*, bool);
2162 static void cp_parser_function_body
2163 (cp_parser
*, bool);
2164 static tree cp_parser_initializer
2165 (cp_parser
*, bool *, bool *);
2166 static tree cp_parser_initializer_clause
2167 (cp_parser
*, bool *);
2168 static tree cp_parser_braced_list
2169 (cp_parser
*, bool*);
2170 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2171 (cp_parser
*, bool *);
2173 static bool cp_parser_ctor_initializer_opt_and_function_body
2174 (cp_parser
*, bool);
2176 static tree cp_parser_late_parsing_omp_declare_simd
2177 (cp_parser
*, tree
);
2179 static tree cp_parser_late_parsing_cilk_simd_fn_info
2180 (cp_parser
*, tree
);
2182 static tree synthesize_implicit_template_parm
2184 static tree finish_fully_implicit_template
2185 (cp_parser
*, tree
);
2187 /* Classes [gram.class] */
2189 static tree cp_parser_class_name
2190 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2191 static tree cp_parser_class_specifier
2193 static tree cp_parser_class_head
2194 (cp_parser
*, bool *);
2195 static enum tag_types cp_parser_class_key
2197 static void cp_parser_type_parameter_key
2198 (cp_parser
* parser
);
2199 static void cp_parser_member_specification_opt
2201 static void cp_parser_member_declaration
2203 static tree cp_parser_pure_specifier
2205 static tree cp_parser_constant_initializer
2208 /* Derived classes [gram.class.derived] */
2210 static tree cp_parser_base_clause
2212 static tree cp_parser_base_specifier
2215 /* Special member functions [gram.special] */
2217 static tree cp_parser_conversion_function_id
2219 static tree cp_parser_conversion_type_id
2221 static cp_declarator
*cp_parser_conversion_declarator_opt
2223 static bool cp_parser_ctor_initializer_opt
2225 static void cp_parser_mem_initializer_list
2227 static tree cp_parser_mem_initializer
2229 static tree cp_parser_mem_initializer_id
2232 /* Overloading [gram.over] */
2234 static tree cp_parser_operator_function_id
2236 static tree cp_parser_operator
2239 /* Templates [gram.temp] */
2241 static void cp_parser_template_declaration
2242 (cp_parser
*, bool);
2243 static tree cp_parser_template_parameter_list
2245 static tree cp_parser_template_parameter
2246 (cp_parser
*, bool *, bool *);
2247 static tree cp_parser_type_parameter
2248 (cp_parser
*, bool *);
2249 static tree cp_parser_template_id
2250 (cp_parser
*, bool, bool, enum tag_types
, bool);
2251 static tree cp_parser_template_name
2252 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2253 static tree cp_parser_template_argument_list
2255 static tree cp_parser_template_argument
2257 static void cp_parser_explicit_instantiation
2259 static void cp_parser_explicit_specialization
2262 /* Exception handling [gram.exception] */
2264 static tree cp_parser_try_block
2266 static bool cp_parser_function_try_block
2268 static void cp_parser_handler_seq
2270 static void cp_parser_handler
2272 static tree cp_parser_exception_declaration
2274 static tree cp_parser_throw_expression
2276 static tree cp_parser_exception_specification_opt
2278 static tree cp_parser_type_id_list
2281 /* GNU Extensions */
2283 static tree cp_parser_asm_specification_opt
2285 static tree cp_parser_asm_operand_list
2287 static tree cp_parser_asm_clobber_list
2289 static tree cp_parser_asm_label_list
2291 static bool cp_next_tokens_can_be_attribute_p
2293 static bool cp_next_tokens_can_be_gnu_attribute_p
2295 static bool cp_next_tokens_can_be_std_attribute_p
2297 static bool cp_nth_tokens_can_be_std_attribute_p
2298 (cp_parser
*, size_t);
2299 static bool cp_nth_tokens_can_be_gnu_attribute_p
2300 (cp_parser
*, size_t);
2301 static bool cp_nth_tokens_can_be_attribute_p
2302 (cp_parser
*, size_t);
2303 static tree cp_parser_attributes_opt
2305 static tree cp_parser_gnu_attributes_opt
2307 static tree cp_parser_gnu_attribute_list
2309 static tree cp_parser_std_attribute
2311 static tree cp_parser_std_attribute_spec
2313 static tree cp_parser_std_attribute_spec_seq
2315 static bool cp_parser_extension_opt
2316 (cp_parser
*, int *);
2317 static void cp_parser_label_declaration
2320 /* Transactional Memory Extensions */
2322 static tree cp_parser_transaction
2323 (cp_parser
*, enum rid
);
2324 static tree cp_parser_transaction_expression
2325 (cp_parser
*, enum rid
);
2326 static bool cp_parser_function_transaction
2327 (cp_parser
*, enum rid
);
2328 static tree cp_parser_transaction_cancel
2331 enum pragma_context
{
2338 static bool cp_parser_pragma
2339 (cp_parser
*, enum pragma_context
);
2341 /* Objective-C++ Productions */
2343 static tree cp_parser_objc_message_receiver
2345 static tree cp_parser_objc_message_args
2347 static tree cp_parser_objc_message_expression
2349 static tree cp_parser_objc_encode_expression
2351 static tree cp_parser_objc_defs_expression
2353 static tree cp_parser_objc_protocol_expression
2355 static tree cp_parser_objc_selector_expression
2357 static tree cp_parser_objc_expression
2359 static bool cp_parser_objc_selector_p
2361 static tree cp_parser_objc_selector
2363 static tree cp_parser_objc_protocol_refs_opt
2365 static void cp_parser_objc_declaration
2366 (cp_parser
*, tree
);
2367 static tree cp_parser_objc_statement
2369 static bool cp_parser_objc_valid_prefix_attributes
2370 (cp_parser
*, tree
*);
2371 static void cp_parser_objc_at_property_declaration
2373 static void cp_parser_objc_at_synthesize_declaration
2375 static void cp_parser_objc_at_dynamic_declaration
2377 static tree cp_parser_objc_struct_declaration
2380 /* Utility Routines */
2382 static tree cp_parser_lookup_name
2383 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2384 static tree cp_parser_lookup_name_simple
2385 (cp_parser
*, tree
, location_t
);
2386 static tree cp_parser_maybe_treat_template_as_class
2388 static bool cp_parser_check_declarator_template_parameters
2389 (cp_parser
*, cp_declarator
*, location_t
);
2390 static bool cp_parser_check_template_parameters
2391 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2392 static tree cp_parser_simple_cast_expression
2394 static tree cp_parser_global_scope_opt
2395 (cp_parser
*, bool);
2396 static bool cp_parser_constructor_declarator_p
2397 (cp_parser
*, bool);
2398 static tree cp_parser_function_definition_from_specifiers_and_declarator
2399 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2400 static tree cp_parser_function_definition_after_declarator
2401 (cp_parser
*, bool);
2402 static void cp_parser_template_declaration_after_export
2403 (cp_parser
*, bool);
2404 static void cp_parser_perform_template_parameter_access_checks
2405 (vec
<deferred_access_check
, va_gc
> *);
2406 static tree cp_parser_single_declaration
2407 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2408 static tree cp_parser_functional_cast
2409 (cp_parser
*, tree
);
2410 static tree cp_parser_save_member_function_body
2411 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2412 static tree cp_parser_save_nsdmi
2414 static tree cp_parser_enclosed_template_argument_list
2416 static void cp_parser_save_default_args
2417 (cp_parser
*, tree
);
2418 static void cp_parser_late_parsing_for_member
2419 (cp_parser
*, tree
);
2420 static tree cp_parser_late_parse_one_default_arg
2421 (cp_parser
*, tree
, tree
, tree
);
2422 static void cp_parser_late_parsing_nsdmi
2423 (cp_parser
*, tree
);
2424 static void cp_parser_late_parsing_default_args
2425 (cp_parser
*, tree
);
2426 static tree cp_parser_sizeof_operand
2427 (cp_parser
*, enum rid
);
2428 static tree cp_parser_trait_expr
2429 (cp_parser
*, enum rid
);
2430 static bool cp_parser_declares_only_class_p
2432 static void cp_parser_set_storage_class
2433 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2434 static void cp_parser_set_decl_spec_type
2435 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2436 static void set_and_check_decl_spec_loc
2437 (cp_decl_specifier_seq
*decl_specs
,
2438 cp_decl_spec ds
, cp_token
*);
2439 static bool cp_parser_friend_p
2440 (const cp_decl_specifier_seq
*);
2441 static void cp_parser_required_error
2442 (cp_parser
*, required_token
, bool);
2443 static cp_token
*cp_parser_require
2444 (cp_parser
*, enum cpp_ttype
, required_token
);
2445 static cp_token
*cp_parser_require_keyword
2446 (cp_parser
*, enum rid
, required_token
);
2447 static bool cp_parser_token_starts_function_definition_p
2449 static bool cp_parser_next_token_starts_class_definition_p
2451 static bool cp_parser_next_token_ends_template_argument_p
2453 static bool cp_parser_nth_token_starts_template_argument_list_p
2454 (cp_parser
*, size_t);
2455 static enum tag_types cp_parser_token_is_class_key
2457 static enum tag_types cp_parser_token_is_type_parameter_key
2459 static void cp_parser_check_class_key
2460 (enum tag_types
, tree type
);
2461 static void cp_parser_check_access_in_redeclaration
2462 (tree type
, location_t location
);
2463 static bool cp_parser_optional_template_keyword
2465 static void cp_parser_pre_parsed_nested_name_specifier
2467 static bool cp_parser_cache_group
2468 (cp_parser
*, enum cpp_ttype
, unsigned);
2469 static tree cp_parser_cache_defarg
2470 (cp_parser
*parser
, bool nsdmi
);
2471 static void cp_parser_parse_tentatively
2473 static void cp_parser_commit_to_tentative_parse
2475 static void cp_parser_commit_to_topmost_tentative_parse
2477 static void cp_parser_abort_tentative_parse
2479 static bool cp_parser_parse_definitely
2481 static inline bool cp_parser_parsing_tentatively
2483 static bool cp_parser_uncommitted_to_tentative_parse_p
2485 static void cp_parser_error
2486 (cp_parser
*, const char *);
2487 static void cp_parser_name_lookup_error
2488 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2489 static bool cp_parser_simulate_error
2491 static bool cp_parser_check_type_definition
2493 static void cp_parser_check_for_definition_in_return_type
2494 (cp_declarator
*, tree
, location_t type_location
);
2495 static void cp_parser_check_for_invalid_template_id
2496 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2497 static bool cp_parser_non_integral_constant_expression
2498 (cp_parser
*, non_integral_constant
);
2499 static void cp_parser_diagnose_invalid_type_name
2500 (cp_parser
*, tree
, location_t
);
2501 static bool cp_parser_parse_and_diagnose_invalid_type_name
2503 static int cp_parser_skip_to_closing_parenthesis
2504 (cp_parser
*, bool, bool, bool);
2505 static void cp_parser_skip_to_end_of_statement
2507 static void cp_parser_consume_semicolon_at_end_of_statement
2509 static void cp_parser_skip_to_end_of_block_or_statement
2511 static bool cp_parser_skip_to_closing_brace
2513 static void cp_parser_skip_to_end_of_template_parameter_list
2515 static void cp_parser_skip_to_pragma_eol
2516 (cp_parser
*, cp_token
*);
2517 static bool cp_parser_error_occurred
2519 static bool cp_parser_allow_gnu_extensions_p
2521 static bool cp_parser_is_pure_string_literal
2523 static bool cp_parser_is_string_literal
2525 static bool cp_parser_is_keyword
2526 (cp_token
*, enum rid
);
2527 static tree cp_parser_make_typename_type
2528 (cp_parser
*, tree
, location_t location
);
2529 static cp_declarator
* cp_parser_make_indirect_declarator
2530 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2531 static bool cp_parser_compound_literal_p
2533 static bool cp_parser_array_designator_p
2535 static bool cp_parser_skip_to_closing_square_bracket
2538 /* Returns nonzero if we are parsing tentatively. */
2541 cp_parser_parsing_tentatively (cp_parser
* parser
)
2543 return parser
->context
->next
!= NULL
;
2546 /* Returns nonzero if TOKEN is a string literal. */
2549 cp_parser_is_pure_string_literal (cp_token
* token
)
2551 return (token
->type
== CPP_STRING
||
2552 token
->type
== CPP_STRING16
||
2553 token
->type
== CPP_STRING32
||
2554 token
->type
== CPP_WSTRING
||
2555 token
->type
== CPP_UTF8STRING
);
2558 /* Returns nonzero if TOKEN is a string literal
2559 of a user-defined string literal. */
2562 cp_parser_is_string_literal (cp_token
* token
)
2564 return (cp_parser_is_pure_string_literal (token
) ||
2565 token
->type
== CPP_STRING_USERDEF
||
2566 token
->type
== CPP_STRING16_USERDEF
||
2567 token
->type
== CPP_STRING32_USERDEF
||
2568 token
->type
== CPP_WSTRING_USERDEF
||
2569 token
->type
== CPP_UTF8STRING_USERDEF
);
2572 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2575 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2577 return token
->keyword
== keyword
;
2580 /* If not parsing tentatively, issue a diagnostic of the form
2581 FILE:LINE: MESSAGE before TOKEN
2582 where TOKEN is the next token in the input stream. MESSAGE
2583 (specified by the caller) is usually of the form "expected
2587 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2589 if (!cp_parser_simulate_error (parser
))
2591 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2592 /* This diagnostic makes more sense if it is tagged to the line
2593 of the token we just peeked at. */
2594 cp_lexer_set_source_position_from_token (token
);
2596 if (token
->type
== CPP_PRAGMA
)
2598 error_at (token
->location
,
2599 "%<#pragma%> is not allowed here");
2600 cp_parser_skip_to_pragma_eol (parser
, token
);
2604 c_parse_error (gmsgid
,
2605 /* Because c_parser_error does not understand
2606 CPP_KEYWORD, keywords are treated like
2608 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2609 token
->u
.value
, token
->flags
);
2613 /* Issue an error about name-lookup failing. NAME is the
2614 IDENTIFIER_NODE DECL is the result of
2615 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2616 the thing that we hoped to find. */
2619 cp_parser_name_lookup_error (cp_parser
* parser
,
2622 name_lookup_error desired
,
2623 location_t location
)
2625 /* If name lookup completely failed, tell the user that NAME was not
2627 if (decl
== error_mark_node
)
2629 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2630 error_at (location
, "%<%E::%E%> has not been declared",
2631 parser
->scope
, name
);
2632 else if (parser
->scope
== global_namespace
)
2633 error_at (location
, "%<::%E%> has not been declared", name
);
2634 else if (parser
->object_scope
2635 && !CLASS_TYPE_P (parser
->object_scope
))
2636 error_at (location
, "request for member %qE in non-class type %qT",
2637 name
, parser
->object_scope
);
2638 else if (parser
->object_scope
)
2639 error_at (location
, "%<%T::%E%> has not been declared",
2640 parser
->object_scope
, name
);
2642 error_at (location
, "%qE has not been declared", name
);
2644 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2649 error_at (location
, "%<%E::%E%> is not a type",
2650 parser
->scope
, name
);
2653 error_at (location
, "%<%E::%E%> is not a class or namespace",
2654 parser
->scope
, name
);
2658 "%<%E::%E%> is not a class, namespace, or enumeration",
2659 parser
->scope
, name
);
2666 else if (parser
->scope
== global_namespace
)
2671 error_at (location
, "%<::%E%> is not a type", name
);
2674 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2678 "%<::%E%> is not a class, namespace, or enumeration",
2690 error_at (location
, "%qE is not a type", name
);
2693 error_at (location
, "%qE is not a class or namespace", name
);
2697 "%qE is not a class, namespace, or enumeration", name
);
2705 /* If we are parsing tentatively, remember that an error has occurred
2706 during this tentative parse. Returns true if the error was
2707 simulated; false if a message should be issued by the caller. */
2710 cp_parser_simulate_error (cp_parser
* parser
)
2712 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2714 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2720 /* This function is called when a type is defined. If type
2721 definitions are forbidden at this point, an error message is
2725 cp_parser_check_type_definition (cp_parser
* parser
)
2727 /* If types are forbidden here, issue a message. */
2728 if (parser
->type_definition_forbidden_message
)
2730 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2731 in the message need to be interpreted. */
2732 error (parser
->type_definition_forbidden_message
);
2738 /* This function is called when the DECLARATOR is processed. The TYPE
2739 was a type defined in the decl-specifiers. If it is invalid to
2740 define a type in the decl-specifiers for DECLARATOR, an error is
2741 issued. TYPE_LOCATION is the location of TYPE and is used
2742 for error reporting. */
2745 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2746 tree type
, location_t type_location
)
2748 /* [dcl.fct] forbids type definitions in return types.
2749 Unfortunately, it's not easy to know whether or not we are
2750 processing a return type until after the fact. */
2752 && (declarator
->kind
== cdk_pointer
2753 || declarator
->kind
== cdk_reference
2754 || declarator
->kind
== cdk_ptrmem
))
2755 declarator
= declarator
->declarator
;
2757 && declarator
->kind
== cdk_function
)
2759 error_at (type_location
,
2760 "new types may not be defined in a return type");
2761 inform (type_location
,
2762 "(perhaps a semicolon is missing after the definition of %qT)",
2767 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2768 "<" in any valid C++ program. If the next token is indeed "<",
2769 issue a message warning the user about what appears to be an
2770 invalid attempt to form a template-id. LOCATION is the location
2771 of the type-specifier (TYPE) */
2774 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2776 enum tag_types tag_type
,
2777 location_t location
)
2779 cp_token_position start
= 0;
2781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2784 error_at (location
, "%qT is not a template", type
);
2785 else if (identifier_p (type
))
2787 if (tag_type
!= none_type
)
2788 error_at (location
, "%qE is not a class template", type
);
2790 error_at (location
, "%qE is not a template", type
);
2793 error_at (location
, "invalid template-id");
2794 /* Remember the location of the invalid "<". */
2795 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2796 start
= cp_lexer_token_position (parser
->lexer
, true);
2797 /* Consume the "<". */
2798 cp_lexer_consume_token (parser
->lexer
);
2799 /* Parse the template arguments. */
2800 cp_parser_enclosed_template_argument_list (parser
);
2801 /* Permanently remove the invalid template arguments so that
2802 this error message is not issued again. */
2804 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2808 /* If parsing an integral constant-expression, issue an error message
2809 about the fact that THING appeared and return true. Otherwise,
2810 return false. In either case, set
2811 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2814 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2815 non_integral_constant thing
)
2817 parser
->non_integral_constant_expression_p
= true;
2818 if (parser
->integral_constant_expression_p
)
2820 if (!parser
->allow_non_integral_constant_expression_p
)
2822 const char *msg
= NULL
;
2826 error ("floating-point literal "
2827 "cannot appear in a constant-expression");
2830 error ("a cast to a type other than an integral or "
2831 "enumeration type cannot appear in a "
2832 "constant-expression");
2835 error ("%<typeid%> operator "
2836 "cannot appear in a constant-expression");
2839 error ("non-constant compound literals "
2840 "cannot appear in a constant-expression");
2843 error ("a function call "
2844 "cannot appear in a constant-expression");
2847 error ("an increment "
2848 "cannot appear in a constant-expression");
2851 error ("an decrement "
2852 "cannot appear in a constant-expression");
2855 error ("an array reference "
2856 "cannot appear in a constant-expression");
2858 case NIC_ADDR_LABEL
:
2859 error ("the address of a label "
2860 "cannot appear in a constant-expression");
2862 case NIC_OVERLOADED
:
2863 error ("calls to overloaded operators "
2864 "cannot appear in a constant-expression");
2866 case NIC_ASSIGNMENT
:
2867 error ("an assignment cannot appear in a constant-expression");
2870 error ("a comma operator "
2871 "cannot appear in a constant-expression");
2873 case NIC_CONSTRUCTOR
:
2874 error ("a call to a constructor "
2875 "cannot appear in a constant-expression");
2877 case NIC_TRANSACTION
:
2878 error ("a transaction expression "
2879 "cannot appear in a constant-expression");
2885 msg
= "__FUNCTION__";
2887 case NIC_PRETTY_FUNC
:
2888 msg
= "__PRETTY_FUNCTION__";
2908 case NIC_PREINCREMENT
:
2911 case NIC_PREDECREMENT
:
2924 error ("%qs cannot appear in a constant-expression", msg
);
2931 /* Emit a diagnostic for an invalid type name. This function commits
2932 to the current active tentative parse, if any. (Otherwise, the
2933 problematic construct might be encountered again later, resulting
2934 in duplicate error messages.) LOCATION is the location of ID. */
2937 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2938 location_t location
)
2940 tree decl
, ambiguous_decls
;
2941 cp_parser_commit_to_tentative_parse (parser
);
2942 /* Try to lookup the identifier. */
2943 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2944 /*is_template=*/false,
2945 /*is_namespace=*/false,
2946 /*check_dependency=*/true,
2947 &ambiguous_decls
, location
);
2948 if (ambiguous_decls
)
2949 /* If the lookup was ambiguous, an error will already have
2952 /* If the lookup found a template-name, it means that the user forgot
2953 to specify an argument list. Emit a useful error message. */
2954 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2956 "invalid use of template-name %qE without an argument list",
2958 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2959 error_at (location
, "invalid use of destructor %qD as a type", id
);
2960 else if (TREE_CODE (decl
) == TYPE_DECL
)
2961 /* Something like 'unsigned A a;' */
2962 error_at (location
, "invalid combination of multiple type-specifiers");
2963 else if (!parser
->scope
)
2965 /* Issue an error message. */
2966 error_at (location
, "%qE does not name a type", id
);
2967 /* If we're in a template class, it's possible that the user was
2968 referring to a type from a base class. For example:
2970 template <typename T> struct A { typedef T X; };
2971 template <typename T> struct B : public A<T> { X x; };
2973 The user should have said "typename A<T>::X". */
2974 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2975 inform (location
, "C++11 %<constexpr%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2978 inform (location
, "C++11 %<noexcept%> only available with "
2979 "-std=c++11 or -std=gnu++11");
2980 else if (cxx_dialect
< cxx11
2981 && TREE_CODE (id
) == IDENTIFIER_NODE
2982 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2983 inform (location
, "C++11 %<thread_local%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (processing_template_decl
&& current_class_type
2986 && TYPE_BINFO (current_class_type
))
2990 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2994 tree base_type
= BINFO_TYPE (b
);
2995 if (CLASS_TYPE_P (base_type
)
2996 && dependent_type_p (base_type
))
2999 /* Go from a particular instantiation of the
3000 template (which will have an empty TYPE_FIELDs),
3001 to the main version. */
3002 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3003 for (field
= TYPE_FIELDS (base_type
);
3005 field
= DECL_CHAIN (field
))
3006 if (TREE_CODE (field
) == TYPE_DECL
3007 && DECL_NAME (field
) == id
)
3010 "(perhaps %<typename %T::%E%> was intended)",
3011 BINFO_TYPE (b
), id
);
3020 /* Here we diagnose qualified-ids where the scope is actually correct,
3021 but the identifier does not resolve to a valid type name. */
3022 else if (parser
->scope
!= error_mark_node
)
3024 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3027 error_at (location_of (id
),
3028 "%qE in namespace %qE does not name a template type",
3031 error_at (location_of (id
),
3032 "%qE in namespace %qE does not name a type",
3035 else if (CLASS_TYPE_P (parser
->scope
)
3036 && constructor_name_p (id
, parser
->scope
))
3039 error_at (location
, "%<%T::%E%> names the constructor, not"
3040 " the type", parser
->scope
, id
);
3041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3042 error_at (location
, "and %qT has no template constructors",
3045 else if (TYPE_P (parser
->scope
)
3046 && dependent_scope_p (parser
->scope
))
3047 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3048 "%qT is a dependent scope",
3049 parser
->scope
, id
, parser
->scope
);
3050 else if (TYPE_P (parser
->scope
))
3052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3053 error_at (location_of (id
),
3054 "%qE in %q#T does not name a template type",
3057 error_at (location_of (id
),
3058 "%qE in %q#T does not name a type",
3066 /* Check for a common situation where a type-name should be present,
3067 but is not, and issue a sensible error message. Returns true if an
3068 invalid type-name was detected.
3070 The situation handled by this function are variable declarations of the
3071 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3072 Usually, `ID' should name a type, but if we got here it means that it
3073 does not. We try to emit the best possible error message depending on
3074 how exactly the id-expression looks like. */
3077 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3080 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3082 /* Avoid duplicate error about ambiguous lookup. */
3083 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3085 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3086 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3090 cp_parser_parse_tentatively (parser
);
3091 id
= cp_parser_id_expression (parser
,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3094 /*template_p=*/NULL
,
3095 /*declarator_p=*/true,
3096 /*optional_p=*/false);
3097 /* If the next token is a (, this is a function with no explicit return
3098 type, i.e. constructor, destructor or conversion op. */
3099 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3100 || TREE_CODE (id
) == TYPE_DECL
)
3102 cp_parser_abort_tentative_parse (parser
);
3105 if (!cp_parser_parse_definitely (parser
))
3108 /* Emit a diagnostic for the invalid type. */
3109 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3111 /* If we aren't in the middle of a declarator (i.e. in a
3112 parameter-declaration-clause), skip to the end of the declaration;
3113 there's no point in trying to process it. */
3114 if (!parser
->in_declarator_p
)
3115 cp_parser_skip_to_end_of_block_or_statement (parser
);
3119 /* Consume tokens up to, and including, the next non-nested closing `)'.
3120 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3121 are doing error recovery. Returns -1 if OR_COMMA is true and we
3122 found an unnested comma. */
3125 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3130 unsigned paren_depth
= 0;
3131 unsigned brace_depth
= 0;
3132 unsigned square_depth
= 0;
3134 if (recovering
&& !or_comma
3135 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3140 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3142 switch (token
->type
)
3145 case CPP_PRAGMA_EOL
:
3146 /* If we've run out of tokens, then there is no closing `)'. */
3149 /* This is good for lambda expression capture-lists. */
3150 case CPP_OPEN_SQUARE
:
3153 case CPP_CLOSE_SQUARE
:
3154 if (!square_depth
--)
3159 /* This matches the processing in skip_to_end_of_statement. */
3164 case CPP_OPEN_BRACE
:
3167 case CPP_CLOSE_BRACE
:
3173 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3178 case CPP_OPEN_PAREN
:
3183 case CPP_CLOSE_PAREN
:
3184 if (!brace_depth
&& !paren_depth
--)
3187 cp_lexer_consume_token (parser
->lexer
);
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser
->lexer
);
3201 /* Consume tokens until we reach the end of the current statement.
3202 Normally, that will be just before consuming a `;'. However, if a
3203 non-nested `}' comes first, then we stop before consuming that. */
3206 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3208 unsigned nesting_depth
= 0;
3210 /* Unwind generic function template scope if necessary. */
3211 if (parser
->fully_implicit_function_template_p
)
3212 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3216 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3218 switch (token
->type
)
3221 case CPP_PRAGMA_EOL
:
3222 /* If we've run out of tokens, stop. */
3226 /* If the next token is a `;', we have reached the end of the
3232 case CPP_CLOSE_BRACE
:
3233 /* If this is a non-nested '}', stop before consuming it.
3234 That way, when confronted with something like:
3238 we stop before consuming the closing '}', even though we
3239 have not yet reached a `;'. */
3240 if (nesting_depth
== 0)
3243 /* If it is the closing '}' for a block that we have
3244 scanned, stop -- but only after consuming the token.
3250 we will stop after the body of the erroneously declared
3251 function, but before consuming the following `typedef'
3253 if (--nesting_depth
== 0)
3255 cp_lexer_consume_token (parser
->lexer
);
3259 case CPP_OPEN_BRACE
:
3267 /* Consume the token. */
3268 cp_lexer_consume_token (parser
->lexer
);
3272 /* This function is called at the end of a statement or declaration.
3273 If the next token is a semicolon, it is consumed; otherwise, error
3274 recovery is attempted. */
3277 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3279 /* Look for the trailing `;'. */
3280 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3282 /* If there is additional (erroneous) input, skip to the end of
3284 cp_parser_skip_to_end_of_statement (parser
);
3285 /* If the next token is now a `;', consume it. */
3286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3287 cp_lexer_consume_token (parser
->lexer
);
3291 /* Skip tokens until we have consumed an entire block, or until we
3292 have consumed a non-nested `;'. */
3295 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3297 int 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);
3303 while (nesting_depth
>= 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 /* Stop if this is an unnested ';'. */
3320 case CPP_CLOSE_BRACE
:
3321 /* Stop if this is an unnested '}', or closes the outermost
3324 if (nesting_depth
< 0)
3330 case CPP_OPEN_BRACE
:
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser
->lexer
);
3344 /* Skip tokens until a non-nested closing curly brace is the next
3345 token, or there are no more tokens. Return true in the first case,
3349 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3351 unsigned nesting_depth
= 0;
3355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3357 switch (token
->type
)
3360 case CPP_PRAGMA_EOL
:
3361 /* If we've run out of tokens, stop. */
3364 case CPP_CLOSE_BRACE
:
3365 /* If the next token is a non-nested `}', then we have reached
3366 the end of the current block. */
3367 if (nesting_depth
-- == 0)
3371 case CPP_OPEN_BRACE
:
3372 /* If it the next token is a `{', then we are entering a new
3373 block. Consume the entire block. */
3381 /* Consume the token. */
3382 cp_lexer_consume_token (parser
->lexer
);
3386 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3387 parameter is the PRAGMA token, allowing us to purge the entire pragma
3391 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3395 parser
->lexer
->in_pragma
= false;
3398 token
= cp_lexer_consume_token (parser
->lexer
);
3399 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3401 /* Ensure that the pragma is not parsed again. */
3402 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3405 /* Require pragma end of line, resyncing with it as necessary. The
3406 arguments are as for cp_parser_skip_to_pragma_eol. */
3409 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3411 parser
->lexer
->in_pragma
= false;
3412 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3413 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3416 /* This is a simple wrapper around make_typename_type. When the id is
3417 an unresolved identifier node, we can provide a superior diagnostic
3418 using cp_parser_diagnose_invalid_type_name. */
3421 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3422 location_t id_location
)
3425 if (identifier_p (id
))
3427 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3428 /*complain=*/tf_none
);
3429 if (result
== error_mark_node
)
3430 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3433 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3436 /* This is a wrapper around the
3437 make_{pointer,ptrmem,reference}_declarator functions that decides
3438 which one to call based on the CODE and CLASS_TYPE arguments. The
3439 CODE argument should be one of the values returned by
3440 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3441 appertain to the pointer or reference. */
3443 static cp_declarator
*
3444 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3445 cp_cv_quals cv_qualifiers
,
3446 cp_declarator
*target
,
3449 if (code
== ERROR_MARK
)
3450 return cp_error_declarator
;
3452 if (code
== INDIRECT_REF
)
3453 if (class_type
== NULL_TREE
)
3454 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3456 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3457 target
, attributes
);
3458 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3459 return make_reference_declarator (cv_qualifiers
, target
,
3461 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3462 return make_reference_declarator (cv_qualifiers
, target
,
3467 /* Create a new C++ parser. */
3470 cp_parser_new (void)
3476 /* cp_lexer_new_main is called before doing GC allocation because
3477 cp_lexer_new_main might load a PCH file. */
3478 lexer
= cp_lexer_new_main ();
3480 /* Initialize the binops_by_token so that we can get the tree
3481 directly from the token. */
3482 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3483 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3485 parser
= ggc_cleared_alloc
<cp_parser
> ();
3486 parser
->lexer
= lexer
;
3487 parser
->context
= cp_parser_context_new (NULL
);
3489 /* For now, we always accept GNU extensions. */
3490 parser
->allow_gnu_extensions_p
= 1;
3492 /* The `>' token is a greater-than operator, not the end of a
3494 parser
->greater_than_is_operator_p
= true;
3496 parser
->default_arg_ok_p
= true;
3498 /* We are not parsing a constant-expression. */
3499 parser
->integral_constant_expression_p
= false;
3500 parser
->allow_non_integral_constant_expression_p
= false;
3501 parser
->non_integral_constant_expression_p
= false;
3503 /* Local variable names are not forbidden. */
3504 parser
->local_variables_forbidden_p
= false;
3506 /* We are not processing an `extern "C"' declaration. */
3507 parser
->in_unbraced_linkage_specification_p
= false;
3509 /* We are not processing a declarator. */
3510 parser
->in_declarator_p
= false;
3512 /* We are not processing a template-argument-list. */
3513 parser
->in_template_argument_list_p
= false;
3515 /* We are not in an iteration statement. */
3516 parser
->in_statement
= 0;
3518 /* We are not in a switch statement. */
3519 parser
->in_switch_statement_p
= false;
3521 /* We are not parsing a type-id inside an expression. */
3522 parser
->in_type_id_in_expr_p
= false;
3524 /* Declarations aren't implicitly extern "C". */
3525 parser
->implicit_extern_c
= false;
3527 /* String literals should be translated to the execution character set. */
3528 parser
->translate_strings_p
= true;
3530 /* We are not parsing a function body. */
3531 parser
->in_function_body
= false;
3533 /* We can correct until told otherwise. */
3534 parser
->colon_corrects_to_scope_p
= true;
3536 /* The unparsed function queue is empty. */
3537 push_unparsed_function_queues (parser
);
3539 /* There are no classes being defined. */
3540 parser
->num_classes_being_defined
= 0;
3542 /* No template parameters apply. */
3543 parser
->num_template_parameter_lists
= 0;
3545 /* Not declaring an implicit function template. */
3546 parser
->auto_is_implicit_function_template_parm_p
= false;
3547 parser
->fully_implicit_function_template_p
= false;
3548 parser
->implicit_template_parms
= 0;
3549 parser
->implicit_template_scope
= 0;
3554 /* Create a cp_lexer structure which will emit the tokens in CACHE
3555 and push it onto the parser's lexer stack. This is used for delayed
3556 parsing of in-class method bodies and default arguments, and should
3557 not be confused with tentative parsing. */
3559 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3561 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3562 lexer
->next
= parser
->lexer
;
3563 parser
->lexer
= lexer
;
3565 /* Move the current source position to that of the first token in the
3567 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3570 /* Pop the top lexer off the parser stack. This is never used for the
3571 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3573 cp_parser_pop_lexer (cp_parser
*parser
)
3575 cp_lexer
*lexer
= parser
->lexer
;
3576 parser
->lexer
= lexer
->next
;
3577 cp_lexer_destroy (lexer
);
3579 /* Put the current source position back where it was before this
3580 lexer was pushed. */
3581 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3584 /* Lexical conventions [gram.lex] */
3586 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3590 cp_parser_identifier (cp_parser
* parser
)
3594 /* Look for the identifier. */
3595 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3596 /* Return the value. */
3597 return token
? token
->u
.value
: error_mark_node
;
3600 /* Parse a sequence of adjacent string constants. Returns a
3601 TREE_STRING representing the combined, nul-terminated string
3602 constant. If TRANSLATE is true, translate the string to the
3603 execution character set. If WIDE_OK is true, a wide string is
3606 C++98 [lex.string] says that if a narrow string literal token is
3607 adjacent to a wide string literal token, the behavior is undefined.
3608 However, C99 6.4.5p4 says that this results in a wide string literal.
3609 We follow C99 here, for consistency with the C front end.
3611 This code is largely lifted from lex_string() in c-lex.c.
3613 FUTURE: ObjC++ will need to handle @-strings here. */
3615 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3616 bool lookup_udlit
= true)
3620 struct obstack str_ob
;
3621 cpp_string str
, istr
, *strs
;
3623 enum cpp_ttype type
, curr_type
;
3624 int have_suffix_p
= 0;
3626 tree suffix_id
= NULL_TREE
;
3627 bool curr_tok_is_userdef_p
= false;
3629 tok
= cp_lexer_peek_token (parser
->lexer
);
3630 if (!cp_parser_is_string_literal (tok
))
3632 cp_parser_error (parser
, "expected string-literal");
3633 return error_mark_node
;
3636 if (cpp_userdef_string_p (tok
->type
))
3638 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3639 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3640 curr_tok_is_userdef_p
= true;
3644 string_tree
= tok
->u
.value
;
3645 curr_type
= tok
->type
;
3649 /* Try to avoid the overhead of creating and destroying an obstack
3650 for the common case of just one string. */
3651 if (!cp_parser_is_string_literal
3652 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3654 cp_lexer_consume_token (parser
->lexer
);
3656 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3657 str
.len
= TREE_STRING_LENGTH (string_tree
);
3660 if (curr_tok_is_userdef_p
)
3662 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3664 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3667 curr_type
= tok
->type
;
3673 gcc_obstack_init (&str_ob
);
3678 cp_lexer_consume_token (parser
->lexer
);
3680 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3681 str
.len
= TREE_STRING_LENGTH (string_tree
);
3683 if (curr_tok_is_userdef_p
)
3685 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3686 if (have_suffix_p
== 0)
3688 suffix_id
= curr_suffix_id
;
3691 else if (have_suffix_p
== 1
3692 && curr_suffix_id
!= suffix_id
)
3694 error ("inconsistent user-defined literal suffixes"
3695 " %qD and %qD in string literal",
3696 suffix_id
, curr_suffix_id
);
3699 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3702 curr_type
= tok
->type
;
3704 if (type
!= curr_type
)
3706 if (type
== CPP_STRING
)
3708 else if (curr_type
!= CPP_STRING
)
3709 error_at (tok
->location
,
3710 "unsupported non-standard concatenation "
3711 "of string literals");
3714 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3716 tok
= cp_lexer_peek_token (parser
->lexer
);
3717 if (cpp_userdef_string_p (tok
->type
))
3719 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3720 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3721 curr_tok_is_userdef_p
= true;
3725 string_tree
= tok
->u
.value
;
3726 curr_type
= tok
->type
;
3727 curr_tok_is_userdef_p
= false;
3730 while (cp_parser_is_string_literal (tok
));
3732 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3735 if (type
!= CPP_STRING
&& !wide_ok
)
3737 cp_parser_error (parser
, "a wide string is invalid in this context");
3741 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3742 (parse_in
, strs
, count
, &istr
, type
))
3744 value
= build_string (istr
.len
, (const char *)istr
.text
);
3745 free (CONST_CAST (unsigned char *, istr
.text
));
3751 case CPP_UTF8STRING
:
3752 TREE_TYPE (value
) = char_array_type_node
;
3755 TREE_TYPE (value
) = char16_array_type_node
;
3758 TREE_TYPE (value
) = char32_array_type_node
;
3761 TREE_TYPE (value
) = wchar_array_type_node
;
3765 value
= fix_string_type (value
);
3769 tree literal
= build_userdef_literal (suffix_id
, value
,
3770 OT_NONE
, NULL_TREE
);
3772 value
= cp_parser_userdef_string_literal (literal
);
3778 /* cpp_interpret_string has issued an error. */
3779 value
= error_mark_node
;
3782 obstack_free (&str_ob
, 0);
3787 /* Look up a literal operator with the name and the exact arguments. */
3790 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3793 decl
= lookup_name (name
);
3794 if (!decl
|| !is_overloaded_fn (decl
))
3795 return error_mark_node
;
3797 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3801 tree fn
= OVL_CURRENT (fns
);
3802 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3803 if (parmtypes
!= NULL_TREE
)
3805 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3806 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3808 tree tparm
= TREE_VALUE (parmtypes
);
3809 tree targ
= TREE_TYPE ((*args
)[ix
]);
3810 bool ptr
= TYPE_PTR_P (tparm
);
3811 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3812 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3814 || !same_type_p (TREE_TYPE (tparm
),
3819 && ix
== vec_safe_length (args
)
3820 /* May be this should be sufficient_parms_p instead,
3821 depending on how exactly should user-defined literals
3822 work in presence of default arguments on the literal
3823 operator parameters. */
3824 && parmtypes
== void_list_node
)
3829 return error_mark_node
;
3832 /* Parse a user-defined char constant. Returns a call to a user-defined
3833 literal operator taking the character as an argument. */
3836 cp_parser_userdef_char_literal (cp_parser
*parser
)
3838 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3839 tree literal
= token
->u
.value
;
3840 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3841 tree value
= USERDEF_LITERAL_VALUE (literal
);
3842 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3845 /* Build up a call to the user-defined operator */
3846 /* Lookup the name we got back from the id-expression. */
3847 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3848 vec_safe_push (args
, value
);
3849 decl
= lookup_literal_operator (name
, args
);
3850 if (!decl
|| decl
== error_mark_node
)
3852 error ("unable to find character literal operator %qD with %qT argument",
3853 name
, TREE_TYPE (value
));
3854 release_tree_vector (args
);
3855 return error_mark_node
;
3857 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3858 release_tree_vector (args
);
3859 if (result
!= error_mark_node
)
3862 error ("unable to find character literal operator %qD with %qT argument",
3863 name
, TREE_TYPE (value
));
3864 return error_mark_node
;
3867 /* A subroutine of cp_parser_userdef_numeric_literal to
3868 create a char... template parameter pack from a string node. */
3871 make_char_string_pack (tree value
)
3874 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3875 const char *str
= TREE_STRING_POINTER (value
);
3876 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3877 tree argvec
= make_tree_vec (1);
3879 /* Fill in CHARVEC with all of the parameters. */
3880 charvec
= make_tree_vec (len
);
3881 for (i
= 0; i
< len
; ++i
)
3882 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3884 /* Build the argument packs. */
3885 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3886 TREE_TYPE (argpack
) = char_type_node
;
3888 TREE_VEC_ELT (argvec
, 0) = argpack
;
3893 /* A subroutine of cp_parser_userdef_numeric_literal to
3894 create a char... template parameter pack from a string node. */
3897 make_string_pack (tree value
)
3900 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3901 const unsigned char *str
3902 = (const unsigned char *) TREE_STRING_POINTER (value
);
3903 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3904 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3905 tree argvec
= make_tree_vec (2);
3907 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3908 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3910 /* First template parm is character type. */
3911 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3913 /* Fill in CHARVEC with all of the parameters. */
3914 charvec
= make_tree_vec (len
);
3915 for (int i
= 0; i
< len
; ++i
)
3916 TREE_VEC_ELT (charvec
, i
)
3917 = double_int_to_tree (str_char_type_node
,
3918 double_int::from_buffer (str
+ i
* sz
, sz
));
3920 /* Build the argument packs. */
3921 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3922 TREE_TYPE (argpack
) = str_char_type_node
;
3924 TREE_VEC_ELT (argvec
, 1) = argpack
;
3929 /* Parse a user-defined numeric constant. returns a call to a user-defined
3930 literal operator. */
3933 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3935 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3936 tree literal
= token
->u
.value
;
3937 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3938 tree value
= USERDEF_LITERAL_VALUE (literal
);
3939 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3940 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3941 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3943 vec
<tree
, va_gc
> *args
;
3945 /* Look for a literal operator taking the exact type of numeric argument
3946 as the literal value. */
3947 args
= make_tree_vector ();
3948 vec_safe_push (args
, value
);
3949 decl
= lookup_literal_operator (name
, args
);
3950 if (decl
&& decl
!= error_mark_node
)
3952 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3953 if (result
!= error_mark_node
)
3955 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3956 warning_at (token
->location
, OPT_Woverflow
,
3957 "integer literal exceeds range of %qT type",
3958 long_long_unsigned_type_node
);
3962 warning_at (token
->location
, OPT_Woverflow
,
3963 "floating literal exceeds range of %qT type",
3964 long_double_type_node
);
3965 else if (overflow
< 0)
3966 warning_at (token
->location
, OPT_Woverflow
,
3967 "floating literal truncated to zero");
3969 release_tree_vector (args
);
3973 release_tree_vector (args
);
3975 /* If the numeric argument didn't work, look for a raw literal
3976 operator taking a const char* argument consisting of the number
3977 in string format. */
3978 args
= make_tree_vector ();
3979 vec_safe_push (args
, num_string
);
3980 decl
= lookup_literal_operator (name
, args
);
3981 if (decl
&& decl
!= error_mark_node
)
3983 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3984 if (result
!= error_mark_node
)
3986 release_tree_vector (args
);
3990 release_tree_vector (args
);
3992 /* If the raw literal didn't work, look for a non-type template
3993 function with parameter pack char.... Call the function with
3994 template parameter characters representing the number. */
3995 args
= make_tree_vector ();
3996 decl
= lookup_literal_operator (name
, args
);
3997 if (decl
&& decl
!= error_mark_node
)
3999 tree tmpl_args
= make_char_string_pack (num_string
);
4000 decl
= lookup_template_function (decl
, tmpl_args
);
4001 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4002 if (result
!= error_mark_node
)
4004 release_tree_vector (args
);
4008 release_tree_vector (args
);
4010 error ("unable to find numeric literal operator %qD", name
);
4011 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4012 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4013 "to enable more built-in suffixes");
4014 return error_mark_node
;
4017 /* Parse a user-defined string constant. Returns a call to a user-defined
4018 literal operator taking a character pointer and the length of the string
4022 cp_parser_userdef_string_literal (tree literal
)
4024 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4025 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4026 tree value
= USERDEF_LITERAL_VALUE (literal
);
4027 int len
= TREE_STRING_LENGTH (value
)
4028 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4030 vec
<tree
, va_gc
> *args
;
4032 /* Look for a template function with typename parameter CharT
4033 and parameter pack CharT... Call the function with
4034 template parameter characters representing the string. */
4035 args
= make_tree_vector ();
4036 decl
= lookup_literal_operator (name
, args
);
4037 if (decl
&& decl
!= error_mark_node
)
4039 tree tmpl_args
= make_string_pack (value
);
4040 decl
= lookup_template_function (decl
, tmpl_args
);
4041 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4042 if (result
!= error_mark_node
)
4044 release_tree_vector (args
);
4048 release_tree_vector (args
);
4050 /* Build up a call to the user-defined operator */
4051 /* Lookup the name we got back from the id-expression. */
4052 args
= make_tree_vector ();
4053 vec_safe_push (args
, value
);
4054 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4055 decl
= lookup_name (name
);
4056 if (!decl
|| decl
== error_mark_node
)
4058 error ("unable to find string literal operator %qD", name
);
4059 release_tree_vector (args
);
4060 return error_mark_node
;
4062 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4063 release_tree_vector (args
);
4064 if (result
!= error_mark_node
)
4067 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4068 name
, TREE_TYPE (value
), size_type_node
);
4069 return error_mark_node
;
4073 /* Basic concepts [gram.basic] */
4075 /* Parse a translation-unit.
4078 declaration-seq [opt]
4080 Returns TRUE if all went well. */
4083 cp_parser_translation_unit (cp_parser
* parser
)
4085 /* The address of the first non-permanent object on the declarator
4087 static void *declarator_obstack_base
;
4091 /* Create the declarator obstack, if necessary. */
4092 if (!cp_error_declarator
)
4094 gcc_obstack_init (&declarator_obstack
);
4095 /* Create the error declarator. */
4096 cp_error_declarator
= make_declarator (cdk_error
);
4097 /* Create the empty parameter list. */
4098 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4099 /* Remember where the base of the declarator obstack lies. */
4100 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4103 cp_parser_declaration_seq_opt (parser
);
4105 /* If there are no tokens left then all went well. */
4106 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4108 /* Get rid of the token array; we don't need it any more. */
4109 cp_lexer_destroy (parser
->lexer
);
4110 parser
->lexer
= NULL
;
4112 /* This file might have been a context that's implicitly extern
4113 "C". If so, pop the lang context. (Only relevant for PCH.) */
4114 if (parser
->implicit_extern_c
)
4116 pop_lang_context ();
4117 parser
->implicit_extern_c
= false;
4121 finish_translation_unit ();
4127 cp_parser_error (parser
, "expected declaration");
4131 /* Make sure the declarator obstack was fully cleaned up. */
4132 gcc_assert (obstack_next_free (&declarator_obstack
)
4133 == declarator_obstack_base
);
4135 /* All went well. */
4139 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4140 decltype context. */
4142 static inline tsubst_flags_t
4143 complain_flags (bool decltype_p
)
4145 tsubst_flags_t complain
= tf_warning_or_error
;
4147 complain
|= tf_decltype
;
4151 /* We're about to parse a collection of statements. If we're currently
4152 parsing tentatively, set up a firewall so that any nested
4153 cp_parser_commit_to_tentative_parse won't affect the current context. */
4155 static cp_token_position
4156 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4158 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4161 cp_parser_parse_tentatively (parser
);
4162 cp_parser_commit_to_topmost_tentative_parse (parser
);
4163 return cp_lexer_token_position (parser
->lexer
, false);
4166 /* We've finished parsing the collection of statements. Wrap up the
4167 firewall and replace the relevant tokens with the parsed form. */
4170 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4176 /* Finish the firewall level. */
4177 cp_parser_parse_definitely (parser
);
4178 /* And remember the result of the parse for when we try again. */
4179 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4180 token
->type
= CPP_PREPARSED_EXPR
;
4181 token
->u
.value
= expr
;
4182 token
->keyword
= RID_MAX
;
4183 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4186 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4187 enclosing parentheses. */
4190 cp_parser_statement_expr (cp_parser
*parser
)
4192 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4194 /* Consume the '('. */
4195 cp_lexer_consume_token (parser
->lexer
);
4196 /* Start the statement-expression. */
4197 tree expr
= begin_stmt_expr ();
4198 /* Parse the compound-statement. */
4199 cp_parser_compound_statement (parser
, expr
, false, false);
4201 expr
= finish_stmt_expr (expr
, false);
4202 /* Consume the ')'. */
4203 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4204 cp_parser_skip_to_end_of_statement (parser
);
4206 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4210 /* Expressions [gram.expr] */
4212 /* Parse a primary-expression.
4219 lambda-expression (C++11)
4224 ( compound-statement )
4225 __builtin_va_arg ( assignment-expression , type-id )
4226 __builtin_offsetof ( type-id , offsetof-expression )
4229 __has_nothrow_assign ( type-id )
4230 __has_nothrow_constructor ( type-id )
4231 __has_nothrow_copy ( type-id )
4232 __has_trivial_assign ( type-id )
4233 __has_trivial_constructor ( type-id )
4234 __has_trivial_copy ( type-id )
4235 __has_trivial_destructor ( type-id )
4236 __has_virtual_destructor ( type-id )
4237 __is_abstract ( type-id )
4238 __is_base_of ( type-id , type-id )
4239 __is_class ( type-id )
4240 __is_empty ( type-id )
4241 __is_enum ( type-id )
4242 __is_final ( type-id )
4243 __is_literal_type ( type-id )
4244 __is_pod ( type-id )
4245 __is_polymorphic ( type-id )
4246 __is_std_layout ( type-id )
4247 __is_trivial ( type-id )
4248 __is_union ( type-id )
4250 Objective-C++ Extension:
4258 ADDRESS_P is true iff this expression was immediately preceded by
4259 "&" and therefore might denote a pointer-to-member. CAST_P is true
4260 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4261 true iff this expression is a template argument.
4263 Returns a representation of the expression. Upon return, *IDK
4264 indicates what kind of id-expression (if any) was present. */
4267 cp_parser_primary_expression (cp_parser
*parser
,
4270 bool template_arg_p
,
4274 cp_token
*token
= NULL
;
4276 /* Assume the primary expression is not an id-expression. */
4277 *idk
= CP_ID_KIND_NONE
;
4279 /* Peek at the next token. */
4280 token
= cp_lexer_peek_token (parser
->lexer
);
4281 switch ((int) token
->type
)
4290 user-defined-literal */
4296 case CPP_PREPARSED_EXPR
:
4297 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4298 return cp_parser_userdef_numeric_literal (parser
);
4299 token
= cp_lexer_consume_token (parser
->lexer
);
4300 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4302 error_at (token
->location
,
4303 "fixed-point types not supported in C++");
4304 return error_mark_node
;
4306 /* Floating-point literals are only allowed in an integral
4307 constant expression if they are cast to an integral or
4308 enumeration type. */
4309 if (TREE_CODE (token
->u
.value
) == REAL_CST
4310 && parser
->integral_constant_expression_p
4313 /* CAST_P will be set even in invalid code like "int(2.7 +
4314 ...)". Therefore, we have to check that the next token
4315 is sure to end the cast. */
4318 cp_token
*next_token
;
4320 next_token
= cp_lexer_peek_token (parser
->lexer
);
4321 if (/* The comma at the end of an
4322 enumerator-definition. */
4323 next_token
->type
!= CPP_COMMA
4324 /* The curly brace at the end of an enum-specifier. */
4325 && next_token
->type
!= CPP_CLOSE_BRACE
4326 /* The end of a statement. */
4327 && next_token
->type
!= CPP_SEMICOLON
4328 /* The end of the cast-expression. */
4329 && next_token
->type
!= CPP_CLOSE_PAREN
4330 /* The end of an array bound. */
4331 && next_token
->type
!= CPP_CLOSE_SQUARE
4332 /* The closing ">" in a template-argument-list. */
4333 && (next_token
->type
!= CPP_GREATER
4334 || parser
->greater_than_is_operator_p
)
4335 /* C++0x only: A ">>" treated like two ">" tokens,
4336 in a template-argument-list. */
4337 && (next_token
->type
!= CPP_RSHIFT
4338 || (cxx_dialect
== cxx98
)
4339 || parser
->greater_than_is_operator_p
))
4343 /* If we are within a cast, then the constraint that the
4344 cast is to an integral or enumeration type will be
4345 checked at that point. If we are not within a cast, then
4346 this code is invalid. */
4348 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4350 return token
->u
.value
;
4352 case CPP_CHAR_USERDEF
:
4353 case CPP_CHAR16_USERDEF
:
4354 case CPP_CHAR32_USERDEF
:
4355 case CPP_WCHAR_USERDEF
:
4356 return cp_parser_userdef_char_literal (parser
);
4362 case CPP_UTF8STRING
:
4363 case CPP_STRING_USERDEF
:
4364 case CPP_STRING16_USERDEF
:
4365 case CPP_STRING32_USERDEF
:
4366 case CPP_WSTRING_USERDEF
:
4367 case CPP_UTF8STRING_USERDEF
:
4368 /* ??? Should wide strings be allowed when parser->translate_strings_p
4369 is false (i.e. in attributes)? If not, we can kill the third
4370 argument to cp_parser_string_literal. */
4371 return cp_parser_string_literal (parser
,
4372 parser
->translate_strings_p
,
4375 case CPP_OPEN_PAREN
:
4376 /* If we see `( { ' then we are looking at the beginning of
4377 a GNU statement-expression. */
4378 if (cp_parser_allow_gnu_extensions_p (parser
)
4379 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4381 /* Statement-expressions are not allowed by the standard. */
4382 pedwarn (token
->location
, OPT_Wpedantic
,
4383 "ISO C++ forbids braced-groups within expressions");
4385 /* And they're not allowed outside of a function-body; you
4386 cannot, for example, write:
4388 int i = ({ int j = 3; j + 1; });
4390 at class or namespace scope. */
4391 if (!parser
->in_function_body
4392 || parser
->in_template_argument_list_p
)
4394 error_at (token
->location
,
4395 "statement-expressions are not allowed outside "
4396 "functions nor in template-argument lists");
4397 cp_parser_skip_to_end_of_block_or_statement (parser
);
4398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4399 cp_lexer_consume_token (parser
->lexer
);
4400 return error_mark_node
;
4403 return cp_parser_statement_expr (parser
);
4405 /* Otherwise it's a normal parenthesized expression. */
4408 bool saved_greater_than_is_operator_p
;
4410 /* Consume the `('. */
4411 cp_lexer_consume_token (parser
->lexer
);
4412 /* Within a parenthesized expression, a `>' token is always
4413 the greater-than operator. */
4414 saved_greater_than_is_operator_p
4415 = parser
->greater_than_is_operator_p
;
4416 parser
->greater_than_is_operator_p
= true;
4418 /* Parse the parenthesized expression. */
4419 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4420 /* Let the front end know that this expression was
4421 enclosed in parentheses. This matters in case, for
4422 example, the expression is of the form `A::B', since
4423 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4425 expr
= finish_parenthesized_expr (expr
);
4426 /* DR 705: Wrapping an unqualified name in parentheses
4427 suppresses arg-dependent lookup. We want to pass back
4428 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4429 (c++/37862), but none of the others. */
4430 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4431 *idk
= CP_ID_KIND_NONE
;
4433 /* The `>' token might be the end of a template-id or
4434 template-parameter-list now. */
4435 parser
->greater_than_is_operator_p
4436 = saved_greater_than_is_operator_p
;
4437 /* Consume the `)'. */
4438 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4439 cp_parser_skip_to_end_of_statement (parser
);
4444 case CPP_OPEN_SQUARE
:
4445 if (c_dialect_objc ())
4446 /* We have an Objective-C++ message. */
4447 return cp_parser_objc_expression (parser
);
4449 tree lam
= cp_parser_lambda_expression (parser
);
4450 /* Don't warn about a failed tentative parse. */
4451 if (cp_parser_error_occurred (parser
))
4452 return error_mark_node
;
4453 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4457 case CPP_OBJC_STRING
:
4458 if (c_dialect_objc ())
4459 /* We have an Objective-C++ string literal. */
4460 return cp_parser_objc_expression (parser
);
4461 cp_parser_error (parser
, "expected primary-expression");
4462 return error_mark_node
;
4465 switch (token
->keyword
)
4467 /* These two are the boolean literals. */
4469 cp_lexer_consume_token (parser
->lexer
);
4470 return boolean_true_node
;
4472 cp_lexer_consume_token (parser
->lexer
);
4473 return boolean_false_node
;
4475 /* The `__null' literal. */
4477 cp_lexer_consume_token (parser
->lexer
);
4480 /* The `nullptr' literal. */
4482 cp_lexer_consume_token (parser
->lexer
);
4483 return nullptr_node
;
4485 /* Recognize the `this' keyword. */
4487 cp_lexer_consume_token (parser
->lexer
);
4488 if (parser
->local_variables_forbidden_p
)
4490 error_at (token
->location
,
4491 "%<this%> may not be used in this context");
4492 return error_mark_node
;
4494 /* Pointers cannot appear in constant-expressions. */
4495 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4496 return error_mark_node
;
4497 return finish_this_expr ();
4499 /* The `operator' keyword can be the beginning of an
4504 case RID_FUNCTION_NAME
:
4505 case RID_PRETTY_FUNCTION_NAME
:
4506 case RID_C99_FUNCTION_NAME
:
4508 non_integral_constant name
;
4510 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4511 __func__ are the names of variables -- but they are
4512 treated specially. Therefore, they are handled here,
4513 rather than relying on the generic id-expression logic
4514 below. Grammatically, these names are id-expressions.
4516 Consume the token. */
4517 token
= cp_lexer_consume_token (parser
->lexer
);
4519 switch (token
->keyword
)
4521 case RID_FUNCTION_NAME
:
4522 name
= NIC_FUNC_NAME
;
4524 case RID_PRETTY_FUNCTION_NAME
:
4525 name
= NIC_PRETTY_FUNC
;
4527 case RID_C99_FUNCTION_NAME
:
4528 name
= NIC_C99_FUNC
;
4534 if (cp_parser_non_integral_constant_expression (parser
, name
))
4535 return error_mark_node
;
4537 /* Look up the name. */
4538 return finish_fname (token
->u
.value
);
4545 source_location type_location
;
4547 /* The `__builtin_va_arg' construct is used to handle
4548 `va_arg'. Consume the `__builtin_va_arg' token. */
4549 cp_lexer_consume_token (parser
->lexer
);
4550 /* Look for the opening `('. */
4551 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4552 /* Now, parse the assignment-expression. */
4553 expression
= cp_parser_assignment_expression (parser
);
4554 /* Look for the `,'. */
4555 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4556 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4557 /* Parse the type-id. */
4558 type
= cp_parser_type_id (parser
);
4559 /* Look for the closing `)'. */
4560 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4561 /* Using `va_arg' in a constant-expression is not
4563 if (cp_parser_non_integral_constant_expression (parser
,
4565 return error_mark_node
;
4566 return build_x_va_arg (type_location
, expression
, type
);
4570 return cp_parser_builtin_offsetof (parser
);
4572 case RID_HAS_NOTHROW_ASSIGN
:
4573 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4574 case RID_HAS_NOTHROW_COPY
:
4575 case RID_HAS_TRIVIAL_ASSIGN
:
4576 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4577 case RID_HAS_TRIVIAL_COPY
:
4578 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4579 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4580 case RID_IS_ABSTRACT
:
4581 case RID_IS_BASE_OF
:
4586 case RID_IS_LITERAL_TYPE
:
4588 case RID_IS_POLYMORPHIC
:
4589 case RID_IS_STD_LAYOUT
:
4590 case RID_IS_TRIVIAL
:
4591 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4592 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4593 case RID_IS_TRIVIALLY_COPYABLE
:
4595 return cp_parser_trait_expr (parser
, token
->keyword
);
4597 /* Objective-C++ expressions. */
4599 case RID_AT_PROTOCOL
:
4600 case RID_AT_SELECTOR
:
4601 return cp_parser_objc_expression (parser
);
4604 if (parser
->in_function_body
4605 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4608 error_at (token
->location
,
4609 "a template declaration cannot appear at block scope");
4610 cp_parser_skip_to_end_of_block_or_statement (parser
);
4611 return error_mark_node
;
4614 cp_parser_error (parser
, "expected primary-expression");
4615 return error_mark_node
;
4618 /* An id-expression can start with either an identifier, a
4619 `::' as the beginning of a qualified-id, or the "operator"
4623 case CPP_TEMPLATE_ID
:
4624 case CPP_NESTED_NAME_SPECIFIER
:
4628 const char *error_msg
;
4631 cp_token
*id_expr_token
;
4634 /* Parse the id-expression. */
4636 = cp_parser_id_expression (parser
,
4637 /*template_keyword_p=*/false,
4638 /*check_dependency_p=*/true,
4640 /*declarator_p=*/false,
4641 /*optional_p=*/false);
4642 if (id_expression
== error_mark_node
)
4643 return error_mark_node
;
4644 id_expr_token
= token
;
4645 token
= cp_lexer_peek_token (parser
->lexer
);
4646 done
= (token
->type
!= CPP_OPEN_SQUARE
4647 && token
->type
!= CPP_OPEN_PAREN
4648 && token
->type
!= CPP_DOT
4649 && token
->type
!= CPP_DEREF
4650 && token
->type
!= CPP_PLUS_PLUS
4651 && token
->type
!= CPP_MINUS_MINUS
);
4652 /* If we have a template-id, then no further lookup is
4653 required. If the template-id was for a template-class, we
4654 will sometimes have a TYPE_DECL at this point. */
4655 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4656 || TREE_CODE (id_expression
) == TYPE_DECL
)
4657 decl
= id_expression
;
4658 /* Look up the name. */
4661 tree ambiguous_decls
;
4663 /* If we already know that this lookup is ambiguous, then
4664 we've already issued an error message; there's no reason
4666 if (id_expr_token
->type
== CPP_NAME
4667 && id_expr_token
->error_reported
)
4669 cp_parser_simulate_error (parser
);
4670 return error_mark_node
;
4673 decl
= cp_parser_lookup_name (parser
, id_expression
,
4676 /*is_namespace=*/false,
4677 /*check_dependency=*/true,
4679 id_expr_token
->location
);
4680 /* If the lookup was ambiguous, an error will already have
4682 if (ambiguous_decls
)
4683 return error_mark_node
;
4685 /* In Objective-C++, we may have an Objective-C 2.0
4686 dot-syntax for classes here. */
4687 if (c_dialect_objc ()
4688 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4689 && TREE_CODE (decl
) == TYPE_DECL
4690 && objc_is_class_name (decl
))
4693 cp_lexer_consume_token (parser
->lexer
);
4694 component
= cp_parser_identifier (parser
);
4695 if (component
== error_mark_node
)
4696 return error_mark_node
;
4698 return objc_build_class_component_ref (id_expression
, component
);
4701 /* In Objective-C++, an instance variable (ivar) may be preferred
4702 to whatever cp_parser_lookup_name() found. */
4703 decl
= objc_lookup_ivar (decl
, id_expression
);
4705 /* If name lookup gives us a SCOPE_REF, then the
4706 qualifying scope was dependent. */
4707 if (TREE_CODE (decl
) == SCOPE_REF
)
4709 /* At this point, we do not know if DECL is a valid
4710 integral constant expression. We assume that it is
4711 in fact such an expression, so that code like:
4713 template <int N> struct A {
4717 is accepted. At template-instantiation time, we
4718 will check that B<N>::i is actually a constant. */
4721 /* Check to see if DECL is a local variable in a context
4722 where that is forbidden. */
4723 if (parser
->local_variables_forbidden_p
4724 && local_variable_p (decl
))
4726 /* It might be that we only found DECL because we are
4727 trying to be generous with pre-ISO scoping rules.
4728 For example, consider:
4732 for (int i = 0; i < 10; ++i) {}
4733 extern void f(int j = i);
4736 Here, name look up will originally find the out
4737 of scope `i'. We need to issue a warning message,
4738 but then use the global `i'. */
4739 decl
= check_for_out_of_scope_variable (decl
);
4740 if (local_variable_p (decl
))
4742 error_at (id_expr_token
->location
,
4743 "local variable %qD may not appear in this context",
4745 return error_mark_node
;
4750 decl
= (finish_id_expression
4751 (id_expression
, decl
, parser
->scope
,
4753 parser
->integral_constant_expression_p
,
4754 parser
->allow_non_integral_constant_expression_p
,
4755 &parser
->non_integral_constant_expression_p
,
4756 template_p
, done
, address_p
,
4759 id_expr_token
->location
));
4761 cp_parser_error (parser
, error_msg
);
4765 /* Anything else is an error. */
4767 cp_parser_error (parser
, "expected primary-expression");
4768 return error_mark_node
;
4773 cp_parser_primary_expression (cp_parser
*parser
,
4776 bool template_arg_p
,
4779 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4780 /*decltype*/false, idk
);
4783 /* Parse an id-expression.
4790 :: [opt] nested-name-specifier template [opt] unqualified-id
4792 :: operator-function-id
4795 Return a representation of the unqualified portion of the
4796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4797 a `::' or nested-name-specifier.
4799 Often, if the id-expression was a qualified-id, the caller will
4800 want to make a SCOPE_REF to represent the qualified-id. This
4801 function does not do this in order to avoid wastefully creating
4802 SCOPE_REFs when they are not required.
4804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4808 uninstantiated templates.
4810 If *TEMPLATE_P is non-NULL, it is set to true iff the
4811 `template' keyword is used to explicitly indicate that the entity
4812 named is a template.
4814 If DECLARATOR_P is true, the id-expression is appearing as part of
4815 a declarator, rather than as part of an expression. */
4818 cp_parser_id_expression (cp_parser
*parser
,
4819 bool template_keyword_p
,
4820 bool check_dependency_p
,
4825 bool global_scope_p
;
4826 bool nested_name_specifier_p
;
4828 /* Assume the `template' keyword was not used. */
4830 *template_p
= template_keyword_p
;
4832 /* Look for the optional `::' operator. */
4834 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4836 /* Look for the optional nested-name-specifier. */
4837 nested_name_specifier_p
4838 = (cp_parser_nested_name_specifier_opt (parser
,
4839 /*typename_keyword_p=*/false,
4844 /* If there is a nested-name-specifier, then we are looking at
4845 the first qualified-id production. */
4846 if (nested_name_specifier_p
)
4849 tree saved_object_scope
;
4850 tree saved_qualifying_scope
;
4851 tree unqualified_id
;
4854 /* See if the next token is the `template' keyword. */
4856 template_p
= &is_template
;
4857 *template_p
= cp_parser_optional_template_keyword (parser
);
4858 /* Name lookup we do during the processing of the
4859 unqualified-id might obliterate SCOPE. */
4860 saved_scope
= parser
->scope
;
4861 saved_object_scope
= parser
->object_scope
;
4862 saved_qualifying_scope
= parser
->qualifying_scope
;
4863 /* Process the final unqualified-id. */
4864 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4867 /*optional_p=*/false);
4868 /* Restore the SAVED_SCOPE for our caller. */
4869 parser
->scope
= saved_scope
;
4870 parser
->object_scope
= saved_object_scope
;
4871 parser
->qualifying_scope
= saved_qualifying_scope
;
4873 return unqualified_id
;
4875 /* Otherwise, if we are in global scope, then we are looking at one
4876 of the other qualified-id productions. */
4877 else if (global_scope_p
)
4882 /* Peek at the next token. */
4883 token
= cp_lexer_peek_token (parser
->lexer
);
4885 /* If it's an identifier, and the next token is not a "<", then
4886 we can avoid the template-id case. This is an optimization
4887 for this common case. */
4888 if (token
->type
== CPP_NAME
4889 && !cp_parser_nth_token_starts_template_argument_list_p
4891 return cp_parser_identifier (parser
);
4893 cp_parser_parse_tentatively (parser
);
4894 /* Try a template-id. */
4895 id
= cp_parser_template_id (parser
,
4896 /*template_keyword_p=*/false,
4897 /*check_dependency_p=*/true,
4900 /* If that worked, we're done. */
4901 if (cp_parser_parse_definitely (parser
))
4904 /* Peek at the next token. (Changes in the token buffer may
4905 have invalidated the pointer obtained above.) */
4906 token
= cp_lexer_peek_token (parser
->lexer
);
4908 switch (token
->type
)
4911 return cp_parser_identifier (parser
);
4914 if (token
->keyword
== RID_OPERATOR
)
4915 return cp_parser_operator_function_id (parser
);
4919 cp_parser_error (parser
, "expected id-expression");
4920 return error_mark_node
;
4924 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4925 /*check_dependency_p=*/true,
4930 /* Parse an unqualified-id.
4934 operator-function-id
4935 conversion-function-id
4939 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4940 keyword, in a construct like `A::template ...'.
4942 Returns a representation of unqualified-id. For the `identifier'
4943 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4944 production a BIT_NOT_EXPR is returned; the operand of the
4945 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4946 other productions, see the documentation accompanying the
4947 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4948 names are looked up in uninstantiated templates. If DECLARATOR_P
4949 is true, the unqualified-id is appearing as part of a declarator,
4950 rather than as part of an expression. */
4953 cp_parser_unqualified_id (cp_parser
* parser
,
4954 bool template_keyword_p
,
4955 bool check_dependency_p
,
4961 /* Peek at the next token. */
4962 token
= cp_lexer_peek_token (parser
->lexer
);
4964 switch ((int) token
->type
)
4970 /* We don't know yet whether or not this will be a
4972 cp_parser_parse_tentatively (parser
);
4973 /* Try a template-id. */
4974 id
= cp_parser_template_id (parser
, template_keyword_p
,
4978 /* If it worked, we're done. */
4979 if (cp_parser_parse_definitely (parser
))
4981 /* Otherwise, it's an ordinary identifier. */
4982 return cp_parser_identifier (parser
);
4985 case CPP_TEMPLATE_ID
:
4986 return cp_parser_template_id (parser
, template_keyword_p
,
4994 tree qualifying_scope
;
4999 /* Consume the `~' token. */
5000 cp_lexer_consume_token (parser
->lexer
);
5001 /* Parse the class-name. The standard, as written, seems to
5004 template <typename T> struct S { ~S (); };
5005 template <typename T> S<T>::~S() {}
5007 is invalid, since `~' must be followed by a class-name, but
5008 `S<T>' is dependent, and so not known to be a class.
5009 That's not right; we need to look in uninstantiated
5010 templates. A further complication arises from:
5012 template <typename T> void f(T t) {
5016 Here, it is not possible to look up `T' in the scope of `T'
5017 itself. We must look in both the current scope, and the
5018 scope of the containing complete expression.
5020 Yet another issue is:
5029 The standard does not seem to say that the `S' in `~S'
5030 should refer to the type `S' and not the data member
5033 /* DR 244 says that we look up the name after the "~" in the
5034 same scope as we looked up the qualifying name. That idea
5035 isn't fully worked out; it's more complicated than that. */
5036 scope
= parser
->scope
;
5037 object_scope
= parser
->object_scope
;
5038 qualifying_scope
= parser
->qualifying_scope
;
5040 /* Check for invalid scopes. */
5041 if (scope
== error_mark_node
)
5043 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5044 cp_lexer_consume_token (parser
->lexer
);
5045 return error_mark_node
;
5047 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5050 error_at (token
->location
,
5051 "scope %qT before %<~%> is not a class-name",
5053 cp_parser_simulate_error (parser
);
5054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5055 cp_lexer_consume_token (parser
->lexer
);
5056 return error_mark_node
;
5058 gcc_assert (!scope
|| TYPE_P (scope
));
5060 /* If the name is of the form "X::~X" it's OK even if X is a
5062 token
= cp_lexer_peek_token (parser
->lexer
);
5064 && token
->type
== CPP_NAME
5065 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5067 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5068 || (CLASS_TYPE_P (scope
)
5069 && constructor_name_p (token
->u
.value
, scope
))))
5071 cp_lexer_consume_token (parser
->lexer
);
5072 return build_nt (BIT_NOT_EXPR
, scope
);
5075 /* ~auto means the destructor of whatever the object is. */
5076 if (cp_parser_is_keyword (token
, RID_AUTO
))
5078 if (cxx_dialect
< cxx14
)
5079 pedwarn (input_location
, 0,
5080 "%<~auto%> only available with "
5081 "-std=c++14 or -std=gnu++14");
5082 cp_lexer_consume_token (parser
->lexer
);
5083 return build_nt (BIT_NOT_EXPR
, make_auto ());
5086 /* If there was an explicit qualification (S::~T), first look
5087 in the scope given by the qualification (i.e., S).
5089 Note: in the calls to cp_parser_class_name below we pass
5090 typename_type so that lookup finds the injected-class-name
5091 rather than the constructor. */
5093 type_decl
= NULL_TREE
;
5096 cp_parser_parse_tentatively (parser
);
5097 type_decl
= cp_parser_class_name (parser
,
5098 /*typename_keyword_p=*/false,
5099 /*template_keyword_p=*/false,
5101 /*check_dependency=*/false,
5102 /*class_head_p=*/false,
5104 if (cp_parser_parse_definitely (parser
))
5107 /* In "N::S::~S", look in "N" as well. */
5108 if (!done
&& scope
&& qualifying_scope
)
5110 cp_parser_parse_tentatively (parser
);
5111 parser
->scope
= qualifying_scope
;
5112 parser
->object_scope
= NULL_TREE
;
5113 parser
->qualifying_scope
= NULL_TREE
;
5115 = cp_parser_class_name (parser
,
5116 /*typename_keyword_p=*/false,
5117 /*template_keyword_p=*/false,
5119 /*check_dependency=*/false,
5120 /*class_head_p=*/false,
5122 if (cp_parser_parse_definitely (parser
))
5125 /* In "p->S::~T", look in the scope given by "*p" as well. */
5126 else if (!done
&& object_scope
)
5128 cp_parser_parse_tentatively (parser
);
5129 parser
->scope
= object_scope
;
5130 parser
->object_scope
= NULL_TREE
;
5131 parser
->qualifying_scope
= NULL_TREE
;
5133 = cp_parser_class_name (parser
,
5134 /*typename_keyword_p=*/false,
5135 /*template_keyword_p=*/false,
5137 /*check_dependency=*/false,
5138 /*class_head_p=*/false,
5140 if (cp_parser_parse_definitely (parser
))
5143 /* Look in the surrounding context. */
5146 parser
->scope
= NULL_TREE
;
5147 parser
->object_scope
= NULL_TREE
;
5148 parser
->qualifying_scope
= NULL_TREE
;
5149 if (processing_template_decl
)
5150 cp_parser_parse_tentatively (parser
);
5152 = cp_parser_class_name (parser
,
5153 /*typename_keyword_p=*/false,
5154 /*template_keyword_p=*/false,
5156 /*check_dependency=*/false,
5157 /*class_head_p=*/false,
5159 if (processing_template_decl
5160 && ! cp_parser_parse_definitely (parser
))
5162 /* We couldn't find a type with this name, so just accept
5163 it and check for a match at instantiation time. */
5164 type_decl
= cp_parser_identifier (parser
);
5165 if (type_decl
!= error_mark_node
)
5166 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5170 /* If an error occurred, assume that the name of the
5171 destructor is the same as the name of the qualifying
5172 class. That allows us to keep parsing after running
5173 into ill-formed destructor names. */
5174 if (type_decl
== error_mark_node
&& scope
)
5175 return build_nt (BIT_NOT_EXPR
, scope
);
5176 else if (type_decl
== error_mark_node
)
5177 return error_mark_node
;
5179 /* Check that destructor name and scope match. */
5180 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5182 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5183 error_at (token
->location
,
5184 "declaration of %<~%T%> as member of %qT",
5186 cp_parser_simulate_error (parser
);
5187 return error_mark_node
;
5192 A typedef-name that names a class shall not be used as the
5193 identifier in the declarator for a destructor declaration. */
5195 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5196 && !DECL_SELF_REFERENCE_P (type_decl
)
5197 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5198 error_at (token
->location
,
5199 "typedef-name %qD used as destructor declarator",
5202 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5206 if (token
->keyword
== RID_OPERATOR
)
5210 /* This could be a template-id, so we try that first. */
5211 cp_parser_parse_tentatively (parser
);
5212 /* Try a template-id. */
5213 id
= cp_parser_template_id (parser
, template_keyword_p
,
5214 /*check_dependency_p=*/true,
5217 /* If that worked, we're done. */
5218 if (cp_parser_parse_definitely (parser
))
5220 /* We still don't know whether we're looking at an
5221 operator-function-id or a conversion-function-id. */
5222 cp_parser_parse_tentatively (parser
);
5223 /* Try an operator-function-id. */
5224 id
= cp_parser_operator_function_id (parser
);
5225 /* If that didn't work, try a conversion-function-id. */
5226 if (!cp_parser_parse_definitely (parser
))
5227 id
= cp_parser_conversion_function_id (parser
);
5228 else if (UDLIT_OPER_P (id
))
5231 const char *name
= UDLIT_OP_SUFFIX (id
);
5232 if (name
[0] != '_' && !in_system_header_at (input_location
)
5234 warning (0, "literal operator suffixes not preceded by %<_%>"
5235 " are reserved for future standardization");
5245 cp_parser_error (parser
, "expected unqualified-id");
5246 return error_mark_node
;
5250 /* Parse an (optional) nested-name-specifier.
5252 nested-name-specifier: [C++98]
5253 class-or-namespace-name :: nested-name-specifier [opt]
5254 class-or-namespace-name :: template nested-name-specifier [opt]
5256 nested-name-specifier: [C++0x]
5259 nested-name-specifier identifier ::
5260 nested-name-specifier template [opt] simple-template-id ::
5262 PARSER->SCOPE should be set appropriately before this function is
5263 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5264 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5267 Sets PARSER->SCOPE to the class (TYPE) or namespace
5268 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5269 it unchanged if there is no nested-name-specifier. Returns the new
5270 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5272 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5273 part of a declaration and/or decl-specifier. */
5276 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5277 bool typename_keyword_p
,
5278 bool check_dependency_p
,
5280 bool is_declaration
)
5282 bool success
= false;
5283 cp_token_position start
= 0;
5286 /* Remember where the nested-name-specifier starts. */
5287 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5289 start
= cp_lexer_token_position (parser
->lexer
, false);
5290 push_deferring_access_checks (dk_deferred
);
5297 tree saved_qualifying_scope
;
5298 bool template_keyword_p
;
5300 /* Spot cases that cannot be the beginning of a
5301 nested-name-specifier. */
5302 token
= cp_lexer_peek_token (parser
->lexer
);
5304 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5305 the already parsed nested-name-specifier. */
5306 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5308 /* Grab the nested-name-specifier and continue the loop. */
5309 cp_parser_pre_parsed_nested_name_specifier (parser
);
5310 /* If we originally encountered this nested-name-specifier
5311 with IS_DECLARATION set to false, we will not have
5312 resolved TYPENAME_TYPEs, so we must do so here. */
5314 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5316 new_scope
= resolve_typename_type (parser
->scope
,
5317 /*only_current_p=*/false);
5318 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5319 parser
->scope
= new_scope
;
5325 /* Spot cases that cannot be the beginning of a
5326 nested-name-specifier. On the second and subsequent times
5327 through the loop, we look for the `template' keyword. */
5328 if (success
&& token
->keyword
== RID_TEMPLATE
)
5330 /* A template-id can start a nested-name-specifier. */
5331 else if (token
->type
== CPP_TEMPLATE_ID
)
5333 /* DR 743: decltype can be used in a nested-name-specifier. */
5334 else if (token_is_decltype (token
))
5338 /* If the next token is not an identifier, then it is
5339 definitely not a type-name or namespace-name. */
5340 if (token
->type
!= CPP_NAME
)
5342 /* If the following token is neither a `<' (to begin a
5343 template-id), nor a `::', then we are not looking at a
5344 nested-name-specifier. */
5345 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5347 if (token
->type
== CPP_COLON
5348 && parser
->colon_corrects_to_scope_p
5349 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5351 error_at (token
->location
,
5352 "found %<:%> in nested-name-specifier, expected %<::%>");
5353 token
->type
= CPP_SCOPE
;
5356 if (token
->type
!= CPP_SCOPE
5357 && !cp_parser_nth_token_starts_template_argument_list_p
5362 /* The nested-name-specifier is optional, so we parse
5364 cp_parser_parse_tentatively (parser
);
5366 /* Look for the optional `template' keyword, if this isn't the
5367 first time through the loop. */
5369 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5371 template_keyword_p
= false;
5373 /* Save the old scope since the name lookup we are about to do
5374 might destroy it. */
5375 old_scope
= parser
->scope
;
5376 saved_qualifying_scope
= parser
->qualifying_scope
;
5377 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5378 look up names in "X<T>::I" in order to determine that "Y" is
5379 a template. So, if we have a typename at this point, we make
5380 an effort to look through it. */
5382 && !typename_keyword_p
5384 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5385 parser
->scope
= resolve_typename_type (parser
->scope
,
5386 /*only_current_p=*/false);
5387 /* Parse the qualifying entity. */
5389 = cp_parser_qualifying_entity (parser
,
5395 /* Look for the `::' token. */
5396 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5398 /* If we found what we wanted, we keep going; otherwise, we're
5400 if (!cp_parser_parse_definitely (parser
))
5402 bool error_p
= false;
5404 /* Restore the OLD_SCOPE since it was valid before the
5405 failed attempt at finding the last
5406 class-or-namespace-name. */
5407 parser
->scope
= old_scope
;
5408 parser
->qualifying_scope
= saved_qualifying_scope
;
5410 /* If the next token is a decltype, and the one after that is a
5411 `::', then the decltype has failed to resolve to a class or
5412 enumeration type. Give this error even when parsing
5413 tentatively since it can't possibly be valid--and we're going
5414 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5415 won't get another chance.*/
5416 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5417 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5420 token
= cp_lexer_consume_token (parser
->lexer
);
5421 error_at (token
->location
, "decltype evaluates to %qT, "
5422 "which is not a class or enumeration type",
5424 parser
->scope
= error_mark_node
;
5428 cp_lexer_consume_token (parser
->lexer
);
5431 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5433 /* If the next token is an identifier, and the one after
5434 that is a `::', then any valid interpretation would have
5435 found a class-or-namespace-name. */
5436 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5437 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5439 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5442 token
= cp_lexer_consume_token (parser
->lexer
);
5445 if (!token
->error_reported
)
5448 tree ambiguous_decls
;
5450 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5452 /*is_template=*/false,
5453 /*is_namespace=*/false,
5454 /*check_dependency=*/true,
5457 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5458 error_at (token
->location
,
5459 "%qD used without template parameters",
5461 else if (ambiguous_decls
)
5463 // cp_parser_lookup_name has the same diagnostic,
5464 // thus make sure to emit it at most once.
5465 if (cp_parser_uncommitted_to_tentative_parse_p
5468 error_at (token
->location
,
5469 "reference to %qD is ambiguous",
5471 print_candidates (ambiguous_decls
);
5473 decl
= error_mark_node
;
5477 if (cxx_dialect
!= cxx98
)
5478 cp_parser_name_lookup_error
5479 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5482 cp_parser_name_lookup_error
5483 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5487 parser
->scope
= error_mark_node
;
5489 /* Treat this as a successful nested-name-specifier
5494 If the name found is not a class-name (clause
5495 _class_) or namespace-name (_namespace.def_), the
5496 program is ill-formed. */
5499 cp_lexer_consume_token (parser
->lexer
);
5503 /* We've found one valid nested-name-specifier. */
5505 /* Name lookup always gives us a DECL. */
5506 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5507 new_scope
= TREE_TYPE (new_scope
);
5508 /* Uses of "template" must be followed by actual templates. */
5509 if (template_keyword_p
5510 && !(CLASS_TYPE_P (new_scope
)
5511 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5512 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5513 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5514 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5515 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5516 == TEMPLATE_ID_EXPR
)))
5517 permerror (input_location
, TYPE_P (new_scope
)
5518 ? G_("%qT is not a template")
5519 : G_("%qD is not a template"),
5521 /* If it is a class scope, try to complete it; we are about to
5522 be looking up names inside the class. */
5523 if (TYPE_P (new_scope
)
5524 /* Since checking types for dependency can be expensive,
5525 avoid doing it if the type is already complete. */
5526 && !COMPLETE_TYPE_P (new_scope
)
5527 /* Do not try to complete dependent types. */
5528 && !dependent_type_p (new_scope
))
5530 new_scope
= complete_type (new_scope
);
5531 /* If it is a typedef to current class, use the current
5532 class instead, as the typedef won't have any names inside
5534 if (!COMPLETE_TYPE_P (new_scope
)
5535 && currently_open_class (new_scope
))
5536 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5538 /* Make sure we look in the right scope the next time through
5540 parser
->scope
= new_scope
;
5543 /* If parsing tentatively, replace the sequence of tokens that makes
5544 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5545 token. That way, should we re-parse the token stream, we will
5546 not have to repeat the effort required to do the parse, nor will
5547 we issue duplicate error messages. */
5548 if (success
&& start
)
5552 token
= cp_lexer_token_at (parser
->lexer
, start
);
5553 /* Reset the contents of the START token. */
5554 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5555 /* Retrieve any deferred checks. Do not pop this access checks yet
5556 so the memory will not be reclaimed during token replacing below. */
5557 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5558 token
->u
.tree_check_value
->value
= parser
->scope
;
5559 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5560 token
->u
.tree_check_value
->qualifying_scope
=
5561 parser
->qualifying_scope
;
5562 token
->keyword
= RID_MAX
;
5564 /* Purge all subsequent tokens. */
5565 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5569 pop_to_parent_deferring_access_checks ();
5571 return success
? parser
->scope
: NULL_TREE
;
5574 /* Parse a nested-name-specifier. See
5575 cp_parser_nested_name_specifier_opt for details. This function
5576 behaves identically, except that it will an issue an error if no
5577 nested-name-specifier is present. */
5580 cp_parser_nested_name_specifier (cp_parser
*parser
,
5581 bool typename_keyword_p
,
5582 bool check_dependency_p
,
5584 bool is_declaration
)
5588 /* Look for the nested-name-specifier. */
5589 scope
= cp_parser_nested_name_specifier_opt (parser
,
5594 /* If it was not present, issue an error message. */
5597 cp_parser_error (parser
, "expected nested-name-specifier");
5598 parser
->scope
= NULL_TREE
;
5604 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5605 this is either a class-name or a namespace-name (which corresponds
5606 to the class-or-namespace-name production in the grammar). For
5607 C++0x, it can also be a type-name that refers to an enumeration
5608 type or a simple-template-id.
5610 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5611 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5612 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5613 TYPE_P is TRUE iff the next name should be taken as a class-name,
5614 even the same name is declared to be another entity in the same
5617 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5618 specified by the class-or-namespace-name. If neither is found the
5619 ERROR_MARK_NODE is returned. */
5622 cp_parser_qualifying_entity (cp_parser
*parser
,
5623 bool typename_keyword_p
,
5624 bool template_keyword_p
,
5625 bool check_dependency_p
,
5627 bool is_declaration
)
5630 tree saved_qualifying_scope
;
5631 tree saved_object_scope
;
5634 bool successful_parse_p
;
5636 /* DR 743: decltype can appear in a nested-name-specifier. */
5637 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5639 scope
= cp_parser_decltype (parser
);
5640 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5641 && !MAYBE_CLASS_TYPE_P (scope
))
5643 cp_parser_simulate_error (parser
);
5644 return error_mark_node
;
5646 if (TYPE_NAME (scope
))
5647 scope
= TYPE_NAME (scope
);
5651 /* Before we try to parse the class-name, we must save away the
5652 current PARSER->SCOPE since cp_parser_class_name will destroy
5654 saved_scope
= parser
->scope
;
5655 saved_qualifying_scope
= parser
->qualifying_scope
;
5656 saved_object_scope
= parser
->object_scope
;
5657 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5658 there is no need to look for a namespace-name. */
5659 only_class_p
= template_keyword_p
5660 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5662 cp_parser_parse_tentatively (parser
);
5663 scope
= cp_parser_class_name (parser
,
5666 type_p
? class_type
: none_type
,
5668 /*class_head_p=*/false,
5670 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5671 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5673 && cxx_dialect
!= cxx98
5674 && !successful_parse_p
)
5676 /* Restore the saved scope. */
5677 parser
->scope
= saved_scope
;
5678 parser
->qualifying_scope
= saved_qualifying_scope
;
5679 parser
->object_scope
= saved_object_scope
;
5681 /* Parse tentatively. */
5682 cp_parser_parse_tentatively (parser
);
5684 /* Parse a type-name */
5685 scope
= cp_parser_type_name (parser
);
5687 /* "If the name found does not designate a namespace or a class,
5688 enumeration, or dependent type, the program is ill-formed."
5690 We cover classes and dependent types above and namespaces below,
5691 so this code is only looking for enums. */
5692 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5693 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5694 cp_parser_simulate_error (parser
);
5696 successful_parse_p
= cp_parser_parse_definitely (parser
);
5698 /* If that didn't work, try for a namespace-name. */
5699 if (!only_class_p
&& !successful_parse_p
)
5701 /* Restore the saved scope. */
5702 parser
->scope
= saved_scope
;
5703 parser
->qualifying_scope
= saved_qualifying_scope
;
5704 parser
->object_scope
= saved_object_scope
;
5705 /* If we are not looking at an identifier followed by the scope
5706 resolution operator, then this is not part of a
5707 nested-name-specifier. (Note that this function is only used
5708 to parse the components of a nested-name-specifier.) */
5709 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5710 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5711 return error_mark_node
;
5712 scope
= cp_parser_namespace_name (parser
);
5718 /* Return true if we are looking at a compound-literal, false otherwise. */
5721 cp_parser_compound_literal_p (cp_parser
*parser
)
5723 /* Consume the `('. */
5724 cp_lexer_consume_token (parser
->lexer
);
5726 cp_lexer_save_tokens (parser
->lexer
);
5728 /* Skip tokens until the next token is a closing parenthesis.
5729 If we find the closing `)', and the next token is a `{', then
5730 we are looking at a compound-literal. */
5731 bool compound_literal_p
5732 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5733 /*consume_paren=*/true)
5734 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5736 /* Roll back the tokens we skipped. */
5737 cp_lexer_rollback_tokens (parser
->lexer
);
5739 return compound_literal_p
;
5742 /* Parse a postfix-expression.
5746 postfix-expression [ expression ]
5747 postfix-expression ( expression-list [opt] )
5748 simple-type-specifier ( expression-list [opt] )
5749 typename :: [opt] nested-name-specifier identifier
5750 ( expression-list [opt] )
5751 typename :: [opt] nested-name-specifier template [opt] template-id
5752 ( expression-list [opt] )
5753 postfix-expression . template [opt] id-expression
5754 postfix-expression -> template [opt] id-expression
5755 postfix-expression . pseudo-destructor-name
5756 postfix-expression -> pseudo-destructor-name
5757 postfix-expression ++
5758 postfix-expression --
5759 dynamic_cast < type-id > ( expression )
5760 static_cast < type-id > ( expression )
5761 reinterpret_cast < type-id > ( expression )
5762 const_cast < type-id > ( expression )
5763 typeid ( expression )
5769 ( type-id ) { initializer-list , [opt] }
5771 This extension is a GNU version of the C99 compound-literal
5772 construct. (The C99 grammar uses `type-name' instead of `type-id',
5773 but they are essentially the same concept.)
5775 If ADDRESS_P is true, the postfix expression is the operand of the
5776 `&' operator. CAST_P is true if this expression is the target of a
5779 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5780 class member access expressions [expr.ref].
5782 Returns a representation of the expression. */
5785 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5786 bool member_access_only_p
, bool decltype_p
,
5787 cp_id_kind
* pidk_return
)
5792 cp_id_kind idk
= CP_ID_KIND_NONE
;
5793 tree postfix_expression
= NULL_TREE
;
5794 bool is_member_access
= false;
5795 int saved_in_statement
= -1;
5797 /* Peek at the next token. */
5798 token
= cp_lexer_peek_token (parser
->lexer
);
5799 loc
= token
->location
;
5800 /* Some of the productions are determined by keywords. */
5801 keyword
= token
->keyword
;
5811 const char *saved_message
;
5812 bool saved_in_type_id_in_expr_p
;
5814 /* All of these can be handled in the same way from the point
5815 of view of parsing. Begin by consuming the token
5816 identifying the cast. */
5817 cp_lexer_consume_token (parser
->lexer
);
5819 /* New types cannot be defined in the cast. */
5820 saved_message
= parser
->type_definition_forbidden_message
;
5821 parser
->type_definition_forbidden_message
5822 = G_("types may not be defined in casts");
5824 /* Look for the opening `<'. */
5825 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5826 /* Parse the type to which we are casting. */
5827 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5828 parser
->in_type_id_in_expr_p
= true;
5829 type
= cp_parser_type_id (parser
);
5830 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5831 /* Look for the closing `>'. */
5832 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5833 /* Restore the old message. */
5834 parser
->type_definition_forbidden_message
= saved_message
;
5836 bool saved_greater_than_is_operator_p
5837 = parser
->greater_than_is_operator_p
;
5838 parser
->greater_than_is_operator_p
= true;
5840 /* And the expression which is being cast. */
5841 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5842 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5843 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5845 parser
->greater_than_is_operator_p
5846 = saved_greater_than_is_operator_p
;
5848 /* Only type conversions to integral or enumeration types
5849 can be used in constant-expressions. */
5850 if (!cast_valid_in_integral_constant_expression_p (type
)
5851 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5852 return error_mark_node
;
5858 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5862 = build_static_cast (type
, expression
, tf_warning_or_error
);
5866 = build_reinterpret_cast (type
, expression
,
5867 tf_warning_or_error
);
5871 = build_const_cast (type
, expression
, tf_warning_or_error
);
5882 const char *saved_message
;
5883 bool saved_in_type_id_in_expr_p
;
5885 /* Consume the `typeid' token. */
5886 cp_lexer_consume_token (parser
->lexer
);
5887 /* Look for the `(' token. */
5888 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5889 /* Types cannot be defined in a `typeid' expression. */
5890 saved_message
= parser
->type_definition_forbidden_message
;
5891 parser
->type_definition_forbidden_message
5892 = G_("types may not be defined in a %<typeid%> expression");
5893 /* We can't be sure yet whether we're looking at a type-id or an
5895 cp_parser_parse_tentatively (parser
);
5896 /* Try a type-id first. */
5897 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5898 parser
->in_type_id_in_expr_p
= true;
5899 type
= cp_parser_type_id (parser
);
5900 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5901 /* Look for the `)' token. Otherwise, we can't be sure that
5902 we're not looking at an expression: consider `typeid (int
5903 (3))', for example. */
5904 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5905 /* If all went well, simply lookup the type-id. */
5906 if (cp_parser_parse_definitely (parser
))
5907 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5908 /* Otherwise, fall back to the expression variant. */
5913 /* Look for an expression. */
5914 expression
= cp_parser_expression (parser
, & idk
);
5915 /* Compute its typeid. */
5916 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5917 /* Look for the `)' token. */
5918 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5920 /* Restore the saved message. */
5921 parser
->type_definition_forbidden_message
= saved_message
;
5922 /* `typeid' may not appear in an integral constant expression. */
5923 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5924 return error_mark_node
;
5931 /* The syntax permitted here is the same permitted for an
5932 elaborated-type-specifier. */
5933 type
= cp_parser_elaborated_type_specifier (parser
,
5934 /*is_friend=*/false,
5935 /*is_declaration=*/false);
5936 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5940 case RID_CILK_SPAWN
:
5942 cp_lexer_consume_token (parser
->lexer
);
5943 token
= cp_lexer_peek_token (parser
->lexer
);
5944 if (token
->type
== CPP_SEMICOLON
)
5946 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5948 postfix_expression
= error_mark_node
;
5951 else if (!current_function_decl
)
5953 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5954 "inside a function");
5955 postfix_expression
= error_mark_node
;
5960 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5961 saved_in_statement
= parser
->in_statement
;
5962 parser
->in_statement
|= IN_CILK_SPAWN
;
5964 cfun
->calls_cilk_spawn
= 1;
5965 postfix_expression
=
5966 cp_parser_postfix_expression (parser
, false, false,
5967 false, false, &idk
);
5970 error_at (token
->location
, "-fcilkplus must be enabled to use"
5971 " %<_Cilk_spawn%>");
5972 cfun
->calls_cilk_spawn
= 0;
5974 else if (saved_in_statement
& IN_CILK_SPAWN
)
5976 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
5977 "are not permitted");
5978 postfix_expression
= error_mark_node
;
5979 cfun
->calls_cilk_spawn
= 0;
5983 postfix_expression
= build_cilk_spawn (token
->location
,
5984 postfix_expression
);
5985 if (postfix_expression
!= error_mark_node
)
5986 SET_EXPR_LOCATION (postfix_expression
, input_location
);
5987 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
5992 case RID_BUILTIN_SHUFFLE
:
5994 vec
<tree
, va_gc
> *vec
;
5998 cp_lexer_consume_token (parser
->lexer
);
5999 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6000 /*cast_p=*/false, /*allow_expansion_p=*/true,
6001 /*non_constant_p=*/NULL
);
6003 return error_mark_node
;
6005 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6008 if (vec
->length () == 2)
6009 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6010 tf_warning_or_error
);
6011 else if (vec
->length () == 3)
6012 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6013 tf_warning_or_error
);
6016 error_at (loc
, "wrong number of arguments to "
6017 "%<__builtin_shuffle%>");
6018 return error_mark_node
;
6027 /* If the next thing is a simple-type-specifier, we may be
6028 looking at a functional cast. We could also be looking at
6029 an id-expression. So, we try the functional cast, and if
6030 that doesn't work we fall back to the primary-expression. */
6031 cp_parser_parse_tentatively (parser
);
6032 /* Look for the simple-type-specifier. */
6033 type
= cp_parser_simple_type_specifier (parser
,
6034 /*decl_specs=*/NULL
,
6035 CP_PARSER_FLAGS_NONE
);
6036 /* Parse the cast itself. */
6037 if (!cp_parser_error_occurred (parser
))
6039 = cp_parser_functional_cast (parser
, type
);
6040 /* If that worked, we're done. */
6041 if (cp_parser_parse_definitely (parser
))
6044 /* If the functional-cast didn't work out, try a
6045 compound-literal. */
6046 if (cp_parser_allow_gnu_extensions_p (parser
)
6047 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6049 tree initializer
= NULL_TREE
;
6051 cp_parser_parse_tentatively (parser
);
6053 /* Avoid calling cp_parser_type_id pointlessly, see comment
6054 in cp_parser_cast_expression about c++/29234. */
6055 if (!cp_parser_compound_literal_p (parser
))
6056 cp_parser_simulate_error (parser
);
6059 /* Parse the type. */
6060 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6061 parser
->in_type_id_in_expr_p
= true;
6062 type
= cp_parser_type_id (parser
);
6063 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6064 /* Look for the `)'. */
6065 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6068 /* If things aren't going well, there's no need to
6070 if (!cp_parser_error_occurred (parser
))
6072 bool non_constant_p
;
6073 /* Parse the brace-enclosed initializer list. */
6074 initializer
= cp_parser_braced_list (parser
,
6077 /* If that worked, we're definitely looking at a
6078 compound-literal expression. */
6079 if (cp_parser_parse_definitely (parser
))
6081 /* Warn the user that a compound literal is not
6082 allowed in standard C++. */
6083 pedwarn (input_location
, OPT_Wpedantic
,
6084 "ISO C++ forbids compound-literals");
6085 /* For simplicity, we disallow compound literals in
6086 constant-expressions. We could
6087 allow compound literals of integer type, whose
6088 initializer was a constant, in constant
6089 expressions. Permitting that usage, as a further
6090 extension, would not change the meaning of any
6091 currently accepted programs. (Of course, as
6092 compound literals are not part of ISO C++, the
6093 standard has nothing to say.) */
6094 if (cp_parser_non_integral_constant_expression (parser
,
6097 postfix_expression
= error_mark_node
;
6100 /* Form the representation of the compound-literal. */
6102 = finish_compound_literal (type
, initializer
,
6103 tf_warning_or_error
);
6108 /* It must be a primary-expression. */
6110 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6111 /*template_arg_p=*/false,
6118 /* Note that we don't need to worry about calling build_cplus_new on a
6119 class-valued CALL_EXPR in decltype when it isn't the end of the
6120 postfix-expression; unary_complex_lvalue will take care of that for
6123 /* Keep looping until the postfix-expression is complete. */
6126 if (idk
== CP_ID_KIND_UNQUALIFIED
6127 && identifier_p (postfix_expression
)
6128 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6129 /* It is not a Koenig lookup function call. */
6131 = unqualified_name_lookup_error (postfix_expression
);
6133 /* Peek at the next token. */
6134 token
= cp_lexer_peek_token (parser
->lexer
);
6136 switch (token
->type
)
6138 case CPP_OPEN_SQUARE
:
6139 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6141 cp_parser_error (parser
,
6142 "two consecutive %<[%> shall "
6143 "only introduce an attribute");
6144 return error_mark_node
;
6147 = cp_parser_postfix_open_square_expression (parser
,
6151 idk
= CP_ID_KIND_NONE
;
6152 is_member_access
= false;
6155 case CPP_OPEN_PAREN
:
6156 /* postfix-expression ( expression-list [opt] ) */
6159 bool is_builtin_constant_p
;
6160 bool saved_integral_constant_expression_p
= false;
6161 bool saved_non_integral_constant_expression_p
= false;
6162 tsubst_flags_t complain
= complain_flags (decltype_p
);
6163 vec
<tree
, va_gc
> *args
;
6165 is_member_access
= false;
6167 is_builtin_constant_p
6168 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6169 if (is_builtin_constant_p
)
6171 /* The whole point of __builtin_constant_p is to allow
6172 non-constant expressions to appear as arguments. */
6173 saved_integral_constant_expression_p
6174 = parser
->integral_constant_expression_p
;
6175 saved_non_integral_constant_expression_p
6176 = parser
->non_integral_constant_expression_p
;
6177 parser
->integral_constant_expression_p
= false;
6179 args
= (cp_parser_parenthesized_expression_list
6181 /*cast_p=*/false, /*allow_expansion_p=*/true,
6182 /*non_constant_p=*/NULL
,
6183 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6184 if (is_builtin_constant_p
)
6186 parser
->integral_constant_expression_p
6187 = saved_integral_constant_expression_p
;
6188 parser
->non_integral_constant_expression_p
6189 = saved_non_integral_constant_expression_p
;
6194 postfix_expression
= error_mark_node
;
6198 /* Function calls are not permitted in
6199 constant-expressions. */
6200 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6201 && cp_parser_non_integral_constant_expression (parser
,
6204 postfix_expression
= error_mark_node
;
6205 release_tree_vector (args
);
6210 if (idk
== CP_ID_KIND_UNQUALIFIED
6211 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6213 if (identifier_p (postfix_expression
))
6215 if (!args
->is_empty ())
6218 if (!any_type_dependent_arguments_p (args
))
6220 = perform_koenig_lookup (postfix_expression
, args
,
6225 = unqualified_fn_lookup_error (postfix_expression
);
6227 /* We do not perform argument-dependent lookup if
6228 normal lookup finds a non-function, in accordance
6229 with the expected resolution of DR 218. */
6230 else if (!args
->is_empty ()
6231 && is_overloaded_fn (postfix_expression
))
6233 tree fn
= get_first_fn (postfix_expression
);
6234 fn
= STRIP_TEMPLATE (fn
);
6236 /* Do not do argument dependent lookup if regular
6237 lookup finds a member function or a block-scope
6238 function declaration. [basic.lookup.argdep]/3 */
6239 if (!DECL_FUNCTION_MEMBER_P (fn
)
6240 && !DECL_LOCAL_FUNCTION_P (fn
))
6243 if (!any_type_dependent_arguments_p (args
))
6245 = perform_koenig_lookup (postfix_expression
, args
,
6251 if (warn_memset_transposed_args
)
6253 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6254 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6255 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6256 && vec_safe_length (args
) == 3
6257 && integer_zerop ((*args
)[2])
6258 && LITERAL_ZERO_P ((*args
)[2])
6259 && !(integer_zerop ((*args
)[1])
6260 && LITERAL_ZERO_P ((*args
)[1])))
6261 warning (OPT_Wmemset_transposed_args
,
6262 "%<memset%> used with constant zero length "
6263 "parameter; this could be due to transposed "
6266 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6267 to avoid leaking those into folder and middle-end. */
6270 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6271 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6272 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6275 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6277 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6278 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6280 if (processing_template_decl
6281 && (type_dependent_expression_p (instance
)
6282 || (!BASELINK_P (fn
)
6283 && TREE_CODE (fn
) != FIELD_DECL
)
6284 || type_dependent_expression_p (fn
)
6285 || any_type_dependent_arguments_p (args
)))
6288 = build_nt_call_vec (postfix_expression
, args
);
6289 release_tree_vector (args
);
6293 if (BASELINK_P (fn
))
6296 = (build_new_method_call
6297 (instance
, fn
, &args
, NULL_TREE
,
6298 (idk
== CP_ID_KIND_QUALIFIED
6299 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6306 = finish_call_expr (postfix_expression
, &args
,
6307 /*disallow_virtual=*/false,
6311 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6312 || TREE_CODE (postfix_expression
) == MEMBER_REF
6313 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6314 postfix_expression
= (build_offset_ref_call_from_tree
6315 (postfix_expression
, &args
,
6317 else if (idk
== CP_ID_KIND_QUALIFIED
)
6318 /* A call to a static class member, or a namespace-scope
6321 = finish_call_expr (postfix_expression
, &args
,
6322 /*disallow_virtual=*/true,
6326 /* All other function calls. */
6328 = finish_call_expr (postfix_expression
, &args
,
6329 /*disallow_virtual=*/false,
6333 protected_set_expr_location (postfix_expression
, token
->location
);
6335 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6336 idk
= CP_ID_KIND_NONE
;
6338 release_tree_vector (args
);
6344 /* postfix-expression . template [opt] id-expression
6345 postfix-expression . pseudo-destructor-name
6346 postfix-expression -> template [opt] id-expression
6347 postfix-expression -> pseudo-destructor-name */
6349 /* Consume the `.' or `->' operator. */
6350 cp_lexer_consume_token (parser
->lexer
);
6353 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6357 is_member_access
= true;
6361 /* postfix-expression ++ */
6362 /* Consume the `++' token. */
6363 cp_lexer_consume_token (parser
->lexer
);
6364 /* Generate a representation for the complete expression. */
6366 = finish_increment_expr (postfix_expression
,
6367 POSTINCREMENT_EXPR
);
6368 /* Increments may not appear in constant-expressions. */
6369 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6370 postfix_expression
= error_mark_node
;
6371 idk
= CP_ID_KIND_NONE
;
6372 is_member_access
= false;
6375 case CPP_MINUS_MINUS
:
6376 /* postfix-expression -- */
6377 /* Consume the `--' token. */
6378 cp_lexer_consume_token (parser
->lexer
);
6379 /* Generate a representation for the complete expression. */
6381 = finish_increment_expr (postfix_expression
,
6382 POSTDECREMENT_EXPR
);
6383 /* Decrements may not appear in constant-expressions. */
6384 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6385 postfix_expression
= error_mark_node
;
6386 idk
= CP_ID_KIND_NONE
;
6387 is_member_access
= false;
6391 if (pidk_return
!= NULL
)
6392 * pidk_return
= idk
;
6393 if (member_access_only_p
)
6394 return is_member_access
? postfix_expression
: error_mark_node
;
6396 return postfix_expression
;
6400 /* We should never get here. */
6402 return error_mark_node
;
6405 /* This function parses Cilk Plus array notations. If a normal array expr. is
6406 parsed then the array index is passed back to the caller through *INIT_INDEX
6407 and the function returns a NULL_TREE. If array notation expr. is parsed,
6408 then *INIT_INDEX is ignored by the caller and the function returns
6409 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6413 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6416 cp_token
*token
= NULL
;
6417 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6418 if (!array_value
|| array_value
== error_mark_node
)
6420 cp_parser_skip_to_end_of_statement (parser
);
6421 return error_mark_node
;
6424 array_type
= TREE_TYPE (array_value
);
6426 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6427 parser
->colon_corrects_to_scope_p
= false;
6428 token
= cp_lexer_peek_token (parser
->lexer
);
6432 cp_parser_error (parser
, "expected %<:%> or numeral");
6433 return error_mark_node
;
6435 else if (token
->type
== CPP_COLON
)
6437 /* Consume the ':'. */
6438 cp_lexer_consume_token (parser
->lexer
);
6440 /* If we are here, then we have a case like this A[:]. */
6441 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6443 cp_parser_error (parser
, "expected %<]%>");
6444 cp_parser_skip_to_end_of_statement (parser
);
6445 return error_mark_node
;
6447 *init_index
= NULL_TREE
;
6449 length_index
= NULL_TREE
;
6453 /* If we are here, then there are three valid possibilities:
6455 2. ARRAY [ EXP : EXP ]
6456 3. ARRAY [ EXP : EXP : EXP ] */
6458 *init_index
= cp_parser_expression (parser
);
6459 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6461 /* This indicates that we have a normal array expression. */
6462 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6466 /* Consume the ':'. */
6467 cp_lexer_consume_token (parser
->lexer
);
6468 length_index
= cp_parser_expression (parser
);
6469 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6471 cp_lexer_consume_token (parser
->lexer
);
6472 stride
= cp_parser_expression (parser
);
6475 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6477 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6478 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6480 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6481 cp_lexer_consume_token (parser
->lexer
);
6482 return error_mark_node
;
6484 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6486 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6487 length_index
, stride
, array_type
);
6491 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6492 by cp_parser_builtin_offsetof. We're looking for
6494 postfix-expression [ expression ]
6495 postfix-expression [ braced-init-list ] (C++11)
6497 FOR_OFFSETOF is set if we're being called in that context, which
6498 changes how we deal with integer constant expressions. */
6501 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6502 tree postfix_expression
,
6506 tree index
= NULL_TREE
;
6507 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6508 bool saved_greater_than_is_operator_p
;
6510 /* Consume the `[' token. */
6511 cp_lexer_consume_token (parser
->lexer
);
6513 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6514 parser
->greater_than_is_operator_p
= true;
6516 /* Parse the index expression. */
6517 /* ??? For offsetof, there is a question of what to allow here. If
6518 offsetof is not being used in an integral constant expression context,
6519 then we *could* get the right answer by computing the value at runtime.
6520 If we are in an integral constant expression context, then we might
6521 could accept any constant expression; hard to say without analysis.
6522 Rather than open the barn door too wide right away, allow only integer
6523 constant expressions here. */
6525 index
= cp_parser_constant_expression (parser
);
6528 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6530 bool expr_nonconst_p
;
6531 cp_lexer_set_source_position (parser
->lexer
);
6532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6533 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6535 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6537 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6538 "braced list index is not allowed with array "
6540 cp_parser_skip_to_end_of_statement (parser
);
6541 return error_mark_node
;
6544 else if (flag_cilkplus
)
6546 /* Here are have these two options:
6547 ARRAY[EXP : EXP] - Array notation expr with default
6549 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6551 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6552 postfix_expression
);
6557 index
= cp_parser_expression (parser
);
6560 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6562 /* Look for the closing `]'. */
6563 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6565 /* Build the ARRAY_REF. */
6566 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6569 /* When not doing offsetof, array references are not permitted in
6570 constant-expressions. */
6572 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6573 postfix_expression
= error_mark_node
;
6575 return postfix_expression
;
6578 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6579 by cp_parser_builtin_offsetof. We're looking for
6581 postfix-expression . template [opt] id-expression
6582 postfix-expression . pseudo-destructor-name
6583 postfix-expression -> template [opt] id-expression
6584 postfix-expression -> pseudo-destructor-name
6586 FOR_OFFSETOF is set if we're being called in that context. That sorta
6587 limits what of the above we'll actually accept, but nevermind.
6588 TOKEN_TYPE is the "." or "->" token, which will already have been
6589 removed from the stream. */
6592 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6593 enum cpp_ttype token_type
,
6594 tree postfix_expression
,
6595 bool for_offsetof
, cp_id_kind
*idk
,
6596 location_t location
)
6600 bool pseudo_destructor_p
;
6601 tree scope
= NULL_TREE
;
6603 /* If this is a `->' operator, dereference the pointer. */
6604 if (token_type
== CPP_DEREF
)
6605 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6606 tf_warning_or_error
);
6607 /* Check to see whether or not the expression is type-dependent. */
6608 dependent_p
= type_dependent_expression_p (postfix_expression
);
6609 /* The identifier following the `->' or `.' is not qualified. */
6610 parser
->scope
= NULL_TREE
;
6611 parser
->qualifying_scope
= NULL_TREE
;
6612 parser
->object_scope
= NULL_TREE
;
6613 *idk
= CP_ID_KIND_NONE
;
6615 /* Enter the scope corresponding to the type of the object
6616 given by the POSTFIX_EXPRESSION. */
6617 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6619 scope
= TREE_TYPE (postfix_expression
);
6620 /* According to the standard, no expression should ever have
6621 reference type. Unfortunately, we do not currently match
6622 the standard in this respect in that our internal representation
6623 of an expression may have reference type even when the standard
6624 says it does not. Therefore, we have to manually obtain the
6625 underlying type here. */
6626 scope
= non_reference (scope
);
6627 /* The type of the POSTFIX_EXPRESSION must be complete. */
6628 if (scope
== unknown_type_node
)
6630 error_at (location
, "%qE does not have class type",
6631 postfix_expression
);
6634 /* Unlike the object expression in other contexts, *this is not
6635 required to be of complete type for purposes of class member
6636 access (5.2.5) outside the member function body. */
6637 else if (postfix_expression
!= current_class_ref
6638 && !(processing_template_decl
&& scope
== current_class_type
))
6639 scope
= complete_type_or_else (scope
, NULL_TREE
);
6640 /* Let the name lookup machinery know that we are processing a
6641 class member access expression. */
6642 parser
->context
->object_type
= scope
;
6643 /* If something went wrong, we want to be able to discern that case,
6644 as opposed to the case where there was no SCOPE due to the type
6645 of expression being dependent. */
6647 scope
= error_mark_node
;
6648 /* If the SCOPE was erroneous, make the various semantic analysis
6649 functions exit quickly -- and without issuing additional error
6651 if (scope
== error_mark_node
)
6652 postfix_expression
= error_mark_node
;
6655 /* Assume this expression is not a pseudo-destructor access. */
6656 pseudo_destructor_p
= false;
6658 /* If the SCOPE is a scalar type, then, if this is a valid program,
6659 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6660 is type dependent, it can be pseudo-destructor-name or something else.
6661 Try to parse it as pseudo-destructor-name first. */
6662 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6667 cp_parser_parse_tentatively (parser
);
6668 /* Parse the pseudo-destructor-name. */
6670 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6673 && (cp_parser_error_occurred (parser
)
6674 || !SCALAR_TYPE_P (type
)))
6675 cp_parser_abort_tentative_parse (parser
);
6676 else if (cp_parser_parse_definitely (parser
))
6678 pseudo_destructor_p
= true;
6680 = finish_pseudo_destructor_expr (postfix_expression
,
6685 if (!pseudo_destructor_p
)
6687 /* If the SCOPE is not a scalar type, we are looking at an
6688 ordinary class member access expression, rather than a
6689 pseudo-destructor-name. */
6691 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6692 /* Parse the id-expression. */
6693 name
= (cp_parser_id_expression
6695 cp_parser_optional_template_keyword (parser
),
6696 /*check_dependency_p=*/true,
6698 /*declarator_p=*/false,
6699 /*optional_p=*/false));
6700 /* In general, build a SCOPE_REF if the member name is qualified.
6701 However, if the name was not dependent and has already been
6702 resolved; there is no need to build the SCOPE_REF. For example;
6704 struct X { void f(); };
6705 template <typename T> void f(T* t) { t->X::f(); }
6707 Even though "t" is dependent, "X::f" is not and has been resolved
6708 to a BASELINK; there is no need to include scope information. */
6710 /* But we do need to remember that there was an explicit scope for
6711 virtual function calls. */
6713 *idk
= CP_ID_KIND_QUALIFIED
;
6715 /* If the name is a template-id that names a type, we will get a
6716 TYPE_DECL here. That is invalid code. */
6717 if (TREE_CODE (name
) == TYPE_DECL
)
6719 error_at (token
->location
, "invalid use of %qD", name
);
6720 postfix_expression
= error_mark_node
;
6724 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6726 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6728 error_at (token
->location
, "%<%D::%D%> is not a class member",
6729 parser
->scope
, name
);
6730 postfix_expression
= error_mark_node
;
6733 name
= build_qualified_name (/*type=*/NULL_TREE
,
6737 parser
->scope
= NULL_TREE
;
6738 parser
->qualifying_scope
= NULL_TREE
;
6739 parser
->object_scope
= NULL_TREE
;
6741 if (parser
->scope
&& name
&& BASELINK_P (name
))
6742 adjust_result_of_qualified_name_lookup
6743 (name
, parser
->scope
, scope
);
6745 = finish_class_member_access_expr (postfix_expression
, name
,
6747 tf_warning_or_error
);
6751 /* We no longer need to look up names in the scope of the object on
6752 the left-hand side of the `.' or `->' operator. */
6753 parser
->context
->object_type
= NULL_TREE
;
6755 /* Outside of offsetof, these operators may not appear in
6756 constant-expressions. */
6758 && (cp_parser_non_integral_constant_expression
6759 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6760 postfix_expression
= error_mark_node
;
6762 return postfix_expression
;
6765 /* Cache of LITERAL_ZERO_P constants. */
6767 static GTY(()) tree literal_zeros
[itk_none
];
6769 /* Parse a parenthesized expression-list.
6772 assignment-expression
6773 expression-list, assignment-expression
6778 identifier, expression-list
6780 CAST_P is true if this expression is the target of a cast.
6782 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6785 Returns a vector of trees. Each element is a representation of an
6786 assignment-expression. NULL is returned if the ( and or ) are
6787 missing. An empty, but allocated, vector is returned on no
6788 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6789 if we are parsing an attribute list for an attribute that wants a
6790 plain identifier argument, normal_attr for an attribute that wants
6791 an expression, or non_attr if we aren't parsing an attribute list. If
6792 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6793 not all of the expressions in the list were constant.
6794 WANT_LITERAL_ZERO_P is true if the caller is interested in
6795 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6796 immediately, this can be removed. */
6798 static vec
<tree
, va_gc
> *
6799 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6800 int is_attribute_list
,
6802 bool allow_expansion_p
,
6803 bool *non_constant_p
,
6804 bool want_literal_zero_p
)
6806 vec
<tree
, va_gc
> *expression_list
;
6807 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6808 tree identifier
= NULL_TREE
;
6809 bool saved_greater_than_is_operator_p
;
6811 /* Assume all the expressions will be constant. */
6813 *non_constant_p
= false;
6815 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6818 expression_list
= make_tree_vector ();
6820 /* Within a parenthesized expression, a `>' token is always
6821 the greater-than operator. */
6822 saved_greater_than_is_operator_p
6823 = parser
->greater_than_is_operator_p
;
6824 parser
->greater_than_is_operator_p
= true;
6826 /* Consume expressions until there are no more. */
6827 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6832 /* At the beginning of attribute lists, check to see if the
6833 next token is an identifier. */
6834 if (is_attribute_list
== id_attr
6835 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6839 /* Consume the identifier. */
6840 token
= cp_lexer_consume_token (parser
->lexer
);
6841 /* Save the identifier. */
6842 identifier
= token
->u
.value
;
6846 bool expr_non_constant_p
;
6848 /* Parse the next assignment-expression. */
6849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6851 /* A braced-init-list. */
6852 cp_lexer_set_source_position (parser
->lexer
);
6853 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6854 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6855 if (non_constant_p
&& expr_non_constant_p
)
6856 *non_constant_p
= true;
6858 else if (non_constant_p
)
6860 expr
= (cp_parser_constant_expression
6861 (parser
, /*allow_non_constant_p=*/true,
6862 &expr_non_constant_p
));
6863 if (expr_non_constant_p
)
6864 *non_constant_p
= true;
6869 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6877 /* If a parameter is literal zero alone, remember it
6878 for -Wmemset-transposed-args warning. */
6879 if (integer_zerop (tok
->u
.value
)
6880 && !TREE_OVERFLOW (tok
->u
.value
)
6881 && want_literal_zero_p
6882 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6884 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6885 == CPP_CLOSE_PAREN
))
6888 for (i
= 0; i
< itk_none
; ++i
)
6889 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6891 if (i
< itk_none
&& literal_zeros
[i
])
6892 expr
= literal_zeros
[i
];
6895 expr
= copy_node (tok
->u
.value
);
6896 LITERAL_ZERO_P (expr
) = 1;
6898 literal_zeros
[i
] = expr
;
6900 /* Consume the 0 token (or '\0', 0LL etc.). */
6901 cp_lexer_consume_token (parser
->lexer
);
6907 if (expr
== NULL_TREE
)
6908 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6913 expr
= instantiate_non_dependent_expr (expr
);
6915 /* If we have an ellipsis, then this is an expression
6917 if (allow_expansion_p
6918 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6920 /* Consume the `...'. */
6921 cp_lexer_consume_token (parser
->lexer
);
6923 /* Build the argument pack. */
6924 expr
= make_pack_expansion (expr
);
6927 /* Add it to the list. We add error_mark_node
6928 expressions to the list, so that we can still tell if
6929 the correct form for a parenthesized expression-list
6930 is found. That gives better errors. */
6931 vec_safe_push (expression_list
, expr
);
6933 if (expr
== error_mark_node
)
6937 /* After the first item, attribute lists look the same as
6938 expression lists. */
6939 is_attribute_list
= non_attr
;
6942 /* If the next token isn't a `,', then we are done. */
6943 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6946 /* Otherwise, consume the `,' and keep going. */
6947 cp_lexer_consume_token (parser
->lexer
);
6950 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6955 /* We try and resync to an unnested comma, as that will give the
6956 user better diagnostics. */
6957 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6958 /*recovering=*/true,
6960 /*consume_paren=*/true);
6965 parser
->greater_than_is_operator_p
6966 = saved_greater_than_is_operator_p
;
6971 parser
->greater_than_is_operator_p
6972 = saved_greater_than_is_operator_p
;
6975 vec_safe_insert (expression_list
, 0, identifier
);
6977 return expression_list
;
6980 /* Parse a pseudo-destructor-name.
6982 pseudo-destructor-name:
6983 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6984 :: [opt] nested-name-specifier template template-id :: ~ type-name
6985 :: [opt] nested-name-specifier [opt] ~ type-name
6987 If either of the first two productions is used, sets *SCOPE to the
6988 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6989 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6990 or ERROR_MARK_NODE if the parse fails. */
6993 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6998 bool nested_name_specifier_p
;
7001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7002 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7003 && !type_dependent_expression_p (object
))
7005 if (cxx_dialect
< cxx14
)
7006 pedwarn (input_location
, 0,
7007 "%<~auto%> only available with "
7008 "-std=c++14 or -std=gnu++14");
7009 cp_lexer_consume_token (parser
->lexer
);
7010 cp_lexer_consume_token (parser
->lexer
);
7012 *type
= TREE_TYPE (object
);
7016 /* Assume that things will not work out. */
7017 *type
= error_mark_node
;
7019 /* Look for the optional `::' operator. */
7020 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7021 /* Look for the optional nested-name-specifier. */
7022 nested_name_specifier_p
7023 = (cp_parser_nested_name_specifier_opt (parser
,
7024 /*typename_keyword_p=*/false,
7025 /*check_dependency_p=*/true,
7027 /*is_declaration=*/false)
7029 /* Now, if we saw a nested-name-specifier, we might be doing the
7030 second production. */
7031 if (nested_name_specifier_p
7032 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7034 /* Consume the `template' keyword. */
7035 cp_lexer_consume_token (parser
->lexer
);
7036 /* Parse the template-id. */
7037 cp_parser_template_id (parser
,
7038 /*template_keyword_p=*/true,
7039 /*check_dependency_p=*/false,
7041 /*is_declaration=*/true);
7042 /* Look for the `::' token. */
7043 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7045 /* If the next token is not a `~', then there might be some
7046 additional qualification. */
7047 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7049 /* At this point, we're looking for "type-name :: ~". The type-name
7050 must not be a class-name, since this is a pseudo-destructor. So,
7051 it must be either an enum-name, or a typedef-name -- both of which
7052 are just identifiers. So, we peek ahead to check that the "::"
7053 and "~" tokens are present; if they are not, then we can avoid
7054 calling type_name. */
7055 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7056 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7057 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7059 cp_parser_error (parser
, "non-scalar type");
7063 /* Look for the type-name. */
7064 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7065 if (*scope
== error_mark_node
)
7068 /* Look for the `::' token. */
7069 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7074 /* Look for the `~'. */
7075 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7077 /* Once we see the ~, this has to be a pseudo-destructor. */
7078 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7079 cp_parser_commit_to_topmost_tentative_parse (parser
);
7081 /* Look for the type-name again. We are not responsible for
7082 checking that it matches the first type-name. */
7083 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7086 /* Parse a unary-expression.
7092 unary-operator cast-expression
7093 sizeof unary-expression
7095 alignof ( type-id ) [C++0x]
7102 __extension__ cast-expression
7103 __alignof__ unary-expression
7104 __alignof__ ( type-id )
7105 alignof unary-expression [C++0x]
7106 __real__ cast-expression
7107 __imag__ cast-expression
7109 sizeof ( type-id ) { initializer-list , [opt] }
7110 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7111 __alignof__ ( type-id ) { initializer-list , [opt] }
7113 ADDRESS_P is true iff the unary-expression is appearing as the
7114 operand of the `&' operator. CAST_P is true if this expression is
7115 the target of a cast.
7117 Returns a representation of the expression. */
7120 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7121 bool address_p
, bool cast_p
, bool decltype_p
)
7124 enum tree_code unary_operator
;
7126 /* Peek at the next token. */
7127 token
= cp_lexer_peek_token (parser
->lexer
);
7128 /* Some keywords give away the kind of expression. */
7129 if (token
->type
== CPP_KEYWORD
)
7131 enum rid keyword
= token
->keyword
;
7140 location_t first_loc
;
7142 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7143 /* Consume the token. */
7144 cp_lexer_consume_token (parser
->lexer
);
7145 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7146 /* Parse the operand. */
7147 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7149 if (TYPE_P (operand
))
7150 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7153 /* ISO C++ defines alignof only with types, not with
7154 expressions. So pedwarn if alignof is used with a non-
7155 type expression. However, __alignof__ is ok. */
7156 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7157 pedwarn (token
->location
, OPT_Wpedantic
,
7158 "ISO C++ does not allow %<alignof%> "
7161 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7163 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7164 SIZEOF_EXPR with the original operand. */
7165 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7167 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7169 if (!processing_template_decl
&& TYPE_P (operand
))
7171 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7172 build1 (NOP_EXPR
, operand
,
7174 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7177 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7178 TREE_SIDE_EFFECTS (ret
) = 0;
7179 TREE_READONLY (ret
) = 1;
7181 SET_EXPR_LOCATION (ret
, first_loc
);
7187 return cp_parser_new_expression (parser
);
7190 return cp_parser_delete_expression (parser
);
7194 /* The saved value of the PEDANTIC flag. */
7198 /* Save away the PEDANTIC flag. */
7199 cp_parser_extension_opt (parser
, &saved_pedantic
);
7200 /* Parse the cast-expression. */
7201 expr
= cp_parser_simple_cast_expression (parser
);
7202 /* Restore the PEDANTIC flag. */
7203 pedantic
= saved_pedantic
;
7213 /* Consume the `__real__' or `__imag__' token. */
7214 cp_lexer_consume_token (parser
->lexer
);
7215 /* Parse the cast-expression. */
7216 expression
= cp_parser_simple_cast_expression (parser
);
7217 /* Create the complete representation. */
7218 return build_x_unary_op (token
->location
,
7219 (keyword
== RID_REALPART
7220 ? REALPART_EXPR
: IMAGPART_EXPR
),
7222 tf_warning_or_error
);
7226 case RID_TRANSACTION_ATOMIC
:
7227 case RID_TRANSACTION_RELAXED
:
7228 return cp_parser_transaction_expression (parser
, keyword
);
7233 const char *saved_message
;
7234 bool saved_integral_constant_expression_p
;
7235 bool saved_non_integral_constant_expression_p
;
7236 bool saved_greater_than_is_operator_p
;
7238 cp_lexer_consume_token (parser
->lexer
);
7239 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7241 saved_message
= parser
->type_definition_forbidden_message
;
7242 parser
->type_definition_forbidden_message
7243 = G_("types may not be defined in %<noexcept%> expressions");
7245 saved_integral_constant_expression_p
7246 = parser
->integral_constant_expression_p
;
7247 saved_non_integral_constant_expression_p
7248 = parser
->non_integral_constant_expression_p
;
7249 parser
->integral_constant_expression_p
= false;
7251 saved_greater_than_is_operator_p
7252 = parser
->greater_than_is_operator_p
;
7253 parser
->greater_than_is_operator_p
= true;
7255 ++cp_unevaluated_operand
;
7256 ++c_inhibit_evaluation_warnings
;
7257 ++cp_noexcept_operand
;
7258 expr
= cp_parser_expression (parser
);
7259 --cp_noexcept_operand
;
7260 --c_inhibit_evaluation_warnings
;
7261 --cp_unevaluated_operand
;
7263 parser
->greater_than_is_operator_p
7264 = saved_greater_than_is_operator_p
;
7266 parser
->integral_constant_expression_p
7267 = saved_integral_constant_expression_p
;
7268 parser
->non_integral_constant_expression_p
7269 = saved_non_integral_constant_expression_p
;
7271 parser
->type_definition_forbidden_message
= saved_message
;
7273 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7274 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7282 /* Look for the `:: new' and `:: delete', which also signal the
7283 beginning of a new-expression, or delete-expression,
7284 respectively. If the next token is `::', then it might be one of
7286 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7290 /* See if the token after the `::' is one of the keywords in
7291 which we're interested. */
7292 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7293 /* If it's `new', we have a new-expression. */
7294 if (keyword
== RID_NEW
)
7295 return cp_parser_new_expression (parser
);
7296 /* Similarly, for `delete'. */
7297 else if (keyword
== RID_DELETE
)
7298 return cp_parser_delete_expression (parser
);
7301 /* Look for a unary operator. */
7302 unary_operator
= cp_parser_unary_operator (token
);
7303 /* The `++' and `--' operators can be handled similarly, even though
7304 they are not technically unary-operators in the grammar. */
7305 if (unary_operator
== ERROR_MARK
)
7307 if (token
->type
== CPP_PLUS_PLUS
)
7308 unary_operator
= PREINCREMENT_EXPR
;
7309 else if (token
->type
== CPP_MINUS_MINUS
)
7310 unary_operator
= PREDECREMENT_EXPR
;
7311 /* Handle the GNU address-of-label extension. */
7312 else if (cp_parser_allow_gnu_extensions_p (parser
)
7313 && token
->type
== CPP_AND_AND
)
7317 location_t loc
= token
->location
;
7319 /* Consume the '&&' token. */
7320 cp_lexer_consume_token (parser
->lexer
);
7321 /* Look for the identifier. */
7322 identifier
= cp_parser_identifier (parser
);
7323 /* Create an expression representing the address. */
7324 expression
= finish_label_address_expr (identifier
, loc
);
7325 if (cp_parser_non_integral_constant_expression (parser
,
7327 expression
= error_mark_node
;
7331 if (unary_operator
!= ERROR_MARK
)
7333 tree cast_expression
;
7334 tree expression
= error_mark_node
;
7335 non_integral_constant non_constant_p
= NIC_NONE
;
7336 location_t loc
= token
->location
;
7337 tsubst_flags_t complain
= complain_flags (decltype_p
);
7339 /* Consume the operator token. */
7340 token
= cp_lexer_consume_token (parser
->lexer
);
7341 /* Parse the cast-expression. */
7343 = cp_parser_cast_expression (parser
,
7344 unary_operator
== ADDR_EXPR
,
7348 /* Now, build an appropriate representation. */
7349 switch (unary_operator
)
7352 non_constant_p
= NIC_STAR
;
7353 expression
= build_x_indirect_ref (loc
, cast_expression
,
7359 non_constant_p
= NIC_ADDR
;
7362 expression
= build_x_unary_op (loc
, unary_operator
,
7367 case PREINCREMENT_EXPR
:
7368 case PREDECREMENT_EXPR
:
7369 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7370 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7372 case UNARY_PLUS_EXPR
:
7374 case TRUTH_NOT_EXPR
:
7375 expression
= finish_unary_op_expr (loc
, unary_operator
,
7376 cast_expression
, complain
);
7383 if (non_constant_p
!= NIC_NONE
7384 && cp_parser_non_integral_constant_expression (parser
,
7386 expression
= error_mark_node
;
7391 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7392 /*member_access_only_p=*/false,
7397 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7398 unary-operator, the corresponding tree code is returned. */
7400 static enum tree_code
7401 cp_parser_unary_operator (cp_token
* token
)
7403 switch (token
->type
)
7406 return INDIRECT_REF
;
7412 return UNARY_PLUS_EXPR
;
7418 return TRUTH_NOT_EXPR
;
7421 return BIT_NOT_EXPR
;
7428 /* Parse a new-expression.
7431 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7432 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7434 Returns a representation of the expression. */
7437 cp_parser_new_expression (cp_parser
* parser
)
7439 bool global_scope_p
;
7440 vec
<tree
, va_gc
> *placement
;
7442 vec
<tree
, va_gc
> *initializer
;
7443 tree nelts
= NULL_TREE
;
7446 /* Look for the optional `::' operator. */
7448 = (cp_parser_global_scope_opt (parser
,
7449 /*current_scope_valid_p=*/false)
7451 /* Look for the `new' operator. */
7452 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7453 /* There's no easy way to tell a new-placement from the
7454 `( type-id )' construct. */
7455 cp_parser_parse_tentatively (parser
);
7456 /* Look for a new-placement. */
7457 placement
= cp_parser_new_placement (parser
);
7458 /* If that didn't work out, there's no new-placement. */
7459 if (!cp_parser_parse_definitely (parser
))
7461 if (placement
!= NULL
)
7462 release_tree_vector (placement
);
7466 /* If the next token is a `(', then we have a parenthesized
7468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7471 const char *saved_message
= parser
->type_definition_forbidden_message
;
7473 /* Consume the `('. */
7474 cp_lexer_consume_token (parser
->lexer
);
7476 /* Parse the type-id. */
7477 parser
->type_definition_forbidden_message
7478 = G_("types may not be defined in a new-expression");
7479 type
= cp_parser_type_id (parser
);
7480 parser
->type_definition_forbidden_message
= saved_message
;
7482 /* Look for the closing `)'. */
7483 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7484 token
= cp_lexer_peek_token (parser
->lexer
);
7485 /* There should not be a direct-new-declarator in this production,
7486 but GCC used to allowed this, so we check and emit a sensible error
7487 message for this case. */
7488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7490 error_at (token
->location
,
7491 "array bound forbidden after parenthesized type-id");
7492 inform (token
->location
,
7493 "try removing the parentheses around the type-id");
7494 cp_parser_direct_new_declarator (parser
);
7497 /* Otherwise, there must be a new-type-id. */
7499 type
= cp_parser_new_type_id (parser
, &nelts
);
7501 /* If the next token is a `(' or '{', then we have a new-initializer. */
7502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7503 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7504 initializer
= cp_parser_new_initializer (parser
);
7508 /* A new-expression may not appear in an integral constant
7510 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7511 ret
= error_mark_node
;
7514 /* Create a representation of the new-expression. */
7515 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7516 tf_warning_or_error
);
7519 if (placement
!= NULL
)
7520 release_tree_vector (placement
);
7521 if (initializer
!= NULL
)
7522 release_tree_vector (initializer
);
7527 /* Parse a new-placement.
7532 Returns the same representation as for an expression-list. */
7534 static vec
<tree
, va_gc
> *
7535 cp_parser_new_placement (cp_parser
* parser
)
7537 vec
<tree
, va_gc
> *expression_list
;
7539 /* Parse the expression-list. */
7540 expression_list
= (cp_parser_parenthesized_expression_list
7541 (parser
, non_attr
, /*cast_p=*/false,
7542 /*allow_expansion_p=*/true,
7543 /*non_constant_p=*/NULL
));
7545 return expression_list
;
7548 /* Parse a new-type-id.
7551 type-specifier-seq new-declarator [opt]
7553 Returns the TYPE allocated. If the new-type-id indicates an array
7554 type, *NELTS is set to the number of elements in the last array
7555 bound; the TYPE will not include the last array bound. */
7558 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7560 cp_decl_specifier_seq type_specifier_seq
;
7561 cp_declarator
*new_declarator
;
7562 cp_declarator
*declarator
;
7563 cp_declarator
*outer_declarator
;
7564 const char *saved_message
;
7566 /* The type-specifier sequence must not contain type definitions.
7567 (It cannot contain declarations of new types either, but if they
7568 are not definitions we will catch that because they are not
7570 saved_message
= parser
->type_definition_forbidden_message
;
7571 parser
->type_definition_forbidden_message
7572 = G_("types may not be defined in a new-type-id");
7573 /* Parse the type-specifier-seq. */
7574 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7575 /*is_trailing_return=*/false,
7576 &type_specifier_seq
);
7577 /* Restore the old message. */
7578 parser
->type_definition_forbidden_message
= saved_message
;
7580 if (type_specifier_seq
.type
== error_mark_node
)
7581 return error_mark_node
;
7583 /* Parse the new-declarator. */
7584 new_declarator
= cp_parser_new_declarator_opt (parser
);
7586 /* Determine the number of elements in the last array dimension, if
7589 /* Skip down to the last array dimension. */
7590 declarator
= new_declarator
;
7591 outer_declarator
= NULL
;
7592 while (declarator
&& (declarator
->kind
== cdk_pointer
7593 || declarator
->kind
== cdk_ptrmem
))
7595 outer_declarator
= declarator
;
7596 declarator
= declarator
->declarator
;
7599 && declarator
->kind
== cdk_array
7600 && declarator
->declarator
7601 && declarator
->declarator
->kind
== cdk_array
)
7603 outer_declarator
= declarator
;
7604 declarator
= declarator
->declarator
;
7607 if (declarator
&& declarator
->kind
== cdk_array
)
7609 *nelts
= declarator
->u
.array
.bounds
;
7610 if (*nelts
== error_mark_node
)
7611 *nelts
= integer_one_node
;
7613 if (outer_declarator
)
7614 outer_declarator
->declarator
= declarator
->declarator
;
7616 new_declarator
= NULL
;
7619 return groktypename (&type_specifier_seq
, new_declarator
, false);
7622 /* Parse an (optional) new-declarator.
7625 ptr-operator new-declarator [opt]
7626 direct-new-declarator
7628 Returns the declarator. */
7630 static cp_declarator
*
7631 cp_parser_new_declarator_opt (cp_parser
* parser
)
7633 enum tree_code code
;
7634 tree type
, std_attributes
= NULL_TREE
;
7635 cp_cv_quals cv_quals
;
7637 /* We don't know if there's a ptr-operator next, or not. */
7638 cp_parser_parse_tentatively (parser
);
7639 /* Look for a ptr-operator. */
7640 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7641 /* If that worked, look for more new-declarators. */
7642 if (cp_parser_parse_definitely (parser
))
7644 cp_declarator
*declarator
;
7646 /* Parse another optional declarator. */
7647 declarator
= cp_parser_new_declarator_opt (parser
);
7649 declarator
= cp_parser_make_indirect_declarator
7650 (code
, type
, cv_quals
, declarator
, std_attributes
);
7655 /* If the next token is a `[', there is a direct-new-declarator. */
7656 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7657 return cp_parser_direct_new_declarator (parser
);
7662 /* Parse a direct-new-declarator.
7664 direct-new-declarator:
7666 direct-new-declarator [constant-expression]
7670 static cp_declarator
*
7671 cp_parser_direct_new_declarator (cp_parser
* parser
)
7673 cp_declarator
*declarator
= NULL
;
7680 /* Look for the opening `['. */
7681 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7683 token
= cp_lexer_peek_token (parser
->lexer
);
7684 expression
= cp_parser_expression (parser
);
7685 /* The standard requires that the expression have integral
7686 type. DR 74 adds enumeration types. We believe that the
7687 real intent is that these expressions be handled like the
7688 expression in a `switch' condition, which also allows
7689 classes with a single conversion to integral or
7690 enumeration type. */
7691 if (!processing_template_decl
)
7694 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7699 error_at (token
->location
,
7700 "expression in new-declarator must have integral "
7701 "or enumeration type");
7702 expression
= error_mark_node
;
7706 /* Look for the closing `]'. */
7707 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7709 /* Add this bound to the declarator. */
7710 declarator
= make_array_declarator (declarator
, expression
);
7712 /* If the next token is not a `[', then there are no more
7714 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7721 /* Parse a new-initializer.
7724 ( expression-list [opt] )
7727 Returns a representation of the expression-list. */
7729 static vec
<tree
, va_gc
> *
7730 cp_parser_new_initializer (cp_parser
* parser
)
7732 vec
<tree
, va_gc
> *expression_list
;
7734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7737 bool expr_non_constant_p
;
7738 cp_lexer_set_source_position (parser
->lexer
);
7739 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7740 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7741 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7742 expression_list
= make_tree_vector_single (t
);
7745 expression_list
= (cp_parser_parenthesized_expression_list
7746 (parser
, non_attr
, /*cast_p=*/false,
7747 /*allow_expansion_p=*/true,
7748 /*non_constant_p=*/NULL
));
7750 return expression_list
;
7753 /* Parse a delete-expression.
7756 :: [opt] delete cast-expression
7757 :: [opt] delete [ ] cast-expression
7759 Returns a representation of the expression. */
7762 cp_parser_delete_expression (cp_parser
* parser
)
7764 bool global_scope_p
;
7768 /* Look for the optional `::' operator. */
7770 = (cp_parser_global_scope_opt (parser
,
7771 /*current_scope_valid_p=*/false)
7773 /* Look for the `delete' keyword. */
7774 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7775 /* See if the array syntax is in use. */
7776 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7778 /* Consume the `[' token. */
7779 cp_lexer_consume_token (parser
->lexer
);
7780 /* Look for the `]' token. */
7781 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7782 /* Remember that this is the `[]' construct. */
7788 /* Parse the cast-expression. */
7789 expression
= cp_parser_simple_cast_expression (parser
);
7791 /* A delete-expression may not appear in an integral constant
7793 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7794 return error_mark_node
;
7796 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7797 tf_warning_or_error
);
7800 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7801 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7805 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7807 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7808 switch (token
->type
)
7814 case CPP_CLOSE_SQUARE
:
7815 case CPP_CLOSE_PAREN
:
7816 case CPP_CLOSE_BRACE
:
7817 case CPP_OPEN_BRACE
:
7821 case CPP_DEREF_STAR
:
7829 case CPP_GREATER_EQ
:
7850 case CPP_OPEN_PAREN
:
7851 /* In ((type ()) () the last () isn't a valid cast-expression,
7852 so the whole must be parsed as postfix-expression. */
7853 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7856 case CPP_OPEN_SQUARE
:
7857 /* '[' may start a primary-expression in obj-c++ and in C++11,
7858 as a lambda-expression, eg, '(void)[]{}'. */
7859 if (cxx_dialect
>= cxx11
)
7861 return c_dialect_objc ();
7864 case CPP_MINUS_MINUS
:
7865 /* '++' and '--' may or may not start a cast-expression:
7867 struct T { void operator++(int); };
7868 void f() { (T())++; }
7881 /* Parse a cast-expression.
7885 ( type-id ) cast-expression
7887 ADDRESS_P is true iff the unary-expression is appearing as the
7888 operand of the `&' operator. CAST_P is true if this expression is
7889 the target of a cast.
7891 Returns a representation of the expression. */
7894 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7895 bool decltype_p
, cp_id_kind
* pidk
)
7897 /* If it's a `(', then we might be looking at a cast. */
7898 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7900 tree type
= NULL_TREE
;
7901 tree expr
= NULL_TREE
;
7902 int cast_expression
= 0;
7903 const char *saved_message
;
7905 /* There's no way to know yet whether or not this is a cast.
7906 For example, `(int (3))' is a unary-expression, while `(int)
7907 3' is a cast. So, we resort to parsing tentatively. */
7908 cp_parser_parse_tentatively (parser
);
7909 /* Types may not be defined in a cast. */
7910 saved_message
= parser
->type_definition_forbidden_message
;
7911 parser
->type_definition_forbidden_message
7912 = G_("types may not be defined in casts");
7913 /* Consume the `('. */
7914 cp_lexer_consume_token (parser
->lexer
);
7915 /* A very tricky bit is that `(struct S) { 3 }' is a
7916 compound-literal (which we permit in C++ as an extension).
7917 But, that construct is not a cast-expression -- it is a
7918 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7919 is legal; if the compound-literal were a cast-expression,
7920 you'd need an extra set of parentheses.) But, if we parse
7921 the type-id, and it happens to be a class-specifier, then we
7922 will commit to the parse at that point, because we cannot
7923 undo the action that is done when creating a new class. So,
7924 then we cannot back up and do a postfix-expression.
7926 Another tricky case is the following (c++/29234):
7928 struct S { void operator () (); };
7935 As a type-id we parse the parenthesized S()() as a function
7936 returning a function, groktypename complains and we cannot
7937 back up in this case either.
7939 Therefore, we scan ahead to the closing `)', and check to see
7940 if the tokens after the `)' can start a cast-expression. Otherwise
7941 we are dealing with an unary-expression, a postfix-expression
7944 Yet another tricky case, in C++11, is the following (c++/54891):
7948 The issue is that usually, besides the case of lambda-expressions,
7949 the parenthesized type-id cannot be followed by '[', and, eg, we
7950 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7951 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7952 we don't commit, we try a cast-expression, then an unary-expression.
7954 Save tokens so that we can put them back. */
7955 cp_lexer_save_tokens (parser
->lexer
);
7957 /* We may be looking at a cast-expression. */
7958 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7959 /*consume_paren=*/true))
7961 = cp_parser_tokens_start_cast_expression (parser
);
7963 /* Roll back the tokens we skipped. */
7964 cp_lexer_rollback_tokens (parser
->lexer
);
7965 /* If we aren't looking at a cast-expression, simulate an error so
7966 that the call to cp_parser_error_occurred below returns true. */
7967 if (!cast_expression
)
7968 cp_parser_simulate_error (parser
);
7971 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7972 parser
->in_type_id_in_expr_p
= true;
7973 /* Look for the type-id. */
7974 type
= cp_parser_type_id (parser
);
7975 /* Look for the closing `)'. */
7976 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7977 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7980 /* Restore the saved message. */
7981 parser
->type_definition_forbidden_message
= saved_message
;
7983 /* At this point this can only be either a cast or a
7984 parenthesized ctor such as `(T ())' that looks like a cast to
7985 function returning T. */
7986 if (!cp_parser_error_occurred (parser
))
7988 /* Only commit if the cast-expression doesn't start with
7989 '++', '--', or '[' in C++11. */
7990 if (cast_expression
> 0)
7991 cp_parser_commit_to_topmost_tentative_parse (parser
);
7993 expr
= cp_parser_cast_expression (parser
,
7994 /*address_p=*/false,
7996 /*decltype_p=*/false,
7999 if (cp_parser_parse_definitely (parser
))
8001 /* Warn about old-style casts, if so requested. */
8002 if (warn_old_style_cast
8003 && !in_system_header_at (input_location
)
8004 && !VOID_TYPE_P (type
)
8005 && current_lang_name
!= lang_name_c
)
8006 warning (OPT_Wold_style_cast
, "use of old-style cast");
8008 /* Only type conversions to integral or enumeration types
8009 can be used in constant-expressions. */
8010 if (!cast_valid_in_integral_constant_expression_p (type
)
8011 && cp_parser_non_integral_constant_expression (parser
,
8013 return error_mark_node
;
8015 /* Perform the cast. */
8016 expr
= build_c_cast (input_location
, type
, expr
);
8021 cp_parser_abort_tentative_parse (parser
);
8024 /* If we get here, then it's not a cast, so it must be a
8025 unary-expression. */
8026 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8027 cast_p
, decltype_p
);
8030 /* Parse a binary expression of the general form:
8034 pm-expression .* cast-expression
8035 pm-expression ->* cast-expression
8037 multiplicative-expression:
8039 multiplicative-expression * pm-expression
8040 multiplicative-expression / pm-expression
8041 multiplicative-expression % pm-expression
8043 additive-expression:
8044 multiplicative-expression
8045 additive-expression + multiplicative-expression
8046 additive-expression - multiplicative-expression
8050 shift-expression << additive-expression
8051 shift-expression >> additive-expression
8053 relational-expression:
8055 relational-expression < shift-expression
8056 relational-expression > shift-expression
8057 relational-expression <= shift-expression
8058 relational-expression >= shift-expression
8062 relational-expression:
8063 relational-expression <? shift-expression
8064 relational-expression >? shift-expression
8066 equality-expression:
8067 relational-expression
8068 equality-expression == relational-expression
8069 equality-expression != relational-expression
8073 and-expression & equality-expression
8075 exclusive-or-expression:
8077 exclusive-or-expression ^ and-expression
8079 inclusive-or-expression:
8080 exclusive-or-expression
8081 inclusive-or-expression | exclusive-or-expression
8083 logical-and-expression:
8084 inclusive-or-expression
8085 logical-and-expression && inclusive-or-expression
8087 logical-or-expression:
8088 logical-and-expression
8089 logical-or-expression || logical-and-expression
8091 All these are implemented with a single function like:
8094 simple-cast-expression
8095 binary-expression <token> binary-expression
8097 CAST_P is true if this expression is the target of a cast.
8099 The binops_by_token map is used to get the tree codes for each <token> type.
8100 binary-expressions are associated according to a precedence table. */
8102 #define TOKEN_PRECEDENCE(token) \
8103 (((token->type == CPP_GREATER \
8104 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8105 && !parser->greater_than_is_operator_p) \
8106 ? PREC_NOT_OPERATOR \
8107 : binops_by_token[token->type].prec)
8110 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8111 bool no_toplevel_fold_p
,
8113 enum cp_parser_prec prec
,
8116 cp_parser_expression_stack stack
;
8117 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8118 cp_parser_expression_stack_entry current
;
8121 enum tree_code rhs_type
;
8122 enum cp_parser_prec new_prec
, lookahead_prec
;
8125 /* Parse the first expression. */
8126 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8127 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8128 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8129 cast_p
, decltype_p
, pidk
);
8130 current
.prec
= prec
;
8132 if (cp_parser_error_occurred (parser
))
8133 return error_mark_node
;
8137 /* Get an operator token. */
8138 token
= cp_lexer_peek_token (parser
->lexer
);
8140 if (warn_cxx0x_compat
8141 && token
->type
== CPP_RSHIFT
8142 && !parser
->greater_than_is_operator_p
)
8144 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
8145 "%<>>%> operator is treated"
8146 " as two right angle brackets in C++11"))
8147 inform (token
->location
,
8148 "suggest parentheses around %<>>%> expression");
8151 new_prec
= TOKEN_PRECEDENCE (token
);
8153 /* Popping an entry off the stack means we completed a subexpression:
8154 - either we found a token which is not an operator (`>' where it is not
8155 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8156 will happen repeatedly;
8157 - or, we found an operator which has lower priority. This is the case
8158 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8160 if (new_prec
<= current
.prec
)
8169 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8170 current
.loc
= token
->location
;
8172 /* We used the operator token. */
8173 cp_lexer_consume_token (parser
->lexer
);
8175 /* For "false && x" or "true || x", x will never be executed;
8176 disable warnings while evaluating it. */
8177 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8178 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8179 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8180 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8182 /* Extract another operand. It may be the RHS of this expression
8183 or the LHS of a new, higher priority expression. */
8184 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8185 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8186 rhs
= cp_parser_simple_cast_expression (parser
);
8188 /* Get another operator token. Look up its precedence to avoid
8189 building a useless (immediately popped) stack entry for common
8190 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8191 token
= cp_lexer_peek_token (parser
->lexer
);
8192 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8193 if (lookahead_prec
> new_prec
)
8195 /* ... and prepare to parse the RHS of the new, higher priority
8196 expression. Since precedence levels on the stack are
8197 monotonically increasing, we do not have to care about
8202 current
.lhs_type
= rhs_type
;
8203 current
.prec
= new_prec
;
8204 new_prec
= lookahead_prec
;
8208 lookahead_prec
= new_prec
;
8209 /* If the stack is not empty, we have parsed into LHS the right side
8210 (`4' in the example above) of an expression we had suspended.
8211 We can use the information on the stack to recover the LHS (`3')
8212 from the stack together with the tree code (`MULT_EXPR'), and
8213 the precedence of the higher level subexpression
8214 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8215 which will be used to actually build the additive expression. */
8217 rhs_type
= current
.lhs_type
;
8222 /* Undo the disabling of warnings done above. */
8223 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8224 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8225 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8226 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8228 if (warn_logical_not_paren
8229 && current
.lhs_type
== TRUTH_NOT_EXPR
)
8230 warn_logical_not_parentheses (current
.loc
, current
.tree_type
, rhs
);
8233 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8234 ERROR_MARK for everything that is not a binary expression.
8235 This makes warn_about_parentheses miss some warnings that
8236 involve unary operators. For unary expressions we should
8237 pass the correct tree_code unless the unary expression was
8238 surrounded by parentheses.
8240 if (no_toplevel_fold_p
8241 && lookahead_prec
<= current
.prec
8243 current
.lhs
= build2 (current
.tree_type
,
8244 TREE_CODE_CLASS (current
.tree_type
)
8246 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8249 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8250 current
.lhs
, current
.lhs_type
,
8251 rhs
, rhs_type
, &overload
,
8252 complain_flags (decltype_p
));
8253 current
.lhs_type
= current
.tree_type
;
8254 if (EXPR_P (current
.lhs
))
8255 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8257 /* If the binary operator required the use of an overloaded operator,
8258 then this expression cannot be an integral constant-expression.
8259 An overloaded operator can be used even if both operands are
8260 otherwise permissible in an integral constant-expression if at
8261 least one of the operands is of enumeration type. */
8264 && cp_parser_non_integral_constant_expression (parser
,
8266 return error_mark_node
;
8273 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8274 bool no_toplevel_fold_p
,
8275 enum cp_parser_prec prec
,
8278 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8279 /*decltype*/false, prec
, pidk
);
8282 /* Parse the `? expression : assignment-expression' part of a
8283 conditional-expression. The LOGICAL_OR_EXPR is the
8284 logical-or-expression that started the conditional-expression.
8285 Returns a representation of the entire conditional-expression.
8287 This routine is used by cp_parser_assignment_expression.
8289 ? expression : assignment-expression
8293 ? : assignment-expression */
8296 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8299 tree assignment_expr
;
8300 struct cp_token
*token
;
8301 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8303 /* Consume the `?' token. */
8304 cp_lexer_consume_token (parser
->lexer
);
8305 token
= cp_lexer_peek_token (parser
->lexer
);
8306 if (cp_parser_allow_gnu_extensions_p (parser
)
8307 && token
->type
== CPP_COLON
)
8309 pedwarn (token
->location
, OPT_Wpedantic
,
8310 "ISO C++ does not allow ?: with omitted middle operand");
8311 /* Implicit true clause. */
8313 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8314 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8318 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8319 parser
->colon_corrects_to_scope_p
= false;
8320 /* Parse the expression. */
8321 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8322 expr
= cp_parser_expression (parser
);
8323 c_inhibit_evaluation_warnings
+=
8324 ((logical_or_expr
== truthvalue_true_node
)
8325 - (logical_or_expr
== truthvalue_false_node
));
8326 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8329 /* The next token should be a `:'. */
8330 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8331 /* Parse the assignment-expression. */
8332 assignment_expr
= cp_parser_assignment_expression (parser
);
8333 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8335 /* Build the conditional-expression. */
8336 return build_x_conditional_expr (loc
, logical_or_expr
,
8339 tf_warning_or_error
);
8342 /* Parse an assignment-expression.
8344 assignment-expression:
8345 conditional-expression
8346 logical-or-expression assignment-operator assignment_expression
8349 CAST_P is true if this expression is the target of a cast.
8350 DECLTYPE_P is true if this expression is the operand of decltype.
8352 Returns a representation for the expression. */
8355 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8356 bool cast_p
, bool decltype_p
)
8360 /* If the next token is the `throw' keyword, then we're looking at
8361 a throw-expression. */
8362 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8363 expr
= cp_parser_throw_expression (parser
);
8364 /* Otherwise, it must be that we are looking at a
8365 logical-or-expression. */
8368 /* Parse the binary expressions (logical-or-expression). */
8369 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8371 PREC_NOT_OPERATOR
, pidk
);
8372 /* If the next token is a `?' then we're actually looking at a
8373 conditional-expression. */
8374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8375 return cp_parser_question_colon_clause (parser
, expr
);
8378 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8380 /* If it's an assignment-operator, we're using the second
8382 enum tree_code assignment_operator
8383 = cp_parser_assignment_operator_opt (parser
);
8384 if (assignment_operator
!= ERROR_MARK
)
8386 bool non_constant_p
;
8387 location_t saved_input_location
;
8389 /* Parse the right-hand side of the assignment. */
8390 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8392 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8393 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8395 /* An assignment may not appear in a
8396 constant-expression. */
8397 if (cp_parser_non_integral_constant_expression (parser
,
8399 return error_mark_node
;
8400 /* Build the assignment expression. Its default
8401 location is the location of the '=' token. */
8402 saved_input_location
= input_location
;
8403 input_location
= loc
;
8404 expr
= build_x_modify_expr (loc
, expr
,
8405 assignment_operator
,
8407 complain_flags (decltype_p
));
8408 input_location
= saved_input_location
;
8416 /* Parse an (optional) assignment-operator.
8418 assignment-operator: one of
8419 = *= /= %= += -= >>= <<= &= ^= |=
8423 assignment-operator: one of
8426 If the next token is an assignment operator, the corresponding tree
8427 code is returned, and the token is consumed. For example, for
8428 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8429 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8430 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8431 operator, ERROR_MARK is returned. */
8433 static enum tree_code
8434 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8439 /* Peek at the next token. */
8440 token
= cp_lexer_peek_token (parser
->lexer
);
8442 switch (token
->type
)
8453 op
= TRUNC_DIV_EXPR
;
8457 op
= TRUNC_MOD_EXPR
;
8489 /* Nothing else is an assignment operator. */
8493 /* If it was an assignment operator, consume it. */
8494 if (op
!= ERROR_MARK
)
8495 cp_lexer_consume_token (parser
->lexer
);
8500 /* Parse an expression.
8503 assignment-expression
8504 expression , assignment-expression
8506 CAST_P is true if this expression is the target of a cast.
8507 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8508 except possibly parenthesized or on the RHS of a comma (N3276).
8510 Returns a representation of the expression. */
8513 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8514 bool cast_p
, bool decltype_p
)
8516 tree expression
= NULL_TREE
;
8517 location_t loc
= UNKNOWN_LOCATION
;
8521 tree assignment_expression
;
8523 /* Parse the next assignment-expression. */
8524 assignment_expression
8525 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8527 /* We don't create a temporary for a call that is the immediate operand
8528 of decltype or on the RHS of a comma. But when we see a comma, we
8529 need to create a temporary for a call on the LHS. */
8530 if (decltype_p
&& !processing_template_decl
8531 && TREE_CODE (assignment_expression
) == CALL_EXPR
8532 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8533 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8534 assignment_expression
8535 = build_cplus_new (TREE_TYPE (assignment_expression
),
8536 assignment_expression
, tf_warning_or_error
);
8538 /* If this is the first assignment-expression, we can just
8541 expression
= assignment_expression
;
8543 expression
= build_x_compound_expr (loc
, expression
,
8544 assignment_expression
,
8545 complain_flags (decltype_p
));
8546 /* If the next token is not a comma, then we are done with the
8548 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8550 /* Consume the `,'. */
8551 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8552 cp_lexer_consume_token (parser
->lexer
);
8553 /* A comma operator cannot appear in a constant-expression. */
8554 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8555 expression
= error_mark_node
;
8561 /* Parse a constant-expression.
8563 constant-expression:
8564 conditional-expression
8566 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8567 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8568 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8569 is false, NON_CONSTANT_P should be NULL. */
8572 cp_parser_constant_expression (cp_parser
* parser
,
8573 bool allow_non_constant_p
,
8574 bool *non_constant_p
)
8576 bool saved_integral_constant_expression_p
;
8577 bool saved_allow_non_integral_constant_expression_p
;
8578 bool saved_non_integral_constant_expression_p
;
8581 /* It might seem that we could simply parse the
8582 conditional-expression, and then check to see if it were
8583 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8584 one that the compiler can figure out is constant, possibly after
8585 doing some simplifications or optimizations. The standard has a
8586 precise definition of constant-expression, and we must honor
8587 that, even though it is somewhat more restrictive.
8593 is not a legal declaration, because `(2, 3)' is not a
8594 constant-expression. The `,' operator is forbidden in a
8595 constant-expression. However, GCC's constant-folding machinery
8596 will fold this operation to an INTEGER_CST for `3'. */
8598 /* Save the old settings. */
8599 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8600 saved_allow_non_integral_constant_expression_p
8601 = parser
->allow_non_integral_constant_expression_p
;
8602 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8603 /* We are now parsing a constant-expression. */
8604 parser
->integral_constant_expression_p
= true;
8605 parser
->allow_non_integral_constant_expression_p
8606 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8607 parser
->non_integral_constant_expression_p
= false;
8608 /* Although the grammar says "conditional-expression", we parse an
8609 "assignment-expression", which also permits "throw-expression"
8610 and the use of assignment operators. In the case that
8611 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8612 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8613 actually essential that we look for an assignment-expression.
8614 For example, cp_parser_initializer_clauses uses this function to
8615 determine whether a particular assignment-expression is in fact
8617 expression
= cp_parser_assignment_expression (parser
);
8618 /* Restore the old settings. */
8619 parser
->integral_constant_expression_p
8620 = saved_integral_constant_expression_p
;
8621 parser
->allow_non_integral_constant_expression_p
8622 = saved_allow_non_integral_constant_expression_p
;
8623 if (cxx_dialect
>= cxx11
)
8625 /* Require an rvalue constant expression here; that's what our
8626 callers expect. Reference constant expressions are handled
8627 separately in e.g. cp_parser_template_argument. */
8628 bool is_const
= potential_rvalue_constant_expression (expression
);
8629 parser
->non_integral_constant_expression_p
= !is_const
;
8630 if (!is_const
&& !allow_non_constant_p
)
8631 require_potential_rvalue_constant_expression (expression
);
8633 if (allow_non_constant_p
)
8634 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8635 parser
->non_integral_constant_expression_p
8636 = saved_non_integral_constant_expression_p
;
8641 /* Parse __builtin_offsetof.
8643 offsetof-expression:
8644 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8646 offsetof-member-designator:
8648 | offsetof-member-designator "." id-expression
8649 | offsetof-member-designator "[" expression "]"
8650 | offsetof-member-designator "->" id-expression */
8653 cp_parser_builtin_offsetof (cp_parser
*parser
)
8655 int save_ice_p
, save_non_ice_p
;
8660 /* We're about to accept non-integral-constant things, but will
8661 definitely yield an integral constant expression. Save and
8662 restore these values around our local parsing. */
8663 save_ice_p
= parser
->integral_constant_expression_p
;
8664 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8666 /* Consume the "__builtin_offsetof" token. */
8667 cp_lexer_consume_token (parser
->lexer
);
8668 /* Consume the opening `('. */
8669 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8670 /* Parse the type-id. */
8671 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8672 type
= cp_parser_type_id (parser
);
8673 /* Look for the `,'. */
8674 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8675 token
= cp_lexer_peek_token (parser
->lexer
);
8677 /* Build the (type *)null that begins the traditional offsetof macro. */
8678 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8679 tf_warning_or_error
);
8681 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8682 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8683 true, &dummy
, token
->location
);
8686 token
= cp_lexer_peek_token (parser
->lexer
);
8687 switch (token
->type
)
8689 case CPP_OPEN_SQUARE
:
8690 /* offsetof-member-designator "[" expression "]" */
8691 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8696 /* offsetof-member-designator "->" identifier */
8697 expr
= grok_array_decl (token
->location
, expr
,
8698 integer_zero_node
, false);
8702 /* offsetof-member-designator "." identifier */
8703 cp_lexer_consume_token (parser
->lexer
);
8704 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8709 case CPP_CLOSE_PAREN
:
8710 /* Consume the ")" token. */
8711 cp_lexer_consume_token (parser
->lexer
);
8715 /* Error. We know the following require will fail, but
8716 that gives the proper error message. */
8717 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8718 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8719 expr
= error_mark_node
;
8725 /* If we're processing a template, we can't finish the semantics yet.
8726 Otherwise we can fold the entire expression now. */
8727 if (processing_template_decl
)
8729 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8730 SET_EXPR_LOCATION (expr
, loc
);
8733 expr
= finish_offsetof (expr
, loc
);
8736 parser
->integral_constant_expression_p
= save_ice_p
;
8737 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8742 /* Parse a trait expression.
8744 Returns a representation of the expression, the underlying type
8745 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8748 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8751 tree type1
, type2
= NULL_TREE
;
8752 bool binary
= false;
8753 bool variadic
= false;
8757 case RID_HAS_NOTHROW_ASSIGN
:
8758 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8760 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8761 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8763 case RID_HAS_NOTHROW_COPY
:
8764 kind
= CPTK_HAS_NOTHROW_COPY
;
8766 case RID_HAS_TRIVIAL_ASSIGN
:
8767 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8769 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8770 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8772 case RID_HAS_TRIVIAL_COPY
:
8773 kind
= CPTK_HAS_TRIVIAL_COPY
;
8775 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8776 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8778 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8779 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8781 case RID_IS_ABSTRACT
:
8782 kind
= CPTK_IS_ABSTRACT
;
8784 case RID_IS_BASE_OF
:
8785 kind
= CPTK_IS_BASE_OF
;
8789 kind
= CPTK_IS_CLASS
;
8792 kind
= CPTK_IS_EMPTY
;
8795 kind
= CPTK_IS_ENUM
;
8798 kind
= CPTK_IS_FINAL
;
8800 case RID_IS_LITERAL_TYPE
:
8801 kind
= CPTK_IS_LITERAL_TYPE
;
8806 case RID_IS_POLYMORPHIC
:
8807 kind
= CPTK_IS_POLYMORPHIC
;
8809 case RID_IS_STD_LAYOUT
:
8810 kind
= CPTK_IS_STD_LAYOUT
;
8812 case RID_IS_TRIVIAL
:
8813 kind
= CPTK_IS_TRIVIAL
;
8815 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8816 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8819 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8820 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8823 case RID_IS_TRIVIALLY_COPYABLE
:
8824 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8827 kind
= CPTK_IS_UNION
;
8829 case RID_UNDERLYING_TYPE
:
8830 kind
= CPTK_UNDERLYING_TYPE
;
8835 case RID_DIRECT_BASES
:
8836 kind
= CPTK_DIRECT_BASES
;
8842 /* Consume the token. */
8843 cp_lexer_consume_token (parser
->lexer
);
8845 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8847 type1
= cp_parser_type_id (parser
);
8849 if (type1
== error_mark_node
)
8850 return error_mark_node
;
8854 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8856 type2
= cp_parser_type_id (parser
);
8858 if (type2
== error_mark_node
)
8859 return error_mark_node
;
8863 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8865 cp_lexer_consume_token (parser
->lexer
);
8866 tree elt
= cp_parser_type_id (parser
);
8867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8869 cp_lexer_consume_token (parser
->lexer
);
8870 elt
= make_pack_expansion (elt
);
8872 if (elt
== error_mark_node
)
8873 return error_mark_node
;
8874 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8878 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8880 /* Complete the trait expression, which may mean either processing
8881 the trait expr now or saving it for template instantiation. */
8884 case CPTK_UNDERLYING_TYPE
:
8885 return finish_underlying_type (type1
);
8887 return finish_bases (type1
, false);
8888 case CPTK_DIRECT_BASES
:
8889 return finish_bases (type1
, true);
8891 return finish_trait_expr (kind
, type1
, type2
);
8895 /* Lambdas that appear in variable initializer or default argument scope
8896 get that in their mangling, so we need to record it. We might as well
8897 use the count for function and namespace scopes as well. */
8898 static GTY(()) tree lambda_scope
;
8899 static GTY(()) int lambda_count
;
8900 typedef struct GTY(()) tree_int
8905 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8908 start_lambda_scope (tree decl
)
8912 /* Once we're inside a function, we ignore other scopes and just push
8913 the function again so that popping works properly. */
8914 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8915 decl
= current_function_decl
;
8916 ti
.t
= lambda_scope
;
8917 ti
.i
= lambda_count
;
8918 vec_safe_push (lambda_scope_stack
, ti
);
8919 if (lambda_scope
!= decl
)
8921 /* Don't reset the count if we're still in the same function. */
8922 lambda_scope
= decl
;
8928 record_lambda_scope (tree lambda
)
8930 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8931 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8935 finish_lambda_scope (void)
8937 tree_int
*p
= &lambda_scope_stack
->last ();
8938 if (lambda_scope
!= p
->t
)
8940 lambda_scope
= p
->t
;
8941 lambda_count
= p
->i
;
8943 lambda_scope_stack
->pop ();
8946 /* Parse a lambda expression.
8949 lambda-introducer lambda-declarator [opt] compound-statement
8951 Returns a representation of the expression. */
8954 cp_parser_lambda_expression (cp_parser
* parser
)
8956 tree lambda_expr
= build_lambda_expr ();
8959 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8960 cp_token_position start
= 0;
8962 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8964 if (cp_unevaluated_operand
)
8966 if (!token
->error_reported
)
8968 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8969 "lambda-expression in unevaluated context");
8970 token
->error_reported
= true;
8974 else if (parser
->in_template_argument_list_p
)
8976 if (!token
->error_reported
)
8978 error_at (token
->location
, "lambda-expression in template-argument");
8979 token
->error_reported
= true;
8984 /* We may be in the middle of deferred access check. Disable
8986 push_deferring_access_checks (dk_no_deferred
);
8988 cp_parser_lambda_introducer (parser
, lambda_expr
);
8990 type
= begin_lambda_type (lambda_expr
);
8991 if (type
== error_mark_node
)
8992 return error_mark_node
;
8994 record_lambda_scope (lambda_expr
);
8996 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8997 determine_visibility (TYPE_NAME (type
));
8999 /* Now that we've started the type, add the capture fields for any
9000 explicit captures. */
9001 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9004 /* Inside the class, surrounding template-parameter-lists do not apply. */
9005 unsigned int saved_num_template_parameter_lists
9006 = parser
->num_template_parameter_lists
;
9007 unsigned char in_statement
= parser
->in_statement
;
9008 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9009 bool fully_implicit_function_template_p
9010 = parser
->fully_implicit_function_template_p
;
9011 tree implicit_template_parms
= parser
->implicit_template_parms
;
9012 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9013 bool auto_is_implicit_function_template_parm_p
9014 = parser
->auto_is_implicit_function_template_parm_p
;
9016 parser
->num_template_parameter_lists
= 0;
9017 parser
->in_statement
= 0;
9018 parser
->in_switch_statement_p
= false;
9019 parser
->fully_implicit_function_template_p
= false;
9020 parser
->implicit_template_parms
= 0;
9021 parser
->implicit_template_scope
= 0;
9022 parser
->auto_is_implicit_function_template_parm_p
= false;
9024 /* By virtue of defining a local class, a lambda expression has access to
9025 the private variables of enclosing classes. */
9027 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9031 if (!cp_parser_error_occurred (parser
)
9032 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9033 && cp_parser_start_tentative_firewall (parser
))
9035 cp_parser_lambda_body (parser
, lambda_expr
);
9037 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9039 if (cp_parser_skip_to_closing_brace (parser
))
9040 cp_lexer_consume_token (parser
->lexer
);
9043 /* The capture list was built up in reverse order; fix that now. */
9044 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9045 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9048 maybe_add_lambda_conv_op (type
);
9050 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9052 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9053 parser
->in_statement
= in_statement
;
9054 parser
->in_switch_statement_p
= in_switch_statement_p
;
9055 parser
->fully_implicit_function_template_p
9056 = fully_implicit_function_template_p
;
9057 parser
->implicit_template_parms
= implicit_template_parms
;
9058 parser
->implicit_template_scope
= implicit_template_scope
;
9059 parser
->auto_is_implicit_function_template_parm_p
9060 = auto_is_implicit_function_template_parm_p
;
9063 pop_deferring_access_checks ();
9065 /* This field is only used during parsing of the lambda. */
9066 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9068 /* This lambda shouldn't have any proxies left at this point. */
9069 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9070 /* And now that we're done, push proxies for an enclosing lambda. */
9071 insert_pending_capture_proxies ();
9074 lambda_expr
= build_lambda_object (lambda_expr
);
9076 lambda_expr
= error_mark_node
;
9078 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9083 /* Parse the beginning of a lambda expression.
9086 [ lambda-capture [opt] ]
9088 LAMBDA_EXPR is the current representation of the lambda expression. */
9091 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9093 /* Need commas after the first capture. */
9096 /* Eat the leading `['. */
9097 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9099 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9101 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9103 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9104 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9106 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9108 cp_lexer_consume_token (parser
->lexer
);
9112 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9114 cp_token
* capture_token
;
9116 tree capture_init_expr
;
9117 cp_id_kind idk
= CP_ID_KIND_NONE
;
9118 bool explicit_init_p
= false;
9120 enum capture_kind_type
9125 enum capture_kind_type capture_kind
= BY_COPY
;
9127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9129 error ("expected end of capture-list");
9136 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9138 /* Possibly capture `this'. */
9139 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9141 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9143 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9144 "with by-copy capture default");
9145 cp_lexer_consume_token (parser
->lexer
);
9146 add_capture (lambda_expr
,
9147 /*id=*/this_identifier
,
9148 /*initializer=*/finish_this_expr(),
9149 /*by_reference_p=*/false,
9154 /* Remember whether we want to capture as a reference or not. */
9155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9157 capture_kind
= BY_REFERENCE
;
9158 cp_lexer_consume_token (parser
->lexer
);
9161 /* Get the identifier. */
9162 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9163 capture_id
= cp_parser_identifier (parser
);
9165 if (capture_id
== error_mark_node
)
9166 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9167 delimiters, but I modified this to stop on unnested ']' as well. It
9168 was already changed to stop on unnested '}', so the
9169 "closing_parenthesis" name is no more misleading with my change. */
9171 cp_parser_skip_to_closing_parenthesis (parser
,
9172 /*recovering=*/true,
9174 /*consume_paren=*/true);
9178 /* Find the initializer for this capture. */
9179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9180 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9181 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9183 bool direct
, non_constant
;
9184 /* An explicit initializer exists. */
9185 if (cxx_dialect
< cxx14
)
9186 pedwarn (input_location
, 0,
9187 "lambda capture initializers "
9188 "only available with -std=c++14 or -std=gnu++14");
9189 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9191 explicit_init_p
= true;
9192 if (capture_init_expr
== NULL_TREE
)
9194 error ("empty initializer for lambda init-capture");
9195 capture_init_expr
= error_mark_node
;
9200 const char* error_msg
;
9202 /* Turn the identifier into an id-expression. */
9204 = cp_parser_lookup_name_simple (parser
, capture_id
,
9205 capture_token
->location
);
9207 if (capture_init_expr
== error_mark_node
)
9209 unqualified_name_lookup_error (capture_id
);
9212 else if (DECL_P (capture_init_expr
)
9213 && (!VAR_P (capture_init_expr
)
9214 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9216 error_at (capture_token
->location
,
9217 "capture of non-variable %qD ",
9219 inform (0, "%q+#D declared here", capture_init_expr
);
9222 if (VAR_P (capture_init_expr
)
9223 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9225 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9226 "%qD with non-automatic storage duration",
9228 inform (0, "%q+#D declared here", capture_init_expr
);
9233 = finish_id_expression
9238 /*integral_constant_expression_p=*/false,
9239 /*allow_non_integral_constant_expression_p=*/false,
9240 /*non_integral_constant_expression_p=*/NULL
,
9241 /*template_p=*/false,
9243 /*address_p=*/false,
9244 /*template_arg_p=*/false,
9246 capture_token
->location
);
9248 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9250 cp_lexer_consume_token (parser
->lexer
);
9251 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9254 check_for_bare_parameter_packs (capture_init_expr
);
9257 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9258 && !explicit_init_p
)
9260 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9261 && capture_kind
== BY_COPY
)
9262 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9263 "of %qD redundant with by-copy capture default",
9265 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9266 && capture_kind
== BY_REFERENCE
)
9267 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9268 "capture of %qD redundant with by-reference capture "
9269 "default", capture_id
);
9272 add_capture (lambda_expr
,
9275 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9279 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9282 /* Parse the (optional) middle of a lambda expression.
9285 < template-parameter-list [opt] >
9286 ( parameter-declaration-clause [opt] )
9287 attribute-specifier [opt]
9289 exception-specification [opt]
9290 lambda-return-type-clause [opt]
9292 LAMBDA_EXPR is the current representation of the lambda expression. */
9295 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9297 /* 5.1.1.4 of the standard says:
9298 If a lambda-expression does not include a lambda-declarator, it is as if
9299 the lambda-declarator were ().
9300 This means an empty parameter list, no attributes, and no exception
9302 tree param_list
= void_list_node
;
9303 tree attributes
= NULL_TREE
;
9304 tree exception_spec
= NULL_TREE
;
9305 tree template_param_list
= NULL_TREE
;
9307 /* The template-parameter-list is optional, but must begin with
9308 an opening angle if present. */
9309 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9311 if (cxx_dialect
< cxx14
)
9312 pedwarn (parser
->lexer
->next_token
->location
, 0,
9313 "lambda templates are only available with "
9314 "-std=c++14 or -std=gnu++14");
9316 cp_lexer_consume_token (parser
->lexer
);
9318 template_param_list
= cp_parser_template_parameter_list (parser
);
9320 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9322 /* We just processed one more parameter list. */
9323 ++parser
->num_template_parameter_lists
;
9326 /* The parameter-declaration-clause is optional (unless
9327 template-parameter-list was given), but must begin with an
9328 opening parenthesis if present. */
9329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9331 cp_lexer_consume_token (parser
->lexer
);
9333 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9335 /* Parse parameters. */
9336 param_list
= cp_parser_parameter_declaration_clause (parser
);
9338 /* Default arguments shall not be specified in the
9339 parameter-declaration-clause of a lambda-declarator. */
9340 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9341 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9342 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9343 "default argument specified for lambda parameter");
9345 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9347 attributes
= cp_parser_attributes_opt (parser
);
9349 /* Parse optional `mutable' keyword. */
9350 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9352 cp_lexer_consume_token (parser
->lexer
);
9353 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9356 /* Parse optional exception specification. */
9357 exception_spec
= cp_parser_exception_specification_opt (parser
);
9359 /* Parse optional trailing return type. */
9360 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9362 cp_lexer_consume_token (parser
->lexer
);
9363 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9364 = cp_parser_trailing_type_id (parser
);
9367 /* The function parameters must be in scope all the way until after the
9368 trailing-return-type in case of decltype. */
9369 pop_bindings_and_leave_scope ();
9371 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9372 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9374 /* Create the function call operator.
9376 Messing with declarators like this is no uglier than building up the
9377 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9380 cp_decl_specifier_seq return_type_specs
;
9381 cp_declarator
* declarator
;
9386 clear_decl_specs (&return_type_specs
);
9387 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9388 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9390 /* Maybe we will deduce the return type later. */
9391 return_type_specs
.type
= make_auto ();
9393 p
= obstack_alloc (&declarator_obstack
, 0);
9395 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9398 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9399 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9400 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9401 VIRT_SPEC_UNSPECIFIED
,
9404 /*late_return_type=*/NULL_TREE
);
9405 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9407 fco
= grokmethod (&return_type_specs
,
9410 if (fco
!= error_mark_node
)
9412 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9413 DECL_ARTIFICIAL (fco
) = 1;
9414 /* Give the object parameter a different name. */
9415 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9416 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9417 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9419 if (template_param_list
)
9421 fco
= finish_member_template_decl (fco
);
9422 finish_template_decl (template_param_list
);
9423 --parser
->num_template_parameter_lists
;
9425 else if (parser
->fully_implicit_function_template_p
)
9426 fco
= finish_fully_implicit_template (parser
, fco
);
9428 finish_member_declaration (fco
);
9430 obstack_free (&declarator_obstack
, p
);
9432 return (fco
!= error_mark_node
);
9436 /* Parse the body of a lambda expression, which is simply
9440 but which requires special handling.
9441 LAMBDA_EXPR is the current representation of the lambda expression. */
9444 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9446 bool nested
= (current_function_decl
!= NULL_TREE
);
9447 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9449 push_function_context ();
9451 /* Still increment function_depth so that we don't GC in the
9452 middle of an expression. */
9454 /* Clear this in case we're in the middle of a default argument. */
9455 parser
->local_variables_forbidden_p
= false;
9457 /* Finish the function call operator
9459 + late_parsing_for_member
9460 + function_definition_after_declarator
9461 + ctor_initializer_opt_and_function_body */
9463 tree fco
= lambda_function (lambda_expr
);
9469 /* Let the front end know that we are going to be defining this
9471 start_preparsed_function (fco
,
9473 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9475 start_lambda_scope (fco
);
9476 body
= begin_function_body ();
9478 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9481 /* Push the proxies for any explicit captures. */
9482 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9483 cap
= TREE_CHAIN (cap
))
9484 build_capture_proxy (TREE_PURPOSE (cap
));
9486 compound_stmt
= begin_compound_stmt (0);
9488 /* 5.1.1.4 of the standard says:
9489 If a lambda-expression does not include a trailing-return-type, it
9490 is as if the trailing-return-type denotes the following type:
9491 * if the compound-statement is of the form
9492 { return attribute-specifier [opt] expression ; }
9493 the type of the returned expression after lvalue-to-rvalue
9494 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9495 (_conv.array_ 4.2), and function-to-pointer conversion
9497 * otherwise, void. */
9499 /* In a lambda that has neither a lambda-return-type-clause
9500 nor a deducible form, errors should be reported for return statements
9501 in the body. Since we used void as the placeholder return type, parsing
9502 the body as usual will give such desired behavior. */
9503 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9504 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9505 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9507 tree expr
= NULL_TREE
;
9508 cp_id_kind idk
= CP_ID_KIND_NONE
;
9510 /* Parse tentatively in case there's more after the initial return
9512 cp_parser_parse_tentatively (parser
);
9514 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9516 expr
= cp_parser_expression (parser
, &idk
);
9518 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9519 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9521 if (cp_parser_parse_definitely (parser
))
9523 if (!processing_template_decl
)
9524 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9526 /* Will get error here if type not deduced yet. */
9527 finish_return_stmt (expr
);
9535 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9536 cp_parser_label_declaration (parser
);
9537 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9538 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9541 finish_compound_stmt (compound_stmt
);
9544 finish_function_body (body
);
9545 finish_lambda_scope ();
9547 /* Finish the function and generate code for it if necessary. */
9548 tree fn
= finish_function (/*inline*/2);
9550 /* Only expand if the call op is not a template. */
9551 if (!DECL_TEMPLATE_INFO (fco
))
9552 expand_or_defer_fn (fn
);
9555 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9557 pop_function_context();
9562 /* Statements [gram.stmt.stmt] */
9564 /* Parse a statement.
9568 expression-statement
9573 declaration-statement
9580 attribute-specifier-seq (opt) expression-statement
9581 attribute-specifier-seq (opt) compound-statement
9582 attribute-specifier-seq (opt) selection-statement
9583 attribute-specifier-seq (opt) iteration-statement
9584 attribute-specifier-seq (opt) jump-statement
9585 declaration-statement
9586 attribute-specifier-seq (opt) try-block
9593 IN_COMPOUND is true when the statement is nested inside a
9594 cp_parser_compound_statement; this matters for certain pragmas.
9596 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9597 is a (possibly labeled) if statement which is not enclosed in braces
9598 and has an else clause. This is used to implement -Wparentheses. */
9601 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9602 bool in_compound
, bool *if_p
)
9604 tree statement
, std_attrs
= NULL_TREE
;
9606 location_t statement_location
, attrs_location
;
9611 /* There is no statement yet. */
9612 statement
= NULL_TREE
;
9614 saved_token_sentinel
saved_tokens (parser
->lexer
);
9615 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9616 if (c_dialect_objc ())
9617 /* In obj-c++, seeing '[[' might be the either the beginning of
9618 c++11 attributes, or a nested objc-message-expression. So
9619 let's parse the c++11 attributes tentatively. */
9620 cp_parser_parse_tentatively (parser
);
9621 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9622 if (c_dialect_objc ())
9624 if (!cp_parser_parse_definitely (parser
))
9625 std_attrs
= NULL_TREE
;
9628 /* Peek at the next token. */
9629 token
= cp_lexer_peek_token (parser
->lexer
);
9630 /* Remember the location of the first token in the statement. */
9631 statement_location
= token
->location
;
9632 /* If this is a keyword, then that will often determine what kind of
9633 statement we have. */
9634 if (token
->type
== CPP_KEYWORD
)
9636 enum rid keyword
= token
->keyword
;
9642 /* Looks like a labeled-statement with a case label.
9643 Parse the label, and then use tail recursion to parse
9645 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9650 statement
= cp_parser_selection_statement (parser
, if_p
);
9656 statement
= cp_parser_iteration_statement (parser
, false);
9662 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9663 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9664 cp_lexer_consume_token (parser
->lexer
);
9665 statement
= error_mark_node
;
9668 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9675 statement
= cp_parser_jump_statement (parser
);
9679 cp_lexer_consume_token (parser
->lexer
);
9682 tree sync_expr
= build_cilk_sync ();
9683 SET_EXPR_LOCATION (sync_expr
,
9685 statement
= finish_expr_stmt (sync_expr
);
9689 error_at (token
->location
, "-fcilkplus must be enabled to use"
9691 statement
= error_mark_node
;
9693 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9696 /* Objective-C++ exception-handling constructs. */
9699 case RID_AT_FINALLY
:
9700 case RID_AT_SYNCHRONIZED
:
9702 statement
= cp_parser_objc_statement (parser
);
9706 statement
= cp_parser_try_block (parser
);
9710 /* This must be a namespace alias definition. */
9711 cp_parser_declaration_statement (parser
);
9714 case RID_TRANSACTION_ATOMIC
:
9715 case RID_TRANSACTION_RELAXED
:
9716 statement
= cp_parser_transaction (parser
, keyword
);
9718 case RID_TRANSACTION_CANCEL
:
9719 statement
= cp_parser_transaction_cancel (parser
);
9723 /* It might be a keyword like `int' that can start a
9724 declaration-statement. */
9728 else if (token
->type
== CPP_NAME
)
9730 /* If the next token is a `:', then we are looking at a
9731 labeled-statement. */
9732 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9733 if (token
->type
== CPP_COLON
)
9735 /* Looks like a labeled-statement with an ordinary label.
9736 Parse the label, and then use tail recursion to parse
9739 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9743 /* Anything that starts with a `{' must be a compound-statement. */
9744 else if (token
->type
== CPP_OPEN_BRACE
)
9745 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9746 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9747 a statement all its own. */
9748 else if (token
->type
== CPP_PRAGMA
)
9750 /* Only certain OpenMP pragmas are attached to statements, and thus
9751 are considered statements themselves. All others are not. In
9752 the context of a compound, accept the pragma as a "statement" and
9753 return so that we can check for a close brace. Otherwise we
9754 require a real statement and must go back and read one. */
9756 cp_parser_pragma (parser
, pragma_compound
);
9757 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9761 else if (token
->type
== CPP_EOF
)
9763 cp_parser_error (parser
, "expected statement");
9767 /* Everything else must be a declaration-statement or an
9768 expression-statement. Try for the declaration-statement
9769 first, unless we are looking at a `;', in which case we know that
9770 we have an expression-statement. */
9773 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9775 if (std_attrs
!= NULL_TREE
)
9777 /* Attributes should be parsed as part of the the
9778 declaration, so let's un-parse them. */
9779 saved_tokens
.rollback();
9780 std_attrs
= NULL_TREE
;
9783 cp_parser_parse_tentatively (parser
);
9784 /* Try to parse the declaration-statement. */
9785 cp_parser_declaration_statement (parser
);
9786 /* If that worked, we're done. */
9787 if (cp_parser_parse_definitely (parser
))
9790 /* Look for an expression-statement instead. */
9791 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9794 /* Set the line number for the statement. */
9795 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9796 SET_EXPR_LOCATION (statement
, statement_location
);
9798 /* Note that for now, we don't do anything with c++11 statements
9799 parsed at this level. */
9800 if (std_attrs
!= NULL_TREE
)
9801 warning_at (attrs_location
,
9803 "attributes at the beginning of statement are ignored");
9806 /* Parse the label for a labeled-statement, i.e.
9809 case constant-expression :
9813 case constant-expression ... constant-expression : statement
9815 When a label is parsed without errors, the label is added to the
9816 parse tree by the finish_* functions, so this function doesn't
9817 have to return the label. */
9820 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9823 tree label
= NULL_TREE
;
9824 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9826 /* The next token should be an identifier. */
9827 token
= cp_lexer_peek_token (parser
->lexer
);
9828 if (token
->type
!= CPP_NAME
9829 && token
->type
!= CPP_KEYWORD
)
9831 cp_parser_error (parser
, "expected labeled-statement");
9835 parser
->colon_corrects_to_scope_p
= false;
9836 switch (token
->keyword
)
9843 /* Consume the `case' token. */
9844 cp_lexer_consume_token (parser
->lexer
);
9845 /* Parse the constant-expression. */
9846 expr
= cp_parser_constant_expression (parser
);
9847 if (check_for_bare_parameter_packs (expr
))
9848 expr
= error_mark_node
;
9850 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9851 if (ellipsis
->type
== CPP_ELLIPSIS
)
9853 /* Consume the `...' token. */
9854 cp_lexer_consume_token (parser
->lexer
);
9855 expr_hi
= cp_parser_constant_expression (parser
);
9856 if (check_for_bare_parameter_packs (expr_hi
))
9857 expr_hi
= error_mark_node
;
9859 /* We don't need to emit warnings here, as the common code
9860 will do this for us. */
9863 expr_hi
= NULL_TREE
;
9865 if (parser
->in_switch_statement_p
)
9866 finish_case_label (token
->location
, expr
, expr_hi
);
9868 error_at (token
->location
,
9869 "case label %qE not within a switch statement",
9875 /* Consume the `default' token. */
9876 cp_lexer_consume_token (parser
->lexer
);
9878 if (parser
->in_switch_statement_p
)
9879 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9881 error_at (token
->location
, "case label not within a switch statement");
9885 /* Anything else must be an ordinary label. */
9886 label
= finish_label_stmt (cp_parser_identifier (parser
));
9890 /* Require the `:' token. */
9891 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9893 /* An ordinary label may optionally be followed by attributes.
9894 However, this is only permitted if the attributes are then
9895 followed by a semicolon. This is because, for backward
9896 compatibility, when parsing
9897 lab: __attribute__ ((unused)) int i;
9898 we want the attribute to attach to "i", not "lab". */
9899 if (label
!= NULL_TREE
9900 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9903 cp_parser_parse_tentatively (parser
);
9904 attrs
= cp_parser_gnu_attributes_opt (parser
);
9905 if (attrs
== NULL_TREE
9906 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9907 cp_parser_abort_tentative_parse (parser
);
9908 else if (!cp_parser_parse_definitely (parser
))
9911 attributes
= chainon (attributes
, attrs
);
9914 if (attributes
!= NULL_TREE
)
9915 cplus_decl_attributes (&label
, attributes
, 0);
9917 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9920 /* Parse an expression-statement.
9922 expression-statement:
9925 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9926 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9927 indicates whether this expression-statement is part of an
9928 expression statement. */
9931 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9933 tree statement
= NULL_TREE
;
9934 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9936 /* If the next token is a ';', then there is no expression
9938 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9940 statement
= cp_parser_expression (parser
);
9941 if (statement
== error_mark_node
9942 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9944 cp_parser_skip_to_end_of_block_or_statement (parser
);
9945 return error_mark_node
;
9949 /* Give a helpful message for "A<T>::type t;" and the like. */
9950 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9951 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9953 if (TREE_CODE (statement
) == SCOPE_REF
)
9954 error_at (token
->location
, "need %<typename%> before %qE because "
9955 "%qT is a dependent scope",
9956 statement
, TREE_OPERAND (statement
, 0));
9957 else if (is_overloaded_fn (statement
)
9958 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9961 tree fn
= get_first_fn (statement
);
9962 error_at (token
->location
,
9963 "%<%T::%D%> names the constructor, not the type",
9964 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9968 /* Consume the final `;'. */
9969 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9971 if (in_statement_expr
9972 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9973 /* This is the final expression statement of a statement
9975 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9977 statement
= finish_expr_stmt (statement
);
9982 /* Parse a compound-statement.
9985 { statement-seq [opt] }
9990 { label-declaration-seq [opt] statement-seq [opt] }
9992 label-declaration-seq:
9994 label-declaration-seq label-declaration
9996 Returns a tree representing the statement. */
9999 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10000 bool in_try
, bool function_body
)
10002 tree compound_stmt
;
10004 /* Consume the `{'. */
10005 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10006 return error_mark_node
;
10007 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10008 && !function_body
&& cxx_dialect
< cxx14
)
10009 pedwarn (input_location
, OPT_Wpedantic
,
10010 "compound-statement in constexpr function");
10011 /* Begin the compound-statement. */
10012 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
10013 /* If the next keyword is `__label__' we have a label declaration. */
10014 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10015 cp_parser_label_declaration (parser
);
10016 /* Parse an (optional) statement-seq. */
10017 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10018 /* Finish the compound-statement. */
10019 finish_compound_stmt (compound_stmt
);
10020 /* Consume the `}'. */
10021 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10023 return compound_stmt
;
10026 /* Parse an (optional) statement-seq.
10030 statement-seq [opt] statement */
10033 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10035 /* Scan statements until there aren't any more. */
10038 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10040 /* If we are looking at a `}', then we have run out of
10041 statements; the same is true if we have reached the end
10042 of file, or have stumbled upon a stray '@end'. */
10043 if (token
->type
== CPP_CLOSE_BRACE
10044 || token
->type
== CPP_EOF
10045 || token
->type
== CPP_PRAGMA_EOL
10046 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10049 /* If we are in a compound statement and find 'else' then
10050 something went wrong. */
10051 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10053 if (parser
->in_statement
& IN_IF_STMT
)
10057 token
= cp_lexer_consume_token (parser
->lexer
);
10058 error_at (token
->location
, "%<else%> without a previous %<if%>");
10062 /* Parse the statement. */
10063 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10067 /* Parse a selection-statement.
10069 selection-statement:
10070 if ( condition ) statement
10071 if ( condition ) statement else statement
10072 switch ( condition ) statement
10074 Returns the new IF_STMT or SWITCH_STMT.
10076 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10077 is a (possibly labeled) if statement which is not enclosed in
10078 braces and has an else clause. This is used to implement
10082 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10090 /* Peek at the next token. */
10091 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10093 /* See what kind of keyword it is. */
10094 keyword
= token
->keyword
;
10103 /* Look for the `('. */
10104 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10106 cp_parser_skip_to_end_of_statement (parser
);
10107 return error_mark_node
;
10110 /* Begin the selection-statement. */
10111 if (keyword
== RID_IF
)
10112 statement
= begin_if_stmt ();
10114 statement
= begin_switch_stmt ();
10116 /* Parse the condition. */
10117 condition
= cp_parser_condition (parser
);
10118 /* Look for the `)'. */
10119 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10120 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10121 /*consume_paren=*/true);
10123 if (keyword
== RID_IF
)
10126 unsigned char in_statement
;
10128 /* Add the condition. */
10129 finish_if_stmt_cond (condition
, statement
);
10131 /* Parse the then-clause. */
10132 in_statement
= parser
->in_statement
;
10133 parser
->in_statement
|= IN_IF_STMT
;
10134 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10136 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10137 add_stmt (build_empty_stmt (loc
));
10138 cp_lexer_consume_token (parser
->lexer
);
10139 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10140 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10141 "empty body in an %<if%> statement");
10145 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
10146 parser
->in_statement
= in_statement
;
10148 finish_then_clause (statement
);
10150 /* If the next token is `else', parse the else-clause. */
10151 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10154 /* Consume the `else' keyword. */
10155 cp_lexer_consume_token (parser
->lexer
);
10156 begin_else_clause (statement
);
10157 /* Parse the else-clause. */
10158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10161 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10163 OPT_Wempty_body
, "suggest braces around "
10164 "empty body in an %<else%> statement");
10165 add_stmt (build_empty_stmt (loc
));
10166 cp_lexer_consume_token (parser
->lexer
);
10169 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10171 finish_else_clause (statement
);
10173 /* If we are currently parsing a then-clause, then
10174 IF_P will not be NULL. We set it to true to
10175 indicate that this if statement has an else clause.
10176 This may trigger the Wparentheses warning below
10177 when we get back up to the parent if statement. */
10183 /* This if statement does not have an else clause. If
10184 NESTED_IF is true, then the then-clause is an if
10185 statement which does have an else clause. We warn
10186 about the potential ambiguity. */
10188 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10189 "suggest explicit braces to avoid ambiguous"
10193 /* Now we're all done with the if-statement. */
10194 finish_if_stmt (statement
);
10198 bool in_switch_statement_p
;
10199 unsigned char in_statement
;
10201 /* Add the condition. */
10202 finish_switch_cond (condition
, statement
);
10204 /* Parse the body of the switch-statement. */
10205 in_switch_statement_p
= parser
->in_switch_statement_p
;
10206 in_statement
= parser
->in_statement
;
10207 parser
->in_switch_statement_p
= true;
10208 parser
->in_statement
|= IN_SWITCH_STMT
;
10209 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10210 parser
->in_switch_statement_p
= in_switch_statement_p
;
10211 parser
->in_statement
= in_statement
;
10213 /* Now we're all done with the switch-statement. */
10214 finish_switch_stmt (statement
);
10222 cp_parser_error (parser
, "expected selection-statement");
10223 return error_mark_node
;
10227 /* Parse a condition.
10231 type-specifier-seq declarator = initializer-clause
10232 type-specifier-seq declarator braced-init-list
10237 type-specifier-seq declarator asm-specification [opt]
10238 attributes [opt] = assignment-expression
10240 Returns the expression that should be tested. */
10243 cp_parser_condition (cp_parser
* parser
)
10245 cp_decl_specifier_seq type_specifiers
;
10246 const char *saved_message
;
10247 int declares_class_or_enum
;
10249 /* Try the declaration first. */
10250 cp_parser_parse_tentatively (parser
);
10251 /* New types are not allowed in the type-specifier-seq for a
10253 saved_message
= parser
->type_definition_forbidden_message
;
10254 parser
->type_definition_forbidden_message
10255 = G_("types may not be defined in conditions");
10256 /* Parse the type-specifier-seq. */
10257 cp_parser_decl_specifier_seq (parser
,
10258 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10260 &declares_class_or_enum
);
10261 /* Restore the saved message. */
10262 parser
->type_definition_forbidden_message
= saved_message
;
10263 /* If all is well, we might be looking at a declaration. */
10264 if (!cp_parser_error_occurred (parser
))
10267 tree asm_specification
;
10269 cp_declarator
*declarator
;
10270 tree initializer
= NULL_TREE
;
10272 /* Parse the declarator. */
10273 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10274 /*ctor_dtor_or_conv_p=*/NULL
,
10275 /*parenthesized_p=*/NULL
,
10276 /*member_p=*/false,
10277 /*friend_p=*/false);
10278 /* Parse the attributes. */
10279 attributes
= cp_parser_attributes_opt (parser
);
10280 /* Parse the asm-specification. */
10281 asm_specification
= cp_parser_asm_specification_opt (parser
);
10282 /* If the next token is not an `=' or '{', then we might still be
10283 looking at an expression. For example:
10287 looks like a decl-specifier-seq and a declarator -- but then
10288 there is no `=', so this is an expression. */
10289 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10290 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10291 cp_parser_simulate_error (parser
);
10293 /* If we did see an `=' or '{', then we are looking at a declaration
10295 if (cp_parser_parse_definitely (parser
))
10298 bool non_constant_p
;
10299 bool flags
= LOOKUP_ONLYCONVERTING
;
10301 /* Create the declaration. */
10302 decl
= start_decl (declarator
, &type_specifiers
,
10303 /*initialized_p=*/true,
10304 attributes
, /*prefix_attributes=*/NULL_TREE
,
10307 /* Parse the initializer. */
10308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10310 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10311 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10316 /* Consume the `='. */
10317 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10318 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10320 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10321 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10323 /* Process the initializer. */
10324 cp_finish_decl (decl
,
10325 initializer
, !non_constant_p
,
10330 pop_scope (pushed_scope
);
10332 return convert_from_reference (decl
);
10335 /* If we didn't even get past the declarator successfully, we are
10336 definitely not looking at a declaration. */
10338 cp_parser_abort_tentative_parse (parser
);
10340 /* Otherwise, we are looking at an expression. */
10341 return cp_parser_expression (parser
);
10344 /* Parses a for-statement or range-for-statement until the closing ')',
10348 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10350 tree init
, scope
, decl
;
10353 /* Begin the for-statement. */
10354 scope
= begin_for_scope (&init
);
10356 /* Parse the initialization. */
10357 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10360 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10362 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10366 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10368 /* Normal for loop */
10369 tree condition
= NULL_TREE
;
10370 tree expression
= NULL_TREE
;
10373 stmt
= begin_for_stmt (scope
, init
);
10374 /* The for-init-statement has already been parsed in
10375 cp_parser_for_init_statement, so no work is needed here. */
10376 finish_for_init_stmt (stmt
);
10378 /* If there's a condition, process it. */
10379 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10380 condition
= cp_parser_condition (parser
);
10383 cp_parser_error (parser
, "missing loop condition in loop with "
10384 "%<GCC ivdep%> pragma");
10385 condition
= error_mark_node
;
10387 finish_for_cond (condition
, stmt
, ivdep
);
10388 /* Look for the `;'. */
10389 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10391 /* If there's an expression, process it. */
10392 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10393 expression
= cp_parser_expression (parser
);
10394 finish_for_expr (expression
, stmt
);
10399 /* Tries to parse a range-based for-statement:
10402 decl-specifier-seq declarator : expression
10404 The decl-specifier-seq declarator and the `:' are already parsed by
10405 cp_parser_for_init_statement. If processing_template_decl it returns a
10406 newly created RANGE_FOR_STMT; if not, it is converted to a
10407 regular FOR_STMT. */
10410 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10413 tree stmt
, range_expr
;
10415 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10417 bool expr_non_constant_p
;
10418 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10421 range_expr
= cp_parser_expression (parser
);
10423 /* If in template, STMT is converted to a normal for-statement
10424 at instantiation. If not, it is done just ahead. */
10425 if (processing_template_decl
)
10427 if (check_for_bare_parameter_packs (range_expr
))
10428 range_expr
= error_mark_node
;
10429 stmt
= begin_range_for_stmt (scope
, init
);
10431 RANGE_FOR_IVDEP (stmt
) = 1;
10432 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10433 if (!type_dependent_expression_p (range_expr
)
10434 /* do_auto_deduction doesn't mess with template init-lists. */
10435 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10436 do_range_for_auto_deduction (range_decl
, range_expr
);
10440 stmt
= begin_for_stmt (scope
, init
);
10441 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10446 /* Subroutine of cp_convert_range_for: given the initializer expression,
10447 builds up the range temporary. */
10450 build_range_temp (tree range_expr
)
10452 tree range_type
, range_temp
;
10454 /* Find out the type deduced by the declaration
10455 `auto &&__range = range_expr'. */
10456 range_type
= cp_build_reference_type (make_auto (), true);
10457 range_type
= do_auto_deduction (range_type
, range_expr
,
10458 type_uses_auto (range_type
));
10460 /* Create the __range variable. */
10461 range_temp
= build_decl (input_location
, VAR_DECL
,
10462 get_identifier ("__for_range"), range_type
);
10463 TREE_USED (range_temp
) = 1;
10464 DECL_ARTIFICIAL (range_temp
) = 1;
10469 /* Used by cp_parser_range_for in template context: we aren't going to
10470 do a full conversion yet, but we still need to resolve auto in the
10471 type of the for-range-declaration if present. This is basically
10472 a shortcut version of cp_convert_range_for. */
10475 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10477 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10480 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10481 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10482 iter_type
= (cp_parser_perform_range_for_lookup
10483 (range_temp
, &begin_dummy
, &end_dummy
));
10486 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10488 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10489 tf_warning_or_error
);
10490 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10491 iter_decl
, auto_node
);
10496 /* Converts a range-based for-statement into a normal
10497 for-statement, as per the definition.
10499 for (RANGE_DECL : RANGE_EXPR)
10502 should be equivalent to:
10505 auto &&__range = RANGE_EXPR;
10506 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10510 RANGE_DECL = *__begin;
10515 If RANGE_EXPR is an array:
10516 BEGIN_EXPR = __range
10517 END_EXPR = __range + ARRAY_SIZE(__range)
10518 Else if RANGE_EXPR has a member 'begin' or 'end':
10519 BEGIN_EXPR = __range.begin()
10520 END_EXPR = __range.end()
10522 BEGIN_EXPR = begin(__range)
10523 END_EXPR = end(__range);
10525 If __range has a member 'begin' but not 'end', or vice versa, we must
10526 still use the second alternative (it will surely fail, however).
10527 When calling begin()/end() in the third alternative we must use
10528 argument dependent lookup, but always considering 'std' as an associated
10532 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10536 tree iter_type
, begin_expr
, end_expr
;
10537 tree condition
, expression
;
10539 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10540 /* If an error happened previously do nothing or else a lot of
10541 unhelpful errors would be issued. */
10542 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10547 if (TREE_CODE (range_expr
) == VAR_DECL
10548 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10549 /* Can't bind a reference to an array of runtime bound. */
10550 range_temp
= range_expr
;
10553 range_temp
= build_range_temp (range_expr
);
10554 pushdecl (range_temp
);
10555 cp_finish_decl (range_temp
, range_expr
,
10556 /*is_constant_init*/false, NULL_TREE
,
10557 LOOKUP_ONLYCONVERTING
);
10558 range_temp
= convert_from_reference (range_temp
);
10560 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10561 &begin_expr
, &end_expr
);
10564 /* The new for initialization statement. */
10565 begin
= build_decl (input_location
, VAR_DECL
,
10566 get_identifier ("__for_begin"), iter_type
);
10567 TREE_USED (begin
) = 1;
10568 DECL_ARTIFICIAL (begin
) = 1;
10570 cp_finish_decl (begin
, begin_expr
,
10571 /*is_constant_init*/false, NULL_TREE
,
10572 LOOKUP_ONLYCONVERTING
);
10574 end
= build_decl (input_location
, VAR_DECL
,
10575 get_identifier ("__for_end"), iter_type
);
10576 TREE_USED (end
) = 1;
10577 DECL_ARTIFICIAL (end
) = 1;
10579 cp_finish_decl (end
, end_expr
,
10580 /*is_constant_init*/false, NULL_TREE
,
10581 LOOKUP_ONLYCONVERTING
);
10583 finish_for_init_stmt (statement
);
10585 /* The new for condition. */
10586 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10589 NULL
, tf_warning_or_error
);
10590 finish_for_cond (condition
, statement
, ivdep
);
10592 /* The new increment expression. */
10593 expression
= finish_unary_op_expr (input_location
,
10594 PREINCREMENT_EXPR
, begin
,
10595 tf_warning_or_error
);
10596 finish_for_expr (expression
, statement
);
10598 /* The declaration is initialized with *__begin inside the loop body. */
10599 cp_finish_decl (range_decl
,
10600 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10601 tf_warning_or_error
),
10602 /*is_constant_init*/false, NULL_TREE
,
10603 LOOKUP_ONLYCONVERTING
);
10608 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10609 We need to solve both at the same time because the method used
10610 depends on the existence of members begin or end.
10611 Returns the type deduced for the iterator expression. */
10614 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10616 if (error_operand_p (range
))
10618 *begin
= *end
= error_mark_node
;
10619 return error_mark_node
;
10622 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10624 error ("range-based %<for%> expression of type %qT "
10625 "has incomplete type", TREE_TYPE (range
));
10626 *begin
= *end
= error_mark_node
;
10627 return error_mark_node
;
10629 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10631 /* If RANGE is an array, we will use pointer arithmetic. */
10633 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10635 array_type_nelts_top (TREE_TYPE (range
)),
10637 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10641 /* If it is not an array, we must do a bit of magic. */
10642 tree id_begin
, id_end
;
10643 tree member_begin
, member_end
;
10645 *begin
= *end
= error_mark_node
;
10647 id_begin
= get_identifier ("begin");
10648 id_end
= get_identifier ("end");
10649 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10650 /*protect=*/2, /*want_type=*/false,
10651 tf_warning_or_error
);
10652 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10653 /*protect=*/2, /*want_type=*/false,
10654 tf_warning_or_error
);
10656 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10658 /* Use the member functions. */
10659 if (member_begin
!= NULL_TREE
)
10660 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10662 error ("range-based %<for%> expression of type %qT has an "
10663 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10665 if (member_end
!= NULL_TREE
)
10666 *end
= cp_parser_range_for_member_function (range
, id_end
);
10668 error ("range-based %<for%> expression of type %qT has a "
10669 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10673 /* Use global functions with ADL. */
10674 vec
<tree
, va_gc
> *vec
;
10675 vec
= make_tree_vector ();
10677 vec_safe_push (vec
, range
);
10679 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10680 tf_warning_or_error
);
10681 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10682 tf_warning_or_error
);
10683 member_end
= perform_koenig_lookup (id_end
, vec
,
10684 tf_warning_or_error
);
10685 *end
= finish_call_expr (member_end
, &vec
, false, true,
10686 tf_warning_or_error
);
10688 release_tree_vector (vec
);
10691 /* Last common checks. */
10692 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10694 /* If one of the expressions is an error do no more checks. */
10695 *begin
= *end
= error_mark_node
;
10696 return error_mark_node
;
10698 else if (type_dependent_expression_p (*begin
)
10699 || type_dependent_expression_p (*end
))
10700 /* Can happen, when, eg, in a template context, Koenig lookup
10701 can't resolve begin/end (c++/58503). */
10705 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10706 /* The unqualified type of the __begin and __end temporaries should
10707 be the same, as required by the multiple auto declaration. */
10708 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10709 error ("inconsistent begin/end types in range-based %<for%> "
10710 "statement: %qT and %qT",
10711 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10717 /* Helper function for cp_parser_perform_range_for_lookup.
10718 Builds a tree for RANGE.IDENTIFIER(). */
10721 cp_parser_range_for_member_function (tree range
, tree identifier
)
10724 vec
<tree
, va_gc
> *vec
;
10726 member
= finish_class_member_access_expr (range
, identifier
,
10727 false, tf_warning_or_error
);
10728 if (member
== error_mark_node
)
10729 return error_mark_node
;
10731 vec
= make_tree_vector ();
10732 res
= finish_call_expr (member
, &vec
,
10733 /*disallow_virtual=*/false,
10734 /*koenig_p=*/false,
10735 tf_warning_or_error
);
10736 release_tree_vector (vec
);
10740 /* Parse an iteration-statement.
10742 iteration-statement:
10743 while ( condition ) statement
10744 do statement while ( expression ) ;
10745 for ( for-init-statement condition [opt] ; expression [opt] )
10748 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10751 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10756 unsigned char in_statement
;
10758 /* Peek at the next token. */
10759 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10761 return error_mark_node
;
10763 /* Remember whether or not we are already within an iteration
10765 in_statement
= parser
->in_statement
;
10767 /* See what kind of keyword it is. */
10768 keyword
= token
->keyword
;
10775 /* Begin the while-statement. */
10776 statement
= begin_while_stmt ();
10777 /* Look for the `('. */
10778 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10779 /* Parse the condition. */
10780 condition
= cp_parser_condition (parser
);
10781 finish_while_stmt_cond (condition
, statement
, ivdep
);
10782 /* Look for the `)'. */
10783 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10784 /* Parse the dependent statement. */
10785 parser
->in_statement
= IN_ITERATION_STMT
;
10786 cp_parser_already_scoped_statement (parser
);
10787 parser
->in_statement
= in_statement
;
10788 /* We're done with the while-statement. */
10789 finish_while_stmt (statement
);
10797 /* Begin the do-statement. */
10798 statement
= begin_do_stmt ();
10799 /* Parse the body of the do-statement. */
10800 parser
->in_statement
= IN_ITERATION_STMT
;
10801 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10802 parser
->in_statement
= in_statement
;
10803 finish_do_body (statement
);
10804 /* Look for the `while' keyword. */
10805 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10806 /* Look for the `('. */
10807 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10808 /* Parse the expression. */
10809 expression
= cp_parser_expression (parser
);
10810 /* We're done with the do-statement. */
10811 finish_do_stmt (expression
, statement
, ivdep
);
10812 /* Look for the `)'. */
10813 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10814 /* Look for the `;'. */
10815 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10821 /* Look for the `('. */
10822 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10824 statement
= cp_parser_for (parser
, ivdep
);
10826 /* Look for the `)'. */
10827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10829 /* Parse the body of the for-statement. */
10830 parser
->in_statement
= IN_ITERATION_STMT
;
10831 cp_parser_already_scoped_statement (parser
);
10832 parser
->in_statement
= in_statement
;
10834 /* We're done with the for-statement. */
10835 finish_for_stmt (statement
);
10840 cp_parser_error (parser
, "expected iteration-statement");
10841 statement
= error_mark_node
;
10848 /* Parse a for-init-statement or the declarator of a range-based-for.
10849 Returns true if a range-based-for declaration is seen.
10851 for-init-statement:
10852 expression-statement
10853 simple-declaration */
10856 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10858 /* If the next token is a `;', then we have an empty
10859 expression-statement. Grammatically, this is also a
10860 simple-declaration, but an invalid one, because it does not
10861 declare anything. Therefore, if we did not handle this case
10862 specially, we would issue an error message about an invalid
10864 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10866 bool is_range_for
= false;
10867 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10869 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10870 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10872 /* N3994 -- for (id : init) ... */
10873 if (cxx_dialect
< cxx1z
)
10874 pedwarn (input_location
, 0, "range-based for loop without a "
10875 "type-specifier only available with "
10876 "-std=c++1z or -std=gnu++1z");
10877 tree name
= cp_parser_identifier (parser
);
10878 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10879 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10881 cp_lexer_consume_token (parser
->lexer
);
10885 /* A colon is used in range-based for. */
10886 parser
->colon_corrects_to_scope_p
= false;
10888 /* We're going to speculatively look for a declaration, falling back
10889 to an expression, if necessary. */
10890 cp_parser_parse_tentatively (parser
);
10891 /* Parse the declaration. */
10892 cp_parser_simple_declaration (parser
,
10893 /*function_definition_allowed_p=*/false,
10895 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10898 /* It is a range-for, consume the ':' */
10899 cp_lexer_consume_token (parser
->lexer
);
10900 is_range_for
= true;
10901 if (cxx_dialect
< cxx11
)
10903 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10904 "range-based %<for%> loops only available with "
10905 "-std=c++11 or -std=gnu++11");
10906 *decl
= error_mark_node
;
10910 /* The ';' is not consumed yet because we told
10911 cp_parser_simple_declaration not to. */
10912 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10914 if (cp_parser_parse_definitely (parser
))
10915 return is_range_for
;
10916 /* If the tentative parse failed, then we shall need to look for an
10917 expression-statement. */
10919 /* If we are here, it is an expression-statement. */
10920 cp_parser_expression_statement (parser
, NULL_TREE
);
10924 /* Parse a jump-statement.
10929 return expression [opt] ;
10930 return braced-init-list ;
10936 goto * expression ;
10938 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10941 cp_parser_jump_statement (cp_parser
* parser
)
10943 tree statement
= error_mark_node
;
10946 unsigned char in_statement
;
10948 /* Peek at the next token. */
10949 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10951 return error_mark_node
;
10953 /* See what kind of keyword it is. */
10954 keyword
= token
->keyword
;
10958 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10959 switch (in_statement
)
10962 error_at (token
->location
, "break statement not within loop or switch");
10965 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10966 || in_statement
== IN_ITERATION_STMT
);
10967 statement
= finish_break_stmt ();
10968 if (in_statement
== IN_ITERATION_STMT
)
10969 break_maybe_infinite_loop ();
10972 error_at (token
->location
, "invalid exit from OpenMP structured block");
10975 error_at (token
->location
, "break statement used with OpenMP for loop");
10977 case IN_CILK_SIMD_FOR
:
10978 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10981 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10985 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10988 error_at (token
->location
, "continue statement not within a loop");
10990 case IN_CILK_SIMD_FOR
:
10991 error_at (token
->location
,
10992 "continue statement within %<#pragma simd%> loop body");
10993 /* Fall through. */
10994 case IN_ITERATION_STMT
:
10996 statement
= finish_continue_stmt ();
10999 error_at (token
->location
, "invalid exit from OpenMP structured block");
11002 gcc_unreachable ();
11004 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11010 bool expr_non_constant_p
;
11012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11014 cp_lexer_set_source_position (parser
->lexer
);
11015 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11016 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11018 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11019 expr
= cp_parser_expression (parser
);
11021 /* If the next token is a `;', then there is no
11024 /* Build the return-statement. */
11025 statement
= finish_return_stmt (expr
);
11026 /* Look for the final `;'. */
11027 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11032 if (parser
->in_function_body
11033 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11035 error ("%<goto%> in %<constexpr%> function");
11036 cp_function_chain
->invalid_constexpr
= true;
11039 /* Create the goto-statement. */
11040 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11042 /* Issue a warning about this use of a GNU extension. */
11043 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11044 /* Consume the '*' token. */
11045 cp_lexer_consume_token (parser
->lexer
);
11046 /* Parse the dependent expression. */
11047 finish_goto_stmt (cp_parser_expression (parser
));
11050 finish_goto_stmt (cp_parser_identifier (parser
));
11051 /* Look for the final `;'. */
11052 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11056 cp_parser_error (parser
, "expected jump-statement");
11063 /* Parse a declaration-statement.
11065 declaration-statement:
11066 block-declaration */
11069 cp_parser_declaration_statement (cp_parser
* parser
)
11073 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11074 p
= obstack_alloc (&declarator_obstack
, 0);
11076 /* Parse the block-declaration. */
11077 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11079 /* Free any declarators allocated. */
11080 obstack_free (&declarator_obstack
, p
);
11083 /* Some dependent statements (like `if (cond) statement'), are
11084 implicitly in their own scope. In other words, if the statement is
11085 a single statement (as opposed to a compound-statement), it is
11086 none-the-less treated as if it were enclosed in braces. Any
11087 declarations appearing in the dependent statement are out of scope
11088 after control passes that point. This function parses a statement,
11089 but ensures that is in its own scope, even if it is not a
11090 compound-statement.
11092 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11093 is a (possibly labeled) if statement which is not enclosed in
11094 braces and has an else clause. This is used to implement
11097 Returns the new statement. */
11100 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
11107 /* Mark if () ; with a special NOP_EXPR. */
11108 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11110 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11111 cp_lexer_consume_token (parser
->lexer
);
11112 statement
= add_stmt (build_empty_stmt (loc
));
11114 /* if a compound is opened, we simply parse the statement directly. */
11115 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11116 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11117 /* If the token is not a `{', then we must take special action. */
11120 /* Create a compound-statement. */
11121 statement
= begin_compound_stmt (0);
11122 /* Parse the dependent-statement. */
11123 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11124 /* Finish the dummy compound-statement. */
11125 finish_compound_stmt (statement
);
11128 /* Return the statement. */
11132 /* For some dependent statements (like `while (cond) statement'), we
11133 have already created a scope. Therefore, even if the dependent
11134 statement is a compound-statement, we do not want to create another
11138 cp_parser_already_scoped_statement (cp_parser
* parser
)
11140 /* If the token is a `{', then we must take special action. */
11141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11142 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11145 /* Avoid calling cp_parser_compound_statement, so that we
11146 don't create a new scope. Do everything else by hand. */
11147 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11148 /* If the next keyword is `__label__' we have a label declaration. */
11149 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11150 cp_parser_label_declaration (parser
);
11151 /* Parse an (optional) statement-seq. */
11152 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11153 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11157 /* Declarations [gram.dcl.dcl] */
11159 /* Parse an optional declaration-sequence.
11163 declaration-seq declaration */
11166 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11172 token
= cp_lexer_peek_token (parser
->lexer
);
11174 if (token
->type
== CPP_CLOSE_BRACE
11175 || token
->type
== CPP_EOF
11176 || token
->type
== CPP_PRAGMA_EOL
)
11179 if (token
->type
== CPP_SEMICOLON
)
11181 /* A declaration consisting of a single semicolon is
11182 invalid. Allow it unless we're being pedantic. */
11183 cp_lexer_consume_token (parser
->lexer
);
11184 if (!in_system_header_at (input_location
))
11185 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11189 /* If we're entering or exiting a region that's implicitly
11190 extern "C", modify the lang context appropriately. */
11191 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11193 push_lang_context (lang_name_c
);
11194 parser
->implicit_extern_c
= true;
11196 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11198 pop_lang_context ();
11199 parser
->implicit_extern_c
= false;
11202 if (token
->type
== CPP_PRAGMA
)
11204 /* A top-level declaration can consist solely of a #pragma.
11205 A nested declaration cannot, so this is done here and not
11206 in cp_parser_declaration. (A #pragma at block scope is
11207 handled in cp_parser_statement.) */
11208 cp_parser_pragma (parser
, pragma_external
);
11212 /* Parse the declaration itself. */
11213 cp_parser_declaration (parser
);
11217 /* Parse a declaration.
11221 function-definition
11222 template-declaration
11223 explicit-instantiation
11224 explicit-specialization
11225 linkage-specification
11226 namespace-definition
11231 __extension__ declaration */
11234 cp_parser_declaration (cp_parser
* parser
)
11238 int saved_pedantic
;
11240 tree attributes
= NULL_TREE
;
11242 /* Check for the `__extension__' keyword. */
11243 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11245 /* Parse the qualified declaration. */
11246 cp_parser_declaration (parser
);
11247 /* Restore the PEDANTIC flag. */
11248 pedantic
= saved_pedantic
;
11253 /* Try to figure out what kind of declaration is present. */
11254 token1
= *cp_lexer_peek_token (parser
->lexer
);
11256 if (token1
.type
!= CPP_EOF
)
11257 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11260 token2
.type
= CPP_EOF
;
11261 token2
.keyword
= RID_MAX
;
11264 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11265 p
= obstack_alloc (&declarator_obstack
, 0);
11267 /* If the next token is `extern' and the following token is a string
11268 literal, then we have a linkage specification. */
11269 if (token1
.keyword
== RID_EXTERN
11270 && cp_parser_is_pure_string_literal (&token2
))
11271 cp_parser_linkage_specification (parser
);
11272 /* If the next token is `template', then we have either a template
11273 declaration, an explicit instantiation, or an explicit
11275 else if (token1
.keyword
== RID_TEMPLATE
)
11277 /* `template <>' indicates a template specialization. */
11278 if (token2
.type
== CPP_LESS
11279 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11280 cp_parser_explicit_specialization (parser
);
11281 /* `template <' indicates a template declaration. */
11282 else if (token2
.type
== CPP_LESS
)
11283 cp_parser_template_declaration (parser
, /*member_p=*/false);
11284 /* Anything else must be an explicit instantiation. */
11286 cp_parser_explicit_instantiation (parser
);
11288 /* If the next token is `export', then we have a template
11290 else if (token1
.keyword
== RID_EXPORT
)
11291 cp_parser_template_declaration (parser
, /*member_p=*/false);
11292 /* If the next token is `extern', 'static' or 'inline' and the one
11293 after that is `template', we have a GNU extended explicit
11294 instantiation directive. */
11295 else if (cp_parser_allow_gnu_extensions_p (parser
)
11296 && (token1
.keyword
== RID_EXTERN
11297 || token1
.keyword
== RID_STATIC
11298 || token1
.keyword
== RID_INLINE
)
11299 && token2
.keyword
== RID_TEMPLATE
)
11300 cp_parser_explicit_instantiation (parser
);
11301 /* If the next token is `namespace', check for a named or unnamed
11302 namespace definition. */
11303 else if (token1
.keyword
== RID_NAMESPACE
11304 && (/* A named namespace definition. */
11305 (token2
.type
== CPP_NAME
11306 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11308 /* An unnamed namespace definition. */
11309 || token2
.type
== CPP_OPEN_BRACE
11310 || token2
.keyword
== RID_ATTRIBUTE
))
11311 cp_parser_namespace_definition (parser
);
11312 /* An inline (associated) namespace definition. */
11313 else if (token1
.keyword
== RID_INLINE
11314 && token2
.keyword
== RID_NAMESPACE
)
11315 cp_parser_namespace_definition (parser
);
11316 /* Objective-C++ declaration/definition. */
11317 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11318 cp_parser_objc_declaration (parser
, NULL_TREE
);
11319 else if (c_dialect_objc ()
11320 && token1
.keyword
== RID_ATTRIBUTE
11321 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11322 cp_parser_objc_declaration (parser
, attributes
);
11323 /* We must have either a block declaration or a function
11326 /* Try to parse a block-declaration, or a function-definition. */
11327 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11329 /* Free any declarators allocated. */
11330 obstack_free (&declarator_obstack
, p
);
11333 /* Parse a block-declaration.
11338 namespace-alias-definition
11345 __extension__ block-declaration
11350 static_assert-declaration
11352 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11353 part of a declaration-statement. */
11356 cp_parser_block_declaration (cp_parser
*parser
,
11360 int saved_pedantic
;
11362 /* Check for the `__extension__' keyword. */
11363 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11365 /* Parse the qualified declaration. */
11366 cp_parser_block_declaration (parser
, statement_p
);
11367 /* Restore the PEDANTIC flag. */
11368 pedantic
= saved_pedantic
;
11373 /* Peek at the next token to figure out which kind of declaration is
11375 token1
= cp_lexer_peek_token (parser
->lexer
);
11377 /* If the next keyword is `asm', we have an asm-definition. */
11378 if (token1
->keyword
== RID_ASM
)
11381 cp_parser_commit_to_tentative_parse (parser
);
11382 cp_parser_asm_definition (parser
);
11384 /* If the next keyword is `namespace', we have a
11385 namespace-alias-definition. */
11386 else if (token1
->keyword
== RID_NAMESPACE
)
11387 cp_parser_namespace_alias_definition (parser
);
11388 /* If the next keyword is `using', we have a
11389 using-declaration, a using-directive, or an alias-declaration. */
11390 else if (token1
->keyword
== RID_USING
)
11395 cp_parser_commit_to_tentative_parse (parser
);
11396 /* If the token after `using' is `namespace', then we have a
11397 using-directive. */
11398 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11399 if (token2
->keyword
== RID_NAMESPACE
)
11400 cp_parser_using_directive (parser
);
11401 /* If the second token after 'using' is '=', then we have an
11402 alias-declaration. */
11403 else if (cxx_dialect
>= cxx11
11404 && token2
->type
== CPP_NAME
11405 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11406 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11407 cp_parser_alias_declaration (parser
);
11408 /* Otherwise, it's a using-declaration. */
11410 cp_parser_using_declaration (parser
,
11411 /*access_declaration_p=*/false);
11413 /* If the next keyword is `__label__' we have a misplaced label
11415 else if (token1
->keyword
== RID_LABEL
)
11417 cp_lexer_consume_token (parser
->lexer
);
11418 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11419 cp_parser_skip_to_end_of_statement (parser
);
11420 /* If the next token is now a `;', consume it. */
11421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11422 cp_lexer_consume_token (parser
->lexer
);
11424 /* If the next token is `static_assert' we have a static assertion. */
11425 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11426 cp_parser_static_assert (parser
, /*member_p=*/false);
11427 /* Anything else must be a simple-declaration. */
11429 cp_parser_simple_declaration (parser
, !statement_p
,
11430 /*maybe_range_for_decl*/NULL
);
11433 /* Parse a simple-declaration.
11435 simple-declaration:
11436 decl-specifier-seq [opt] init-declarator-list [opt] ;
11438 init-declarator-list:
11440 init-declarator-list , init-declarator
11442 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11443 function-definition as a simple-declaration.
11445 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11446 parsed declaration if it is an uninitialized single declarator not followed
11447 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11448 if present, will not be consumed. */
11451 cp_parser_simple_declaration (cp_parser
* parser
,
11452 bool function_definition_allowed_p
,
11453 tree
*maybe_range_for_decl
)
11455 cp_decl_specifier_seq decl_specifiers
;
11456 int declares_class_or_enum
;
11457 bool saw_declarator
;
11459 if (maybe_range_for_decl
)
11460 *maybe_range_for_decl
= NULL_TREE
;
11462 /* Defer access checks until we know what is being declared; the
11463 checks for names appearing in the decl-specifier-seq should be
11464 done as if we were in the scope of the thing being declared. */
11465 push_deferring_access_checks (dk_deferred
);
11467 /* Parse the decl-specifier-seq. We have to keep track of whether
11468 or not the decl-specifier-seq declares a named class or
11469 enumeration type, since that is the only case in which the
11470 init-declarator-list is allowed to be empty.
11474 In a simple-declaration, the optional init-declarator-list can be
11475 omitted only when declaring a class or enumeration, that is when
11476 the decl-specifier-seq contains either a class-specifier, an
11477 elaborated-type-specifier, or an enum-specifier. */
11478 cp_parser_decl_specifier_seq (parser
,
11479 CP_PARSER_FLAGS_OPTIONAL
,
11481 &declares_class_or_enum
);
11482 /* We no longer need to defer access checks. */
11483 stop_deferring_access_checks ();
11485 /* In a block scope, a valid declaration must always have a
11486 decl-specifier-seq. By not trying to parse declarators, we can
11487 resolve the declaration/expression ambiguity more quickly. */
11488 if (!function_definition_allowed_p
11489 && !decl_specifiers
.any_specifiers_p
)
11491 cp_parser_error (parser
, "expected declaration");
11495 /* If the next two tokens are both identifiers, the code is
11496 erroneous. The usual cause of this situation is code like:
11500 where "T" should name a type -- but does not. */
11501 if (!decl_specifiers
.any_type_specifiers_p
11502 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11504 /* If parsing tentatively, we should commit; we really are
11505 looking at a declaration. */
11506 cp_parser_commit_to_tentative_parse (parser
);
11511 /* If we have seen at least one decl-specifier, and the next token
11512 is not a parenthesis, then we must be looking at a declaration.
11513 (After "int (" we might be looking at a functional cast.) */
11514 if (decl_specifiers
.any_specifiers_p
11515 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11516 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11517 && !cp_parser_error_occurred (parser
))
11518 cp_parser_commit_to_tentative_parse (parser
);
11520 /* Keep going until we hit the `;' at the end of the simple
11522 saw_declarator
= false;
11523 while (cp_lexer_next_token_is_not (parser
->lexer
,
11527 bool function_definition_p
;
11530 if (saw_declarator
)
11532 /* If we are processing next declarator, coma is expected */
11533 token
= cp_lexer_peek_token (parser
->lexer
);
11534 gcc_assert (token
->type
== CPP_COMMA
);
11535 cp_lexer_consume_token (parser
->lexer
);
11536 if (maybe_range_for_decl
)
11537 *maybe_range_for_decl
= error_mark_node
;
11540 saw_declarator
= true;
11542 /* Parse the init-declarator. */
11543 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11545 function_definition_allowed_p
,
11546 /*member_p=*/false,
11547 declares_class_or_enum
,
11548 &function_definition_p
,
11549 maybe_range_for_decl
);
11550 /* If an error occurred while parsing tentatively, exit quickly.
11551 (That usually happens when in the body of a function; each
11552 statement is treated as a declaration-statement until proven
11554 if (cp_parser_error_occurred (parser
))
11556 /* Handle function definitions specially. */
11557 if (function_definition_p
)
11559 /* If the next token is a `,', then we are probably
11560 processing something like:
11564 which is erroneous. */
11565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11567 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11568 error_at (token
->location
,
11570 " declarations and function-definitions is forbidden");
11572 /* Otherwise, we're done with the list of declarators. */
11575 pop_deferring_access_checks ();
11579 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11580 *maybe_range_for_decl
= decl
;
11581 /* The next token should be either a `,' or a `;'. */
11582 token
= cp_lexer_peek_token (parser
->lexer
);
11583 /* If it's a `,', there are more declarators to come. */
11584 if (token
->type
== CPP_COMMA
)
11585 /* will be consumed next time around */;
11586 /* If it's a `;', we are done. */
11587 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11589 /* Anything else is an error. */
11592 /* If we have already issued an error message we don't need
11593 to issue another one. */
11594 if (decl
!= error_mark_node
11595 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11596 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11597 /* Skip tokens until we reach the end of the statement. */
11598 cp_parser_skip_to_end_of_statement (parser
);
11599 /* If the next token is now a `;', consume it. */
11600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11601 cp_lexer_consume_token (parser
->lexer
);
11604 /* After the first time around, a function-definition is not
11605 allowed -- even if it was OK at first. For example:
11610 function_definition_allowed_p
= false;
11613 /* Issue an error message if no declarators are present, and the
11614 decl-specifier-seq does not itself declare a class or
11615 enumeration: [dcl.dcl]/3. */
11616 if (!saw_declarator
)
11618 if (cp_parser_declares_only_class_p (parser
))
11620 if (!declares_class_or_enum
11621 && decl_specifiers
.type
11622 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11623 /* Ensure an error is issued anyway when finish_decltype_type,
11624 called via cp_parser_decl_specifier_seq, returns a class or
11625 an enumeration (c++/51786). */
11626 decl_specifiers
.type
= NULL_TREE
;
11627 shadow_tag (&decl_specifiers
);
11629 /* Perform any deferred access checks. */
11630 perform_deferred_access_checks (tf_warning_or_error
);
11633 /* Consume the `;'. */
11634 if (!maybe_range_for_decl
)
11635 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11638 pop_deferring_access_checks ();
11641 /* Parse a decl-specifier-seq.
11643 decl-specifier-seq:
11644 decl-specifier-seq [opt] decl-specifier
11645 decl-specifier attribute-specifier-seq [opt] (C++11)
11648 storage-class-specifier
11659 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11661 The parser flags FLAGS is used to control type-specifier parsing.
11663 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11666 1: one of the decl-specifiers is an elaborated-type-specifier
11667 (i.e., a type declaration)
11668 2: one of the decl-specifiers is an enum-specifier or a
11669 class-specifier (i.e., a type definition)
11674 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11675 cp_parser_flags flags
,
11676 cp_decl_specifier_seq
*decl_specs
,
11677 int* declares_class_or_enum
)
11679 bool constructor_possible_p
= !parser
->in_declarator_p
;
11680 bool found_decl_spec
= false;
11681 cp_token
*start_token
= NULL
;
11684 /* Clear DECL_SPECS. */
11685 clear_decl_specs (decl_specs
);
11687 /* Assume no class or enumeration type is declared. */
11688 *declares_class_or_enum
= 0;
11690 /* Keep reading specifiers until there are no more to read. */
11693 bool constructor_p
;
11697 /* Peek at the next token. */
11698 token
= cp_lexer_peek_token (parser
->lexer
);
11700 /* Save the first token of the decl spec list for error
11703 start_token
= token
;
11704 /* Handle attributes. */
11705 if (cp_next_tokens_can_be_attribute_p (parser
))
11707 /* Parse the attributes. */
11708 tree attrs
= cp_parser_attributes_opt (parser
);
11710 /* In a sequence of declaration specifiers, c++11 attributes
11711 appertain to the type that precede them. In that case
11714 The attribute-specifier-seq affects the type only for
11715 the declaration it appears in, not other declarations
11716 involving the same type.
11718 But for now let's force the user to position the
11719 attribute either at the beginning of the declaration or
11720 after the declarator-id, which would clearly mean that it
11721 applies to the declarator. */
11722 if (cxx11_attribute_p (attrs
))
11724 if (!found_decl_spec
)
11725 /* The c++11 attribute is at the beginning of the
11726 declaration. It appertains to the entity being
11730 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11732 /* This is an attribute following a
11733 class-specifier. */
11734 if (decl_specs
->type_definition_p
)
11735 warn_misplaced_attr_for_class_type (token
->location
,
11741 decl_specs
->std_attributes
11742 = chainon (decl_specs
->std_attributes
,
11744 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11745 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11751 decl_specs
->attributes
11752 = chainon (decl_specs
->attributes
,
11754 if (decl_specs
->locations
[ds_attribute
] == 0)
11755 decl_specs
->locations
[ds_attribute
] = token
->location
;
11758 /* Assume we will find a decl-specifier keyword. */
11759 found_decl_spec
= true;
11760 /* If the next token is an appropriate keyword, we can simply
11761 add it to the list. */
11762 switch (token
->keyword
)
11768 if (!at_class_scope_p ())
11770 error_at (token
->location
, "%<friend%> used outside of class");
11771 cp_lexer_purge_token (parser
->lexer
);
11776 /* Consume the token. */
11777 cp_lexer_consume_token (parser
->lexer
);
11781 case RID_CONSTEXPR
:
11783 cp_lexer_consume_token (parser
->lexer
);
11786 /* function-specifier:
11793 cp_parser_function_specifier_opt (parser
, decl_specs
);
11800 /* Consume the token. */
11801 cp_lexer_consume_token (parser
->lexer
);
11802 /* A constructor declarator cannot appear in a typedef. */
11803 constructor_possible_p
= false;
11804 /* The "typedef" keyword can only occur in a declaration; we
11805 may as well commit at this point. */
11806 cp_parser_commit_to_tentative_parse (parser
);
11808 if (decl_specs
->storage_class
!= sc_none
)
11809 decl_specs
->conflicting_specifiers_p
= true;
11812 /* storage-class-specifier:
11822 if (cxx_dialect
== cxx98
)
11824 /* Consume the token. */
11825 cp_lexer_consume_token (parser
->lexer
);
11827 /* Complain about `auto' as a storage specifier, if
11828 we're complaining about C++0x compatibility. */
11829 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11830 " changes meaning in C++11; please remove it");
11832 /* Set the storage class anyway. */
11833 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11837 /* C++0x auto type-specifier. */
11838 found_decl_spec
= false;
11845 /* Consume the token. */
11846 cp_lexer_consume_token (parser
->lexer
);
11847 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11851 /* Consume the token. */
11853 cp_lexer_consume_token (parser
->lexer
);
11857 /* We did not yet find a decl-specifier yet. */
11858 found_decl_spec
= false;
11862 if (found_decl_spec
11863 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11864 && token
->keyword
!= RID_CONSTEXPR
)
11865 error ("decl-specifier invalid in condition");
11868 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11870 /* Constructors are a special case. The `S' in `S()' is not a
11871 decl-specifier; it is the beginning of the declarator. */
11873 = (!found_decl_spec
11874 && constructor_possible_p
11875 && (cp_parser_constructor_declarator_p
11876 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11878 /* If we don't have a DECL_SPEC yet, then we must be looking at
11879 a type-specifier. */
11880 if (!found_decl_spec
&& !constructor_p
)
11882 int decl_spec_declares_class_or_enum
;
11883 bool is_cv_qualifier
;
11887 = cp_parser_type_specifier (parser
, flags
,
11889 /*is_declaration=*/true,
11890 &decl_spec_declares_class_or_enum
,
11892 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11894 /* If this type-specifier referenced a user-defined type
11895 (a typedef, class-name, etc.), then we can't allow any
11896 more such type-specifiers henceforth.
11900 The longest sequence of decl-specifiers that could
11901 possibly be a type name is taken as the
11902 decl-specifier-seq of a declaration. The sequence shall
11903 be self-consistent as described below.
11907 As a general rule, at most one type-specifier is allowed
11908 in the complete decl-specifier-seq of a declaration. The
11909 only exceptions are the following:
11911 -- const or volatile can be combined with any other
11914 -- signed or unsigned can be combined with char, long,
11922 void g (const int Pc);
11924 Here, Pc is *not* part of the decl-specifier seq; it's
11925 the declarator. Therefore, once we see a type-specifier
11926 (other than a cv-qualifier), we forbid any additional
11927 user-defined types. We *do* still allow things like `int
11928 int' to be considered a decl-specifier-seq, and issue the
11929 error message later. */
11930 if (type_spec
&& !is_cv_qualifier
)
11931 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11932 /* A constructor declarator cannot follow a type-specifier. */
11935 constructor_possible_p
= false;
11936 found_decl_spec
= true;
11937 if (!is_cv_qualifier
)
11938 decl_specs
->any_type_specifiers_p
= true;
11942 /* If we still do not have a DECL_SPEC, then there are no more
11943 decl-specifiers. */
11944 if (!found_decl_spec
)
11947 decl_specs
->any_specifiers_p
= true;
11948 /* After we see one decl-specifier, further decl-specifiers are
11949 always optional. */
11950 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11953 /* Don't allow a friend specifier with a class definition. */
11954 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11955 && (*declares_class_or_enum
& 2))
11956 error_at (decl_specs
->locations
[ds_friend
],
11957 "class definition may not be declared a friend");
11960 /* Parse an (optional) storage-class-specifier.
11962 storage-class-specifier:
11971 storage-class-specifier:
11974 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11977 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11979 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11982 if (cxx_dialect
!= cxx98
)
11984 /* Fall through for C++98. */
11991 /* Consume the token. */
11992 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11999 /* Parse an (optional) function-specifier.
12001 function-specifier:
12006 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12007 Updates DECL_SPECS, if it is non-NULL. */
12010 cp_parser_function_specifier_opt (cp_parser
* parser
,
12011 cp_decl_specifier_seq
*decl_specs
)
12013 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12014 switch (token
->keyword
)
12017 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12021 /* 14.5.2.3 [temp.mem]
12023 A member function template shall not be virtual. */
12024 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12025 error_at (token
->location
, "templates may not be %<virtual%>");
12027 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12031 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12038 /* Consume the token. */
12039 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12042 /* Parse a linkage-specification.
12044 linkage-specification:
12045 extern string-literal { declaration-seq [opt] }
12046 extern string-literal declaration */
12049 cp_parser_linkage_specification (cp_parser
* parser
)
12053 /* Look for the `extern' keyword. */
12054 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12056 /* Look for the string-literal. */
12057 linkage
= cp_parser_string_literal (parser
, false, false);
12059 /* Transform the literal into an identifier. If the literal is a
12060 wide-character string, or contains embedded NULs, then we can't
12061 handle it as the user wants. */
12062 if (strlen (TREE_STRING_POINTER (linkage
))
12063 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12065 cp_parser_error (parser
, "invalid linkage-specification");
12066 /* Assume C++ linkage. */
12067 linkage
= lang_name_cplusplus
;
12070 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12072 /* We're now using the new linkage. */
12073 push_lang_context (linkage
);
12075 /* If the next token is a `{', then we're using the first
12077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12079 cp_ensure_no_omp_declare_simd (parser
);
12081 /* Consume the `{' token. */
12082 cp_lexer_consume_token (parser
->lexer
);
12083 /* Parse the declarations. */
12084 cp_parser_declaration_seq_opt (parser
);
12085 /* Look for the closing `}'. */
12086 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12088 /* Otherwise, there's just one declaration. */
12091 bool saved_in_unbraced_linkage_specification_p
;
12093 saved_in_unbraced_linkage_specification_p
12094 = parser
->in_unbraced_linkage_specification_p
;
12095 parser
->in_unbraced_linkage_specification_p
= true;
12096 cp_parser_declaration (parser
);
12097 parser
->in_unbraced_linkage_specification_p
12098 = saved_in_unbraced_linkage_specification_p
;
12101 /* We're done with the linkage-specification. */
12102 pop_lang_context ();
12105 /* Parse a static_assert-declaration.
12107 static_assert-declaration:
12108 static_assert ( constant-expression , string-literal ) ;
12110 If MEMBER_P, this static_assert is a class member. */
12113 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12118 location_t saved_loc
;
12121 /* Peek at the `static_assert' token so we can keep track of exactly
12122 where the static assertion started. */
12123 token
= cp_lexer_peek_token (parser
->lexer
);
12124 saved_loc
= token
->location
;
12126 /* Look for the `static_assert' keyword. */
12127 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12131 /* We know we are in a static assertion; commit to any tentative
12133 if (cp_parser_parsing_tentatively (parser
))
12134 cp_parser_commit_to_tentative_parse (parser
);
12136 /* Parse the `(' starting the static assertion condition. */
12137 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12139 /* Parse the constant-expression. Allow a non-constant expression
12140 here in order to give better diagnostics in finish_static_assert. */
12142 cp_parser_constant_expression (parser
,
12143 /*allow_non_constant_p=*/true,
12144 /*non_constant_p=*/&dummy
);
12146 /* Parse the separating `,'. */
12147 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12149 /* Parse the string-literal message. */
12150 message
= cp_parser_string_literal (parser
,
12151 /*translate=*/false,
12154 /* A `)' completes the static assertion. */
12155 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12156 cp_parser_skip_to_closing_parenthesis (parser
,
12157 /*recovering=*/true,
12158 /*or_comma=*/false,
12159 /*consume_paren=*/true);
12161 /* A semicolon terminates the declaration. */
12162 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12164 /* Complete the static assertion, which may mean either processing
12165 the static assert now or saving it for template instantiation. */
12166 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12169 /* Parse the expression in decltype ( expression ). */
12172 cp_parser_decltype_expr (cp_parser
*parser
,
12173 bool &id_expression_or_member_access_p
)
12175 cp_token
*id_expr_start_token
;
12178 /* First, try parsing an id-expression. */
12179 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12180 cp_parser_parse_tentatively (parser
);
12181 expr
= cp_parser_id_expression (parser
,
12182 /*template_keyword_p=*/false,
12183 /*check_dependency_p=*/true,
12184 /*template_p=*/NULL
,
12185 /*declarator_p=*/false,
12186 /*optional_p=*/false);
12188 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12190 bool non_integral_constant_expression_p
= false;
12191 tree id_expression
= expr
;
12193 const char *error_msg
;
12195 if (identifier_p (expr
))
12196 /* Lookup the name we got back from the id-expression. */
12197 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12198 id_expr_start_token
->location
);
12201 && expr
!= error_mark_node
12202 && TREE_CODE (expr
) != TYPE_DECL
12203 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12204 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12205 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12207 /* Complete lookup of the id-expression. */
12208 expr
= (finish_id_expression
12209 (id_expression
, expr
, parser
->scope
, &idk
,
12210 /*integral_constant_expression_p=*/false,
12211 /*allow_non_integral_constant_expression_p=*/true,
12212 &non_integral_constant_expression_p
,
12213 /*template_p=*/false,
12215 /*address_p=*/false,
12216 /*template_arg_p=*/false,
12218 id_expr_start_token
->location
));
12220 if (expr
== error_mark_node
)
12221 /* We found an id-expression, but it was something that we
12222 should not have found. This is an error, not something
12223 we can recover from, so note that we found an
12224 id-expression and we'll recover as gracefully as
12226 id_expression_or_member_access_p
= true;
12230 && expr
!= error_mark_node
12231 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12232 /* We have an id-expression. */
12233 id_expression_or_member_access_p
= true;
12236 if (!id_expression_or_member_access_p
)
12238 /* Abort the id-expression parse. */
12239 cp_parser_abort_tentative_parse (parser
);
12241 /* Parsing tentatively, again. */
12242 cp_parser_parse_tentatively (parser
);
12244 /* Parse a class member access. */
12245 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12246 /*cast_p=*/false, /*decltype*/true,
12247 /*member_access_only_p=*/true, NULL
);
12250 && expr
!= error_mark_node
12251 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12252 /* We have an id-expression. */
12253 id_expression_or_member_access_p
= true;
12256 if (id_expression_or_member_access_p
)
12257 /* We have parsed the complete id-expression or member access. */
12258 cp_parser_parse_definitely (parser
);
12261 /* Abort our attempt to parse an id-expression or member access
12263 cp_parser_abort_tentative_parse (parser
);
12265 /* Parse a full expression. */
12266 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12267 /*decltype_p=*/true);
12273 /* Parse a `decltype' type. Returns the type.
12275 simple-type-specifier:
12276 decltype ( expression )
12278 decltype ( auto ) */
12281 cp_parser_decltype (cp_parser
*parser
)
12284 bool id_expression_or_member_access_p
= false;
12285 const char *saved_message
;
12286 bool saved_integral_constant_expression_p
;
12287 bool saved_non_integral_constant_expression_p
;
12288 bool saved_greater_than_is_operator_p
;
12289 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12291 if (start_token
->type
== CPP_DECLTYPE
)
12293 /* Already parsed. */
12294 cp_lexer_consume_token (parser
->lexer
);
12295 return start_token
->u
.value
;
12298 /* Look for the `decltype' token. */
12299 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12300 return error_mark_node
;
12302 /* Parse the opening `('. */
12303 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12304 return error_mark_node
;
12306 /* decltype (auto) */
12307 if (cxx_dialect
>= cxx14
12308 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12310 cp_lexer_consume_token (parser
->lexer
);
12311 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12312 return error_mark_node
;
12313 expr
= make_decltype_auto ();
12314 AUTO_IS_DECLTYPE (expr
) = true;
12318 /* Types cannot be defined in a `decltype' expression. Save away the
12320 saved_message
= parser
->type_definition_forbidden_message
;
12322 /* And create the new one. */
12323 parser
->type_definition_forbidden_message
12324 = G_("types may not be defined in %<decltype%> expressions");
12326 /* The restrictions on constant-expressions do not apply inside
12327 decltype expressions. */
12328 saved_integral_constant_expression_p
12329 = parser
->integral_constant_expression_p
;
12330 saved_non_integral_constant_expression_p
12331 = parser
->non_integral_constant_expression_p
;
12332 parser
->integral_constant_expression_p
= false;
12334 /* Within a parenthesized expression, a `>' token is always
12335 the greater-than operator. */
12336 saved_greater_than_is_operator_p
12337 = parser
->greater_than_is_operator_p
;
12338 parser
->greater_than_is_operator_p
= true;
12340 /* Do not actually evaluate the expression. */
12341 ++cp_unevaluated_operand
;
12343 /* Do not warn about problems with the expression. */
12344 ++c_inhibit_evaluation_warnings
;
12346 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12348 /* Go back to evaluating expressions. */
12349 --cp_unevaluated_operand
;
12350 --c_inhibit_evaluation_warnings
;
12352 /* The `>' token might be the end of a template-id or
12353 template-parameter-list now. */
12354 parser
->greater_than_is_operator_p
12355 = saved_greater_than_is_operator_p
;
12357 /* Restore the old message and the integral constant expression
12359 parser
->type_definition_forbidden_message
= saved_message
;
12360 parser
->integral_constant_expression_p
12361 = saved_integral_constant_expression_p
;
12362 parser
->non_integral_constant_expression_p
12363 = saved_non_integral_constant_expression_p
;
12365 /* Parse to the closing `)'. */
12366 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12368 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12369 /*consume_paren=*/true);
12370 return error_mark_node
;
12373 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12374 tf_warning_or_error
);
12377 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12379 start_token
->type
= CPP_DECLTYPE
;
12380 start_token
->u
.value
= expr
;
12381 start_token
->keyword
= RID_MAX
;
12382 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12387 /* Special member functions [gram.special] */
12389 /* Parse a conversion-function-id.
12391 conversion-function-id:
12392 operator conversion-type-id
12394 Returns an IDENTIFIER_NODE representing the operator. */
12397 cp_parser_conversion_function_id (cp_parser
* parser
)
12401 tree saved_qualifying_scope
;
12402 tree saved_object_scope
;
12403 tree pushed_scope
= NULL_TREE
;
12405 /* Look for the `operator' token. */
12406 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12407 return error_mark_node
;
12408 /* When we parse the conversion-type-id, the current scope will be
12409 reset. However, we need that information in able to look up the
12410 conversion function later, so we save it here. */
12411 saved_scope
= parser
->scope
;
12412 saved_qualifying_scope
= parser
->qualifying_scope
;
12413 saved_object_scope
= parser
->object_scope
;
12414 /* We must enter the scope of the class so that the names of
12415 entities declared within the class are available in the
12416 conversion-type-id. For example, consider:
12423 S::operator I() { ... }
12425 In order to see that `I' is a type-name in the definition, we
12426 must be in the scope of `S'. */
12428 pushed_scope
= push_scope (saved_scope
);
12429 /* Parse the conversion-type-id. */
12430 type
= cp_parser_conversion_type_id (parser
);
12431 /* Leave the scope of the class, if any. */
12433 pop_scope (pushed_scope
);
12434 /* Restore the saved scope. */
12435 parser
->scope
= saved_scope
;
12436 parser
->qualifying_scope
= saved_qualifying_scope
;
12437 parser
->object_scope
= saved_object_scope
;
12438 /* If the TYPE is invalid, indicate failure. */
12439 if (type
== error_mark_node
)
12440 return error_mark_node
;
12441 return mangle_conv_op_name_for_type (type
);
12444 /* Parse a conversion-type-id:
12446 conversion-type-id:
12447 type-specifier-seq conversion-declarator [opt]
12449 Returns the TYPE specified. */
12452 cp_parser_conversion_type_id (cp_parser
* parser
)
12455 cp_decl_specifier_seq type_specifiers
;
12456 cp_declarator
*declarator
;
12457 tree type_specified
;
12458 const char *saved_message
;
12460 /* Parse the attributes. */
12461 attributes
= cp_parser_attributes_opt (parser
);
12463 saved_message
= parser
->type_definition_forbidden_message
;
12464 parser
->type_definition_forbidden_message
12465 = G_("types may not be defined in a conversion-type-id");
12467 /* Parse the type-specifiers. */
12468 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12469 /*is_trailing_return=*/false,
12472 parser
->type_definition_forbidden_message
= saved_message
;
12474 /* If that didn't work, stop. */
12475 if (type_specifiers
.type
== error_mark_node
)
12476 return error_mark_node
;
12477 /* Parse the conversion-declarator. */
12478 declarator
= cp_parser_conversion_declarator_opt (parser
);
12480 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12481 /*initialized=*/0, &attributes
);
12483 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12485 /* Don't give this error when parsing tentatively. This happens to
12486 work because we always parse this definitively once. */
12487 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12488 && type_uses_auto (type_specified
))
12490 if (cxx_dialect
< cxx14
)
12492 error ("invalid use of %<auto%> in conversion operator");
12493 return error_mark_node
;
12495 else if (template_parm_scope_p ())
12496 warning (0, "use of %<auto%> in member template "
12497 "conversion operator can never be deduced");
12500 return type_specified
;
12503 /* Parse an (optional) conversion-declarator.
12505 conversion-declarator:
12506 ptr-operator conversion-declarator [opt]
12510 static cp_declarator
*
12511 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12513 enum tree_code code
;
12514 tree class_type
, std_attributes
= NULL_TREE
;
12515 cp_cv_quals cv_quals
;
12517 /* We don't know if there's a ptr-operator next, or not. */
12518 cp_parser_parse_tentatively (parser
);
12519 /* Try the ptr-operator. */
12520 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12522 /* If it worked, look for more conversion-declarators. */
12523 if (cp_parser_parse_definitely (parser
))
12525 cp_declarator
*declarator
;
12527 /* Parse another optional declarator. */
12528 declarator
= cp_parser_conversion_declarator_opt (parser
);
12530 declarator
= cp_parser_make_indirect_declarator
12531 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12539 /* Parse an (optional) ctor-initializer.
12542 : mem-initializer-list
12544 Returns TRUE iff the ctor-initializer was actually present. */
12547 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12549 /* If the next token is not a `:', then there is no
12550 ctor-initializer. */
12551 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12553 /* Do default initialization of any bases and members. */
12554 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12555 finish_mem_initializers (NULL_TREE
);
12560 /* Consume the `:' token. */
12561 cp_lexer_consume_token (parser
->lexer
);
12562 /* And the mem-initializer-list. */
12563 cp_parser_mem_initializer_list (parser
);
12568 /* Parse a mem-initializer-list.
12570 mem-initializer-list:
12571 mem-initializer ... [opt]
12572 mem-initializer ... [opt] , mem-initializer-list */
12575 cp_parser_mem_initializer_list (cp_parser
* parser
)
12577 tree mem_initializer_list
= NULL_TREE
;
12578 tree target_ctor
= error_mark_node
;
12579 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12581 /* Let the semantic analysis code know that we are starting the
12582 mem-initializer-list. */
12583 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12584 error_at (token
->location
,
12585 "only constructors take member initializers");
12587 /* Loop through the list. */
12590 tree mem_initializer
;
12592 token
= cp_lexer_peek_token (parser
->lexer
);
12593 /* Parse the mem-initializer. */
12594 mem_initializer
= cp_parser_mem_initializer (parser
);
12595 /* If the next token is a `...', we're expanding member initializers. */
12596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12598 /* Consume the `...'. */
12599 cp_lexer_consume_token (parser
->lexer
);
12601 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12602 can be expanded but members cannot. */
12603 if (mem_initializer
!= error_mark_node
12604 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12606 error_at (token
->location
,
12607 "cannot expand initializer for member %<%D%>",
12608 TREE_PURPOSE (mem_initializer
));
12609 mem_initializer
= error_mark_node
;
12612 /* Construct the pack expansion type. */
12613 if (mem_initializer
!= error_mark_node
)
12614 mem_initializer
= make_pack_expansion (mem_initializer
);
12616 if (target_ctor
!= error_mark_node
12617 && mem_initializer
!= error_mark_node
)
12619 error ("mem-initializer for %qD follows constructor delegation",
12620 TREE_PURPOSE (mem_initializer
));
12621 mem_initializer
= error_mark_node
;
12623 /* Look for a target constructor. */
12624 if (mem_initializer
!= error_mark_node
12625 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12626 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12628 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12629 if (mem_initializer_list
)
12631 error ("constructor delegation follows mem-initializer for %qD",
12632 TREE_PURPOSE (mem_initializer_list
));
12633 mem_initializer
= error_mark_node
;
12635 target_ctor
= mem_initializer
;
12637 /* Add it to the list, unless it was erroneous. */
12638 if (mem_initializer
!= error_mark_node
)
12640 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12641 mem_initializer_list
= mem_initializer
;
12643 /* If the next token is not a `,', we're done. */
12644 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12646 /* Consume the `,' token. */
12647 cp_lexer_consume_token (parser
->lexer
);
12650 /* Perform semantic analysis. */
12651 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12652 finish_mem_initializers (mem_initializer_list
);
12655 /* Parse a mem-initializer.
12658 mem-initializer-id ( expression-list [opt] )
12659 mem-initializer-id braced-init-list
12664 ( expression-list [opt] )
12666 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12667 class) or FIELD_DECL (for a non-static data member) to initialize;
12668 the TREE_VALUE is the expression-list. An empty initialization
12669 list is represented by void_list_node. */
12672 cp_parser_mem_initializer (cp_parser
* parser
)
12674 tree mem_initializer_id
;
12675 tree expression_list
;
12677 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12679 /* Find out what is being initialized. */
12680 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12682 permerror (token
->location
,
12683 "anachronistic old-style base class initializer");
12684 mem_initializer_id
= NULL_TREE
;
12688 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12689 if (mem_initializer_id
== error_mark_node
)
12690 return mem_initializer_id
;
12692 member
= expand_member_init (mem_initializer_id
);
12693 if (member
&& !DECL_P (member
))
12694 in_base_initializer
= 1;
12696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12698 bool expr_non_constant_p
;
12699 cp_lexer_set_source_position (parser
->lexer
);
12700 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12701 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12702 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12703 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12707 vec
<tree
, va_gc
> *vec
;
12708 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12710 /*allow_expansion_p=*/true,
12711 /*non_constant_p=*/NULL
);
12713 return error_mark_node
;
12714 expression_list
= build_tree_list_vec (vec
);
12715 release_tree_vector (vec
);
12718 if (expression_list
== error_mark_node
)
12719 return error_mark_node
;
12720 if (!expression_list
)
12721 expression_list
= void_type_node
;
12723 in_base_initializer
= 0;
12725 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12728 /* Parse a mem-initializer-id.
12730 mem-initializer-id:
12731 :: [opt] nested-name-specifier [opt] class-name
12734 Returns a TYPE indicating the class to be initializer for the first
12735 production. Returns an IDENTIFIER_NODE indicating the data member
12736 to be initialized for the second production. */
12739 cp_parser_mem_initializer_id (cp_parser
* parser
)
12741 bool global_scope_p
;
12742 bool nested_name_specifier_p
;
12743 bool template_p
= false;
12746 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12748 /* `typename' is not allowed in this context ([temp.res]). */
12749 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12751 error_at (token
->location
,
12752 "keyword %<typename%> not allowed in this context (a qualified "
12753 "member initializer is implicitly a type)");
12754 cp_lexer_consume_token (parser
->lexer
);
12756 /* Look for the optional `::' operator. */
12758 = (cp_parser_global_scope_opt (parser
,
12759 /*current_scope_valid_p=*/false)
12761 /* Look for the optional nested-name-specifier. The simplest way to
12766 The keyword `typename' is not permitted in a base-specifier or
12767 mem-initializer; in these contexts a qualified name that
12768 depends on a template-parameter is implicitly assumed to be a
12771 is to assume that we have seen the `typename' keyword at this
12773 nested_name_specifier_p
12774 = (cp_parser_nested_name_specifier_opt (parser
,
12775 /*typename_keyword_p=*/true,
12776 /*check_dependency_p=*/true,
12778 /*is_declaration=*/true)
12780 if (nested_name_specifier_p
)
12781 template_p
= cp_parser_optional_template_keyword (parser
);
12782 /* If there is a `::' operator or a nested-name-specifier, then we
12783 are definitely looking for a class-name. */
12784 if (global_scope_p
|| nested_name_specifier_p
)
12785 return cp_parser_class_name (parser
,
12786 /*typename_keyword_p=*/true,
12787 /*template_keyword_p=*/template_p
,
12789 /*check_dependency_p=*/true,
12790 /*class_head_p=*/false,
12791 /*is_declaration=*/true);
12792 /* Otherwise, we could also be looking for an ordinary identifier. */
12793 cp_parser_parse_tentatively (parser
);
12794 /* Try a class-name. */
12795 id
= cp_parser_class_name (parser
,
12796 /*typename_keyword_p=*/true,
12797 /*template_keyword_p=*/false,
12799 /*check_dependency_p=*/true,
12800 /*class_head_p=*/false,
12801 /*is_declaration=*/true);
12802 /* If we found one, we're done. */
12803 if (cp_parser_parse_definitely (parser
))
12805 /* Otherwise, look for an ordinary identifier. */
12806 return cp_parser_identifier (parser
);
12809 /* Overloading [gram.over] */
12811 /* Parse an operator-function-id.
12813 operator-function-id:
12816 Returns an IDENTIFIER_NODE for the operator which is a
12817 human-readable spelling of the identifier, e.g., `operator +'. */
12820 cp_parser_operator_function_id (cp_parser
* parser
)
12822 /* Look for the `operator' keyword. */
12823 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12824 return error_mark_node
;
12825 /* And then the name of the operator itself. */
12826 return cp_parser_operator (parser
);
12829 /* Return an identifier node for a user-defined literal operator.
12830 The suffix identifier is chained to the operator name identifier. */
12833 cp_literal_operator_id (const char* name
)
12836 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12837 + strlen (name
) + 10);
12838 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12839 identifier
= get_identifier (buffer
);
12844 /* Parse an operator.
12847 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12848 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12849 || ++ -- , ->* -> () []
12856 Returns an IDENTIFIER_NODE for the operator which is a
12857 human-readable spelling of the identifier, e.g., `operator +'. */
12860 cp_parser_operator (cp_parser
* parser
)
12862 tree id
= NULL_TREE
;
12866 /* Peek at the next token. */
12867 token
= cp_lexer_peek_token (parser
->lexer
);
12868 /* Figure out which operator we have. */
12869 switch (token
->type
)
12875 /* The keyword should be either `new' or `delete'. */
12876 if (token
->keyword
== RID_NEW
)
12878 else if (token
->keyword
== RID_DELETE
)
12883 /* Consume the `new' or `delete' token. */
12884 cp_lexer_consume_token (parser
->lexer
);
12886 /* Peek at the next token. */
12887 token
= cp_lexer_peek_token (parser
->lexer
);
12888 /* If it's a `[' token then this is the array variant of the
12890 if (token
->type
== CPP_OPEN_SQUARE
)
12892 /* Consume the `[' token. */
12893 cp_lexer_consume_token (parser
->lexer
);
12894 /* Look for the `]' token. */
12895 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12896 id
= ansi_opname (op
== NEW_EXPR
12897 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12899 /* Otherwise, we have the non-array variant. */
12901 id
= ansi_opname (op
);
12907 id
= ansi_opname (PLUS_EXPR
);
12911 id
= ansi_opname (MINUS_EXPR
);
12915 id
= ansi_opname (MULT_EXPR
);
12919 id
= ansi_opname (TRUNC_DIV_EXPR
);
12923 id
= ansi_opname (TRUNC_MOD_EXPR
);
12927 id
= ansi_opname (BIT_XOR_EXPR
);
12931 id
= ansi_opname (BIT_AND_EXPR
);
12935 id
= ansi_opname (BIT_IOR_EXPR
);
12939 id
= ansi_opname (BIT_NOT_EXPR
);
12943 id
= ansi_opname (TRUTH_NOT_EXPR
);
12947 id
= ansi_assopname (NOP_EXPR
);
12951 id
= ansi_opname (LT_EXPR
);
12955 id
= ansi_opname (GT_EXPR
);
12959 id
= ansi_assopname (PLUS_EXPR
);
12963 id
= ansi_assopname (MINUS_EXPR
);
12967 id
= ansi_assopname (MULT_EXPR
);
12971 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12975 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12979 id
= ansi_assopname (BIT_XOR_EXPR
);
12983 id
= ansi_assopname (BIT_AND_EXPR
);
12987 id
= ansi_assopname (BIT_IOR_EXPR
);
12991 id
= ansi_opname (LSHIFT_EXPR
);
12995 id
= ansi_opname (RSHIFT_EXPR
);
12998 case CPP_LSHIFT_EQ
:
12999 id
= ansi_assopname (LSHIFT_EXPR
);
13002 case CPP_RSHIFT_EQ
:
13003 id
= ansi_assopname (RSHIFT_EXPR
);
13007 id
= ansi_opname (EQ_EXPR
);
13011 id
= ansi_opname (NE_EXPR
);
13015 id
= ansi_opname (LE_EXPR
);
13018 case CPP_GREATER_EQ
:
13019 id
= ansi_opname (GE_EXPR
);
13023 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13027 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13030 case CPP_PLUS_PLUS
:
13031 id
= ansi_opname (POSTINCREMENT_EXPR
);
13034 case CPP_MINUS_MINUS
:
13035 id
= ansi_opname (PREDECREMENT_EXPR
);
13039 id
= ansi_opname (COMPOUND_EXPR
);
13042 case CPP_DEREF_STAR
:
13043 id
= ansi_opname (MEMBER_REF
);
13047 id
= ansi_opname (COMPONENT_REF
);
13050 case CPP_OPEN_PAREN
:
13051 /* Consume the `('. */
13052 cp_lexer_consume_token (parser
->lexer
);
13053 /* Look for the matching `)'. */
13054 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13055 return ansi_opname (CALL_EXPR
);
13057 case CPP_OPEN_SQUARE
:
13058 /* Consume the `['. */
13059 cp_lexer_consume_token (parser
->lexer
);
13060 /* Look for the matching `]'. */
13061 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13062 return ansi_opname (ARRAY_REF
);
13064 case CPP_UTF8STRING
:
13065 case CPP_UTF8STRING_USERDEF
:
13071 case CPP_STRING_USERDEF
:
13072 case CPP_WSTRING_USERDEF
:
13073 case CPP_STRING16_USERDEF
:
13074 case CPP_STRING32_USERDEF
:
13076 tree str
, string_tree
;
13079 if (cxx_dialect
== cxx98
)
13080 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13082 /* Consume the string. */
13083 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13084 /*wide_ok=*/true, /*lookup_udlit=*/false);
13085 if (str
== error_mark_node
)
13086 return error_mark_node
;
13087 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13089 string_tree
= USERDEF_LITERAL_VALUE (str
);
13090 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13095 /* Look for the suffix identifier. */
13096 token
= cp_lexer_peek_token (parser
->lexer
);
13097 if (token
->type
== CPP_NAME
)
13098 id
= cp_parser_identifier (parser
);
13099 else if (token
->type
== CPP_KEYWORD
)
13101 error ("unexpected keyword;"
13102 " remove space between quotes and suffix identifier");
13103 return error_mark_node
;
13107 error ("expected suffix identifier");
13108 return error_mark_node
;
13111 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13112 (TREE_TYPE (TREE_TYPE (string_tree
))));
13113 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13116 error ("expected empty string after %<operator%> keyword");
13117 return error_mark_node
;
13119 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13122 error ("invalid encoding prefix in literal operator");
13123 return error_mark_node
;
13125 if (id
!= error_mark_node
)
13127 const char *name
= IDENTIFIER_POINTER (id
);
13128 id
= cp_literal_operator_id (name
);
13134 /* Anything else is an error. */
13138 /* If we have selected an identifier, we need to consume the
13141 cp_lexer_consume_token (parser
->lexer
);
13142 /* Otherwise, no valid operator name was present. */
13145 cp_parser_error (parser
, "expected operator");
13146 id
= error_mark_node
;
13152 /* Parse a template-declaration.
13154 template-declaration:
13155 export [opt] template < template-parameter-list > declaration
13157 If MEMBER_P is TRUE, this template-declaration occurs within a
13160 The grammar rule given by the standard isn't correct. What
13161 is really meant is:
13163 template-declaration:
13164 export [opt] template-parameter-list-seq
13165 decl-specifier-seq [opt] init-declarator [opt] ;
13166 export [opt] template-parameter-list-seq
13167 function-definition
13169 template-parameter-list-seq:
13170 template-parameter-list-seq [opt]
13171 template < template-parameter-list > */
13174 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13176 /* Check for `export'. */
13177 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13179 /* Consume the `export' token. */
13180 cp_lexer_consume_token (parser
->lexer
);
13181 /* Warn that we do not support `export'. */
13182 warning (0, "keyword %<export%> not implemented, and will be ignored");
13185 cp_parser_template_declaration_after_export (parser
, member_p
);
13188 /* Parse a template-parameter-list.
13190 template-parameter-list:
13192 template-parameter-list , template-parameter
13194 Returns a TREE_LIST. Each node represents a template parameter.
13195 The nodes are connected via their TREE_CHAINs. */
13198 cp_parser_template_parameter_list (cp_parser
* parser
)
13200 tree parameter_list
= NULL_TREE
;
13202 begin_template_parm_list ();
13204 /* The loop below parses the template parms. We first need to know
13205 the total number of template parms to be able to compute proper
13206 canonical types of each dependent type. So after the loop, when
13207 we know the total number of template parms,
13208 end_template_parm_list computes the proper canonical types and
13209 fixes up the dependent types accordingly. */
13214 bool is_parameter_pack
;
13215 location_t parm_loc
;
13217 /* Parse the template-parameter. */
13218 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13219 parameter
= cp_parser_template_parameter (parser
,
13221 &is_parameter_pack
);
13222 /* Add it to the list. */
13223 if (parameter
!= error_mark_node
)
13224 parameter_list
= process_template_parm (parameter_list
,
13228 is_parameter_pack
);
13231 tree err_parm
= build_tree_list (parameter
, parameter
);
13232 parameter_list
= chainon (parameter_list
, err_parm
);
13235 /* If the next token is not a `,', we're done. */
13236 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13238 /* Otherwise, consume the `,' token. */
13239 cp_lexer_consume_token (parser
->lexer
);
13242 return end_template_parm_list (parameter_list
);
13245 /* Parse a template-parameter.
13247 template-parameter:
13249 parameter-declaration
13251 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13252 the parameter. The TREE_PURPOSE is the default value, if any.
13253 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13254 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13255 set to true iff this parameter is a parameter pack. */
13258 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13259 bool *is_parameter_pack
)
13262 cp_parameter_declarator
*parameter_declarator
;
13263 cp_declarator
*id_declarator
;
13266 /* Assume it is a type parameter or a template parameter. */
13267 *is_non_type
= false;
13268 /* Assume it not a parameter pack. */
13269 *is_parameter_pack
= false;
13270 /* Peek at the next token. */
13271 token
= cp_lexer_peek_token (parser
->lexer
);
13272 /* If it is `class' or `template', we have a type-parameter. */
13273 if (token
->keyword
== RID_TEMPLATE
)
13274 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13275 /* If it is `class' or `typename' we do not know yet whether it is a
13276 type parameter or a non-type parameter. Consider:
13278 template <typename T, typename T::X X> ...
13282 template <class C, class D*> ...
13284 Here, the first parameter is a type parameter, and the second is
13285 a non-type parameter. We can tell by looking at the token after
13286 the identifier -- if it is a `,', `=', or `>' then we have a type
13288 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13290 /* Peek at the token after `class' or `typename'. */
13291 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13292 /* If it's an ellipsis, we have a template type parameter
13294 if (token
->type
== CPP_ELLIPSIS
)
13295 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13296 /* If it's an identifier, skip it. */
13297 if (token
->type
== CPP_NAME
)
13298 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13299 /* Now, see if the token looks like the end of a template
13301 if (token
->type
== CPP_COMMA
13302 || token
->type
== CPP_EQ
13303 || token
->type
== CPP_GREATER
)
13304 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13307 /* Otherwise, it is a non-type parameter.
13311 When parsing a default template-argument for a non-type
13312 template-parameter, the first non-nested `>' is taken as the end
13313 of the template parameter-list rather than a greater-than
13315 *is_non_type
= true;
13316 parameter_declarator
13317 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13318 /*parenthesized_p=*/NULL
);
13320 if (!parameter_declarator
)
13321 return error_mark_node
;
13323 /* If the parameter declaration is marked as a parameter pack, set
13324 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13325 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13327 if (parameter_declarator
->declarator
13328 && parameter_declarator
->declarator
->parameter_pack_p
)
13330 *is_parameter_pack
= true;
13331 parameter_declarator
->declarator
->parameter_pack_p
= false;
13334 if (parameter_declarator
->default_argument
)
13336 /* Can happen in some cases of erroneous input (c++/34892). */
13337 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13338 /* Consume the `...' for better error recovery. */
13339 cp_lexer_consume_token (parser
->lexer
);
13341 /* If the next token is an ellipsis, and we don't already have it
13342 marked as a parameter pack, then we have a parameter pack (that
13343 has no declarator). */
13344 else if (!*is_parameter_pack
13345 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13346 && (declarator_can_be_parameter_pack
13347 (parameter_declarator
->declarator
)))
13349 /* Consume the `...'. */
13350 cp_lexer_consume_token (parser
->lexer
);
13351 maybe_warn_variadic_templates ();
13353 *is_parameter_pack
= true;
13355 /* We might end up with a pack expansion as the type of the non-type
13356 template parameter, in which case this is a non-type template
13358 else if (parameter_declarator
->decl_specifiers
.type
13359 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13361 *is_parameter_pack
= true;
13362 parameter_declarator
->decl_specifiers
.type
=
13363 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13366 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13368 /* Parameter packs cannot have default arguments. However, a
13369 user may try to do so, so we'll parse them and give an
13370 appropriate diagnostic here. */
13372 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13374 /* Find the name of the parameter pack. */
13375 id_declarator
= parameter_declarator
->declarator
;
13376 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13377 id_declarator
= id_declarator
->declarator
;
13379 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13380 error_at (start_token
->location
,
13381 "template parameter pack %qD cannot have a default argument",
13382 id_declarator
->u
.id
.unqualified_name
);
13384 error_at (start_token
->location
,
13385 "template parameter pack cannot have a default argument");
13387 /* Parse the default argument, but throw away the result. */
13388 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13391 parm
= grokdeclarator (parameter_declarator
->declarator
,
13392 ¶meter_declarator
->decl_specifiers
,
13393 TPARM
, /*initialized=*/0,
13394 /*attrlist=*/NULL
);
13395 if (parm
== error_mark_node
)
13396 return error_mark_node
;
13398 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13401 /* Parse a type-parameter.
13404 class identifier [opt]
13405 class identifier [opt] = type-id
13406 typename identifier [opt]
13407 typename identifier [opt] = type-id
13408 template < template-parameter-list > class identifier [opt]
13409 template < template-parameter-list > class identifier [opt]
13412 GNU Extension (variadic templates):
13415 class ... identifier [opt]
13416 typename ... identifier [opt]
13418 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13419 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13420 the declaration of the parameter.
13422 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13425 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13430 /* Look for a keyword to tell us what kind of parameter this is. */
13431 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13433 return error_mark_node
;
13435 switch (token
->keyword
)
13441 tree default_argument
;
13443 /* If the next token is an ellipsis, we have a template
13445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13447 /* Consume the `...' token. */
13448 cp_lexer_consume_token (parser
->lexer
);
13449 maybe_warn_variadic_templates ();
13451 *is_parameter_pack
= true;
13454 /* If the next token is an identifier, then it names the
13456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13457 identifier
= cp_parser_identifier (parser
);
13459 identifier
= NULL_TREE
;
13461 /* Create the parameter. */
13462 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13464 /* If the next token is an `=', we have a default argument. */
13465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13467 /* Consume the `=' token. */
13468 cp_lexer_consume_token (parser
->lexer
);
13469 /* Parse the default-argument. */
13470 push_deferring_access_checks (dk_no_deferred
);
13471 default_argument
= cp_parser_type_id (parser
);
13473 /* Template parameter packs cannot have default
13475 if (*is_parameter_pack
)
13478 error_at (token
->location
,
13479 "template parameter pack %qD cannot have a "
13480 "default argument", identifier
);
13482 error_at (token
->location
,
13483 "template parameter packs cannot have "
13484 "default arguments");
13485 default_argument
= NULL_TREE
;
13487 pop_deferring_access_checks ();
13490 default_argument
= NULL_TREE
;
13492 /* Create the combined representation of the parameter and the
13493 default argument. */
13494 parameter
= build_tree_list (default_argument
, parameter
);
13501 tree default_argument
;
13503 /* Look for the `<'. */
13504 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13505 /* Parse the template-parameter-list. */
13506 cp_parser_template_parameter_list (parser
);
13507 /* Look for the `>'. */
13508 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13509 /* Look for the `class' or 'typename' keywords. */
13510 cp_parser_type_parameter_key (parser
);
13511 /* If the next token is an ellipsis, we have a template
13513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13515 /* Consume the `...' token. */
13516 cp_lexer_consume_token (parser
->lexer
);
13517 maybe_warn_variadic_templates ();
13519 *is_parameter_pack
= true;
13521 /* If the next token is an `=', then there is a
13522 default-argument. If the next token is a `>', we are at
13523 the end of the parameter-list. If the next token is a `,',
13524 then we are at the end of this parameter. */
13525 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13526 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13527 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13529 identifier
= cp_parser_identifier (parser
);
13530 /* Treat invalid names as if the parameter were nameless. */
13531 if (identifier
== error_mark_node
)
13532 identifier
= NULL_TREE
;
13535 identifier
= NULL_TREE
;
13537 /* Create the template parameter. */
13538 parameter
= finish_template_template_parm (class_type_node
,
13541 /* If the next token is an `=', then there is a
13542 default-argument. */
13543 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13547 /* Consume the `='. */
13548 cp_lexer_consume_token (parser
->lexer
);
13549 /* Parse the id-expression. */
13550 push_deferring_access_checks (dk_no_deferred
);
13551 /* save token before parsing the id-expression, for error
13553 token
= cp_lexer_peek_token (parser
->lexer
);
13555 = cp_parser_id_expression (parser
,
13556 /*template_keyword_p=*/false,
13557 /*check_dependency_p=*/true,
13558 /*template_p=*/&is_template
,
13559 /*declarator_p=*/false,
13560 /*optional_p=*/false);
13561 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13562 /* If the id-expression was a template-id that refers to
13563 a template-class, we already have the declaration here,
13564 so no further lookup is needed. */
13567 /* Look up the name. */
13569 = cp_parser_lookup_name (parser
, default_argument
,
13571 /*is_template=*/is_template
,
13572 /*is_namespace=*/false,
13573 /*check_dependency=*/true,
13574 /*ambiguous_decls=*/NULL
,
13576 /* See if the default argument is valid. */
13578 = check_template_template_default_arg (default_argument
);
13580 /* Template parameter packs cannot have default
13582 if (*is_parameter_pack
)
13585 error_at (token
->location
,
13586 "template parameter pack %qD cannot "
13587 "have a default argument",
13590 error_at (token
->location
, "template parameter packs cannot "
13591 "have default arguments");
13592 default_argument
= NULL_TREE
;
13594 pop_deferring_access_checks ();
13597 default_argument
= NULL_TREE
;
13599 /* Create the combined representation of the parameter and the
13600 default argument. */
13601 parameter
= build_tree_list (default_argument
, parameter
);
13606 gcc_unreachable ();
13613 /* Parse a template-id.
13616 template-name < template-argument-list [opt] >
13618 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13619 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13620 returned. Otherwise, if the template-name names a function, or set
13621 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13622 names a class, returns a TYPE_DECL for the specialization.
13624 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13625 uninstantiated templates. */
13628 cp_parser_template_id (cp_parser
*parser
,
13629 bool template_keyword_p
,
13630 bool check_dependency_p
,
13631 enum tag_types tag_type
,
13632 bool is_declaration
)
13638 cp_token_position start_of_id
= 0;
13639 deferred_access_check
*chk
;
13640 vec
<deferred_access_check
, va_gc
> *access_check
;
13641 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13642 bool is_identifier
;
13644 /* If the next token corresponds to a template-id, there is no need
13646 next_token
= cp_lexer_peek_token (parser
->lexer
);
13647 if (next_token
->type
== CPP_TEMPLATE_ID
)
13649 struct tree_check
*check_value
;
13651 /* Get the stored value. */
13652 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13653 /* Perform any access checks that were deferred. */
13654 access_check
= check_value
->checks
;
13657 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13658 perform_or_defer_access_check (chk
->binfo
,
13661 tf_warning_or_error
);
13663 /* Return the stored value. */
13664 return check_value
->value
;
13667 /* Avoid performing name lookup if there is no possibility of
13668 finding a template-id. */
13669 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13670 || (next_token
->type
== CPP_NAME
13671 && !cp_parser_nth_token_starts_template_argument_list_p
13674 cp_parser_error (parser
, "expected template-id");
13675 return error_mark_node
;
13678 /* Remember where the template-id starts. */
13679 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13680 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13682 push_deferring_access_checks (dk_deferred
);
13684 /* Parse the template-name. */
13685 is_identifier
= false;
13686 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13687 check_dependency_p
,
13691 if (templ
== error_mark_node
|| is_identifier
)
13693 pop_deferring_access_checks ();
13697 /* If we find the sequence `[:' after a template-name, it's probably
13698 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13699 parse correctly the argument list. */
13700 next_token
= cp_lexer_peek_token (parser
->lexer
);
13701 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13702 if (next_token
->type
== CPP_OPEN_SQUARE
13703 && next_token
->flags
& DIGRAPH
13704 && next_token_2
->type
== CPP_COLON
13705 && !(next_token_2
->flags
& PREV_WHITE
))
13707 cp_parser_parse_tentatively (parser
);
13708 /* Change `:' into `::'. */
13709 next_token_2
->type
= CPP_SCOPE
;
13710 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13712 cp_lexer_consume_token (parser
->lexer
);
13714 /* Parse the arguments. */
13715 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13716 if (!cp_parser_parse_definitely (parser
))
13718 /* If we couldn't parse an argument list, then we revert our changes
13719 and return simply an error. Maybe this is not a template-id
13721 next_token_2
->type
= CPP_COLON
;
13722 cp_parser_error (parser
, "expected %<<%>");
13723 pop_deferring_access_checks ();
13724 return error_mark_node
;
13726 /* Otherwise, emit an error about the invalid digraph, but continue
13727 parsing because we got our argument list. */
13728 if (permerror (next_token
->location
,
13729 "%<<::%> cannot begin a template-argument list"))
13731 static bool hint
= false;
13732 inform (next_token
->location
,
13733 "%<<:%> is an alternate spelling for %<[%>."
13734 " Insert whitespace between %<<%> and %<::%>");
13735 if (!hint
&& !flag_permissive
)
13737 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13738 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13739 "accept your code)");
13746 /* Look for the `<' that starts the template-argument-list. */
13747 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13749 pop_deferring_access_checks ();
13750 return error_mark_node
;
13752 /* Parse the arguments. */
13753 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13756 /* Build a representation of the specialization. */
13757 if (identifier_p (templ
))
13758 template_id
= build_min_nt_loc (next_token
->location
,
13761 else if (DECL_TYPE_TEMPLATE_P (templ
)
13762 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13764 bool entering_scope
;
13765 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13766 template (rather than some instantiation thereof) only if
13767 is not nested within some other construct. For example, in
13768 "template <typename T> void f(T) { A<T>::", A<T> is just an
13769 instantiation of A. */
13770 entering_scope
= (template_parm_scope_p ()
13771 && cp_lexer_next_token_is (parser
->lexer
,
13774 = finish_template_type (templ
, arguments
, entering_scope
);
13776 else if (variable_template_p (templ
))
13778 template_id
= lookup_template_variable (templ
, arguments
);
13782 /* If it's not a class-template or a template-template, it should be
13783 a function-template. */
13784 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13785 || TREE_CODE (templ
) == OVERLOAD
13786 || BASELINK_P (templ
)));
13788 template_id
= lookup_template_function (templ
, arguments
);
13791 /* If parsing tentatively, replace the sequence of tokens that makes
13792 up the template-id with a CPP_TEMPLATE_ID token. That way,
13793 should we re-parse the token stream, we will not have to repeat
13794 the effort required to do the parse, nor will we issue duplicate
13795 error messages about problems during instantiation of the
13798 /* Don't do this if we had a parse error in a declarator; re-parsing
13799 might succeed if a name changes meaning (60361). */
13800 && !(cp_parser_error_occurred (parser
)
13801 && cp_parser_parsing_tentatively (parser
)
13802 && parser
->in_declarator_p
))
13804 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13806 /* Reset the contents of the START_OF_ID token. */
13807 token
->type
= CPP_TEMPLATE_ID
;
13808 /* Retrieve any deferred checks. Do not pop this access checks yet
13809 so the memory will not be reclaimed during token replacing below. */
13810 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13811 token
->u
.tree_check_value
->value
= template_id
;
13812 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13813 token
->keyword
= RID_MAX
;
13815 /* Purge all subsequent tokens. */
13816 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13818 /* ??? Can we actually assume that, if template_id ==
13819 error_mark_node, we will have issued a diagnostic to the
13820 user, as opposed to simply marking the tentative parse as
13822 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13823 error_at (token
->location
, "parse error in template argument list");
13826 pop_to_parent_deferring_access_checks ();
13827 return template_id
;
13830 /* Parse a template-name.
13835 The standard should actually say:
13839 operator-function-id
13841 A defect report has been filed about this issue.
13843 A conversion-function-id cannot be a template name because they cannot
13844 be part of a template-id. In fact, looking at this code:
13846 a.operator K<int>()
13848 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13849 It is impossible to call a templated conversion-function-id with an
13850 explicit argument list, since the only allowed template parameter is
13851 the type to which it is converting.
13853 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13854 `template' keyword, in a construction like:
13858 In that case `f' is taken to be a template-name, even though there
13859 is no way of knowing for sure.
13861 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13862 name refers to a set of overloaded functions, at least one of which
13863 is a template, or an IDENTIFIER_NODE with the name of the template,
13864 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13865 names are looked up inside uninstantiated templates. */
13868 cp_parser_template_name (cp_parser
* parser
,
13869 bool template_keyword_p
,
13870 bool check_dependency_p
,
13871 bool is_declaration
,
13872 enum tag_types tag_type
,
13873 bool *is_identifier
)
13878 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13880 /* If the next token is `operator', then we have either an
13881 operator-function-id or a conversion-function-id. */
13882 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13884 /* We don't know whether we're looking at an
13885 operator-function-id or a conversion-function-id. */
13886 cp_parser_parse_tentatively (parser
);
13887 /* Try an operator-function-id. */
13888 identifier
= cp_parser_operator_function_id (parser
);
13889 /* If that didn't work, try a conversion-function-id. */
13890 if (!cp_parser_parse_definitely (parser
))
13892 cp_parser_error (parser
, "expected template-name");
13893 return error_mark_node
;
13896 /* Look for the identifier. */
13898 identifier
= cp_parser_identifier (parser
);
13900 /* If we didn't find an identifier, we don't have a template-id. */
13901 if (identifier
== error_mark_node
)
13902 return error_mark_node
;
13904 /* If the name immediately followed the `template' keyword, then it
13905 is a template-name. However, if the next token is not `<', then
13906 we do not treat it as a template-name, since it is not being used
13907 as part of a template-id. This enables us to handle constructs
13910 template <typename T> struct S { S(); };
13911 template <typename T> S<T>::S();
13913 correctly. We would treat `S' as a template -- if it were `S<T>'
13914 -- but we do not if there is no `<'. */
13916 if (processing_template_decl
13917 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13919 /* In a declaration, in a dependent context, we pretend that the
13920 "template" keyword was present in order to improve error
13921 recovery. For example, given:
13923 template <typename T> void f(T::X<int>);
13925 we want to treat "X<int>" as a template-id. */
13927 && !template_keyword_p
13928 && parser
->scope
&& TYPE_P (parser
->scope
)
13929 && check_dependency_p
13930 && dependent_scope_p (parser
->scope
)
13931 /* Do not do this for dtors (or ctors), since they never
13932 need the template keyword before their name. */
13933 && !constructor_name_p (identifier
, parser
->scope
))
13935 cp_token_position start
= 0;
13937 /* Explain what went wrong. */
13938 error_at (token
->location
, "non-template %qD used as template",
13940 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13941 parser
->scope
, identifier
);
13942 /* If parsing tentatively, find the location of the "<" token. */
13943 if (cp_parser_simulate_error (parser
))
13944 start
= cp_lexer_token_position (parser
->lexer
, true);
13945 /* Parse the template arguments so that we can issue error
13946 messages about them. */
13947 cp_lexer_consume_token (parser
->lexer
);
13948 cp_parser_enclosed_template_argument_list (parser
);
13949 /* Skip tokens until we find a good place from which to
13950 continue parsing. */
13951 cp_parser_skip_to_closing_parenthesis (parser
,
13952 /*recovering=*/true,
13954 /*consume_paren=*/false);
13955 /* If parsing tentatively, permanently remove the
13956 template argument list. That will prevent duplicate
13957 error messages from being issued about the missing
13958 "template" keyword. */
13960 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13962 *is_identifier
= true;
13966 /* If the "template" keyword is present, then there is generally
13967 no point in doing name-lookup, so we just return IDENTIFIER.
13968 But, if the qualifying scope is non-dependent then we can
13969 (and must) do name-lookup normally. */
13970 if (template_keyword_p
13972 || (TYPE_P (parser
->scope
)
13973 && dependent_type_p (parser
->scope
))))
13977 /* Look up the name. */
13978 decl
= cp_parser_lookup_name (parser
, identifier
,
13980 /*is_template=*/true,
13981 /*is_namespace=*/false,
13982 check_dependency_p
,
13983 /*ambiguous_decls=*/NULL
,
13986 /* If DECL is a template, then the name was a template-name. */
13987 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13989 if (TREE_DEPRECATED (decl
)
13990 && deprecated_state
!= DEPRECATED_SUPPRESS
)
13991 warn_deprecated_use (decl
, NULL_TREE
);
13995 tree fn
= NULL_TREE
;
13997 /* The standard does not explicitly indicate whether a name that
13998 names a set of overloaded declarations, some of which are
13999 templates, is a template-name. However, such a name should
14000 be a template-name; otherwise, there is no way to form a
14001 template-id for the overloaded templates. */
14002 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14003 if (TREE_CODE (fns
) == OVERLOAD
)
14004 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14005 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14010 /* The name does not name a template. */
14011 cp_parser_error (parser
, "expected template-name");
14012 return error_mark_node
;
14016 /* If DECL is dependent, and refers to a function, then just return
14017 its name; we will look it up again during template instantiation. */
14018 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14020 tree scope
= ovl_scope (decl
);
14021 if (TYPE_P (scope
) && dependent_type_p (scope
))
14028 /* Parse a template-argument-list.
14030 template-argument-list:
14031 template-argument ... [opt]
14032 template-argument-list , template-argument ... [opt]
14034 Returns a TREE_VEC containing the arguments. */
14037 cp_parser_template_argument_list (cp_parser
* parser
)
14039 tree fixed_args
[10];
14040 unsigned n_args
= 0;
14041 unsigned alloced
= 10;
14042 tree
*arg_ary
= fixed_args
;
14044 bool saved_in_template_argument_list_p
;
14046 bool saved_non_ice_p
;
14048 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14049 parser
->in_template_argument_list_p
= true;
14050 /* Even if the template-id appears in an integral
14051 constant-expression, the contents of the argument list do
14053 saved_ice_p
= parser
->integral_constant_expression_p
;
14054 parser
->integral_constant_expression_p
= false;
14055 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14056 parser
->non_integral_constant_expression_p
= false;
14058 /* Parse the arguments. */
14064 /* Consume the comma. */
14065 cp_lexer_consume_token (parser
->lexer
);
14067 /* Parse the template-argument. */
14068 argument
= cp_parser_template_argument (parser
);
14070 /* If the next token is an ellipsis, we're expanding a template
14072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14074 if (argument
== error_mark_node
)
14076 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14077 error_at (token
->location
,
14078 "expected parameter pack before %<...%>");
14080 /* Consume the `...' token. */
14081 cp_lexer_consume_token (parser
->lexer
);
14083 /* Make the argument into a TYPE_PACK_EXPANSION or
14084 EXPR_PACK_EXPANSION. */
14085 argument
= make_pack_expansion (argument
);
14088 if (n_args
== alloced
)
14092 if (arg_ary
== fixed_args
)
14094 arg_ary
= XNEWVEC (tree
, alloced
);
14095 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14098 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14100 arg_ary
[n_args
++] = argument
;
14102 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14104 vec
= make_tree_vec (n_args
);
14107 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14109 if (arg_ary
!= fixed_args
)
14111 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14112 parser
->integral_constant_expression_p
= saved_ice_p
;
14113 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14114 #ifdef ENABLE_CHECKING
14115 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14120 /* Parse a template-argument.
14123 assignment-expression
14127 The representation is that of an assignment-expression, type-id, or
14128 id-expression -- except that the qualified id-expression is
14129 evaluated, so that the value returned is either a DECL or an
14132 Although the standard says "assignment-expression", it forbids
14133 throw-expressions or assignments in the template argument.
14134 Therefore, we use "conditional-expression" instead. */
14137 cp_parser_template_argument (cp_parser
* parser
)
14142 bool maybe_type_id
= false;
14143 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14144 location_t loc
= 0;
14147 /* There's really no way to know what we're looking at, so we just
14148 try each alternative in order.
14152 In a template-argument, an ambiguity between a type-id and an
14153 expression is resolved to a type-id, regardless of the form of
14154 the corresponding template-parameter.
14156 Therefore, we try a type-id first. */
14157 cp_parser_parse_tentatively (parser
);
14158 argument
= cp_parser_template_type_arg (parser
);
14159 /* If there was no error parsing the type-id but the next token is a
14160 '>>', our behavior depends on which dialect of C++ we're
14161 parsing. In C++98, we probably found a typo for '> >'. But there
14162 are type-id which are also valid expressions. For instance:
14164 struct X { int operator >> (int); };
14165 template <int V> struct Foo {};
14168 Here 'X()' is a valid type-id of a function type, but the user just
14169 wanted to write the expression "X() >> 5". Thus, we remember that we
14170 found a valid type-id, but we still try to parse the argument as an
14171 expression to see what happens.
14173 In C++0x, the '>>' will be considered two separate '>'
14175 if (!cp_parser_error_occurred (parser
)
14176 && cxx_dialect
== cxx98
14177 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14179 maybe_type_id
= true;
14180 cp_parser_abort_tentative_parse (parser
);
14184 /* If the next token isn't a `,' or a `>', then this argument wasn't
14185 really finished. This means that the argument is not a valid
14187 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14188 cp_parser_error (parser
, "expected template-argument");
14189 /* If that worked, we're done. */
14190 if (cp_parser_parse_definitely (parser
))
14193 /* We're still not sure what the argument will be. */
14194 cp_parser_parse_tentatively (parser
);
14195 /* Try a template. */
14196 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14197 argument
= cp_parser_id_expression (parser
,
14198 /*template_keyword_p=*/false,
14199 /*check_dependency_p=*/true,
14201 /*declarator_p=*/false,
14202 /*optional_p=*/false);
14203 /* If the next token isn't a `,' or a `>', then this argument wasn't
14204 really finished. */
14205 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14206 cp_parser_error (parser
, "expected template-argument");
14207 if (!cp_parser_error_occurred (parser
))
14209 /* Figure out what is being referred to. If the id-expression
14210 was for a class template specialization, then we will have a
14211 TYPE_DECL at this point. There is no need to do name lookup
14212 at this point in that case. */
14213 if (TREE_CODE (argument
) != TYPE_DECL
)
14214 argument
= cp_parser_lookup_name (parser
, argument
,
14216 /*is_template=*/template_p
,
14217 /*is_namespace=*/false,
14218 /*check_dependency=*/true,
14219 /*ambiguous_decls=*/NULL
,
14220 argument_start_token
->location
);
14221 if (TREE_CODE (argument
) != TEMPLATE_DECL
14222 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14223 cp_parser_error (parser
, "expected template-name");
14225 if (cp_parser_parse_definitely (parser
))
14227 if (TREE_DEPRECATED (argument
))
14228 warn_deprecated_use (argument
, NULL_TREE
);
14231 /* It must be a non-type argument. There permitted cases are given
14232 in [temp.arg.nontype]:
14234 -- an integral constant-expression of integral or enumeration
14237 -- the name of a non-type template-parameter; or
14239 -- the name of an object or function with external linkage...
14241 -- the address of an object or function with external linkage...
14243 -- a pointer to member... */
14244 /* Look for a non-type template parameter. */
14245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14247 cp_parser_parse_tentatively (parser
);
14248 argument
= cp_parser_primary_expression (parser
,
14249 /*address_p=*/false,
14251 /*template_arg_p=*/true,
14253 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14254 || !cp_parser_next_token_ends_template_argument_p (parser
))
14255 cp_parser_simulate_error (parser
);
14256 if (cp_parser_parse_definitely (parser
))
14260 /* If the next token is "&", the argument must be the address of an
14261 object or function with external linkage. */
14262 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14265 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14266 cp_lexer_consume_token (parser
->lexer
);
14268 /* See if we might have an id-expression. */
14269 token
= cp_lexer_peek_token (parser
->lexer
);
14270 if (token
->type
== CPP_NAME
14271 || token
->keyword
== RID_OPERATOR
14272 || token
->type
== CPP_SCOPE
14273 || token
->type
== CPP_TEMPLATE_ID
14274 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14276 cp_parser_parse_tentatively (parser
);
14277 argument
= cp_parser_primary_expression (parser
,
14280 /*template_arg_p=*/true,
14282 if (cp_parser_error_occurred (parser
)
14283 || !cp_parser_next_token_ends_template_argument_p (parser
))
14284 cp_parser_abort_tentative_parse (parser
);
14289 if (INDIRECT_REF_P (argument
))
14291 /* Strip the dereference temporarily. */
14292 gcc_assert (REFERENCE_REF_P (argument
));
14293 argument
= TREE_OPERAND (argument
, 0);
14296 /* If we're in a template, we represent a qualified-id referring
14297 to a static data member as a SCOPE_REF even if the scope isn't
14298 dependent so that we can check access control later. */
14300 if (TREE_CODE (probe
) == SCOPE_REF
)
14301 probe
= TREE_OPERAND (probe
, 1);
14304 /* A variable without external linkage might still be a
14305 valid constant-expression, so no error is issued here
14306 if the external-linkage check fails. */
14307 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14308 cp_parser_simulate_error (parser
);
14310 else if (is_overloaded_fn (argument
))
14311 /* All overloaded functions are allowed; if the external
14312 linkage test does not pass, an error will be issued
14316 && (TREE_CODE (argument
) == OFFSET_REF
14317 || TREE_CODE (argument
) == SCOPE_REF
))
14318 /* A pointer-to-member. */
14320 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14323 cp_parser_simulate_error (parser
);
14325 if (cp_parser_parse_definitely (parser
))
14328 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14329 tf_warning_or_error
);
14331 argument
= convert_from_reference (argument
);
14336 /* If the argument started with "&", there are no other valid
14337 alternatives at this point. */
14340 cp_parser_error (parser
, "invalid non-type template argument");
14341 return error_mark_node
;
14344 /* If the argument wasn't successfully parsed as a type-id followed
14345 by '>>', the argument can only be a constant expression now.
14346 Otherwise, we try parsing the constant-expression tentatively,
14347 because the argument could really be a type-id. */
14349 cp_parser_parse_tentatively (parser
);
14350 argument
= cp_parser_constant_expression (parser
);
14352 if (!maybe_type_id
)
14354 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14355 cp_parser_error (parser
, "expected template-argument");
14356 if (cp_parser_parse_definitely (parser
))
14358 /* We did our best to parse the argument as a non type-id, but that
14359 was the only alternative that matched (albeit with a '>' after
14360 it). We can assume it's just a typo from the user, and a
14361 diagnostic will then be issued. */
14362 return cp_parser_template_type_arg (parser
);
14365 /* Parse an explicit-instantiation.
14367 explicit-instantiation:
14368 template declaration
14370 Although the standard says `declaration', what it really means is:
14372 explicit-instantiation:
14373 template decl-specifier-seq [opt] declarator [opt] ;
14375 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14376 supposed to be allowed. A defect report has been filed about this
14381 explicit-instantiation:
14382 storage-class-specifier template
14383 decl-specifier-seq [opt] declarator [opt] ;
14384 function-specifier template
14385 decl-specifier-seq [opt] declarator [opt] ; */
14388 cp_parser_explicit_instantiation (cp_parser
* parser
)
14390 int declares_class_or_enum
;
14391 cp_decl_specifier_seq decl_specifiers
;
14392 tree extension_specifier
= NULL_TREE
;
14394 timevar_push (TV_TEMPLATE_INST
);
14396 /* Look for an (optional) storage-class-specifier or
14397 function-specifier. */
14398 if (cp_parser_allow_gnu_extensions_p (parser
))
14400 extension_specifier
14401 = cp_parser_storage_class_specifier_opt (parser
);
14402 if (!extension_specifier
)
14403 extension_specifier
14404 = cp_parser_function_specifier_opt (parser
,
14405 /*decl_specs=*/NULL
);
14408 /* Look for the `template' keyword. */
14409 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14410 /* Let the front end know that we are processing an explicit
14412 begin_explicit_instantiation ();
14413 /* [temp.explicit] says that we are supposed to ignore access
14414 control while processing explicit instantiation directives. */
14415 push_deferring_access_checks (dk_no_check
);
14416 /* Parse a decl-specifier-seq. */
14417 cp_parser_decl_specifier_seq (parser
,
14418 CP_PARSER_FLAGS_OPTIONAL
,
14420 &declares_class_or_enum
);
14421 /* If there was exactly one decl-specifier, and it declared a class,
14422 and there's no declarator, then we have an explicit type
14424 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14428 type
= check_tag_decl (&decl_specifiers
,
14429 /*explicit_type_instantiation_p=*/true);
14430 /* Turn access control back on for names used during
14431 template instantiation. */
14432 pop_deferring_access_checks ();
14434 do_type_instantiation (type
, extension_specifier
,
14435 /*complain=*/tf_error
);
14439 cp_declarator
*declarator
;
14442 /* Parse the declarator. */
14444 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14445 /*ctor_dtor_or_conv_p=*/NULL
,
14446 /*parenthesized_p=*/NULL
,
14447 /*member_p=*/false,
14448 /*friend_p=*/false);
14449 if (declares_class_or_enum
& 2)
14450 cp_parser_check_for_definition_in_return_type (declarator
,
14451 decl_specifiers
.type
,
14452 decl_specifiers
.locations
[ds_type_spec
]);
14453 if (declarator
!= cp_error_declarator
)
14455 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14456 permerror (decl_specifiers
.locations
[ds_inline
],
14457 "explicit instantiation shall not use"
14458 " %<inline%> specifier");
14459 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14460 permerror (decl_specifiers
.locations
[ds_constexpr
],
14461 "explicit instantiation shall not use"
14462 " %<constexpr%> specifier");
14464 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14465 NORMAL
, 0, &decl_specifiers
.attributes
);
14466 /* Turn access control back on for names used during
14467 template instantiation. */
14468 pop_deferring_access_checks ();
14469 /* Do the explicit instantiation. */
14470 do_decl_instantiation (decl
, extension_specifier
);
14474 pop_deferring_access_checks ();
14475 /* Skip the body of the explicit instantiation. */
14476 cp_parser_skip_to_end_of_statement (parser
);
14479 /* We're done with the instantiation. */
14480 end_explicit_instantiation ();
14482 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14484 timevar_pop (TV_TEMPLATE_INST
);
14487 /* Parse an explicit-specialization.
14489 explicit-specialization:
14490 template < > declaration
14492 Although the standard says `declaration', what it really means is:
14494 explicit-specialization:
14495 template <> decl-specifier [opt] init-declarator [opt] ;
14496 template <> function-definition
14497 template <> explicit-specialization
14498 template <> template-declaration */
14501 cp_parser_explicit_specialization (cp_parser
* parser
)
14503 bool need_lang_pop
;
14504 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14506 /* Look for the `template' keyword. */
14507 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14508 /* Look for the `<'. */
14509 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14510 /* Look for the `>'. */
14511 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14512 /* We have processed another parameter list. */
14513 ++parser
->num_template_parameter_lists
;
14516 A template ... explicit specialization ... shall not have C
14518 if (current_lang_name
== lang_name_c
)
14520 error_at (token
->location
, "template specialization with C linkage");
14521 /* Give it C++ linkage to avoid confusing other parts of the
14523 push_lang_context (lang_name_cplusplus
);
14524 need_lang_pop
= true;
14527 need_lang_pop
= false;
14528 /* Let the front end know that we are beginning a specialization. */
14529 if (!begin_specialization ())
14531 end_specialization ();
14535 /* If the next keyword is `template', we need to figure out whether
14536 or not we're looking a template-declaration. */
14537 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14539 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14540 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14541 cp_parser_template_declaration_after_export (parser
,
14542 /*member_p=*/false);
14544 cp_parser_explicit_specialization (parser
);
14547 /* Parse the dependent declaration. */
14548 cp_parser_single_declaration (parser
,
14550 /*member_p=*/false,
14551 /*explicit_specialization_p=*/true,
14552 /*friend_p=*/NULL
);
14553 /* We're done with the specialization. */
14554 end_specialization ();
14555 /* For the erroneous case of a template with C linkage, we pushed an
14556 implicit C++ linkage scope; exit that scope now. */
14558 pop_lang_context ();
14559 /* We're done with this parameter list. */
14560 --parser
->num_template_parameter_lists
;
14563 /* Parse a type-specifier.
14566 simple-type-specifier
14569 elaborated-type-specifier
14577 Returns a representation of the type-specifier. For a
14578 class-specifier, enum-specifier, or elaborated-type-specifier, a
14579 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14581 The parser flags FLAGS is used to control type-specifier parsing.
14583 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14584 in a decl-specifier-seq.
14586 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14587 class-specifier, enum-specifier, or elaborated-type-specifier, then
14588 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14589 if a type is declared; 2 if it is defined. Otherwise, it is set to
14592 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14593 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14594 is set to FALSE. */
14597 cp_parser_type_specifier (cp_parser
* parser
,
14598 cp_parser_flags flags
,
14599 cp_decl_specifier_seq
*decl_specs
,
14600 bool is_declaration
,
14601 int* declares_class_or_enum
,
14602 bool* is_cv_qualifier
)
14604 tree type_spec
= NULL_TREE
;
14607 cp_decl_spec ds
= ds_last
;
14609 /* Assume this type-specifier does not declare a new type. */
14610 if (declares_class_or_enum
)
14611 *declares_class_or_enum
= 0;
14612 /* And that it does not specify a cv-qualifier. */
14613 if (is_cv_qualifier
)
14614 *is_cv_qualifier
= false;
14615 /* Peek at the next token. */
14616 token
= cp_lexer_peek_token (parser
->lexer
);
14618 /* If we're looking at a keyword, we can use that to guide the
14619 production we choose. */
14620 keyword
= token
->keyword
;
14624 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14625 goto elaborated_type_specifier
;
14627 /* Look for the enum-specifier. */
14628 type_spec
= cp_parser_enum_specifier (parser
);
14629 /* If that worked, we're done. */
14632 if (declares_class_or_enum
)
14633 *declares_class_or_enum
= 2;
14635 cp_parser_set_decl_spec_type (decl_specs
,
14638 /*type_definition_p=*/true);
14642 goto elaborated_type_specifier
;
14644 /* Any of these indicate either a class-specifier, or an
14645 elaborated-type-specifier. */
14649 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14650 goto elaborated_type_specifier
;
14652 /* Parse tentatively so that we can back up if we don't find a
14653 class-specifier. */
14654 cp_parser_parse_tentatively (parser
);
14655 /* Look for the class-specifier. */
14656 type_spec
= cp_parser_class_specifier (parser
);
14657 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14658 /* If that worked, we're done. */
14659 if (cp_parser_parse_definitely (parser
))
14661 if (declares_class_or_enum
)
14662 *declares_class_or_enum
= 2;
14664 cp_parser_set_decl_spec_type (decl_specs
,
14667 /*type_definition_p=*/true);
14671 /* Fall through. */
14672 elaborated_type_specifier
:
14673 /* We're declaring (not defining) a class or enum. */
14674 if (declares_class_or_enum
)
14675 *declares_class_or_enum
= 1;
14677 /* Fall through. */
14679 /* Look for an elaborated-type-specifier. */
14681 = (cp_parser_elaborated_type_specifier
14683 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14686 cp_parser_set_decl_spec_type (decl_specs
,
14689 /*type_definition_p=*/false);
14694 if (is_cv_qualifier
)
14695 *is_cv_qualifier
= true;
14700 if (is_cv_qualifier
)
14701 *is_cv_qualifier
= true;
14706 if (is_cv_qualifier
)
14707 *is_cv_qualifier
= true;
14711 /* The `__complex__' keyword is a GNU extension. */
14719 /* Handle simple keywords. */
14724 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14725 decl_specs
->any_specifiers_p
= true;
14727 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14730 /* If we do not already have a type-specifier, assume we are looking
14731 at a simple-type-specifier. */
14732 type_spec
= cp_parser_simple_type_specifier (parser
,
14736 /* If we didn't find a type-specifier, and a type-specifier was not
14737 optional in this context, issue an error message. */
14738 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14740 cp_parser_error (parser
, "expected type specifier");
14741 return error_mark_node
;
14747 /* Parse a simple-type-specifier.
14749 simple-type-specifier:
14750 :: [opt] nested-name-specifier [opt] type-name
14751 :: [opt] nested-name-specifier template template-id
14766 simple-type-specifier:
14768 decltype ( expression )
14771 __underlying_type ( type-id )
14775 simple-type-specifier:
14777 __typeof__ unary-expression
14778 __typeof__ ( type-id )
14779 __typeof__ ( type-id ) { initializer-list , [opt] }
14781 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14782 appropriately updated. */
14785 cp_parser_simple_type_specifier (cp_parser
* parser
,
14786 cp_decl_specifier_seq
*decl_specs
,
14787 cp_parser_flags flags
)
14789 tree type
= NULL_TREE
;
14793 /* Peek at the next token. */
14794 token
= cp_lexer_peek_token (parser
->lexer
);
14796 /* If we're looking at a keyword, things are easy. */
14797 switch (token
->keyword
)
14801 decl_specs
->explicit_char_p
= true;
14802 type
= char_type_node
;
14805 type
= char16_type_node
;
14808 type
= char32_type_node
;
14811 type
= wchar_type_node
;
14814 type
= boolean_type_node
;
14817 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14818 type
= short_integer_type_node
;
14822 decl_specs
->explicit_int_p
= true;
14823 type
= integer_type_node
;
14829 idx
= token
->keyword
- RID_INT_N_0
;
14830 if (! int_n_enabled_p
[idx
])
14834 decl_specs
->explicit_intN_p
= true;
14835 decl_specs
->int_n_idx
= idx
;
14837 type
= int_n_trees
[idx
].signed_type
;
14841 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14842 type
= long_integer_type_node
;
14845 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14846 type
= integer_type_node
;
14849 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14850 type
= unsigned_type_node
;
14853 type
= float_type_node
;
14856 type
= double_type_node
;
14859 type
= void_type_node
;
14863 maybe_warn_cpp0x (CPP0X_AUTO
);
14864 if (parser
->auto_is_implicit_function_template_parm_p
)
14866 if (cxx_dialect
>= cxx14
)
14867 type
= synthesize_implicit_template_parm (parser
);
14869 type
= error_mark_node
;
14871 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14873 if (cxx_dialect
< cxx14
)
14874 error_at (token
->location
,
14875 "use of %<auto%> in lambda parameter declaration "
14876 "only available with "
14877 "-std=c++14 or -std=gnu++14");
14879 else if (cxx_dialect
< cxx14
)
14880 error_at (token
->location
,
14881 "use of %<auto%> in parameter declaration "
14882 "only available with "
14883 "-std=c++14 or -std=gnu++14");
14885 pedwarn (token
->location
, OPT_Wpedantic
,
14886 "ISO C++ forbids use of %<auto%> in parameter "
14890 type
= make_auto ();
14894 /* Since DR 743, decltype can either be a simple-type-specifier by
14895 itself or begin a nested-name-specifier. Parsing it will replace
14896 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14897 handling below decide what to do. */
14898 cp_parser_decltype (parser
);
14899 cp_lexer_set_token_position (parser
->lexer
, token
);
14903 /* Consume the `typeof' token. */
14904 cp_lexer_consume_token (parser
->lexer
);
14905 /* Parse the operand to `typeof'. */
14906 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14907 /* If it is not already a TYPE, take its type. */
14908 if (!TYPE_P (type
))
14909 type
= finish_typeof (type
);
14912 cp_parser_set_decl_spec_type (decl_specs
, type
,
14914 /*type_definition_p=*/false);
14918 case RID_UNDERLYING_TYPE
:
14919 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14921 cp_parser_set_decl_spec_type (decl_specs
, type
,
14923 /*type_definition_p=*/false);
14928 case RID_DIRECT_BASES
:
14929 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14931 cp_parser_set_decl_spec_type (decl_specs
, type
,
14933 /*type_definition_p=*/false);
14939 /* If token is an already-parsed decltype not followed by ::,
14940 it's a simple-type-specifier. */
14941 if (token
->type
== CPP_DECLTYPE
14942 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14944 type
= token
->u
.value
;
14947 cp_parser_set_decl_spec_type (decl_specs
, type
,
14949 /*type_definition_p=*/false);
14950 /* Remember that we are handling a decltype in order to
14951 implement the resolution of DR 1510 when the argument
14952 isn't instantiation dependent. */
14953 decl_specs
->decltype_p
= true;
14955 cp_lexer_consume_token (parser
->lexer
);
14959 /* If the type-specifier was for a built-in type, we're done. */
14962 /* Record the type. */
14964 && (token
->keyword
!= RID_SIGNED
14965 && token
->keyword
!= RID_UNSIGNED
14966 && token
->keyword
!= RID_SHORT
14967 && token
->keyword
!= RID_LONG
))
14968 cp_parser_set_decl_spec_type (decl_specs
,
14971 /*type_definition_p=*/false);
14973 decl_specs
->any_specifiers_p
= true;
14975 /* Consume the token. */
14976 cp_lexer_consume_token (parser
->lexer
);
14978 if (type
== error_mark_node
)
14979 return error_mark_node
;
14981 /* There is no valid C++ program where a non-template type is
14982 followed by a "<". That usually indicates that the user thought
14983 that the type was a template. */
14984 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14987 return TYPE_NAME (type
);
14990 /* The type-specifier must be a user-defined type. */
14991 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14996 /* Don't gobble tokens or issue error messages if this is an
14997 optional type-specifier. */
14998 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14999 cp_parser_parse_tentatively (parser
);
15001 /* Look for the optional `::' operator. */
15003 = (cp_parser_global_scope_opt (parser
,
15004 /*current_scope_valid_p=*/false)
15006 /* Look for the nested-name specifier. */
15008 = (cp_parser_nested_name_specifier_opt (parser
,
15009 /*typename_keyword_p=*/false,
15010 /*check_dependency_p=*/true,
15012 /*is_declaration=*/false)
15014 token
= cp_lexer_peek_token (parser
->lexer
);
15015 /* If we have seen a nested-name-specifier, and the next token
15016 is `template', then we are using the template-id production. */
15018 && cp_parser_optional_template_keyword (parser
))
15020 /* Look for the template-id. */
15021 type
= cp_parser_template_id (parser
,
15022 /*template_keyword_p=*/true,
15023 /*check_dependency_p=*/true,
15025 /*is_declaration=*/false);
15026 /* If the template-id did not name a type, we are out of
15028 if (TREE_CODE (type
) != TYPE_DECL
)
15030 cp_parser_error (parser
, "expected template-id for type");
15034 /* Otherwise, look for a type-name. */
15036 type
= cp_parser_type_name (parser
);
15037 /* Keep track of all name-lookups performed in class scopes. */
15041 && TREE_CODE (type
) == TYPE_DECL
15042 && identifier_p (DECL_NAME (type
)))
15043 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15044 /* If it didn't work out, we don't have a TYPE. */
15045 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15046 && !cp_parser_parse_definitely (parser
))
15048 if (type
&& decl_specs
)
15049 cp_parser_set_decl_spec_type (decl_specs
, type
,
15051 /*type_definition_p=*/false);
15054 /* If we didn't get a type-name, issue an error message. */
15055 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15057 cp_parser_error (parser
, "expected type-name");
15058 return error_mark_node
;
15061 if (type
&& type
!= error_mark_node
)
15063 /* See if TYPE is an Objective-C type, and if so, parse and
15064 accept any protocol references following it. Do this before
15065 the cp_parser_check_for_invalid_template_id() call, because
15066 Objective-C types can be followed by '<...>' which would
15067 enclose protocol names rather than template arguments, and so
15068 everything is fine. */
15069 if (c_dialect_objc () && !parser
->scope
15070 && (objc_is_id (type
) || objc_is_class_name (type
)))
15072 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15073 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15075 /* Clobber the "unqualified" type previously entered into
15076 DECL_SPECS with the new, improved protocol-qualified version. */
15078 decl_specs
->type
= qual_type
;
15083 /* There is no valid C++ program where a non-template type is
15084 followed by a "<". That usually indicates that the user
15085 thought that the type was a template. */
15086 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15094 /* Parse a type-name.
15100 simple-template-id [in c++0x]
15108 Returns a TYPE_DECL for the type. */
15111 cp_parser_type_name (cp_parser
* parser
)
15115 /* We can't know yet whether it is a class-name or not. */
15116 cp_parser_parse_tentatively (parser
);
15117 /* Try a class-name. */
15118 type_decl
= cp_parser_class_name (parser
,
15119 /*typename_keyword_p=*/false,
15120 /*template_keyword_p=*/false,
15122 /*check_dependency_p=*/true,
15123 /*class_head_p=*/false,
15124 /*is_declaration=*/false);
15125 /* If it's not a class-name, keep looking. */
15126 if (!cp_parser_parse_definitely (parser
))
15128 if (cxx_dialect
< cxx11
)
15129 /* It must be a typedef-name or an enum-name. */
15130 return cp_parser_nonclass_name (parser
);
15132 cp_parser_parse_tentatively (parser
);
15133 /* It is either a simple-template-id representing an
15134 instantiation of an alias template... */
15135 type_decl
= cp_parser_template_id (parser
,
15136 /*template_keyword_p=*/false,
15137 /*check_dependency_p=*/true,
15139 /*is_declaration=*/false);
15140 /* Note that this must be an instantiation of an alias template
15141 because [temp.names]/6 says:
15143 A template-id that names an alias template specialization
15146 Whereas [temp.names]/7 says:
15148 A simple-template-id that names a class template
15149 specialization is a class-name. */
15150 if (type_decl
!= NULL_TREE
15151 && TREE_CODE (type_decl
) == TYPE_DECL
15152 && TYPE_DECL_ALIAS_P (type_decl
))
15153 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15155 cp_parser_simulate_error (parser
);
15157 if (!cp_parser_parse_definitely (parser
))
15158 /* ... Or a typedef-name or an enum-name. */
15159 return cp_parser_nonclass_name (parser
);
15165 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15173 Returns a TYPE_DECL for the type. */
15176 cp_parser_nonclass_name (cp_parser
* parser
)
15181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15182 identifier
= cp_parser_identifier (parser
);
15183 if (identifier
== error_mark_node
)
15184 return error_mark_node
;
15186 /* Look up the type-name. */
15187 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15189 type_decl
= strip_using_decl (type_decl
);
15191 if (TREE_CODE (type_decl
) != TYPE_DECL
15192 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15194 /* See if this is an Objective-C type. */
15195 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15196 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15198 type_decl
= TYPE_NAME (type
);
15201 /* Issue an error if we did not find a type-name. */
15202 if (TREE_CODE (type_decl
) != TYPE_DECL
15203 /* In Objective-C, we have the complication that class names are
15204 normally type names and start declarations (eg, the
15205 "NSObject" in "NSObject *object;"), but can be used in an
15206 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15207 is an expression. So, a classname followed by a dot is not a
15208 valid type-name. */
15209 || (objc_is_class_name (TREE_TYPE (type_decl
))
15210 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15212 if (!cp_parser_simulate_error (parser
))
15213 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15214 NLE_TYPE
, token
->location
);
15215 return error_mark_node
;
15217 /* Remember that the name was used in the definition of the
15218 current class so that we can check later to see if the
15219 meaning would have been different after the class was
15220 entirely defined. */
15221 else if (type_decl
!= error_mark_node
15223 maybe_note_name_used_in_class (identifier
, type_decl
);
15228 /* Parse an elaborated-type-specifier. Note that the grammar given
15229 here incorporates the resolution to DR68.
15231 elaborated-type-specifier:
15232 class-key :: [opt] nested-name-specifier [opt] identifier
15233 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15234 enum-key :: [opt] nested-name-specifier [opt] identifier
15235 typename :: [opt] nested-name-specifier identifier
15236 typename :: [opt] nested-name-specifier template [opt]
15241 elaborated-type-specifier:
15242 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15243 class-key attributes :: [opt] nested-name-specifier [opt]
15244 template [opt] template-id
15245 enum attributes :: [opt] nested-name-specifier [opt] identifier
15247 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15248 declared `friend'. If IS_DECLARATION is TRUE, then this
15249 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15250 something is being declared.
15252 Returns the TYPE specified. */
15255 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15257 bool is_declaration
)
15259 enum tag_types tag_type
;
15261 tree type
= NULL_TREE
;
15262 tree attributes
= NULL_TREE
;
15264 cp_token
*token
= NULL
;
15266 /* See if we're looking at the `enum' keyword. */
15267 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15269 /* Consume the `enum' token. */
15270 cp_lexer_consume_token (parser
->lexer
);
15271 /* Remember that it's an enumeration type. */
15272 tag_type
= enum_type
;
15273 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15274 enums) is used here. */
15275 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15276 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15278 pedwarn (input_location
, 0, "elaborated-type-specifier "
15279 "for a scoped enum must not use the %<%D%> keyword",
15280 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15281 /* Consume the `struct' or `class' and parse it anyway. */
15282 cp_lexer_consume_token (parser
->lexer
);
15284 /* Parse the attributes. */
15285 attributes
= cp_parser_attributes_opt (parser
);
15287 /* Or, it might be `typename'. */
15288 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15291 /* Consume the `typename' token. */
15292 cp_lexer_consume_token (parser
->lexer
);
15293 /* Remember that it's a `typename' type. */
15294 tag_type
= typename_type
;
15296 /* Otherwise it must be a class-key. */
15299 tag_type
= cp_parser_class_key (parser
);
15300 if (tag_type
== none_type
)
15301 return error_mark_node
;
15302 /* Parse the attributes. */
15303 attributes
= cp_parser_attributes_opt (parser
);
15306 /* Look for the `::' operator. */
15307 globalscope
= cp_parser_global_scope_opt (parser
,
15308 /*current_scope_valid_p=*/false);
15309 /* Look for the nested-name-specifier. */
15310 if (tag_type
== typename_type
&& !globalscope
)
15312 if (!cp_parser_nested_name_specifier (parser
,
15313 /*typename_keyword_p=*/true,
15314 /*check_dependency_p=*/true,
15317 return error_mark_node
;
15320 /* Even though `typename' is not present, the proposed resolution
15321 to Core Issue 180 says that in `class A<T>::B', `B' should be
15322 considered a type-name, even if `A<T>' is dependent. */
15323 cp_parser_nested_name_specifier_opt (parser
,
15324 /*typename_keyword_p=*/true,
15325 /*check_dependency_p=*/true,
15328 /* For everything but enumeration types, consider a template-id.
15329 For an enumeration type, consider only a plain identifier. */
15330 if (tag_type
!= enum_type
)
15332 bool template_p
= false;
15335 /* Allow the `template' keyword. */
15336 template_p
= cp_parser_optional_template_keyword (parser
);
15337 /* If we didn't see `template', we don't know if there's a
15338 template-id or not. */
15340 cp_parser_parse_tentatively (parser
);
15341 /* Parse the template-id. */
15342 token
= cp_lexer_peek_token (parser
->lexer
);
15343 decl
= cp_parser_template_id (parser
, template_p
,
15344 /*check_dependency_p=*/true,
15347 /* If we didn't find a template-id, look for an ordinary
15349 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15351 /* We can get here when cp_parser_template_id, called by
15352 cp_parser_class_name with tag_type == none_type, succeeds
15353 and caches a BASELINK. Then, when called again here,
15354 instead of failing and returning an error_mark_node
15355 returns it (see template/typename17.C in C++11).
15356 ??? Could we diagnose this earlier? */
15357 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15359 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15360 type
= error_mark_node
;
15362 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15363 in effect, then we must assume that, upon instantiation, the
15364 template will correspond to a class. */
15365 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15366 && tag_type
== typename_type
)
15367 type
= make_typename_type (parser
->scope
, decl
,
15369 /*complain=*/tf_error
);
15370 /* If the `typename' keyword is in effect and DECL is not a type
15371 decl, then type is non existent. */
15372 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15374 else if (TREE_CODE (decl
) == TYPE_DECL
)
15375 type
= check_elaborated_type_specifier (tag_type
, decl
,
15376 /*allow_template_p=*/true);
15377 else if (decl
== error_mark_node
)
15378 type
= error_mark_node
;
15383 token
= cp_lexer_peek_token (parser
->lexer
);
15384 identifier
= cp_parser_identifier (parser
);
15386 if (identifier
== error_mark_node
)
15388 parser
->scope
= NULL_TREE
;
15389 return error_mark_node
;
15392 /* For a `typename', we needn't call xref_tag. */
15393 if (tag_type
== typename_type
15394 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15395 return cp_parser_make_typename_type (parser
, identifier
,
15398 /* Template parameter lists apply only if we are not within a
15399 function parameter list. */
15400 bool template_parm_lists_apply
15401 = parser
->num_template_parameter_lists
;
15402 if (template_parm_lists_apply
)
15403 for (cp_binding_level
*s
= current_binding_level
;
15404 s
&& s
->kind
!= sk_template_parms
;
15405 s
= s
->level_chain
)
15406 if (s
->kind
== sk_function_parms
)
15407 template_parm_lists_apply
= false;
15409 /* Look up a qualified name in the usual way. */
15413 tree ambiguous_decls
;
15415 decl
= cp_parser_lookup_name (parser
, identifier
,
15417 /*is_template=*/false,
15418 /*is_namespace=*/false,
15419 /*check_dependency=*/true,
15423 /* If the lookup was ambiguous, an error will already have been
15425 if (ambiguous_decls
)
15426 return error_mark_node
;
15428 /* If we are parsing friend declaration, DECL may be a
15429 TEMPLATE_DECL tree node here. However, we need to check
15430 whether this TEMPLATE_DECL results in valid code. Consider
15431 the following example:
15434 template <class T> class C {};
15437 template <class T> friend class N::C; // #1, valid code
15439 template <class T> class Y {
15440 friend class N::C; // #2, invalid code
15443 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15444 name lookup of `N::C'. We see that friend declaration must
15445 be template for the code to be valid. Note that
15446 processing_template_decl does not work here since it is
15447 always 1 for the above two cases. */
15449 decl
= (cp_parser_maybe_treat_template_as_class
15450 (decl
, /*tag_name_p=*/is_friend
15451 && template_parm_lists_apply
));
15453 if (TREE_CODE (decl
) != TYPE_DECL
)
15455 cp_parser_diagnose_invalid_type_name (parser
,
15458 return error_mark_node
;
15461 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15463 bool allow_template
= (template_parm_lists_apply
15464 || DECL_SELF_REFERENCE_P (decl
));
15465 type
= check_elaborated_type_specifier (tag_type
, decl
,
15468 if (type
== error_mark_node
)
15469 return error_mark_node
;
15472 /* Forward declarations of nested types, such as
15477 are invalid unless all components preceding the final '::'
15478 are complete. If all enclosing types are complete, these
15479 declarations become merely pointless.
15481 Invalid forward declarations of nested types are errors
15482 caught elsewhere in parsing. Those that are pointless arrive
15485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15486 && !is_friend
&& !processing_explicit_instantiation
)
15487 warning (0, "declaration %qD does not declare anything", decl
);
15489 type
= TREE_TYPE (decl
);
15493 /* An elaborated-type-specifier sometimes introduces a new type and
15494 sometimes names an existing type. Normally, the rule is that it
15495 introduces a new type only if there is not an existing type of
15496 the same name already in scope. For example, given:
15499 void f() { struct S s; }
15501 the `struct S' in the body of `f' is the same `struct S' as in
15502 the global scope; the existing definition is used. However, if
15503 there were no global declaration, this would introduce a new
15504 local class named `S'.
15506 An exception to this rule applies to the following code:
15508 namespace N { struct S; }
15510 Here, the elaborated-type-specifier names a new type
15511 unconditionally; even if there is already an `S' in the
15512 containing scope this declaration names a new type.
15513 This exception only applies if the elaborated-type-specifier
15514 forms the complete declaration:
15518 A declaration consisting solely of `class-key identifier ;' is
15519 either a redeclaration of the name in the current scope or a
15520 forward declaration of the identifier as a class name. It
15521 introduces the name into the current scope.
15523 We are in this situation precisely when the next token is a `;'.
15525 An exception to the exception is that a `friend' declaration does
15526 *not* name a new type; i.e., given:
15528 struct S { friend struct T; };
15530 `T' is not a new type in the scope of `S'.
15532 Also, `new struct S' or `sizeof (struct S)' never results in the
15533 definition of a new type; a new type can only be declared in a
15534 declaration context. */
15540 /* Friends have special name lookup rules. */
15541 ts
= ts_within_enclosing_non_class
;
15542 else if (is_declaration
15543 && cp_lexer_next_token_is (parser
->lexer
,
15545 /* This is a `class-key identifier ;' */
15551 (template_parm_lists_apply
15552 && (cp_parser_next_token_starts_class_definition_p (parser
)
15553 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15554 /* An unqualified name was used to reference this type, so
15555 there were no qualifying templates. */
15556 if (template_parm_lists_apply
15557 && !cp_parser_check_template_parameters (parser
,
15558 /*num_templates=*/0,
15560 /*declarator=*/NULL
))
15561 return error_mark_node
;
15562 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15566 if (type
== error_mark_node
)
15567 return error_mark_node
;
15569 /* Allow attributes on forward declarations of classes. */
15572 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15573 warning (OPT_Wattributes
,
15574 "attributes ignored on uninstantiated type");
15575 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15576 && ! processing_explicit_instantiation
)
15577 warning (OPT_Wattributes
,
15578 "attributes ignored on template instantiation");
15579 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15580 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15582 warning (OPT_Wattributes
,
15583 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15586 if (tag_type
!= enum_type
)
15588 /* Indicate whether this class was declared as a `class' or as a
15590 if (TREE_CODE (type
) == RECORD_TYPE
)
15591 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15592 cp_parser_check_class_key (tag_type
, type
);
15595 /* A "<" cannot follow an elaborated type specifier. If that
15596 happens, the user was probably trying to form a template-id. */
15597 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15603 /* Parse an enum-specifier.
15606 enum-head { enumerator-list [opt] }
15607 enum-head { enumerator-list , } [C++0x]
15610 enum-key identifier [opt] enum-base [opt]
15611 enum-key nested-name-specifier identifier enum-base [opt]
15616 enum struct [C++0x]
15619 : type-specifier-seq
15621 opaque-enum-specifier:
15622 enum-key identifier enum-base [opt] ;
15625 enum-key attributes[opt] identifier [opt] enum-base [opt]
15626 { enumerator-list [opt] }attributes[opt]
15627 enum-key attributes[opt] identifier [opt] enum-base [opt]
15628 { enumerator-list, }attributes[opt] [C++0x]
15630 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15631 if the token stream isn't an enum-specifier after all. */
15634 cp_parser_enum_specifier (cp_parser
* parser
)
15637 tree type
= NULL_TREE
;
15639 tree nested_name_specifier
= NULL_TREE
;
15641 bool scoped_enum_p
= false;
15642 bool has_underlying_type
= false;
15643 bool nested_being_defined
= false;
15644 bool new_value_list
= false;
15645 bool is_new_type
= false;
15646 bool is_anonymous
= false;
15647 tree underlying_type
= NULL_TREE
;
15648 cp_token
*type_start_token
= NULL
;
15649 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15651 parser
->colon_corrects_to_scope_p
= false;
15653 /* Parse tentatively so that we can back up if we don't find a
15655 cp_parser_parse_tentatively (parser
);
15657 /* Caller guarantees that the current token is 'enum', an identifier
15658 possibly follows, and the token after that is an opening brace.
15659 If we don't have an identifier, fabricate an anonymous name for
15660 the enumeration being defined. */
15661 cp_lexer_consume_token (parser
->lexer
);
15663 /* Parse the "class" or "struct", which indicates a scoped
15664 enumeration type in C++0x. */
15665 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15666 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15668 if (cxx_dialect
< cxx11
)
15669 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15671 /* Consume the `struct' or `class' token. */
15672 cp_lexer_consume_token (parser
->lexer
);
15674 scoped_enum_p
= true;
15677 attributes
= cp_parser_attributes_opt (parser
);
15679 /* Clear the qualification. */
15680 parser
->scope
= NULL_TREE
;
15681 parser
->qualifying_scope
= NULL_TREE
;
15682 parser
->object_scope
= NULL_TREE
;
15684 /* Figure out in what scope the declaration is being placed. */
15685 prev_scope
= current_scope ();
15687 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15689 push_deferring_access_checks (dk_no_check
);
15690 nested_name_specifier
15691 = cp_parser_nested_name_specifier_opt (parser
,
15692 /*typename_keyword_p=*/true,
15693 /*check_dependency_p=*/false,
15695 /*is_declaration=*/false);
15697 if (nested_name_specifier
)
15701 identifier
= cp_parser_identifier (parser
);
15702 name
= cp_parser_lookup_name (parser
, identifier
,
15704 /*is_template=*/false,
15705 /*is_namespace=*/false,
15706 /*check_dependency=*/true,
15707 /*ambiguous_decls=*/NULL
,
15709 if (name
&& name
!= error_mark_node
)
15711 type
= TREE_TYPE (name
);
15712 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15714 /* Are template enums allowed in ISO? */
15715 if (template_parm_scope_p ())
15716 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15717 "%qD is an enumeration template", name
);
15718 /* ignore a typename reference, for it will be solved by name
15723 else if (nested_name_specifier
== error_mark_node
)
15724 /* We already issued an error. */;
15726 error_at (type_start_token
->location
,
15727 "%qD is not an enumerator-name", identifier
);
15731 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15732 identifier
= cp_parser_identifier (parser
);
15735 identifier
= make_anon_name ();
15736 is_anonymous
= true;
15738 error_at (type_start_token
->location
,
15739 "anonymous scoped enum is not allowed");
15742 pop_deferring_access_checks ();
15744 /* Check for the `:' that denotes a specified underlying type in C++0x.
15745 Note that a ':' could also indicate a bitfield width, however. */
15746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15748 cp_decl_specifier_seq type_specifiers
;
15750 /* Consume the `:'. */
15751 cp_lexer_consume_token (parser
->lexer
);
15753 /* Parse the type-specifier-seq. */
15754 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15755 /*is_trailing_return=*/false,
15758 /* At this point this is surely not elaborated type specifier. */
15759 if (!cp_parser_parse_definitely (parser
))
15762 if (cxx_dialect
< cxx11
)
15763 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15765 has_underlying_type
= true;
15767 /* If that didn't work, stop. */
15768 if (type_specifiers
.type
!= error_mark_node
)
15770 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15771 /*initialized=*/0, NULL
);
15772 if (underlying_type
== error_mark_node
15773 || check_for_bare_parameter_packs (underlying_type
))
15774 underlying_type
= NULL_TREE
;
15778 /* Look for the `{' but don't consume it yet. */
15779 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15781 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15783 cp_parser_error (parser
, "expected %<{%>");
15784 if (has_underlying_type
)
15790 /* An opaque-enum-specifier must have a ';' here. */
15791 if ((scoped_enum_p
|| underlying_type
)
15792 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15794 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15795 if (has_underlying_type
)
15803 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15806 if (nested_name_specifier
)
15808 if (CLASS_TYPE_P (nested_name_specifier
))
15810 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15811 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15812 push_scope (nested_name_specifier
);
15814 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15816 push_nested_namespace (nested_name_specifier
);
15820 /* Issue an error message if type-definitions are forbidden here. */
15821 if (!cp_parser_check_type_definition (parser
))
15822 type
= error_mark_node
;
15824 /* Create the new type. We do this before consuming the opening
15825 brace so the enum will be recorded as being on the line of its
15826 tag (or the 'enum' keyword, if there is no tag). */
15827 type
= start_enum (identifier
, type
, underlying_type
,
15828 scoped_enum_p
, &is_new_type
);
15830 /* If the next token is not '{' it is an opaque-enum-specifier or an
15831 elaborated-type-specifier. */
15832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15834 timevar_push (TV_PARSE_ENUM
);
15835 if (nested_name_specifier
15836 && nested_name_specifier
!= error_mark_node
)
15838 /* The following catches invalid code such as:
15839 enum class S<int>::E { A, B, C }; */
15840 if (!processing_specialization
15841 && CLASS_TYPE_P (nested_name_specifier
)
15842 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15843 error_at (type_start_token
->location
, "cannot add an enumerator "
15844 "list to a template instantiation");
15846 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15848 error_at (type_start_token
->location
,
15849 "%<%T::%E%> has not been declared",
15850 TYPE_CONTEXT (nested_name_specifier
),
15851 nested_name_specifier
);
15852 type
= error_mark_node
;
15854 /* If that scope does not contain the scope in which the
15855 class was originally declared, the program is invalid. */
15856 else if (prev_scope
&& !is_ancestor (prev_scope
,
15857 nested_name_specifier
))
15859 if (at_namespace_scope_p ())
15860 error_at (type_start_token
->location
,
15861 "declaration of %qD in namespace %qD which does not "
15863 type
, prev_scope
, nested_name_specifier
);
15865 error_at (type_start_token
->location
,
15866 "declaration of %qD in %qD which does not "
15868 type
, prev_scope
, nested_name_specifier
);
15869 type
= error_mark_node
;
15874 begin_scope (sk_scoped_enum
, type
);
15876 /* Consume the opening brace. */
15877 cp_lexer_consume_token (parser
->lexer
);
15879 if (type
== error_mark_node
)
15880 ; /* Nothing to add */
15881 else if (OPAQUE_ENUM_P (type
)
15882 || (cxx_dialect
> cxx98
&& processing_specialization
))
15884 new_value_list
= true;
15885 SET_OPAQUE_ENUM_P (type
, false);
15886 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15890 error_at (type_start_token
->location
,
15891 "multiple definition of %q#T", type
);
15892 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15893 "previous definition here");
15894 type
= error_mark_node
;
15897 if (type
== error_mark_node
)
15898 cp_parser_skip_to_end_of_block_or_statement (parser
);
15899 /* If the next token is not '}', then there are some enumerators. */
15900 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15902 if (is_anonymous
&& !scoped_enum_p
)
15903 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15904 "ISO C++ forbids empty anonymous enum");
15907 cp_parser_enumerator_list (parser
, type
);
15909 /* Consume the final '}'. */
15910 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15914 timevar_pop (TV_PARSE_ENUM
);
15918 /* If a ';' follows, then it is an opaque-enum-specifier
15919 and additional restrictions apply. */
15920 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15923 error_at (type_start_token
->location
,
15924 "opaque-enum-specifier without name");
15925 else if (nested_name_specifier
)
15926 error_at (type_start_token
->location
,
15927 "opaque-enum-specifier must use a simple identifier");
15931 /* Look for trailing attributes to apply to this enumeration, and
15932 apply them if appropriate. */
15933 if (cp_parser_allow_gnu_extensions_p (parser
))
15935 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15936 trailing_attr
= chainon (trailing_attr
, attributes
);
15937 cplus_decl_attributes (&type
,
15939 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15942 /* Finish up the enumeration. */
15943 if (type
!= error_mark_node
)
15945 if (new_value_list
)
15946 finish_enum_value_list (type
);
15948 finish_enum (type
);
15951 if (nested_name_specifier
)
15953 if (CLASS_TYPE_P (nested_name_specifier
))
15955 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15956 pop_scope (nested_name_specifier
);
15958 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15960 pop_nested_namespace (nested_name_specifier
);
15964 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15968 /* Parse an enumerator-list. The enumerators all have the indicated
15972 enumerator-definition
15973 enumerator-list , enumerator-definition */
15976 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15980 /* Parse an enumerator-definition. */
15981 cp_parser_enumerator_definition (parser
, type
);
15983 /* If the next token is not a ',', we've reached the end of
15985 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15987 /* Otherwise, consume the `,' and keep going. */
15988 cp_lexer_consume_token (parser
->lexer
);
15989 /* If the next token is a `}', there is a trailing comma. */
15990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15992 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
15993 pedwarn (input_location
, OPT_Wpedantic
,
15994 "comma at end of enumerator list");
16000 /* Parse an enumerator-definition. The enumerator has the indicated
16003 enumerator-definition:
16005 enumerator = constant-expression
16011 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16017 /* Save the input location because we are interested in the location
16018 of the identifier and not the location of the explicit value. */
16019 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16021 /* Look for the identifier. */
16022 identifier
= cp_parser_identifier (parser
);
16023 if (identifier
== error_mark_node
)
16026 /* If the next token is an '=', then there is an explicit value. */
16027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16029 /* Consume the `=' token. */
16030 cp_lexer_consume_token (parser
->lexer
);
16031 /* Parse the value. */
16032 value
= cp_parser_constant_expression (parser
);
16037 /* If we are processing a template, make sure the initializer of the
16038 enumerator doesn't contain any bare template parameter pack. */
16039 if (check_for_bare_parameter_packs (value
))
16040 value
= error_mark_node
;
16042 /* Create the enumerator. */
16043 build_enumerator (identifier
, value
, type
, loc
);
16046 /* Parse a namespace-name.
16049 original-namespace-name
16052 Returns the NAMESPACE_DECL for the namespace. */
16055 cp_parser_namespace_name (cp_parser
* parser
)
16058 tree namespace_decl
;
16060 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16062 /* Get the name of the namespace. */
16063 identifier
= cp_parser_identifier (parser
);
16064 if (identifier
== error_mark_node
)
16065 return error_mark_node
;
16067 /* Look up the identifier in the currently active scope. Look only
16068 for namespaces, due to:
16070 [basic.lookup.udir]
16072 When looking up a namespace-name in a using-directive or alias
16073 definition, only namespace names are considered.
16077 [basic.lookup.qual]
16079 During the lookup of a name preceding the :: scope resolution
16080 operator, object, function, and enumerator names are ignored.
16082 (Note that cp_parser_qualifying_entity only calls this
16083 function if the token after the name is the scope resolution
16085 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16087 /*is_template=*/false,
16088 /*is_namespace=*/true,
16089 /*check_dependency=*/true,
16090 /*ambiguous_decls=*/NULL
,
16092 /* If it's not a namespace, issue an error. */
16093 if (namespace_decl
== error_mark_node
16094 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16096 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16097 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16098 cp_parser_error (parser
, "expected namespace-name");
16099 namespace_decl
= error_mark_node
;
16102 return namespace_decl
;
16105 /* Parse a namespace-definition.
16107 namespace-definition:
16108 named-namespace-definition
16109 unnamed-namespace-definition
16111 named-namespace-definition:
16112 original-namespace-definition
16113 extension-namespace-definition
16115 original-namespace-definition:
16116 namespace identifier { namespace-body }
16118 extension-namespace-definition:
16119 namespace original-namespace-name { namespace-body }
16121 unnamed-namespace-definition:
16122 namespace { namespace-body } */
16125 cp_parser_namespace_definition (cp_parser
* parser
)
16127 tree identifier
, attribs
;
16128 bool has_visibility
;
16131 cp_ensure_no_omp_declare_simd (parser
);
16132 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16134 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16136 cp_lexer_consume_token (parser
->lexer
);
16141 /* Look for the `namespace' keyword. */
16142 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16144 /* Get the name of the namespace. We do not attempt to distinguish
16145 between an original-namespace-definition and an
16146 extension-namespace-definition at this point. The semantic
16147 analysis routines are responsible for that. */
16148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16149 identifier
= cp_parser_identifier (parser
);
16151 identifier
= NULL_TREE
;
16153 /* Parse any specified attributes. */
16154 attribs
= cp_parser_attributes_opt (parser
);
16156 /* Look for the `{' to start the namespace. */
16157 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16158 /* Start the namespace. */
16159 push_namespace (identifier
);
16161 /* "inline namespace" is equivalent to a stub namespace definition
16162 followed by a strong using directive. */
16165 tree name_space
= current_namespace
;
16166 /* Set up namespace association. */
16167 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16168 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16169 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16170 /* Import the contents of the inline namespace. */
16172 do_using_directive (name_space
);
16173 push_namespace (identifier
);
16176 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16178 /* Parse the body of the namespace. */
16179 cp_parser_namespace_body (parser
);
16181 if (has_visibility
)
16182 pop_visibility (1);
16184 /* Finish the namespace. */
16186 /* Look for the final `}'. */
16187 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16190 /* Parse a namespace-body.
16193 declaration-seq [opt] */
16196 cp_parser_namespace_body (cp_parser
* parser
)
16198 cp_parser_declaration_seq_opt (parser
);
16201 /* Parse a namespace-alias-definition.
16203 namespace-alias-definition:
16204 namespace identifier = qualified-namespace-specifier ; */
16207 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16210 tree namespace_specifier
;
16212 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16214 /* Look for the `namespace' keyword. */
16215 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16216 /* Look for the identifier. */
16217 identifier
= cp_parser_identifier (parser
);
16218 if (identifier
== error_mark_node
)
16220 /* Look for the `=' token. */
16221 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16222 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16224 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16225 /* Skip the definition. */
16226 cp_lexer_consume_token (parser
->lexer
);
16227 if (cp_parser_skip_to_closing_brace (parser
))
16228 cp_lexer_consume_token (parser
->lexer
);
16231 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16232 /* Look for the qualified-namespace-specifier. */
16233 namespace_specifier
16234 = cp_parser_qualified_namespace_specifier (parser
);
16235 /* Look for the `;' token. */
16236 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16238 /* Register the alias in the symbol table. */
16239 do_namespace_alias (identifier
, namespace_specifier
);
16242 /* Parse a qualified-namespace-specifier.
16244 qualified-namespace-specifier:
16245 :: [opt] nested-name-specifier [opt] namespace-name
16247 Returns a NAMESPACE_DECL corresponding to the specified
16251 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16253 /* Look for the optional `::'. */
16254 cp_parser_global_scope_opt (parser
,
16255 /*current_scope_valid_p=*/false);
16257 /* Look for the optional nested-name-specifier. */
16258 cp_parser_nested_name_specifier_opt (parser
,
16259 /*typename_keyword_p=*/false,
16260 /*check_dependency_p=*/true,
16262 /*is_declaration=*/true);
16264 return cp_parser_namespace_name (parser
);
16267 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16268 access declaration.
16271 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16272 using :: unqualified-id ;
16274 access-declaration:
16280 cp_parser_using_declaration (cp_parser
* parser
,
16281 bool access_declaration_p
)
16284 bool typename_p
= false;
16285 bool global_scope_p
;
16289 int oldcount
= errorcount
;
16290 cp_token
*diag_token
= NULL
;
16292 if (access_declaration_p
)
16294 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16295 cp_parser_parse_tentatively (parser
);
16299 /* Look for the `using' keyword. */
16300 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16302 /* Peek at the next token. */
16303 token
= cp_lexer_peek_token (parser
->lexer
);
16304 /* See if it's `typename'. */
16305 if (token
->keyword
== RID_TYPENAME
)
16307 /* Remember that we've seen it. */
16309 /* Consume the `typename' token. */
16310 cp_lexer_consume_token (parser
->lexer
);
16314 /* Look for the optional global scope qualification. */
16316 = (cp_parser_global_scope_opt (parser
,
16317 /*current_scope_valid_p=*/false)
16320 /* If we saw `typename', or didn't see `::', then there must be a
16321 nested-name-specifier present. */
16322 if (typename_p
|| !global_scope_p
)
16324 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16325 /*check_dependency_p=*/true,
16327 /*is_declaration=*/true);
16328 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16330 cp_parser_skip_to_end_of_block_or_statement (parser
);
16334 /* Otherwise, we could be in either of the two productions. In that
16335 case, treat the nested-name-specifier as optional. */
16337 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16338 /*typename_keyword_p=*/false,
16339 /*check_dependency_p=*/true,
16341 /*is_declaration=*/true);
16343 qscope
= global_namespace
;
16344 else if (UNSCOPED_ENUM_P (qscope
))
16345 qscope
= CP_TYPE_CONTEXT (qscope
);
16347 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16348 /* Something has already gone wrong; there's no need to parse
16349 further. Since an error has occurred, the return value of
16350 cp_parser_parse_definitely will be false, as required. */
16351 return cp_parser_parse_definitely (parser
);
16353 token
= cp_lexer_peek_token (parser
->lexer
);
16354 /* Parse the unqualified-id. */
16355 identifier
= cp_parser_unqualified_id (parser
,
16356 /*template_keyword_p=*/false,
16357 /*check_dependency_p=*/true,
16358 /*declarator_p=*/true,
16359 /*optional_p=*/false);
16361 if (access_declaration_p
)
16363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16364 cp_parser_simulate_error (parser
);
16365 if (!cp_parser_parse_definitely (parser
))
16369 /* The function we call to handle a using-declaration is different
16370 depending on what scope we are in. */
16371 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16373 else if (!identifier_p (identifier
)
16374 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16375 /* [namespace.udecl]
16377 A using declaration shall not name a template-id. */
16378 error_at (token
->location
,
16379 "a template-id may not appear in a using-declaration");
16382 if (at_class_scope_p ())
16384 /* Create the USING_DECL. */
16385 decl
= do_class_using_decl (parser
->scope
, identifier
);
16387 if (decl
&& typename_p
)
16388 USING_DECL_TYPENAME_P (decl
) = 1;
16390 if (check_for_bare_parameter_packs (decl
))
16392 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16396 /* Add it to the list of members in this class. */
16397 finish_member_declaration (decl
);
16401 decl
= cp_parser_lookup_name_simple (parser
,
16404 if (decl
== error_mark_node
)
16405 cp_parser_name_lookup_error (parser
, identifier
,
16408 else if (check_for_bare_parameter_packs (decl
))
16410 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16413 else if (!at_namespace_scope_p ())
16414 do_local_using_decl (decl
, qscope
, identifier
);
16416 do_toplevel_using_decl (decl
, qscope
, identifier
);
16420 /* Look for the final `;'. */
16421 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16423 if (access_declaration_p
&& errorcount
== oldcount
)
16424 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16425 "access declarations are deprecated "
16426 "in favour of using-declarations; "
16427 "suggestion: add the %<using%> keyword");
16432 /* Parse an alias-declaration.
16435 using identifier attribute-specifier-seq [opt] = type-id */
16438 cp_parser_alias_declaration (cp_parser
* parser
)
16440 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16441 location_t id_location
;
16442 cp_declarator
*declarator
;
16443 cp_decl_specifier_seq decl_specs
;
16445 const char *saved_message
= NULL
;
16447 /* Look for the `using' keyword. */
16448 cp_token
*using_token
16449 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16450 if (using_token
== NULL
)
16451 return error_mark_node
;
16453 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16454 id
= cp_parser_identifier (parser
);
16455 if (id
== error_mark_node
)
16456 return error_mark_node
;
16458 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16459 attributes
= cp_parser_attributes_opt (parser
);
16460 if (attributes
== error_mark_node
)
16461 return error_mark_node
;
16463 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16465 if (cp_parser_error_occurred (parser
))
16466 return error_mark_node
;
16468 cp_parser_commit_to_tentative_parse (parser
);
16470 /* Now we are going to parse the type-id of the declaration. */
16475 "A type-specifier-seq shall not define a class or enumeration
16476 unless it appears in the type-id of an alias-declaration (7.1.3) that
16477 is not the declaration of a template-declaration."
16479 In other words, if we currently are in an alias template, the
16480 type-id should not define a type.
16482 So let's set parser->type_definition_forbidden_message in that
16483 case; cp_parser_check_type_definition (called by
16484 cp_parser_class_specifier) will then emit an error if a type is
16485 defined in the type-id. */
16486 if (parser
->num_template_parameter_lists
)
16488 saved_message
= parser
->type_definition_forbidden_message
;
16489 parser
->type_definition_forbidden_message
=
16490 G_("types may not be defined in alias template declarations");
16493 type
= cp_parser_type_id (parser
);
16495 /* Restore the error message if need be. */
16496 if (parser
->num_template_parameter_lists
)
16497 parser
->type_definition_forbidden_message
= saved_message
;
16499 if (type
== error_mark_node
16500 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16502 cp_parser_skip_to_end_of_block_or_statement (parser
);
16503 return error_mark_node
;
16506 /* A typedef-name can also be introduced by an alias-declaration. The
16507 identifier following the using keyword becomes a typedef-name. It has
16508 the same semantics as if it were introduced by the typedef
16509 specifier. In particular, it does not define a new type and it shall
16510 not appear in the type-id. */
16512 clear_decl_specs (&decl_specs
);
16513 decl_specs
.type
= type
;
16514 if (attributes
!= NULL_TREE
)
16516 decl_specs
.attributes
= attributes
;
16517 set_and_check_decl_spec_loc (&decl_specs
,
16521 set_and_check_decl_spec_loc (&decl_specs
,
16524 set_and_check_decl_spec_loc (&decl_specs
,
16528 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16529 declarator
->id_loc
= id_location
;
16531 member_p
= at_class_scope_p ();
16533 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16534 NULL_TREE
, attributes
);
16536 decl
= start_decl (declarator
, &decl_specs
, 0,
16537 attributes
, NULL_TREE
, &pushed_scope
);
16538 if (decl
== error_mark_node
)
16541 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16544 pop_scope (pushed_scope
);
16546 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16547 added into the symbol table; otherwise, return the TYPE_DECL. */
16548 if (DECL_LANG_SPECIFIC (decl
)
16549 && DECL_TEMPLATE_INFO (decl
)
16550 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16552 decl
= DECL_TI_TEMPLATE (decl
);
16554 check_member_template (decl
);
16560 /* Parse a using-directive.
16563 using namespace :: [opt] nested-name-specifier [opt]
16564 namespace-name ; */
16567 cp_parser_using_directive (cp_parser
* parser
)
16569 tree namespace_decl
;
16572 /* Look for the `using' keyword. */
16573 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16574 /* And the `namespace' keyword. */
16575 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16576 /* Look for the optional `::' operator. */
16577 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16578 /* And the optional nested-name-specifier. */
16579 cp_parser_nested_name_specifier_opt (parser
,
16580 /*typename_keyword_p=*/false,
16581 /*check_dependency_p=*/true,
16583 /*is_declaration=*/true);
16584 /* Get the namespace being used. */
16585 namespace_decl
= cp_parser_namespace_name (parser
);
16586 /* And any specified attributes. */
16587 attribs
= cp_parser_attributes_opt (parser
);
16588 /* Update the symbol table. */
16589 parse_using_directive (namespace_decl
, attribs
);
16590 /* Look for the final `;'. */
16591 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16594 /* Parse an asm-definition.
16597 asm ( string-literal ) ;
16602 asm volatile [opt] ( string-literal ) ;
16603 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16604 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16605 : asm-operand-list [opt] ) ;
16606 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16607 : asm-operand-list [opt]
16608 : asm-clobber-list [opt] ) ;
16609 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16610 : asm-clobber-list [opt]
16611 : asm-goto-list ) ; */
16614 cp_parser_asm_definition (cp_parser
* parser
)
16617 tree outputs
= NULL_TREE
;
16618 tree inputs
= NULL_TREE
;
16619 tree clobbers
= NULL_TREE
;
16620 tree labels
= NULL_TREE
;
16622 bool volatile_p
= false;
16623 bool extended_p
= false;
16624 bool invalid_inputs_p
= false;
16625 bool invalid_outputs_p
= false;
16626 bool goto_p
= false;
16627 required_token missing
= RT_NONE
;
16629 /* Look for the `asm' keyword. */
16630 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16632 if (parser
->in_function_body
16633 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16635 error ("%<asm%> in %<constexpr%> function");
16636 cp_function_chain
->invalid_constexpr
= true;
16639 /* See if the next token is `volatile'. */
16640 if (cp_parser_allow_gnu_extensions_p (parser
)
16641 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16643 /* Remember that we saw the `volatile' keyword. */
16645 /* Consume the token. */
16646 cp_lexer_consume_token (parser
->lexer
);
16648 if (cp_parser_allow_gnu_extensions_p (parser
)
16649 && parser
->in_function_body
16650 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16652 /* Remember that we saw the `goto' keyword. */
16654 /* Consume the token. */
16655 cp_lexer_consume_token (parser
->lexer
);
16657 /* Look for the opening `('. */
16658 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16660 /* Look for the string. */
16661 string
= cp_parser_string_literal (parser
, false, false);
16662 if (string
== error_mark_node
)
16664 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16665 /*consume_paren=*/true);
16669 /* If we're allowing GNU extensions, check for the extended assembly
16670 syntax. Unfortunately, the `:' tokens need not be separated by
16671 a space in C, and so, for compatibility, we tolerate that here
16672 too. Doing that means that we have to treat the `::' operator as
16674 if (cp_parser_allow_gnu_extensions_p (parser
)
16675 && parser
->in_function_body
16676 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16677 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16679 bool inputs_p
= false;
16680 bool clobbers_p
= false;
16681 bool labels_p
= false;
16683 /* The extended syntax was used. */
16686 /* Look for outputs. */
16687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16689 /* Consume the `:'. */
16690 cp_lexer_consume_token (parser
->lexer
);
16691 /* Parse the output-operands. */
16692 if (cp_lexer_next_token_is_not (parser
->lexer
,
16694 && cp_lexer_next_token_is_not (parser
->lexer
,
16696 && cp_lexer_next_token_is_not (parser
->lexer
,
16699 outputs
= cp_parser_asm_operand_list (parser
);
16701 if (outputs
== error_mark_node
)
16702 invalid_outputs_p
= true;
16704 /* If the next token is `::', there are no outputs, and the
16705 next token is the beginning of the inputs. */
16706 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16707 /* The inputs are coming next. */
16710 /* Look for inputs. */
16712 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16714 /* Consume the `:' or `::'. */
16715 cp_lexer_consume_token (parser
->lexer
);
16716 /* Parse the output-operands. */
16717 if (cp_lexer_next_token_is_not (parser
->lexer
,
16719 && cp_lexer_next_token_is_not (parser
->lexer
,
16721 && cp_lexer_next_token_is_not (parser
->lexer
,
16723 inputs
= cp_parser_asm_operand_list (parser
);
16725 if (inputs
== error_mark_node
)
16726 invalid_inputs_p
= true;
16728 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16729 /* The clobbers are coming next. */
16732 /* Look for clobbers. */
16734 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16737 /* Consume the `:' or `::'. */
16738 cp_lexer_consume_token (parser
->lexer
);
16739 /* Parse the clobbers. */
16740 if (cp_lexer_next_token_is_not (parser
->lexer
,
16742 && cp_lexer_next_token_is_not (parser
->lexer
,
16744 clobbers
= cp_parser_asm_clobber_list (parser
);
16747 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16748 /* The labels are coming next. */
16751 /* Look for labels. */
16753 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16756 /* Consume the `:' or `::'. */
16757 cp_lexer_consume_token (parser
->lexer
);
16758 /* Parse the labels. */
16759 labels
= cp_parser_asm_label_list (parser
);
16762 if (goto_p
&& !labels_p
)
16763 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16766 missing
= RT_COLON_SCOPE
;
16768 /* Look for the closing `)'. */
16769 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16770 missing
? missing
: RT_CLOSE_PAREN
))
16771 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16772 /*consume_paren=*/true);
16773 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16775 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16777 /* Create the ASM_EXPR. */
16778 if (parser
->in_function_body
)
16780 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16781 inputs
, clobbers
, labels
);
16782 /* If the extended syntax was not used, mark the ASM_EXPR. */
16785 tree temp
= asm_stmt
;
16786 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16787 temp
= TREE_OPERAND (temp
, 0);
16789 ASM_INPUT_P (temp
) = 1;
16793 symtab
->finalize_toplevel_asm (string
);
16797 /* Declarators [gram.dcl.decl] */
16799 /* Parse an init-declarator.
16802 declarator initializer [opt]
16807 declarator asm-specification [opt] attributes [opt] initializer [opt]
16809 function-definition:
16810 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16812 decl-specifier-seq [opt] declarator function-try-block
16816 function-definition:
16817 __extension__ function-definition
16821 function-definition:
16822 decl-specifier-seq [opt] declarator function-transaction-block
16824 The DECL_SPECIFIERS apply to this declarator. Returns a
16825 representation of the entity declared. If MEMBER_P is TRUE, then
16826 this declarator appears in a class scope. The new DECL created by
16827 this declarator is returned.
16829 The CHECKS are access checks that should be performed once we know
16830 what entity is being declared (and, therefore, what classes have
16833 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16834 for a function-definition here as well. If the declarator is a
16835 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16836 be TRUE upon return. By that point, the function-definition will
16837 have been completely parsed.
16839 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16842 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16843 parsed declaration if it is an uninitialized single declarator not followed
16844 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16845 if present, will not be consumed. If returned, this declarator will be
16846 created with SD_INITIALIZED but will not call cp_finish_decl. */
16849 cp_parser_init_declarator (cp_parser
* parser
,
16850 cp_decl_specifier_seq
*decl_specifiers
,
16851 vec
<deferred_access_check
, va_gc
> *checks
,
16852 bool function_definition_allowed_p
,
16854 int declares_class_or_enum
,
16855 bool* function_definition_p
,
16856 tree
* maybe_range_for_decl
)
16858 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16859 *attributes_start_token
= NULL
;
16860 cp_declarator
*declarator
;
16861 tree prefix_attributes
;
16862 tree attributes
= NULL
;
16863 tree asm_specification
;
16865 tree decl
= NULL_TREE
;
16867 int is_initialized
;
16868 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16869 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16871 enum cpp_ttype initialization_kind
;
16872 bool is_direct_init
= false;
16873 bool is_non_constant_init
;
16874 int ctor_dtor_or_conv_p
;
16875 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16876 tree pushed_scope
= NULL_TREE
;
16877 bool range_for_decl_p
= false;
16878 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16880 /* Gather the attributes that were provided with the
16881 decl-specifiers. */
16882 prefix_attributes
= decl_specifiers
->attributes
;
16884 /* Assume that this is not the declarator for a function
16886 if (function_definition_p
)
16887 *function_definition_p
= false;
16889 /* Default arguments are only permitted for function parameters. */
16890 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16891 parser
->default_arg_ok_p
= false;
16893 /* Defer access checks while parsing the declarator; we cannot know
16894 what names are accessible until we know what is being
16896 resume_deferring_access_checks ();
16898 /* Parse the declarator. */
16899 token
= cp_lexer_peek_token (parser
->lexer
);
16901 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16902 &ctor_dtor_or_conv_p
,
16903 /*parenthesized_p=*/NULL
,
16904 member_p
, friend_p
);
16905 /* Gather up the deferred checks. */
16906 stop_deferring_access_checks ();
16908 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16910 /* If the DECLARATOR was erroneous, there's no need to go
16912 if (declarator
== cp_error_declarator
)
16913 return error_mark_node
;
16915 /* Check that the number of template-parameter-lists is OK. */
16916 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16918 return error_mark_node
;
16920 if (declares_class_or_enum
& 2)
16921 cp_parser_check_for_definition_in_return_type (declarator
,
16922 decl_specifiers
->type
,
16923 decl_specifiers
->locations
[ds_type_spec
]);
16925 /* Figure out what scope the entity declared by the DECLARATOR is
16926 located in. `grokdeclarator' sometimes changes the scope, so
16927 we compute it now. */
16928 scope
= get_scope_of_declarator (declarator
);
16930 /* Perform any lookups in the declared type which were thought to be
16931 dependent, but are not in the scope of the declarator. */
16932 decl_specifiers
->type
16933 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16935 /* If we're allowing GNU extensions, look for an
16936 asm-specification. */
16937 if (cp_parser_allow_gnu_extensions_p (parser
))
16939 /* Look for an asm-specification. */
16940 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16941 asm_specification
= cp_parser_asm_specification_opt (parser
);
16944 asm_specification
= NULL_TREE
;
16946 /* Look for attributes. */
16947 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16948 attributes
= cp_parser_attributes_opt (parser
);
16950 /* Peek at the next token. */
16951 token
= cp_lexer_peek_token (parser
->lexer
);
16953 bool bogus_implicit_tmpl
= false;
16955 if (function_declarator_p (declarator
))
16957 /* Check to see if the token indicates the start of a
16958 function-definition. */
16959 if (cp_parser_token_starts_function_definition_p (token
))
16961 if (!function_definition_allowed_p
)
16963 /* If a function-definition should not appear here, issue an
16965 cp_parser_error (parser
,
16966 "a function-definition is not allowed here");
16967 return error_mark_node
;
16970 location_t func_brace_location
16971 = cp_lexer_peek_token (parser
->lexer
)->location
;
16973 /* Neither attributes nor an asm-specification are allowed
16974 on a function-definition. */
16975 if (asm_specification
)
16976 error_at (asm_spec_start_token
->location
,
16977 "an asm-specification is not allowed "
16978 "on a function-definition");
16980 error_at (attributes_start_token
->location
,
16981 "attributes are not allowed "
16982 "on a function-definition");
16983 /* This is a function-definition. */
16984 *function_definition_p
= true;
16986 /* Parse the function definition. */
16988 decl
= cp_parser_save_member_function_body (parser
,
16991 prefix_attributes
);
16994 (cp_parser_function_definition_from_specifiers_and_declarator
16995 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16997 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16999 /* This is where the prologue starts... */
17000 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
17001 = func_brace_location
;
17007 else if (parser
->fully_implicit_function_template_p
)
17009 /* A non-template declaration involving a function parameter list
17010 containing an implicit template parameter will be made into a
17011 template. If the resulting declaration is not going to be an
17012 actual function then finish the template scope here to prevent it.
17013 An error message will be issued once we have a decl to talk about.
17015 FIXME probably we should do type deduction rather than create an
17016 implicit template, but the standard currently doesn't allow it. */
17017 bogus_implicit_tmpl
= true;
17018 finish_fully_implicit_template (parser
, NULL_TREE
);
17023 Only in function declarations for constructors, destructors, and
17024 type conversions can the decl-specifier-seq be omitted.
17026 We explicitly postpone this check past the point where we handle
17027 function-definitions because we tolerate function-definitions
17028 that are missing their return types in some modes. */
17029 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
17031 cp_parser_error (parser
,
17032 "expected constructor, destructor, or type conversion");
17033 return error_mark_node
;
17036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17037 if (token
->type
== CPP_EQ
17038 || token
->type
== CPP_OPEN_PAREN
17039 || token
->type
== CPP_OPEN_BRACE
)
17041 is_initialized
= SD_INITIALIZED
;
17042 initialization_kind
= token
->type
;
17043 if (maybe_range_for_decl
)
17044 *maybe_range_for_decl
= error_mark_node
;
17046 if (token
->type
== CPP_EQ
17047 && function_declarator_p (declarator
))
17049 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17050 if (t2
->keyword
== RID_DEFAULT
)
17051 is_initialized
= SD_DEFAULTED
;
17052 else if (t2
->keyword
== RID_DELETE
)
17053 is_initialized
= SD_DELETED
;
17058 /* If the init-declarator isn't initialized and isn't followed by a
17059 `,' or `;', it's not a valid init-declarator. */
17060 if (token
->type
!= CPP_COMMA
17061 && token
->type
!= CPP_SEMICOLON
)
17063 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17064 range_for_decl_p
= true;
17067 cp_parser_error (parser
, "expected initializer");
17068 return error_mark_node
;
17071 is_initialized
= SD_UNINITIALIZED
;
17072 initialization_kind
= CPP_EOF
;
17075 /* Because start_decl has side-effects, we should only call it if we
17076 know we're going ahead. By this point, we know that we cannot
17077 possibly be looking at any other construct. */
17078 cp_parser_commit_to_tentative_parse (parser
);
17080 /* Enter the newly declared entry in the symbol table. If we're
17081 processing a declaration in a class-specifier, we wait until
17082 after processing the initializer. */
17085 if (parser
->in_unbraced_linkage_specification_p
)
17086 decl_specifiers
->storage_class
= sc_extern
;
17087 decl
= start_decl (declarator
, decl_specifiers
,
17088 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17089 attributes
, prefix_attributes
, &pushed_scope
);
17090 cp_finalize_omp_declare_simd (parser
, decl
);
17091 /* Adjust location of decl if declarator->id_loc is more appropriate:
17092 set, and decl wasn't merged with another decl, in which case its
17093 location would be different from input_location, and more accurate. */
17095 && declarator
->id_loc
!= UNKNOWN_LOCATION
17096 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17097 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17100 /* Enter the SCOPE. That way unqualified names appearing in the
17101 initializer will be looked up in SCOPE. */
17102 pushed_scope
= push_scope (scope
);
17104 /* Perform deferred access control checks, now that we know in which
17105 SCOPE the declared entity resides. */
17106 if (!member_p
&& decl
)
17108 tree saved_current_function_decl
= NULL_TREE
;
17110 /* If the entity being declared is a function, pretend that we
17111 are in its scope. If it is a `friend', it may have access to
17112 things that would not otherwise be accessible. */
17113 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17115 saved_current_function_decl
= current_function_decl
;
17116 current_function_decl
= decl
;
17119 /* Perform access checks for template parameters. */
17120 cp_parser_perform_template_parameter_access_checks (checks
);
17122 /* Perform the access control checks for the declarator and the
17123 decl-specifiers. */
17124 perform_deferred_access_checks (tf_warning_or_error
);
17126 /* Restore the saved value. */
17127 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17128 current_function_decl
= saved_current_function_decl
;
17131 /* Parse the initializer. */
17132 initializer
= NULL_TREE
;
17133 is_direct_init
= false;
17134 is_non_constant_init
= true;
17135 if (is_initialized
)
17137 if (function_declarator_p (declarator
))
17139 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
17140 if (initialization_kind
== CPP_EQ
)
17141 initializer
= cp_parser_pure_specifier (parser
);
17144 /* If the declaration was erroneous, we don't really
17145 know what the user intended, so just silently
17146 consume the initializer. */
17147 if (decl
!= error_mark_node
)
17148 error_at (initializer_start_token
->location
,
17149 "initializer provided for function");
17150 cp_parser_skip_to_closing_parenthesis (parser
,
17151 /*recovering=*/true,
17152 /*or_comma=*/false,
17153 /*consume_paren=*/true);
17158 /* We want to record the extra mangling scope for in-class
17159 initializers of class members and initializers of static data
17160 member templates. The former involves deferring
17161 parsing of the initializer until end of class as with default
17162 arguments. So right here we only handle the latter. */
17163 if (!member_p
&& processing_template_decl
)
17164 start_lambda_scope (decl
);
17165 initializer
= cp_parser_initializer (parser
,
17167 &is_non_constant_init
);
17168 if (!member_p
&& processing_template_decl
)
17169 finish_lambda_scope ();
17170 if (initializer
== error_mark_node
)
17171 cp_parser_skip_to_end_of_statement (parser
);
17175 /* The old parser allows attributes to appear after a parenthesized
17176 initializer. Mark Mitchell proposed removing this functionality
17177 on the GCC mailing lists on 2002-08-13. This parser accepts the
17178 attributes -- but ignores them. */
17179 if (cp_parser_allow_gnu_extensions_p (parser
)
17180 && initialization_kind
== CPP_OPEN_PAREN
)
17181 if (cp_parser_attributes_opt (parser
))
17182 warning (OPT_Wattributes
,
17183 "attributes after parenthesized initializer ignored");
17185 /* And now complain about a non-function implicit template. */
17186 if (bogus_implicit_tmpl
)
17187 error_at (DECL_SOURCE_LOCATION (decl
),
17188 "non-function %qD declared as implicit template", decl
);
17190 /* For an in-class declaration, use `grokfield' to create the
17196 pop_scope (pushed_scope
);
17197 pushed_scope
= NULL_TREE
;
17199 decl
= grokfield (declarator
, decl_specifiers
,
17200 initializer
, !is_non_constant_init
,
17201 /*asmspec=*/NULL_TREE
,
17202 chainon (attributes
, prefix_attributes
));
17203 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17204 cp_parser_save_default_args (parser
, decl
);
17205 cp_finalize_omp_declare_simd (parser
, decl
);
17208 /* Finish processing the declaration. But, skip member
17210 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17212 cp_finish_decl (decl
,
17213 initializer
, !is_non_constant_init
,
17215 /* If the initializer is in parentheses, then this is
17216 a direct-initialization, which means that an
17217 `explicit' constructor is OK. Otherwise, an
17218 `explicit' constructor cannot be used. */
17219 ((is_direct_init
|| !is_initialized
)
17220 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17222 else if ((cxx_dialect
!= cxx98
) && friend_p
17223 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17224 /* Core issue #226 (C++0x only): A default template-argument
17225 shall not be specified in a friend class template
17227 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17228 /*is_partial=*/false, /*is_friend_decl=*/1);
17230 if (!friend_p
&& pushed_scope
)
17231 pop_scope (pushed_scope
);
17233 if (function_declarator_p (declarator
)
17234 && parser
->fully_implicit_function_template_p
)
17237 decl
= finish_fully_implicit_template (parser
, decl
);
17239 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17245 /* Parse a declarator.
17249 ptr-operator declarator
17251 abstract-declarator:
17252 ptr-operator abstract-declarator [opt]
17253 direct-abstract-declarator
17258 attributes [opt] direct-declarator
17259 attributes [opt] ptr-operator declarator
17261 abstract-declarator:
17262 attributes [opt] ptr-operator abstract-declarator [opt]
17263 attributes [opt] direct-abstract-declarator
17265 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17266 detect constructor, destructor or conversion operators. It is set
17267 to -1 if the declarator is a name, and +1 if it is a
17268 function. Otherwise it is set to zero. Usually you just want to
17269 test for >0, but internally the negative value is used.
17271 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17272 a decl-specifier-seq unless it declares a constructor, destructor,
17273 or conversion. It might seem that we could check this condition in
17274 semantic analysis, rather than parsing, but that makes it difficult
17275 to handle something like `f()'. We want to notice that there are
17276 no decl-specifiers, and therefore realize that this is an
17277 expression, not a declaration.)
17279 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17280 the declarator is a direct-declarator of the form "(...)".
17282 MEMBER_P is true iff this declarator is a member-declarator.
17284 FRIEND_P is true iff this declarator is a friend. */
17286 static cp_declarator
*
17287 cp_parser_declarator (cp_parser
* parser
,
17288 cp_parser_declarator_kind dcl_kind
,
17289 int* ctor_dtor_or_conv_p
,
17290 bool* parenthesized_p
,
17291 bool member_p
, bool friend_p
)
17293 cp_declarator
*declarator
;
17294 enum tree_code code
;
17295 cp_cv_quals cv_quals
;
17297 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17299 /* Assume this is not a constructor, destructor, or type-conversion
17301 if (ctor_dtor_or_conv_p
)
17302 *ctor_dtor_or_conv_p
= 0;
17304 if (cp_parser_allow_gnu_extensions_p (parser
))
17305 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17307 /* Check for the ptr-operator production. */
17308 cp_parser_parse_tentatively (parser
);
17309 /* Parse the ptr-operator. */
17310 code
= cp_parser_ptr_operator (parser
,
17315 /* If that worked, then we have a ptr-operator. */
17316 if (cp_parser_parse_definitely (parser
))
17318 /* If a ptr-operator was found, then this declarator was not
17320 if (parenthesized_p
)
17321 *parenthesized_p
= true;
17322 /* The dependent declarator is optional if we are parsing an
17323 abstract-declarator. */
17324 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17325 cp_parser_parse_tentatively (parser
);
17327 /* Parse the dependent declarator. */
17328 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17329 /*ctor_dtor_or_conv_p=*/NULL
,
17330 /*parenthesized_p=*/NULL
,
17331 /*member_p=*/false,
17334 /* If we are parsing an abstract-declarator, we must handle the
17335 case where the dependent declarator is absent. */
17336 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17337 && !cp_parser_parse_definitely (parser
))
17340 declarator
= cp_parser_make_indirect_declarator
17341 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17343 /* Everything else is a direct-declarator. */
17346 if (parenthesized_p
)
17347 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17349 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17350 ctor_dtor_or_conv_p
,
17351 member_p
, friend_p
);
17354 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17355 declarator
->attributes
= gnu_attributes
;
17359 /* Parse a direct-declarator or direct-abstract-declarator.
17363 direct-declarator ( parameter-declaration-clause )
17364 cv-qualifier-seq [opt]
17365 ref-qualifier [opt]
17366 exception-specification [opt]
17367 direct-declarator [ constant-expression [opt] ]
17370 direct-abstract-declarator:
17371 direct-abstract-declarator [opt]
17372 ( parameter-declaration-clause )
17373 cv-qualifier-seq [opt]
17374 ref-qualifier [opt]
17375 exception-specification [opt]
17376 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17377 ( abstract-declarator )
17379 Returns a representation of the declarator. DCL_KIND is
17380 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17381 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17382 we are parsing a direct-declarator. It is
17383 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17384 of ambiguity we prefer an abstract declarator, as per
17385 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17386 as for cp_parser_declarator. */
17388 static cp_declarator
*
17389 cp_parser_direct_declarator (cp_parser
* parser
,
17390 cp_parser_declarator_kind dcl_kind
,
17391 int* ctor_dtor_or_conv_p
,
17392 bool member_p
, bool friend_p
)
17395 cp_declarator
*declarator
= NULL
;
17396 tree scope
= NULL_TREE
;
17397 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17398 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17400 tree pushed_scope
= NULL_TREE
;
17404 /* Peek at the next token. */
17405 token
= cp_lexer_peek_token (parser
->lexer
);
17406 if (token
->type
== CPP_OPEN_PAREN
)
17408 /* This is either a parameter-declaration-clause, or a
17409 parenthesized declarator. When we know we are parsing a
17410 named declarator, it must be a parenthesized declarator
17411 if FIRST is true. For instance, `(int)' is a
17412 parameter-declaration-clause, with an omitted
17413 direct-abstract-declarator. But `((*))', is a
17414 parenthesized abstract declarator. Finally, when T is a
17415 template parameter `(T)' is a
17416 parameter-declaration-clause, and not a parenthesized
17419 We first try and parse a parameter-declaration-clause,
17420 and then try a nested declarator (if FIRST is true).
17422 It is not an error for it not to be a
17423 parameter-declaration-clause, even when FIRST is
17429 The first is the declaration of a function while the
17430 second is the definition of a variable, including its
17433 Having seen only the parenthesis, we cannot know which of
17434 these two alternatives should be selected. Even more
17435 complex are examples like:
17440 The former is a function-declaration; the latter is a
17441 variable initialization.
17443 Thus again, we try a parameter-declaration-clause, and if
17444 that fails, we back out and return. */
17446 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17449 bool is_declarator
= false;
17451 /* In a member-declarator, the only valid interpretation
17452 of a parenthesis is the start of a
17453 parameter-declaration-clause. (It is invalid to
17454 initialize a static data member with a parenthesized
17455 initializer; only the "=" form of initialization is
17458 cp_parser_parse_tentatively (parser
);
17460 /* Consume the `('. */
17461 cp_lexer_consume_token (parser
->lexer
);
17464 /* If this is going to be an abstract declarator, we're
17465 in a declarator and we can't have default args. */
17466 parser
->default_arg_ok_p
= false;
17467 parser
->in_declarator_p
= true;
17470 begin_scope (sk_function_parms
, NULL_TREE
);
17472 /* Parse the parameter-declaration-clause. */
17473 params
= cp_parser_parameter_declaration_clause (parser
);
17475 /* Consume the `)'. */
17476 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17478 /* If all went well, parse the cv-qualifier-seq,
17479 ref-qualifier and the exception-specification. */
17480 if (member_p
|| cp_parser_parse_definitely (parser
))
17482 cp_cv_quals cv_quals
;
17483 cp_virt_specifiers virt_specifiers
;
17484 cp_ref_qualifier ref_qual
;
17485 tree exception_specification
;
17488 bool memfn
= (member_p
|| (pushed_scope
17489 && CLASS_TYPE_P (pushed_scope
)));
17491 is_declarator
= true;
17493 if (ctor_dtor_or_conv_p
)
17494 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17497 /* Parse the cv-qualifier-seq. */
17498 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17499 /* Parse the ref-qualifier. */
17500 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17501 /* And the exception-specification. */
17502 exception_specification
17503 = cp_parser_exception_specification_opt (parser
);
17505 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17507 /* In here, we handle cases where attribute is used after
17508 the function declaration. For example:
17509 void func (int x) __attribute__((vector(..))); */
17511 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17513 cp_parser_parse_tentatively (parser
);
17514 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17515 if (cp_lexer_next_token_is_not (parser
->lexer
,
17517 && cp_lexer_next_token_is_not (parser
->lexer
,
17519 cp_parser_abort_tentative_parse (parser
);
17520 else if (!cp_parser_parse_definitely (parser
))
17523 attrs
= chainon (attr
, attrs
);
17525 late_return
= (cp_parser_late_return_type_opt
17526 (parser
, declarator
,
17527 memfn
? cv_quals
: -1));
17530 /* Parse the virt-specifier-seq. */
17531 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17533 /* Create the function-declarator. */
17534 declarator
= make_call_declarator (declarator
,
17539 exception_specification
,
17541 declarator
->std_attributes
= attrs
;
17542 /* Any subsequent parameter lists are to do with
17543 return type, so are not those of the declared
17545 parser
->default_arg_ok_p
= false;
17548 /* Remove the function parms from scope. */
17549 pop_bindings_and_leave_scope ();
17552 /* Repeat the main loop. */
17556 /* If this is the first, we can try a parenthesized
17560 bool saved_in_type_id_in_expr_p
;
17562 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17563 parser
->in_declarator_p
= saved_in_declarator_p
;
17565 /* Consume the `('. */
17566 cp_lexer_consume_token (parser
->lexer
);
17567 /* Parse the nested declarator. */
17568 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17569 parser
->in_type_id_in_expr_p
= true;
17571 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17572 /*parenthesized_p=*/NULL
,
17573 member_p
, friend_p
);
17574 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17576 /* Expect a `)'. */
17577 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17578 declarator
= cp_error_declarator
;
17579 if (declarator
== cp_error_declarator
)
17582 goto handle_declarator
;
17584 /* Otherwise, we must be done. */
17588 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17589 && token
->type
== CPP_OPEN_SQUARE
17590 && !cp_next_tokens_can_be_attribute_p (parser
))
17592 /* Parse an array-declarator. */
17593 tree bounds
, attrs
;
17595 if (ctor_dtor_or_conv_p
)
17596 *ctor_dtor_or_conv_p
= 0;
17599 parser
->default_arg_ok_p
= false;
17600 parser
->in_declarator_p
= true;
17601 /* Consume the `['. */
17602 cp_lexer_consume_token (parser
->lexer
);
17603 /* Peek at the next token. */
17604 token
= cp_lexer_peek_token (parser
->lexer
);
17605 /* If the next token is `]', then there is no
17606 constant-expression. */
17607 if (token
->type
!= CPP_CLOSE_SQUARE
)
17609 bool non_constant_p
;
17611 = cp_parser_constant_expression (parser
,
17612 /*allow_non_constant=*/true,
17614 if (!non_constant_p
)
17616 else if (error_operand_p (bounds
))
17617 /* Already gave an error. */;
17618 else if (!parser
->in_function_body
17619 || current_binding_level
->kind
== sk_function_parms
)
17621 /* Normally, the array bound must be an integral constant
17622 expression. However, as an extension, we allow VLAs
17623 in function scopes as long as they aren't part of a
17624 parameter declaration. */
17625 cp_parser_error (parser
,
17626 "array bound is not an integer constant");
17627 bounds
= error_mark_node
;
17629 else if (processing_template_decl
17630 && !type_dependent_expression_p (bounds
))
17632 /* Remember this wasn't a constant-expression. */
17633 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17634 TREE_SIDE_EFFECTS (bounds
) = 1;
17638 bounds
= NULL_TREE
;
17639 /* Look for the closing `]'. */
17640 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17642 declarator
= cp_error_declarator
;
17646 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17647 declarator
= make_array_declarator (declarator
, bounds
);
17648 declarator
->std_attributes
= attrs
;
17650 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17653 tree qualifying_scope
;
17654 tree unqualified_name
;
17656 special_function_kind sfk
;
17658 bool pack_expansion_p
= false;
17659 cp_token
*declarator_id_start_token
;
17661 /* Parse a declarator-id */
17662 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17665 cp_parser_parse_tentatively (parser
);
17667 /* If we see an ellipsis, we should be looking at a
17669 if (token
->type
== CPP_ELLIPSIS
)
17671 /* Consume the `...' */
17672 cp_lexer_consume_token (parser
->lexer
);
17674 pack_expansion_p
= true;
17678 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17680 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17681 qualifying_scope
= parser
->scope
;
17686 if (!unqualified_name
&& pack_expansion_p
)
17688 /* Check whether an error occurred. */
17689 okay
= !cp_parser_error_occurred (parser
);
17691 /* We already consumed the ellipsis to mark a
17692 parameter pack, but we have no way to report it,
17693 so abort the tentative parse. We will be exiting
17694 immediately anyway. */
17695 cp_parser_abort_tentative_parse (parser
);
17698 okay
= cp_parser_parse_definitely (parser
);
17701 unqualified_name
= error_mark_node
;
17702 else if (unqualified_name
17703 && (qualifying_scope
17704 || (!identifier_p (unqualified_name
))))
17706 cp_parser_error (parser
, "expected unqualified-id");
17707 unqualified_name
= error_mark_node
;
17711 if (!unqualified_name
)
17713 if (unqualified_name
== error_mark_node
)
17715 declarator
= cp_error_declarator
;
17716 pack_expansion_p
= false;
17717 declarator
->parameter_pack_p
= false;
17721 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17723 if (qualifying_scope
&& at_namespace_scope_p ()
17724 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17726 /* In the declaration of a member of a template class
17727 outside of the class itself, the SCOPE will sometimes
17728 be a TYPENAME_TYPE. For example, given:
17730 template <typename T>
17731 int S<T>::R::i = 3;
17733 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17734 this context, we must resolve S<T>::R to an ordinary
17735 type, rather than a typename type.
17737 The reason we normally avoid resolving TYPENAME_TYPEs
17738 is that a specialization of `S' might render
17739 `S<T>::R' not a type. However, if `S' is
17740 specialized, then this `i' will not be used, so there
17741 is no harm in resolving the types here. */
17744 /* Resolve the TYPENAME_TYPE. */
17745 type
= resolve_typename_type (qualifying_scope
,
17746 /*only_current_p=*/false);
17747 /* If that failed, the declarator is invalid. */
17748 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17750 if (typedef_variant_p (type
))
17751 error_at (declarator_id_start_token
->location
,
17752 "cannot define member of dependent typedef "
17755 error_at (declarator_id_start_token
->location
,
17756 "%<%T::%E%> is not a type",
17757 TYPE_CONTEXT (qualifying_scope
),
17758 TYPE_IDENTIFIER (qualifying_scope
));
17760 qualifying_scope
= type
;
17765 if (unqualified_name
)
17769 if (qualifying_scope
17770 && CLASS_TYPE_P (qualifying_scope
))
17771 class_type
= qualifying_scope
;
17773 class_type
= current_class_type
;
17775 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17777 tree name_type
= TREE_TYPE (unqualified_name
);
17778 if (class_type
&& same_type_p (name_type
, class_type
))
17780 if (qualifying_scope
17781 && CLASSTYPE_USE_TEMPLATE (name_type
))
17783 error_at (declarator_id_start_token
->location
,
17784 "invalid use of constructor as a template");
17785 inform (declarator_id_start_token
->location
,
17786 "use %<%T::%D%> instead of %<%T::%D%> to "
17787 "name the constructor in a qualified name",
17789 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17790 class_type
, name_type
);
17791 declarator
= cp_error_declarator
;
17795 unqualified_name
= constructor_name (class_type
);
17799 /* We do not attempt to print the declarator
17800 here because we do not have enough
17801 information about its original syntactic
17803 cp_parser_error (parser
, "invalid declarator");
17804 declarator
= cp_error_declarator
;
17811 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17812 sfk
= sfk_destructor
;
17813 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17814 sfk
= sfk_conversion
;
17815 else if (/* There's no way to declare a constructor
17816 for an anonymous type, even if the type
17817 got a name for linkage purposes. */
17818 !TYPE_WAS_ANONYMOUS (class_type
)
17819 /* Handle correctly (c++/19200):
17833 friend void N::S();
17836 && class_type
!= qualifying_scope
)
17837 && constructor_name_p (unqualified_name
,
17840 unqualified_name
= constructor_name (class_type
);
17841 sfk
= sfk_constructor
;
17843 else if (is_overloaded_fn (unqualified_name
)
17844 && DECL_CONSTRUCTOR_P (get_first_fn
17845 (unqualified_name
)))
17846 sfk
= sfk_constructor
;
17848 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17849 *ctor_dtor_or_conv_p
= -1;
17852 declarator
= make_id_declarator (qualifying_scope
,
17855 declarator
->std_attributes
= attrs
;
17856 declarator
->id_loc
= token
->location
;
17857 declarator
->parameter_pack_p
= pack_expansion_p
;
17859 if (pack_expansion_p
)
17860 maybe_warn_variadic_templates ();
17863 handle_declarator
:;
17864 scope
= get_scope_of_declarator (declarator
);
17867 /* Any names that appear after the declarator-id for a
17868 member are looked up in the containing scope. */
17869 if (at_function_scope_p ())
17871 /* But declarations with qualified-ids can't appear in a
17873 cp_parser_error (parser
, "qualified-id in declaration");
17874 declarator
= cp_error_declarator
;
17877 pushed_scope
= push_scope (scope
);
17879 parser
->in_declarator_p
= true;
17880 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17881 || (declarator
&& declarator
->kind
== cdk_id
))
17882 /* Default args are only allowed on function
17884 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17886 parser
->default_arg_ok_p
= false;
17895 /* For an abstract declarator, we might wind up with nothing at this
17896 point. That's an error; the declarator is not optional. */
17898 cp_parser_error (parser
, "expected declarator");
17900 /* If we entered a scope, we must exit it now. */
17902 pop_scope (pushed_scope
);
17904 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17905 parser
->in_declarator_p
= saved_in_declarator_p
;
17910 /* Parse a ptr-operator.
17913 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17914 * cv-qualifier-seq [opt]
17916 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17917 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17922 & cv-qualifier-seq [opt]
17924 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17925 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17926 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17927 filled in with the TYPE containing the member. *CV_QUALS is
17928 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17929 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17930 Note that the tree codes returned by this function have nothing
17931 to do with the types of trees that will be eventually be created
17932 to represent the pointer or reference type being parsed. They are
17933 just constants with suggestive names. */
17934 static enum tree_code
17935 cp_parser_ptr_operator (cp_parser
* parser
,
17937 cp_cv_quals
*cv_quals
,
17940 enum tree_code code
= ERROR_MARK
;
17942 tree attrs
= NULL_TREE
;
17944 /* Assume that it's not a pointer-to-member. */
17946 /* And that there are no cv-qualifiers. */
17947 *cv_quals
= TYPE_UNQUALIFIED
;
17949 /* Peek at the next token. */
17950 token
= cp_lexer_peek_token (parser
->lexer
);
17952 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17953 if (token
->type
== CPP_MULT
)
17954 code
= INDIRECT_REF
;
17955 else if (token
->type
== CPP_AND
)
17957 else if ((cxx_dialect
!= cxx98
) &&
17958 token
->type
== CPP_AND_AND
) /* C++0x only */
17959 code
= NON_LVALUE_EXPR
;
17961 if (code
!= ERROR_MARK
)
17963 /* Consume the `*', `&' or `&&'. */
17964 cp_lexer_consume_token (parser
->lexer
);
17966 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17967 `&', if we are allowing GNU extensions. (The only qualifier
17968 that can legally appear after `&' is `restrict', but that is
17969 enforced during semantic analysis. */
17970 if (code
== INDIRECT_REF
17971 || cp_parser_allow_gnu_extensions_p (parser
))
17972 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17974 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17975 if (attributes
!= NULL
)
17976 *attributes
= attrs
;
17980 /* Try the pointer-to-member case. */
17981 cp_parser_parse_tentatively (parser
);
17982 /* Look for the optional `::' operator. */
17983 cp_parser_global_scope_opt (parser
,
17984 /*current_scope_valid_p=*/false);
17985 /* Look for the nested-name specifier. */
17986 token
= cp_lexer_peek_token (parser
->lexer
);
17987 cp_parser_nested_name_specifier (parser
,
17988 /*typename_keyword_p=*/false,
17989 /*check_dependency_p=*/true,
17991 /*is_declaration=*/false);
17992 /* If we found it, and the next token is a `*', then we are
17993 indeed looking at a pointer-to-member operator. */
17994 if (!cp_parser_error_occurred (parser
)
17995 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17997 /* Indicate that the `*' operator was used. */
17998 code
= INDIRECT_REF
;
18000 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18001 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
18002 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
18003 error_at (token
->location
, "cannot form pointer to member of "
18004 "non-class %q#T", parser
->scope
);
18007 /* The type of which the member is a member is given by the
18009 *type
= parser
->scope
;
18010 /* The next name will not be qualified. */
18011 parser
->scope
= NULL_TREE
;
18012 parser
->qualifying_scope
= NULL_TREE
;
18013 parser
->object_scope
= NULL_TREE
;
18014 /* Look for optional c++11 attributes. */
18015 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18016 if (attributes
!= NULL
)
18017 *attributes
= attrs
;
18018 /* Look for the optional cv-qualifier-seq. */
18019 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18022 /* If that didn't work we don't have a ptr-operator. */
18023 if (!cp_parser_parse_definitely (parser
))
18024 cp_parser_error (parser
, "expected ptr-operator");
18030 /* Parse an (optional) cv-qualifier-seq.
18033 cv-qualifier cv-qualifier-seq [opt]
18044 Returns a bitmask representing the cv-qualifiers. */
18047 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18049 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18054 cp_cv_quals cv_qualifier
;
18056 /* Peek at the next token. */
18057 token
= cp_lexer_peek_token (parser
->lexer
);
18058 /* See if it's a cv-qualifier. */
18059 switch (token
->keyword
)
18062 cv_qualifier
= TYPE_QUAL_CONST
;
18066 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18070 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18074 cv_qualifier
= TYPE_UNQUALIFIED
;
18081 if (cv_quals
& cv_qualifier
)
18083 error_at (token
->location
, "duplicate cv-qualifier");
18084 cp_lexer_purge_token (parser
->lexer
);
18088 cp_lexer_consume_token (parser
->lexer
);
18089 cv_quals
|= cv_qualifier
;
18096 /* Parse an (optional) ref-qualifier
18102 Returns cp_ref_qualifier representing ref-qualifier. */
18104 static cp_ref_qualifier
18105 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18107 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18109 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18110 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18115 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18116 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18118 switch (token
->type
)
18121 curr_ref_qual
= REF_QUAL_LVALUE
;
18125 curr_ref_qual
= REF_QUAL_RVALUE
;
18129 curr_ref_qual
= REF_QUAL_NONE
;
18133 if (!curr_ref_qual
)
18137 error_at (token
->location
, "multiple ref-qualifiers");
18138 cp_lexer_purge_token (parser
->lexer
);
18142 ref_qual
= curr_ref_qual
;
18143 cp_lexer_consume_token (parser
->lexer
);
18150 /* Parse an (optional) virt-specifier-seq.
18152 virt-specifier-seq:
18153 virt-specifier virt-specifier-seq [opt]
18159 Returns a bitmask representing the virt-specifiers. */
18161 static cp_virt_specifiers
18162 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18164 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18169 cp_virt_specifiers virt_specifier
;
18171 /* Peek at the next token. */
18172 token
= cp_lexer_peek_token (parser
->lexer
);
18173 /* See if it's a virt-specifier-qualifier. */
18174 if (token
->type
!= CPP_NAME
)
18176 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18178 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18179 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18181 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18183 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18184 virt_specifier
= VIRT_SPEC_FINAL
;
18186 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18188 virt_specifier
= VIRT_SPEC_FINAL
;
18193 if (virt_specifiers
& virt_specifier
)
18195 error_at (token
->location
, "duplicate virt-specifier");
18196 cp_lexer_purge_token (parser
->lexer
);
18200 cp_lexer_consume_token (parser
->lexer
);
18201 virt_specifiers
|= virt_specifier
;
18204 return virt_specifiers
;
18207 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18208 is in scope even though it isn't real. */
18211 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18215 if (current_class_ptr
)
18217 /* We don't clear this between NSDMIs. Is it already what we want? */
18218 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18219 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18220 && cp_type_quals (type
) == quals
)
18224 this_parm
= build_this_parm (ctype
, quals
);
18225 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18226 current_class_ptr
= NULL_TREE
;
18228 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18229 current_class_ptr
= this_parm
;
18232 /* Return true iff our current scope is a non-static data member
18236 parsing_nsdmi (void)
18238 /* We recognize NSDMI context by the context-less 'this' pointer set up
18239 by the function above. */
18240 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18245 /* Parse a late-specified return type, if any. This is not a separate
18246 non-terminal, but part of a function declarator, which looks like
18248 -> trailing-type-specifier-seq abstract-declarator(opt)
18250 Returns the type indicated by the type-id.
18252 In addition to this this parses any queued up omp declare simd
18253 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18255 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18259 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18263 tree type
= NULL_TREE
;
18264 bool declare_simd_p
= (parser
->omp_declare_simd
18266 && declarator
->kind
== cdk_id
);
18268 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18269 && declarator
&& declarator
->kind
== cdk_id
);
18271 /* Peek at the next token. */
18272 token
= cp_lexer_peek_token (parser
->lexer
);
18273 /* A late-specified return type is indicated by an initial '->'. */
18274 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18277 tree save_ccp
= current_class_ptr
;
18278 tree save_ccr
= current_class_ref
;
18281 /* DR 1207: 'this' is in scope in the trailing return type. */
18282 inject_this_parameter (current_class_type
, quals
);
18285 if (token
->type
== CPP_DEREF
)
18287 /* Consume the ->. */
18288 cp_lexer_consume_token (parser
->lexer
);
18290 type
= cp_parser_trailing_type_id (parser
);
18293 if (cilk_simd_fn_vector_p
)
18294 declarator
->std_attributes
18295 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18296 declarator
->std_attributes
);
18297 if (declare_simd_p
)
18298 declarator
->std_attributes
18299 = cp_parser_late_parsing_omp_declare_simd (parser
,
18300 declarator
->std_attributes
);
18304 current_class_ptr
= save_ccp
;
18305 current_class_ref
= save_ccr
;
18311 /* Parse a declarator-id.
18315 :: [opt] nested-name-specifier [opt] type-name
18317 In the `id-expression' case, the value returned is as for
18318 cp_parser_id_expression if the id-expression was an unqualified-id.
18319 If the id-expression was a qualified-id, then a SCOPE_REF is
18320 returned. The first operand is the scope (either a NAMESPACE_DECL
18321 or TREE_TYPE), but the second is still just a representation of an
18325 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18328 /* The expression must be an id-expression. Assume that qualified
18329 names are the names of types so that:
18332 int S<T>::R::i = 3;
18334 will work; we must treat `S<T>::R' as the name of a type.
18335 Similarly, assume that qualified names are templates, where
18339 int S<T>::R<T>::i = 3;
18342 id
= cp_parser_id_expression (parser
,
18343 /*template_keyword_p=*/false,
18344 /*check_dependency_p=*/false,
18345 /*template_p=*/NULL
,
18346 /*declarator_p=*/true,
18348 if (id
&& BASELINK_P (id
))
18349 id
= BASELINK_FUNCTIONS (id
);
18353 /* Parse a type-id.
18356 type-specifier-seq abstract-declarator [opt]
18358 Returns the TYPE specified. */
18361 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18362 bool is_trailing_return
)
18364 cp_decl_specifier_seq type_specifier_seq
;
18365 cp_declarator
*abstract_declarator
;
18367 /* Parse the type-specifier-seq. */
18368 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18369 is_trailing_return
,
18370 &type_specifier_seq
);
18371 if (type_specifier_seq
.type
== error_mark_node
)
18372 return error_mark_node
;
18374 /* There might or might not be an abstract declarator. */
18375 cp_parser_parse_tentatively (parser
);
18376 /* Look for the declarator. */
18377 abstract_declarator
18378 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18379 /*parenthesized_p=*/NULL
,
18380 /*member_p=*/false,
18381 /*friend_p=*/false);
18382 /* Check to see if there really was a declarator. */
18383 if (!cp_parser_parse_definitely (parser
))
18384 abstract_declarator
= NULL
;
18386 if (type_specifier_seq
.type
18387 /* None of the valid uses of 'auto' in C++14 involve the type-id
18388 nonterminal, but it is valid in a trailing-return-type. */
18389 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18390 && type_uses_auto (type_specifier_seq
.type
))
18392 /* A type-id with type 'auto' is only ok if the abstract declarator
18393 is a function declarator with a late-specified return type. */
18394 if (abstract_declarator
18395 && abstract_declarator
->kind
== cdk_function
18396 && abstract_declarator
->u
.function
.late_return_type
)
18400 error ("invalid use of %<auto%>");
18401 return error_mark_node
;
18405 return groktypename (&type_specifier_seq
, abstract_declarator
,
18409 static tree
cp_parser_type_id (cp_parser
*parser
)
18411 return cp_parser_type_id_1 (parser
, false, false);
18414 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18417 const char *saved_message
= parser
->type_definition_forbidden_message
;
18418 parser
->type_definition_forbidden_message
18419 = G_("types may not be defined in template arguments");
18420 r
= cp_parser_type_id_1 (parser
, true, false);
18421 parser
->type_definition_forbidden_message
= saved_message
;
18422 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18424 error ("invalid use of %<auto%> in template argument");
18425 r
= error_mark_node
;
18430 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18432 return cp_parser_type_id_1 (parser
, false, true);
18435 /* Parse a type-specifier-seq.
18437 type-specifier-seq:
18438 type-specifier type-specifier-seq [opt]
18442 type-specifier-seq:
18443 attributes type-specifier-seq [opt]
18445 If IS_DECLARATION is true, we are at the start of a "condition" or
18446 exception-declaration, so we might be followed by a declarator-id.
18448 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18449 i.e. we've just seen "->".
18451 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18454 cp_parser_type_specifier_seq (cp_parser
* parser
,
18455 bool is_declaration
,
18456 bool is_trailing_return
,
18457 cp_decl_specifier_seq
*type_specifier_seq
)
18459 bool seen_type_specifier
= false;
18460 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18461 cp_token
*start_token
= NULL
;
18463 /* Clear the TYPE_SPECIFIER_SEQ. */
18464 clear_decl_specs (type_specifier_seq
);
18466 /* In the context of a trailing return type, enum E { } is an
18467 elaborated-type-specifier followed by a function-body, not an
18469 if (is_trailing_return
)
18470 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18472 /* Parse the type-specifiers and attributes. */
18475 tree type_specifier
;
18476 bool is_cv_qualifier
;
18478 /* Check for attributes first. */
18479 if (cp_next_tokens_can_be_attribute_p (parser
))
18481 type_specifier_seq
->attributes
=
18482 chainon (type_specifier_seq
->attributes
,
18483 cp_parser_attributes_opt (parser
));
18487 /* record the token of the beginning of the type specifier seq,
18488 for error reporting purposes*/
18490 start_token
= cp_lexer_peek_token (parser
->lexer
);
18492 /* Look for the type-specifier. */
18493 type_specifier
= cp_parser_type_specifier (parser
,
18495 type_specifier_seq
,
18496 /*is_declaration=*/false,
18499 if (!type_specifier
)
18501 /* If the first type-specifier could not be found, this is not a
18502 type-specifier-seq at all. */
18503 if (!seen_type_specifier
)
18505 /* Set in_declarator_p to avoid skipping to the semicolon. */
18506 int in_decl
= parser
->in_declarator_p
;
18507 parser
->in_declarator_p
= true;
18509 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18510 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18511 cp_parser_error (parser
, "expected type-specifier");
18513 parser
->in_declarator_p
= in_decl
;
18515 type_specifier_seq
->type
= error_mark_node
;
18518 /* If subsequent type-specifiers could not be found, the
18519 type-specifier-seq is complete. */
18523 seen_type_specifier
= true;
18524 /* The standard says that a condition can be:
18526 type-specifier-seq declarator = assignment-expression
18533 we should treat the "S" as a declarator, not as a
18534 type-specifier. The standard doesn't say that explicitly for
18535 type-specifier-seq, but it does say that for
18536 decl-specifier-seq in an ordinary declaration. Perhaps it
18537 would be clearer just to allow a decl-specifier-seq here, and
18538 then add a semantic restriction that if any decl-specifiers
18539 that are not type-specifiers appear, the program is invalid. */
18540 if (is_declaration
&& !is_cv_qualifier
)
18541 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18545 /* Return whether the function currently being declared has an associated
18546 template parameter list. */
18549 function_being_declared_is_template_p (cp_parser
* parser
)
18551 if (!current_template_parms
|| processing_template_parmlist
)
18554 if (parser
->implicit_template_scope
)
18557 if (at_class_scope_p ()
18558 && TYPE_BEING_DEFINED (current_class_type
))
18559 return parser
->num_template_parameter_lists
!= 0;
18561 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18562 (current_class_type
));
18565 /* Parse a parameter-declaration-clause.
18567 parameter-declaration-clause:
18568 parameter-declaration-list [opt] ... [opt]
18569 parameter-declaration-list , ...
18571 Returns a representation for the parameter declarations. A return
18572 value of NULL indicates a parameter-declaration-clause consisting
18573 only of an ellipsis. */
18576 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18585 int auto_is_implicit_function_template_parm_p
;
18587 parser
->auto_is_implicit_function_template_parm_p
18588 = auto_is_implicit_function_template_parm_p
;
18590 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18594 if (!processing_specialization
18595 && !processing_template_parmlist
18596 && !processing_explicit_instantiation
)
18597 if (!current_function_decl
18598 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18599 parser
->auto_is_implicit_function_template_parm_p
= true;
18601 /* Peek at the next token. */
18602 token
= cp_lexer_peek_token (parser
->lexer
);
18603 /* Check for trivial parameter-declaration-clauses. */
18604 if (token
->type
== CPP_ELLIPSIS
)
18606 /* Consume the `...' token. */
18607 cp_lexer_consume_token (parser
->lexer
);
18610 else if (token
->type
== CPP_CLOSE_PAREN
)
18611 /* There are no parameters. */
18613 #ifndef NO_IMPLICIT_EXTERN_C
18614 if (in_system_header_at (input_location
)
18615 && current_class_type
== NULL
18616 && current_lang_name
== lang_name_c
)
18620 return void_list_node
;
18622 /* Check for `(void)', too, which is a special case. */
18623 else if (token
->keyword
== RID_VOID
18624 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18625 == CPP_CLOSE_PAREN
))
18627 /* Consume the `void' token. */
18628 cp_lexer_consume_token (parser
->lexer
);
18629 /* There are no parameters. */
18630 return void_list_node
;
18633 /* Parse the parameter-declaration-list. */
18634 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18635 /* If a parse error occurred while parsing the
18636 parameter-declaration-list, then the entire
18637 parameter-declaration-clause is erroneous. */
18641 /* Peek at the next token. */
18642 token
= cp_lexer_peek_token (parser
->lexer
);
18643 /* If it's a `,', the clause should terminate with an ellipsis. */
18644 if (token
->type
== CPP_COMMA
)
18646 /* Consume the `,'. */
18647 cp_lexer_consume_token (parser
->lexer
);
18648 /* Expect an ellipsis. */
18650 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18652 /* It might also be `...' if the optional trailing `,' was
18654 else if (token
->type
== CPP_ELLIPSIS
)
18656 /* Consume the `...' token. */
18657 cp_lexer_consume_token (parser
->lexer
);
18658 /* And remember that we saw it. */
18662 ellipsis_p
= false;
18664 /* Finish the parameter list. */
18666 parameters
= chainon (parameters
, void_list_node
);
18671 /* Parse a parameter-declaration-list.
18673 parameter-declaration-list:
18674 parameter-declaration
18675 parameter-declaration-list , parameter-declaration
18677 Returns a representation of the parameter-declaration-list, as for
18678 cp_parser_parameter_declaration_clause. However, the
18679 `void_list_node' is never appended to the list. Upon return,
18680 *IS_ERROR will be true iff an error occurred. */
18683 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18685 tree parameters
= NULL_TREE
;
18686 tree
*tail
= ¶meters
;
18687 bool saved_in_unbraced_linkage_specification_p
;
18690 /* Assume all will go well. */
18692 /* The special considerations that apply to a function within an
18693 unbraced linkage specifications do not apply to the parameters
18694 to the function. */
18695 saved_in_unbraced_linkage_specification_p
18696 = parser
->in_unbraced_linkage_specification_p
;
18697 parser
->in_unbraced_linkage_specification_p
= false;
18699 /* Look for more parameters. */
18702 cp_parameter_declarator
*parameter
;
18703 tree decl
= error_mark_node
;
18704 bool parenthesized_p
= false;
18705 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18706 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18707 (current_template_parms
)) : 0);
18709 /* Parse the parameter. */
18711 = cp_parser_parameter_declaration (parser
,
18712 /*template_parm_p=*/false,
18715 /* We don't know yet if the enclosing context is deprecated, so wait
18716 and warn in grokparms if appropriate. */
18717 deprecated_state
= DEPRECATED_SUPPRESS
;
18721 /* If a function parameter pack was specified and an implicit template
18722 parameter was introduced during cp_parser_parameter_declaration,
18723 change any implicit parameters introduced into packs. */
18724 if (parser
->implicit_template_parms
18725 && parameter
->declarator
18726 && parameter
->declarator
->parameter_pack_p
)
18728 int latest_template_parm_idx
= TREE_VEC_LENGTH
18729 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18731 if (latest_template_parm_idx
!= template_parm_idx
)
18732 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18733 (parameter
->decl_specifiers
.type
,
18734 template_parm_idx
, latest_template_parm_idx
);
18737 decl
= grokdeclarator (parameter
->declarator
,
18738 ¶meter
->decl_specifiers
,
18740 parameter
->default_argument
!= NULL_TREE
,
18741 ¶meter
->decl_specifiers
.attributes
);
18744 deprecated_state
= DEPRECATED_NORMAL
;
18746 /* If a parse error occurred parsing the parameter declaration,
18747 then the entire parameter-declaration-list is erroneous. */
18748 if (decl
== error_mark_node
)
18751 parameters
= error_mark_node
;
18755 if (parameter
->decl_specifiers
.attributes
)
18756 cplus_decl_attributes (&decl
,
18757 parameter
->decl_specifiers
.attributes
,
18759 if (DECL_NAME (decl
))
18760 decl
= pushdecl (decl
);
18762 if (decl
!= error_mark_node
)
18764 retrofit_lang_decl (decl
);
18765 DECL_PARM_INDEX (decl
) = ++index
;
18766 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18769 /* Add the new parameter to the list. */
18770 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18771 tail
= &TREE_CHAIN (*tail
);
18773 /* Peek at the next token. */
18774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18775 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18776 /* These are for Objective-C++ */
18777 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18778 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18779 /* The parameter-declaration-list is complete. */
18781 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18785 /* Peek at the next token. */
18786 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18787 /* If it's an ellipsis, then the list is complete. */
18788 if (token
->type
== CPP_ELLIPSIS
)
18790 /* Otherwise, there must be more parameters. Consume the
18792 cp_lexer_consume_token (parser
->lexer
);
18793 /* When parsing something like:
18795 int i(float f, double d)
18797 we can tell after seeing the declaration for "f" that we
18798 are not looking at an initialization of a variable "i",
18799 but rather at the declaration of a function "i".
18801 Due to the fact that the parsing of template arguments
18802 (as specified to a template-id) requires backtracking we
18803 cannot use this technique when inside a template argument
18805 if (!parser
->in_template_argument_list_p
18806 && !parser
->in_type_id_in_expr_p
18807 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18808 /* However, a parameter-declaration of the form
18809 "float(f)" (which is a valid declaration of a
18810 parameter "f") can also be interpreted as an
18811 expression (the conversion of "f" to "float"). */
18812 && !parenthesized_p
)
18813 cp_parser_commit_to_tentative_parse (parser
);
18817 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18818 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18819 cp_parser_skip_to_closing_parenthesis (parser
,
18820 /*recovering=*/true,
18821 /*or_comma=*/false,
18822 /*consume_paren=*/false);
18827 parser
->in_unbraced_linkage_specification_p
18828 = saved_in_unbraced_linkage_specification_p
;
18830 /* Reset implicit_template_scope if we are about to leave the function
18831 parameter list that introduced it. Note that for out-of-line member
18832 definitions, there will be one or more class scopes before we get to
18833 the template parameter scope. */
18835 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18836 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18838 while (maybe_its
->kind
== sk_class
)
18839 maybe_its
= maybe_its
->level_chain
;
18840 if (maybe_its
== its
)
18842 parser
->implicit_template_parms
= 0;
18843 parser
->implicit_template_scope
= 0;
18850 /* Parse a parameter declaration.
18852 parameter-declaration:
18853 decl-specifier-seq ... [opt] declarator
18854 decl-specifier-seq declarator = assignment-expression
18855 decl-specifier-seq ... [opt] abstract-declarator [opt]
18856 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18858 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18859 declares a template parameter. (In that case, a non-nested `>'
18860 token encountered during the parsing of the assignment-expression
18861 is not interpreted as a greater-than operator.)
18863 Returns a representation of the parameter, or NULL if an error
18864 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18865 true iff the declarator is of the form "(p)". */
18867 static cp_parameter_declarator
*
18868 cp_parser_parameter_declaration (cp_parser
*parser
,
18869 bool template_parm_p
,
18870 bool *parenthesized_p
)
18872 int declares_class_or_enum
;
18873 cp_decl_specifier_seq decl_specifiers
;
18874 cp_declarator
*declarator
;
18875 tree default_argument
;
18876 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18877 const char *saved_message
;
18879 /* In a template parameter, `>' is not an operator.
18883 When parsing a default template-argument for a non-type
18884 template-parameter, the first non-nested `>' is taken as the end
18885 of the template parameter-list rather than a greater-than
18888 /* Type definitions may not appear in parameter types. */
18889 saved_message
= parser
->type_definition_forbidden_message
;
18890 parser
->type_definition_forbidden_message
18891 = G_("types may not be defined in parameter types");
18893 /* Parse the declaration-specifiers. */
18894 cp_parser_decl_specifier_seq (parser
,
18895 CP_PARSER_FLAGS_NONE
,
18897 &declares_class_or_enum
);
18899 /* Complain about missing 'typename' or other invalid type names. */
18900 if (!decl_specifiers
.any_type_specifiers_p
18901 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18902 decl_specifiers
.type
= error_mark_node
;
18904 /* If an error occurred, there's no reason to attempt to parse the
18905 rest of the declaration. */
18906 if (cp_parser_error_occurred (parser
))
18908 parser
->type_definition_forbidden_message
= saved_message
;
18912 /* Peek at the next token. */
18913 token
= cp_lexer_peek_token (parser
->lexer
);
18915 /* If the next token is a `)', `,', `=', `>', or `...', then there
18916 is no declarator. However, when variadic templates are enabled,
18917 there may be a declarator following `...'. */
18918 if (token
->type
== CPP_CLOSE_PAREN
18919 || token
->type
== CPP_COMMA
18920 || token
->type
== CPP_EQ
18921 || token
->type
== CPP_GREATER
)
18924 if (parenthesized_p
)
18925 *parenthesized_p
= false;
18927 /* Otherwise, there should be a declarator. */
18930 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18931 parser
->default_arg_ok_p
= false;
18933 /* After seeing a decl-specifier-seq, if the next token is not a
18934 "(", there is no possibility that the code is a valid
18935 expression. Therefore, if parsing tentatively, we commit at
18937 if (!parser
->in_template_argument_list_p
18938 /* In an expression context, having seen:
18942 we cannot be sure whether we are looking at a
18943 function-type (taking a "char" as a parameter) or a cast
18944 of some object of type "char" to "int". */
18945 && !parser
->in_type_id_in_expr_p
18946 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18947 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18948 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18949 cp_parser_commit_to_tentative_parse (parser
);
18950 /* Parse the declarator. */
18951 declarator_token_start
= token
;
18952 declarator
= cp_parser_declarator (parser
,
18953 CP_PARSER_DECLARATOR_EITHER
,
18954 /*ctor_dtor_or_conv_p=*/NULL
,
18956 /*member_p=*/false,
18957 /*friend_p=*/false);
18958 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18959 /* After the declarator, allow more attributes. */
18960 decl_specifiers
.attributes
18961 = chainon (decl_specifiers
.attributes
,
18962 cp_parser_attributes_opt (parser
));
18965 /* If the next token is an ellipsis, and we have not seen a
18966 declarator name, and the type of the declarator contains parameter
18967 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18968 a parameter pack expansion expression. Otherwise, leave the
18969 ellipsis for a C-style variadic function. */
18970 token
= cp_lexer_peek_token (parser
->lexer
);
18971 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18973 tree type
= decl_specifiers
.type
;
18975 if (type
&& DECL_P (type
))
18976 type
= TREE_TYPE (type
);
18979 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18980 && declarator_can_be_parameter_pack (declarator
)
18981 && (!declarator
|| !declarator
->parameter_pack_p
)
18982 && uses_parameter_packs (type
))
18984 /* Consume the `...'. */
18985 cp_lexer_consume_token (parser
->lexer
);
18986 maybe_warn_variadic_templates ();
18988 /* Build a pack expansion type */
18990 declarator
->parameter_pack_p
= true;
18992 decl_specifiers
.type
= make_pack_expansion (type
);
18996 /* The restriction on defining new types applies only to the type
18997 of the parameter, not to the default argument. */
18998 parser
->type_definition_forbidden_message
= saved_message
;
19000 /* If the next token is `=', then process a default argument. */
19001 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19003 token
= cp_lexer_peek_token (parser
->lexer
);
19004 /* If we are defining a class, then the tokens that make up the
19005 default argument must be saved and processed later. */
19006 if (!template_parm_p
&& at_class_scope_p ()
19007 && TYPE_BEING_DEFINED (current_class_type
)
19008 && !LAMBDA_TYPE_P (current_class_type
))
19009 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
19010 /* Outside of a class definition, we can just parse the
19011 assignment-expression. */
19014 = cp_parser_default_argument (parser
, template_parm_p
);
19016 if (!parser
->default_arg_ok_p
)
19018 if (flag_permissive
)
19019 warning (0, "deprecated use of default argument for parameter of non-function");
19022 error_at (token
->location
,
19023 "default arguments are only "
19024 "permitted for function parameters");
19025 default_argument
= NULL_TREE
;
19028 else if ((declarator
&& declarator
->parameter_pack_p
)
19029 || (decl_specifiers
.type
19030 && PACK_EXPANSION_P (decl_specifiers
.type
)))
19032 /* Find the name of the parameter pack. */
19033 cp_declarator
*id_declarator
= declarator
;
19034 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
19035 id_declarator
= id_declarator
->declarator
;
19037 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19038 error_at (declarator_token_start
->location
,
19040 ? G_("template parameter pack %qD "
19041 "cannot have a default argument")
19042 : G_("parameter pack %qD cannot have "
19043 "a default argument"),
19044 id_declarator
->u
.id
.unqualified_name
);
19046 error_at (declarator_token_start
->location
,
19048 ? G_("template parameter pack cannot have "
19049 "a default argument")
19050 : G_("parameter pack cannot have a "
19051 "default argument"));
19053 default_argument
= NULL_TREE
;
19057 default_argument
= NULL_TREE
;
19059 return make_parameter_declarator (&decl_specifiers
,
19064 /* Parse a default argument and return it.
19066 TEMPLATE_PARM_P is true if this is a default argument for a
19067 non-type template parameter. */
19069 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19071 tree default_argument
= NULL_TREE
;
19072 bool saved_greater_than_is_operator_p
;
19073 bool saved_local_variables_forbidden_p
;
19074 bool non_constant_p
, is_direct_init
;
19076 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19078 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19079 parser
->greater_than_is_operator_p
= !template_parm_p
;
19080 /* Local variable names (and the `this' keyword) may not
19081 appear in a default argument. */
19082 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19083 parser
->local_variables_forbidden_p
= true;
19084 /* Parse the assignment-expression. */
19085 if (template_parm_p
)
19086 push_deferring_access_checks (dk_no_deferred
);
19087 tree saved_class_ptr
= NULL_TREE
;
19088 tree saved_class_ref
= NULL_TREE
;
19089 /* The "this" pointer is not valid in a default argument. */
19092 saved_class_ptr
= current_class_ptr
;
19093 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19094 saved_class_ref
= current_class_ref
;
19095 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19098 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19099 /* Restore the "this" pointer. */
19102 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19103 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19105 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19106 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19107 if (template_parm_p
)
19108 pop_deferring_access_checks ();
19109 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19110 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19112 return default_argument
;
19115 /* Parse a function-body.
19118 compound_statement */
19121 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19123 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19126 /* Parse a ctor-initializer-opt followed by a function-body. Return
19127 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19128 is true we are parsing a function-try-block. */
19131 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19132 bool in_function_try_block
)
19135 bool ctor_initializer_p
;
19136 const bool check_body_p
=
19137 DECL_CONSTRUCTOR_P (current_function_decl
)
19138 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19141 /* Begin the function body. */
19142 body
= begin_function_body ();
19143 /* Parse the optional ctor-initializer. */
19144 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19146 /* If we're parsing a constexpr constructor definition, we need
19147 to check that the constructor body is indeed empty. However,
19148 before we get to cp_parser_function_body lot of junk has been
19149 generated, so we can't just check that we have an empty block.
19150 Rather we take a snapshot of the outermost block, and check whether
19151 cp_parser_function_body changed its state. */
19154 list
= cur_stmt_list
;
19155 if (STATEMENT_LIST_TAIL (list
))
19156 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19158 /* Parse the function-body. */
19159 cp_parser_function_body (parser
, in_function_try_block
);
19161 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19162 /* Finish the function body. */
19163 finish_function_body (body
);
19165 return ctor_initializer_p
;
19168 /* Parse an initializer.
19171 = initializer-clause
19172 ( expression-list )
19174 Returns an expression representing the initializer. If no
19175 initializer is present, NULL_TREE is returned.
19177 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19178 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19179 set to TRUE if there is no initializer present. If there is an
19180 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19181 is set to true; otherwise it is set to false. */
19184 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19185 bool* non_constant_p
)
19190 /* Peek at the next token. */
19191 token
= cp_lexer_peek_token (parser
->lexer
);
19193 /* Let our caller know whether or not this initializer was
19195 *is_direct_init
= (token
->type
!= CPP_EQ
);
19196 /* Assume that the initializer is constant. */
19197 *non_constant_p
= false;
19199 if (token
->type
== CPP_EQ
)
19201 /* Consume the `='. */
19202 cp_lexer_consume_token (parser
->lexer
);
19203 /* Parse the initializer-clause. */
19204 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19206 else if (token
->type
== CPP_OPEN_PAREN
)
19208 vec
<tree
, va_gc
> *vec
;
19209 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19211 /*allow_expansion_p=*/true,
19214 return error_mark_node
;
19215 init
= build_tree_list_vec (vec
);
19216 release_tree_vector (vec
);
19218 else if (token
->type
== CPP_OPEN_BRACE
)
19220 cp_lexer_set_source_position (parser
->lexer
);
19221 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19222 init
= cp_parser_braced_list (parser
, non_constant_p
);
19223 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19227 /* Anything else is an error. */
19228 cp_parser_error (parser
, "expected initializer");
19229 init
= error_mark_node
;
19235 /* Parse an initializer-clause.
19237 initializer-clause:
19238 assignment-expression
19241 Returns an expression representing the initializer.
19243 If the `assignment-expression' production is used the value
19244 returned is simply a representation for the expression.
19246 Otherwise, calls cp_parser_braced_list. */
19249 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19253 /* Assume the expression is constant. */
19254 *non_constant_p
= false;
19256 /* If it is not a `{', then we are looking at an
19257 assignment-expression. */
19258 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19261 = cp_parser_constant_expression (parser
,
19262 /*allow_non_constant_p=*/true,
19266 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19268 return initializer
;
19271 /* Parse a brace-enclosed initializer list.
19274 { initializer-list , [opt] }
19277 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19278 the elements of the initializer-list (or NULL, if the last
19279 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19280 NULL_TREE. There is no way to detect whether or not the optional
19281 trailing `,' was provided. NON_CONSTANT_P is as for
19282 cp_parser_initializer. */
19285 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19289 /* Consume the `{' token. */
19290 cp_lexer_consume_token (parser
->lexer
);
19291 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19292 initializer
= make_node (CONSTRUCTOR
);
19293 /* If it's not a `}', then there is a non-trivial initializer. */
19294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19296 /* Parse the initializer list. */
19297 CONSTRUCTOR_ELTS (initializer
)
19298 = cp_parser_initializer_list (parser
, non_constant_p
);
19299 /* A trailing `,' token is allowed. */
19300 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19301 cp_lexer_consume_token (parser
->lexer
);
19304 *non_constant_p
= false;
19305 /* Now, there should be a trailing `}'. */
19306 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19307 TREE_TYPE (initializer
) = init_list_type_node
;
19308 return initializer
;
19311 /* Consume tokens up to, and including, the next non-nested closing `]'.
19312 Returns true iff we found a closing `]'. */
19315 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19317 unsigned square_depth
= 0;
19321 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19323 switch (token
->type
)
19326 case CPP_PRAGMA_EOL
:
19327 /* If we've run out of tokens, then there is no closing `]'. */
19330 case CPP_OPEN_SQUARE
:
19334 case CPP_CLOSE_SQUARE
:
19335 if (!square_depth
--)
19337 cp_lexer_consume_token (parser
->lexer
);
19346 /* Consume the token. */
19347 cp_lexer_consume_token (parser
->lexer
);
19351 /* Return true if we are looking at an array-designator, false otherwise. */
19354 cp_parser_array_designator_p (cp_parser
*parser
)
19356 /* Consume the `['. */
19357 cp_lexer_consume_token (parser
->lexer
);
19359 cp_lexer_save_tokens (parser
->lexer
);
19361 /* Skip tokens until the next token is a closing square bracket.
19362 If we find the closing `]', and the next token is a `=', then
19363 we are looking at an array designator. */
19364 bool array_designator_p
19365 = (cp_parser_skip_to_closing_square_bracket (parser
)
19366 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19368 /* Roll back the tokens we skipped. */
19369 cp_lexer_rollback_tokens (parser
->lexer
);
19371 return array_designator_p
;
19374 /* Parse an initializer-list.
19377 initializer-clause ... [opt]
19378 initializer-list , initializer-clause ... [opt]
19383 designation initializer-clause ...[opt]
19384 initializer-list , designation initializer-clause ...[opt]
19389 [ constant-expression ] =
19391 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19392 for the initializer. If the INDEX of the elt is non-NULL, it is the
19393 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19394 as for cp_parser_initializer. */
19396 static vec
<constructor_elt
, va_gc
> *
19397 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19399 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19401 /* Assume all of the expressions are constant. */
19402 *non_constant_p
= false;
19404 /* Parse the rest of the list. */
19410 bool clause_non_constant_p
;
19412 /* If the next token is an identifier and the following one is a
19413 colon, we are looking at the GNU designated-initializer
19415 if (cp_parser_allow_gnu_extensions_p (parser
)
19416 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19417 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19419 /* Warn the user that they are using an extension. */
19420 pedwarn (input_location
, OPT_Wpedantic
,
19421 "ISO C++ does not allow designated initializers");
19422 /* Consume the identifier. */
19423 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19424 /* Consume the `:'. */
19425 cp_lexer_consume_token (parser
->lexer
);
19427 /* Also handle the C99 syntax, '. id ='. */
19428 else if (cp_parser_allow_gnu_extensions_p (parser
)
19429 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19430 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19431 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19433 /* Warn the user that they are using an extension. */
19434 pedwarn (input_location
, OPT_Wpedantic
,
19435 "ISO C++ does not allow C99 designated initializers");
19436 /* Consume the `.'. */
19437 cp_lexer_consume_token (parser
->lexer
);
19438 /* Consume the identifier. */
19439 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19440 /* Consume the `='. */
19441 cp_lexer_consume_token (parser
->lexer
);
19443 /* Also handle C99 array designators, '[ const ] ='. */
19444 else if (cp_parser_allow_gnu_extensions_p (parser
)
19445 && !c_dialect_objc ()
19446 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19448 /* In C++11, [ could start a lambda-introducer. */
19449 bool non_const
= false;
19451 cp_parser_parse_tentatively (parser
);
19453 if (!cp_parser_array_designator_p (parser
))
19455 cp_parser_simulate_error (parser
);
19456 designator
= NULL_TREE
;
19460 designator
= cp_parser_constant_expression (parser
, true,
19462 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19463 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19466 if (!cp_parser_parse_definitely (parser
))
19467 designator
= NULL_TREE
;
19468 else if (non_const
)
19469 require_potential_rvalue_constant_expression (designator
);
19472 designator
= NULL_TREE
;
19474 /* Parse the initializer. */
19475 initializer
= cp_parser_initializer_clause (parser
,
19476 &clause_non_constant_p
);
19477 /* If any clause is non-constant, so is the entire initializer. */
19478 if (clause_non_constant_p
)
19479 *non_constant_p
= true;
19481 /* If we have an ellipsis, this is an initializer pack
19483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19485 /* Consume the `...'. */
19486 cp_lexer_consume_token (parser
->lexer
);
19488 /* Turn the initializer into an initializer expansion. */
19489 initializer
= make_pack_expansion (initializer
);
19492 /* Add it to the vector. */
19493 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19495 /* If the next token is not a comma, we have reached the end of
19497 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19500 /* Peek at the next token. */
19501 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19502 /* If the next token is a `}', then we're still done. An
19503 initializer-clause can have a trailing `,' after the
19504 initializer-list and before the closing `}'. */
19505 if (token
->type
== CPP_CLOSE_BRACE
)
19508 /* Consume the `,' token. */
19509 cp_lexer_consume_token (parser
->lexer
);
19515 /* Classes [gram.class] */
19517 /* Parse a class-name.
19523 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19524 to indicate that names looked up in dependent types should be
19525 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19526 keyword has been used to indicate that the name that appears next
19527 is a template. TAG_TYPE indicates the explicit tag given before
19528 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19529 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19530 is the class being defined in a class-head.
19532 Returns the TYPE_DECL representing the class. */
19535 cp_parser_class_name (cp_parser
*parser
,
19536 bool typename_keyword_p
,
19537 bool template_keyword_p
,
19538 enum tag_types tag_type
,
19539 bool check_dependency_p
,
19541 bool is_declaration
)
19547 tree identifier
= NULL_TREE
;
19549 /* All class-names start with an identifier. */
19550 token
= cp_lexer_peek_token (parser
->lexer
);
19551 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19553 cp_parser_error (parser
, "expected class-name");
19554 return error_mark_node
;
19557 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19558 to a template-id, so we save it here. */
19559 scope
= parser
->scope
;
19560 if (scope
== error_mark_node
)
19561 return error_mark_node
;
19563 /* Any name names a type if we're following the `typename' keyword
19564 in a qualified name where the enclosing scope is type-dependent. */
19565 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19566 && dependent_type_p (scope
));
19567 /* Handle the common case (an identifier, but not a template-id)
19569 if (token
->type
== CPP_NAME
19570 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19572 cp_token
*identifier_token
;
19575 /* Look for the identifier. */
19576 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19577 ambiguous_p
= identifier_token
->error_reported
;
19578 identifier
= cp_parser_identifier (parser
);
19579 /* If the next token isn't an identifier, we are certainly not
19580 looking at a class-name. */
19581 if (identifier
== error_mark_node
)
19582 decl
= error_mark_node
;
19583 /* If we know this is a type-name, there's no need to look it
19585 else if (typename_p
)
19589 tree ambiguous_decls
;
19590 /* If we already know that this lookup is ambiguous, then
19591 we've already issued an error message; there's no reason
19595 cp_parser_simulate_error (parser
);
19596 return error_mark_node
;
19598 /* If the next token is a `::', then the name must be a type
19601 [basic.lookup.qual]
19603 During the lookup for a name preceding the :: scope
19604 resolution operator, object, function, and enumerator
19605 names are ignored. */
19606 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19607 tag_type
= typename_type
;
19608 /* Look up the name. */
19609 decl
= cp_parser_lookup_name (parser
, identifier
,
19611 /*is_template=*/false,
19612 /*is_namespace=*/false,
19613 check_dependency_p
,
19615 identifier_token
->location
);
19616 if (ambiguous_decls
)
19618 if (cp_parser_parsing_tentatively (parser
))
19619 cp_parser_simulate_error (parser
);
19620 return error_mark_node
;
19626 /* Try a template-id. */
19627 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19628 check_dependency_p
,
19631 if (decl
== error_mark_node
)
19632 return error_mark_node
;
19635 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19637 /* If this is a typename, create a TYPENAME_TYPE. */
19638 if (typename_p
&& decl
!= error_mark_node
)
19640 decl
= make_typename_type (scope
, decl
, typename_type
,
19641 /*complain=*/tf_error
);
19642 if (decl
!= error_mark_node
)
19643 decl
= TYPE_NAME (decl
);
19646 decl
= strip_using_decl (decl
);
19648 /* Check to see that it is really the name of a class. */
19649 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19650 && identifier_p (TREE_OPERAND (decl
, 0))
19651 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19652 /* Situations like this:
19654 template <typename T> struct A {
19655 typename T::template X<int>::I i;
19658 are problematic. Is `T::template X<int>' a class-name? The
19659 standard does not seem to be definitive, but there is no other
19660 valid interpretation of the following `::'. Therefore, those
19661 names are considered class-names. */
19663 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19664 if (decl
!= error_mark_node
)
19665 decl
= TYPE_NAME (decl
);
19667 else if (TREE_CODE (decl
) != TYPE_DECL
19668 || TREE_TYPE (decl
) == error_mark_node
19669 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19670 /* In Objective-C 2.0, a classname followed by '.' starts a
19671 dot-syntax expression, and it's not a type-name. */
19672 || (c_dialect_objc ()
19673 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19674 && objc_is_class_name (decl
)))
19675 decl
= error_mark_node
;
19677 if (decl
== error_mark_node
)
19678 cp_parser_error (parser
, "expected class-name");
19679 else if (identifier
&& !parser
->scope
)
19680 maybe_note_name_used_in_class (identifier
, decl
);
19685 /* Parse a class-specifier.
19688 class-head { member-specification [opt] }
19690 Returns the TREE_TYPE representing the class. */
19693 cp_parser_class_specifier_1 (cp_parser
* parser
)
19696 tree attributes
= NULL_TREE
;
19697 bool nested_name_specifier_p
;
19698 unsigned saved_num_template_parameter_lists
;
19699 bool saved_in_function_body
;
19700 unsigned char in_statement
;
19701 bool in_switch_statement_p
;
19702 bool saved_in_unbraced_linkage_specification_p
;
19703 tree old_scope
= NULL_TREE
;
19704 tree scope
= NULL_TREE
;
19705 cp_token
*closing_brace
;
19707 push_deferring_access_checks (dk_no_deferred
);
19709 /* Parse the class-head. */
19710 type
= cp_parser_class_head (parser
,
19711 &nested_name_specifier_p
);
19712 /* If the class-head was a semantic disaster, skip the entire body
19716 cp_parser_skip_to_end_of_block_or_statement (parser
);
19717 pop_deferring_access_checks ();
19718 return error_mark_node
;
19721 /* Look for the `{'. */
19722 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19724 pop_deferring_access_checks ();
19725 return error_mark_node
;
19728 cp_ensure_no_omp_declare_simd (parser
);
19730 /* Issue an error message if type-definitions are forbidden here. */
19731 cp_parser_check_type_definition (parser
);
19732 /* Remember that we are defining one more class. */
19733 ++parser
->num_classes_being_defined
;
19734 /* Inside the class, surrounding template-parameter-lists do not
19736 saved_num_template_parameter_lists
19737 = parser
->num_template_parameter_lists
;
19738 parser
->num_template_parameter_lists
= 0;
19739 /* We are not in a function body. */
19740 saved_in_function_body
= parser
->in_function_body
;
19741 parser
->in_function_body
= false;
19742 /* Or in a loop. */
19743 in_statement
= parser
->in_statement
;
19744 parser
->in_statement
= 0;
19745 /* Or in a switch. */
19746 in_switch_statement_p
= parser
->in_switch_statement_p
;
19747 parser
->in_switch_statement_p
= false;
19748 /* We are not immediately inside an extern "lang" block. */
19749 saved_in_unbraced_linkage_specification_p
19750 = parser
->in_unbraced_linkage_specification_p
;
19751 parser
->in_unbraced_linkage_specification_p
= false;
19753 /* Start the class. */
19754 if (nested_name_specifier_p
)
19756 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19757 old_scope
= push_inner_scope (scope
);
19759 type
= begin_class_definition (type
);
19761 if (type
== error_mark_node
)
19762 /* If the type is erroneous, skip the entire body of the class. */
19763 cp_parser_skip_to_closing_brace (parser
);
19765 /* Parse the member-specification. */
19766 cp_parser_member_specification_opt (parser
);
19768 /* Look for the trailing `}'. */
19769 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19770 /* Look for trailing attributes to apply to this class. */
19771 if (cp_parser_allow_gnu_extensions_p (parser
))
19772 attributes
= cp_parser_gnu_attributes_opt (parser
);
19773 if (type
!= error_mark_node
)
19774 type
= finish_struct (type
, attributes
);
19775 if (nested_name_specifier_p
)
19776 pop_inner_scope (old_scope
, scope
);
19778 /* We've finished a type definition. Check for the common syntax
19779 error of forgetting a semicolon after the definition. We need to
19780 be careful, as we can't just check for not-a-semicolon and be done
19781 with it; the user might have typed:
19783 class X { } c = ...;
19784 class X { } *p = ...;
19786 and so forth. Instead, enumerate all the possible tokens that
19787 might follow this production; if we don't see one of them, then
19788 complain and silently insert the semicolon. */
19790 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19791 bool want_semicolon
= true;
19793 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19794 /* Don't try to parse c++11 attributes here. As per the
19795 grammar, that should be a task for
19796 cp_parser_decl_specifier_seq. */
19797 want_semicolon
= false;
19799 switch (token
->type
)
19802 case CPP_SEMICOLON
:
19805 case CPP_OPEN_PAREN
:
19806 case CPP_CLOSE_PAREN
:
19808 want_semicolon
= false;
19811 /* While it's legal for type qualifiers and storage class
19812 specifiers to follow type definitions in the grammar, only
19813 compiler testsuites contain code like that. Assume that if
19814 we see such code, then what we're really seeing is a case
19818 const <type> var = ...;
19823 static <type> func (...) ...
19825 i.e. the qualifier or specifier applies to the next
19826 declaration. To do so, however, we need to look ahead one
19827 more token to see if *that* token is a type specifier.
19829 This code could be improved to handle:
19832 static const <type> var = ...; */
19834 if (keyword_is_decl_specifier (token
->keyword
))
19836 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19838 /* Handling user-defined types here would be nice, but very
19841 = (lookahead
->type
== CPP_KEYWORD
19842 && keyword_begins_type_specifier (lookahead
->keyword
));
19849 /* If we don't have a type, then something is very wrong and we
19850 shouldn't try to do anything clever. Likewise for not seeing the
19852 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19854 cp_token_position prev
19855 = cp_lexer_previous_token_position (parser
->lexer
);
19856 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19857 location_t loc
= prev_token
->location
;
19859 if (CLASSTYPE_DECLARED_CLASS (type
))
19860 error_at (loc
, "expected %<;%> after class definition");
19861 else if (TREE_CODE (type
) == RECORD_TYPE
)
19862 error_at (loc
, "expected %<;%> after struct definition");
19863 else if (TREE_CODE (type
) == UNION_TYPE
)
19864 error_at (loc
, "expected %<;%> after union definition");
19866 gcc_unreachable ();
19868 /* Unget one token and smash it to look as though we encountered
19869 a semicolon in the input stream. */
19870 cp_lexer_set_token_position (parser
->lexer
, prev
);
19871 token
= cp_lexer_peek_token (parser
->lexer
);
19872 token
->type
= CPP_SEMICOLON
;
19873 token
->keyword
= RID_MAX
;
19877 /* If this class is not itself within the scope of another class,
19878 then we need to parse the bodies of all of the queued function
19879 definitions. Note that the queued functions defined in a class
19880 are not always processed immediately following the
19881 class-specifier for that class. Consider:
19884 struct B { void f() { sizeof (A); } };
19887 If `f' were processed before the processing of `A' were
19888 completed, there would be no way to compute the size of `A'.
19889 Note that the nesting we are interested in here is lexical --
19890 not the semantic nesting given by TYPE_CONTEXT. In particular,
19893 struct A { struct B; };
19894 struct A::B { void f() { } };
19896 there is no need to delay the parsing of `A::B::f'. */
19897 if (--parser
->num_classes_being_defined
== 0)
19900 tree class_type
= NULL_TREE
;
19901 tree pushed_scope
= NULL_TREE
;
19903 cp_default_arg_entry
*e
;
19904 tree save_ccp
, save_ccr
;
19906 /* In a first pass, parse default arguments to the functions.
19907 Then, in a second pass, parse the bodies of the functions.
19908 This two-phased approach handles cases like:
19916 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19919 /* If there are default arguments that have not yet been processed,
19920 take care of them now. */
19921 if (class_type
!= e
->class_type
)
19924 pop_scope (pushed_scope
);
19925 class_type
= e
->class_type
;
19926 pushed_scope
= push_scope (class_type
);
19928 /* Make sure that any template parameters are in scope. */
19929 maybe_begin_member_template_processing (decl
);
19930 /* Parse the default argument expressions. */
19931 cp_parser_late_parsing_default_args (parser
, decl
);
19932 /* Remove any template parameters from the symbol table. */
19933 maybe_end_member_template_processing ();
19935 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19936 /* Now parse any NSDMIs. */
19937 save_ccp
= current_class_ptr
;
19938 save_ccr
= current_class_ref
;
19939 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19941 if (class_type
!= DECL_CONTEXT (decl
))
19944 pop_scope (pushed_scope
);
19945 class_type
= DECL_CONTEXT (decl
);
19946 pushed_scope
= push_scope (class_type
);
19948 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19949 cp_parser_late_parsing_nsdmi (parser
, decl
);
19951 vec_safe_truncate (unparsed_nsdmis
, 0);
19952 current_class_ptr
= save_ccp
;
19953 current_class_ref
= save_ccr
;
19955 pop_scope (pushed_scope
);
19957 /* Now do some post-NSDMI bookkeeping. */
19958 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
19959 after_nsdmi_defaulted_late_checks (class_type
);
19960 vec_safe_truncate (unparsed_classes
, 0);
19961 after_nsdmi_defaulted_late_checks (type
);
19963 /* Now parse the body of the functions. */
19966 /* OpenMP UDRs need to be parsed before all other functions. */
19967 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19968 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19969 cp_parser_late_parsing_for_member (parser
, decl
);
19970 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19971 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19972 cp_parser_late_parsing_for_member (parser
, decl
);
19975 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19976 cp_parser_late_parsing_for_member (parser
, decl
);
19977 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19980 vec_safe_push (unparsed_classes
, type
);
19982 /* Put back any saved access checks. */
19983 pop_deferring_access_checks ();
19985 /* Restore saved state. */
19986 parser
->in_switch_statement_p
= in_switch_statement_p
;
19987 parser
->in_statement
= in_statement
;
19988 parser
->in_function_body
= saved_in_function_body
;
19989 parser
->num_template_parameter_lists
19990 = saved_num_template_parameter_lists
;
19991 parser
->in_unbraced_linkage_specification_p
19992 = saved_in_unbraced_linkage_specification_p
;
19998 cp_parser_class_specifier (cp_parser
* parser
)
20001 timevar_push (TV_PARSE_STRUCT
);
20002 ret
= cp_parser_class_specifier_1 (parser
);
20003 timevar_pop (TV_PARSE_STRUCT
);
20007 /* Parse a class-head.
20010 class-key identifier [opt] base-clause [opt]
20011 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20012 class-key nested-name-specifier [opt] template-id
20015 class-virt-specifier:
20019 class-key attributes identifier [opt] base-clause [opt]
20020 class-key attributes nested-name-specifier identifier base-clause [opt]
20021 class-key attributes nested-name-specifier [opt] template-id
20024 Upon return BASES is initialized to the list of base classes (or
20025 NULL, if there are none) in the same form returned by
20026 cp_parser_base_clause.
20028 Returns the TYPE of the indicated class. Sets
20029 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20030 involving a nested-name-specifier was used, and FALSE otherwise.
20032 Returns error_mark_node if this is not a class-head.
20034 Returns NULL_TREE if the class-head is syntactically valid, but
20035 semantically invalid in a way that means we should skip the entire
20036 body of the class. */
20039 cp_parser_class_head (cp_parser
* parser
,
20040 bool* nested_name_specifier_p
)
20042 tree nested_name_specifier
;
20043 enum tag_types class_key
;
20044 tree id
= NULL_TREE
;
20045 tree type
= NULL_TREE
;
20048 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20049 bool template_id_p
= false;
20050 bool qualified_p
= false;
20051 bool invalid_nested_name_p
= false;
20052 bool invalid_explicit_specialization_p
= false;
20053 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20054 tree pushed_scope
= NULL_TREE
;
20055 unsigned num_templates
;
20056 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20057 /* Assume no nested-name-specifier will be present. */
20058 *nested_name_specifier_p
= false;
20059 /* Assume no template parameter lists will be used in defining the
20062 parser
->colon_corrects_to_scope_p
= false;
20064 /* Look for the class-key. */
20065 class_key
= cp_parser_class_key (parser
);
20066 if (class_key
== none_type
)
20067 return error_mark_node
;
20069 /* Parse the attributes. */
20070 attributes
= cp_parser_attributes_opt (parser
);
20072 /* If the next token is `::', that is invalid -- but sometimes
20073 people do try to write:
20077 Handle this gracefully by accepting the extra qualifier, and then
20078 issuing an error about it later if this really is a
20079 class-head. If it turns out just to be an elaborated type
20080 specifier, remain silent. */
20081 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20082 qualified_p
= true;
20084 push_deferring_access_checks (dk_no_check
);
20086 /* Determine the name of the class. Begin by looking for an
20087 optional nested-name-specifier. */
20088 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20089 nested_name_specifier
20090 = cp_parser_nested_name_specifier_opt (parser
,
20091 /*typename_keyword_p=*/false,
20092 /*check_dependency_p=*/false,
20094 /*is_declaration=*/false);
20095 /* If there was a nested-name-specifier, then there *must* be an
20097 if (nested_name_specifier
)
20099 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20100 /* Although the grammar says `identifier', it really means
20101 `class-name' or `template-name'. You are only allowed to
20102 define a class that has already been declared with this
20105 The proposed resolution for Core Issue 180 says that wherever
20106 you see `class T::X' you should treat `X' as a type-name.
20108 It is OK to define an inaccessible class; for example:
20110 class A { class B; };
20113 We do not know if we will see a class-name, or a
20114 template-name. We look for a class-name first, in case the
20115 class-name is a template-id; if we looked for the
20116 template-name first we would stop after the template-name. */
20117 cp_parser_parse_tentatively (parser
);
20118 type
= cp_parser_class_name (parser
,
20119 /*typename_keyword_p=*/false,
20120 /*template_keyword_p=*/false,
20122 /*check_dependency_p=*/false,
20123 /*class_head_p=*/true,
20124 /*is_declaration=*/false);
20125 /* If that didn't work, ignore the nested-name-specifier. */
20126 if (!cp_parser_parse_definitely (parser
))
20128 invalid_nested_name_p
= true;
20129 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20130 id
= cp_parser_identifier (parser
);
20131 if (id
== error_mark_node
)
20134 /* If we could not find a corresponding TYPE, treat this
20135 declaration like an unqualified declaration. */
20136 if (type
== error_mark_node
)
20137 nested_name_specifier
= NULL_TREE
;
20138 /* Otherwise, count the number of templates used in TYPE and its
20139 containing scopes. */
20144 for (scope
= TREE_TYPE (type
);
20145 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20146 scope
= get_containing_scope (scope
))
20148 && CLASS_TYPE_P (scope
)
20149 && CLASSTYPE_TEMPLATE_INFO (scope
)
20150 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20151 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20152 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20156 /* Otherwise, the identifier is optional. */
20159 /* We don't know whether what comes next is a template-id,
20160 an identifier, or nothing at all. */
20161 cp_parser_parse_tentatively (parser
);
20162 /* Check for a template-id. */
20163 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20164 id
= cp_parser_template_id (parser
,
20165 /*template_keyword_p=*/false,
20166 /*check_dependency_p=*/true,
20168 /*is_declaration=*/true);
20169 /* If that didn't work, it could still be an identifier. */
20170 if (!cp_parser_parse_definitely (parser
))
20172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20174 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20175 id
= cp_parser_identifier (parser
);
20182 template_id_p
= true;
20187 pop_deferring_access_checks ();
20191 cp_parser_check_for_invalid_template_id (parser
, id
,
20193 type_start_token
->location
);
20195 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20197 /* If it's not a `:' or a `{' then we can't really be looking at a
20198 class-head, since a class-head only appears as part of a
20199 class-specifier. We have to detect this situation before calling
20200 xref_tag, since that has irreversible side-effects. */
20201 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20203 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20204 type
= error_mark_node
;
20208 /* At this point, we're going ahead with the class-specifier, even
20209 if some other problem occurs. */
20210 cp_parser_commit_to_tentative_parse (parser
);
20211 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20213 cp_parser_error (parser
,
20214 "cannot specify %<override%> for a class");
20215 type
= error_mark_node
;
20218 /* Issue the error about the overly-qualified name now. */
20221 cp_parser_error (parser
,
20222 "global qualification of class name is invalid");
20223 type
= error_mark_node
;
20226 else if (invalid_nested_name_p
)
20228 cp_parser_error (parser
,
20229 "qualified name does not name a class");
20230 type
= error_mark_node
;
20233 else if (nested_name_specifier
)
20237 /* Reject typedef-names in class heads. */
20238 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20240 error_at (type_start_token
->location
,
20241 "invalid class name in declaration of %qD",
20247 /* Figure out in what scope the declaration is being placed. */
20248 scope
= current_scope ();
20249 /* If that scope does not contain the scope in which the
20250 class was originally declared, the program is invalid. */
20251 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20253 if (at_namespace_scope_p ())
20254 error_at (type_start_token
->location
,
20255 "declaration of %qD in namespace %qD which does not "
20257 type
, scope
, nested_name_specifier
);
20259 error_at (type_start_token
->location
,
20260 "declaration of %qD in %qD which does not enclose %qD",
20261 type
, scope
, nested_name_specifier
);
20267 A declarator-id shall not be qualified except for the
20268 definition of a ... nested class outside of its class
20269 ... [or] the definition or explicit instantiation of a
20270 class member of a namespace outside of its namespace. */
20271 if (scope
== nested_name_specifier
)
20273 permerror (nested_name_specifier_token_start
->location
,
20274 "extra qualification not allowed");
20275 nested_name_specifier
= NULL_TREE
;
20279 /* An explicit-specialization must be preceded by "template <>". If
20280 it is not, try to recover gracefully. */
20281 if (at_namespace_scope_p ()
20282 && parser
->num_template_parameter_lists
== 0
20285 error_at (type_start_token
->location
,
20286 "an explicit specialization must be preceded by %<template <>%>");
20287 invalid_explicit_specialization_p
= true;
20288 /* Take the same action that would have been taken by
20289 cp_parser_explicit_specialization. */
20290 ++parser
->num_template_parameter_lists
;
20291 begin_specialization ();
20293 /* There must be no "return" statements between this point and the
20294 end of this function; set "type "to the correct return value and
20295 use "goto done;" to return. */
20296 /* Make sure that the right number of template parameters were
20298 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20299 type_start_token
->location
,
20300 /*declarator=*/NULL
))
20302 /* If something went wrong, there is no point in even trying to
20303 process the class-definition. */
20308 /* Look up the type. */
20311 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20312 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20313 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20315 error_at (type_start_token
->location
,
20316 "function template %qD redeclared as a class template", id
);
20317 type
= error_mark_node
;
20321 type
= TREE_TYPE (id
);
20322 type
= maybe_process_partial_specialization (type
);
20324 if (nested_name_specifier
)
20325 pushed_scope
= push_scope (nested_name_specifier
);
20327 else if (nested_name_specifier
)
20333 template <typename T> struct S { struct T };
20334 template <typename T> struct S<T>::T { };
20336 we will get a TYPENAME_TYPE when processing the definition of
20337 `S::T'. We need to resolve it to the actual type before we
20338 try to define it. */
20339 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20341 class_type
= resolve_typename_type (TREE_TYPE (type
),
20342 /*only_current_p=*/false);
20343 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20344 type
= TYPE_NAME (class_type
);
20347 cp_parser_error (parser
, "could not resolve typename type");
20348 type
= error_mark_node
;
20352 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20353 == error_mark_node
)
20359 class_type
= current_class_type
;
20360 /* Enter the scope indicated by the nested-name-specifier. */
20361 pushed_scope
= push_scope (nested_name_specifier
);
20362 /* Get the canonical version of this type. */
20363 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20364 /* Call push_template_decl if it seems like we should be defining a
20365 template either from the template headers or the type we're
20366 defining, so that we diagnose both extra and missing headers. */
20367 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20368 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20369 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20371 type
= push_template_decl (type
);
20372 if (type
== error_mark_node
)
20379 type
= TREE_TYPE (type
);
20380 *nested_name_specifier_p
= true;
20382 else /* The name is not a nested name. */
20384 /* If the class was unnamed, create a dummy name. */
20386 id
= make_anon_name ();
20387 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20388 parser
->num_template_parameter_lists
);
20391 /* Indicate whether this class was declared as a `class' or as a
20393 if (TREE_CODE (type
) == RECORD_TYPE
)
20394 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20395 cp_parser_check_class_key (class_key
, type
);
20397 /* If this type was already complete, and we see another definition,
20398 that's an error. */
20399 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20401 error_at (type_start_token
->location
, "redefinition of %q#T",
20403 error_at (type_start_token
->location
, "previous definition of %q+#T",
20408 else if (type
== error_mark_node
)
20413 /* Apply attributes now, before any use of the class as a template
20414 argument in its base list. */
20415 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20416 fixup_attribute_variants (type
);
20419 /* We will have entered the scope containing the class; the names of
20420 base classes should be looked up in that context. For example:
20422 struct A { struct B {}; struct C; };
20423 struct A::C : B {};
20427 /* Get the list of base-classes, if there is one. */
20428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20430 /* PR59482: enter the class scope so that base-specifiers are looked
20434 bases
= cp_parser_base_clause (parser
);
20435 /* PR59482: get out of the previously pushed class scope so that the
20436 subsequent pops pop the right thing. */
20443 /* If we're really defining a class, process the base classes.
20444 If they're invalid, fail. */
20445 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20446 && !xref_basetypes (type
, bases
))
20450 /* Leave the scope given by the nested-name-specifier. We will
20451 enter the class scope itself while processing the members. */
20453 pop_scope (pushed_scope
);
20455 if (invalid_explicit_specialization_p
)
20457 end_specialization ();
20458 --parser
->num_template_parameter_lists
;
20462 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20463 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20464 CLASSTYPE_FINAL (type
) = 1;
20466 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20470 /* Parse a class-key.
20477 Returns the kind of class-key specified, or none_type to indicate
20480 static enum tag_types
20481 cp_parser_class_key (cp_parser
* parser
)
20484 enum tag_types tag_type
;
20486 /* Look for the class-key. */
20487 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20491 /* Check to see if the TOKEN is a class-key. */
20492 tag_type
= cp_parser_token_is_class_key (token
);
20494 cp_parser_error (parser
, "expected class-key");
20498 /* Parse a type-parameter-key.
20500 type-parameter-key:
20506 cp_parser_type_parameter_key (cp_parser
* parser
)
20508 /* Look for the type-parameter-key. */
20509 enum tag_types tag_type
= none_type
;
20510 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20511 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20513 cp_lexer_consume_token (parser
->lexer
);
20514 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20515 /* typename is not allowed in a template template parameter
20516 by the standard until C++1Z. */
20517 pedwarn (token
->location
, OPT_Wpedantic
,
20518 "ISO C++ forbids typename key in template template parameter;"
20519 " use -std=c++1z or -std=gnu++1z");
20522 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20527 /* Parse an (optional) member-specification.
20529 member-specification:
20530 member-declaration member-specification [opt]
20531 access-specifier : member-specification [opt] */
20534 cp_parser_member_specification_opt (cp_parser
* parser
)
20541 /* Peek at the next token. */
20542 token
= cp_lexer_peek_token (parser
->lexer
);
20543 /* If it's a `}', or EOF then we've seen all the members. */
20544 if (token
->type
== CPP_CLOSE_BRACE
20545 || token
->type
== CPP_EOF
20546 || token
->type
== CPP_PRAGMA_EOL
)
20549 /* See if this token is a keyword. */
20550 keyword
= token
->keyword
;
20554 case RID_PROTECTED
:
20556 /* Consume the access-specifier. */
20557 cp_lexer_consume_token (parser
->lexer
);
20558 /* Remember which access-specifier is active. */
20559 current_access_specifier
= token
->u
.value
;
20560 /* Look for the `:'. */
20561 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20565 /* Accept #pragmas at class scope. */
20566 if (token
->type
== CPP_PRAGMA
)
20568 cp_parser_pragma (parser
, pragma_member
);
20572 /* Otherwise, the next construction must be a
20573 member-declaration. */
20574 cp_parser_member_declaration (parser
);
20579 /* Parse a member-declaration.
20581 member-declaration:
20582 decl-specifier-seq [opt] member-declarator-list [opt] ;
20583 function-definition ; [opt]
20584 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20586 template-declaration
20589 member-declarator-list:
20591 member-declarator-list , member-declarator
20594 declarator pure-specifier [opt]
20595 declarator constant-initializer [opt]
20596 identifier [opt] : constant-expression
20600 member-declaration:
20601 __extension__ member-declaration
20604 declarator attributes [opt] pure-specifier [opt]
20605 declarator attributes [opt] constant-initializer [opt]
20606 identifier [opt] attributes [opt] : constant-expression
20610 member-declaration:
20611 static_assert-declaration */
20614 cp_parser_member_declaration (cp_parser
* parser
)
20616 cp_decl_specifier_seq decl_specifiers
;
20617 tree prefix_attributes
;
20619 int declares_class_or_enum
;
20621 cp_token
*token
= NULL
;
20622 cp_token
*decl_spec_token_start
= NULL
;
20623 cp_token
*initializer_token_start
= NULL
;
20624 int saved_pedantic
;
20625 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20627 /* Check for the `__extension__' keyword. */
20628 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20631 cp_parser_member_declaration (parser
);
20632 /* Restore the old value of the PEDANTIC flag. */
20633 pedantic
= saved_pedantic
;
20638 /* Check for a template-declaration. */
20639 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20641 /* An explicit specialization here is an error condition, and we
20642 expect the specialization handler to detect and report this. */
20643 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20644 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20645 cp_parser_explicit_specialization (parser
);
20647 cp_parser_template_declaration (parser
, /*member_p=*/true);
20652 /* Check for a using-declaration. */
20653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20655 if (cxx_dialect
< cxx11
)
20657 /* Parse the using-declaration. */
20658 cp_parser_using_declaration (parser
,
20659 /*access_declaration_p=*/false);
20665 bool alias_decl_expected
;
20666 cp_parser_parse_tentatively (parser
);
20667 decl
= cp_parser_alias_declaration (parser
);
20668 /* Note that if we actually see the '=' token after the
20669 identifier, cp_parser_alias_declaration commits the
20670 tentative parse. In that case, we really expects an
20671 alias-declaration. Otherwise, we expect a using
20673 alias_decl_expected
=
20674 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20675 cp_parser_parse_definitely (parser
);
20677 if (alias_decl_expected
)
20678 finish_member_declaration (decl
);
20680 cp_parser_using_declaration (parser
,
20681 /*access_declaration_p=*/false);
20686 /* Check for @defs. */
20687 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20690 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20691 ivar
= ivar_chains
;
20695 ivar
= TREE_CHAIN (member
);
20696 TREE_CHAIN (member
) = NULL_TREE
;
20697 finish_member_declaration (member
);
20702 /* If the next token is `static_assert' we have a static assertion. */
20703 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20705 cp_parser_static_assert (parser
, /*member_p=*/true);
20709 parser
->colon_corrects_to_scope_p
= false;
20711 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20714 /* Parse the decl-specifier-seq. */
20715 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20716 cp_parser_decl_specifier_seq (parser
,
20717 CP_PARSER_FLAGS_OPTIONAL
,
20719 &declares_class_or_enum
);
20720 /* Check for an invalid type-name. */
20721 if (!decl_specifiers
.any_type_specifiers_p
20722 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20724 /* If there is no declarator, then the decl-specifier-seq should
20726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20728 /* If there was no decl-specifier-seq, and the next token is a
20729 `;', then we have something like:
20735 Each member-declaration shall declare at least one member
20736 name of the class. */
20737 if (!decl_specifiers
.any_specifiers_p
)
20739 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20740 if (!in_system_header_at (token
->location
))
20741 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20747 /* See if this declaration is a friend. */
20748 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20749 /* If there were decl-specifiers, check to see if there was
20750 a class-declaration. */
20751 type
= check_tag_decl (&decl_specifiers
,
20752 /*explicit_type_instantiation_p=*/false);
20753 /* Nested classes have already been added to the class, but
20754 a `friend' needs to be explicitly registered. */
20757 /* If the `friend' keyword was present, the friend must
20758 be introduced with a class-key. */
20759 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20760 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20761 "in C++03 a class-key must be used "
20762 "when declaring a friend");
20765 template <typename T> struct A {
20766 friend struct A<T>::B;
20769 A<T>::B will be represented by a TYPENAME_TYPE, and
20770 therefore not recognized by check_tag_decl. */
20773 type
= decl_specifiers
.type
;
20774 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20775 type
= TREE_TYPE (type
);
20777 if (!type
|| !TYPE_P (type
))
20778 error_at (decl_spec_token_start
->location
,
20779 "friend declaration does not name a class or "
20782 make_friend_class (current_class_type
, type
,
20783 /*complain=*/true);
20785 /* If there is no TYPE, an error message will already have
20787 else if (!type
|| type
== error_mark_node
)
20789 /* An anonymous aggregate has to be handled specially; such
20790 a declaration really declares a data member (with a
20791 particular type), as opposed to a nested class. */
20792 else if (ANON_AGGR_TYPE_P (type
))
20795 if (decl_specifiers
.storage_class
!= sc_none
)
20796 error_at (decl_spec_token_start
->location
,
20797 "a storage class on an anonymous aggregate "
20798 "in class scope is not allowed");
20800 /* Remove constructors and such from TYPE, now that we
20801 know it is an anonymous aggregate. */
20802 fixup_anonymous_aggr (type
);
20803 /* And make the corresponding data member. */
20804 decl
= build_decl (decl_spec_token_start
->location
,
20805 FIELD_DECL
, NULL_TREE
, type
);
20806 /* Add it to the class. */
20807 finish_member_declaration (decl
);
20810 cp_parser_check_access_in_redeclaration
20812 decl_spec_token_start
->location
);
20817 bool assume_semicolon
= false;
20819 /* Clear attributes from the decl_specifiers but keep them
20820 around as prefix attributes that apply them to the entity
20822 prefix_attributes
= decl_specifiers
.attributes
;
20823 decl_specifiers
.attributes
= NULL_TREE
;
20825 /* See if these declarations will be friends. */
20826 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20828 /* Keep going until we hit the `;' at the end of the
20830 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20832 tree attributes
= NULL_TREE
;
20833 tree first_attribute
;
20835 /* Peek at the next token. */
20836 token
= cp_lexer_peek_token (parser
->lexer
);
20838 /* Check for a bitfield declaration. */
20839 if (token
->type
== CPP_COLON
20840 || (token
->type
== CPP_NAME
20841 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20847 /* Get the name of the bitfield. Note that we cannot just
20848 check TOKEN here because it may have been invalidated by
20849 the call to cp_lexer_peek_nth_token above. */
20850 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20851 identifier
= cp_parser_identifier (parser
);
20853 identifier
= NULL_TREE
;
20855 /* Consume the `:' token. */
20856 cp_lexer_consume_token (parser
->lexer
);
20857 /* Get the width of the bitfield. */
20859 = cp_parser_constant_expression (parser
);
20861 /* Look for attributes that apply to the bitfield. */
20862 attributes
= cp_parser_attributes_opt (parser
);
20863 /* Remember which attributes are prefix attributes and
20865 first_attribute
= attributes
;
20866 /* Combine the attributes. */
20867 attributes
= chainon (prefix_attributes
, attributes
);
20869 /* Create the bitfield declaration. */
20870 decl
= grokbitfield (identifier
20871 ? make_id_declarator (NULL_TREE
,
20881 cp_declarator
*declarator
;
20883 tree asm_specification
;
20884 int ctor_dtor_or_conv_p
;
20886 /* Parse the declarator. */
20888 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20889 &ctor_dtor_or_conv_p
,
20890 /*parenthesized_p=*/NULL
,
20894 /* If something went wrong parsing the declarator, make sure
20895 that we at least consume some tokens. */
20896 if (declarator
== cp_error_declarator
)
20898 /* Skip to the end of the statement. */
20899 cp_parser_skip_to_end_of_statement (parser
);
20900 /* If the next token is not a semicolon, that is
20901 probably because we just skipped over the body of
20902 a function. So, we consume a semicolon if
20903 present, but do not issue an error message if it
20905 if (cp_lexer_next_token_is (parser
->lexer
,
20907 cp_lexer_consume_token (parser
->lexer
);
20911 if (declares_class_or_enum
& 2)
20912 cp_parser_check_for_definition_in_return_type
20913 (declarator
, decl_specifiers
.type
,
20914 decl_specifiers
.locations
[ds_type_spec
]);
20916 /* Look for an asm-specification. */
20917 asm_specification
= cp_parser_asm_specification_opt (parser
);
20918 /* Look for attributes that apply to the declaration. */
20919 attributes
= cp_parser_attributes_opt (parser
);
20920 /* Remember which attributes are prefix attributes and
20922 first_attribute
= attributes
;
20923 /* Combine the attributes. */
20924 attributes
= chainon (prefix_attributes
, attributes
);
20926 /* If it's an `=', then we have a constant-initializer or a
20927 pure-specifier. It is not correct to parse the
20928 initializer before registering the member declaration
20929 since the member declaration should be in scope while
20930 its initializer is processed. However, the rest of the
20931 front end does not yet provide an interface that allows
20932 us to handle this correctly. */
20933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20937 A pure-specifier shall be used only in the declaration of
20938 a virtual function.
20940 A member-declarator can contain a constant-initializer
20941 only if it declares a static member of integral or
20944 Therefore, if the DECLARATOR is for a function, we look
20945 for a pure-specifier; otherwise, we look for a
20946 constant-initializer. When we call `grokfield', it will
20947 perform more stringent semantics checks. */
20948 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20949 if (function_declarator_p (declarator
)
20950 || (decl_specifiers
.type
20951 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20952 && declarator
->kind
== cdk_id
20953 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20954 == FUNCTION_TYPE
)))
20955 initializer
= cp_parser_pure_specifier (parser
);
20956 else if (decl_specifiers
.storage_class
!= sc_static
)
20957 initializer
= cp_parser_save_nsdmi (parser
);
20958 else if (cxx_dialect
>= cxx11
)
20961 /* Don't require a constant rvalue in C++11, since we
20962 might want a reference constant. We'll enforce
20963 constancy later. */
20964 cp_lexer_consume_token (parser
->lexer
);
20965 /* Parse the initializer. */
20966 initializer
= cp_parser_initializer_clause (parser
,
20970 /* Parse the initializer. */
20971 initializer
= cp_parser_constant_initializer (parser
);
20973 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20974 && !function_declarator_p (declarator
))
20977 if (decl_specifiers
.storage_class
!= sc_static
)
20978 initializer
= cp_parser_save_nsdmi (parser
);
20980 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20982 /* Otherwise, there is no initializer. */
20984 initializer
= NULL_TREE
;
20986 /* See if we are probably looking at a function
20987 definition. We are certainly not looking at a
20988 member-declarator. Calling `grokfield' has
20989 side-effects, so we must not do it unless we are sure
20990 that we are looking at a member-declarator. */
20991 if (cp_parser_token_starts_function_definition_p
20992 (cp_lexer_peek_token (parser
->lexer
)))
20994 /* The grammar does not allow a pure-specifier to be
20995 used when a member function is defined. (It is
20996 possible that this fact is an oversight in the
20997 standard, since a pure function may be defined
20998 outside of the class-specifier. */
20999 if (initializer
&& initializer_token_start
)
21000 error_at (initializer_token_start
->location
,
21001 "pure-specifier on function-definition");
21002 decl
= cp_parser_save_member_function_body (parser
,
21006 if (parser
->fully_implicit_function_template_p
)
21007 decl
= finish_fully_implicit_template (parser
, decl
);
21008 /* If the member was not a friend, declare it here. */
21010 finish_member_declaration (decl
);
21011 /* Peek at the next token. */
21012 token
= cp_lexer_peek_token (parser
->lexer
);
21013 /* If the next token is a semicolon, consume it. */
21014 if (token
->type
== CPP_SEMICOLON
)
21015 cp_lexer_consume_token (parser
->lexer
);
21019 if (declarator
->kind
== cdk_function
)
21020 declarator
->id_loc
= token
->location
;
21021 /* Create the declaration. */
21022 decl
= grokfield (declarator
, &decl_specifiers
,
21023 initializer
, /*init_const_expr_p=*/true,
21024 asm_specification
, attributes
);
21025 if (parser
->fully_implicit_function_template_p
)
21028 finish_fully_implicit_template (parser
, 0);
21030 decl
= finish_fully_implicit_template (parser
, decl
);
21034 cp_finalize_omp_declare_simd (parser
, decl
);
21036 /* Reset PREFIX_ATTRIBUTES. */
21037 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21038 attributes
= TREE_CHAIN (attributes
);
21040 TREE_CHAIN (attributes
) = NULL_TREE
;
21042 /* If there is any qualification still in effect, clear it
21043 now; we will be starting fresh with the next declarator. */
21044 parser
->scope
= NULL_TREE
;
21045 parser
->qualifying_scope
= NULL_TREE
;
21046 parser
->object_scope
= NULL_TREE
;
21047 /* If it's a `,', then there are more declarators. */
21048 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21050 cp_lexer_consume_token (parser
->lexer
);
21051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21053 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21054 error_at (token
->location
,
21055 "stray %<,%> at end of member declaration");
21058 /* If the next token isn't a `;', then we have a parse error. */
21059 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21062 /* The next token might be a ways away from where the
21063 actual semicolon is missing. Find the previous token
21064 and use that for our error position. */
21065 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21066 error_at (token
->location
,
21067 "expected %<;%> at end of member declaration");
21069 /* Assume that the user meant to provide a semicolon. If
21070 we were to cp_parser_skip_to_end_of_statement, we might
21071 skip to a semicolon inside a member function definition
21072 and issue nonsensical error messages. */
21073 assume_semicolon
= true;
21078 /* Add DECL to the list of members. */
21080 /* Explicitly include, eg, NSDMIs, for better error
21081 recovery (c++/58650). */
21082 || !DECL_DECLARES_FUNCTION_P (decl
))
21083 finish_member_declaration (decl
);
21085 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21086 cp_parser_save_default_args (parser
, decl
);
21087 else if (TREE_CODE (decl
) == FIELD_DECL
21088 && !DECL_C_BIT_FIELD (decl
)
21089 && DECL_INITIAL (decl
))
21090 /* Add DECL to the queue of NSDMI to be parsed later. */
21091 vec_safe_push (unparsed_nsdmis
, decl
);
21094 if (assume_semicolon
)
21099 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21101 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21104 /* Parse a pure-specifier.
21109 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21110 Otherwise, ERROR_MARK_NODE is returned. */
21113 cp_parser_pure_specifier (cp_parser
* parser
)
21117 /* Look for the `=' token. */
21118 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21119 return error_mark_node
;
21120 /* Look for the `0' token. */
21121 token
= cp_lexer_peek_token (parser
->lexer
);
21123 if (token
->type
== CPP_EOF
21124 || token
->type
== CPP_PRAGMA_EOL
)
21125 return error_mark_node
;
21127 cp_lexer_consume_token (parser
->lexer
);
21129 /* Accept = default or = delete in c++0x mode. */
21130 if (token
->keyword
== RID_DEFAULT
21131 || token
->keyword
== RID_DELETE
)
21133 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21134 return token
->u
.value
;
21137 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21138 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21140 cp_parser_error (parser
,
21141 "invalid pure specifier (only %<= 0%> is allowed)");
21142 cp_parser_skip_to_end_of_statement (parser
);
21143 return error_mark_node
;
21145 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21147 error_at (token
->location
, "templates may not be %<virtual%>");
21148 return error_mark_node
;
21151 return integer_zero_node
;
21154 /* Parse a constant-initializer.
21156 constant-initializer:
21157 = constant-expression
21159 Returns a representation of the constant-expression. */
21162 cp_parser_constant_initializer (cp_parser
* parser
)
21164 /* Look for the `=' token. */
21165 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21166 return error_mark_node
;
21168 /* It is invalid to write:
21170 struct S { static const int i = { 7 }; };
21173 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21175 cp_parser_error (parser
,
21176 "a brace-enclosed initializer is not allowed here");
21177 /* Consume the opening brace. */
21178 cp_lexer_consume_token (parser
->lexer
);
21179 /* Skip the initializer. */
21180 cp_parser_skip_to_closing_brace (parser
);
21181 /* Look for the trailing `}'. */
21182 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21184 return error_mark_node
;
21187 return cp_parser_constant_expression (parser
);
21190 /* Derived classes [gram.class.derived] */
21192 /* Parse a base-clause.
21195 : base-specifier-list
21197 base-specifier-list:
21198 base-specifier ... [opt]
21199 base-specifier-list , base-specifier ... [opt]
21201 Returns a TREE_LIST representing the base-classes, in the order in
21202 which they were declared. The representation of each node is as
21203 described by cp_parser_base_specifier.
21205 In the case that no bases are specified, this function will return
21206 NULL_TREE, not ERROR_MARK_NODE. */
21209 cp_parser_base_clause (cp_parser
* parser
)
21211 tree bases
= NULL_TREE
;
21213 /* Look for the `:' that begins the list. */
21214 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21216 /* Scan the base-specifier-list. */
21221 bool pack_expansion_p
= false;
21223 /* Look for the base-specifier. */
21224 base
= cp_parser_base_specifier (parser
);
21225 /* Look for the (optional) ellipsis. */
21226 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21228 /* Consume the `...'. */
21229 cp_lexer_consume_token (parser
->lexer
);
21231 pack_expansion_p
= true;
21234 /* Add BASE to the front of the list. */
21235 if (base
&& base
!= error_mark_node
)
21237 if (pack_expansion_p
)
21238 /* Make this a pack expansion type. */
21239 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21241 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21243 TREE_CHAIN (base
) = bases
;
21247 /* Peek at the next token. */
21248 token
= cp_lexer_peek_token (parser
->lexer
);
21249 /* If it's not a comma, then the list is complete. */
21250 if (token
->type
!= CPP_COMMA
)
21252 /* Consume the `,'. */
21253 cp_lexer_consume_token (parser
->lexer
);
21256 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21257 base class had a qualified name. However, the next name that
21258 appears is certainly not qualified. */
21259 parser
->scope
= NULL_TREE
;
21260 parser
->qualifying_scope
= NULL_TREE
;
21261 parser
->object_scope
= NULL_TREE
;
21263 return nreverse (bases
);
21266 /* Parse a base-specifier.
21269 :: [opt] nested-name-specifier [opt] class-name
21270 virtual access-specifier [opt] :: [opt] nested-name-specifier
21272 access-specifier virtual [opt] :: [opt] nested-name-specifier
21275 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21276 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21277 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21278 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21281 cp_parser_base_specifier (cp_parser
* parser
)
21285 bool virtual_p
= false;
21286 bool duplicate_virtual_error_issued_p
= false;
21287 bool duplicate_access_error_issued_p
= false;
21288 bool class_scope_p
, template_p
;
21289 tree access
= access_default_node
;
21292 /* Process the optional `virtual' and `access-specifier'. */
21295 /* Peek at the next token. */
21296 token
= cp_lexer_peek_token (parser
->lexer
);
21297 /* Process `virtual'. */
21298 switch (token
->keyword
)
21301 /* If `virtual' appears more than once, issue an error. */
21302 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21304 cp_parser_error (parser
,
21305 "%<virtual%> specified more than once in base-specified");
21306 duplicate_virtual_error_issued_p
= true;
21311 /* Consume the `virtual' token. */
21312 cp_lexer_consume_token (parser
->lexer
);
21317 case RID_PROTECTED
:
21319 /* If more than one access specifier appears, issue an
21321 if (access
!= access_default_node
21322 && !duplicate_access_error_issued_p
)
21324 cp_parser_error (parser
,
21325 "more than one access specifier in base-specified");
21326 duplicate_access_error_issued_p
= true;
21329 access
= ridpointers
[(int) token
->keyword
];
21331 /* Consume the access-specifier. */
21332 cp_lexer_consume_token (parser
->lexer
);
21341 /* It is not uncommon to see programs mechanically, erroneously, use
21342 the 'typename' keyword to denote (dependent) qualified types
21343 as base classes. */
21344 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21346 token
= cp_lexer_peek_token (parser
->lexer
);
21347 if (!processing_template_decl
)
21348 error_at (token
->location
,
21349 "keyword %<typename%> not allowed outside of templates");
21351 error_at (token
->location
,
21352 "keyword %<typename%> not allowed in this context "
21353 "(the base class is implicitly a type)");
21354 cp_lexer_consume_token (parser
->lexer
);
21357 /* Look for the optional `::' operator. */
21358 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21359 /* Look for the nested-name-specifier. The simplest way to
21364 The keyword `typename' is not permitted in a base-specifier or
21365 mem-initializer; in these contexts a qualified name that
21366 depends on a template-parameter is implicitly assumed to be a
21369 is to pretend that we have seen the `typename' keyword at this
21371 cp_parser_nested_name_specifier_opt (parser
,
21372 /*typename_keyword_p=*/true,
21373 /*check_dependency_p=*/true,
21375 /*is_declaration=*/true);
21376 /* If the base class is given by a qualified name, assume that names
21377 we see are type names or templates, as appropriate. */
21378 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21379 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21382 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21383 /* DR 950 allows decltype as a base-specifier. */
21384 type
= cp_parser_decltype (parser
);
21387 /* Otherwise, look for the class-name. */
21388 type
= cp_parser_class_name (parser
,
21392 /*check_dependency_p=*/true,
21393 /*class_head_p=*/false,
21394 /*is_declaration=*/true);
21395 type
= TREE_TYPE (type
);
21398 if (type
== error_mark_node
)
21399 return error_mark_node
;
21401 return finish_base_specifier (type
, access
, virtual_p
);
21404 /* Exception handling [gram.exception] */
21406 /* Parse an (optional) noexcept-specification.
21408 noexcept-specification:
21409 noexcept ( constant-expression ) [opt]
21411 If no noexcept-specification is present, returns NULL_TREE.
21412 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21413 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21414 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21415 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21416 in which case a boolean condition is returned instead. */
21419 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21420 bool require_constexpr
,
21421 bool* consumed_expr
,
21425 const char *saved_message
;
21427 /* Peek at the next token. */
21428 token
= cp_lexer_peek_token (parser
->lexer
);
21430 /* Is it a noexcept-specification? */
21431 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21434 cp_lexer_consume_token (parser
->lexer
);
21436 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21438 cp_lexer_consume_token (parser
->lexer
);
21440 if (require_constexpr
)
21442 /* Types may not be defined in an exception-specification. */
21443 saved_message
= parser
->type_definition_forbidden_message
;
21444 parser
->type_definition_forbidden_message
21445 = G_("types may not be defined in an exception-specification");
21447 expr
= cp_parser_constant_expression (parser
);
21449 /* Restore the saved message. */
21450 parser
->type_definition_forbidden_message
= saved_message
;
21454 expr
= cp_parser_expression (parser
);
21455 *consumed_expr
= true;
21458 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21462 expr
= boolean_true_node
;
21463 if (!require_constexpr
)
21464 *consumed_expr
= false;
21467 /* We cannot build a noexcept-spec right away because this will check
21468 that expr is a constexpr. */
21470 return build_noexcept_spec (expr
, tf_warning_or_error
);
21478 /* Parse an (optional) exception-specification.
21480 exception-specification:
21481 throw ( type-id-list [opt] )
21483 Returns a TREE_LIST representing the exception-specification. The
21484 TREE_VALUE of each node is a type. */
21487 cp_parser_exception_specification_opt (cp_parser
* parser
)
21491 const char *saved_message
;
21493 /* Peek at the next token. */
21494 token
= cp_lexer_peek_token (parser
->lexer
);
21496 /* Is it a noexcept-specification? */
21497 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21499 if (type_id_list
!= NULL_TREE
)
21500 return type_id_list
;
21502 /* If it's not `throw', then there's no exception-specification. */
21503 if (!cp_parser_is_keyword (token
, RID_THROW
))
21507 /* Enable this once a lot of code has transitioned to noexcept? */
21508 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21509 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21510 "deprecated in C++0x; use %<noexcept%> instead");
21513 /* Consume the `throw'. */
21514 cp_lexer_consume_token (parser
->lexer
);
21516 /* Look for the `('. */
21517 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21519 /* Peek at the next token. */
21520 token
= cp_lexer_peek_token (parser
->lexer
);
21521 /* If it's not a `)', then there is a type-id-list. */
21522 if (token
->type
!= CPP_CLOSE_PAREN
)
21524 /* Types may not be defined in an exception-specification. */
21525 saved_message
= parser
->type_definition_forbidden_message
;
21526 parser
->type_definition_forbidden_message
21527 = G_("types may not be defined in an exception-specification");
21528 /* Parse the type-id-list. */
21529 type_id_list
= cp_parser_type_id_list (parser
);
21530 /* Restore the saved message. */
21531 parser
->type_definition_forbidden_message
= saved_message
;
21534 type_id_list
= empty_except_spec
;
21536 /* Look for the `)'. */
21537 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21539 return type_id_list
;
21542 /* Parse an (optional) type-id-list.
21546 type-id-list , type-id ... [opt]
21548 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21549 in the order that the types were presented. */
21552 cp_parser_type_id_list (cp_parser
* parser
)
21554 tree types
= NULL_TREE
;
21561 /* Get the next type-id. */
21562 type
= cp_parser_type_id (parser
);
21563 /* Parse the optional ellipsis. */
21564 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21566 /* Consume the `...'. */
21567 cp_lexer_consume_token (parser
->lexer
);
21569 /* Turn the type into a pack expansion expression. */
21570 type
= make_pack_expansion (type
);
21572 /* Add it to the list. */
21573 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21574 /* Peek at the next token. */
21575 token
= cp_lexer_peek_token (parser
->lexer
);
21576 /* If it is not a `,', we are done. */
21577 if (token
->type
!= CPP_COMMA
)
21579 /* Consume the `,'. */
21580 cp_lexer_consume_token (parser
->lexer
);
21583 return nreverse (types
);
21586 /* Parse a try-block.
21589 try compound-statement handler-seq */
21592 cp_parser_try_block (cp_parser
* parser
)
21596 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21597 if (parser
->in_function_body
21598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21599 error ("%<try%> in %<constexpr%> function");
21601 try_block
= begin_try_block ();
21602 cp_parser_compound_statement (parser
, NULL
, true, false);
21603 finish_try_block (try_block
);
21604 cp_parser_handler_seq (parser
);
21605 finish_handler_sequence (try_block
);
21610 /* Parse a function-try-block.
21612 function-try-block:
21613 try ctor-initializer [opt] function-body handler-seq */
21616 cp_parser_function_try_block (cp_parser
* parser
)
21618 tree compound_stmt
;
21620 bool ctor_initializer_p
;
21622 /* Look for the `try' keyword. */
21623 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21625 /* Let the rest of the front end know where we are. */
21626 try_block
= begin_function_try_block (&compound_stmt
);
21627 /* Parse the function-body. */
21628 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21629 (parser
, /*in_function_try_block=*/true);
21630 /* We're done with the `try' part. */
21631 finish_function_try_block (try_block
);
21632 /* Parse the handlers. */
21633 cp_parser_handler_seq (parser
);
21634 /* We're done with the handlers. */
21635 finish_function_handler_sequence (try_block
, compound_stmt
);
21637 return ctor_initializer_p
;
21640 /* Parse a handler-seq.
21643 handler handler-seq [opt] */
21646 cp_parser_handler_seq (cp_parser
* parser
)
21652 /* Parse the handler. */
21653 cp_parser_handler (parser
);
21654 /* Peek at the next token. */
21655 token
= cp_lexer_peek_token (parser
->lexer
);
21656 /* If it's not `catch' then there are no more handlers. */
21657 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21662 /* Parse a handler.
21665 catch ( exception-declaration ) compound-statement */
21668 cp_parser_handler (cp_parser
* parser
)
21673 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21674 handler
= begin_handler ();
21675 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21676 declaration
= cp_parser_exception_declaration (parser
);
21677 finish_handler_parms (declaration
, handler
);
21678 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21679 cp_parser_compound_statement (parser
, NULL
, false, false);
21680 finish_handler (handler
);
21683 /* Parse an exception-declaration.
21685 exception-declaration:
21686 type-specifier-seq declarator
21687 type-specifier-seq abstract-declarator
21691 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21692 ellipsis variant is used. */
21695 cp_parser_exception_declaration (cp_parser
* parser
)
21697 cp_decl_specifier_seq type_specifiers
;
21698 cp_declarator
*declarator
;
21699 const char *saved_message
;
21701 /* If it's an ellipsis, it's easy to handle. */
21702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21704 /* Consume the `...' token. */
21705 cp_lexer_consume_token (parser
->lexer
);
21709 /* Types may not be defined in exception-declarations. */
21710 saved_message
= parser
->type_definition_forbidden_message
;
21711 parser
->type_definition_forbidden_message
21712 = G_("types may not be defined in exception-declarations");
21714 /* Parse the type-specifier-seq. */
21715 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21716 /*is_trailing_return=*/false,
21718 /* If it's a `)', then there is no declarator. */
21719 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21722 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21723 /*ctor_dtor_or_conv_p=*/NULL
,
21724 /*parenthesized_p=*/NULL
,
21725 /*member_p=*/false,
21726 /*friend_p=*/false);
21728 /* Restore the saved message. */
21729 parser
->type_definition_forbidden_message
= saved_message
;
21731 if (!type_specifiers
.any_specifiers_p
)
21732 return error_mark_node
;
21734 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21737 /* Parse a throw-expression.
21740 throw assignment-expression [opt]
21742 Returns a THROW_EXPR representing the throw-expression. */
21745 cp_parser_throw_expression (cp_parser
* parser
)
21750 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21751 token
= cp_lexer_peek_token (parser
->lexer
);
21752 /* Figure out whether or not there is an assignment-expression
21753 following the "throw" keyword. */
21754 if (token
->type
== CPP_COMMA
21755 || token
->type
== CPP_SEMICOLON
21756 || token
->type
== CPP_CLOSE_PAREN
21757 || token
->type
== CPP_CLOSE_SQUARE
21758 || token
->type
== CPP_CLOSE_BRACE
21759 || token
->type
== CPP_COLON
)
21760 expression
= NULL_TREE
;
21762 expression
= cp_parser_assignment_expression (parser
);
21764 return build_throw (expression
);
21767 /* GNU Extensions */
21769 /* Parse an (optional) asm-specification.
21772 asm ( string-literal )
21774 If the asm-specification is present, returns a STRING_CST
21775 corresponding to the string-literal. Otherwise, returns
21779 cp_parser_asm_specification_opt (cp_parser
* parser
)
21782 tree asm_specification
;
21784 /* Peek at the next token. */
21785 token
= cp_lexer_peek_token (parser
->lexer
);
21786 /* If the next token isn't the `asm' keyword, then there's no
21787 asm-specification. */
21788 if (!cp_parser_is_keyword (token
, RID_ASM
))
21791 /* Consume the `asm' token. */
21792 cp_lexer_consume_token (parser
->lexer
);
21793 /* Look for the `('. */
21794 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21796 /* Look for the string-literal. */
21797 asm_specification
= cp_parser_string_literal (parser
, false, false);
21799 /* Look for the `)'. */
21800 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21802 return asm_specification
;
21805 /* Parse an asm-operand-list.
21809 asm-operand-list , asm-operand
21812 string-literal ( expression )
21813 [ string-literal ] string-literal ( expression )
21815 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21816 each node is the expression. The TREE_PURPOSE is itself a
21817 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21818 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21819 is a STRING_CST for the string literal before the parenthesis. Returns
21820 ERROR_MARK_NODE if any of the operands are invalid. */
21823 cp_parser_asm_operand_list (cp_parser
* parser
)
21825 tree asm_operands
= NULL_TREE
;
21826 bool invalid_operands
= false;
21830 tree string_literal
;
21834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21836 /* Consume the `[' token. */
21837 cp_lexer_consume_token (parser
->lexer
);
21838 /* Read the operand name. */
21839 name
= cp_parser_identifier (parser
);
21840 if (name
!= error_mark_node
)
21841 name
= build_string (IDENTIFIER_LENGTH (name
),
21842 IDENTIFIER_POINTER (name
));
21843 /* Look for the closing `]'. */
21844 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21848 /* Look for the string-literal. */
21849 string_literal
= cp_parser_string_literal (parser
, false, false);
21851 /* Look for the `('. */
21852 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21853 /* Parse the expression. */
21854 expression
= cp_parser_expression (parser
);
21855 /* Look for the `)'. */
21856 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21858 if (name
== error_mark_node
21859 || string_literal
== error_mark_node
21860 || expression
== error_mark_node
)
21861 invalid_operands
= true;
21863 /* Add this operand to the list. */
21864 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21867 /* If the next token is not a `,', there are no more
21869 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21871 /* Consume the `,'. */
21872 cp_lexer_consume_token (parser
->lexer
);
21875 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21878 /* Parse an asm-clobber-list.
21882 asm-clobber-list , string-literal
21884 Returns a TREE_LIST, indicating the clobbers in the order that they
21885 appeared. The TREE_VALUE of each node is a STRING_CST. */
21888 cp_parser_asm_clobber_list (cp_parser
* parser
)
21890 tree clobbers
= NULL_TREE
;
21894 tree string_literal
;
21896 /* Look for the string literal. */
21897 string_literal
= cp_parser_string_literal (parser
, false, false);
21898 /* Add it to the list. */
21899 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21900 /* If the next token is not a `,', then the list is
21902 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21904 /* Consume the `,' token. */
21905 cp_lexer_consume_token (parser
->lexer
);
21911 /* Parse an asm-label-list.
21915 asm-label-list , identifier
21917 Returns a TREE_LIST, indicating the labels in the order that they
21918 appeared. The TREE_VALUE of each node is a label. */
21921 cp_parser_asm_label_list (cp_parser
* parser
)
21923 tree labels
= NULL_TREE
;
21927 tree identifier
, label
, name
;
21929 /* Look for the identifier. */
21930 identifier
= cp_parser_identifier (parser
);
21931 if (!error_operand_p (identifier
))
21933 label
= lookup_label (identifier
);
21934 if (TREE_CODE (label
) == LABEL_DECL
)
21936 TREE_USED (label
) = 1;
21937 check_goto (label
);
21938 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21939 IDENTIFIER_POINTER (identifier
));
21940 labels
= tree_cons (name
, label
, labels
);
21943 /* If the next token is not a `,', then the list is
21945 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21947 /* Consume the `,' token. */
21948 cp_lexer_consume_token (parser
->lexer
);
21951 return nreverse (labels
);
21954 /* Return TRUE iff the next tokens in the stream are possibly the
21955 beginning of a GNU extension attribute. */
21958 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21960 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21963 /* Return TRUE iff the next tokens in the stream are possibly the
21964 beginning of a standard C++-11 attribute specifier. */
21967 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21969 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21972 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21973 beginning of a standard C++-11 attribute specifier. */
21976 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21978 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21980 return (cxx_dialect
>= cxx11
21981 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21982 || (token
->type
== CPP_OPEN_SQUARE
21983 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21984 && token
->type
== CPP_OPEN_SQUARE
)));
21987 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21988 beginning of a GNU extension attribute. */
21991 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21993 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21995 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21998 /* Return true iff the next tokens can be the beginning of either a
21999 GNU attribute list, or a standard C++11 attribute sequence. */
22002 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
22004 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
22005 || cp_next_tokens_can_be_std_attribute_p (parser
));
22008 /* Return true iff the next Nth tokens can be the beginning of either
22009 a GNU attribute list, or a standard C++11 attribute sequence. */
22012 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
22014 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
22015 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
22018 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22019 of GNU attributes, or return NULL. */
22022 cp_parser_attributes_opt (cp_parser
*parser
)
22024 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
22025 return cp_parser_gnu_attributes_opt (parser
);
22026 return cp_parser_std_attribute_spec_seq (parser
);
22029 #define CILK_SIMD_FN_CLAUSE_MASK \
22030 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22031 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22032 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22033 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22034 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22036 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22037 vector [(<clauses>)] */
22040 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22042 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22043 cp_token
*token
= v_token
;
22046 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22047 parser
->cilk_simd_fn_info
->error_seen
= false;
22048 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22049 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22051 int paren_scope
= 0;
22052 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22054 cp_lexer_consume_token (parser
->lexer
);
22055 v_token
= cp_lexer_peek_token (parser
->lexer
);
22058 while (paren_scope
> 0)
22060 token
= cp_lexer_peek_token (parser
->lexer
);
22061 if (token
->type
== CPP_OPEN_PAREN
)
22063 else if (token
->type
== CPP_CLOSE_PAREN
)
22065 /* Do not push the last ')' */
22066 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22067 cp_lexer_consume_token (parser
->lexer
);
22070 token
->type
= CPP_PRAGMA_EOL
;
22071 parser
->lexer
->next_token
= token
;
22072 cp_lexer_consume_token (parser
->lexer
);
22074 struct cp_token_cache
*cp
22075 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22076 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22079 /* Parse an (optional) series of attributes.
22082 attributes attribute
22085 __attribute__ (( attribute-list [opt] ))
22087 The return value is as for cp_parser_gnu_attribute_list. */
22090 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22092 tree attributes
= NULL_TREE
;
22097 tree attribute_list
;
22100 /* Peek at the next token. */
22101 token
= cp_lexer_peek_token (parser
->lexer
);
22102 /* If it's not `__attribute__', then we're done. */
22103 if (token
->keyword
!= RID_ATTRIBUTE
)
22106 /* Consume the `__attribute__' keyword. */
22107 cp_lexer_consume_token (parser
->lexer
);
22108 /* Look for the two `(' tokens. */
22109 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22110 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22112 /* Peek at the next token. */
22113 token
= cp_lexer_peek_token (parser
->lexer
);
22114 if (token
->type
!= CPP_CLOSE_PAREN
)
22115 /* Parse the attribute-list. */
22116 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22118 /* If the next token is a `)', then there is no attribute
22120 attribute_list
= NULL
;
22122 /* Look for the two `)' tokens. */
22123 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22125 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22128 cp_parser_skip_to_end_of_statement (parser
);
22130 /* Add these new attributes to the list. */
22131 attributes
= chainon (attributes
, attribute_list
);
22137 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22138 "__vector" or "__vector__." */
22141 is_cilkplus_vector_p (tree name
)
22143 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22148 /* Parse a GNU attribute-list.
22152 attribute-list , attribute
22156 identifier ( identifier )
22157 identifier ( identifier , expression-list )
22158 identifier ( expression-list )
22160 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22161 to an attribute. The TREE_PURPOSE of each node is the identifier
22162 indicating which attribute is in use. The TREE_VALUE represents
22163 the arguments, if any. */
22166 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22168 tree attribute_list
= NULL_TREE
;
22169 bool save_translate_strings_p
= parser
->translate_strings_p
;
22171 parser
->translate_strings_p
= false;
22178 /* Look for the identifier. We also allow keywords here; for
22179 example `__attribute__ ((const))' is legal. */
22180 token
= cp_lexer_peek_token (parser
->lexer
);
22181 if (token
->type
== CPP_NAME
22182 || token
->type
== CPP_KEYWORD
)
22184 tree arguments
= NULL_TREE
;
22186 /* Consume the token, but save it since we need it for the
22187 SIMD enabled function parsing. */
22188 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22190 /* Save away the identifier that indicates which attribute
22192 identifier
= (token
->type
== CPP_KEYWORD
)
22193 /* For keywords, use the canonical spelling, not the
22194 parsed identifier. */
22195 ? ridpointers
[(int) token
->keyword
]
22196 : id_token
->u
.value
;
22198 attribute
= build_tree_list (identifier
, NULL_TREE
);
22200 /* Peek at the next token. */
22201 token
= cp_lexer_peek_token (parser
->lexer
);
22202 /* If it's an `(', then parse the attribute arguments. */
22203 if (token
->type
== CPP_OPEN_PAREN
)
22205 vec
<tree
, va_gc
> *vec
;
22206 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22207 ? id_attr
: normal_attr
);
22208 if (is_cilkplus_vector_p (identifier
))
22210 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22214 vec
= cp_parser_parenthesized_expression_list
22215 (parser
, attr_flag
, /*cast_p=*/false,
22216 /*allow_expansion_p=*/false,
22217 /*non_constant_p=*/NULL
);
22219 arguments
= error_mark_node
;
22222 arguments
= build_tree_list_vec (vec
);
22223 release_tree_vector (vec
);
22225 /* Save the arguments away. */
22226 TREE_VALUE (attribute
) = arguments
;
22228 else if (is_cilkplus_vector_p (identifier
))
22230 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22234 if (arguments
!= error_mark_node
)
22236 /* Add this attribute to the list. */
22237 TREE_CHAIN (attribute
) = attribute_list
;
22238 attribute_list
= attribute
;
22241 token
= cp_lexer_peek_token (parser
->lexer
);
22243 /* Now, look for more attributes. If the next token isn't a
22244 `,', we're done. */
22245 if (token
->type
!= CPP_COMMA
)
22248 /* Consume the comma and keep going. */
22249 cp_lexer_consume_token (parser
->lexer
);
22251 parser
->translate_strings_p
= save_translate_strings_p
;
22253 /* We built up the list in reverse order. */
22254 return nreverse (attribute_list
);
22257 /* Parse a standard C++11 attribute.
22259 The returned representation is a TREE_LIST which TREE_PURPOSE is
22260 the scoped name of the attribute, and the TREE_VALUE is its
22263 Note that the scoped name of the attribute is itself a TREE_LIST
22264 which TREE_PURPOSE is the namespace of the attribute, and
22265 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22266 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22267 and which TREE_PURPOSE is directly the attribute name.
22269 Clients of the attribute code should use get_attribute_namespace
22270 and get_attribute_name to get the actual namespace and name of
22271 attributes, regardless of their being GNU or C++11 attributes.
22274 attribute-token attribute-argument-clause [opt]
22278 attribute-scoped-token
22280 attribute-scoped-token:
22281 attribute-namespace :: identifier
22283 attribute-namespace:
22286 attribute-argument-clause:
22287 ( balanced-token-seq )
22289 balanced-token-seq:
22290 balanced-token [opt]
22291 balanced-token-seq balanced-token
22294 ( balanced-token-seq )
22295 [ balanced-token-seq ]
22296 { balanced-token-seq }. */
22299 cp_parser_std_attribute (cp_parser
*parser
)
22301 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22304 /* First, parse name of the the attribute, a.k.a
22305 attribute-token. */
22307 token
= cp_lexer_peek_token (parser
->lexer
);
22308 if (token
->type
== CPP_NAME
)
22309 attr_id
= token
->u
.value
;
22310 else if (token
->type
== CPP_KEYWORD
)
22311 attr_id
= ridpointers
[(int) token
->keyword
];
22312 else if (token
->flags
& NAMED_OP
)
22313 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22315 if (attr_id
== NULL_TREE
)
22318 cp_lexer_consume_token (parser
->lexer
);
22320 token
= cp_lexer_peek_token (parser
->lexer
);
22321 if (token
->type
== CPP_SCOPE
)
22323 /* We are seeing a scoped attribute token. */
22325 cp_lexer_consume_token (parser
->lexer
);
22328 token
= cp_lexer_consume_token (parser
->lexer
);
22329 if (token
->type
== CPP_NAME
)
22330 attr_id
= token
->u
.value
;
22331 else if (token
->type
== CPP_KEYWORD
)
22332 attr_id
= ridpointers
[(int) token
->keyword
];
22335 error_at (token
->location
,
22336 "expected an identifier for the attribute name");
22337 return error_mark_node
;
22339 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22341 token
= cp_lexer_peek_token (parser
->lexer
);
22345 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22347 /* C++11 noreturn attribute is equivalent to GNU's. */
22348 if (is_attribute_p ("noreturn", attr_id
))
22349 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22350 /* C++14 deprecated attribute is equivalent to GNU's. */
22351 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22353 if (cxx_dialect
== cxx11
)
22354 pedwarn (token
->location
, OPT_Wpedantic
,
22355 "%<deprecated%> is a C++14 feature;"
22356 " use %<gnu::deprecated%>");
22357 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22361 /* Now parse the optional argument clause of the attribute. */
22363 if (token
->type
!= CPP_OPEN_PAREN
)
22367 vec
<tree
, va_gc
> *vec
;
22368 int attr_flag
= normal_attr
;
22370 if (attr_ns
== get_identifier ("gnu")
22371 && attribute_takes_identifier_p (attr_id
))
22372 /* A GNU attribute that takes an identifier in parameter. */
22373 attr_flag
= id_attr
;
22375 vec
= cp_parser_parenthesized_expression_list
22376 (parser
, attr_flag
, /*cast_p=*/false,
22377 /*allow_expansion_p=*/true,
22378 /*non_constant_p=*/NULL
);
22380 arguments
= error_mark_node
;
22383 arguments
= build_tree_list_vec (vec
);
22384 release_tree_vector (vec
);
22387 if (arguments
== error_mark_node
)
22388 attribute
= error_mark_node
;
22390 TREE_VALUE (attribute
) = arguments
;
22396 /* Parse a list of standard C++-11 attributes.
22400 attribute-list , attribute[opt]
22402 attribute-list , attribute ...
22406 cp_parser_std_attribute_list (cp_parser
*parser
)
22408 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22409 cp_token
*token
= NULL
;
22413 attribute
= cp_parser_std_attribute (parser
);
22414 if (attribute
== error_mark_node
)
22416 if (attribute
!= NULL_TREE
)
22418 TREE_CHAIN (attribute
) = attributes
;
22419 attributes
= attribute
;
22421 token
= cp_lexer_peek_token (parser
->lexer
);
22422 if (token
->type
!= CPP_COMMA
)
22424 cp_lexer_consume_token (parser
->lexer
);
22426 attributes
= nreverse (attributes
);
22430 /* Parse a standard C++-11 attribute specifier.
22432 attribute-specifier:
22433 [ [ attribute-list ] ]
22434 alignment-specifier
22436 alignment-specifier:
22437 alignas ( type-id ... [opt] )
22438 alignas ( alignment-expression ... [opt] ). */
22441 cp_parser_std_attribute_spec (cp_parser
*parser
)
22443 tree attributes
= NULL_TREE
;
22444 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22446 if (token
->type
== CPP_OPEN_SQUARE
22447 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22449 cp_lexer_consume_token (parser
->lexer
);
22450 cp_lexer_consume_token (parser
->lexer
);
22452 attributes
= cp_parser_std_attribute_list (parser
);
22454 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22455 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22456 cp_parser_skip_to_end_of_statement (parser
);
22458 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22459 when we are sure that we have actually parsed them. */
22460 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22466 /* Look for an alignment-specifier. */
22468 token
= cp_lexer_peek_token (parser
->lexer
);
22470 if (token
->type
!= CPP_KEYWORD
22471 || token
->keyword
!= RID_ALIGNAS
)
22474 cp_lexer_consume_token (parser
->lexer
);
22475 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22477 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22479 cp_parser_error (parser
, "expected %<(%>");
22480 return error_mark_node
;
22483 cp_parser_parse_tentatively (parser
);
22484 alignas_expr
= cp_parser_type_id (parser
);
22486 if (!cp_parser_parse_definitely (parser
))
22488 gcc_assert (alignas_expr
== error_mark_node
22489 || alignas_expr
== NULL_TREE
);
22492 cp_parser_assignment_expression (parser
);
22493 if (alignas_expr
== error_mark_node
)
22494 cp_parser_skip_to_end_of_statement (parser
);
22495 if (alignas_expr
== NULL_TREE
22496 || alignas_expr
== error_mark_node
)
22497 return alignas_expr
;
22500 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22502 cp_parser_error (parser
, "expected %<)%>");
22503 return error_mark_node
;
22506 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22508 /* Build the C++-11 representation of an 'aligned'
22511 build_tree_list (build_tree_list (get_identifier ("gnu"),
22512 get_identifier ("aligned")),
22513 build_tree_list (NULL_TREE
, alignas_expr
));
22519 /* Parse a standard C++-11 attribute-specifier-seq.
22521 attribute-specifier-seq:
22522 attribute-specifier-seq [opt] attribute-specifier
22526 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22528 tree attr_specs
= NULL
;
22532 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22533 if (attr_spec
== NULL_TREE
)
22535 if (attr_spec
== error_mark_node
)
22536 return error_mark_node
;
22538 TREE_CHAIN (attr_spec
) = attr_specs
;
22539 attr_specs
= attr_spec
;
22542 attr_specs
= nreverse (attr_specs
);
22546 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22547 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22548 current value of the PEDANTIC flag, regardless of whether or not
22549 the `__extension__' keyword is present. The caller is responsible
22550 for restoring the value of the PEDANTIC flag. */
22553 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22555 /* Save the old value of the PEDANTIC flag. */
22556 *saved_pedantic
= pedantic
;
22558 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22560 /* Consume the `__extension__' token. */
22561 cp_lexer_consume_token (parser
->lexer
);
22562 /* We're not being pedantic while the `__extension__' keyword is
22572 /* Parse a label declaration.
22575 __label__ label-declarator-seq ;
22577 label-declarator-seq:
22578 identifier , label-declarator-seq
22582 cp_parser_label_declaration (cp_parser
* parser
)
22584 /* Look for the `__label__' keyword. */
22585 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22591 /* Look for an identifier. */
22592 identifier
= cp_parser_identifier (parser
);
22593 /* If we failed, stop. */
22594 if (identifier
== error_mark_node
)
22596 /* Declare it as a label. */
22597 finish_label_decl (identifier
);
22598 /* If the next token is a `;', stop. */
22599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22601 /* Look for the `,' separating the label declarations. */
22602 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22605 /* Look for the final `;'. */
22606 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22609 /* Support Functions */
22611 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22612 NAME should have one of the representations used for an
22613 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22614 is returned. If PARSER->SCOPE is a dependent type, then a
22615 SCOPE_REF is returned.
22617 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22618 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22619 was formed. Abstractly, such entities should not be passed to this
22620 function, because they do not need to be looked up, but it is
22621 simpler to check for this special case here, rather than at the
22624 In cases not explicitly covered above, this function returns a
22625 DECL, OVERLOAD, or baselink representing the result of the lookup.
22626 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22629 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22630 (e.g., "struct") that was used. In that case bindings that do not
22631 refer to types are ignored.
22633 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22636 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22639 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22642 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22643 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22644 NULL_TREE otherwise. */
22647 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22648 enum tag_types tag_type
,
22651 bool check_dependency
,
22652 tree
*ambiguous_decls
,
22653 location_t name_location
)
22656 tree object_type
= parser
->context
->object_type
;
22658 /* Assume that the lookup will be unambiguous. */
22659 if (ambiguous_decls
)
22660 *ambiguous_decls
= NULL_TREE
;
22662 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22663 no longer valid. Note that if we are parsing tentatively, and
22664 the parse fails, OBJECT_TYPE will be automatically restored. */
22665 parser
->context
->object_type
= NULL_TREE
;
22667 if (name
== error_mark_node
)
22668 return error_mark_node
;
22670 /* A template-id has already been resolved; there is no lookup to
22672 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22674 if (BASELINK_P (name
))
22676 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22677 == TEMPLATE_ID_EXPR
);
22681 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22682 it should already have been checked to make sure that the name
22683 used matches the type being destroyed. */
22684 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22688 /* Figure out to which type this destructor applies. */
22690 type
= parser
->scope
;
22691 else if (object_type
)
22692 type
= object_type
;
22694 type
= current_class_type
;
22695 /* If that's not a class type, there is no destructor. */
22696 if (!type
|| !CLASS_TYPE_P (type
))
22697 return error_mark_node
;
22698 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22699 lazily_declare_fn (sfk_destructor
, type
);
22700 if (!CLASSTYPE_DESTRUCTORS (type
))
22701 return error_mark_node
;
22702 /* If it was a class type, return the destructor. */
22703 return CLASSTYPE_DESTRUCTORS (type
);
22706 /* By this point, the NAME should be an ordinary identifier. If
22707 the id-expression was a qualified name, the qualifying scope is
22708 stored in PARSER->SCOPE at this point. */
22709 gcc_assert (identifier_p (name
));
22711 /* Perform the lookup. */
22716 if (parser
->scope
== error_mark_node
)
22717 return error_mark_node
;
22719 /* If the SCOPE is dependent, the lookup must be deferred until
22720 the template is instantiated -- unless we are explicitly
22721 looking up names in uninstantiated templates. Even then, we
22722 cannot look up the name if the scope is not a class type; it
22723 might, for example, be a template type parameter. */
22724 dependent_p
= (TYPE_P (parser
->scope
)
22725 && dependent_scope_p (parser
->scope
));
22726 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22728 /* Defer lookup. */
22729 decl
= error_mark_node
;
22732 tree pushed_scope
= NULL_TREE
;
22734 /* If PARSER->SCOPE is a dependent type, then it must be a
22735 class type, and we must not be checking dependencies;
22736 otherwise, we would have processed this lookup above. So
22737 that PARSER->SCOPE is not considered a dependent base by
22738 lookup_member, we must enter the scope here. */
22740 pushed_scope
= push_scope (parser
->scope
);
22742 /* If the PARSER->SCOPE is a template specialization, it
22743 may be instantiated during name lookup. In that case,
22744 errors may be issued. Even if we rollback the current
22745 tentative parse, those errors are valid. */
22746 decl
= lookup_qualified_name (parser
->scope
, name
,
22747 tag_type
!= none_type
,
22748 /*complain=*/true);
22750 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22751 lookup result and the nested-name-specifier nominates a class C:
22752 * if the name specified after the nested-name-specifier, when
22753 looked up in C, is the injected-class-name of C (Clause 9), or
22754 * if the name specified after the nested-name-specifier is the
22755 same as the identifier or the simple-template-id's template-
22756 name in the last component of the nested-name-specifier,
22757 the name is instead considered to name the constructor of
22758 class C. [ Note: for example, the constructor is not an
22759 acceptable lookup result in an elaborated-type-specifier so
22760 the constructor would not be used in place of the
22761 injected-class-name. --end note ] Such a constructor name
22762 shall be used only in the declarator-id of a declaration that
22763 names a constructor or in a using-declaration. */
22764 if (tag_type
== none_type
22765 && DECL_SELF_REFERENCE_P (decl
)
22766 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22767 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22768 tag_type
!= none_type
,
22769 /*complain=*/true);
22771 /* If we have a single function from a using decl, pull it out. */
22772 if (TREE_CODE (decl
) == OVERLOAD
22773 && !really_overloaded_fn (decl
))
22774 decl
= OVL_FUNCTION (decl
);
22777 pop_scope (pushed_scope
);
22780 /* If the scope is a dependent type and either we deferred lookup or
22781 we did lookup but didn't find the name, rememeber the name. */
22782 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22783 && dependent_type_p (parser
->scope
))
22789 /* The resolution to Core Issue 180 says that `struct
22790 A::B' should be considered a type-name, even if `A'
22792 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22793 /*complain=*/tf_error
);
22794 if (type
!= error_mark_node
)
22795 decl
= TYPE_NAME (type
);
22797 else if (is_template
22798 && (cp_parser_next_token_ends_template_argument_p (parser
)
22799 || cp_lexer_next_token_is (parser
->lexer
,
22801 decl
= make_unbound_class_template (parser
->scope
,
22803 /*complain=*/tf_error
);
22805 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22806 parser
->scope
, name
,
22809 parser
->qualifying_scope
= parser
->scope
;
22810 parser
->object_scope
= NULL_TREE
;
22812 else if (object_type
)
22814 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22815 OBJECT_TYPE is not a class. */
22816 if (CLASS_TYPE_P (object_type
))
22817 /* If the OBJECT_TYPE is a template specialization, it may
22818 be instantiated during name lookup. In that case, errors
22819 may be issued. Even if we rollback the current tentative
22820 parse, those errors are valid. */
22821 decl
= lookup_member (object_type
,
22824 tag_type
!= none_type
,
22825 tf_warning_or_error
);
22830 /* Look it up in the enclosing context. */
22831 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22833 /*block_p=*/true, is_namespace
, 0);
22834 parser
->object_scope
= object_type
;
22835 parser
->qualifying_scope
= NULL_TREE
;
22839 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22841 /*block_p=*/true, is_namespace
, 0);
22842 parser
->qualifying_scope
= NULL_TREE
;
22843 parser
->object_scope
= NULL_TREE
;
22846 /* If the lookup failed, let our caller know. */
22847 if (!decl
|| decl
== error_mark_node
)
22848 return error_mark_node
;
22850 /* Pull out the template from an injected-class-name (or multiple). */
22852 decl
= maybe_get_template_decl_from_type_decl (decl
);
22854 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22855 if (TREE_CODE (decl
) == TREE_LIST
)
22857 if (ambiguous_decls
)
22858 *ambiguous_decls
= decl
;
22859 /* The error message we have to print is too complicated for
22860 cp_parser_error, so we incorporate its actions directly. */
22861 if (!cp_parser_simulate_error (parser
))
22863 error_at (name_location
, "reference to %qD is ambiguous",
22865 print_candidates (decl
);
22867 return error_mark_node
;
22870 gcc_assert (DECL_P (decl
)
22871 || TREE_CODE (decl
) == OVERLOAD
22872 || TREE_CODE (decl
) == SCOPE_REF
22873 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22874 || BASELINK_P (decl
));
22876 /* If we have resolved the name of a member declaration, check to
22877 see if the declaration is accessible. When the name resolves to
22878 set of overloaded functions, accessibility is checked when
22879 overload resolution is done.
22881 During an explicit instantiation, access is not checked at all,
22882 as per [temp.explicit]. */
22884 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22886 maybe_record_typedef_use (decl
);
22891 /* Like cp_parser_lookup_name, but for use in the typical case where
22892 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22893 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22896 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22898 return cp_parser_lookup_name (parser
, name
,
22900 /*is_template=*/false,
22901 /*is_namespace=*/false,
22902 /*check_dependency=*/true,
22903 /*ambiguous_decls=*/NULL
,
22907 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22908 the current context, return the TYPE_DECL. If TAG_NAME_P is
22909 true, the DECL indicates the class being defined in a class-head,
22910 or declared in an elaborated-type-specifier.
22912 Otherwise, return DECL. */
22915 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22917 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22918 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22921 template <typename T> struct B;
22924 template <typename T> struct A::B {};
22926 Similarly, in an elaborated-type-specifier:
22928 namespace N { struct X{}; }
22931 template <typename T> friend struct N::X;
22934 However, if the DECL refers to a class type, and we are in
22935 the scope of the class, then the name lookup automatically
22936 finds the TYPE_DECL created by build_self_reference rather
22937 than a TEMPLATE_DECL. For example, in:
22939 template <class T> struct S {
22943 there is no need to handle such case. */
22945 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22946 return DECL_TEMPLATE_RESULT (decl
);
22951 /* If too many, or too few, template-parameter lists apply to the
22952 declarator, issue an error message. Returns TRUE if all went well,
22953 and FALSE otherwise. */
22956 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22957 cp_declarator
*declarator
,
22958 location_t declarator_location
)
22960 switch (declarator
->kind
)
22964 unsigned num_templates
= 0;
22965 tree scope
= declarator
->u
.id
.qualifying_scope
;
22968 num_templates
= num_template_headers_for_class (scope
);
22969 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22970 == TEMPLATE_ID_EXPR
)
22971 /* If the DECLARATOR has the form `X<y>' then it uses one
22972 additional level of template parameters. */
22975 return cp_parser_check_template_parameters
22976 (parser
, num_templates
, declarator_location
, declarator
);
22982 case cdk_reference
:
22984 return (cp_parser_check_declarator_template_parameters
22985 (parser
, declarator
->declarator
, declarator_location
));
22991 gcc_unreachable ();
22996 /* NUM_TEMPLATES were used in the current declaration. If that is
22997 invalid, return FALSE and issue an error messages. Otherwise,
22998 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22999 declarator and we can print more accurate diagnostics. */
23002 cp_parser_check_template_parameters (cp_parser
* parser
,
23003 unsigned num_templates
,
23004 location_t location
,
23005 cp_declarator
*declarator
)
23007 /* If there are the same number of template classes and parameter
23008 lists, that's OK. */
23009 if (parser
->num_template_parameter_lists
== num_templates
)
23011 /* If there are more, but only one more, then we are referring to a
23012 member template. That's OK too. */
23013 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
23015 /* If there are more template classes than parameter lists, we have
23018 template <class T> void S<T>::R<T>::f (); */
23019 if (parser
->num_template_parameter_lists
< num_templates
)
23021 if (declarator
&& !current_function_decl
)
23022 error_at (location
, "specializing member %<%T::%E%> "
23023 "requires %<template<>%> syntax",
23024 declarator
->u
.id
.qualifying_scope
,
23025 declarator
->u
.id
.unqualified_name
);
23026 else if (declarator
)
23027 error_at (location
, "invalid declaration of %<%T::%E%>",
23028 declarator
->u
.id
.qualifying_scope
,
23029 declarator
->u
.id
.unqualified_name
);
23031 error_at (location
, "too few template-parameter-lists");
23034 /* Otherwise, there are too many template parameter lists. We have
23037 template <class T> template <class U> void S::f(); */
23038 error_at (location
, "too many template-parameter-lists");
23042 /* Parse an optional `::' token indicating that the following name is
23043 from the global namespace. If so, PARSER->SCOPE is set to the
23044 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23045 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23046 Returns the new value of PARSER->SCOPE, if the `::' token is
23047 present, and NULL_TREE otherwise. */
23050 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23054 /* Peek at the next token. */
23055 token
= cp_lexer_peek_token (parser
->lexer
);
23056 /* If we're looking at a `::' token then we're starting from the
23057 global namespace, not our current location. */
23058 if (token
->type
== CPP_SCOPE
)
23060 /* Consume the `::' token. */
23061 cp_lexer_consume_token (parser
->lexer
);
23062 /* Set the SCOPE so that we know where to start the lookup. */
23063 parser
->scope
= global_namespace
;
23064 parser
->qualifying_scope
= global_namespace
;
23065 parser
->object_scope
= NULL_TREE
;
23067 return parser
->scope
;
23069 else if (!current_scope_valid_p
)
23071 parser
->scope
= NULL_TREE
;
23072 parser
->qualifying_scope
= NULL_TREE
;
23073 parser
->object_scope
= NULL_TREE
;
23079 /* Returns TRUE if the upcoming token sequence is the start of a
23080 constructor declarator. If FRIEND_P is true, the declarator is
23081 preceded by the `friend' specifier. */
23084 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23086 bool constructor_p
;
23087 bool outside_class_specifier_p
;
23088 tree nested_name_specifier
;
23089 cp_token
*next_token
;
23091 /* The common case is that this is not a constructor declarator, so
23092 try to avoid doing lots of work if at all possible. It's not
23093 valid declare a constructor at function scope. */
23094 if (parser
->in_function_body
)
23096 /* And only certain tokens can begin a constructor declarator. */
23097 next_token
= cp_lexer_peek_token (parser
->lexer
);
23098 if (next_token
->type
!= CPP_NAME
23099 && next_token
->type
!= CPP_SCOPE
23100 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23101 && next_token
->type
!= CPP_TEMPLATE_ID
)
23104 /* Parse tentatively; we are going to roll back all of the tokens
23106 cp_parser_parse_tentatively (parser
);
23107 /* Assume that we are looking at a constructor declarator. */
23108 constructor_p
= true;
23110 /* Look for the optional `::' operator. */
23111 cp_parser_global_scope_opt (parser
,
23112 /*current_scope_valid_p=*/false);
23113 /* Look for the nested-name-specifier. */
23114 nested_name_specifier
23115 = (cp_parser_nested_name_specifier_opt (parser
,
23116 /*typename_keyword_p=*/false,
23117 /*check_dependency_p=*/false,
23119 /*is_declaration=*/false));
23121 outside_class_specifier_p
= (!at_class_scope_p ()
23122 || !TYPE_BEING_DEFINED (current_class_type
)
23125 /* Outside of a class-specifier, there must be a
23126 nested-name-specifier. */
23127 if (!nested_name_specifier
&& outside_class_specifier_p
)
23128 constructor_p
= false;
23129 else if (nested_name_specifier
== error_mark_node
)
23130 constructor_p
= false;
23132 /* If we have a class scope, this is easy; DR 147 says that S::S always
23133 names the constructor, and no other qualified name could. */
23134 if (constructor_p
&& nested_name_specifier
23135 && CLASS_TYPE_P (nested_name_specifier
))
23137 tree id
= cp_parser_unqualified_id (parser
,
23138 /*template_keyword_p=*/false,
23139 /*check_dependency_p=*/false,
23140 /*declarator_p=*/true,
23141 /*optional_p=*/false);
23142 if (is_overloaded_fn (id
))
23143 id
= DECL_NAME (get_first_fn (id
));
23144 if (!constructor_name_p (id
, nested_name_specifier
))
23145 constructor_p
= false;
23147 /* If we still think that this might be a constructor-declarator,
23148 look for a class-name. */
23149 else if (constructor_p
)
23153 template <typename T> struct S {
23157 we must recognize that the nested `S' names a class. */
23159 type_decl
= cp_parser_class_name (parser
,
23160 /*typename_keyword_p=*/false,
23161 /*template_keyword_p=*/false,
23163 /*check_dependency_p=*/false,
23164 /*class_head_p=*/false,
23165 /*is_declaration=*/false);
23166 /* If there was no class-name, then this is not a constructor.
23167 Otherwise, if we are in a class-specifier and we aren't
23168 handling a friend declaration, check that its type matches
23169 current_class_type (c++/38313). Note: error_mark_node
23170 is left alone for error recovery purposes. */
23171 constructor_p
= (!cp_parser_error_occurred (parser
)
23172 && (outside_class_specifier_p
23173 || type_decl
== error_mark_node
23174 || same_type_p (current_class_type
,
23175 TREE_TYPE (type_decl
))));
23177 /* If we're still considering a constructor, we have to see a `(',
23178 to begin the parameter-declaration-clause, followed by either a
23179 `)', an `...', or a decl-specifier. We need to check for a
23180 type-specifier to avoid being fooled into thinking that:
23184 is a constructor. (It is actually a function named `f' that
23185 takes one parameter (of type `int') and returns a value of type
23188 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23189 constructor_p
= false;
23192 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23193 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23194 /* A parameter declaration begins with a decl-specifier,
23195 which is either the "attribute" keyword, a storage class
23196 specifier, or (usually) a type-specifier. */
23197 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23200 tree pushed_scope
= NULL_TREE
;
23201 unsigned saved_num_template_parameter_lists
;
23203 /* Names appearing in the type-specifier should be looked up
23204 in the scope of the class. */
23205 if (current_class_type
)
23209 type
= TREE_TYPE (type_decl
);
23210 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23212 type
= resolve_typename_type (type
,
23213 /*only_current_p=*/false);
23214 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23216 cp_parser_abort_tentative_parse (parser
);
23220 pushed_scope
= push_scope (type
);
23223 /* Inside the constructor parameter list, surrounding
23224 template-parameter-lists do not apply. */
23225 saved_num_template_parameter_lists
23226 = parser
->num_template_parameter_lists
;
23227 parser
->num_template_parameter_lists
= 0;
23229 /* Look for the type-specifier. */
23230 cp_parser_type_specifier (parser
,
23231 CP_PARSER_FLAGS_NONE
,
23232 /*decl_specs=*/NULL
,
23233 /*is_declarator=*/true,
23234 /*declares_class_or_enum=*/NULL
,
23235 /*is_cv_qualifier=*/NULL
);
23237 parser
->num_template_parameter_lists
23238 = saved_num_template_parameter_lists
;
23240 /* Leave the scope of the class. */
23242 pop_scope (pushed_scope
);
23244 constructor_p
= !cp_parser_error_occurred (parser
);
23248 /* We did not really want to consume any tokens. */
23249 cp_parser_abort_tentative_parse (parser
);
23251 return constructor_p
;
23254 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23255 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23256 they must be performed once we are in the scope of the function.
23258 Returns the function defined. */
23261 cp_parser_function_definition_from_specifiers_and_declarator
23262 (cp_parser
* parser
,
23263 cp_decl_specifier_seq
*decl_specifiers
,
23265 const cp_declarator
*declarator
)
23270 /* Begin the function-definition. */
23271 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23273 /* The things we're about to see are not directly qualified by any
23274 template headers we've seen thus far. */
23275 reset_specialization ();
23277 /* If there were names looked up in the decl-specifier-seq that we
23278 did not check, check them now. We must wait until we are in the
23279 scope of the function to perform the checks, since the function
23280 might be a friend. */
23281 perform_deferred_access_checks (tf_warning_or_error
);
23285 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23286 parser
->omp_declare_simd
= NULL
;
23291 /* Skip the entire function. */
23292 cp_parser_skip_to_end_of_block_or_statement (parser
);
23293 fn
= error_mark_node
;
23295 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23297 /* Seen already, skip it. An error message has already been output. */
23298 cp_parser_skip_to_end_of_block_or_statement (parser
);
23299 fn
= current_function_decl
;
23300 current_function_decl
= NULL_TREE
;
23301 /* If this is a function from a class, pop the nested class. */
23302 if (current_class_name
)
23303 pop_nested_class ();
23308 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23309 tv
= TV_PARSE_INLINE
;
23311 tv
= TV_PARSE_FUNC
;
23313 fn
= cp_parser_function_definition_after_declarator (parser
,
23314 /*inline_p=*/false);
23321 /* Parse the part of a function-definition that follows the
23322 declarator. INLINE_P is TRUE iff this function is an inline
23323 function defined within a class-specifier.
23325 Returns the function defined. */
23328 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23332 bool ctor_initializer_p
= false;
23333 bool saved_in_unbraced_linkage_specification_p
;
23334 bool saved_in_function_body
;
23335 unsigned saved_num_template_parameter_lists
;
23337 bool fully_implicit_function_template_p
23338 = parser
->fully_implicit_function_template_p
;
23339 parser
->fully_implicit_function_template_p
= false;
23340 tree implicit_template_parms
23341 = parser
->implicit_template_parms
;
23342 parser
->implicit_template_parms
= 0;
23343 cp_binding_level
* implicit_template_scope
23344 = parser
->implicit_template_scope
;
23345 parser
->implicit_template_scope
= 0;
23347 saved_in_function_body
= parser
->in_function_body
;
23348 parser
->in_function_body
= true;
23349 /* If the next token is `return', then the code may be trying to
23350 make use of the "named return value" extension that G++ used to
23352 token
= cp_lexer_peek_token (parser
->lexer
);
23353 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23355 /* Consume the `return' keyword. */
23356 cp_lexer_consume_token (parser
->lexer
);
23357 /* Look for the identifier that indicates what value is to be
23359 cp_parser_identifier (parser
);
23360 /* Issue an error message. */
23361 error_at (token
->location
,
23362 "named return values are no longer supported");
23363 /* Skip tokens until we reach the start of the function body. */
23366 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23367 if (token
->type
== CPP_OPEN_BRACE
23368 || token
->type
== CPP_EOF
23369 || token
->type
== CPP_PRAGMA_EOL
)
23371 cp_lexer_consume_token (parser
->lexer
);
23374 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23375 anything declared inside `f'. */
23376 saved_in_unbraced_linkage_specification_p
23377 = parser
->in_unbraced_linkage_specification_p
;
23378 parser
->in_unbraced_linkage_specification_p
= false;
23379 /* Inside the function, surrounding template-parameter-lists do not
23381 saved_num_template_parameter_lists
23382 = parser
->num_template_parameter_lists
;
23383 parser
->num_template_parameter_lists
= 0;
23385 start_lambda_scope (current_function_decl
);
23387 /* If the next token is `try', `__transaction_atomic', or
23388 `__transaction_relaxed`, then we are looking at either function-try-block
23389 or function-transaction-block. Note that all of these include the
23391 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23392 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23393 RID_TRANSACTION_ATOMIC
);
23394 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23395 RID_TRANSACTION_RELAXED
))
23396 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23397 RID_TRANSACTION_RELAXED
);
23398 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23399 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23401 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23402 (parser
, /*in_function_try_block=*/false);
23404 finish_lambda_scope ();
23406 /* Finish the function. */
23407 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23408 (inline_p
? 2 : 0));
23409 /* Generate code for it, if necessary. */
23410 expand_or_defer_fn (fn
);
23411 /* Restore the saved values. */
23412 parser
->in_unbraced_linkage_specification_p
23413 = saved_in_unbraced_linkage_specification_p
;
23414 parser
->num_template_parameter_lists
23415 = saved_num_template_parameter_lists
;
23416 parser
->in_function_body
= saved_in_function_body
;
23418 parser
->fully_implicit_function_template_p
23419 = fully_implicit_function_template_p
;
23420 parser
->implicit_template_parms
23421 = implicit_template_parms
;
23422 parser
->implicit_template_scope
23423 = implicit_template_scope
;
23425 if (parser
->fully_implicit_function_template_p
)
23426 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23431 /* Parse a template-declaration, assuming that the `export' (and
23432 `extern') keywords, if present, has already been scanned. MEMBER_P
23433 is as for cp_parser_template_declaration. */
23436 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23438 tree decl
= NULL_TREE
;
23439 vec
<deferred_access_check
, va_gc
> *checks
;
23440 tree parameter_list
;
23441 bool friend_p
= false;
23442 bool need_lang_pop
;
23445 /* Look for the `template' keyword. */
23446 token
= cp_lexer_peek_token (parser
->lexer
);
23447 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23451 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23453 if (at_class_scope_p () && current_function_decl
)
23455 /* 14.5.2.2 [temp.mem]
23457 A local class shall not have member templates. */
23458 error_at (token
->location
,
23459 "invalid declaration of member template in local class");
23460 cp_parser_skip_to_end_of_block_or_statement (parser
);
23465 A template ... shall not have C linkage. */
23466 if (current_lang_name
== lang_name_c
)
23468 error_at (token
->location
, "template with C linkage");
23469 /* Give it C++ linkage to avoid confusing other parts of the
23471 push_lang_context (lang_name_cplusplus
);
23472 need_lang_pop
= true;
23475 need_lang_pop
= false;
23477 /* We cannot perform access checks on the template parameter
23478 declarations until we know what is being declared, just as we
23479 cannot check the decl-specifier list. */
23480 push_deferring_access_checks (dk_deferred
);
23482 /* If the next token is `>', then we have an invalid
23483 specialization. Rather than complain about an invalid template
23484 parameter, issue an error message here. */
23485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23487 cp_parser_error (parser
, "invalid explicit specialization");
23488 begin_specialization ();
23489 parameter_list
= NULL_TREE
;
23493 /* Parse the template parameters. */
23494 parameter_list
= cp_parser_template_parameter_list (parser
);
23497 /* Get the deferred access checks from the parameter list. These
23498 will be checked once we know what is being declared, as for a
23499 member template the checks must be performed in the scope of the
23500 class containing the member. */
23501 checks
= get_deferred_access_checks ();
23503 /* Look for the `>'. */
23504 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23505 /* We just processed one more parameter list. */
23506 ++parser
->num_template_parameter_lists
;
23507 /* If the next token is `template', there are more template
23509 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23511 cp_parser_template_declaration_after_export (parser
, member_p
);
23512 else if (cxx_dialect
>= cxx11
23513 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23514 decl
= cp_parser_alias_declaration (parser
);
23517 /* There are no access checks when parsing a template, as we do not
23518 know if a specialization will be a friend. */
23519 push_deferring_access_checks (dk_no_check
);
23520 token
= cp_lexer_peek_token (parser
->lexer
);
23521 decl
= cp_parser_single_declaration (parser
,
23524 /*explicit_specialization_p=*/false,
23526 pop_deferring_access_checks ();
23528 /* If this is a member template declaration, let the front
23530 if (member_p
&& !friend_p
&& decl
)
23532 if (TREE_CODE (decl
) == TYPE_DECL
)
23533 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23535 decl
= finish_member_template_decl (decl
);
23537 else if (friend_p
&& decl
23538 && DECL_DECLARES_TYPE_P (decl
))
23539 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23540 /*complain=*/true);
23542 /* We are done with the current parameter list. */
23543 --parser
->num_template_parameter_lists
;
23545 pop_deferring_access_checks ();
23548 finish_template_decl (parameter_list
);
23550 /* Check the template arguments for a literal operator template. */
23552 && DECL_DECLARES_FUNCTION_P (decl
)
23553 && UDLIT_OPER_P (DECL_NAME (decl
)))
23556 if (parameter_list
== NULL_TREE
)
23560 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23561 if (num_parms
== 1)
23563 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23564 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23565 if (TREE_TYPE (parm
) != char_type_node
23566 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23569 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23571 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23572 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23573 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23574 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23575 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23576 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23584 if (cxx_dialect
>= cxx14
)
23585 error ("literal operator template %qD has invalid parameter list."
23586 " Expected non-type template argument pack <char...>"
23587 " or <typename CharT, CharT...>",
23590 error ("literal operator template %qD has invalid parameter list."
23591 " Expected non-type template argument pack <char...>",
23595 /* Register member declarations. */
23596 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23597 finish_member_declaration (decl
);
23598 /* For the erroneous case of a template with C linkage, we pushed an
23599 implicit C++ linkage scope; exit that scope now. */
23601 pop_lang_context ();
23602 /* If DECL is a function template, we must return to parse it later.
23603 (Even though there is no definition, there might be default
23604 arguments that need handling.) */
23605 if (member_p
&& decl
23606 && DECL_DECLARES_FUNCTION_P (decl
))
23607 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23610 /* Perform the deferred access checks from a template-parameter-list.
23611 CHECKS is a TREE_LIST of access checks, as returned by
23612 get_deferred_access_checks. */
23615 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23617 ++processing_template_parmlist
;
23618 perform_access_checks (checks
, tf_warning_or_error
);
23619 --processing_template_parmlist
;
23622 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23623 `function-definition' sequence that follows a template header.
23624 If MEMBER_P is true, this declaration appears in a class scope.
23626 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23627 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23630 cp_parser_single_declaration (cp_parser
* parser
,
23631 vec
<deferred_access_check
, va_gc
> *checks
,
23633 bool explicit_specialization_p
,
23636 int declares_class_or_enum
;
23637 tree decl
= NULL_TREE
;
23638 cp_decl_specifier_seq decl_specifiers
;
23639 bool function_definition_p
= false;
23640 cp_token
*decl_spec_token_start
;
23642 /* This function is only used when processing a template
23644 gcc_assert (innermost_scope_kind () == sk_template_parms
23645 || innermost_scope_kind () == sk_template_spec
);
23647 /* Defer access checks until we know what is being declared. */
23648 push_deferring_access_checks (dk_deferred
);
23650 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23652 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23653 cp_parser_decl_specifier_seq (parser
,
23654 CP_PARSER_FLAGS_OPTIONAL
,
23656 &declares_class_or_enum
);
23658 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23660 /* There are no template typedefs. */
23661 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23663 error_at (decl_spec_token_start
->location
,
23664 "template declaration of %<typedef%>");
23665 decl
= error_mark_node
;
23668 /* Gather up the access checks that occurred the
23669 decl-specifier-seq. */
23670 stop_deferring_access_checks ();
23672 /* Check for the declaration of a template class. */
23673 if (declares_class_or_enum
)
23675 if (cp_parser_declares_only_class_p (parser
))
23677 decl
= shadow_tag (&decl_specifiers
);
23682 friend template <typename T> struct A<T>::B;
23685 A<T>::B will be represented by a TYPENAME_TYPE, and
23686 therefore not recognized by shadow_tag. */
23687 if (friend_p
&& *friend_p
23689 && decl_specifiers
.type
23690 && TYPE_P (decl_specifiers
.type
))
23691 decl
= decl_specifiers
.type
;
23693 if (decl
&& decl
!= error_mark_node
)
23694 decl
= TYPE_NAME (decl
);
23696 decl
= error_mark_node
;
23698 /* Perform access checks for template parameters. */
23699 cp_parser_perform_template_parameter_access_checks (checks
);
23703 /* Complain about missing 'typename' or other invalid type names. */
23704 if (!decl_specifiers
.any_type_specifiers_p
23705 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23707 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23708 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23709 the rest of this declaration. */
23710 decl
= error_mark_node
;
23714 /* If it's not a template class, try for a template function. If
23715 the next token is a `;', then this declaration does not declare
23716 anything. But, if there were errors in the decl-specifiers, then
23717 the error might well have come from an attempted class-specifier.
23718 In that case, there's no need to warn about a missing declarator. */
23720 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23721 || decl_specifiers
.type
!= error_mark_node
))
23723 decl
= cp_parser_init_declarator (parser
,
23726 /*function_definition_allowed_p=*/true,
23728 declares_class_or_enum
,
23729 &function_definition_p
,
23732 /* 7.1.1-1 [dcl.stc]
23734 A storage-class-specifier shall not be specified in an explicit
23735 specialization... */
23737 && explicit_specialization_p
23738 && decl_specifiers
.storage_class
!= sc_none
)
23740 error_at (decl_spec_token_start
->location
,
23741 "explicit template specialization cannot have a storage class");
23742 decl
= error_mark_node
;
23745 if (decl
&& VAR_P (decl
))
23746 check_template_variable (decl
);
23749 /* Look for a trailing `;' after the declaration. */
23750 if (!function_definition_p
23751 && (decl
== error_mark_node
23752 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23753 cp_parser_skip_to_end_of_block_or_statement (parser
);
23756 pop_deferring_access_checks ();
23758 /* Clear any current qualification; whatever comes next is the start
23759 of something new. */
23760 parser
->scope
= NULL_TREE
;
23761 parser
->qualifying_scope
= NULL_TREE
;
23762 parser
->object_scope
= NULL_TREE
;
23767 /* Parse a cast-expression that is not the operand of a unary "&". */
23770 cp_parser_simple_cast_expression (cp_parser
*parser
)
23772 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23773 /*cast_p=*/false, /*decltype*/false, NULL
);
23776 /* Parse a functional cast to TYPE. Returns an expression
23777 representing the cast. */
23780 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23782 vec
<tree
, va_gc
> *vec
;
23783 tree expression_list
;
23788 type
= error_mark_node
;
23790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23792 cp_lexer_set_source_position (parser
->lexer
);
23793 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23794 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23795 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23796 if (TREE_CODE (type
) == TYPE_DECL
)
23797 type
= TREE_TYPE (type
);
23798 return finish_compound_literal (type
, expression_list
,
23799 tf_warning_or_error
);
23803 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23805 /*allow_expansion_p=*/true,
23806 /*non_constant_p=*/NULL
);
23808 expression_list
= error_mark_node
;
23811 expression_list
= build_tree_list_vec (vec
);
23812 release_tree_vector (vec
);
23815 cast
= build_functional_cast (type
, expression_list
,
23816 tf_warning_or_error
);
23817 /* [expr.const]/1: In an integral constant expression "only type
23818 conversions to integral or enumeration type can be used". */
23819 if (TREE_CODE (type
) == TYPE_DECL
)
23820 type
= TREE_TYPE (type
);
23821 if (cast
!= error_mark_node
23822 && !cast_valid_in_integral_constant_expression_p (type
)
23823 && cp_parser_non_integral_constant_expression (parser
,
23825 return error_mark_node
;
23829 /* Save the tokens that make up the body of a member function defined
23830 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23831 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23832 specifiers applied to the declaration. Returns the FUNCTION_DECL
23833 for the member function. */
23836 cp_parser_save_member_function_body (cp_parser
* parser
,
23837 cp_decl_specifier_seq
*decl_specifiers
,
23838 cp_declarator
*declarator
,
23845 /* Create the FUNCTION_DECL. */
23846 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23847 cp_finalize_omp_declare_simd (parser
, fn
);
23848 /* If something went badly wrong, bail out now. */
23849 if (fn
== error_mark_node
)
23851 /* If there's a function-body, skip it. */
23852 if (cp_parser_token_starts_function_definition_p
23853 (cp_lexer_peek_token (parser
->lexer
)))
23854 cp_parser_skip_to_end_of_block_or_statement (parser
);
23855 return error_mark_node
;
23858 /* Remember it, if there default args to post process. */
23859 cp_parser_save_default_args (parser
, fn
);
23861 /* Save away the tokens that make up the body of the
23863 first
= parser
->lexer
->next_token
;
23864 /* Handle function try blocks. */
23865 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23866 cp_lexer_consume_token (parser
->lexer
);
23867 /* We can have braced-init-list mem-initializers before the fn body. */
23868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23870 cp_lexer_consume_token (parser
->lexer
);
23871 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23873 /* cache_group will stop after an un-nested { } pair, too. */
23874 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23877 /* variadic mem-inits have ... after the ')'. */
23878 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23879 cp_lexer_consume_token (parser
->lexer
);
23882 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23883 /* Handle function try blocks. */
23884 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23885 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23886 last
= parser
->lexer
->next_token
;
23888 /* Save away the inline definition; we will process it when the
23889 class is complete. */
23890 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23891 DECL_PENDING_INLINE_P (fn
) = 1;
23893 /* We need to know that this was defined in the class, so that
23894 friend templates are handled correctly. */
23895 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23897 /* Add FN to the queue of functions to be parsed later. */
23898 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23903 /* Save the tokens that make up the in-class initializer for a non-static
23904 data member. Returns a DEFAULT_ARG. */
23907 cp_parser_save_nsdmi (cp_parser
* parser
)
23909 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23912 /* Parse a template-argument-list, as well as the trailing ">" (but
23913 not the opening "<"). See cp_parser_template_argument_list for the
23917 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23921 tree saved_qualifying_scope
;
23922 tree saved_object_scope
;
23923 bool saved_greater_than_is_operator_p
;
23924 int saved_unevaluated_operand
;
23925 int saved_inhibit_evaluation_warnings
;
23929 When parsing a template-id, the first non-nested `>' is taken as
23930 the end of the template-argument-list rather than a greater-than
23932 saved_greater_than_is_operator_p
23933 = parser
->greater_than_is_operator_p
;
23934 parser
->greater_than_is_operator_p
= false;
23935 /* Parsing the argument list may modify SCOPE, so we save it
23937 saved_scope
= parser
->scope
;
23938 saved_qualifying_scope
= parser
->qualifying_scope
;
23939 saved_object_scope
= parser
->object_scope
;
23940 /* We need to evaluate the template arguments, even though this
23941 template-id may be nested within a "sizeof". */
23942 saved_unevaluated_operand
= cp_unevaluated_operand
;
23943 cp_unevaluated_operand
= 0;
23944 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23945 c_inhibit_evaluation_warnings
= 0;
23946 /* Parse the template-argument-list itself. */
23947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23948 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23949 arguments
= NULL_TREE
;
23951 arguments
= cp_parser_template_argument_list (parser
);
23952 /* Look for the `>' that ends the template-argument-list. If we find
23953 a '>>' instead, it's probably just a typo. */
23954 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23956 if (cxx_dialect
!= cxx98
)
23958 /* In C++0x, a `>>' in a template argument list or cast
23959 expression is considered to be two separate `>'
23960 tokens. So, change the current token to a `>', but don't
23961 consume it: it will be consumed later when the outer
23962 template argument list (or cast expression) is parsed.
23963 Note that this replacement of `>' for `>>' is necessary
23964 even if we are parsing tentatively: in the tentative
23965 case, after calling
23966 cp_parser_enclosed_template_argument_list we will always
23967 throw away all of the template arguments and the first
23968 closing `>', either because the template argument list
23969 was erroneous or because we are replacing those tokens
23970 with a CPP_TEMPLATE_ID token. The second `>' (which will
23971 not have been thrown away) is needed either to close an
23972 outer template argument list or to complete a new-style
23974 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23975 token
->type
= CPP_GREATER
;
23977 else if (!saved_greater_than_is_operator_p
)
23979 /* If we're in a nested template argument list, the '>>' has
23980 to be a typo for '> >'. We emit the error message, but we
23981 continue parsing and we push a '>' as next token, so that
23982 the argument list will be parsed correctly. Note that the
23983 global source location is still on the token before the
23984 '>>', so we need to say explicitly where we want it. */
23985 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23986 error_at (token
->location
, "%<>>%> should be %<> >%> "
23987 "within a nested template argument list");
23989 token
->type
= CPP_GREATER
;
23993 /* If this is not a nested template argument list, the '>>'
23994 is a typo for '>'. Emit an error message and continue.
23995 Same deal about the token location, but here we can get it
23996 right by consuming the '>>' before issuing the diagnostic. */
23997 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23998 error_at (token
->location
,
23999 "spurious %<>>%>, use %<>%> to terminate "
24000 "a template argument list");
24004 cp_parser_skip_to_end_of_template_parameter_list (parser
);
24005 /* The `>' token might be a greater-than operator again now. */
24006 parser
->greater_than_is_operator_p
24007 = saved_greater_than_is_operator_p
;
24008 /* Restore the SAVED_SCOPE. */
24009 parser
->scope
= saved_scope
;
24010 parser
->qualifying_scope
= saved_qualifying_scope
;
24011 parser
->object_scope
= saved_object_scope
;
24012 cp_unevaluated_operand
= saved_unevaluated_operand
;
24013 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24018 /* MEMBER_FUNCTION is a member function, or a friend. If default
24019 arguments, or the body of the function have not yet been parsed,
24023 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
24025 timevar_push (TV_PARSE_INMETH
);
24026 /* If this member is a template, get the underlying
24028 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
24029 member_function
= DECL_TEMPLATE_RESULT (member_function
);
24031 /* There should not be any class definitions in progress at this
24032 point; the bodies of members are only parsed outside of all class
24034 gcc_assert (parser
->num_classes_being_defined
== 0);
24035 /* While we're parsing the member functions we might encounter more
24036 classes. We want to handle them right away, but we don't want
24037 them getting mixed up with functions that are currently in the
24039 push_unparsed_function_queues (parser
);
24041 /* Make sure that any template parameters are in scope. */
24042 maybe_begin_member_template_processing (member_function
);
24044 /* If the body of the function has not yet been parsed, parse it
24046 if (DECL_PENDING_INLINE_P (member_function
))
24048 tree function_scope
;
24049 cp_token_cache
*tokens
;
24051 /* The function is no longer pending; we are processing it. */
24052 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24053 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24054 DECL_PENDING_INLINE_P (member_function
) = 0;
24056 /* If this is a local class, enter the scope of the containing
24058 function_scope
= current_function_decl
;
24059 if (function_scope
)
24060 push_function_context ();
24062 /* Push the body of the function onto the lexer stack. */
24063 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24065 /* Let the front end know that we going to be defining this
24067 start_preparsed_function (member_function
, NULL_TREE
,
24068 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24070 /* Don't do access checking if it is a templated function. */
24071 if (processing_template_decl
)
24072 push_deferring_access_checks (dk_no_check
);
24074 /* #pragma omp declare reduction needs special parsing. */
24075 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24077 parser
->lexer
->in_pragma
= true;
24078 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24079 finish_function (/*inline*/2);
24080 cp_check_omp_declare_reduction (member_function
);
24083 /* Now, parse the body of the function. */
24084 cp_parser_function_definition_after_declarator (parser
,
24085 /*inline_p=*/true);
24087 if (processing_template_decl
)
24088 pop_deferring_access_checks ();
24090 /* Leave the scope of the containing function. */
24091 if (function_scope
)
24092 pop_function_context ();
24093 cp_parser_pop_lexer (parser
);
24096 /* Remove any template parameters from the symbol table. */
24097 maybe_end_member_template_processing ();
24099 /* Restore the queue. */
24100 pop_unparsed_function_queues (parser
);
24101 timevar_pop (TV_PARSE_INMETH
);
24104 /* If DECL contains any default args, remember it on the unparsed
24105 functions queue. */
24108 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24112 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24114 probe
= TREE_CHAIN (probe
))
24115 if (TREE_PURPOSE (probe
))
24117 cp_default_arg_entry entry
= {current_class_type
, decl
};
24118 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24123 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24124 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24125 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24126 from the parameter-type-list. */
24129 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24130 tree default_arg
, tree parmtype
)
24132 cp_token_cache
*tokens
;
24136 if (default_arg
== error_mark_node
)
24137 return error_mark_node
;
24139 /* Push the saved tokens for the default argument onto the parser's
24141 tokens
= DEFARG_TOKENS (default_arg
);
24142 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24144 start_lambda_scope (decl
);
24146 /* Parse the default argument. */
24147 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24148 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24151 finish_lambda_scope ();
24153 if (parsed_arg
== error_mark_node
)
24154 cp_parser_skip_to_end_of_statement (parser
);
24156 if (!processing_template_decl
)
24158 /* In a non-template class, check conversions now. In a template,
24159 we'll wait and instantiate these as needed. */
24160 if (TREE_CODE (decl
) == PARM_DECL
)
24161 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24162 tf_warning_or_error
);
24164 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24167 /* If the token stream has not been completely used up, then
24168 there was extra junk after the end of the default
24170 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24172 if (TREE_CODE (decl
) == PARM_DECL
)
24173 cp_parser_error (parser
, "expected %<,%>");
24175 cp_parser_error (parser
, "expected %<;%>");
24178 /* Revert to the main lexer. */
24179 cp_parser_pop_lexer (parser
);
24184 /* FIELD is a non-static data member with an initializer which we saved for
24185 later; parse it now. */
24188 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24192 maybe_begin_member_template_processing (field
);
24194 push_unparsed_function_queues (parser
);
24195 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24196 DECL_INITIAL (field
),
24198 pop_unparsed_function_queues (parser
);
24200 maybe_end_member_template_processing ();
24202 DECL_INITIAL (field
) = def
;
24205 /* FN is a FUNCTION_DECL which may contains a parameter with an
24206 unparsed DEFAULT_ARG. Parse the default args now. This function
24207 assumes that the current scope is the scope in which the default
24208 argument should be processed. */
24211 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24213 bool saved_local_variables_forbidden_p
;
24214 tree parm
, parmdecl
;
24216 /* While we're parsing the default args, we might (due to the
24217 statement expression extension) encounter more classes. We want
24218 to handle them right away, but we don't want them getting mixed
24219 up with default args that are currently in the queue. */
24220 push_unparsed_function_queues (parser
);
24222 /* Local variable names (and the `this' keyword) may not appear
24223 in a default argument. */
24224 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24225 parser
->local_variables_forbidden_p
= true;
24227 push_defarg_context (fn
);
24229 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24230 parmdecl
= DECL_ARGUMENTS (fn
);
24231 parm
&& parm
!= void_list_node
;
24232 parm
= TREE_CHAIN (parm
),
24233 parmdecl
= DECL_CHAIN (parmdecl
))
24235 tree default_arg
= TREE_PURPOSE (parm
);
24237 vec
<tree
, va_gc
> *insts
;
24244 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24245 /* This can happen for a friend declaration for a function
24246 already declared with default arguments. */
24250 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24252 TREE_VALUE (parm
));
24253 if (parsed_arg
== error_mark_node
)
24258 TREE_PURPOSE (parm
) = parsed_arg
;
24260 /* Update any instantiations we've already created. */
24261 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24262 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24263 TREE_PURPOSE (copy
) = parsed_arg
;
24266 pop_defarg_context ();
24268 /* Make sure no default arg is missing. */
24269 check_default_args (fn
);
24271 /* Restore the state of local_variables_forbidden_p. */
24272 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24274 /* Restore the queue. */
24275 pop_unparsed_function_queues (parser
);
24278 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24280 sizeof ... ( identifier )
24282 where the 'sizeof' token has already been consumed. */
24285 cp_parser_sizeof_pack (cp_parser
*parser
)
24287 /* Consume the `...'. */
24288 cp_lexer_consume_token (parser
->lexer
);
24289 maybe_warn_variadic_templates ();
24291 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24293 cp_lexer_consume_token (parser
->lexer
);
24295 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24296 "%<sizeof...%> argument must be surrounded by parentheses");
24298 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24299 tree name
= cp_parser_identifier (parser
);
24300 if (name
== error_mark_node
)
24301 return error_mark_node
;
24302 /* The name is not qualified. */
24303 parser
->scope
= NULL_TREE
;
24304 parser
->qualifying_scope
= NULL_TREE
;
24305 parser
->object_scope
= NULL_TREE
;
24306 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24307 if (expr
== error_mark_node
)
24308 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24310 if (TREE_CODE (expr
) == TYPE_DECL
)
24311 expr
= TREE_TYPE (expr
);
24312 else if (TREE_CODE (expr
) == CONST_DECL
)
24313 expr
= DECL_INITIAL (expr
);
24314 expr
= make_pack_expansion (expr
);
24317 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24322 /* Parse the operand of `sizeof' (or a similar operator). Returns
24323 either a TYPE or an expression, depending on the form of the
24324 input. The KEYWORD indicates which kind of expression we have
24328 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24330 tree expr
= NULL_TREE
;
24331 const char *saved_message
;
24333 bool saved_integral_constant_expression_p
;
24334 bool saved_non_integral_constant_expression_p
;
24336 /* If it's a `...', then we are computing the length of a parameter
24338 if (keyword
== RID_SIZEOF
24339 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24340 return cp_parser_sizeof_pack (parser
);
24342 /* Types cannot be defined in a `sizeof' expression. Save away the
24344 saved_message
= parser
->type_definition_forbidden_message
;
24345 /* And create the new one. */
24346 tmp
= concat ("types may not be defined in %<",
24347 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24348 "%> expressions", NULL
);
24349 parser
->type_definition_forbidden_message
= tmp
;
24351 /* The restrictions on constant-expressions do not apply inside
24352 sizeof expressions. */
24353 saved_integral_constant_expression_p
24354 = parser
->integral_constant_expression_p
;
24355 saved_non_integral_constant_expression_p
24356 = parser
->non_integral_constant_expression_p
;
24357 parser
->integral_constant_expression_p
= false;
24359 /* Do not actually evaluate the expression. */
24360 ++cp_unevaluated_operand
;
24361 ++c_inhibit_evaluation_warnings
;
24362 /* If it's a `(', then we might be looking at the type-id
24364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24366 tree type
= NULL_TREE
;
24368 /* We can't be sure yet whether we're looking at a type-id or an
24370 cp_parser_parse_tentatively (parser
);
24371 /* Note: as a GNU Extension, compound literals are considered
24372 postfix-expressions as they are in C99, so they are valid
24373 arguments to sizeof. See comment in cp_parser_cast_expression
24375 if (cp_parser_compound_literal_p (parser
))
24376 cp_parser_simulate_error (parser
);
24379 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24380 parser
->in_type_id_in_expr_p
= true;
24381 /* Look for the type-id. */
24382 type
= cp_parser_type_id (parser
);
24383 /* Look for the closing `)'. */
24384 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24385 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24388 /* If all went well, then we're done. */
24389 if (cp_parser_parse_definitely (parser
))
24391 cp_decl_specifier_seq decl_specs
;
24393 /* Build a trivial decl-specifier-seq. */
24394 clear_decl_specs (&decl_specs
);
24395 decl_specs
.type
= type
;
24397 /* Call grokdeclarator to figure out what type this is. */
24398 expr
= grokdeclarator (NULL
,
24402 /*attrlist=*/NULL
);
24406 /* If the type-id production did not work out, then we must be
24407 looking at the unary-expression production. */
24409 expr
= cp_parser_unary_expression (parser
);
24411 /* Go back to evaluating expressions. */
24412 --cp_unevaluated_operand
;
24413 --c_inhibit_evaluation_warnings
;
24415 /* Free the message we created. */
24417 /* And restore the old one. */
24418 parser
->type_definition_forbidden_message
= saved_message
;
24419 parser
->integral_constant_expression_p
24420 = saved_integral_constant_expression_p
;
24421 parser
->non_integral_constant_expression_p
24422 = saved_non_integral_constant_expression_p
;
24427 /* If the current declaration has no declarator, return true. */
24430 cp_parser_declares_only_class_p (cp_parser
*parser
)
24432 /* If the next token is a `;' or a `,' then there is no
24434 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24435 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24438 /* Update the DECL_SPECS to reflect the storage class indicated by
24442 cp_parser_set_storage_class (cp_parser
*parser
,
24443 cp_decl_specifier_seq
*decl_specs
,
24447 cp_storage_class storage_class
;
24449 if (parser
->in_unbraced_linkage_specification_p
)
24451 error_at (token
->location
, "invalid use of %qD in linkage specification",
24452 ridpointers
[keyword
]);
24455 else if (decl_specs
->storage_class
!= sc_none
)
24457 decl_specs
->conflicting_specifiers_p
= true;
24461 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24462 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24463 && decl_specs
->gnu_thread_keyword_p
)
24465 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24466 "%<__thread%> before %qD", ridpointers
[keyword
]);
24472 storage_class
= sc_auto
;
24475 storage_class
= sc_register
;
24478 storage_class
= sc_static
;
24481 storage_class
= sc_extern
;
24484 storage_class
= sc_mutable
;
24487 gcc_unreachable ();
24489 decl_specs
->storage_class
= storage_class
;
24490 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24492 /* A storage class specifier cannot be applied alongside a typedef
24493 specifier. If there is a typedef specifier present then set
24494 conflicting_specifiers_p which will trigger an error later
24495 on in grokdeclarator. */
24496 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24497 decl_specs
->conflicting_specifiers_p
= true;
24500 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24501 is true, the type is a class or enum definition. */
24504 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24507 bool type_definition_p
)
24509 decl_specs
->any_specifiers_p
= true;
24511 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24512 (with, for example, in "typedef int wchar_t;") we remember that
24513 this is what happened. In system headers, we ignore these
24514 declarations so that G++ can work with system headers that are not
24516 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24517 && !type_definition_p
24518 && (type_spec
== boolean_type_node
24519 || type_spec
== char16_type_node
24520 || type_spec
== char32_type_node
24521 || type_spec
== wchar_type_node
)
24522 && (decl_specs
->type
24523 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24524 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24525 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24526 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24528 decl_specs
->redefined_builtin_type
= type_spec
;
24529 set_and_check_decl_spec_loc (decl_specs
,
24530 ds_redefined_builtin_type_spec
,
24532 if (!decl_specs
->type
)
24534 decl_specs
->type
= type_spec
;
24535 decl_specs
->type_definition_p
= false;
24536 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24539 else if (decl_specs
->type
)
24540 decl_specs
->multiple_types_p
= true;
24543 decl_specs
->type
= type_spec
;
24544 decl_specs
->type_definition_p
= type_definition_p
;
24545 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24546 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24550 /* True iff TOKEN is the GNU keyword __thread. */
24553 token_is__thread (cp_token
*token
)
24555 gcc_assert (token
->keyword
== RID_THREAD
);
24556 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24559 /* Set the location for a declarator specifier and check if it is
24562 DECL_SPECS is the sequence of declarator specifiers onto which to
24565 DS is the single declarator specifier to set which location is to
24566 be set onto the existing sequence of declarators.
24568 LOCATION is the location for the declarator specifier to
24572 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24573 cp_decl_spec ds
, cp_token
*token
)
24575 gcc_assert (ds
< ds_last
);
24577 if (decl_specs
== NULL
)
24580 source_location location
= token
->location
;
24582 if (decl_specs
->locations
[ds
] == 0)
24584 decl_specs
->locations
[ds
] = location
;
24585 if (ds
== ds_thread
)
24586 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24592 if (decl_specs
->locations
[ds_long_long
] != 0)
24593 error_at (location
,
24594 "%<long long long%> is too long for GCC");
24597 decl_specs
->locations
[ds_long_long
] = location
;
24598 pedwarn_cxx98 (location
,
24600 "ISO C++ 1998 does not support %<long long%>");
24603 else if (ds
== ds_thread
)
24605 bool gnu
= token_is__thread (token
);
24606 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24607 error_at (location
,
24608 "both %<__thread%> and %<thread_local%> specified");
24610 error_at (location
, "duplicate %qD", token
->u
.value
);
24614 static const char *const decl_spec_names
[] = {
24631 error_at (location
,
24632 "duplicate %qs", decl_spec_names
[ds
]);
24637 /* Return true iff the declarator specifier DS is present in the
24638 sequence of declarator specifiers DECL_SPECS. */
24641 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24644 gcc_assert (ds
< ds_last
);
24646 if (decl_specs
== NULL
)
24649 return decl_specs
->locations
[ds
] != 0;
24652 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24653 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24656 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24658 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24661 /* Issue an error message indicating that TOKEN_DESC was expected.
24662 If KEYWORD is true, it indicated this function is called by
24663 cp_parser_require_keword and the required token can only be
24664 a indicated keyword. */
24667 cp_parser_required_error (cp_parser
*parser
,
24668 required_token token_desc
,
24671 switch (token_desc
)
24674 cp_parser_error (parser
, "expected %<new%>");
24677 cp_parser_error (parser
, "expected %<delete%>");
24680 cp_parser_error (parser
, "expected %<return%>");
24683 cp_parser_error (parser
, "expected %<while%>");
24686 cp_parser_error (parser
, "expected %<extern%>");
24688 case RT_STATIC_ASSERT
:
24689 cp_parser_error (parser
, "expected %<static_assert%>");
24692 cp_parser_error (parser
, "expected %<decltype%>");
24695 cp_parser_error (parser
, "expected %<operator%>");
24698 cp_parser_error (parser
, "expected %<class%>");
24701 cp_parser_error (parser
, "expected %<template%>");
24704 cp_parser_error (parser
, "expected %<namespace%>");
24707 cp_parser_error (parser
, "expected %<using%>");
24710 cp_parser_error (parser
, "expected %<asm%>");
24713 cp_parser_error (parser
, "expected %<try%>");
24716 cp_parser_error (parser
, "expected %<catch%>");
24719 cp_parser_error (parser
, "expected %<throw%>");
24722 cp_parser_error (parser
, "expected %<__label__%>");
24725 cp_parser_error (parser
, "expected %<@try%>");
24727 case RT_AT_SYNCHRONIZED
:
24728 cp_parser_error (parser
, "expected %<@synchronized%>");
24731 cp_parser_error (parser
, "expected %<@throw%>");
24733 case RT_TRANSACTION_ATOMIC
:
24734 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24736 case RT_TRANSACTION_RELAXED
:
24737 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24744 switch (token_desc
)
24747 cp_parser_error (parser
, "expected %<;%>");
24749 case RT_OPEN_PAREN
:
24750 cp_parser_error (parser
, "expected %<(%>");
24752 case RT_CLOSE_BRACE
:
24753 cp_parser_error (parser
, "expected %<}%>");
24755 case RT_OPEN_BRACE
:
24756 cp_parser_error (parser
, "expected %<{%>");
24758 case RT_CLOSE_SQUARE
:
24759 cp_parser_error (parser
, "expected %<]%>");
24761 case RT_OPEN_SQUARE
:
24762 cp_parser_error (parser
, "expected %<[%>");
24765 cp_parser_error (parser
, "expected %<,%>");
24768 cp_parser_error (parser
, "expected %<::%>");
24771 cp_parser_error (parser
, "expected %<<%>");
24774 cp_parser_error (parser
, "expected %<>%>");
24777 cp_parser_error (parser
, "expected %<=%>");
24780 cp_parser_error (parser
, "expected %<...%>");
24783 cp_parser_error (parser
, "expected %<*%>");
24786 cp_parser_error (parser
, "expected %<~%>");
24789 cp_parser_error (parser
, "expected %<:%>");
24791 case RT_COLON_SCOPE
:
24792 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24794 case RT_CLOSE_PAREN
:
24795 cp_parser_error (parser
, "expected %<)%>");
24797 case RT_COMMA_CLOSE_PAREN
:
24798 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24800 case RT_PRAGMA_EOL
:
24801 cp_parser_error (parser
, "expected end of line");
24804 cp_parser_error (parser
, "expected identifier");
24807 cp_parser_error (parser
, "expected selection-statement");
24809 case RT_INTERATION
:
24810 cp_parser_error (parser
, "expected iteration-statement");
24813 cp_parser_error (parser
, "expected jump-statement");
24816 cp_parser_error (parser
, "expected class-key");
24818 case RT_CLASS_TYPENAME_TEMPLATE
:
24819 cp_parser_error (parser
,
24820 "expected %<class%>, %<typename%>, or %<template%>");
24823 gcc_unreachable ();
24827 gcc_unreachable ();
24832 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24833 issue an error message indicating that TOKEN_DESC was expected.
24835 Returns the token consumed, if the token had the appropriate type.
24836 Otherwise, returns NULL. */
24839 cp_parser_require (cp_parser
* parser
,
24840 enum cpp_ttype type
,
24841 required_token token_desc
)
24843 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24844 return cp_lexer_consume_token (parser
->lexer
);
24847 /* Output the MESSAGE -- unless we're parsing tentatively. */
24848 if (!cp_parser_simulate_error (parser
))
24849 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24854 /* An error message is produced if the next token is not '>'.
24855 All further tokens are skipped until the desired token is
24856 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24859 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24861 /* Current level of '< ... >'. */
24862 unsigned level
= 0;
24863 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24864 unsigned nesting_depth
= 0;
24866 /* Are we ready, yet? If not, issue error message. */
24867 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24870 /* Skip tokens until the desired token is found. */
24873 /* Peek at the next token. */
24874 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24877 if (!nesting_depth
)
24882 if (cxx_dialect
== cxx98
)
24883 /* C++0x views the `>>' operator as two `>' tokens, but
24886 else if (!nesting_depth
&& level
-- == 0)
24888 /* We've hit a `>>' where the first `>' closes the
24889 template argument list, and the second `>' is
24890 spurious. Just consume the `>>' and stop; we've
24891 already produced at least one error. */
24892 cp_lexer_consume_token (parser
->lexer
);
24895 /* Fall through for C++0x, so we handle the second `>' in
24899 if (!nesting_depth
&& level
-- == 0)
24901 /* We've reached the token we want, consume it and stop. */
24902 cp_lexer_consume_token (parser
->lexer
);
24907 case CPP_OPEN_PAREN
:
24908 case CPP_OPEN_SQUARE
:
24912 case CPP_CLOSE_PAREN
:
24913 case CPP_CLOSE_SQUARE
:
24914 if (nesting_depth
-- == 0)
24919 case CPP_PRAGMA_EOL
:
24920 case CPP_SEMICOLON
:
24921 case CPP_OPEN_BRACE
:
24922 case CPP_CLOSE_BRACE
:
24923 /* The '>' was probably forgotten, don't look further. */
24930 /* Consume this token. */
24931 cp_lexer_consume_token (parser
->lexer
);
24935 /* If the next token is the indicated keyword, consume it. Otherwise,
24936 issue an error message indicating that TOKEN_DESC was expected.
24938 Returns the token consumed, if the token had the appropriate type.
24939 Otherwise, returns NULL. */
24942 cp_parser_require_keyword (cp_parser
* parser
,
24944 required_token token_desc
)
24946 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24948 if (token
&& token
->keyword
!= keyword
)
24950 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24957 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24958 function-definition. */
24961 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24963 return (/* An ordinary function-body begins with an `{'. */
24964 token
->type
== CPP_OPEN_BRACE
24965 /* A ctor-initializer begins with a `:'. */
24966 || token
->type
== CPP_COLON
24967 /* A function-try-block begins with `try'. */
24968 || token
->keyword
== RID_TRY
24969 /* A function-transaction-block begins with `__transaction_atomic'
24970 or `__transaction_relaxed'. */
24971 || token
->keyword
== RID_TRANSACTION_ATOMIC
24972 || token
->keyword
== RID_TRANSACTION_RELAXED
24973 /* The named return value extension begins with `return'. */
24974 || token
->keyword
== RID_RETURN
);
24977 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24981 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24985 token
= cp_lexer_peek_token (parser
->lexer
);
24986 return (token
->type
== CPP_OPEN_BRACE
24987 || (token
->type
== CPP_COLON
24988 && !parser
->colon_doesnt_start_class_def_p
));
24991 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24992 C++0x) ending a template-argument. */
24995 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24999 token
= cp_lexer_peek_token (parser
->lexer
);
25000 return (token
->type
== CPP_COMMA
25001 || token
->type
== CPP_GREATER
25002 || token
->type
== CPP_ELLIPSIS
25003 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
25006 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25007 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25010 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
25015 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25016 if (token
->type
== CPP_LESS
)
25018 /* Check for the sequence `<::' in the original code. It would be lexed as
25019 `[:', where `[' is a digraph, and there is no whitespace before
25021 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
25024 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
25025 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
25031 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25032 or none_type otherwise. */
25034 static enum tag_types
25035 cp_parser_token_is_class_key (cp_token
* token
)
25037 switch (token
->keyword
)
25042 return record_type
;
25051 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25052 or none_type otherwise or if the token is null. */
25054 static enum tag_types
25055 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25060 switch (token
->keyword
)
25065 return typename_type
;
25072 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25075 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25077 if (type
== error_mark_node
)
25079 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25081 if (permerror (input_location
, "%qs tag used in naming %q#T",
25082 class_key
== union_type
? "union"
25083 : class_key
== record_type
? "struct" : "class",
25085 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25086 "%q#T was previously declared here", type
);
25090 /* Issue an error message if DECL is redeclared with different
25091 access than its original declaration [class.access.spec/3].
25092 This applies to nested classes and nested class templates.
25096 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25098 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25101 if ((TREE_PRIVATE (decl
)
25102 != (current_access_specifier
== access_private_node
))
25103 || (TREE_PROTECTED (decl
)
25104 != (current_access_specifier
== access_protected_node
)))
25105 error_at (location
, "%qD redeclared with different access", decl
);
25108 /* Look for the `template' keyword, as a syntactic disambiguator.
25109 Return TRUE iff it is present, in which case it will be
25113 cp_parser_optional_template_keyword (cp_parser
*parser
)
25115 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25117 /* In C++98 the `template' keyword can only be used within templates;
25118 outside templates the parser can always figure out what is a
25119 template and what is not. In C++11, per the resolution of DR 468,
25120 `template' is allowed in cases where it is not strictly necessary. */
25121 if (!processing_template_decl
25122 && pedantic
&& cxx_dialect
== cxx98
)
25124 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25125 pedwarn (token
->location
, OPT_Wpedantic
,
25126 "in C++98 %<template%> (as a disambiguator) is only "
25127 "allowed within templates");
25128 /* If this part of the token stream is rescanned, the same
25129 error message would be generated. So, we purge the token
25130 from the stream. */
25131 cp_lexer_purge_token (parser
->lexer
);
25136 /* Consume the `template' keyword. */
25137 cp_lexer_consume_token (parser
->lexer
);
25144 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25145 set PARSER->SCOPE, and perform other related actions. */
25148 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25151 struct tree_check
*check_value
;
25152 deferred_access_check
*chk
;
25153 vec
<deferred_access_check
, va_gc
> *checks
;
25155 /* Get the stored value. */
25156 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25157 /* Perform any access checks that were deferred. */
25158 checks
= check_value
->checks
;
25161 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25162 perform_or_defer_access_check (chk
->binfo
,
25164 chk
->diag_decl
, tf_warning_or_error
);
25166 /* Set the scope from the stored value. */
25167 parser
->scope
= check_value
->value
;
25168 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25169 parser
->object_scope
= NULL_TREE
;
25172 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25173 encounter the end of a block before what we were looking for. */
25176 cp_parser_cache_group (cp_parser
*parser
,
25177 enum cpp_ttype end
,
25182 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25184 /* Abort a parenthesized expression if we encounter a semicolon. */
25185 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25186 && token
->type
== CPP_SEMICOLON
)
25188 /* If we've reached the end of the file, stop. */
25189 if (token
->type
== CPP_EOF
25190 || (end
!= CPP_PRAGMA_EOL
25191 && token
->type
== CPP_PRAGMA_EOL
))
25193 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25194 /* We've hit the end of an enclosing block, so there's been some
25195 kind of syntax error. */
25198 /* Consume the token. */
25199 cp_lexer_consume_token (parser
->lexer
);
25200 /* See if it starts a new group. */
25201 if (token
->type
== CPP_OPEN_BRACE
)
25203 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25204 /* In theory this should probably check end == '}', but
25205 cp_parser_save_member_function_body needs it to exit
25206 after either '}' or ')' when called with ')'. */
25210 else if (token
->type
== CPP_OPEN_PAREN
)
25212 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25213 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25216 else if (token
->type
== CPP_PRAGMA
)
25217 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25218 else if (token
->type
== end
)
25223 /* Like above, for caching a default argument or NSDMI. Both of these are
25224 terminated by a non-nested comma, but it can be unclear whether or not a
25225 comma is nested in a template argument list unless we do more parsing.
25226 In order to handle this ambiguity, when we encounter a ',' after a '<'
25227 we try to parse what follows as a parameter-declaration-list (in the
25228 case of a default argument) or a member-declarator (in the case of an
25229 NSDMI). If that succeeds, then we stop caching. */
25232 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25234 unsigned depth
= 0;
25235 int maybe_template_id
= 0;
25236 cp_token
*first_token
;
25238 tree default_argument
;
25240 /* Add tokens until we have processed the entire default
25241 argument. We add the range [first_token, token). */
25242 first_token
= cp_lexer_peek_token (parser
->lexer
);
25243 if (first_token
->type
== CPP_OPEN_BRACE
)
25245 /* For list-initialization, this is straightforward. */
25246 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25247 token
= cp_lexer_peek_token (parser
->lexer
);
25253 /* Peek at the next token. */
25254 token
= cp_lexer_peek_token (parser
->lexer
);
25255 /* What we do depends on what token we have. */
25256 switch (token
->type
)
25258 /* In valid code, a default argument must be
25259 immediately followed by a `,' `)', or `...'. */
25261 if (depth
== 0 && maybe_template_id
)
25263 /* If we've seen a '<', we might be in a
25264 template-argument-list. Until Core issue 325 is
25265 resolved, we don't know how this situation ought
25266 to be handled, so try to DTRT. We check whether
25267 what comes after the comma is a valid parameter
25268 declaration list. If it is, then the comma ends
25269 the default argument; otherwise the default
25270 argument continues. */
25271 bool error
= false;
25273 /* Set ITALP so cp_parser_parameter_declaration_list
25274 doesn't decide to commit to this parse. */
25275 bool saved_italp
= parser
->in_template_argument_list_p
;
25276 parser
->in_template_argument_list_p
= true;
25278 cp_parser_parse_tentatively (parser
);
25279 cp_lexer_consume_token (parser
->lexer
);
25283 int ctor_dtor_or_conv_p
;
25284 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25285 &ctor_dtor_or_conv_p
,
25286 /*parenthesized_p=*/NULL
,
25288 /*friend_p=*/false);
25292 begin_scope (sk_function_parms
, NULL_TREE
);
25293 cp_parser_parameter_declaration_list (parser
, &error
);
25294 pop_bindings_and_leave_scope ();
25296 if (!cp_parser_error_occurred (parser
) && !error
)
25298 cp_parser_abort_tentative_parse (parser
);
25300 parser
->in_template_argument_list_p
= saved_italp
;
25303 case CPP_CLOSE_PAREN
:
25305 /* If we run into a non-nested `;', `}', or `]',
25306 then the code is invalid -- but the default
25307 argument is certainly over. */
25308 case CPP_SEMICOLON
:
25309 case CPP_CLOSE_BRACE
:
25310 case CPP_CLOSE_SQUARE
:
25312 /* Handle correctly int n = sizeof ... ( p ); */
25313 && token
->type
!= CPP_ELLIPSIS
)
25315 /* Update DEPTH, if necessary. */
25316 else if (token
->type
== CPP_CLOSE_PAREN
25317 || token
->type
== CPP_CLOSE_BRACE
25318 || token
->type
== CPP_CLOSE_SQUARE
)
25322 case CPP_OPEN_PAREN
:
25323 case CPP_OPEN_SQUARE
:
25324 case CPP_OPEN_BRACE
:
25330 /* This might be the comparison operator, or it might
25331 start a template argument list. */
25332 ++maybe_template_id
;
25336 if (cxx_dialect
== cxx98
)
25338 /* Fall through for C++0x, which treats the `>>'
25339 operator like two `>' tokens in certain
25345 /* This might be an operator, or it might close a
25346 template argument list. But if a previous '<'
25347 started a template argument list, this will have
25348 closed it, so we can't be in one anymore. */
25349 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25350 if (maybe_template_id
< 0)
25351 maybe_template_id
= 0;
25355 /* If we run out of tokens, issue an error message. */
25357 case CPP_PRAGMA_EOL
:
25358 error_at (token
->location
, "file ends in default argument");
25364 /* In these cases, we should look for template-ids.
25365 For example, if the default argument is
25366 `X<int, double>()', we need to do name lookup to
25367 figure out whether or not `X' is a template; if
25368 so, the `,' does not end the default argument.
25370 That is not yet done. */
25377 /* If we've reached the end, stop. */
25381 /* Add the token to the token block. */
25382 token
= cp_lexer_consume_token (parser
->lexer
);
25385 /* Create a DEFAULT_ARG to represent the unparsed default
25387 default_argument
= make_node (DEFAULT_ARG
);
25388 DEFARG_TOKENS (default_argument
)
25389 = cp_token_cache_new (first_token
, token
);
25390 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25392 return default_argument
;
25395 /* Begin parsing tentatively. We always save tokens while parsing
25396 tentatively so that if the tentative parsing fails we can restore the
25400 cp_parser_parse_tentatively (cp_parser
* parser
)
25402 /* Enter a new parsing context. */
25403 parser
->context
= cp_parser_context_new (parser
->context
);
25404 /* Begin saving tokens. */
25405 cp_lexer_save_tokens (parser
->lexer
);
25406 /* In order to avoid repetitive access control error messages,
25407 access checks are queued up until we are no longer parsing
25409 push_deferring_access_checks (dk_deferred
);
25412 /* Commit to the currently active tentative parse. */
25415 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25417 cp_parser_context
*context
;
25420 /* Mark all of the levels as committed. */
25421 lexer
= parser
->lexer
;
25422 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25424 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25426 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25427 while (!cp_lexer_saving_tokens (lexer
))
25428 lexer
= lexer
->next
;
25429 cp_lexer_commit_tokens (lexer
);
25433 /* Commit to the topmost currently active tentative parse.
25435 Note that this function shouldn't be called when there are
25436 irreversible side-effects while in a tentative state. For
25437 example, we shouldn't create a permanent entry in the symbol
25438 table, or issue an error message that might not apply if the
25439 tentative parse is aborted. */
25442 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25444 cp_parser_context
*context
= parser
->context
;
25445 cp_lexer
*lexer
= parser
->lexer
;
25449 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25451 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25453 while (!cp_lexer_saving_tokens (lexer
))
25454 lexer
= lexer
->next
;
25455 cp_lexer_commit_tokens (lexer
);
25459 /* Abort the currently active tentative parse. All consumed tokens
25460 will be rolled back, and no diagnostics will be issued. */
25463 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25465 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25466 || errorcount
> 0);
25467 cp_parser_simulate_error (parser
);
25468 /* Now, pretend that we want to see if the construct was
25469 successfully parsed. */
25470 cp_parser_parse_definitely (parser
);
25473 /* Stop parsing tentatively. If a parse error has occurred, restore the
25474 token stream. Otherwise, commit to the tokens we have consumed.
25475 Returns true if no error occurred; false otherwise. */
25478 cp_parser_parse_definitely (cp_parser
* parser
)
25480 bool error_occurred
;
25481 cp_parser_context
*context
;
25483 /* Remember whether or not an error occurred, since we are about to
25484 destroy that information. */
25485 error_occurred
= cp_parser_error_occurred (parser
);
25486 /* Remove the topmost context from the stack. */
25487 context
= parser
->context
;
25488 parser
->context
= context
->next
;
25489 /* If no parse errors occurred, commit to the tentative parse. */
25490 if (!error_occurred
)
25492 /* Commit to the tokens read tentatively, unless that was
25494 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25495 cp_lexer_commit_tokens (parser
->lexer
);
25497 pop_to_parent_deferring_access_checks ();
25499 /* Otherwise, if errors occurred, roll back our state so that things
25500 are just as they were before we began the tentative parse. */
25503 cp_lexer_rollback_tokens (parser
->lexer
);
25504 pop_deferring_access_checks ();
25506 /* Add the context to the front of the free list. */
25507 context
->next
= cp_parser_context_free_list
;
25508 cp_parser_context_free_list
= context
;
25510 return !error_occurred
;
25513 /* Returns true if we are parsing tentatively and are not committed to
25514 this tentative parse. */
25517 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25519 return (cp_parser_parsing_tentatively (parser
)
25520 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25523 /* Returns nonzero iff an error has occurred during the most recent
25524 tentative parse. */
25527 cp_parser_error_occurred (cp_parser
* parser
)
25529 return (cp_parser_parsing_tentatively (parser
)
25530 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25533 /* Returns nonzero if GNU extensions are allowed. */
25536 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25538 return parser
->allow_gnu_extensions_p
;
25541 /* Objective-C++ Productions */
25544 /* Parse an Objective-C expression, which feeds into a primary-expression
25548 objc-message-expression
25549 objc-string-literal
25550 objc-encode-expression
25551 objc-protocol-expression
25552 objc-selector-expression
25554 Returns a tree representation of the expression. */
25557 cp_parser_objc_expression (cp_parser
* parser
)
25559 /* Try to figure out what kind of declaration is present. */
25560 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25564 case CPP_OPEN_SQUARE
:
25565 return cp_parser_objc_message_expression (parser
);
25567 case CPP_OBJC_STRING
:
25568 kwd
= cp_lexer_consume_token (parser
->lexer
);
25569 return objc_build_string_object (kwd
->u
.value
);
25572 switch (kwd
->keyword
)
25574 case RID_AT_ENCODE
:
25575 return cp_parser_objc_encode_expression (parser
);
25577 case RID_AT_PROTOCOL
:
25578 return cp_parser_objc_protocol_expression (parser
);
25580 case RID_AT_SELECTOR
:
25581 return cp_parser_objc_selector_expression (parser
);
25587 error_at (kwd
->location
,
25588 "misplaced %<@%D%> Objective-C++ construct",
25590 cp_parser_skip_to_end_of_block_or_statement (parser
);
25593 return error_mark_node
;
25596 /* Parse an Objective-C message expression.
25598 objc-message-expression:
25599 [ objc-message-receiver objc-message-args ]
25601 Returns a representation of an Objective-C message. */
25604 cp_parser_objc_message_expression (cp_parser
* parser
)
25606 tree receiver
, messageargs
;
25608 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25609 receiver
= cp_parser_objc_message_receiver (parser
);
25610 messageargs
= cp_parser_objc_message_args (parser
);
25611 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25613 return objc_build_message_expr (receiver
, messageargs
);
25616 /* Parse an objc-message-receiver.
25618 objc-message-receiver:
25620 simple-type-specifier
25622 Returns a representation of the type or expression. */
25625 cp_parser_objc_message_receiver (cp_parser
* parser
)
25629 /* An Objective-C message receiver may be either (1) a type
25630 or (2) an expression. */
25631 cp_parser_parse_tentatively (parser
);
25632 rcv
= cp_parser_expression (parser
);
25634 if (cp_parser_parse_definitely (parser
))
25637 rcv
= cp_parser_simple_type_specifier (parser
,
25638 /*decl_specs=*/NULL
,
25639 CP_PARSER_FLAGS_NONE
);
25641 return objc_get_class_reference (rcv
);
25644 /* Parse the arguments and selectors comprising an Objective-C message.
25649 objc-selector-args , objc-comma-args
25651 objc-selector-args:
25652 objc-selector [opt] : assignment-expression
25653 objc-selector-args objc-selector [opt] : assignment-expression
25656 assignment-expression
25657 objc-comma-args , assignment-expression
25659 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25660 selector arguments and TREE_VALUE containing a list of comma
25664 cp_parser_objc_message_args (cp_parser
* parser
)
25666 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25667 bool maybe_unary_selector_p
= true;
25668 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25670 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25672 tree selector
= NULL_TREE
, arg
;
25674 if (token
->type
!= CPP_COLON
)
25675 selector
= cp_parser_objc_selector (parser
);
25677 /* Detect if we have a unary selector. */
25678 if (maybe_unary_selector_p
25679 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25680 return build_tree_list (selector
, NULL_TREE
);
25682 maybe_unary_selector_p
= false;
25683 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25684 arg
= cp_parser_assignment_expression (parser
);
25687 = chainon (sel_args
,
25688 build_tree_list (selector
, arg
));
25690 token
= cp_lexer_peek_token (parser
->lexer
);
25693 /* Handle non-selector arguments, if any. */
25694 while (token
->type
== CPP_COMMA
)
25698 cp_lexer_consume_token (parser
->lexer
);
25699 arg
= cp_parser_assignment_expression (parser
);
25702 = chainon (addl_args
,
25703 build_tree_list (NULL_TREE
, arg
));
25705 token
= cp_lexer_peek_token (parser
->lexer
);
25708 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25710 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25711 return build_tree_list (error_mark_node
, error_mark_node
);
25714 return build_tree_list (sel_args
, addl_args
);
25717 /* Parse an Objective-C encode expression.
25719 objc-encode-expression:
25720 @encode objc-typename
25722 Returns an encoded representation of the type argument. */
25725 cp_parser_objc_encode_expression (cp_parser
* parser
)
25730 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25731 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25732 token
= cp_lexer_peek_token (parser
->lexer
);
25733 type
= complete_type (cp_parser_type_id (parser
));
25734 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25738 error_at (token
->location
,
25739 "%<@encode%> must specify a type as an argument");
25740 return error_mark_node
;
25743 /* This happens if we find @encode(T) (where T is a template
25744 typename or something dependent on a template typename) when
25745 parsing a template. In that case, we can't compile it
25746 immediately, but we rather create an AT_ENCODE_EXPR which will
25747 need to be instantiated when the template is used.
25749 if (dependent_type_p (type
))
25751 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25752 TREE_READONLY (value
) = 1;
25756 return objc_build_encode_expr (type
);
25759 /* Parse an Objective-C @defs expression. */
25762 cp_parser_objc_defs_expression (cp_parser
*parser
)
25766 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25767 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25768 name
= cp_parser_identifier (parser
);
25769 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25771 return objc_get_class_ivars (name
);
25774 /* Parse an Objective-C protocol expression.
25776 objc-protocol-expression:
25777 @protocol ( identifier )
25779 Returns a representation of the protocol expression. */
25782 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25786 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25787 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25788 proto
= cp_parser_identifier (parser
);
25789 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25791 return objc_build_protocol_expr (proto
);
25794 /* Parse an Objective-C selector expression.
25796 objc-selector-expression:
25797 @selector ( objc-method-signature )
25799 objc-method-signature:
25805 objc-selector-seq objc-selector :
25807 Returns a representation of the method selector. */
25810 cp_parser_objc_selector_expression (cp_parser
* parser
)
25812 tree sel_seq
= NULL_TREE
;
25813 bool maybe_unary_selector_p
= true;
25815 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25817 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25818 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25819 token
= cp_lexer_peek_token (parser
->lexer
);
25821 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25822 || token
->type
== CPP_SCOPE
)
25824 tree selector
= NULL_TREE
;
25826 if (token
->type
!= CPP_COLON
25827 || token
->type
== CPP_SCOPE
)
25828 selector
= cp_parser_objc_selector (parser
);
25830 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25831 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25833 /* Detect if we have a unary selector. */
25834 if (maybe_unary_selector_p
)
25836 sel_seq
= selector
;
25837 goto finish_selector
;
25841 cp_parser_error (parser
, "expected %<:%>");
25844 maybe_unary_selector_p
= false;
25845 token
= cp_lexer_consume_token (parser
->lexer
);
25847 if (token
->type
== CPP_SCOPE
)
25850 = chainon (sel_seq
,
25851 build_tree_list (selector
, NULL_TREE
));
25853 = chainon (sel_seq
,
25854 build_tree_list (NULL_TREE
, NULL_TREE
));
25858 = chainon (sel_seq
,
25859 build_tree_list (selector
, NULL_TREE
));
25861 token
= cp_lexer_peek_token (parser
->lexer
);
25865 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25867 return objc_build_selector_expr (loc
, sel_seq
);
25870 /* Parse a list of identifiers.
25872 objc-identifier-list:
25874 objc-identifier-list , identifier
25876 Returns a TREE_LIST of identifier nodes. */
25879 cp_parser_objc_identifier_list (cp_parser
* parser
)
25885 identifier
= cp_parser_identifier (parser
);
25886 if (identifier
== error_mark_node
)
25887 return error_mark_node
;
25889 list
= build_tree_list (NULL_TREE
, identifier
);
25890 sep
= cp_lexer_peek_token (parser
->lexer
);
25892 while (sep
->type
== CPP_COMMA
)
25894 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25895 identifier
= cp_parser_identifier (parser
);
25896 if (identifier
== error_mark_node
)
25899 list
= chainon (list
, build_tree_list (NULL_TREE
,
25901 sep
= cp_lexer_peek_token (parser
->lexer
);
25907 /* Parse an Objective-C alias declaration.
25909 objc-alias-declaration:
25910 @compatibility_alias identifier identifier ;
25912 This function registers the alias mapping with the Objective-C front end.
25913 It returns nothing. */
25916 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25920 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25921 alias
= cp_parser_identifier (parser
);
25922 orig
= cp_parser_identifier (parser
);
25923 objc_declare_alias (alias
, orig
);
25924 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25927 /* Parse an Objective-C class forward-declaration.
25929 objc-class-declaration:
25930 @class objc-identifier-list ;
25932 The function registers the forward declarations with the Objective-C
25933 front end. It returns nothing. */
25936 cp_parser_objc_class_declaration (cp_parser
* parser
)
25938 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25943 id
= cp_parser_identifier (parser
);
25944 if (id
== error_mark_node
)
25947 objc_declare_class (id
);
25949 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25950 cp_lexer_consume_token (parser
->lexer
);
25954 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25957 /* Parse a list of Objective-C protocol references.
25959 objc-protocol-refs-opt:
25960 objc-protocol-refs [opt]
25962 objc-protocol-refs:
25963 < objc-identifier-list >
25965 Returns a TREE_LIST of identifiers, if any. */
25968 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25970 tree protorefs
= NULL_TREE
;
25972 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25974 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25975 protorefs
= cp_parser_objc_identifier_list (parser
);
25976 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25982 /* Parse a Objective-C visibility specification. */
25985 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25987 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25989 switch (vis
->keyword
)
25991 case RID_AT_PRIVATE
:
25992 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25994 case RID_AT_PROTECTED
:
25995 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25997 case RID_AT_PUBLIC
:
25998 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
26000 case RID_AT_PACKAGE
:
26001 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
26007 /* Eat '@private'/'@protected'/'@public'. */
26008 cp_lexer_consume_token (parser
->lexer
);
26011 /* Parse an Objective-C method type. Return 'true' if it is a class
26012 (+) method, and 'false' if it is an instance (-) method. */
26015 cp_parser_objc_method_type (cp_parser
* parser
)
26017 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
26023 /* Parse an Objective-C protocol qualifier. */
26026 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
26028 tree quals
= NULL_TREE
, node
;
26029 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26031 node
= token
->u
.value
;
26033 while (node
&& identifier_p (node
)
26034 && (node
== ridpointers
[(int) RID_IN
]
26035 || node
== ridpointers
[(int) RID_OUT
]
26036 || node
== ridpointers
[(int) RID_INOUT
]
26037 || node
== ridpointers
[(int) RID_BYCOPY
]
26038 || node
== ridpointers
[(int) RID_BYREF
]
26039 || node
== ridpointers
[(int) RID_ONEWAY
]))
26041 quals
= tree_cons (NULL_TREE
, node
, quals
);
26042 cp_lexer_consume_token (parser
->lexer
);
26043 token
= cp_lexer_peek_token (parser
->lexer
);
26044 node
= token
->u
.value
;
26050 /* Parse an Objective-C typename. */
26053 cp_parser_objc_typename (cp_parser
* parser
)
26055 tree type_name
= NULL_TREE
;
26057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26059 tree proto_quals
, cp_type
= NULL_TREE
;
26061 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26062 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26064 /* An ObjC type name may consist of just protocol qualifiers, in which
26065 case the type shall default to 'id'. */
26066 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26068 cp_type
= cp_parser_type_id (parser
);
26070 /* If the type could not be parsed, an error has already
26071 been produced. For error recovery, behave as if it had
26072 not been specified, which will use the default type
26074 if (cp_type
== error_mark_node
)
26076 cp_type
= NULL_TREE
;
26077 /* We need to skip to the closing parenthesis as
26078 cp_parser_type_id() does not seem to do it for
26080 cp_parser_skip_to_closing_parenthesis (parser
,
26081 /*recovering=*/true,
26082 /*or_comma=*/false,
26083 /*consume_paren=*/false);
26087 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26088 type_name
= build_tree_list (proto_quals
, cp_type
);
26094 /* Check to see if TYPE refers to an Objective-C selector name. */
26097 cp_parser_objc_selector_p (enum cpp_ttype type
)
26099 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26100 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26101 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26102 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26103 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26106 /* Parse an Objective-C selector. */
26109 cp_parser_objc_selector (cp_parser
* parser
)
26111 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26113 if (!cp_parser_objc_selector_p (token
->type
))
26115 error_at (token
->location
, "invalid Objective-C++ selector name");
26116 return error_mark_node
;
26119 /* C++ operator names are allowed to appear in ObjC selectors. */
26120 switch (token
->type
)
26122 case CPP_AND_AND
: return get_identifier ("and");
26123 case CPP_AND_EQ
: return get_identifier ("and_eq");
26124 case CPP_AND
: return get_identifier ("bitand");
26125 case CPP_OR
: return get_identifier ("bitor");
26126 case CPP_COMPL
: return get_identifier ("compl");
26127 case CPP_NOT
: return get_identifier ("not");
26128 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26129 case CPP_OR_OR
: return get_identifier ("or");
26130 case CPP_OR_EQ
: return get_identifier ("or_eq");
26131 case CPP_XOR
: return get_identifier ("xor");
26132 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26133 default: return token
->u
.value
;
26137 /* Parse an Objective-C params list. */
26140 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26142 tree params
= NULL_TREE
;
26143 bool maybe_unary_selector_p
= true;
26144 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26146 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26148 tree selector
= NULL_TREE
, type_name
, identifier
;
26149 tree parm_attr
= NULL_TREE
;
26151 if (token
->keyword
== RID_ATTRIBUTE
)
26154 if (token
->type
!= CPP_COLON
)
26155 selector
= cp_parser_objc_selector (parser
);
26157 /* Detect if we have a unary selector. */
26158 if (maybe_unary_selector_p
26159 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26161 params
= selector
; /* Might be followed by attributes. */
26165 maybe_unary_selector_p
= false;
26166 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26168 /* Something went quite wrong. There should be a colon
26169 here, but there is not. Stop parsing parameters. */
26172 type_name
= cp_parser_objc_typename (parser
);
26173 /* New ObjC allows attributes on parameters too. */
26174 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26175 parm_attr
= cp_parser_attributes_opt (parser
);
26176 identifier
= cp_parser_identifier (parser
);
26180 objc_build_keyword_decl (selector
,
26185 token
= cp_lexer_peek_token (parser
->lexer
);
26188 if (params
== NULL_TREE
)
26190 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26191 return error_mark_node
;
26194 /* We allow tail attributes for the method. */
26195 if (token
->keyword
== RID_ATTRIBUTE
)
26197 *attributes
= cp_parser_attributes_opt (parser
);
26198 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26199 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26201 cp_parser_error (parser
,
26202 "method attributes must be specified at the end");
26203 return error_mark_node
;
26206 if (params
== NULL_TREE
)
26208 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26209 return error_mark_node
;
26214 /* Parse the non-keyword Objective-C params. */
26217 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26220 tree params
= make_node (TREE_LIST
);
26221 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26222 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26224 while (token
->type
== CPP_COMMA
)
26226 cp_parameter_declarator
*parmdecl
;
26229 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26230 token
= cp_lexer_peek_token (parser
->lexer
);
26232 if (token
->type
== CPP_ELLIPSIS
)
26234 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26236 token
= cp_lexer_peek_token (parser
->lexer
);
26240 /* TODO: parse attributes for tail parameters. */
26241 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26242 parm
= grokdeclarator (parmdecl
->declarator
,
26243 &parmdecl
->decl_specifiers
,
26244 PARM
, /*initialized=*/0,
26245 /*attrlist=*/NULL
);
26247 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26248 token
= cp_lexer_peek_token (parser
->lexer
);
26251 /* We allow tail attributes for the method. */
26252 if (token
->keyword
== RID_ATTRIBUTE
)
26254 if (*attributes
== NULL_TREE
)
26256 *attributes
= cp_parser_attributes_opt (parser
);
26257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26258 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26262 /* We have an error, but parse the attributes, so that we can
26264 *attributes
= cp_parser_attributes_opt (parser
);
26266 cp_parser_error (parser
,
26267 "method attributes must be specified at the end");
26268 return error_mark_node
;
26274 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26277 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26279 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26281 /* If the next token is `extern' and the following token is a string
26282 literal, then we have a linkage specification. */
26283 if (token
->keyword
== RID_EXTERN
26284 && cp_parser_is_pure_string_literal
26285 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26286 cp_parser_linkage_specification (parser
);
26287 /* Handle #pragma, if any. */
26288 else if (token
->type
== CPP_PRAGMA
)
26289 cp_parser_pragma (parser
, pragma_objc_icode
);
26290 /* Allow stray semicolons. */
26291 else if (token
->type
== CPP_SEMICOLON
)
26292 cp_lexer_consume_token (parser
->lexer
);
26293 /* Mark methods as optional or required, when building protocols. */
26294 else if (token
->keyword
== RID_AT_OPTIONAL
)
26296 cp_lexer_consume_token (parser
->lexer
);
26297 objc_set_method_opt (true);
26299 else if (token
->keyword
== RID_AT_REQUIRED
)
26301 cp_lexer_consume_token (parser
->lexer
);
26302 objc_set_method_opt (false);
26304 else if (token
->keyword
== RID_NAMESPACE
)
26305 cp_parser_namespace_definition (parser
);
26306 /* Other stray characters must generate errors. */
26307 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26309 cp_lexer_consume_token (parser
->lexer
);
26310 error ("stray %qs between Objective-C++ methods",
26311 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26313 /* Finally, try to parse a block-declaration, or a function-definition. */
26315 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26318 /* Parse a method signature. */
26321 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26323 tree rettype
, kwdparms
, optparms
;
26324 bool ellipsis
= false;
26325 bool is_class_method
;
26327 is_class_method
= cp_parser_objc_method_type (parser
);
26328 rettype
= cp_parser_objc_typename (parser
);
26329 *attributes
= NULL_TREE
;
26330 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26331 if (kwdparms
== error_mark_node
)
26332 return error_mark_node
;
26333 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26334 if (optparms
== error_mark_node
)
26335 return error_mark_node
;
26337 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26341 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26344 cp_lexer_save_tokens (parser
->lexer
);
26345 tattr
= cp_parser_attributes_opt (parser
);
26346 gcc_assert (tattr
) ;
26348 /* If the attributes are followed by a method introducer, this is not allowed.
26349 Dump the attributes and flag the situation. */
26350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26351 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26354 /* Otherwise, the attributes introduce some interstitial code, possibly so
26355 rewind to allow that check. */
26356 cp_lexer_rollback_tokens (parser
->lexer
);
26360 /* Parse an Objective-C method prototype list. */
26363 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26365 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26367 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26369 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26371 tree attributes
, sig
;
26372 bool is_class_method
;
26373 if (token
->type
== CPP_PLUS
)
26374 is_class_method
= true;
26376 is_class_method
= false;
26377 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26378 if (sig
== error_mark_node
)
26380 cp_parser_skip_to_end_of_block_or_statement (parser
);
26381 token
= cp_lexer_peek_token (parser
->lexer
);
26384 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26385 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26387 else if (token
->keyword
== RID_AT_PROPERTY
)
26388 cp_parser_objc_at_property_declaration (parser
);
26389 else if (token
->keyword
== RID_ATTRIBUTE
26390 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26391 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26393 "prefix attributes are ignored for methods");
26395 /* Allow for interspersed non-ObjC++ code. */
26396 cp_parser_objc_interstitial_code (parser
);
26398 token
= cp_lexer_peek_token (parser
->lexer
);
26401 if (token
->type
!= CPP_EOF
)
26402 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26404 cp_parser_error (parser
, "expected %<@end%>");
26406 objc_finish_interface ();
26409 /* Parse an Objective-C method definition list. */
26412 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26414 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26416 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26420 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26423 tree sig
, attribute
;
26424 bool is_class_method
;
26425 if (token
->type
== CPP_PLUS
)
26426 is_class_method
= true;
26428 is_class_method
= false;
26429 push_deferring_access_checks (dk_deferred
);
26430 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26431 if (sig
== error_mark_node
)
26433 cp_parser_skip_to_end_of_block_or_statement (parser
);
26434 token
= cp_lexer_peek_token (parser
->lexer
);
26437 objc_start_method_definition (is_class_method
, sig
, attribute
,
26440 /* For historical reasons, we accept an optional semicolon. */
26441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26442 cp_lexer_consume_token (parser
->lexer
);
26444 ptk
= cp_lexer_peek_token (parser
->lexer
);
26445 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26446 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26448 perform_deferred_access_checks (tf_warning_or_error
);
26449 stop_deferring_access_checks ();
26450 meth
= cp_parser_function_definition_after_declarator (parser
,
26452 pop_deferring_access_checks ();
26453 objc_finish_method_definition (meth
);
26456 /* The following case will be removed once @synthesize is
26457 completely implemented. */
26458 else if (token
->keyword
== RID_AT_PROPERTY
)
26459 cp_parser_objc_at_property_declaration (parser
);
26460 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26461 cp_parser_objc_at_synthesize_declaration (parser
);
26462 else if (token
->keyword
== RID_AT_DYNAMIC
)
26463 cp_parser_objc_at_dynamic_declaration (parser
);
26464 else if (token
->keyword
== RID_ATTRIBUTE
26465 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26466 warning_at (token
->location
, OPT_Wattributes
,
26467 "prefix attributes are ignored for methods");
26469 /* Allow for interspersed non-ObjC++ code. */
26470 cp_parser_objc_interstitial_code (parser
);
26472 token
= cp_lexer_peek_token (parser
->lexer
);
26475 if (token
->type
!= CPP_EOF
)
26476 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26478 cp_parser_error (parser
, "expected %<@end%>");
26480 objc_finish_implementation ();
26483 /* Parse Objective-C ivars. */
26486 cp_parser_objc_class_ivars (cp_parser
* parser
)
26488 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26490 if (token
->type
!= CPP_OPEN_BRACE
)
26491 return; /* No ivars specified. */
26493 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26494 token
= cp_lexer_peek_token (parser
->lexer
);
26496 while (token
->type
!= CPP_CLOSE_BRACE
26497 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26499 cp_decl_specifier_seq declspecs
;
26500 int decl_class_or_enum_p
;
26501 tree prefix_attributes
;
26503 cp_parser_objc_visibility_spec (parser
);
26505 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26508 cp_parser_decl_specifier_seq (parser
,
26509 CP_PARSER_FLAGS_OPTIONAL
,
26511 &decl_class_or_enum_p
);
26513 /* auto, register, static, extern, mutable. */
26514 if (declspecs
.storage_class
!= sc_none
)
26516 cp_parser_error (parser
, "invalid type for instance variable");
26517 declspecs
.storage_class
= sc_none
;
26520 /* thread_local. */
26521 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26523 cp_parser_error (parser
, "invalid type for instance variable");
26524 declspecs
.locations
[ds_thread
] = 0;
26528 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26530 cp_parser_error (parser
, "invalid type for instance variable");
26531 declspecs
.locations
[ds_typedef
] = 0;
26534 prefix_attributes
= declspecs
.attributes
;
26535 declspecs
.attributes
= NULL_TREE
;
26537 /* Keep going until we hit the `;' at the end of the
26539 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26541 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26542 cp_declarator
*declarator
= NULL
;
26543 int ctor_dtor_or_conv_p
;
26545 /* Check for a (possibly unnamed) bitfield declaration. */
26546 token
= cp_lexer_peek_token (parser
->lexer
);
26547 if (token
->type
== CPP_COLON
)
26550 if (token
->type
== CPP_NAME
26551 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26554 /* Get the name of the bitfield. */
26555 declarator
= make_id_declarator (NULL_TREE
,
26556 cp_parser_identifier (parser
),
26560 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26561 /* Get the width of the bitfield. */
26563 = cp_parser_constant_expression (parser
);
26567 /* Parse the declarator. */
26569 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26570 &ctor_dtor_or_conv_p
,
26571 /*parenthesized_p=*/NULL
,
26572 /*member_p=*/false,
26573 /*friend_p=*/false);
26576 /* Look for attributes that apply to the ivar. */
26577 attributes
= cp_parser_attributes_opt (parser
);
26578 /* Remember which attributes are prefix attributes and
26580 first_attribute
= attributes
;
26581 /* Combine the attributes. */
26582 attributes
= chainon (prefix_attributes
, attributes
);
26585 /* Create the bitfield declaration. */
26586 decl
= grokbitfield (declarator
, &declspecs
,
26590 decl
= grokfield (declarator
, &declspecs
,
26591 NULL_TREE
, /*init_const_expr_p=*/false,
26592 NULL_TREE
, attributes
);
26594 /* Add the instance variable. */
26595 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26596 objc_add_instance_variable (decl
);
26598 /* Reset PREFIX_ATTRIBUTES. */
26599 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26600 attributes
= TREE_CHAIN (attributes
);
26602 TREE_CHAIN (attributes
) = NULL_TREE
;
26604 token
= cp_lexer_peek_token (parser
->lexer
);
26606 if (token
->type
== CPP_COMMA
)
26608 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26614 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26615 token
= cp_lexer_peek_token (parser
->lexer
);
26618 if (token
->keyword
== RID_AT_END
)
26619 cp_parser_error (parser
, "expected %<}%>");
26621 /* Do not consume the RID_AT_END, so it will be read again as terminating
26622 the @interface of @implementation. */
26623 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26624 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26626 /* For historical reasons, we accept an optional semicolon. */
26627 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26628 cp_lexer_consume_token (parser
->lexer
);
26631 /* Parse an Objective-C protocol declaration. */
26634 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26636 tree proto
, protorefs
;
26639 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26642 tok
= cp_lexer_peek_token (parser
->lexer
);
26643 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26644 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26648 /* See if we have a forward declaration or a definition. */
26649 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26651 /* Try a forward declaration first. */
26652 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26658 id
= cp_parser_identifier (parser
);
26659 if (id
== error_mark_node
)
26662 objc_declare_protocol (id
, attributes
);
26664 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26665 cp_lexer_consume_token (parser
->lexer
);
26669 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26672 /* Ok, we got a full-fledged definition (or at least should). */
26675 proto
= cp_parser_identifier (parser
);
26676 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26677 objc_start_protocol (proto
, protorefs
, attributes
);
26678 cp_parser_objc_method_prototype_list (parser
);
26682 /* Parse an Objective-C superclass or category. */
26685 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26688 tree
*categ
, bool *is_class_extension
)
26690 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26692 *super
= *categ
= NULL_TREE
;
26693 *is_class_extension
= false;
26694 if (next
->type
== CPP_COLON
)
26696 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26697 *super
= cp_parser_identifier (parser
);
26699 else if (next
->type
== CPP_OPEN_PAREN
)
26701 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26703 /* If there is no category name, and this is an @interface, we
26704 have a class extension. */
26705 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26707 *categ
= NULL_TREE
;
26708 *is_class_extension
= true;
26711 *categ
= cp_parser_identifier (parser
);
26713 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26717 /* Parse an Objective-C class interface. */
26720 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26722 tree name
, super
, categ
, protos
;
26723 bool is_class_extension
;
26725 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26726 name
= cp_parser_identifier (parser
);
26727 if (name
== error_mark_node
)
26729 /* It's hard to recover because even if valid @interface stuff
26730 is to follow, we can't compile it (or validate it) if we
26731 don't even know which class it refers to. Let's assume this
26732 was a stray '@interface' token in the stream and skip it.
26736 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26737 &is_class_extension
);
26738 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26740 /* We have either a class or a category on our hands. */
26741 if (categ
|| is_class_extension
)
26742 objc_start_category_interface (name
, categ
, protos
, attributes
);
26745 objc_start_class_interface (name
, super
, protos
, attributes
);
26746 /* Handle instance variable declarations, if any. */
26747 cp_parser_objc_class_ivars (parser
);
26748 objc_continue_interface ();
26751 cp_parser_objc_method_prototype_list (parser
);
26754 /* Parse an Objective-C class implementation. */
26757 cp_parser_objc_class_implementation (cp_parser
* parser
)
26759 tree name
, super
, categ
;
26760 bool is_class_extension
;
26762 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26763 name
= cp_parser_identifier (parser
);
26764 if (name
== error_mark_node
)
26766 /* It's hard to recover because even if valid @implementation
26767 stuff is to follow, we can't compile it (or validate it) if
26768 we don't even know which class it refers to. Let's assume
26769 this was a stray '@implementation' token in the stream and
26774 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26775 &is_class_extension
);
26777 /* We have either a class or a category on our hands. */
26779 objc_start_category_implementation (name
, categ
);
26782 objc_start_class_implementation (name
, super
);
26783 /* Handle instance variable declarations, if any. */
26784 cp_parser_objc_class_ivars (parser
);
26785 objc_continue_implementation ();
26788 cp_parser_objc_method_definition_list (parser
);
26791 /* Consume the @end token and finish off the implementation. */
26794 cp_parser_objc_end_implementation (cp_parser
* parser
)
26796 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26797 objc_finish_implementation ();
26800 /* Parse an Objective-C declaration. */
26803 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26805 /* Try to figure out what kind of declaration is present. */
26806 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26809 switch (kwd
->keyword
)
26814 error_at (kwd
->location
, "attributes may not be specified before"
26815 " the %<@%D%> Objective-C++ keyword",
26819 case RID_AT_IMPLEMENTATION
:
26820 warning_at (kwd
->location
, OPT_Wattributes
,
26821 "prefix attributes are ignored before %<@%D%>",
26828 switch (kwd
->keyword
)
26831 cp_parser_objc_alias_declaration (parser
);
26834 cp_parser_objc_class_declaration (parser
);
26836 case RID_AT_PROTOCOL
:
26837 cp_parser_objc_protocol_declaration (parser
, attributes
);
26839 case RID_AT_INTERFACE
:
26840 cp_parser_objc_class_interface (parser
, attributes
);
26842 case RID_AT_IMPLEMENTATION
:
26843 cp_parser_objc_class_implementation (parser
);
26846 cp_parser_objc_end_implementation (parser
);
26849 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26851 cp_parser_skip_to_end_of_block_or_statement (parser
);
26855 /* Parse an Objective-C try-catch-finally statement.
26857 objc-try-catch-finally-stmt:
26858 @try compound-statement objc-catch-clause-seq [opt]
26859 objc-finally-clause [opt]
26861 objc-catch-clause-seq:
26862 objc-catch-clause objc-catch-clause-seq [opt]
26865 @catch ( objc-exception-declaration ) compound-statement
26867 objc-finally-clause:
26868 @finally compound-statement
26870 objc-exception-declaration:
26871 parameter-declaration
26874 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26878 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26879 for C. Keep them in sync. */
26882 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26884 location_t location
;
26887 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26888 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26889 objc_maybe_warn_exceptions (location
);
26890 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26891 node, lest it get absorbed into the surrounding block. */
26892 stmt
= push_stmt_list ();
26893 cp_parser_compound_statement (parser
, NULL
, false, false);
26894 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26896 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26898 cp_parameter_declarator
*parm
;
26899 tree parameter_declaration
= error_mark_node
;
26900 bool seen_open_paren
= false;
26902 cp_lexer_consume_token (parser
->lexer
);
26903 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26904 seen_open_paren
= true;
26905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26907 /* We have "@catch (...)" (where the '...' are literally
26908 what is in the code). Skip the '...'.
26909 parameter_declaration is set to NULL_TREE, and
26910 objc_being_catch_clauses() knows that that means
26912 cp_lexer_consume_token (parser
->lexer
);
26913 parameter_declaration
= NULL_TREE
;
26917 /* We have "@catch (NSException *exception)" or something
26918 like that. Parse the parameter declaration. */
26919 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26921 parameter_declaration
= error_mark_node
;
26923 parameter_declaration
= grokdeclarator (parm
->declarator
,
26924 &parm
->decl_specifiers
,
26925 PARM
, /*initialized=*/0,
26926 /*attrlist=*/NULL
);
26928 if (seen_open_paren
)
26929 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26932 /* If there was no open parenthesis, we are recovering from
26933 an error, and we are trying to figure out what mistake
26934 the user has made. */
26936 /* If there is an immediate closing parenthesis, the user
26937 probably forgot the opening one (ie, they typed "@catch
26938 NSException *e)". Parse the closing parenthesis and keep
26940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26941 cp_lexer_consume_token (parser
->lexer
);
26943 /* If these is no immediate closing parenthesis, the user
26944 probably doesn't know that parenthesis are required at
26945 all (ie, they typed "@catch NSException *e"). So, just
26946 forget about the closing parenthesis and keep going. */
26948 objc_begin_catch_clause (parameter_declaration
);
26949 cp_parser_compound_statement (parser
, NULL
, false, false);
26950 objc_finish_catch_clause ();
26952 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26954 cp_lexer_consume_token (parser
->lexer
);
26955 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26956 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26957 node, lest it get absorbed into the surrounding block. */
26958 stmt
= push_stmt_list ();
26959 cp_parser_compound_statement (parser
, NULL
, false, false);
26960 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26963 return objc_finish_try_stmt ();
26966 /* Parse an Objective-C synchronized statement.
26968 objc-synchronized-stmt:
26969 @synchronized ( expression ) compound-statement
26971 Returns NULL_TREE. */
26974 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26976 location_t location
;
26979 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26981 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26982 objc_maybe_warn_exceptions (location
);
26983 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26984 lock
= cp_parser_expression (parser
);
26985 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26987 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26988 node, lest it get absorbed into the surrounding block. */
26989 stmt
= push_stmt_list ();
26990 cp_parser_compound_statement (parser
, NULL
, false, false);
26992 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26995 /* Parse an Objective-C throw statement.
26998 @throw assignment-expression [opt] ;
27000 Returns a constructed '@throw' statement. */
27003 cp_parser_objc_throw_statement (cp_parser
*parser
)
27005 tree expr
= NULL_TREE
;
27006 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27008 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
27010 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27011 expr
= cp_parser_expression (parser
);
27013 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27015 return objc_build_throw_stmt (loc
, expr
);
27018 /* Parse an Objective-C statement. */
27021 cp_parser_objc_statement (cp_parser
* parser
)
27023 /* Try to figure out what kind of declaration is present. */
27024 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27026 switch (kwd
->keyword
)
27029 return cp_parser_objc_try_catch_finally_statement (parser
);
27030 case RID_AT_SYNCHRONIZED
:
27031 return cp_parser_objc_synchronized_statement (parser
);
27033 return cp_parser_objc_throw_statement (parser
);
27035 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27037 cp_parser_skip_to_end_of_block_or_statement (parser
);
27040 return error_mark_node
;
27043 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27044 look ahead to see if an objc keyword follows the attributes. This
27045 is to detect the use of prefix attributes on ObjC @interface and
27049 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27051 cp_lexer_save_tokens (parser
->lexer
);
27052 *attrib
= cp_parser_attributes_opt (parser
);
27053 gcc_assert (*attrib
);
27054 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27056 cp_lexer_commit_tokens (parser
->lexer
);
27059 cp_lexer_rollback_tokens (parser
->lexer
);
27063 /* This routine is a minimal replacement for
27064 c_parser_struct_declaration () used when parsing the list of
27065 types/names or ObjC++ properties. For example, when parsing the
27068 @property (readonly) int a, b, c;
27070 this function is responsible for parsing "int a, int b, int c" and
27071 returning the declarations as CHAIN of DECLs.
27073 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27074 similar parsing. */
27076 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27078 tree decls
= NULL_TREE
;
27079 cp_decl_specifier_seq declspecs
;
27080 int decl_class_or_enum_p
;
27081 tree prefix_attributes
;
27083 cp_parser_decl_specifier_seq (parser
,
27084 CP_PARSER_FLAGS_NONE
,
27086 &decl_class_or_enum_p
);
27088 if (declspecs
.type
== error_mark_node
)
27089 return error_mark_node
;
27091 /* auto, register, static, extern, mutable. */
27092 if (declspecs
.storage_class
!= sc_none
)
27094 cp_parser_error (parser
, "invalid type for property");
27095 declspecs
.storage_class
= sc_none
;
27098 /* thread_local. */
27099 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27101 cp_parser_error (parser
, "invalid type for property");
27102 declspecs
.locations
[ds_thread
] = 0;
27106 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27108 cp_parser_error (parser
, "invalid type for property");
27109 declspecs
.locations
[ds_typedef
] = 0;
27112 prefix_attributes
= declspecs
.attributes
;
27113 declspecs
.attributes
= NULL_TREE
;
27115 /* Keep going until we hit the `;' at the end of the declaration. */
27116 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27118 tree attributes
, first_attribute
, decl
;
27119 cp_declarator
*declarator
;
27122 /* Parse the declarator. */
27123 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27124 NULL
, NULL
, false, false);
27126 /* Look for attributes that apply to the ivar. */
27127 attributes
= cp_parser_attributes_opt (parser
);
27128 /* Remember which attributes are prefix attributes and
27130 first_attribute
= attributes
;
27131 /* Combine the attributes. */
27132 attributes
= chainon (prefix_attributes
, attributes
);
27134 decl
= grokfield (declarator
, &declspecs
,
27135 NULL_TREE
, /*init_const_expr_p=*/false,
27136 NULL_TREE
, attributes
);
27138 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27139 return error_mark_node
;
27141 /* Reset PREFIX_ATTRIBUTES. */
27142 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27143 attributes
= TREE_CHAIN (attributes
);
27145 TREE_CHAIN (attributes
) = NULL_TREE
;
27147 DECL_CHAIN (decl
) = decls
;
27150 token
= cp_lexer_peek_token (parser
->lexer
);
27151 if (token
->type
== CPP_COMMA
)
27153 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27162 /* Parse an Objective-C @property declaration. The syntax is:
27164 objc-property-declaration:
27165 '@property' objc-property-attributes[opt] struct-declaration ;
27167 objc-property-attributes:
27168 '(' objc-property-attribute-list ')'
27170 objc-property-attribute-list:
27171 objc-property-attribute
27172 objc-property-attribute-list, objc-property-attribute
27174 objc-property-attribute
27175 'getter' = identifier
27176 'setter' = identifier
27185 @property NSString *name;
27186 @property (readonly) id object;
27187 @property (retain, nonatomic, getter=getTheName) id name;
27188 @property int a, b, c;
27190 PS: This function is identical to
27191 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27193 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27195 /* The following variables hold the attributes of the properties as
27196 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27197 seen. When we see an attribute, we set them to 'true' (if they
27198 are boolean properties) or to the identifier (if they have an
27199 argument, ie, for getter and setter). Note that here we only
27200 parse the list of attributes, check the syntax and accumulate the
27201 attributes that we find. objc_add_property_declaration() will
27202 then process the information. */
27203 bool property_assign
= false;
27204 bool property_copy
= false;
27205 tree property_getter_ident
= NULL_TREE
;
27206 bool property_nonatomic
= false;
27207 bool property_readonly
= false;
27208 bool property_readwrite
= false;
27209 bool property_retain
= false;
27210 tree property_setter_ident
= NULL_TREE
;
27212 /* 'properties' is the list of properties that we read. Usually a
27213 single one, but maybe more (eg, in "@property int a, b, c;" there
27218 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27220 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27222 /* Parse the optional attribute list... */
27223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27226 cp_lexer_consume_token (parser
->lexer
);
27230 bool syntax_error
= false;
27231 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27234 if (token
->type
!= CPP_NAME
)
27236 cp_parser_error (parser
, "expected identifier");
27239 keyword
= C_RID_CODE (token
->u
.value
);
27240 cp_lexer_consume_token (parser
->lexer
);
27243 case RID_ASSIGN
: property_assign
= true; break;
27244 case RID_COPY
: property_copy
= true; break;
27245 case RID_NONATOMIC
: property_nonatomic
= true; break;
27246 case RID_READONLY
: property_readonly
= true; break;
27247 case RID_READWRITE
: property_readwrite
= true; break;
27248 case RID_RETAIN
: property_retain
= true; break;
27252 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27254 if (keyword
== RID_GETTER
)
27255 cp_parser_error (parser
,
27256 "missing %<=%> (after %<getter%> attribute)");
27258 cp_parser_error (parser
,
27259 "missing %<=%> (after %<setter%> attribute)");
27260 syntax_error
= true;
27263 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27264 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27266 cp_parser_error (parser
, "expected identifier");
27267 syntax_error
= true;
27270 if (keyword
== RID_SETTER
)
27272 if (property_setter_ident
!= NULL_TREE
)
27274 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27275 cp_lexer_consume_token (parser
->lexer
);
27278 property_setter_ident
= cp_parser_objc_selector (parser
);
27279 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27280 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27282 cp_lexer_consume_token (parser
->lexer
);
27286 if (property_getter_ident
!= NULL_TREE
)
27288 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27289 cp_lexer_consume_token (parser
->lexer
);
27292 property_getter_ident
= cp_parser_objc_selector (parser
);
27296 cp_parser_error (parser
, "unknown property attribute");
27297 syntax_error
= true;
27304 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27305 cp_lexer_consume_token (parser
->lexer
);
27310 /* FIXME: "@property (setter, assign);" will generate a spurious
27311 "error: expected ‘)’ before ‘,’ token". This is because
27312 cp_parser_require, unlike the C counterpart, will produce an
27313 error even if we are in error recovery. */
27314 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27316 cp_parser_skip_to_closing_parenthesis (parser
,
27317 /*recovering=*/true,
27318 /*or_comma=*/false,
27319 /*consume_paren=*/true);
27323 /* ... and the property declaration(s). */
27324 properties
= cp_parser_objc_struct_declaration (parser
);
27326 if (properties
== error_mark_node
)
27328 cp_parser_skip_to_end_of_statement (parser
);
27329 /* If the next token is now a `;', consume it. */
27330 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27331 cp_lexer_consume_token (parser
->lexer
);
27335 if (properties
== NULL_TREE
)
27336 cp_parser_error (parser
, "expected identifier");
27339 /* Comma-separated properties are chained together in
27340 reverse order; add them one by one. */
27341 properties
= nreverse (properties
);
27343 for (; properties
; properties
= TREE_CHAIN (properties
))
27344 objc_add_property_declaration (loc
, copy_node (properties
),
27345 property_readonly
, property_readwrite
,
27346 property_assign
, property_retain
,
27347 property_copy
, property_nonatomic
,
27348 property_getter_ident
, property_setter_ident
);
27351 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27354 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27356 objc-synthesize-declaration:
27357 @synthesize objc-synthesize-identifier-list ;
27359 objc-synthesize-identifier-list:
27360 objc-synthesize-identifier
27361 objc-synthesize-identifier-list, objc-synthesize-identifier
27363 objc-synthesize-identifier
27365 identifier = identifier
27368 @synthesize MyProperty;
27369 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27371 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27372 for C. Keep them in sync.
27375 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27377 tree list
= NULL_TREE
;
27379 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27381 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27384 tree property
, ivar
;
27385 property
= cp_parser_identifier (parser
);
27386 if (property
== error_mark_node
)
27388 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27393 cp_lexer_consume_token (parser
->lexer
);
27394 ivar
= cp_parser_identifier (parser
);
27395 if (ivar
== error_mark_node
)
27397 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27403 list
= chainon (list
, build_tree_list (ivar
, property
));
27404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27405 cp_lexer_consume_token (parser
->lexer
);
27409 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27410 objc_add_synthesize_declaration (loc
, list
);
27413 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27415 objc-dynamic-declaration:
27416 @dynamic identifier-list ;
27419 @dynamic MyProperty;
27420 @dynamic MyProperty, AnotherProperty;
27422 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27423 for C. Keep them in sync.
27426 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27428 tree list
= NULL_TREE
;
27430 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27432 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27436 property
= cp_parser_identifier (parser
);
27437 if (property
== error_mark_node
)
27439 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27442 list
= chainon (list
, build_tree_list (NULL
, property
));
27443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27444 cp_lexer_consume_token (parser
->lexer
);
27448 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27449 objc_add_dynamic_declaration (loc
, list
);
27453 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27455 /* Returns name of the next clause.
27456 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27457 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27458 returned and the token is consumed. */
27460 static pragma_omp_clause
27461 cp_parser_omp_clause_name (cp_parser
*parser
)
27463 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27465 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27466 result
= PRAGMA_OMP_CLAUSE_IF
;
27467 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27468 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27469 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
27470 result
= PRAGMA_OACC_CLAUSE_DELETE
;
27471 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27472 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27473 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27474 result
= PRAGMA_OMP_CLAUSE_FOR
;
27475 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27477 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27478 const char *p
= IDENTIFIER_POINTER (id
);
27483 if (!strcmp ("aligned", p
))
27484 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27485 else if (!strcmp ("async", p
))
27486 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
27489 if (!strcmp ("collapse", p
))
27490 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27491 else if (!strcmp ("copy", p
))
27492 result
= PRAGMA_OACC_CLAUSE_COPY
;
27493 else if (!strcmp ("copyin", p
))
27494 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27495 else if (!strcmp ("copyout", p
))
27496 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
27497 else if (!strcmp ("copyprivate", p
))
27498 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27499 else if (!strcmp ("create", p
))
27500 result
= PRAGMA_OACC_CLAUSE_CREATE
;
27503 if (!strcmp ("depend", p
))
27504 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27505 else if (!strcmp ("device", p
))
27506 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27507 else if (!strcmp ("deviceptr", p
))
27508 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
27509 else if (!strcmp ("dist_schedule", p
))
27510 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27513 if (!strcmp ("final", p
))
27514 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27515 else if (!strcmp ("firstprivate", p
))
27516 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27517 else if (!strcmp ("from", p
))
27518 result
= PRAGMA_OMP_CLAUSE_FROM
;
27521 if (!strcmp ("host", p
))
27522 result
= PRAGMA_OACC_CLAUSE_HOST
;
27525 if (!strcmp ("inbranch", p
))
27526 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27529 if (!strcmp ("lastprivate", p
))
27530 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27531 else if (!strcmp ("linear", p
))
27532 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27535 if (!strcmp ("map", p
))
27536 result
= PRAGMA_OMP_CLAUSE_MAP
;
27537 else if (!strcmp ("mergeable", p
))
27538 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27539 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27540 result
= PRAGMA_CILK_CLAUSE_MASK
;
27543 if (!strcmp ("notinbranch", p
))
27544 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27545 else if (!strcmp ("nowait", p
))
27546 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27547 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27548 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27549 else if (!strcmp ("num_gangs", p
))
27550 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
27551 else if (!strcmp ("num_teams", p
))
27552 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27553 else if (!strcmp ("num_threads", p
))
27554 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27555 else if (!strcmp ("num_workers", p
))
27556 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
27559 if (!strcmp ("ordered", p
))
27560 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27563 if (!strcmp ("parallel", p
))
27564 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27565 else if (!strcmp ("present", p
))
27566 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
27567 else if (!strcmp ("present_or_copy", p
)
27568 || !strcmp ("pcopy", p
))
27569 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
27570 else if (!strcmp ("present_or_copyin", p
)
27571 || !strcmp ("pcopyin", p
))
27572 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
27573 else if (!strcmp ("present_or_copyout", p
)
27574 || !strcmp ("pcopyout", p
))
27575 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
27576 else if (!strcmp ("present_or_create", p
)
27577 || !strcmp ("pcreate", p
))
27578 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
27579 else if (!strcmp ("private", p
))
27580 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27581 else if (!strcmp ("proc_bind", p
))
27582 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27585 if (!strcmp ("reduction", p
))
27586 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27589 if (!strcmp ("safelen", p
))
27590 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27591 else if (!strcmp ("schedule", p
))
27592 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27593 else if (!strcmp ("sections", p
))
27594 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27595 else if (!strcmp ("self", p
))
27596 result
= PRAGMA_OACC_CLAUSE_SELF
;
27597 else if (!strcmp ("shared", p
))
27598 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27599 else if (!strcmp ("simdlen", p
))
27600 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27603 if (!strcmp ("taskgroup", p
))
27604 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27605 else if (!strcmp ("thread_limit", p
))
27606 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27607 else if (!strcmp ("to", p
))
27608 result
= PRAGMA_OMP_CLAUSE_TO
;
27611 if (!strcmp ("uniform", p
))
27612 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27613 else if (!strcmp ("untied", p
))
27614 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27617 if (!strcmp ("vector_length", p
))
27618 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
27619 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27620 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27623 if (!strcmp ("wait", p
))
27624 result
= PRAGMA_OACC_CLAUSE_WAIT
;
27629 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27630 cp_lexer_consume_token (parser
->lexer
);
27635 /* Validate that a clause of the given type does not already exist. */
27638 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27639 const char *name
, location_t location
)
27643 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27644 if (OMP_CLAUSE_CODE (c
) == code
)
27646 error_at (location
, "too many %qs clauses", name
);
27654 variable-list , identifier
27656 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27657 colon). An opening parenthesis will have been consumed by the caller.
27659 If KIND is nonzero, create the appropriate node and install the decl
27660 in OMP_CLAUSE_DECL and add the node to the head of the list.
27662 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27663 return the list created.
27665 COLON can be NULL if only closing parenthesis should end the list,
27666 or pointer to bool which will receive false if the list is terminated
27667 by closing parenthesis or true if the list is terminated by colon. */
27670 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27671 tree list
, bool *colon
)
27674 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27677 parser
->colon_corrects_to_scope_p
= false;
27684 token
= cp_lexer_peek_token (parser
->lexer
);
27685 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27686 /*check_dependency_p=*/true,
27687 /*template_p=*/NULL
,
27688 /*declarator_p=*/false,
27689 /*optional_p=*/false);
27690 if (name
== error_mark_node
)
27693 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27694 if (decl
== error_mark_node
)
27695 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27697 else if (kind
!= 0)
27701 case OMP_CLAUSE__CACHE_
:
27702 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
27704 error_at (token
->location
, "expected %<[%>");
27705 decl
= error_mark_node
;
27708 /* FALL THROUGH. */
27709 case OMP_CLAUSE_MAP
:
27710 case OMP_CLAUSE_FROM
:
27711 case OMP_CLAUSE_TO
:
27712 case OMP_CLAUSE_DEPEND
:
27713 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27715 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27717 parser
->colon_corrects_to_scope_p
= false;
27718 cp_lexer_consume_token (parser
->lexer
);
27719 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27720 low_bound
= cp_parser_expression (parser
);
27722 parser
->colon_corrects_to_scope_p
27723 = saved_colon_corrects_to_scope_p
;
27724 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27725 length
= integer_one_node
;
27728 /* Look for `:'. */
27729 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27731 if (!cp_lexer_next_token_is (parser
->lexer
,
27733 length
= cp_parser_expression (parser
);
27735 /* Look for the closing `]'. */
27736 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27740 if (kind
== OMP_CLAUSE__CACHE_
)
27742 if (TREE_CODE (low_bound
) != INTEGER_CST
27743 && !TREE_READONLY (low_bound
))
27745 error_at (token
->location
,
27746 "%qD is not a constant", low_bound
);
27747 decl
= error_mark_node
;
27750 if (TREE_CODE (length
) != INTEGER_CST
27751 && !TREE_READONLY (length
))
27753 error_at (token
->location
,
27754 "%qD is not a constant", length
);
27755 decl
= error_mark_node
;
27759 decl
= tree_cons (low_bound
, length
, decl
);
27766 tree u
= build_omp_clause (token
->location
, kind
);
27767 OMP_CLAUSE_DECL (u
) = decl
;
27768 OMP_CLAUSE_CHAIN (u
) = list
;
27772 list
= tree_cons (decl
, NULL_TREE
, list
);
27775 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27777 cp_lexer_consume_token (parser
->lexer
);
27781 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27783 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27786 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27790 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27794 /* Try to resync to an unnested comma. Copied from
27795 cp_parser_parenthesized_expression_list. */
27798 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27799 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27800 /*recovering=*/true,
27802 /*consume_paren=*/true);
27810 /* Similarly, but expect leading and trailing parenthesis. This is a very
27811 common case for omp clauses. */
27814 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27816 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27817 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27822 copy ( variable-list )
27823 copyin ( variable-list )
27824 copyout ( variable-list )
27825 create ( variable-list )
27826 delete ( variable-list )
27827 present ( variable-list )
27828 present_or_copy ( variable-list )
27829 pcopy ( variable-list )
27830 present_or_copyin ( variable-list )
27831 pcopyin ( variable-list )
27832 present_or_copyout ( variable-list )
27833 pcopyout ( variable-list )
27834 present_or_create ( variable-list )
27835 pcreate ( variable-list ) */
27838 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
27841 enum omp_clause_map_kind kind
;
27844 case PRAGMA_OACC_CLAUSE_COPY
:
27845 kind
= OMP_CLAUSE_MAP_FORCE_TOFROM
;
27847 case PRAGMA_OACC_CLAUSE_COPYIN
:
27848 kind
= OMP_CLAUSE_MAP_FORCE_TO
;
27850 case PRAGMA_OACC_CLAUSE_COPYOUT
:
27851 kind
= OMP_CLAUSE_MAP_FORCE_FROM
;
27853 case PRAGMA_OACC_CLAUSE_CREATE
:
27854 kind
= OMP_CLAUSE_MAP_FORCE_ALLOC
;
27856 case PRAGMA_OACC_CLAUSE_DELETE
:
27857 kind
= OMP_CLAUSE_MAP_FORCE_DEALLOC
;
27859 case PRAGMA_OACC_CLAUSE_DEVICE
:
27860 kind
= OMP_CLAUSE_MAP_FORCE_TO
;
27862 case PRAGMA_OACC_CLAUSE_HOST
:
27863 case PRAGMA_OACC_CLAUSE_SELF
:
27864 kind
= OMP_CLAUSE_MAP_FORCE_FROM
;
27866 case PRAGMA_OACC_CLAUSE_PRESENT
:
27867 kind
= OMP_CLAUSE_MAP_FORCE_PRESENT
;
27869 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
27870 kind
= OMP_CLAUSE_MAP_TOFROM
;
27872 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
27873 kind
= OMP_CLAUSE_MAP_TO
;
27875 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
27876 kind
= OMP_CLAUSE_MAP_FROM
;
27878 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
27879 kind
= OMP_CLAUSE_MAP_ALLOC
;
27882 gcc_unreachable ();
27885 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
27887 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
27888 OMP_CLAUSE_MAP_KIND (c
) = kind
;
27894 deviceptr ( variable-list ) */
27897 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
27899 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27902 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27903 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27904 variable-list must only allow for pointer variables. */
27905 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
27906 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
27908 tree v
= TREE_PURPOSE (t
);
27910 /* FIXME diagnostics: Ideally we should keep individual
27911 locations for all the variables in the var list to make the
27912 following errors more precise. Perhaps
27913 c_parser_omp_var_list_parens should construct a list of
27914 locations to go along with the var list. */
27916 if (TREE_CODE (v
) != VAR_DECL
)
27917 error_at (loc
, "%qD is not a variable", v
);
27918 else if (TREE_TYPE (v
) == error_mark_node
)
27920 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
27921 error_at (loc
, "%qD is not a pointer variable", v
);
27923 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
27924 OMP_CLAUSE_MAP_KIND (u
) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR
;
27925 OMP_CLAUSE_DECL (u
) = v
;
27926 OMP_CLAUSE_CHAIN (u
) = list
;
27934 vector_length ( expression ) */
27937 cp_parser_oacc_clause_vector_length (cp_parser
*parser
, tree list
)
27940 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27941 bool error
= false;
27943 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27946 t
= cp_parser_condition (parser
);
27947 if (t
== error_mark_node
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
27949 error_at (location
, "expected positive integer expression");
27953 if (error
|| !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27955 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27956 /*or_comma=*/false,
27957 /*consume_paren=*/true);
27961 check_no_duplicate_clause (list
, OMP_CLAUSE_VECTOR_LENGTH
, "vector_length",
27964 c
= build_omp_clause (location
, OMP_CLAUSE_VECTOR_LENGTH
);
27965 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c
) = t
;
27966 OMP_CLAUSE_CHAIN (c
) = list
;
27973 Parse wait clause or directive parameters. */
27976 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
27978 vec
<tree
, va_gc
> *args
;
27981 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
27983 /*allow_expansion_p=*/true,
27984 /*non_constant_p=*/NULL
);
27986 if (args
== NULL
|| args
->length () == 0)
27988 cp_parser_error (parser
, "expected integer expression before ')'");
27990 release_tree_vector (args
);
27994 args_tree
= build_tree_list_vec (args
);
27996 release_tree_vector (args
);
27998 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
28000 tree targ
= TREE_VALUE (t
);
28002 if (targ
!= error_mark_node
)
28004 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
28005 error ("%<wait%> expression must be integral");
28008 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
28010 mark_rvalue_use (targ
);
28011 OMP_CLAUSE_DECL (c
) = targ
;
28012 OMP_CLAUSE_CHAIN (c
) = list
;
28022 wait ( int-expr-list ) */
28025 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
28027 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28029 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
28032 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
28038 collapse ( constant-expression ) */
28041 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
28047 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28048 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28051 num
= cp_parser_constant_expression (parser
);
28053 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28054 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28055 /*or_comma=*/false,
28056 /*consume_paren=*/true);
28058 if (num
== error_mark_node
)
28060 num
= fold_non_dependent_expr (num
);
28061 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
28062 || !tree_fits_shwi_p (num
)
28063 || (n
= tree_to_shwi (num
)) <= 0
28066 error_at (loc
, "collapse argument needs positive constant integer expression");
28070 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
28071 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
28072 OMP_CLAUSE_CHAIN (c
) = list
;
28073 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
28079 default ( shared | none ) */
28082 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
28084 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
28087 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28091 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28092 const char *p
= IDENTIFIER_POINTER (id
);
28097 if (strcmp ("none", p
) != 0)
28099 kind
= OMP_CLAUSE_DEFAULT_NONE
;
28103 if (strcmp ("shared", p
) != 0)
28105 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
28112 cp_lexer_consume_token (parser
->lexer
);
28117 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
28120 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28121 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28122 /*or_comma=*/false,
28123 /*consume_paren=*/true);
28125 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
28128 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
28129 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
28130 OMP_CLAUSE_CHAIN (c
) = list
;
28131 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
28137 final ( expression ) */
28140 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
28144 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28147 t
= cp_parser_condition (parser
);
28149 if (t
== error_mark_node
28150 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28151 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28152 /*or_comma=*/false,
28153 /*consume_paren=*/true);
28155 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
28157 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
28158 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
28159 OMP_CLAUSE_CHAIN (c
) = list
;
28165 if ( expression ) */
28168 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
28172 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28175 t
= cp_parser_condition (parser
);
28177 if (t
== error_mark_node
28178 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28179 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28180 /*or_comma=*/false,
28181 /*consume_paren=*/true);
28183 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
28185 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
28186 OMP_CLAUSE_IF_EXPR (c
) = t
;
28187 OMP_CLAUSE_CHAIN (c
) = list
;
28196 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
28197 tree list
, location_t location
)
28201 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
28204 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
28205 OMP_CLAUSE_CHAIN (c
) = list
;
28213 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
28214 tree list
, location_t location
)
28218 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
28220 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
28221 OMP_CLAUSE_CHAIN (c
) = list
;
28226 num_gangs ( expression ) */
28229 cp_parser_omp_clause_num_gangs (cp_parser
*parser
, tree list
)
28232 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28234 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28237 t
= cp_parser_condition (parser
);
28239 if (t
== error_mark_node
28240 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28241 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28242 /*or_comma=*/false,
28243 /*consume_paren=*/true);
28245 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28247 error_at (location
, "expected positive integer expression");
28251 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_GANGS
, "num_gangs", location
);
28253 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_GANGS
);
28254 OMP_CLAUSE_NUM_GANGS_EXPR (c
) = t
;
28255 OMP_CLAUSE_CHAIN (c
) = list
;
28262 num_threads ( expression ) */
28265 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
28266 location_t location
)
28270 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28273 t
= cp_parser_expression (parser
);
28275 if (t
== error_mark_node
28276 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28277 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28278 /*or_comma=*/false,
28279 /*consume_paren=*/true);
28281 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
28282 "num_threads", location
);
28284 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
28285 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
28286 OMP_CLAUSE_CHAIN (c
) = list
;
28292 num_workers ( expression ) */
28295 cp_parser_omp_clause_num_workers (cp_parser
*parser
, tree list
)
28298 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28300 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28303 t
= cp_parser_condition (parser
);
28305 if (t
== error_mark_node
28306 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28307 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28308 /*or_comma=*/false,
28309 /*consume_paren=*/true);
28311 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28313 error_at (location
, "expected positive integer expression");
28317 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_WORKERS
, "num_gangs",
28320 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_WORKERS
);
28321 OMP_CLAUSE_NUM_WORKERS_EXPR (c
) = t
;
28322 OMP_CLAUSE_CHAIN (c
) = list
;
28332 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
28333 tree list
, location_t location
)
28337 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
28338 "ordered", location
);
28340 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
28341 OMP_CLAUSE_CHAIN (c
) = list
;
28346 reduction ( reduction-operator : variable-list )
28348 reduction-operator:
28349 One of: + * - & ^ | && ||
28353 reduction-operator:
28354 One of: + * - & ^ | && || min max
28358 reduction-operator:
28359 One of: + * - & ^ | && ||
28363 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
28365 enum tree_code code
= ERROR_MARK
;
28366 tree nlist
, c
, id
= NULL_TREE
;
28368 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28371 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28373 case CPP_PLUS
: code
= PLUS_EXPR
; break;
28374 case CPP_MULT
: code
= MULT_EXPR
; break;
28375 case CPP_MINUS
: code
= MINUS_EXPR
; break;
28376 case CPP_AND
: code
= BIT_AND_EXPR
; break;
28377 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
28378 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
28379 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
28380 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
28384 if (code
!= ERROR_MARK
)
28385 cp_lexer_consume_token (parser
->lexer
);
28388 bool saved_colon_corrects_to_scope_p
;
28389 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
28390 parser
->colon_corrects_to_scope_p
= false;
28391 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
28392 /*check_dependency_p=*/true,
28393 /*template_p=*/NULL
,
28394 /*declarator_p=*/false,
28395 /*optional_p=*/false);
28396 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
28397 if (identifier_p (id
))
28399 const char *p
= IDENTIFIER_POINTER (id
);
28401 if (strcmp (p
, "min") == 0)
28403 else if (strcmp (p
, "max") == 0)
28405 else if (id
== ansi_opname (PLUS_EXPR
))
28407 else if (id
== ansi_opname (MULT_EXPR
))
28409 else if (id
== ansi_opname (MINUS_EXPR
))
28411 else if (id
== ansi_opname (BIT_AND_EXPR
))
28412 code
= BIT_AND_EXPR
;
28413 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28414 code
= BIT_IOR_EXPR
;
28415 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28416 code
= BIT_XOR_EXPR
;
28417 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28418 code
= TRUTH_ANDIF_EXPR
;
28419 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28420 code
= TRUTH_ORIF_EXPR
;
28421 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28422 tree scope
= parser
->scope
;
28424 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28425 parser
->scope
= NULL_TREE
;
28426 parser
->qualifying_scope
= NULL_TREE
;
28427 parser
->object_scope
= NULL_TREE
;
28431 error ("invalid reduction-identifier");
28433 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28434 /*or_comma=*/false,
28435 /*consume_paren=*/true);
28440 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28443 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28445 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28447 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28448 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28455 schedule ( schedule-kind )
28456 schedule ( schedule-kind , expression )
28459 static | dynamic | guided | runtime | auto */
28462 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28466 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28469 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28473 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28474 const char *p
= IDENTIFIER_POINTER (id
);
28479 if (strcmp ("dynamic", p
) != 0)
28481 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28485 if (strcmp ("guided", p
) != 0)
28487 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28491 if (strcmp ("runtime", p
) != 0)
28493 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28500 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28501 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28502 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28503 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28506 cp_lexer_consume_token (parser
->lexer
);
28508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28511 cp_lexer_consume_token (parser
->lexer
);
28513 token
= cp_lexer_peek_token (parser
->lexer
);
28514 t
= cp_parser_assignment_expression (parser
);
28516 if (t
== error_mark_node
)
28518 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28519 error_at (token
->location
, "schedule %<runtime%> does not take "
28520 "a %<chunk_size%> parameter");
28521 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28522 error_at (token
->location
, "schedule %<auto%> does not take "
28523 "a %<chunk_size%> parameter");
28525 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28527 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28530 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28533 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28534 OMP_CLAUSE_CHAIN (c
) = list
;
28538 cp_parser_error (parser
, "invalid schedule kind");
28540 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28541 /*or_comma=*/false,
28542 /*consume_paren=*/true);
28550 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28551 tree list
, location_t location
)
28555 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28557 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28558 OMP_CLAUSE_CHAIN (c
) = list
;
28567 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28568 tree list
, location_t location
)
28570 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28571 tree c
= build_omp_clause (location
, code
);
28572 OMP_CLAUSE_CHAIN (c
) = list
;
28583 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28584 enum omp_clause_code code
,
28585 tree list
, location_t location
)
28587 tree c
= build_omp_clause (location
, code
);
28588 OMP_CLAUSE_CHAIN (c
) = list
;
28593 num_teams ( expression ) */
28596 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28597 location_t location
)
28601 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28604 t
= cp_parser_expression (parser
);
28606 if (t
== error_mark_node
28607 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28608 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28609 /*or_comma=*/false,
28610 /*consume_paren=*/true);
28612 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28613 "num_teams", location
);
28615 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28616 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28617 OMP_CLAUSE_CHAIN (c
) = list
;
28623 thread_limit ( expression ) */
28626 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28627 location_t location
)
28631 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28634 t
= cp_parser_expression (parser
);
28636 if (t
== error_mark_node
28637 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28638 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28639 /*or_comma=*/false,
28640 /*consume_paren=*/true);
28642 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28643 "thread_limit", location
);
28645 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28646 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28647 OMP_CLAUSE_CHAIN (c
) = list
;
28653 aligned ( variable-list )
28654 aligned ( variable-list : constant-expression ) */
28657 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28659 tree nlist
, c
, alignment
= NULL_TREE
;
28662 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28665 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28670 alignment
= cp_parser_constant_expression (parser
);
28672 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28673 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28674 /*or_comma=*/false,
28675 /*consume_paren=*/true);
28677 if (alignment
== error_mark_node
)
28678 alignment
= NULL_TREE
;
28681 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28682 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28688 linear ( variable-list )
28689 linear ( variable-list : expression ) */
28692 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28693 bool is_cilk_simd_fn
)
28695 tree nlist
, c
, step
= integer_one_node
;
28698 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28701 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28706 step
= cp_parser_expression (parser
);
28708 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28710 sorry ("using parameters for %<linear%> step is not supported yet");
28711 step
= integer_one_node
;
28713 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28714 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28715 /*or_comma=*/false,
28716 /*consume_paren=*/true);
28718 if (step
== error_mark_node
)
28722 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28723 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28729 safelen ( constant-expression ) */
28732 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28733 location_t location
)
28737 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28740 t
= cp_parser_constant_expression (parser
);
28742 if (t
== error_mark_node
28743 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28744 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28745 /*or_comma=*/false,
28746 /*consume_paren=*/true);
28748 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28750 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28751 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28752 OMP_CLAUSE_CHAIN (c
) = list
;
28758 simdlen ( constant-expression ) */
28761 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28762 location_t location
)
28766 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28769 t
= cp_parser_constant_expression (parser
);
28771 if (t
== error_mark_node
28772 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28773 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28774 /*or_comma=*/false,
28775 /*consume_paren=*/true);
28777 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28779 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28780 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28781 OMP_CLAUSE_CHAIN (c
) = list
;
28787 depend ( depend-kind : variable-list )
28790 in | out | inout */
28793 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28796 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28798 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28803 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28804 const char *p
= IDENTIFIER_POINTER (id
);
28806 if (strcmp ("in", p
) == 0)
28807 kind
= OMP_CLAUSE_DEPEND_IN
;
28808 else if (strcmp ("inout", p
) == 0)
28809 kind
= OMP_CLAUSE_DEPEND_INOUT
;
28810 else if (strcmp ("out", p
) == 0)
28811 kind
= OMP_CLAUSE_DEPEND_OUT
;
28818 cp_lexer_consume_token (parser
->lexer
);
28819 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28822 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
28825 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28826 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
28831 cp_parser_error (parser
, "invalid depend kind");
28833 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28834 /*or_comma=*/false,
28835 /*consume_paren=*/true);
28840 map ( map-kind : variable-list )
28841 map ( variable-list )
28844 alloc | to | from | tofrom */
28847 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28850 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28852 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28856 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28858 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28859 const char *p
= IDENTIFIER_POINTER (id
);
28861 if (strcmp ("alloc", p
) == 0)
28862 kind
= OMP_CLAUSE_MAP_ALLOC
;
28863 else if (strcmp ("to", p
) == 0)
28864 kind
= OMP_CLAUSE_MAP_TO
;
28865 else if (strcmp ("from", p
) == 0)
28866 kind
= OMP_CLAUSE_MAP_FROM
;
28867 else if (strcmp ("tofrom", p
) == 0)
28868 kind
= OMP_CLAUSE_MAP_TOFROM
;
28871 cp_parser_error (parser
, "invalid map kind");
28872 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28873 /*or_comma=*/false,
28874 /*consume_paren=*/true);
28877 cp_lexer_consume_token (parser
->lexer
);
28878 cp_lexer_consume_token (parser
->lexer
);
28881 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28884 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28885 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28891 device ( expression ) */
28894 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28895 location_t location
)
28899 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28902 t
= cp_parser_expression (parser
);
28904 if (t
== error_mark_node
28905 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28906 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28907 /*or_comma=*/false,
28908 /*consume_paren=*/true);
28910 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28911 "device", location
);
28913 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28914 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28915 OMP_CLAUSE_CHAIN (c
) = list
;
28921 dist_schedule ( static )
28922 dist_schedule ( static , expression ) */
28925 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28926 location_t location
)
28930 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28933 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28935 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28937 cp_lexer_consume_token (parser
->lexer
);
28939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28941 cp_lexer_consume_token (parser
->lexer
);
28943 t
= cp_parser_assignment_expression (parser
);
28945 if (t
== error_mark_node
)
28947 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28949 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28952 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28955 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28957 OMP_CLAUSE_CHAIN (c
) = list
;
28961 cp_parser_error (parser
, "invalid dist_schedule kind");
28963 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28964 /*or_comma=*/false,
28965 /*consume_paren=*/true);
28970 proc_bind ( proc-bind-kind )
28973 master | close | spread */
28976 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28977 location_t location
)
28980 enum omp_clause_proc_bind_kind kind
;
28982 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28988 const char *p
= IDENTIFIER_POINTER (id
);
28990 if (strcmp ("master", p
) == 0)
28991 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28992 else if (strcmp ("close", p
) == 0)
28993 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28994 else if (strcmp ("spread", p
) == 0)
28995 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
29002 cp_lexer_consume_token (parser
->lexer
);
29003 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
29006 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
29007 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
29009 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
29010 OMP_CLAUSE_CHAIN (c
) = list
;
29014 cp_parser_error (parser
, "invalid depend kind");
29016 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29017 /*or_comma=*/false,
29018 /*consume_paren=*/true);
29023 async [( int-expr )] */
29026 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
29029 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29031 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
29033 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
29035 cp_lexer_consume_token (parser
->lexer
);
29037 t
= cp_parser_expression (parser
);
29038 if (t
== error_mark_node
29039 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29040 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29041 /*or_comma=*/false,
29042 /*consume_paren=*/true);
29045 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
29047 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
29048 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
29049 OMP_CLAUSE_CHAIN (c
) = list
;
29055 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29056 is a bitmask in MASK. Return the list of clauses found. */
29059 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29060 const char *where
, cp_token
*pragma_tok
,
29061 bool finish_p
= true)
29063 tree clauses
= NULL
;
29066 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29069 pragma_omp_clause c_kind
;
29070 const char *c_name
;
29071 tree prev
= clauses
;
29073 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29074 cp_lexer_consume_token (parser
->lexer
);
29076 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
29077 c_kind
= cp_parser_omp_clause_name (parser
);
29081 case PRAGMA_OACC_CLAUSE_ASYNC
:
29082 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
29085 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
29086 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
29087 c_name
= "collapse";
29089 case PRAGMA_OACC_CLAUSE_COPY
:
29090 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29093 case PRAGMA_OACC_CLAUSE_COPYIN
:
29094 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29097 case PRAGMA_OACC_CLAUSE_COPYOUT
:
29098 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29099 c_name
= "copyout";
29101 case PRAGMA_OACC_CLAUSE_CREATE
:
29102 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29105 case PRAGMA_OACC_CLAUSE_DELETE
:
29106 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29109 case PRAGMA_OACC_CLAUSE_DEVICE
:
29110 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29113 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
29114 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
29115 c_name
= "deviceptr";
29117 case PRAGMA_OACC_CLAUSE_HOST
:
29118 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29121 case PRAGMA_OACC_CLAUSE_IF
:
29122 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
);
29125 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
29126 clauses
= cp_parser_omp_clause_num_gangs (parser
, clauses
);
29127 c_name
= "num_gangs";
29129 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
29130 clauses
= cp_parser_omp_clause_num_workers (parser
, clauses
);
29131 c_name
= "num_workers";
29133 case PRAGMA_OACC_CLAUSE_PRESENT
:
29134 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29135 c_name
= "present";
29137 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
29138 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29139 c_name
= "present_or_copy";
29141 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
29142 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29143 c_name
= "present_or_copyin";
29145 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
29146 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29147 c_name
= "present_or_copyout";
29149 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
29150 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29151 c_name
= "present_or_create";
29153 case PRAGMA_OACC_CLAUSE_REDUCTION
:
29154 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29155 c_name
= "reduction";
29157 case PRAGMA_OACC_CLAUSE_SELF
:
29158 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29161 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
29163 cp_parser_oacc_clause_vector_length (parser
, clauses
);
29164 c_name
= "vector_length";
29166 case PRAGMA_OACC_CLAUSE_WAIT
:
29167 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
29171 cp_parser_error (parser
, "expected clause");
29177 if (((mask
>> c_kind
) & 1) == 0)
29179 /* Remove the invalid clause(s) from the list to avoid
29180 confusing the rest of the compiler. */
29182 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
29187 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29190 return finish_omp_clauses (clauses
);
29195 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29196 is a bitmask in MASK. Return the list of clauses found; the result
29197 of clause default goes in *pdefault. */
29200 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29201 const char *where
, cp_token
*pragma_tok
,
29202 bool finish_p
= true)
29204 tree clauses
= NULL
;
29206 cp_token
*token
= NULL
;
29207 bool cilk_simd_fn
= false;
29209 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29211 pragma_omp_clause c_kind
;
29212 const char *c_name
;
29213 tree prev
= clauses
;
29215 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29216 cp_lexer_consume_token (parser
->lexer
);
29218 token
= cp_lexer_peek_token (parser
->lexer
);
29219 c_kind
= cp_parser_omp_clause_name (parser
);
29223 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
29224 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
29226 c_name
= "collapse";
29228 case PRAGMA_OMP_CLAUSE_COPYIN
:
29229 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
29232 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
29233 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
29235 c_name
= "copyprivate";
29237 case PRAGMA_OMP_CLAUSE_DEFAULT
:
29238 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
29240 c_name
= "default";
29242 case PRAGMA_OMP_CLAUSE_FINAL
:
29243 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
29246 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
29247 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
29249 c_name
= "firstprivate";
29251 case PRAGMA_OMP_CLAUSE_IF
:
29252 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
29255 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
29256 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
29258 c_name
= "lastprivate";
29260 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
29261 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
29263 c_name
= "mergeable";
29265 case PRAGMA_OMP_CLAUSE_NOWAIT
:
29266 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
29269 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
29270 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
29272 c_name
= "num_threads";
29274 case PRAGMA_OMP_CLAUSE_ORDERED
:
29275 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
29277 c_name
= "ordered";
29279 case PRAGMA_OMP_CLAUSE_PRIVATE
:
29280 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
29282 c_name
= "private";
29284 case PRAGMA_OMP_CLAUSE_REDUCTION
:
29285 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29286 c_name
= "reduction";
29288 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
29289 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
29291 c_name
= "schedule";
29293 case PRAGMA_OMP_CLAUSE_SHARED
:
29294 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
29298 case PRAGMA_OMP_CLAUSE_UNTIED
:
29299 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
29303 case PRAGMA_OMP_CLAUSE_INBRANCH
:
29304 case PRAGMA_CILK_CLAUSE_MASK
:
29305 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
29306 clauses
, token
->location
);
29307 c_name
= "inbranch";
29309 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
29310 case PRAGMA_CILK_CLAUSE_NOMASK
:
29311 clauses
= cp_parser_omp_clause_branch (parser
,
29312 OMP_CLAUSE_NOTINBRANCH
,
29313 clauses
, token
->location
);
29314 c_name
= "notinbranch";
29316 case PRAGMA_OMP_CLAUSE_PARALLEL
:
29317 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
29318 clauses
, token
->location
);
29319 c_name
= "parallel";
29323 error_at (token
->location
, "%qs must be the first clause of %qs",
29328 case PRAGMA_OMP_CLAUSE_FOR
:
29329 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
29330 clauses
, token
->location
);
29333 goto clause_not_first
;
29335 case PRAGMA_OMP_CLAUSE_SECTIONS
:
29336 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
29337 clauses
, token
->location
);
29338 c_name
= "sections";
29340 goto clause_not_first
;
29342 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
29343 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
29344 clauses
, token
->location
);
29345 c_name
= "taskgroup";
29347 goto clause_not_first
;
29349 case PRAGMA_OMP_CLAUSE_TO
:
29350 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
29354 case PRAGMA_OMP_CLAUSE_FROM
:
29355 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
29359 case PRAGMA_OMP_CLAUSE_UNIFORM
:
29360 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
29362 c_name
= "uniform";
29364 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
29365 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
29367 c_name
= "num_teams";
29369 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
29370 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
29372 c_name
= "thread_limit";
29374 case PRAGMA_OMP_CLAUSE_ALIGNED
:
29375 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
29376 c_name
= "aligned";
29378 case PRAGMA_OMP_CLAUSE_LINEAR
:
29379 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
29380 cilk_simd_fn
= true;
29381 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
29384 case PRAGMA_OMP_CLAUSE_DEPEND
:
29385 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
29388 case PRAGMA_OMP_CLAUSE_MAP
:
29389 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
29392 case PRAGMA_OMP_CLAUSE_DEVICE
:
29393 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
29397 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
29398 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
29400 c_name
= "dist_schedule";
29402 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
29403 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
29405 c_name
= "proc_bind";
29407 case PRAGMA_OMP_CLAUSE_SAFELEN
:
29408 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
29410 c_name
= "safelen";
29412 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
29413 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
29415 c_name
= "simdlen";
29417 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
29418 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
29419 c_name
= "simdlen";
29422 cp_parser_error (parser
, "expected clause");
29428 if (((mask
>> c_kind
) & 1) == 0)
29430 /* Remove the invalid clause(s) from the list to avoid
29431 confusing the rest of the compiler. */
29433 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
29437 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29438 no reason to skip to the end. */
29439 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
29440 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29442 return finish_omp_clauses (clauses
);
29450 In practice, we're also interested in adding the statement to an
29451 outer node. So it is convenient if we work around the fact that
29452 cp_parser_statement calls add_stmt. */
29455 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
29457 unsigned save
= parser
->in_statement
;
29459 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29460 This preserves the "not within loop or switch" style error messages
29461 for nonsense cases like
29467 if (parser
->in_statement
)
29468 parser
->in_statement
= IN_OMP_BLOCK
;
29474 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
29476 parser
->in_statement
= save
;
29480 cp_parser_omp_structured_block (cp_parser
*parser
)
29482 tree stmt
= begin_omp_structured_block ();
29483 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
29485 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29487 cp_parser_end_omp_structured_block (parser
, save
);
29488 return finish_omp_structured_block (stmt
);
29492 # pragma omp atomic new-line
29496 x binop= expr | x++ | ++x | x-- | --x
29498 +, *, -, /, &, ^, |, <<, >>
29500 where x is an lvalue expression with scalar type.
29503 # pragma omp atomic new-line
29506 # pragma omp atomic read new-line
29509 # pragma omp atomic write new-line
29512 # pragma omp atomic update new-line
29515 # pragma omp atomic capture new-line
29518 # pragma omp atomic capture new-line
29526 expression-stmt | x = x binop expr
29528 v = expression-stmt
29530 { v = x; update-stmt; } | { update-stmt; v = x; }
29534 expression-stmt | x = x binop expr | x = expr binop x
29538 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29540 where x and v are lvalue expressions with scalar type. */
29543 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
29545 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
29546 tree rhs1
= NULL_TREE
, orig_lhs
;
29547 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
29548 bool structured_block
= false;
29549 bool seq_cst
= false;
29551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29553 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29554 const char *p
= IDENTIFIER_POINTER (id
);
29556 if (!strcmp (p
, "seq_cst"))
29559 cp_lexer_consume_token (parser
->lexer
);
29560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29561 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29562 cp_lexer_consume_token (parser
->lexer
);
29565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29567 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29568 const char *p
= IDENTIFIER_POINTER (id
);
29570 if (!strcmp (p
, "read"))
29571 code
= OMP_ATOMIC_READ
;
29572 else if (!strcmp (p
, "write"))
29574 else if (!strcmp (p
, "update"))
29576 else if (!strcmp (p
, "capture"))
29577 code
= OMP_ATOMIC_CAPTURE_NEW
;
29581 cp_lexer_consume_token (parser
->lexer
);
29585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29586 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29587 cp_lexer_consume_token (parser
->lexer
);
29589 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29591 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29592 const char *p
= IDENTIFIER_POINTER (id
);
29594 if (!strcmp (p
, "seq_cst"))
29597 cp_lexer_consume_token (parser
->lexer
);
29601 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29605 case OMP_ATOMIC_READ
:
29606 case NOP_EXPR
: /* atomic write */
29607 v
= cp_parser_unary_expression (parser
);
29608 if (v
== error_mark_node
)
29610 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29612 if (code
== NOP_EXPR
)
29613 lhs
= cp_parser_expression (parser
);
29615 lhs
= cp_parser_unary_expression (parser
);
29616 if (lhs
== error_mark_node
)
29618 if (code
== NOP_EXPR
)
29620 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29628 case OMP_ATOMIC_CAPTURE_NEW
:
29629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29631 cp_lexer_consume_token (parser
->lexer
);
29632 structured_block
= true;
29636 v
= cp_parser_unary_expression (parser
);
29637 if (v
== error_mark_node
)
29639 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29647 lhs
= cp_parser_unary_expression (parser
);
29649 switch (TREE_CODE (lhs
))
29654 case POSTINCREMENT_EXPR
:
29655 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29656 code
= OMP_ATOMIC_CAPTURE_OLD
;
29658 case PREINCREMENT_EXPR
:
29659 lhs
= TREE_OPERAND (lhs
, 0);
29660 opcode
= PLUS_EXPR
;
29661 rhs
= integer_one_node
;
29664 case POSTDECREMENT_EXPR
:
29665 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29666 code
= OMP_ATOMIC_CAPTURE_OLD
;
29668 case PREDECREMENT_EXPR
:
29669 lhs
= TREE_OPERAND (lhs
, 0);
29670 opcode
= MINUS_EXPR
;
29671 rhs
= integer_one_node
;
29674 case COMPOUND_EXPR
:
29675 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29676 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29677 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29678 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29680 (TREE_OPERAND (lhs
, 1), 0), 0)))
29682 /* Undo effects of boolean_increment for post {in,de}crement. */
29683 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29686 if (TREE_CODE (lhs
) == MODIFY_EXPR
29687 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29689 /* Undo effects of boolean_increment. */
29690 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29692 /* This is pre or post increment. */
29693 rhs
= TREE_OPERAND (lhs
, 1);
29694 lhs
= TREE_OPERAND (lhs
, 0);
29696 if (code
== OMP_ATOMIC_CAPTURE_NEW
29697 && !structured_block
29698 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29699 code
= OMP_ATOMIC_CAPTURE_OLD
;
29705 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29708 opcode
= MULT_EXPR
;
29711 opcode
= TRUNC_DIV_EXPR
;
29714 opcode
= PLUS_EXPR
;
29717 opcode
= MINUS_EXPR
;
29719 case CPP_LSHIFT_EQ
:
29720 opcode
= LSHIFT_EXPR
;
29722 case CPP_RSHIFT_EQ
:
29723 opcode
= RSHIFT_EXPR
;
29726 opcode
= BIT_AND_EXPR
;
29729 opcode
= BIT_IOR_EXPR
;
29732 opcode
= BIT_XOR_EXPR
;
29735 enum cp_parser_prec oprec
;
29737 cp_lexer_consume_token (parser
->lexer
);
29738 cp_parser_parse_tentatively (parser
);
29739 rhs1
= cp_parser_simple_cast_expression (parser
);
29740 if (rhs1
== error_mark_node
)
29742 cp_parser_abort_tentative_parse (parser
);
29743 cp_parser_simple_cast_expression (parser
);
29746 token
= cp_lexer_peek_token (parser
->lexer
);
29747 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29749 cp_parser_abort_tentative_parse (parser
);
29750 cp_parser_parse_tentatively (parser
);
29751 rhs
= cp_parser_binary_expression (parser
, false, true,
29752 PREC_NOT_OPERATOR
, NULL
);
29753 if (rhs
== error_mark_node
)
29755 cp_parser_abort_tentative_parse (parser
);
29756 cp_parser_binary_expression (parser
, false, true,
29757 PREC_NOT_OPERATOR
, NULL
);
29760 switch (TREE_CODE (rhs
))
29763 case TRUNC_DIV_EXPR
:
29771 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29773 if (cp_parser_parse_definitely (parser
))
29775 opcode
= TREE_CODE (rhs
);
29776 rhs1
= TREE_OPERAND (rhs
, 0);
29777 rhs
= TREE_OPERAND (rhs
, 1);
29787 cp_parser_abort_tentative_parse (parser
);
29788 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29790 rhs
= cp_parser_expression (parser
);
29791 if (rhs
== error_mark_node
)
29797 cp_parser_error (parser
,
29798 "invalid form of %<#pragma omp atomic%>");
29801 if (!cp_parser_parse_definitely (parser
))
29803 switch (token
->type
)
29805 case CPP_SEMICOLON
:
29806 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29808 code
= OMP_ATOMIC_CAPTURE_OLD
;
29813 cp_lexer_consume_token (parser
->lexer
);
29816 else if (structured_block
)
29823 cp_parser_error (parser
,
29824 "invalid form of %<#pragma omp atomic%>");
29827 opcode
= MULT_EXPR
;
29830 opcode
= TRUNC_DIV_EXPR
;
29833 opcode
= PLUS_EXPR
;
29836 opcode
= MINUS_EXPR
;
29839 opcode
= LSHIFT_EXPR
;
29842 opcode
= RSHIFT_EXPR
;
29845 opcode
= BIT_AND_EXPR
;
29848 opcode
= BIT_IOR_EXPR
;
29851 opcode
= BIT_XOR_EXPR
;
29854 cp_parser_error (parser
,
29855 "invalid operator for %<#pragma omp atomic%>");
29858 oprec
= TOKEN_PRECEDENCE (token
);
29859 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
29860 if (commutative_tree_code (opcode
))
29861 oprec
= (enum cp_parser_prec
) (oprec
- 1);
29862 cp_lexer_consume_token (parser
->lexer
);
29863 rhs
= cp_parser_binary_expression (parser
, false, false,
29865 if (rhs
== error_mark_node
)
29870 cp_parser_error (parser
,
29871 "invalid operator for %<#pragma omp atomic%>");
29874 cp_lexer_consume_token (parser
->lexer
);
29876 rhs
= cp_parser_expression (parser
);
29877 if (rhs
== error_mark_node
)
29882 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29884 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
29886 v
= cp_parser_unary_expression (parser
);
29887 if (v
== error_mark_node
)
29889 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29891 lhs1
= cp_parser_unary_expression (parser
);
29892 if (lhs1
== error_mark_node
)
29895 if (structured_block
)
29897 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29898 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29901 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
29902 if (!structured_block
)
29903 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29907 cp_parser_skip_to_end_of_block_or_statement (parser
);
29908 if (structured_block
)
29910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29911 cp_lexer_consume_token (parser
->lexer
);
29912 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
29914 cp_parser_skip_to_end_of_block_or_statement (parser
);
29915 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29916 cp_lexer_consume_token (parser
->lexer
);
29923 # pragma omp barrier new-line */
29926 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
29928 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29929 finish_omp_barrier ();
29933 # pragma omp critical [(name)] new-line
29934 structured-block */
29937 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
29939 tree stmt
, name
= NULL
;
29941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29943 cp_lexer_consume_token (parser
->lexer
);
29945 name
= cp_parser_identifier (parser
);
29947 if (name
== error_mark_node
29948 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29949 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29950 /*or_comma=*/false,
29951 /*consume_paren=*/true);
29952 if (name
== error_mark_node
)
29955 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29957 stmt
= cp_parser_omp_structured_block (parser
);
29958 return c_finish_omp_critical (input_location
, stmt
, name
);
29962 # pragma omp flush flush-vars[opt] new-line
29965 ( variable-list ) */
29968 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
29970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29971 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29972 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29974 finish_omp_flush ();
29977 /* Helper function, to parse omp for increment expression. */
29980 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
29982 tree cond
= cp_parser_binary_expression (parser
, false, true,
29983 PREC_NOT_OPERATOR
, NULL
);
29984 if (cond
== error_mark_node
29985 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29987 cp_parser_skip_to_end_of_statement (parser
);
29988 return error_mark_node
;
29991 switch (TREE_CODE (cond
))
29999 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30001 /* Fall through: OpenMP disallows NE_EXPR. */
30003 return error_mark_node
;
30006 /* If decl is an iterator, preserve LHS and RHS of the relational
30007 expr until finish_omp_for. */
30009 && (type_dependent_expression_p (decl
)
30010 || CLASS_TYPE_P (TREE_TYPE (decl
))))
30013 return build_x_binary_op (input_location
, TREE_CODE (cond
),
30014 TREE_OPERAND (cond
, 0), ERROR_MARK
,
30015 TREE_OPERAND (cond
, 1), ERROR_MARK
,
30016 /*overload=*/NULL
, tf_warning_or_error
);
30019 /* Helper function, to parse omp for increment expression. */
30022 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
30024 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30030 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30032 op
= (token
->type
== CPP_PLUS_PLUS
30033 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
30034 cp_lexer_consume_token (parser
->lexer
);
30035 lhs
= cp_parser_simple_cast_expression (parser
);
30037 return error_mark_node
;
30038 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30041 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
30043 return error_mark_node
;
30045 token
= cp_lexer_peek_token (parser
->lexer
);
30046 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30048 op
= (token
->type
== CPP_PLUS_PLUS
30049 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
30050 cp_lexer_consume_token (parser
->lexer
);
30051 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30054 op
= cp_parser_assignment_operator_opt (parser
);
30055 if (op
== ERROR_MARK
)
30056 return error_mark_node
;
30058 if (op
!= NOP_EXPR
)
30060 rhs
= cp_parser_assignment_expression (parser
);
30061 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
30062 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30065 lhs
= cp_parser_binary_expression (parser
, false, false,
30066 PREC_ADDITIVE_EXPRESSION
, NULL
);
30067 token
= cp_lexer_peek_token (parser
->lexer
);
30068 decl_first
= lhs
== decl
;
30071 if (token
->type
!= CPP_PLUS
30072 && token
->type
!= CPP_MINUS
)
30073 return error_mark_node
;
30077 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
30078 cp_lexer_consume_token (parser
->lexer
);
30079 rhs
= cp_parser_binary_expression (parser
, false, false,
30080 PREC_ADDITIVE_EXPRESSION
, NULL
);
30081 token
= cp_lexer_peek_token (parser
->lexer
);
30082 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
30084 if (lhs
== NULL_TREE
)
30086 if (op
== PLUS_EXPR
)
30089 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
30090 tf_warning_or_error
);
30093 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
30094 ERROR_MARK
, NULL
, tf_warning_or_error
);
30097 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
30101 if (rhs
!= decl
|| op
== MINUS_EXPR
)
30102 return error_mark_node
;
30103 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
30106 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
30108 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30111 /* Parse the initialization statement of either an OpenMP for loop or
30112 a Cilk Plus for loop.
30114 Return true if the resulting construct should have an
30115 OMP_CLAUSE_PRIVATE added to it. */
30118 cp_parser_omp_for_loop_init (cp_parser
*parser
,
30119 enum tree_code code
,
30120 tree
&this_pre_body
,
30121 vec
<tree
, va_gc
> *for_block
,
30126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30129 bool add_private_clause
= false;
30131 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30135 integer-type var = lb
30136 random-access-iterator-type var = lb
30137 pointer-type var = lb
30139 cp_decl_specifier_seq type_specifiers
;
30141 /* First, try to parse as an initialized declaration. See
30142 cp_parser_condition, from whence the bulk of this is copied. */
30144 cp_parser_parse_tentatively (parser
);
30145 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
30146 /*is_trailing_return=*/false,
30148 if (cp_parser_parse_definitely (parser
))
30150 /* If parsing a type specifier seq succeeded, then this
30151 MUST be a initialized declaration. */
30152 tree asm_specification
, attributes
;
30153 cp_declarator
*declarator
;
30155 declarator
= cp_parser_declarator (parser
,
30156 CP_PARSER_DECLARATOR_NAMED
,
30157 /*ctor_dtor_or_conv_p=*/NULL
,
30158 /*parenthesized_p=*/NULL
,
30159 /*member_p=*/false,
30160 /*friend_p=*/false);
30161 attributes
= cp_parser_attributes_opt (parser
);
30162 asm_specification
= cp_parser_asm_specification_opt (parser
);
30164 if (declarator
== cp_error_declarator
)
30165 cp_parser_skip_to_end_of_statement (parser
);
30169 tree pushed_scope
, auto_node
;
30171 decl
= start_decl (declarator
, &type_specifiers
,
30172 SD_INITIALIZED
, attributes
,
30173 /*prefix_attributes=*/NULL_TREE
,
30176 auto_node
= type_uses_auto (TREE_TYPE (decl
));
30177 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30179 if (cp_lexer_next_token_is (parser
->lexer
,
30182 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
30183 error ("parenthesized initialization is not allowed in "
30184 "OpenMP %<for%> loop");
30186 error ("parenthesized initialization is "
30187 "not allowed in for-loop");
30190 /* Trigger an error. */
30191 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30193 init
= error_mark_node
;
30194 cp_parser_skip_to_end_of_statement (parser
);
30196 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
30197 || type_dependent_expression_p (decl
)
30200 bool is_direct_init
, is_non_constant_init
;
30202 init
= cp_parser_initializer (parser
,
30204 &is_non_constant_init
);
30209 = do_auto_deduction (TREE_TYPE (decl
), init
,
30212 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
30213 && !type_dependent_expression_p (decl
))
30217 cp_finish_decl (decl
, init
, !is_non_constant_init
,
30219 LOOKUP_ONLYCONVERTING
);
30220 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
30222 vec_safe_push (for_block
, this_pre_body
);
30226 init
= pop_stmt_list (this_pre_body
);
30227 this_pre_body
= NULL_TREE
;
30232 cp_lexer_consume_token (parser
->lexer
);
30233 init
= cp_parser_assignment_expression (parser
);
30236 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
30237 init
= error_mark_node
;
30239 cp_finish_decl (decl
, NULL_TREE
,
30240 /*init_const_expr_p=*/false,
30242 LOOKUP_ONLYCONVERTING
);
30246 pop_scope (pushed_scope
);
30252 /* If parsing a type specifier sequence failed, then
30253 this MUST be a simple expression. */
30254 if (code
== CILK_FOR
)
30255 error ("%<_Cilk_for%> allows expression instead of declaration only "
30256 "in C, not in C++");
30257 cp_parser_parse_tentatively (parser
);
30258 decl
= cp_parser_primary_expression (parser
, false, false,
30260 if (!cp_parser_error_occurred (parser
)
30263 && CLASS_TYPE_P (TREE_TYPE (decl
)))
30267 cp_parser_parse_definitely (parser
);
30268 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30269 rhs
= cp_parser_assignment_expression (parser
);
30270 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
30273 tf_warning_or_error
));
30274 add_private_clause
= true;
30279 cp_parser_abort_tentative_parse (parser
);
30280 init
= cp_parser_expression (parser
);
30283 if (TREE_CODE (init
) == MODIFY_EXPR
30284 || TREE_CODE (init
) == MODOP_EXPR
)
30285 real_decl
= TREE_OPERAND (init
, 0);
30289 return add_private_clause
;
30292 /* Parse the restricted form of the for statement allowed by OpenMP. */
30295 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
30298 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
30299 tree real_decl
, initv
, condv
, incrv
, declv
;
30300 tree this_pre_body
, cl
;
30301 location_t loc_first
;
30302 bool collapse_err
= false;
30303 int i
, collapse
= 1, nbraces
= 0;
30304 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
30306 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
30307 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
30308 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
30310 gcc_assert (collapse
>= 1);
30312 declv
= make_tree_vec (collapse
);
30313 initv
= make_tree_vec (collapse
);
30314 condv
= make_tree_vec (collapse
);
30315 incrv
= make_tree_vec (collapse
);
30317 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
30319 for (i
= 0; i
< collapse
; i
++)
30321 int bracecount
= 0;
30322 bool add_private_clause
= false;
30325 if (code
!= CILK_FOR
30326 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30328 cp_parser_error (parser
, "for statement expected");
30331 if (code
== CILK_FOR
30332 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
30334 cp_parser_error (parser
, "_Cilk_for statement expected");
30337 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
30339 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30342 init
= decl
= real_decl
= NULL
;
30343 this_pre_body
= push_stmt_list ();
30346 |= cp_parser_omp_for_loop_init (parser
, code
,
30347 this_pre_body
, for_block
,
30348 init
, decl
, real_decl
);
30350 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30353 this_pre_body
= pop_stmt_list (this_pre_body
);
30357 pre_body
= push_stmt_list ();
30359 add_stmt (this_pre_body
);
30360 pre_body
= pop_stmt_list (pre_body
);
30363 pre_body
= this_pre_body
;
30368 if (cclauses
!= NULL
30369 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
30370 && real_decl
!= NULL_TREE
)
30373 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
30374 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
30375 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30377 error_at (loc
, "iteration variable %qD"
30378 " should not be firstprivate", real_decl
);
30379 *c
= OMP_CLAUSE_CHAIN (*c
);
30381 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
30382 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30384 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30385 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
30386 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
30387 OMP_CLAUSE_DECL (l
) = real_decl
;
30388 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
30389 if (code
== OMP_SIMD
)
30391 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30392 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
30396 OMP_CLAUSE_CHAIN (l
) = clauses
;
30399 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
30400 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
30401 add_private_clause
= false;
30405 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
30406 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30407 add_private_clause
= false;
30408 c
= &OMP_CLAUSE_CHAIN (*c
);
30412 if (add_private_clause
)
30415 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30417 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
30418 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
30419 && OMP_CLAUSE_DECL (c
) == decl
)
30421 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
30422 && OMP_CLAUSE_DECL (c
) == decl
)
30423 error_at (loc
, "iteration variable %qD "
30424 "should not be firstprivate",
30426 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
30427 && OMP_CLAUSE_DECL (c
) == decl
)
30428 error_at (loc
, "iteration variable %qD should not be reduction",
30433 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
30434 OMP_CLAUSE_DECL (c
) = decl
;
30435 c
= finish_omp_clauses (c
);
30438 OMP_CLAUSE_CHAIN (c
) = clauses
;
30445 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30446 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
30447 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30450 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30452 /* If decl is an iterator, preserve the operator on decl
30453 until finish_omp_for. */
30455 && ((processing_template_decl
30456 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
30457 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
30458 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
30460 incr
= cp_parser_expression (parser
);
30461 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
30462 SET_EXPR_LOCATION (incr
, input_location
);
30465 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30466 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30467 /*or_comma=*/false,
30468 /*consume_paren=*/true);
30470 TREE_VEC_ELT (declv
, i
) = decl
;
30471 TREE_VEC_ELT (initv
, i
) = init
;
30472 TREE_VEC_ELT (condv
, i
) = cond
;
30473 TREE_VEC_ELT (incrv
, i
) = incr
;
30475 if (i
== collapse
- 1)
30478 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30479 in between the collapsed for loops to be still considered perfectly
30480 nested. Hopefully the final version clarifies this.
30481 For now handle (multiple) {'s and empty statements. */
30482 cp_parser_parse_tentatively (parser
);
30485 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30487 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30489 cp_lexer_consume_token (parser
->lexer
);
30492 else if (bracecount
30493 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30494 cp_lexer_consume_token (parser
->lexer
);
30497 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30498 error_at (loc
, "not enough collapsed for loops");
30499 collapse_err
= true;
30500 cp_parser_abort_tentative_parse (parser
);
30509 cp_parser_parse_definitely (parser
);
30510 nbraces
+= bracecount
;
30514 /* Note that we saved the original contents of this flag when we entered
30515 the structured block, and so we don't need to re-save it here. */
30516 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30517 parser
->in_statement
= IN_CILK_SIMD_FOR
;
30519 parser
->in_statement
= IN_OMP_FOR
;
30521 /* Note that the grammar doesn't call for a structured block here,
30522 though the loop as a whole is a structured block. */
30523 body
= push_stmt_list ();
30524 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30525 body
= pop_stmt_list (body
);
30527 if (declv
== NULL_TREE
)
30530 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
30531 pre_body
, clauses
);
30535 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30537 cp_lexer_consume_token (parser
->lexer
);
30540 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30541 cp_lexer_consume_token (parser
->lexer
);
30546 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30547 "collapsed loops not perfectly nested");
30549 collapse_err
= true;
30550 cp_parser_statement_seq_opt (parser
, NULL
);
30551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30556 while (!for_block
->is_empty ())
30557 add_stmt (pop_stmt_list (for_block
->pop ()));
30558 release_tree_vector (for_block
);
30563 /* Helper function for OpenMP parsing, split clauses and call
30564 finish_omp_clauses on each of the set of clauses afterwards. */
30567 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
30568 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
30571 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
30572 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30574 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30578 #pragma omp simd simd-clause[optseq] new-line
30581 #define OMP_SIMD_CLAUSE_MASK \
30582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30591 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30592 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30594 tree clauses
, sb
, ret
;
30596 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30598 strcat (p_name
, " simd");
30599 mask
|= OMP_SIMD_CLAUSE_MASK
;
30600 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30602 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30606 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30607 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30610 sb
= begin_omp_structured_block ();
30611 save
= cp_parser_begin_omp_structured_block (parser
);
30613 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30615 cp_parser_end_omp_structured_block (parser
, save
);
30616 add_stmt (finish_omp_structured_block (sb
));
30622 #pragma omp for for-clause[optseq] new-line
30626 #pragma omp for simd for-simd-clause[optseq] new-line
30629 #define OMP_FOR_CLAUSE_MASK \
30630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30640 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30641 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30643 tree clauses
, sb
, ret
;
30645 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30647 strcat (p_name
, " for");
30648 mask
|= OMP_FOR_CLAUSE_MASK
;
30650 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30654 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30655 const char *p
= IDENTIFIER_POINTER (id
);
30657 if (strcmp (p
, "simd") == 0)
30659 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30660 if (cclauses
== NULL
)
30661 cclauses
= cclauses_buf
;
30663 cp_lexer_consume_token (parser
->lexer
);
30664 if (!flag_openmp
) /* flag_openmp_simd */
30665 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30667 sb
= begin_omp_structured_block ();
30668 save
= cp_parser_begin_omp_structured_block (parser
);
30669 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30671 cp_parser_end_omp_structured_block (parser
, save
);
30672 tree body
= finish_omp_structured_block (sb
);
30675 ret
= make_node (OMP_FOR
);
30676 TREE_TYPE (ret
) = void_type_node
;
30677 OMP_FOR_BODY (ret
) = body
;
30678 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30679 SET_EXPR_LOCATION (ret
, loc
);
30684 if (!flag_openmp
) /* flag_openmp_simd */
30686 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30690 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30694 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30695 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30698 sb
= begin_omp_structured_block ();
30699 save
= cp_parser_begin_omp_structured_block (parser
);
30701 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30703 cp_parser_end_omp_structured_block (parser
, save
);
30704 add_stmt (finish_omp_structured_block (sb
));
30710 # pragma omp master new-line
30711 structured-block */
30714 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30716 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30717 return c_finish_omp_master (input_location
,
30718 cp_parser_omp_structured_block (parser
));
30722 # pragma omp ordered new-line
30723 structured-block */
30726 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30728 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30729 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30730 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30736 { section-sequence }
30739 section-directive[opt] structured-block
30740 section-sequence section-directive structured-block */
30743 cp_parser_omp_sections_scope (cp_parser
*parser
)
30745 tree stmt
, substmt
;
30746 bool error_suppress
= false;
30749 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30752 stmt
= push_stmt_list ();
30754 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30756 substmt
= cp_parser_omp_structured_block (parser
);
30757 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30758 add_stmt (substmt
);
30763 tok
= cp_lexer_peek_token (parser
->lexer
);
30764 if (tok
->type
== CPP_CLOSE_BRACE
)
30766 if (tok
->type
== CPP_EOF
)
30769 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30771 cp_lexer_consume_token (parser
->lexer
);
30772 cp_parser_require_pragma_eol (parser
, tok
);
30773 error_suppress
= false;
30775 else if (!error_suppress
)
30777 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30778 error_suppress
= true;
30781 substmt
= cp_parser_omp_structured_block (parser
);
30782 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30783 add_stmt (substmt
);
30785 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30787 substmt
= pop_stmt_list (stmt
);
30789 stmt
= make_node (OMP_SECTIONS
);
30790 TREE_TYPE (stmt
) = void_type_node
;
30791 OMP_SECTIONS_BODY (stmt
) = substmt
;
30798 # pragma omp sections sections-clause[optseq] newline
30801 #define OMP_SECTIONS_CLAUSE_MASK \
30802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30809 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
30810 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30813 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30815 strcat (p_name
, " sections");
30816 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
30818 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30820 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30824 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
30825 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
30828 ret
= cp_parser_omp_sections_scope (parser
);
30830 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
30836 # pragma omp parallel parallel-clause[optseq] new-line
30838 # pragma omp parallel for parallel-for-clause[optseq] new-line
30840 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30844 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30845 structured-block */
30847 #define OMP_PARALLEL_CLAUSE_MASK \
30848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30859 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
30860 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30862 tree stmt
, clauses
, block
;
30864 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30866 strcat (p_name
, " parallel");
30867 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
30869 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30871 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30872 if (cclauses
== NULL
)
30873 cclauses
= cclauses_buf
;
30875 cp_lexer_consume_token (parser
->lexer
);
30876 if (!flag_openmp
) /* flag_openmp_simd */
30877 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30878 block
= begin_omp_parallel ();
30879 save
= cp_parser_begin_omp_structured_block (parser
);
30880 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30881 cp_parser_end_omp_structured_block (parser
, save
);
30882 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30884 if (ret
== NULL_TREE
)
30886 OMP_PARALLEL_COMBINED (stmt
) = 1;
30891 error_at (loc
, "expected %<for%> after %qs", p_name
);
30892 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30895 else if (!flag_openmp
) /* flag_openmp_simd */
30897 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30900 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30902 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30903 const char *p
= IDENTIFIER_POINTER (id
);
30904 if (strcmp (p
, "sections") == 0)
30906 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30907 cclauses
= cclauses_buf
;
30909 cp_lexer_consume_token (parser
->lexer
);
30910 block
= begin_omp_parallel ();
30911 save
= cp_parser_begin_omp_structured_block (parser
);
30912 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30913 cp_parser_end_omp_structured_block (parser
, save
);
30914 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30916 OMP_PARALLEL_COMBINED (stmt
) = 1;
30921 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
30923 block
= begin_omp_parallel ();
30924 save
= cp_parser_begin_omp_structured_block (parser
);
30925 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30926 cp_parser_end_omp_structured_block (parser
, save
);
30927 stmt
= finish_omp_parallel (clauses
, block
);
30932 # pragma omp single single-clause[optseq] new-line
30933 structured-block */
30935 #define OMP_SINGLE_CLAUSE_MASK \
30936 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30942 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
30944 tree stmt
= make_node (OMP_SINGLE
);
30945 TREE_TYPE (stmt
) = void_type_node
;
30947 OMP_SINGLE_CLAUSES (stmt
)
30948 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
30949 "#pragma omp single", pragma_tok
);
30950 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30952 return add_stmt (stmt
);
30956 # pragma omp task task-clause[optseq] new-line
30957 structured-block */
30959 #define OMP_TASK_CLAUSE_MASK \
30960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30971 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
30973 tree clauses
, block
;
30976 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
30977 "#pragma omp task", pragma_tok
);
30978 block
= begin_omp_task ();
30979 save
= cp_parser_begin_omp_structured_block (parser
);
30980 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30981 cp_parser_end_omp_structured_block (parser
, save
);
30982 return finish_omp_task (clauses
, block
);
30986 # pragma omp taskwait new-line */
30989 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
30991 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30992 finish_omp_taskwait ();
30996 # pragma omp taskyield new-line */
30999 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
31001 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31002 finish_omp_taskyield ();
31006 # pragma omp taskgroup new-line
31007 structured-block */
31010 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
31012 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31013 return c_finish_omp_taskgroup (input_location
,
31014 cp_parser_omp_structured_block (parser
));
31019 # pragma omp threadprivate (variable-list) */
31022 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
31026 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31027 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31029 finish_omp_threadprivate (vars
);
31033 # pragma omp cancel cancel-clause[optseq] new-line */
31035 #define OMP_CANCEL_CLAUSE_MASK \
31036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31043 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
31045 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
31046 "#pragma omp cancel", pragma_tok
);
31047 finish_omp_cancel (clauses
);
31051 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31053 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31060 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
31063 bool point_seen
= false;
31065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31067 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31068 const char *p
= IDENTIFIER_POINTER (id
);
31070 if (strcmp (p
, "point") == 0)
31072 cp_lexer_consume_token (parser
->lexer
);
31078 cp_parser_error (parser
, "expected %<point%>");
31079 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31083 clauses
= cp_parser_omp_all_clauses (parser
,
31084 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
31085 "#pragma omp cancellation point",
31087 finish_omp_cancellation_point (clauses
);
31091 #pragma omp distribute distribute-clause[optseq] new-line
31094 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31101 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
31102 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31104 tree clauses
, sb
, ret
;
31106 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31108 strcat (p_name
, " distribute");
31109 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
31111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31113 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31114 const char *p
= IDENTIFIER_POINTER (id
);
31116 bool parallel
= false;
31118 if (strcmp (p
, "simd") == 0)
31121 parallel
= strcmp (p
, "parallel") == 0;
31122 if (parallel
|| simd
)
31124 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31125 if (cclauses
== NULL
)
31126 cclauses
= cclauses_buf
;
31127 cp_lexer_consume_token (parser
->lexer
);
31128 if (!flag_openmp
) /* flag_openmp_simd */
31131 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31134 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31137 sb
= begin_omp_structured_block ();
31138 save
= cp_parser_begin_omp_structured_block (parser
);
31140 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31143 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31145 cp_parser_end_omp_structured_block (parser
, save
);
31146 tree body
= finish_omp_structured_block (sb
);
31149 ret
= make_node (OMP_DISTRIBUTE
);
31150 TREE_TYPE (ret
) = void_type_node
;
31151 OMP_FOR_BODY (ret
) = body
;
31152 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31153 SET_EXPR_LOCATION (ret
, loc
);
31158 if (!flag_openmp
) /* flag_openmp_simd */
31160 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31164 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31168 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
31169 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31172 sb
= begin_omp_structured_block ();
31173 save
= cp_parser_begin_omp_structured_block (parser
);
31175 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
31177 cp_parser_end_omp_structured_block (parser
, save
);
31178 add_stmt (finish_omp_structured_block (sb
));
31184 # pragma omp teams teams-clause[optseq] new-line
31185 structured-block */
31187 #define OMP_TEAMS_CLAUSE_MASK \
31188 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31197 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
31198 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31200 tree clauses
, sb
, ret
;
31202 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31204 strcat (p_name
, " teams");
31205 mask
|= OMP_TEAMS_CLAUSE_MASK
;
31207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31209 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31210 const char *p
= IDENTIFIER_POINTER (id
);
31211 if (strcmp (p
, "distribute") == 0)
31213 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31214 if (cclauses
== NULL
)
31215 cclauses
= cclauses_buf
;
31217 cp_lexer_consume_token (parser
->lexer
);
31218 if (!flag_openmp
) /* flag_openmp_simd */
31219 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31221 sb
= begin_omp_structured_block ();
31222 save
= cp_parser_begin_omp_structured_block (parser
);
31223 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31225 cp_parser_end_omp_structured_block (parser
, save
);
31226 tree body
= finish_omp_structured_block (sb
);
31229 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31230 ret
= make_node (OMP_TEAMS
);
31231 TREE_TYPE (ret
) = void_type_node
;
31232 OMP_TEAMS_CLAUSES (ret
) = clauses
;
31233 OMP_TEAMS_BODY (ret
) = body
;
31234 return add_stmt (ret
);
31237 if (!flag_openmp
) /* flag_openmp_simd */
31239 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31243 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31247 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
31248 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31251 tree stmt
= make_node (OMP_TEAMS
);
31252 TREE_TYPE (stmt
) = void_type_node
;
31253 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
31254 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31256 return add_stmt (stmt
);
31260 # pragma omp target data target-data-clause[optseq] new-line
31261 structured-block */
31263 #define OMP_TARGET_DATA_CLAUSE_MASK \
31264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31269 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31271 tree stmt
= make_node (OMP_TARGET_DATA
);
31272 TREE_TYPE (stmt
) = void_type_node
;
31274 OMP_TARGET_DATA_CLAUSES (stmt
)
31275 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
31276 "#pragma omp target data", pragma_tok
);
31277 keep_next_level (true);
31278 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31280 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31281 return add_stmt (stmt
);
31285 # pragma omp target update target-update-clause[optseq] new-line */
31287 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31294 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
31295 enum pragma_context context
)
31297 if (context
== pragma_stmt
)
31299 error_at (pragma_tok
->location
,
31300 "%<#pragma omp target update%> may only be "
31301 "used in compound statements");
31302 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31307 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
31308 "#pragma omp target update", pragma_tok
);
31309 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
31310 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
31312 error_at (pragma_tok
->location
,
31313 "%<#pragma omp target update must contain at least one "
31314 "%<from%> or %<to%> clauses");
31318 tree stmt
= make_node (OMP_TARGET_UPDATE
);
31319 TREE_TYPE (stmt
) = void_type_node
;
31320 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
31321 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31327 # pragma omp target target-clause[optseq] new-line
31328 structured-block */
31330 #define OMP_TARGET_CLAUSE_MASK \
31331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31336 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
31337 enum pragma_context context
)
31339 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31341 cp_parser_error (parser
, "expected declaration specifiers");
31342 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31346 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31348 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31349 const char *p
= IDENTIFIER_POINTER (id
);
31351 if (strcmp (p
, "teams") == 0)
31353 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
31354 char p_name
[sizeof ("#pragma omp target teams distribute "
31355 "parallel for simd")];
31357 cp_lexer_consume_token (parser
->lexer
);
31358 strcpy (p_name
, "#pragma omp target");
31359 if (!flag_openmp
) /* flag_openmp_simd */
31361 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31362 OMP_TARGET_CLAUSE_MASK
,
31364 return stmt
!= NULL_TREE
;
31366 keep_next_level (true);
31367 tree sb
= begin_omp_structured_block ();
31368 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
31369 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31370 OMP_TARGET_CLAUSE_MASK
, cclauses
);
31371 cp_parser_end_omp_structured_block (parser
, save
);
31372 tree body
= finish_omp_structured_block (sb
);
31373 if (ret
== NULL_TREE
)
31375 tree stmt
= make_node (OMP_TARGET
);
31376 TREE_TYPE (stmt
) = void_type_node
;
31377 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
31378 OMP_TARGET_BODY (stmt
) = body
;
31382 else if (!flag_openmp
) /* flag_openmp_simd */
31384 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31387 else if (strcmp (p
, "data") == 0)
31389 cp_lexer_consume_token (parser
->lexer
);
31390 cp_parser_omp_target_data (parser
, pragma_tok
);
31393 else if (strcmp (p
, "update") == 0)
31395 cp_lexer_consume_token (parser
->lexer
);
31396 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
31400 tree stmt
= make_node (OMP_TARGET
);
31401 TREE_TYPE (stmt
) = void_type_node
;
31403 OMP_TARGET_CLAUSES (stmt
)
31404 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
31405 "#pragma omp target", pragma_tok
);
31406 keep_next_level (true);
31407 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31409 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31415 # pragma acc cache (variable-list) new-line
31419 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
31421 tree stmt
, clauses
;
31423 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
31424 clauses
= finish_omp_clauses (clauses
);
31426 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
31428 stmt
= make_node (OACC_CACHE
);
31429 TREE_TYPE (stmt
) = void_type_node
;
31430 OACC_CACHE_CLAUSES (stmt
) = clauses
;
31431 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31438 # pragma acc data oacc-data-clause[optseq] new-line
31439 structured-block */
31441 #define OACC_DATA_CLAUSE_MASK \
31442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31455 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31457 tree stmt
, clauses
, block
;
31460 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
31461 "#pragma acc data", pragma_tok
);
31463 block
= begin_omp_parallel ();
31464 save
= cp_parser_begin_omp_structured_block (parser
);
31465 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31466 cp_parser_end_omp_structured_block (parser
, save
);
31467 stmt
= finish_oacc_data (clauses
, block
);
31472 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31476 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31478 LOC is the location of the #pragma token.
31481 #define OACC_ENTER_DATA_CLAUSE_MASK \
31482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31490 #define OACC_EXIT_DATA_CLAUSE_MASK \
31491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31498 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
31501 tree stmt
, clauses
;
31503 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
31504 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31506 cp_parser_error (parser
, enter
31507 ? "expected %<data%> in %<#pragma acc enter data%>"
31508 : "expected %<data%> in %<#pragma acc exit data%>");
31509 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31514 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
31515 if (strcmp (p
, "data") != 0)
31517 cp_parser_error (parser
, "invalid pragma");
31518 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31522 cp_lexer_consume_token (parser
->lexer
);
31525 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
31526 "#pragma acc enter data", pragma_tok
);
31528 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
31529 "#pragma acc exit data", pragma_tok
);
31531 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31533 error_at (pragma_tok
->location
,
31534 "%<#pragma acc enter data%> has no data movement clause");
31538 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
31539 TREE_TYPE (stmt
) = void_type_node
;
31541 OACC_ENTER_DATA_CLAUSES (stmt
) = clauses
;
31543 OACC_EXIT_DATA_CLAUSES (stmt
) = clauses
;
31544 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31550 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31551 structured-block */
31553 #define OACC_KERNELS_CLAUSE_MASK \
31554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31569 cp_parser_oacc_kernels (cp_parser
*parser
, cp_token
*pragma_tok
)
31571 tree stmt
, clauses
, block
;
31574 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_KERNELS_CLAUSE_MASK
,
31575 "#pragma acc kernels", pragma_tok
);
31577 block
= begin_omp_parallel ();
31578 save
= cp_parser_begin_omp_structured_block (parser
);
31579 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31580 cp_parser_end_omp_structured_block (parser
, save
);
31581 stmt
= finish_oacc_kernels (clauses
, block
);
31586 # pragma acc loop oacc-loop-clause[optseq] new-line
31587 structured-block */
31589 #define OACC_LOOP_CLAUSE_MASK \
31590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31594 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
)
31596 tree stmt
, clauses
, block
;
31599 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_LOOP_CLAUSE_MASK
,
31600 "#pragma acc loop", pragma_tok
);
31602 block
= begin_omp_structured_block ();
31603 save
= cp_parser_begin_omp_structured_block (parser
);
31604 stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
31605 cp_parser_end_omp_structured_block (parser
, save
);
31606 add_stmt (finish_omp_structured_block (block
));
31611 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31612 structured-block */
31614 #define OACC_PARALLEL_CLAUSE_MASK \
31615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31634 cp_parser_oacc_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
31636 tree stmt
, clauses
, block
;
31639 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_PARALLEL_CLAUSE_MASK
,
31640 "#pragma acc parallel", pragma_tok
);
31642 block
= begin_omp_parallel ();
31643 save
= cp_parser_begin_omp_structured_block (parser
);
31644 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31645 cp_parser_end_omp_structured_block (parser
, save
);
31646 stmt
= finish_oacc_parallel (clauses
, block
);
31651 # pragma acc update oacc-update-clause[optseq] new-line
31654 #define OACC_UPDATE_CLAUSE_MASK \
31655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31663 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
31665 tree stmt
, clauses
;
31667 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
31668 "#pragma acc update", pragma_tok
);
31670 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31672 error_at (pragma_tok
->location
,
31673 "%<#pragma acc update%> must contain at least one "
31674 "%<device%> or %<host/self%> clause");
31678 stmt
= make_node (OACC_UPDATE
);
31679 TREE_TYPE (stmt
) = void_type_node
;
31680 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
31681 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31687 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31689 LOC is the location of the #pragma token.
31692 #define OACC_WAIT_CLAUSE_MASK \
31693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31696 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
31698 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
31699 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31701 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31702 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
31704 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
31705 "#pragma acc wait", pragma_tok
);
31707 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
31713 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31715 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31716 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31724 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
31725 enum pragma_context context
)
31727 bool first_p
= parser
->omp_declare_simd
== NULL
;
31728 cp_omp_declare_simd_data data
;
31731 data
.error_seen
= false;
31732 data
.fndecl_seen
= false;
31733 data
.tokens
= vNULL
;
31734 parser
->omp_declare_simd
= &data
;
31736 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31737 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31738 cp_lexer_consume_token (parser
->lexer
);
31739 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31740 parser
->omp_declare_simd
->error_seen
= true;
31741 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31742 struct cp_token_cache
*cp
31743 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
31744 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
31747 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
31748 cp_parser_pragma (parser
, context
);
31751 case pragma_external
:
31752 cp_parser_declaration (parser
);
31754 case pragma_member
:
31755 cp_parser_member_declaration (parser
);
31757 case pragma_objc_icode
:
31758 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31761 cp_parser_declaration_statement (parser
);
31764 if (parser
->omp_declare_simd
31765 && !parser
->omp_declare_simd
->error_seen
31766 && !parser
->omp_declare_simd
->fndecl_seen
)
31767 error_at (pragma_tok
->location
,
31768 "%<#pragma omp declare simd%> not immediately followed by "
31769 "function declaration or definition");
31770 data
.tokens
.release ();
31771 parser
->omp_declare_simd
= NULL
;
31775 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31776 This function is modelled similar to the late parsing of omp declare
31780 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
31782 struct cp_token_cache
*ce
;
31783 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
31786 if (parser
->omp_declare_simd
!= NULL
)
31788 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31789 " marked as a Cilk Plus SIMD-enabled function");
31790 XDELETE (parser
->cilk_simd_fn_info
);
31791 parser
->cilk_simd_fn_info
= NULL
;
31794 if (!info
->error_seen
&& info
->fndecl_seen
)
31796 error ("vector attribute not immediately followed by a single function"
31797 " declaration or definition");
31798 info
->error_seen
= true;
31800 if (info
->error_seen
)
31803 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
31807 cp_parser_push_lexer_for_tokens (parser
, ce
);
31808 parser
->lexer
->in_pragma
= true;
31809 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
31810 "SIMD-enabled functions attribute",
31812 cp_parser_pop_lexer (parser
);
31814 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31816 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
31817 TREE_CHAIN (c
) = attrs
;
31820 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31821 TREE_CHAIN (c
) = attrs
;
31822 if (processing_template_decl
)
31823 ATTR_IS_DEPENDENT (c
) = 1;
31826 info
->fndecl_seen
= true;
31827 XDELETE (parser
->cilk_simd_fn_info
);
31828 parser
->cilk_simd_fn_info
= NULL
;
31832 /* Finalize #pragma omp declare simd clauses after direct declarator has
31833 been parsed, and put that into "omp declare simd" attribute. */
31836 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
31838 struct cp_token_cache
*ce
;
31839 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
31842 if (!data
->error_seen
&& data
->fndecl_seen
)
31844 error ("%<#pragma omp declare simd%> not immediately followed by "
31845 "a single function declaration or definition");
31846 data
->error_seen
= true;
31849 if (data
->error_seen
)
31852 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
31856 cp_parser_push_lexer_for_tokens (parser
, ce
);
31857 parser
->lexer
->in_pragma
= true;
31858 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
31859 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31860 cp_lexer_consume_token (parser
->lexer
);
31861 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
31862 "#pragma omp declare simd", pragma_tok
);
31863 cp_parser_pop_lexer (parser
);
31865 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31866 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31867 TREE_CHAIN (c
) = attrs
;
31868 if (processing_template_decl
)
31869 ATTR_IS_DEPENDENT (c
) = 1;
31873 data
->fndecl_seen
= true;
31879 # pragma omp declare target new-line
31880 declarations and definitions
31881 # pragma omp end declare target new-line */
31884 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31886 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31887 scope_chain
->omp_declare_target_attribute
++;
31891 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31893 const char *p
= "";
31894 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31896 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31897 p
= IDENTIFIER_POINTER (id
);
31899 if (strcmp (p
, "declare") == 0)
31901 cp_lexer_consume_token (parser
->lexer
);
31903 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31905 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31906 p
= IDENTIFIER_POINTER (id
);
31908 if (strcmp (p
, "target") == 0)
31909 cp_lexer_consume_token (parser
->lexer
);
31912 cp_parser_error (parser
, "expected %<target%>");
31913 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31919 cp_parser_error (parser
, "expected %<declare%>");
31920 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31923 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31924 if (!scope_chain
->omp_declare_target_attribute
)
31925 error_at (pragma_tok
->location
,
31926 "%<#pragma omp end declare target%> without corresponding "
31927 "%<#pragma omp declare target%>");
31929 scope_chain
->omp_declare_target_attribute
--;
31932 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31933 expression and optional initializer clause of
31934 #pragma omp declare reduction. We store the expression(s) as
31935 either 3, 6 or 7 special statements inside of the artificial function's
31936 body. The first two statements are DECL_EXPRs for the artificial
31937 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31938 expression that uses those variables.
31939 If there was any INITIALIZER clause, this is followed by further statements,
31940 the fourth and fifth statements are DECL_EXPRs for the artificial
31941 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31942 constructor variant (first token after open paren is not omp_priv),
31943 then the sixth statement is a statement with the function call expression
31944 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31945 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31946 to initialize the OMP_PRIV artificial variable and there is seventh
31947 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31950 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
31952 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
31953 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
31954 type
= TREE_TYPE (type
);
31955 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
31956 DECL_ARTIFICIAL (omp_out
) = 1;
31957 pushdecl (omp_out
);
31958 add_decl_expr (omp_out
);
31959 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
31960 DECL_ARTIFICIAL (omp_in
) = 1;
31962 add_decl_expr (omp_in
);
31964 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
31966 keep_next_level (true);
31967 tree block
= begin_omp_structured_block ();
31968 combiner
= cp_parser_expression (parser
);
31969 finish_expr_stmt (combiner
);
31970 block
= finish_omp_structured_block (block
);
31973 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31976 const char *p
= "";
31977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31979 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31980 p
= IDENTIFIER_POINTER (id
);
31983 if (strcmp (p
, "initializer") == 0)
31985 cp_lexer_consume_token (parser
->lexer
);
31986 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31990 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31992 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31993 p
= IDENTIFIER_POINTER (id
);
31996 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
31997 DECL_ARTIFICIAL (omp_priv
) = 1;
31998 pushdecl (omp_priv
);
31999 add_decl_expr (omp_priv
);
32000 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
32001 DECL_ARTIFICIAL (omp_orig
) = 1;
32002 pushdecl (omp_orig
);
32003 add_decl_expr (omp_orig
);
32005 keep_next_level (true);
32006 block
= begin_omp_structured_block ();
32009 if (strcmp (p
, "omp_priv") == 0)
32011 bool is_direct_init
, is_non_constant_init
;
32013 cp_lexer_consume_token (parser
->lexer
);
32014 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
32016 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32017 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
32019 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
32020 == CPP_CLOSE_PAREN
))
32022 finish_omp_structured_block (block
);
32023 error ("invalid initializer clause");
32026 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
32027 &is_non_constant_init
);
32028 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
32029 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
32033 cp_parser_parse_tentatively (parser
);
32034 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32035 /*check_dependency_p=*/true,
32036 /*template_p=*/NULL
,
32037 /*declarator_p=*/false,
32038 /*optional_p=*/false);
32039 vec
<tree
, va_gc
> *args
;
32040 if (fn_name
== error_mark_node
32041 || cp_parser_error_occurred (parser
)
32042 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32043 || ((args
= cp_parser_parenthesized_expression_list
32044 (parser
, non_attr
, /*cast_p=*/false,
32045 /*allow_expansion_p=*/true,
32046 /*non_constant_p=*/NULL
)),
32047 cp_parser_error_occurred (parser
)))
32049 finish_omp_structured_block (block
);
32050 cp_parser_abort_tentative_parse (parser
);
32051 cp_parser_error (parser
, "expected id-expression (arguments)");
32056 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
32057 if (arg
== omp_priv
32058 || (TREE_CODE (arg
) == ADDR_EXPR
32059 && TREE_OPERAND (arg
, 0) == omp_priv
))
32061 cp_parser_abort_tentative_parse (parser
);
32062 if (arg
== NULL_TREE
)
32063 error ("one of the initializer call arguments should be %<omp_priv%>"
32064 " or %<&omp_priv%>");
32065 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
32067 finish_expr_stmt (initializer
);
32070 block
= finish_omp_structured_block (block
);
32071 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
32075 add_decl_expr (omp_orig
);
32077 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32081 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
32082 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
32088 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32089 initializer-clause[opt] new-line
32091 initializer-clause:
32092 initializer (omp_priv initializer)
32093 initializer (function-name (argument-list)) */
32096 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
32097 enum pragma_context
)
32099 auto_vec
<tree
> types
;
32100 enum tree_code reduc_code
= ERROR_MARK
;
32101 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
32103 cp_token
*first_token
;
32104 cp_token_cache
*cp
;
32108 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32109 p
= obstack_alloc (&declarator_obstack
, 0);
32111 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32114 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32117 reduc_code
= PLUS_EXPR
;
32120 reduc_code
= MULT_EXPR
;
32123 reduc_code
= MINUS_EXPR
;
32126 reduc_code
= BIT_AND_EXPR
;
32129 reduc_code
= BIT_XOR_EXPR
;
32132 reduc_code
= BIT_IOR_EXPR
;
32135 reduc_code
= TRUTH_ANDIF_EXPR
;
32138 reduc_code
= TRUTH_ORIF_EXPR
;
32141 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
32144 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32145 "%<|%>, %<&&%>, %<||%> or identifier");
32149 if (reduc_code
!= ERROR_MARK
)
32150 cp_lexer_consume_token (parser
->lexer
);
32152 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
32153 if (reduc_id
== error_mark_node
)
32156 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32159 /* Types may not be defined in declare reduction type list. */
32160 const char *saved_message
;
32161 saved_message
= parser
->type_definition_forbidden_message
;
32162 parser
->type_definition_forbidden_message
32163 = G_("types may not be defined in declare reduction type list");
32164 bool saved_colon_corrects_to_scope_p
;
32165 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32166 parser
->colon_corrects_to_scope_p
= false;
32167 bool saved_colon_doesnt_start_class_def_p
;
32168 saved_colon_doesnt_start_class_def_p
32169 = parser
->colon_doesnt_start_class_def_p
;
32170 parser
->colon_doesnt_start_class_def_p
= true;
32174 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32175 type
= cp_parser_type_id (parser
);
32176 if (type
== error_mark_node
)
32178 else if (ARITHMETIC_TYPE_P (type
)
32179 && (orig_reduc_id
== NULL_TREE
32180 || (TREE_CODE (type
) != COMPLEX_TYPE
32181 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32183 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32185 error_at (loc
, "predeclared arithmetic type %qT in "
32186 "%<#pragma omp declare reduction%>", type
);
32187 else if (TREE_CODE (type
) == FUNCTION_TYPE
32188 || TREE_CODE (type
) == METHOD_TYPE
32189 || TREE_CODE (type
) == ARRAY_TYPE
)
32190 error_at (loc
, "function or array type %qT in "
32191 "%<#pragma omp declare reduction%>", type
);
32192 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
32193 error_at (loc
, "reference type %qT in "
32194 "%<#pragma omp declare reduction%>", type
);
32195 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
32196 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
32197 "%<#pragma omp declare reduction%>", type
);
32199 types
.safe_push (type
);
32201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32202 cp_lexer_consume_token (parser
->lexer
);
32207 /* Restore the saved message. */
32208 parser
->type_definition_forbidden_message
= saved_message
;
32209 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32210 parser
->colon_doesnt_start_class_def_p
32211 = saved_colon_doesnt_start_class_def_p
;
32213 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
32214 || types
.is_empty ())
32217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32221 first_token
= cp_lexer_peek_token (parser
->lexer
);
32224 FOR_EACH_VEC_ELT (types
, i
, type
)
32227 = build_function_type_list (void_type_node
,
32228 cp_build_reference_type (type
, false),
32230 tree this_reduc_id
= reduc_id
;
32231 if (!dependent_type_p (type
))
32232 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
32233 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
32234 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
32235 DECL_ARTIFICIAL (fndecl
) = 1;
32236 DECL_EXTERNAL (fndecl
) = 1;
32237 DECL_DECLARED_INLINE_P (fndecl
) = 1;
32238 DECL_IGNORED_P (fndecl
) = 1;
32239 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
32240 DECL_ATTRIBUTES (fndecl
)
32241 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
32242 DECL_ATTRIBUTES (fndecl
));
32243 if (processing_template_decl
)
32244 fndecl
= push_template_decl (fndecl
);
32245 bool block_scope
= false;
32246 tree block
= NULL_TREE
;
32247 if (current_function_decl
)
32249 block_scope
= true;
32250 DECL_CONTEXT (fndecl
) = global_namespace
;
32251 if (!processing_template_decl
)
32254 else if (current_class_type
)
32258 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32259 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
32260 cp_lexer_consume_token (parser
->lexer
);
32261 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32263 cp
= cp_token_cache_new (first_token
,
32264 cp_lexer_peek_nth_token (parser
->lexer
,
32267 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
32268 finish_member_declaration (fndecl
);
32269 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
32270 DECL_PENDING_INLINE_P (fndecl
) = 1;
32271 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
32276 DECL_CONTEXT (fndecl
) = current_namespace
;
32280 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
32282 block
= begin_omp_structured_block ();
32285 cp_parser_push_lexer_for_tokens (parser
, cp
);
32286 parser
->lexer
->in_pragma
= true;
32288 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
32291 finish_function (0);
32293 DECL_CONTEXT (fndecl
) = current_function_decl
;
32295 cp_parser_pop_lexer (parser
);
32299 cp_parser_pop_lexer (parser
);
32301 finish_function (0);
32304 DECL_CONTEXT (fndecl
) = current_function_decl
;
32305 block
= finish_omp_structured_block (block
);
32306 if (TREE_CODE (block
) == BIND_EXPR
)
32307 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
32308 else if (TREE_CODE (block
) == STATEMENT_LIST
)
32309 DECL_SAVED_TREE (fndecl
) = block
;
32310 if (processing_template_decl
)
32311 add_decl_expr (fndecl
);
32313 cp_check_omp_declare_reduction (fndecl
);
32314 if (cp
== NULL
&& types
.length () > 1)
32315 cp
= cp_token_cache_new (first_token
,
32316 cp_lexer_peek_nth_token (parser
->lexer
, 2));
32317 if (errs
!= errorcount
)
32321 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32324 /* Free any declarators allocated. */
32325 obstack_free (&declarator_obstack
, p
);
32329 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32330 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32331 initializer-clause[opt] new-line
32332 #pragma omp declare target new-line */
32335 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
32336 enum pragma_context context
)
32338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32340 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32341 const char *p
= IDENTIFIER_POINTER (id
);
32343 if (strcmp (p
, "simd") == 0)
32345 cp_lexer_consume_token (parser
->lexer
);
32346 cp_parser_omp_declare_simd (parser
, pragma_tok
,
32350 cp_ensure_no_omp_declare_simd (parser
);
32351 if (strcmp (p
, "reduction") == 0)
32353 cp_lexer_consume_token (parser
->lexer
);
32354 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
32358 if (!flag_openmp
) /* flag_openmp_simd */
32360 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32363 if (strcmp (p
, "target") == 0)
32365 cp_lexer_consume_token (parser
->lexer
);
32366 cp_parser_omp_declare_target (parser
, pragma_tok
);
32370 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
32372 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32375 /* Main entry point to OpenMP statement pragmas. */
32378 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
32381 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
32382 omp_clause_mask
mask (0);
32384 switch (pragma_tok
->pragma_kind
)
32386 case PRAGMA_OACC_CACHE
:
32387 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
32389 case PRAGMA_OACC_DATA
:
32390 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
32392 case PRAGMA_OACC_ENTER_DATA
:
32393 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
32395 case PRAGMA_OACC_EXIT_DATA
:
32396 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
32398 case PRAGMA_OACC_KERNELS
:
32399 stmt
= cp_parser_oacc_kernels (parser
, pragma_tok
);
32401 case PRAGMA_OACC_LOOP
:
32402 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
);
32404 case PRAGMA_OACC_PARALLEL
:
32405 stmt
= cp_parser_oacc_parallel (parser
, pragma_tok
);
32407 case PRAGMA_OACC_UPDATE
:
32408 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
32410 case PRAGMA_OACC_WAIT
:
32411 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
32413 case PRAGMA_OMP_ATOMIC
:
32414 cp_parser_omp_atomic (parser
, pragma_tok
);
32416 case PRAGMA_OMP_CRITICAL
:
32417 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
32419 case PRAGMA_OMP_DISTRIBUTE
:
32420 strcpy (p_name
, "#pragma omp");
32421 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
32423 case PRAGMA_OMP_FOR
:
32424 strcpy (p_name
, "#pragma omp");
32425 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
32427 case PRAGMA_OMP_MASTER
:
32428 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
32430 case PRAGMA_OMP_ORDERED
:
32431 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
32433 case PRAGMA_OMP_PARALLEL
:
32434 strcpy (p_name
, "#pragma omp");
32435 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
32437 case PRAGMA_OMP_SECTIONS
:
32438 strcpy (p_name
, "#pragma omp");
32439 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
32441 case PRAGMA_OMP_SIMD
:
32442 strcpy (p_name
, "#pragma omp");
32443 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
32445 case PRAGMA_OMP_SINGLE
:
32446 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
32448 case PRAGMA_OMP_TASK
:
32449 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
32451 case PRAGMA_OMP_TASKGROUP
:
32452 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
32454 case PRAGMA_OMP_TEAMS
:
32455 strcpy (p_name
, "#pragma omp");
32456 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
32459 gcc_unreachable ();
32463 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
32466 /* Transactional Memory parsing routines. */
32468 /* Parse a transaction attribute.
32474 ??? Simplify this when C++0x bracket attributes are
32475 implemented properly. */
32478 cp_parser_txn_attribute_opt (cp_parser
*parser
)
32481 tree attr_name
, attr
= NULL
;
32483 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32484 return cp_parser_attributes_opt (parser
);
32486 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
32488 cp_lexer_consume_token (parser
->lexer
);
32489 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
32492 token
= cp_lexer_peek_token (parser
->lexer
);
32493 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
32495 token
= cp_lexer_consume_token (parser
->lexer
);
32497 attr_name
= (token
->type
== CPP_KEYWORD
32498 /* For keywords, use the canonical spelling,
32499 not the parsed identifier. */
32500 ? ridpointers
[(int) token
->keyword
]
32502 attr
= build_tree_list (attr_name
, NULL_TREE
);
32505 cp_parser_error (parser
, "expected identifier");
32507 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32509 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32513 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32515 transaction-statement:
32516 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32518 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32522 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
32524 unsigned char old_in
= parser
->in_transaction
;
32525 unsigned char this_in
= 1, new_in
;
32527 tree stmt
, attrs
, noex
;
32529 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32530 || keyword
== RID_TRANSACTION_RELAXED
);
32531 token
= cp_parser_require_keyword (parser
, keyword
,
32532 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32533 : RT_TRANSACTION_RELAXED
));
32534 gcc_assert (token
!= NULL
);
32536 if (keyword
== RID_TRANSACTION_RELAXED
)
32537 this_in
|= TM_STMT_ATTR_RELAXED
;
32540 attrs
= cp_parser_txn_attribute_opt (parser
);
32542 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32545 /* Parse a noexcept specification. */
32546 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
32548 /* Keep track if we're in the lexical scope of an outer transaction. */
32549 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
32551 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
32553 parser
->in_transaction
= new_in
;
32554 cp_parser_compound_statement (parser
, NULL
, false, false);
32555 parser
->in_transaction
= old_in
;
32557 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
32562 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32564 transaction-expression:
32565 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32566 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32570 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
32572 unsigned char old_in
= parser
->in_transaction
;
32573 unsigned char this_in
= 1;
32578 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32579 || keyword
== RID_TRANSACTION_RELAXED
);
32582 error (keyword
== RID_TRANSACTION_RELAXED
32583 ? G_("%<__transaction_relaxed%> without transactional memory "
32585 : G_("%<__transaction_atomic%> without transactional memory "
32586 "support enabled"));
32588 token
= cp_parser_require_keyword (parser
, keyword
,
32589 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32590 : RT_TRANSACTION_RELAXED
));
32591 gcc_assert (token
!= NULL
);
32593 if (keyword
== RID_TRANSACTION_RELAXED
)
32594 this_in
|= TM_STMT_ATTR_RELAXED
;
32596 /* Set this early. This might mean that we allow transaction_cancel in
32597 an expression that we find out later actually has to be a constexpr.
32598 However, we expect that cxx_constant_value will be able to deal with
32599 this; also, if the noexcept has no constexpr, then what we parse next
32600 really is a transaction's body. */
32601 parser
->in_transaction
= this_in
;
32603 /* Parse a noexcept specification. */
32604 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
32607 if (!noex
|| !noex_expr
32608 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32610 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
32612 expr
= cp_parser_expression (parser
);
32613 expr
= finish_parenthesized_expr (expr
);
32615 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
32619 /* The only expression that is available got parsed for the noexcept
32620 already. noexcept is true then. */
32622 noex
= boolean_true_node
;
32625 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
32626 parser
->in_transaction
= old_in
;
32628 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
32629 return error_mark_node
;
32631 return (flag_tm
? expr
: error_mark_node
);
32634 /* Parse a function-transaction-block.
32636 function-transaction-block:
32637 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32639 __transaction_atomic txn-attribute[opt] function-try-block
32640 __transaction_relaxed ctor-initializer[opt] function-body
32641 __transaction_relaxed function-try-block
32645 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
32647 unsigned char old_in
= parser
->in_transaction
;
32648 unsigned char new_in
= 1;
32649 tree compound_stmt
, stmt
, attrs
;
32650 bool ctor_initializer_p
;
32653 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32654 || keyword
== RID_TRANSACTION_RELAXED
);
32655 token
= cp_parser_require_keyword (parser
, keyword
,
32656 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32657 : RT_TRANSACTION_RELAXED
));
32658 gcc_assert (token
!= NULL
);
32660 if (keyword
== RID_TRANSACTION_RELAXED
)
32661 new_in
|= TM_STMT_ATTR_RELAXED
;
32664 attrs
= cp_parser_txn_attribute_opt (parser
);
32666 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32669 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
32671 parser
->in_transaction
= new_in
;
32673 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
32674 ctor_initializer_p
= cp_parser_function_try_block (parser
);
32676 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
32677 (parser
, /*in_function_try_block=*/false);
32679 parser
->in_transaction
= old_in
;
32681 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
32683 return ctor_initializer_p
;
32686 /* Parse a __transaction_cancel statement.
32689 __transaction_cancel txn-attribute[opt] ;
32690 __transaction_cancel txn-attribute[opt] throw-expression ;
32692 ??? Cancel and throw is not yet implemented. */
32695 cp_parser_transaction_cancel (cp_parser
*parser
)
32698 bool is_outer
= false;
32701 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
32702 RT_TRANSACTION_CANCEL
);
32703 gcc_assert (token
!= NULL
);
32705 attrs
= cp_parser_txn_attribute_opt (parser
);
32707 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
32709 /* ??? Parse cancel-and-throw here. */
32711 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32715 error_at (token
->location
, "%<__transaction_cancel%> without "
32716 "transactional memory support enabled");
32717 return error_mark_node
;
32719 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
32721 error_at (token
->location
, "%<__transaction_cancel%> within a "
32722 "%<__transaction_relaxed%>");
32723 return error_mark_node
;
32727 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
32728 && !is_tm_may_cancel_outer (current_function_decl
))
32730 error_at (token
->location
, "outer %<__transaction_cancel%> not "
32731 "within outer %<__transaction_atomic%>");
32732 error_at (token
->location
,
32733 " or a %<transaction_may_cancel_outer%> function");
32734 return error_mark_node
;
32737 else if (parser
->in_transaction
== 0)
32739 error_at (token
->location
, "%<__transaction_cancel%> not within "
32740 "%<__transaction_atomic%>");
32741 return error_mark_node
;
32744 stmt
= build_tm_abort_call (token
->location
, is_outer
);
32752 static GTY (()) cp_parser
*the_parser
;
32755 /* Special handling for the first token or line in the file. The first
32756 thing in the file might be #pragma GCC pch_preprocess, which loads a
32757 PCH file, which is a GC collection point. So we need to handle this
32758 first pragma without benefit of an existing lexer structure.
32760 Always returns one token to the caller in *FIRST_TOKEN. This is
32761 either the true first token of the file, or the first token after
32762 the initial pragma. */
32765 cp_parser_initial_pragma (cp_token
*first_token
)
32769 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32770 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
32773 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32774 if (first_token
->type
== CPP_STRING
)
32776 name
= first_token
->u
.value
;
32778 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32779 if (first_token
->type
!= CPP_PRAGMA_EOL
)
32780 error_at (first_token
->location
,
32781 "junk at end of %<#pragma GCC pch_preprocess%>");
32784 error_at (first_token
->location
, "expected string literal");
32786 /* Skip to the end of the pragma. */
32787 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
32788 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32790 /* Now actually load the PCH file. */
32792 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
32794 /* Read one more token to return to our caller. We have to do this
32795 after reading the PCH file in, since its pointers have to be
32797 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32800 /* Parses the grainsize pragma for the _Cilk_for statement.
32802 #pragma cilk grainsize = <VALUE>. */
32805 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
32807 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32809 tree exp
= cp_parser_binary_expression (parser
, false, false,
32810 PREC_NOT_OPERATOR
, NULL
);
32811 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32812 if (!exp
|| exp
== error_mark_node
)
32814 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
32818 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32819 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32820 cp_parser_cilk_for (parser
, exp
);
32822 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
32823 "%<#pragma cilk grainsize%> is not followed by "
32827 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32830 /* Normal parsing of a pragma token. Here we can (and must) use the
32834 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
32836 cp_token
*pragma_tok
;
32839 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
32840 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
32841 parser
->lexer
->in_pragma
= true;
32843 id
= pragma_tok
->pragma_kind
;
32844 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
32845 cp_ensure_no_omp_declare_simd (parser
);
32848 case PRAGMA_GCC_PCH_PREPROCESS
:
32849 error_at (pragma_tok
->location
,
32850 "%<#pragma GCC pch_preprocess%> must be first");
32853 case PRAGMA_OMP_BARRIER
:
32856 case pragma_compound
:
32857 cp_parser_omp_barrier (parser
, pragma_tok
);
32860 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
32861 "used in compound statements");
32868 case PRAGMA_OMP_FLUSH
:
32871 case pragma_compound
:
32872 cp_parser_omp_flush (parser
, pragma_tok
);
32875 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
32876 "used in compound statements");
32883 case PRAGMA_OMP_TASKWAIT
:
32886 case pragma_compound
:
32887 cp_parser_omp_taskwait (parser
, pragma_tok
);
32890 error_at (pragma_tok
->location
,
32891 "%<#pragma omp taskwait%> may only be "
32892 "used in compound statements");
32899 case PRAGMA_OMP_TASKYIELD
:
32902 case pragma_compound
:
32903 cp_parser_omp_taskyield (parser
, pragma_tok
);
32906 error_at (pragma_tok
->location
,
32907 "%<#pragma omp taskyield%> may only be "
32908 "used in compound statements");
32915 case PRAGMA_OMP_CANCEL
:
32918 case pragma_compound
:
32919 cp_parser_omp_cancel (parser
, pragma_tok
);
32922 error_at (pragma_tok
->location
,
32923 "%<#pragma omp cancel%> may only be "
32924 "used in compound statements");
32931 case PRAGMA_OMP_CANCELLATION_POINT
:
32934 case pragma_compound
:
32935 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
32938 error_at (pragma_tok
->location
,
32939 "%<#pragma omp cancellation point%> may only be "
32940 "used in compound statements");
32947 case PRAGMA_OMP_THREADPRIVATE
:
32948 cp_parser_omp_threadprivate (parser
, pragma_tok
);
32951 case PRAGMA_OMP_DECLARE_REDUCTION
:
32952 cp_parser_omp_declare (parser
, pragma_tok
, context
);
32955 case PRAGMA_OACC_CACHE
:
32956 case PRAGMA_OACC_DATA
:
32957 case PRAGMA_OACC_ENTER_DATA
:
32958 case PRAGMA_OACC_EXIT_DATA
:
32959 case PRAGMA_OACC_KERNELS
:
32960 case PRAGMA_OACC_PARALLEL
:
32961 case PRAGMA_OACC_LOOP
:
32962 case PRAGMA_OACC_UPDATE
:
32963 case PRAGMA_OACC_WAIT
:
32964 case PRAGMA_OMP_ATOMIC
:
32965 case PRAGMA_OMP_CRITICAL
:
32966 case PRAGMA_OMP_DISTRIBUTE
:
32967 case PRAGMA_OMP_FOR
:
32968 case PRAGMA_OMP_MASTER
:
32969 case PRAGMA_OMP_ORDERED
:
32970 case PRAGMA_OMP_PARALLEL
:
32971 case PRAGMA_OMP_SECTIONS
:
32972 case PRAGMA_OMP_SIMD
:
32973 case PRAGMA_OMP_SINGLE
:
32974 case PRAGMA_OMP_TASK
:
32975 case PRAGMA_OMP_TASKGROUP
:
32976 case PRAGMA_OMP_TEAMS
:
32977 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
32979 cp_parser_omp_construct (parser
, pragma_tok
);
32982 case PRAGMA_OMP_TARGET
:
32983 return cp_parser_omp_target (parser
, pragma_tok
, context
);
32985 case PRAGMA_OMP_END_DECLARE_TARGET
:
32986 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
32989 case PRAGMA_OMP_SECTION
:
32990 error_at (pragma_tok
->location
,
32991 "%<#pragma omp section%> may only be used in "
32992 "%<#pragma omp sections%> construct");
32997 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32999 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33000 if (tok
->type
!= CPP_KEYWORD
33001 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
33002 && tok
->keyword
!= RID_DO
))
33004 cp_parser_error (parser
, "for, while or do statement expected");
33007 cp_parser_iteration_statement (parser
, true);
33011 case PRAGMA_CILK_SIMD
:
33012 if (context
== pragma_external
)
33014 error_at (pragma_tok
->location
,
33015 "%<#pragma simd%> must be inside a function");
33018 cp_parser_cilk_simd (parser
, pragma_tok
);
33021 case PRAGMA_CILK_GRAINSIZE
:
33022 if (context
== pragma_external
)
33024 error_at (pragma_tok
->location
,
33025 "%<#pragma cilk grainsize%> must be inside a function");
33029 /* Ignore the pragma if Cilk Plus is not enabled. */
33032 cp_parser_cilk_grainsize (parser
, pragma_tok
);
33037 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
33038 "%<#pragma cilk grainsize%>");
33043 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
33044 c_invoke_pragma_handler (id
);
33048 cp_parser_error (parser
, "expected declaration specifiers");
33052 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33056 /* The interface the pragma parsers have to the lexer. */
33059 pragma_lex (tree
*value
)
33062 enum cpp_ttype ret
;
33064 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33067 *value
= tok
->u
.value
;
33069 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
33071 else if (ret
== CPP_STRING
)
33072 *value
= cp_parser_string_literal (the_parser
, false, false);
33075 cp_lexer_consume_token (the_parser
->lexer
);
33076 if (ret
== CPP_KEYWORD
)
33084 /* External interface. */
33086 /* Parse one entire translation unit. */
33089 c_parse_file (void)
33091 static bool already_called
= false;
33093 if (already_called
)
33094 fatal_error ("inter-module optimizations not implemented for C++");
33095 already_called
= true;
33097 the_parser
= cp_parser_new ();
33098 push_deferring_access_checks (flag_access_control
33099 ? dk_no_deferred
: dk_no_check
);
33100 cp_parser_translation_unit (the_parser
);
33104 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33105 vectorlength clause:
33107 vectorlength ( constant-expression ) */
33110 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
33113 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33115 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33116 safelen clause. Thus, vectorlength is represented as OMP 4.0
33117 safelen. For SIMD-enabled function it is represented by OMP 4.0
33120 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
33123 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
33126 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33127 return error_mark_node
;
33129 expr
= cp_parser_constant_expression (parser
);
33130 expr
= maybe_constant_value (expr
);
33132 /* If expr == error_mark_node, then don't emit any errors nor
33133 create a clause. if any of the above functions returns
33134 error mark node then they would have emitted an error message. */
33135 if (expr
== error_mark_node
)
33137 else if (!TREE_TYPE (expr
)
33138 || !TREE_CONSTANT (expr
)
33139 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
33140 error_at (loc
, "vectorlength must be an integer constant");
33141 else if (TREE_CONSTANT (expr
)
33142 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
33143 error_at (loc
, "vectorlength must be a power of 2");
33149 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
33150 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
33151 OMP_CLAUSE_CHAIN (c
) = clauses
;
33156 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
33157 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
33158 OMP_CLAUSE_CHAIN (c
) = clauses
;
33163 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33164 return error_mark_node
;
33168 /* Handles the Cilk Plus #pragma simd linear clause.
33170 linear ( simd-linear-variable-list )
33172 simd-linear-variable-list:
33173 simd-linear-variable
33174 simd-linear-variable-list , simd-linear-variable
33176 simd-linear-variable:
33178 id-expression : simd-linear-step
33181 conditional-expression */
33184 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
33186 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33188 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33190 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33192 cp_parser_error (parser
, "expected identifier");
33193 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33194 return error_mark_node
;
33197 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33198 parser
->colon_corrects_to_scope_p
= false;
33201 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33202 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33204 cp_parser_error (parser
, "expected variable-name");
33205 clauses
= error_mark_node
;
33209 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
33211 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
33213 if (decl
== error_mark_node
)
33215 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
33217 clauses
= error_mark_node
;
33221 tree e
= NULL_TREE
;
33222 tree step_size
= integer_one_node
;
33224 /* If present, parse the linear step. Otherwise, assume the default
33226 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
33228 cp_lexer_consume_token (parser
->lexer
);
33230 e
= cp_parser_assignment_expression (parser
);
33231 e
= maybe_constant_value (e
);
33233 if (e
== error_mark_node
)
33235 /* If an error has occurred, then the whole pragma is
33236 considered ill-formed. Thus, no reason to keep
33238 clauses
= error_mark_node
;
33241 else if (type_dependent_expression_p (e
)
33242 || value_dependent_expression_p (e
)
33244 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
33245 && (TREE_CONSTANT (e
)
33249 cp_parser_error (parser
,
33250 "step size must be an integer constant "
33251 "expression or an integer variable");
33254 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33255 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33256 OMP_CLAUSE_DECL (l
) = decl
;
33257 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
33258 OMP_CLAUSE_CHAIN (l
) = clauses
;
33261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33262 cp_lexer_consume_token (parser
->lexer
);
33263 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33267 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33268 "expected %<,%> or %<)%> after %qE", decl
);
33269 clauses
= error_mark_node
;
33273 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33274 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33278 /* Returns the name of the next clause. If the clause is not
33279 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33280 token is not consumed. Otherwise, the appropriate enum from the
33281 pragma_simd_clause is returned and the token is consumed. */
33283 static pragma_omp_clause
33284 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
33286 pragma_omp_clause clause_type
;
33287 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33289 if (token
->keyword
== RID_PRIVATE
)
33290 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
33291 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
33292 return PRAGMA_CILK_CLAUSE_NONE
;
33293 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
33294 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
33295 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
33296 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
33297 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
33298 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
33299 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
33300 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
33301 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
33302 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
33304 return PRAGMA_CILK_CLAUSE_NONE
;
33306 cp_lexer_consume_token (parser
->lexer
);
33307 return clause_type
;
33310 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33313 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
33315 tree clauses
= NULL_TREE
;
33317 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
33318 && clauses
!= error_mark_node
)
33320 pragma_omp_clause c_kind
;
33321 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
33322 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
33323 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
33324 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
33325 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
33326 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
33327 /* Use the OpenMP 4.0 equivalent function. */
33328 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
33329 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
33330 /* Use the OpenMP 4.0 equivalent function. */
33331 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33333 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
33334 /* Use the OMP 4.0 equivalent function. */
33335 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33337 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
33338 /* Use the OMP 4.0 equivalent function. */
33339 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33342 clauses
= error_mark_node
;
33343 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
33348 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
33350 if (clauses
== error_mark_node
)
33351 return error_mark_node
;
33353 return c_finish_cilk_clauses (clauses
);
33356 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33359 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
33361 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
33363 if (clauses
== error_mark_node
)
33366 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
33368 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33369 "for statement expected");
33373 tree sb
= begin_omp_structured_block ();
33374 int save
= cp_parser_begin_omp_structured_block (parser
);
33375 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
33377 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
33378 cp_parser_end_omp_structured_block (parser
, save
);
33379 add_stmt (finish_omp_structured_block (sb
));
33382 /* Main entry-point for parsing Cilk Plus _Cilk_for
33383 loops. The return value is error_mark_node
33384 when errors happen and CILK_FOR tree on success. */
33387 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
33389 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
33390 gcc_unreachable ();
33392 tree sb
= begin_omp_structured_block ();
33393 int save
= cp_parser_begin_omp_structured_block (parser
);
33395 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
33396 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
33397 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
33398 clauses
= finish_omp_clauses (clauses
);
33400 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
33402 cpp_validate_cilk_plus_loop (ret
);
33404 ret
= error_mark_node
;
33406 cp_parser_end_omp_structured_block (parser
, save
);
33407 add_stmt (finish_omp_structured_block (sb
));
33411 /* Create an identifier for a generic parameter type (a synthesized
33412 template parameter implied by `auto' or a concept identifier). */
33414 static GTY(()) int generic_parm_count
;
33416 make_generic_type_name ()
33419 sprintf (buf
, "auto:%d", ++generic_parm_count
);
33420 return get_identifier (buf
);
33423 /* Predicate that behaves as is_auto_or_concept but matches the parent
33424 node of the generic type rather than the generic type itself. This
33425 allows for type transformation in add_implicit_template_parms. */
33428 tree_type_is_auto_or_concept (const_tree t
)
33430 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
33433 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33434 (creating a new template parameter list if necessary). Returns the newly
33435 created template type parm. */
33438 synthesize_implicit_template_parm (cp_parser
*parser
)
33440 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
33442 /* We are either continuing a function template that already contains implicit
33443 template parameters, creating a new fully-implicit function template, or
33444 extending an existing explicit function template with implicit template
33447 cp_binding_level
*const entry_scope
= current_binding_level
;
33449 bool become_template
= false;
33450 cp_binding_level
*parent_scope
= 0;
33452 if (parser
->implicit_template_scope
)
33454 gcc_assert (parser
->implicit_template_parms
);
33456 current_binding_level
= parser
->implicit_template_scope
;
33460 /* Roll back to the existing template parameter scope (in the case of
33461 extending an explicit function template) or introduce a new template
33462 parameter scope ahead of the function parameter scope (or class scope
33463 in the case of out-of-line member definitions). The function scope is
33464 added back after template parameter synthesis below. */
33466 cp_binding_level
*scope
= entry_scope
;
33468 while (scope
->kind
== sk_function_parms
)
33470 parent_scope
= scope
;
33471 scope
= scope
->level_chain
;
33473 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
33475 /* If not defining a class, then any class scope is a scope level in
33476 an out-of-line member definition. In this case simply wind back
33477 beyond the first such scope to inject the template parameter list.
33478 Otherwise wind back to the class being defined. The latter can
33479 occur in class member friend declarations such as:
33485 friend void A::foo (auto);
33488 The template parameter list synthesized for the friend declaration
33489 must be injected in the scope of 'B'. This can also occur in
33490 erroneous cases such as:
33496 void B::foo (auto) {}
33499 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33500 but, nevertheless, the template parameter list synthesized for the
33501 declarator should be injected into the scope of 'A' as if the
33502 ill-formed template was specified explicitly. */
33504 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
33506 parent_scope
= scope
;
33507 scope
= scope
->level_chain
;
33511 current_binding_level
= scope
;
33513 if (scope
->kind
!= sk_template_parms
33514 || !function_being_declared_is_template_p (parser
))
33516 /* Introduce a new template parameter list for implicit template
33519 become_template
= true;
33521 parser
->implicit_template_scope
33522 = begin_scope (sk_template_parms
, NULL
);
33524 ++processing_template_decl
;
33526 parser
->fully_implicit_function_template_p
= true;
33527 ++parser
->num_template_parameter_lists
;
33531 /* Synthesize implicit template parameters at the end of the explicit
33532 template parameter list. */
33534 gcc_assert (current_template_parms
);
33536 parser
->implicit_template_scope
= scope
;
33538 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33539 parser
->implicit_template_parms
33540 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
33544 /* Synthesize a new template parameter and track the current template
33545 parameter chain with implicit_template_parms. */
33547 tree synth_id
= make_generic_type_name ();
33548 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
33551 = process_template_parm (parser
->implicit_template_parms
,
33553 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
33554 /*non_type=*/false,
33555 /*param_pack=*/false);
33558 if (parser
->implicit_template_parms
)
33559 parser
->implicit_template_parms
33560 = TREE_CHAIN (parser
->implicit_template_parms
);
33562 parser
->implicit_template_parms
= new_parm
;
33564 tree new_type
= TREE_TYPE (getdecls ());
33566 /* If creating a fully implicit function template, start the new implicit
33567 template parameter list with this synthesized type, otherwise grow the
33568 current template parameter list. */
33570 if (become_template
)
33572 parent_scope
->level_chain
= current_binding_level
;
33574 tree new_parms
= make_tree_vec (1);
33575 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
33576 current_template_parms
= tree_cons (size_int (processing_template_decl
),
33577 new_parms
, current_template_parms
);
33581 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33582 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
33583 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
33584 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
33587 current_binding_level
= entry_scope
;
33592 /* Finish the declaration of a fully implicit function template. Such a
33593 template has no explicit template parameter list so has not been through the
33594 normal template head and tail processing. synthesize_implicit_template_parm
33595 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33596 provided if the declaration is a class member such that its template
33597 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33598 form is returned. Otherwise NULL_TREE is returned. */
33601 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
33603 gcc_assert (parser
->fully_implicit_function_template_p
);
33605 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
33606 && DECL_VIRTUAL_P (member_decl_opt
))
33608 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
33609 "implicit templates may not be %<virtual%>");
33610 DECL_VIRTUAL_P (member_decl_opt
) = false;
33613 if (member_decl_opt
)
33614 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
33615 end_template_decl ();
33617 parser
->fully_implicit_function_template_p
= false;
33618 --parser
->num_template_parameter_lists
;
33620 return member_decl_opt
;
33623 #include "gt-cp-parser.h"