2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "print-tree.h"
30 #include "stringpool.h"
32 #include "trans-mem.h"
35 #include "c-family/c-pragma.h"
38 #include "diagnostic-core.h"
40 #include "hard-reg-set.h"
43 #include "c-family/c-common.h"
44 #include "c-family/c-objc.h"
46 #include "tree-pretty-print.h"
48 #include "type-utils.h"
50 #include "gomp-constants.h"
55 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
56 and c-lex.c) and the C++ parser. */
58 static cp_token eof_token
=
60 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
63 /* The various kinds of non integral constant we encounter. */
64 typedef enum non_integral_constant
{
66 /* floating-point literal */
70 /* %<__FUNCTION__%> */
72 /* %<__PRETTY_FUNCTION__%> */
80 /* %<typeid%> operator */
82 /* non-constant compound literals */
90 /* an array reference */
96 /* the address of a label */
110 /* calls to overloaded operators */
114 /* a comma operator */
116 /* a call to a constructor */
118 /* a transaction expression */
120 } non_integral_constant
;
122 /* The various kinds of errors about name-lookup failing. */
123 typedef enum name_lookup_error
{
128 /* is not a class or namespace */
130 /* is not a class, namespace, or enumeration */
134 /* The various kinds of required token */
135 typedef enum required_token
{
137 RT_SEMICOLON
, /* ';' */
138 RT_OPEN_PAREN
, /* '(' */
139 RT_CLOSE_BRACE
, /* '}' */
140 RT_OPEN_BRACE
, /* '{' */
141 RT_CLOSE_SQUARE
, /* ']' */
142 RT_OPEN_SQUARE
, /* '[' */
146 RT_GREATER
, /* '>' */
148 RT_ELLIPSIS
, /* '...' */
152 RT_COLON_SCOPE
, /* ':' or '::' */
153 RT_CLOSE_PAREN
, /* ')' */
154 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
155 RT_PRAGMA_EOL
, /* end of line */
156 RT_NAME
, /* identifier */
158 /* The type is CPP_KEYWORD */
160 RT_DELETE
, /* delete */
161 RT_RETURN
, /* return */
162 RT_WHILE
, /* while */
163 RT_EXTERN
, /* extern */
164 RT_STATIC_ASSERT
, /* static_assert */
165 RT_DECLTYPE
, /* decltype */
166 RT_OPERATOR
, /* operator */
167 RT_CLASS
, /* class */
168 RT_TEMPLATE
, /* template */
169 RT_NAMESPACE
, /* namespace */
170 RT_USING
, /* using */
173 RT_CATCH
, /* catch */
174 RT_THROW
, /* throw */
175 RT_LABEL
, /* __label__ */
176 RT_AT_TRY
, /* @try */
177 RT_AT_SYNCHRONIZED
, /* @synchronized */
178 RT_AT_THROW
, /* @throw */
180 RT_SELECT
, /* selection-statement */
181 RT_INTERATION
, /* iteration-statement */
182 RT_JUMP
, /* jump-statement */
183 RT_CLASS_KEY
, /* class-key */
184 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
185 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
186 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
187 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
192 static cp_lexer
*cp_lexer_new_main
194 static cp_lexer
*cp_lexer_new_from_tokens
195 (cp_token_cache
*tokens
);
196 static void cp_lexer_destroy
198 static int cp_lexer_saving_tokens
200 static cp_token
*cp_lexer_token_at
201 (cp_lexer
*, cp_token_position
);
202 static void cp_lexer_get_preprocessor_token
203 (cp_lexer
*, cp_token
*);
204 static inline cp_token
*cp_lexer_peek_token
206 static cp_token
*cp_lexer_peek_nth_token
207 (cp_lexer
*, size_t);
208 static inline bool cp_lexer_next_token_is
209 (cp_lexer
*, enum cpp_ttype
);
210 static bool cp_lexer_next_token_is_not
211 (cp_lexer
*, enum cpp_ttype
);
212 static bool cp_lexer_next_token_is_keyword
213 (cp_lexer
*, enum rid
);
214 static cp_token
*cp_lexer_consume_token
216 static void cp_lexer_purge_token
218 static void cp_lexer_purge_tokens_after
219 (cp_lexer
*, cp_token_position
);
220 static void cp_lexer_save_tokens
222 static void cp_lexer_commit_tokens
224 static void cp_lexer_rollback_tokens
226 static void cp_lexer_print_token
227 (FILE *, cp_token
*);
228 static inline bool cp_lexer_debugging_p
230 static void cp_lexer_start_debugging
231 (cp_lexer
*) ATTRIBUTE_UNUSED
;
232 static void cp_lexer_stop_debugging
233 (cp_lexer
*) ATTRIBUTE_UNUSED
;
235 static cp_token_cache
*cp_token_cache_new
236 (cp_token
*, cp_token
*);
238 static void cp_parser_initial_pragma
241 static tree cp_literal_operator_id
244 static void cp_parser_cilk_simd
245 (cp_parser
*, cp_token
*);
246 static tree cp_parser_cilk_for
248 static bool cp_parser_omp_declare_reduction_exprs
250 static tree cp_parser_cilk_simd_vectorlength
251 (cp_parser
*, tree
, bool);
253 /* Manifest constants. */
254 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
255 #define CP_SAVED_TOKEN_STACK 5
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream
;
262 /* Nonzero if we are parsing an unevaluated operand: an operand to
263 sizeof, typeof, or alignof. */
264 int cp_unevaluated_operand
;
266 /* Dump up to NUM tokens in BUFFER to FILE starting with token
267 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
268 first token in BUFFER. If NUM is 0, dump all the tokens. If
269 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
270 highlighted by surrounding it in [[ ]]. */
273 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
274 cp_token
*start_token
, unsigned num
,
275 cp_token
*curr_token
)
277 unsigned i
, nprinted
;
281 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
287 num
= buffer
->length ();
289 if (start_token
== NULL
)
290 start_token
= buffer
->address ();
292 if (start_token
> buffer
->address ())
294 cp_lexer_print_token (file
, &(*buffer
)[0]);
295 fprintf (file
, " ... ");
300 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
302 if (token
== start_token
)
309 if (token
== curr_token
)
310 fprintf (file
, "[[");
312 cp_lexer_print_token (file
, token
);
314 if (token
== curr_token
)
315 fprintf (file
, "]]");
321 case CPP_CLOSE_BRACE
:
331 if (i
== num
&& i
< buffer
->length ())
333 fprintf (file
, " ... ");
334 cp_lexer_print_token (file
, &buffer
->last ());
337 fprintf (file
, "\n");
341 /* Dump all tokens in BUFFER to stderr. */
344 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
346 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
350 debug (vec
<cp_token
, va_gc
> &ref
)
352 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> *ptr
)
361 fprintf (stderr
, "<nil>\n");
365 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
366 description for T. */
369 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
373 fprintf (file
, "%s: ", desc
);
374 print_node_brief (file
, "", t
, 0);
379 /* Dump parser context C to FILE. */
382 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
384 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
385 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
386 print_node_brief (file
, "", c
->object_type
, 0);
387 fprintf (file
, "}\n");
391 /* Print the stack of parsing contexts to FILE starting with FIRST. */
394 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
397 cp_parser_context
*c
;
399 fprintf (file
, "Parsing context stack:\n");
400 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
402 fprintf (file
, "\t#%u: ", i
);
403 cp_debug_print_context (file
, c
);
408 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
411 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
414 fprintf (file
, "%s: true\n", desc
);
418 /* Print an unparsed function entry UF to FILE. */
421 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
424 cp_default_arg_entry
*default_arg_fn
;
427 fprintf (file
, "\tFunctions with default args:\n");
429 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
432 fprintf (file
, "\t\tClass type: ");
433 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
434 fprintf (file
, "\t\tDeclaration: ");
435 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
436 fprintf (file
, "\n");
439 fprintf (file
, "\n\tFunctions with definitions that require "
440 "post-processing\n\t\t");
441 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
443 print_node_brief (file
, "", fn
, 0);
446 fprintf (file
, "\n");
448 fprintf (file
, "\n\tNon-static data members with initializers that require "
449 "post-processing\n\t\t");
450 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
452 print_node_brief (file
, "", fn
, 0);
455 fprintf (file
, "\n");
459 /* Print the stack of unparsed member functions S to FILE. */
462 cp_debug_print_unparsed_queues (FILE *file
,
463 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
466 cp_unparsed_functions_entry
*uf
;
468 fprintf (file
, "Unparsed functions\n");
469 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
471 fprintf (file
, "#%u:\n", i
);
472 cp_debug_print_unparsed_function (file
, uf
);
477 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
478 the given PARSER. If FILE is NULL, the output is printed on stderr. */
481 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
483 cp_token
*next_token
, *first_token
, *start_token
;
488 next_token
= parser
->lexer
->next_token
;
489 first_token
= parser
->lexer
->buffer
->address ();
490 start_token
= (next_token
> first_token
+ window_size
/ 2)
491 ? next_token
- window_size
/ 2
493 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
498 /* Dump debugging information for the given PARSER. If FILE is NULL,
499 the output is printed on stderr. */
502 cp_debug_parser (FILE *file
, cp_parser
*parser
)
504 const size_t window_size
= 20;
506 expanded_location eloc
;
511 fprintf (file
, "Parser state\n\n");
512 fprintf (file
, "Number of tokens: %u\n",
513 vec_safe_length (parser
->lexer
->buffer
));
514 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
515 cp_debug_print_tree_if_set (file
, "Object scope",
516 parser
->object_scope
);
517 cp_debug_print_tree_if_set (file
, "Qualifying scope",
518 parser
->qualifying_scope
);
519 cp_debug_print_context_stack (file
, parser
->context
);
520 cp_debug_print_flag (file
, "Allow GNU extensions",
521 parser
->allow_gnu_extensions_p
);
522 cp_debug_print_flag (file
, "'>' token is greater-than",
523 parser
->greater_than_is_operator_p
);
524 cp_debug_print_flag (file
, "Default args allowed in current "
525 "parameter list", parser
->default_arg_ok_p
);
526 cp_debug_print_flag (file
, "Parsing integral constant-expression",
527 parser
->integral_constant_expression_p
);
528 cp_debug_print_flag (file
, "Allow non-constant expression in current "
529 "constant-expression",
530 parser
->allow_non_integral_constant_expression_p
);
531 cp_debug_print_flag (file
, "Seen non-constant expression",
532 parser
->non_integral_constant_expression_p
);
533 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
535 parser
->local_variables_forbidden_p
);
536 cp_debug_print_flag (file
, "In unbraced linkage specification",
537 parser
->in_unbraced_linkage_specification_p
);
538 cp_debug_print_flag (file
, "Parsing a declarator",
539 parser
->in_declarator_p
);
540 cp_debug_print_flag (file
, "In template argument list",
541 parser
->in_template_argument_list_p
);
542 cp_debug_print_flag (file
, "Parsing an iteration statement",
543 parser
->in_statement
& IN_ITERATION_STMT
);
544 cp_debug_print_flag (file
, "Parsing a switch statement",
545 parser
->in_statement
& IN_SWITCH_STMT
);
546 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
547 parser
->in_statement
& IN_OMP_BLOCK
);
548 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
549 parser
->in_statement
& IN_CILK_SIMD_FOR
);
550 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
551 parser
->in_statement
& IN_OMP_FOR
);
552 cp_debug_print_flag (file
, "Parsing an if statement",
553 parser
->in_statement
& IN_IF_STMT
);
554 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
555 "context", parser
->in_type_id_in_expr_p
);
556 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
557 parser
->implicit_extern_c
);
558 cp_debug_print_flag (file
, "String expressions should be translated "
559 "to execution character set",
560 parser
->translate_strings_p
);
561 cp_debug_print_flag (file
, "Parsing function body outside of a "
562 "local class", parser
->in_function_body
);
563 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
564 parser
->colon_corrects_to_scope_p
);
565 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
566 parser
->colon_doesnt_start_class_def_p
);
567 if (parser
->type_definition_forbidden_message
)
568 fprintf (file
, "Error message for forbidden type definitions: %s\n",
569 parser
->type_definition_forbidden_message
);
570 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
571 fprintf (file
, "Number of class definitions in progress: %u\n",
572 parser
->num_classes_being_defined
);
573 fprintf (file
, "Number of template parameter lists for the current "
574 "declaration: %u\n", parser
->num_template_parameter_lists
);
575 cp_debug_parser_tokens (file
, parser
, window_size
);
576 token
= parser
->lexer
->next_token
;
577 fprintf (file
, "Next token to parse:\n");
578 fprintf (file
, "\tToken: ");
579 cp_lexer_print_token (file
, token
);
580 eloc
= expand_location (token
->location
);
581 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
582 fprintf (file
, "\tLine: %d\n", eloc
.line
);
583 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
587 debug (cp_parser
&ref
)
589 cp_debug_parser (stderr
, &ref
);
593 debug (cp_parser
*ptr
)
598 fprintf (stderr
, "<nil>\n");
601 /* Allocate memory for a new lexer object and return it. */
604 cp_lexer_alloc (void)
608 c_common_no_more_pch ();
610 /* Allocate the memory. */
611 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
613 /* Initially we are not debugging. */
614 lexer
->debugging_p
= false;
616 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
618 /* Create the buffer. */
619 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
625 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 cp_lexer_new_main (void)
634 /* It's possible that parsing the first pragma will load a PCH file,
635 which is a GC collection point. So we have to do that before
636 allocating any memory. */
637 cp_parser_initial_pragma (&token
);
639 lexer
= cp_lexer_alloc ();
641 /* Put the first token in the buffer. */
642 lexer
->buffer
->quick_push (token
);
644 /* Get the remaining tokens from the preprocessor. */
645 while (token
.type
!= CPP_EOF
)
647 cp_lexer_get_preprocessor_token (lexer
, &token
);
648 vec_safe_push (lexer
->buffer
, token
);
651 lexer
->last_token
= lexer
->buffer
->address ()
652 + lexer
->buffer
->length ()
654 lexer
->next_token
= lexer
->buffer
->length ()
655 ? lexer
->buffer
->address ()
658 /* Subsequent preprocessor diagnostics should use compiler
659 diagnostic functions to get the compiler source location. */
662 gcc_assert (!lexer
->next_token
->purged_p
);
666 /* Create a new lexer whose token stream is primed with the tokens in
667 CACHE. When these tokens are exhausted, no new tokens will be read. */
670 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
672 cp_token
*first
= cache
->first
;
673 cp_token
*last
= cache
->last
;
674 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
676 /* We do not own the buffer. */
677 lexer
->buffer
= NULL
;
678 lexer
->next_token
= first
== last
? &eof_token
: first
;
679 lexer
->last_token
= last
;
681 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
683 /* Initially we are not debugging. */
684 lexer
->debugging_p
= false;
686 gcc_assert (!lexer
->next_token
->purged_p
);
690 /* Frees all resources associated with LEXER. */
693 cp_lexer_destroy (cp_lexer
*lexer
)
695 vec_free (lexer
->buffer
);
696 lexer
->saved_tokens
.release ();
700 /* Returns nonzero if debugging information should be output. */
703 cp_lexer_debugging_p (cp_lexer
*lexer
)
705 return lexer
->debugging_p
;
709 static inline cp_token_position
710 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
712 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
714 return lexer
->next_token
- previous_p
;
717 static inline cp_token
*
718 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
724 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
726 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
729 static inline cp_token_position
730 cp_lexer_previous_token_position (cp_lexer
*lexer
)
732 if (lexer
->next_token
== &eof_token
)
733 return lexer
->last_token
- 1;
735 return cp_lexer_token_position (lexer
, true);
738 static inline cp_token
*
739 cp_lexer_previous_token (cp_lexer
*lexer
)
741 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
743 return cp_lexer_token_at (lexer
, tp
);
746 /* nonzero if we are presently saving tokens. */
749 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
751 return lexer
->saved_tokens
.length () != 0;
754 /* Store the next token from the preprocessor in *TOKEN. Return true
755 if we reach EOF. If LEXER is NULL, assume we are handling an
756 initial #pragma pch_preprocess, and thus want the lexer to return
757 processed strings. */
760 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
762 static int is_extern_c
= 0;
764 /* Get a new token from the preprocessor. */
766 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
767 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
768 token
->keyword
= RID_MAX
;
769 token
->pragma_kind
= PRAGMA_NONE
;
770 token
->purged_p
= false;
771 token
->error_reported
= false;
773 /* On some systems, some header files are surrounded by an
774 implicit extern "C" block. Set a flag in the token if it
775 comes from such a header. */
776 is_extern_c
+= pending_lang_change
;
777 pending_lang_change
= 0;
778 token
->implicit_extern_c
= is_extern_c
> 0;
780 /* Check to see if this token is a keyword. */
781 if (token
->type
== CPP_NAME
)
783 if (C_IS_RESERVED_WORD (token
->u
.value
))
785 /* Mark this token as a keyword. */
786 token
->type
= CPP_KEYWORD
;
787 /* Record which keyword. */
788 token
->keyword
= C_RID_CODE (token
->u
.value
);
792 if (warn_cxx11_compat
793 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
794 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
796 /* Warn about the C++0x keyword (but still treat it as
798 warning (OPT_Wc__11_compat
,
799 "identifier %qE is a keyword in C++11",
802 /* Clear out the C_RID_CODE so we don't warn about this
803 particular identifier-turned-keyword again. */
804 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
807 token
->keyword
= RID_MAX
;
810 else if (token
->type
== CPP_AT_NAME
)
812 /* This only happens in Objective-C++; it must be a keyword. */
813 token
->type
= CPP_KEYWORD
;
814 switch (C_RID_CODE (token
->u
.value
))
816 /* Replace 'class' with '@class', 'private' with '@private',
817 etc. This prevents confusion with the C++ keyword
818 'class', and makes the tokens consistent with other
819 Objective-C 'AT' keywords. For example '@class' is
820 reported as RID_AT_CLASS which is consistent with
821 '@synchronized', which is reported as
824 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
825 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
826 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
827 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
828 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
829 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
830 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
831 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
834 else if (token
->type
== CPP_PRAGMA
)
836 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
837 token
->pragma_kind
= ((enum pragma_kind
)
838 TREE_INT_CST_LOW (token
->u
.value
));
839 token
->u
.value
= NULL_TREE
;
843 /* Update the globals input_location and the input file stack from TOKEN. */
845 cp_lexer_set_source_position_from_token (cp_token
*token
)
847 if (token
->type
!= CPP_EOF
)
849 input_location
= token
->location
;
853 /* Update the globals input_location and the input file stack from LEXER. */
855 cp_lexer_set_source_position (cp_lexer
*lexer
)
857 cp_token
*token
= cp_lexer_peek_token (lexer
);
858 cp_lexer_set_source_position_from_token (token
);
861 /* Return a pointer to the next token in the token stream, but do not
864 static inline cp_token
*
865 cp_lexer_peek_token (cp_lexer
*lexer
)
867 if (cp_lexer_debugging_p (lexer
))
869 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
870 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
871 putc ('\n', cp_lexer_debug_stream
);
873 return lexer
->next_token
;
876 /* Return true if the next token has the indicated TYPE. */
879 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
881 return cp_lexer_peek_token (lexer
)->type
== type
;
884 /* Return true if the next token does not have the indicated TYPE. */
887 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
889 return !cp_lexer_next_token_is (lexer
, type
);
892 /* Return true if the next token is the indicated KEYWORD. */
895 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
897 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
901 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
903 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
907 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
909 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
912 /* Return true if the next token is not the indicated KEYWORD. */
915 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
917 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
920 /* Return true if the next token is a keyword for a decl-specifier. */
923 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
927 token
= cp_lexer_peek_token (lexer
);
928 switch (token
->keyword
)
930 /* auto specifier: storage-class-specifier in C++,
931 simple-type-specifier in C++0x. */
933 /* Storage classes. */
939 /* Elaborated type specifiers. */
945 /* Simple type specifiers. */
959 /* GNU extensions. */
962 /* C++0x extensions. */
964 case RID_UNDERLYING_TYPE
:
968 if (token
->keyword
>= RID_FIRST_INT_N
969 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
970 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
976 /* Returns TRUE iff the token T begins a decltype type. */
979 token_is_decltype (cp_token
*t
)
981 return (t
->keyword
== RID_DECLTYPE
982 || t
->type
== CPP_DECLTYPE
);
985 /* Returns TRUE iff the next token begins a decltype type. */
988 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
990 cp_token
*t
= cp_lexer_peek_token (lexer
);
991 return token_is_decltype (t
);
994 /* Return a pointer to the Nth token in the token stream. If N is 1,
995 then this is precisely equivalent to cp_lexer_peek_token (except
996 that it is not inline). One would like to disallow that case, but
997 there is one case (cp_parser_nth_token_starts_template_id) where
998 the caller passes a variable for N and it might be 1. */
1001 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1005 /* N is 1-based, not zero-based. */
1008 if (cp_lexer_debugging_p (lexer
))
1009 fprintf (cp_lexer_debug_stream
,
1010 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1013 token
= lexer
->next_token
;
1014 gcc_assert (!n
|| token
!= &eof_token
);
1018 if (token
== lexer
->last_token
)
1024 if (!token
->purged_p
)
1028 if (cp_lexer_debugging_p (lexer
))
1030 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1031 putc ('\n', cp_lexer_debug_stream
);
1037 /* Return the next token, and advance the lexer's next_token pointer
1038 to point to the next non-purged token. */
1041 cp_lexer_consume_token (cp_lexer
* lexer
)
1043 cp_token
*token
= lexer
->next_token
;
1045 gcc_assert (token
!= &eof_token
);
1046 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1050 lexer
->next_token
++;
1051 if (lexer
->next_token
== lexer
->last_token
)
1053 lexer
->next_token
= &eof_token
;
1058 while (lexer
->next_token
->purged_p
);
1060 cp_lexer_set_source_position_from_token (token
);
1062 /* Provide debugging output. */
1063 if (cp_lexer_debugging_p (lexer
))
1065 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1066 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1067 putc ('\n', cp_lexer_debug_stream
);
1073 /* Permanently remove the next token from the token stream, and
1074 advance the next_token pointer to refer to the next non-purged
1078 cp_lexer_purge_token (cp_lexer
*lexer
)
1080 cp_token
*tok
= lexer
->next_token
;
1082 gcc_assert (tok
!= &eof_token
);
1083 tok
->purged_p
= true;
1084 tok
->location
= UNKNOWN_LOCATION
;
1085 tok
->u
.value
= NULL_TREE
;
1086 tok
->keyword
= RID_MAX
;
1091 if (tok
== lexer
->last_token
)
1097 while (tok
->purged_p
);
1098 lexer
->next_token
= tok
;
1101 /* Permanently remove all tokens after TOK, up to, but not
1102 including, the token that will be returned next by
1103 cp_lexer_peek_token. */
1106 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1108 cp_token
*peek
= lexer
->next_token
;
1110 if (peek
== &eof_token
)
1111 peek
= lexer
->last_token
;
1113 gcc_assert (tok
< peek
);
1115 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1117 tok
->purged_p
= true;
1118 tok
->location
= UNKNOWN_LOCATION
;
1119 tok
->u
.value
= NULL_TREE
;
1120 tok
->keyword
= RID_MAX
;
1124 /* Begin saving tokens. All tokens consumed after this point will be
1128 cp_lexer_save_tokens (cp_lexer
* lexer
)
1130 /* Provide debugging output. */
1131 if (cp_lexer_debugging_p (lexer
))
1132 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1134 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1137 /* Commit to the portion of the token stream most recently saved. */
1140 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1142 /* Provide debugging output. */
1143 if (cp_lexer_debugging_p (lexer
))
1144 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1146 lexer
->saved_tokens
.pop ();
1149 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1150 to the token stream. Stop saving tokens. */
1153 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1155 /* Provide debugging output. */
1156 if (cp_lexer_debugging_p (lexer
))
1157 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1159 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1162 /* RAII wrapper around the above functions, with sanity checking. Creating
1163 a variable saves tokens, which are committed when the variable is
1164 destroyed unless they are explicitly rolled back by calling the rollback
1167 struct saved_token_sentinel
1172 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1174 len
= lexer
->saved_tokens
.length ();
1175 cp_lexer_save_tokens (lexer
);
1179 cp_lexer_rollback_tokens (lexer
);
1182 ~saved_token_sentinel()
1185 cp_lexer_commit_tokens (lexer
);
1186 gcc_assert (lexer
->saved_tokens
.length () == len
);
1190 /* Print a representation of the TOKEN on the STREAM. */
1193 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1195 /* We don't use cpp_type2name here because the parser defines
1196 a few tokens of its own. */
1197 static const char *const token_names
[] = {
1198 /* cpplib-defined token types */
1199 #define OP(e, s) #e,
1200 #define TK(e, s) #e,
1204 /* C++ parser token types - see "Manifest constants", above. */
1207 "NESTED_NAME_SPECIFIER",
1210 /* For some tokens, print the associated data. */
1211 switch (token
->type
)
1214 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1215 For example, `struct' is mapped to an INTEGER_CST. */
1216 if (!identifier_p (token
->u
.value
))
1218 /* else fall through */
1220 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1227 case CPP_UTF8STRING
:
1228 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1232 print_generic_expr (stream
, token
->u
.value
, 0);
1236 /* If we have a name for the token, print it out. Otherwise, we
1237 simply give the numeric code. */
1238 if (token
->type
< ARRAY_SIZE(token_names
))
1239 fputs (token_names
[token
->type
], stream
);
1241 fprintf (stream
, "[%d]", token
->type
);
1247 debug (cp_token
&ref
)
1249 cp_lexer_print_token (stderr
, &ref
);
1250 fprintf (stderr
, "\n");
1254 debug (cp_token
*ptr
)
1259 fprintf (stderr
, "<nil>\n");
1263 /* Start emitting debugging information. */
1266 cp_lexer_start_debugging (cp_lexer
* lexer
)
1268 lexer
->debugging_p
= true;
1269 cp_lexer_debug_stream
= stderr
;
1272 /* Stop emitting debugging information. */
1275 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1277 lexer
->debugging_p
= false;
1278 cp_lexer_debug_stream
= NULL
;
1281 /* Create a new cp_token_cache, representing a range of tokens. */
1283 static cp_token_cache
*
1284 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1286 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1287 cache
->first
= first
;
1292 /* Diagnose if #pragma omp declare simd isn't followed immediately
1293 by function declaration or definition. */
1296 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1298 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1300 error ("%<#pragma omp declare simd%> not immediately followed by "
1301 "function declaration or definition");
1302 parser
->omp_declare_simd
= NULL
;
1306 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1307 and put that into "omp declare simd" attribute. */
1310 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1312 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1314 if (fndecl
== error_mark_node
)
1316 parser
->omp_declare_simd
= NULL
;
1319 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1321 cp_ensure_no_omp_declare_simd (parser
);
1327 /* Decl-specifiers. */
1329 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1332 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1334 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1339 /* Nothing other than the parser should be creating declarators;
1340 declarators are a semi-syntactic representation of C++ entities.
1341 Other parts of the front end that need to create entities (like
1342 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1344 static cp_declarator
*make_call_declarator
1345 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1346 static cp_declarator
*make_array_declarator
1347 (cp_declarator
*, tree
);
1348 static cp_declarator
*make_pointer_declarator
1349 (cp_cv_quals
, cp_declarator
*, tree
);
1350 static cp_declarator
*make_reference_declarator
1351 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1352 static cp_declarator
*make_ptrmem_declarator
1353 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1355 /* An erroneous declarator. */
1356 static cp_declarator
*cp_error_declarator
;
1358 /* The obstack on which declarators and related data structures are
1360 static struct obstack declarator_obstack
;
1362 /* Alloc BYTES from the declarator memory pool. */
1364 static inline void *
1365 alloc_declarator (size_t bytes
)
1367 return obstack_alloc (&declarator_obstack
, bytes
);
1370 /* Allocate a declarator of the indicated KIND. Clear fields that are
1371 common to all declarators. */
1373 static cp_declarator
*
1374 make_declarator (cp_declarator_kind kind
)
1376 cp_declarator
*declarator
;
1378 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1379 declarator
->kind
= kind
;
1380 declarator
->attributes
= NULL_TREE
;
1381 declarator
->std_attributes
= NULL_TREE
;
1382 declarator
->declarator
= NULL
;
1383 declarator
->parameter_pack_p
= false;
1384 declarator
->id_loc
= UNKNOWN_LOCATION
;
1389 /* Make a declarator for a generalized identifier. If
1390 QUALIFYING_SCOPE is non-NULL, the identifier is
1391 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1392 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1395 static cp_declarator
*
1396 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1397 special_function_kind sfk
)
1399 cp_declarator
*declarator
;
1401 /* It is valid to write:
1403 class C { void f(); };
1407 The standard is not clear about whether `typedef const C D' is
1408 legal; as of 2002-09-15 the committee is considering that
1409 question. EDG 3.0 allows that syntax. Therefore, we do as
1411 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1412 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1414 gcc_assert (identifier_p (unqualified_name
)
1415 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1416 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1418 declarator
= make_declarator (cdk_id
);
1419 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1420 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1421 declarator
->u
.id
.sfk
= sfk
;
1426 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1427 of modifiers such as const or volatile to apply to the pointer
1428 type, represented as identifiers. ATTRIBUTES represent the attributes that
1429 appertain to the pointer or reference. */
1432 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1435 cp_declarator
*declarator
;
1437 declarator
= make_declarator (cdk_pointer
);
1438 declarator
->declarator
= target
;
1439 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1440 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1443 declarator
->id_loc
= target
->id_loc
;
1444 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1445 target
->parameter_pack_p
= false;
1448 declarator
->parameter_pack_p
= false;
1450 declarator
->std_attributes
= attributes
;
1455 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1456 represent the attributes that appertain to the pointer or
1460 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1461 bool rvalue_ref
, tree attributes
)
1463 cp_declarator
*declarator
;
1465 declarator
= make_declarator (cdk_reference
);
1466 declarator
->declarator
= target
;
1467 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1468 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1471 declarator
->id_loc
= target
->id_loc
;
1472 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1473 target
->parameter_pack_p
= false;
1476 declarator
->parameter_pack_p
= false;
1478 declarator
->std_attributes
= attributes
;
1483 /* Like make_pointer_declarator -- but for a pointer to a non-static
1484 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1485 appertain to the pointer or reference. */
1488 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1489 cp_declarator
*pointee
,
1492 cp_declarator
*declarator
;
1494 declarator
= make_declarator (cdk_ptrmem
);
1495 declarator
->declarator
= pointee
;
1496 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1497 declarator
->u
.pointer
.class_type
= class_type
;
1501 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1502 pointee
->parameter_pack_p
= false;
1505 declarator
->parameter_pack_p
= false;
1507 declarator
->std_attributes
= attributes
;
1512 /* Make a declarator for the function given by TARGET, with the
1513 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1514 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1515 indicates what exceptions can be thrown. */
1518 make_call_declarator (cp_declarator
*target
,
1520 cp_cv_quals cv_qualifiers
,
1521 cp_virt_specifiers virt_specifiers
,
1522 cp_ref_qualifier ref_qualifier
,
1523 tree exception_specification
,
1524 tree late_return_type
)
1526 cp_declarator
*declarator
;
1528 declarator
= make_declarator (cdk_function
);
1529 declarator
->declarator
= target
;
1530 declarator
->u
.function
.parameters
= parms
;
1531 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1532 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1533 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1534 declarator
->u
.function
.exception_specification
= exception_specification
;
1535 declarator
->u
.function
.late_return_type
= late_return_type
;
1538 declarator
->id_loc
= target
->id_loc
;
1539 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1540 target
->parameter_pack_p
= false;
1543 declarator
->parameter_pack_p
= false;
1548 /* Make a declarator for an array of BOUNDS elements, each of which is
1549 defined by ELEMENT. */
1552 make_array_declarator (cp_declarator
*element
, tree bounds
)
1554 cp_declarator
*declarator
;
1556 declarator
= make_declarator (cdk_array
);
1557 declarator
->declarator
= element
;
1558 declarator
->u
.array
.bounds
= bounds
;
1561 declarator
->id_loc
= element
->id_loc
;
1562 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1563 element
->parameter_pack_p
= false;
1566 declarator
->parameter_pack_p
= false;
1571 /* Determine whether the declarator we've seen so far can be a
1572 parameter pack, when followed by an ellipsis. */
1574 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1576 if (declarator
&& declarator
->parameter_pack_p
)
1577 /* We already saw an ellipsis. */
1580 /* Search for a declarator name, or any other declarator that goes
1581 after the point where the ellipsis could appear in a parameter
1582 pack. If we find any of these, then this declarator can not be
1583 made into a parameter pack. */
1585 while (declarator
&& !found
)
1587 switch ((int)declarator
->kind
)
1598 declarator
= declarator
->declarator
;
1606 cp_parameter_declarator
*no_parameters
;
1608 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1609 DECLARATOR and DEFAULT_ARGUMENT. */
1611 cp_parameter_declarator
*
1612 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1613 cp_declarator
*declarator
,
1614 tree default_argument
,
1615 bool template_parameter_pack_p
= false)
1617 cp_parameter_declarator
*parameter
;
1619 parameter
= ((cp_parameter_declarator
*)
1620 alloc_declarator (sizeof (cp_parameter_declarator
)));
1621 parameter
->next
= NULL
;
1622 if (decl_specifiers
)
1623 parameter
->decl_specifiers
= *decl_specifiers
;
1625 clear_decl_specs (¶meter
->decl_specifiers
);
1626 parameter
->declarator
= declarator
;
1627 parameter
->default_argument
= default_argument
;
1628 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1633 /* Returns true iff DECLARATOR is a declaration for a function. */
1636 function_declarator_p (const cp_declarator
*declarator
)
1640 if (declarator
->kind
== cdk_function
1641 && declarator
->declarator
->kind
== cdk_id
)
1643 if (declarator
->kind
== cdk_id
1644 || declarator
->kind
== cdk_error
)
1646 declarator
= declarator
->declarator
;
1656 A cp_parser parses the token stream as specified by the C++
1657 grammar. Its job is purely parsing, not semantic analysis. For
1658 example, the parser breaks the token stream into declarators,
1659 expressions, statements, and other similar syntactic constructs.
1660 It does not check that the types of the expressions on either side
1661 of an assignment-statement are compatible, or that a function is
1662 not declared with a parameter of type `void'.
1664 The parser invokes routines elsewhere in the compiler to perform
1665 semantic analysis and to build up the abstract syntax tree for the
1668 The parser (and the template instantiation code, which is, in a
1669 way, a close relative of parsing) are the only parts of the
1670 compiler that should be calling push_scope and pop_scope, or
1671 related functions. The parser (and template instantiation code)
1672 keeps track of what scope is presently active; everything else
1673 should simply honor that. (The code that generates static
1674 initializers may also need to set the scope, in order to check
1675 access control correctly when emitting the initializers.)
1680 The parser is of the standard recursive-descent variety. Upcoming
1681 tokens in the token stream are examined in order to determine which
1682 production to use when parsing a non-terminal. Some C++ constructs
1683 require arbitrary look ahead to disambiguate. For example, it is
1684 impossible, in the general case, to tell whether a statement is an
1685 expression or declaration without scanning the entire statement.
1686 Therefore, the parser is capable of "parsing tentatively." When the
1687 parser is not sure what construct comes next, it enters this mode.
1688 Then, while we attempt to parse the construct, the parser queues up
1689 error messages, rather than issuing them immediately, and saves the
1690 tokens it consumes. If the construct is parsed successfully, the
1691 parser "commits", i.e., it issues any queued error messages and
1692 the tokens that were being preserved are permanently discarded.
1693 If, however, the construct is not parsed successfully, the parser
1694 rolls back its state completely so that it can resume parsing using
1695 a different alternative.
1700 The performance of the parser could probably be improved substantially.
1701 We could often eliminate the need to parse tentatively by looking ahead
1702 a little bit. In some places, this approach might not entirely eliminate
1703 the need to parse tentatively, but it might still speed up the average
1706 /* Flags that are passed to some parsing functions. These values can
1707 be bitwise-ored together. */
1712 CP_PARSER_FLAGS_NONE
= 0x0,
1713 /* The construct is optional. If it is not present, then no error
1714 should be issued. */
1715 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1716 /* When parsing a type-specifier, treat user-defined type-names
1717 as non-type identifiers. */
1718 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1719 /* When parsing a type-specifier, do not try to parse a class-specifier
1720 or enum-specifier. */
1721 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1722 /* When parsing a decl-specifier-seq, only allow type-specifier or
1724 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1727 /* This type is used for parameters and variables which hold
1728 combinations of the above flags. */
1729 typedef int cp_parser_flags
;
1731 /* The different kinds of declarators we want to parse. */
1733 typedef enum cp_parser_declarator_kind
1735 /* We want an abstract declarator. */
1736 CP_PARSER_DECLARATOR_ABSTRACT
,
1737 /* We want a named declarator. */
1738 CP_PARSER_DECLARATOR_NAMED
,
1739 /* We don't mind, but the name must be an unqualified-id. */
1740 CP_PARSER_DECLARATOR_EITHER
1741 } cp_parser_declarator_kind
;
1743 /* The precedence values used to parse binary expressions. The minimum value
1744 of PREC must be 1, because zero is reserved to quickly discriminate
1745 binary operators from other tokens. */
1750 PREC_LOGICAL_OR_EXPRESSION
,
1751 PREC_LOGICAL_AND_EXPRESSION
,
1752 PREC_INCLUSIVE_OR_EXPRESSION
,
1753 PREC_EXCLUSIVE_OR_EXPRESSION
,
1754 PREC_AND_EXPRESSION
,
1755 PREC_EQUALITY_EXPRESSION
,
1756 PREC_RELATIONAL_EXPRESSION
,
1757 PREC_SHIFT_EXPRESSION
,
1758 PREC_ADDITIVE_EXPRESSION
,
1759 PREC_MULTIPLICATIVE_EXPRESSION
,
1761 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1764 /* A mapping from a token type to a corresponding tree node type, with a
1765 precedence value. */
1767 typedef struct cp_parser_binary_operations_map_node
1769 /* The token type. */
1770 enum cpp_ttype token_type
;
1771 /* The corresponding tree code. */
1772 enum tree_code tree_type
;
1773 /* The precedence of this operator. */
1774 enum cp_parser_prec prec
;
1775 } cp_parser_binary_operations_map_node
;
1777 typedef struct cp_parser_expression_stack_entry
1779 /* Left hand side of the binary operation we are currently
1782 /* Original tree code for left hand side, if it was a binary
1783 expression itself (used for -Wparentheses). */
1784 enum tree_code lhs_type
;
1785 /* Tree code for the binary operation we are parsing. */
1786 enum tree_code tree_type
;
1787 /* Precedence of the binary operation we are parsing. */
1788 enum cp_parser_prec prec
;
1789 /* Location of the binary operation we are parsing. */
1791 } cp_parser_expression_stack_entry
;
1793 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1794 entries because precedence levels on the stack are monotonically
1796 typedef struct cp_parser_expression_stack_entry
1797 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1801 /* Constructors and destructors. */
1803 static cp_parser_context
*cp_parser_context_new
1804 (cp_parser_context
*);
1806 /* Class variables. */
1808 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1810 /* The operator-precedence table used by cp_parser_binary_expression.
1811 Transformed into an associative array (binops_by_token) by
1814 static const cp_parser_binary_operations_map_node binops
[] = {
1815 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1816 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1818 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1819 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1820 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1822 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1823 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1825 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1826 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1828 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1829 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1830 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1831 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1833 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1834 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1836 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1838 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1840 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1842 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1844 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1847 /* The same as binops, but initialized by cp_parser_new so that
1848 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1850 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1852 /* Constructors and destructors. */
1854 /* Construct a new context. The context below this one on the stack
1855 is given by NEXT. */
1857 static cp_parser_context
*
1858 cp_parser_context_new (cp_parser_context
* next
)
1860 cp_parser_context
*context
;
1862 /* Allocate the storage. */
1863 if (cp_parser_context_free_list
!= NULL
)
1865 /* Pull the first entry from the free list. */
1866 context
= cp_parser_context_free_list
;
1867 cp_parser_context_free_list
= context
->next
;
1868 memset (context
, 0, sizeof (*context
));
1871 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1873 /* No errors have occurred yet in this context. */
1874 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1875 /* If this is not the bottommost context, copy information that we
1876 need from the previous context. */
1879 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1880 expression, then we are parsing one in this context, too. */
1881 context
->object_type
= next
->object_type
;
1882 /* Thread the stack. */
1883 context
->next
= next
;
1889 /* Managing the unparsed function queues. */
1891 #define unparsed_funs_with_default_args \
1892 parser->unparsed_queues->last ().funs_with_default_args
1893 #define unparsed_funs_with_definitions \
1894 parser->unparsed_queues->last ().funs_with_definitions
1895 #define unparsed_nsdmis \
1896 parser->unparsed_queues->last ().nsdmis
1897 #define unparsed_classes \
1898 parser->unparsed_queues->last ().classes
1901 push_unparsed_function_queues (cp_parser
*parser
)
1903 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1904 vec_safe_push (parser
->unparsed_queues
, e
);
1908 pop_unparsed_function_queues (cp_parser
*parser
)
1910 release_tree_vector (unparsed_funs_with_definitions
);
1911 parser
->unparsed_queues
->pop ();
1916 /* Constructors and destructors. */
1918 static cp_parser
*cp_parser_new
1921 /* Routines to parse various constructs.
1923 Those that return `tree' will return the error_mark_node (rather
1924 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1925 Sometimes, they will return an ordinary node if error-recovery was
1926 attempted, even though a parse error occurred. So, to check
1927 whether or not a parse error occurred, you should always use
1928 cp_parser_error_occurred. If the construct is optional (indicated
1929 either by an `_opt' in the name of the function that does the
1930 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1931 the construct is not present. */
1933 /* Lexical conventions [gram.lex] */
1935 static tree cp_parser_identifier
1937 static tree cp_parser_string_literal
1938 (cp_parser
*, bool, bool, bool);
1939 static tree cp_parser_userdef_char_literal
1941 static tree cp_parser_userdef_string_literal
1943 static tree cp_parser_userdef_numeric_literal
1946 /* Basic concepts [gram.basic] */
1948 static bool cp_parser_translation_unit
1951 /* Expressions [gram.expr] */
1953 static tree cp_parser_primary_expression
1954 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1955 static tree cp_parser_id_expression
1956 (cp_parser
*, bool, bool, bool *, bool, bool);
1957 static tree cp_parser_unqualified_id
1958 (cp_parser
*, bool, bool, bool, bool);
1959 static tree cp_parser_nested_name_specifier_opt
1960 (cp_parser
*, bool, bool, bool, bool);
1961 static tree cp_parser_nested_name_specifier
1962 (cp_parser
*, bool, bool, bool, bool);
1963 static tree cp_parser_qualifying_entity
1964 (cp_parser
*, bool, bool, bool, bool, bool);
1965 static tree cp_parser_postfix_expression
1966 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1967 static tree cp_parser_postfix_open_square_expression
1968 (cp_parser
*, tree
, bool, bool);
1969 static tree cp_parser_postfix_dot_deref_expression
1970 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1971 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1972 (cp_parser
*, int, bool, bool, bool *, bool = false);
1973 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1974 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1975 static void cp_parser_pseudo_destructor_name
1976 (cp_parser
*, tree
, tree
*, tree
*);
1977 static tree cp_parser_unary_expression
1978 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1979 static enum tree_code cp_parser_unary_operator
1981 static tree cp_parser_new_expression
1983 static vec
<tree
, va_gc
> *cp_parser_new_placement
1985 static tree cp_parser_new_type_id
1986 (cp_parser
*, tree
*);
1987 static cp_declarator
*cp_parser_new_declarator_opt
1989 static cp_declarator
*cp_parser_direct_new_declarator
1991 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1993 static tree cp_parser_delete_expression
1995 static tree cp_parser_cast_expression
1996 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1997 static tree cp_parser_binary_expression
1998 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1999 static tree cp_parser_question_colon_clause
2000 (cp_parser
*, tree
);
2001 static tree cp_parser_assignment_expression
2002 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2003 static enum tree_code cp_parser_assignment_operator_opt
2005 static tree cp_parser_expression
2006 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2007 static tree cp_parser_constant_expression
2008 (cp_parser
*, bool = false, bool * = NULL
);
2009 static tree cp_parser_builtin_offsetof
2011 static tree cp_parser_lambda_expression
2013 static void cp_parser_lambda_introducer
2014 (cp_parser
*, tree
);
2015 static bool cp_parser_lambda_declarator_opt
2016 (cp_parser
*, tree
);
2017 static void cp_parser_lambda_body
2018 (cp_parser
*, tree
);
2020 /* Statements [gram.stmt.stmt] */
2022 static void cp_parser_statement
2023 (cp_parser
*, tree
, bool, bool *);
2024 static void cp_parser_label_for_labeled_statement
2025 (cp_parser
*, tree
);
2026 static tree cp_parser_expression_statement
2027 (cp_parser
*, tree
);
2028 static tree cp_parser_compound_statement
2029 (cp_parser
*, tree
, bool, bool);
2030 static void cp_parser_statement_seq_opt
2031 (cp_parser
*, tree
);
2032 static tree cp_parser_selection_statement
2033 (cp_parser
*, bool *);
2034 static tree cp_parser_condition
2036 static tree cp_parser_iteration_statement
2037 (cp_parser
*, bool);
2038 static bool cp_parser_for_init_statement
2039 (cp_parser
*, tree
*decl
);
2040 static tree cp_parser_for
2041 (cp_parser
*, bool);
2042 static tree cp_parser_c_for
2043 (cp_parser
*, tree
, tree
, bool);
2044 static tree cp_parser_range_for
2045 (cp_parser
*, tree
, tree
, tree
, bool);
2046 static void do_range_for_auto_deduction
2048 static tree cp_parser_perform_range_for_lookup
2049 (tree
, tree
*, tree
*);
2050 static tree cp_parser_range_for_member_function
2052 static tree cp_parser_jump_statement
2054 static void cp_parser_declaration_statement
2057 static tree cp_parser_implicitly_scoped_statement
2058 (cp_parser
*, bool *, location_t
, const char *);
2059 static void cp_parser_already_scoped_statement
2060 (cp_parser
*, location_t
, const char *);
2062 /* Declarations [gram.dcl.dcl] */
2064 static void cp_parser_declaration_seq_opt
2066 static void cp_parser_declaration
2068 static void cp_parser_block_declaration
2069 (cp_parser
*, bool);
2070 static void cp_parser_simple_declaration
2071 (cp_parser
*, bool, tree
*);
2072 static void cp_parser_decl_specifier_seq
2073 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2074 static tree cp_parser_storage_class_specifier_opt
2076 static tree cp_parser_function_specifier_opt
2077 (cp_parser
*, cp_decl_specifier_seq
*);
2078 static tree cp_parser_type_specifier
2079 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2081 static tree cp_parser_simple_type_specifier
2082 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2083 static tree cp_parser_type_name
2085 static tree cp_parser_nonclass_name
2086 (cp_parser
* parser
);
2087 static tree cp_parser_elaborated_type_specifier
2088 (cp_parser
*, bool, bool);
2089 static tree cp_parser_enum_specifier
2091 static void cp_parser_enumerator_list
2092 (cp_parser
*, tree
);
2093 static void cp_parser_enumerator_definition
2094 (cp_parser
*, tree
);
2095 static tree cp_parser_namespace_name
2097 static void cp_parser_namespace_definition
2099 static void cp_parser_namespace_body
2101 static tree cp_parser_qualified_namespace_specifier
2103 static void cp_parser_namespace_alias_definition
2105 static bool cp_parser_using_declaration
2106 (cp_parser
*, bool);
2107 static void cp_parser_using_directive
2109 static tree cp_parser_alias_declaration
2111 static void cp_parser_asm_definition
2113 static void cp_parser_linkage_specification
2115 static void cp_parser_static_assert
2116 (cp_parser
*, bool);
2117 static tree cp_parser_decltype
2120 /* Declarators [gram.dcl.decl] */
2122 static tree cp_parser_init_declarator
2123 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2124 bool, bool, int, bool *, tree
*, location_t
*);
2125 static cp_declarator
*cp_parser_declarator
2126 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2127 static cp_declarator
*cp_parser_direct_declarator
2128 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2129 static enum tree_code cp_parser_ptr_operator
2130 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2131 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2133 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2135 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2137 static tree cp_parser_late_return_type_opt
2138 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2139 static tree cp_parser_declarator_id
2140 (cp_parser
*, bool);
2141 static tree cp_parser_type_id
2143 static tree cp_parser_template_type_arg
2145 static tree
cp_parser_trailing_type_id (cp_parser
*);
2146 static tree cp_parser_type_id_1
2147 (cp_parser
*, bool, bool);
2148 static void cp_parser_type_specifier_seq
2149 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2150 static tree cp_parser_parameter_declaration_clause
2152 static tree cp_parser_parameter_declaration_list
2153 (cp_parser
*, bool *);
2154 static cp_parameter_declarator
*cp_parser_parameter_declaration
2155 (cp_parser
*, bool, bool *);
2156 static tree cp_parser_default_argument
2157 (cp_parser
*, bool);
2158 static void cp_parser_function_body
2159 (cp_parser
*, bool);
2160 static tree cp_parser_initializer
2161 (cp_parser
*, bool *, bool *);
2162 static tree cp_parser_initializer_clause
2163 (cp_parser
*, bool *);
2164 static tree cp_parser_braced_list
2165 (cp_parser
*, bool*);
2166 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2167 (cp_parser
*, bool *);
2169 static bool cp_parser_ctor_initializer_opt_and_function_body
2170 (cp_parser
*, bool);
2172 static tree cp_parser_late_parsing_omp_declare_simd
2173 (cp_parser
*, tree
);
2175 static tree cp_parser_late_parsing_cilk_simd_fn_info
2176 (cp_parser
*, tree
);
2178 static tree synthesize_implicit_template_parm
2180 static tree finish_fully_implicit_template
2181 (cp_parser
*, tree
);
2183 /* Classes [gram.class] */
2185 static tree cp_parser_class_name
2186 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2187 static tree cp_parser_class_specifier
2189 static tree cp_parser_class_head
2190 (cp_parser
*, bool *);
2191 static enum tag_types cp_parser_class_key
2193 static void cp_parser_type_parameter_key
2194 (cp_parser
* parser
);
2195 static void cp_parser_member_specification_opt
2197 static void cp_parser_member_declaration
2199 static tree cp_parser_pure_specifier
2201 static tree cp_parser_constant_initializer
2204 /* Derived classes [gram.class.derived] */
2206 static tree cp_parser_base_clause
2208 static tree cp_parser_base_specifier
2211 /* Special member functions [gram.special] */
2213 static tree cp_parser_conversion_function_id
2215 static tree cp_parser_conversion_type_id
2217 static cp_declarator
*cp_parser_conversion_declarator_opt
2219 static bool cp_parser_ctor_initializer_opt
2221 static void cp_parser_mem_initializer_list
2223 static tree cp_parser_mem_initializer
2225 static tree cp_parser_mem_initializer_id
2228 /* Overloading [gram.over] */
2230 static tree cp_parser_operator_function_id
2232 static tree cp_parser_operator
2235 /* Templates [gram.temp] */
2237 static void cp_parser_template_declaration
2238 (cp_parser
*, bool);
2239 static tree cp_parser_template_parameter_list
2241 static tree cp_parser_template_parameter
2242 (cp_parser
*, bool *, bool *);
2243 static tree cp_parser_type_parameter
2244 (cp_parser
*, bool *);
2245 static tree cp_parser_template_id
2246 (cp_parser
*, bool, bool, enum tag_types
, bool);
2247 static tree cp_parser_template_name
2248 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2249 static tree cp_parser_template_argument_list
2251 static tree cp_parser_template_argument
2253 static void cp_parser_explicit_instantiation
2255 static void cp_parser_explicit_specialization
2258 /* Exception handling [gram.exception] */
2260 static tree cp_parser_try_block
2262 static bool cp_parser_function_try_block
2264 static void cp_parser_handler_seq
2266 static void cp_parser_handler
2268 static tree cp_parser_exception_declaration
2270 static tree cp_parser_throw_expression
2272 static tree cp_parser_exception_specification_opt
2274 static tree cp_parser_type_id_list
2277 /* GNU Extensions */
2279 static tree cp_parser_asm_specification_opt
2281 static tree cp_parser_asm_operand_list
2283 static tree cp_parser_asm_clobber_list
2285 static tree cp_parser_asm_label_list
2287 static bool cp_next_tokens_can_be_attribute_p
2289 static bool cp_next_tokens_can_be_gnu_attribute_p
2291 static bool cp_next_tokens_can_be_std_attribute_p
2293 static bool cp_nth_tokens_can_be_std_attribute_p
2294 (cp_parser
*, size_t);
2295 static bool cp_nth_tokens_can_be_gnu_attribute_p
2296 (cp_parser
*, size_t);
2297 static bool cp_nth_tokens_can_be_attribute_p
2298 (cp_parser
*, size_t);
2299 static tree cp_parser_attributes_opt
2301 static tree cp_parser_gnu_attributes_opt
2303 static tree cp_parser_gnu_attribute_list
2305 static tree cp_parser_std_attribute
2307 static tree cp_parser_std_attribute_spec
2309 static tree cp_parser_std_attribute_spec_seq
2311 static bool cp_parser_extension_opt
2312 (cp_parser
*, int *);
2313 static void cp_parser_label_declaration
2316 /* Transactional Memory Extensions */
2318 static tree cp_parser_transaction
2319 (cp_parser
*, enum rid
);
2320 static tree cp_parser_transaction_expression
2321 (cp_parser
*, enum rid
);
2322 static bool cp_parser_function_transaction
2323 (cp_parser
*, enum rid
);
2324 static tree cp_parser_transaction_cancel
2327 enum pragma_context
{
2334 static bool cp_parser_pragma
2335 (cp_parser
*, enum pragma_context
);
2337 /* Objective-C++ Productions */
2339 static tree cp_parser_objc_message_receiver
2341 static tree cp_parser_objc_message_args
2343 static tree cp_parser_objc_message_expression
2345 static tree cp_parser_objc_encode_expression
2347 static tree cp_parser_objc_defs_expression
2349 static tree cp_parser_objc_protocol_expression
2351 static tree cp_parser_objc_selector_expression
2353 static tree cp_parser_objc_expression
2355 static bool cp_parser_objc_selector_p
2357 static tree cp_parser_objc_selector
2359 static tree cp_parser_objc_protocol_refs_opt
2361 static void cp_parser_objc_declaration
2362 (cp_parser
*, tree
);
2363 static tree cp_parser_objc_statement
2365 static bool cp_parser_objc_valid_prefix_attributes
2366 (cp_parser
*, tree
*);
2367 static void cp_parser_objc_at_property_declaration
2369 static void cp_parser_objc_at_synthesize_declaration
2371 static void cp_parser_objc_at_dynamic_declaration
2373 static tree cp_parser_objc_struct_declaration
2376 /* Utility Routines */
2378 static tree cp_parser_lookup_name
2379 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2380 static tree cp_parser_lookup_name_simple
2381 (cp_parser
*, tree
, location_t
);
2382 static tree cp_parser_maybe_treat_template_as_class
2384 static bool cp_parser_check_declarator_template_parameters
2385 (cp_parser
*, cp_declarator
*, location_t
);
2386 static bool cp_parser_check_template_parameters
2387 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2388 static tree cp_parser_simple_cast_expression
2390 static tree cp_parser_global_scope_opt
2391 (cp_parser
*, bool);
2392 static bool cp_parser_constructor_declarator_p
2393 (cp_parser
*, bool);
2394 static tree cp_parser_function_definition_from_specifiers_and_declarator
2395 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2396 static tree cp_parser_function_definition_after_declarator
2397 (cp_parser
*, bool);
2398 static void cp_parser_template_declaration_after_export
2399 (cp_parser
*, bool);
2400 static void cp_parser_perform_template_parameter_access_checks
2401 (vec
<deferred_access_check
, va_gc
> *);
2402 static tree cp_parser_single_declaration
2403 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2404 static tree cp_parser_functional_cast
2405 (cp_parser
*, tree
);
2406 static tree cp_parser_save_member_function_body
2407 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2408 static tree cp_parser_save_nsdmi
2410 static tree cp_parser_enclosed_template_argument_list
2412 static void cp_parser_save_default_args
2413 (cp_parser
*, tree
);
2414 static void cp_parser_late_parsing_for_member
2415 (cp_parser
*, tree
);
2416 static tree cp_parser_late_parse_one_default_arg
2417 (cp_parser
*, tree
, tree
, tree
);
2418 static void cp_parser_late_parsing_nsdmi
2419 (cp_parser
*, tree
);
2420 static void cp_parser_late_parsing_default_args
2421 (cp_parser
*, tree
);
2422 static tree cp_parser_sizeof_operand
2423 (cp_parser
*, enum rid
);
2424 static tree cp_parser_trait_expr
2425 (cp_parser
*, enum rid
);
2426 static bool cp_parser_declares_only_class_p
2428 static void cp_parser_set_storage_class
2429 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2430 static void cp_parser_set_decl_spec_type
2431 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2432 static void set_and_check_decl_spec_loc
2433 (cp_decl_specifier_seq
*decl_specs
,
2434 cp_decl_spec ds
, cp_token
*);
2435 static bool cp_parser_friend_p
2436 (const cp_decl_specifier_seq
*);
2437 static void cp_parser_required_error
2438 (cp_parser
*, required_token
, bool);
2439 static cp_token
*cp_parser_require
2440 (cp_parser
*, enum cpp_ttype
, required_token
);
2441 static cp_token
*cp_parser_require_keyword
2442 (cp_parser
*, enum rid
, required_token
);
2443 static bool cp_parser_token_starts_function_definition_p
2445 static bool cp_parser_next_token_starts_class_definition_p
2447 static bool cp_parser_next_token_ends_template_argument_p
2449 static bool cp_parser_nth_token_starts_template_argument_list_p
2450 (cp_parser
*, size_t);
2451 static enum tag_types cp_parser_token_is_class_key
2453 static enum tag_types cp_parser_token_is_type_parameter_key
2455 static void cp_parser_check_class_key
2456 (enum tag_types
, tree type
);
2457 static void cp_parser_check_access_in_redeclaration
2458 (tree type
, location_t location
);
2459 static bool cp_parser_optional_template_keyword
2461 static void cp_parser_pre_parsed_nested_name_specifier
2463 static bool cp_parser_cache_group
2464 (cp_parser
*, enum cpp_ttype
, unsigned);
2465 static tree cp_parser_cache_defarg
2466 (cp_parser
*parser
, bool nsdmi
);
2467 static void cp_parser_parse_tentatively
2469 static void cp_parser_commit_to_tentative_parse
2471 static void cp_parser_commit_to_topmost_tentative_parse
2473 static void cp_parser_abort_tentative_parse
2475 static bool cp_parser_parse_definitely
2477 static inline bool cp_parser_parsing_tentatively
2479 static bool cp_parser_uncommitted_to_tentative_parse_p
2481 static void cp_parser_error
2482 (cp_parser
*, const char *);
2483 static void cp_parser_name_lookup_error
2484 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2485 static bool cp_parser_simulate_error
2487 static bool cp_parser_check_type_definition
2489 static void cp_parser_check_for_definition_in_return_type
2490 (cp_declarator
*, tree
, location_t type_location
);
2491 static void cp_parser_check_for_invalid_template_id
2492 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2493 static bool cp_parser_non_integral_constant_expression
2494 (cp_parser
*, non_integral_constant
);
2495 static void cp_parser_diagnose_invalid_type_name
2496 (cp_parser
*, tree
, location_t
);
2497 static bool cp_parser_parse_and_diagnose_invalid_type_name
2499 static int cp_parser_skip_to_closing_parenthesis
2500 (cp_parser
*, bool, bool, bool);
2501 static void cp_parser_skip_to_end_of_statement
2503 static void cp_parser_consume_semicolon_at_end_of_statement
2505 static void cp_parser_skip_to_end_of_block_or_statement
2507 static bool cp_parser_skip_to_closing_brace
2509 static void cp_parser_skip_to_end_of_template_parameter_list
2511 static void cp_parser_skip_to_pragma_eol
2512 (cp_parser
*, cp_token
*);
2513 static bool cp_parser_error_occurred
2515 static bool cp_parser_allow_gnu_extensions_p
2517 static bool cp_parser_is_pure_string_literal
2519 static bool cp_parser_is_string_literal
2521 static bool cp_parser_is_keyword
2522 (cp_token
*, enum rid
);
2523 static tree cp_parser_make_typename_type
2524 (cp_parser
*, tree
, location_t location
);
2525 static cp_declarator
* cp_parser_make_indirect_declarator
2526 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2527 static bool cp_parser_compound_literal_p
2529 static bool cp_parser_array_designator_p
2531 static bool cp_parser_skip_to_closing_square_bracket
2534 /* Returns nonzero if we are parsing tentatively. */
2537 cp_parser_parsing_tentatively (cp_parser
* parser
)
2539 return parser
->context
->next
!= NULL
;
2542 /* Returns nonzero if TOKEN is a string literal. */
2545 cp_parser_is_pure_string_literal (cp_token
* token
)
2547 return (token
->type
== CPP_STRING
||
2548 token
->type
== CPP_STRING16
||
2549 token
->type
== CPP_STRING32
||
2550 token
->type
== CPP_WSTRING
||
2551 token
->type
== CPP_UTF8STRING
);
2554 /* Returns nonzero if TOKEN is a string literal
2555 of a user-defined string literal. */
2558 cp_parser_is_string_literal (cp_token
* token
)
2560 return (cp_parser_is_pure_string_literal (token
) ||
2561 token
->type
== CPP_STRING_USERDEF
||
2562 token
->type
== CPP_STRING16_USERDEF
||
2563 token
->type
== CPP_STRING32_USERDEF
||
2564 token
->type
== CPP_WSTRING_USERDEF
||
2565 token
->type
== CPP_UTF8STRING_USERDEF
);
2568 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2571 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2573 return token
->keyword
== keyword
;
2576 /* If not parsing tentatively, issue a diagnostic of the form
2577 FILE:LINE: MESSAGE before TOKEN
2578 where TOKEN is the next token in the input stream. MESSAGE
2579 (specified by the caller) is usually of the form "expected
2583 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2585 if (!cp_parser_simulate_error (parser
))
2587 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2588 /* This diagnostic makes more sense if it is tagged to the line
2589 of the token we just peeked at. */
2590 cp_lexer_set_source_position_from_token (token
);
2592 if (token
->type
== CPP_PRAGMA
)
2594 error_at (token
->location
,
2595 "%<#pragma%> is not allowed here");
2596 cp_parser_skip_to_pragma_eol (parser
, token
);
2600 c_parse_error (gmsgid
,
2601 /* Because c_parser_error does not understand
2602 CPP_KEYWORD, keywords are treated like
2604 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2605 token
->u
.value
, token
->flags
);
2609 /* Issue an error about name-lookup failing. NAME is the
2610 IDENTIFIER_NODE DECL is the result of
2611 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2612 the thing that we hoped to find. */
2615 cp_parser_name_lookup_error (cp_parser
* parser
,
2618 name_lookup_error desired
,
2619 location_t location
)
2621 /* If name lookup completely failed, tell the user that NAME was not
2623 if (decl
== error_mark_node
)
2625 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2626 error_at (location
, "%<%E::%E%> has not been declared",
2627 parser
->scope
, name
);
2628 else if (parser
->scope
== global_namespace
)
2629 error_at (location
, "%<::%E%> has not been declared", name
);
2630 else if (parser
->object_scope
2631 && !CLASS_TYPE_P (parser
->object_scope
))
2632 error_at (location
, "request for member %qE in non-class type %qT",
2633 name
, parser
->object_scope
);
2634 else if (parser
->object_scope
)
2635 error_at (location
, "%<%T::%E%> has not been declared",
2636 parser
->object_scope
, name
);
2638 error_at (location
, "%qE has not been declared", name
);
2640 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2645 error_at (location
, "%<%E::%E%> is not a type",
2646 parser
->scope
, name
);
2649 error_at (location
, "%<%E::%E%> is not a class or namespace",
2650 parser
->scope
, name
);
2654 "%<%E::%E%> is not a class, namespace, or enumeration",
2655 parser
->scope
, name
);
2662 else if (parser
->scope
== global_namespace
)
2667 error_at (location
, "%<::%E%> is not a type", name
);
2670 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2674 "%<::%E%> is not a class, namespace, or enumeration",
2686 error_at (location
, "%qE is not a type", name
);
2689 error_at (location
, "%qE is not a class or namespace", name
);
2693 "%qE is not a class, namespace, or enumeration", name
);
2701 /* If we are parsing tentatively, remember that an error has occurred
2702 during this tentative parse. Returns true if the error was
2703 simulated; false if a message should be issued by the caller. */
2706 cp_parser_simulate_error (cp_parser
* parser
)
2708 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2710 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2716 /* This function is called when a type is defined. If type
2717 definitions are forbidden at this point, an error message is
2721 cp_parser_check_type_definition (cp_parser
* parser
)
2723 /* If types are forbidden here, issue a message. */
2724 if (parser
->type_definition_forbidden_message
)
2726 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2727 in the message need to be interpreted. */
2728 error (parser
->type_definition_forbidden_message
);
2734 /* This function is called when the DECLARATOR is processed. The TYPE
2735 was a type defined in the decl-specifiers. If it is invalid to
2736 define a type in the decl-specifiers for DECLARATOR, an error is
2737 issued. TYPE_LOCATION is the location of TYPE and is used
2738 for error reporting. */
2741 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2742 tree type
, location_t type_location
)
2744 /* [dcl.fct] forbids type definitions in return types.
2745 Unfortunately, it's not easy to know whether or not we are
2746 processing a return type until after the fact. */
2748 && (declarator
->kind
== cdk_pointer
2749 || declarator
->kind
== cdk_reference
2750 || declarator
->kind
== cdk_ptrmem
))
2751 declarator
= declarator
->declarator
;
2753 && declarator
->kind
== cdk_function
)
2755 error_at (type_location
,
2756 "new types may not be defined in a return type");
2757 inform (type_location
,
2758 "(perhaps a semicolon is missing after the definition of %qT)",
2763 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2764 "<" in any valid C++ program. If the next token is indeed "<",
2765 issue a message warning the user about what appears to be an
2766 invalid attempt to form a template-id. LOCATION is the location
2767 of the type-specifier (TYPE) */
2770 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2772 enum tag_types tag_type
,
2773 location_t location
)
2775 cp_token_position start
= 0;
2777 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2780 error_at (location
, "%qT is not a template", type
);
2781 else if (identifier_p (type
))
2783 if (tag_type
!= none_type
)
2784 error_at (location
, "%qE is not a class template", type
);
2786 error_at (location
, "%qE is not a template", type
);
2789 error_at (location
, "invalid template-id");
2790 /* Remember the location of the invalid "<". */
2791 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2792 start
= cp_lexer_token_position (parser
->lexer
, true);
2793 /* Consume the "<". */
2794 cp_lexer_consume_token (parser
->lexer
);
2795 /* Parse the template arguments. */
2796 cp_parser_enclosed_template_argument_list (parser
);
2797 /* Permanently remove the invalid template arguments so that
2798 this error message is not issued again. */
2800 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2804 /* If parsing an integral constant-expression, issue an error message
2805 about the fact that THING appeared and return true. Otherwise,
2806 return false. In either case, set
2807 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2810 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2811 non_integral_constant thing
)
2813 parser
->non_integral_constant_expression_p
= true;
2814 if (parser
->integral_constant_expression_p
)
2816 if (!parser
->allow_non_integral_constant_expression_p
)
2818 const char *msg
= NULL
;
2822 error ("floating-point literal "
2823 "cannot appear in a constant-expression");
2826 error ("a cast to a type other than an integral or "
2827 "enumeration type cannot appear in a "
2828 "constant-expression");
2831 error ("%<typeid%> operator "
2832 "cannot appear in a constant-expression");
2835 error ("non-constant compound literals "
2836 "cannot appear in a constant-expression");
2839 error ("a function call "
2840 "cannot appear in a constant-expression");
2843 error ("an increment "
2844 "cannot appear in a constant-expression");
2847 error ("an decrement "
2848 "cannot appear in a constant-expression");
2851 error ("an array reference "
2852 "cannot appear in a constant-expression");
2854 case NIC_ADDR_LABEL
:
2855 error ("the address of a label "
2856 "cannot appear in a constant-expression");
2858 case NIC_OVERLOADED
:
2859 error ("calls to overloaded operators "
2860 "cannot appear in a constant-expression");
2862 case NIC_ASSIGNMENT
:
2863 error ("an assignment cannot appear in a constant-expression");
2866 error ("a comma operator "
2867 "cannot appear in a constant-expression");
2869 case NIC_CONSTRUCTOR
:
2870 error ("a call to a constructor "
2871 "cannot appear in a constant-expression");
2873 case NIC_TRANSACTION
:
2874 error ("a transaction expression "
2875 "cannot appear in a constant-expression");
2881 msg
= "__FUNCTION__";
2883 case NIC_PRETTY_FUNC
:
2884 msg
= "__PRETTY_FUNCTION__";
2904 case NIC_PREINCREMENT
:
2907 case NIC_PREDECREMENT
:
2920 error ("%qs cannot appear in a constant-expression", msg
);
2927 /* Emit a diagnostic for an invalid type name. This function commits
2928 to the current active tentative parse, if any. (Otherwise, the
2929 problematic construct might be encountered again later, resulting
2930 in duplicate error messages.) LOCATION is the location of ID. */
2933 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2934 location_t location
)
2936 tree decl
, ambiguous_decls
;
2937 cp_parser_commit_to_tentative_parse (parser
);
2938 /* Try to lookup the identifier. */
2939 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2940 /*is_template=*/false,
2941 /*is_namespace=*/false,
2942 /*check_dependency=*/true,
2943 &ambiguous_decls
, location
);
2944 if (ambiguous_decls
)
2945 /* If the lookup was ambiguous, an error will already have
2948 /* If the lookup found a template-name, it means that the user forgot
2949 to specify an argument list. Emit a useful error message. */
2950 if (DECL_TYPE_TEMPLATE_P (decl
))
2953 "invalid use of template-name %qE without an argument list",
2955 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
2957 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2958 error_at (location
, "invalid use of destructor %qD as a type", id
);
2959 else if (TREE_CODE (decl
) == TYPE_DECL
)
2960 /* Something like 'unsigned A a;' */
2961 error_at (location
, "invalid combination of multiple type-specifiers");
2962 else if (!parser
->scope
)
2964 /* Issue an error message. */
2965 error_at (location
, "%qE does not name a type", id
);
2966 /* If we're in a template class, it's possible that the user was
2967 referring to a type from a base class. For example:
2969 template <typename T> struct A { typedef T X; };
2970 template <typename T> struct B : public A<T> { X x; };
2972 The user should have said "typename A<T>::X". */
2973 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2974 inform (location
, "C++11 %<constexpr%> only available with "
2975 "-std=c++11 or -std=gnu++11");
2976 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2977 inform (location
, "C++11 %<noexcept%> only available with "
2978 "-std=c++11 or -std=gnu++11");
2979 else if (cxx_dialect
< cxx11
2980 && TREE_CODE (id
) == IDENTIFIER_NODE
2981 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2982 inform (location
, "C++11 %<thread_local%> only available with "
2983 "-std=c++11 or -std=gnu++11");
2984 else if (processing_template_decl
&& current_class_type
2985 && TYPE_BINFO (current_class_type
))
2989 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2993 tree base_type
= BINFO_TYPE (b
);
2994 if (CLASS_TYPE_P (base_type
)
2995 && dependent_type_p (base_type
))
2998 /* Go from a particular instantiation of the
2999 template (which will have an empty TYPE_FIELDs),
3000 to the main version. */
3001 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3002 for (field
= TYPE_FIELDS (base_type
);
3004 field
= DECL_CHAIN (field
))
3005 if (TREE_CODE (field
) == TYPE_DECL
3006 && DECL_NAME (field
) == id
)
3009 "(perhaps %<typename %T::%E%> was intended)",
3010 BINFO_TYPE (b
), id
);
3019 /* Here we diagnose qualified-ids where the scope is actually correct,
3020 but the identifier does not resolve to a valid type name. */
3021 else if (parser
->scope
!= error_mark_node
)
3023 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3026 error_at (location_of (id
),
3027 "%qE in namespace %qE does not name a template type",
3030 error_at (location_of (id
),
3031 "%qE in namespace %qE does not name a type",
3034 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3036 else if (CLASS_TYPE_P (parser
->scope
)
3037 && constructor_name_p (id
, parser
->scope
))
3040 error_at (location
, "%<%T::%E%> names the constructor, not"
3041 " the type", parser
->scope
, id
);
3042 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3043 error_at (location
, "and %qT has no template constructors",
3046 else if (TYPE_P (parser
->scope
)
3047 && dependent_scope_p (parser
->scope
))
3048 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3049 "%qT is a dependent scope",
3050 parser
->scope
, id
, parser
->scope
);
3051 else if (TYPE_P (parser
->scope
))
3053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3054 error_at (location_of (id
),
3055 "%qE in %q#T does not name a template type",
3058 error_at (location_of (id
),
3059 "%qE in %q#T does not name a type",
3062 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3069 /* Check for a common situation where a type-name should be present,
3070 but is not, and issue a sensible error message. Returns true if an
3071 invalid type-name was detected.
3073 The situation handled by this function are variable declarations of the
3074 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3075 Usually, `ID' should name a type, but if we got here it means that it
3076 does not. We try to emit the best possible error message depending on
3077 how exactly the id-expression looks like. */
3080 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3083 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3085 /* Avoid duplicate error about ambiguous lookup. */
3086 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3088 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3089 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3093 cp_parser_parse_tentatively (parser
);
3094 id
= cp_parser_id_expression (parser
,
3095 /*template_keyword_p=*/false,
3096 /*check_dependency_p=*/true,
3097 /*template_p=*/NULL
,
3098 /*declarator_p=*/true,
3099 /*optional_p=*/false);
3100 /* If the next token is a (, this is a function with no explicit return
3101 type, i.e. constructor, destructor or conversion op. */
3102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3103 || TREE_CODE (id
) == TYPE_DECL
)
3105 cp_parser_abort_tentative_parse (parser
);
3108 if (!cp_parser_parse_definitely (parser
))
3111 /* Emit a diagnostic for the invalid type. */
3112 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3114 /* If we aren't in the middle of a declarator (i.e. in a
3115 parameter-declaration-clause), skip to the end of the declaration;
3116 there's no point in trying to process it. */
3117 if (!parser
->in_declarator_p
)
3118 cp_parser_skip_to_end_of_block_or_statement (parser
);
3122 /* Consume tokens up to, and including, the next non-nested closing `)'.
3123 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3124 are doing error recovery. Returns -1 if OR_COMMA is true and we
3125 found an unnested comma. */
3128 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3133 unsigned paren_depth
= 0;
3134 unsigned brace_depth
= 0;
3135 unsigned square_depth
= 0;
3137 if (recovering
&& !or_comma
3138 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3143 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3145 switch (token
->type
)
3148 case CPP_PRAGMA_EOL
:
3149 /* If we've run out of tokens, then there is no closing `)'. */
3152 /* This is good for lambda expression capture-lists. */
3153 case CPP_OPEN_SQUARE
:
3156 case CPP_CLOSE_SQUARE
:
3157 if (!square_depth
--)
3162 /* This matches the processing in skip_to_end_of_statement. */
3167 case CPP_OPEN_BRACE
:
3170 case CPP_CLOSE_BRACE
:
3176 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3181 case CPP_OPEN_PAREN
:
3186 case CPP_CLOSE_PAREN
:
3187 if (!brace_depth
&& !paren_depth
--)
3190 cp_lexer_consume_token (parser
->lexer
);
3199 /* Consume the token. */
3200 cp_lexer_consume_token (parser
->lexer
);
3204 /* Consume tokens until we reach the end of the current statement.
3205 Normally, that will be just before consuming a `;'. However, if a
3206 non-nested `}' comes first, then we stop before consuming that. */
3209 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3211 unsigned nesting_depth
= 0;
3213 /* Unwind generic function template scope if necessary. */
3214 if (parser
->fully_implicit_function_template_p
)
3215 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3219 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3221 switch (token
->type
)
3224 case CPP_PRAGMA_EOL
:
3225 /* If we've run out of tokens, stop. */
3229 /* If the next token is a `;', we have reached the end of the
3235 case CPP_CLOSE_BRACE
:
3236 /* If this is a non-nested '}', stop before consuming it.
3237 That way, when confronted with something like:
3241 we stop before consuming the closing '}', even though we
3242 have not yet reached a `;'. */
3243 if (nesting_depth
== 0)
3246 /* If it is the closing '}' for a block that we have
3247 scanned, stop -- but only after consuming the token.
3253 we will stop after the body of the erroneously declared
3254 function, but before consuming the following `typedef'
3256 if (--nesting_depth
== 0)
3258 cp_lexer_consume_token (parser
->lexer
);
3262 case CPP_OPEN_BRACE
:
3270 /* Consume the token. */
3271 cp_lexer_consume_token (parser
->lexer
);
3275 /* This function is called at the end of a statement or declaration.
3276 If the next token is a semicolon, it is consumed; otherwise, error
3277 recovery is attempted. */
3280 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3282 /* Look for the trailing `;'. */
3283 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3285 /* If there is additional (erroneous) input, skip to the end of
3287 cp_parser_skip_to_end_of_statement (parser
);
3288 /* If the next token is now a `;', consume it. */
3289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3290 cp_lexer_consume_token (parser
->lexer
);
3294 /* Skip tokens until we have consumed an entire block, or until we
3295 have consumed a non-nested `;'. */
3298 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3300 int nesting_depth
= 0;
3302 /* Unwind generic function template scope if necessary. */
3303 if (parser
->fully_implicit_function_template_p
)
3304 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3306 while (nesting_depth
>= 0)
3308 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3310 switch (token
->type
)
3313 case CPP_PRAGMA_EOL
:
3314 /* If we've run out of tokens, stop. */
3318 /* Stop if this is an unnested ';'. */
3323 case CPP_CLOSE_BRACE
:
3324 /* Stop if this is an unnested '}', or closes the outermost
3327 if (nesting_depth
< 0)
3333 case CPP_OPEN_BRACE
:
3342 /* Consume the token. */
3343 cp_lexer_consume_token (parser
->lexer
);
3347 /* Skip tokens until a non-nested closing curly brace is the next
3348 token, or there are no more tokens. Return true in the first case,
3352 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3354 unsigned nesting_depth
= 0;
3358 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3360 switch (token
->type
)
3363 case CPP_PRAGMA_EOL
:
3364 /* If we've run out of tokens, stop. */
3367 case CPP_CLOSE_BRACE
:
3368 /* If the next token is a non-nested `}', then we have reached
3369 the end of the current block. */
3370 if (nesting_depth
-- == 0)
3374 case CPP_OPEN_BRACE
:
3375 /* If it the next token is a `{', then we are entering a new
3376 block. Consume the entire block. */
3384 /* Consume the token. */
3385 cp_lexer_consume_token (parser
->lexer
);
3389 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3390 parameter is the PRAGMA token, allowing us to purge the entire pragma
3394 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3398 parser
->lexer
->in_pragma
= false;
3401 token
= cp_lexer_consume_token (parser
->lexer
);
3402 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3404 /* Ensure that the pragma is not parsed again. */
3405 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3408 /* Require pragma end of line, resyncing with it as necessary. The
3409 arguments are as for cp_parser_skip_to_pragma_eol. */
3412 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3414 parser
->lexer
->in_pragma
= false;
3415 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3416 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3419 /* This is a simple wrapper around make_typename_type. When the id is
3420 an unresolved identifier node, we can provide a superior diagnostic
3421 using cp_parser_diagnose_invalid_type_name. */
3424 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3425 location_t id_location
)
3428 if (identifier_p (id
))
3430 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3431 /*complain=*/tf_none
);
3432 if (result
== error_mark_node
)
3433 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3436 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3439 /* This is a wrapper around the
3440 make_{pointer,ptrmem,reference}_declarator functions that decides
3441 which one to call based on the CODE and CLASS_TYPE arguments. The
3442 CODE argument should be one of the values returned by
3443 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3444 appertain to the pointer or reference. */
3446 static cp_declarator
*
3447 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3448 cp_cv_quals cv_qualifiers
,
3449 cp_declarator
*target
,
3452 if (code
== ERROR_MARK
)
3453 return cp_error_declarator
;
3455 if (code
== INDIRECT_REF
)
3456 if (class_type
== NULL_TREE
)
3457 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3459 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3460 target
, attributes
);
3461 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3462 return make_reference_declarator (cv_qualifiers
, target
,
3464 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3465 return make_reference_declarator (cv_qualifiers
, target
,
3470 /* Create a new C++ parser. */
3473 cp_parser_new (void)
3479 /* cp_lexer_new_main is called before doing GC allocation because
3480 cp_lexer_new_main might load a PCH file. */
3481 lexer
= cp_lexer_new_main ();
3483 /* Initialize the binops_by_token so that we can get the tree
3484 directly from the token. */
3485 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3486 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3488 parser
= ggc_cleared_alloc
<cp_parser
> ();
3489 parser
->lexer
= lexer
;
3490 parser
->context
= cp_parser_context_new (NULL
);
3492 /* For now, we always accept GNU extensions. */
3493 parser
->allow_gnu_extensions_p
= 1;
3495 /* The `>' token is a greater-than operator, not the end of a
3497 parser
->greater_than_is_operator_p
= true;
3499 parser
->default_arg_ok_p
= true;
3501 /* We are not parsing a constant-expression. */
3502 parser
->integral_constant_expression_p
= false;
3503 parser
->allow_non_integral_constant_expression_p
= false;
3504 parser
->non_integral_constant_expression_p
= false;
3506 /* Local variable names are not forbidden. */
3507 parser
->local_variables_forbidden_p
= false;
3509 /* We are not processing an `extern "C"' declaration. */
3510 parser
->in_unbraced_linkage_specification_p
= false;
3512 /* We are not processing a declarator. */
3513 parser
->in_declarator_p
= false;
3515 /* We are not processing a template-argument-list. */
3516 parser
->in_template_argument_list_p
= false;
3518 /* We are not in an iteration statement. */
3519 parser
->in_statement
= 0;
3521 /* We are not in a switch statement. */
3522 parser
->in_switch_statement_p
= false;
3524 /* We are not parsing a type-id inside an expression. */
3525 parser
->in_type_id_in_expr_p
= false;
3527 /* Declarations aren't implicitly extern "C". */
3528 parser
->implicit_extern_c
= false;
3530 /* String literals should be translated to the execution character set. */
3531 parser
->translate_strings_p
= true;
3533 /* We are not parsing a function body. */
3534 parser
->in_function_body
= false;
3536 /* We can correct until told otherwise. */
3537 parser
->colon_corrects_to_scope_p
= true;
3539 /* The unparsed function queue is empty. */
3540 push_unparsed_function_queues (parser
);
3542 /* There are no classes being defined. */
3543 parser
->num_classes_being_defined
= 0;
3545 /* No template parameters apply. */
3546 parser
->num_template_parameter_lists
= 0;
3548 /* Not declaring an implicit function template. */
3549 parser
->auto_is_implicit_function_template_parm_p
= false;
3550 parser
->fully_implicit_function_template_p
= false;
3551 parser
->implicit_template_parms
= 0;
3552 parser
->implicit_template_scope
= 0;
3557 /* Create a cp_lexer structure which will emit the tokens in CACHE
3558 and push it onto the parser's lexer stack. This is used for delayed
3559 parsing of in-class method bodies and default arguments, and should
3560 not be confused with tentative parsing. */
3562 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3564 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3565 lexer
->next
= parser
->lexer
;
3566 parser
->lexer
= lexer
;
3568 /* Move the current source position to that of the first token in the
3570 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3573 /* Pop the top lexer off the parser stack. This is never used for the
3574 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3576 cp_parser_pop_lexer (cp_parser
*parser
)
3578 cp_lexer
*lexer
= parser
->lexer
;
3579 parser
->lexer
= lexer
->next
;
3580 cp_lexer_destroy (lexer
);
3582 /* Put the current source position back where it was before this
3583 lexer was pushed. */
3584 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3587 /* Lexical conventions [gram.lex] */
3589 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3593 cp_parser_identifier (cp_parser
* parser
)
3597 /* Look for the identifier. */
3598 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3599 /* Return the value. */
3600 return token
? token
->u
.value
: error_mark_node
;
3603 /* Parse a sequence of adjacent string constants. Returns a
3604 TREE_STRING representing the combined, nul-terminated string
3605 constant. If TRANSLATE is true, translate the string to the
3606 execution character set. If WIDE_OK is true, a wide string is
3609 C++98 [lex.string] says that if a narrow string literal token is
3610 adjacent to a wide string literal token, the behavior is undefined.
3611 However, C99 6.4.5p4 says that this results in a wide string literal.
3612 We follow C99 here, for consistency with the C front end.
3614 This code is largely lifted from lex_string() in c-lex.c.
3616 FUTURE: ObjC++ will need to handle @-strings here. */
3618 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3619 bool lookup_udlit
= true)
3623 struct obstack str_ob
;
3624 cpp_string str
, istr
, *strs
;
3626 enum cpp_ttype type
, curr_type
;
3627 int have_suffix_p
= 0;
3629 tree suffix_id
= NULL_TREE
;
3630 bool curr_tok_is_userdef_p
= false;
3632 tok
= cp_lexer_peek_token (parser
->lexer
);
3633 if (!cp_parser_is_string_literal (tok
))
3635 cp_parser_error (parser
, "expected string-literal");
3636 return error_mark_node
;
3639 if (cpp_userdef_string_p (tok
->type
))
3641 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3642 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3643 curr_tok_is_userdef_p
= true;
3647 string_tree
= tok
->u
.value
;
3648 curr_type
= tok
->type
;
3652 /* Try to avoid the overhead of creating and destroying an obstack
3653 for the common case of just one string. */
3654 if (!cp_parser_is_string_literal
3655 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3657 cp_lexer_consume_token (parser
->lexer
);
3659 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3660 str
.len
= TREE_STRING_LENGTH (string_tree
);
3663 if (curr_tok_is_userdef_p
)
3665 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3667 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3670 curr_type
= tok
->type
;
3676 gcc_obstack_init (&str_ob
);
3681 cp_lexer_consume_token (parser
->lexer
);
3683 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3684 str
.len
= TREE_STRING_LENGTH (string_tree
);
3686 if (curr_tok_is_userdef_p
)
3688 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3689 if (have_suffix_p
== 0)
3691 suffix_id
= curr_suffix_id
;
3694 else if (have_suffix_p
== 1
3695 && curr_suffix_id
!= suffix_id
)
3697 error ("inconsistent user-defined literal suffixes"
3698 " %qD and %qD in string literal",
3699 suffix_id
, curr_suffix_id
);
3702 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3705 curr_type
= tok
->type
;
3707 if (type
!= curr_type
)
3709 if (type
== CPP_STRING
)
3711 else if (curr_type
!= CPP_STRING
)
3712 error_at (tok
->location
,
3713 "unsupported non-standard concatenation "
3714 "of string literals");
3717 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3719 tok
= cp_lexer_peek_token (parser
->lexer
);
3720 if (cpp_userdef_string_p (tok
->type
))
3722 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3723 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3724 curr_tok_is_userdef_p
= true;
3728 string_tree
= tok
->u
.value
;
3729 curr_type
= tok
->type
;
3730 curr_tok_is_userdef_p
= false;
3733 while (cp_parser_is_string_literal (tok
));
3735 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3738 if (type
!= CPP_STRING
&& !wide_ok
)
3740 cp_parser_error (parser
, "a wide string is invalid in this context");
3744 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3745 (parse_in
, strs
, count
, &istr
, type
))
3747 value
= build_string (istr
.len
, (const char *)istr
.text
);
3748 free (CONST_CAST (unsigned char *, istr
.text
));
3754 case CPP_UTF8STRING
:
3755 TREE_TYPE (value
) = char_array_type_node
;
3758 TREE_TYPE (value
) = char16_array_type_node
;
3761 TREE_TYPE (value
) = char32_array_type_node
;
3764 TREE_TYPE (value
) = wchar_array_type_node
;
3768 value
= fix_string_type (value
);
3772 tree literal
= build_userdef_literal (suffix_id
, value
,
3773 OT_NONE
, NULL_TREE
);
3775 value
= cp_parser_userdef_string_literal (literal
);
3781 /* cpp_interpret_string has issued an error. */
3782 value
= error_mark_node
;
3785 obstack_free (&str_ob
, 0);
3790 /* Look up a literal operator with the name and the exact arguments. */
3793 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3796 decl
= lookup_name (name
);
3797 if (!decl
|| !is_overloaded_fn (decl
))
3798 return error_mark_node
;
3800 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3804 tree fn
= OVL_CURRENT (fns
);
3805 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3806 if (parmtypes
!= NULL_TREE
)
3808 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3809 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3811 tree tparm
= TREE_VALUE (parmtypes
);
3812 tree targ
= TREE_TYPE ((*args
)[ix
]);
3813 bool ptr
= TYPE_PTR_P (tparm
);
3814 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3815 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3817 || !same_type_p (TREE_TYPE (tparm
),
3822 && ix
== vec_safe_length (args
)
3823 /* May be this should be sufficient_parms_p instead,
3824 depending on how exactly should user-defined literals
3825 work in presence of default arguments on the literal
3826 operator parameters. */
3827 && parmtypes
== void_list_node
)
3832 return error_mark_node
;
3835 /* Parse a user-defined char constant. Returns a call to a user-defined
3836 literal operator taking the character as an argument. */
3839 cp_parser_userdef_char_literal (cp_parser
*parser
)
3841 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3842 tree literal
= token
->u
.value
;
3843 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3844 tree value
= USERDEF_LITERAL_VALUE (literal
);
3845 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3848 /* Build up a call to the user-defined operator */
3849 /* Lookup the name we got back from the id-expression. */
3850 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3851 vec_safe_push (args
, value
);
3852 decl
= lookup_literal_operator (name
, args
);
3853 if (!decl
|| decl
== error_mark_node
)
3855 error ("unable to find character literal operator %qD with %qT argument",
3856 name
, TREE_TYPE (value
));
3857 release_tree_vector (args
);
3858 return error_mark_node
;
3860 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3861 release_tree_vector (args
);
3865 /* A subroutine of cp_parser_userdef_numeric_literal to
3866 create a char... template parameter pack from a string node. */
3869 make_char_string_pack (tree value
)
3872 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3873 const char *str
= TREE_STRING_POINTER (value
);
3874 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3875 tree argvec
= make_tree_vec (1);
3877 /* Fill in CHARVEC with all of the parameters. */
3878 charvec
= make_tree_vec (len
);
3879 for (i
= 0; i
< len
; ++i
)
3880 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3882 /* Build the argument packs. */
3883 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3884 TREE_TYPE (argpack
) = char_type_node
;
3886 TREE_VEC_ELT (argvec
, 0) = argpack
;
3891 /* A subroutine of cp_parser_userdef_numeric_literal to
3892 create a char... template parameter pack from a string node. */
3895 make_string_pack (tree value
)
3898 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3899 const unsigned char *str
3900 = (const unsigned char *) TREE_STRING_POINTER (value
);
3901 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3902 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3903 tree argvec
= make_tree_vec (2);
3905 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3906 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3908 /* First template parm is character type. */
3909 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3911 /* Fill in CHARVEC with all of the parameters. */
3912 charvec
= make_tree_vec (len
);
3913 for (int i
= 0; i
< len
; ++i
)
3914 TREE_VEC_ELT (charvec
, i
)
3915 = double_int_to_tree (str_char_type_node
,
3916 double_int::from_buffer (str
+ i
* sz
, sz
));
3918 /* Build the argument packs. */
3919 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3920 TREE_TYPE (argpack
) = str_char_type_node
;
3922 TREE_VEC_ELT (argvec
, 1) = argpack
;
3927 /* Parse a user-defined numeric constant. returns a call to a user-defined
3928 literal operator. */
3931 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3933 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3934 tree literal
= token
->u
.value
;
3935 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3936 tree value
= USERDEF_LITERAL_VALUE (literal
);
3937 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3938 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3939 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3941 vec
<tree
, va_gc
> *args
;
3943 /* Look for a literal operator taking the exact type of numeric argument
3944 as the literal value. */
3945 args
= make_tree_vector ();
3946 vec_safe_push (args
, value
);
3947 decl
= lookup_literal_operator (name
, args
);
3948 if (decl
&& decl
!= error_mark_node
)
3950 result
= finish_call_expr (decl
, &args
, false, true,
3951 tf_warning_or_error
);
3953 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3955 warning_at (token
->location
, OPT_Woverflow
,
3956 "integer literal exceeds range of %qT type",
3957 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");
3970 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,
3984 tf_warning_or_error
);
3985 release_tree_vector (args
);
3988 release_tree_vector (args
);
3990 /* If the raw literal didn't work, look for a non-type template
3991 function with parameter pack char.... Call the function with
3992 template parameter characters representing the number. */
3993 args
= make_tree_vector ();
3994 decl
= lookup_literal_operator (name
, args
);
3995 if (decl
&& decl
!= error_mark_node
)
3997 tree tmpl_args
= make_char_string_pack (num_string
);
3998 decl
= lookup_template_function (decl
, tmpl_args
);
3999 result
= finish_call_expr (decl
, &args
, false, true,
4000 tf_warning_or_error
);
4001 release_tree_vector (args
);
4005 release_tree_vector (args
);
4007 error ("unable to find numeric literal operator %qD", name
);
4008 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4009 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4010 "to enable more built-in suffixes");
4011 return error_mark_node
;
4014 /* Parse a user-defined string constant. Returns a call to a user-defined
4015 literal operator taking a character pointer and the length of the string
4019 cp_parser_userdef_string_literal (tree literal
)
4021 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4022 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4023 tree value
= USERDEF_LITERAL_VALUE (literal
);
4024 int len
= TREE_STRING_LENGTH (value
)
4025 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4027 vec
<tree
, va_gc
> *args
;
4029 /* Build up a call to the user-defined operator. */
4030 /* Lookup the name we got back from the id-expression. */
4031 args
= make_tree_vector ();
4032 vec_safe_push (args
, value
);
4033 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4034 decl
= lookup_literal_operator (name
, args
);
4036 if (decl
&& decl
!= error_mark_node
)
4038 result
= finish_call_expr (decl
, &args
, false, true,
4039 tf_warning_or_error
);
4040 release_tree_vector (args
);
4043 release_tree_vector (args
);
4045 /* Look for a template function with typename parameter CharT
4046 and parameter pack CharT... Call the function with
4047 template parameter characters representing the string. */
4048 args
= make_tree_vector ();
4049 decl
= lookup_literal_operator (name
, args
);
4050 if (decl
&& decl
!= error_mark_node
)
4052 tree tmpl_args
= make_string_pack (value
);
4053 decl
= lookup_template_function (decl
, tmpl_args
);
4054 result
= finish_call_expr (decl
, &args
, false, true,
4055 tf_warning_or_error
);
4056 release_tree_vector (args
);
4059 release_tree_vector (args
);
4061 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4062 name
, TREE_TYPE (value
), size_type_node
);
4063 return error_mark_node
;
4067 /* Basic concepts [gram.basic] */
4069 /* Parse a translation-unit.
4072 declaration-seq [opt]
4074 Returns TRUE if all went well. */
4077 cp_parser_translation_unit (cp_parser
* parser
)
4079 /* The address of the first non-permanent object on the declarator
4081 static void *declarator_obstack_base
;
4085 /* Create the declarator obstack, if necessary. */
4086 if (!cp_error_declarator
)
4088 gcc_obstack_init (&declarator_obstack
);
4089 /* Create the error declarator. */
4090 cp_error_declarator
= make_declarator (cdk_error
);
4091 /* Create the empty parameter list. */
4092 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4093 /* Remember where the base of the declarator obstack lies. */
4094 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4097 cp_parser_declaration_seq_opt (parser
);
4099 /* If there are no tokens left then all went well. */
4100 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4102 /* Get rid of the token array; we don't need it any more. */
4103 cp_lexer_destroy (parser
->lexer
);
4104 parser
->lexer
= NULL
;
4106 /* This file might have been a context that's implicitly extern
4107 "C". If so, pop the lang context. (Only relevant for PCH.) */
4108 if (parser
->implicit_extern_c
)
4110 pop_lang_context ();
4111 parser
->implicit_extern_c
= false;
4115 finish_translation_unit ();
4121 cp_parser_error (parser
, "expected declaration");
4125 /* Make sure the declarator obstack was fully cleaned up. */
4126 gcc_assert (obstack_next_free (&declarator_obstack
)
4127 == declarator_obstack_base
);
4129 /* All went well. */
4133 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4134 decltype context. */
4136 static inline tsubst_flags_t
4137 complain_flags (bool decltype_p
)
4139 tsubst_flags_t complain
= tf_warning_or_error
;
4141 complain
|= tf_decltype
;
4145 /* We're about to parse a collection of statements. If we're currently
4146 parsing tentatively, set up a firewall so that any nested
4147 cp_parser_commit_to_tentative_parse won't affect the current context. */
4149 static cp_token_position
4150 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4152 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4155 cp_parser_parse_tentatively (parser
);
4156 cp_parser_commit_to_topmost_tentative_parse (parser
);
4157 return cp_lexer_token_position (parser
->lexer
, false);
4160 /* We've finished parsing the collection of statements. Wrap up the
4161 firewall and replace the relevant tokens with the parsed form. */
4164 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4170 /* Finish the firewall level. */
4171 cp_parser_parse_definitely (parser
);
4172 /* And remember the result of the parse for when we try again. */
4173 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4174 token
->type
= CPP_PREPARSED_EXPR
;
4175 token
->u
.value
= expr
;
4176 token
->keyword
= RID_MAX
;
4177 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4180 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4181 enclosing parentheses. */
4184 cp_parser_statement_expr (cp_parser
*parser
)
4186 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4188 /* Consume the '('. */
4189 cp_lexer_consume_token (parser
->lexer
);
4190 /* Start the statement-expression. */
4191 tree expr
= begin_stmt_expr ();
4192 /* Parse the compound-statement. */
4193 cp_parser_compound_statement (parser
, expr
, false, false);
4195 expr
= finish_stmt_expr (expr
, false);
4196 /* Consume the ')'. */
4197 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4198 cp_parser_skip_to_end_of_statement (parser
);
4200 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4204 /* Expressions [gram.expr] */
4206 /* Parse a primary-expression.
4213 lambda-expression (C++11)
4218 ( compound-statement )
4219 __builtin_va_arg ( assignment-expression , type-id )
4220 __builtin_offsetof ( type-id , offsetof-expression )
4223 __has_nothrow_assign ( type-id )
4224 __has_nothrow_constructor ( type-id )
4225 __has_nothrow_copy ( type-id )
4226 __has_trivial_assign ( type-id )
4227 __has_trivial_constructor ( type-id )
4228 __has_trivial_copy ( type-id )
4229 __has_trivial_destructor ( type-id )
4230 __has_virtual_destructor ( type-id )
4231 __is_abstract ( type-id )
4232 __is_base_of ( type-id , type-id )
4233 __is_class ( type-id )
4234 __is_empty ( type-id )
4235 __is_enum ( type-id )
4236 __is_final ( type-id )
4237 __is_literal_type ( type-id )
4238 __is_pod ( type-id )
4239 __is_polymorphic ( type-id )
4240 __is_std_layout ( type-id )
4241 __is_trivial ( type-id )
4242 __is_union ( type-id )
4244 Objective-C++ Extension:
4252 ADDRESS_P is true iff this expression was immediately preceded by
4253 "&" and therefore might denote a pointer-to-member. CAST_P is true
4254 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4255 true iff this expression is a template argument.
4257 Returns a representation of the expression. Upon return, *IDK
4258 indicates what kind of id-expression (if any) was present. */
4261 cp_parser_primary_expression (cp_parser
*parser
,
4264 bool template_arg_p
,
4268 cp_token
*token
= NULL
;
4270 /* Assume the primary expression is not an id-expression. */
4271 *idk
= CP_ID_KIND_NONE
;
4273 /* Peek at the next token. */
4274 token
= cp_lexer_peek_token (parser
->lexer
);
4275 switch ((int) token
->type
)
4284 user-defined-literal */
4291 case CPP_PREPARSED_EXPR
:
4292 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4293 return cp_parser_userdef_numeric_literal (parser
);
4294 token
= cp_lexer_consume_token (parser
->lexer
);
4295 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4297 error_at (token
->location
,
4298 "fixed-point types not supported in C++");
4299 return error_mark_node
;
4301 /* Floating-point literals are only allowed in an integral
4302 constant expression if they are cast to an integral or
4303 enumeration type. */
4304 if (TREE_CODE (token
->u
.value
) == REAL_CST
4305 && parser
->integral_constant_expression_p
4308 /* CAST_P will be set even in invalid code like "int(2.7 +
4309 ...)". Therefore, we have to check that the next token
4310 is sure to end the cast. */
4313 cp_token
*next_token
;
4315 next_token
= cp_lexer_peek_token (parser
->lexer
);
4316 if (/* The comma at the end of an
4317 enumerator-definition. */
4318 next_token
->type
!= CPP_COMMA
4319 /* The curly brace at the end of an enum-specifier. */
4320 && next_token
->type
!= CPP_CLOSE_BRACE
4321 /* The end of a statement. */
4322 && next_token
->type
!= CPP_SEMICOLON
4323 /* The end of the cast-expression. */
4324 && next_token
->type
!= CPP_CLOSE_PAREN
4325 /* The end of an array bound. */
4326 && next_token
->type
!= CPP_CLOSE_SQUARE
4327 /* The closing ">" in a template-argument-list. */
4328 && (next_token
->type
!= CPP_GREATER
4329 || parser
->greater_than_is_operator_p
)
4330 /* C++0x only: A ">>" treated like two ">" tokens,
4331 in a template-argument-list. */
4332 && (next_token
->type
!= CPP_RSHIFT
4333 || (cxx_dialect
== cxx98
)
4334 || parser
->greater_than_is_operator_p
))
4338 /* If we are within a cast, then the constraint that the
4339 cast is to an integral or enumeration type will be
4340 checked at that point. If we are not within a cast, then
4341 this code is invalid. */
4343 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4345 return token
->u
.value
;
4347 case CPP_CHAR_USERDEF
:
4348 case CPP_CHAR16_USERDEF
:
4349 case CPP_CHAR32_USERDEF
:
4350 case CPP_WCHAR_USERDEF
:
4351 case CPP_UTF8CHAR_USERDEF
:
4352 return cp_parser_userdef_char_literal (parser
);
4358 case CPP_UTF8STRING
:
4359 case CPP_STRING_USERDEF
:
4360 case CPP_STRING16_USERDEF
:
4361 case CPP_STRING32_USERDEF
:
4362 case CPP_WSTRING_USERDEF
:
4363 case CPP_UTF8STRING_USERDEF
:
4364 /* ??? Should wide strings be allowed when parser->translate_strings_p
4365 is false (i.e. in attributes)? If not, we can kill the third
4366 argument to cp_parser_string_literal. */
4367 return cp_parser_string_literal (parser
,
4368 parser
->translate_strings_p
,
4371 case CPP_OPEN_PAREN
:
4372 /* If we see `( { ' then we are looking at the beginning of
4373 a GNU statement-expression. */
4374 if (cp_parser_allow_gnu_extensions_p (parser
)
4375 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4377 /* Statement-expressions are not allowed by the standard. */
4378 pedwarn (token
->location
, OPT_Wpedantic
,
4379 "ISO C++ forbids braced-groups within expressions");
4381 /* And they're not allowed outside of a function-body; you
4382 cannot, for example, write:
4384 int i = ({ int j = 3; j + 1; });
4386 at class or namespace scope. */
4387 if (!parser
->in_function_body
4388 || parser
->in_template_argument_list_p
)
4390 error_at (token
->location
,
4391 "statement-expressions are not allowed outside "
4392 "functions nor in template-argument lists");
4393 cp_parser_skip_to_end_of_block_or_statement (parser
);
4394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4395 cp_lexer_consume_token (parser
->lexer
);
4396 return error_mark_node
;
4399 return cp_parser_statement_expr (parser
);
4401 /* Otherwise it's a normal parenthesized expression. */
4404 bool saved_greater_than_is_operator_p
;
4406 /* Consume the `('. */
4407 cp_lexer_consume_token (parser
->lexer
);
4408 /* Within a parenthesized expression, a `>' token is always
4409 the greater-than operator. */
4410 saved_greater_than_is_operator_p
4411 = parser
->greater_than_is_operator_p
;
4412 parser
->greater_than_is_operator_p
= true;
4414 /* Parse the parenthesized expression. */
4415 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4416 /* Let the front end know that this expression was
4417 enclosed in parentheses. This matters in case, for
4418 example, the expression is of the form `A::B', since
4419 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4421 expr
= finish_parenthesized_expr (expr
);
4422 /* DR 705: Wrapping an unqualified name in parentheses
4423 suppresses arg-dependent lookup. We want to pass back
4424 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4425 (c++/37862), but none of the others. */
4426 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4427 *idk
= CP_ID_KIND_NONE
;
4429 /* The `>' token might be the end of a template-id or
4430 template-parameter-list now. */
4431 parser
->greater_than_is_operator_p
4432 = saved_greater_than_is_operator_p
;
4433 /* Consume the `)'. */
4434 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4435 cp_parser_skip_to_end_of_statement (parser
);
4440 case CPP_OPEN_SQUARE
:
4442 if (c_dialect_objc ())
4444 /* We might have an Objective-C++ message. */
4445 cp_parser_parse_tentatively (parser
);
4446 tree msg
= cp_parser_objc_message_expression (parser
);
4447 /* If that works out, we're done ... */
4448 if (cp_parser_parse_definitely (parser
))
4450 /* ... else, fall though to see if it's a lambda. */
4452 tree lam
= cp_parser_lambda_expression (parser
);
4453 /* Don't warn about a failed tentative parse. */
4454 if (cp_parser_error_occurred (parser
))
4455 return error_mark_node
;
4456 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4460 case CPP_OBJC_STRING
:
4461 if (c_dialect_objc ())
4462 /* We have an Objective-C++ string literal. */
4463 return cp_parser_objc_expression (parser
);
4464 cp_parser_error (parser
, "expected primary-expression");
4465 return error_mark_node
;
4468 switch (token
->keyword
)
4470 /* These two are the boolean literals. */
4472 cp_lexer_consume_token (parser
->lexer
);
4473 return boolean_true_node
;
4475 cp_lexer_consume_token (parser
->lexer
);
4476 return boolean_false_node
;
4478 /* The `__null' literal. */
4480 cp_lexer_consume_token (parser
->lexer
);
4483 /* The `nullptr' literal. */
4485 cp_lexer_consume_token (parser
->lexer
);
4486 return nullptr_node
;
4488 /* Recognize the `this' keyword. */
4490 cp_lexer_consume_token (parser
->lexer
);
4491 if (parser
->local_variables_forbidden_p
)
4493 error_at (token
->location
,
4494 "%<this%> may not be used in this context");
4495 return error_mark_node
;
4497 /* Pointers cannot appear in constant-expressions. */
4498 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4499 return error_mark_node
;
4500 return finish_this_expr ();
4502 /* The `operator' keyword can be the beginning of an
4507 case RID_FUNCTION_NAME
:
4508 case RID_PRETTY_FUNCTION_NAME
:
4509 case RID_C99_FUNCTION_NAME
:
4511 non_integral_constant name
;
4513 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4514 __func__ are the names of variables -- but they are
4515 treated specially. Therefore, they are handled here,
4516 rather than relying on the generic id-expression logic
4517 below. Grammatically, these names are id-expressions.
4519 Consume the token. */
4520 token
= cp_lexer_consume_token (parser
->lexer
);
4522 switch (token
->keyword
)
4524 case RID_FUNCTION_NAME
:
4525 name
= NIC_FUNC_NAME
;
4527 case RID_PRETTY_FUNCTION_NAME
:
4528 name
= NIC_PRETTY_FUNC
;
4530 case RID_C99_FUNCTION_NAME
:
4531 name
= NIC_C99_FUNC
;
4537 if (cp_parser_non_integral_constant_expression (parser
, name
))
4538 return error_mark_node
;
4540 /* Look up the name. */
4541 return finish_fname (token
->u
.value
);
4548 source_location type_location
;
4550 /* The `__builtin_va_arg' construct is used to handle
4551 `va_arg'. Consume the `__builtin_va_arg' token. */
4552 cp_lexer_consume_token (parser
->lexer
);
4553 /* Look for the opening `('. */
4554 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4555 /* Now, parse the assignment-expression. */
4556 expression
= cp_parser_assignment_expression (parser
);
4557 /* Look for the `,'. */
4558 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4559 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4560 /* Parse the type-id. */
4561 type
= cp_parser_type_id (parser
);
4562 /* Look for the closing `)'. */
4563 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4564 /* Using `va_arg' in a constant-expression is not
4566 if (cp_parser_non_integral_constant_expression (parser
,
4568 return error_mark_node
;
4569 return build_x_va_arg (type_location
, expression
, type
);
4573 return cp_parser_builtin_offsetof (parser
);
4575 case RID_HAS_NOTHROW_ASSIGN
:
4576 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4577 case RID_HAS_NOTHROW_COPY
:
4578 case RID_HAS_TRIVIAL_ASSIGN
:
4579 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4580 case RID_HAS_TRIVIAL_COPY
:
4581 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4582 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4583 case RID_IS_ABSTRACT
:
4584 case RID_IS_BASE_OF
:
4589 case RID_IS_LITERAL_TYPE
:
4591 case RID_IS_POLYMORPHIC
:
4592 case RID_IS_STD_LAYOUT
:
4593 case RID_IS_TRIVIAL
:
4594 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4595 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4596 case RID_IS_TRIVIALLY_COPYABLE
:
4598 return cp_parser_trait_expr (parser
, token
->keyword
);
4600 /* Objective-C++ expressions. */
4602 case RID_AT_PROTOCOL
:
4603 case RID_AT_SELECTOR
:
4604 return cp_parser_objc_expression (parser
);
4607 if (parser
->in_function_body
4608 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4611 error_at (token
->location
,
4612 "a template declaration cannot appear at block scope");
4613 cp_parser_skip_to_end_of_block_or_statement (parser
);
4614 return error_mark_node
;
4617 cp_parser_error (parser
, "expected primary-expression");
4618 return error_mark_node
;
4621 /* An id-expression can start with either an identifier, a
4622 `::' as the beginning of a qualified-id, or the "operator"
4626 case CPP_TEMPLATE_ID
:
4627 case CPP_NESTED_NAME_SPECIFIER
:
4631 const char *error_msg
;
4634 cp_token
*id_expr_token
;
4637 /* Parse the id-expression. */
4639 = cp_parser_id_expression (parser
,
4640 /*template_keyword_p=*/false,
4641 /*check_dependency_p=*/true,
4643 /*declarator_p=*/false,
4644 /*optional_p=*/false);
4645 if (id_expression
== error_mark_node
)
4646 return error_mark_node
;
4647 id_expr_token
= token
;
4648 token
= cp_lexer_peek_token (parser
->lexer
);
4649 done
= (token
->type
!= CPP_OPEN_SQUARE
4650 && token
->type
!= CPP_OPEN_PAREN
4651 && token
->type
!= CPP_DOT
4652 && token
->type
!= CPP_DEREF
4653 && token
->type
!= CPP_PLUS_PLUS
4654 && token
->type
!= CPP_MINUS_MINUS
);
4655 /* If we have a template-id, then no further lookup is
4656 required. If the template-id was for a template-class, we
4657 will sometimes have a TYPE_DECL at this point. */
4658 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4659 || TREE_CODE (id_expression
) == TYPE_DECL
)
4660 decl
= id_expression
;
4661 /* Look up the name. */
4664 tree ambiguous_decls
;
4666 /* If we already know that this lookup is ambiguous, then
4667 we've already issued an error message; there's no reason
4669 if (id_expr_token
->type
== CPP_NAME
4670 && id_expr_token
->error_reported
)
4672 cp_parser_simulate_error (parser
);
4673 return error_mark_node
;
4676 decl
= cp_parser_lookup_name (parser
, id_expression
,
4679 /*is_namespace=*/false,
4680 /*check_dependency=*/true,
4682 id_expr_token
->location
);
4683 /* If the lookup was ambiguous, an error will already have
4685 if (ambiguous_decls
)
4686 return error_mark_node
;
4688 /* In Objective-C++, we may have an Objective-C 2.0
4689 dot-syntax for classes here. */
4690 if (c_dialect_objc ()
4691 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4692 && TREE_CODE (decl
) == TYPE_DECL
4693 && objc_is_class_name (decl
))
4696 cp_lexer_consume_token (parser
->lexer
);
4697 component
= cp_parser_identifier (parser
);
4698 if (component
== error_mark_node
)
4699 return error_mark_node
;
4701 return objc_build_class_component_ref (id_expression
, component
);
4704 /* In Objective-C++, an instance variable (ivar) may be preferred
4705 to whatever cp_parser_lookup_name() found. */
4706 decl
= objc_lookup_ivar (decl
, id_expression
);
4708 /* If name lookup gives us a SCOPE_REF, then the
4709 qualifying scope was dependent. */
4710 if (TREE_CODE (decl
) == SCOPE_REF
)
4712 /* At this point, we do not know if DECL is a valid
4713 integral constant expression. We assume that it is
4714 in fact such an expression, so that code like:
4716 template <int N> struct A {
4720 is accepted. At template-instantiation time, we
4721 will check that B<N>::i is actually a constant. */
4724 /* Check to see if DECL is a local variable in a context
4725 where that is forbidden. */
4726 if (parser
->local_variables_forbidden_p
4727 && local_variable_p (decl
))
4729 /* It might be that we only found DECL because we are
4730 trying to be generous with pre-ISO scoping rules.
4731 For example, consider:
4735 for (int i = 0; i < 10; ++i) {}
4736 extern void f(int j = i);
4739 Here, name look up will originally find the out
4740 of scope `i'. We need to issue a warning message,
4741 but then use the global `i'. */
4742 decl
= check_for_out_of_scope_variable (decl
);
4743 if (local_variable_p (decl
))
4745 error_at (id_expr_token
->location
,
4746 "local variable %qD may not appear in this context",
4748 return error_mark_node
;
4753 decl
= (finish_id_expression
4754 (id_expression
, decl
, parser
->scope
,
4756 parser
->integral_constant_expression_p
,
4757 parser
->allow_non_integral_constant_expression_p
,
4758 &parser
->non_integral_constant_expression_p
,
4759 template_p
, done
, address_p
,
4762 id_expr_token
->location
));
4764 cp_parser_error (parser
, error_msg
);
4768 /* Anything else is an error. */
4770 cp_parser_error (parser
, "expected primary-expression");
4771 return error_mark_node
;
4776 cp_parser_primary_expression (cp_parser
*parser
,
4779 bool template_arg_p
,
4782 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4783 /*decltype*/false, idk
);
4786 /* Parse an id-expression.
4793 :: [opt] nested-name-specifier template [opt] unqualified-id
4795 :: operator-function-id
4798 Return a representation of the unqualified portion of the
4799 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4800 a `::' or nested-name-specifier.
4802 Often, if the id-expression was a qualified-id, the caller will
4803 want to make a SCOPE_REF to represent the qualified-id. This
4804 function does not do this in order to avoid wastefully creating
4805 SCOPE_REFs when they are not required.
4807 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4810 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4811 uninstantiated templates.
4813 If *TEMPLATE_P is non-NULL, it is set to true iff the
4814 `template' keyword is used to explicitly indicate that the entity
4815 named is a template.
4817 If DECLARATOR_P is true, the id-expression is appearing as part of
4818 a declarator, rather than as part of an expression. */
4821 cp_parser_id_expression (cp_parser
*parser
,
4822 bool template_keyword_p
,
4823 bool check_dependency_p
,
4828 bool global_scope_p
;
4829 bool nested_name_specifier_p
;
4831 /* Assume the `template' keyword was not used. */
4833 *template_p
= template_keyword_p
;
4835 /* Look for the optional `::' operator. */
4837 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4839 /* Look for the optional nested-name-specifier. */
4840 nested_name_specifier_p
4841 = (cp_parser_nested_name_specifier_opt (parser
,
4842 /*typename_keyword_p=*/false,
4847 /* If there is a nested-name-specifier, then we are looking at
4848 the first qualified-id production. */
4849 if (nested_name_specifier_p
)
4852 tree saved_object_scope
;
4853 tree saved_qualifying_scope
;
4854 tree unqualified_id
;
4857 /* See if the next token is the `template' keyword. */
4859 template_p
= &is_template
;
4860 *template_p
= cp_parser_optional_template_keyword (parser
);
4861 /* Name lookup we do during the processing of the
4862 unqualified-id might obliterate SCOPE. */
4863 saved_scope
= parser
->scope
;
4864 saved_object_scope
= parser
->object_scope
;
4865 saved_qualifying_scope
= parser
->qualifying_scope
;
4866 /* Process the final unqualified-id. */
4867 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4870 /*optional_p=*/false);
4871 /* Restore the SAVED_SCOPE for our caller. */
4872 parser
->scope
= saved_scope
;
4873 parser
->object_scope
= saved_object_scope
;
4874 parser
->qualifying_scope
= saved_qualifying_scope
;
4876 return unqualified_id
;
4878 /* Otherwise, if we are in global scope, then we are looking at one
4879 of the other qualified-id productions. */
4880 else if (global_scope_p
)
4885 /* Peek at the next token. */
4886 token
= cp_lexer_peek_token (parser
->lexer
);
4888 /* If it's an identifier, and the next token is not a "<", then
4889 we can avoid the template-id case. This is an optimization
4890 for this common case. */
4891 if (token
->type
== CPP_NAME
4892 && !cp_parser_nth_token_starts_template_argument_list_p
4894 return cp_parser_identifier (parser
);
4896 cp_parser_parse_tentatively (parser
);
4897 /* Try a template-id. */
4898 id
= cp_parser_template_id (parser
,
4899 /*template_keyword_p=*/false,
4900 /*check_dependency_p=*/true,
4903 /* If that worked, we're done. */
4904 if (cp_parser_parse_definitely (parser
))
4907 /* Peek at the next token. (Changes in the token buffer may
4908 have invalidated the pointer obtained above.) */
4909 token
= cp_lexer_peek_token (parser
->lexer
);
4911 switch (token
->type
)
4914 return cp_parser_identifier (parser
);
4917 if (token
->keyword
== RID_OPERATOR
)
4918 return cp_parser_operator_function_id (parser
);
4922 cp_parser_error (parser
, "expected id-expression");
4923 return error_mark_node
;
4927 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4928 /*check_dependency_p=*/true,
4933 /* Parse an unqualified-id.
4937 operator-function-id
4938 conversion-function-id
4942 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4943 keyword, in a construct like `A::template ...'.
4945 Returns a representation of unqualified-id. For the `identifier'
4946 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4947 production a BIT_NOT_EXPR is returned; the operand of the
4948 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4949 other productions, see the documentation accompanying the
4950 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4951 names are looked up in uninstantiated templates. If DECLARATOR_P
4952 is true, the unqualified-id is appearing as part of a declarator,
4953 rather than as part of an expression. */
4956 cp_parser_unqualified_id (cp_parser
* parser
,
4957 bool template_keyword_p
,
4958 bool check_dependency_p
,
4964 /* Peek at the next token. */
4965 token
= cp_lexer_peek_token (parser
->lexer
);
4967 switch ((int) token
->type
)
4973 /* We don't know yet whether or not this will be a
4975 cp_parser_parse_tentatively (parser
);
4976 /* Try a template-id. */
4977 id
= cp_parser_template_id (parser
, template_keyword_p
,
4981 /* If it worked, we're done. */
4982 if (cp_parser_parse_definitely (parser
))
4984 /* Otherwise, it's an ordinary identifier. */
4985 return cp_parser_identifier (parser
);
4988 case CPP_TEMPLATE_ID
:
4989 return cp_parser_template_id (parser
, template_keyword_p
,
4997 tree qualifying_scope
;
5002 /* Consume the `~' token. */
5003 cp_lexer_consume_token (parser
->lexer
);
5004 /* Parse the class-name. The standard, as written, seems to
5007 template <typename T> struct S { ~S (); };
5008 template <typename T> S<T>::~S() {}
5010 is invalid, since `~' must be followed by a class-name, but
5011 `S<T>' is dependent, and so not known to be a class.
5012 That's not right; we need to look in uninstantiated
5013 templates. A further complication arises from:
5015 template <typename T> void f(T t) {
5019 Here, it is not possible to look up `T' in the scope of `T'
5020 itself. We must look in both the current scope, and the
5021 scope of the containing complete expression.
5023 Yet another issue is:
5032 The standard does not seem to say that the `S' in `~S'
5033 should refer to the type `S' and not the data member
5036 /* DR 244 says that we look up the name after the "~" in the
5037 same scope as we looked up the qualifying name. That idea
5038 isn't fully worked out; it's more complicated than that. */
5039 scope
= parser
->scope
;
5040 object_scope
= parser
->object_scope
;
5041 qualifying_scope
= parser
->qualifying_scope
;
5043 /* Check for invalid scopes. */
5044 if (scope
== error_mark_node
)
5046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5047 cp_lexer_consume_token (parser
->lexer
);
5048 return error_mark_node
;
5050 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5052 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5053 error_at (token
->location
,
5054 "scope %qT before %<~%> is not a class-name",
5056 cp_parser_simulate_error (parser
);
5057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5058 cp_lexer_consume_token (parser
->lexer
);
5059 return error_mark_node
;
5061 gcc_assert (!scope
|| TYPE_P (scope
));
5063 /* If the name is of the form "X::~X" it's OK even if X is a
5065 token
= cp_lexer_peek_token (parser
->lexer
);
5067 && token
->type
== CPP_NAME
5068 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5070 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5071 || (CLASS_TYPE_P (scope
)
5072 && constructor_name_p (token
->u
.value
, scope
))))
5074 cp_lexer_consume_token (parser
->lexer
);
5075 return build_nt (BIT_NOT_EXPR
, scope
);
5078 /* ~auto means the destructor of whatever the object is. */
5079 if (cp_parser_is_keyword (token
, RID_AUTO
))
5081 if (cxx_dialect
< cxx14
)
5082 pedwarn (input_location
, 0,
5083 "%<~auto%> only available with "
5084 "-std=c++14 or -std=gnu++14");
5085 cp_lexer_consume_token (parser
->lexer
);
5086 return build_nt (BIT_NOT_EXPR
, make_auto ());
5089 /* If there was an explicit qualification (S::~T), first look
5090 in the scope given by the qualification (i.e., S).
5092 Note: in the calls to cp_parser_class_name below we pass
5093 typename_type so that lookup finds the injected-class-name
5094 rather than the constructor. */
5096 type_decl
= NULL_TREE
;
5099 cp_parser_parse_tentatively (parser
);
5100 type_decl
= cp_parser_class_name (parser
,
5101 /*typename_keyword_p=*/false,
5102 /*template_keyword_p=*/false,
5104 /*check_dependency=*/false,
5105 /*class_head_p=*/false,
5107 if (cp_parser_parse_definitely (parser
))
5110 /* In "N::S::~S", look in "N" as well. */
5111 if (!done
&& scope
&& qualifying_scope
)
5113 cp_parser_parse_tentatively (parser
);
5114 parser
->scope
= qualifying_scope
;
5115 parser
->object_scope
= NULL_TREE
;
5116 parser
->qualifying_scope
= NULL_TREE
;
5118 = cp_parser_class_name (parser
,
5119 /*typename_keyword_p=*/false,
5120 /*template_keyword_p=*/false,
5122 /*check_dependency=*/false,
5123 /*class_head_p=*/false,
5125 if (cp_parser_parse_definitely (parser
))
5128 /* In "p->S::~T", look in the scope given by "*p" as well. */
5129 else if (!done
&& object_scope
)
5131 cp_parser_parse_tentatively (parser
);
5132 parser
->scope
= object_scope
;
5133 parser
->object_scope
= NULL_TREE
;
5134 parser
->qualifying_scope
= NULL_TREE
;
5136 = cp_parser_class_name (parser
,
5137 /*typename_keyword_p=*/false,
5138 /*template_keyword_p=*/false,
5140 /*check_dependency=*/false,
5141 /*class_head_p=*/false,
5143 if (cp_parser_parse_definitely (parser
))
5146 /* Look in the surrounding context. */
5149 parser
->scope
= NULL_TREE
;
5150 parser
->object_scope
= NULL_TREE
;
5151 parser
->qualifying_scope
= NULL_TREE
;
5152 if (processing_template_decl
)
5153 cp_parser_parse_tentatively (parser
);
5155 = cp_parser_class_name (parser
,
5156 /*typename_keyword_p=*/false,
5157 /*template_keyword_p=*/false,
5159 /*check_dependency=*/false,
5160 /*class_head_p=*/false,
5162 if (processing_template_decl
5163 && ! cp_parser_parse_definitely (parser
))
5165 /* We couldn't find a type with this name. If we're parsing
5166 tentatively, fail and try something else. */
5167 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5169 cp_parser_simulate_error (parser
);
5170 return error_mark_node
;
5172 /* Otherwise, accept it and check for a match at instantiation
5174 type_decl
= cp_parser_identifier (parser
);
5175 if (type_decl
!= error_mark_node
)
5176 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5180 /* If an error occurred, assume that the name of the
5181 destructor is the same as the name of the qualifying
5182 class. That allows us to keep parsing after running
5183 into ill-formed destructor names. */
5184 if (type_decl
== error_mark_node
&& scope
)
5185 return build_nt (BIT_NOT_EXPR
, scope
);
5186 else if (type_decl
== error_mark_node
)
5187 return error_mark_node
;
5189 /* Check that destructor name and scope match. */
5190 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5192 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5193 error_at (token
->location
,
5194 "declaration of %<~%T%> as member of %qT",
5196 cp_parser_simulate_error (parser
);
5197 return error_mark_node
;
5202 A typedef-name that names a class shall not be used as the
5203 identifier in the declarator for a destructor declaration. */
5205 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5206 && !DECL_SELF_REFERENCE_P (type_decl
)
5207 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5208 error_at (token
->location
,
5209 "typedef-name %qD used as destructor declarator",
5212 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5216 if (token
->keyword
== RID_OPERATOR
)
5220 /* This could be a template-id, so we try that first. */
5221 cp_parser_parse_tentatively (parser
);
5222 /* Try a template-id. */
5223 id
= cp_parser_template_id (parser
, template_keyword_p
,
5224 /*check_dependency_p=*/true,
5227 /* If that worked, we're done. */
5228 if (cp_parser_parse_definitely (parser
))
5230 /* We still don't know whether we're looking at an
5231 operator-function-id or a conversion-function-id. */
5232 cp_parser_parse_tentatively (parser
);
5233 /* Try an operator-function-id. */
5234 id
= cp_parser_operator_function_id (parser
);
5235 /* If that didn't work, try a conversion-function-id. */
5236 if (!cp_parser_parse_definitely (parser
))
5237 id
= cp_parser_conversion_function_id (parser
);
5238 else if (UDLIT_OPER_P (id
))
5241 const char *name
= UDLIT_OP_SUFFIX (id
);
5242 if (name
[0] != '_' && !in_system_header_at (input_location
)
5244 warning (0, "literal operator suffixes not preceded by %<_%>"
5245 " are reserved for future standardization");
5255 cp_parser_error (parser
, "expected unqualified-id");
5256 return error_mark_node
;
5260 /* Parse an (optional) nested-name-specifier.
5262 nested-name-specifier: [C++98]
5263 class-or-namespace-name :: nested-name-specifier [opt]
5264 class-or-namespace-name :: template nested-name-specifier [opt]
5266 nested-name-specifier: [C++0x]
5269 nested-name-specifier identifier ::
5270 nested-name-specifier template [opt] simple-template-id ::
5272 PARSER->SCOPE should be set appropriately before this function is
5273 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5274 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5277 Sets PARSER->SCOPE to the class (TYPE) or namespace
5278 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5279 it unchanged if there is no nested-name-specifier. Returns the new
5280 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5282 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5283 part of a declaration and/or decl-specifier. */
5286 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5287 bool typename_keyword_p
,
5288 bool check_dependency_p
,
5290 bool is_declaration
)
5292 bool success
= false;
5293 cp_token_position start
= 0;
5296 /* Remember where the nested-name-specifier starts. */
5297 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5299 start
= cp_lexer_token_position (parser
->lexer
, false);
5300 push_deferring_access_checks (dk_deferred
);
5307 tree saved_qualifying_scope
;
5308 bool template_keyword_p
;
5310 /* Spot cases that cannot be the beginning of a
5311 nested-name-specifier. */
5312 token
= cp_lexer_peek_token (parser
->lexer
);
5314 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5315 the already parsed nested-name-specifier. */
5316 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5318 /* Grab the nested-name-specifier and continue the loop. */
5319 cp_parser_pre_parsed_nested_name_specifier (parser
);
5320 /* If we originally encountered this nested-name-specifier
5321 with IS_DECLARATION set to false, we will not have
5322 resolved TYPENAME_TYPEs, so we must do so here. */
5324 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5326 new_scope
= resolve_typename_type (parser
->scope
,
5327 /*only_current_p=*/false);
5328 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5329 parser
->scope
= new_scope
;
5335 /* Spot cases that cannot be the beginning of a
5336 nested-name-specifier. On the second and subsequent times
5337 through the loop, we look for the `template' keyword. */
5338 if (success
&& token
->keyword
== RID_TEMPLATE
)
5340 /* A template-id can start a nested-name-specifier. */
5341 else if (token
->type
== CPP_TEMPLATE_ID
)
5343 /* DR 743: decltype can be used in a nested-name-specifier. */
5344 else if (token_is_decltype (token
))
5348 /* If the next token is not an identifier, then it is
5349 definitely not a type-name or namespace-name. */
5350 if (token
->type
!= CPP_NAME
)
5352 /* If the following token is neither a `<' (to begin a
5353 template-id), nor a `::', then we are not looking at a
5354 nested-name-specifier. */
5355 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5357 if (token
->type
== CPP_COLON
5358 && parser
->colon_corrects_to_scope_p
5359 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5361 error_at (token
->location
,
5362 "found %<:%> in nested-name-specifier, expected %<::%>");
5363 token
->type
= CPP_SCOPE
;
5366 if (token
->type
!= CPP_SCOPE
5367 && !cp_parser_nth_token_starts_template_argument_list_p
5372 /* The nested-name-specifier is optional, so we parse
5374 cp_parser_parse_tentatively (parser
);
5376 /* Look for the optional `template' keyword, if this isn't the
5377 first time through the loop. */
5379 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5381 template_keyword_p
= false;
5383 /* Save the old scope since the name lookup we are about to do
5384 might destroy it. */
5385 old_scope
= parser
->scope
;
5386 saved_qualifying_scope
= parser
->qualifying_scope
;
5387 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5388 look up names in "X<T>::I" in order to determine that "Y" is
5389 a template. So, if we have a typename at this point, we make
5390 an effort to look through it. */
5392 && !typename_keyword_p
5394 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5395 parser
->scope
= resolve_typename_type (parser
->scope
,
5396 /*only_current_p=*/false);
5397 /* Parse the qualifying entity. */
5399 = cp_parser_qualifying_entity (parser
,
5405 /* Look for the `::' token. */
5406 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5408 /* If we found what we wanted, we keep going; otherwise, we're
5410 if (!cp_parser_parse_definitely (parser
))
5412 bool error_p
= false;
5414 /* Restore the OLD_SCOPE since it was valid before the
5415 failed attempt at finding the last
5416 class-or-namespace-name. */
5417 parser
->scope
= old_scope
;
5418 parser
->qualifying_scope
= saved_qualifying_scope
;
5420 /* If the next token is a decltype, and the one after that is a
5421 `::', then the decltype has failed to resolve to a class or
5422 enumeration type. Give this error even when parsing
5423 tentatively since it can't possibly be valid--and we're going
5424 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5425 won't get another chance.*/
5426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5427 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5430 token
= cp_lexer_consume_token (parser
->lexer
);
5431 error_at (token
->location
, "decltype evaluates to %qT, "
5432 "which is not a class or enumeration type",
5434 parser
->scope
= error_mark_node
;
5438 cp_lexer_consume_token (parser
->lexer
);
5441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5442 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5444 /* If we have a non-type template-id followed by ::, it can't
5445 possibly be valid. */
5446 token
= cp_lexer_peek_token (parser
->lexer
);
5447 tree tid
= token
->u
.tree_check_value
->value
;
5448 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5449 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5451 tree tmpl
= NULL_TREE
;
5452 if (is_overloaded_fn (tid
))
5454 tree fns
= get_fns (tid
);
5455 if (!OVL_CHAIN (fns
))
5456 tmpl
= OVL_CURRENT (fns
);
5457 error_at (token
->location
, "function template-id %qD "
5458 "in nested-name-specifier", tid
);
5462 /* Variable template. */
5463 tmpl
= TREE_OPERAND (tid
, 0);
5464 gcc_assert (variable_template_p (tmpl
));
5465 error_at (token
->location
, "variable template-id %qD "
5466 "in nested-name-specifier", tid
);
5469 inform (DECL_SOURCE_LOCATION (tmpl
),
5470 "%qD declared here", tmpl
);
5472 parser
->scope
= error_mark_node
;
5476 cp_lexer_consume_token (parser
->lexer
);
5477 cp_lexer_consume_token (parser
->lexer
);
5481 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5483 /* If the next token is an identifier, and the one after
5484 that is a `::', then any valid interpretation would have
5485 found a class-or-namespace-name. */
5486 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5487 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5489 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5492 token
= cp_lexer_consume_token (parser
->lexer
);
5495 if (!token
->error_reported
)
5498 tree ambiguous_decls
;
5500 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5502 /*is_template=*/false,
5503 /*is_namespace=*/false,
5504 /*check_dependency=*/true,
5507 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5508 error_at (token
->location
,
5509 "%qD used without template parameters",
5511 else if (ambiguous_decls
)
5513 // cp_parser_lookup_name has the same diagnostic,
5514 // thus make sure to emit it at most once.
5515 if (cp_parser_uncommitted_to_tentative_parse_p
5518 error_at (token
->location
,
5519 "reference to %qD is ambiguous",
5521 print_candidates (ambiguous_decls
);
5523 decl
= error_mark_node
;
5527 if (cxx_dialect
!= cxx98
)
5528 cp_parser_name_lookup_error
5529 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5532 cp_parser_name_lookup_error
5533 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5537 parser
->scope
= error_mark_node
;
5539 /* Treat this as a successful nested-name-specifier
5544 If the name found is not a class-name (clause
5545 _class_) or namespace-name (_namespace.def_), the
5546 program is ill-formed. */
5549 cp_lexer_consume_token (parser
->lexer
);
5553 /* We've found one valid nested-name-specifier. */
5555 /* Name lookup always gives us a DECL. */
5556 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5557 new_scope
= TREE_TYPE (new_scope
);
5558 /* Uses of "template" must be followed by actual templates. */
5559 if (template_keyword_p
5560 && !(CLASS_TYPE_P (new_scope
)
5561 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5562 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5563 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5564 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5565 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5566 == TEMPLATE_ID_EXPR
)))
5567 permerror (input_location
, TYPE_P (new_scope
)
5568 ? G_("%qT is not a template")
5569 : G_("%qD is not a template"),
5571 /* If it is a class scope, try to complete it; we are about to
5572 be looking up names inside the class. */
5573 if (TYPE_P (new_scope
)
5574 /* Since checking types for dependency can be expensive,
5575 avoid doing it if the type is already complete. */
5576 && !COMPLETE_TYPE_P (new_scope
)
5577 /* Do not try to complete dependent types. */
5578 && !dependent_type_p (new_scope
))
5580 new_scope
= complete_type (new_scope
);
5581 /* If it is a typedef to current class, use the current
5582 class instead, as the typedef won't have any names inside
5584 if (!COMPLETE_TYPE_P (new_scope
)
5585 && currently_open_class (new_scope
))
5586 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5588 /* Make sure we look in the right scope the next time through
5590 parser
->scope
= new_scope
;
5593 /* If parsing tentatively, replace the sequence of tokens that makes
5594 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5595 token. That way, should we re-parse the token stream, we will
5596 not have to repeat the effort required to do the parse, nor will
5597 we issue duplicate error messages. */
5598 if (success
&& start
)
5602 token
= cp_lexer_token_at (parser
->lexer
, start
);
5603 /* Reset the contents of the START token. */
5604 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5605 /* Retrieve any deferred checks. Do not pop this access checks yet
5606 so the memory will not be reclaimed during token replacing below. */
5607 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5608 token
->u
.tree_check_value
->value
= parser
->scope
;
5609 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5610 token
->u
.tree_check_value
->qualifying_scope
=
5611 parser
->qualifying_scope
;
5612 token
->keyword
= RID_MAX
;
5614 /* Purge all subsequent tokens. */
5615 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5619 pop_to_parent_deferring_access_checks ();
5621 return success
? parser
->scope
: NULL_TREE
;
5624 /* Parse a nested-name-specifier. See
5625 cp_parser_nested_name_specifier_opt for details. This function
5626 behaves identically, except that it will an issue an error if no
5627 nested-name-specifier is present. */
5630 cp_parser_nested_name_specifier (cp_parser
*parser
,
5631 bool typename_keyword_p
,
5632 bool check_dependency_p
,
5634 bool is_declaration
)
5638 /* Look for the nested-name-specifier. */
5639 scope
= cp_parser_nested_name_specifier_opt (parser
,
5644 /* If it was not present, issue an error message. */
5647 cp_parser_error (parser
, "expected nested-name-specifier");
5648 parser
->scope
= NULL_TREE
;
5654 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5655 this is either a class-name or a namespace-name (which corresponds
5656 to the class-or-namespace-name production in the grammar). For
5657 C++0x, it can also be a type-name that refers to an enumeration
5658 type or a simple-template-id.
5660 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5661 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5662 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5663 TYPE_P is TRUE iff the next name should be taken as a class-name,
5664 even the same name is declared to be another entity in the same
5667 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5668 specified by the class-or-namespace-name. If neither is found the
5669 ERROR_MARK_NODE is returned. */
5672 cp_parser_qualifying_entity (cp_parser
*parser
,
5673 bool typename_keyword_p
,
5674 bool template_keyword_p
,
5675 bool check_dependency_p
,
5677 bool is_declaration
)
5680 tree saved_qualifying_scope
;
5681 tree saved_object_scope
;
5684 bool successful_parse_p
;
5686 /* DR 743: decltype can appear in a nested-name-specifier. */
5687 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5689 scope
= cp_parser_decltype (parser
);
5690 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5691 && !MAYBE_CLASS_TYPE_P (scope
))
5693 cp_parser_simulate_error (parser
);
5694 return error_mark_node
;
5696 if (TYPE_NAME (scope
))
5697 scope
= TYPE_NAME (scope
);
5701 /* Before we try to parse the class-name, we must save away the
5702 current PARSER->SCOPE since cp_parser_class_name will destroy
5704 saved_scope
= parser
->scope
;
5705 saved_qualifying_scope
= parser
->qualifying_scope
;
5706 saved_object_scope
= parser
->object_scope
;
5707 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5708 there is no need to look for a namespace-name. */
5709 only_class_p
= template_keyword_p
5710 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5712 cp_parser_parse_tentatively (parser
);
5713 scope
= cp_parser_class_name (parser
,
5716 type_p
? class_type
: none_type
,
5718 /*class_head_p=*/false,
5720 /*enum_ok=*/cxx_dialect
> cxx98
);
5721 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5722 /* If that didn't work, try for a namespace-name. */
5723 if (!only_class_p
&& !successful_parse_p
)
5725 /* Restore the saved scope. */
5726 parser
->scope
= saved_scope
;
5727 parser
->qualifying_scope
= saved_qualifying_scope
;
5728 parser
->object_scope
= saved_object_scope
;
5729 /* If we are not looking at an identifier followed by the scope
5730 resolution operator, then this is not part of a
5731 nested-name-specifier. (Note that this function is only used
5732 to parse the components of a nested-name-specifier.) */
5733 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5734 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5735 return error_mark_node
;
5736 scope
= cp_parser_namespace_name (parser
);
5742 /* Return true if we are looking at a compound-literal, false otherwise. */
5745 cp_parser_compound_literal_p (cp_parser
*parser
)
5747 /* Consume the `('. */
5748 cp_lexer_consume_token (parser
->lexer
);
5750 cp_lexer_save_tokens (parser
->lexer
);
5752 /* Skip tokens until the next token is a closing parenthesis.
5753 If we find the closing `)', and the next token is a `{', then
5754 we are looking at a compound-literal. */
5755 bool compound_literal_p
5756 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5757 /*consume_paren=*/true)
5758 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5760 /* Roll back the tokens we skipped. */
5761 cp_lexer_rollback_tokens (parser
->lexer
);
5763 return compound_literal_p
;
5766 /* Parse a postfix-expression.
5770 postfix-expression [ expression ]
5771 postfix-expression ( expression-list [opt] )
5772 simple-type-specifier ( expression-list [opt] )
5773 typename :: [opt] nested-name-specifier identifier
5774 ( expression-list [opt] )
5775 typename :: [opt] nested-name-specifier template [opt] template-id
5776 ( expression-list [opt] )
5777 postfix-expression . template [opt] id-expression
5778 postfix-expression -> template [opt] id-expression
5779 postfix-expression . pseudo-destructor-name
5780 postfix-expression -> pseudo-destructor-name
5781 postfix-expression ++
5782 postfix-expression --
5783 dynamic_cast < type-id > ( expression )
5784 static_cast < type-id > ( expression )
5785 reinterpret_cast < type-id > ( expression )
5786 const_cast < type-id > ( expression )
5787 typeid ( expression )
5793 ( type-id ) { initializer-list , [opt] }
5795 This extension is a GNU version of the C99 compound-literal
5796 construct. (The C99 grammar uses `type-name' instead of `type-id',
5797 but they are essentially the same concept.)
5799 If ADDRESS_P is true, the postfix expression is the operand of the
5800 `&' operator. CAST_P is true if this expression is the target of a
5803 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5804 class member access expressions [expr.ref].
5806 Returns a representation of the expression. */
5809 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5810 bool member_access_only_p
, bool decltype_p
,
5811 cp_id_kind
* pidk_return
)
5816 cp_id_kind idk
= CP_ID_KIND_NONE
;
5817 tree postfix_expression
= NULL_TREE
;
5818 bool is_member_access
= false;
5819 int saved_in_statement
= -1;
5821 /* Peek at the next token. */
5822 token
= cp_lexer_peek_token (parser
->lexer
);
5823 loc
= token
->location
;
5824 /* Some of the productions are determined by keywords. */
5825 keyword
= token
->keyword
;
5835 const char *saved_message
;
5836 bool saved_in_type_id_in_expr_p
;
5838 /* All of these can be handled in the same way from the point
5839 of view of parsing. Begin by consuming the token
5840 identifying the cast. */
5841 cp_lexer_consume_token (parser
->lexer
);
5843 /* New types cannot be defined in the cast. */
5844 saved_message
= parser
->type_definition_forbidden_message
;
5845 parser
->type_definition_forbidden_message
5846 = G_("types may not be defined in casts");
5848 /* Look for the opening `<'. */
5849 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5850 /* Parse the type to which we are casting. */
5851 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5852 parser
->in_type_id_in_expr_p
= true;
5853 type
= cp_parser_type_id (parser
);
5854 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5855 /* Look for the closing `>'. */
5856 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5857 /* Restore the old message. */
5858 parser
->type_definition_forbidden_message
= saved_message
;
5860 bool saved_greater_than_is_operator_p
5861 = parser
->greater_than_is_operator_p
;
5862 parser
->greater_than_is_operator_p
= true;
5864 /* And the expression which is being cast. */
5865 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5866 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5867 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5869 parser
->greater_than_is_operator_p
5870 = saved_greater_than_is_operator_p
;
5872 /* Only type conversions to integral or enumeration types
5873 can be used in constant-expressions. */
5874 if (!cast_valid_in_integral_constant_expression_p (type
)
5875 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5876 return error_mark_node
;
5882 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5886 = build_static_cast (type
, expression
, tf_warning_or_error
);
5890 = build_reinterpret_cast (type
, expression
,
5891 tf_warning_or_error
);
5895 = build_const_cast (type
, expression
, tf_warning_or_error
);
5906 const char *saved_message
;
5907 bool saved_in_type_id_in_expr_p
;
5909 /* Consume the `typeid' token. */
5910 cp_lexer_consume_token (parser
->lexer
);
5911 /* Look for the `(' token. */
5912 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5913 /* Types cannot be defined in a `typeid' expression. */
5914 saved_message
= parser
->type_definition_forbidden_message
;
5915 parser
->type_definition_forbidden_message
5916 = G_("types may not be defined in a %<typeid%> expression");
5917 /* We can't be sure yet whether we're looking at a type-id or an
5919 cp_parser_parse_tentatively (parser
);
5920 /* Try a type-id first. */
5921 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5922 parser
->in_type_id_in_expr_p
= true;
5923 type
= cp_parser_type_id (parser
);
5924 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5925 /* Look for the `)' token. Otherwise, we can't be sure that
5926 we're not looking at an expression: consider `typeid (int
5927 (3))', for example. */
5928 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5929 /* If all went well, simply lookup the type-id. */
5930 if (cp_parser_parse_definitely (parser
))
5931 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5932 /* Otherwise, fall back to the expression variant. */
5937 /* Look for an expression. */
5938 expression
= cp_parser_expression (parser
, & idk
);
5939 /* Compute its typeid. */
5940 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5941 /* Look for the `)' token. */
5942 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5944 /* Restore the saved message. */
5945 parser
->type_definition_forbidden_message
= saved_message
;
5946 /* `typeid' may not appear in an integral constant expression. */
5947 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5948 return error_mark_node
;
5955 /* The syntax permitted here is the same permitted for an
5956 elaborated-type-specifier. */
5957 type
= cp_parser_elaborated_type_specifier (parser
,
5958 /*is_friend=*/false,
5959 /*is_declaration=*/false);
5960 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5964 case RID_CILK_SPAWN
:
5966 cp_lexer_consume_token (parser
->lexer
);
5967 token
= cp_lexer_peek_token (parser
->lexer
);
5968 if (token
->type
== CPP_SEMICOLON
)
5970 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5972 postfix_expression
= error_mark_node
;
5975 else if (!current_function_decl
)
5977 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5978 "inside a function");
5979 postfix_expression
= error_mark_node
;
5984 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5985 saved_in_statement
= parser
->in_statement
;
5986 parser
->in_statement
|= IN_CILK_SPAWN
;
5988 cfun
->calls_cilk_spawn
= 1;
5989 postfix_expression
=
5990 cp_parser_postfix_expression (parser
, false, false,
5991 false, false, &idk
);
5994 error_at (token
->location
, "-fcilkplus must be enabled to use"
5995 " %<_Cilk_spawn%>");
5996 cfun
->calls_cilk_spawn
= 0;
5998 else if (saved_in_statement
& IN_CILK_SPAWN
)
6000 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6001 "are not permitted");
6002 postfix_expression
= error_mark_node
;
6003 cfun
->calls_cilk_spawn
= 0;
6007 postfix_expression
= build_cilk_spawn (token
->location
,
6008 postfix_expression
);
6009 if (postfix_expression
!= error_mark_node
)
6010 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6011 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6016 case RID_BUILTIN_SHUFFLE
:
6018 vec
<tree
, va_gc
> *vec
;
6022 cp_lexer_consume_token (parser
->lexer
);
6023 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6024 /*cast_p=*/false, /*allow_expansion_p=*/true,
6025 /*non_constant_p=*/NULL
);
6027 return error_mark_node
;
6029 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6032 if (vec
->length () == 2)
6033 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6034 tf_warning_or_error
);
6035 else if (vec
->length () == 3)
6036 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6037 tf_warning_or_error
);
6040 error_at (loc
, "wrong number of arguments to "
6041 "%<__builtin_shuffle%>");
6042 return error_mark_node
;
6051 /* If the next thing is a simple-type-specifier, we may be
6052 looking at a functional cast. We could also be looking at
6053 an id-expression. So, we try the functional cast, and if
6054 that doesn't work we fall back to the primary-expression. */
6055 cp_parser_parse_tentatively (parser
);
6056 /* Look for the simple-type-specifier. */
6057 type
= cp_parser_simple_type_specifier (parser
,
6058 /*decl_specs=*/NULL
,
6059 CP_PARSER_FLAGS_NONE
);
6060 /* Parse the cast itself. */
6061 if (!cp_parser_error_occurred (parser
))
6063 = cp_parser_functional_cast (parser
, type
);
6064 /* If that worked, we're done. */
6065 if (cp_parser_parse_definitely (parser
))
6068 /* If the functional-cast didn't work out, try a
6069 compound-literal. */
6070 if (cp_parser_allow_gnu_extensions_p (parser
)
6071 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6073 tree initializer
= NULL_TREE
;
6075 cp_parser_parse_tentatively (parser
);
6077 /* Avoid calling cp_parser_type_id pointlessly, see comment
6078 in cp_parser_cast_expression about c++/29234. */
6079 if (!cp_parser_compound_literal_p (parser
))
6080 cp_parser_simulate_error (parser
);
6083 /* Parse the type. */
6084 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6085 parser
->in_type_id_in_expr_p
= true;
6086 type
= cp_parser_type_id (parser
);
6087 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6088 /* Look for the `)'. */
6089 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6092 /* If things aren't going well, there's no need to
6094 if (!cp_parser_error_occurred (parser
))
6096 bool non_constant_p
;
6097 /* Parse the brace-enclosed initializer list. */
6098 initializer
= cp_parser_braced_list (parser
,
6101 /* If that worked, we're definitely looking at a
6102 compound-literal expression. */
6103 if (cp_parser_parse_definitely (parser
))
6105 /* Warn the user that a compound literal is not
6106 allowed in standard C++. */
6107 pedwarn (input_location
, OPT_Wpedantic
,
6108 "ISO C++ forbids compound-literals");
6109 /* For simplicity, we disallow compound literals in
6110 constant-expressions. We could
6111 allow compound literals of integer type, whose
6112 initializer was a constant, in constant
6113 expressions. Permitting that usage, as a further
6114 extension, would not change the meaning of any
6115 currently accepted programs. (Of course, as
6116 compound literals are not part of ISO C++, the
6117 standard has nothing to say.) */
6118 if (cp_parser_non_integral_constant_expression (parser
,
6121 postfix_expression
= error_mark_node
;
6124 /* Form the representation of the compound-literal. */
6126 = finish_compound_literal (type
, initializer
,
6127 tf_warning_or_error
);
6132 /* It must be a primary-expression. */
6134 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6135 /*template_arg_p=*/false,
6142 /* Note that we don't need to worry about calling build_cplus_new on a
6143 class-valued CALL_EXPR in decltype when it isn't the end of the
6144 postfix-expression; unary_complex_lvalue will take care of that for
6147 /* Keep looping until the postfix-expression is complete. */
6150 if (idk
== CP_ID_KIND_UNQUALIFIED
6151 && identifier_p (postfix_expression
)
6152 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6153 /* It is not a Koenig lookup function call. */
6155 = unqualified_name_lookup_error (postfix_expression
);
6157 /* Peek at the next token. */
6158 token
= cp_lexer_peek_token (parser
->lexer
);
6160 switch (token
->type
)
6162 case CPP_OPEN_SQUARE
:
6163 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6165 cp_parser_error (parser
,
6166 "two consecutive %<[%> shall "
6167 "only introduce an attribute");
6168 return error_mark_node
;
6171 = cp_parser_postfix_open_square_expression (parser
,
6175 idk
= CP_ID_KIND_NONE
;
6176 is_member_access
= false;
6179 case CPP_OPEN_PAREN
:
6180 /* postfix-expression ( expression-list [opt] ) */
6183 bool is_builtin_constant_p
;
6184 bool saved_integral_constant_expression_p
= false;
6185 bool saved_non_integral_constant_expression_p
= false;
6186 tsubst_flags_t complain
= complain_flags (decltype_p
);
6187 vec
<tree
, va_gc
> *args
;
6189 is_member_access
= false;
6191 is_builtin_constant_p
6192 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6193 if (is_builtin_constant_p
)
6195 /* The whole point of __builtin_constant_p is to allow
6196 non-constant expressions to appear as arguments. */
6197 saved_integral_constant_expression_p
6198 = parser
->integral_constant_expression_p
;
6199 saved_non_integral_constant_expression_p
6200 = parser
->non_integral_constant_expression_p
;
6201 parser
->integral_constant_expression_p
= false;
6203 args
= (cp_parser_parenthesized_expression_list
6205 /*cast_p=*/false, /*allow_expansion_p=*/true,
6206 /*non_constant_p=*/NULL
,
6207 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6208 if (is_builtin_constant_p
)
6210 parser
->integral_constant_expression_p
6211 = saved_integral_constant_expression_p
;
6212 parser
->non_integral_constant_expression_p
6213 = saved_non_integral_constant_expression_p
;
6218 postfix_expression
= error_mark_node
;
6222 /* Function calls are not permitted in
6223 constant-expressions. */
6224 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6225 && cp_parser_non_integral_constant_expression (parser
,
6228 postfix_expression
= error_mark_node
;
6229 release_tree_vector (args
);
6234 if (idk
== CP_ID_KIND_UNQUALIFIED
6235 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6237 if (identifier_p (postfix_expression
))
6239 if (!args
->is_empty ())
6242 if (!any_type_dependent_arguments_p (args
))
6244 = perform_koenig_lookup (postfix_expression
, args
,
6249 = unqualified_fn_lookup_error (postfix_expression
);
6251 /* We do not perform argument-dependent lookup if
6252 normal lookup finds a non-function, in accordance
6253 with the expected resolution of DR 218. */
6254 else if (!args
->is_empty ()
6255 && is_overloaded_fn (postfix_expression
))
6257 tree fn
= get_first_fn (postfix_expression
);
6258 fn
= STRIP_TEMPLATE (fn
);
6260 /* Do not do argument dependent lookup if regular
6261 lookup finds a member function or a block-scope
6262 function declaration. [basic.lookup.argdep]/3 */
6263 if (!DECL_FUNCTION_MEMBER_P (fn
)
6264 && !DECL_LOCAL_FUNCTION_P (fn
))
6267 if (!any_type_dependent_arguments_p (args
))
6269 = perform_koenig_lookup (postfix_expression
, args
,
6275 if (warn_memset_transposed_args
)
6277 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6278 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6279 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6280 && vec_safe_length (args
) == 3
6281 && integer_zerop ((*args
)[2])
6282 && LITERAL_ZERO_P ((*args
)[2])
6283 && !(integer_zerop ((*args
)[1])
6284 && LITERAL_ZERO_P ((*args
)[1])))
6285 warning (OPT_Wmemset_transposed_args
,
6286 "%<memset%> used with constant zero length "
6287 "parameter; this could be due to transposed "
6290 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6291 to avoid leaking those into folder and middle-end. */
6294 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6295 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6296 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6299 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6301 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6302 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6304 if (processing_template_decl
6305 && (type_dependent_expression_p (instance
)
6306 || (!BASELINK_P (fn
)
6307 && TREE_CODE (fn
) != FIELD_DECL
)
6308 || type_dependent_expression_p (fn
)
6309 || any_type_dependent_arguments_p (args
)))
6312 = build_nt_call_vec (postfix_expression
, args
);
6313 release_tree_vector (args
);
6317 if (BASELINK_P (fn
))
6320 = (build_new_method_call
6321 (instance
, fn
, &args
, NULL_TREE
,
6322 (idk
== CP_ID_KIND_QUALIFIED
6323 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6330 = finish_call_expr (postfix_expression
, &args
,
6331 /*disallow_virtual=*/false,
6335 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6336 || TREE_CODE (postfix_expression
) == MEMBER_REF
6337 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6338 postfix_expression
= (build_offset_ref_call_from_tree
6339 (postfix_expression
, &args
,
6341 else if (idk
== CP_ID_KIND_QUALIFIED
)
6342 /* A call to a static class member, or a namespace-scope
6345 = finish_call_expr (postfix_expression
, &args
,
6346 /*disallow_virtual=*/true,
6350 /* All other function calls. */
6352 = finish_call_expr (postfix_expression
, &args
,
6353 /*disallow_virtual=*/false,
6357 protected_set_expr_location (postfix_expression
, token
->location
);
6359 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6360 idk
= CP_ID_KIND_NONE
;
6362 release_tree_vector (args
);
6368 /* postfix-expression . template [opt] id-expression
6369 postfix-expression . pseudo-destructor-name
6370 postfix-expression -> template [opt] id-expression
6371 postfix-expression -> pseudo-destructor-name */
6373 /* Consume the `.' or `->' operator. */
6374 cp_lexer_consume_token (parser
->lexer
);
6377 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6381 is_member_access
= true;
6385 /* postfix-expression ++ */
6386 /* Consume the `++' token. */
6387 cp_lexer_consume_token (parser
->lexer
);
6388 /* Generate a representation for the complete expression. */
6390 = finish_increment_expr (postfix_expression
,
6391 POSTINCREMENT_EXPR
);
6392 /* Increments may not appear in constant-expressions. */
6393 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6394 postfix_expression
= error_mark_node
;
6395 idk
= CP_ID_KIND_NONE
;
6396 is_member_access
= false;
6399 case CPP_MINUS_MINUS
:
6400 /* postfix-expression -- */
6401 /* Consume the `--' token. */
6402 cp_lexer_consume_token (parser
->lexer
);
6403 /* Generate a representation for the complete expression. */
6405 = finish_increment_expr (postfix_expression
,
6406 POSTDECREMENT_EXPR
);
6407 /* Decrements may not appear in constant-expressions. */
6408 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6409 postfix_expression
= error_mark_node
;
6410 idk
= CP_ID_KIND_NONE
;
6411 is_member_access
= false;
6415 if (pidk_return
!= NULL
)
6416 * pidk_return
= idk
;
6417 if (member_access_only_p
)
6418 return is_member_access
? postfix_expression
: error_mark_node
;
6420 return postfix_expression
;
6424 /* We should never get here. */
6426 return error_mark_node
;
6429 /* This function parses Cilk Plus array notations. If a normal array expr. is
6430 parsed then the array index is passed back to the caller through *INIT_INDEX
6431 and the function returns a NULL_TREE. If array notation expr. is parsed,
6432 then *INIT_INDEX is ignored by the caller and the function returns
6433 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6437 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6440 cp_token
*token
= NULL
;
6441 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6442 if (!array_value
|| array_value
== error_mark_node
)
6444 cp_parser_skip_to_end_of_statement (parser
);
6445 return error_mark_node
;
6448 array_type
= TREE_TYPE (array_value
);
6450 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6451 parser
->colon_corrects_to_scope_p
= false;
6452 token
= cp_lexer_peek_token (parser
->lexer
);
6456 cp_parser_error (parser
, "expected %<:%> or numeral");
6457 return error_mark_node
;
6459 else if (token
->type
== CPP_COLON
)
6461 /* Consume the ':'. */
6462 cp_lexer_consume_token (parser
->lexer
);
6464 /* If we are here, then we have a case like this A[:]. */
6465 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6467 cp_parser_error (parser
, "expected %<]%>");
6468 cp_parser_skip_to_end_of_statement (parser
);
6469 return error_mark_node
;
6471 *init_index
= NULL_TREE
;
6473 length_index
= NULL_TREE
;
6477 /* If we are here, then there are three valid possibilities:
6479 2. ARRAY [ EXP : EXP ]
6480 3. ARRAY [ EXP : EXP : EXP ] */
6482 *init_index
= cp_parser_expression (parser
);
6483 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6485 /* This indicates that we have a normal array expression. */
6486 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6490 /* Consume the ':'. */
6491 cp_lexer_consume_token (parser
->lexer
);
6492 length_index
= cp_parser_expression (parser
);
6493 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6495 cp_lexer_consume_token (parser
->lexer
);
6496 stride
= cp_parser_expression (parser
);
6499 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6501 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6502 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6504 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6505 cp_lexer_consume_token (parser
->lexer
);
6506 return error_mark_node
;
6508 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6510 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6511 length_index
, stride
, array_type
);
6515 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6516 by cp_parser_builtin_offsetof. We're looking for
6518 postfix-expression [ expression ]
6519 postfix-expression [ braced-init-list ] (C++11)
6521 FOR_OFFSETOF is set if we're being called in that context, which
6522 changes how we deal with integer constant expressions. */
6525 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6526 tree postfix_expression
,
6530 tree index
= NULL_TREE
;
6531 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6532 bool saved_greater_than_is_operator_p
;
6534 /* Consume the `[' token. */
6535 cp_lexer_consume_token (parser
->lexer
);
6537 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6538 parser
->greater_than_is_operator_p
= true;
6540 /* Parse the index expression. */
6541 /* ??? For offsetof, there is a question of what to allow here. If
6542 offsetof is not being used in an integral constant expression context,
6543 then we *could* get the right answer by computing the value at runtime.
6544 If we are in an integral constant expression context, then we might
6545 could accept any constant expression; hard to say without analysis.
6546 Rather than open the barn door too wide right away, allow only integer
6547 constant expressions here. */
6549 index
= cp_parser_constant_expression (parser
);
6552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6554 bool expr_nonconst_p
;
6555 cp_lexer_set_source_position (parser
->lexer
);
6556 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6557 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6559 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6561 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6562 "braced list index is not allowed with array "
6564 cp_parser_skip_to_end_of_statement (parser
);
6565 return error_mark_node
;
6568 else if (flag_cilkplus
)
6570 /* Here are have these two options:
6571 ARRAY[EXP : EXP] - Array notation expr with default
6573 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6575 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6576 postfix_expression
);
6581 index
= cp_parser_expression (parser
);
6584 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6586 /* Look for the closing `]'. */
6587 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6589 /* Build the ARRAY_REF. */
6590 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6593 /* When not doing offsetof, array references are not permitted in
6594 constant-expressions. */
6596 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6597 postfix_expression
= error_mark_node
;
6599 return postfix_expression
;
6602 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6603 by cp_parser_builtin_offsetof. We're looking for
6605 postfix-expression . template [opt] id-expression
6606 postfix-expression . pseudo-destructor-name
6607 postfix-expression -> template [opt] id-expression
6608 postfix-expression -> pseudo-destructor-name
6610 FOR_OFFSETOF is set if we're being called in that context. That sorta
6611 limits what of the above we'll actually accept, but nevermind.
6612 TOKEN_TYPE is the "." or "->" token, which will already have been
6613 removed from the stream. */
6616 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6617 enum cpp_ttype token_type
,
6618 tree postfix_expression
,
6619 bool for_offsetof
, cp_id_kind
*idk
,
6620 location_t location
)
6624 bool pseudo_destructor_p
;
6625 tree scope
= NULL_TREE
;
6627 /* If this is a `->' operator, dereference the pointer. */
6628 if (token_type
== CPP_DEREF
)
6629 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6630 tf_warning_or_error
);
6631 /* Check to see whether or not the expression is type-dependent. */
6632 dependent_p
= type_dependent_expression_p (postfix_expression
);
6633 /* The identifier following the `->' or `.' is not qualified. */
6634 parser
->scope
= NULL_TREE
;
6635 parser
->qualifying_scope
= NULL_TREE
;
6636 parser
->object_scope
= NULL_TREE
;
6637 *idk
= CP_ID_KIND_NONE
;
6639 /* Enter the scope corresponding to the type of the object
6640 given by the POSTFIX_EXPRESSION. */
6641 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6643 scope
= TREE_TYPE (postfix_expression
);
6644 /* According to the standard, no expression should ever have
6645 reference type. Unfortunately, we do not currently match
6646 the standard in this respect in that our internal representation
6647 of an expression may have reference type even when the standard
6648 says it does not. Therefore, we have to manually obtain the
6649 underlying type here. */
6650 scope
= non_reference (scope
);
6651 /* The type of the POSTFIX_EXPRESSION must be complete. */
6652 if (scope
== unknown_type_node
)
6654 error_at (location
, "%qE does not have class type",
6655 postfix_expression
);
6658 /* Unlike the object expression in other contexts, *this is not
6659 required to be of complete type for purposes of class member
6660 access (5.2.5) outside the member function body. */
6661 else if (postfix_expression
!= current_class_ref
6662 && !(processing_template_decl
&& scope
== current_class_type
))
6663 scope
= complete_type_or_else (scope
, NULL_TREE
);
6664 /* Let the name lookup machinery know that we are processing a
6665 class member access expression. */
6666 parser
->context
->object_type
= scope
;
6667 /* If something went wrong, we want to be able to discern that case,
6668 as opposed to the case where there was no SCOPE due to the type
6669 of expression being dependent. */
6671 scope
= error_mark_node
;
6672 /* If the SCOPE was erroneous, make the various semantic analysis
6673 functions exit quickly -- and without issuing additional error
6675 if (scope
== error_mark_node
)
6676 postfix_expression
= error_mark_node
;
6679 /* Assume this expression is not a pseudo-destructor access. */
6680 pseudo_destructor_p
= false;
6682 /* If the SCOPE is a scalar type, then, if this is a valid program,
6683 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6684 is type dependent, it can be pseudo-destructor-name or something else.
6685 Try to parse it as pseudo-destructor-name first. */
6686 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6691 cp_parser_parse_tentatively (parser
);
6692 /* Parse the pseudo-destructor-name. */
6694 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6697 && (cp_parser_error_occurred (parser
)
6698 || !SCALAR_TYPE_P (type
)))
6699 cp_parser_abort_tentative_parse (parser
);
6700 else if (cp_parser_parse_definitely (parser
))
6702 pseudo_destructor_p
= true;
6704 = finish_pseudo_destructor_expr (postfix_expression
,
6709 if (!pseudo_destructor_p
)
6711 /* If the SCOPE is not a scalar type, we are looking at an
6712 ordinary class member access expression, rather than a
6713 pseudo-destructor-name. */
6715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6716 /* Parse the id-expression. */
6717 name
= (cp_parser_id_expression
6719 cp_parser_optional_template_keyword (parser
),
6720 /*check_dependency_p=*/true,
6722 /*declarator_p=*/false,
6723 /*optional_p=*/false));
6724 /* In general, build a SCOPE_REF if the member name is qualified.
6725 However, if the name was not dependent and has already been
6726 resolved; there is no need to build the SCOPE_REF. For example;
6728 struct X { void f(); };
6729 template <typename T> void f(T* t) { t->X::f(); }
6731 Even though "t" is dependent, "X::f" is not and has been resolved
6732 to a BASELINK; there is no need to include scope information. */
6734 /* But we do need to remember that there was an explicit scope for
6735 virtual function calls. */
6737 *idk
= CP_ID_KIND_QUALIFIED
;
6739 /* If the name is a template-id that names a type, we will get a
6740 TYPE_DECL here. That is invalid code. */
6741 if (TREE_CODE (name
) == TYPE_DECL
)
6743 error_at (token
->location
, "invalid use of %qD", name
);
6744 postfix_expression
= error_mark_node
;
6748 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6750 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6752 error_at (token
->location
, "%<%D::%D%> is not a class member",
6753 parser
->scope
, name
);
6754 postfix_expression
= error_mark_node
;
6757 name
= build_qualified_name (/*type=*/NULL_TREE
,
6761 parser
->scope
= NULL_TREE
;
6762 parser
->qualifying_scope
= NULL_TREE
;
6763 parser
->object_scope
= NULL_TREE
;
6765 if (parser
->scope
&& name
&& BASELINK_P (name
))
6766 adjust_result_of_qualified_name_lookup
6767 (name
, parser
->scope
, scope
);
6769 = finish_class_member_access_expr (postfix_expression
, name
,
6771 tf_warning_or_error
);
6775 /* We no longer need to look up names in the scope of the object on
6776 the left-hand side of the `.' or `->' operator. */
6777 parser
->context
->object_type
= NULL_TREE
;
6779 /* Outside of offsetof, these operators may not appear in
6780 constant-expressions. */
6782 && (cp_parser_non_integral_constant_expression
6783 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6784 postfix_expression
= error_mark_node
;
6786 return postfix_expression
;
6789 /* Cache of LITERAL_ZERO_P constants. */
6791 static GTY(()) tree literal_zeros
[itk_none
];
6793 /* Parse a parenthesized expression-list.
6796 assignment-expression
6797 expression-list, assignment-expression
6802 identifier, expression-list
6804 CAST_P is true if this expression is the target of a cast.
6806 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6809 Returns a vector of trees. Each element is a representation of an
6810 assignment-expression. NULL is returned if the ( and or ) are
6811 missing. An empty, but allocated, vector is returned on no
6812 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6813 if we are parsing an attribute list for an attribute that wants a
6814 plain identifier argument, normal_attr for an attribute that wants
6815 an expression, or non_attr if we aren't parsing an attribute list. If
6816 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6817 not all of the expressions in the list were constant.
6818 WANT_LITERAL_ZERO_P is true if the caller is interested in
6819 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6820 immediately, this can be removed. */
6822 static vec
<tree
, va_gc
> *
6823 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6824 int is_attribute_list
,
6826 bool allow_expansion_p
,
6827 bool *non_constant_p
,
6828 bool want_literal_zero_p
)
6830 vec
<tree
, va_gc
> *expression_list
;
6831 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6832 tree identifier
= NULL_TREE
;
6833 bool saved_greater_than_is_operator_p
;
6835 /* Assume all the expressions will be constant. */
6837 *non_constant_p
= false;
6839 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6842 expression_list
= make_tree_vector ();
6844 /* Within a parenthesized expression, a `>' token is always
6845 the greater-than operator. */
6846 saved_greater_than_is_operator_p
6847 = parser
->greater_than_is_operator_p
;
6848 parser
->greater_than_is_operator_p
= true;
6850 /* Consume expressions until there are no more. */
6851 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6856 /* At the beginning of attribute lists, check to see if the
6857 next token is an identifier. */
6858 if (is_attribute_list
== id_attr
6859 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6863 /* Consume the identifier. */
6864 token
= cp_lexer_consume_token (parser
->lexer
);
6865 /* Save the identifier. */
6866 identifier
= token
->u
.value
;
6870 bool expr_non_constant_p
;
6872 /* Parse the next assignment-expression. */
6873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6875 /* A braced-init-list. */
6876 cp_lexer_set_source_position (parser
->lexer
);
6877 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6878 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6879 if (non_constant_p
&& expr_non_constant_p
)
6880 *non_constant_p
= true;
6882 else if (non_constant_p
)
6884 expr
= (cp_parser_constant_expression
6885 (parser
, /*allow_non_constant_p=*/true,
6886 &expr_non_constant_p
));
6887 if (expr_non_constant_p
)
6888 *non_constant_p
= true;
6893 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6902 /* If a parameter is literal zero alone, remember it
6903 for -Wmemset-transposed-args warning. */
6904 if (integer_zerop (tok
->u
.value
)
6905 && !TREE_OVERFLOW (tok
->u
.value
)
6906 && want_literal_zero_p
6907 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6909 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6910 == CPP_CLOSE_PAREN
))
6913 for (i
= 0; i
< itk_none
; ++i
)
6914 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6916 if (i
< itk_none
&& literal_zeros
[i
])
6917 expr
= literal_zeros
[i
];
6920 expr
= copy_node (tok
->u
.value
);
6921 LITERAL_ZERO_P (expr
) = 1;
6923 literal_zeros
[i
] = expr
;
6925 /* Consume the 0 token (or '\0', 0LL etc.). */
6926 cp_lexer_consume_token (parser
->lexer
);
6932 if (expr
== NULL_TREE
)
6933 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6938 expr
= instantiate_non_dependent_expr (expr
);
6940 /* If we have an ellipsis, then this is an expression
6942 if (allow_expansion_p
6943 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6945 /* Consume the `...'. */
6946 cp_lexer_consume_token (parser
->lexer
);
6948 /* Build the argument pack. */
6949 expr
= make_pack_expansion (expr
);
6952 /* Add it to the list. We add error_mark_node
6953 expressions to the list, so that we can still tell if
6954 the correct form for a parenthesized expression-list
6955 is found. That gives better errors. */
6956 vec_safe_push (expression_list
, expr
);
6958 if (expr
== error_mark_node
)
6962 /* After the first item, attribute lists look the same as
6963 expression lists. */
6964 is_attribute_list
= non_attr
;
6967 /* If the next token isn't a `,', then we are done. */
6968 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6971 /* Otherwise, consume the `,' and keep going. */
6972 cp_lexer_consume_token (parser
->lexer
);
6975 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6980 /* We try and resync to an unnested comma, as that will give the
6981 user better diagnostics. */
6982 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6983 /*recovering=*/true,
6985 /*consume_paren=*/true);
6990 parser
->greater_than_is_operator_p
6991 = saved_greater_than_is_operator_p
;
6996 parser
->greater_than_is_operator_p
6997 = saved_greater_than_is_operator_p
;
7000 vec_safe_insert (expression_list
, 0, identifier
);
7002 return expression_list
;
7005 /* Parse a pseudo-destructor-name.
7007 pseudo-destructor-name:
7008 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7009 :: [opt] nested-name-specifier template template-id :: ~ type-name
7010 :: [opt] nested-name-specifier [opt] ~ type-name
7012 If either of the first two productions is used, sets *SCOPE to the
7013 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7014 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7015 or ERROR_MARK_NODE if the parse fails. */
7018 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7023 bool nested_name_specifier_p
;
7026 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7027 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7028 && !type_dependent_expression_p (object
))
7030 if (cxx_dialect
< cxx14
)
7031 pedwarn (input_location
, 0,
7032 "%<~auto%> only available with "
7033 "-std=c++14 or -std=gnu++14");
7034 cp_lexer_consume_token (parser
->lexer
);
7035 cp_lexer_consume_token (parser
->lexer
);
7037 *type
= TREE_TYPE (object
);
7041 /* Assume that things will not work out. */
7042 *type
= error_mark_node
;
7044 /* Look for the optional `::' operator. */
7045 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7046 /* Look for the optional nested-name-specifier. */
7047 nested_name_specifier_p
7048 = (cp_parser_nested_name_specifier_opt (parser
,
7049 /*typename_keyword_p=*/false,
7050 /*check_dependency_p=*/true,
7052 /*is_declaration=*/false)
7054 /* Now, if we saw a nested-name-specifier, we might be doing the
7055 second production. */
7056 if (nested_name_specifier_p
7057 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7059 /* Consume the `template' keyword. */
7060 cp_lexer_consume_token (parser
->lexer
);
7061 /* Parse the template-id. */
7062 cp_parser_template_id (parser
,
7063 /*template_keyword_p=*/true,
7064 /*check_dependency_p=*/false,
7066 /*is_declaration=*/true);
7067 /* Look for the `::' token. */
7068 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7070 /* If the next token is not a `~', then there might be some
7071 additional qualification. */
7072 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7074 /* At this point, we're looking for "type-name :: ~". The type-name
7075 must not be a class-name, since this is a pseudo-destructor. So,
7076 it must be either an enum-name, or a typedef-name -- both of which
7077 are just identifiers. So, we peek ahead to check that the "::"
7078 and "~" tokens are present; if they are not, then we can avoid
7079 calling type_name. */
7080 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7081 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7082 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7084 cp_parser_error (parser
, "non-scalar type");
7088 /* Look for the type-name. */
7089 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7090 if (*scope
== error_mark_node
)
7093 /* Look for the `::' token. */
7094 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7099 /* Look for the `~'. */
7100 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7102 /* Once we see the ~, this has to be a pseudo-destructor. */
7103 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7104 cp_parser_commit_to_topmost_tentative_parse (parser
);
7106 /* Look for the type-name again. We are not responsible for
7107 checking that it matches the first type-name. */
7108 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7111 /* Parse a unary-expression.
7117 unary-operator cast-expression
7118 sizeof unary-expression
7120 alignof ( type-id ) [C++0x]
7127 __extension__ cast-expression
7128 __alignof__ unary-expression
7129 __alignof__ ( type-id )
7130 alignof unary-expression [C++0x]
7131 __real__ cast-expression
7132 __imag__ cast-expression
7134 sizeof ( type-id ) { initializer-list , [opt] }
7135 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7136 __alignof__ ( type-id ) { initializer-list , [opt] }
7138 ADDRESS_P is true iff the unary-expression is appearing as the
7139 operand of the `&' operator. CAST_P is true if this expression is
7140 the target of a cast.
7142 Returns a representation of the expression. */
7145 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7146 bool address_p
, bool cast_p
, bool decltype_p
)
7149 enum tree_code unary_operator
;
7151 /* Peek at the next token. */
7152 token
= cp_lexer_peek_token (parser
->lexer
);
7153 /* Some keywords give away the kind of expression. */
7154 if (token
->type
== CPP_KEYWORD
)
7156 enum rid keyword
= token
->keyword
;
7165 location_t first_loc
;
7167 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7168 /* Consume the token. */
7169 cp_lexer_consume_token (parser
->lexer
);
7170 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7171 /* Parse the operand. */
7172 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7174 if (TYPE_P (operand
))
7175 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7178 /* ISO C++ defines alignof only with types, not with
7179 expressions. So pedwarn if alignof is used with a non-
7180 type expression. However, __alignof__ is ok. */
7181 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7182 pedwarn (token
->location
, OPT_Wpedantic
,
7183 "ISO C++ does not allow %<alignof%> "
7186 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7188 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7189 SIZEOF_EXPR with the original operand. */
7190 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7192 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7194 if (!processing_template_decl
&& TYPE_P (operand
))
7196 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7197 build1 (NOP_EXPR
, operand
,
7199 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7202 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7203 TREE_SIDE_EFFECTS (ret
) = 0;
7204 TREE_READONLY (ret
) = 1;
7206 SET_EXPR_LOCATION (ret
, first_loc
);
7212 return cp_parser_new_expression (parser
);
7215 return cp_parser_delete_expression (parser
);
7219 /* The saved value of the PEDANTIC flag. */
7223 /* Save away the PEDANTIC flag. */
7224 cp_parser_extension_opt (parser
, &saved_pedantic
);
7225 /* Parse the cast-expression. */
7226 expr
= cp_parser_simple_cast_expression (parser
);
7227 /* Restore the PEDANTIC flag. */
7228 pedantic
= saved_pedantic
;
7238 /* Consume the `__real__' or `__imag__' token. */
7239 cp_lexer_consume_token (parser
->lexer
);
7240 /* Parse the cast-expression. */
7241 expression
= cp_parser_simple_cast_expression (parser
);
7242 /* Create the complete representation. */
7243 return build_x_unary_op (token
->location
,
7244 (keyword
== RID_REALPART
7245 ? REALPART_EXPR
: IMAGPART_EXPR
),
7247 tf_warning_or_error
);
7251 case RID_TRANSACTION_ATOMIC
:
7252 case RID_TRANSACTION_RELAXED
:
7253 return cp_parser_transaction_expression (parser
, keyword
);
7258 const char *saved_message
;
7259 bool saved_integral_constant_expression_p
;
7260 bool saved_non_integral_constant_expression_p
;
7261 bool saved_greater_than_is_operator_p
;
7263 cp_lexer_consume_token (parser
->lexer
);
7264 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7266 saved_message
= parser
->type_definition_forbidden_message
;
7267 parser
->type_definition_forbidden_message
7268 = G_("types may not be defined in %<noexcept%> expressions");
7270 saved_integral_constant_expression_p
7271 = parser
->integral_constant_expression_p
;
7272 saved_non_integral_constant_expression_p
7273 = parser
->non_integral_constant_expression_p
;
7274 parser
->integral_constant_expression_p
= false;
7276 saved_greater_than_is_operator_p
7277 = parser
->greater_than_is_operator_p
;
7278 parser
->greater_than_is_operator_p
= true;
7280 ++cp_unevaluated_operand
;
7281 ++c_inhibit_evaluation_warnings
;
7282 ++cp_noexcept_operand
;
7283 expr
= cp_parser_expression (parser
);
7284 --cp_noexcept_operand
;
7285 --c_inhibit_evaluation_warnings
;
7286 --cp_unevaluated_operand
;
7288 parser
->greater_than_is_operator_p
7289 = saved_greater_than_is_operator_p
;
7291 parser
->integral_constant_expression_p
7292 = saved_integral_constant_expression_p
;
7293 parser
->non_integral_constant_expression_p
7294 = saved_non_integral_constant_expression_p
;
7296 parser
->type_definition_forbidden_message
= saved_message
;
7298 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7299 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7307 /* Look for the `:: new' and `:: delete', which also signal the
7308 beginning of a new-expression, or delete-expression,
7309 respectively. If the next token is `::', then it might be one of
7311 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7315 /* See if the token after the `::' is one of the keywords in
7316 which we're interested. */
7317 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7318 /* If it's `new', we have a new-expression. */
7319 if (keyword
== RID_NEW
)
7320 return cp_parser_new_expression (parser
);
7321 /* Similarly, for `delete'. */
7322 else if (keyword
== RID_DELETE
)
7323 return cp_parser_delete_expression (parser
);
7326 /* Look for a unary operator. */
7327 unary_operator
= cp_parser_unary_operator (token
);
7328 /* The `++' and `--' operators can be handled similarly, even though
7329 they are not technically unary-operators in the grammar. */
7330 if (unary_operator
== ERROR_MARK
)
7332 if (token
->type
== CPP_PLUS_PLUS
)
7333 unary_operator
= PREINCREMENT_EXPR
;
7334 else if (token
->type
== CPP_MINUS_MINUS
)
7335 unary_operator
= PREDECREMENT_EXPR
;
7336 /* Handle the GNU address-of-label extension. */
7337 else if (cp_parser_allow_gnu_extensions_p (parser
)
7338 && token
->type
== CPP_AND_AND
)
7342 location_t loc
= token
->location
;
7344 /* Consume the '&&' token. */
7345 cp_lexer_consume_token (parser
->lexer
);
7346 /* Look for the identifier. */
7347 identifier
= cp_parser_identifier (parser
);
7348 /* Create an expression representing the address. */
7349 expression
= finish_label_address_expr (identifier
, loc
);
7350 if (cp_parser_non_integral_constant_expression (parser
,
7352 expression
= error_mark_node
;
7356 if (unary_operator
!= ERROR_MARK
)
7358 tree cast_expression
;
7359 tree expression
= error_mark_node
;
7360 non_integral_constant non_constant_p
= NIC_NONE
;
7361 location_t loc
= token
->location
;
7362 tsubst_flags_t complain
= complain_flags (decltype_p
);
7364 /* Consume the operator token. */
7365 token
= cp_lexer_consume_token (parser
->lexer
);
7366 /* Parse the cast-expression. */
7368 = cp_parser_cast_expression (parser
,
7369 unary_operator
== ADDR_EXPR
,
7373 /* Now, build an appropriate representation. */
7374 switch (unary_operator
)
7377 non_constant_p
= NIC_STAR
;
7378 expression
= build_x_indirect_ref (loc
, cast_expression
,
7384 non_constant_p
= NIC_ADDR
;
7387 expression
= build_x_unary_op (loc
, unary_operator
,
7392 case PREINCREMENT_EXPR
:
7393 case PREDECREMENT_EXPR
:
7394 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7395 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7397 case UNARY_PLUS_EXPR
:
7399 case TRUTH_NOT_EXPR
:
7400 expression
= finish_unary_op_expr (loc
, unary_operator
,
7401 cast_expression
, complain
);
7408 if (non_constant_p
!= NIC_NONE
7409 && cp_parser_non_integral_constant_expression (parser
,
7411 expression
= error_mark_node
;
7416 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7417 /*member_access_only_p=*/false,
7422 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7423 unary-operator, the corresponding tree code is returned. */
7425 static enum tree_code
7426 cp_parser_unary_operator (cp_token
* token
)
7428 switch (token
->type
)
7431 return INDIRECT_REF
;
7437 return UNARY_PLUS_EXPR
;
7443 return TRUTH_NOT_EXPR
;
7446 return BIT_NOT_EXPR
;
7453 /* Parse a new-expression.
7456 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7457 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7459 Returns a representation of the expression. */
7462 cp_parser_new_expression (cp_parser
* parser
)
7464 bool global_scope_p
;
7465 vec
<tree
, va_gc
> *placement
;
7467 vec
<tree
, va_gc
> *initializer
;
7468 tree nelts
= NULL_TREE
;
7471 /* Look for the optional `::' operator. */
7473 = (cp_parser_global_scope_opt (parser
,
7474 /*current_scope_valid_p=*/false)
7476 /* Look for the `new' operator. */
7477 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7478 /* There's no easy way to tell a new-placement from the
7479 `( type-id )' construct. */
7480 cp_parser_parse_tentatively (parser
);
7481 /* Look for a new-placement. */
7482 placement
= cp_parser_new_placement (parser
);
7483 /* If that didn't work out, there's no new-placement. */
7484 if (!cp_parser_parse_definitely (parser
))
7486 if (placement
!= NULL
)
7487 release_tree_vector (placement
);
7491 /* If the next token is a `(', then we have a parenthesized
7493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7496 const char *saved_message
= parser
->type_definition_forbidden_message
;
7498 /* Consume the `('. */
7499 cp_lexer_consume_token (parser
->lexer
);
7501 /* Parse the type-id. */
7502 parser
->type_definition_forbidden_message
7503 = G_("types may not be defined in a new-expression");
7504 type
= cp_parser_type_id (parser
);
7505 parser
->type_definition_forbidden_message
= saved_message
;
7507 /* Look for the closing `)'. */
7508 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7509 token
= cp_lexer_peek_token (parser
->lexer
);
7510 /* There should not be a direct-new-declarator in this production,
7511 but GCC used to allowed this, so we check and emit a sensible error
7512 message for this case. */
7513 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7515 error_at (token
->location
,
7516 "array bound forbidden after parenthesized type-id");
7517 inform (token
->location
,
7518 "try removing the parentheses around the type-id");
7519 cp_parser_direct_new_declarator (parser
);
7522 /* Otherwise, there must be a new-type-id. */
7524 type
= cp_parser_new_type_id (parser
, &nelts
);
7526 /* If the next token is a `(' or '{', then we have a new-initializer. */
7527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7528 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7529 initializer
= cp_parser_new_initializer (parser
);
7533 /* A new-expression may not appear in an integral constant
7535 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7536 ret
= error_mark_node
;
7539 /* Create a representation of the new-expression. */
7540 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7541 tf_warning_or_error
);
7544 if (placement
!= NULL
)
7545 release_tree_vector (placement
);
7546 if (initializer
!= NULL
)
7547 release_tree_vector (initializer
);
7552 /* Parse a new-placement.
7557 Returns the same representation as for an expression-list. */
7559 static vec
<tree
, va_gc
> *
7560 cp_parser_new_placement (cp_parser
* parser
)
7562 vec
<tree
, va_gc
> *expression_list
;
7564 /* Parse the expression-list. */
7565 expression_list
= (cp_parser_parenthesized_expression_list
7566 (parser
, non_attr
, /*cast_p=*/false,
7567 /*allow_expansion_p=*/true,
7568 /*non_constant_p=*/NULL
));
7570 if (expression_list
&& expression_list
->is_empty ())
7571 error ("expected expression-list or type-id");
7573 return expression_list
;
7576 /* Parse a new-type-id.
7579 type-specifier-seq new-declarator [opt]
7581 Returns the TYPE allocated. If the new-type-id indicates an array
7582 type, *NELTS is set to the number of elements in the last array
7583 bound; the TYPE will not include the last array bound. */
7586 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7588 cp_decl_specifier_seq type_specifier_seq
;
7589 cp_declarator
*new_declarator
;
7590 cp_declarator
*declarator
;
7591 cp_declarator
*outer_declarator
;
7592 const char *saved_message
;
7594 /* The type-specifier sequence must not contain type definitions.
7595 (It cannot contain declarations of new types either, but if they
7596 are not definitions we will catch that because they are not
7598 saved_message
= parser
->type_definition_forbidden_message
;
7599 parser
->type_definition_forbidden_message
7600 = G_("types may not be defined in a new-type-id");
7601 /* Parse the type-specifier-seq. */
7602 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7603 /*is_trailing_return=*/false,
7604 &type_specifier_seq
);
7605 /* Restore the old message. */
7606 parser
->type_definition_forbidden_message
= saved_message
;
7608 if (type_specifier_seq
.type
== error_mark_node
)
7609 return error_mark_node
;
7611 /* Parse the new-declarator. */
7612 new_declarator
= cp_parser_new_declarator_opt (parser
);
7614 /* Determine the number of elements in the last array dimension, if
7617 /* Skip down to the last array dimension. */
7618 declarator
= new_declarator
;
7619 outer_declarator
= NULL
;
7620 while (declarator
&& (declarator
->kind
== cdk_pointer
7621 || declarator
->kind
== cdk_ptrmem
))
7623 outer_declarator
= declarator
;
7624 declarator
= declarator
->declarator
;
7627 && declarator
->kind
== cdk_array
7628 && declarator
->declarator
7629 && declarator
->declarator
->kind
== cdk_array
)
7631 outer_declarator
= declarator
;
7632 declarator
= declarator
->declarator
;
7635 if (declarator
&& declarator
->kind
== cdk_array
)
7637 *nelts
= declarator
->u
.array
.bounds
;
7638 if (*nelts
== error_mark_node
)
7639 *nelts
= integer_one_node
;
7641 if (outer_declarator
)
7642 outer_declarator
->declarator
= declarator
->declarator
;
7644 new_declarator
= NULL
;
7647 return groktypename (&type_specifier_seq
, new_declarator
, false);
7650 /* Parse an (optional) new-declarator.
7653 ptr-operator new-declarator [opt]
7654 direct-new-declarator
7656 Returns the declarator. */
7658 static cp_declarator
*
7659 cp_parser_new_declarator_opt (cp_parser
* parser
)
7661 enum tree_code code
;
7662 tree type
, std_attributes
= NULL_TREE
;
7663 cp_cv_quals cv_quals
;
7665 /* We don't know if there's a ptr-operator next, or not. */
7666 cp_parser_parse_tentatively (parser
);
7667 /* Look for a ptr-operator. */
7668 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7669 /* If that worked, look for more new-declarators. */
7670 if (cp_parser_parse_definitely (parser
))
7672 cp_declarator
*declarator
;
7674 /* Parse another optional declarator. */
7675 declarator
= cp_parser_new_declarator_opt (parser
);
7677 declarator
= cp_parser_make_indirect_declarator
7678 (code
, type
, cv_quals
, declarator
, std_attributes
);
7683 /* If the next token is a `[', there is a direct-new-declarator. */
7684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7685 return cp_parser_direct_new_declarator (parser
);
7690 /* Parse a direct-new-declarator.
7692 direct-new-declarator:
7694 direct-new-declarator [constant-expression]
7698 static cp_declarator
*
7699 cp_parser_direct_new_declarator (cp_parser
* parser
)
7701 cp_declarator
*declarator
= NULL
;
7708 /* Look for the opening `['. */
7709 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7711 token
= cp_lexer_peek_token (parser
->lexer
);
7712 expression
= cp_parser_expression (parser
);
7713 /* The standard requires that the expression have integral
7714 type. DR 74 adds enumeration types. We believe that the
7715 real intent is that these expressions be handled like the
7716 expression in a `switch' condition, which also allows
7717 classes with a single conversion to integral or
7718 enumeration type. */
7719 if (!processing_template_decl
)
7722 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7727 error_at (token
->location
,
7728 "expression in new-declarator must have integral "
7729 "or enumeration type");
7730 expression
= error_mark_node
;
7734 /* Look for the closing `]'. */
7735 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7737 /* Add this bound to the declarator. */
7738 declarator
= make_array_declarator (declarator
, expression
);
7740 /* If the next token is not a `[', then there are no more
7742 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7749 /* Parse a new-initializer.
7752 ( expression-list [opt] )
7755 Returns a representation of the expression-list. */
7757 static vec
<tree
, va_gc
> *
7758 cp_parser_new_initializer (cp_parser
* parser
)
7760 vec
<tree
, va_gc
> *expression_list
;
7762 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7765 bool expr_non_constant_p
;
7766 cp_lexer_set_source_position (parser
->lexer
);
7767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7768 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7769 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7770 expression_list
= make_tree_vector_single (t
);
7773 expression_list
= (cp_parser_parenthesized_expression_list
7774 (parser
, non_attr
, /*cast_p=*/false,
7775 /*allow_expansion_p=*/true,
7776 /*non_constant_p=*/NULL
));
7778 return expression_list
;
7781 /* Parse a delete-expression.
7784 :: [opt] delete cast-expression
7785 :: [opt] delete [ ] cast-expression
7787 Returns a representation of the expression. */
7790 cp_parser_delete_expression (cp_parser
* parser
)
7792 bool global_scope_p
;
7796 /* Look for the optional `::' operator. */
7798 = (cp_parser_global_scope_opt (parser
,
7799 /*current_scope_valid_p=*/false)
7801 /* Look for the `delete' keyword. */
7802 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7803 /* See if the array syntax is in use. */
7804 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7806 /* Consume the `[' token. */
7807 cp_lexer_consume_token (parser
->lexer
);
7808 /* Look for the `]' token. */
7809 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7810 /* Remember that this is the `[]' construct. */
7816 /* Parse the cast-expression. */
7817 expression
= cp_parser_simple_cast_expression (parser
);
7819 /* A delete-expression may not appear in an integral constant
7821 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7822 return error_mark_node
;
7824 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7825 tf_warning_or_error
);
7828 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7829 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7833 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7835 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7836 switch (token
->type
)
7842 case CPP_CLOSE_SQUARE
:
7843 case CPP_CLOSE_PAREN
:
7844 case CPP_CLOSE_BRACE
:
7845 case CPP_OPEN_BRACE
:
7849 case CPP_DEREF_STAR
:
7857 case CPP_GREATER_EQ
:
7878 case CPP_OPEN_PAREN
:
7879 /* In ((type ()) () the last () isn't a valid cast-expression,
7880 so the whole must be parsed as postfix-expression. */
7881 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7884 case CPP_OPEN_SQUARE
:
7885 /* '[' may start a primary-expression in obj-c++ and in C++11,
7886 as a lambda-expression, eg, '(void)[]{}'. */
7887 if (cxx_dialect
>= cxx11
)
7889 return c_dialect_objc ();
7892 case CPP_MINUS_MINUS
:
7893 /* '++' and '--' may or may not start a cast-expression:
7895 struct T { void operator++(int); };
7896 void f() { (T())++; }
7909 /* Parse a cast-expression.
7913 ( type-id ) cast-expression
7915 ADDRESS_P is true iff the unary-expression is appearing as the
7916 operand of the `&' operator. CAST_P is true if this expression is
7917 the target of a cast.
7919 Returns a representation of the expression. */
7922 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7923 bool decltype_p
, cp_id_kind
* pidk
)
7925 /* If it's a `(', then we might be looking at a cast. */
7926 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7928 tree type
= NULL_TREE
;
7929 tree expr
= NULL_TREE
;
7930 int cast_expression
= 0;
7931 const char *saved_message
;
7933 /* There's no way to know yet whether or not this is a cast.
7934 For example, `(int (3))' is a unary-expression, while `(int)
7935 3' is a cast. So, we resort to parsing tentatively. */
7936 cp_parser_parse_tentatively (parser
);
7937 /* Types may not be defined in a cast. */
7938 saved_message
= parser
->type_definition_forbidden_message
;
7939 parser
->type_definition_forbidden_message
7940 = G_("types may not be defined in casts");
7941 /* Consume the `('. */
7942 cp_lexer_consume_token (parser
->lexer
);
7943 /* A very tricky bit is that `(struct S) { 3 }' is a
7944 compound-literal (which we permit in C++ as an extension).
7945 But, that construct is not a cast-expression -- it is a
7946 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7947 is legal; if the compound-literal were a cast-expression,
7948 you'd need an extra set of parentheses.) But, if we parse
7949 the type-id, and it happens to be a class-specifier, then we
7950 will commit to the parse at that point, because we cannot
7951 undo the action that is done when creating a new class. So,
7952 then we cannot back up and do a postfix-expression.
7954 Another tricky case is the following (c++/29234):
7956 struct S { void operator () (); };
7963 As a type-id we parse the parenthesized S()() as a function
7964 returning a function, groktypename complains and we cannot
7965 back up in this case either.
7967 Therefore, we scan ahead to the closing `)', and check to see
7968 if the tokens after the `)' can start a cast-expression. Otherwise
7969 we are dealing with an unary-expression, a postfix-expression
7972 Yet another tricky case, in C++11, is the following (c++/54891):
7976 The issue is that usually, besides the case of lambda-expressions,
7977 the parenthesized type-id cannot be followed by '[', and, eg, we
7978 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7979 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7980 we don't commit, we try a cast-expression, then an unary-expression.
7982 Save tokens so that we can put them back. */
7983 cp_lexer_save_tokens (parser
->lexer
);
7985 /* We may be looking at a cast-expression. */
7986 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7987 /*consume_paren=*/true))
7989 = cp_parser_tokens_start_cast_expression (parser
);
7991 /* Roll back the tokens we skipped. */
7992 cp_lexer_rollback_tokens (parser
->lexer
);
7993 /* If we aren't looking at a cast-expression, simulate an error so
7994 that the call to cp_parser_error_occurred below returns true. */
7995 if (!cast_expression
)
7996 cp_parser_simulate_error (parser
);
7999 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8000 parser
->in_type_id_in_expr_p
= true;
8001 /* Look for the type-id. */
8002 type
= cp_parser_type_id (parser
);
8003 /* Look for the closing `)'. */
8004 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8005 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8008 /* Restore the saved message. */
8009 parser
->type_definition_forbidden_message
= saved_message
;
8011 /* At this point this can only be either a cast or a
8012 parenthesized ctor such as `(T ())' that looks like a cast to
8013 function returning T. */
8014 if (!cp_parser_error_occurred (parser
))
8016 /* Only commit if the cast-expression doesn't start with
8017 '++', '--', or '[' in C++11. */
8018 if (cast_expression
> 0)
8019 cp_parser_commit_to_topmost_tentative_parse (parser
);
8021 expr
= cp_parser_cast_expression (parser
,
8022 /*address_p=*/false,
8024 /*decltype_p=*/false,
8027 if (cp_parser_parse_definitely (parser
))
8029 /* Warn about old-style casts, if so requested. */
8030 if (warn_old_style_cast
8031 && !in_system_header_at (input_location
)
8032 && !VOID_TYPE_P (type
)
8033 && current_lang_name
!= lang_name_c
)
8034 warning (OPT_Wold_style_cast
, "use of old-style cast");
8036 /* Only type conversions to integral or enumeration types
8037 can be used in constant-expressions. */
8038 if (!cast_valid_in_integral_constant_expression_p (type
)
8039 && cp_parser_non_integral_constant_expression (parser
,
8041 return error_mark_node
;
8043 /* Perform the cast. */
8044 expr
= build_c_cast (input_location
, type
, expr
);
8049 cp_parser_abort_tentative_parse (parser
);
8052 /* If we get here, then it's not a cast, so it must be a
8053 unary-expression. */
8054 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8055 cast_p
, decltype_p
);
8058 /* Parse a binary expression of the general form:
8062 pm-expression .* cast-expression
8063 pm-expression ->* cast-expression
8065 multiplicative-expression:
8067 multiplicative-expression * pm-expression
8068 multiplicative-expression / pm-expression
8069 multiplicative-expression % pm-expression
8071 additive-expression:
8072 multiplicative-expression
8073 additive-expression + multiplicative-expression
8074 additive-expression - multiplicative-expression
8078 shift-expression << additive-expression
8079 shift-expression >> additive-expression
8081 relational-expression:
8083 relational-expression < shift-expression
8084 relational-expression > shift-expression
8085 relational-expression <= shift-expression
8086 relational-expression >= shift-expression
8090 relational-expression:
8091 relational-expression <? shift-expression
8092 relational-expression >? shift-expression
8094 equality-expression:
8095 relational-expression
8096 equality-expression == relational-expression
8097 equality-expression != relational-expression
8101 and-expression & equality-expression
8103 exclusive-or-expression:
8105 exclusive-or-expression ^ and-expression
8107 inclusive-or-expression:
8108 exclusive-or-expression
8109 inclusive-or-expression | exclusive-or-expression
8111 logical-and-expression:
8112 inclusive-or-expression
8113 logical-and-expression && inclusive-or-expression
8115 logical-or-expression:
8116 logical-and-expression
8117 logical-or-expression || logical-and-expression
8119 All these are implemented with a single function like:
8122 simple-cast-expression
8123 binary-expression <token> binary-expression
8125 CAST_P is true if this expression is the target of a cast.
8127 The binops_by_token map is used to get the tree codes for each <token> type.
8128 binary-expressions are associated according to a precedence table. */
8130 #define TOKEN_PRECEDENCE(token) \
8131 (((token->type == CPP_GREATER \
8132 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8133 && !parser->greater_than_is_operator_p) \
8134 ? PREC_NOT_OPERATOR \
8135 : binops_by_token[token->type].prec)
8138 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8139 bool no_toplevel_fold_p
,
8141 enum cp_parser_prec prec
,
8144 cp_parser_expression_stack stack
;
8145 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8146 cp_parser_expression_stack_entry current
;
8149 enum tree_code rhs_type
;
8150 enum cp_parser_prec new_prec
, lookahead_prec
;
8153 /* Parse the first expression. */
8154 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8155 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8156 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8157 cast_p
, decltype_p
, pidk
);
8158 current
.prec
= prec
;
8160 if (cp_parser_error_occurred (parser
))
8161 return error_mark_node
;
8165 /* Get an operator token. */
8166 token
= cp_lexer_peek_token (parser
->lexer
);
8168 if (warn_cxx11_compat
8169 && token
->type
== CPP_RSHIFT
8170 && !parser
->greater_than_is_operator_p
)
8172 if (warning_at (token
->location
, OPT_Wc__11_compat
,
8173 "%<>>%> operator is treated"
8174 " as two right angle brackets in C++11"))
8175 inform (token
->location
,
8176 "suggest parentheses around %<>>%> expression");
8179 new_prec
= TOKEN_PRECEDENCE (token
);
8181 /* Popping an entry off the stack means we completed a subexpression:
8182 - either we found a token which is not an operator (`>' where it is not
8183 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8184 will happen repeatedly;
8185 - or, we found an operator which has lower priority. This is the case
8186 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8188 if (new_prec
<= current
.prec
)
8197 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8198 current
.loc
= token
->location
;
8200 /* We used the operator token. */
8201 cp_lexer_consume_token (parser
->lexer
);
8203 /* For "false && x" or "true || x", x will never be executed;
8204 disable warnings while evaluating it. */
8205 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8206 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8207 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8208 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8210 /* Extract another operand. It may be the RHS of this expression
8211 or the LHS of a new, higher priority expression. */
8212 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8213 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8214 rhs
= cp_parser_simple_cast_expression (parser
);
8216 /* Get another operator token. Look up its precedence to avoid
8217 building a useless (immediately popped) stack entry for common
8218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8219 token
= cp_lexer_peek_token (parser
->lexer
);
8220 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8221 if (lookahead_prec
> new_prec
)
8223 /* ... and prepare to parse the RHS of the new, higher priority
8224 expression. Since precedence levels on the stack are
8225 monotonically increasing, we do not have to care about
8230 current
.lhs_type
= rhs_type
;
8231 current
.prec
= new_prec
;
8232 new_prec
= lookahead_prec
;
8236 lookahead_prec
= new_prec
;
8237 /* If the stack is not empty, we have parsed into LHS the right side
8238 (`4' in the example above) of an expression we had suspended.
8239 We can use the information on the stack to recover the LHS (`3')
8240 from the stack together with the tree code (`MULT_EXPR'), and
8241 the precedence of the higher level subexpression
8242 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8243 which will be used to actually build the additive expression. */
8245 rhs_type
= current
.lhs_type
;
8250 /* Undo the disabling of warnings done above. */
8251 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8252 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8253 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8254 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8256 if (warn_logical_not_paren
8257 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
8258 && current
.lhs_type
== TRUTH_NOT_EXPR
8259 /* Avoid warning for !!x == y. */
8260 && (TREE_CODE (current
.lhs
) != NE_EXPR
8261 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
8262 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
8263 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
8264 /* Avoid warning for !b == y where b is boolean. */
8265 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
8266 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
8268 /* Avoid warning for !!b == y where b is boolean. */
8269 && (!DECL_P (current
.lhs
)
8270 || TREE_TYPE (current
.lhs
) == NULL_TREE
8271 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
8272 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
8273 maybe_constant_value (rhs
));
8276 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8277 ERROR_MARK for everything that is not a binary expression.
8278 This makes warn_about_parentheses miss some warnings that
8279 involve unary operators. For unary expressions we should
8280 pass the correct tree_code unless the unary expression was
8281 surrounded by parentheses.
8283 if (no_toplevel_fold_p
8284 && lookahead_prec
<= current
.prec
8286 current
.lhs
= build2 (current
.tree_type
,
8287 TREE_CODE_CLASS (current
.tree_type
)
8289 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8292 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8293 current
.lhs
, current
.lhs_type
,
8294 rhs
, rhs_type
, &overload
,
8295 complain_flags (decltype_p
));
8296 current
.lhs_type
= current
.tree_type
;
8297 if (EXPR_P (current
.lhs
))
8298 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8300 /* If the binary operator required the use of an overloaded operator,
8301 then this expression cannot be an integral constant-expression.
8302 An overloaded operator can be used even if both operands are
8303 otherwise permissible in an integral constant-expression if at
8304 least one of the operands is of enumeration type. */
8307 && cp_parser_non_integral_constant_expression (parser
,
8309 return error_mark_node
;
8316 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8317 bool no_toplevel_fold_p
,
8318 enum cp_parser_prec prec
,
8321 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8322 /*decltype*/false, prec
, pidk
);
8325 /* Parse the `? expression : assignment-expression' part of a
8326 conditional-expression. The LOGICAL_OR_EXPR is the
8327 logical-or-expression that started the conditional-expression.
8328 Returns a representation of the entire conditional-expression.
8330 This routine is used by cp_parser_assignment_expression.
8332 ? expression : assignment-expression
8336 ? : assignment-expression */
8339 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8342 tree assignment_expr
;
8343 struct cp_token
*token
;
8344 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8346 /* Consume the `?' token. */
8347 cp_lexer_consume_token (parser
->lexer
);
8348 token
= cp_lexer_peek_token (parser
->lexer
);
8349 if (cp_parser_allow_gnu_extensions_p (parser
)
8350 && token
->type
== CPP_COLON
)
8352 pedwarn (token
->location
, OPT_Wpedantic
,
8353 "ISO C++ does not allow ?: with omitted middle operand");
8354 /* Implicit true clause. */
8356 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8357 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8361 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8362 parser
->colon_corrects_to_scope_p
= false;
8363 /* Parse the expression. */
8364 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8365 expr
= cp_parser_expression (parser
);
8366 c_inhibit_evaluation_warnings
+=
8367 ((logical_or_expr
== truthvalue_true_node
)
8368 - (logical_or_expr
== truthvalue_false_node
));
8369 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8372 /* The next token should be a `:'. */
8373 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8374 /* Parse the assignment-expression. */
8375 assignment_expr
= cp_parser_assignment_expression (parser
);
8376 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8378 /* Build the conditional-expression. */
8379 return build_x_conditional_expr (loc
, logical_or_expr
,
8382 tf_warning_or_error
);
8385 /* Parse an assignment-expression.
8387 assignment-expression:
8388 conditional-expression
8389 logical-or-expression assignment-operator assignment_expression
8392 CAST_P is true if this expression is the target of a cast.
8393 DECLTYPE_P is true if this expression is the operand of decltype.
8395 Returns a representation for the expression. */
8398 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8399 bool cast_p
, bool decltype_p
)
8403 /* If the next token is the `throw' keyword, then we're looking at
8404 a throw-expression. */
8405 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8406 expr
= cp_parser_throw_expression (parser
);
8407 /* Otherwise, it must be that we are looking at a
8408 logical-or-expression. */
8411 /* Parse the binary expressions (logical-or-expression). */
8412 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8414 PREC_NOT_OPERATOR
, pidk
);
8415 /* If the next token is a `?' then we're actually looking at a
8416 conditional-expression. */
8417 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8418 return cp_parser_question_colon_clause (parser
, expr
);
8421 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8423 /* If it's an assignment-operator, we're using the second
8425 enum tree_code assignment_operator
8426 = cp_parser_assignment_operator_opt (parser
);
8427 if (assignment_operator
!= ERROR_MARK
)
8429 bool non_constant_p
;
8430 location_t saved_input_location
;
8432 /* Parse the right-hand side of the assignment. */
8433 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8435 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8436 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8438 /* An assignment may not appear in a
8439 constant-expression. */
8440 if (cp_parser_non_integral_constant_expression (parser
,
8442 return error_mark_node
;
8443 /* Build the assignment expression. Its default
8444 location is the location of the '=' token. */
8445 saved_input_location
= input_location
;
8446 input_location
= loc
;
8447 expr
= build_x_modify_expr (loc
, expr
,
8448 assignment_operator
,
8450 complain_flags (decltype_p
));
8451 input_location
= saved_input_location
;
8459 /* Parse an (optional) assignment-operator.
8461 assignment-operator: one of
8462 = *= /= %= += -= >>= <<= &= ^= |=
8466 assignment-operator: one of
8469 If the next token is an assignment operator, the corresponding tree
8470 code is returned, and the token is consumed. For example, for
8471 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8472 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8473 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8474 operator, ERROR_MARK is returned. */
8476 static enum tree_code
8477 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8482 /* Peek at the next token. */
8483 token
= cp_lexer_peek_token (parser
->lexer
);
8485 switch (token
->type
)
8496 op
= TRUNC_DIV_EXPR
;
8500 op
= TRUNC_MOD_EXPR
;
8532 /* Nothing else is an assignment operator. */
8536 /* If it was an assignment operator, consume it. */
8537 if (op
!= ERROR_MARK
)
8538 cp_lexer_consume_token (parser
->lexer
);
8543 /* Parse an expression.
8546 assignment-expression
8547 expression , assignment-expression
8549 CAST_P is true if this expression is the target of a cast.
8550 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8551 except possibly parenthesized or on the RHS of a comma (N3276).
8553 Returns a representation of the expression. */
8556 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8557 bool cast_p
, bool decltype_p
)
8559 tree expression
= NULL_TREE
;
8560 location_t loc
= UNKNOWN_LOCATION
;
8564 tree assignment_expression
;
8566 /* Parse the next assignment-expression. */
8567 assignment_expression
8568 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8570 /* We don't create a temporary for a call that is the immediate operand
8571 of decltype or on the RHS of a comma. But when we see a comma, we
8572 need to create a temporary for a call on the LHS. */
8573 if (decltype_p
&& !processing_template_decl
8574 && TREE_CODE (assignment_expression
) == CALL_EXPR
8575 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8576 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8577 assignment_expression
8578 = build_cplus_new (TREE_TYPE (assignment_expression
),
8579 assignment_expression
, tf_warning_or_error
);
8581 /* If this is the first assignment-expression, we can just
8584 expression
= assignment_expression
;
8586 expression
= build_x_compound_expr (loc
, expression
,
8587 assignment_expression
,
8588 complain_flags (decltype_p
));
8589 /* If the next token is not a comma, then we are done with the
8591 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8593 /* Consume the `,'. */
8594 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8595 cp_lexer_consume_token (parser
->lexer
);
8596 /* A comma operator cannot appear in a constant-expression. */
8597 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8598 expression
= error_mark_node
;
8604 /* Parse a constant-expression.
8606 constant-expression:
8607 conditional-expression
8609 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8610 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8611 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8612 is false, NON_CONSTANT_P should be NULL. */
8615 cp_parser_constant_expression (cp_parser
* parser
,
8616 bool allow_non_constant_p
,
8617 bool *non_constant_p
)
8619 bool saved_integral_constant_expression_p
;
8620 bool saved_allow_non_integral_constant_expression_p
;
8621 bool saved_non_integral_constant_expression_p
;
8624 /* It might seem that we could simply parse the
8625 conditional-expression, and then check to see if it were
8626 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8627 one that the compiler can figure out is constant, possibly after
8628 doing some simplifications or optimizations. The standard has a
8629 precise definition of constant-expression, and we must honor
8630 that, even though it is somewhat more restrictive.
8636 is not a legal declaration, because `(2, 3)' is not a
8637 constant-expression. The `,' operator is forbidden in a
8638 constant-expression. However, GCC's constant-folding machinery
8639 will fold this operation to an INTEGER_CST for `3'. */
8641 /* Save the old settings. */
8642 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8643 saved_allow_non_integral_constant_expression_p
8644 = parser
->allow_non_integral_constant_expression_p
;
8645 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8646 /* We are now parsing a constant-expression. */
8647 parser
->integral_constant_expression_p
= true;
8648 parser
->allow_non_integral_constant_expression_p
8649 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8650 parser
->non_integral_constant_expression_p
= false;
8651 /* Although the grammar says "conditional-expression", we parse an
8652 "assignment-expression", which also permits "throw-expression"
8653 and the use of assignment operators. In the case that
8654 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8655 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8656 actually essential that we look for an assignment-expression.
8657 For example, cp_parser_initializer_clauses uses this function to
8658 determine whether a particular assignment-expression is in fact
8660 expression
= cp_parser_assignment_expression (parser
);
8661 /* Restore the old settings. */
8662 parser
->integral_constant_expression_p
8663 = saved_integral_constant_expression_p
;
8664 parser
->allow_non_integral_constant_expression_p
8665 = saved_allow_non_integral_constant_expression_p
;
8666 if (cxx_dialect
>= cxx11
)
8668 /* Require an rvalue constant expression here; that's what our
8669 callers expect. Reference constant expressions are handled
8670 separately in e.g. cp_parser_template_argument. */
8671 bool is_const
= potential_rvalue_constant_expression (expression
);
8672 parser
->non_integral_constant_expression_p
= !is_const
;
8673 if (!is_const
&& !allow_non_constant_p
)
8674 require_potential_rvalue_constant_expression (expression
);
8676 if (allow_non_constant_p
)
8677 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8678 parser
->non_integral_constant_expression_p
8679 = saved_non_integral_constant_expression_p
;
8684 /* Parse __builtin_offsetof.
8686 offsetof-expression:
8687 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8689 offsetof-member-designator:
8691 | offsetof-member-designator "." id-expression
8692 | offsetof-member-designator "[" expression "]"
8693 | offsetof-member-designator "->" id-expression */
8696 cp_parser_builtin_offsetof (cp_parser
*parser
)
8698 int save_ice_p
, save_non_ice_p
;
8703 /* We're about to accept non-integral-constant things, but will
8704 definitely yield an integral constant expression. Save and
8705 restore these values around our local parsing. */
8706 save_ice_p
= parser
->integral_constant_expression_p
;
8707 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8709 /* Consume the "__builtin_offsetof" token. */
8710 cp_lexer_consume_token (parser
->lexer
);
8711 /* Consume the opening `('. */
8712 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8713 /* Parse the type-id. */
8714 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8715 type
= cp_parser_type_id (parser
);
8716 /* Look for the `,'. */
8717 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8718 token
= cp_lexer_peek_token (parser
->lexer
);
8720 /* Build the (type *)null that begins the traditional offsetof macro. */
8721 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8722 tf_warning_or_error
);
8724 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8725 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8726 true, &dummy
, token
->location
);
8729 token
= cp_lexer_peek_token (parser
->lexer
);
8730 switch (token
->type
)
8732 case CPP_OPEN_SQUARE
:
8733 /* offsetof-member-designator "[" expression "]" */
8734 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8739 /* offsetof-member-designator "->" identifier */
8740 expr
= grok_array_decl (token
->location
, expr
,
8741 integer_zero_node
, false);
8745 /* offsetof-member-designator "." identifier */
8746 cp_lexer_consume_token (parser
->lexer
);
8747 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8752 case CPP_CLOSE_PAREN
:
8753 /* Consume the ")" token. */
8754 cp_lexer_consume_token (parser
->lexer
);
8758 /* Error. We know the following require will fail, but
8759 that gives the proper error message. */
8760 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8761 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8762 expr
= error_mark_node
;
8768 expr
= finish_offsetof (expr
, loc
);
8771 parser
->integral_constant_expression_p
= save_ice_p
;
8772 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8777 /* Parse a trait expression.
8779 Returns a representation of the expression, the underlying type
8780 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8783 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8786 tree type1
, type2
= NULL_TREE
;
8787 bool binary
= false;
8788 bool variadic
= false;
8792 case RID_HAS_NOTHROW_ASSIGN
:
8793 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8795 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8796 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8798 case RID_HAS_NOTHROW_COPY
:
8799 kind
= CPTK_HAS_NOTHROW_COPY
;
8801 case RID_HAS_TRIVIAL_ASSIGN
:
8802 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8804 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8805 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8807 case RID_HAS_TRIVIAL_COPY
:
8808 kind
= CPTK_HAS_TRIVIAL_COPY
;
8810 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8811 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8813 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8814 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8816 case RID_IS_ABSTRACT
:
8817 kind
= CPTK_IS_ABSTRACT
;
8819 case RID_IS_BASE_OF
:
8820 kind
= CPTK_IS_BASE_OF
;
8824 kind
= CPTK_IS_CLASS
;
8827 kind
= CPTK_IS_EMPTY
;
8830 kind
= CPTK_IS_ENUM
;
8833 kind
= CPTK_IS_FINAL
;
8835 case RID_IS_LITERAL_TYPE
:
8836 kind
= CPTK_IS_LITERAL_TYPE
;
8841 case RID_IS_POLYMORPHIC
:
8842 kind
= CPTK_IS_POLYMORPHIC
;
8844 case RID_IS_STD_LAYOUT
:
8845 kind
= CPTK_IS_STD_LAYOUT
;
8847 case RID_IS_TRIVIAL
:
8848 kind
= CPTK_IS_TRIVIAL
;
8850 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8851 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8854 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8855 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8858 case RID_IS_TRIVIALLY_COPYABLE
:
8859 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8862 kind
= CPTK_IS_UNION
;
8864 case RID_UNDERLYING_TYPE
:
8865 kind
= CPTK_UNDERLYING_TYPE
;
8870 case RID_DIRECT_BASES
:
8871 kind
= CPTK_DIRECT_BASES
;
8877 /* Consume the token. */
8878 cp_lexer_consume_token (parser
->lexer
);
8880 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8882 type1
= cp_parser_type_id (parser
);
8884 if (type1
== error_mark_node
)
8885 return error_mark_node
;
8889 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8891 type2
= cp_parser_type_id (parser
);
8893 if (type2
== error_mark_node
)
8894 return error_mark_node
;
8898 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8900 cp_lexer_consume_token (parser
->lexer
);
8901 tree elt
= cp_parser_type_id (parser
);
8902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8904 cp_lexer_consume_token (parser
->lexer
);
8905 elt
= make_pack_expansion (elt
);
8907 if (elt
== error_mark_node
)
8908 return error_mark_node
;
8909 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8913 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8915 /* Complete the trait expression, which may mean either processing
8916 the trait expr now or saving it for template instantiation. */
8919 case CPTK_UNDERLYING_TYPE
:
8920 return finish_underlying_type (type1
);
8922 return finish_bases (type1
, false);
8923 case CPTK_DIRECT_BASES
:
8924 return finish_bases (type1
, true);
8926 return finish_trait_expr (kind
, type1
, type2
);
8930 /* Lambdas that appear in variable initializer or default argument scope
8931 get that in their mangling, so we need to record it. We might as well
8932 use the count for function and namespace scopes as well. */
8933 static GTY(()) tree lambda_scope
;
8934 static GTY(()) int lambda_count
;
8935 typedef struct GTY(()) tree_int
8940 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8943 start_lambda_scope (tree decl
)
8947 /* Once we're inside a function, we ignore other scopes and just push
8948 the function again so that popping works properly. */
8949 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8950 decl
= current_function_decl
;
8951 ti
.t
= lambda_scope
;
8952 ti
.i
= lambda_count
;
8953 vec_safe_push (lambda_scope_stack
, ti
);
8954 if (lambda_scope
!= decl
)
8956 /* Don't reset the count if we're still in the same function. */
8957 lambda_scope
= decl
;
8963 record_lambda_scope (tree lambda
)
8965 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8966 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8970 finish_lambda_scope (void)
8972 tree_int
*p
= &lambda_scope_stack
->last ();
8973 if (lambda_scope
!= p
->t
)
8975 lambda_scope
= p
->t
;
8976 lambda_count
= p
->i
;
8978 lambda_scope_stack
->pop ();
8981 /* Parse a lambda expression.
8984 lambda-introducer lambda-declarator [opt] compound-statement
8986 Returns a representation of the expression. */
8989 cp_parser_lambda_expression (cp_parser
* parser
)
8991 tree lambda_expr
= build_lambda_expr ();
8994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8995 cp_token_position start
= 0;
8997 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8999 if (cp_unevaluated_operand
)
9001 if (!token
->error_reported
)
9003 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9004 "lambda-expression in unevaluated context");
9005 token
->error_reported
= true;
9009 else if (parser
->in_template_argument_list_p
)
9011 if (!token
->error_reported
)
9013 error_at (token
->location
, "lambda-expression in template-argument");
9014 token
->error_reported
= true;
9019 /* We may be in the middle of deferred access check. Disable
9021 push_deferring_access_checks (dk_no_deferred
);
9023 cp_parser_lambda_introducer (parser
, lambda_expr
);
9025 type
= begin_lambda_type (lambda_expr
);
9026 if (type
== error_mark_node
)
9027 return error_mark_node
;
9029 record_lambda_scope (lambda_expr
);
9031 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9032 determine_visibility (TYPE_NAME (type
));
9034 /* Now that we've started the type, add the capture fields for any
9035 explicit captures. */
9036 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9039 /* Inside the class, surrounding template-parameter-lists do not apply. */
9040 unsigned int saved_num_template_parameter_lists
9041 = parser
->num_template_parameter_lists
;
9042 unsigned char in_statement
= parser
->in_statement
;
9043 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9044 bool fully_implicit_function_template_p
9045 = parser
->fully_implicit_function_template_p
;
9046 tree implicit_template_parms
= parser
->implicit_template_parms
;
9047 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9048 bool auto_is_implicit_function_template_parm_p
9049 = parser
->auto_is_implicit_function_template_parm_p
;
9051 parser
->num_template_parameter_lists
= 0;
9052 parser
->in_statement
= 0;
9053 parser
->in_switch_statement_p
= false;
9054 parser
->fully_implicit_function_template_p
= false;
9055 parser
->implicit_template_parms
= 0;
9056 parser
->implicit_template_scope
= 0;
9057 parser
->auto_is_implicit_function_template_parm_p
= false;
9059 /* By virtue of defining a local class, a lambda expression has access to
9060 the private variables of enclosing classes. */
9062 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9066 if (!cp_parser_error_occurred (parser
)
9067 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9068 && cp_parser_start_tentative_firewall (parser
))
9070 cp_parser_lambda_body (parser
, lambda_expr
);
9072 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9074 if (cp_parser_skip_to_closing_brace (parser
))
9075 cp_lexer_consume_token (parser
->lexer
);
9078 /* The capture list was built up in reverse order; fix that now. */
9079 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9080 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9083 maybe_add_lambda_conv_op (type
);
9085 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9087 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9088 parser
->in_statement
= in_statement
;
9089 parser
->in_switch_statement_p
= in_switch_statement_p
;
9090 parser
->fully_implicit_function_template_p
9091 = fully_implicit_function_template_p
;
9092 parser
->implicit_template_parms
= implicit_template_parms
;
9093 parser
->implicit_template_scope
= implicit_template_scope
;
9094 parser
->auto_is_implicit_function_template_parm_p
9095 = auto_is_implicit_function_template_parm_p
;
9098 pop_deferring_access_checks ();
9100 /* This field is only used during parsing of the lambda. */
9101 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9103 /* This lambda shouldn't have any proxies left at this point. */
9104 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9105 /* And now that we're done, push proxies for an enclosing lambda. */
9106 insert_pending_capture_proxies ();
9109 lambda_expr
= build_lambda_object (lambda_expr
);
9111 lambda_expr
= error_mark_node
;
9113 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9118 /* Parse the beginning of a lambda expression.
9121 [ lambda-capture [opt] ]
9123 LAMBDA_EXPR is the current representation of the lambda expression. */
9126 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9128 /* Need commas after the first capture. */
9131 /* Eat the leading `['. */
9132 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9134 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9136 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9137 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9138 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9139 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9143 cp_lexer_consume_token (parser
->lexer
);
9147 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9149 cp_token
* capture_token
;
9151 tree capture_init_expr
;
9152 cp_id_kind idk
= CP_ID_KIND_NONE
;
9153 bool explicit_init_p
= false;
9155 enum capture_kind_type
9160 enum capture_kind_type capture_kind
= BY_COPY
;
9162 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9164 error ("expected end of capture-list");
9171 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9173 /* Possibly capture `this'. */
9174 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9176 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9177 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9178 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9179 "with by-copy capture default");
9180 cp_lexer_consume_token (parser
->lexer
);
9181 add_capture (lambda_expr
,
9182 /*id=*/this_identifier
,
9183 /*initializer=*/finish_this_expr(),
9184 /*by_reference_p=*/false,
9189 /* Remember whether we want to capture as a reference or not. */
9190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9192 capture_kind
= BY_REFERENCE
;
9193 cp_lexer_consume_token (parser
->lexer
);
9196 /* Get the identifier. */
9197 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9198 capture_id
= cp_parser_identifier (parser
);
9200 if (capture_id
== error_mark_node
)
9201 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9202 delimiters, but I modified this to stop on unnested ']' as well. It
9203 was already changed to stop on unnested '}', so the
9204 "closing_parenthesis" name is no more misleading with my change. */
9206 cp_parser_skip_to_closing_parenthesis (parser
,
9207 /*recovering=*/true,
9209 /*consume_paren=*/true);
9213 /* Find the initializer for this capture. */
9214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9215 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9216 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9218 bool direct
, non_constant
;
9219 /* An explicit initializer exists. */
9220 if (cxx_dialect
< cxx14
)
9221 pedwarn (input_location
, 0,
9222 "lambda capture initializers "
9223 "only available with -std=c++14 or -std=gnu++14");
9224 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9226 explicit_init_p
= true;
9227 if (capture_init_expr
== NULL_TREE
)
9229 error ("empty initializer for lambda init-capture");
9230 capture_init_expr
= error_mark_node
;
9235 const char* error_msg
;
9237 /* Turn the identifier into an id-expression. */
9239 = cp_parser_lookup_name_simple (parser
, capture_id
,
9240 capture_token
->location
);
9242 if (capture_init_expr
== error_mark_node
)
9244 unqualified_name_lookup_error (capture_id
);
9247 else if (DECL_P (capture_init_expr
)
9248 && (!VAR_P (capture_init_expr
)
9249 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9251 error_at (capture_token
->location
,
9252 "capture of non-variable %qD ",
9254 inform (0, "%q+#D declared here", capture_init_expr
);
9257 if (VAR_P (capture_init_expr
)
9258 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9260 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9261 "%qD with non-automatic storage duration",
9263 inform (0, "%q+#D declared here", capture_init_expr
);
9268 = finish_id_expression
9273 /*integral_constant_expression_p=*/false,
9274 /*allow_non_integral_constant_expression_p=*/false,
9275 /*non_integral_constant_expression_p=*/NULL
,
9276 /*template_p=*/false,
9278 /*address_p=*/false,
9279 /*template_arg_p=*/false,
9281 capture_token
->location
);
9283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9285 cp_lexer_consume_token (parser
->lexer
);
9286 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9289 check_for_bare_parameter_packs (capture_init_expr
);
9292 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9293 && !explicit_init_p
)
9295 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9296 && capture_kind
== BY_COPY
)
9297 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9298 "of %qD redundant with by-copy capture default",
9300 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9301 && capture_kind
== BY_REFERENCE
)
9302 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9303 "capture of %qD redundant with by-reference capture "
9304 "default", capture_id
);
9307 add_capture (lambda_expr
,
9310 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9314 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9317 /* Parse the (optional) middle of a lambda expression.
9320 < template-parameter-list [opt] >
9321 ( parameter-declaration-clause [opt] )
9322 attribute-specifier [opt]
9324 exception-specification [opt]
9325 lambda-return-type-clause [opt]
9327 LAMBDA_EXPR is the current representation of the lambda expression. */
9330 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9332 /* 5.1.1.4 of the standard says:
9333 If a lambda-expression does not include a lambda-declarator, it is as if
9334 the lambda-declarator were ().
9335 This means an empty parameter list, no attributes, and no exception
9337 tree param_list
= void_list_node
;
9338 tree attributes
= NULL_TREE
;
9339 tree exception_spec
= NULL_TREE
;
9340 tree template_param_list
= NULL_TREE
;
9342 /* The template-parameter-list is optional, but must begin with
9343 an opening angle if present. */
9344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9346 if (cxx_dialect
< cxx14
)
9347 pedwarn (parser
->lexer
->next_token
->location
, 0,
9348 "lambda templates are only available with "
9349 "-std=c++14 or -std=gnu++14");
9351 cp_lexer_consume_token (parser
->lexer
);
9353 template_param_list
= cp_parser_template_parameter_list (parser
);
9355 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9357 /* We just processed one more parameter list. */
9358 ++parser
->num_template_parameter_lists
;
9361 /* The parameter-declaration-clause is optional (unless
9362 template-parameter-list was given), but must begin with an
9363 opening parenthesis if present. */
9364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9366 cp_lexer_consume_token (parser
->lexer
);
9368 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9370 /* Parse parameters. */
9371 param_list
= cp_parser_parameter_declaration_clause (parser
);
9373 /* Default arguments shall not be specified in the
9374 parameter-declaration-clause of a lambda-declarator. */
9375 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9376 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9377 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9378 "default argument specified for lambda parameter");
9380 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9382 attributes
= cp_parser_attributes_opt (parser
);
9384 /* Parse optional `mutable' keyword. */
9385 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9387 cp_lexer_consume_token (parser
->lexer
);
9388 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9391 /* Parse optional exception specification. */
9392 exception_spec
= cp_parser_exception_specification_opt (parser
);
9394 /* Parse optional trailing return type. */
9395 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9397 cp_lexer_consume_token (parser
->lexer
);
9398 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9399 = cp_parser_trailing_type_id (parser
);
9402 /* The function parameters must be in scope all the way until after the
9403 trailing-return-type in case of decltype. */
9404 pop_bindings_and_leave_scope ();
9406 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9407 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9409 /* Create the function call operator.
9411 Messing with declarators like this is no uglier than building up the
9412 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9415 cp_decl_specifier_seq return_type_specs
;
9416 cp_declarator
* declarator
;
9421 clear_decl_specs (&return_type_specs
);
9422 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9423 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9425 /* Maybe we will deduce the return type later. */
9426 return_type_specs
.type
= make_auto ();
9428 p
= obstack_alloc (&declarator_obstack
, 0);
9430 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9433 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9434 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9435 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9436 VIRT_SPEC_UNSPECIFIED
,
9439 /*late_return_type=*/NULL_TREE
);
9440 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9442 fco
= grokmethod (&return_type_specs
,
9445 if (fco
!= error_mark_node
)
9447 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9448 DECL_ARTIFICIAL (fco
) = 1;
9449 /* Give the object parameter a different name. */
9450 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9451 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9452 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9454 if (template_param_list
)
9456 fco
= finish_member_template_decl (fco
);
9457 finish_template_decl (template_param_list
);
9458 --parser
->num_template_parameter_lists
;
9460 else if (parser
->fully_implicit_function_template_p
)
9461 fco
= finish_fully_implicit_template (parser
, fco
);
9463 finish_member_declaration (fco
);
9465 obstack_free (&declarator_obstack
, p
);
9467 return (fco
!= error_mark_node
);
9471 /* Parse the body of a lambda expression, which is simply
9475 but which requires special handling.
9476 LAMBDA_EXPR is the current representation of the lambda expression. */
9479 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9481 bool nested
= (current_function_decl
!= NULL_TREE
);
9482 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9484 push_function_context ();
9486 /* Still increment function_depth so that we don't GC in the
9487 middle of an expression. */
9489 /* Clear this in case we're in the middle of a default argument. */
9490 parser
->local_variables_forbidden_p
= false;
9492 /* Finish the function call operator
9494 + late_parsing_for_member
9495 + function_definition_after_declarator
9496 + ctor_initializer_opt_and_function_body */
9498 tree fco
= lambda_function (lambda_expr
);
9504 /* Let the front end know that we are going to be defining this
9506 start_preparsed_function (fco
,
9508 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9510 start_lambda_scope (fco
);
9511 body
= begin_function_body ();
9513 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9516 /* Push the proxies for any explicit captures. */
9517 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9518 cap
= TREE_CHAIN (cap
))
9519 build_capture_proxy (TREE_PURPOSE (cap
));
9521 compound_stmt
= begin_compound_stmt (0);
9523 /* 5.1.1.4 of the standard says:
9524 If a lambda-expression does not include a trailing-return-type, it
9525 is as if the trailing-return-type denotes the following type:
9526 * if the compound-statement is of the form
9527 { return attribute-specifier [opt] expression ; }
9528 the type of the returned expression after lvalue-to-rvalue
9529 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9530 (_conv.array_ 4.2), and function-to-pointer conversion
9532 * otherwise, void. */
9534 /* In a lambda that has neither a lambda-return-type-clause
9535 nor a deducible form, errors should be reported for return statements
9536 in the body. Since we used void as the placeholder return type, parsing
9537 the body as usual will give such desired behavior. */
9538 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9539 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9540 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9542 tree expr
= NULL_TREE
;
9543 cp_id_kind idk
= CP_ID_KIND_NONE
;
9545 /* Parse tentatively in case there's more after the initial return
9547 cp_parser_parse_tentatively (parser
);
9549 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9551 expr
= cp_parser_expression (parser
, &idk
);
9553 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9554 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9556 if (cp_parser_parse_definitely (parser
))
9558 if (!processing_template_decl
)
9559 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9561 /* Will get error here if type not deduced yet. */
9562 finish_return_stmt (expr
);
9570 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9571 cp_parser_label_declaration (parser
);
9572 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9573 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9576 finish_compound_stmt (compound_stmt
);
9579 finish_function_body (body
);
9580 finish_lambda_scope ();
9582 /* Finish the function and generate code for it if necessary. */
9583 tree fn
= finish_function (/*inline*/2);
9585 /* Only expand if the call op is not a template. */
9586 if (!DECL_TEMPLATE_INFO (fco
))
9587 expand_or_defer_fn (fn
);
9590 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9592 pop_function_context();
9597 /* Statements [gram.stmt.stmt] */
9599 /* Parse a statement.
9603 expression-statement
9608 declaration-statement
9615 attribute-specifier-seq (opt) expression-statement
9616 attribute-specifier-seq (opt) compound-statement
9617 attribute-specifier-seq (opt) selection-statement
9618 attribute-specifier-seq (opt) iteration-statement
9619 attribute-specifier-seq (opt) jump-statement
9620 declaration-statement
9621 attribute-specifier-seq (opt) try-block
9628 IN_COMPOUND is true when the statement is nested inside a
9629 cp_parser_compound_statement; this matters for certain pragmas.
9631 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9632 is a (possibly labeled) if statement which is not enclosed in braces
9633 and has an else clause. This is used to implement -Wparentheses. */
9636 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9637 bool in_compound
, bool *if_p
)
9639 tree statement
, std_attrs
= NULL_TREE
;
9641 location_t statement_location
, attrs_location
;
9646 /* There is no statement yet. */
9647 statement
= NULL_TREE
;
9649 saved_token_sentinel
saved_tokens (parser
->lexer
);
9650 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9651 if (c_dialect_objc ())
9652 /* In obj-c++, seeing '[[' might be the either the beginning of
9653 c++11 attributes, or a nested objc-message-expression. So
9654 let's parse the c++11 attributes tentatively. */
9655 cp_parser_parse_tentatively (parser
);
9656 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9657 if (c_dialect_objc ())
9659 if (!cp_parser_parse_definitely (parser
))
9660 std_attrs
= NULL_TREE
;
9663 /* Peek at the next token. */
9664 token
= cp_lexer_peek_token (parser
->lexer
);
9665 /* Remember the location of the first token in the statement. */
9666 statement_location
= token
->location
;
9667 /* If this is a keyword, then that will often determine what kind of
9668 statement we have. */
9669 if (token
->type
== CPP_KEYWORD
)
9671 enum rid keyword
= token
->keyword
;
9677 /* Looks like a labeled-statement with a case label.
9678 Parse the label, and then use tail recursion to parse
9680 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9685 statement
= cp_parser_selection_statement (parser
, if_p
);
9691 statement
= cp_parser_iteration_statement (parser
, false);
9697 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9698 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9699 cp_lexer_consume_token (parser
->lexer
);
9700 statement
= error_mark_node
;
9703 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9710 statement
= cp_parser_jump_statement (parser
);
9714 cp_lexer_consume_token (parser
->lexer
);
9717 tree sync_expr
= build_cilk_sync ();
9718 SET_EXPR_LOCATION (sync_expr
,
9720 statement
= finish_expr_stmt (sync_expr
);
9724 error_at (token
->location
, "-fcilkplus must be enabled to use"
9726 statement
= error_mark_node
;
9728 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9731 /* Objective-C++ exception-handling constructs. */
9734 case RID_AT_FINALLY
:
9735 case RID_AT_SYNCHRONIZED
:
9737 statement
= cp_parser_objc_statement (parser
);
9741 statement
= cp_parser_try_block (parser
);
9745 /* This must be a namespace alias definition. */
9746 cp_parser_declaration_statement (parser
);
9749 case RID_TRANSACTION_ATOMIC
:
9750 case RID_TRANSACTION_RELAXED
:
9751 statement
= cp_parser_transaction (parser
, keyword
);
9753 case RID_TRANSACTION_CANCEL
:
9754 statement
= cp_parser_transaction_cancel (parser
);
9758 /* It might be a keyword like `int' that can start a
9759 declaration-statement. */
9763 else if (token
->type
== CPP_NAME
)
9765 /* If the next token is a `:', then we are looking at a
9766 labeled-statement. */
9767 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9768 if (token
->type
== CPP_COLON
)
9770 /* Looks like a labeled-statement with an ordinary label.
9771 Parse the label, and then use tail recursion to parse
9774 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9778 /* Anything that starts with a `{' must be a compound-statement. */
9779 else if (token
->type
== CPP_OPEN_BRACE
)
9780 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9781 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9782 a statement all its own. */
9783 else if (token
->type
== CPP_PRAGMA
)
9785 /* Only certain OpenMP pragmas are attached to statements, and thus
9786 are considered statements themselves. All others are not. In
9787 the context of a compound, accept the pragma as a "statement" and
9788 return so that we can check for a close brace. Otherwise we
9789 require a real statement and must go back and read one. */
9791 cp_parser_pragma (parser
, pragma_compound
);
9792 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9796 else if (token
->type
== CPP_EOF
)
9798 cp_parser_error (parser
, "expected statement");
9802 /* Everything else must be a declaration-statement or an
9803 expression-statement. Try for the declaration-statement
9804 first, unless we are looking at a `;', in which case we know that
9805 we have an expression-statement. */
9808 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9810 if (std_attrs
!= NULL_TREE
)
9812 /* Attributes should be parsed as part of the the
9813 declaration, so let's un-parse them. */
9814 saved_tokens
.rollback();
9815 std_attrs
= NULL_TREE
;
9818 cp_parser_parse_tentatively (parser
);
9819 /* Try to parse the declaration-statement. */
9820 cp_parser_declaration_statement (parser
);
9821 /* If that worked, we're done. */
9822 if (cp_parser_parse_definitely (parser
))
9825 /* Look for an expression-statement instead. */
9826 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9829 /* Set the line number for the statement. */
9830 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9831 SET_EXPR_LOCATION (statement
, statement_location
);
9833 /* Note that for now, we don't do anything with c++11 statements
9834 parsed at this level. */
9835 if (std_attrs
!= NULL_TREE
)
9836 warning_at (attrs_location
,
9838 "attributes at the beginning of statement are ignored");
9841 /* Parse the label for a labeled-statement, i.e.
9844 case constant-expression :
9848 case constant-expression ... constant-expression : statement
9850 When a label is parsed without errors, the label is added to the
9851 parse tree by the finish_* functions, so this function doesn't
9852 have to return the label. */
9855 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9858 tree label
= NULL_TREE
;
9859 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9861 /* The next token should be an identifier. */
9862 token
= cp_lexer_peek_token (parser
->lexer
);
9863 if (token
->type
!= CPP_NAME
9864 && token
->type
!= CPP_KEYWORD
)
9866 cp_parser_error (parser
, "expected labeled-statement");
9870 parser
->colon_corrects_to_scope_p
= false;
9871 switch (token
->keyword
)
9878 /* Consume the `case' token. */
9879 cp_lexer_consume_token (parser
->lexer
);
9880 /* Parse the constant-expression. */
9881 expr
= cp_parser_constant_expression (parser
);
9882 if (check_for_bare_parameter_packs (expr
))
9883 expr
= error_mark_node
;
9885 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9886 if (ellipsis
->type
== CPP_ELLIPSIS
)
9888 /* Consume the `...' token. */
9889 cp_lexer_consume_token (parser
->lexer
);
9890 expr_hi
= cp_parser_constant_expression (parser
);
9891 if (check_for_bare_parameter_packs (expr_hi
))
9892 expr_hi
= error_mark_node
;
9894 /* We don't need to emit warnings here, as the common code
9895 will do this for us. */
9898 expr_hi
= NULL_TREE
;
9900 if (parser
->in_switch_statement_p
)
9901 finish_case_label (token
->location
, expr
, expr_hi
);
9903 error_at (token
->location
,
9904 "case label %qE not within a switch statement",
9910 /* Consume the `default' token. */
9911 cp_lexer_consume_token (parser
->lexer
);
9913 if (parser
->in_switch_statement_p
)
9914 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9916 error_at (token
->location
, "case label not within a switch statement");
9920 /* Anything else must be an ordinary label. */
9921 label
= finish_label_stmt (cp_parser_identifier (parser
));
9925 /* Require the `:' token. */
9926 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9928 /* An ordinary label may optionally be followed by attributes.
9929 However, this is only permitted if the attributes are then
9930 followed by a semicolon. This is because, for backward
9931 compatibility, when parsing
9932 lab: __attribute__ ((unused)) int i;
9933 we want the attribute to attach to "i", not "lab". */
9934 if (label
!= NULL_TREE
9935 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9938 cp_parser_parse_tentatively (parser
);
9939 attrs
= cp_parser_gnu_attributes_opt (parser
);
9940 if (attrs
== NULL_TREE
9941 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9942 cp_parser_abort_tentative_parse (parser
);
9943 else if (!cp_parser_parse_definitely (parser
))
9946 attributes
= chainon (attributes
, attrs
);
9949 if (attributes
!= NULL_TREE
)
9950 cplus_decl_attributes (&label
, attributes
, 0);
9952 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9955 /* Parse an expression-statement.
9957 expression-statement:
9960 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9961 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9962 indicates whether this expression-statement is part of an
9963 expression statement. */
9966 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9968 tree statement
= NULL_TREE
;
9969 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9971 /* If the next token is a ';', then there is no expression
9973 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9975 statement
= cp_parser_expression (parser
);
9976 if (statement
== error_mark_node
9977 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9979 cp_parser_skip_to_end_of_block_or_statement (parser
);
9980 return error_mark_node
;
9984 /* Give a helpful message for "A<T>::type t;" and the like. */
9985 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9986 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9988 if (TREE_CODE (statement
) == SCOPE_REF
)
9989 error_at (token
->location
, "need %<typename%> before %qE because "
9990 "%qT is a dependent scope",
9991 statement
, TREE_OPERAND (statement
, 0));
9992 else if (is_overloaded_fn (statement
)
9993 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9996 tree fn
= get_first_fn (statement
);
9997 error_at (token
->location
,
9998 "%<%T::%D%> names the constructor, not the type",
9999 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10003 /* Consume the final `;'. */
10004 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10006 if (in_statement_expr
10007 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10008 /* This is the final expression statement of a statement
10010 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10011 else if (statement
)
10012 statement
= finish_expr_stmt (statement
);
10017 /* Parse a compound-statement.
10019 compound-statement:
10020 { statement-seq [opt] }
10024 compound-statement:
10025 { label-declaration-seq [opt] statement-seq [opt] }
10027 label-declaration-seq:
10029 label-declaration-seq label-declaration
10031 Returns a tree representing the statement. */
10034 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10035 bool in_try
, bool function_body
)
10037 tree compound_stmt
;
10039 /* Consume the `{'. */
10040 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10041 return error_mark_node
;
10042 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10043 && !function_body
&& cxx_dialect
< cxx14
)
10044 pedwarn (input_location
, OPT_Wpedantic
,
10045 "compound-statement in constexpr function");
10046 /* Begin the compound-statement. */
10047 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
10048 /* If the next keyword is `__label__' we have a label declaration. */
10049 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10050 cp_parser_label_declaration (parser
);
10051 /* Parse an (optional) statement-seq. */
10052 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10053 /* Finish the compound-statement. */
10054 finish_compound_stmt (compound_stmt
);
10055 /* Consume the `}'. */
10056 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10058 return compound_stmt
;
10061 /* Parse an (optional) statement-seq.
10065 statement-seq [opt] statement */
10068 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10070 /* Scan statements until there aren't any more. */
10073 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10075 /* If we are looking at a `}', then we have run out of
10076 statements; the same is true if we have reached the end
10077 of file, or have stumbled upon a stray '@end'. */
10078 if (token
->type
== CPP_CLOSE_BRACE
10079 || token
->type
== CPP_EOF
10080 || token
->type
== CPP_PRAGMA_EOL
10081 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10084 /* If we are in a compound statement and find 'else' then
10085 something went wrong. */
10086 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10088 if (parser
->in_statement
& IN_IF_STMT
)
10092 token
= cp_lexer_consume_token (parser
->lexer
);
10093 error_at (token
->location
, "%<else%> without a previous %<if%>");
10097 /* Parse the statement. */
10098 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10102 /* Parse a selection-statement.
10104 selection-statement:
10105 if ( condition ) statement
10106 if ( condition ) statement else statement
10107 switch ( condition ) statement
10109 Returns the new IF_STMT or SWITCH_STMT.
10111 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10112 is a (possibly labeled) if statement which is not enclosed in
10113 braces and has an else clause. This is used to implement
10117 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10125 /* Peek at the next token. */
10126 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10128 /* See what kind of keyword it is. */
10129 keyword
= token
->keyword
;
10138 /* Look for the `('. */
10139 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10141 cp_parser_skip_to_end_of_statement (parser
);
10142 return error_mark_node
;
10145 /* Begin the selection-statement. */
10146 if (keyword
== RID_IF
)
10147 statement
= begin_if_stmt ();
10149 statement
= begin_switch_stmt ();
10151 /* Parse the condition. */
10152 condition
= cp_parser_condition (parser
);
10153 /* Look for the `)'. */
10154 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10155 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10156 /*consume_paren=*/true);
10158 if (keyword
== RID_IF
)
10161 unsigned char in_statement
;
10163 /* Add the condition. */
10164 finish_if_stmt_cond (condition
, statement
);
10166 /* Parse the then-clause. */
10167 in_statement
= parser
->in_statement
;
10168 parser
->in_statement
|= IN_IF_STMT
;
10169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10171 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10172 add_stmt (build_empty_stmt (loc
));
10173 cp_lexer_consume_token (parser
->lexer
);
10174 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10175 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10176 "empty body in an %<if%> statement");
10180 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
10181 token
->location
, "if");
10182 parser
->in_statement
= in_statement
;
10184 finish_then_clause (statement
);
10186 /* If the next token is `else', parse the else-clause. */
10187 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10190 /* Consume the `else' keyword. */
10191 location_t else_tok_loc
10192 = cp_lexer_consume_token (parser
->lexer
)->location
;
10193 begin_else_clause (statement
);
10194 /* Parse the else-clause. */
10195 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10198 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10200 OPT_Wempty_body
, "suggest braces around "
10201 "empty body in an %<else%> statement");
10202 add_stmt (build_empty_stmt (loc
));
10203 cp_lexer_consume_token (parser
->lexer
);
10206 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10207 else_tok_loc
, "else");
10209 finish_else_clause (statement
);
10211 /* If we are currently parsing a then-clause, then
10212 IF_P will not be NULL. We set it to true to
10213 indicate that this if statement has an else clause.
10214 This may trigger the Wparentheses warning below
10215 when we get back up to the parent if statement. */
10221 /* This if statement does not have an else clause. If
10222 NESTED_IF is true, then the then-clause is an if
10223 statement which does have an else clause. We warn
10224 about the potential ambiguity. */
10226 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10227 "suggest explicit braces to avoid ambiguous"
10231 /* Now we're all done with the if-statement. */
10232 finish_if_stmt (statement
);
10236 bool in_switch_statement_p
;
10237 unsigned char in_statement
;
10239 /* Add the condition. */
10240 finish_switch_cond (condition
, statement
);
10242 /* Parse the body of the switch-statement. */
10243 in_switch_statement_p
= parser
->in_switch_statement_p
;
10244 in_statement
= parser
->in_statement
;
10245 parser
->in_switch_statement_p
= true;
10246 parser
->in_statement
|= IN_SWITCH_STMT
;
10247 cp_parser_implicitly_scoped_statement (parser
, NULL
,
10249 parser
->in_switch_statement_p
= in_switch_statement_p
;
10250 parser
->in_statement
= in_statement
;
10252 /* Now we're all done with the switch-statement. */
10253 finish_switch_stmt (statement
);
10261 cp_parser_error (parser
, "expected selection-statement");
10262 return error_mark_node
;
10266 /* Parse a condition.
10270 type-specifier-seq declarator = initializer-clause
10271 type-specifier-seq declarator braced-init-list
10276 type-specifier-seq declarator asm-specification [opt]
10277 attributes [opt] = assignment-expression
10279 Returns the expression that should be tested. */
10282 cp_parser_condition (cp_parser
* parser
)
10284 cp_decl_specifier_seq type_specifiers
;
10285 const char *saved_message
;
10286 int declares_class_or_enum
;
10288 /* Try the declaration first. */
10289 cp_parser_parse_tentatively (parser
);
10290 /* New types are not allowed in the type-specifier-seq for a
10292 saved_message
= parser
->type_definition_forbidden_message
;
10293 parser
->type_definition_forbidden_message
10294 = G_("types may not be defined in conditions");
10295 /* Parse the type-specifier-seq. */
10296 cp_parser_decl_specifier_seq (parser
,
10297 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10299 &declares_class_or_enum
);
10300 /* Restore the saved message. */
10301 parser
->type_definition_forbidden_message
= saved_message
;
10302 /* If all is well, we might be looking at a declaration. */
10303 if (!cp_parser_error_occurred (parser
))
10306 tree asm_specification
;
10308 cp_declarator
*declarator
;
10309 tree initializer
= NULL_TREE
;
10311 /* Parse the declarator. */
10312 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10313 /*ctor_dtor_or_conv_p=*/NULL
,
10314 /*parenthesized_p=*/NULL
,
10315 /*member_p=*/false,
10316 /*friend_p=*/false);
10317 /* Parse the attributes. */
10318 attributes
= cp_parser_attributes_opt (parser
);
10319 /* Parse the asm-specification. */
10320 asm_specification
= cp_parser_asm_specification_opt (parser
);
10321 /* If the next token is not an `=' or '{', then we might still be
10322 looking at an expression. For example:
10326 looks like a decl-specifier-seq and a declarator -- but then
10327 there is no `=', so this is an expression. */
10328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10329 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10330 cp_parser_simulate_error (parser
);
10332 /* If we did see an `=' or '{', then we are looking at a declaration
10334 if (cp_parser_parse_definitely (parser
))
10337 bool non_constant_p
;
10338 bool flags
= LOOKUP_ONLYCONVERTING
;
10340 /* Create the declaration. */
10341 decl
= start_decl (declarator
, &type_specifiers
,
10342 /*initialized_p=*/true,
10343 attributes
, /*prefix_attributes=*/NULL_TREE
,
10346 /* Parse the initializer. */
10347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10349 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10350 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10355 /* Consume the `='. */
10356 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10357 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10359 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10360 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10362 /* Process the initializer. */
10363 cp_finish_decl (decl
,
10364 initializer
, !non_constant_p
,
10369 pop_scope (pushed_scope
);
10371 return convert_from_reference (decl
);
10374 /* If we didn't even get past the declarator successfully, we are
10375 definitely not looking at a declaration. */
10377 cp_parser_abort_tentative_parse (parser
);
10379 /* Otherwise, we are looking at an expression. */
10380 return cp_parser_expression (parser
);
10383 /* Parses a for-statement or range-for-statement until the closing ')',
10387 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10389 tree init
, scope
, decl
;
10392 /* Begin the for-statement. */
10393 scope
= begin_for_scope (&init
);
10395 /* Parse the initialization. */
10396 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10399 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10401 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10405 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10407 /* Normal for loop */
10408 tree condition
= NULL_TREE
;
10409 tree expression
= NULL_TREE
;
10412 stmt
= begin_for_stmt (scope
, init
);
10413 /* The for-init-statement has already been parsed in
10414 cp_parser_for_init_statement, so no work is needed here. */
10415 finish_for_init_stmt (stmt
);
10417 /* If there's a condition, process it. */
10418 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10419 condition
= cp_parser_condition (parser
);
10422 cp_parser_error (parser
, "missing loop condition in loop with "
10423 "%<GCC ivdep%> pragma");
10424 condition
= error_mark_node
;
10426 finish_for_cond (condition
, stmt
, ivdep
);
10427 /* Look for the `;'. */
10428 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10430 /* If there's an expression, process it. */
10431 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10432 expression
= cp_parser_expression (parser
);
10433 finish_for_expr (expression
, stmt
);
10438 /* Tries to parse a range-based for-statement:
10441 decl-specifier-seq declarator : expression
10443 The decl-specifier-seq declarator and the `:' are already parsed by
10444 cp_parser_for_init_statement. If processing_template_decl it returns a
10445 newly created RANGE_FOR_STMT; if not, it is converted to a
10446 regular FOR_STMT. */
10449 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10452 tree stmt
, range_expr
;
10454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10456 bool expr_non_constant_p
;
10457 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10460 range_expr
= cp_parser_expression (parser
);
10462 /* If in template, STMT is converted to a normal for-statement
10463 at instantiation. If not, it is done just ahead. */
10464 if (processing_template_decl
)
10466 if (check_for_bare_parameter_packs (range_expr
))
10467 range_expr
= error_mark_node
;
10468 stmt
= begin_range_for_stmt (scope
, init
);
10470 RANGE_FOR_IVDEP (stmt
) = 1;
10471 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10472 if (!type_dependent_expression_p (range_expr
)
10473 /* do_auto_deduction doesn't mess with template init-lists. */
10474 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10475 do_range_for_auto_deduction (range_decl
, range_expr
);
10479 stmt
= begin_for_stmt (scope
, init
);
10480 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10485 /* Subroutine of cp_convert_range_for: given the initializer expression,
10486 builds up the range temporary. */
10489 build_range_temp (tree range_expr
)
10491 tree range_type
, range_temp
;
10493 /* Find out the type deduced by the declaration
10494 `auto &&__range = range_expr'. */
10495 range_type
= cp_build_reference_type (make_auto (), true);
10496 range_type
= do_auto_deduction (range_type
, range_expr
,
10497 type_uses_auto (range_type
));
10499 /* Create the __range variable. */
10500 range_temp
= build_decl (input_location
, VAR_DECL
,
10501 get_identifier ("__for_range"), range_type
);
10502 TREE_USED (range_temp
) = 1;
10503 DECL_ARTIFICIAL (range_temp
) = 1;
10508 /* Used by cp_parser_range_for in template context: we aren't going to
10509 do a full conversion yet, but we still need to resolve auto in the
10510 type of the for-range-declaration if present. This is basically
10511 a shortcut version of cp_convert_range_for. */
10514 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10516 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10519 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10520 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10521 iter_type
= (cp_parser_perform_range_for_lookup
10522 (range_temp
, &begin_dummy
, &end_dummy
));
10525 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10527 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10528 tf_warning_or_error
);
10529 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10530 iter_decl
, auto_node
);
10535 /* Converts a range-based for-statement into a normal
10536 for-statement, as per the definition.
10538 for (RANGE_DECL : RANGE_EXPR)
10541 should be equivalent to:
10544 auto &&__range = RANGE_EXPR;
10545 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10549 RANGE_DECL = *__begin;
10554 If RANGE_EXPR is an array:
10555 BEGIN_EXPR = __range
10556 END_EXPR = __range + ARRAY_SIZE(__range)
10557 Else if RANGE_EXPR has a member 'begin' or 'end':
10558 BEGIN_EXPR = __range.begin()
10559 END_EXPR = __range.end()
10561 BEGIN_EXPR = begin(__range)
10562 END_EXPR = end(__range);
10564 If __range has a member 'begin' but not 'end', or vice versa, we must
10565 still use the second alternative (it will surely fail, however).
10566 When calling begin()/end() in the third alternative we must use
10567 argument dependent lookup, but always considering 'std' as an associated
10571 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10575 tree iter_type
, begin_expr
, end_expr
;
10576 tree condition
, expression
;
10578 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10579 /* If an error happened previously do nothing or else a lot of
10580 unhelpful errors would be issued. */
10581 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10586 if (VAR_P (range_expr
)
10587 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10588 /* Can't bind a reference to an array of runtime bound. */
10589 range_temp
= range_expr
;
10592 range_temp
= build_range_temp (range_expr
);
10593 pushdecl (range_temp
);
10594 cp_finish_decl (range_temp
, range_expr
,
10595 /*is_constant_init*/false, NULL_TREE
,
10596 LOOKUP_ONLYCONVERTING
);
10597 range_temp
= convert_from_reference (range_temp
);
10599 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10600 &begin_expr
, &end_expr
);
10603 /* The new for initialization statement. */
10604 begin
= build_decl (input_location
, VAR_DECL
,
10605 get_identifier ("__for_begin"), iter_type
);
10606 TREE_USED (begin
) = 1;
10607 DECL_ARTIFICIAL (begin
) = 1;
10609 cp_finish_decl (begin
, begin_expr
,
10610 /*is_constant_init*/false, NULL_TREE
,
10611 LOOKUP_ONLYCONVERTING
);
10613 end
= build_decl (input_location
, VAR_DECL
,
10614 get_identifier ("__for_end"), iter_type
);
10615 TREE_USED (end
) = 1;
10616 DECL_ARTIFICIAL (end
) = 1;
10618 cp_finish_decl (end
, end_expr
,
10619 /*is_constant_init*/false, NULL_TREE
,
10620 LOOKUP_ONLYCONVERTING
);
10622 finish_for_init_stmt (statement
);
10624 /* The new for condition. */
10625 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10628 NULL
, tf_warning_or_error
);
10629 finish_for_cond (condition
, statement
, ivdep
);
10631 /* The new increment expression. */
10632 expression
= finish_unary_op_expr (input_location
,
10633 PREINCREMENT_EXPR
, begin
,
10634 tf_warning_or_error
);
10635 finish_for_expr (expression
, statement
);
10637 /* The declaration is initialized with *__begin inside the loop body. */
10638 cp_finish_decl (range_decl
,
10639 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10640 tf_warning_or_error
),
10641 /*is_constant_init*/false, NULL_TREE
,
10642 LOOKUP_ONLYCONVERTING
);
10647 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10648 We need to solve both at the same time because the method used
10649 depends on the existence of members begin or end.
10650 Returns the type deduced for the iterator expression. */
10653 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10655 if (error_operand_p (range
))
10657 *begin
= *end
= error_mark_node
;
10658 return error_mark_node
;
10661 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10663 error ("range-based %<for%> expression of type %qT "
10664 "has incomplete type", TREE_TYPE (range
));
10665 *begin
= *end
= error_mark_node
;
10666 return error_mark_node
;
10668 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10670 /* If RANGE is an array, we will use pointer arithmetic. */
10672 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10674 array_type_nelts_top (TREE_TYPE (range
)),
10676 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10680 /* If it is not an array, we must do a bit of magic. */
10681 tree id_begin
, id_end
;
10682 tree member_begin
, member_end
;
10684 *begin
= *end
= error_mark_node
;
10686 id_begin
= get_identifier ("begin");
10687 id_end
= get_identifier ("end");
10688 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10689 /*protect=*/2, /*want_type=*/false,
10690 tf_warning_or_error
);
10691 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10692 /*protect=*/2, /*want_type=*/false,
10693 tf_warning_or_error
);
10695 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10697 /* Use the member functions. */
10698 if (member_begin
!= NULL_TREE
)
10699 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10701 error ("range-based %<for%> expression of type %qT has an "
10702 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10704 if (member_end
!= NULL_TREE
)
10705 *end
= cp_parser_range_for_member_function (range
, id_end
);
10707 error ("range-based %<for%> expression of type %qT has a "
10708 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10712 /* Use global functions with ADL. */
10713 vec
<tree
, va_gc
> *vec
;
10714 vec
= make_tree_vector ();
10716 vec_safe_push (vec
, range
);
10718 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10719 tf_warning_or_error
);
10720 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10721 tf_warning_or_error
);
10722 member_end
= perform_koenig_lookup (id_end
, vec
,
10723 tf_warning_or_error
);
10724 *end
= finish_call_expr (member_end
, &vec
, false, true,
10725 tf_warning_or_error
);
10727 release_tree_vector (vec
);
10730 /* Last common checks. */
10731 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10733 /* If one of the expressions is an error do no more checks. */
10734 *begin
= *end
= error_mark_node
;
10735 return error_mark_node
;
10737 else if (type_dependent_expression_p (*begin
)
10738 || type_dependent_expression_p (*end
))
10739 /* Can happen, when, eg, in a template context, Koenig lookup
10740 can't resolve begin/end (c++/58503). */
10744 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10745 /* The unqualified type of the __begin and __end temporaries should
10746 be the same, as required by the multiple auto declaration. */
10747 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10748 error ("inconsistent begin/end types in range-based %<for%> "
10749 "statement: %qT and %qT",
10750 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10756 /* Helper function for cp_parser_perform_range_for_lookup.
10757 Builds a tree for RANGE.IDENTIFIER(). */
10760 cp_parser_range_for_member_function (tree range
, tree identifier
)
10763 vec
<tree
, va_gc
> *vec
;
10765 member
= finish_class_member_access_expr (range
, identifier
,
10766 false, tf_warning_or_error
);
10767 if (member
== error_mark_node
)
10768 return error_mark_node
;
10770 vec
= make_tree_vector ();
10771 res
= finish_call_expr (member
, &vec
,
10772 /*disallow_virtual=*/false,
10773 /*koenig_p=*/false,
10774 tf_warning_or_error
);
10775 release_tree_vector (vec
);
10779 /* Parse an iteration-statement.
10781 iteration-statement:
10782 while ( condition ) statement
10783 do statement while ( expression ) ;
10784 for ( for-init-statement condition [opt] ; expression [opt] )
10787 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10790 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10793 location_t tok_loc
;
10796 unsigned char in_statement
;
10798 /* Peek at the next token. */
10799 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10801 return error_mark_node
;
10803 tok_loc
= token
->location
;
10805 /* Remember whether or not we are already within an iteration
10807 in_statement
= parser
->in_statement
;
10809 /* See what kind of keyword it is. */
10810 keyword
= token
->keyword
;
10817 /* Begin the while-statement. */
10818 statement
= begin_while_stmt ();
10819 /* Look for the `('. */
10820 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10821 /* Parse the condition. */
10822 condition
= cp_parser_condition (parser
);
10823 finish_while_stmt_cond (condition
, statement
, ivdep
);
10824 /* Look for the `)'. */
10825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10826 /* Parse the dependent statement. */
10827 parser
->in_statement
= IN_ITERATION_STMT
;
10828 cp_parser_already_scoped_statement (parser
, tok_loc
, "while");
10829 parser
->in_statement
= in_statement
;
10830 /* We're done with the while-statement. */
10831 finish_while_stmt (statement
);
10839 /* Begin the do-statement. */
10840 statement
= begin_do_stmt ();
10841 /* Parse the body of the do-statement. */
10842 parser
->in_statement
= IN_ITERATION_STMT
;
10843 cp_parser_implicitly_scoped_statement (parser
, NULL
, 0, "do");
10844 parser
->in_statement
= in_statement
;
10845 finish_do_body (statement
);
10846 /* Look for the `while' keyword. */
10847 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10848 /* Look for the `('. */
10849 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10850 /* Parse the expression. */
10851 expression
= cp_parser_expression (parser
);
10852 /* We're done with the do-statement. */
10853 finish_do_stmt (expression
, statement
, ivdep
);
10854 /* Look for the `)'. */
10855 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10856 /* Look for the `;'. */
10857 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10863 /* Look for the `('. */
10864 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10866 statement
= cp_parser_for (parser
, ivdep
);
10868 /* Look for the `)'. */
10869 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10871 /* Parse the body of the for-statement. */
10872 parser
->in_statement
= IN_ITERATION_STMT
;
10873 cp_parser_already_scoped_statement (parser
, tok_loc
, "for");
10874 parser
->in_statement
= in_statement
;
10876 /* We're done with the for-statement. */
10877 finish_for_stmt (statement
);
10882 cp_parser_error (parser
, "expected iteration-statement");
10883 statement
= error_mark_node
;
10890 /* Parse a for-init-statement or the declarator of a range-based-for.
10891 Returns true if a range-based-for declaration is seen.
10893 for-init-statement:
10894 expression-statement
10895 simple-declaration */
10898 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10900 /* If the next token is a `;', then we have an empty
10901 expression-statement. Grammatically, this is also a
10902 simple-declaration, but an invalid one, because it does not
10903 declare anything. Therefore, if we did not handle this case
10904 specially, we would issue an error message about an invalid
10906 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10908 bool is_range_for
= false;
10909 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10912 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10914 /* N3994 -- for (id : init) ... */
10915 if (cxx_dialect
< cxx1z
)
10916 pedwarn (input_location
, 0, "range-based for loop without a "
10917 "type-specifier only available with "
10918 "-std=c++1z or -std=gnu++1z");
10919 tree name
= cp_parser_identifier (parser
);
10920 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10921 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10923 cp_lexer_consume_token (parser
->lexer
);
10927 /* A colon is used in range-based for. */
10928 parser
->colon_corrects_to_scope_p
= false;
10930 /* We're going to speculatively look for a declaration, falling back
10931 to an expression, if necessary. */
10932 cp_parser_parse_tentatively (parser
);
10933 /* Parse the declaration. */
10934 cp_parser_simple_declaration (parser
,
10935 /*function_definition_allowed_p=*/false,
10937 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10940 /* It is a range-for, consume the ':' */
10941 cp_lexer_consume_token (parser
->lexer
);
10942 is_range_for
= true;
10943 if (cxx_dialect
< cxx11
)
10945 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10946 "range-based %<for%> loops only available with "
10947 "-std=c++11 or -std=gnu++11");
10948 *decl
= error_mark_node
;
10952 /* The ';' is not consumed yet because we told
10953 cp_parser_simple_declaration not to. */
10954 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10956 if (cp_parser_parse_definitely (parser
))
10957 return is_range_for
;
10958 /* If the tentative parse failed, then we shall need to look for an
10959 expression-statement. */
10961 /* If we are here, it is an expression-statement. */
10962 cp_parser_expression_statement (parser
, NULL_TREE
);
10966 /* Parse a jump-statement.
10971 return expression [opt] ;
10972 return braced-init-list ;
10978 goto * expression ;
10980 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10983 cp_parser_jump_statement (cp_parser
* parser
)
10985 tree statement
= error_mark_node
;
10988 unsigned char in_statement
;
10990 /* Peek at the next token. */
10991 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10993 return error_mark_node
;
10995 /* See what kind of keyword it is. */
10996 keyword
= token
->keyword
;
11000 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11001 switch (in_statement
)
11004 error_at (token
->location
, "break statement not within loop or switch");
11007 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11008 || in_statement
== IN_ITERATION_STMT
);
11009 statement
= finish_break_stmt ();
11010 if (in_statement
== IN_ITERATION_STMT
)
11011 break_maybe_infinite_loop ();
11014 error_at (token
->location
, "invalid exit from OpenMP structured block");
11017 error_at (token
->location
, "break statement used with OpenMP for loop");
11019 case IN_CILK_SIMD_FOR
:
11020 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11023 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11027 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11030 error_at (token
->location
, "continue statement not within a loop");
11032 case IN_CILK_SIMD_FOR
:
11033 error_at (token
->location
,
11034 "continue statement within %<#pragma simd%> loop body");
11035 /* Fall through. */
11036 case IN_ITERATION_STMT
:
11038 statement
= finish_continue_stmt ();
11041 error_at (token
->location
, "invalid exit from OpenMP structured block");
11044 gcc_unreachable ();
11046 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11052 bool expr_non_constant_p
;
11054 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11056 cp_lexer_set_source_position (parser
->lexer
);
11057 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11058 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11060 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11061 expr
= cp_parser_expression (parser
);
11063 /* If the next token is a `;', then there is no
11066 /* Build the return-statement. */
11067 statement
= finish_return_stmt (expr
);
11068 /* Look for the final `;'. */
11069 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11074 if (parser
->in_function_body
11075 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11077 error ("%<goto%> in %<constexpr%> function");
11078 cp_function_chain
->invalid_constexpr
= true;
11081 /* Create the goto-statement. */
11082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11084 /* Issue a warning about this use of a GNU extension. */
11085 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11086 /* Consume the '*' token. */
11087 cp_lexer_consume_token (parser
->lexer
);
11088 /* Parse the dependent expression. */
11089 finish_goto_stmt (cp_parser_expression (parser
));
11092 finish_goto_stmt (cp_parser_identifier (parser
));
11093 /* Look for the final `;'. */
11094 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11098 cp_parser_error (parser
, "expected jump-statement");
11105 /* Parse a declaration-statement.
11107 declaration-statement:
11108 block-declaration */
11111 cp_parser_declaration_statement (cp_parser
* parser
)
11115 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11116 p
= obstack_alloc (&declarator_obstack
, 0);
11118 /* Parse the block-declaration. */
11119 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11121 /* Free any declarators allocated. */
11122 obstack_free (&declarator_obstack
, p
);
11125 /* Some dependent statements (like `if (cond) statement'), are
11126 implicitly in their own scope. In other words, if the statement is
11127 a single statement (as opposed to a compound-statement), it is
11128 none-the-less treated as if it were enclosed in braces. Any
11129 declarations appearing in the dependent statement are out of scope
11130 after control passes that point. This function parses a statement,
11131 but ensures that is in its own scope, even if it is not a
11132 compound-statement.
11134 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11135 is a (possibly labeled) if statement which is not enclosed in
11136 braces and has an else clause. This is used to implement
11139 Returns the new statement. */
11142 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
11143 location_t guard_loc
,
11144 const char *guard_kind
)
11151 /* Mark if () ; with a special NOP_EXPR. */
11152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11154 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11155 cp_lexer_consume_token (parser
->lexer
);
11156 statement
= add_stmt (build_empty_stmt (loc
));
11158 /* if a compound is opened, we simply parse the statement directly. */
11159 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11160 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11161 /* If the token is not a `{', then we must take special action. */
11164 /* Create a compound-statement. */
11165 statement
= begin_compound_stmt (0);
11166 /* Parse the dependent-statement. */
11167 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11168 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11169 /* Finish the dummy compound-statement. */
11170 finish_compound_stmt (statement
);
11171 cp_token
*next_tok
= cp_lexer_peek_token (parser
->lexer
);
11172 if (next_tok
->keyword
!= RID_ELSE
)
11174 location_t next_stmt_loc
= next_tok
->location
;
11175 warn_for_misleading_indentation (guard_loc
, body_loc
,
11176 next_stmt_loc
, next_tok
->type
,
11181 /* Return the statement. */
11185 /* For some dependent statements (like `while (cond) statement'), we
11186 have already created a scope. Therefore, even if the dependent
11187 statement is a compound-statement, we do not want to create another
11191 cp_parser_already_scoped_statement (cp_parser
* parser
, location_t guard_loc
,
11192 const char *guard_kind
)
11194 /* If the token is a `{', then we must take special action. */
11195 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11197 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11198 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11199 cp_token
*next_tok
= cp_lexer_peek_token (parser
->lexer
);
11200 location_t next_stmt_loc
= next_tok
->location
;
11201 warn_for_misleading_indentation (guard_loc
, body_loc
,
11202 next_stmt_loc
, next_tok
->type
,
11207 /* Avoid calling cp_parser_compound_statement, so that we
11208 don't create a new scope. Do everything else by hand. */
11209 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11210 /* If the next keyword is `__label__' we have a label declaration. */
11211 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11212 cp_parser_label_declaration (parser
);
11213 /* Parse an (optional) statement-seq. */
11214 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11215 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11219 /* Declarations [gram.dcl.dcl] */
11221 /* Parse an optional declaration-sequence.
11225 declaration-seq declaration */
11228 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11234 token
= cp_lexer_peek_token (parser
->lexer
);
11236 if (token
->type
== CPP_CLOSE_BRACE
11237 || token
->type
== CPP_EOF
11238 || token
->type
== CPP_PRAGMA_EOL
)
11241 if (token
->type
== CPP_SEMICOLON
)
11243 /* A declaration consisting of a single semicolon is
11244 invalid. Allow it unless we're being pedantic. */
11245 cp_lexer_consume_token (parser
->lexer
);
11246 if (!in_system_header_at (input_location
))
11247 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11251 /* If we're entering or exiting a region that's implicitly
11252 extern "C", modify the lang context appropriately. */
11253 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11255 push_lang_context (lang_name_c
);
11256 parser
->implicit_extern_c
= true;
11258 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11260 pop_lang_context ();
11261 parser
->implicit_extern_c
= false;
11264 if (token
->type
== CPP_PRAGMA
)
11266 /* A top-level declaration can consist solely of a #pragma.
11267 A nested declaration cannot, so this is done here and not
11268 in cp_parser_declaration. (A #pragma at block scope is
11269 handled in cp_parser_statement.) */
11270 cp_parser_pragma (parser
, pragma_external
);
11274 /* Parse the declaration itself. */
11275 cp_parser_declaration (parser
);
11279 /* Parse a declaration.
11283 function-definition
11284 template-declaration
11285 explicit-instantiation
11286 explicit-specialization
11287 linkage-specification
11288 namespace-definition
11293 __extension__ declaration */
11296 cp_parser_declaration (cp_parser
* parser
)
11300 int saved_pedantic
;
11302 tree attributes
= NULL_TREE
;
11304 /* Check for the `__extension__' keyword. */
11305 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11307 /* Parse the qualified declaration. */
11308 cp_parser_declaration (parser
);
11309 /* Restore the PEDANTIC flag. */
11310 pedantic
= saved_pedantic
;
11315 /* Try to figure out what kind of declaration is present. */
11316 token1
= *cp_lexer_peek_token (parser
->lexer
);
11318 if (token1
.type
!= CPP_EOF
)
11319 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11322 token2
.type
= CPP_EOF
;
11323 token2
.keyword
= RID_MAX
;
11326 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11327 p
= obstack_alloc (&declarator_obstack
, 0);
11329 /* If the next token is `extern' and the following token is a string
11330 literal, then we have a linkage specification. */
11331 if (token1
.keyword
== RID_EXTERN
11332 && cp_parser_is_pure_string_literal (&token2
))
11333 cp_parser_linkage_specification (parser
);
11334 /* If the next token is `template', then we have either a template
11335 declaration, an explicit instantiation, or an explicit
11337 else if (token1
.keyword
== RID_TEMPLATE
)
11339 /* `template <>' indicates a template specialization. */
11340 if (token2
.type
== CPP_LESS
11341 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11342 cp_parser_explicit_specialization (parser
);
11343 /* `template <' indicates a template declaration. */
11344 else if (token2
.type
== CPP_LESS
)
11345 cp_parser_template_declaration (parser
, /*member_p=*/false);
11346 /* Anything else must be an explicit instantiation. */
11348 cp_parser_explicit_instantiation (parser
);
11350 /* If the next token is `export', then we have a template
11352 else if (token1
.keyword
== RID_EXPORT
)
11353 cp_parser_template_declaration (parser
, /*member_p=*/false);
11354 /* If the next token is `extern', 'static' or 'inline' and the one
11355 after that is `template', we have a GNU extended explicit
11356 instantiation directive. */
11357 else if (cp_parser_allow_gnu_extensions_p (parser
)
11358 && (token1
.keyword
== RID_EXTERN
11359 || token1
.keyword
== RID_STATIC
11360 || token1
.keyword
== RID_INLINE
)
11361 && token2
.keyword
== RID_TEMPLATE
)
11362 cp_parser_explicit_instantiation (parser
);
11363 /* If the next token is `namespace', check for a named or unnamed
11364 namespace definition. */
11365 else if (token1
.keyword
== RID_NAMESPACE
11366 && (/* A named namespace definition. */
11367 (token2
.type
== CPP_NAME
11368 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11370 /* An unnamed namespace definition. */
11371 || token2
.type
== CPP_OPEN_BRACE
11372 || token2
.keyword
== RID_ATTRIBUTE
))
11373 cp_parser_namespace_definition (parser
);
11374 /* An inline (associated) namespace definition. */
11375 else if (token1
.keyword
== RID_INLINE
11376 && token2
.keyword
== RID_NAMESPACE
)
11377 cp_parser_namespace_definition (parser
);
11378 /* Objective-C++ declaration/definition. */
11379 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11380 cp_parser_objc_declaration (parser
, NULL_TREE
);
11381 else if (c_dialect_objc ()
11382 && token1
.keyword
== RID_ATTRIBUTE
11383 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11384 cp_parser_objc_declaration (parser
, attributes
);
11385 /* We must have either a block declaration or a function
11388 /* Try to parse a block-declaration, or a function-definition. */
11389 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11391 /* Free any declarators allocated. */
11392 obstack_free (&declarator_obstack
, p
);
11395 /* Parse a block-declaration.
11400 namespace-alias-definition
11407 __extension__ block-declaration
11412 static_assert-declaration
11414 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11415 part of a declaration-statement. */
11418 cp_parser_block_declaration (cp_parser
*parser
,
11422 int saved_pedantic
;
11424 /* Check for the `__extension__' keyword. */
11425 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11427 /* Parse the qualified declaration. */
11428 cp_parser_block_declaration (parser
, statement_p
);
11429 /* Restore the PEDANTIC flag. */
11430 pedantic
= saved_pedantic
;
11435 /* Peek at the next token to figure out which kind of declaration is
11437 token1
= cp_lexer_peek_token (parser
->lexer
);
11439 /* If the next keyword is `asm', we have an asm-definition. */
11440 if (token1
->keyword
== RID_ASM
)
11443 cp_parser_commit_to_tentative_parse (parser
);
11444 cp_parser_asm_definition (parser
);
11446 /* If the next keyword is `namespace', we have a
11447 namespace-alias-definition. */
11448 else if (token1
->keyword
== RID_NAMESPACE
)
11449 cp_parser_namespace_alias_definition (parser
);
11450 /* If the next keyword is `using', we have a
11451 using-declaration, a using-directive, or an alias-declaration. */
11452 else if (token1
->keyword
== RID_USING
)
11457 cp_parser_commit_to_tentative_parse (parser
);
11458 /* If the token after `using' is `namespace', then we have a
11459 using-directive. */
11460 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11461 if (token2
->keyword
== RID_NAMESPACE
)
11462 cp_parser_using_directive (parser
);
11463 /* If the second token after 'using' is '=', then we have an
11464 alias-declaration. */
11465 else if (cxx_dialect
>= cxx11
11466 && token2
->type
== CPP_NAME
11467 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11468 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11469 cp_parser_alias_declaration (parser
);
11470 /* Otherwise, it's a using-declaration. */
11472 cp_parser_using_declaration (parser
,
11473 /*access_declaration_p=*/false);
11475 /* If the next keyword is `__label__' we have a misplaced label
11477 else if (token1
->keyword
== RID_LABEL
)
11479 cp_lexer_consume_token (parser
->lexer
);
11480 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11481 cp_parser_skip_to_end_of_statement (parser
);
11482 /* If the next token is now a `;', consume it. */
11483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11484 cp_lexer_consume_token (parser
->lexer
);
11486 /* If the next token is `static_assert' we have a static assertion. */
11487 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11488 cp_parser_static_assert (parser
, /*member_p=*/false);
11489 /* Anything else must be a simple-declaration. */
11491 cp_parser_simple_declaration (parser
, !statement_p
,
11492 /*maybe_range_for_decl*/NULL
);
11495 /* Parse a simple-declaration.
11497 simple-declaration:
11498 decl-specifier-seq [opt] init-declarator-list [opt] ;
11500 init-declarator-list:
11502 init-declarator-list , init-declarator
11504 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11505 function-definition as a simple-declaration.
11507 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11508 parsed declaration if it is an uninitialized single declarator not followed
11509 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11510 if present, will not be consumed. */
11513 cp_parser_simple_declaration (cp_parser
* parser
,
11514 bool function_definition_allowed_p
,
11515 tree
*maybe_range_for_decl
)
11517 cp_decl_specifier_seq decl_specifiers
;
11518 int declares_class_or_enum
;
11519 bool saw_declarator
;
11520 location_t comma_loc
= UNKNOWN_LOCATION
;
11521 location_t init_loc
= UNKNOWN_LOCATION
;
11523 if (maybe_range_for_decl
)
11524 *maybe_range_for_decl
= NULL_TREE
;
11526 /* Defer access checks until we know what is being declared; the
11527 checks for names appearing in the decl-specifier-seq should be
11528 done as if we were in the scope of the thing being declared. */
11529 push_deferring_access_checks (dk_deferred
);
11531 /* Parse the decl-specifier-seq. We have to keep track of whether
11532 or not the decl-specifier-seq declares a named class or
11533 enumeration type, since that is the only case in which the
11534 init-declarator-list is allowed to be empty.
11538 In a simple-declaration, the optional init-declarator-list can be
11539 omitted only when declaring a class or enumeration, that is when
11540 the decl-specifier-seq contains either a class-specifier, an
11541 elaborated-type-specifier, or an enum-specifier. */
11542 cp_parser_decl_specifier_seq (parser
,
11543 CP_PARSER_FLAGS_OPTIONAL
,
11545 &declares_class_or_enum
);
11546 /* We no longer need to defer access checks. */
11547 stop_deferring_access_checks ();
11549 /* In a block scope, a valid declaration must always have a
11550 decl-specifier-seq. By not trying to parse declarators, we can
11551 resolve the declaration/expression ambiguity more quickly. */
11552 if (!function_definition_allowed_p
11553 && !decl_specifiers
.any_specifiers_p
)
11555 cp_parser_error (parser
, "expected declaration");
11559 /* If the next two tokens are both identifiers, the code is
11560 erroneous. The usual cause of this situation is code like:
11564 where "T" should name a type -- but does not. */
11565 if (!decl_specifiers
.any_type_specifiers_p
11566 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11568 /* If parsing tentatively, we should commit; we really are
11569 looking at a declaration. */
11570 cp_parser_commit_to_tentative_parse (parser
);
11575 /* If we have seen at least one decl-specifier, and the next token
11576 is not a parenthesis, then we must be looking at a declaration.
11577 (After "int (" we might be looking at a functional cast.) */
11578 if (decl_specifiers
.any_specifiers_p
11579 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11580 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11581 && !cp_parser_error_occurred (parser
))
11582 cp_parser_commit_to_tentative_parse (parser
);
11584 /* Keep going until we hit the `;' at the end of the simple
11586 saw_declarator
= false;
11587 while (cp_lexer_next_token_is_not (parser
->lexer
,
11591 bool function_definition_p
;
11594 if (saw_declarator
)
11596 /* If we are processing next declarator, comma is expected */
11597 token
= cp_lexer_peek_token (parser
->lexer
);
11598 gcc_assert (token
->type
== CPP_COMMA
);
11599 cp_lexer_consume_token (parser
->lexer
);
11600 if (maybe_range_for_decl
)
11602 *maybe_range_for_decl
= error_mark_node
;
11603 if (comma_loc
== UNKNOWN_LOCATION
)
11604 comma_loc
= token
->location
;
11608 saw_declarator
= true;
11610 /* Parse the init-declarator. */
11611 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11613 function_definition_allowed_p
,
11614 /*member_p=*/false,
11615 declares_class_or_enum
,
11616 &function_definition_p
,
11617 maybe_range_for_decl
,
11619 /* If an error occurred while parsing tentatively, exit quickly.
11620 (That usually happens when in the body of a function; each
11621 statement is treated as a declaration-statement until proven
11623 if (cp_parser_error_occurred (parser
))
11625 /* Handle function definitions specially. */
11626 if (function_definition_p
)
11628 /* If the next token is a `,', then we are probably
11629 processing something like:
11633 which is erroneous. */
11634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11636 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11637 error_at (token
->location
,
11639 " declarations and function-definitions is forbidden");
11641 /* Otherwise, we're done with the list of declarators. */
11644 pop_deferring_access_checks ();
11648 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11649 *maybe_range_for_decl
= decl
;
11650 /* The next token should be either a `,' or a `;'. */
11651 token
= cp_lexer_peek_token (parser
->lexer
);
11652 /* If it's a `,', there are more declarators to come. */
11653 if (token
->type
== CPP_COMMA
)
11654 /* will be consumed next time around */;
11655 /* If it's a `;', we are done. */
11656 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11658 /* Anything else is an error. */
11661 /* If we have already issued an error message we don't need
11662 to issue another one. */
11663 if ((decl
!= error_mark_node
11664 && DECL_INITIAL (decl
) != error_mark_node
)
11665 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11666 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11667 /* Skip tokens until we reach the end of the statement. */
11668 cp_parser_skip_to_end_of_statement (parser
);
11669 /* If the next token is now a `;', consume it. */
11670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11671 cp_lexer_consume_token (parser
->lexer
);
11674 /* After the first time around, a function-definition is not
11675 allowed -- even if it was OK at first. For example:
11680 function_definition_allowed_p
= false;
11683 /* Issue an error message if no declarators are present, and the
11684 decl-specifier-seq does not itself declare a class or
11685 enumeration: [dcl.dcl]/3. */
11686 if (!saw_declarator
)
11688 if (cp_parser_declares_only_class_p (parser
))
11690 if (!declares_class_or_enum
11691 && decl_specifiers
.type
11692 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11693 /* Ensure an error is issued anyway when finish_decltype_type,
11694 called via cp_parser_decl_specifier_seq, returns a class or
11695 an enumeration (c++/51786). */
11696 decl_specifiers
.type
= NULL_TREE
;
11697 shadow_tag (&decl_specifiers
);
11699 /* Perform any deferred access checks. */
11700 perform_deferred_access_checks (tf_warning_or_error
);
11703 /* Consume the `;'. */
11704 if (!maybe_range_for_decl
)
11705 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11706 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11708 if (init_loc
!= UNKNOWN_LOCATION
)
11709 error_at (init_loc
, "initializer in range-based %<for%> loop");
11710 if (comma_loc
!= UNKNOWN_LOCATION
)
11711 error_at (comma_loc
,
11712 "multiple declarations in range-based %<for%> loop");
11716 pop_deferring_access_checks ();
11719 /* Parse a decl-specifier-seq.
11721 decl-specifier-seq:
11722 decl-specifier-seq [opt] decl-specifier
11723 decl-specifier attribute-specifier-seq [opt] (C++11)
11726 storage-class-specifier
11737 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11739 The parser flags FLAGS is used to control type-specifier parsing.
11741 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11744 1: one of the decl-specifiers is an elaborated-type-specifier
11745 (i.e., a type declaration)
11746 2: one of the decl-specifiers is an enum-specifier or a
11747 class-specifier (i.e., a type definition)
11752 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11753 cp_parser_flags flags
,
11754 cp_decl_specifier_seq
*decl_specs
,
11755 int* declares_class_or_enum
)
11757 bool constructor_possible_p
= !parser
->in_declarator_p
;
11758 bool found_decl_spec
= false;
11759 cp_token
*start_token
= NULL
;
11762 /* Clear DECL_SPECS. */
11763 clear_decl_specs (decl_specs
);
11765 /* Assume no class or enumeration type is declared. */
11766 *declares_class_or_enum
= 0;
11768 /* Keep reading specifiers until there are no more to read. */
11771 bool constructor_p
;
11775 /* Peek at the next token. */
11776 token
= cp_lexer_peek_token (parser
->lexer
);
11778 /* Save the first token of the decl spec list for error
11781 start_token
= token
;
11782 /* Handle attributes. */
11783 if (cp_next_tokens_can_be_attribute_p (parser
))
11785 /* Parse the attributes. */
11786 tree attrs
= cp_parser_attributes_opt (parser
);
11788 /* In a sequence of declaration specifiers, c++11 attributes
11789 appertain to the type that precede them. In that case
11792 The attribute-specifier-seq affects the type only for
11793 the declaration it appears in, not other declarations
11794 involving the same type.
11796 But for now let's force the user to position the
11797 attribute either at the beginning of the declaration or
11798 after the declarator-id, which would clearly mean that it
11799 applies to the declarator. */
11800 if (cxx11_attribute_p (attrs
))
11802 if (!found_decl_spec
)
11803 /* The c++11 attribute is at the beginning of the
11804 declaration. It appertains to the entity being
11808 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11810 /* This is an attribute following a
11811 class-specifier. */
11812 if (decl_specs
->type_definition_p
)
11813 warn_misplaced_attr_for_class_type (token
->location
,
11819 decl_specs
->std_attributes
11820 = chainon (decl_specs
->std_attributes
,
11822 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11823 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11829 decl_specs
->attributes
11830 = chainon (decl_specs
->attributes
,
11832 if (decl_specs
->locations
[ds_attribute
] == 0)
11833 decl_specs
->locations
[ds_attribute
] = token
->location
;
11836 /* Assume we will find a decl-specifier keyword. */
11837 found_decl_spec
= true;
11838 /* If the next token is an appropriate keyword, we can simply
11839 add it to the list. */
11840 switch (token
->keyword
)
11846 if (!at_class_scope_p ())
11848 error_at (token
->location
, "%<friend%> used outside of class");
11849 cp_lexer_purge_token (parser
->lexer
);
11854 /* Consume the token. */
11855 cp_lexer_consume_token (parser
->lexer
);
11859 case RID_CONSTEXPR
:
11861 cp_lexer_consume_token (parser
->lexer
);
11864 /* function-specifier:
11871 cp_parser_function_specifier_opt (parser
, decl_specs
);
11878 /* Consume the token. */
11879 cp_lexer_consume_token (parser
->lexer
);
11880 /* A constructor declarator cannot appear in a typedef. */
11881 constructor_possible_p
= false;
11882 /* The "typedef" keyword can only occur in a declaration; we
11883 may as well commit at this point. */
11884 cp_parser_commit_to_tentative_parse (parser
);
11886 if (decl_specs
->storage_class
!= sc_none
)
11887 decl_specs
->conflicting_specifiers_p
= true;
11890 /* storage-class-specifier:
11900 if (cxx_dialect
== cxx98
)
11902 /* Consume the token. */
11903 cp_lexer_consume_token (parser
->lexer
);
11905 /* Complain about `auto' as a storage specifier, if
11906 we're complaining about C++0x compatibility. */
11907 warning_at (token
->location
, OPT_Wc__11_compat
, "%<auto%>"
11908 " changes meaning in C++11; please remove it");
11910 /* Set the storage class anyway. */
11911 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11915 /* C++0x auto type-specifier. */
11916 found_decl_spec
= false;
11923 /* Consume the token. */
11924 cp_lexer_consume_token (parser
->lexer
);
11925 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11929 /* Consume the token. */
11931 cp_lexer_consume_token (parser
->lexer
);
11935 /* We did not yet find a decl-specifier yet. */
11936 found_decl_spec
= false;
11940 if (found_decl_spec
11941 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11942 && token
->keyword
!= RID_CONSTEXPR
)
11943 error ("decl-specifier invalid in condition");
11946 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11948 /* Constructors are a special case. The `S' in `S()' is not a
11949 decl-specifier; it is the beginning of the declarator. */
11951 = (!found_decl_spec
11952 && constructor_possible_p
11953 && (cp_parser_constructor_declarator_p
11954 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11956 /* If we don't have a DECL_SPEC yet, then we must be looking at
11957 a type-specifier. */
11958 if (!found_decl_spec
&& !constructor_p
)
11960 int decl_spec_declares_class_or_enum
;
11961 bool is_cv_qualifier
;
11965 = cp_parser_type_specifier (parser
, flags
,
11967 /*is_declaration=*/true,
11968 &decl_spec_declares_class_or_enum
,
11970 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11972 /* If this type-specifier referenced a user-defined type
11973 (a typedef, class-name, etc.), then we can't allow any
11974 more such type-specifiers henceforth.
11978 The longest sequence of decl-specifiers that could
11979 possibly be a type name is taken as the
11980 decl-specifier-seq of a declaration. The sequence shall
11981 be self-consistent as described below.
11985 As a general rule, at most one type-specifier is allowed
11986 in the complete decl-specifier-seq of a declaration. The
11987 only exceptions are the following:
11989 -- const or volatile can be combined with any other
11992 -- signed or unsigned can be combined with char, long,
12000 void g (const int Pc);
12002 Here, Pc is *not* part of the decl-specifier seq; it's
12003 the declarator. Therefore, once we see a type-specifier
12004 (other than a cv-qualifier), we forbid any additional
12005 user-defined types. We *do* still allow things like `int
12006 int' to be considered a decl-specifier-seq, and issue the
12007 error message later. */
12008 if (type_spec
&& !is_cv_qualifier
)
12009 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
12010 /* A constructor declarator cannot follow a type-specifier. */
12013 constructor_possible_p
= false;
12014 found_decl_spec
= true;
12015 if (!is_cv_qualifier
)
12016 decl_specs
->any_type_specifiers_p
= true;
12020 /* If we still do not have a DECL_SPEC, then there are no more
12021 decl-specifiers. */
12022 if (!found_decl_spec
)
12025 decl_specs
->any_specifiers_p
= true;
12026 /* After we see one decl-specifier, further decl-specifiers are
12027 always optional. */
12028 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12031 /* Don't allow a friend specifier with a class definition. */
12032 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12033 && (*declares_class_or_enum
& 2))
12034 error_at (decl_specs
->locations
[ds_friend
],
12035 "class definition may not be declared a friend");
12038 /* Parse an (optional) storage-class-specifier.
12040 storage-class-specifier:
12049 storage-class-specifier:
12052 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12055 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12057 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12060 if (cxx_dialect
!= cxx98
)
12062 /* Fall through for C++98. */
12069 /* Consume the token. */
12070 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12077 /* Parse an (optional) function-specifier.
12079 function-specifier:
12084 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12085 Updates DECL_SPECS, if it is non-NULL. */
12088 cp_parser_function_specifier_opt (cp_parser
* parser
,
12089 cp_decl_specifier_seq
*decl_specs
)
12091 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12092 switch (token
->keyword
)
12095 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12099 /* 14.5.2.3 [temp.mem]
12101 A member function template shall not be virtual. */
12102 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12103 error_at (token
->location
, "templates may not be %<virtual%>");
12105 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12109 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12116 /* Consume the token. */
12117 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12120 /* Parse a linkage-specification.
12122 linkage-specification:
12123 extern string-literal { declaration-seq [opt] }
12124 extern string-literal declaration */
12127 cp_parser_linkage_specification (cp_parser
* parser
)
12131 /* Look for the `extern' keyword. */
12132 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12134 /* Look for the string-literal. */
12135 linkage
= cp_parser_string_literal (parser
, false, false);
12137 /* Transform the literal into an identifier. If the literal is a
12138 wide-character string, or contains embedded NULs, then we can't
12139 handle it as the user wants. */
12140 if (strlen (TREE_STRING_POINTER (linkage
))
12141 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12143 cp_parser_error (parser
, "invalid linkage-specification");
12144 /* Assume C++ linkage. */
12145 linkage
= lang_name_cplusplus
;
12148 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12150 /* We're now using the new linkage. */
12151 push_lang_context (linkage
);
12153 /* If the next token is a `{', then we're using the first
12155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12157 cp_ensure_no_omp_declare_simd (parser
);
12159 /* Consume the `{' token. */
12160 cp_lexer_consume_token (parser
->lexer
);
12161 /* Parse the declarations. */
12162 cp_parser_declaration_seq_opt (parser
);
12163 /* Look for the closing `}'. */
12164 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12166 /* Otherwise, there's just one declaration. */
12169 bool saved_in_unbraced_linkage_specification_p
;
12171 saved_in_unbraced_linkage_specification_p
12172 = parser
->in_unbraced_linkage_specification_p
;
12173 parser
->in_unbraced_linkage_specification_p
= true;
12174 cp_parser_declaration (parser
);
12175 parser
->in_unbraced_linkage_specification_p
12176 = saved_in_unbraced_linkage_specification_p
;
12179 /* We're done with the linkage-specification. */
12180 pop_lang_context ();
12183 /* Parse a static_assert-declaration.
12185 static_assert-declaration:
12186 static_assert ( constant-expression , string-literal ) ;
12187 static_assert ( constant-expression ) ; (C++1Z)
12189 If MEMBER_P, this static_assert is a class member. */
12192 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12197 location_t saved_loc
;
12200 /* Peek at the `static_assert' token so we can keep track of exactly
12201 where the static assertion started. */
12202 token
= cp_lexer_peek_token (parser
->lexer
);
12203 saved_loc
= token
->location
;
12205 /* Look for the `static_assert' keyword. */
12206 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12210 /* We know we are in a static assertion; commit to any tentative
12212 if (cp_parser_parsing_tentatively (parser
))
12213 cp_parser_commit_to_tentative_parse (parser
);
12215 /* Parse the `(' starting the static assertion condition. */
12216 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12218 /* Parse the constant-expression. Allow a non-constant expression
12219 here in order to give better diagnostics in finish_static_assert. */
12221 cp_parser_constant_expression (parser
,
12222 /*allow_non_constant_p=*/true,
12223 /*non_constant_p=*/&dummy
);
12225 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12227 if (cxx_dialect
< cxx1z
)
12228 pedwarn (input_location
, OPT_Wpedantic
,
12229 "static_assert without a message "
12230 "only available with -std=c++1z or -std=gnu++1z");
12232 cp_lexer_consume_token (parser
->lexer
);
12233 message
= build_string (1, "");
12234 TREE_TYPE (message
) = char_array_type_node
;
12235 fix_string_type (message
);
12239 /* Parse the separating `,'. */
12240 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12242 /* Parse the string-literal message. */
12243 message
= cp_parser_string_literal (parser
,
12244 /*translate=*/false,
12247 /* A `)' completes the static assertion. */
12248 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12249 cp_parser_skip_to_closing_parenthesis (parser
,
12250 /*recovering=*/true,
12251 /*or_comma=*/false,
12252 /*consume_paren=*/true);
12255 /* A semicolon terminates the declaration. */
12256 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12258 /* Complete the static assertion, which may mean either processing
12259 the static assert now or saving it for template instantiation. */
12260 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12263 /* Parse the expression in decltype ( expression ). */
12266 cp_parser_decltype_expr (cp_parser
*parser
,
12267 bool &id_expression_or_member_access_p
)
12269 cp_token
*id_expr_start_token
;
12272 /* First, try parsing an id-expression. */
12273 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12274 cp_parser_parse_tentatively (parser
);
12275 expr
= cp_parser_id_expression (parser
,
12276 /*template_keyword_p=*/false,
12277 /*check_dependency_p=*/true,
12278 /*template_p=*/NULL
,
12279 /*declarator_p=*/false,
12280 /*optional_p=*/false);
12282 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12284 bool non_integral_constant_expression_p
= false;
12285 tree id_expression
= expr
;
12287 const char *error_msg
;
12289 if (identifier_p (expr
))
12290 /* Lookup the name we got back from the id-expression. */
12291 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12292 id_expr_start_token
->location
);
12295 && expr
!= error_mark_node
12296 && TREE_CODE (expr
) != TYPE_DECL
12297 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12298 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12299 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12301 /* Complete lookup of the id-expression. */
12302 expr
= (finish_id_expression
12303 (id_expression
, expr
, parser
->scope
, &idk
,
12304 /*integral_constant_expression_p=*/false,
12305 /*allow_non_integral_constant_expression_p=*/true,
12306 &non_integral_constant_expression_p
,
12307 /*template_p=*/false,
12309 /*address_p=*/false,
12310 /*template_arg_p=*/false,
12312 id_expr_start_token
->location
));
12314 if (expr
== error_mark_node
)
12315 /* We found an id-expression, but it was something that we
12316 should not have found. This is an error, not something
12317 we can recover from, so note that we found an
12318 id-expression and we'll recover as gracefully as
12320 id_expression_or_member_access_p
= true;
12324 && expr
!= error_mark_node
12325 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12326 /* We have an id-expression. */
12327 id_expression_or_member_access_p
= true;
12330 if (!id_expression_or_member_access_p
)
12332 /* Abort the id-expression parse. */
12333 cp_parser_abort_tentative_parse (parser
);
12335 /* Parsing tentatively, again. */
12336 cp_parser_parse_tentatively (parser
);
12338 /* Parse a class member access. */
12339 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12340 /*cast_p=*/false, /*decltype*/true,
12341 /*member_access_only_p=*/true, NULL
);
12344 && expr
!= error_mark_node
12345 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12346 /* We have an id-expression. */
12347 id_expression_or_member_access_p
= true;
12350 if (id_expression_or_member_access_p
)
12351 /* We have parsed the complete id-expression or member access. */
12352 cp_parser_parse_definitely (parser
);
12355 /* Abort our attempt to parse an id-expression or member access
12357 cp_parser_abort_tentative_parse (parser
);
12359 /* Parse a full expression. */
12360 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12361 /*decltype_p=*/true);
12367 /* Parse a `decltype' type. Returns the type.
12369 simple-type-specifier:
12370 decltype ( expression )
12372 decltype ( auto ) */
12375 cp_parser_decltype (cp_parser
*parser
)
12378 bool id_expression_or_member_access_p
= false;
12379 const char *saved_message
;
12380 bool saved_integral_constant_expression_p
;
12381 bool saved_non_integral_constant_expression_p
;
12382 bool saved_greater_than_is_operator_p
;
12383 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12385 if (start_token
->type
== CPP_DECLTYPE
)
12387 /* Already parsed. */
12388 cp_lexer_consume_token (parser
->lexer
);
12389 return start_token
->u
.value
;
12392 /* Look for the `decltype' token. */
12393 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12394 return error_mark_node
;
12396 /* Parse the opening `('. */
12397 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12398 return error_mark_node
;
12400 /* decltype (auto) */
12401 if (cxx_dialect
>= cxx14
12402 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12404 cp_lexer_consume_token (parser
->lexer
);
12405 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12406 return error_mark_node
;
12407 expr
= make_decltype_auto ();
12408 AUTO_IS_DECLTYPE (expr
) = true;
12412 /* Types cannot be defined in a `decltype' expression. Save away the
12414 saved_message
= parser
->type_definition_forbidden_message
;
12416 /* And create the new one. */
12417 parser
->type_definition_forbidden_message
12418 = G_("types may not be defined in %<decltype%> expressions");
12420 /* The restrictions on constant-expressions do not apply inside
12421 decltype expressions. */
12422 saved_integral_constant_expression_p
12423 = parser
->integral_constant_expression_p
;
12424 saved_non_integral_constant_expression_p
12425 = parser
->non_integral_constant_expression_p
;
12426 parser
->integral_constant_expression_p
= false;
12428 /* Within a parenthesized expression, a `>' token is always
12429 the greater-than operator. */
12430 saved_greater_than_is_operator_p
12431 = parser
->greater_than_is_operator_p
;
12432 parser
->greater_than_is_operator_p
= true;
12434 /* Do not actually evaluate the expression. */
12435 ++cp_unevaluated_operand
;
12437 /* Do not warn about problems with the expression. */
12438 ++c_inhibit_evaluation_warnings
;
12440 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12442 /* Go back to evaluating expressions. */
12443 --cp_unevaluated_operand
;
12444 --c_inhibit_evaluation_warnings
;
12446 /* The `>' token might be the end of a template-id or
12447 template-parameter-list now. */
12448 parser
->greater_than_is_operator_p
12449 = saved_greater_than_is_operator_p
;
12451 /* Restore the old message and the integral constant expression
12453 parser
->type_definition_forbidden_message
= saved_message
;
12454 parser
->integral_constant_expression_p
12455 = saved_integral_constant_expression_p
;
12456 parser
->non_integral_constant_expression_p
12457 = saved_non_integral_constant_expression_p
;
12459 /* Parse to the closing `)'. */
12460 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12462 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12463 /*consume_paren=*/true);
12464 return error_mark_node
;
12467 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12468 tf_warning_or_error
);
12471 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12473 start_token
->type
= CPP_DECLTYPE
;
12474 start_token
->u
.value
= expr
;
12475 start_token
->keyword
= RID_MAX
;
12476 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12481 /* Special member functions [gram.special] */
12483 /* Parse a conversion-function-id.
12485 conversion-function-id:
12486 operator conversion-type-id
12488 Returns an IDENTIFIER_NODE representing the operator. */
12491 cp_parser_conversion_function_id (cp_parser
* parser
)
12495 tree saved_qualifying_scope
;
12496 tree saved_object_scope
;
12497 tree pushed_scope
= NULL_TREE
;
12499 /* Look for the `operator' token. */
12500 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12501 return error_mark_node
;
12502 /* When we parse the conversion-type-id, the current scope will be
12503 reset. However, we need that information in able to look up the
12504 conversion function later, so we save it here. */
12505 saved_scope
= parser
->scope
;
12506 saved_qualifying_scope
= parser
->qualifying_scope
;
12507 saved_object_scope
= parser
->object_scope
;
12508 /* We must enter the scope of the class so that the names of
12509 entities declared within the class are available in the
12510 conversion-type-id. For example, consider:
12517 S::operator I() { ... }
12519 In order to see that `I' is a type-name in the definition, we
12520 must be in the scope of `S'. */
12522 pushed_scope
= push_scope (saved_scope
);
12523 /* Parse the conversion-type-id. */
12524 type
= cp_parser_conversion_type_id (parser
);
12525 /* Leave the scope of the class, if any. */
12527 pop_scope (pushed_scope
);
12528 /* Restore the saved scope. */
12529 parser
->scope
= saved_scope
;
12530 parser
->qualifying_scope
= saved_qualifying_scope
;
12531 parser
->object_scope
= saved_object_scope
;
12532 /* If the TYPE is invalid, indicate failure. */
12533 if (type
== error_mark_node
)
12534 return error_mark_node
;
12535 return mangle_conv_op_name_for_type (type
);
12538 /* Parse a conversion-type-id:
12540 conversion-type-id:
12541 type-specifier-seq conversion-declarator [opt]
12543 Returns the TYPE specified. */
12546 cp_parser_conversion_type_id (cp_parser
* parser
)
12549 cp_decl_specifier_seq type_specifiers
;
12550 cp_declarator
*declarator
;
12551 tree type_specified
;
12552 const char *saved_message
;
12554 /* Parse the attributes. */
12555 attributes
= cp_parser_attributes_opt (parser
);
12557 saved_message
= parser
->type_definition_forbidden_message
;
12558 parser
->type_definition_forbidden_message
12559 = G_("types may not be defined in a conversion-type-id");
12561 /* Parse the type-specifiers. */
12562 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12563 /*is_trailing_return=*/false,
12566 parser
->type_definition_forbidden_message
= saved_message
;
12568 /* If that didn't work, stop. */
12569 if (type_specifiers
.type
== error_mark_node
)
12570 return error_mark_node
;
12571 /* Parse the conversion-declarator. */
12572 declarator
= cp_parser_conversion_declarator_opt (parser
);
12574 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12575 /*initialized=*/0, &attributes
);
12577 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12579 /* Don't give this error when parsing tentatively. This happens to
12580 work because we always parse this definitively once. */
12581 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12582 && type_uses_auto (type_specified
))
12584 if (cxx_dialect
< cxx14
)
12586 error ("invalid use of %<auto%> in conversion operator");
12587 return error_mark_node
;
12589 else if (template_parm_scope_p ())
12590 warning (0, "use of %<auto%> in member template "
12591 "conversion operator can never be deduced");
12594 return type_specified
;
12597 /* Parse an (optional) conversion-declarator.
12599 conversion-declarator:
12600 ptr-operator conversion-declarator [opt]
12604 static cp_declarator
*
12605 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12607 enum tree_code code
;
12608 tree class_type
, std_attributes
= NULL_TREE
;
12609 cp_cv_quals cv_quals
;
12611 /* We don't know if there's a ptr-operator next, or not. */
12612 cp_parser_parse_tentatively (parser
);
12613 /* Try the ptr-operator. */
12614 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12616 /* If it worked, look for more conversion-declarators. */
12617 if (cp_parser_parse_definitely (parser
))
12619 cp_declarator
*declarator
;
12621 /* Parse another optional declarator. */
12622 declarator
= cp_parser_conversion_declarator_opt (parser
);
12624 declarator
= cp_parser_make_indirect_declarator
12625 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12633 /* Parse an (optional) ctor-initializer.
12636 : mem-initializer-list
12638 Returns TRUE iff the ctor-initializer was actually present. */
12641 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12643 /* If the next token is not a `:', then there is no
12644 ctor-initializer. */
12645 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12647 /* Do default initialization of any bases and members. */
12648 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12649 finish_mem_initializers (NULL_TREE
);
12654 /* Consume the `:' token. */
12655 cp_lexer_consume_token (parser
->lexer
);
12656 /* And the mem-initializer-list. */
12657 cp_parser_mem_initializer_list (parser
);
12662 /* Parse a mem-initializer-list.
12664 mem-initializer-list:
12665 mem-initializer ... [opt]
12666 mem-initializer ... [opt] , mem-initializer-list */
12669 cp_parser_mem_initializer_list (cp_parser
* parser
)
12671 tree mem_initializer_list
= NULL_TREE
;
12672 tree target_ctor
= error_mark_node
;
12673 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12675 /* Let the semantic analysis code know that we are starting the
12676 mem-initializer-list. */
12677 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12678 error_at (token
->location
,
12679 "only constructors take member initializers");
12681 /* Loop through the list. */
12684 tree mem_initializer
;
12686 token
= cp_lexer_peek_token (parser
->lexer
);
12687 /* Parse the mem-initializer. */
12688 mem_initializer
= cp_parser_mem_initializer (parser
);
12689 /* If the next token is a `...', we're expanding member initializers. */
12690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12692 /* Consume the `...'. */
12693 cp_lexer_consume_token (parser
->lexer
);
12695 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12696 can be expanded but members cannot. */
12697 if (mem_initializer
!= error_mark_node
12698 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12700 error_at (token
->location
,
12701 "cannot expand initializer for member %<%D%>",
12702 TREE_PURPOSE (mem_initializer
));
12703 mem_initializer
= error_mark_node
;
12706 /* Construct the pack expansion type. */
12707 if (mem_initializer
!= error_mark_node
)
12708 mem_initializer
= make_pack_expansion (mem_initializer
);
12710 if (target_ctor
!= error_mark_node
12711 && mem_initializer
!= error_mark_node
)
12713 error ("mem-initializer for %qD follows constructor delegation",
12714 TREE_PURPOSE (mem_initializer
));
12715 mem_initializer
= error_mark_node
;
12717 /* Look for a target constructor. */
12718 if (mem_initializer
!= error_mark_node
12719 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12720 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12722 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12723 if (mem_initializer_list
)
12725 error ("constructor delegation follows mem-initializer for %qD",
12726 TREE_PURPOSE (mem_initializer_list
));
12727 mem_initializer
= error_mark_node
;
12729 target_ctor
= mem_initializer
;
12731 /* Add it to the list, unless it was erroneous. */
12732 if (mem_initializer
!= error_mark_node
)
12734 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12735 mem_initializer_list
= mem_initializer
;
12737 /* If the next token is not a `,', we're done. */
12738 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12740 /* Consume the `,' token. */
12741 cp_lexer_consume_token (parser
->lexer
);
12744 /* Perform semantic analysis. */
12745 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12746 finish_mem_initializers (mem_initializer_list
);
12749 /* Parse a mem-initializer.
12752 mem-initializer-id ( expression-list [opt] )
12753 mem-initializer-id braced-init-list
12758 ( expression-list [opt] )
12760 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12761 class) or FIELD_DECL (for a non-static data member) to initialize;
12762 the TREE_VALUE is the expression-list. An empty initialization
12763 list is represented by void_list_node. */
12766 cp_parser_mem_initializer (cp_parser
* parser
)
12768 tree mem_initializer_id
;
12769 tree expression_list
;
12771 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12773 /* Find out what is being initialized. */
12774 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12776 permerror (token
->location
,
12777 "anachronistic old-style base class initializer");
12778 mem_initializer_id
= NULL_TREE
;
12782 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12783 if (mem_initializer_id
== error_mark_node
)
12784 return mem_initializer_id
;
12786 member
= expand_member_init (mem_initializer_id
);
12787 if (member
&& !DECL_P (member
))
12788 in_base_initializer
= 1;
12790 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12792 bool expr_non_constant_p
;
12793 cp_lexer_set_source_position (parser
->lexer
);
12794 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12795 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12796 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12797 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12801 vec
<tree
, va_gc
> *vec
;
12802 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12804 /*allow_expansion_p=*/true,
12805 /*non_constant_p=*/NULL
);
12807 return error_mark_node
;
12808 expression_list
= build_tree_list_vec (vec
);
12809 release_tree_vector (vec
);
12812 if (expression_list
== error_mark_node
)
12813 return error_mark_node
;
12814 if (!expression_list
)
12815 expression_list
= void_type_node
;
12817 in_base_initializer
= 0;
12819 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12822 /* Parse a mem-initializer-id.
12824 mem-initializer-id:
12825 :: [opt] nested-name-specifier [opt] class-name
12826 decltype-specifier (C++11)
12829 Returns a TYPE indicating the class to be initialized for the first
12830 production (and the second in C++11). Returns an IDENTIFIER_NODE
12831 indicating the data member to be initialized for the last production. */
12834 cp_parser_mem_initializer_id (cp_parser
* parser
)
12836 bool global_scope_p
;
12837 bool nested_name_specifier_p
;
12838 bool template_p
= false;
12841 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12843 /* `typename' is not allowed in this context ([temp.res]). */
12844 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12846 error_at (token
->location
,
12847 "keyword %<typename%> not allowed in this context (a qualified "
12848 "member initializer is implicitly a type)");
12849 cp_lexer_consume_token (parser
->lexer
);
12851 /* Look for the optional `::' operator. */
12853 = (cp_parser_global_scope_opt (parser
,
12854 /*current_scope_valid_p=*/false)
12856 /* Look for the optional nested-name-specifier. The simplest way to
12861 The keyword `typename' is not permitted in a base-specifier or
12862 mem-initializer; in these contexts a qualified name that
12863 depends on a template-parameter is implicitly assumed to be a
12866 is to assume that we have seen the `typename' keyword at this
12868 nested_name_specifier_p
12869 = (cp_parser_nested_name_specifier_opt (parser
,
12870 /*typename_keyword_p=*/true,
12871 /*check_dependency_p=*/true,
12873 /*is_declaration=*/true)
12875 if (nested_name_specifier_p
)
12876 template_p
= cp_parser_optional_template_keyword (parser
);
12877 /* If there is a `::' operator or a nested-name-specifier, then we
12878 are definitely looking for a class-name. */
12879 if (global_scope_p
|| nested_name_specifier_p
)
12880 return cp_parser_class_name (parser
,
12881 /*typename_keyword_p=*/true,
12882 /*template_keyword_p=*/template_p
,
12884 /*check_dependency_p=*/true,
12885 /*class_head_p=*/false,
12886 /*is_declaration=*/true);
12887 /* Otherwise, we could also be looking for an ordinary identifier. */
12888 cp_parser_parse_tentatively (parser
);
12889 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
12890 /* Try a decltype-specifier. */
12891 id
= cp_parser_decltype (parser
);
12893 /* Otherwise, try a class-name. */
12894 id
= cp_parser_class_name (parser
,
12895 /*typename_keyword_p=*/true,
12896 /*template_keyword_p=*/false,
12898 /*check_dependency_p=*/true,
12899 /*class_head_p=*/false,
12900 /*is_declaration=*/true);
12901 /* If we found one, we're done. */
12902 if (cp_parser_parse_definitely (parser
))
12904 /* Otherwise, look for an ordinary identifier. */
12905 return cp_parser_identifier (parser
);
12908 /* Overloading [gram.over] */
12910 /* Parse an operator-function-id.
12912 operator-function-id:
12915 Returns an IDENTIFIER_NODE for the operator which is a
12916 human-readable spelling of the identifier, e.g., `operator +'. */
12919 cp_parser_operator_function_id (cp_parser
* parser
)
12921 /* Look for the `operator' keyword. */
12922 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12923 return error_mark_node
;
12924 /* And then the name of the operator itself. */
12925 return cp_parser_operator (parser
);
12928 /* Return an identifier node for a user-defined literal operator.
12929 The suffix identifier is chained to the operator name identifier. */
12932 cp_literal_operator_id (const char* name
)
12935 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12936 + strlen (name
) + 10);
12937 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12938 identifier
= get_identifier (buffer
);
12943 /* Parse an operator.
12946 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12947 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12948 || ++ -- , ->* -> () []
12955 Returns an IDENTIFIER_NODE for the operator which is a
12956 human-readable spelling of the identifier, e.g., `operator +'. */
12959 cp_parser_operator (cp_parser
* parser
)
12961 tree id
= NULL_TREE
;
12965 /* Peek at the next token. */
12966 token
= cp_lexer_peek_token (parser
->lexer
);
12967 /* Figure out which operator we have. */
12968 switch (token
->type
)
12974 /* The keyword should be either `new' or `delete'. */
12975 if (token
->keyword
== RID_NEW
)
12977 else if (token
->keyword
== RID_DELETE
)
12982 /* Consume the `new' or `delete' token. */
12983 cp_lexer_consume_token (parser
->lexer
);
12985 /* Peek at the next token. */
12986 token
= cp_lexer_peek_token (parser
->lexer
);
12987 /* If it's a `[' token then this is the array variant of the
12989 if (token
->type
== CPP_OPEN_SQUARE
)
12991 /* Consume the `[' token. */
12992 cp_lexer_consume_token (parser
->lexer
);
12993 /* Look for the `]' token. */
12994 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12995 id
= ansi_opname (op
== NEW_EXPR
12996 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12998 /* Otherwise, we have the non-array variant. */
13000 id
= ansi_opname (op
);
13006 id
= ansi_opname (PLUS_EXPR
);
13010 id
= ansi_opname (MINUS_EXPR
);
13014 id
= ansi_opname (MULT_EXPR
);
13018 id
= ansi_opname (TRUNC_DIV_EXPR
);
13022 id
= ansi_opname (TRUNC_MOD_EXPR
);
13026 id
= ansi_opname (BIT_XOR_EXPR
);
13030 id
= ansi_opname (BIT_AND_EXPR
);
13034 id
= ansi_opname (BIT_IOR_EXPR
);
13038 id
= ansi_opname (BIT_NOT_EXPR
);
13042 id
= ansi_opname (TRUTH_NOT_EXPR
);
13046 id
= ansi_assopname (NOP_EXPR
);
13050 id
= ansi_opname (LT_EXPR
);
13054 id
= ansi_opname (GT_EXPR
);
13058 id
= ansi_assopname (PLUS_EXPR
);
13062 id
= ansi_assopname (MINUS_EXPR
);
13066 id
= ansi_assopname (MULT_EXPR
);
13070 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13074 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13078 id
= ansi_assopname (BIT_XOR_EXPR
);
13082 id
= ansi_assopname (BIT_AND_EXPR
);
13086 id
= ansi_assopname (BIT_IOR_EXPR
);
13090 id
= ansi_opname (LSHIFT_EXPR
);
13094 id
= ansi_opname (RSHIFT_EXPR
);
13097 case CPP_LSHIFT_EQ
:
13098 id
= ansi_assopname (LSHIFT_EXPR
);
13101 case CPP_RSHIFT_EQ
:
13102 id
= ansi_assopname (RSHIFT_EXPR
);
13106 id
= ansi_opname (EQ_EXPR
);
13110 id
= ansi_opname (NE_EXPR
);
13114 id
= ansi_opname (LE_EXPR
);
13117 case CPP_GREATER_EQ
:
13118 id
= ansi_opname (GE_EXPR
);
13122 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13126 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13129 case CPP_PLUS_PLUS
:
13130 id
= ansi_opname (POSTINCREMENT_EXPR
);
13133 case CPP_MINUS_MINUS
:
13134 id
= ansi_opname (PREDECREMENT_EXPR
);
13138 id
= ansi_opname (COMPOUND_EXPR
);
13141 case CPP_DEREF_STAR
:
13142 id
= ansi_opname (MEMBER_REF
);
13146 id
= ansi_opname (COMPONENT_REF
);
13149 case CPP_OPEN_PAREN
:
13150 /* Consume the `('. */
13151 cp_lexer_consume_token (parser
->lexer
);
13152 /* Look for the matching `)'. */
13153 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13154 return ansi_opname (CALL_EXPR
);
13156 case CPP_OPEN_SQUARE
:
13157 /* Consume the `['. */
13158 cp_lexer_consume_token (parser
->lexer
);
13159 /* Look for the matching `]'. */
13160 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13161 return ansi_opname (ARRAY_REF
);
13163 case CPP_UTF8STRING
:
13164 case CPP_UTF8STRING_USERDEF
:
13170 case CPP_STRING_USERDEF
:
13171 case CPP_WSTRING_USERDEF
:
13172 case CPP_STRING16_USERDEF
:
13173 case CPP_STRING32_USERDEF
:
13175 tree str
, string_tree
;
13178 if (cxx_dialect
== cxx98
)
13179 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13181 /* Consume the string. */
13182 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13183 /*wide_ok=*/true, /*lookup_udlit=*/false);
13184 if (str
== error_mark_node
)
13185 return error_mark_node
;
13186 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13188 string_tree
= USERDEF_LITERAL_VALUE (str
);
13189 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13194 /* Look for the suffix identifier. */
13195 token
= cp_lexer_peek_token (parser
->lexer
);
13196 if (token
->type
== CPP_NAME
)
13197 id
= cp_parser_identifier (parser
);
13198 else if (token
->type
== CPP_KEYWORD
)
13200 error ("unexpected keyword;"
13201 " remove space between quotes and suffix identifier");
13202 return error_mark_node
;
13206 error ("expected suffix identifier");
13207 return error_mark_node
;
13210 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13211 (TREE_TYPE (TREE_TYPE (string_tree
))));
13212 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13215 error ("expected empty string after %<operator%> keyword");
13216 return error_mark_node
;
13218 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13221 error ("invalid encoding prefix in literal operator");
13222 return error_mark_node
;
13224 if (id
!= error_mark_node
)
13226 const char *name
= IDENTIFIER_POINTER (id
);
13227 id
= cp_literal_operator_id (name
);
13233 /* Anything else is an error. */
13237 /* If we have selected an identifier, we need to consume the
13240 cp_lexer_consume_token (parser
->lexer
);
13241 /* Otherwise, no valid operator name was present. */
13244 cp_parser_error (parser
, "expected operator");
13245 id
= error_mark_node
;
13251 /* Parse a template-declaration.
13253 template-declaration:
13254 export [opt] template < template-parameter-list > declaration
13256 If MEMBER_P is TRUE, this template-declaration occurs within a
13259 The grammar rule given by the standard isn't correct. What
13260 is really meant is:
13262 template-declaration:
13263 export [opt] template-parameter-list-seq
13264 decl-specifier-seq [opt] init-declarator [opt] ;
13265 export [opt] template-parameter-list-seq
13266 function-definition
13268 template-parameter-list-seq:
13269 template-parameter-list-seq [opt]
13270 template < template-parameter-list > */
13273 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13275 /* Check for `export'. */
13276 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13278 /* Consume the `export' token. */
13279 cp_lexer_consume_token (parser
->lexer
);
13280 /* Warn that we do not support `export'. */
13281 warning (0, "keyword %<export%> not implemented, and will be ignored");
13284 cp_parser_template_declaration_after_export (parser
, member_p
);
13287 /* Parse a template-parameter-list.
13289 template-parameter-list:
13291 template-parameter-list , template-parameter
13293 Returns a TREE_LIST. Each node represents a template parameter.
13294 The nodes are connected via their TREE_CHAINs. */
13297 cp_parser_template_parameter_list (cp_parser
* parser
)
13299 tree parameter_list
= NULL_TREE
;
13301 begin_template_parm_list ();
13303 /* The loop below parses the template parms. We first need to know
13304 the total number of template parms to be able to compute proper
13305 canonical types of each dependent type. So after the loop, when
13306 we know the total number of template parms,
13307 end_template_parm_list computes the proper canonical types and
13308 fixes up the dependent types accordingly. */
13313 bool is_parameter_pack
;
13314 location_t parm_loc
;
13316 /* Parse the template-parameter. */
13317 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13318 parameter
= cp_parser_template_parameter (parser
,
13320 &is_parameter_pack
);
13321 /* Add it to the list. */
13322 if (parameter
!= error_mark_node
)
13323 parameter_list
= process_template_parm (parameter_list
,
13327 is_parameter_pack
);
13330 tree err_parm
= build_tree_list (parameter
, parameter
);
13331 parameter_list
= chainon (parameter_list
, err_parm
);
13334 /* If the next token is not a `,', we're done. */
13335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13337 /* Otherwise, consume the `,' token. */
13338 cp_lexer_consume_token (parser
->lexer
);
13341 return end_template_parm_list (parameter_list
);
13344 /* Parse a default argument for a type template-parameter.
13345 Note that diagnostics are handled in cp_parser_template_parameter. */
13348 cp_parser_default_type_template_argument (cp_parser
*parser
)
13350 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
13352 /* Consume the `=' token. */
13353 cp_lexer_consume_token (parser
->lexer
);
13355 /* Parse the default-argument. */
13356 push_deferring_access_checks (dk_no_deferred
);
13357 tree default_argument
= cp_parser_type_id (parser
);
13358 pop_deferring_access_checks ();
13360 return default_argument
;
13363 /* Parse a default argument for a template template-parameter. */
13366 cp_parser_default_template_template_argument (cp_parser
*parser
)
13368 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
13372 /* Consume the `='. */
13373 cp_lexer_consume_token (parser
->lexer
);
13374 /* Parse the id-expression. */
13375 push_deferring_access_checks (dk_no_deferred
);
13376 /* save token before parsing the id-expression, for error
13378 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
13379 tree default_argument
13380 = cp_parser_id_expression (parser
,
13381 /*template_keyword_p=*/false,
13382 /*check_dependency_p=*/true,
13383 /*template_p=*/&is_template
,
13384 /*declarator_p=*/false,
13385 /*optional_p=*/false);
13386 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13387 /* If the id-expression was a template-id that refers to
13388 a template-class, we already have the declaration here,
13389 so no further lookup is needed. */
13392 /* Look up the name. */
13394 = cp_parser_lookup_name (parser
, default_argument
,
13396 /*is_template=*/is_template
,
13397 /*is_namespace=*/false,
13398 /*check_dependency=*/true,
13399 /*ambiguous_decls=*/NULL
,
13401 /* See if the default argument is valid. */
13402 default_argument
= check_template_template_default_arg (default_argument
);
13403 pop_deferring_access_checks ();
13404 return default_argument
;
13407 /* Parse a template-parameter.
13409 template-parameter:
13411 parameter-declaration
13413 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13414 the parameter. The TREE_PURPOSE is the default value, if any.
13415 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13416 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13417 set to true iff this parameter is a parameter pack. */
13420 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13421 bool *is_parameter_pack
)
13424 cp_parameter_declarator
*parameter_declarator
;
13427 /* Assume it is a type parameter or a template parameter. */
13428 *is_non_type
= false;
13429 /* Assume it not a parameter pack. */
13430 *is_parameter_pack
= false;
13431 /* Peek at the next token. */
13432 token
= cp_lexer_peek_token (parser
->lexer
);
13433 /* If it is `class' or `template', we have a type-parameter. */
13434 if (token
->keyword
== RID_TEMPLATE
)
13435 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13436 /* If it is `class' or `typename' we do not know yet whether it is a
13437 type parameter or a non-type parameter. Consider:
13439 template <typename T, typename T::X X> ...
13443 template <class C, class D*> ...
13445 Here, the first parameter is a type parameter, and the second is
13446 a non-type parameter. We can tell by looking at the token after
13447 the identifier -- if it is a `,', `=', or `>' then we have a type
13449 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13451 /* Peek at the token after `class' or `typename'. */
13452 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13453 /* If it's an ellipsis, we have a template type parameter
13455 if (token
->type
== CPP_ELLIPSIS
)
13456 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13457 /* If it's an identifier, skip it. */
13458 if (token
->type
== CPP_NAME
)
13459 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13460 /* Now, see if the token looks like the end of a template
13462 if (token
->type
== CPP_COMMA
13463 || token
->type
== CPP_EQ
13464 || token
->type
== CPP_GREATER
)
13465 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13468 /* Otherwise, it is a non-type parameter.
13472 When parsing a default template-argument for a non-type
13473 template-parameter, the first non-nested `>' is taken as the end
13474 of the template parameter-list rather than a greater-than
13476 *is_non_type
= true;
13477 parameter_declarator
13478 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13479 /*parenthesized_p=*/NULL
);
13481 if (!parameter_declarator
)
13482 return error_mark_node
;
13484 /* If the parameter declaration is marked as a parameter pack, set
13485 *IS_PARAMETER_PACK to notify the caller. */
13486 if (parameter_declarator
->template_parameter_pack_p
)
13487 *is_parameter_pack
= true;
13489 if (parameter_declarator
->default_argument
)
13491 /* Can happen in some cases of erroneous input (c++/34892). */
13492 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13493 /* Consume the `...' for better error recovery. */
13494 cp_lexer_consume_token (parser
->lexer
);
13497 parm
= grokdeclarator (parameter_declarator
->declarator
,
13498 ¶meter_declarator
->decl_specifiers
,
13499 TPARM
, /*initialized=*/0,
13500 /*attrlist=*/NULL
);
13501 if (parm
== error_mark_node
)
13502 return error_mark_node
;
13504 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13507 /* Parse a type-parameter.
13510 class identifier [opt]
13511 class identifier [opt] = type-id
13512 typename identifier [opt]
13513 typename identifier [opt] = type-id
13514 template < template-parameter-list > class identifier [opt]
13515 template < template-parameter-list > class identifier [opt]
13518 GNU Extension (variadic templates):
13521 class ... identifier [opt]
13522 typename ... identifier [opt]
13524 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13525 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13526 the declaration of the parameter.
13528 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13531 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13536 /* Look for a keyword to tell us what kind of parameter this is. */
13537 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13539 return error_mark_node
;
13541 switch (token
->keyword
)
13547 tree default_argument
;
13549 /* If the next token is an ellipsis, we have a template
13551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13553 /* Consume the `...' token. */
13554 cp_lexer_consume_token (parser
->lexer
);
13555 maybe_warn_variadic_templates ();
13557 *is_parameter_pack
= true;
13560 /* If the next token is an identifier, then it names the
13562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13563 identifier
= cp_parser_identifier (parser
);
13565 identifier
= NULL_TREE
;
13567 /* Create the parameter. */
13568 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13570 /* If the next token is an `=', we have a default argument. */
13571 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13574 = cp_parser_default_type_template_argument (parser
);
13576 /* Template parameter packs cannot have default
13578 if (*is_parameter_pack
)
13581 error_at (token
->location
,
13582 "template parameter pack %qD cannot have a "
13583 "default argument", identifier
);
13585 error_at (token
->location
,
13586 "template parameter packs cannot have "
13587 "default arguments");
13588 default_argument
= NULL_TREE
;
13590 else if (check_for_bare_parameter_packs (default_argument
))
13591 default_argument
= error_mark_node
;
13594 default_argument
= NULL_TREE
;
13596 /* Create the combined representation of the parameter and the
13597 default argument. */
13598 parameter
= build_tree_list (default_argument
, parameter
);
13605 tree default_argument
;
13607 /* Look for the `<'. */
13608 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13609 /* Parse the template-parameter-list. */
13610 cp_parser_template_parameter_list (parser
);
13611 /* Look for the `>'. */
13612 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13613 /* Look for the `class' or 'typename' keywords. */
13614 cp_parser_type_parameter_key (parser
);
13615 /* If the next token is an ellipsis, we have a template
13617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13619 /* Consume the `...' token. */
13620 cp_lexer_consume_token (parser
->lexer
);
13621 maybe_warn_variadic_templates ();
13623 *is_parameter_pack
= true;
13625 /* If the next token is an `=', then there is a
13626 default-argument. If the next token is a `>', we are at
13627 the end of the parameter-list. If the next token is a `,',
13628 then we are at the end of this parameter. */
13629 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13630 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13631 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13633 identifier
= cp_parser_identifier (parser
);
13634 /* Treat invalid names as if the parameter were nameless. */
13635 if (identifier
== error_mark_node
)
13636 identifier
= NULL_TREE
;
13639 identifier
= NULL_TREE
;
13641 /* Create the template parameter. */
13642 parameter
= finish_template_template_parm (class_type_node
,
13645 /* If the next token is an `=', then there is a
13646 default-argument. */
13647 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13650 = cp_parser_default_template_template_argument (parser
);
13652 /* Template parameter packs cannot have default
13654 if (*is_parameter_pack
)
13657 error_at (token
->location
,
13658 "template parameter pack %qD cannot "
13659 "have a default argument",
13662 error_at (token
->location
, "template parameter packs cannot "
13663 "have default arguments");
13664 default_argument
= NULL_TREE
;
13668 default_argument
= NULL_TREE
;
13670 /* Create the combined representation of the parameter and the
13671 default argument. */
13672 parameter
= build_tree_list (default_argument
, parameter
);
13677 gcc_unreachable ();
13684 /* Parse a template-id.
13687 template-name < template-argument-list [opt] >
13689 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13690 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13691 returned. Otherwise, if the template-name names a function, or set
13692 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13693 names a class, returns a TYPE_DECL for the specialization.
13695 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13696 uninstantiated templates. */
13699 cp_parser_template_id (cp_parser
*parser
,
13700 bool template_keyword_p
,
13701 bool check_dependency_p
,
13702 enum tag_types tag_type
,
13703 bool is_declaration
)
13709 cp_token_position start_of_id
= 0;
13710 deferred_access_check
*chk
;
13711 vec
<deferred_access_check
, va_gc
> *access_check
;
13712 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13713 bool is_identifier
;
13715 /* If the next token corresponds to a template-id, there is no need
13717 next_token
= cp_lexer_peek_token (parser
->lexer
);
13718 if (next_token
->type
== CPP_TEMPLATE_ID
)
13720 struct tree_check
*check_value
;
13722 /* Get the stored value. */
13723 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13724 /* Perform any access checks that were deferred. */
13725 access_check
= check_value
->checks
;
13728 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13729 perform_or_defer_access_check (chk
->binfo
,
13732 tf_warning_or_error
);
13734 /* Return the stored value. */
13735 return check_value
->value
;
13738 /* Avoid performing name lookup if there is no possibility of
13739 finding a template-id. */
13740 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13741 || (next_token
->type
== CPP_NAME
13742 && !cp_parser_nth_token_starts_template_argument_list_p
13745 cp_parser_error (parser
, "expected template-id");
13746 return error_mark_node
;
13749 /* Remember where the template-id starts. */
13750 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13751 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13753 push_deferring_access_checks (dk_deferred
);
13755 /* Parse the template-name. */
13756 is_identifier
= false;
13757 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13758 check_dependency_p
,
13762 if (templ
== error_mark_node
|| is_identifier
)
13764 pop_deferring_access_checks ();
13768 /* If we find the sequence `[:' after a template-name, it's probably
13769 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13770 parse correctly the argument list. */
13771 next_token
= cp_lexer_peek_token (parser
->lexer
);
13772 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13773 if (next_token
->type
== CPP_OPEN_SQUARE
13774 && next_token
->flags
& DIGRAPH
13775 && next_token_2
->type
== CPP_COLON
13776 && !(next_token_2
->flags
& PREV_WHITE
))
13778 cp_parser_parse_tentatively (parser
);
13779 /* Change `:' into `::'. */
13780 next_token_2
->type
= CPP_SCOPE
;
13781 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13783 cp_lexer_consume_token (parser
->lexer
);
13785 /* Parse the arguments. */
13786 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13787 if (!cp_parser_parse_definitely (parser
))
13789 /* If we couldn't parse an argument list, then we revert our changes
13790 and return simply an error. Maybe this is not a template-id
13792 next_token_2
->type
= CPP_COLON
;
13793 cp_parser_error (parser
, "expected %<<%>");
13794 pop_deferring_access_checks ();
13795 return error_mark_node
;
13797 /* Otherwise, emit an error about the invalid digraph, but continue
13798 parsing because we got our argument list. */
13799 if (permerror (next_token
->location
,
13800 "%<<::%> cannot begin a template-argument list"))
13802 static bool hint
= false;
13803 inform (next_token
->location
,
13804 "%<<:%> is an alternate spelling for %<[%>."
13805 " Insert whitespace between %<<%> and %<::%>");
13806 if (!hint
&& !flag_permissive
)
13808 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13809 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13810 "accept your code)");
13817 /* Look for the `<' that starts the template-argument-list. */
13818 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13820 pop_deferring_access_checks ();
13821 return error_mark_node
;
13823 /* Parse the arguments. */
13824 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13827 /* Build a representation of the specialization. */
13828 if (identifier_p (templ
))
13829 template_id
= build_min_nt_loc (next_token
->location
,
13832 else if (DECL_TYPE_TEMPLATE_P (templ
)
13833 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13835 bool entering_scope
;
13836 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13837 template (rather than some instantiation thereof) only if
13838 is not nested within some other construct. For example, in
13839 "template <typename T> void f(T) { A<T>::", A<T> is just an
13840 instantiation of A. */
13841 entering_scope
= (template_parm_scope_p ()
13842 && cp_lexer_next_token_is (parser
->lexer
,
13845 = finish_template_type (templ
, arguments
, entering_scope
);
13847 else if (variable_template_p (templ
))
13849 template_id
= lookup_template_variable (templ
, arguments
);
13850 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
13851 SET_EXPR_LOCATION (template_id
, next_token
->location
);
13855 /* If it's not a class-template or a template-template, it should be
13856 a function-template. */
13857 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13858 || TREE_CODE (templ
) == OVERLOAD
13859 || BASELINK_P (templ
)));
13861 template_id
= lookup_template_function (templ
, arguments
);
13862 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
13863 SET_EXPR_LOCATION (template_id
, next_token
->location
);
13866 /* If parsing tentatively, replace the sequence of tokens that makes
13867 up the template-id with a CPP_TEMPLATE_ID token. That way,
13868 should we re-parse the token stream, we will not have to repeat
13869 the effort required to do the parse, nor will we issue duplicate
13870 error messages about problems during instantiation of the
13873 /* Don't do this if we had a parse error in a declarator; re-parsing
13874 might succeed if a name changes meaning (60361). */
13875 && !(cp_parser_error_occurred (parser
)
13876 && cp_parser_parsing_tentatively (parser
)
13877 && parser
->in_declarator_p
))
13879 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13881 /* Reset the contents of the START_OF_ID token. */
13882 token
->type
= CPP_TEMPLATE_ID
;
13883 /* Retrieve any deferred checks. Do not pop this access checks yet
13884 so the memory will not be reclaimed during token replacing below. */
13885 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13886 token
->u
.tree_check_value
->value
= template_id
;
13887 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13888 token
->keyword
= RID_MAX
;
13890 /* Purge all subsequent tokens. */
13891 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13893 /* ??? Can we actually assume that, if template_id ==
13894 error_mark_node, we will have issued a diagnostic to the
13895 user, as opposed to simply marking the tentative parse as
13897 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13898 error_at (token
->location
, "parse error in template argument list");
13901 pop_to_parent_deferring_access_checks ();
13902 return template_id
;
13905 /* Parse a template-name.
13910 The standard should actually say:
13914 operator-function-id
13916 A defect report has been filed about this issue.
13918 A conversion-function-id cannot be a template name because they cannot
13919 be part of a template-id. In fact, looking at this code:
13921 a.operator K<int>()
13923 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13924 It is impossible to call a templated conversion-function-id with an
13925 explicit argument list, since the only allowed template parameter is
13926 the type to which it is converting.
13928 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13929 `template' keyword, in a construction like:
13933 In that case `f' is taken to be a template-name, even though there
13934 is no way of knowing for sure.
13936 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13937 name refers to a set of overloaded functions, at least one of which
13938 is a template, or an IDENTIFIER_NODE with the name of the template,
13939 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13940 names are looked up inside uninstantiated templates. */
13943 cp_parser_template_name (cp_parser
* parser
,
13944 bool template_keyword_p
,
13945 bool check_dependency_p
,
13946 bool is_declaration
,
13947 enum tag_types tag_type
,
13948 bool *is_identifier
)
13953 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13955 /* If the next token is `operator', then we have either an
13956 operator-function-id or a conversion-function-id. */
13957 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13959 /* We don't know whether we're looking at an
13960 operator-function-id or a conversion-function-id. */
13961 cp_parser_parse_tentatively (parser
);
13962 /* Try an operator-function-id. */
13963 identifier
= cp_parser_operator_function_id (parser
);
13964 /* If that didn't work, try a conversion-function-id. */
13965 if (!cp_parser_parse_definitely (parser
))
13967 cp_parser_error (parser
, "expected template-name");
13968 return error_mark_node
;
13971 /* Look for the identifier. */
13973 identifier
= cp_parser_identifier (parser
);
13975 /* If we didn't find an identifier, we don't have a template-id. */
13976 if (identifier
== error_mark_node
)
13977 return error_mark_node
;
13979 /* If the name immediately followed the `template' keyword, then it
13980 is a template-name. However, if the next token is not `<', then
13981 we do not treat it as a template-name, since it is not being used
13982 as part of a template-id. This enables us to handle constructs
13985 template <typename T> struct S { S(); };
13986 template <typename T> S<T>::S();
13988 correctly. We would treat `S' as a template -- if it were `S<T>'
13989 -- but we do not if there is no `<'. */
13991 if (processing_template_decl
13992 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13994 /* In a declaration, in a dependent context, we pretend that the
13995 "template" keyword was present in order to improve error
13996 recovery. For example, given:
13998 template <typename T> void f(T::X<int>);
14000 we want to treat "X<int>" as a template-id. */
14002 && !template_keyword_p
14003 && parser
->scope
&& TYPE_P (parser
->scope
)
14004 && check_dependency_p
14005 && dependent_scope_p (parser
->scope
)
14006 /* Do not do this for dtors (or ctors), since they never
14007 need the template keyword before their name. */
14008 && !constructor_name_p (identifier
, parser
->scope
))
14010 cp_token_position start
= 0;
14012 /* Explain what went wrong. */
14013 error_at (token
->location
, "non-template %qD used as template",
14015 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
14016 parser
->scope
, identifier
);
14017 /* If parsing tentatively, find the location of the "<" token. */
14018 if (cp_parser_simulate_error (parser
))
14019 start
= cp_lexer_token_position (parser
->lexer
, true);
14020 /* Parse the template arguments so that we can issue error
14021 messages about them. */
14022 cp_lexer_consume_token (parser
->lexer
);
14023 cp_parser_enclosed_template_argument_list (parser
);
14024 /* Skip tokens until we find a good place from which to
14025 continue parsing. */
14026 cp_parser_skip_to_closing_parenthesis (parser
,
14027 /*recovering=*/true,
14029 /*consume_paren=*/false);
14030 /* If parsing tentatively, permanently remove the
14031 template argument list. That will prevent duplicate
14032 error messages from being issued about the missing
14033 "template" keyword. */
14035 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
14037 *is_identifier
= true;
14041 /* If the "template" keyword is present, then there is generally
14042 no point in doing name-lookup, so we just return IDENTIFIER.
14043 But, if the qualifying scope is non-dependent then we can
14044 (and must) do name-lookup normally. */
14045 if (template_keyword_p
14047 || (TYPE_P (parser
->scope
)
14048 && dependent_type_p (parser
->scope
))))
14052 /* Look up the name. */
14053 decl
= cp_parser_lookup_name (parser
, identifier
,
14055 /*is_template=*/true,
14056 /*is_namespace=*/false,
14057 check_dependency_p
,
14058 /*ambiguous_decls=*/NULL
,
14061 decl
= strip_using_decl (decl
);
14063 /* If DECL is a template, then the name was a template-name. */
14064 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
14066 if (TREE_DEPRECATED (decl
)
14067 && deprecated_state
!= DEPRECATED_SUPPRESS
)
14068 warn_deprecated_use (decl
, NULL_TREE
);
14072 tree fn
= NULL_TREE
;
14074 /* The standard does not explicitly indicate whether a name that
14075 names a set of overloaded declarations, some of which are
14076 templates, is a template-name. However, such a name should
14077 be a template-name; otherwise, there is no way to form a
14078 template-id for the overloaded templates. */
14079 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14080 if (TREE_CODE (fns
) == OVERLOAD
)
14081 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14082 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14087 /* The name does not name a template. */
14088 cp_parser_error (parser
, "expected template-name");
14089 return error_mark_node
;
14093 /* If DECL is dependent, and refers to a function, then just return
14094 its name; we will look it up again during template instantiation. */
14095 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14097 tree scope
= ovl_scope (decl
);
14098 if (TYPE_P (scope
) && dependent_type_p (scope
))
14105 /* Parse a template-argument-list.
14107 template-argument-list:
14108 template-argument ... [opt]
14109 template-argument-list , template-argument ... [opt]
14111 Returns a TREE_VEC containing the arguments. */
14114 cp_parser_template_argument_list (cp_parser
* parser
)
14116 tree fixed_args
[10];
14117 unsigned n_args
= 0;
14118 unsigned alloced
= 10;
14119 tree
*arg_ary
= fixed_args
;
14121 bool saved_in_template_argument_list_p
;
14123 bool saved_non_ice_p
;
14125 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14126 parser
->in_template_argument_list_p
= true;
14127 /* Even if the template-id appears in an integral
14128 constant-expression, the contents of the argument list do
14130 saved_ice_p
= parser
->integral_constant_expression_p
;
14131 parser
->integral_constant_expression_p
= false;
14132 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14133 parser
->non_integral_constant_expression_p
= false;
14135 /* Parse the arguments. */
14141 /* Consume the comma. */
14142 cp_lexer_consume_token (parser
->lexer
);
14144 /* Parse the template-argument. */
14145 argument
= cp_parser_template_argument (parser
);
14147 /* If the next token is an ellipsis, we're expanding a template
14149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14151 if (argument
== error_mark_node
)
14153 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14154 error_at (token
->location
,
14155 "expected parameter pack before %<...%>");
14157 /* Consume the `...' token. */
14158 cp_lexer_consume_token (parser
->lexer
);
14160 /* Make the argument into a TYPE_PACK_EXPANSION or
14161 EXPR_PACK_EXPANSION. */
14162 argument
= make_pack_expansion (argument
);
14165 if (n_args
== alloced
)
14169 if (arg_ary
== fixed_args
)
14171 arg_ary
= XNEWVEC (tree
, alloced
);
14172 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14175 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14177 arg_ary
[n_args
++] = argument
;
14179 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14181 vec
= make_tree_vec (n_args
);
14184 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14186 if (arg_ary
!= fixed_args
)
14188 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14189 parser
->integral_constant_expression_p
= saved_ice_p
;
14190 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14191 #ifdef ENABLE_CHECKING
14192 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14197 /* Parse a template-argument.
14200 assignment-expression
14204 The representation is that of an assignment-expression, type-id, or
14205 id-expression -- except that the qualified id-expression is
14206 evaluated, so that the value returned is either a DECL or an
14209 Although the standard says "assignment-expression", it forbids
14210 throw-expressions or assignments in the template argument.
14211 Therefore, we use "conditional-expression" instead. */
14214 cp_parser_template_argument (cp_parser
* parser
)
14219 bool maybe_type_id
= false;
14220 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14221 location_t loc
= 0;
14224 /* There's really no way to know what we're looking at, so we just
14225 try each alternative in order.
14229 In a template-argument, an ambiguity between a type-id and an
14230 expression is resolved to a type-id, regardless of the form of
14231 the corresponding template-parameter.
14233 Therefore, we try a type-id first. */
14234 cp_parser_parse_tentatively (parser
);
14235 argument
= cp_parser_template_type_arg (parser
);
14236 /* If there was no error parsing the type-id but the next token is a
14237 '>>', our behavior depends on which dialect of C++ we're
14238 parsing. In C++98, we probably found a typo for '> >'. But there
14239 are type-id which are also valid expressions. For instance:
14241 struct X { int operator >> (int); };
14242 template <int V> struct Foo {};
14245 Here 'X()' is a valid type-id of a function type, but the user just
14246 wanted to write the expression "X() >> 5". Thus, we remember that we
14247 found a valid type-id, but we still try to parse the argument as an
14248 expression to see what happens.
14250 In C++0x, the '>>' will be considered two separate '>'
14252 if (!cp_parser_error_occurred (parser
)
14253 && cxx_dialect
== cxx98
14254 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14256 maybe_type_id
= true;
14257 cp_parser_abort_tentative_parse (parser
);
14261 /* If the next token isn't a `,' or a `>', then this argument wasn't
14262 really finished. This means that the argument is not a valid
14264 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14265 cp_parser_error (parser
, "expected template-argument");
14266 /* If that worked, we're done. */
14267 if (cp_parser_parse_definitely (parser
))
14270 /* We're still not sure what the argument will be. */
14271 cp_parser_parse_tentatively (parser
);
14272 /* Try a template. */
14273 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14274 argument
= cp_parser_id_expression (parser
,
14275 /*template_keyword_p=*/false,
14276 /*check_dependency_p=*/true,
14278 /*declarator_p=*/false,
14279 /*optional_p=*/false);
14280 /* If the next token isn't a `,' or a `>', then this argument wasn't
14281 really finished. */
14282 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14283 cp_parser_error (parser
, "expected template-argument");
14284 if (!cp_parser_error_occurred (parser
))
14286 /* Figure out what is being referred to. If the id-expression
14287 was for a class template specialization, then we will have a
14288 TYPE_DECL at this point. There is no need to do name lookup
14289 at this point in that case. */
14290 if (TREE_CODE (argument
) != TYPE_DECL
)
14291 argument
= cp_parser_lookup_name (parser
, argument
,
14293 /*is_template=*/template_p
,
14294 /*is_namespace=*/false,
14295 /*check_dependency=*/true,
14296 /*ambiguous_decls=*/NULL
,
14297 argument_start_token
->location
);
14298 if (TREE_CODE (argument
) != TEMPLATE_DECL
14299 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14300 cp_parser_error (parser
, "expected template-name");
14302 if (cp_parser_parse_definitely (parser
))
14304 if (TREE_DEPRECATED (argument
))
14305 warn_deprecated_use (argument
, NULL_TREE
);
14308 /* It must be a non-type argument. There permitted cases are given
14309 in [temp.arg.nontype]:
14311 -- an integral constant-expression of integral or enumeration
14314 -- the name of a non-type template-parameter; or
14316 -- the name of an object or function with external linkage...
14318 -- the address of an object or function with external linkage...
14320 -- a pointer to member... */
14321 /* Look for a non-type template parameter. */
14322 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14324 cp_parser_parse_tentatively (parser
);
14325 argument
= cp_parser_primary_expression (parser
,
14326 /*address_p=*/false,
14328 /*template_arg_p=*/true,
14330 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14331 || !cp_parser_next_token_ends_template_argument_p (parser
))
14332 cp_parser_simulate_error (parser
);
14333 if (cp_parser_parse_definitely (parser
))
14337 /* If the next token is "&", the argument must be the address of an
14338 object or function with external linkage. */
14339 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14342 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14343 cp_lexer_consume_token (parser
->lexer
);
14345 /* See if we might have an id-expression. */
14346 token
= cp_lexer_peek_token (parser
->lexer
);
14347 if (token
->type
== CPP_NAME
14348 || token
->keyword
== RID_OPERATOR
14349 || token
->type
== CPP_SCOPE
14350 || token
->type
== CPP_TEMPLATE_ID
14351 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14353 cp_parser_parse_tentatively (parser
);
14354 argument
= cp_parser_primary_expression (parser
,
14357 /*template_arg_p=*/true,
14359 if (cp_parser_error_occurred (parser
)
14360 || !cp_parser_next_token_ends_template_argument_p (parser
))
14361 cp_parser_abort_tentative_parse (parser
);
14366 if (INDIRECT_REF_P (argument
))
14368 /* Strip the dereference temporarily. */
14369 gcc_assert (REFERENCE_REF_P (argument
));
14370 argument
= TREE_OPERAND (argument
, 0);
14373 /* If we're in a template, we represent a qualified-id referring
14374 to a static data member as a SCOPE_REF even if the scope isn't
14375 dependent so that we can check access control later. */
14377 if (TREE_CODE (probe
) == SCOPE_REF
)
14378 probe
= TREE_OPERAND (probe
, 1);
14381 /* A variable without external linkage might still be a
14382 valid constant-expression, so no error is issued here
14383 if the external-linkage check fails. */
14384 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14385 cp_parser_simulate_error (parser
);
14387 else if (is_overloaded_fn (argument
))
14388 /* All overloaded functions are allowed; if the external
14389 linkage test does not pass, an error will be issued
14393 && (TREE_CODE (argument
) == OFFSET_REF
14394 || TREE_CODE (argument
) == SCOPE_REF
))
14395 /* A pointer-to-member. */
14397 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14400 cp_parser_simulate_error (parser
);
14402 if (cp_parser_parse_definitely (parser
))
14405 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14406 tf_warning_or_error
);
14408 argument
= convert_from_reference (argument
);
14413 /* If the argument started with "&", there are no other valid
14414 alternatives at this point. */
14417 cp_parser_error (parser
, "invalid non-type template argument");
14418 return error_mark_node
;
14421 /* If the argument wasn't successfully parsed as a type-id followed
14422 by '>>', the argument can only be a constant expression now.
14423 Otherwise, we try parsing the constant-expression tentatively,
14424 because the argument could really be a type-id. */
14426 cp_parser_parse_tentatively (parser
);
14427 argument
= cp_parser_constant_expression (parser
);
14429 if (!maybe_type_id
)
14431 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14432 cp_parser_error (parser
, "expected template-argument");
14433 if (cp_parser_parse_definitely (parser
))
14435 /* We did our best to parse the argument as a non type-id, but that
14436 was the only alternative that matched (albeit with a '>' after
14437 it). We can assume it's just a typo from the user, and a
14438 diagnostic will then be issued. */
14439 return cp_parser_template_type_arg (parser
);
14442 /* Parse an explicit-instantiation.
14444 explicit-instantiation:
14445 template declaration
14447 Although the standard says `declaration', what it really means is:
14449 explicit-instantiation:
14450 template decl-specifier-seq [opt] declarator [opt] ;
14452 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14453 supposed to be allowed. A defect report has been filed about this
14458 explicit-instantiation:
14459 storage-class-specifier template
14460 decl-specifier-seq [opt] declarator [opt] ;
14461 function-specifier template
14462 decl-specifier-seq [opt] declarator [opt] ; */
14465 cp_parser_explicit_instantiation (cp_parser
* parser
)
14467 int declares_class_or_enum
;
14468 cp_decl_specifier_seq decl_specifiers
;
14469 tree extension_specifier
= NULL_TREE
;
14471 timevar_push (TV_TEMPLATE_INST
);
14473 /* Look for an (optional) storage-class-specifier or
14474 function-specifier. */
14475 if (cp_parser_allow_gnu_extensions_p (parser
))
14477 extension_specifier
14478 = cp_parser_storage_class_specifier_opt (parser
);
14479 if (!extension_specifier
)
14480 extension_specifier
14481 = cp_parser_function_specifier_opt (parser
,
14482 /*decl_specs=*/NULL
);
14485 /* Look for the `template' keyword. */
14486 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14487 /* Let the front end know that we are processing an explicit
14489 begin_explicit_instantiation ();
14490 /* [temp.explicit] says that we are supposed to ignore access
14491 control while processing explicit instantiation directives. */
14492 push_deferring_access_checks (dk_no_check
);
14493 /* Parse a decl-specifier-seq. */
14494 cp_parser_decl_specifier_seq (parser
,
14495 CP_PARSER_FLAGS_OPTIONAL
,
14497 &declares_class_or_enum
);
14498 /* If there was exactly one decl-specifier, and it declared a class,
14499 and there's no declarator, then we have an explicit type
14501 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14505 type
= check_tag_decl (&decl_specifiers
,
14506 /*explicit_type_instantiation_p=*/true);
14507 /* Turn access control back on for names used during
14508 template instantiation. */
14509 pop_deferring_access_checks ();
14511 do_type_instantiation (type
, extension_specifier
,
14512 /*complain=*/tf_error
);
14516 cp_declarator
*declarator
;
14519 /* Parse the declarator. */
14521 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14522 /*ctor_dtor_or_conv_p=*/NULL
,
14523 /*parenthesized_p=*/NULL
,
14524 /*member_p=*/false,
14525 /*friend_p=*/false);
14526 if (declares_class_or_enum
& 2)
14527 cp_parser_check_for_definition_in_return_type (declarator
,
14528 decl_specifiers
.type
,
14529 decl_specifiers
.locations
[ds_type_spec
]);
14530 if (declarator
!= cp_error_declarator
)
14532 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14533 permerror (decl_specifiers
.locations
[ds_inline
],
14534 "explicit instantiation shall not use"
14535 " %<inline%> specifier");
14536 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14537 permerror (decl_specifiers
.locations
[ds_constexpr
],
14538 "explicit instantiation shall not use"
14539 " %<constexpr%> specifier");
14541 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14542 NORMAL
, 0, &decl_specifiers
.attributes
);
14543 /* Turn access control back on for names used during
14544 template instantiation. */
14545 pop_deferring_access_checks ();
14546 /* Do the explicit instantiation. */
14547 do_decl_instantiation (decl
, extension_specifier
);
14551 pop_deferring_access_checks ();
14552 /* Skip the body of the explicit instantiation. */
14553 cp_parser_skip_to_end_of_statement (parser
);
14556 /* We're done with the instantiation. */
14557 end_explicit_instantiation ();
14559 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14561 timevar_pop (TV_TEMPLATE_INST
);
14564 /* Parse an explicit-specialization.
14566 explicit-specialization:
14567 template < > declaration
14569 Although the standard says `declaration', what it really means is:
14571 explicit-specialization:
14572 template <> decl-specifier [opt] init-declarator [opt] ;
14573 template <> function-definition
14574 template <> explicit-specialization
14575 template <> template-declaration */
14578 cp_parser_explicit_specialization (cp_parser
* parser
)
14580 bool need_lang_pop
;
14581 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14583 /* Look for the `template' keyword. */
14584 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14585 /* Look for the `<'. */
14586 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14587 /* Look for the `>'. */
14588 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14589 /* We have processed another parameter list. */
14590 ++parser
->num_template_parameter_lists
;
14593 A template ... explicit specialization ... shall not have C
14595 if (current_lang_name
== lang_name_c
)
14597 error_at (token
->location
, "template specialization with C linkage");
14598 /* Give it C++ linkage to avoid confusing other parts of the
14600 push_lang_context (lang_name_cplusplus
);
14601 need_lang_pop
= true;
14604 need_lang_pop
= false;
14605 /* Let the front end know that we are beginning a specialization. */
14606 if (!begin_specialization ())
14608 end_specialization ();
14612 /* If the next keyword is `template', we need to figure out whether
14613 or not we're looking a template-declaration. */
14614 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14616 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14617 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14618 cp_parser_template_declaration_after_export (parser
,
14619 /*member_p=*/false);
14621 cp_parser_explicit_specialization (parser
);
14624 /* Parse the dependent declaration. */
14625 cp_parser_single_declaration (parser
,
14627 /*member_p=*/false,
14628 /*explicit_specialization_p=*/true,
14629 /*friend_p=*/NULL
);
14630 /* We're done with the specialization. */
14631 end_specialization ();
14632 /* For the erroneous case of a template with C linkage, we pushed an
14633 implicit C++ linkage scope; exit that scope now. */
14635 pop_lang_context ();
14636 /* We're done with this parameter list. */
14637 --parser
->num_template_parameter_lists
;
14640 /* Parse a type-specifier.
14643 simple-type-specifier
14646 elaborated-type-specifier
14654 Returns a representation of the type-specifier. For a
14655 class-specifier, enum-specifier, or elaborated-type-specifier, a
14656 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14658 The parser flags FLAGS is used to control type-specifier parsing.
14660 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14661 in a decl-specifier-seq.
14663 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14664 class-specifier, enum-specifier, or elaborated-type-specifier, then
14665 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14666 if a type is declared; 2 if it is defined. Otherwise, it is set to
14669 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14670 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14671 is set to FALSE. */
14674 cp_parser_type_specifier (cp_parser
* parser
,
14675 cp_parser_flags flags
,
14676 cp_decl_specifier_seq
*decl_specs
,
14677 bool is_declaration
,
14678 int* declares_class_or_enum
,
14679 bool* is_cv_qualifier
)
14681 tree type_spec
= NULL_TREE
;
14684 cp_decl_spec ds
= ds_last
;
14686 /* Assume this type-specifier does not declare a new type. */
14687 if (declares_class_or_enum
)
14688 *declares_class_or_enum
= 0;
14689 /* And that it does not specify a cv-qualifier. */
14690 if (is_cv_qualifier
)
14691 *is_cv_qualifier
= false;
14692 /* Peek at the next token. */
14693 token
= cp_lexer_peek_token (parser
->lexer
);
14695 /* If we're looking at a keyword, we can use that to guide the
14696 production we choose. */
14697 keyword
= token
->keyword
;
14701 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14702 goto elaborated_type_specifier
;
14704 /* Look for the enum-specifier. */
14705 type_spec
= cp_parser_enum_specifier (parser
);
14706 /* If that worked, we're done. */
14709 if (declares_class_or_enum
)
14710 *declares_class_or_enum
= 2;
14712 cp_parser_set_decl_spec_type (decl_specs
,
14715 /*type_definition_p=*/true);
14719 goto elaborated_type_specifier
;
14721 /* Any of these indicate either a class-specifier, or an
14722 elaborated-type-specifier. */
14726 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14727 goto elaborated_type_specifier
;
14729 /* Parse tentatively so that we can back up if we don't find a
14730 class-specifier. */
14731 cp_parser_parse_tentatively (parser
);
14732 /* Look for the class-specifier. */
14733 type_spec
= cp_parser_class_specifier (parser
);
14734 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14735 /* If that worked, we're done. */
14736 if (cp_parser_parse_definitely (parser
))
14738 if (declares_class_or_enum
)
14739 *declares_class_or_enum
= 2;
14741 cp_parser_set_decl_spec_type (decl_specs
,
14744 /*type_definition_p=*/true);
14748 /* Fall through. */
14749 elaborated_type_specifier
:
14750 /* We're declaring (not defining) a class or enum. */
14751 if (declares_class_or_enum
)
14752 *declares_class_or_enum
= 1;
14754 /* Fall through. */
14756 /* Look for an elaborated-type-specifier. */
14758 = (cp_parser_elaborated_type_specifier
14760 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14763 cp_parser_set_decl_spec_type (decl_specs
,
14766 /*type_definition_p=*/false);
14771 if (is_cv_qualifier
)
14772 *is_cv_qualifier
= true;
14777 if (is_cv_qualifier
)
14778 *is_cv_qualifier
= true;
14783 if (is_cv_qualifier
)
14784 *is_cv_qualifier
= true;
14788 /* The `__complex__' keyword is a GNU extension. */
14796 /* Handle simple keywords. */
14801 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14802 decl_specs
->any_specifiers_p
= true;
14804 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14807 /* If we do not already have a type-specifier, assume we are looking
14808 at a simple-type-specifier. */
14809 type_spec
= cp_parser_simple_type_specifier (parser
,
14813 /* If we didn't find a type-specifier, and a type-specifier was not
14814 optional in this context, issue an error message. */
14815 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14817 cp_parser_error (parser
, "expected type specifier");
14818 return error_mark_node
;
14824 /* Parse a simple-type-specifier.
14826 simple-type-specifier:
14827 :: [opt] nested-name-specifier [opt] type-name
14828 :: [opt] nested-name-specifier template template-id
14843 simple-type-specifier:
14845 decltype ( expression )
14848 __underlying_type ( type-id )
14852 simple-type-specifier:
14854 __typeof__ unary-expression
14855 __typeof__ ( type-id )
14856 __typeof__ ( type-id ) { initializer-list , [opt] }
14858 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14859 appropriately updated. */
14862 cp_parser_simple_type_specifier (cp_parser
* parser
,
14863 cp_decl_specifier_seq
*decl_specs
,
14864 cp_parser_flags flags
)
14866 tree type
= NULL_TREE
;
14870 /* Peek at the next token. */
14871 token
= cp_lexer_peek_token (parser
->lexer
);
14873 /* If we're looking at a keyword, things are easy. */
14874 switch (token
->keyword
)
14878 decl_specs
->explicit_char_p
= true;
14879 type
= char_type_node
;
14882 type
= char16_type_node
;
14885 type
= char32_type_node
;
14888 type
= wchar_type_node
;
14891 type
= boolean_type_node
;
14894 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14895 type
= short_integer_type_node
;
14899 decl_specs
->explicit_int_p
= true;
14900 type
= integer_type_node
;
14906 idx
= token
->keyword
- RID_INT_N_0
;
14907 if (! int_n_enabled_p
[idx
])
14911 decl_specs
->explicit_intN_p
= true;
14912 decl_specs
->int_n_idx
= idx
;
14914 type
= int_n_trees
[idx
].signed_type
;
14918 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14919 type
= long_integer_type_node
;
14922 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14923 type
= integer_type_node
;
14926 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14927 type
= unsigned_type_node
;
14930 type
= float_type_node
;
14933 type
= double_type_node
;
14936 type
= void_type_node
;
14940 maybe_warn_cpp0x (CPP0X_AUTO
);
14941 if (parser
->auto_is_implicit_function_template_parm_p
)
14943 /* The 'auto' might be the placeholder return type for a function decl
14944 with trailing return type. */
14945 bool have_trailing_return_fn_decl
= false;
14946 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
14949 cp_parser_parse_tentatively (parser
);
14950 cp_lexer_consume_token (parser
->lexer
);
14951 cp_lexer_consume_token (parser
->lexer
);
14952 if (cp_parser_skip_to_closing_parenthesis (parser
,
14953 /*recovering*/false,
14955 /*consume_paren*/true))
14956 have_trailing_return_fn_decl
14957 = cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
);
14958 cp_parser_abort_tentative_parse (parser
);
14961 if (have_trailing_return_fn_decl
)
14963 type
= make_auto ();
14967 if (cxx_dialect
>= cxx14
)
14968 type
= synthesize_implicit_template_parm (parser
);
14970 type
= error_mark_node
;
14972 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14974 if (cxx_dialect
< cxx14
)
14975 error_at (token
->location
,
14976 "use of %<auto%> in lambda parameter declaration "
14977 "only available with "
14978 "-std=c++14 or -std=gnu++14");
14980 else if (cxx_dialect
< cxx14
)
14981 error_at (token
->location
,
14982 "use of %<auto%> in parameter declaration "
14983 "only available with "
14984 "-std=c++14 or -std=gnu++14");
14986 pedwarn (token
->location
, OPT_Wpedantic
,
14987 "ISO C++ forbids use of %<auto%> in parameter "
14991 type
= make_auto ();
14995 /* Since DR 743, decltype can either be a simple-type-specifier by
14996 itself or begin a nested-name-specifier. Parsing it will replace
14997 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14998 handling below decide what to do. */
14999 cp_parser_decltype (parser
);
15000 cp_lexer_set_token_position (parser
->lexer
, token
);
15004 /* Consume the `typeof' token. */
15005 cp_lexer_consume_token (parser
->lexer
);
15006 /* Parse the operand to `typeof'. */
15007 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
15008 /* If it is not already a TYPE, take its type. */
15009 if (!TYPE_P (type
))
15010 type
= finish_typeof (type
);
15013 cp_parser_set_decl_spec_type (decl_specs
, type
,
15015 /*type_definition_p=*/false);
15019 case RID_UNDERLYING_TYPE
:
15020 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
15022 cp_parser_set_decl_spec_type (decl_specs
, type
,
15024 /*type_definition_p=*/false);
15029 case RID_DIRECT_BASES
:
15030 type
= cp_parser_trait_expr (parser
, token
->keyword
);
15032 cp_parser_set_decl_spec_type (decl_specs
, type
,
15034 /*type_definition_p=*/false);
15040 /* If token is an already-parsed decltype not followed by ::,
15041 it's a simple-type-specifier. */
15042 if (token
->type
== CPP_DECLTYPE
15043 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
15045 type
= token
->u
.value
;
15048 cp_parser_set_decl_spec_type (decl_specs
, type
,
15050 /*type_definition_p=*/false);
15051 /* Remember that we are handling a decltype in order to
15052 implement the resolution of DR 1510 when the argument
15053 isn't instantiation dependent. */
15054 decl_specs
->decltype_p
= true;
15056 cp_lexer_consume_token (parser
->lexer
);
15060 /* If the type-specifier was for a built-in type, we're done. */
15063 /* Record the type. */
15065 && (token
->keyword
!= RID_SIGNED
15066 && token
->keyword
!= RID_UNSIGNED
15067 && token
->keyword
!= RID_SHORT
15068 && token
->keyword
!= RID_LONG
))
15069 cp_parser_set_decl_spec_type (decl_specs
,
15072 /*type_definition_p=*/false);
15074 decl_specs
->any_specifiers_p
= true;
15076 /* Consume the token. */
15077 cp_lexer_consume_token (parser
->lexer
);
15079 if (type
== error_mark_node
)
15080 return error_mark_node
;
15082 /* There is no valid C++ program where a non-template type is
15083 followed by a "<". That usually indicates that the user thought
15084 that the type was a template. */
15085 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
15088 return TYPE_NAME (type
);
15091 /* The type-specifier must be a user-defined type. */
15092 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
15097 /* Don't gobble tokens or issue error messages if this is an
15098 optional type-specifier. */
15099 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
15100 cp_parser_parse_tentatively (parser
);
15102 /* Look for the optional `::' operator. */
15104 = (cp_parser_global_scope_opt (parser
,
15105 /*current_scope_valid_p=*/false)
15107 /* Look for the nested-name specifier. */
15109 = (cp_parser_nested_name_specifier_opt (parser
,
15110 /*typename_keyword_p=*/false,
15111 /*check_dependency_p=*/true,
15113 /*is_declaration=*/false)
15115 token
= cp_lexer_peek_token (parser
->lexer
);
15116 /* If we have seen a nested-name-specifier, and the next token
15117 is `template', then we are using the template-id production. */
15119 && cp_parser_optional_template_keyword (parser
))
15121 /* Look for the template-id. */
15122 type
= cp_parser_template_id (parser
,
15123 /*template_keyword_p=*/true,
15124 /*check_dependency_p=*/true,
15126 /*is_declaration=*/false);
15127 /* If the template-id did not name a type, we are out of
15129 if (TREE_CODE (type
) != TYPE_DECL
)
15131 cp_parser_error (parser
, "expected template-id for type");
15135 /* Otherwise, look for a type-name. */
15137 type
= cp_parser_type_name (parser
);
15138 /* Keep track of all name-lookups performed in class scopes. */
15142 && TREE_CODE (type
) == TYPE_DECL
15143 && identifier_p (DECL_NAME (type
)))
15144 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15145 /* If it didn't work out, we don't have a TYPE. */
15146 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15147 && !cp_parser_parse_definitely (parser
))
15149 if (type
&& decl_specs
)
15150 cp_parser_set_decl_spec_type (decl_specs
, type
,
15152 /*type_definition_p=*/false);
15155 /* If we didn't get a type-name, issue an error message. */
15156 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15158 cp_parser_error (parser
, "expected type-name");
15159 return error_mark_node
;
15162 if (type
&& type
!= error_mark_node
)
15164 /* See if TYPE is an Objective-C type, and if so, parse and
15165 accept any protocol references following it. Do this before
15166 the cp_parser_check_for_invalid_template_id() call, because
15167 Objective-C types can be followed by '<...>' which would
15168 enclose protocol names rather than template arguments, and so
15169 everything is fine. */
15170 if (c_dialect_objc () && !parser
->scope
15171 && (objc_is_id (type
) || objc_is_class_name (type
)))
15173 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15174 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15176 /* Clobber the "unqualified" type previously entered into
15177 DECL_SPECS with the new, improved protocol-qualified version. */
15179 decl_specs
->type
= qual_type
;
15184 /* There is no valid C++ program where a non-template type is
15185 followed by a "<". That usually indicates that the user
15186 thought that the type was a template. */
15187 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15195 /* Parse a type-name.
15201 simple-template-id [in c++0x]
15209 Returns a TYPE_DECL for the type. */
15212 cp_parser_type_name (cp_parser
* parser
)
15216 /* We can't know yet whether it is a class-name or not. */
15217 cp_parser_parse_tentatively (parser
);
15218 /* Try a class-name. */
15219 type_decl
= cp_parser_class_name (parser
,
15220 /*typename_keyword_p=*/false,
15221 /*template_keyword_p=*/false,
15223 /*check_dependency_p=*/true,
15224 /*class_head_p=*/false,
15225 /*is_declaration=*/false);
15226 /* If it's not a class-name, keep looking. */
15227 if (!cp_parser_parse_definitely (parser
))
15229 if (cxx_dialect
< cxx11
)
15230 /* It must be a typedef-name or an enum-name. */
15231 return cp_parser_nonclass_name (parser
);
15233 cp_parser_parse_tentatively (parser
);
15234 /* It is either a simple-template-id representing an
15235 instantiation of an alias template... */
15236 type_decl
= cp_parser_template_id (parser
,
15237 /*template_keyword_p=*/false,
15238 /*check_dependency_p=*/true,
15240 /*is_declaration=*/false);
15241 /* Note that this must be an instantiation of an alias template
15242 because [temp.names]/6 says:
15244 A template-id that names an alias template specialization
15247 Whereas [temp.names]/7 says:
15249 A simple-template-id that names a class template
15250 specialization is a class-name. */
15251 if (type_decl
!= NULL_TREE
15252 && TREE_CODE (type_decl
) == TYPE_DECL
15253 && TYPE_DECL_ALIAS_P (type_decl
))
15254 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15256 cp_parser_simulate_error (parser
);
15258 if (!cp_parser_parse_definitely (parser
))
15259 /* ... Or a typedef-name or an enum-name. */
15260 return cp_parser_nonclass_name (parser
);
15266 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15274 Returns a TYPE_DECL for the type. */
15277 cp_parser_nonclass_name (cp_parser
* parser
)
15282 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15283 identifier
= cp_parser_identifier (parser
);
15284 if (identifier
== error_mark_node
)
15285 return error_mark_node
;
15287 /* Look up the type-name. */
15288 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15290 type_decl
= strip_using_decl (type_decl
);
15292 if (TREE_CODE (type_decl
) != TYPE_DECL
15293 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15295 /* See if this is an Objective-C type. */
15296 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15297 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15299 type_decl
= TYPE_NAME (type
);
15302 /* Issue an error if we did not find a type-name. */
15303 if (TREE_CODE (type_decl
) != TYPE_DECL
15304 /* In Objective-C, we have the complication that class names are
15305 normally type names and start declarations (eg, the
15306 "NSObject" in "NSObject *object;"), but can be used in an
15307 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15308 is an expression. So, a classname followed by a dot is not a
15309 valid type-name. */
15310 || (objc_is_class_name (TREE_TYPE (type_decl
))
15311 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15313 if (!cp_parser_simulate_error (parser
))
15314 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15315 NLE_TYPE
, token
->location
);
15316 return error_mark_node
;
15318 /* Remember that the name was used in the definition of the
15319 current class so that we can check later to see if the
15320 meaning would have been different after the class was
15321 entirely defined. */
15322 else if (type_decl
!= error_mark_node
15324 maybe_note_name_used_in_class (identifier
, type_decl
);
15329 /* Parse an elaborated-type-specifier. Note that the grammar given
15330 here incorporates the resolution to DR68.
15332 elaborated-type-specifier:
15333 class-key :: [opt] nested-name-specifier [opt] identifier
15334 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15335 enum-key :: [opt] nested-name-specifier [opt] identifier
15336 typename :: [opt] nested-name-specifier identifier
15337 typename :: [opt] nested-name-specifier template [opt]
15342 elaborated-type-specifier:
15343 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15344 class-key attributes :: [opt] nested-name-specifier [opt]
15345 template [opt] template-id
15346 enum attributes :: [opt] nested-name-specifier [opt] identifier
15348 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15349 declared `friend'. If IS_DECLARATION is TRUE, then this
15350 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15351 something is being declared.
15353 Returns the TYPE specified. */
15356 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15358 bool is_declaration
)
15360 enum tag_types tag_type
;
15362 tree type
= NULL_TREE
;
15363 tree attributes
= NULL_TREE
;
15365 cp_token
*token
= NULL
;
15367 /* See if we're looking at the `enum' keyword. */
15368 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15370 /* Consume the `enum' token. */
15371 cp_lexer_consume_token (parser
->lexer
);
15372 /* Remember that it's an enumeration type. */
15373 tag_type
= enum_type
;
15374 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15375 enums) is used here. */
15376 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15377 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15379 pedwarn (input_location
, 0, "elaborated-type-specifier "
15380 "for a scoped enum must not use the %<%D%> keyword",
15381 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15382 /* Consume the `struct' or `class' and parse it anyway. */
15383 cp_lexer_consume_token (parser
->lexer
);
15385 /* Parse the attributes. */
15386 attributes
= cp_parser_attributes_opt (parser
);
15388 /* Or, it might be `typename'. */
15389 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15392 /* Consume the `typename' token. */
15393 cp_lexer_consume_token (parser
->lexer
);
15394 /* Remember that it's a `typename' type. */
15395 tag_type
= typename_type
;
15397 /* Otherwise it must be a class-key. */
15400 tag_type
= cp_parser_class_key (parser
);
15401 if (tag_type
== none_type
)
15402 return error_mark_node
;
15403 /* Parse the attributes. */
15404 attributes
= cp_parser_attributes_opt (parser
);
15407 /* Look for the `::' operator. */
15408 globalscope
= cp_parser_global_scope_opt (parser
,
15409 /*current_scope_valid_p=*/false);
15410 /* Look for the nested-name-specifier. */
15411 if (tag_type
== typename_type
&& !globalscope
)
15413 if (!cp_parser_nested_name_specifier (parser
,
15414 /*typename_keyword_p=*/true,
15415 /*check_dependency_p=*/true,
15418 return error_mark_node
;
15421 /* Even though `typename' is not present, the proposed resolution
15422 to Core Issue 180 says that in `class A<T>::B', `B' should be
15423 considered a type-name, even if `A<T>' is dependent. */
15424 cp_parser_nested_name_specifier_opt (parser
,
15425 /*typename_keyword_p=*/true,
15426 /*check_dependency_p=*/true,
15429 /* For everything but enumeration types, consider a template-id.
15430 For an enumeration type, consider only a plain identifier. */
15431 if (tag_type
!= enum_type
)
15433 bool template_p
= false;
15436 /* Allow the `template' keyword. */
15437 template_p
= cp_parser_optional_template_keyword (parser
);
15438 /* If we didn't see `template', we don't know if there's a
15439 template-id or not. */
15441 cp_parser_parse_tentatively (parser
);
15442 /* Parse the template-id. */
15443 token
= cp_lexer_peek_token (parser
->lexer
);
15444 decl
= cp_parser_template_id (parser
, template_p
,
15445 /*check_dependency_p=*/true,
15448 /* If we didn't find a template-id, look for an ordinary
15450 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15452 /* We can get here when cp_parser_template_id, called by
15453 cp_parser_class_name with tag_type == none_type, succeeds
15454 and caches a BASELINK. Then, when called again here,
15455 instead of failing and returning an error_mark_node
15456 returns it (see template/typename17.C in C++11).
15457 ??? Could we diagnose this earlier? */
15458 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15460 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15461 type
= error_mark_node
;
15463 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15464 in effect, then we must assume that, upon instantiation, the
15465 template will correspond to a class. */
15466 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15467 && tag_type
== typename_type
)
15468 type
= make_typename_type (parser
->scope
, decl
,
15470 /*complain=*/tf_error
);
15471 /* If the `typename' keyword is in effect and DECL is not a type
15472 decl, then type is non existent. */
15473 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15475 else if (TREE_CODE (decl
) == TYPE_DECL
)
15476 type
= check_elaborated_type_specifier (tag_type
, decl
,
15477 /*allow_template_p=*/true);
15478 else if (decl
== error_mark_node
)
15479 type
= error_mark_node
;
15484 token
= cp_lexer_peek_token (parser
->lexer
);
15485 identifier
= cp_parser_identifier (parser
);
15487 if (identifier
== error_mark_node
)
15489 parser
->scope
= NULL_TREE
;
15490 return error_mark_node
;
15493 /* For a `typename', we needn't call xref_tag. */
15494 if (tag_type
== typename_type
15495 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15496 return cp_parser_make_typename_type (parser
, identifier
,
15499 /* Template parameter lists apply only if we are not within a
15500 function parameter list. */
15501 bool template_parm_lists_apply
15502 = parser
->num_template_parameter_lists
;
15503 if (template_parm_lists_apply
)
15504 for (cp_binding_level
*s
= current_binding_level
;
15505 s
&& s
->kind
!= sk_template_parms
;
15506 s
= s
->level_chain
)
15507 if (s
->kind
== sk_function_parms
)
15508 template_parm_lists_apply
= false;
15510 /* Look up a qualified name in the usual way. */
15514 tree ambiguous_decls
;
15516 decl
= cp_parser_lookup_name (parser
, identifier
,
15518 /*is_template=*/false,
15519 /*is_namespace=*/false,
15520 /*check_dependency=*/true,
15524 /* If the lookup was ambiguous, an error will already have been
15526 if (ambiguous_decls
)
15527 return error_mark_node
;
15529 /* If we are parsing friend declaration, DECL may be a
15530 TEMPLATE_DECL tree node here. However, we need to check
15531 whether this TEMPLATE_DECL results in valid code. Consider
15532 the following example:
15535 template <class T> class C {};
15538 template <class T> friend class N::C; // #1, valid code
15540 template <class T> class Y {
15541 friend class N::C; // #2, invalid code
15544 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15545 name lookup of `N::C'. We see that friend declaration must
15546 be template for the code to be valid. Note that
15547 processing_template_decl does not work here since it is
15548 always 1 for the above two cases. */
15550 decl
= (cp_parser_maybe_treat_template_as_class
15551 (decl
, /*tag_name_p=*/is_friend
15552 && template_parm_lists_apply
));
15554 if (TREE_CODE (decl
) != TYPE_DECL
)
15556 cp_parser_diagnose_invalid_type_name (parser
,
15559 return error_mark_node
;
15562 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15564 bool allow_template
= (template_parm_lists_apply
15565 || DECL_SELF_REFERENCE_P (decl
));
15566 type
= check_elaborated_type_specifier (tag_type
, decl
,
15569 if (type
== error_mark_node
)
15570 return error_mark_node
;
15573 /* Forward declarations of nested types, such as
15578 are invalid unless all components preceding the final '::'
15579 are complete. If all enclosing types are complete, these
15580 declarations become merely pointless.
15582 Invalid forward declarations of nested types are errors
15583 caught elsewhere in parsing. Those that are pointless arrive
15586 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15587 && !is_friend
&& !processing_explicit_instantiation
)
15588 warning (0, "declaration %qD does not declare anything", decl
);
15590 type
= TREE_TYPE (decl
);
15594 /* An elaborated-type-specifier sometimes introduces a new type and
15595 sometimes names an existing type. Normally, the rule is that it
15596 introduces a new type only if there is not an existing type of
15597 the same name already in scope. For example, given:
15600 void f() { struct S s; }
15602 the `struct S' in the body of `f' is the same `struct S' as in
15603 the global scope; the existing definition is used. However, if
15604 there were no global declaration, this would introduce a new
15605 local class named `S'.
15607 An exception to this rule applies to the following code:
15609 namespace N { struct S; }
15611 Here, the elaborated-type-specifier names a new type
15612 unconditionally; even if there is already an `S' in the
15613 containing scope this declaration names a new type.
15614 This exception only applies if the elaborated-type-specifier
15615 forms the complete declaration:
15619 A declaration consisting solely of `class-key identifier ;' is
15620 either a redeclaration of the name in the current scope or a
15621 forward declaration of the identifier as a class name. It
15622 introduces the name into the current scope.
15624 We are in this situation precisely when the next token is a `;'.
15626 An exception to the exception is that a `friend' declaration does
15627 *not* name a new type; i.e., given:
15629 struct S { friend struct T; };
15631 `T' is not a new type in the scope of `S'.
15633 Also, `new struct S' or `sizeof (struct S)' never results in the
15634 definition of a new type; a new type can only be declared in a
15635 declaration context. */
15641 /* Friends have special name lookup rules. */
15642 ts
= ts_within_enclosing_non_class
;
15643 else if (is_declaration
15644 && cp_lexer_next_token_is (parser
->lexer
,
15646 /* This is a `class-key identifier ;' */
15652 (template_parm_lists_apply
15653 && (cp_parser_next_token_starts_class_definition_p (parser
)
15654 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15655 /* An unqualified name was used to reference this type, so
15656 there were no qualifying templates. */
15657 if (template_parm_lists_apply
15658 && !cp_parser_check_template_parameters (parser
,
15659 /*num_templates=*/0,
15661 /*declarator=*/NULL
))
15662 return error_mark_node
;
15663 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15667 if (type
== error_mark_node
)
15668 return error_mark_node
;
15670 /* Allow attributes on forward declarations of classes. */
15673 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15674 warning (OPT_Wattributes
,
15675 "attributes ignored on uninstantiated type");
15676 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15677 && ! processing_explicit_instantiation
)
15678 warning (OPT_Wattributes
,
15679 "attributes ignored on template instantiation");
15680 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15681 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15683 warning (OPT_Wattributes
,
15684 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15687 if (tag_type
!= enum_type
)
15689 /* Indicate whether this class was declared as a `class' or as a
15691 if (TREE_CODE (type
) == RECORD_TYPE
)
15692 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15693 cp_parser_check_class_key (tag_type
, type
);
15696 /* A "<" cannot follow an elaborated type specifier. If that
15697 happens, the user was probably trying to form a template-id. */
15698 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15704 /* Parse an enum-specifier.
15707 enum-head { enumerator-list [opt] }
15708 enum-head { enumerator-list , } [C++0x]
15711 enum-key identifier [opt] enum-base [opt]
15712 enum-key nested-name-specifier identifier enum-base [opt]
15717 enum struct [C++0x]
15720 : type-specifier-seq
15722 opaque-enum-specifier:
15723 enum-key identifier enum-base [opt] ;
15726 enum-key attributes[opt] identifier [opt] enum-base [opt]
15727 { enumerator-list [opt] }attributes[opt]
15728 enum-key attributes[opt] identifier [opt] enum-base [opt]
15729 { enumerator-list, }attributes[opt] [C++0x]
15731 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15732 if the token stream isn't an enum-specifier after all. */
15735 cp_parser_enum_specifier (cp_parser
* parser
)
15738 tree type
= NULL_TREE
;
15740 tree nested_name_specifier
= NULL_TREE
;
15742 bool scoped_enum_p
= false;
15743 bool has_underlying_type
= false;
15744 bool nested_being_defined
= false;
15745 bool new_value_list
= false;
15746 bool is_new_type
= false;
15747 bool is_anonymous
= false;
15748 tree underlying_type
= NULL_TREE
;
15749 cp_token
*type_start_token
= NULL
;
15750 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15752 parser
->colon_corrects_to_scope_p
= false;
15754 /* Parse tentatively so that we can back up if we don't find a
15756 cp_parser_parse_tentatively (parser
);
15758 /* Caller guarantees that the current token is 'enum', an identifier
15759 possibly follows, and the token after that is an opening brace.
15760 If we don't have an identifier, fabricate an anonymous name for
15761 the enumeration being defined. */
15762 cp_lexer_consume_token (parser
->lexer
);
15764 /* Parse the "class" or "struct", which indicates a scoped
15765 enumeration type in C++0x. */
15766 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15767 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15769 if (cxx_dialect
< cxx11
)
15770 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15772 /* Consume the `struct' or `class' token. */
15773 cp_lexer_consume_token (parser
->lexer
);
15775 scoped_enum_p
= true;
15778 attributes
= cp_parser_attributes_opt (parser
);
15780 /* Clear the qualification. */
15781 parser
->scope
= NULL_TREE
;
15782 parser
->qualifying_scope
= NULL_TREE
;
15783 parser
->object_scope
= NULL_TREE
;
15785 /* Figure out in what scope the declaration is being placed. */
15786 prev_scope
= current_scope ();
15788 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15790 push_deferring_access_checks (dk_no_check
);
15791 nested_name_specifier
15792 = cp_parser_nested_name_specifier_opt (parser
,
15793 /*typename_keyword_p=*/true,
15794 /*check_dependency_p=*/false,
15796 /*is_declaration=*/false);
15798 if (nested_name_specifier
)
15802 identifier
= cp_parser_identifier (parser
);
15803 name
= cp_parser_lookup_name (parser
, identifier
,
15805 /*is_template=*/false,
15806 /*is_namespace=*/false,
15807 /*check_dependency=*/true,
15808 /*ambiguous_decls=*/NULL
,
15810 if (name
&& name
!= error_mark_node
)
15812 type
= TREE_TYPE (name
);
15813 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15815 /* Are template enums allowed in ISO? */
15816 if (template_parm_scope_p ())
15817 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15818 "%qD is an enumeration template", name
);
15819 /* ignore a typename reference, for it will be solved by name
15824 else if (nested_name_specifier
== error_mark_node
)
15825 /* We already issued an error. */;
15827 error_at (type_start_token
->location
,
15828 "%qD is not an enumerator-name", identifier
);
15832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15833 identifier
= cp_parser_identifier (parser
);
15836 identifier
= make_anon_name ();
15837 is_anonymous
= true;
15839 error_at (type_start_token
->location
,
15840 "anonymous scoped enum is not allowed");
15843 pop_deferring_access_checks ();
15845 /* Check for the `:' that denotes a specified underlying type in C++0x.
15846 Note that a ':' could also indicate a bitfield width, however. */
15847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15849 cp_decl_specifier_seq type_specifiers
;
15851 /* Consume the `:'. */
15852 cp_lexer_consume_token (parser
->lexer
);
15854 /* Parse the type-specifier-seq. */
15855 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15856 /*is_trailing_return=*/false,
15859 /* At this point this is surely not elaborated type specifier. */
15860 if (!cp_parser_parse_definitely (parser
))
15863 if (cxx_dialect
< cxx11
)
15864 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15866 has_underlying_type
= true;
15868 /* If that didn't work, stop. */
15869 if (type_specifiers
.type
!= error_mark_node
)
15871 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15872 /*initialized=*/0, NULL
);
15873 if (underlying_type
== error_mark_node
15874 || check_for_bare_parameter_packs (underlying_type
))
15875 underlying_type
= NULL_TREE
;
15879 /* Look for the `{' but don't consume it yet. */
15880 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15882 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15884 cp_parser_error (parser
, "expected %<{%>");
15885 if (has_underlying_type
)
15891 /* An opaque-enum-specifier must have a ';' here. */
15892 if ((scoped_enum_p
|| underlying_type
)
15893 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15895 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15896 if (has_underlying_type
)
15904 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15907 if (nested_name_specifier
)
15909 if (CLASS_TYPE_P (nested_name_specifier
))
15911 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15912 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15913 push_scope (nested_name_specifier
);
15915 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15917 push_nested_namespace (nested_name_specifier
);
15921 /* Issue an error message if type-definitions are forbidden here. */
15922 if (!cp_parser_check_type_definition (parser
))
15923 type
= error_mark_node
;
15925 /* Create the new type. We do this before consuming the opening
15926 brace so the enum will be recorded as being on the line of its
15927 tag (or the 'enum' keyword, if there is no tag). */
15928 type
= start_enum (identifier
, type
, underlying_type
,
15929 scoped_enum_p
, &is_new_type
);
15931 /* If the next token is not '{' it is an opaque-enum-specifier or an
15932 elaborated-type-specifier. */
15933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15935 timevar_push (TV_PARSE_ENUM
);
15936 if (nested_name_specifier
15937 && nested_name_specifier
!= error_mark_node
)
15939 /* The following catches invalid code such as:
15940 enum class S<int>::E { A, B, C }; */
15941 if (!processing_specialization
15942 && CLASS_TYPE_P (nested_name_specifier
)
15943 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15944 error_at (type_start_token
->location
, "cannot add an enumerator "
15945 "list to a template instantiation");
15947 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15949 error_at (type_start_token
->location
,
15950 "%<%T::%E%> has not been declared",
15951 TYPE_CONTEXT (nested_name_specifier
),
15952 nested_name_specifier
);
15953 type
= error_mark_node
;
15955 /* If that scope does not contain the scope in which the
15956 class was originally declared, the program is invalid. */
15957 else if (prev_scope
&& !is_ancestor (prev_scope
,
15958 nested_name_specifier
))
15960 if (at_namespace_scope_p ())
15961 error_at (type_start_token
->location
,
15962 "declaration of %qD in namespace %qD which does not "
15964 type
, prev_scope
, nested_name_specifier
);
15966 error_at (type_start_token
->location
,
15967 "declaration of %qD in %qD which does not "
15969 type
, prev_scope
, nested_name_specifier
);
15970 type
= error_mark_node
;
15975 begin_scope (sk_scoped_enum
, type
);
15977 /* Consume the opening brace. */
15978 cp_lexer_consume_token (parser
->lexer
);
15980 if (type
== error_mark_node
)
15981 ; /* Nothing to add */
15982 else if (OPAQUE_ENUM_P (type
)
15983 || (cxx_dialect
> cxx98
&& processing_specialization
))
15985 new_value_list
= true;
15986 SET_OPAQUE_ENUM_P (type
, false);
15987 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15991 error_at (type_start_token
->location
,
15992 "multiple definition of %q#T", type
);
15993 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15994 "previous definition here");
15995 type
= error_mark_node
;
15998 if (type
== error_mark_node
)
15999 cp_parser_skip_to_end_of_block_or_statement (parser
);
16000 /* If the next token is not '}', then there are some enumerators. */
16001 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16003 if (is_anonymous
&& !scoped_enum_p
)
16004 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
16005 "ISO C++ forbids empty anonymous enum");
16008 cp_parser_enumerator_list (parser
, type
);
16010 /* Consume the final '}'. */
16011 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16015 timevar_pop (TV_PARSE_ENUM
);
16019 /* If a ';' follows, then it is an opaque-enum-specifier
16020 and additional restrictions apply. */
16021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
16024 error_at (type_start_token
->location
,
16025 "opaque-enum-specifier without name");
16026 else if (nested_name_specifier
)
16027 error_at (type_start_token
->location
,
16028 "opaque-enum-specifier must use a simple identifier");
16032 /* Look for trailing attributes to apply to this enumeration, and
16033 apply them if appropriate. */
16034 if (cp_parser_allow_gnu_extensions_p (parser
))
16036 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
16037 trailing_attr
= chainon (trailing_attr
, attributes
);
16038 cplus_decl_attributes (&type
,
16040 (int) ATTR_FLAG_TYPE_IN_PLACE
);
16043 /* Finish up the enumeration. */
16044 if (type
!= error_mark_node
)
16046 if (new_value_list
)
16047 finish_enum_value_list (type
);
16049 finish_enum (type
);
16052 if (nested_name_specifier
)
16054 if (CLASS_TYPE_P (nested_name_specifier
))
16056 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
16057 pop_scope (nested_name_specifier
);
16059 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16061 pop_nested_namespace (nested_name_specifier
);
16065 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
16069 /* Parse an enumerator-list. The enumerators all have the indicated
16073 enumerator-definition
16074 enumerator-list , enumerator-definition */
16077 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
16081 /* Parse an enumerator-definition. */
16082 cp_parser_enumerator_definition (parser
, type
);
16084 /* If the next token is not a ',', we've reached the end of
16086 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16088 /* Otherwise, consume the `,' and keep going. */
16089 cp_lexer_consume_token (parser
->lexer
);
16090 /* If the next token is a `}', there is a trailing comma. */
16091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16093 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
16094 pedwarn (input_location
, OPT_Wpedantic
,
16095 "comma at end of enumerator list");
16101 /* Parse an enumerator-definition. The enumerator has the indicated
16104 enumerator-definition:
16106 enumerator = constant-expression
16113 enumerator-definition:
16114 enumerator attributes [opt]
16115 enumerator attributes [opt] = constant-expression */
16118 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16124 /* Save the input location because we are interested in the location
16125 of the identifier and not the location of the explicit value. */
16126 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16128 /* Look for the identifier. */
16129 identifier
= cp_parser_identifier (parser
);
16130 if (identifier
== error_mark_node
)
16133 /* Parse any specified attributes. */
16134 tree attrs
= cp_parser_attributes_opt (parser
);
16136 /* If the next token is an '=', then there is an explicit value. */
16137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16139 /* Consume the `=' token. */
16140 cp_lexer_consume_token (parser
->lexer
);
16141 /* Parse the value. */
16142 value
= cp_parser_constant_expression (parser
);
16147 /* If we are processing a template, make sure the initializer of the
16148 enumerator doesn't contain any bare template parameter pack. */
16149 if (check_for_bare_parameter_packs (value
))
16150 value
= error_mark_node
;
16152 /* Create the enumerator. */
16153 build_enumerator (identifier
, value
, type
, attrs
, loc
);
16156 /* Parse a namespace-name.
16159 original-namespace-name
16162 Returns the NAMESPACE_DECL for the namespace. */
16165 cp_parser_namespace_name (cp_parser
* parser
)
16168 tree namespace_decl
;
16170 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16172 /* Get the name of the namespace. */
16173 identifier
= cp_parser_identifier (parser
);
16174 if (identifier
== error_mark_node
)
16175 return error_mark_node
;
16177 /* Look up the identifier in the currently active scope. Look only
16178 for namespaces, due to:
16180 [basic.lookup.udir]
16182 When looking up a namespace-name in a using-directive or alias
16183 definition, only namespace names are considered.
16187 [basic.lookup.qual]
16189 During the lookup of a name preceding the :: scope resolution
16190 operator, object, function, and enumerator names are ignored.
16192 (Note that cp_parser_qualifying_entity only calls this
16193 function if the token after the name is the scope resolution
16195 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16197 /*is_template=*/false,
16198 /*is_namespace=*/true,
16199 /*check_dependency=*/true,
16200 /*ambiguous_decls=*/NULL
,
16202 /* If it's not a namespace, issue an error. */
16203 if (namespace_decl
== error_mark_node
16204 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16206 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16207 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16208 cp_parser_error (parser
, "expected namespace-name");
16209 namespace_decl
= error_mark_node
;
16212 return namespace_decl
;
16215 /* Parse a namespace-definition.
16217 namespace-definition:
16218 named-namespace-definition
16219 unnamed-namespace-definition
16221 named-namespace-definition:
16222 original-namespace-definition
16223 extension-namespace-definition
16225 original-namespace-definition:
16226 namespace identifier { namespace-body }
16228 extension-namespace-definition:
16229 namespace original-namespace-name { namespace-body }
16231 unnamed-namespace-definition:
16232 namespace { namespace-body } */
16235 cp_parser_namespace_definition (cp_parser
* parser
)
16237 tree identifier
, attribs
;
16238 bool has_visibility
;
16241 cp_ensure_no_omp_declare_simd (parser
);
16242 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16244 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16246 cp_lexer_consume_token (parser
->lexer
);
16251 /* Look for the `namespace' keyword. */
16252 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16254 /* Get the name of the namespace. We do not attempt to distinguish
16255 between an original-namespace-definition and an
16256 extension-namespace-definition at this point. The semantic
16257 analysis routines are responsible for that. */
16258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16259 identifier
= cp_parser_identifier (parser
);
16261 identifier
= NULL_TREE
;
16263 /* Parse any specified attributes. */
16264 attribs
= cp_parser_attributes_opt (parser
);
16266 /* Look for the `{' to start the namespace. */
16267 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16268 /* Start the namespace. */
16269 push_namespace (identifier
);
16271 /* "inline namespace" is equivalent to a stub namespace definition
16272 followed by a strong using directive. */
16275 tree name_space
= current_namespace
;
16276 /* Set up namespace association. */
16277 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16278 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16279 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16280 /* Import the contents of the inline namespace. */
16282 do_using_directive (name_space
);
16283 push_namespace (identifier
);
16286 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16288 /* Parse the body of the namespace. */
16289 cp_parser_namespace_body (parser
);
16291 if (has_visibility
)
16292 pop_visibility (1);
16294 /* Finish the namespace. */
16296 /* Look for the final `}'. */
16297 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16300 /* Parse a namespace-body.
16303 declaration-seq [opt] */
16306 cp_parser_namespace_body (cp_parser
* parser
)
16308 cp_parser_declaration_seq_opt (parser
);
16311 /* Parse a namespace-alias-definition.
16313 namespace-alias-definition:
16314 namespace identifier = qualified-namespace-specifier ; */
16317 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16320 tree namespace_specifier
;
16322 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16324 /* Look for the `namespace' keyword. */
16325 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16326 /* Look for the identifier. */
16327 identifier
= cp_parser_identifier (parser
);
16328 if (identifier
== error_mark_node
)
16330 /* Look for the `=' token. */
16331 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16332 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16334 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16335 /* Skip the definition. */
16336 cp_lexer_consume_token (parser
->lexer
);
16337 if (cp_parser_skip_to_closing_brace (parser
))
16338 cp_lexer_consume_token (parser
->lexer
);
16341 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16342 /* Look for the qualified-namespace-specifier. */
16343 namespace_specifier
16344 = cp_parser_qualified_namespace_specifier (parser
);
16345 /* Look for the `;' token. */
16346 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16348 /* Register the alias in the symbol table. */
16349 do_namespace_alias (identifier
, namespace_specifier
);
16352 /* Parse a qualified-namespace-specifier.
16354 qualified-namespace-specifier:
16355 :: [opt] nested-name-specifier [opt] namespace-name
16357 Returns a NAMESPACE_DECL corresponding to the specified
16361 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16363 /* Look for the optional `::'. */
16364 cp_parser_global_scope_opt (parser
,
16365 /*current_scope_valid_p=*/false);
16367 /* Look for the optional nested-name-specifier. */
16368 cp_parser_nested_name_specifier_opt (parser
,
16369 /*typename_keyword_p=*/false,
16370 /*check_dependency_p=*/true,
16372 /*is_declaration=*/true);
16374 return cp_parser_namespace_name (parser
);
16377 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16378 access declaration.
16381 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16382 using :: unqualified-id ;
16384 access-declaration:
16390 cp_parser_using_declaration (cp_parser
* parser
,
16391 bool access_declaration_p
)
16394 bool typename_p
= false;
16395 bool global_scope_p
;
16399 int oldcount
= errorcount
;
16400 cp_token
*diag_token
= NULL
;
16402 if (access_declaration_p
)
16404 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16405 cp_parser_parse_tentatively (parser
);
16409 /* Look for the `using' keyword. */
16410 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16412 /* Peek at the next token. */
16413 token
= cp_lexer_peek_token (parser
->lexer
);
16414 /* See if it's `typename'. */
16415 if (token
->keyword
== RID_TYPENAME
)
16417 /* Remember that we've seen it. */
16419 /* Consume the `typename' token. */
16420 cp_lexer_consume_token (parser
->lexer
);
16424 /* Look for the optional global scope qualification. */
16426 = (cp_parser_global_scope_opt (parser
,
16427 /*current_scope_valid_p=*/false)
16430 /* If we saw `typename', or didn't see `::', then there must be a
16431 nested-name-specifier present. */
16432 if (typename_p
|| !global_scope_p
)
16434 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16435 /*check_dependency_p=*/true,
16437 /*is_declaration=*/true);
16438 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16440 cp_parser_skip_to_end_of_block_or_statement (parser
);
16444 /* Otherwise, we could be in either of the two productions. In that
16445 case, treat the nested-name-specifier as optional. */
16447 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16448 /*typename_keyword_p=*/false,
16449 /*check_dependency_p=*/true,
16451 /*is_declaration=*/true);
16453 qscope
= global_namespace
;
16454 else if (UNSCOPED_ENUM_P (qscope
))
16455 qscope
= CP_TYPE_CONTEXT (qscope
);
16457 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16458 /* Something has already gone wrong; there's no need to parse
16459 further. Since an error has occurred, the return value of
16460 cp_parser_parse_definitely will be false, as required. */
16461 return cp_parser_parse_definitely (parser
);
16463 token
= cp_lexer_peek_token (parser
->lexer
);
16464 /* Parse the unqualified-id. */
16465 identifier
= cp_parser_unqualified_id (parser
,
16466 /*template_keyword_p=*/false,
16467 /*check_dependency_p=*/true,
16468 /*declarator_p=*/true,
16469 /*optional_p=*/false);
16471 if (access_declaration_p
)
16473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16474 cp_parser_simulate_error (parser
);
16475 if (!cp_parser_parse_definitely (parser
))
16479 /* The function we call to handle a using-declaration is different
16480 depending on what scope we are in. */
16481 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16483 else if (!identifier_p (identifier
)
16484 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16485 /* [namespace.udecl]
16487 A using declaration shall not name a template-id. */
16488 error_at (token
->location
,
16489 "a template-id may not appear in a using-declaration");
16492 if (at_class_scope_p ())
16494 /* Create the USING_DECL. */
16495 decl
= do_class_using_decl (parser
->scope
, identifier
);
16497 if (decl
&& typename_p
)
16498 USING_DECL_TYPENAME_P (decl
) = 1;
16500 if (check_for_bare_parameter_packs (decl
))
16502 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16506 /* Add it to the list of members in this class. */
16507 finish_member_declaration (decl
);
16511 decl
= cp_parser_lookup_name_simple (parser
,
16514 if (decl
== error_mark_node
)
16515 cp_parser_name_lookup_error (parser
, identifier
,
16518 else if (check_for_bare_parameter_packs (decl
))
16520 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16523 else if (!at_namespace_scope_p ())
16524 do_local_using_decl (decl
, qscope
, identifier
);
16526 do_toplevel_using_decl (decl
, qscope
, identifier
);
16530 /* Look for the final `;'. */
16531 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16533 if (access_declaration_p
&& errorcount
== oldcount
)
16534 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16535 "access declarations are deprecated "
16536 "in favour of using-declarations; "
16537 "suggestion: add the %<using%> keyword");
16542 /* Parse an alias-declaration.
16545 using identifier attribute-specifier-seq [opt] = type-id */
16548 cp_parser_alias_declaration (cp_parser
* parser
)
16550 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16551 location_t id_location
;
16552 cp_declarator
*declarator
;
16553 cp_decl_specifier_seq decl_specs
;
16555 const char *saved_message
= NULL
;
16557 /* Look for the `using' keyword. */
16558 cp_token
*using_token
16559 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16560 if (using_token
== NULL
)
16561 return error_mark_node
;
16563 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16564 id
= cp_parser_identifier (parser
);
16565 if (id
== error_mark_node
)
16566 return error_mark_node
;
16568 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16569 attributes
= cp_parser_attributes_opt (parser
);
16570 if (attributes
== error_mark_node
)
16571 return error_mark_node
;
16573 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16575 if (cp_parser_error_occurred (parser
))
16576 return error_mark_node
;
16578 cp_parser_commit_to_tentative_parse (parser
);
16580 /* Now we are going to parse the type-id of the declaration. */
16585 "A type-specifier-seq shall not define a class or enumeration
16586 unless it appears in the type-id of an alias-declaration (7.1.3) that
16587 is not the declaration of a template-declaration."
16589 In other words, if we currently are in an alias template, the
16590 type-id should not define a type.
16592 So let's set parser->type_definition_forbidden_message in that
16593 case; cp_parser_check_type_definition (called by
16594 cp_parser_class_specifier) will then emit an error if a type is
16595 defined in the type-id. */
16596 if (parser
->num_template_parameter_lists
)
16598 saved_message
= parser
->type_definition_forbidden_message
;
16599 parser
->type_definition_forbidden_message
=
16600 G_("types may not be defined in alias template declarations");
16603 type
= cp_parser_type_id (parser
);
16605 /* Restore the error message if need be. */
16606 if (parser
->num_template_parameter_lists
)
16607 parser
->type_definition_forbidden_message
= saved_message
;
16609 if (type
== error_mark_node
16610 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16612 cp_parser_skip_to_end_of_block_or_statement (parser
);
16613 return error_mark_node
;
16616 /* A typedef-name can also be introduced by an alias-declaration. The
16617 identifier following the using keyword becomes a typedef-name. It has
16618 the same semantics as if it were introduced by the typedef
16619 specifier. In particular, it does not define a new type and it shall
16620 not appear in the type-id. */
16622 clear_decl_specs (&decl_specs
);
16623 decl_specs
.type
= type
;
16624 if (attributes
!= NULL_TREE
)
16626 decl_specs
.attributes
= attributes
;
16627 set_and_check_decl_spec_loc (&decl_specs
,
16631 set_and_check_decl_spec_loc (&decl_specs
,
16634 set_and_check_decl_spec_loc (&decl_specs
,
16638 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16639 declarator
->id_loc
= id_location
;
16641 member_p
= at_class_scope_p ();
16643 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16644 NULL_TREE
, attributes
);
16646 decl
= start_decl (declarator
, &decl_specs
, 0,
16647 attributes
, NULL_TREE
, &pushed_scope
);
16648 if (decl
== error_mark_node
)
16651 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16654 pop_scope (pushed_scope
);
16656 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16657 added into the symbol table; otherwise, return the TYPE_DECL. */
16658 if (DECL_LANG_SPECIFIC (decl
)
16659 && DECL_TEMPLATE_INFO (decl
)
16660 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16662 decl
= DECL_TI_TEMPLATE (decl
);
16664 check_member_template (decl
);
16670 /* Parse a using-directive.
16673 using namespace :: [opt] nested-name-specifier [opt]
16674 namespace-name ; */
16677 cp_parser_using_directive (cp_parser
* parser
)
16679 tree namespace_decl
;
16682 /* Look for the `using' keyword. */
16683 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16684 /* And the `namespace' keyword. */
16685 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16686 /* Look for the optional `::' operator. */
16687 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16688 /* And the optional nested-name-specifier. */
16689 cp_parser_nested_name_specifier_opt (parser
,
16690 /*typename_keyword_p=*/false,
16691 /*check_dependency_p=*/true,
16693 /*is_declaration=*/true);
16694 /* Get the namespace being used. */
16695 namespace_decl
= cp_parser_namespace_name (parser
);
16696 /* And any specified attributes. */
16697 attribs
= cp_parser_attributes_opt (parser
);
16698 /* Update the symbol table. */
16699 parse_using_directive (namespace_decl
, attribs
);
16700 /* Look for the final `;'. */
16701 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16704 /* Parse an asm-definition.
16707 asm ( string-literal ) ;
16712 asm volatile [opt] ( string-literal ) ;
16713 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16714 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16715 : asm-operand-list [opt] ) ;
16716 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16717 : asm-operand-list [opt]
16718 : asm-clobber-list [opt] ) ;
16719 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16720 : asm-clobber-list [opt]
16721 : asm-goto-list ) ; */
16724 cp_parser_asm_definition (cp_parser
* parser
)
16727 tree outputs
= NULL_TREE
;
16728 tree inputs
= NULL_TREE
;
16729 tree clobbers
= NULL_TREE
;
16730 tree labels
= NULL_TREE
;
16732 bool volatile_p
= false;
16733 bool extended_p
= false;
16734 bool invalid_inputs_p
= false;
16735 bool invalid_outputs_p
= false;
16736 bool goto_p
= false;
16737 required_token missing
= RT_NONE
;
16739 /* Look for the `asm' keyword. */
16740 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16742 if (parser
->in_function_body
16743 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16745 error ("%<asm%> in %<constexpr%> function");
16746 cp_function_chain
->invalid_constexpr
= true;
16749 /* See if the next token is `volatile'. */
16750 if (cp_parser_allow_gnu_extensions_p (parser
)
16751 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16753 /* Remember that we saw the `volatile' keyword. */
16755 /* Consume the token. */
16756 cp_lexer_consume_token (parser
->lexer
);
16758 if (cp_parser_allow_gnu_extensions_p (parser
)
16759 && parser
->in_function_body
16760 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16762 /* Remember that we saw the `goto' keyword. */
16764 /* Consume the token. */
16765 cp_lexer_consume_token (parser
->lexer
);
16767 /* Look for the opening `('. */
16768 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16770 /* Look for the string. */
16771 string
= cp_parser_string_literal (parser
, false, false);
16772 if (string
== error_mark_node
)
16774 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16775 /*consume_paren=*/true);
16779 /* If we're allowing GNU extensions, check for the extended assembly
16780 syntax. Unfortunately, the `:' tokens need not be separated by
16781 a space in C, and so, for compatibility, we tolerate that here
16782 too. Doing that means that we have to treat the `::' operator as
16784 if (cp_parser_allow_gnu_extensions_p (parser
)
16785 && parser
->in_function_body
16786 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16787 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16789 bool inputs_p
= false;
16790 bool clobbers_p
= false;
16791 bool labels_p
= false;
16793 /* The extended syntax was used. */
16796 /* Look for outputs. */
16797 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16799 /* Consume the `:'. */
16800 cp_lexer_consume_token (parser
->lexer
);
16801 /* Parse the output-operands. */
16802 if (cp_lexer_next_token_is_not (parser
->lexer
,
16804 && cp_lexer_next_token_is_not (parser
->lexer
,
16806 && cp_lexer_next_token_is_not (parser
->lexer
,
16810 outputs
= cp_parser_asm_operand_list (parser
);
16811 if (outputs
== error_mark_node
)
16812 invalid_outputs_p
= true;
16815 /* If the next token is `::', there are no outputs, and the
16816 next token is the beginning of the inputs. */
16817 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16818 /* The inputs are coming next. */
16821 /* Look for inputs. */
16823 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16825 /* Consume the `:' or `::'. */
16826 cp_lexer_consume_token (parser
->lexer
);
16827 /* Parse the output-operands. */
16828 if (cp_lexer_next_token_is_not (parser
->lexer
,
16830 && cp_lexer_next_token_is_not (parser
->lexer
,
16832 && cp_lexer_next_token_is_not (parser
->lexer
,
16835 inputs
= cp_parser_asm_operand_list (parser
);
16836 if (inputs
== error_mark_node
)
16837 invalid_inputs_p
= true;
16840 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16841 /* The clobbers are coming next. */
16844 /* Look for clobbers. */
16846 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16849 /* Consume the `:' or `::'. */
16850 cp_lexer_consume_token (parser
->lexer
);
16851 /* Parse the clobbers. */
16852 if (cp_lexer_next_token_is_not (parser
->lexer
,
16854 && cp_lexer_next_token_is_not (parser
->lexer
,
16856 clobbers
= cp_parser_asm_clobber_list (parser
);
16859 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16860 /* The labels are coming next. */
16863 /* Look for labels. */
16865 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16868 /* Consume the `:' or `::'. */
16869 cp_lexer_consume_token (parser
->lexer
);
16870 /* Parse the labels. */
16871 labels
= cp_parser_asm_label_list (parser
);
16874 if (goto_p
&& !labels_p
)
16875 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16878 missing
= RT_COLON_SCOPE
;
16880 /* Look for the closing `)'. */
16881 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16882 missing
? missing
: RT_CLOSE_PAREN
))
16883 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16884 /*consume_paren=*/true);
16885 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16887 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16889 /* Create the ASM_EXPR. */
16890 if (parser
->in_function_body
)
16892 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16893 inputs
, clobbers
, labels
);
16894 /* If the extended syntax was not used, mark the ASM_EXPR. */
16897 tree temp
= asm_stmt
;
16898 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16899 temp
= TREE_OPERAND (temp
, 0);
16901 ASM_INPUT_P (temp
) = 1;
16905 symtab
->finalize_toplevel_asm (string
);
16909 /* Declarators [gram.dcl.decl] */
16911 /* Parse an init-declarator.
16914 declarator initializer [opt]
16919 declarator asm-specification [opt] attributes [opt] initializer [opt]
16921 function-definition:
16922 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16924 decl-specifier-seq [opt] declarator function-try-block
16928 function-definition:
16929 __extension__ function-definition
16933 function-definition:
16934 decl-specifier-seq [opt] declarator function-transaction-block
16936 The DECL_SPECIFIERS apply to this declarator. Returns a
16937 representation of the entity declared. If MEMBER_P is TRUE, then
16938 this declarator appears in a class scope. The new DECL created by
16939 this declarator is returned.
16941 The CHECKS are access checks that should be performed once we know
16942 what entity is being declared (and, therefore, what classes have
16945 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16946 for a function-definition here as well. If the declarator is a
16947 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16948 be TRUE upon return. By that point, the function-definition will
16949 have been completely parsed.
16951 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16954 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16955 parsed declaration if it is an uninitialized single declarator not followed
16956 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16957 if present, will not be consumed. If returned, this declarator will be
16958 created with SD_INITIALIZED but will not call cp_finish_decl.
16960 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16961 and there is an initializer, the pointed location_t is set to the
16962 location of the '=' or `(', or '{' in C++11 token introducing the
16966 cp_parser_init_declarator (cp_parser
* parser
,
16967 cp_decl_specifier_seq
*decl_specifiers
,
16968 vec
<deferred_access_check
, va_gc
> *checks
,
16969 bool function_definition_allowed_p
,
16971 int declares_class_or_enum
,
16972 bool* function_definition_p
,
16973 tree
* maybe_range_for_decl
,
16974 location_t
* init_loc
)
16976 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16977 *attributes_start_token
= NULL
;
16978 cp_declarator
*declarator
;
16979 tree prefix_attributes
;
16980 tree attributes
= NULL
;
16981 tree asm_specification
;
16983 tree decl
= NULL_TREE
;
16985 int is_initialized
;
16986 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16987 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16989 enum cpp_ttype initialization_kind
;
16990 bool is_direct_init
= false;
16991 bool is_non_constant_init
;
16992 int ctor_dtor_or_conv_p
;
16993 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16994 tree pushed_scope
= NULL_TREE
;
16995 bool range_for_decl_p
= false;
16996 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16997 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
16999 /* Gather the attributes that were provided with the
17000 decl-specifiers. */
17001 prefix_attributes
= decl_specifiers
->attributes
;
17003 /* Assume that this is not the declarator for a function
17005 if (function_definition_p
)
17006 *function_definition_p
= false;
17008 /* Default arguments are only permitted for function parameters. */
17009 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
17010 parser
->default_arg_ok_p
= false;
17012 /* Defer access checks while parsing the declarator; we cannot know
17013 what names are accessible until we know what is being
17015 resume_deferring_access_checks ();
17017 /* Parse the declarator. */
17018 token
= cp_lexer_peek_token (parser
->lexer
);
17020 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17021 &ctor_dtor_or_conv_p
,
17022 /*parenthesized_p=*/NULL
,
17023 member_p
, friend_p
);
17024 /* Gather up the deferred checks. */
17025 stop_deferring_access_checks ();
17027 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17029 /* If the DECLARATOR was erroneous, there's no need to go
17031 if (declarator
== cp_error_declarator
)
17032 return error_mark_node
;
17034 /* Check that the number of template-parameter-lists is OK. */
17035 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
17037 return error_mark_node
;
17039 if (declares_class_or_enum
& 2)
17040 cp_parser_check_for_definition_in_return_type (declarator
,
17041 decl_specifiers
->type
,
17042 decl_specifiers
->locations
[ds_type_spec
]);
17044 /* Figure out what scope the entity declared by the DECLARATOR is
17045 located in. `grokdeclarator' sometimes changes the scope, so
17046 we compute it now. */
17047 scope
= get_scope_of_declarator (declarator
);
17049 /* Perform any lookups in the declared type which were thought to be
17050 dependent, but are not in the scope of the declarator. */
17051 decl_specifiers
->type
17052 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
17054 /* If we're allowing GNU extensions, look for an
17055 asm-specification. */
17056 if (cp_parser_allow_gnu_extensions_p (parser
))
17058 /* Look for an asm-specification. */
17059 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
17060 asm_specification
= cp_parser_asm_specification_opt (parser
);
17063 asm_specification
= NULL_TREE
;
17065 /* Look for attributes. */
17066 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
17067 attributes
= cp_parser_attributes_opt (parser
);
17069 /* Peek at the next token. */
17070 token
= cp_lexer_peek_token (parser
->lexer
);
17072 bool bogus_implicit_tmpl
= false;
17074 if (function_declarator_p (declarator
))
17076 /* Check to see if the token indicates the start of a
17077 function-definition. */
17078 if (cp_parser_token_starts_function_definition_p (token
))
17080 if (!function_definition_allowed_p
)
17082 /* If a function-definition should not appear here, issue an
17084 cp_parser_error (parser
,
17085 "a function-definition is not allowed here");
17086 return error_mark_node
;
17089 location_t func_brace_location
17090 = cp_lexer_peek_token (parser
->lexer
)->location
;
17092 /* Neither attributes nor an asm-specification are allowed
17093 on a function-definition. */
17094 if (asm_specification
)
17095 error_at (asm_spec_start_token
->location
,
17096 "an asm-specification is not allowed "
17097 "on a function-definition");
17099 error_at (attributes_start_token
->location
,
17100 "attributes are not allowed "
17101 "on a function-definition");
17102 /* This is a function-definition. */
17103 *function_definition_p
= true;
17105 /* Parse the function definition. */
17107 decl
= cp_parser_save_member_function_body (parser
,
17110 prefix_attributes
);
17113 (cp_parser_function_definition_from_specifiers_and_declarator
17114 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
17116 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
17118 /* This is where the prologue starts... */
17119 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
17120 = func_brace_location
;
17126 else if (parser
->fully_implicit_function_template_p
)
17128 /* A non-template declaration involving a function parameter list
17129 containing an implicit template parameter will be made into a
17130 template. If the resulting declaration is not going to be an
17131 actual function then finish the template scope here to prevent it.
17132 An error message will be issued once we have a decl to talk about.
17134 FIXME probably we should do type deduction rather than create an
17135 implicit template, but the standard currently doesn't allow it. */
17136 bogus_implicit_tmpl
= true;
17137 finish_fully_implicit_template (parser
, NULL_TREE
);
17142 Only in function declarations for constructors, destructors, and
17143 type conversions can the decl-specifier-seq be omitted.
17145 We explicitly postpone this check past the point where we handle
17146 function-definitions because we tolerate function-definitions
17147 that are missing their return types in some modes. */
17148 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
17150 cp_parser_error (parser
,
17151 "expected constructor, destructor, or type conversion");
17152 return error_mark_node
;
17155 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17156 if (token
->type
== CPP_EQ
17157 || token
->type
== CPP_OPEN_PAREN
17158 || token
->type
== CPP_OPEN_BRACE
)
17160 is_initialized
= SD_INITIALIZED
;
17161 initialization_kind
= token
->type
;
17162 if (maybe_range_for_decl
)
17163 *maybe_range_for_decl
= error_mark_node
;
17164 tmp_init_loc
= token
->location
;
17165 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
17166 *init_loc
= tmp_init_loc
;
17168 if (token
->type
== CPP_EQ
17169 && function_declarator_p (declarator
))
17171 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17172 if (t2
->keyword
== RID_DEFAULT
)
17173 is_initialized
= SD_DEFAULTED
;
17174 else if (t2
->keyword
== RID_DELETE
)
17175 is_initialized
= SD_DELETED
;
17180 /* If the init-declarator isn't initialized and isn't followed by a
17181 `,' or `;', it's not a valid init-declarator. */
17182 if (token
->type
!= CPP_COMMA
17183 && token
->type
!= CPP_SEMICOLON
)
17185 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17186 range_for_decl_p
= true;
17189 if (!maybe_range_for_decl
)
17190 cp_parser_error (parser
, "expected initializer");
17191 return error_mark_node
;
17194 is_initialized
= SD_UNINITIALIZED
;
17195 initialization_kind
= CPP_EOF
;
17198 /* Because start_decl has side-effects, we should only call it if we
17199 know we're going ahead. By this point, we know that we cannot
17200 possibly be looking at any other construct. */
17201 cp_parser_commit_to_tentative_parse (parser
);
17203 /* Enter the newly declared entry in the symbol table. If we're
17204 processing a declaration in a class-specifier, we wait until
17205 after processing the initializer. */
17208 if (parser
->in_unbraced_linkage_specification_p
)
17209 decl_specifiers
->storage_class
= sc_extern
;
17210 decl
= start_decl (declarator
, decl_specifiers
,
17211 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17212 attributes
, prefix_attributes
, &pushed_scope
);
17213 cp_finalize_omp_declare_simd (parser
, decl
);
17214 /* Adjust location of decl if declarator->id_loc is more appropriate:
17215 set, and decl wasn't merged with another decl, in which case its
17216 location would be different from input_location, and more accurate. */
17218 && declarator
->id_loc
!= UNKNOWN_LOCATION
17219 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17220 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17223 /* Enter the SCOPE. That way unqualified names appearing in the
17224 initializer will be looked up in SCOPE. */
17225 pushed_scope
= push_scope (scope
);
17227 /* Perform deferred access control checks, now that we know in which
17228 SCOPE the declared entity resides. */
17229 if (!member_p
&& decl
)
17231 tree saved_current_function_decl
= NULL_TREE
;
17233 /* If the entity being declared is a function, pretend that we
17234 are in its scope. If it is a `friend', it may have access to
17235 things that would not otherwise be accessible. */
17236 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17238 saved_current_function_decl
= current_function_decl
;
17239 current_function_decl
= decl
;
17242 /* Perform access checks for template parameters. */
17243 cp_parser_perform_template_parameter_access_checks (checks
);
17245 /* Perform the access control checks for the declarator and the
17246 decl-specifiers. */
17247 perform_deferred_access_checks (tf_warning_or_error
);
17249 /* Restore the saved value. */
17250 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17251 current_function_decl
= saved_current_function_decl
;
17254 /* Parse the initializer. */
17255 initializer
= NULL_TREE
;
17256 is_direct_init
= false;
17257 is_non_constant_init
= true;
17258 if (is_initialized
)
17260 if (function_declarator_p (declarator
))
17262 if (initialization_kind
== CPP_EQ
)
17263 initializer
= cp_parser_pure_specifier (parser
);
17266 /* If the declaration was erroneous, we don't really
17267 know what the user intended, so just silently
17268 consume the initializer. */
17269 if (decl
!= error_mark_node
)
17270 error_at (tmp_init_loc
, "initializer provided for function");
17271 cp_parser_skip_to_closing_parenthesis (parser
,
17272 /*recovering=*/true,
17273 /*or_comma=*/false,
17274 /*consume_paren=*/true);
17279 /* We want to record the extra mangling scope for in-class
17280 initializers of class members and initializers of static data
17281 member templates. The former involves deferring
17282 parsing of the initializer until end of class as with default
17283 arguments. So right here we only handle the latter. */
17284 if (!member_p
&& processing_template_decl
)
17285 start_lambda_scope (decl
);
17286 initializer
= cp_parser_initializer (parser
,
17288 &is_non_constant_init
);
17289 if (!member_p
&& processing_template_decl
)
17290 finish_lambda_scope ();
17291 if (initializer
== error_mark_node
)
17292 cp_parser_skip_to_end_of_statement (parser
);
17296 /* The old parser allows attributes to appear after a parenthesized
17297 initializer. Mark Mitchell proposed removing this functionality
17298 on the GCC mailing lists on 2002-08-13. This parser accepts the
17299 attributes -- but ignores them. */
17300 if (cp_parser_allow_gnu_extensions_p (parser
)
17301 && initialization_kind
== CPP_OPEN_PAREN
)
17302 if (cp_parser_attributes_opt (parser
))
17303 warning (OPT_Wattributes
,
17304 "attributes after parenthesized initializer ignored");
17306 /* And now complain about a non-function implicit template. */
17307 if (bogus_implicit_tmpl
)
17308 error_at (DECL_SOURCE_LOCATION (decl
),
17309 "non-function %qD declared as implicit template", decl
);
17311 /* For an in-class declaration, use `grokfield' to create the
17317 pop_scope (pushed_scope
);
17318 pushed_scope
= NULL_TREE
;
17320 decl
= grokfield (declarator
, decl_specifiers
,
17321 initializer
, !is_non_constant_init
,
17322 /*asmspec=*/NULL_TREE
,
17323 chainon (attributes
, prefix_attributes
));
17324 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17325 cp_parser_save_default_args (parser
, decl
);
17326 cp_finalize_omp_declare_simd (parser
, decl
);
17329 /* Finish processing the declaration. But, skip member
17331 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17333 cp_finish_decl (decl
,
17334 initializer
, !is_non_constant_init
,
17336 /* If the initializer is in parentheses, then this is
17337 a direct-initialization, which means that an
17338 `explicit' constructor is OK. Otherwise, an
17339 `explicit' constructor cannot be used. */
17340 ((is_direct_init
|| !is_initialized
)
17341 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17343 else if ((cxx_dialect
!= cxx98
) && friend_p
17344 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17345 /* Core issue #226 (C++0x only): A default template-argument
17346 shall not be specified in a friend class template
17348 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17349 /*is_partial=*/false, /*is_friend_decl=*/1);
17351 if (!friend_p
&& pushed_scope
)
17352 pop_scope (pushed_scope
);
17354 if (function_declarator_p (declarator
)
17355 && parser
->fully_implicit_function_template_p
)
17358 decl
= finish_fully_implicit_template (parser
, decl
);
17360 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17366 /* Parse a declarator.
17370 ptr-operator declarator
17372 abstract-declarator:
17373 ptr-operator abstract-declarator [opt]
17374 direct-abstract-declarator
17379 attributes [opt] direct-declarator
17380 attributes [opt] ptr-operator declarator
17382 abstract-declarator:
17383 attributes [opt] ptr-operator abstract-declarator [opt]
17384 attributes [opt] direct-abstract-declarator
17386 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17387 detect constructor, destructor or conversion operators. It is set
17388 to -1 if the declarator is a name, and +1 if it is a
17389 function. Otherwise it is set to zero. Usually you just want to
17390 test for >0, but internally the negative value is used.
17392 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17393 a decl-specifier-seq unless it declares a constructor, destructor,
17394 or conversion. It might seem that we could check this condition in
17395 semantic analysis, rather than parsing, but that makes it difficult
17396 to handle something like `f()'. We want to notice that there are
17397 no decl-specifiers, and therefore realize that this is an
17398 expression, not a declaration.)
17400 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17401 the declarator is a direct-declarator of the form "(...)".
17403 MEMBER_P is true iff this declarator is a member-declarator.
17405 FRIEND_P is true iff this declarator is a friend. */
17407 static cp_declarator
*
17408 cp_parser_declarator (cp_parser
* parser
,
17409 cp_parser_declarator_kind dcl_kind
,
17410 int* ctor_dtor_or_conv_p
,
17411 bool* parenthesized_p
,
17412 bool member_p
, bool friend_p
)
17414 cp_declarator
*declarator
;
17415 enum tree_code code
;
17416 cp_cv_quals cv_quals
;
17418 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17420 /* Assume this is not a constructor, destructor, or type-conversion
17422 if (ctor_dtor_or_conv_p
)
17423 *ctor_dtor_or_conv_p
= 0;
17425 if (cp_parser_allow_gnu_extensions_p (parser
))
17426 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17428 /* Check for the ptr-operator production. */
17429 cp_parser_parse_tentatively (parser
);
17430 /* Parse the ptr-operator. */
17431 code
= cp_parser_ptr_operator (parser
,
17436 /* If that worked, then we have a ptr-operator. */
17437 if (cp_parser_parse_definitely (parser
))
17439 /* If a ptr-operator was found, then this declarator was not
17441 if (parenthesized_p
)
17442 *parenthesized_p
= true;
17443 /* The dependent declarator is optional if we are parsing an
17444 abstract-declarator. */
17445 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17446 cp_parser_parse_tentatively (parser
);
17448 /* Parse the dependent declarator. */
17449 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17450 /*ctor_dtor_or_conv_p=*/NULL
,
17451 /*parenthesized_p=*/NULL
,
17452 /*member_p=*/false,
17455 /* If we are parsing an abstract-declarator, we must handle the
17456 case where the dependent declarator is absent. */
17457 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17458 && !cp_parser_parse_definitely (parser
))
17461 declarator
= cp_parser_make_indirect_declarator
17462 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17464 /* Everything else is a direct-declarator. */
17467 if (parenthesized_p
)
17468 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17470 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17471 ctor_dtor_or_conv_p
,
17472 member_p
, friend_p
);
17475 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17476 declarator
->attributes
= gnu_attributes
;
17480 /* Parse a direct-declarator or direct-abstract-declarator.
17484 direct-declarator ( parameter-declaration-clause )
17485 cv-qualifier-seq [opt]
17486 ref-qualifier [opt]
17487 exception-specification [opt]
17488 direct-declarator [ constant-expression [opt] ]
17491 direct-abstract-declarator:
17492 direct-abstract-declarator [opt]
17493 ( parameter-declaration-clause )
17494 cv-qualifier-seq [opt]
17495 ref-qualifier [opt]
17496 exception-specification [opt]
17497 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17498 ( abstract-declarator )
17500 Returns a representation of the declarator. DCL_KIND is
17501 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17502 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17503 we are parsing a direct-declarator. It is
17504 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17505 of ambiguity we prefer an abstract declarator, as per
17506 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17507 as for cp_parser_declarator. */
17509 static cp_declarator
*
17510 cp_parser_direct_declarator (cp_parser
* parser
,
17511 cp_parser_declarator_kind dcl_kind
,
17512 int* ctor_dtor_or_conv_p
,
17513 bool member_p
, bool friend_p
)
17516 cp_declarator
*declarator
= NULL
;
17517 tree scope
= NULL_TREE
;
17518 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17519 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17521 tree pushed_scope
= NULL_TREE
;
17525 /* Peek at the next token. */
17526 token
= cp_lexer_peek_token (parser
->lexer
);
17527 if (token
->type
== CPP_OPEN_PAREN
)
17529 /* This is either a parameter-declaration-clause, or a
17530 parenthesized declarator. When we know we are parsing a
17531 named declarator, it must be a parenthesized declarator
17532 if FIRST is true. For instance, `(int)' is a
17533 parameter-declaration-clause, with an omitted
17534 direct-abstract-declarator. But `((*))', is a
17535 parenthesized abstract declarator. Finally, when T is a
17536 template parameter `(T)' is a
17537 parameter-declaration-clause, and not a parenthesized
17540 We first try and parse a parameter-declaration-clause,
17541 and then try a nested declarator (if FIRST is true).
17543 It is not an error for it not to be a
17544 parameter-declaration-clause, even when FIRST is
17550 The first is the declaration of a function while the
17551 second is the definition of a variable, including its
17554 Having seen only the parenthesis, we cannot know which of
17555 these two alternatives should be selected. Even more
17556 complex are examples like:
17561 The former is a function-declaration; the latter is a
17562 variable initialization.
17564 Thus again, we try a parameter-declaration-clause, and if
17565 that fails, we back out and return. */
17567 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17570 bool is_declarator
= false;
17572 /* In a member-declarator, the only valid interpretation
17573 of a parenthesis is the start of a
17574 parameter-declaration-clause. (It is invalid to
17575 initialize a static data member with a parenthesized
17576 initializer; only the "=" form of initialization is
17579 cp_parser_parse_tentatively (parser
);
17581 /* Consume the `('. */
17582 cp_lexer_consume_token (parser
->lexer
);
17585 /* If this is going to be an abstract declarator, we're
17586 in a declarator and we can't have default args. */
17587 parser
->default_arg_ok_p
= false;
17588 parser
->in_declarator_p
= true;
17591 begin_scope (sk_function_parms
, NULL_TREE
);
17593 /* Parse the parameter-declaration-clause. */
17594 params
= cp_parser_parameter_declaration_clause (parser
);
17596 /* Consume the `)'. */
17597 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17599 /* If all went well, parse the cv-qualifier-seq,
17600 ref-qualifier and the exception-specification. */
17601 if (member_p
|| cp_parser_parse_definitely (parser
))
17603 cp_cv_quals cv_quals
;
17604 cp_virt_specifiers virt_specifiers
;
17605 cp_ref_qualifier ref_qual
;
17606 tree exception_specification
;
17609 bool memfn
= (member_p
|| (pushed_scope
17610 && CLASS_TYPE_P (pushed_scope
)));
17612 is_declarator
= true;
17614 if (ctor_dtor_or_conv_p
)
17615 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17618 /* Parse the cv-qualifier-seq. */
17619 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17620 /* Parse the ref-qualifier. */
17621 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17622 /* And the exception-specification. */
17623 exception_specification
17624 = cp_parser_exception_specification_opt (parser
);
17626 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17628 /* In here, we handle cases where attribute is used after
17629 the function declaration. For example:
17630 void func (int x) __attribute__((vector(..))); */
17632 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17634 cp_parser_parse_tentatively (parser
);
17635 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17636 if (cp_lexer_next_token_is_not (parser
->lexer
,
17638 && cp_lexer_next_token_is_not (parser
->lexer
,
17640 cp_parser_abort_tentative_parse (parser
);
17641 else if (!cp_parser_parse_definitely (parser
))
17644 attrs
= chainon (attr
, attrs
);
17646 late_return
= (cp_parser_late_return_type_opt
17647 (parser
, declarator
,
17648 memfn
? cv_quals
: -1));
17651 /* Parse the virt-specifier-seq. */
17652 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17654 /* Create the function-declarator. */
17655 declarator
= make_call_declarator (declarator
,
17660 exception_specification
,
17662 declarator
->std_attributes
= attrs
;
17663 /* Any subsequent parameter lists are to do with
17664 return type, so are not those of the declared
17666 parser
->default_arg_ok_p
= false;
17669 /* Remove the function parms from scope. */
17670 pop_bindings_and_leave_scope ();
17673 /* Repeat the main loop. */
17677 /* If this is the first, we can try a parenthesized
17681 bool saved_in_type_id_in_expr_p
;
17683 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17684 parser
->in_declarator_p
= saved_in_declarator_p
;
17686 /* Consume the `('. */
17687 cp_lexer_consume_token (parser
->lexer
);
17688 /* Parse the nested declarator. */
17689 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17690 parser
->in_type_id_in_expr_p
= true;
17692 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17693 /*parenthesized_p=*/NULL
,
17694 member_p
, friend_p
);
17695 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17697 /* Expect a `)'. */
17698 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17699 declarator
= cp_error_declarator
;
17700 if (declarator
== cp_error_declarator
)
17703 goto handle_declarator
;
17705 /* Otherwise, we must be done. */
17709 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17710 && token
->type
== CPP_OPEN_SQUARE
17711 && !cp_next_tokens_can_be_attribute_p (parser
))
17713 /* Parse an array-declarator. */
17714 tree bounds
, attrs
;
17716 if (ctor_dtor_or_conv_p
)
17717 *ctor_dtor_or_conv_p
= 0;
17720 parser
->default_arg_ok_p
= false;
17721 parser
->in_declarator_p
= true;
17722 /* Consume the `['. */
17723 cp_lexer_consume_token (parser
->lexer
);
17724 /* Peek at the next token. */
17725 token
= cp_lexer_peek_token (parser
->lexer
);
17726 /* If the next token is `]', then there is no
17727 constant-expression. */
17728 if (token
->type
!= CPP_CLOSE_SQUARE
)
17730 bool non_constant_p
;
17732 = cp_parser_constant_expression (parser
,
17733 /*allow_non_constant=*/true,
17735 if (!non_constant_p
)
17737 else if (error_operand_p (bounds
))
17738 /* Already gave an error. */;
17739 else if (!parser
->in_function_body
17740 || current_binding_level
->kind
== sk_function_parms
)
17742 /* Normally, the array bound must be an integral constant
17743 expression. However, as an extension, we allow VLAs
17744 in function scopes as long as they aren't part of a
17745 parameter declaration. */
17746 cp_parser_error (parser
,
17747 "array bound is not an integer constant");
17748 bounds
= error_mark_node
;
17750 else if (processing_template_decl
17751 && !type_dependent_expression_p (bounds
))
17753 /* Remember this wasn't a constant-expression. */
17754 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17755 TREE_SIDE_EFFECTS (bounds
) = 1;
17759 bounds
= NULL_TREE
;
17760 /* Look for the closing `]'. */
17761 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17763 declarator
= cp_error_declarator
;
17767 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17768 declarator
= make_array_declarator (declarator
, bounds
);
17769 declarator
->std_attributes
= attrs
;
17771 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17774 tree qualifying_scope
;
17775 tree unqualified_name
;
17777 special_function_kind sfk
;
17779 bool pack_expansion_p
= false;
17780 cp_token
*declarator_id_start_token
;
17782 /* Parse a declarator-id */
17783 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17786 cp_parser_parse_tentatively (parser
);
17788 /* If we see an ellipsis, we should be looking at a
17790 if (token
->type
== CPP_ELLIPSIS
)
17792 /* Consume the `...' */
17793 cp_lexer_consume_token (parser
->lexer
);
17795 pack_expansion_p
= true;
17799 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17801 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17802 qualifying_scope
= parser
->scope
;
17807 if (!unqualified_name
&& pack_expansion_p
)
17809 /* Check whether an error occurred. */
17810 okay
= !cp_parser_error_occurred (parser
);
17812 /* We already consumed the ellipsis to mark a
17813 parameter pack, but we have no way to report it,
17814 so abort the tentative parse. We will be exiting
17815 immediately anyway. */
17816 cp_parser_abort_tentative_parse (parser
);
17819 okay
= cp_parser_parse_definitely (parser
);
17822 unqualified_name
= error_mark_node
;
17823 else if (unqualified_name
17824 && (qualifying_scope
17825 || (!identifier_p (unqualified_name
))))
17827 cp_parser_error (parser
, "expected unqualified-id");
17828 unqualified_name
= error_mark_node
;
17832 if (!unqualified_name
)
17834 if (unqualified_name
== error_mark_node
)
17836 declarator
= cp_error_declarator
;
17837 pack_expansion_p
= false;
17838 declarator
->parameter_pack_p
= false;
17842 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17844 if (qualifying_scope
&& at_namespace_scope_p ()
17845 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17847 /* In the declaration of a member of a template class
17848 outside of the class itself, the SCOPE will sometimes
17849 be a TYPENAME_TYPE. For example, given:
17851 template <typename T>
17852 int S<T>::R::i = 3;
17854 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17855 this context, we must resolve S<T>::R to an ordinary
17856 type, rather than a typename type.
17858 The reason we normally avoid resolving TYPENAME_TYPEs
17859 is that a specialization of `S' might render
17860 `S<T>::R' not a type. However, if `S' is
17861 specialized, then this `i' will not be used, so there
17862 is no harm in resolving the types here. */
17865 /* Resolve the TYPENAME_TYPE. */
17866 type
= resolve_typename_type (qualifying_scope
,
17867 /*only_current_p=*/false);
17868 /* If that failed, the declarator is invalid. */
17869 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17871 if (typedef_variant_p (type
))
17872 error_at (declarator_id_start_token
->location
,
17873 "cannot define member of dependent typedef "
17876 error_at (declarator_id_start_token
->location
,
17877 "%<%T::%E%> is not a type",
17878 TYPE_CONTEXT (qualifying_scope
),
17879 TYPE_IDENTIFIER (qualifying_scope
));
17881 qualifying_scope
= type
;
17886 if (unqualified_name
)
17890 if (qualifying_scope
17891 && CLASS_TYPE_P (qualifying_scope
))
17892 class_type
= qualifying_scope
;
17894 class_type
= current_class_type
;
17896 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17898 tree name_type
= TREE_TYPE (unqualified_name
);
17899 if (class_type
&& same_type_p (name_type
, class_type
))
17901 if (qualifying_scope
17902 && CLASSTYPE_USE_TEMPLATE (name_type
))
17904 error_at (declarator_id_start_token
->location
,
17905 "invalid use of constructor as a template");
17906 inform (declarator_id_start_token
->location
,
17907 "use %<%T::%D%> instead of %<%T::%D%> to "
17908 "name the constructor in a qualified name",
17910 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17911 class_type
, name_type
);
17912 declarator
= cp_error_declarator
;
17916 unqualified_name
= constructor_name (class_type
);
17920 /* We do not attempt to print the declarator
17921 here because we do not have enough
17922 information about its original syntactic
17924 cp_parser_error (parser
, "invalid declarator");
17925 declarator
= cp_error_declarator
;
17932 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17933 sfk
= sfk_destructor
;
17934 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17935 sfk
= sfk_conversion
;
17936 else if (/* There's no way to declare a constructor
17937 for an anonymous type, even if the type
17938 got a name for linkage purposes. */
17939 !TYPE_WAS_ANONYMOUS (class_type
)
17940 /* Handle correctly (c++/19200):
17954 friend void N::S();
17957 && class_type
!= qualifying_scope
)
17958 && constructor_name_p (unqualified_name
,
17961 unqualified_name
= constructor_name (class_type
);
17962 sfk
= sfk_constructor
;
17964 else if (is_overloaded_fn (unqualified_name
)
17965 && DECL_CONSTRUCTOR_P (get_first_fn
17966 (unqualified_name
)))
17967 sfk
= sfk_constructor
;
17969 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17970 *ctor_dtor_or_conv_p
= -1;
17973 declarator
= make_id_declarator (qualifying_scope
,
17976 declarator
->std_attributes
= attrs
;
17977 declarator
->id_loc
= token
->location
;
17978 declarator
->parameter_pack_p
= pack_expansion_p
;
17980 if (pack_expansion_p
)
17981 maybe_warn_variadic_templates ();
17984 handle_declarator
:;
17985 scope
= get_scope_of_declarator (declarator
);
17988 /* Any names that appear after the declarator-id for a
17989 member are looked up in the containing scope. */
17990 if (at_function_scope_p ())
17992 /* But declarations with qualified-ids can't appear in a
17994 cp_parser_error (parser
, "qualified-id in declaration");
17995 declarator
= cp_error_declarator
;
17998 pushed_scope
= push_scope (scope
);
18000 parser
->in_declarator_p
= true;
18001 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
18002 || (declarator
&& declarator
->kind
== cdk_id
))
18003 /* Default args are only allowed on function
18005 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18007 parser
->default_arg_ok_p
= false;
18016 /* For an abstract declarator, we might wind up with nothing at this
18017 point. That's an error; the declarator is not optional. */
18019 cp_parser_error (parser
, "expected declarator");
18021 /* If we entered a scope, we must exit it now. */
18023 pop_scope (pushed_scope
);
18025 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18026 parser
->in_declarator_p
= saved_in_declarator_p
;
18031 /* Parse a ptr-operator.
18034 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18035 * cv-qualifier-seq [opt]
18037 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18038 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18043 & cv-qualifier-seq [opt]
18045 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18046 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18047 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18048 filled in with the TYPE containing the member. *CV_QUALS is
18049 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18050 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18051 Note that the tree codes returned by this function have nothing
18052 to do with the types of trees that will be eventually be created
18053 to represent the pointer or reference type being parsed. They are
18054 just constants with suggestive names. */
18055 static enum tree_code
18056 cp_parser_ptr_operator (cp_parser
* parser
,
18058 cp_cv_quals
*cv_quals
,
18061 enum tree_code code
= ERROR_MARK
;
18063 tree attrs
= NULL_TREE
;
18065 /* Assume that it's not a pointer-to-member. */
18067 /* And that there are no cv-qualifiers. */
18068 *cv_quals
= TYPE_UNQUALIFIED
;
18070 /* Peek at the next token. */
18071 token
= cp_lexer_peek_token (parser
->lexer
);
18073 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18074 if (token
->type
== CPP_MULT
)
18075 code
= INDIRECT_REF
;
18076 else if (token
->type
== CPP_AND
)
18078 else if ((cxx_dialect
!= cxx98
) &&
18079 token
->type
== CPP_AND_AND
) /* C++0x only */
18080 code
= NON_LVALUE_EXPR
;
18082 if (code
!= ERROR_MARK
)
18084 /* Consume the `*', `&' or `&&'. */
18085 cp_lexer_consume_token (parser
->lexer
);
18087 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18088 `&', if we are allowing GNU extensions. (The only qualifier
18089 that can legally appear after `&' is `restrict', but that is
18090 enforced during semantic analysis. */
18091 if (code
== INDIRECT_REF
18092 || cp_parser_allow_gnu_extensions_p (parser
))
18093 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18095 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18096 if (attributes
!= NULL
)
18097 *attributes
= attrs
;
18101 /* Try the pointer-to-member case. */
18102 cp_parser_parse_tentatively (parser
);
18103 /* Look for the optional `::' operator. */
18104 cp_parser_global_scope_opt (parser
,
18105 /*current_scope_valid_p=*/false);
18106 /* Look for the nested-name specifier. */
18107 token
= cp_lexer_peek_token (parser
->lexer
);
18108 cp_parser_nested_name_specifier (parser
,
18109 /*typename_keyword_p=*/false,
18110 /*check_dependency_p=*/true,
18112 /*is_declaration=*/false);
18113 /* If we found it, and the next token is a `*', then we are
18114 indeed looking at a pointer-to-member operator. */
18115 if (!cp_parser_error_occurred (parser
)
18116 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
18118 /* Indicate that the `*' operator was used. */
18119 code
= INDIRECT_REF
;
18121 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18122 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
18123 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
18124 error_at (token
->location
, "cannot form pointer to member of "
18125 "non-class %q#T", parser
->scope
);
18128 /* The type of which the member is a member is given by the
18130 *type
= parser
->scope
;
18131 /* The next name will not be qualified. */
18132 parser
->scope
= NULL_TREE
;
18133 parser
->qualifying_scope
= NULL_TREE
;
18134 parser
->object_scope
= NULL_TREE
;
18135 /* Look for optional c++11 attributes. */
18136 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18137 if (attributes
!= NULL
)
18138 *attributes
= attrs
;
18139 /* Look for the optional cv-qualifier-seq. */
18140 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18143 /* If that didn't work we don't have a ptr-operator. */
18144 if (!cp_parser_parse_definitely (parser
))
18145 cp_parser_error (parser
, "expected ptr-operator");
18151 /* Parse an (optional) cv-qualifier-seq.
18154 cv-qualifier cv-qualifier-seq [opt]
18165 Returns a bitmask representing the cv-qualifiers. */
18168 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18170 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18175 cp_cv_quals cv_qualifier
;
18177 /* Peek at the next token. */
18178 token
= cp_lexer_peek_token (parser
->lexer
);
18179 /* See if it's a cv-qualifier. */
18180 switch (token
->keyword
)
18183 cv_qualifier
= TYPE_QUAL_CONST
;
18187 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18191 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18195 cv_qualifier
= TYPE_UNQUALIFIED
;
18202 if (cv_quals
& cv_qualifier
)
18204 error_at (token
->location
, "duplicate cv-qualifier");
18205 cp_lexer_purge_token (parser
->lexer
);
18209 cp_lexer_consume_token (parser
->lexer
);
18210 cv_quals
|= cv_qualifier
;
18217 /* Parse an (optional) ref-qualifier
18223 Returns cp_ref_qualifier representing ref-qualifier. */
18225 static cp_ref_qualifier
18226 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18228 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18230 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18231 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18236 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18237 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18239 switch (token
->type
)
18242 curr_ref_qual
= REF_QUAL_LVALUE
;
18246 curr_ref_qual
= REF_QUAL_RVALUE
;
18250 curr_ref_qual
= REF_QUAL_NONE
;
18254 if (!curr_ref_qual
)
18258 error_at (token
->location
, "multiple ref-qualifiers");
18259 cp_lexer_purge_token (parser
->lexer
);
18263 ref_qual
= curr_ref_qual
;
18264 cp_lexer_consume_token (parser
->lexer
);
18271 /* Parse an (optional) virt-specifier-seq.
18273 virt-specifier-seq:
18274 virt-specifier virt-specifier-seq [opt]
18280 Returns a bitmask representing the virt-specifiers. */
18282 static cp_virt_specifiers
18283 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18285 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18290 cp_virt_specifiers virt_specifier
;
18292 /* Peek at the next token. */
18293 token
= cp_lexer_peek_token (parser
->lexer
);
18294 /* See if it's a virt-specifier-qualifier. */
18295 if (token
->type
!= CPP_NAME
)
18297 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18299 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18300 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18302 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18304 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18305 virt_specifier
= VIRT_SPEC_FINAL
;
18307 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18309 virt_specifier
= VIRT_SPEC_FINAL
;
18314 if (virt_specifiers
& virt_specifier
)
18316 error_at (token
->location
, "duplicate virt-specifier");
18317 cp_lexer_purge_token (parser
->lexer
);
18321 cp_lexer_consume_token (parser
->lexer
);
18322 virt_specifiers
|= virt_specifier
;
18325 return virt_specifiers
;
18328 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18329 is in scope even though it isn't real. */
18332 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18336 if (current_class_ptr
)
18338 /* We don't clear this between NSDMIs. Is it already what we want? */
18339 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18340 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18341 && cp_type_quals (type
) == quals
)
18345 this_parm
= build_this_parm (ctype
, quals
);
18346 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18347 current_class_ptr
= NULL_TREE
;
18349 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18350 current_class_ptr
= this_parm
;
18353 /* Return true iff our current scope is a non-static data member
18357 parsing_nsdmi (void)
18359 /* We recognize NSDMI context by the context-less 'this' pointer set up
18360 by the function above. */
18361 if (current_class_ptr
18362 && TREE_CODE (current_class_ptr
) == PARM_DECL
18363 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18368 /* Parse a late-specified return type, if any. This is not a separate
18369 non-terminal, but part of a function declarator, which looks like
18371 -> trailing-type-specifier-seq abstract-declarator(opt)
18373 Returns the type indicated by the type-id.
18375 In addition to this, parse any queued up omp declare simd
18376 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18378 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18382 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18386 tree type
= NULL_TREE
;
18387 bool declare_simd_p
= (parser
->omp_declare_simd
18389 && declarator
->kind
== cdk_id
);
18391 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18392 && declarator
&& declarator
->kind
== cdk_id
);
18394 /* Peek at the next token. */
18395 token
= cp_lexer_peek_token (parser
->lexer
);
18396 /* A late-specified return type is indicated by an initial '->'. */
18397 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18400 tree save_ccp
= current_class_ptr
;
18401 tree save_ccr
= current_class_ref
;
18404 /* DR 1207: 'this' is in scope in the trailing return type. */
18405 inject_this_parameter (current_class_type
, quals
);
18408 if (token
->type
== CPP_DEREF
)
18410 /* Consume the ->. */
18411 cp_lexer_consume_token (parser
->lexer
);
18413 type
= cp_parser_trailing_type_id (parser
);
18416 if (cilk_simd_fn_vector_p
)
18417 declarator
->std_attributes
18418 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18419 declarator
->std_attributes
);
18420 if (declare_simd_p
)
18421 declarator
->std_attributes
18422 = cp_parser_late_parsing_omp_declare_simd (parser
,
18423 declarator
->std_attributes
);
18427 current_class_ptr
= save_ccp
;
18428 current_class_ref
= save_ccr
;
18434 /* Parse a declarator-id.
18438 :: [opt] nested-name-specifier [opt] type-name
18440 In the `id-expression' case, the value returned is as for
18441 cp_parser_id_expression if the id-expression was an unqualified-id.
18442 If the id-expression was a qualified-id, then a SCOPE_REF is
18443 returned. The first operand is the scope (either a NAMESPACE_DECL
18444 or TREE_TYPE), but the second is still just a representation of an
18448 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18451 /* The expression must be an id-expression. Assume that qualified
18452 names are the names of types so that:
18455 int S<T>::R::i = 3;
18457 will work; we must treat `S<T>::R' as the name of a type.
18458 Similarly, assume that qualified names are templates, where
18462 int S<T>::R<T>::i = 3;
18465 id
= cp_parser_id_expression (parser
,
18466 /*template_keyword_p=*/false,
18467 /*check_dependency_p=*/false,
18468 /*template_p=*/NULL
,
18469 /*declarator_p=*/true,
18471 if (id
&& BASELINK_P (id
))
18472 id
= BASELINK_FUNCTIONS (id
);
18476 /* Parse a type-id.
18479 type-specifier-seq abstract-declarator [opt]
18481 Returns the TYPE specified. */
18484 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18485 bool is_trailing_return
)
18487 cp_decl_specifier_seq type_specifier_seq
;
18488 cp_declarator
*abstract_declarator
;
18490 /* Parse the type-specifier-seq. */
18491 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18492 is_trailing_return
,
18493 &type_specifier_seq
);
18494 if (type_specifier_seq
.type
== error_mark_node
)
18495 return error_mark_node
;
18497 /* There might or might not be an abstract declarator. */
18498 cp_parser_parse_tentatively (parser
);
18499 /* Look for the declarator. */
18500 abstract_declarator
18501 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18502 /*parenthesized_p=*/NULL
,
18503 /*member_p=*/false,
18504 /*friend_p=*/false);
18505 /* Check to see if there really was a declarator. */
18506 if (!cp_parser_parse_definitely (parser
))
18507 abstract_declarator
= NULL
;
18509 if (type_specifier_seq
.type
18510 /* None of the valid uses of 'auto' in C++14 involve the type-id
18511 nonterminal, but it is valid in a trailing-return-type. */
18512 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18513 && type_uses_auto (type_specifier_seq
.type
))
18515 /* A type-id with type 'auto' is only ok if the abstract declarator
18516 is a function declarator with a late-specified return type. */
18517 if (abstract_declarator
18518 && abstract_declarator
->kind
== cdk_function
18519 && abstract_declarator
->u
.function
.late_return_type
)
18523 error ("invalid use of %<auto%>");
18524 return error_mark_node
;
18528 return groktypename (&type_specifier_seq
, abstract_declarator
,
18532 static tree
cp_parser_type_id (cp_parser
*parser
)
18534 return cp_parser_type_id_1 (parser
, false, false);
18537 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18540 const char *saved_message
= parser
->type_definition_forbidden_message
;
18541 parser
->type_definition_forbidden_message
18542 = G_("types may not be defined in template arguments");
18543 r
= cp_parser_type_id_1 (parser
, true, false);
18544 parser
->type_definition_forbidden_message
= saved_message
;
18545 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18547 error ("invalid use of %<auto%> in template argument");
18548 r
= error_mark_node
;
18553 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18555 return cp_parser_type_id_1 (parser
, false, true);
18558 /* Parse a type-specifier-seq.
18560 type-specifier-seq:
18561 type-specifier type-specifier-seq [opt]
18565 type-specifier-seq:
18566 attributes type-specifier-seq [opt]
18568 If IS_DECLARATION is true, we are at the start of a "condition" or
18569 exception-declaration, so we might be followed by a declarator-id.
18571 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18572 i.e. we've just seen "->".
18574 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18577 cp_parser_type_specifier_seq (cp_parser
* parser
,
18578 bool is_declaration
,
18579 bool is_trailing_return
,
18580 cp_decl_specifier_seq
*type_specifier_seq
)
18582 bool seen_type_specifier
= false;
18583 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18584 cp_token
*start_token
= NULL
;
18586 /* Clear the TYPE_SPECIFIER_SEQ. */
18587 clear_decl_specs (type_specifier_seq
);
18589 /* In the context of a trailing return type, enum E { } is an
18590 elaborated-type-specifier followed by a function-body, not an
18592 if (is_trailing_return
)
18593 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18595 /* Parse the type-specifiers and attributes. */
18598 tree type_specifier
;
18599 bool is_cv_qualifier
;
18601 /* Check for attributes first. */
18602 if (cp_next_tokens_can_be_attribute_p (parser
))
18604 type_specifier_seq
->attributes
=
18605 chainon (type_specifier_seq
->attributes
,
18606 cp_parser_attributes_opt (parser
));
18610 /* record the token of the beginning of the type specifier seq,
18611 for error reporting purposes*/
18613 start_token
= cp_lexer_peek_token (parser
->lexer
);
18615 /* Look for the type-specifier. */
18616 type_specifier
= cp_parser_type_specifier (parser
,
18618 type_specifier_seq
,
18619 /*is_declaration=*/false,
18622 if (!type_specifier
)
18624 /* If the first type-specifier could not be found, this is not a
18625 type-specifier-seq at all. */
18626 if (!seen_type_specifier
)
18628 /* Set in_declarator_p to avoid skipping to the semicolon. */
18629 int in_decl
= parser
->in_declarator_p
;
18630 parser
->in_declarator_p
= true;
18632 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18633 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18634 cp_parser_error (parser
, "expected type-specifier");
18636 parser
->in_declarator_p
= in_decl
;
18638 type_specifier_seq
->type
= error_mark_node
;
18641 /* If subsequent type-specifiers could not be found, the
18642 type-specifier-seq is complete. */
18646 seen_type_specifier
= true;
18647 /* The standard says that a condition can be:
18649 type-specifier-seq declarator = assignment-expression
18656 we should treat the "S" as a declarator, not as a
18657 type-specifier. The standard doesn't say that explicitly for
18658 type-specifier-seq, but it does say that for
18659 decl-specifier-seq in an ordinary declaration. Perhaps it
18660 would be clearer just to allow a decl-specifier-seq here, and
18661 then add a semantic restriction that if any decl-specifiers
18662 that are not type-specifiers appear, the program is invalid. */
18663 if (is_declaration
&& !is_cv_qualifier
)
18664 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18668 /* Return whether the function currently being declared has an associated
18669 template parameter list. */
18672 function_being_declared_is_template_p (cp_parser
* parser
)
18674 if (!current_template_parms
|| processing_template_parmlist
)
18677 if (parser
->implicit_template_scope
)
18680 if (at_class_scope_p ()
18681 && TYPE_BEING_DEFINED (current_class_type
))
18682 return parser
->num_template_parameter_lists
!= 0;
18684 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18685 (current_class_type
));
18688 /* Parse a parameter-declaration-clause.
18690 parameter-declaration-clause:
18691 parameter-declaration-list [opt] ... [opt]
18692 parameter-declaration-list , ...
18694 Returns a representation for the parameter declarations. A return
18695 value of NULL indicates a parameter-declaration-clause consisting
18696 only of an ellipsis. */
18699 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18708 int auto_is_implicit_function_template_parm_p
;
18710 parser
->auto_is_implicit_function_template_parm_p
18711 = auto_is_implicit_function_template_parm_p
;
18713 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18717 if (!processing_specialization
18718 && !processing_template_parmlist
18719 && !processing_explicit_instantiation
)
18720 if (!current_function_decl
18721 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18722 parser
->auto_is_implicit_function_template_parm_p
= true;
18724 /* Peek at the next token. */
18725 token
= cp_lexer_peek_token (parser
->lexer
);
18726 /* Check for trivial parameter-declaration-clauses. */
18727 if (token
->type
== CPP_ELLIPSIS
)
18729 /* Consume the `...' token. */
18730 cp_lexer_consume_token (parser
->lexer
);
18733 else if (token
->type
== CPP_CLOSE_PAREN
)
18734 /* There are no parameters. */
18736 #ifndef NO_IMPLICIT_EXTERN_C
18737 if (in_system_header_at (input_location
)
18738 && current_class_type
== NULL
18739 && current_lang_name
== lang_name_c
)
18743 return void_list_node
;
18745 /* Check for `(void)', too, which is a special case. */
18746 else if (token
->keyword
== RID_VOID
18747 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18748 == CPP_CLOSE_PAREN
))
18750 /* Consume the `void' token. */
18751 cp_lexer_consume_token (parser
->lexer
);
18752 /* There are no parameters. */
18753 return void_list_node
;
18756 /* Parse the parameter-declaration-list. */
18757 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18758 /* If a parse error occurred while parsing the
18759 parameter-declaration-list, then the entire
18760 parameter-declaration-clause is erroneous. */
18764 /* Peek at the next token. */
18765 token
= cp_lexer_peek_token (parser
->lexer
);
18766 /* If it's a `,', the clause should terminate with an ellipsis. */
18767 if (token
->type
== CPP_COMMA
)
18769 /* Consume the `,'. */
18770 cp_lexer_consume_token (parser
->lexer
);
18771 /* Expect an ellipsis. */
18773 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18775 /* It might also be `...' if the optional trailing `,' was
18777 else if (token
->type
== CPP_ELLIPSIS
)
18779 /* Consume the `...' token. */
18780 cp_lexer_consume_token (parser
->lexer
);
18781 /* And remember that we saw it. */
18785 ellipsis_p
= false;
18787 /* Finish the parameter list. */
18789 parameters
= chainon (parameters
, void_list_node
);
18794 /* Parse a parameter-declaration-list.
18796 parameter-declaration-list:
18797 parameter-declaration
18798 parameter-declaration-list , parameter-declaration
18800 Returns a representation of the parameter-declaration-list, as for
18801 cp_parser_parameter_declaration_clause. However, the
18802 `void_list_node' is never appended to the list. Upon return,
18803 *IS_ERROR will be true iff an error occurred. */
18806 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18808 tree parameters
= NULL_TREE
;
18809 tree
*tail
= ¶meters
;
18810 bool saved_in_unbraced_linkage_specification_p
;
18813 /* Assume all will go well. */
18815 /* The special considerations that apply to a function within an
18816 unbraced linkage specifications do not apply to the parameters
18817 to the function. */
18818 saved_in_unbraced_linkage_specification_p
18819 = parser
->in_unbraced_linkage_specification_p
;
18820 parser
->in_unbraced_linkage_specification_p
= false;
18822 /* Look for more parameters. */
18825 cp_parameter_declarator
*parameter
;
18826 tree decl
= error_mark_node
;
18827 bool parenthesized_p
= false;
18828 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18829 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18830 (current_template_parms
)) : 0);
18832 /* Parse the parameter. */
18834 = cp_parser_parameter_declaration (parser
,
18835 /*template_parm_p=*/false,
18838 /* We don't know yet if the enclosing context is deprecated, so wait
18839 and warn in grokparms if appropriate. */
18840 deprecated_state
= DEPRECATED_SUPPRESS
;
18844 /* If a function parameter pack was specified and an implicit template
18845 parameter was introduced during cp_parser_parameter_declaration,
18846 change any implicit parameters introduced into packs. */
18847 if (parser
->implicit_template_parms
18848 && parameter
->declarator
18849 && parameter
->declarator
->parameter_pack_p
)
18851 int latest_template_parm_idx
= TREE_VEC_LENGTH
18852 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18854 if (latest_template_parm_idx
!= template_parm_idx
)
18855 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18856 (parameter
->decl_specifiers
.type
,
18857 template_parm_idx
, latest_template_parm_idx
);
18860 decl
= grokdeclarator (parameter
->declarator
,
18861 ¶meter
->decl_specifiers
,
18863 parameter
->default_argument
!= NULL_TREE
,
18864 ¶meter
->decl_specifiers
.attributes
);
18867 deprecated_state
= DEPRECATED_NORMAL
;
18869 /* If a parse error occurred parsing the parameter declaration,
18870 then the entire parameter-declaration-list is erroneous. */
18871 if (decl
== error_mark_node
)
18874 parameters
= error_mark_node
;
18878 if (parameter
->decl_specifiers
.attributes
)
18879 cplus_decl_attributes (&decl
,
18880 parameter
->decl_specifiers
.attributes
,
18882 if (DECL_NAME (decl
))
18883 decl
= pushdecl (decl
);
18885 if (decl
!= error_mark_node
)
18887 retrofit_lang_decl (decl
);
18888 DECL_PARM_INDEX (decl
) = ++index
;
18889 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18892 /* Add the new parameter to the list. */
18893 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18894 tail
= &TREE_CHAIN (*tail
);
18896 /* Peek at the next token. */
18897 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18898 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18899 /* These are for Objective-C++ */
18900 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18901 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18902 /* The parameter-declaration-list is complete. */
18904 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18908 /* Peek at the next token. */
18909 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18910 /* If it's an ellipsis, then the list is complete. */
18911 if (token
->type
== CPP_ELLIPSIS
)
18913 /* Otherwise, there must be more parameters. Consume the
18915 cp_lexer_consume_token (parser
->lexer
);
18916 /* When parsing something like:
18918 int i(float f, double d)
18920 we can tell after seeing the declaration for "f" that we
18921 are not looking at an initialization of a variable "i",
18922 but rather at the declaration of a function "i".
18924 Due to the fact that the parsing of template arguments
18925 (as specified to a template-id) requires backtracking we
18926 cannot use this technique when inside a template argument
18928 if (!parser
->in_template_argument_list_p
18929 && !parser
->in_type_id_in_expr_p
18930 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18931 /* However, a parameter-declaration of the form
18932 "float(f)" (which is a valid declaration of a
18933 parameter "f") can also be interpreted as an
18934 expression (the conversion of "f" to "float"). */
18935 && !parenthesized_p
)
18936 cp_parser_commit_to_tentative_parse (parser
);
18940 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18941 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18942 cp_parser_skip_to_closing_parenthesis (parser
,
18943 /*recovering=*/true,
18944 /*or_comma=*/false,
18945 /*consume_paren=*/false);
18950 parser
->in_unbraced_linkage_specification_p
18951 = saved_in_unbraced_linkage_specification_p
;
18953 /* Reset implicit_template_scope if we are about to leave the function
18954 parameter list that introduced it. Note that for out-of-line member
18955 definitions, there will be one or more class scopes before we get to
18956 the template parameter scope. */
18958 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18959 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18961 while (maybe_its
->kind
== sk_class
)
18962 maybe_its
= maybe_its
->level_chain
;
18963 if (maybe_its
== its
)
18965 parser
->implicit_template_parms
= 0;
18966 parser
->implicit_template_scope
= 0;
18973 /* Parse a parameter declaration.
18975 parameter-declaration:
18976 decl-specifier-seq ... [opt] declarator
18977 decl-specifier-seq declarator = assignment-expression
18978 decl-specifier-seq ... [opt] abstract-declarator [opt]
18979 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18981 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18982 declares a template parameter. (In that case, a non-nested `>'
18983 token encountered during the parsing of the assignment-expression
18984 is not interpreted as a greater-than operator.)
18986 Returns a representation of the parameter, or NULL if an error
18987 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18988 true iff the declarator is of the form "(p)". */
18990 static cp_parameter_declarator
*
18991 cp_parser_parameter_declaration (cp_parser
*parser
,
18992 bool template_parm_p
,
18993 bool *parenthesized_p
)
18995 int declares_class_or_enum
;
18996 cp_decl_specifier_seq decl_specifiers
;
18997 cp_declarator
*declarator
;
18998 tree default_argument
;
18999 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
19000 const char *saved_message
;
19001 bool template_parameter_pack_p
= false;
19003 /* In a template parameter, `>' is not an operator.
19007 When parsing a default template-argument for a non-type
19008 template-parameter, the first non-nested `>' is taken as the end
19009 of the template parameter-list rather than a greater-than
19012 /* Type definitions may not appear in parameter types. */
19013 saved_message
= parser
->type_definition_forbidden_message
;
19014 parser
->type_definition_forbidden_message
19015 = G_("types may not be defined in parameter types");
19017 /* Parse the declaration-specifiers. */
19018 cp_parser_decl_specifier_seq (parser
,
19019 CP_PARSER_FLAGS_NONE
,
19021 &declares_class_or_enum
);
19023 /* Complain about missing 'typename' or other invalid type names. */
19024 if (!decl_specifiers
.any_type_specifiers_p
19025 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
19026 decl_specifiers
.type
= error_mark_node
;
19028 /* If an error occurred, there's no reason to attempt to parse the
19029 rest of the declaration. */
19030 if (cp_parser_error_occurred (parser
))
19032 parser
->type_definition_forbidden_message
= saved_message
;
19036 /* Peek at the next token. */
19037 token
= cp_lexer_peek_token (parser
->lexer
);
19039 /* If the next token is a `)', `,', `=', `>', or `...', then there
19040 is no declarator. However, when variadic templates are enabled,
19041 there may be a declarator following `...'. */
19042 if (token
->type
== CPP_CLOSE_PAREN
19043 || token
->type
== CPP_COMMA
19044 || token
->type
== CPP_EQ
19045 || token
->type
== CPP_GREATER
)
19048 if (parenthesized_p
)
19049 *parenthesized_p
= false;
19051 /* Otherwise, there should be a declarator. */
19054 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19055 parser
->default_arg_ok_p
= false;
19057 /* After seeing a decl-specifier-seq, if the next token is not a
19058 "(", there is no possibility that the code is a valid
19059 expression. Therefore, if parsing tentatively, we commit at
19061 if (!parser
->in_template_argument_list_p
19062 /* In an expression context, having seen:
19066 we cannot be sure whether we are looking at a
19067 function-type (taking a "char" as a parameter) or a cast
19068 of some object of type "char" to "int". */
19069 && !parser
->in_type_id_in_expr_p
19070 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19071 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19072 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
19073 cp_parser_commit_to_tentative_parse (parser
);
19074 /* Parse the declarator. */
19075 declarator_token_start
= token
;
19076 declarator
= cp_parser_declarator (parser
,
19077 CP_PARSER_DECLARATOR_EITHER
,
19078 /*ctor_dtor_or_conv_p=*/NULL
,
19080 /*member_p=*/false,
19081 /*friend_p=*/false);
19082 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19083 /* After the declarator, allow more attributes. */
19084 decl_specifiers
.attributes
19085 = chainon (decl_specifiers
.attributes
,
19086 cp_parser_attributes_opt (parser
));
19088 /* If the declarator is a template parameter pack, remember that and
19089 clear the flag in the declarator itself so we don't get errors
19090 from grokdeclarator. */
19091 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
19093 declarator
->parameter_pack_p
= false;
19094 template_parameter_pack_p
= true;
19098 /* If the next token is an ellipsis, and we have not seen a
19099 declarator name, and the type of the declarator contains parameter
19100 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19101 a parameter pack expansion expression. Otherwise, leave the
19102 ellipsis for a C-style variadic function. */
19103 token
= cp_lexer_peek_token (parser
->lexer
);
19104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19106 tree type
= decl_specifiers
.type
;
19108 if (type
&& DECL_P (type
))
19109 type
= TREE_TYPE (type
);
19112 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
19113 && declarator_can_be_parameter_pack (declarator
)
19114 && (template_parm_p
|| uses_parameter_packs (type
)))
19116 /* Consume the `...'. */
19117 cp_lexer_consume_token (parser
->lexer
);
19118 maybe_warn_variadic_templates ();
19120 /* Build a pack expansion type */
19121 if (template_parm_p
)
19122 template_parameter_pack_p
= true;
19123 else if (declarator
)
19124 declarator
->parameter_pack_p
= true;
19126 decl_specifiers
.type
= make_pack_expansion (type
);
19130 /* The restriction on defining new types applies only to the type
19131 of the parameter, not to the default argument. */
19132 parser
->type_definition_forbidden_message
= saved_message
;
19134 /* If the next token is `=', then process a default argument. */
19135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19137 token
= cp_lexer_peek_token (parser
->lexer
);
19138 /* If we are defining a class, then the tokens that make up the
19139 default argument must be saved and processed later. */
19140 if (!template_parm_p
&& at_class_scope_p ()
19141 && TYPE_BEING_DEFINED (current_class_type
)
19142 && !LAMBDA_TYPE_P (current_class_type
))
19143 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
19144 /* Outside of a class definition, we can just parse the
19145 assignment-expression. */
19148 = cp_parser_default_argument (parser
, template_parm_p
);
19150 if (!parser
->default_arg_ok_p
)
19152 permerror (token
->location
,
19153 "default arguments are only "
19154 "permitted for function parameters");
19156 else if ((declarator
&& declarator
->parameter_pack_p
)
19157 || template_parameter_pack_p
19158 || (decl_specifiers
.type
19159 && PACK_EXPANSION_P (decl_specifiers
.type
)))
19161 /* Find the name of the parameter pack. */
19162 cp_declarator
*id_declarator
= declarator
;
19163 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
19164 id_declarator
= id_declarator
->declarator
;
19166 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19167 error_at (declarator_token_start
->location
,
19169 ? G_("template parameter pack %qD "
19170 "cannot have a default argument")
19171 : G_("parameter pack %qD cannot have "
19172 "a default argument"),
19173 id_declarator
->u
.id
.unqualified_name
);
19175 error_at (declarator_token_start
->location
,
19177 ? G_("template parameter pack cannot have "
19178 "a default argument")
19179 : G_("parameter pack cannot have a "
19180 "default argument"));
19182 default_argument
= NULL_TREE
;
19186 default_argument
= NULL_TREE
;
19188 return make_parameter_declarator (&decl_specifiers
,
19191 template_parameter_pack_p
);
19194 /* Parse a default argument and return it.
19196 TEMPLATE_PARM_P is true if this is a default argument for a
19197 non-type template parameter. */
19199 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19201 tree default_argument
= NULL_TREE
;
19202 bool saved_greater_than_is_operator_p
;
19203 bool saved_local_variables_forbidden_p
;
19204 bool non_constant_p
, is_direct_init
;
19206 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19208 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19209 parser
->greater_than_is_operator_p
= !template_parm_p
;
19210 /* Local variable names (and the `this' keyword) may not
19211 appear in a default argument. */
19212 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19213 parser
->local_variables_forbidden_p
= true;
19214 /* Parse the assignment-expression. */
19215 if (template_parm_p
)
19216 push_deferring_access_checks (dk_no_deferred
);
19217 tree saved_class_ptr
= NULL_TREE
;
19218 tree saved_class_ref
= NULL_TREE
;
19219 /* The "this" pointer is not valid in a default argument. */
19222 saved_class_ptr
= current_class_ptr
;
19223 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19224 saved_class_ref
= current_class_ref
;
19225 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19228 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19229 /* Restore the "this" pointer. */
19232 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19233 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19235 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19236 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19237 if (template_parm_p
)
19238 pop_deferring_access_checks ();
19239 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19240 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19242 return default_argument
;
19245 /* Parse a function-body.
19248 compound_statement */
19251 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19253 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19256 /* Parse a ctor-initializer-opt followed by a function-body. Return
19257 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19258 is true we are parsing a function-try-block. */
19261 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19262 bool in_function_try_block
)
19265 bool ctor_initializer_p
;
19266 const bool check_body_p
=
19267 DECL_CONSTRUCTOR_P (current_function_decl
)
19268 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19271 /* Begin the function body. */
19272 body
= begin_function_body ();
19273 /* Parse the optional ctor-initializer. */
19274 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19276 /* If we're parsing a constexpr constructor definition, we need
19277 to check that the constructor body is indeed empty. However,
19278 before we get to cp_parser_function_body lot of junk has been
19279 generated, so we can't just check that we have an empty block.
19280 Rather we take a snapshot of the outermost block, and check whether
19281 cp_parser_function_body changed its state. */
19284 list
= cur_stmt_list
;
19285 if (STATEMENT_LIST_TAIL (list
))
19286 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19288 /* Parse the function-body. */
19289 cp_parser_function_body (parser
, in_function_try_block
);
19291 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19292 /* Finish the function body. */
19293 finish_function_body (body
);
19295 return ctor_initializer_p
;
19298 /* Parse an initializer.
19301 = initializer-clause
19302 ( expression-list )
19304 Returns an expression representing the initializer. If no
19305 initializer is present, NULL_TREE is returned.
19307 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19308 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19309 set to TRUE if there is no initializer present. If there is an
19310 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19311 is set to true; otherwise it is set to false. */
19314 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19315 bool* non_constant_p
)
19320 /* Peek at the next token. */
19321 token
= cp_lexer_peek_token (parser
->lexer
);
19323 /* Let our caller know whether or not this initializer was
19325 *is_direct_init
= (token
->type
!= CPP_EQ
);
19326 /* Assume that the initializer is constant. */
19327 *non_constant_p
= false;
19329 if (token
->type
== CPP_EQ
)
19331 /* Consume the `='. */
19332 cp_lexer_consume_token (parser
->lexer
);
19333 /* Parse the initializer-clause. */
19334 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19336 else if (token
->type
== CPP_OPEN_PAREN
)
19338 vec
<tree
, va_gc
> *vec
;
19339 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19341 /*allow_expansion_p=*/true,
19344 return error_mark_node
;
19345 init
= build_tree_list_vec (vec
);
19346 release_tree_vector (vec
);
19348 else if (token
->type
== CPP_OPEN_BRACE
)
19350 cp_lexer_set_source_position (parser
->lexer
);
19351 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19352 init
= cp_parser_braced_list (parser
, non_constant_p
);
19353 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19357 /* Anything else is an error. */
19358 cp_parser_error (parser
, "expected initializer");
19359 init
= error_mark_node
;
19365 /* Parse an initializer-clause.
19367 initializer-clause:
19368 assignment-expression
19371 Returns an expression representing the initializer.
19373 If the `assignment-expression' production is used the value
19374 returned is simply a representation for the expression.
19376 Otherwise, calls cp_parser_braced_list. */
19379 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19383 /* Assume the expression is constant. */
19384 *non_constant_p
= false;
19386 /* If it is not a `{', then we are looking at an
19387 assignment-expression. */
19388 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19391 = cp_parser_constant_expression (parser
,
19392 /*allow_non_constant_p=*/true,
19396 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19398 return initializer
;
19401 /* Parse a brace-enclosed initializer list.
19404 { initializer-list , [opt] }
19407 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19408 the elements of the initializer-list (or NULL, if the last
19409 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19410 NULL_TREE. There is no way to detect whether or not the optional
19411 trailing `,' was provided. NON_CONSTANT_P is as for
19412 cp_parser_initializer. */
19415 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19419 /* Consume the `{' token. */
19420 cp_lexer_consume_token (parser
->lexer
);
19421 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19422 initializer
= make_node (CONSTRUCTOR
);
19423 /* If it's not a `}', then there is a non-trivial initializer. */
19424 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19426 /* Parse the initializer list. */
19427 CONSTRUCTOR_ELTS (initializer
)
19428 = cp_parser_initializer_list (parser
, non_constant_p
);
19429 /* A trailing `,' token is allowed. */
19430 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19431 cp_lexer_consume_token (parser
->lexer
);
19434 *non_constant_p
= false;
19435 /* Now, there should be a trailing `}'. */
19436 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19437 TREE_TYPE (initializer
) = init_list_type_node
;
19438 return initializer
;
19441 /* Consume tokens up to, and including, the next non-nested closing `]'.
19442 Returns true iff we found a closing `]'. */
19445 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19447 unsigned square_depth
= 0;
19451 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19453 switch (token
->type
)
19456 case CPP_PRAGMA_EOL
:
19457 /* If we've run out of tokens, then there is no closing `]'. */
19460 case CPP_OPEN_SQUARE
:
19464 case CPP_CLOSE_SQUARE
:
19465 if (!square_depth
--)
19467 cp_lexer_consume_token (parser
->lexer
);
19476 /* Consume the token. */
19477 cp_lexer_consume_token (parser
->lexer
);
19481 /* Return true if we are looking at an array-designator, false otherwise. */
19484 cp_parser_array_designator_p (cp_parser
*parser
)
19486 /* Consume the `['. */
19487 cp_lexer_consume_token (parser
->lexer
);
19489 cp_lexer_save_tokens (parser
->lexer
);
19491 /* Skip tokens until the next token is a closing square bracket.
19492 If we find the closing `]', and the next token is a `=', then
19493 we are looking at an array designator. */
19494 bool array_designator_p
19495 = (cp_parser_skip_to_closing_square_bracket (parser
)
19496 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19498 /* Roll back the tokens we skipped. */
19499 cp_lexer_rollback_tokens (parser
->lexer
);
19501 return array_designator_p
;
19504 /* Parse an initializer-list.
19507 initializer-clause ... [opt]
19508 initializer-list , initializer-clause ... [opt]
19513 designation initializer-clause ...[opt]
19514 initializer-list , designation initializer-clause ...[opt]
19519 [ constant-expression ] =
19521 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19522 for the initializer. If the INDEX of the elt is non-NULL, it is the
19523 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19524 as for cp_parser_initializer. */
19526 static vec
<constructor_elt
, va_gc
> *
19527 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19529 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19531 /* Assume all of the expressions are constant. */
19532 *non_constant_p
= false;
19534 /* Parse the rest of the list. */
19540 bool clause_non_constant_p
;
19542 /* If the next token is an identifier and the following one is a
19543 colon, we are looking at the GNU designated-initializer
19545 if (cp_parser_allow_gnu_extensions_p (parser
)
19546 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19547 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19549 /* Warn the user that they are using an extension. */
19550 pedwarn (input_location
, OPT_Wpedantic
,
19551 "ISO C++ does not allow designated initializers");
19552 /* Consume the identifier. */
19553 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19554 /* Consume the `:'. */
19555 cp_lexer_consume_token (parser
->lexer
);
19557 /* Also handle the C99 syntax, '. id ='. */
19558 else if (cp_parser_allow_gnu_extensions_p (parser
)
19559 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19560 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19561 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19563 /* Warn the user that they are using an extension. */
19564 pedwarn (input_location
, OPT_Wpedantic
,
19565 "ISO C++ does not allow C99 designated initializers");
19566 /* Consume the `.'. */
19567 cp_lexer_consume_token (parser
->lexer
);
19568 /* Consume the identifier. */
19569 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19570 /* Consume the `='. */
19571 cp_lexer_consume_token (parser
->lexer
);
19573 /* Also handle C99 array designators, '[ const ] ='. */
19574 else if (cp_parser_allow_gnu_extensions_p (parser
)
19575 && !c_dialect_objc ()
19576 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19578 /* In C++11, [ could start a lambda-introducer. */
19579 bool non_const
= false;
19581 cp_parser_parse_tentatively (parser
);
19583 if (!cp_parser_array_designator_p (parser
))
19585 cp_parser_simulate_error (parser
);
19586 designator
= NULL_TREE
;
19590 designator
= cp_parser_constant_expression (parser
, true,
19592 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19593 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19596 if (!cp_parser_parse_definitely (parser
))
19597 designator
= NULL_TREE
;
19598 else if (non_const
)
19599 require_potential_rvalue_constant_expression (designator
);
19602 designator
= NULL_TREE
;
19604 /* Parse the initializer. */
19605 initializer
= cp_parser_initializer_clause (parser
,
19606 &clause_non_constant_p
);
19607 /* If any clause is non-constant, so is the entire initializer. */
19608 if (clause_non_constant_p
)
19609 *non_constant_p
= true;
19611 /* If we have an ellipsis, this is an initializer pack
19613 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19615 /* Consume the `...'. */
19616 cp_lexer_consume_token (parser
->lexer
);
19618 /* Turn the initializer into an initializer expansion. */
19619 initializer
= make_pack_expansion (initializer
);
19622 /* Add it to the vector. */
19623 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19625 /* If the next token is not a comma, we have reached the end of
19627 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19630 /* Peek at the next token. */
19631 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19632 /* If the next token is a `}', then we're still done. An
19633 initializer-clause can have a trailing `,' after the
19634 initializer-list and before the closing `}'. */
19635 if (token
->type
== CPP_CLOSE_BRACE
)
19638 /* Consume the `,' token. */
19639 cp_lexer_consume_token (parser
->lexer
);
19645 /* Classes [gram.class] */
19647 /* Parse a class-name.
19653 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19654 to indicate that names looked up in dependent types should be
19655 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19656 keyword has been used to indicate that the name that appears next
19657 is a template. TAG_TYPE indicates the explicit tag given before
19658 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19659 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19660 is the class being defined in a class-head. If ENUM_OK is TRUE,
19661 enum-names are also accepted.
19663 Returns the TYPE_DECL representing the class. */
19666 cp_parser_class_name (cp_parser
*parser
,
19667 bool typename_keyword_p
,
19668 bool template_keyword_p
,
19669 enum tag_types tag_type
,
19670 bool check_dependency_p
,
19672 bool is_declaration
,
19679 tree identifier
= NULL_TREE
;
19681 /* All class-names start with an identifier. */
19682 token
= cp_lexer_peek_token (parser
->lexer
);
19683 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19685 cp_parser_error (parser
, "expected class-name");
19686 return error_mark_node
;
19689 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19690 to a template-id, so we save it here. */
19691 scope
= parser
->scope
;
19692 if (scope
== error_mark_node
)
19693 return error_mark_node
;
19695 /* Any name names a type if we're following the `typename' keyword
19696 in a qualified name where the enclosing scope is type-dependent. */
19697 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19698 && dependent_type_p (scope
));
19699 /* Handle the common case (an identifier, but not a template-id)
19701 if (token
->type
== CPP_NAME
19702 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19704 cp_token
*identifier_token
;
19707 /* Look for the identifier. */
19708 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19709 ambiguous_p
= identifier_token
->error_reported
;
19710 identifier
= cp_parser_identifier (parser
);
19711 /* If the next token isn't an identifier, we are certainly not
19712 looking at a class-name. */
19713 if (identifier
== error_mark_node
)
19714 decl
= error_mark_node
;
19715 /* If we know this is a type-name, there's no need to look it
19717 else if (typename_p
)
19721 tree ambiguous_decls
;
19722 /* If we already know that this lookup is ambiguous, then
19723 we've already issued an error message; there's no reason
19727 cp_parser_simulate_error (parser
);
19728 return error_mark_node
;
19730 /* If the next token is a `::', then the name must be a type
19733 [basic.lookup.qual]
19735 During the lookup for a name preceding the :: scope
19736 resolution operator, object, function, and enumerator
19737 names are ignored. */
19738 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19739 tag_type
= typename_type
;
19740 /* Look up the name. */
19741 decl
= cp_parser_lookup_name (parser
, identifier
,
19743 /*is_template=*/false,
19744 /*is_namespace=*/false,
19745 check_dependency_p
,
19747 identifier_token
->location
);
19748 if (ambiguous_decls
)
19750 if (cp_parser_parsing_tentatively (parser
))
19751 cp_parser_simulate_error (parser
);
19752 return error_mark_node
;
19758 /* Try a template-id. */
19759 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19760 check_dependency_p
,
19763 if (decl
== error_mark_node
)
19764 return error_mark_node
;
19767 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19769 /* If this is a typename, create a TYPENAME_TYPE. */
19770 if (typename_p
&& decl
!= error_mark_node
)
19772 decl
= make_typename_type (scope
, decl
, typename_type
,
19773 /*complain=*/tf_error
);
19774 if (decl
!= error_mark_node
)
19775 decl
= TYPE_NAME (decl
);
19778 decl
= strip_using_decl (decl
);
19780 /* Check to see that it is really the name of a class. */
19781 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19782 && identifier_p (TREE_OPERAND (decl
, 0))
19783 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19784 /* Situations like this:
19786 template <typename T> struct A {
19787 typename T::template X<int>::I i;
19790 are problematic. Is `T::template X<int>' a class-name? The
19791 standard does not seem to be definitive, but there is no other
19792 valid interpretation of the following `::'. Therefore, those
19793 names are considered class-names. */
19795 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19796 if (decl
!= error_mark_node
)
19797 decl
= TYPE_NAME (decl
);
19799 else if (TREE_CODE (decl
) != TYPE_DECL
19800 || TREE_TYPE (decl
) == error_mark_node
19801 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19802 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
19803 /* In Objective-C 2.0, a classname followed by '.' starts a
19804 dot-syntax expression, and it's not a type-name. */
19805 || (c_dialect_objc ()
19806 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19807 && objc_is_class_name (decl
)))
19808 decl
= error_mark_node
;
19810 if (decl
== error_mark_node
)
19811 cp_parser_error (parser
, "expected class-name");
19812 else if (identifier
&& !parser
->scope
)
19813 maybe_note_name_used_in_class (identifier
, decl
);
19818 /* Parse a class-specifier.
19821 class-head { member-specification [opt] }
19823 Returns the TREE_TYPE representing the class. */
19826 cp_parser_class_specifier_1 (cp_parser
* parser
)
19829 tree attributes
= NULL_TREE
;
19830 bool nested_name_specifier_p
;
19831 unsigned saved_num_template_parameter_lists
;
19832 bool saved_in_function_body
;
19833 unsigned char in_statement
;
19834 bool in_switch_statement_p
;
19835 bool saved_in_unbraced_linkage_specification_p
;
19836 tree old_scope
= NULL_TREE
;
19837 tree scope
= NULL_TREE
;
19838 cp_token
*closing_brace
;
19840 push_deferring_access_checks (dk_no_deferred
);
19842 /* Parse the class-head. */
19843 type
= cp_parser_class_head (parser
,
19844 &nested_name_specifier_p
);
19845 /* If the class-head was a semantic disaster, skip the entire body
19849 cp_parser_skip_to_end_of_block_or_statement (parser
);
19850 pop_deferring_access_checks ();
19851 return error_mark_node
;
19854 /* Look for the `{'. */
19855 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19857 pop_deferring_access_checks ();
19858 return error_mark_node
;
19861 cp_ensure_no_omp_declare_simd (parser
);
19863 /* Issue an error message if type-definitions are forbidden here. */
19864 cp_parser_check_type_definition (parser
);
19865 /* Remember that we are defining one more class. */
19866 ++parser
->num_classes_being_defined
;
19867 /* Inside the class, surrounding template-parameter-lists do not
19869 saved_num_template_parameter_lists
19870 = parser
->num_template_parameter_lists
;
19871 parser
->num_template_parameter_lists
= 0;
19872 /* We are not in a function body. */
19873 saved_in_function_body
= parser
->in_function_body
;
19874 parser
->in_function_body
= false;
19875 /* Or in a loop. */
19876 in_statement
= parser
->in_statement
;
19877 parser
->in_statement
= 0;
19878 /* Or in a switch. */
19879 in_switch_statement_p
= parser
->in_switch_statement_p
;
19880 parser
->in_switch_statement_p
= false;
19881 /* We are not immediately inside an extern "lang" block. */
19882 saved_in_unbraced_linkage_specification_p
19883 = parser
->in_unbraced_linkage_specification_p
;
19884 parser
->in_unbraced_linkage_specification_p
= false;
19886 /* Start the class. */
19887 if (nested_name_specifier_p
)
19889 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19890 old_scope
= push_inner_scope (scope
);
19892 type
= begin_class_definition (type
);
19894 if (type
== error_mark_node
)
19895 /* If the type is erroneous, skip the entire body of the class. */
19896 cp_parser_skip_to_closing_brace (parser
);
19898 /* Parse the member-specification. */
19899 cp_parser_member_specification_opt (parser
);
19901 /* Look for the trailing `}'. */
19902 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19903 /* Look for trailing attributes to apply to this class. */
19904 if (cp_parser_allow_gnu_extensions_p (parser
))
19905 attributes
= cp_parser_gnu_attributes_opt (parser
);
19906 if (type
!= error_mark_node
)
19907 type
= finish_struct (type
, attributes
);
19908 if (nested_name_specifier_p
)
19909 pop_inner_scope (old_scope
, scope
);
19911 /* We've finished a type definition. Check for the common syntax
19912 error of forgetting a semicolon after the definition. We need to
19913 be careful, as we can't just check for not-a-semicolon and be done
19914 with it; the user might have typed:
19916 class X { } c = ...;
19917 class X { } *p = ...;
19919 and so forth. Instead, enumerate all the possible tokens that
19920 might follow this production; if we don't see one of them, then
19921 complain and silently insert the semicolon. */
19923 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19924 bool want_semicolon
= true;
19926 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19927 /* Don't try to parse c++11 attributes here. As per the
19928 grammar, that should be a task for
19929 cp_parser_decl_specifier_seq. */
19930 want_semicolon
= false;
19932 switch (token
->type
)
19935 case CPP_SEMICOLON
:
19938 case CPP_OPEN_PAREN
:
19939 case CPP_CLOSE_PAREN
:
19941 want_semicolon
= false;
19944 /* While it's legal for type qualifiers and storage class
19945 specifiers to follow type definitions in the grammar, only
19946 compiler testsuites contain code like that. Assume that if
19947 we see such code, then what we're really seeing is a case
19951 const <type> var = ...;
19956 static <type> func (...) ...
19958 i.e. the qualifier or specifier applies to the next
19959 declaration. To do so, however, we need to look ahead one
19960 more token to see if *that* token is a type specifier.
19962 This code could be improved to handle:
19965 static const <type> var = ...; */
19967 if (keyword_is_decl_specifier (token
->keyword
))
19969 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19971 /* Handling user-defined types here would be nice, but very
19974 = (lookahead
->type
== CPP_KEYWORD
19975 && keyword_begins_type_specifier (lookahead
->keyword
));
19982 /* If we don't have a type, then something is very wrong and we
19983 shouldn't try to do anything clever. Likewise for not seeing the
19985 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19987 cp_token_position prev
19988 = cp_lexer_previous_token_position (parser
->lexer
);
19989 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19990 location_t loc
= prev_token
->location
;
19992 if (CLASSTYPE_DECLARED_CLASS (type
))
19993 error_at (loc
, "expected %<;%> after class definition");
19994 else if (TREE_CODE (type
) == RECORD_TYPE
)
19995 error_at (loc
, "expected %<;%> after struct definition");
19996 else if (TREE_CODE (type
) == UNION_TYPE
)
19997 error_at (loc
, "expected %<;%> after union definition");
19999 gcc_unreachable ();
20001 /* Unget one token and smash it to look as though we encountered
20002 a semicolon in the input stream. */
20003 cp_lexer_set_token_position (parser
->lexer
, prev
);
20004 token
= cp_lexer_peek_token (parser
->lexer
);
20005 token
->type
= CPP_SEMICOLON
;
20006 token
->keyword
= RID_MAX
;
20010 /* If this class is not itself within the scope of another class,
20011 then we need to parse the bodies of all of the queued function
20012 definitions. Note that the queued functions defined in a class
20013 are not always processed immediately following the
20014 class-specifier for that class. Consider:
20017 struct B { void f() { sizeof (A); } };
20020 If `f' were processed before the processing of `A' were
20021 completed, there would be no way to compute the size of `A'.
20022 Note that the nesting we are interested in here is lexical --
20023 not the semantic nesting given by TYPE_CONTEXT. In particular,
20026 struct A { struct B; };
20027 struct A::B { void f() { } };
20029 there is no need to delay the parsing of `A::B::f'. */
20030 if (--parser
->num_classes_being_defined
== 0)
20033 tree class_type
= NULL_TREE
;
20034 tree pushed_scope
= NULL_TREE
;
20036 cp_default_arg_entry
*e
;
20037 tree save_ccp
, save_ccr
;
20039 /* In a first pass, parse default arguments to the functions.
20040 Then, in a second pass, parse the bodies of the functions.
20041 This two-phased approach handles cases like:
20049 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
20052 /* If there are default arguments that have not yet been processed,
20053 take care of them now. */
20054 if (class_type
!= e
->class_type
)
20057 pop_scope (pushed_scope
);
20058 class_type
= e
->class_type
;
20059 pushed_scope
= push_scope (class_type
);
20061 /* Make sure that any template parameters are in scope. */
20062 maybe_begin_member_template_processing (decl
);
20063 /* Parse the default argument expressions. */
20064 cp_parser_late_parsing_default_args (parser
, decl
);
20065 /* Remove any template parameters from the symbol table. */
20066 maybe_end_member_template_processing ();
20068 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
20069 /* Now parse any NSDMIs. */
20070 save_ccp
= current_class_ptr
;
20071 save_ccr
= current_class_ref
;
20072 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
20074 if (class_type
!= DECL_CONTEXT (decl
))
20077 pop_scope (pushed_scope
);
20078 class_type
= DECL_CONTEXT (decl
);
20079 pushed_scope
= push_scope (class_type
);
20081 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
20082 cp_parser_late_parsing_nsdmi (parser
, decl
);
20084 vec_safe_truncate (unparsed_nsdmis
, 0);
20085 current_class_ptr
= save_ccp
;
20086 current_class_ref
= save_ccr
;
20088 pop_scope (pushed_scope
);
20090 /* Now do some post-NSDMI bookkeeping. */
20091 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
20092 after_nsdmi_defaulted_late_checks (class_type
);
20093 vec_safe_truncate (unparsed_classes
, 0);
20094 after_nsdmi_defaulted_late_checks (type
);
20096 /* Now parse the body of the functions. */
20099 /* OpenMP UDRs need to be parsed before all other functions. */
20100 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20101 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
20102 cp_parser_late_parsing_for_member (parser
, decl
);
20103 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20104 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
20105 cp_parser_late_parsing_for_member (parser
, decl
);
20108 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20109 cp_parser_late_parsing_for_member (parser
, decl
);
20110 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
20113 vec_safe_push (unparsed_classes
, type
);
20115 /* Put back any saved access checks. */
20116 pop_deferring_access_checks ();
20118 /* Restore saved state. */
20119 parser
->in_switch_statement_p
= in_switch_statement_p
;
20120 parser
->in_statement
= in_statement
;
20121 parser
->in_function_body
= saved_in_function_body
;
20122 parser
->num_template_parameter_lists
20123 = saved_num_template_parameter_lists
;
20124 parser
->in_unbraced_linkage_specification_p
20125 = saved_in_unbraced_linkage_specification_p
;
20131 cp_parser_class_specifier (cp_parser
* parser
)
20134 timevar_push (TV_PARSE_STRUCT
);
20135 ret
= cp_parser_class_specifier_1 (parser
);
20136 timevar_pop (TV_PARSE_STRUCT
);
20140 /* Parse a class-head.
20143 class-key identifier [opt] base-clause [opt]
20144 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20145 class-key nested-name-specifier [opt] template-id
20148 class-virt-specifier:
20152 class-key attributes identifier [opt] base-clause [opt]
20153 class-key attributes nested-name-specifier identifier base-clause [opt]
20154 class-key attributes nested-name-specifier [opt] template-id
20157 Upon return BASES is initialized to the list of base classes (or
20158 NULL, if there are none) in the same form returned by
20159 cp_parser_base_clause.
20161 Returns the TYPE of the indicated class. Sets
20162 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20163 involving a nested-name-specifier was used, and FALSE otherwise.
20165 Returns error_mark_node if this is not a class-head.
20167 Returns NULL_TREE if the class-head is syntactically valid, but
20168 semantically invalid in a way that means we should skip the entire
20169 body of the class. */
20172 cp_parser_class_head (cp_parser
* parser
,
20173 bool* nested_name_specifier_p
)
20175 tree nested_name_specifier
;
20176 enum tag_types class_key
;
20177 tree id
= NULL_TREE
;
20178 tree type
= NULL_TREE
;
20181 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20182 bool template_id_p
= false;
20183 bool qualified_p
= false;
20184 bool invalid_nested_name_p
= false;
20185 bool invalid_explicit_specialization_p
= false;
20186 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20187 tree pushed_scope
= NULL_TREE
;
20188 unsigned num_templates
;
20189 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20190 /* Assume no nested-name-specifier will be present. */
20191 *nested_name_specifier_p
= false;
20192 /* Assume no template parameter lists will be used in defining the
20195 parser
->colon_corrects_to_scope_p
= false;
20197 /* Look for the class-key. */
20198 class_key
= cp_parser_class_key (parser
);
20199 if (class_key
== none_type
)
20200 return error_mark_node
;
20202 /* Parse the attributes. */
20203 attributes
= cp_parser_attributes_opt (parser
);
20205 /* If the next token is `::', that is invalid -- but sometimes
20206 people do try to write:
20210 Handle this gracefully by accepting the extra qualifier, and then
20211 issuing an error about it later if this really is a
20212 class-head. If it turns out just to be an elaborated type
20213 specifier, remain silent. */
20214 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20215 qualified_p
= true;
20217 push_deferring_access_checks (dk_no_check
);
20219 /* Determine the name of the class. Begin by looking for an
20220 optional nested-name-specifier. */
20221 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20222 nested_name_specifier
20223 = cp_parser_nested_name_specifier_opt (parser
,
20224 /*typename_keyword_p=*/false,
20225 /*check_dependency_p=*/false,
20227 /*is_declaration=*/false);
20228 /* If there was a nested-name-specifier, then there *must* be an
20230 if (nested_name_specifier
)
20232 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20233 /* Although the grammar says `identifier', it really means
20234 `class-name' or `template-name'. You are only allowed to
20235 define a class that has already been declared with this
20238 The proposed resolution for Core Issue 180 says that wherever
20239 you see `class T::X' you should treat `X' as a type-name.
20241 It is OK to define an inaccessible class; for example:
20243 class A { class B; };
20246 We do not know if we will see a class-name, or a
20247 template-name. We look for a class-name first, in case the
20248 class-name is a template-id; if we looked for the
20249 template-name first we would stop after the template-name. */
20250 cp_parser_parse_tentatively (parser
);
20251 type
= cp_parser_class_name (parser
,
20252 /*typename_keyword_p=*/false,
20253 /*template_keyword_p=*/false,
20255 /*check_dependency_p=*/false,
20256 /*class_head_p=*/true,
20257 /*is_declaration=*/false);
20258 /* If that didn't work, ignore the nested-name-specifier. */
20259 if (!cp_parser_parse_definitely (parser
))
20261 invalid_nested_name_p
= true;
20262 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20263 id
= cp_parser_identifier (parser
);
20264 if (id
== error_mark_node
)
20267 /* If we could not find a corresponding TYPE, treat this
20268 declaration like an unqualified declaration. */
20269 if (type
== error_mark_node
)
20270 nested_name_specifier
= NULL_TREE
;
20271 /* Otherwise, count the number of templates used in TYPE and its
20272 containing scopes. */
20277 for (scope
= TREE_TYPE (type
);
20278 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20279 scope
= get_containing_scope (scope
))
20281 && CLASS_TYPE_P (scope
)
20282 && CLASSTYPE_TEMPLATE_INFO (scope
)
20283 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20284 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20285 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20289 /* Otherwise, the identifier is optional. */
20292 /* We don't know whether what comes next is a template-id,
20293 an identifier, or nothing at all. */
20294 cp_parser_parse_tentatively (parser
);
20295 /* Check for a template-id. */
20296 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20297 id
= cp_parser_template_id (parser
,
20298 /*template_keyword_p=*/false,
20299 /*check_dependency_p=*/true,
20301 /*is_declaration=*/true);
20302 /* If that didn't work, it could still be an identifier. */
20303 if (!cp_parser_parse_definitely (parser
))
20305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20307 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20308 id
= cp_parser_identifier (parser
);
20315 template_id_p
= true;
20320 pop_deferring_access_checks ();
20324 cp_parser_check_for_invalid_template_id (parser
, id
,
20326 type_start_token
->location
);
20328 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20330 /* If it's not a `:' or a `{' then we can't really be looking at a
20331 class-head, since a class-head only appears as part of a
20332 class-specifier. We have to detect this situation before calling
20333 xref_tag, since that has irreversible side-effects. */
20334 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20336 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20337 type
= error_mark_node
;
20341 /* At this point, we're going ahead with the class-specifier, even
20342 if some other problem occurs. */
20343 cp_parser_commit_to_tentative_parse (parser
);
20344 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20346 cp_parser_error (parser
,
20347 "cannot specify %<override%> for a class");
20348 type
= error_mark_node
;
20351 /* Issue the error about the overly-qualified name now. */
20354 cp_parser_error (parser
,
20355 "global qualification of class name is invalid");
20356 type
= error_mark_node
;
20359 else if (invalid_nested_name_p
)
20361 cp_parser_error (parser
,
20362 "qualified name does not name a class");
20363 type
= error_mark_node
;
20366 else if (nested_name_specifier
)
20370 /* Reject typedef-names in class heads. */
20371 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20373 error_at (type_start_token
->location
,
20374 "invalid class name in declaration of %qD",
20380 /* Figure out in what scope the declaration is being placed. */
20381 scope
= current_scope ();
20382 /* If that scope does not contain the scope in which the
20383 class was originally declared, the program is invalid. */
20384 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20386 if (at_namespace_scope_p ())
20387 error_at (type_start_token
->location
,
20388 "declaration of %qD in namespace %qD which does not "
20390 type
, scope
, nested_name_specifier
);
20392 error_at (type_start_token
->location
,
20393 "declaration of %qD in %qD which does not enclose %qD",
20394 type
, scope
, nested_name_specifier
);
20400 A declarator-id shall not be qualified except for the
20401 definition of a ... nested class outside of its class
20402 ... [or] the definition or explicit instantiation of a
20403 class member of a namespace outside of its namespace. */
20404 if (scope
== nested_name_specifier
)
20406 permerror (nested_name_specifier_token_start
->location
,
20407 "extra qualification not allowed");
20408 nested_name_specifier
= NULL_TREE
;
20412 /* An explicit-specialization must be preceded by "template <>". If
20413 it is not, try to recover gracefully. */
20414 if (at_namespace_scope_p ()
20415 && parser
->num_template_parameter_lists
== 0
20418 error_at (type_start_token
->location
,
20419 "an explicit specialization must be preceded by %<template <>%>");
20420 invalid_explicit_specialization_p
= true;
20421 /* Take the same action that would have been taken by
20422 cp_parser_explicit_specialization. */
20423 ++parser
->num_template_parameter_lists
;
20424 begin_specialization ();
20426 /* There must be no "return" statements between this point and the
20427 end of this function; set "type "to the correct return value and
20428 use "goto done;" to return. */
20429 /* Make sure that the right number of template parameters were
20431 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20432 type_start_token
->location
,
20433 /*declarator=*/NULL
))
20435 /* If something went wrong, there is no point in even trying to
20436 process the class-definition. */
20441 /* Look up the type. */
20444 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20445 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20446 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20448 error_at (type_start_token
->location
,
20449 "function template %qD redeclared as a class template", id
);
20450 type
= error_mark_node
;
20454 type
= TREE_TYPE (id
);
20455 type
= maybe_process_partial_specialization (type
);
20457 if (nested_name_specifier
)
20458 pushed_scope
= push_scope (nested_name_specifier
);
20460 else if (nested_name_specifier
)
20466 template <typename T> struct S { struct T };
20467 template <typename T> struct S<T>::T { };
20469 we will get a TYPENAME_TYPE when processing the definition of
20470 `S::T'. We need to resolve it to the actual type before we
20471 try to define it. */
20472 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20474 class_type
= resolve_typename_type (TREE_TYPE (type
),
20475 /*only_current_p=*/false);
20476 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20477 type
= TYPE_NAME (class_type
);
20480 cp_parser_error (parser
, "could not resolve typename type");
20481 type
= error_mark_node
;
20485 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20486 == error_mark_node
)
20492 class_type
= current_class_type
;
20493 /* Enter the scope indicated by the nested-name-specifier. */
20494 pushed_scope
= push_scope (nested_name_specifier
);
20495 /* Get the canonical version of this type. */
20496 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20497 /* Call push_template_decl if it seems like we should be defining a
20498 template either from the template headers or the type we're
20499 defining, so that we diagnose both extra and missing headers. */
20500 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20501 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20502 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20504 type
= push_template_decl (type
);
20505 if (type
== error_mark_node
)
20512 type
= TREE_TYPE (type
);
20513 *nested_name_specifier_p
= true;
20515 else /* The name is not a nested name. */
20517 /* If the class was unnamed, create a dummy name. */
20519 id
= make_anon_name ();
20520 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20521 parser
->num_template_parameter_lists
);
20524 /* Indicate whether this class was declared as a `class' or as a
20526 if (TREE_CODE (type
) == RECORD_TYPE
)
20527 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20528 cp_parser_check_class_key (class_key
, type
);
20530 /* If this type was already complete, and we see another definition,
20531 that's an error. */
20532 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20534 error_at (type_start_token
->location
, "redefinition of %q#T",
20536 error_at (type_start_token
->location
, "previous definition of %q+#T",
20541 else if (type
== error_mark_node
)
20546 /* Apply attributes now, before any use of the class as a template
20547 argument in its base list. */
20548 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20549 fixup_attribute_variants (type
);
20552 /* We will have entered the scope containing the class; the names of
20553 base classes should be looked up in that context. For example:
20555 struct A { struct B {}; struct C; };
20556 struct A::C : B {};
20560 /* Get the list of base-classes, if there is one. */
20561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20563 /* PR59482: enter the class scope so that base-specifiers are looked
20567 bases
= cp_parser_base_clause (parser
);
20568 /* PR59482: get out of the previously pushed class scope so that the
20569 subsequent pops pop the right thing. */
20576 /* If we're really defining a class, process the base classes.
20577 If they're invalid, fail. */
20578 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20579 && !xref_basetypes (type
, bases
))
20583 /* Leave the scope given by the nested-name-specifier. We will
20584 enter the class scope itself while processing the members. */
20586 pop_scope (pushed_scope
);
20588 if (invalid_explicit_specialization_p
)
20590 end_specialization ();
20591 --parser
->num_template_parameter_lists
;
20595 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20596 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20597 CLASSTYPE_FINAL (type
) = 1;
20599 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20603 /* Parse a class-key.
20610 Returns the kind of class-key specified, or none_type to indicate
20613 static enum tag_types
20614 cp_parser_class_key (cp_parser
* parser
)
20617 enum tag_types tag_type
;
20619 /* Look for the class-key. */
20620 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20624 /* Check to see if the TOKEN is a class-key. */
20625 tag_type
= cp_parser_token_is_class_key (token
);
20627 cp_parser_error (parser
, "expected class-key");
20631 /* Parse a type-parameter-key.
20633 type-parameter-key:
20639 cp_parser_type_parameter_key (cp_parser
* parser
)
20641 /* Look for the type-parameter-key. */
20642 enum tag_types tag_type
= none_type
;
20643 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20644 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20646 cp_lexer_consume_token (parser
->lexer
);
20647 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20648 /* typename is not allowed in a template template parameter
20649 by the standard until C++1Z. */
20650 pedwarn (token
->location
, OPT_Wpedantic
,
20651 "ISO C++ forbids typename key in template template parameter;"
20652 " use -std=c++1z or -std=gnu++1z");
20655 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20660 /* Parse an (optional) member-specification.
20662 member-specification:
20663 member-declaration member-specification [opt]
20664 access-specifier : member-specification [opt] */
20667 cp_parser_member_specification_opt (cp_parser
* parser
)
20674 /* Peek at the next token. */
20675 token
= cp_lexer_peek_token (parser
->lexer
);
20676 /* If it's a `}', or EOF then we've seen all the members. */
20677 if (token
->type
== CPP_CLOSE_BRACE
20678 || token
->type
== CPP_EOF
20679 || token
->type
== CPP_PRAGMA_EOL
)
20682 /* See if this token is a keyword. */
20683 keyword
= token
->keyword
;
20687 case RID_PROTECTED
:
20689 /* Consume the access-specifier. */
20690 cp_lexer_consume_token (parser
->lexer
);
20691 /* Remember which access-specifier is active. */
20692 current_access_specifier
= token
->u
.value
;
20693 /* Look for the `:'. */
20694 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20698 /* Accept #pragmas at class scope. */
20699 if (token
->type
== CPP_PRAGMA
)
20701 cp_parser_pragma (parser
, pragma_member
);
20705 /* Otherwise, the next construction must be a
20706 member-declaration. */
20707 cp_parser_member_declaration (parser
);
20712 /* Parse a member-declaration.
20714 member-declaration:
20715 decl-specifier-seq [opt] member-declarator-list [opt] ;
20716 function-definition ; [opt]
20717 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20719 template-declaration
20722 member-declarator-list:
20724 member-declarator-list , member-declarator
20727 declarator pure-specifier [opt]
20728 declarator constant-initializer [opt]
20729 identifier [opt] : constant-expression
20733 member-declaration:
20734 __extension__ member-declaration
20737 declarator attributes [opt] pure-specifier [opt]
20738 declarator attributes [opt] constant-initializer [opt]
20739 identifier [opt] attributes [opt] : constant-expression
20743 member-declaration:
20744 static_assert-declaration */
20747 cp_parser_member_declaration (cp_parser
* parser
)
20749 cp_decl_specifier_seq decl_specifiers
;
20750 tree prefix_attributes
;
20752 int declares_class_or_enum
;
20754 cp_token
*token
= NULL
;
20755 cp_token
*decl_spec_token_start
= NULL
;
20756 cp_token
*initializer_token_start
= NULL
;
20757 int saved_pedantic
;
20758 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20760 /* Check for the `__extension__' keyword. */
20761 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20764 cp_parser_member_declaration (parser
);
20765 /* Restore the old value of the PEDANTIC flag. */
20766 pedantic
= saved_pedantic
;
20771 /* Check for a template-declaration. */
20772 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20774 /* An explicit specialization here is an error condition, and we
20775 expect the specialization handler to detect and report this. */
20776 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20777 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20778 cp_parser_explicit_specialization (parser
);
20780 cp_parser_template_declaration (parser
, /*member_p=*/true);
20785 /* Check for a using-declaration. */
20786 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20788 if (cxx_dialect
< cxx11
)
20790 /* Parse the using-declaration. */
20791 cp_parser_using_declaration (parser
,
20792 /*access_declaration_p=*/false);
20798 bool alias_decl_expected
;
20799 cp_parser_parse_tentatively (parser
);
20800 decl
= cp_parser_alias_declaration (parser
);
20801 /* Note that if we actually see the '=' token after the
20802 identifier, cp_parser_alias_declaration commits the
20803 tentative parse. In that case, we really expect an
20804 alias-declaration. Otherwise, we expect a using
20806 alias_decl_expected
=
20807 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20808 cp_parser_parse_definitely (parser
);
20810 if (alias_decl_expected
)
20811 finish_member_declaration (decl
);
20813 cp_parser_using_declaration (parser
,
20814 /*access_declaration_p=*/false);
20819 /* Check for @defs. */
20820 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20823 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20824 ivar
= ivar_chains
;
20828 ivar
= TREE_CHAIN (member
);
20829 TREE_CHAIN (member
) = NULL_TREE
;
20830 finish_member_declaration (member
);
20835 /* If the next token is `static_assert' we have a static assertion. */
20836 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20838 cp_parser_static_assert (parser
, /*member_p=*/true);
20842 parser
->colon_corrects_to_scope_p
= false;
20844 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20847 /* Parse the decl-specifier-seq. */
20848 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20849 cp_parser_decl_specifier_seq (parser
,
20850 CP_PARSER_FLAGS_OPTIONAL
,
20852 &declares_class_or_enum
);
20853 /* Check for an invalid type-name. */
20854 if (!decl_specifiers
.any_type_specifiers_p
20855 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20857 /* If there is no declarator, then the decl-specifier-seq should
20859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20861 /* If there was no decl-specifier-seq, and the next token is a
20862 `;', then we have something like:
20868 Each member-declaration shall declare at least one member
20869 name of the class. */
20870 if (!decl_specifiers
.any_specifiers_p
)
20872 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20873 if (!in_system_header_at (token
->location
))
20874 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20880 /* See if this declaration is a friend. */
20881 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20882 /* If there were decl-specifiers, check to see if there was
20883 a class-declaration. */
20884 type
= check_tag_decl (&decl_specifiers
,
20885 /*explicit_type_instantiation_p=*/false);
20886 /* Nested classes have already been added to the class, but
20887 a `friend' needs to be explicitly registered. */
20890 /* If the `friend' keyword was present, the friend must
20891 be introduced with a class-key. */
20892 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20893 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20894 "in C++03 a class-key must be used "
20895 "when declaring a friend");
20898 template <typename T> struct A {
20899 friend struct A<T>::B;
20902 A<T>::B will be represented by a TYPENAME_TYPE, and
20903 therefore not recognized by check_tag_decl. */
20906 type
= decl_specifiers
.type
;
20907 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20908 type
= TREE_TYPE (type
);
20910 if (!type
|| !TYPE_P (type
))
20911 error_at (decl_spec_token_start
->location
,
20912 "friend declaration does not name a class or "
20915 make_friend_class (current_class_type
, type
,
20916 /*complain=*/true);
20918 /* If there is no TYPE, an error message will already have
20920 else if (!type
|| type
== error_mark_node
)
20922 /* An anonymous aggregate has to be handled specially; such
20923 a declaration really declares a data member (with a
20924 particular type), as opposed to a nested class. */
20925 else if (ANON_AGGR_TYPE_P (type
))
20928 if (decl_specifiers
.storage_class
!= sc_none
)
20929 error_at (decl_spec_token_start
->location
,
20930 "a storage class on an anonymous aggregate "
20931 "in class scope is not allowed");
20933 /* Remove constructors and such from TYPE, now that we
20934 know it is an anonymous aggregate. */
20935 fixup_anonymous_aggr (type
);
20936 /* And make the corresponding data member. */
20937 decl
= build_decl (decl_spec_token_start
->location
,
20938 FIELD_DECL
, NULL_TREE
, type
);
20939 /* Add it to the class. */
20940 finish_member_declaration (decl
);
20943 cp_parser_check_access_in_redeclaration
20945 decl_spec_token_start
->location
);
20950 bool assume_semicolon
= false;
20952 /* Clear attributes from the decl_specifiers but keep them
20953 around as prefix attributes that apply them to the entity
20955 prefix_attributes
= decl_specifiers
.attributes
;
20956 decl_specifiers
.attributes
= NULL_TREE
;
20958 /* See if these declarations will be friends. */
20959 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20961 /* Keep going until we hit the `;' at the end of the
20963 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20965 tree attributes
= NULL_TREE
;
20966 tree first_attribute
;
20968 /* Peek at the next token. */
20969 token
= cp_lexer_peek_token (parser
->lexer
);
20971 /* Check for a bitfield declaration. */
20972 if (token
->type
== CPP_COLON
20973 || (token
->type
== CPP_NAME
20974 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20980 /* Get the name of the bitfield. Note that we cannot just
20981 check TOKEN here because it may have been invalidated by
20982 the call to cp_lexer_peek_nth_token above. */
20983 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20984 identifier
= cp_parser_identifier (parser
);
20986 identifier
= NULL_TREE
;
20988 /* Consume the `:' token. */
20989 cp_lexer_consume_token (parser
->lexer
);
20990 /* Get the width of the bitfield. */
20992 = cp_parser_constant_expression (parser
);
20994 /* Look for attributes that apply to the bitfield. */
20995 attributes
= cp_parser_attributes_opt (parser
);
20996 /* Remember which attributes are prefix attributes and
20998 first_attribute
= attributes
;
20999 /* Combine the attributes. */
21000 attributes
= chainon (prefix_attributes
, attributes
);
21002 /* Create the bitfield declaration. */
21003 decl
= grokbitfield (identifier
21004 ? make_id_declarator (NULL_TREE
,
21014 cp_declarator
*declarator
;
21016 tree asm_specification
;
21017 int ctor_dtor_or_conv_p
;
21019 /* Parse the declarator. */
21021 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21022 &ctor_dtor_or_conv_p
,
21023 /*parenthesized_p=*/NULL
,
21027 /* If something went wrong parsing the declarator, make sure
21028 that we at least consume some tokens. */
21029 if (declarator
== cp_error_declarator
)
21031 /* Skip to the end of the statement. */
21032 cp_parser_skip_to_end_of_statement (parser
);
21033 /* If the next token is not a semicolon, that is
21034 probably because we just skipped over the body of
21035 a function. So, we consume a semicolon if
21036 present, but do not issue an error message if it
21038 if (cp_lexer_next_token_is (parser
->lexer
,
21040 cp_lexer_consume_token (parser
->lexer
);
21044 if (declares_class_or_enum
& 2)
21045 cp_parser_check_for_definition_in_return_type
21046 (declarator
, decl_specifiers
.type
,
21047 decl_specifiers
.locations
[ds_type_spec
]);
21049 /* Look for an asm-specification. */
21050 asm_specification
= cp_parser_asm_specification_opt (parser
);
21051 /* Look for attributes that apply to the declaration. */
21052 attributes
= cp_parser_attributes_opt (parser
);
21053 /* Remember which attributes are prefix attributes and
21055 first_attribute
= attributes
;
21056 /* Combine the attributes. */
21057 attributes
= chainon (prefix_attributes
, attributes
);
21059 /* If it's an `=', then we have a constant-initializer or a
21060 pure-specifier. It is not correct to parse the
21061 initializer before registering the member declaration
21062 since the member declaration should be in scope while
21063 its initializer is processed. However, the rest of the
21064 front end does not yet provide an interface that allows
21065 us to handle this correctly. */
21066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21070 A pure-specifier shall be used only in the declaration of
21071 a virtual function.
21073 A member-declarator can contain a constant-initializer
21074 only if it declares a static member of integral or
21077 Therefore, if the DECLARATOR is for a function, we look
21078 for a pure-specifier; otherwise, we look for a
21079 constant-initializer. When we call `grokfield', it will
21080 perform more stringent semantics checks. */
21081 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
21082 if (function_declarator_p (declarator
)
21083 || (decl_specifiers
.type
21084 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
21085 && declarator
->kind
== cdk_id
21086 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
21087 == FUNCTION_TYPE
)))
21088 initializer
= cp_parser_pure_specifier (parser
);
21089 else if (decl_specifiers
.storage_class
!= sc_static
)
21090 initializer
= cp_parser_save_nsdmi (parser
);
21091 else if (cxx_dialect
>= cxx11
)
21094 /* Don't require a constant rvalue in C++11, since we
21095 might want a reference constant. We'll enforce
21096 constancy later. */
21097 cp_lexer_consume_token (parser
->lexer
);
21098 /* Parse the initializer. */
21099 initializer
= cp_parser_initializer_clause (parser
,
21103 /* Parse the initializer. */
21104 initializer
= cp_parser_constant_initializer (parser
);
21106 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
21107 && !function_declarator_p (declarator
))
21110 if (decl_specifiers
.storage_class
!= sc_static
)
21111 initializer
= cp_parser_save_nsdmi (parser
);
21113 initializer
= cp_parser_initializer (parser
, &x
, &x
);
21115 /* Otherwise, there is no initializer. */
21117 initializer
= NULL_TREE
;
21119 /* See if we are probably looking at a function
21120 definition. We are certainly not looking at a
21121 member-declarator. Calling `grokfield' has
21122 side-effects, so we must not do it unless we are sure
21123 that we are looking at a member-declarator. */
21124 if (cp_parser_token_starts_function_definition_p
21125 (cp_lexer_peek_token (parser
->lexer
)))
21127 /* The grammar does not allow a pure-specifier to be
21128 used when a member function is defined. (It is
21129 possible that this fact is an oversight in the
21130 standard, since a pure function may be defined
21131 outside of the class-specifier. */
21132 if (initializer
&& initializer_token_start
)
21133 error_at (initializer_token_start
->location
,
21134 "pure-specifier on function-definition");
21135 decl
= cp_parser_save_member_function_body (parser
,
21139 if (parser
->fully_implicit_function_template_p
)
21140 decl
= finish_fully_implicit_template (parser
, decl
);
21141 /* If the member was not a friend, declare it here. */
21143 finish_member_declaration (decl
);
21144 /* Peek at the next token. */
21145 token
= cp_lexer_peek_token (parser
->lexer
);
21146 /* If the next token is a semicolon, consume it. */
21147 if (token
->type
== CPP_SEMICOLON
)
21148 cp_lexer_consume_token (parser
->lexer
);
21152 if (declarator
->kind
== cdk_function
)
21153 declarator
->id_loc
= token
->location
;
21154 /* Create the declaration. */
21155 decl
= grokfield (declarator
, &decl_specifiers
,
21156 initializer
, /*init_const_expr_p=*/true,
21157 asm_specification
, attributes
);
21158 if (parser
->fully_implicit_function_template_p
)
21161 finish_fully_implicit_template (parser
, 0);
21163 decl
= finish_fully_implicit_template (parser
, decl
);
21167 cp_finalize_omp_declare_simd (parser
, decl
);
21169 /* Reset PREFIX_ATTRIBUTES. */
21170 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21171 attributes
= TREE_CHAIN (attributes
);
21173 TREE_CHAIN (attributes
) = NULL_TREE
;
21175 /* If there is any qualification still in effect, clear it
21176 now; we will be starting fresh with the next declarator. */
21177 parser
->scope
= NULL_TREE
;
21178 parser
->qualifying_scope
= NULL_TREE
;
21179 parser
->object_scope
= NULL_TREE
;
21180 /* If it's a `,', then there are more declarators. */
21181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21183 cp_lexer_consume_token (parser
->lexer
);
21184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21186 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21187 error_at (token
->location
,
21188 "stray %<,%> at end of member declaration");
21191 /* If the next token isn't a `;', then we have a parse error. */
21192 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21195 /* The next token might be a ways away from where the
21196 actual semicolon is missing. Find the previous token
21197 and use that for our error position. */
21198 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21199 error_at (token
->location
,
21200 "expected %<;%> at end of member declaration");
21202 /* Assume that the user meant to provide a semicolon. If
21203 we were to cp_parser_skip_to_end_of_statement, we might
21204 skip to a semicolon inside a member function definition
21205 and issue nonsensical error messages. */
21206 assume_semicolon
= true;
21211 /* Add DECL to the list of members. */
21213 /* Explicitly include, eg, NSDMIs, for better error
21214 recovery (c++/58650). */
21215 || !DECL_DECLARES_FUNCTION_P (decl
))
21216 finish_member_declaration (decl
);
21218 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21219 cp_parser_save_default_args (parser
, decl
);
21220 else if (TREE_CODE (decl
) == FIELD_DECL
21221 && !DECL_C_BIT_FIELD (decl
)
21222 && DECL_INITIAL (decl
))
21223 /* Add DECL to the queue of NSDMI to be parsed later. */
21224 vec_safe_push (unparsed_nsdmis
, decl
);
21227 if (assume_semicolon
)
21232 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21234 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21237 /* Parse a pure-specifier.
21242 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21243 Otherwise, ERROR_MARK_NODE is returned. */
21246 cp_parser_pure_specifier (cp_parser
* parser
)
21250 /* Look for the `=' token. */
21251 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21252 return error_mark_node
;
21253 /* Look for the `0' token. */
21254 token
= cp_lexer_peek_token (parser
->lexer
);
21256 if (token
->type
== CPP_EOF
21257 || token
->type
== CPP_PRAGMA_EOL
)
21258 return error_mark_node
;
21260 cp_lexer_consume_token (parser
->lexer
);
21262 /* Accept = default or = delete in c++0x mode. */
21263 if (token
->keyword
== RID_DEFAULT
21264 || token
->keyword
== RID_DELETE
)
21266 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21267 return token
->u
.value
;
21270 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21271 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21273 cp_parser_error (parser
,
21274 "invalid pure specifier (only %<= 0%> is allowed)");
21275 cp_parser_skip_to_end_of_statement (parser
);
21276 return error_mark_node
;
21278 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21280 error_at (token
->location
, "templates may not be %<virtual%>");
21281 return error_mark_node
;
21284 return integer_zero_node
;
21287 /* Parse a constant-initializer.
21289 constant-initializer:
21290 = constant-expression
21292 Returns a representation of the constant-expression. */
21295 cp_parser_constant_initializer (cp_parser
* parser
)
21297 /* Look for the `=' token. */
21298 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21299 return error_mark_node
;
21301 /* It is invalid to write:
21303 struct S { static const int i = { 7 }; };
21306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21308 cp_parser_error (parser
,
21309 "a brace-enclosed initializer is not allowed here");
21310 /* Consume the opening brace. */
21311 cp_lexer_consume_token (parser
->lexer
);
21312 /* Skip the initializer. */
21313 cp_parser_skip_to_closing_brace (parser
);
21314 /* Look for the trailing `}'. */
21315 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21317 return error_mark_node
;
21320 return cp_parser_constant_expression (parser
);
21323 /* Derived classes [gram.class.derived] */
21325 /* Parse a base-clause.
21328 : base-specifier-list
21330 base-specifier-list:
21331 base-specifier ... [opt]
21332 base-specifier-list , base-specifier ... [opt]
21334 Returns a TREE_LIST representing the base-classes, in the order in
21335 which they were declared. The representation of each node is as
21336 described by cp_parser_base_specifier.
21338 In the case that no bases are specified, this function will return
21339 NULL_TREE, not ERROR_MARK_NODE. */
21342 cp_parser_base_clause (cp_parser
* parser
)
21344 tree bases
= NULL_TREE
;
21346 /* Look for the `:' that begins the list. */
21347 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21349 /* Scan the base-specifier-list. */
21354 bool pack_expansion_p
= false;
21356 /* Look for the base-specifier. */
21357 base
= cp_parser_base_specifier (parser
);
21358 /* Look for the (optional) ellipsis. */
21359 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21361 /* Consume the `...'. */
21362 cp_lexer_consume_token (parser
->lexer
);
21364 pack_expansion_p
= true;
21367 /* Add BASE to the front of the list. */
21368 if (base
&& base
!= error_mark_node
)
21370 if (pack_expansion_p
)
21371 /* Make this a pack expansion type. */
21372 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21374 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21376 TREE_CHAIN (base
) = bases
;
21380 /* Peek at the next token. */
21381 token
= cp_lexer_peek_token (parser
->lexer
);
21382 /* If it's not a comma, then the list is complete. */
21383 if (token
->type
!= CPP_COMMA
)
21385 /* Consume the `,'. */
21386 cp_lexer_consume_token (parser
->lexer
);
21389 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21390 base class had a qualified name. However, the next name that
21391 appears is certainly not qualified. */
21392 parser
->scope
= NULL_TREE
;
21393 parser
->qualifying_scope
= NULL_TREE
;
21394 parser
->object_scope
= NULL_TREE
;
21396 return nreverse (bases
);
21399 /* Parse a base-specifier.
21402 :: [opt] nested-name-specifier [opt] class-name
21403 virtual access-specifier [opt] :: [opt] nested-name-specifier
21405 access-specifier virtual [opt] :: [opt] nested-name-specifier
21408 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21409 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21410 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21411 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21414 cp_parser_base_specifier (cp_parser
* parser
)
21418 bool virtual_p
= false;
21419 bool duplicate_virtual_error_issued_p
= false;
21420 bool duplicate_access_error_issued_p
= false;
21421 bool class_scope_p
, template_p
;
21422 tree access
= access_default_node
;
21425 /* Process the optional `virtual' and `access-specifier'. */
21428 /* Peek at the next token. */
21429 token
= cp_lexer_peek_token (parser
->lexer
);
21430 /* Process `virtual'. */
21431 switch (token
->keyword
)
21434 /* If `virtual' appears more than once, issue an error. */
21435 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21437 cp_parser_error (parser
,
21438 "%<virtual%> specified more than once in base-specified");
21439 duplicate_virtual_error_issued_p
= true;
21444 /* Consume the `virtual' token. */
21445 cp_lexer_consume_token (parser
->lexer
);
21450 case RID_PROTECTED
:
21452 /* If more than one access specifier appears, issue an
21454 if (access
!= access_default_node
21455 && !duplicate_access_error_issued_p
)
21457 cp_parser_error (parser
,
21458 "more than one access specifier in base-specified");
21459 duplicate_access_error_issued_p
= true;
21462 access
= ridpointers
[(int) token
->keyword
];
21464 /* Consume the access-specifier. */
21465 cp_lexer_consume_token (parser
->lexer
);
21474 /* It is not uncommon to see programs mechanically, erroneously, use
21475 the 'typename' keyword to denote (dependent) qualified types
21476 as base classes. */
21477 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21479 token
= cp_lexer_peek_token (parser
->lexer
);
21480 if (!processing_template_decl
)
21481 error_at (token
->location
,
21482 "keyword %<typename%> not allowed outside of templates");
21484 error_at (token
->location
,
21485 "keyword %<typename%> not allowed in this context "
21486 "(the base class is implicitly a type)");
21487 cp_lexer_consume_token (parser
->lexer
);
21490 /* Look for the optional `::' operator. */
21491 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21492 /* Look for the nested-name-specifier. The simplest way to
21497 The keyword `typename' is not permitted in a base-specifier or
21498 mem-initializer; in these contexts a qualified name that
21499 depends on a template-parameter is implicitly assumed to be a
21502 is to pretend that we have seen the `typename' keyword at this
21504 cp_parser_nested_name_specifier_opt (parser
,
21505 /*typename_keyword_p=*/true,
21506 /*check_dependency_p=*/true,
21508 /*is_declaration=*/true);
21509 /* If the base class is given by a qualified name, assume that names
21510 we see are type names or templates, as appropriate. */
21511 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21512 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21515 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21516 /* DR 950 allows decltype as a base-specifier. */
21517 type
= cp_parser_decltype (parser
);
21520 /* Otherwise, look for the class-name. */
21521 type
= cp_parser_class_name (parser
,
21525 /*check_dependency_p=*/true,
21526 /*class_head_p=*/false,
21527 /*is_declaration=*/true);
21528 type
= TREE_TYPE (type
);
21531 if (type
== error_mark_node
)
21532 return error_mark_node
;
21534 return finish_base_specifier (type
, access
, virtual_p
);
21537 /* Exception handling [gram.exception] */
21539 /* Parse an (optional) noexcept-specification.
21541 noexcept-specification:
21542 noexcept ( constant-expression ) [opt]
21544 If no noexcept-specification is present, returns NULL_TREE.
21545 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21546 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21547 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21548 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21549 in which case a boolean condition is returned instead. */
21552 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21553 bool require_constexpr
,
21554 bool* consumed_expr
,
21558 const char *saved_message
;
21560 /* Peek at the next token. */
21561 token
= cp_lexer_peek_token (parser
->lexer
);
21563 /* Is it a noexcept-specification? */
21564 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21567 cp_lexer_consume_token (parser
->lexer
);
21569 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21571 cp_lexer_consume_token (parser
->lexer
);
21573 if (require_constexpr
)
21575 /* Types may not be defined in an exception-specification. */
21576 saved_message
= parser
->type_definition_forbidden_message
;
21577 parser
->type_definition_forbidden_message
21578 = G_("types may not be defined in an exception-specification");
21580 expr
= cp_parser_constant_expression (parser
);
21582 /* Restore the saved message. */
21583 parser
->type_definition_forbidden_message
= saved_message
;
21587 expr
= cp_parser_expression (parser
);
21588 *consumed_expr
= true;
21591 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21595 expr
= boolean_true_node
;
21596 if (!require_constexpr
)
21597 *consumed_expr
= false;
21600 /* We cannot build a noexcept-spec right away because this will check
21601 that expr is a constexpr. */
21603 return build_noexcept_spec (expr
, tf_warning_or_error
);
21611 /* Parse an (optional) exception-specification.
21613 exception-specification:
21614 throw ( type-id-list [opt] )
21616 Returns a TREE_LIST representing the exception-specification. The
21617 TREE_VALUE of each node is a type. */
21620 cp_parser_exception_specification_opt (cp_parser
* parser
)
21624 const char *saved_message
;
21626 /* Peek at the next token. */
21627 token
= cp_lexer_peek_token (parser
->lexer
);
21629 /* Is it a noexcept-specification? */
21630 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21632 if (type_id_list
!= NULL_TREE
)
21633 return type_id_list
;
21635 /* If it's not `throw', then there's no exception-specification. */
21636 if (!cp_parser_is_keyword (token
, RID_THROW
))
21640 /* Enable this once a lot of code has transitioned to noexcept? */
21641 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21642 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21643 "deprecated in C++0x; use %<noexcept%> instead");
21646 /* Consume the `throw'. */
21647 cp_lexer_consume_token (parser
->lexer
);
21649 /* Look for the `('. */
21650 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21652 /* Peek at the next token. */
21653 token
= cp_lexer_peek_token (parser
->lexer
);
21654 /* If it's not a `)', then there is a type-id-list. */
21655 if (token
->type
!= CPP_CLOSE_PAREN
)
21657 /* Types may not be defined in an exception-specification. */
21658 saved_message
= parser
->type_definition_forbidden_message
;
21659 parser
->type_definition_forbidden_message
21660 = G_("types may not be defined in an exception-specification");
21661 /* Parse the type-id-list. */
21662 type_id_list
= cp_parser_type_id_list (parser
);
21663 /* Restore the saved message. */
21664 parser
->type_definition_forbidden_message
= saved_message
;
21667 type_id_list
= empty_except_spec
;
21669 /* Look for the `)'. */
21670 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21672 return type_id_list
;
21675 /* Parse an (optional) type-id-list.
21679 type-id-list , type-id ... [opt]
21681 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21682 in the order that the types were presented. */
21685 cp_parser_type_id_list (cp_parser
* parser
)
21687 tree types
= NULL_TREE
;
21694 /* Get the next type-id. */
21695 type
= cp_parser_type_id (parser
);
21696 /* Parse the optional ellipsis. */
21697 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21699 /* Consume the `...'. */
21700 cp_lexer_consume_token (parser
->lexer
);
21702 /* Turn the type into a pack expansion expression. */
21703 type
= make_pack_expansion (type
);
21705 /* Add it to the list. */
21706 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21707 /* Peek at the next token. */
21708 token
= cp_lexer_peek_token (parser
->lexer
);
21709 /* If it is not a `,', we are done. */
21710 if (token
->type
!= CPP_COMMA
)
21712 /* Consume the `,'. */
21713 cp_lexer_consume_token (parser
->lexer
);
21716 return nreverse (types
);
21719 /* Parse a try-block.
21722 try compound-statement handler-seq */
21725 cp_parser_try_block (cp_parser
* parser
)
21729 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21730 if (parser
->in_function_body
21731 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21732 error ("%<try%> in %<constexpr%> function");
21734 try_block
= begin_try_block ();
21735 cp_parser_compound_statement (parser
, NULL
, true, false);
21736 finish_try_block (try_block
);
21737 cp_parser_handler_seq (parser
);
21738 finish_handler_sequence (try_block
);
21743 /* Parse a function-try-block.
21745 function-try-block:
21746 try ctor-initializer [opt] function-body handler-seq */
21749 cp_parser_function_try_block (cp_parser
* parser
)
21751 tree compound_stmt
;
21753 bool ctor_initializer_p
;
21755 /* Look for the `try' keyword. */
21756 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21758 /* Let the rest of the front end know where we are. */
21759 try_block
= begin_function_try_block (&compound_stmt
);
21760 /* Parse the function-body. */
21761 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21762 (parser
, /*in_function_try_block=*/true);
21763 /* We're done with the `try' part. */
21764 finish_function_try_block (try_block
);
21765 /* Parse the handlers. */
21766 cp_parser_handler_seq (parser
);
21767 /* We're done with the handlers. */
21768 finish_function_handler_sequence (try_block
, compound_stmt
);
21770 return ctor_initializer_p
;
21773 /* Parse a handler-seq.
21776 handler handler-seq [opt] */
21779 cp_parser_handler_seq (cp_parser
* parser
)
21785 /* Parse the handler. */
21786 cp_parser_handler (parser
);
21787 /* Peek at the next token. */
21788 token
= cp_lexer_peek_token (parser
->lexer
);
21789 /* If it's not `catch' then there are no more handlers. */
21790 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21795 /* Parse a handler.
21798 catch ( exception-declaration ) compound-statement */
21801 cp_parser_handler (cp_parser
* parser
)
21806 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21807 handler
= begin_handler ();
21808 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21809 declaration
= cp_parser_exception_declaration (parser
);
21810 finish_handler_parms (declaration
, handler
);
21811 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21812 cp_parser_compound_statement (parser
, NULL
, false, false);
21813 finish_handler (handler
);
21816 /* Parse an exception-declaration.
21818 exception-declaration:
21819 type-specifier-seq declarator
21820 type-specifier-seq abstract-declarator
21824 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21825 ellipsis variant is used. */
21828 cp_parser_exception_declaration (cp_parser
* parser
)
21830 cp_decl_specifier_seq type_specifiers
;
21831 cp_declarator
*declarator
;
21832 const char *saved_message
;
21834 /* If it's an ellipsis, it's easy to handle. */
21835 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21837 /* Consume the `...' token. */
21838 cp_lexer_consume_token (parser
->lexer
);
21842 /* Types may not be defined in exception-declarations. */
21843 saved_message
= parser
->type_definition_forbidden_message
;
21844 parser
->type_definition_forbidden_message
21845 = G_("types may not be defined in exception-declarations");
21847 /* Parse the type-specifier-seq. */
21848 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21849 /*is_trailing_return=*/false,
21851 /* If it's a `)', then there is no declarator. */
21852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21855 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21856 /*ctor_dtor_or_conv_p=*/NULL
,
21857 /*parenthesized_p=*/NULL
,
21858 /*member_p=*/false,
21859 /*friend_p=*/false);
21861 /* Restore the saved message. */
21862 parser
->type_definition_forbidden_message
= saved_message
;
21864 if (!type_specifiers
.any_specifiers_p
)
21865 return error_mark_node
;
21867 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21870 /* Parse a throw-expression.
21873 throw assignment-expression [opt]
21875 Returns a THROW_EXPR representing the throw-expression. */
21878 cp_parser_throw_expression (cp_parser
* parser
)
21883 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21884 token
= cp_lexer_peek_token (parser
->lexer
);
21885 /* Figure out whether or not there is an assignment-expression
21886 following the "throw" keyword. */
21887 if (token
->type
== CPP_COMMA
21888 || token
->type
== CPP_SEMICOLON
21889 || token
->type
== CPP_CLOSE_PAREN
21890 || token
->type
== CPP_CLOSE_SQUARE
21891 || token
->type
== CPP_CLOSE_BRACE
21892 || token
->type
== CPP_COLON
)
21893 expression
= NULL_TREE
;
21895 expression
= cp_parser_assignment_expression (parser
);
21897 return build_throw (expression
);
21900 /* GNU Extensions */
21902 /* Parse an (optional) asm-specification.
21905 asm ( string-literal )
21907 If the asm-specification is present, returns a STRING_CST
21908 corresponding to the string-literal. Otherwise, returns
21912 cp_parser_asm_specification_opt (cp_parser
* parser
)
21915 tree asm_specification
;
21917 /* Peek at the next token. */
21918 token
= cp_lexer_peek_token (parser
->lexer
);
21919 /* If the next token isn't the `asm' keyword, then there's no
21920 asm-specification. */
21921 if (!cp_parser_is_keyword (token
, RID_ASM
))
21924 /* Consume the `asm' token. */
21925 cp_lexer_consume_token (parser
->lexer
);
21926 /* Look for the `('. */
21927 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21929 /* Look for the string-literal. */
21930 asm_specification
= cp_parser_string_literal (parser
, false, false);
21932 /* Look for the `)'. */
21933 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21935 return asm_specification
;
21938 /* Parse an asm-operand-list.
21942 asm-operand-list , asm-operand
21945 string-literal ( expression )
21946 [ string-literal ] string-literal ( expression )
21948 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21949 each node is the expression. The TREE_PURPOSE is itself a
21950 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21951 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21952 is a STRING_CST for the string literal before the parenthesis. Returns
21953 ERROR_MARK_NODE if any of the operands are invalid. */
21956 cp_parser_asm_operand_list (cp_parser
* parser
)
21958 tree asm_operands
= NULL_TREE
;
21959 bool invalid_operands
= false;
21963 tree string_literal
;
21967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21969 /* Consume the `[' token. */
21970 cp_lexer_consume_token (parser
->lexer
);
21971 /* Read the operand name. */
21972 name
= cp_parser_identifier (parser
);
21973 if (name
!= error_mark_node
)
21974 name
= build_string (IDENTIFIER_LENGTH (name
),
21975 IDENTIFIER_POINTER (name
));
21976 /* Look for the closing `]'. */
21977 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21981 /* Look for the string-literal. */
21982 string_literal
= cp_parser_string_literal (parser
, false, false);
21984 /* Look for the `('. */
21985 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21986 /* Parse the expression. */
21987 expression
= cp_parser_expression (parser
);
21988 /* Look for the `)'. */
21989 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21991 if (name
== error_mark_node
21992 || string_literal
== error_mark_node
21993 || expression
== error_mark_node
)
21994 invalid_operands
= true;
21996 /* Add this operand to the list. */
21997 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
22000 /* If the next token is not a `,', there are no more
22002 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22004 /* Consume the `,'. */
22005 cp_lexer_consume_token (parser
->lexer
);
22008 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
22011 /* Parse an asm-clobber-list.
22015 asm-clobber-list , string-literal
22017 Returns a TREE_LIST, indicating the clobbers in the order that they
22018 appeared. The TREE_VALUE of each node is a STRING_CST. */
22021 cp_parser_asm_clobber_list (cp_parser
* parser
)
22023 tree clobbers
= NULL_TREE
;
22027 tree string_literal
;
22029 /* Look for the string literal. */
22030 string_literal
= cp_parser_string_literal (parser
, false, false);
22031 /* Add it to the list. */
22032 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
22033 /* If the next token is not a `,', then the list is
22035 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22037 /* Consume the `,' token. */
22038 cp_lexer_consume_token (parser
->lexer
);
22044 /* Parse an asm-label-list.
22048 asm-label-list , identifier
22050 Returns a TREE_LIST, indicating the labels in the order that they
22051 appeared. The TREE_VALUE of each node is a label. */
22054 cp_parser_asm_label_list (cp_parser
* parser
)
22056 tree labels
= NULL_TREE
;
22060 tree identifier
, label
, name
;
22062 /* Look for the identifier. */
22063 identifier
= cp_parser_identifier (parser
);
22064 if (!error_operand_p (identifier
))
22066 label
= lookup_label (identifier
);
22067 if (TREE_CODE (label
) == LABEL_DECL
)
22069 TREE_USED (label
) = 1;
22070 check_goto (label
);
22071 name
= build_string (IDENTIFIER_LENGTH (identifier
),
22072 IDENTIFIER_POINTER (identifier
));
22073 labels
= tree_cons (name
, label
, labels
);
22076 /* If the next token is not a `,', then the list is
22078 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22080 /* Consume the `,' token. */
22081 cp_lexer_consume_token (parser
->lexer
);
22084 return nreverse (labels
);
22087 /* Return TRUE iff the next tokens in the stream are possibly the
22088 beginning of a GNU extension attribute. */
22091 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
22093 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
22096 /* Return TRUE iff the next tokens in the stream are possibly the
22097 beginning of a standard C++-11 attribute specifier. */
22100 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
22102 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
22105 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22106 beginning of a standard C++-11 attribute specifier. */
22109 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
22111 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22113 return (cxx_dialect
>= cxx11
22114 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
22115 || (token
->type
== CPP_OPEN_SQUARE
22116 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
22117 && token
->type
== CPP_OPEN_SQUARE
)));
22120 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22121 beginning of a GNU extension attribute. */
22124 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
22126 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22128 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
22131 /* Return true iff the next tokens can be the beginning of either a
22132 GNU attribute list, or a standard C++11 attribute sequence. */
22135 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
22137 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
22138 || cp_next_tokens_can_be_std_attribute_p (parser
));
22141 /* Return true iff the next Nth tokens can be the beginning of either
22142 a GNU attribute list, or a standard C++11 attribute sequence. */
22145 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
22147 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
22148 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
22151 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22152 of GNU attributes, or return NULL. */
22155 cp_parser_attributes_opt (cp_parser
*parser
)
22157 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
22158 return cp_parser_gnu_attributes_opt (parser
);
22159 return cp_parser_std_attribute_spec_seq (parser
);
22162 #define CILK_SIMD_FN_CLAUSE_MASK \
22163 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22164 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22165 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22166 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22167 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22169 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22170 vector [(<clauses>)] */
22173 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22175 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22176 cp_token
*token
= v_token
;
22179 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22180 parser
->cilk_simd_fn_info
->error_seen
= false;
22181 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22182 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22184 int paren_scope
= 0;
22185 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22187 cp_lexer_consume_token (parser
->lexer
);
22188 v_token
= cp_lexer_peek_token (parser
->lexer
);
22191 while (paren_scope
> 0)
22193 token
= cp_lexer_peek_token (parser
->lexer
);
22194 if (token
->type
== CPP_OPEN_PAREN
)
22196 else if (token
->type
== CPP_CLOSE_PAREN
)
22198 /* Do not push the last ')' */
22199 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22200 cp_lexer_consume_token (parser
->lexer
);
22203 token
->type
= CPP_PRAGMA_EOL
;
22204 parser
->lexer
->next_token
= token
;
22205 cp_lexer_consume_token (parser
->lexer
);
22207 struct cp_token_cache
*cp
22208 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22209 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22212 /* Parse an (optional) series of attributes.
22215 attributes attribute
22218 __attribute__ (( attribute-list [opt] ))
22220 The return value is as for cp_parser_gnu_attribute_list. */
22223 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22225 tree attributes
= NULL_TREE
;
22230 tree attribute_list
;
22233 /* Peek at the next token. */
22234 token
= cp_lexer_peek_token (parser
->lexer
);
22235 /* If it's not `__attribute__', then we're done. */
22236 if (token
->keyword
!= RID_ATTRIBUTE
)
22239 /* Consume the `__attribute__' keyword. */
22240 cp_lexer_consume_token (parser
->lexer
);
22241 /* Look for the two `(' tokens. */
22242 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22243 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22245 /* Peek at the next token. */
22246 token
= cp_lexer_peek_token (parser
->lexer
);
22247 if (token
->type
!= CPP_CLOSE_PAREN
)
22248 /* Parse the attribute-list. */
22249 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22251 /* If the next token is a `)', then there is no attribute
22253 attribute_list
= NULL
;
22255 /* Look for the two `)' tokens. */
22256 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22258 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22261 cp_parser_skip_to_end_of_statement (parser
);
22263 /* Add these new attributes to the list. */
22264 attributes
= chainon (attributes
, attribute_list
);
22270 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22271 "__vector" or "__vector__." */
22274 is_cilkplus_vector_p (tree name
)
22276 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22281 /* Parse a GNU attribute-list.
22285 attribute-list , attribute
22289 identifier ( identifier )
22290 identifier ( identifier , expression-list )
22291 identifier ( expression-list )
22293 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22294 to an attribute. The TREE_PURPOSE of each node is the identifier
22295 indicating which attribute is in use. The TREE_VALUE represents
22296 the arguments, if any. */
22299 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22301 tree attribute_list
= NULL_TREE
;
22302 bool save_translate_strings_p
= parser
->translate_strings_p
;
22304 parser
->translate_strings_p
= false;
22311 /* Look for the identifier. We also allow keywords here; for
22312 example `__attribute__ ((const))' is legal. */
22313 token
= cp_lexer_peek_token (parser
->lexer
);
22314 if (token
->type
== CPP_NAME
22315 || token
->type
== CPP_KEYWORD
)
22317 tree arguments
= NULL_TREE
;
22319 /* Consume the token, but save it since we need it for the
22320 SIMD enabled function parsing. */
22321 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22323 /* Save away the identifier that indicates which attribute
22325 identifier
= (token
->type
== CPP_KEYWORD
)
22326 /* For keywords, use the canonical spelling, not the
22327 parsed identifier. */
22328 ? ridpointers
[(int) token
->keyword
]
22329 : id_token
->u
.value
;
22331 attribute
= build_tree_list (identifier
, NULL_TREE
);
22333 /* Peek at the next token. */
22334 token
= cp_lexer_peek_token (parser
->lexer
);
22335 /* If it's an `(', then parse the attribute arguments. */
22336 if (token
->type
== CPP_OPEN_PAREN
)
22338 vec
<tree
, va_gc
> *vec
;
22339 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22340 ? id_attr
: normal_attr
);
22341 if (is_cilkplus_vector_p (identifier
))
22343 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22347 vec
= cp_parser_parenthesized_expression_list
22348 (parser
, attr_flag
, /*cast_p=*/false,
22349 /*allow_expansion_p=*/false,
22350 /*non_constant_p=*/NULL
);
22352 arguments
= error_mark_node
;
22355 arguments
= build_tree_list_vec (vec
);
22356 release_tree_vector (vec
);
22358 /* Save the arguments away. */
22359 TREE_VALUE (attribute
) = arguments
;
22361 else if (is_cilkplus_vector_p (identifier
))
22363 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22367 if (arguments
!= error_mark_node
)
22369 /* Add this attribute to the list. */
22370 TREE_CHAIN (attribute
) = attribute_list
;
22371 attribute_list
= attribute
;
22374 token
= cp_lexer_peek_token (parser
->lexer
);
22376 /* Now, look for more attributes. If the next token isn't a
22377 `,', we're done. */
22378 if (token
->type
!= CPP_COMMA
)
22381 /* Consume the comma and keep going. */
22382 cp_lexer_consume_token (parser
->lexer
);
22384 parser
->translate_strings_p
= save_translate_strings_p
;
22386 /* We built up the list in reverse order. */
22387 return nreverse (attribute_list
);
22390 /* Parse a standard C++11 attribute.
22392 The returned representation is a TREE_LIST which TREE_PURPOSE is
22393 the scoped name of the attribute, and the TREE_VALUE is its
22396 Note that the scoped name of the attribute is itself a TREE_LIST
22397 which TREE_PURPOSE is the namespace of the attribute, and
22398 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22399 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22400 and which TREE_PURPOSE is directly the attribute name.
22402 Clients of the attribute code should use get_attribute_namespace
22403 and get_attribute_name to get the actual namespace and name of
22404 attributes, regardless of their being GNU or C++11 attributes.
22407 attribute-token attribute-argument-clause [opt]
22411 attribute-scoped-token
22413 attribute-scoped-token:
22414 attribute-namespace :: identifier
22416 attribute-namespace:
22419 attribute-argument-clause:
22420 ( balanced-token-seq )
22422 balanced-token-seq:
22423 balanced-token [opt]
22424 balanced-token-seq balanced-token
22427 ( balanced-token-seq )
22428 [ balanced-token-seq ]
22429 { balanced-token-seq }. */
22432 cp_parser_std_attribute (cp_parser
*parser
)
22434 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22437 /* First, parse name of the attribute, a.k.a attribute-token. */
22439 token
= cp_lexer_peek_token (parser
->lexer
);
22440 if (token
->type
== CPP_NAME
)
22441 attr_id
= token
->u
.value
;
22442 else if (token
->type
== CPP_KEYWORD
)
22443 attr_id
= ridpointers
[(int) token
->keyword
];
22444 else if (token
->flags
& NAMED_OP
)
22445 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22447 if (attr_id
== NULL_TREE
)
22450 cp_lexer_consume_token (parser
->lexer
);
22452 token
= cp_lexer_peek_token (parser
->lexer
);
22453 if (token
->type
== CPP_SCOPE
)
22455 /* We are seeing a scoped attribute token. */
22457 cp_lexer_consume_token (parser
->lexer
);
22460 token
= cp_lexer_consume_token (parser
->lexer
);
22461 if (token
->type
== CPP_NAME
)
22462 attr_id
= token
->u
.value
;
22463 else if (token
->type
== CPP_KEYWORD
)
22464 attr_id
= ridpointers
[(int) token
->keyword
];
22467 error_at (token
->location
,
22468 "expected an identifier for the attribute name");
22469 return error_mark_node
;
22471 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22473 token
= cp_lexer_peek_token (parser
->lexer
);
22477 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22479 /* C++11 noreturn attribute is equivalent to GNU's. */
22480 if (is_attribute_p ("noreturn", attr_id
))
22481 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22482 /* C++14 deprecated attribute is equivalent to GNU's. */
22483 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22485 if (cxx_dialect
== cxx11
)
22486 pedwarn (token
->location
, OPT_Wpedantic
,
22487 "%<deprecated%> is a C++14 feature;"
22488 " use %<gnu::deprecated%>");
22489 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22493 /* Now parse the optional argument clause of the attribute. */
22495 if (token
->type
!= CPP_OPEN_PAREN
)
22499 vec
<tree
, va_gc
> *vec
;
22500 int attr_flag
= normal_attr
;
22502 if (attr_ns
== get_identifier ("gnu")
22503 && attribute_takes_identifier_p (attr_id
))
22504 /* A GNU attribute that takes an identifier in parameter. */
22505 attr_flag
= id_attr
;
22507 vec
= cp_parser_parenthesized_expression_list
22508 (parser
, attr_flag
, /*cast_p=*/false,
22509 /*allow_expansion_p=*/true,
22510 /*non_constant_p=*/NULL
);
22512 arguments
= error_mark_node
;
22515 arguments
= build_tree_list_vec (vec
);
22516 release_tree_vector (vec
);
22519 if (arguments
== error_mark_node
)
22520 attribute
= error_mark_node
;
22522 TREE_VALUE (attribute
) = arguments
;
22528 /* Check that the attribute ATTRIBUTE appears at most once in the
22529 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
22530 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
22531 isn't implemented yet in GCC. */
22534 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
22538 tree name
= get_attribute_name (attribute
);
22539 if (is_attribute_p ("noreturn", name
)
22540 && lookup_attribute ("noreturn", attributes
))
22541 error ("attribute noreturn can appear at most once "
22542 "in an attribute-list");
22543 else if (is_attribute_p ("deprecated", name
)
22544 && lookup_attribute ("deprecated", attributes
))
22545 error ("attribute deprecated can appear at most once "
22546 "in an attribute-list");
22550 /* Parse a list of standard C++-11 attributes.
22554 attribute-list , attribute[opt]
22556 attribute-list , attribute ...
22560 cp_parser_std_attribute_list (cp_parser
*parser
)
22562 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22563 cp_token
*token
= NULL
;
22567 attribute
= cp_parser_std_attribute (parser
);
22568 if (attribute
== error_mark_node
)
22570 if (attribute
!= NULL_TREE
)
22572 cp_parser_check_std_attribute (attributes
, attribute
);
22573 TREE_CHAIN (attribute
) = attributes
;
22574 attributes
= attribute
;
22576 token
= cp_lexer_peek_token (parser
->lexer
);
22577 if (token
->type
== CPP_ELLIPSIS
)
22579 cp_lexer_consume_token (parser
->lexer
);
22580 TREE_VALUE (attribute
)
22581 = make_pack_expansion (TREE_VALUE (attribute
));
22582 token
= cp_lexer_peek_token (parser
->lexer
);
22584 if (token
->type
!= CPP_COMMA
)
22586 cp_lexer_consume_token (parser
->lexer
);
22588 attributes
= nreverse (attributes
);
22592 /* Parse a standard C++-11 attribute specifier.
22594 attribute-specifier:
22595 [ [ attribute-list ] ]
22596 alignment-specifier
22598 alignment-specifier:
22599 alignas ( type-id ... [opt] )
22600 alignas ( alignment-expression ... [opt] ). */
22603 cp_parser_std_attribute_spec (cp_parser
*parser
)
22605 tree attributes
= NULL_TREE
;
22606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22608 if (token
->type
== CPP_OPEN_SQUARE
22609 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22611 cp_lexer_consume_token (parser
->lexer
);
22612 cp_lexer_consume_token (parser
->lexer
);
22614 attributes
= cp_parser_std_attribute_list (parser
);
22616 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22617 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22618 cp_parser_skip_to_end_of_statement (parser
);
22620 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22621 when we are sure that we have actually parsed them. */
22622 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22628 /* Look for an alignment-specifier. */
22630 token
= cp_lexer_peek_token (parser
->lexer
);
22632 if (token
->type
!= CPP_KEYWORD
22633 || token
->keyword
!= RID_ALIGNAS
)
22636 cp_lexer_consume_token (parser
->lexer
);
22637 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22639 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22641 cp_parser_error (parser
, "expected %<(%>");
22642 return error_mark_node
;
22645 cp_parser_parse_tentatively (parser
);
22646 alignas_expr
= cp_parser_type_id (parser
);
22648 if (!cp_parser_parse_definitely (parser
))
22650 gcc_assert (alignas_expr
== error_mark_node
22651 || alignas_expr
== NULL_TREE
);
22654 cp_parser_assignment_expression (parser
);
22655 if (alignas_expr
== error_mark_node
)
22656 cp_parser_skip_to_end_of_statement (parser
);
22657 if (alignas_expr
== NULL_TREE
22658 || alignas_expr
== error_mark_node
)
22659 return alignas_expr
;
22662 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22663 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
22665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22667 cp_lexer_consume_token (parser
->lexer
);
22668 alignas_expr
= make_pack_expansion (alignas_expr
);
22671 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22673 cp_parser_error (parser
, "expected %<)%>");
22674 return error_mark_node
;
22677 /* Build the C++-11 representation of an 'aligned'
22680 build_tree_list (build_tree_list (get_identifier ("gnu"),
22681 get_identifier ("aligned")),
22688 /* Parse a standard C++-11 attribute-specifier-seq.
22690 attribute-specifier-seq:
22691 attribute-specifier-seq [opt] attribute-specifier
22695 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22697 tree attr_specs
= NULL
;
22701 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22702 if (attr_spec
== NULL_TREE
)
22704 if (attr_spec
== error_mark_node
)
22705 return error_mark_node
;
22707 TREE_CHAIN (attr_spec
) = attr_specs
;
22708 attr_specs
= attr_spec
;
22711 attr_specs
= nreverse (attr_specs
);
22715 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22716 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22717 current value of the PEDANTIC flag, regardless of whether or not
22718 the `__extension__' keyword is present. The caller is responsible
22719 for restoring the value of the PEDANTIC flag. */
22722 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22724 /* Save the old value of the PEDANTIC flag. */
22725 *saved_pedantic
= pedantic
;
22727 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22729 /* Consume the `__extension__' token. */
22730 cp_lexer_consume_token (parser
->lexer
);
22731 /* We're not being pedantic while the `__extension__' keyword is
22741 /* Parse a label declaration.
22744 __label__ label-declarator-seq ;
22746 label-declarator-seq:
22747 identifier , label-declarator-seq
22751 cp_parser_label_declaration (cp_parser
* parser
)
22753 /* Look for the `__label__' keyword. */
22754 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22760 /* Look for an identifier. */
22761 identifier
= cp_parser_identifier (parser
);
22762 /* If we failed, stop. */
22763 if (identifier
== error_mark_node
)
22765 /* Declare it as a label. */
22766 finish_label_decl (identifier
);
22767 /* If the next token is a `;', stop. */
22768 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22770 /* Look for the `,' separating the label declarations. */
22771 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22774 /* Look for the final `;'. */
22775 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22778 /* Support Functions */
22780 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22781 NAME should have one of the representations used for an
22782 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22783 is returned. If PARSER->SCOPE is a dependent type, then a
22784 SCOPE_REF is returned.
22786 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22787 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22788 was formed. Abstractly, such entities should not be passed to this
22789 function, because they do not need to be looked up, but it is
22790 simpler to check for this special case here, rather than at the
22793 In cases not explicitly covered above, this function returns a
22794 DECL, OVERLOAD, or baselink representing the result of the lookup.
22795 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22798 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22799 (e.g., "struct") that was used. In that case bindings that do not
22800 refer to types are ignored.
22802 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22805 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22808 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22811 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22812 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22813 NULL_TREE otherwise. */
22816 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22817 enum tag_types tag_type
,
22820 bool check_dependency
,
22821 tree
*ambiguous_decls
,
22822 location_t name_location
)
22825 tree object_type
= parser
->context
->object_type
;
22827 /* Assume that the lookup will be unambiguous. */
22828 if (ambiguous_decls
)
22829 *ambiguous_decls
= NULL_TREE
;
22831 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22832 no longer valid. Note that if we are parsing tentatively, and
22833 the parse fails, OBJECT_TYPE will be automatically restored. */
22834 parser
->context
->object_type
= NULL_TREE
;
22836 if (name
== error_mark_node
)
22837 return error_mark_node
;
22839 /* A template-id has already been resolved; there is no lookup to
22841 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22843 if (BASELINK_P (name
))
22845 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22846 == TEMPLATE_ID_EXPR
);
22850 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22851 it should already have been checked to make sure that the name
22852 used matches the type being destroyed. */
22853 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22857 /* Figure out to which type this destructor applies. */
22859 type
= parser
->scope
;
22860 else if (object_type
)
22861 type
= object_type
;
22863 type
= current_class_type
;
22864 /* If that's not a class type, there is no destructor. */
22865 if (!type
|| !CLASS_TYPE_P (type
))
22866 return error_mark_node
;
22867 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22868 lazily_declare_fn (sfk_destructor
, type
);
22869 if (!CLASSTYPE_DESTRUCTORS (type
))
22870 return error_mark_node
;
22871 /* If it was a class type, return the destructor. */
22872 return CLASSTYPE_DESTRUCTORS (type
);
22875 /* By this point, the NAME should be an ordinary identifier. If
22876 the id-expression was a qualified name, the qualifying scope is
22877 stored in PARSER->SCOPE at this point. */
22878 gcc_assert (identifier_p (name
));
22880 /* Perform the lookup. */
22885 if (parser
->scope
== error_mark_node
)
22886 return error_mark_node
;
22888 /* If the SCOPE is dependent, the lookup must be deferred until
22889 the template is instantiated -- unless we are explicitly
22890 looking up names in uninstantiated templates. Even then, we
22891 cannot look up the name if the scope is not a class type; it
22892 might, for example, be a template type parameter. */
22893 dependent_p
= (TYPE_P (parser
->scope
)
22894 && dependent_scope_p (parser
->scope
));
22895 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22897 /* Defer lookup. */
22898 decl
= error_mark_node
;
22901 tree pushed_scope
= NULL_TREE
;
22903 /* If PARSER->SCOPE is a dependent type, then it must be a
22904 class type, and we must not be checking dependencies;
22905 otherwise, we would have processed this lookup above. So
22906 that PARSER->SCOPE is not considered a dependent base by
22907 lookup_member, we must enter the scope here. */
22909 pushed_scope
= push_scope (parser
->scope
);
22911 /* If the PARSER->SCOPE is a template specialization, it
22912 may be instantiated during name lookup. In that case,
22913 errors may be issued. Even if we rollback the current
22914 tentative parse, those errors are valid. */
22915 decl
= lookup_qualified_name (parser
->scope
, name
,
22916 tag_type
!= none_type
,
22917 /*complain=*/true);
22919 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22920 lookup result and the nested-name-specifier nominates a class C:
22921 * if the name specified after the nested-name-specifier, when
22922 looked up in C, is the injected-class-name of C (Clause 9), or
22923 * if the name specified after the nested-name-specifier is the
22924 same as the identifier or the simple-template-id's template-
22925 name in the last component of the nested-name-specifier,
22926 the name is instead considered to name the constructor of
22927 class C. [ Note: for example, the constructor is not an
22928 acceptable lookup result in an elaborated-type-specifier so
22929 the constructor would not be used in place of the
22930 injected-class-name. --end note ] Such a constructor name
22931 shall be used only in the declarator-id of a declaration that
22932 names a constructor or in a using-declaration. */
22933 if (tag_type
== none_type
22934 && DECL_SELF_REFERENCE_P (decl
)
22935 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22936 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22937 tag_type
!= none_type
,
22938 /*complain=*/true);
22940 /* If we have a single function from a using decl, pull it out. */
22941 if (TREE_CODE (decl
) == OVERLOAD
22942 && !really_overloaded_fn (decl
))
22943 decl
= OVL_FUNCTION (decl
);
22946 pop_scope (pushed_scope
);
22949 /* If the scope is a dependent type and either we deferred lookup or
22950 we did lookup but didn't find the name, rememeber the name. */
22951 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22952 && dependent_type_p (parser
->scope
))
22958 /* The resolution to Core Issue 180 says that `struct
22959 A::B' should be considered a type-name, even if `A'
22961 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22962 /*complain=*/tf_error
);
22963 if (type
!= error_mark_node
)
22964 decl
= TYPE_NAME (type
);
22966 else if (is_template
22967 && (cp_parser_next_token_ends_template_argument_p (parser
)
22968 || cp_lexer_next_token_is (parser
->lexer
,
22970 decl
= make_unbound_class_template (parser
->scope
,
22972 /*complain=*/tf_error
);
22974 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22975 parser
->scope
, name
,
22978 parser
->qualifying_scope
= parser
->scope
;
22979 parser
->object_scope
= NULL_TREE
;
22981 else if (object_type
)
22983 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22984 OBJECT_TYPE is not a class. */
22985 if (CLASS_TYPE_P (object_type
))
22986 /* If the OBJECT_TYPE is a template specialization, it may
22987 be instantiated during name lookup. In that case, errors
22988 may be issued. Even if we rollback the current tentative
22989 parse, those errors are valid. */
22990 decl
= lookup_member (object_type
,
22993 tag_type
!= none_type
,
22994 tf_warning_or_error
);
22999 /* Look it up in the enclosing context. */
23000 decl
= lookup_name_real (name
, tag_type
!= none_type
,
23002 /*block_p=*/true, is_namespace
, 0);
23003 parser
->object_scope
= object_type
;
23004 parser
->qualifying_scope
= NULL_TREE
;
23008 decl
= lookup_name_real (name
, tag_type
!= none_type
,
23010 /*block_p=*/true, is_namespace
, 0);
23011 parser
->qualifying_scope
= NULL_TREE
;
23012 parser
->object_scope
= NULL_TREE
;
23015 /* If the lookup failed, let our caller know. */
23016 if (!decl
|| decl
== error_mark_node
)
23017 return error_mark_node
;
23019 /* Pull out the template from an injected-class-name (or multiple). */
23021 decl
= maybe_get_template_decl_from_type_decl (decl
);
23023 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
23024 if (TREE_CODE (decl
) == TREE_LIST
)
23026 if (ambiguous_decls
)
23027 *ambiguous_decls
= decl
;
23028 /* The error message we have to print is too complicated for
23029 cp_parser_error, so we incorporate its actions directly. */
23030 if (!cp_parser_simulate_error (parser
))
23032 error_at (name_location
, "reference to %qD is ambiguous",
23034 print_candidates (decl
);
23036 return error_mark_node
;
23039 gcc_assert (DECL_P (decl
)
23040 || TREE_CODE (decl
) == OVERLOAD
23041 || TREE_CODE (decl
) == SCOPE_REF
23042 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
23043 || BASELINK_P (decl
));
23045 /* If we have resolved the name of a member declaration, check to
23046 see if the declaration is accessible. When the name resolves to
23047 set of overloaded functions, accessibility is checked when
23048 overload resolution is done.
23050 During an explicit instantiation, access is not checked at all,
23051 as per [temp.explicit]. */
23053 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
23055 maybe_record_typedef_use (decl
);
23060 /* Like cp_parser_lookup_name, but for use in the typical case where
23061 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23062 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
23065 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
23067 return cp_parser_lookup_name (parser
, name
,
23069 /*is_template=*/false,
23070 /*is_namespace=*/false,
23071 /*check_dependency=*/true,
23072 /*ambiguous_decls=*/NULL
,
23076 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23077 the current context, return the TYPE_DECL. If TAG_NAME_P is
23078 true, the DECL indicates the class being defined in a class-head,
23079 or declared in an elaborated-type-specifier.
23081 Otherwise, return DECL. */
23084 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
23086 /* If the TEMPLATE_DECL is being declared as part of a class-head,
23087 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23090 template <typename T> struct B;
23093 template <typename T> struct A::B {};
23095 Similarly, in an elaborated-type-specifier:
23097 namespace N { struct X{}; }
23100 template <typename T> friend struct N::X;
23103 However, if the DECL refers to a class type, and we are in
23104 the scope of the class, then the name lookup automatically
23105 finds the TYPE_DECL created by build_self_reference rather
23106 than a TEMPLATE_DECL. For example, in:
23108 template <class T> struct S {
23112 there is no need to handle such case. */
23114 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
23115 return DECL_TEMPLATE_RESULT (decl
);
23120 /* If too many, or too few, template-parameter lists apply to the
23121 declarator, issue an error message. Returns TRUE if all went well,
23122 and FALSE otherwise. */
23125 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
23126 cp_declarator
*declarator
,
23127 location_t declarator_location
)
23129 switch (declarator
->kind
)
23133 unsigned num_templates
= 0;
23134 tree scope
= declarator
->u
.id
.qualifying_scope
;
23137 num_templates
= num_template_headers_for_class (scope
);
23138 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
23139 == TEMPLATE_ID_EXPR
)
23140 /* If the DECLARATOR has the form `X<y>' then it uses one
23141 additional level of template parameters. */
23144 return cp_parser_check_template_parameters
23145 (parser
, num_templates
, declarator_location
, declarator
);
23151 case cdk_reference
:
23153 return (cp_parser_check_declarator_template_parameters
23154 (parser
, declarator
->declarator
, declarator_location
));
23160 gcc_unreachable ();
23165 /* NUM_TEMPLATES were used in the current declaration. If that is
23166 invalid, return FALSE and issue an error messages. Otherwise,
23167 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23168 declarator and we can print more accurate diagnostics. */
23171 cp_parser_check_template_parameters (cp_parser
* parser
,
23172 unsigned num_templates
,
23173 location_t location
,
23174 cp_declarator
*declarator
)
23176 /* If there are the same number of template classes and parameter
23177 lists, that's OK. */
23178 if (parser
->num_template_parameter_lists
== num_templates
)
23180 /* If there are more, but only one more, then we are referring to a
23181 member template. That's OK too. */
23182 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
23184 /* If there are more template classes than parameter lists, we have
23187 template <class T> void S<T>::R<T>::f (); */
23188 if (parser
->num_template_parameter_lists
< num_templates
)
23190 if (declarator
&& !current_function_decl
)
23191 error_at (location
, "specializing member %<%T::%E%> "
23192 "requires %<template<>%> syntax",
23193 declarator
->u
.id
.qualifying_scope
,
23194 declarator
->u
.id
.unqualified_name
);
23195 else if (declarator
)
23196 error_at (location
, "invalid declaration of %<%T::%E%>",
23197 declarator
->u
.id
.qualifying_scope
,
23198 declarator
->u
.id
.unqualified_name
);
23200 error_at (location
, "too few template-parameter-lists");
23203 /* Otherwise, there are too many template parameter lists. We have
23206 template <class T> template <class U> void S::f(); */
23207 error_at (location
, "too many template-parameter-lists");
23211 /* Parse an optional `::' token indicating that the following name is
23212 from the global namespace. If so, PARSER->SCOPE is set to the
23213 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23214 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23215 Returns the new value of PARSER->SCOPE, if the `::' token is
23216 present, and NULL_TREE otherwise. */
23219 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23223 /* Peek at the next token. */
23224 token
= cp_lexer_peek_token (parser
->lexer
);
23225 /* If we're looking at a `::' token then we're starting from the
23226 global namespace, not our current location. */
23227 if (token
->type
== CPP_SCOPE
)
23229 /* Consume the `::' token. */
23230 cp_lexer_consume_token (parser
->lexer
);
23231 /* Set the SCOPE so that we know where to start the lookup. */
23232 parser
->scope
= global_namespace
;
23233 parser
->qualifying_scope
= global_namespace
;
23234 parser
->object_scope
= NULL_TREE
;
23236 return parser
->scope
;
23238 else if (!current_scope_valid_p
)
23240 parser
->scope
= NULL_TREE
;
23241 parser
->qualifying_scope
= NULL_TREE
;
23242 parser
->object_scope
= NULL_TREE
;
23248 /* Returns TRUE if the upcoming token sequence is the start of a
23249 constructor declarator. If FRIEND_P is true, the declarator is
23250 preceded by the `friend' specifier. */
23253 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23255 bool constructor_p
;
23256 bool outside_class_specifier_p
;
23257 tree nested_name_specifier
;
23258 cp_token
*next_token
;
23260 /* The common case is that this is not a constructor declarator, so
23261 try to avoid doing lots of work if at all possible. It's not
23262 valid declare a constructor at function scope. */
23263 if (parser
->in_function_body
)
23265 /* And only certain tokens can begin a constructor declarator. */
23266 next_token
= cp_lexer_peek_token (parser
->lexer
);
23267 if (next_token
->type
!= CPP_NAME
23268 && next_token
->type
!= CPP_SCOPE
23269 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23270 && next_token
->type
!= CPP_TEMPLATE_ID
)
23273 /* Parse tentatively; we are going to roll back all of the tokens
23275 cp_parser_parse_tentatively (parser
);
23276 /* Assume that we are looking at a constructor declarator. */
23277 constructor_p
= true;
23279 /* Look for the optional `::' operator. */
23280 cp_parser_global_scope_opt (parser
,
23281 /*current_scope_valid_p=*/false);
23282 /* Look for the nested-name-specifier. */
23283 nested_name_specifier
23284 = (cp_parser_nested_name_specifier_opt (parser
,
23285 /*typename_keyword_p=*/false,
23286 /*check_dependency_p=*/false,
23288 /*is_declaration=*/false));
23290 outside_class_specifier_p
= (!at_class_scope_p ()
23291 || !TYPE_BEING_DEFINED (current_class_type
)
23294 /* Outside of a class-specifier, there must be a
23295 nested-name-specifier. */
23296 if (!nested_name_specifier
&& outside_class_specifier_p
)
23297 constructor_p
= false;
23298 else if (nested_name_specifier
== error_mark_node
)
23299 constructor_p
= false;
23301 /* If we have a class scope, this is easy; DR 147 says that S::S always
23302 names the constructor, and no other qualified name could. */
23303 if (constructor_p
&& nested_name_specifier
23304 && CLASS_TYPE_P (nested_name_specifier
))
23306 tree id
= cp_parser_unqualified_id (parser
,
23307 /*template_keyword_p=*/false,
23308 /*check_dependency_p=*/false,
23309 /*declarator_p=*/true,
23310 /*optional_p=*/false);
23311 if (is_overloaded_fn (id
))
23312 id
= DECL_NAME (get_first_fn (id
));
23313 if (!constructor_name_p (id
, nested_name_specifier
))
23314 constructor_p
= false;
23316 /* If we still think that this might be a constructor-declarator,
23317 look for a class-name. */
23318 else if (constructor_p
)
23322 template <typename T> struct S {
23326 we must recognize that the nested `S' names a class. */
23328 type_decl
= cp_parser_class_name (parser
,
23329 /*typename_keyword_p=*/false,
23330 /*template_keyword_p=*/false,
23332 /*check_dependency_p=*/false,
23333 /*class_head_p=*/false,
23334 /*is_declaration=*/false);
23335 /* If there was no class-name, then this is not a constructor.
23336 Otherwise, if we are in a class-specifier and we aren't
23337 handling a friend declaration, check that its type matches
23338 current_class_type (c++/38313). Note: error_mark_node
23339 is left alone for error recovery purposes. */
23340 constructor_p
= (!cp_parser_error_occurred (parser
)
23341 && (outside_class_specifier_p
23342 || type_decl
== error_mark_node
23343 || same_type_p (current_class_type
,
23344 TREE_TYPE (type_decl
))));
23346 /* If we're still considering a constructor, we have to see a `(',
23347 to begin the parameter-declaration-clause, followed by either a
23348 `)', an `...', or a decl-specifier. We need to check for a
23349 type-specifier to avoid being fooled into thinking that:
23353 is a constructor. (It is actually a function named `f' that
23354 takes one parameter (of type `int') and returns a value of type
23357 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23358 constructor_p
= false;
23361 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23362 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23363 /* A parameter declaration begins with a decl-specifier,
23364 which is either the "attribute" keyword, a storage class
23365 specifier, or (usually) a type-specifier. */
23366 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23369 tree pushed_scope
= NULL_TREE
;
23370 unsigned saved_num_template_parameter_lists
;
23372 /* Names appearing in the type-specifier should be looked up
23373 in the scope of the class. */
23374 if (current_class_type
)
23378 type
= TREE_TYPE (type_decl
);
23379 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23381 type
= resolve_typename_type (type
,
23382 /*only_current_p=*/false);
23383 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23385 cp_parser_abort_tentative_parse (parser
);
23389 pushed_scope
= push_scope (type
);
23392 /* Inside the constructor parameter list, surrounding
23393 template-parameter-lists do not apply. */
23394 saved_num_template_parameter_lists
23395 = parser
->num_template_parameter_lists
;
23396 parser
->num_template_parameter_lists
= 0;
23398 /* Look for the type-specifier. */
23399 cp_parser_type_specifier (parser
,
23400 CP_PARSER_FLAGS_NONE
,
23401 /*decl_specs=*/NULL
,
23402 /*is_declarator=*/true,
23403 /*declares_class_or_enum=*/NULL
,
23404 /*is_cv_qualifier=*/NULL
);
23406 parser
->num_template_parameter_lists
23407 = saved_num_template_parameter_lists
;
23409 /* Leave the scope of the class. */
23411 pop_scope (pushed_scope
);
23413 constructor_p
= !cp_parser_error_occurred (parser
);
23417 /* We did not really want to consume any tokens. */
23418 cp_parser_abort_tentative_parse (parser
);
23420 return constructor_p
;
23423 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23424 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23425 they must be performed once we are in the scope of the function.
23427 Returns the function defined. */
23430 cp_parser_function_definition_from_specifiers_and_declarator
23431 (cp_parser
* parser
,
23432 cp_decl_specifier_seq
*decl_specifiers
,
23434 const cp_declarator
*declarator
)
23439 /* Begin the function-definition. */
23440 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23442 /* The things we're about to see are not directly qualified by any
23443 template headers we've seen thus far. */
23444 reset_specialization ();
23446 /* If there were names looked up in the decl-specifier-seq that we
23447 did not check, check them now. We must wait until we are in the
23448 scope of the function to perform the checks, since the function
23449 might be a friend. */
23450 perform_deferred_access_checks (tf_warning_or_error
);
23454 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23455 parser
->omp_declare_simd
= NULL
;
23460 /* Skip the entire function. */
23461 cp_parser_skip_to_end_of_block_or_statement (parser
);
23462 fn
= error_mark_node
;
23464 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23466 /* Seen already, skip it. An error message has already been output. */
23467 cp_parser_skip_to_end_of_block_or_statement (parser
);
23468 fn
= current_function_decl
;
23469 current_function_decl
= NULL_TREE
;
23470 /* If this is a function from a class, pop the nested class. */
23471 if (current_class_name
)
23472 pop_nested_class ();
23477 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23478 tv
= TV_PARSE_INLINE
;
23480 tv
= TV_PARSE_FUNC
;
23482 fn
= cp_parser_function_definition_after_declarator (parser
,
23483 /*inline_p=*/false);
23490 /* Parse the part of a function-definition that follows the
23491 declarator. INLINE_P is TRUE iff this function is an inline
23492 function defined within a class-specifier.
23494 Returns the function defined. */
23497 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23501 bool ctor_initializer_p
= false;
23502 bool saved_in_unbraced_linkage_specification_p
;
23503 bool saved_in_function_body
;
23504 unsigned saved_num_template_parameter_lists
;
23506 bool fully_implicit_function_template_p
23507 = parser
->fully_implicit_function_template_p
;
23508 parser
->fully_implicit_function_template_p
= false;
23509 tree implicit_template_parms
23510 = parser
->implicit_template_parms
;
23511 parser
->implicit_template_parms
= 0;
23512 cp_binding_level
* implicit_template_scope
23513 = parser
->implicit_template_scope
;
23514 parser
->implicit_template_scope
= 0;
23516 saved_in_function_body
= parser
->in_function_body
;
23517 parser
->in_function_body
= true;
23518 /* If the next token is `return', then the code may be trying to
23519 make use of the "named return value" extension that G++ used to
23521 token
= cp_lexer_peek_token (parser
->lexer
);
23522 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23524 /* Consume the `return' keyword. */
23525 cp_lexer_consume_token (parser
->lexer
);
23526 /* Look for the identifier that indicates what value is to be
23528 cp_parser_identifier (parser
);
23529 /* Issue an error message. */
23530 error_at (token
->location
,
23531 "named return values are no longer supported");
23532 /* Skip tokens until we reach the start of the function body. */
23535 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23536 if (token
->type
== CPP_OPEN_BRACE
23537 || token
->type
== CPP_EOF
23538 || token
->type
== CPP_PRAGMA_EOL
)
23540 cp_lexer_consume_token (parser
->lexer
);
23543 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23544 anything declared inside `f'. */
23545 saved_in_unbraced_linkage_specification_p
23546 = parser
->in_unbraced_linkage_specification_p
;
23547 parser
->in_unbraced_linkage_specification_p
= false;
23548 /* Inside the function, surrounding template-parameter-lists do not
23550 saved_num_template_parameter_lists
23551 = parser
->num_template_parameter_lists
;
23552 parser
->num_template_parameter_lists
= 0;
23554 start_lambda_scope (current_function_decl
);
23556 /* If the next token is `try', `__transaction_atomic', or
23557 `__transaction_relaxed`, then we are looking at either function-try-block
23558 or function-transaction-block. Note that all of these include the
23560 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23561 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23562 RID_TRANSACTION_ATOMIC
);
23563 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23564 RID_TRANSACTION_RELAXED
))
23565 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23566 RID_TRANSACTION_RELAXED
);
23567 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23568 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23570 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23571 (parser
, /*in_function_try_block=*/false);
23573 finish_lambda_scope ();
23575 /* Finish the function. */
23576 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23577 (inline_p
? 2 : 0));
23578 /* Generate code for it, if necessary. */
23579 expand_or_defer_fn (fn
);
23580 /* Restore the saved values. */
23581 parser
->in_unbraced_linkage_specification_p
23582 = saved_in_unbraced_linkage_specification_p
;
23583 parser
->num_template_parameter_lists
23584 = saved_num_template_parameter_lists
;
23585 parser
->in_function_body
= saved_in_function_body
;
23587 parser
->fully_implicit_function_template_p
23588 = fully_implicit_function_template_p
;
23589 parser
->implicit_template_parms
23590 = implicit_template_parms
;
23591 parser
->implicit_template_scope
23592 = implicit_template_scope
;
23594 if (parser
->fully_implicit_function_template_p
)
23595 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23600 /* Parse a template-declaration, assuming that the `export' (and
23601 `extern') keywords, if present, has already been scanned. MEMBER_P
23602 is as for cp_parser_template_declaration. */
23605 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23607 tree decl
= NULL_TREE
;
23608 vec
<deferred_access_check
, va_gc
> *checks
;
23609 tree parameter_list
;
23610 bool friend_p
= false;
23611 bool need_lang_pop
;
23614 /* Look for the `template' keyword. */
23615 token
= cp_lexer_peek_token (parser
->lexer
);
23616 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23620 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23622 if (at_class_scope_p () && current_function_decl
)
23624 /* 14.5.2.2 [temp.mem]
23626 A local class shall not have member templates. */
23627 error_at (token
->location
,
23628 "invalid declaration of member template in local class");
23629 cp_parser_skip_to_end_of_block_or_statement (parser
);
23634 A template ... shall not have C linkage. */
23635 if (current_lang_name
== lang_name_c
)
23637 error_at (token
->location
, "template with C linkage");
23638 /* Give it C++ linkage to avoid confusing other parts of the
23640 push_lang_context (lang_name_cplusplus
);
23641 need_lang_pop
= true;
23644 need_lang_pop
= false;
23646 /* We cannot perform access checks on the template parameter
23647 declarations until we know what is being declared, just as we
23648 cannot check the decl-specifier list. */
23649 push_deferring_access_checks (dk_deferred
);
23651 /* If the next token is `>', then we have an invalid
23652 specialization. Rather than complain about an invalid template
23653 parameter, issue an error message here. */
23654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23656 cp_parser_error (parser
, "invalid explicit specialization");
23657 begin_specialization ();
23658 parameter_list
= NULL_TREE
;
23662 /* Parse the template parameters. */
23663 parameter_list
= cp_parser_template_parameter_list (parser
);
23666 /* Get the deferred access checks from the parameter list. These
23667 will be checked once we know what is being declared, as for a
23668 member template the checks must be performed in the scope of the
23669 class containing the member. */
23670 checks
= get_deferred_access_checks ();
23672 /* Look for the `>'. */
23673 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23674 /* We just processed one more parameter list. */
23675 ++parser
->num_template_parameter_lists
;
23676 /* If the next token is `template', there are more template
23678 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23680 cp_parser_template_declaration_after_export (parser
, member_p
);
23681 else if (cxx_dialect
>= cxx11
23682 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23683 decl
= cp_parser_alias_declaration (parser
);
23686 /* There are no access checks when parsing a template, as we do not
23687 know if a specialization will be a friend. */
23688 push_deferring_access_checks (dk_no_check
);
23689 token
= cp_lexer_peek_token (parser
->lexer
);
23690 decl
= cp_parser_single_declaration (parser
,
23693 /*explicit_specialization_p=*/false,
23695 pop_deferring_access_checks ();
23697 /* If this is a member template declaration, let the front
23699 if (member_p
&& !friend_p
&& decl
)
23701 if (TREE_CODE (decl
) == TYPE_DECL
)
23702 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23704 decl
= finish_member_template_decl (decl
);
23706 else if (friend_p
&& decl
23707 && DECL_DECLARES_TYPE_P (decl
))
23708 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23709 /*complain=*/true);
23711 /* We are done with the current parameter list. */
23712 --parser
->num_template_parameter_lists
;
23714 pop_deferring_access_checks ();
23717 finish_template_decl (parameter_list
);
23719 /* Check the template arguments for a literal operator template. */
23721 && DECL_DECLARES_FUNCTION_P (decl
)
23722 && UDLIT_OPER_P (DECL_NAME (decl
)))
23725 if (parameter_list
== NULL_TREE
)
23729 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23730 if (num_parms
== 1)
23732 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23733 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23734 if (TREE_TYPE (parm
) != char_type_node
23735 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23738 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23740 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23741 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23742 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23743 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23744 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23745 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23753 if (cxx_dialect
>= cxx14
)
23754 error ("literal operator template %qD has invalid parameter list."
23755 " Expected non-type template argument pack <char...>"
23756 " or <typename CharT, CharT...>",
23759 error ("literal operator template %qD has invalid parameter list."
23760 " Expected non-type template argument pack <char...>",
23764 /* Register member declarations. */
23765 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23766 finish_member_declaration (decl
);
23767 /* For the erroneous case of a template with C linkage, we pushed an
23768 implicit C++ linkage scope; exit that scope now. */
23770 pop_lang_context ();
23771 /* If DECL is a function template, we must return to parse it later.
23772 (Even though there is no definition, there might be default
23773 arguments that need handling.) */
23774 if (member_p
&& decl
23775 && DECL_DECLARES_FUNCTION_P (decl
))
23776 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23779 /* Perform the deferred access checks from a template-parameter-list.
23780 CHECKS is a TREE_LIST of access checks, as returned by
23781 get_deferred_access_checks. */
23784 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23786 ++processing_template_parmlist
;
23787 perform_access_checks (checks
, tf_warning_or_error
);
23788 --processing_template_parmlist
;
23791 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23792 `function-definition' sequence that follows a template header.
23793 If MEMBER_P is true, this declaration appears in a class scope.
23795 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23796 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23799 cp_parser_single_declaration (cp_parser
* parser
,
23800 vec
<deferred_access_check
, va_gc
> *checks
,
23802 bool explicit_specialization_p
,
23805 int declares_class_or_enum
;
23806 tree decl
= NULL_TREE
;
23807 cp_decl_specifier_seq decl_specifiers
;
23808 bool function_definition_p
= false;
23809 cp_token
*decl_spec_token_start
;
23811 /* This function is only used when processing a template
23813 gcc_assert (innermost_scope_kind () == sk_template_parms
23814 || innermost_scope_kind () == sk_template_spec
);
23816 /* Defer access checks until we know what is being declared. */
23817 push_deferring_access_checks (dk_deferred
);
23819 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23821 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23822 cp_parser_decl_specifier_seq (parser
,
23823 CP_PARSER_FLAGS_OPTIONAL
,
23825 &declares_class_or_enum
);
23827 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23829 /* There are no template typedefs. */
23830 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23832 error_at (decl_spec_token_start
->location
,
23833 "template declaration of %<typedef%>");
23834 decl
= error_mark_node
;
23837 /* Gather up the access checks that occurred the
23838 decl-specifier-seq. */
23839 stop_deferring_access_checks ();
23841 /* Check for the declaration of a template class. */
23842 if (declares_class_or_enum
)
23844 if (cp_parser_declares_only_class_p (parser
))
23846 decl
= shadow_tag (&decl_specifiers
);
23851 friend template <typename T> struct A<T>::B;
23854 A<T>::B will be represented by a TYPENAME_TYPE, and
23855 therefore not recognized by shadow_tag. */
23856 if (friend_p
&& *friend_p
23858 && decl_specifiers
.type
23859 && TYPE_P (decl_specifiers
.type
))
23860 decl
= decl_specifiers
.type
;
23862 if (decl
&& decl
!= error_mark_node
)
23863 decl
= TYPE_NAME (decl
);
23865 decl
= error_mark_node
;
23867 /* Perform access checks for template parameters. */
23868 cp_parser_perform_template_parameter_access_checks (checks
);
23872 /* Complain about missing 'typename' or other invalid type names. */
23873 if (!decl_specifiers
.any_type_specifiers_p
23874 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23876 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23877 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23878 the rest of this declaration. */
23879 decl
= error_mark_node
;
23883 /* If it's not a template class, try for a template function. If
23884 the next token is a `;', then this declaration does not declare
23885 anything. But, if there were errors in the decl-specifiers, then
23886 the error might well have come from an attempted class-specifier.
23887 In that case, there's no need to warn about a missing declarator. */
23889 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23890 || decl_specifiers
.type
!= error_mark_node
))
23892 decl
= cp_parser_init_declarator (parser
,
23895 /*function_definition_allowed_p=*/true,
23897 declares_class_or_enum
,
23898 &function_definition_p
,
23901 /* 7.1.1-1 [dcl.stc]
23903 A storage-class-specifier shall not be specified in an explicit
23904 specialization... */
23906 && explicit_specialization_p
23907 && decl_specifiers
.storage_class
!= sc_none
)
23909 error_at (decl_spec_token_start
->location
,
23910 "explicit template specialization cannot have a storage class");
23911 decl
= error_mark_node
;
23914 if (decl
&& VAR_P (decl
))
23915 check_template_variable (decl
);
23918 /* Look for a trailing `;' after the declaration. */
23919 if (!function_definition_p
23920 && (decl
== error_mark_node
23921 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23922 cp_parser_skip_to_end_of_block_or_statement (parser
);
23925 pop_deferring_access_checks ();
23927 /* Clear any current qualification; whatever comes next is the start
23928 of something new. */
23929 parser
->scope
= NULL_TREE
;
23930 parser
->qualifying_scope
= NULL_TREE
;
23931 parser
->object_scope
= NULL_TREE
;
23936 /* Parse a cast-expression that is not the operand of a unary "&". */
23939 cp_parser_simple_cast_expression (cp_parser
*parser
)
23941 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23942 /*cast_p=*/false, /*decltype*/false, NULL
);
23945 /* Parse a functional cast to TYPE. Returns an expression
23946 representing the cast. */
23949 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23951 vec
<tree
, va_gc
> *vec
;
23952 tree expression_list
;
23957 type
= error_mark_node
;
23959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23961 cp_lexer_set_source_position (parser
->lexer
);
23962 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23963 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23964 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23965 if (TREE_CODE (type
) == TYPE_DECL
)
23966 type
= TREE_TYPE (type
);
23967 return finish_compound_literal (type
, expression_list
,
23968 tf_warning_or_error
);
23972 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23974 /*allow_expansion_p=*/true,
23975 /*non_constant_p=*/NULL
);
23977 expression_list
= error_mark_node
;
23980 expression_list
= build_tree_list_vec (vec
);
23981 release_tree_vector (vec
);
23984 cast
= build_functional_cast (type
, expression_list
,
23985 tf_warning_or_error
);
23986 /* [expr.const]/1: In an integral constant expression "only type
23987 conversions to integral or enumeration type can be used". */
23988 if (TREE_CODE (type
) == TYPE_DECL
)
23989 type
= TREE_TYPE (type
);
23990 if (cast
!= error_mark_node
23991 && !cast_valid_in_integral_constant_expression_p (type
)
23992 && cp_parser_non_integral_constant_expression (parser
,
23994 return error_mark_node
;
23998 /* Save the tokens that make up the body of a member function defined
23999 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
24000 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
24001 specifiers applied to the declaration. Returns the FUNCTION_DECL
24002 for the member function. */
24005 cp_parser_save_member_function_body (cp_parser
* parser
,
24006 cp_decl_specifier_seq
*decl_specifiers
,
24007 cp_declarator
*declarator
,
24014 /* Create the FUNCTION_DECL. */
24015 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
24016 cp_finalize_omp_declare_simd (parser
, fn
);
24017 /* If something went badly wrong, bail out now. */
24018 if (fn
== error_mark_node
)
24020 /* If there's a function-body, skip it. */
24021 if (cp_parser_token_starts_function_definition_p
24022 (cp_lexer_peek_token (parser
->lexer
)))
24023 cp_parser_skip_to_end_of_block_or_statement (parser
);
24024 return error_mark_node
;
24027 /* Remember it, if there default args to post process. */
24028 cp_parser_save_default_args (parser
, fn
);
24030 /* Save away the tokens that make up the body of the
24032 first
= parser
->lexer
->next_token
;
24033 /* Handle function try blocks. */
24034 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
24035 cp_lexer_consume_token (parser
->lexer
);
24036 /* We can have braced-init-list mem-initializers before the fn body. */
24037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24039 cp_lexer_consume_token (parser
->lexer
);
24040 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
24042 /* cache_group will stop after an un-nested { } pair, too. */
24043 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
24046 /* variadic mem-inits have ... after the ')'. */
24047 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24048 cp_lexer_consume_token (parser
->lexer
);
24051 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24052 /* Handle function try blocks. */
24053 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
24054 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
24055 last
= parser
->lexer
->next_token
;
24057 /* Save away the inline definition; we will process it when the
24058 class is complete. */
24059 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
24060 DECL_PENDING_INLINE_P (fn
) = 1;
24062 /* We need to know that this was defined in the class, so that
24063 friend templates are handled correctly. */
24064 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
24066 /* Add FN to the queue of functions to be parsed later. */
24067 vec_safe_push (unparsed_funs_with_definitions
, fn
);
24072 /* Save the tokens that make up the in-class initializer for a non-static
24073 data member. Returns a DEFAULT_ARG. */
24076 cp_parser_save_nsdmi (cp_parser
* parser
)
24078 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
24081 /* Parse a template-argument-list, as well as the trailing ">" (but
24082 not the opening "<"). See cp_parser_template_argument_list for the
24086 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
24090 tree saved_qualifying_scope
;
24091 tree saved_object_scope
;
24092 bool saved_greater_than_is_operator_p
;
24093 int saved_unevaluated_operand
;
24094 int saved_inhibit_evaluation_warnings
;
24098 When parsing a template-id, the first non-nested `>' is taken as
24099 the end of the template-argument-list rather than a greater-than
24101 saved_greater_than_is_operator_p
24102 = parser
->greater_than_is_operator_p
;
24103 parser
->greater_than_is_operator_p
= false;
24104 /* Parsing the argument list may modify SCOPE, so we save it
24106 saved_scope
= parser
->scope
;
24107 saved_qualifying_scope
= parser
->qualifying_scope
;
24108 saved_object_scope
= parser
->object_scope
;
24109 /* We need to evaluate the template arguments, even though this
24110 template-id may be nested within a "sizeof". */
24111 saved_unevaluated_operand
= cp_unevaluated_operand
;
24112 cp_unevaluated_operand
= 0;
24113 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24114 c_inhibit_evaluation_warnings
= 0;
24115 /* Parse the template-argument-list itself. */
24116 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
24117 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24118 arguments
= NULL_TREE
;
24120 arguments
= cp_parser_template_argument_list (parser
);
24121 /* Look for the `>' that ends the template-argument-list. If we find
24122 a '>>' instead, it's probably just a typo. */
24123 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24125 if (cxx_dialect
!= cxx98
)
24127 /* In C++0x, a `>>' in a template argument list or cast
24128 expression is considered to be two separate `>'
24129 tokens. So, change the current token to a `>', but don't
24130 consume it: it will be consumed later when the outer
24131 template argument list (or cast expression) is parsed.
24132 Note that this replacement of `>' for `>>' is necessary
24133 even if we are parsing tentatively: in the tentative
24134 case, after calling
24135 cp_parser_enclosed_template_argument_list we will always
24136 throw away all of the template arguments and the first
24137 closing `>', either because the template argument list
24138 was erroneous or because we are replacing those tokens
24139 with a CPP_TEMPLATE_ID token. The second `>' (which will
24140 not have been thrown away) is needed either to close an
24141 outer template argument list or to complete a new-style
24143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24144 token
->type
= CPP_GREATER
;
24146 else if (!saved_greater_than_is_operator_p
)
24148 /* If we're in a nested template argument list, the '>>' has
24149 to be a typo for '> >'. We emit the error message, but we
24150 continue parsing and we push a '>' as next token, so that
24151 the argument list will be parsed correctly. Note that the
24152 global source location is still on the token before the
24153 '>>', so we need to say explicitly where we want it. */
24154 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24155 error_at (token
->location
, "%<>>%> should be %<> >%> "
24156 "within a nested template argument list");
24158 token
->type
= CPP_GREATER
;
24162 /* If this is not a nested template argument list, the '>>'
24163 is a typo for '>'. Emit an error message and continue.
24164 Same deal about the token location, but here we can get it
24165 right by consuming the '>>' before issuing the diagnostic. */
24166 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
24167 error_at (token
->location
,
24168 "spurious %<>>%>, use %<>%> to terminate "
24169 "a template argument list");
24173 cp_parser_skip_to_end_of_template_parameter_list (parser
);
24174 /* The `>' token might be a greater-than operator again now. */
24175 parser
->greater_than_is_operator_p
24176 = saved_greater_than_is_operator_p
;
24177 /* Restore the SAVED_SCOPE. */
24178 parser
->scope
= saved_scope
;
24179 parser
->qualifying_scope
= saved_qualifying_scope
;
24180 parser
->object_scope
= saved_object_scope
;
24181 cp_unevaluated_operand
= saved_unevaluated_operand
;
24182 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24187 /* MEMBER_FUNCTION is a member function, or a friend. If default
24188 arguments, or the body of the function have not yet been parsed,
24192 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
24194 timevar_push (TV_PARSE_INMETH
);
24195 /* If this member is a template, get the underlying
24197 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
24198 member_function
= DECL_TEMPLATE_RESULT (member_function
);
24200 /* There should not be any class definitions in progress at this
24201 point; the bodies of members are only parsed outside of all class
24203 gcc_assert (parser
->num_classes_being_defined
== 0);
24204 /* While we're parsing the member functions we might encounter more
24205 classes. We want to handle them right away, but we don't want
24206 them getting mixed up with functions that are currently in the
24208 push_unparsed_function_queues (parser
);
24210 /* Make sure that any template parameters are in scope. */
24211 maybe_begin_member_template_processing (member_function
);
24213 /* If the body of the function has not yet been parsed, parse it
24215 if (DECL_PENDING_INLINE_P (member_function
))
24217 tree function_scope
;
24218 cp_token_cache
*tokens
;
24220 /* The function is no longer pending; we are processing it. */
24221 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24222 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24223 DECL_PENDING_INLINE_P (member_function
) = 0;
24225 /* If this is a local class, enter the scope of the containing
24227 function_scope
= current_function_decl
;
24228 if (function_scope
)
24229 push_function_context ();
24231 /* Push the body of the function onto the lexer stack. */
24232 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24234 /* Let the front end know that we going to be defining this
24236 start_preparsed_function (member_function
, NULL_TREE
,
24237 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24239 /* Don't do access checking if it is a templated function. */
24240 if (processing_template_decl
)
24241 push_deferring_access_checks (dk_no_check
);
24243 /* #pragma omp declare reduction needs special parsing. */
24244 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24246 parser
->lexer
->in_pragma
= true;
24247 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24248 finish_function (/*inline*/2);
24249 cp_check_omp_declare_reduction (member_function
);
24252 /* Now, parse the body of the function. */
24253 cp_parser_function_definition_after_declarator (parser
,
24254 /*inline_p=*/true);
24256 if (processing_template_decl
)
24257 pop_deferring_access_checks ();
24259 /* Leave the scope of the containing function. */
24260 if (function_scope
)
24261 pop_function_context ();
24262 cp_parser_pop_lexer (parser
);
24265 /* Remove any template parameters from the symbol table. */
24266 maybe_end_member_template_processing ();
24268 /* Restore the queue. */
24269 pop_unparsed_function_queues (parser
);
24270 timevar_pop (TV_PARSE_INMETH
);
24273 /* If DECL contains any default args, remember it on the unparsed
24274 functions queue. */
24277 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24281 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24283 probe
= TREE_CHAIN (probe
))
24284 if (TREE_PURPOSE (probe
))
24286 cp_default_arg_entry entry
= {current_class_type
, decl
};
24287 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24292 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24293 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24294 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24295 from the parameter-type-list. */
24298 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24299 tree default_arg
, tree parmtype
)
24301 cp_token_cache
*tokens
;
24305 if (default_arg
== error_mark_node
)
24306 return error_mark_node
;
24308 /* Push the saved tokens for the default argument onto the parser's
24310 tokens
= DEFARG_TOKENS (default_arg
);
24311 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24313 start_lambda_scope (decl
);
24315 /* Parse the default argument. */
24316 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24317 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24318 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24320 finish_lambda_scope ();
24322 if (parsed_arg
== error_mark_node
)
24323 cp_parser_skip_to_end_of_statement (parser
);
24325 if (!processing_template_decl
)
24327 /* In a non-template class, check conversions now. In a template,
24328 we'll wait and instantiate these as needed. */
24329 if (TREE_CODE (decl
) == PARM_DECL
)
24330 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24331 tf_warning_or_error
);
24333 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24336 /* If the token stream has not been completely used up, then
24337 there was extra junk after the end of the default
24339 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24341 if (TREE_CODE (decl
) == PARM_DECL
)
24342 cp_parser_error (parser
, "expected %<,%>");
24344 cp_parser_error (parser
, "expected %<;%>");
24347 /* Revert to the main lexer. */
24348 cp_parser_pop_lexer (parser
);
24353 /* FIELD is a non-static data member with an initializer which we saved for
24354 later; parse it now. */
24357 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24361 maybe_begin_member_template_processing (field
);
24363 push_unparsed_function_queues (parser
);
24364 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24365 DECL_INITIAL (field
),
24367 pop_unparsed_function_queues (parser
);
24369 maybe_end_member_template_processing ();
24371 DECL_INITIAL (field
) = def
;
24374 /* FN is a FUNCTION_DECL which may contains a parameter with an
24375 unparsed DEFAULT_ARG. Parse the default args now. This function
24376 assumes that the current scope is the scope in which the default
24377 argument should be processed. */
24380 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24382 bool saved_local_variables_forbidden_p
;
24383 tree parm
, parmdecl
;
24385 /* While we're parsing the default args, we might (due to the
24386 statement expression extension) encounter more classes. We want
24387 to handle them right away, but we don't want them getting mixed
24388 up with default args that are currently in the queue. */
24389 push_unparsed_function_queues (parser
);
24391 /* Local variable names (and the `this' keyword) may not appear
24392 in a default argument. */
24393 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24394 parser
->local_variables_forbidden_p
= true;
24396 push_defarg_context (fn
);
24398 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24399 parmdecl
= DECL_ARGUMENTS (fn
);
24400 parm
&& parm
!= void_list_node
;
24401 parm
= TREE_CHAIN (parm
),
24402 parmdecl
= DECL_CHAIN (parmdecl
))
24404 tree default_arg
= TREE_PURPOSE (parm
);
24406 vec
<tree
, va_gc
> *insts
;
24413 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24414 /* This can happen for a friend declaration for a function
24415 already declared with default arguments. */
24419 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24421 TREE_VALUE (parm
));
24422 if (parsed_arg
== error_mark_node
)
24427 TREE_PURPOSE (parm
) = parsed_arg
;
24429 /* Update any instantiations we've already created. */
24430 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24431 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24432 TREE_PURPOSE (copy
) = parsed_arg
;
24435 pop_defarg_context ();
24437 /* Make sure no default arg is missing. */
24438 check_default_args (fn
);
24440 /* Restore the state of local_variables_forbidden_p. */
24441 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24443 /* Restore the queue. */
24444 pop_unparsed_function_queues (parser
);
24447 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24449 sizeof ... ( identifier )
24451 where the 'sizeof' token has already been consumed. */
24454 cp_parser_sizeof_pack (cp_parser
*parser
)
24456 /* Consume the `...'. */
24457 cp_lexer_consume_token (parser
->lexer
);
24458 maybe_warn_variadic_templates ();
24460 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24462 cp_lexer_consume_token (parser
->lexer
);
24464 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24465 "%<sizeof...%> argument must be surrounded by parentheses");
24467 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24468 tree name
= cp_parser_identifier (parser
);
24469 if (name
== error_mark_node
)
24470 return error_mark_node
;
24471 /* The name is not qualified. */
24472 parser
->scope
= NULL_TREE
;
24473 parser
->qualifying_scope
= NULL_TREE
;
24474 parser
->object_scope
= NULL_TREE
;
24475 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24476 if (expr
== error_mark_node
)
24477 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24479 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
24480 expr
= TREE_TYPE (expr
);
24481 else if (TREE_CODE (expr
) == CONST_DECL
)
24482 expr
= DECL_INITIAL (expr
);
24483 expr
= make_pack_expansion (expr
);
24486 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24491 /* Parse the operand of `sizeof' (or a similar operator). Returns
24492 either a TYPE or an expression, depending on the form of the
24493 input. The KEYWORD indicates which kind of expression we have
24497 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24499 tree expr
= NULL_TREE
;
24500 const char *saved_message
;
24502 bool saved_integral_constant_expression_p
;
24503 bool saved_non_integral_constant_expression_p
;
24505 /* If it's a `...', then we are computing the length of a parameter
24507 if (keyword
== RID_SIZEOF
24508 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24509 return cp_parser_sizeof_pack (parser
);
24511 /* Types cannot be defined in a `sizeof' expression. Save away the
24513 saved_message
= parser
->type_definition_forbidden_message
;
24514 /* And create the new one. */
24515 tmp
= concat ("types may not be defined in %<",
24516 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24517 "%> expressions", NULL
);
24518 parser
->type_definition_forbidden_message
= tmp
;
24520 /* The restrictions on constant-expressions do not apply inside
24521 sizeof expressions. */
24522 saved_integral_constant_expression_p
24523 = parser
->integral_constant_expression_p
;
24524 saved_non_integral_constant_expression_p
24525 = parser
->non_integral_constant_expression_p
;
24526 parser
->integral_constant_expression_p
= false;
24528 /* Do not actually evaluate the expression. */
24529 ++cp_unevaluated_operand
;
24530 ++c_inhibit_evaluation_warnings
;
24531 /* If it's a `(', then we might be looking at the type-id
24533 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24535 tree type
= NULL_TREE
;
24537 /* We can't be sure yet whether we're looking at a type-id or an
24539 cp_parser_parse_tentatively (parser
);
24540 /* Note: as a GNU Extension, compound literals are considered
24541 postfix-expressions as they are in C99, so they are valid
24542 arguments to sizeof. See comment in cp_parser_cast_expression
24544 if (cp_parser_compound_literal_p (parser
))
24545 cp_parser_simulate_error (parser
);
24548 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24549 parser
->in_type_id_in_expr_p
= true;
24550 /* Look for the type-id. */
24551 type
= cp_parser_type_id (parser
);
24552 /* Look for the closing `)'. */
24553 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24554 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24557 /* If all went well, then we're done. */
24558 if (cp_parser_parse_definitely (parser
))
24560 cp_decl_specifier_seq decl_specs
;
24562 /* Build a trivial decl-specifier-seq. */
24563 clear_decl_specs (&decl_specs
);
24564 decl_specs
.type
= type
;
24566 /* Call grokdeclarator to figure out what type this is. */
24567 expr
= grokdeclarator (NULL
,
24571 /*attrlist=*/NULL
);
24575 /* If the type-id production did not work out, then we must be
24576 looking at the unary-expression production. */
24578 expr
= cp_parser_unary_expression (parser
);
24580 /* Go back to evaluating expressions. */
24581 --cp_unevaluated_operand
;
24582 --c_inhibit_evaluation_warnings
;
24584 /* Free the message we created. */
24586 /* And restore the old one. */
24587 parser
->type_definition_forbidden_message
= saved_message
;
24588 parser
->integral_constant_expression_p
24589 = saved_integral_constant_expression_p
;
24590 parser
->non_integral_constant_expression_p
24591 = saved_non_integral_constant_expression_p
;
24596 /* If the current declaration has no declarator, return true. */
24599 cp_parser_declares_only_class_p (cp_parser
*parser
)
24601 /* If the next token is a `;' or a `,' then there is no
24603 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24604 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24607 /* Update the DECL_SPECS to reflect the storage class indicated by
24611 cp_parser_set_storage_class (cp_parser
*parser
,
24612 cp_decl_specifier_seq
*decl_specs
,
24616 cp_storage_class storage_class
;
24618 if (parser
->in_unbraced_linkage_specification_p
)
24620 error_at (token
->location
, "invalid use of %qD in linkage specification",
24621 ridpointers
[keyword
]);
24624 else if (decl_specs
->storage_class
!= sc_none
)
24626 decl_specs
->conflicting_specifiers_p
= true;
24630 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24631 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24632 && decl_specs
->gnu_thread_keyword_p
)
24634 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24635 "%<__thread%> before %qD", ridpointers
[keyword
]);
24641 storage_class
= sc_auto
;
24644 storage_class
= sc_register
;
24647 storage_class
= sc_static
;
24650 storage_class
= sc_extern
;
24653 storage_class
= sc_mutable
;
24656 gcc_unreachable ();
24658 decl_specs
->storage_class
= storage_class
;
24659 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24661 /* A storage class specifier cannot be applied alongside a typedef
24662 specifier. If there is a typedef specifier present then set
24663 conflicting_specifiers_p which will trigger an error later
24664 on in grokdeclarator. */
24665 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24666 decl_specs
->conflicting_specifiers_p
= true;
24669 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24670 is true, the type is a class or enum definition. */
24673 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24676 bool type_definition_p
)
24678 decl_specs
->any_specifiers_p
= true;
24680 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24681 (with, for example, in "typedef int wchar_t;") we remember that
24682 this is what happened. In system headers, we ignore these
24683 declarations so that G++ can work with system headers that are not
24685 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24686 && !type_definition_p
24687 && (type_spec
== boolean_type_node
24688 || type_spec
== char16_type_node
24689 || type_spec
== char32_type_node
24690 || type_spec
== wchar_type_node
)
24691 && (decl_specs
->type
24692 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24693 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24694 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24695 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24697 decl_specs
->redefined_builtin_type
= type_spec
;
24698 set_and_check_decl_spec_loc (decl_specs
,
24699 ds_redefined_builtin_type_spec
,
24701 if (!decl_specs
->type
)
24703 decl_specs
->type
= type_spec
;
24704 decl_specs
->type_definition_p
= false;
24705 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24708 else if (decl_specs
->type
)
24709 decl_specs
->multiple_types_p
= true;
24712 decl_specs
->type
= type_spec
;
24713 decl_specs
->type_definition_p
= type_definition_p
;
24714 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24715 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24719 /* True iff TOKEN is the GNU keyword __thread. */
24722 token_is__thread (cp_token
*token
)
24724 gcc_assert (token
->keyword
== RID_THREAD
);
24725 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24728 /* Set the location for a declarator specifier and check if it is
24731 DECL_SPECS is the sequence of declarator specifiers onto which to
24734 DS is the single declarator specifier to set which location is to
24735 be set onto the existing sequence of declarators.
24737 LOCATION is the location for the declarator specifier to
24741 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24742 cp_decl_spec ds
, cp_token
*token
)
24744 gcc_assert (ds
< ds_last
);
24746 if (decl_specs
== NULL
)
24749 source_location location
= token
->location
;
24751 if (decl_specs
->locations
[ds
] == 0)
24753 decl_specs
->locations
[ds
] = location
;
24754 if (ds
== ds_thread
)
24755 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24761 if (decl_specs
->locations
[ds_long_long
] != 0)
24762 error_at (location
,
24763 "%<long long long%> is too long for GCC");
24766 decl_specs
->locations
[ds_long_long
] = location
;
24767 pedwarn_cxx98 (location
,
24769 "ISO C++ 1998 does not support %<long long%>");
24772 else if (ds
== ds_thread
)
24774 bool gnu
= token_is__thread (token
);
24775 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24776 error_at (location
,
24777 "both %<__thread%> and %<thread_local%> specified");
24779 error_at (location
, "duplicate %qD", token
->u
.value
);
24783 static const char *const decl_spec_names
[] = {
24800 error_at (location
,
24801 "duplicate %qs", decl_spec_names
[ds
]);
24806 /* Return true iff the declarator specifier DS is present in the
24807 sequence of declarator specifiers DECL_SPECS. */
24810 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24813 gcc_assert (ds
< ds_last
);
24815 if (decl_specs
== NULL
)
24818 return decl_specs
->locations
[ds
] != 0;
24821 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24822 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24825 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24827 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24830 /* Issue an error message indicating that TOKEN_DESC was expected.
24831 If KEYWORD is true, it indicated this function is called by
24832 cp_parser_require_keword and the required token can only be
24833 a indicated keyword. */
24836 cp_parser_required_error (cp_parser
*parser
,
24837 required_token token_desc
,
24840 switch (token_desc
)
24843 cp_parser_error (parser
, "expected %<new%>");
24846 cp_parser_error (parser
, "expected %<delete%>");
24849 cp_parser_error (parser
, "expected %<return%>");
24852 cp_parser_error (parser
, "expected %<while%>");
24855 cp_parser_error (parser
, "expected %<extern%>");
24857 case RT_STATIC_ASSERT
:
24858 cp_parser_error (parser
, "expected %<static_assert%>");
24861 cp_parser_error (parser
, "expected %<decltype%>");
24864 cp_parser_error (parser
, "expected %<operator%>");
24867 cp_parser_error (parser
, "expected %<class%>");
24870 cp_parser_error (parser
, "expected %<template%>");
24873 cp_parser_error (parser
, "expected %<namespace%>");
24876 cp_parser_error (parser
, "expected %<using%>");
24879 cp_parser_error (parser
, "expected %<asm%>");
24882 cp_parser_error (parser
, "expected %<try%>");
24885 cp_parser_error (parser
, "expected %<catch%>");
24888 cp_parser_error (parser
, "expected %<throw%>");
24891 cp_parser_error (parser
, "expected %<__label__%>");
24894 cp_parser_error (parser
, "expected %<@try%>");
24896 case RT_AT_SYNCHRONIZED
:
24897 cp_parser_error (parser
, "expected %<@synchronized%>");
24900 cp_parser_error (parser
, "expected %<@throw%>");
24902 case RT_TRANSACTION_ATOMIC
:
24903 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24905 case RT_TRANSACTION_RELAXED
:
24906 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24913 switch (token_desc
)
24916 cp_parser_error (parser
, "expected %<;%>");
24918 case RT_OPEN_PAREN
:
24919 cp_parser_error (parser
, "expected %<(%>");
24921 case RT_CLOSE_BRACE
:
24922 cp_parser_error (parser
, "expected %<}%>");
24924 case RT_OPEN_BRACE
:
24925 cp_parser_error (parser
, "expected %<{%>");
24927 case RT_CLOSE_SQUARE
:
24928 cp_parser_error (parser
, "expected %<]%>");
24930 case RT_OPEN_SQUARE
:
24931 cp_parser_error (parser
, "expected %<[%>");
24934 cp_parser_error (parser
, "expected %<,%>");
24937 cp_parser_error (parser
, "expected %<::%>");
24940 cp_parser_error (parser
, "expected %<<%>");
24943 cp_parser_error (parser
, "expected %<>%>");
24946 cp_parser_error (parser
, "expected %<=%>");
24949 cp_parser_error (parser
, "expected %<...%>");
24952 cp_parser_error (parser
, "expected %<*%>");
24955 cp_parser_error (parser
, "expected %<~%>");
24958 cp_parser_error (parser
, "expected %<:%>");
24960 case RT_COLON_SCOPE
:
24961 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24963 case RT_CLOSE_PAREN
:
24964 cp_parser_error (parser
, "expected %<)%>");
24966 case RT_COMMA_CLOSE_PAREN
:
24967 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24969 case RT_PRAGMA_EOL
:
24970 cp_parser_error (parser
, "expected end of line");
24973 cp_parser_error (parser
, "expected identifier");
24976 cp_parser_error (parser
, "expected selection-statement");
24978 case RT_INTERATION
:
24979 cp_parser_error (parser
, "expected iteration-statement");
24982 cp_parser_error (parser
, "expected jump-statement");
24985 cp_parser_error (parser
, "expected class-key");
24987 case RT_CLASS_TYPENAME_TEMPLATE
:
24988 cp_parser_error (parser
,
24989 "expected %<class%>, %<typename%>, or %<template%>");
24992 gcc_unreachable ();
24996 gcc_unreachable ();
25001 /* If the next token is of the indicated TYPE, consume it. Otherwise,
25002 issue an error message indicating that TOKEN_DESC was expected.
25004 Returns the token consumed, if the token had the appropriate type.
25005 Otherwise, returns NULL. */
25008 cp_parser_require (cp_parser
* parser
,
25009 enum cpp_ttype type
,
25010 required_token token_desc
)
25012 if (cp_lexer_next_token_is (parser
->lexer
, type
))
25013 return cp_lexer_consume_token (parser
->lexer
);
25016 /* Output the MESSAGE -- unless we're parsing tentatively. */
25017 if (!cp_parser_simulate_error (parser
))
25018 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
25023 /* An error message is produced if the next token is not '>'.
25024 All further tokens are skipped until the desired token is
25025 found or '{', '}', ';' or an unbalanced ')' or ']'. */
25028 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
25030 /* Current level of '< ... >'. */
25031 unsigned level
= 0;
25032 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
25033 unsigned nesting_depth
= 0;
25035 /* Are we ready, yet? If not, issue error message. */
25036 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
25039 /* Skip tokens until the desired token is found. */
25042 /* Peek at the next token. */
25043 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
25046 if (!nesting_depth
)
25051 if (cxx_dialect
== cxx98
)
25052 /* C++0x views the `>>' operator as two `>' tokens, but
25055 else if (!nesting_depth
&& level
-- == 0)
25057 /* We've hit a `>>' where the first `>' closes the
25058 template argument list, and the second `>' is
25059 spurious. Just consume the `>>' and stop; we've
25060 already produced at least one error. */
25061 cp_lexer_consume_token (parser
->lexer
);
25064 /* Fall through for C++0x, so we handle the second `>' in
25068 if (!nesting_depth
&& level
-- == 0)
25070 /* We've reached the token we want, consume it and stop. */
25071 cp_lexer_consume_token (parser
->lexer
);
25076 case CPP_OPEN_PAREN
:
25077 case CPP_OPEN_SQUARE
:
25081 case CPP_CLOSE_PAREN
:
25082 case CPP_CLOSE_SQUARE
:
25083 if (nesting_depth
-- == 0)
25088 case CPP_PRAGMA_EOL
:
25089 case CPP_SEMICOLON
:
25090 case CPP_OPEN_BRACE
:
25091 case CPP_CLOSE_BRACE
:
25092 /* The '>' was probably forgotten, don't look further. */
25099 /* Consume this token. */
25100 cp_lexer_consume_token (parser
->lexer
);
25104 /* If the next token is the indicated keyword, consume it. Otherwise,
25105 issue an error message indicating that TOKEN_DESC was expected.
25107 Returns the token consumed, if the token had the appropriate type.
25108 Otherwise, returns NULL. */
25111 cp_parser_require_keyword (cp_parser
* parser
,
25113 required_token token_desc
)
25115 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
25117 if (token
&& token
->keyword
!= keyword
)
25119 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
25126 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25127 function-definition. */
25130 cp_parser_token_starts_function_definition_p (cp_token
* token
)
25132 return (/* An ordinary function-body begins with an `{'. */
25133 token
->type
== CPP_OPEN_BRACE
25134 /* A ctor-initializer begins with a `:'. */
25135 || token
->type
== CPP_COLON
25136 /* A function-try-block begins with `try'. */
25137 || token
->keyword
== RID_TRY
25138 /* A function-transaction-block begins with `__transaction_atomic'
25139 or `__transaction_relaxed'. */
25140 || token
->keyword
== RID_TRANSACTION_ATOMIC
25141 || token
->keyword
== RID_TRANSACTION_RELAXED
25142 /* The named return value extension begins with `return'. */
25143 || token
->keyword
== RID_RETURN
);
25146 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25150 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
25154 token
= cp_lexer_peek_token (parser
->lexer
);
25155 return (token
->type
== CPP_OPEN_BRACE
25156 || (token
->type
== CPP_COLON
25157 && !parser
->colon_doesnt_start_class_def_p
));
25160 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25161 C++0x) ending a template-argument. */
25164 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
25168 token
= cp_lexer_peek_token (parser
->lexer
);
25169 return (token
->type
== CPP_COMMA
25170 || token
->type
== CPP_GREATER
25171 || token
->type
== CPP_ELLIPSIS
25172 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
25175 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25176 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25179 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
25184 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25185 if (token
->type
== CPP_LESS
)
25187 /* Check for the sequence `<::' in the original code. It would be lexed as
25188 `[:', where `[' is a digraph, and there is no whitespace before
25190 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
25193 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
25194 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
25200 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25201 or none_type otherwise. */
25203 static enum tag_types
25204 cp_parser_token_is_class_key (cp_token
* token
)
25206 switch (token
->keyword
)
25211 return record_type
;
25220 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25221 or none_type otherwise or if the token is null. */
25223 static enum tag_types
25224 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25229 switch (token
->keyword
)
25234 return typename_type
;
25241 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25244 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25246 if (type
== error_mark_node
)
25248 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25250 if (permerror (input_location
, "%qs tag used in naming %q#T",
25251 class_key
== union_type
? "union"
25252 : class_key
== record_type
? "struct" : "class",
25254 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25255 "%q#T was previously declared here", type
);
25259 /* Issue an error message if DECL is redeclared with different
25260 access than its original declaration [class.access.spec/3].
25261 This applies to nested classes and nested class templates.
25265 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25267 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25270 if ((TREE_PRIVATE (decl
)
25271 != (current_access_specifier
== access_private_node
))
25272 || (TREE_PROTECTED (decl
)
25273 != (current_access_specifier
== access_protected_node
)))
25274 error_at (location
, "%qD redeclared with different access", decl
);
25277 /* Look for the `template' keyword, as a syntactic disambiguator.
25278 Return TRUE iff it is present, in which case it will be
25282 cp_parser_optional_template_keyword (cp_parser
*parser
)
25284 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25286 /* In C++98 the `template' keyword can only be used within templates;
25287 outside templates the parser can always figure out what is a
25288 template and what is not. In C++11, per the resolution of DR 468,
25289 `template' is allowed in cases where it is not strictly necessary. */
25290 if (!processing_template_decl
25291 && pedantic
&& cxx_dialect
== cxx98
)
25293 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25294 pedwarn (token
->location
, OPT_Wpedantic
,
25295 "in C++98 %<template%> (as a disambiguator) is only "
25296 "allowed within templates");
25297 /* If this part of the token stream is rescanned, the same
25298 error message would be generated. So, we purge the token
25299 from the stream. */
25300 cp_lexer_purge_token (parser
->lexer
);
25305 /* Consume the `template' keyword. */
25306 cp_lexer_consume_token (parser
->lexer
);
25313 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25314 set PARSER->SCOPE, and perform other related actions. */
25317 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25320 struct tree_check
*check_value
;
25321 deferred_access_check
*chk
;
25322 vec
<deferred_access_check
, va_gc
> *checks
;
25324 /* Get the stored value. */
25325 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25326 /* Perform any access checks that were deferred. */
25327 checks
= check_value
->checks
;
25330 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25331 perform_or_defer_access_check (chk
->binfo
,
25333 chk
->diag_decl
, tf_warning_or_error
);
25335 /* Set the scope from the stored value. */
25336 parser
->scope
= check_value
->value
;
25337 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25338 parser
->object_scope
= NULL_TREE
;
25341 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25342 encounter the end of a block before what we were looking for. */
25345 cp_parser_cache_group (cp_parser
*parser
,
25346 enum cpp_ttype end
,
25351 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25353 /* Abort a parenthesized expression if we encounter a semicolon. */
25354 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25355 && token
->type
== CPP_SEMICOLON
)
25357 /* If we've reached the end of the file, stop. */
25358 if (token
->type
== CPP_EOF
25359 || (end
!= CPP_PRAGMA_EOL
25360 && token
->type
== CPP_PRAGMA_EOL
))
25362 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25363 /* We've hit the end of an enclosing block, so there's been some
25364 kind of syntax error. */
25367 /* Consume the token. */
25368 cp_lexer_consume_token (parser
->lexer
);
25369 /* See if it starts a new group. */
25370 if (token
->type
== CPP_OPEN_BRACE
)
25372 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25373 /* In theory this should probably check end == '}', but
25374 cp_parser_save_member_function_body needs it to exit
25375 after either '}' or ')' when called with ')'. */
25379 else if (token
->type
== CPP_OPEN_PAREN
)
25381 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25382 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25385 else if (token
->type
== CPP_PRAGMA
)
25386 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25387 else if (token
->type
== end
)
25392 /* Like above, for caching a default argument or NSDMI. Both of these are
25393 terminated by a non-nested comma, but it can be unclear whether or not a
25394 comma is nested in a template argument list unless we do more parsing.
25395 In order to handle this ambiguity, when we encounter a ',' after a '<'
25396 we try to parse what follows as a parameter-declaration-list (in the
25397 case of a default argument) or a member-declarator (in the case of an
25398 NSDMI). If that succeeds, then we stop caching. */
25401 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25403 unsigned depth
= 0;
25404 int maybe_template_id
= 0;
25405 cp_token
*first_token
;
25407 tree default_argument
;
25409 /* Add tokens until we have processed the entire default
25410 argument. We add the range [first_token, token). */
25411 first_token
= cp_lexer_peek_token (parser
->lexer
);
25412 if (first_token
->type
== CPP_OPEN_BRACE
)
25414 /* For list-initialization, this is straightforward. */
25415 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25416 token
= cp_lexer_peek_token (parser
->lexer
);
25422 /* Peek at the next token. */
25423 token
= cp_lexer_peek_token (parser
->lexer
);
25424 /* What we do depends on what token we have. */
25425 switch (token
->type
)
25427 /* In valid code, a default argument must be
25428 immediately followed by a `,' `)', or `...'. */
25430 if (depth
== 0 && maybe_template_id
)
25432 /* If we've seen a '<', we might be in a
25433 template-argument-list. Until Core issue 325 is
25434 resolved, we don't know how this situation ought
25435 to be handled, so try to DTRT. We check whether
25436 what comes after the comma is a valid parameter
25437 declaration list. If it is, then the comma ends
25438 the default argument; otherwise the default
25439 argument continues. */
25440 bool error
= false;
25443 /* Set ITALP so cp_parser_parameter_declaration_list
25444 doesn't decide to commit to this parse. */
25445 bool saved_italp
= parser
->in_template_argument_list_p
;
25446 parser
->in_template_argument_list_p
= true;
25448 cp_parser_parse_tentatively (parser
);
25452 /* Parse declarators until we reach a non-comma or
25453 somthing that cannot be an initializer.
25454 Just checking whether we're looking at a single
25455 declarator is insufficient. Consider:
25456 int var = tuple<T,U>::x;
25457 The template parameter 'U' looks exactly like a
25461 int ctor_dtor_or_conv_p
;
25462 cp_lexer_consume_token (parser
->lexer
);
25463 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25464 &ctor_dtor_or_conv_p
,
25465 /*parenthesized_p=*/NULL
,
25467 /*friend_p=*/false);
25468 peek
= cp_lexer_peek_token (parser
->lexer
);
25469 if (cp_parser_error_occurred (parser
))
25472 while (peek
->type
== CPP_COMMA
);
25473 /* If we met an '=' or ';' then the original comma
25474 was the end of the NSDMI. Otherwise assume
25475 we're still in the NSDMI. */
25476 error
= (peek
->type
!= CPP_EQ
25477 && peek
->type
!= CPP_SEMICOLON
);
25481 cp_lexer_consume_token (parser
->lexer
);
25482 begin_scope (sk_function_parms
, NULL_TREE
);
25483 cp_parser_parameter_declaration_list (parser
, &error
);
25484 pop_bindings_and_leave_scope ();
25486 if (!cp_parser_error_occurred (parser
) && !error
)
25488 cp_parser_abort_tentative_parse (parser
);
25490 parser
->in_template_argument_list_p
= saved_italp
;
25493 case CPP_CLOSE_PAREN
:
25495 /* If we run into a non-nested `;', `}', or `]',
25496 then the code is invalid -- but the default
25497 argument is certainly over. */
25498 case CPP_SEMICOLON
:
25499 case CPP_CLOSE_BRACE
:
25500 case CPP_CLOSE_SQUARE
:
25502 /* Handle correctly int n = sizeof ... ( p ); */
25503 && token
->type
!= CPP_ELLIPSIS
)
25505 /* Update DEPTH, if necessary. */
25506 else if (token
->type
== CPP_CLOSE_PAREN
25507 || token
->type
== CPP_CLOSE_BRACE
25508 || token
->type
== CPP_CLOSE_SQUARE
)
25512 case CPP_OPEN_PAREN
:
25513 case CPP_OPEN_SQUARE
:
25514 case CPP_OPEN_BRACE
:
25520 /* This might be the comparison operator, or it might
25521 start a template argument list. */
25522 ++maybe_template_id
;
25526 if (cxx_dialect
== cxx98
)
25528 /* Fall through for C++0x, which treats the `>>'
25529 operator like two `>' tokens in certain
25535 /* This might be an operator, or it might close a
25536 template argument list. But if a previous '<'
25537 started a template argument list, this will have
25538 closed it, so we can't be in one anymore. */
25539 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25540 if (maybe_template_id
< 0)
25541 maybe_template_id
= 0;
25545 /* If we run out of tokens, issue an error message. */
25547 case CPP_PRAGMA_EOL
:
25548 error_at (token
->location
, "file ends in default argument");
25554 /* In these cases, we should look for template-ids.
25555 For example, if the default argument is
25556 `X<int, double>()', we need to do name lookup to
25557 figure out whether or not `X' is a template; if
25558 so, the `,' does not end the default argument.
25560 That is not yet done. */
25567 /* If we've reached the end, stop. */
25571 /* Add the token to the token block. */
25572 token
= cp_lexer_consume_token (parser
->lexer
);
25575 /* Create a DEFAULT_ARG to represent the unparsed default
25577 default_argument
= make_node (DEFAULT_ARG
);
25578 DEFARG_TOKENS (default_argument
)
25579 = cp_token_cache_new (first_token
, token
);
25580 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25582 return default_argument
;
25585 /* Begin parsing tentatively. We always save tokens while parsing
25586 tentatively so that if the tentative parsing fails we can restore the
25590 cp_parser_parse_tentatively (cp_parser
* parser
)
25592 /* Enter a new parsing context. */
25593 parser
->context
= cp_parser_context_new (parser
->context
);
25594 /* Begin saving tokens. */
25595 cp_lexer_save_tokens (parser
->lexer
);
25596 /* In order to avoid repetitive access control error messages,
25597 access checks are queued up until we are no longer parsing
25599 push_deferring_access_checks (dk_deferred
);
25602 /* Commit to the currently active tentative parse. */
25605 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25607 cp_parser_context
*context
;
25610 /* Mark all of the levels as committed. */
25611 lexer
= parser
->lexer
;
25612 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25614 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25616 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25617 while (!cp_lexer_saving_tokens (lexer
))
25618 lexer
= lexer
->next
;
25619 cp_lexer_commit_tokens (lexer
);
25623 /* Commit to the topmost currently active tentative parse.
25625 Note that this function shouldn't be called when there are
25626 irreversible side-effects while in a tentative state. For
25627 example, we shouldn't create a permanent entry in the symbol
25628 table, or issue an error message that might not apply if the
25629 tentative parse is aborted. */
25632 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25634 cp_parser_context
*context
= parser
->context
;
25635 cp_lexer
*lexer
= parser
->lexer
;
25639 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25641 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25643 while (!cp_lexer_saving_tokens (lexer
))
25644 lexer
= lexer
->next
;
25645 cp_lexer_commit_tokens (lexer
);
25649 /* Abort the currently active tentative parse. All consumed tokens
25650 will be rolled back, and no diagnostics will be issued. */
25653 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25655 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25656 || errorcount
> 0);
25657 cp_parser_simulate_error (parser
);
25658 /* Now, pretend that we want to see if the construct was
25659 successfully parsed. */
25660 cp_parser_parse_definitely (parser
);
25663 /* Stop parsing tentatively. If a parse error has occurred, restore the
25664 token stream. Otherwise, commit to the tokens we have consumed.
25665 Returns true if no error occurred; false otherwise. */
25668 cp_parser_parse_definitely (cp_parser
* parser
)
25670 bool error_occurred
;
25671 cp_parser_context
*context
;
25673 /* Remember whether or not an error occurred, since we are about to
25674 destroy that information. */
25675 error_occurred
= cp_parser_error_occurred (parser
);
25676 /* Remove the topmost context from the stack. */
25677 context
= parser
->context
;
25678 parser
->context
= context
->next
;
25679 /* If no parse errors occurred, commit to the tentative parse. */
25680 if (!error_occurred
)
25682 /* Commit to the tokens read tentatively, unless that was
25684 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25685 cp_lexer_commit_tokens (parser
->lexer
);
25687 pop_to_parent_deferring_access_checks ();
25689 /* Otherwise, if errors occurred, roll back our state so that things
25690 are just as they were before we began the tentative parse. */
25693 cp_lexer_rollback_tokens (parser
->lexer
);
25694 pop_deferring_access_checks ();
25696 /* Add the context to the front of the free list. */
25697 context
->next
= cp_parser_context_free_list
;
25698 cp_parser_context_free_list
= context
;
25700 return !error_occurred
;
25703 /* Returns true if we are parsing tentatively and are not committed to
25704 this tentative parse. */
25707 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25709 return (cp_parser_parsing_tentatively (parser
)
25710 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25713 /* Returns nonzero iff an error has occurred during the most recent
25714 tentative parse. */
25717 cp_parser_error_occurred (cp_parser
* parser
)
25719 return (cp_parser_parsing_tentatively (parser
)
25720 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25723 /* Returns nonzero if GNU extensions are allowed. */
25726 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25728 return parser
->allow_gnu_extensions_p
;
25731 /* Objective-C++ Productions */
25734 /* Parse an Objective-C expression, which feeds into a primary-expression
25738 objc-message-expression
25739 objc-string-literal
25740 objc-encode-expression
25741 objc-protocol-expression
25742 objc-selector-expression
25744 Returns a tree representation of the expression. */
25747 cp_parser_objc_expression (cp_parser
* parser
)
25749 /* Try to figure out what kind of declaration is present. */
25750 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25754 case CPP_OPEN_SQUARE
:
25755 return cp_parser_objc_message_expression (parser
);
25757 case CPP_OBJC_STRING
:
25758 kwd
= cp_lexer_consume_token (parser
->lexer
);
25759 return objc_build_string_object (kwd
->u
.value
);
25762 switch (kwd
->keyword
)
25764 case RID_AT_ENCODE
:
25765 return cp_parser_objc_encode_expression (parser
);
25767 case RID_AT_PROTOCOL
:
25768 return cp_parser_objc_protocol_expression (parser
);
25770 case RID_AT_SELECTOR
:
25771 return cp_parser_objc_selector_expression (parser
);
25777 error_at (kwd
->location
,
25778 "misplaced %<@%D%> Objective-C++ construct",
25780 cp_parser_skip_to_end_of_block_or_statement (parser
);
25783 return error_mark_node
;
25786 /* Parse an Objective-C message expression.
25788 objc-message-expression:
25789 [ objc-message-receiver objc-message-args ]
25791 Returns a representation of an Objective-C message. */
25794 cp_parser_objc_message_expression (cp_parser
* parser
)
25796 tree receiver
, messageargs
;
25798 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25799 receiver
= cp_parser_objc_message_receiver (parser
);
25800 messageargs
= cp_parser_objc_message_args (parser
);
25801 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25803 return objc_build_message_expr (receiver
, messageargs
);
25806 /* Parse an objc-message-receiver.
25808 objc-message-receiver:
25810 simple-type-specifier
25812 Returns a representation of the type or expression. */
25815 cp_parser_objc_message_receiver (cp_parser
* parser
)
25819 /* An Objective-C message receiver may be either (1) a type
25820 or (2) an expression. */
25821 cp_parser_parse_tentatively (parser
);
25822 rcv
= cp_parser_expression (parser
);
25824 /* If that worked out, fine. */
25825 if (cp_parser_parse_definitely (parser
))
25828 cp_parser_parse_tentatively (parser
);
25829 rcv
= cp_parser_simple_type_specifier (parser
,
25830 /*decl_specs=*/NULL
,
25831 CP_PARSER_FLAGS_NONE
);
25833 if (cp_parser_parse_definitely (parser
))
25834 return objc_get_class_reference (rcv
);
25836 cp_parser_error (parser
, "objective-c++ message receiver expected");
25837 return error_mark_node
;
25840 /* Parse the arguments and selectors comprising an Objective-C message.
25845 objc-selector-args , objc-comma-args
25847 objc-selector-args:
25848 objc-selector [opt] : assignment-expression
25849 objc-selector-args objc-selector [opt] : assignment-expression
25852 assignment-expression
25853 objc-comma-args , assignment-expression
25855 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25856 selector arguments and TREE_VALUE containing a list of comma
25860 cp_parser_objc_message_args (cp_parser
* parser
)
25862 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25863 bool maybe_unary_selector_p
= true;
25864 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25866 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25868 tree selector
= NULL_TREE
, arg
;
25870 if (token
->type
!= CPP_COLON
)
25871 selector
= cp_parser_objc_selector (parser
);
25873 /* Detect if we have a unary selector. */
25874 if (maybe_unary_selector_p
25875 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25876 return build_tree_list (selector
, NULL_TREE
);
25878 maybe_unary_selector_p
= false;
25879 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25880 arg
= cp_parser_assignment_expression (parser
);
25883 = chainon (sel_args
,
25884 build_tree_list (selector
, arg
));
25886 token
= cp_lexer_peek_token (parser
->lexer
);
25889 /* Handle non-selector arguments, if any. */
25890 while (token
->type
== CPP_COMMA
)
25894 cp_lexer_consume_token (parser
->lexer
);
25895 arg
= cp_parser_assignment_expression (parser
);
25898 = chainon (addl_args
,
25899 build_tree_list (NULL_TREE
, arg
));
25901 token
= cp_lexer_peek_token (parser
->lexer
);
25904 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25906 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25907 return build_tree_list (error_mark_node
, error_mark_node
);
25910 return build_tree_list (sel_args
, addl_args
);
25913 /* Parse an Objective-C encode expression.
25915 objc-encode-expression:
25916 @encode objc-typename
25918 Returns an encoded representation of the type argument. */
25921 cp_parser_objc_encode_expression (cp_parser
* parser
)
25926 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25927 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25928 token
= cp_lexer_peek_token (parser
->lexer
);
25929 type
= complete_type (cp_parser_type_id (parser
));
25930 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25934 error_at (token
->location
,
25935 "%<@encode%> must specify a type as an argument");
25936 return error_mark_node
;
25939 /* This happens if we find @encode(T) (where T is a template
25940 typename or something dependent on a template typename) when
25941 parsing a template. In that case, we can't compile it
25942 immediately, but we rather create an AT_ENCODE_EXPR which will
25943 need to be instantiated when the template is used.
25945 if (dependent_type_p (type
))
25947 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25948 TREE_READONLY (value
) = 1;
25952 return objc_build_encode_expr (type
);
25955 /* Parse an Objective-C @defs expression. */
25958 cp_parser_objc_defs_expression (cp_parser
*parser
)
25962 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25963 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25964 name
= cp_parser_identifier (parser
);
25965 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25967 return objc_get_class_ivars (name
);
25970 /* Parse an Objective-C protocol expression.
25972 objc-protocol-expression:
25973 @protocol ( identifier )
25975 Returns a representation of the protocol expression. */
25978 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25982 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25983 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25984 proto
= cp_parser_identifier (parser
);
25985 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25987 return objc_build_protocol_expr (proto
);
25990 /* Parse an Objective-C selector expression.
25992 objc-selector-expression:
25993 @selector ( objc-method-signature )
25995 objc-method-signature:
26001 objc-selector-seq objc-selector :
26003 Returns a representation of the method selector. */
26006 cp_parser_objc_selector_expression (cp_parser
* parser
)
26008 tree sel_seq
= NULL_TREE
;
26009 bool maybe_unary_selector_p
= true;
26011 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26013 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
26014 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26015 token
= cp_lexer_peek_token (parser
->lexer
);
26017 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
26018 || token
->type
== CPP_SCOPE
)
26020 tree selector
= NULL_TREE
;
26022 if (token
->type
!= CPP_COLON
26023 || token
->type
== CPP_SCOPE
)
26024 selector
= cp_parser_objc_selector (parser
);
26026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
26027 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
26029 /* Detect if we have a unary selector. */
26030 if (maybe_unary_selector_p
)
26032 sel_seq
= selector
;
26033 goto finish_selector
;
26037 cp_parser_error (parser
, "expected %<:%>");
26040 maybe_unary_selector_p
= false;
26041 token
= cp_lexer_consume_token (parser
->lexer
);
26043 if (token
->type
== CPP_SCOPE
)
26046 = chainon (sel_seq
,
26047 build_tree_list (selector
, NULL_TREE
));
26049 = chainon (sel_seq
,
26050 build_tree_list (NULL_TREE
, NULL_TREE
));
26054 = chainon (sel_seq
,
26055 build_tree_list (selector
, NULL_TREE
));
26057 token
= cp_lexer_peek_token (parser
->lexer
);
26061 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26063 return objc_build_selector_expr (loc
, sel_seq
);
26066 /* Parse a list of identifiers.
26068 objc-identifier-list:
26070 objc-identifier-list , identifier
26072 Returns a TREE_LIST of identifier nodes. */
26075 cp_parser_objc_identifier_list (cp_parser
* parser
)
26081 identifier
= cp_parser_identifier (parser
);
26082 if (identifier
== error_mark_node
)
26083 return error_mark_node
;
26085 list
= build_tree_list (NULL_TREE
, identifier
);
26086 sep
= cp_lexer_peek_token (parser
->lexer
);
26088 while (sep
->type
== CPP_COMMA
)
26090 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26091 identifier
= cp_parser_identifier (parser
);
26092 if (identifier
== error_mark_node
)
26095 list
= chainon (list
, build_tree_list (NULL_TREE
,
26097 sep
= cp_lexer_peek_token (parser
->lexer
);
26103 /* Parse an Objective-C alias declaration.
26105 objc-alias-declaration:
26106 @compatibility_alias identifier identifier ;
26108 This function registers the alias mapping with the Objective-C front end.
26109 It returns nothing. */
26112 cp_parser_objc_alias_declaration (cp_parser
* parser
)
26116 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
26117 alias
= cp_parser_identifier (parser
);
26118 orig
= cp_parser_identifier (parser
);
26119 objc_declare_alias (alias
, orig
);
26120 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26123 /* Parse an Objective-C class forward-declaration.
26125 objc-class-declaration:
26126 @class objc-identifier-list ;
26128 The function registers the forward declarations with the Objective-C
26129 front end. It returns nothing. */
26132 cp_parser_objc_class_declaration (cp_parser
* parser
)
26134 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
26139 id
= cp_parser_identifier (parser
);
26140 if (id
== error_mark_node
)
26143 objc_declare_class (id
);
26145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26146 cp_lexer_consume_token (parser
->lexer
);
26150 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26153 /* Parse a list of Objective-C protocol references.
26155 objc-protocol-refs-opt:
26156 objc-protocol-refs [opt]
26158 objc-protocol-refs:
26159 < objc-identifier-list >
26161 Returns a TREE_LIST of identifiers, if any. */
26164 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
26166 tree protorefs
= NULL_TREE
;
26168 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
26170 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
26171 protorefs
= cp_parser_objc_identifier_list (parser
);
26172 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
26178 /* Parse a Objective-C visibility specification. */
26181 cp_parser_objc_visibility_spec (cp_parser
* parser
)
26183 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
26185 switch (vis
->keyword
)
26187 case RID_AT_PRIVATE
:
26188 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
26190 case RID_AT_PROTECTED
:
26191 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
26193 case RID_AT_PUBLIC
:
26194 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
26196 case RID_AT_PACKAGE
:
26197 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
26203 /* Eat '@private'/'@protected'/'@public'. */
26204 cp_lexer_consume_token (parser
->lexer
);
26207 /* Parse an Objective-C method type. Return 'true' if it is a class
26208 (+) method, and 'false' if it is an instance (-) method. */
26211 cp_parser_objc_method_type (cp_parser
* parser
)
26213 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
26219 /* Parse an Objective-C protocol qualifier. */
26222 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
26224 tree quals
= NULL_TREE
, node
;
26225 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26227 node
= token
->u
.value
;
26229 while (node
&& identifier_p (node
)
26230 && (node
== ridpointers
[(int) RID_IN
]
26231 || node
== ridpointers
[(int) RID_OUT
]
26232 || node
== ridpointers
[(int) RID_INOUT
]
26233 || node
== ridpointers
[(int) RID_BYCOPY
]
26234 || node
== ridpointers
[(int) RID_BYREF
]
26235 || node
== ridpointers
[(int) RID_ONEWAY
]))
26237 quals
= tree_cons (NULL_TREE
, node
, quals
);
26238 cp_lexer_consume_token (parser
->lexer
);
26239 token
= cp_lexer_peek_token (parser
->lexer
);
26240 node
= token
->u
.value
;
26246 /* Parse an Objective-C typename. */
26249 cp_parser_objc_typename (cp_parser
* parser
)
26251 tree type_name
= NULL_TREE
;
26253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26255 tree proto_quals
, cp_type
= NULL_TREE
;
26257 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26258 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26260 /* An ObjC type name may consist of just protocol qualifiers, in which
26261 case the type shall default to 'id'. */
26262 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26264 cp_type
= cp_parser_type_id (parser
);
26266 /* If the type could not be parsed, an error has already
26267 been produced. For error recovery, behave as if it had
26268 not been specified, which will use the default type
26270 if (cp_type
== error_mark_node
)
26272 cp_type
= NULL_TREE
;
26273 /* We need to skip to the closing parenthesis as
26274 cp_parser_type_id() does not seem to do it for
26276 cp_parser_skip_to_closing_parenthesis (parser
,
26277 /*recovering=*/true,
26278 /*or_comma=*/false,
26279 /*consume_paren=*/false);
26283 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26284 type_name
= build_tree_list (proto_quals
, cp_type
);
26290 /* Check to see if TYPE refers to an Objective-C selector name. */
26293 cp_parser_objc_selector_p (enum cpp_ttype type
)
26295 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26296 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26297 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26298 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26299 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26302 /* Parse an Objective-C selector. */
26305 cp_parser_objc_selector (cp_parser
* parser
)
26307 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26309 if (!cp_parser_objc_selector_p (token
->type
))
26311 error_at (token
->location
, "invalid Objective-C++ selector name");
26312 return error_mark_node
;
26315 /* C++ operator names are allowed to appear in ObjC selectors. */
26316 switch (token
->type
)
26318 case CPP_AND_AND
: return get_identifier ("and");
26319 case CPP_AND_EQ
: return get_identifier ("and_eq");
26320 case CPP_AND
: return get_identifier ("bitand");
26321 case CPP_OR
: return get_identifier ("bitor");
26322 case CPP_COMPL
: return get_identifier ("compl");
26323 case CPP_NOT
: return get_identifier ("not");
26324 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26325 case CPP_OR_OR
: return get_identifier ("or");
26326 case CPP_OR_EQ
: return get_identifier ("or_eq");
26327 case CPP_XOR
: return get_identifier ("xor");
26328 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26329 default: return token
->u
.value
;
26333 /* Parse an Objective-C params list. */
26336 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26338 tree params
= NULL_TREE
;
26339 bool maybe_unary_selector_p
= true;
26340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26342 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26344 tree selector
= NULL_TREE
, type_name
, identifier
;
26345 tree parm_attr
= NULL_TREE
;
26347 if (token
->keyword
== RID_ATTRIBUTE
)
26350 if (token
->type
!= CPP_COLON
)
26351 selector
= cp_parser_objc_selector (parser
);
26353 /* Detect if we have a unary selector. */
26354 if (maybe_unary_selector_p
26355 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26357 params
= selector
; /* Might be followed by attributes. */
26361 maybe_unary_selector_p
= false;
26362 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26364 /* Something went quite wrong. There should be a colon
26365 here, but there is not. Stop parsing parameters. */
26368 type_name
= cp_parser_objc_typename (parser
);
26369 /* New ObjC allows attributes on parameters too. */
26370 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26371 parm_attr
= cp_parser_attributes_opt (parser
);
26372 identifier
= cp_parser_identifier (parser
);
26376 objc_build_keyword_decl (selector
,
26381 token
= cp_lexer_peek_token (parser
->lexer
);
26384 if (params
== NULL_TREE
)
26386 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26387 return error_mark_node
;
26390 /* We allow tail attributes for the method. */
26391 if (token
->keyword
== RID_ATTRIBUTE
)
26393 *attributes
= cp_parser_attributes_opt (parser
);
26394 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26395 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26397 cp_parser_error (parser
,
26398 "method attributes must be specified at the end");
26399 return error_mark_node
;
26402 if (params
== NULL_TREE
)
26404 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26405 return error_mark_node
;
26410 /* Parse the non-keyword Objective-C params. */
26413 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26416 tree params
= make_node (TREE_LIST
);
26417 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26418 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26420 while (token
->type
== CPP_COMMA
)
26422 cp_parameter_declarator
*parmdecl
;
26425 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26426 token
= cp_lexer_peek_token (parser
->lexer
);
26428 if (token
->type
== CPP_ELLIPSIS
)
26430 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26432 token
= cp_lexer_peek_token (parser
->lexer
);
26436 /* TODO: parse attributes for tail parameters. */
26437 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26438 parm
= grokdeclarator (parmdecl
->declarator
,
26439 &parmdecl
->decl_specifiers
,
26440 PARM
, /*initialized=*/0,
26441 /*attrlist=*/NULL
);
26443 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26444 token
= cp_lexer_peek_token (parser
->lexer
);
26447 /* We allow tail attributes for the method. */
26448 if (token
->keyword
== RID_ATTRIBUTE
)
26450 if (*attributes
== NULL_TREE
)
26452 *attributes
= cp_parser_attributes_opt (parser
);
26453 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26454 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26458 /* We have an error, but parse the attributes, so that we can
26460 *attributes
= cp_parser_attributes_opt (parser
);
26462 cp_parser_error (parser
,
26463 "method attributes must be specified at the end");
26464 return error_mark_node
;
26470 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26473 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26475 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26477 /* If the next token is `extern' and the following token is a string
26478 literal, then we have a linkage specification. */
26479 if (token
->keyword
== RID_EXTERN
26480 && cp_parser_is_pure_string_literal
26481 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26482 cp_parser_linkage_specification (parser
);
26483 /* Handle #pragma, if any. */
26484 else if (token
->type
== CPP_PRAGMA
)
26485 cp_parser_pragma (parser
, pragma_objc_icode
);
26486 /* Allow stray semicolons. */
26487 else if (token
->type
== CPP_SEMICOLON
)
26488 cp_lexer_consume_token (parser
->lexer
);
26489 /* Mark methods as optional or required, when building protocols. */
26490 else if (token
->keyword
== RID_AT_OPTIONAL
)
26492 cp_lexer_consume_token (parser
->lexer
);
26493 objc_set_method_opt (true);
26495 else if (token
->keyword
== RID_AT_REQUIRED
)
26497 cp_lexer_consume_token (parser
->lexer
);
26498 objc_set_method_opt (false);
26500 else if (token
->keyword
== RID_NAMESPACE
)
26501 cp_parser_namespace_definition (parser
);
26502 /* Other stray characters must generate errors. */
26503 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26505 cp_lexer_consume_token (parser
->lexer
);
26506 error ("stray %qs between Objective-C++ methods",
26507 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26509 /* Finally, try to parse a block-declaration, or a function-definition. */
26511 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26514 /* Parse a method signature. */
26517 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26519 tree rettype
, kwdparms
, optparms
;
26520 bool ellipsis
= false;
26521 bool is_class_method
;
26523 is_class_method
= cp_parser_objc_method_type (parser
);
26524 rettype
= cp_parser_objc_typename (parser
);
26525 *attributes
= NULL_TREE
;
26526 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26527 if (kwdparms
== error_mark_node
)
26528 return error_mark_node
;
26529 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26530 if (optparms
== error_mark_node
)
26531 return error_mark_node
;
26533 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26537 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26540 cp_lexer_save_tokens (parser
->lexer
);
26541 tattr
= cp_parser_attributes_opt (parser
);
26542 gcc_assert (tattr
) ;
26544 /* If the attributes are followed by a method introducer, this is not allowed.
26545 Dump the attributes and flag the situation. */
26546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26547 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26550 /* Otherwise, the attributes introduce some interstitial code, possibly so
26551 rewind to allow that check. */
26552 cp_lexer_rollback_tokens (parser
->lexer
);
26556 /* Parse an Objective-C method prototype list. */
26559 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26563 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26565 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26567 tree attributes
, sig
;
26568 bool is_class_method
;
26569 if (token
->type
== CPP_PLUS
)
26570 is_class_method
= true;
26572 is_class_method
= false;
26573 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26574 if (sig
== error_mark_node
)
26576 cp_parser_skip_to_end_of_block_or_statement (parser
);
26577 token
= cp_lexer_peek_token (parser
->lexer
);
26580 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26581 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26583 else if (token
->keyword
== RID_AT_PROPERTY
)
26584 cp_parser_objc_at_property_declaration (parser
);
26585 else if (token
->keyword
== RID_ATTRIBUTE
26586 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26587 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26589 "prefix attributes are ignored for methods");
26591 /* Allow for interspersed non-ObjC++ code. */
26592 cp_parser_objc_interstitial_code (parser
);
26594 token
= cp_lexer_peek_token (parser
->lexer
);
26597 if (token
->type
!= CPP_EOF
)
26598 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26600 cp_parser_error (parser
, "expected %<@end%>");
26602 objc_finish_interface ();
26605 /* Parse an Objective-C method definition list. */
26608 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26610 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26612 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26616 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26619 tree sig
, attribute
;
26620 bool is_class_method
;
26621 if (token
->type
== CPP_PLUS
)
26622 is_class_method
= true;
26624 is_class_method
= false;
26625 push_deferring_access_checks (dk_deferred
);
26626 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26627 if (sig
== error_mark_node
)
26629 cp_parser_skip_to_end_of_block_or_statement (parser
);
26630 token
= cp_lexer_peek_token (parser
->lexer
);
26633 objc_start_method_definition (is_class_method
, sig
, attribute
,
26636 /* For historical reasons, we accept an optional semicolon. */
26637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26638 cp_lexer_consume_token (parser
->lexer
);
26640 ptk
= cp_lexer_peek_token (parser
->lexer
);
26641 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26642 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26644 perform_deferred_access_checks (tf_warning_or_error
);
26645 stop_deferring_access_checks ();
26646 meth
= cp_parser_function_definition_after_declarator (parser
,
26648 pop_deferring_access_checks ();
26649 objc_finish_method_definition (meth
);
26652 /* The following case will be removed once @synthesize is
26653 completely implemented. */
26654 else if (token
->keyword
== RID_AT_PROPERTY
)
26655 cp_parser_objc_at_property_declaration (parser
);
26656 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26657 cp_parser_objc_at_synthesize_declaration (parser
);
26658 else if (token
->keyword
== RID_AT_DYNAMIC
)
26659 cp_parser_objc_at_dynamic_declaration (parser
);
26660 else if (token
->keyword
== RID_ATTRIBUTE
26661 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26662 warning_at (token
->location
, OPT_Wattributes
,
26663 "prefix attributes are ignored for methods");
26665 /* Allow for interspersed non-ObjC++ code. */
26666 cp_parser_objc_interstitial_code (parser
);
26668 token
= cp_lexer_peek_token (parser
->lexer
);
26671 if (token
->type
!= CPP_EOF
)
26672 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26674 cp_parser_error (parser
, "expected %<@end%>");
26676 objc_finish_implementation ();
26679 /* Parse Objective-C ivars. */
26682 cp_parser_objc_class_ivars (cp_parser
* parser
)
26684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26686 if (token
->type
!= CPP_OPEN_BRACE
)
26687 return; /* No ivars specified. */
26689 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26690 token
= cp_lexer_peek_token (parser
->lexer
);
26692 while (token
->type
!= CPP_CLOSE_BRACE
26693 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26695 cp_decl_specifier_seq declspecs
;
26696 int decl_class_or_enum_p
;
26697 tree prefix_attributes
;
26699 cp_parser_objc_visibility_spec (parser
);
26701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26704 cp_parser_decl_specifier_seq (parser
,
26705 CP_PARSER_FLAGS_OPTIONAL
,
26707 &decl_class_or_enum_p
);
26709 /* auto, register, static, extern, mutable. */
26710 if (declspecs
.storage_class
!= sc_none
)
26712 cp_parser_error (parser
, "invalid type for instance variable");
26713 declspecs
.storage_class
= sc_none
;
26716 /* thread_local. */
26717 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26719 cp_parser_error (parser
, "invalid type for instance variable");
26720 declspecs
.locations
[ds_thread
] = 0;
26724 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26726 cp_parser_error (parser
, "invalid type for instance variable");
26727 declspecs
.locations
[ds_typedef
] = 0;
26730 prefix_attributes
= declspecs
.attributes
;
26731 declspecs
.attributes
= NULL_TREE
;
26733 /* Keep going until we hit the `;' at the end of the
26735 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26737 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26738 cp_declarator
*declarator
= NULL
;
26739 int ctor_dtor_or_conv_p
;
26741 /* Check for a (possibly unnamed) bitfield declaration. */
26742 token
= cp_lexer_peek_token (parser
->lexer
);
26743 if (token
->type
== CPP_COLON
)
26746 if (token
->type
== CPP_NAME
26747 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26750 /* Get the name of the bitfield. */
26751 declarator
= make_id_declarator (NULL_TREE
,
26752 cp_parser_identifier (parser
),
26756 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26757 /* Get the width of the bitfield. */
26759 = cp_parser_constant_expression (parser
);
26763 /* Parse the declarator. */
26765 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26766 &ctor_dtor_or_conv_p
,
26767 /*parenthesized_p=*/NULL
,
26768 /*member_p=*/false,
26769 /*friend_p=*/false);
26772 /* Look for attributes that apply to the ivar. */
26773 attributes
= cp_parser_attributes_opt (parser
);
26774 /* Remember which attributes are prefix attributes and
26776 first_attribute
= attributes
;
26777 /* Combine the attributes. */
26778 attributes
= chainon (prefix_attributes
, attributes
);
26781 /* Create the bitfield declaration. */
26782 decl
= grokbitfield (declarator
, &declspecs
,
26786 decl
= grokfield (declarator
, &declspecs
,
26787 NULL_TREE
, /*init_const_expr_p=*/false,
26788 NULL_TREE
, attributes
);
26790 /* Add the instance variable. */
26791 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26792 objc_add_instance_variable (decl
);
26794 /* Reset PREFIX_ATTRIBUTES. */
26795 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26796 attributes
= TREE_CHAIN (attributes
);
26798 TREE_CHAIN (attributes
) = NULL_TREE
;
26800 token
= cp_lexer_peek_token (parser
->lexer
);
26802 if (token
->type
== CPP_COMMA
)
26804 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26810 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26811 token
= cp_lexer_peek_token (parser
->lexer
);
26814 if (token
->keyword
== RID_AT_END
)
26815 cp_parser_error (parser
, "expected %<}%>");
26817 /* Do not consume the RID_AT_END, so it will be read again as terminating
26818 the @interface of @implementation. */
26819 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26820 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26822 /* For historical reasons, we accept an optional semicolon. */
26823 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26824 cp_lexer_consume_token (parser
->lexer
);
26827 /* Parse an Objective-C protocol declaration. */
26830 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26832 tree proto
, protorefs
;
26835 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26836 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26838 tok
= cp_lexer_peek_token (parser
->lexer
);
26839 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26840 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26844 /* See if we have a forward declaration or a definition. */
26845 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26847 /* Try a forward declaration first. */
26848 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26854 id
= cp_parser_identifier (parser
);
26855 if (id
== error_mark_node
)
26858 objc_declare_protocol (id
, attributes
);
26860 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26861 cp_lexer_consume_token (parser
->lexer
);
26865 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26868 /* Ok, we got a full-fledged definition (or at least should). */
26871 proto
= cp_parser_identifier (parser
);
26872 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26873 objc_start_protocol (proto
, protorefs
, attributes
);
26874 cp_parser_objc_method_prototype_list (parser
);
26878 /* Parse an Objective-C superclass or category. */
26881 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26884 tree
*categ
, bool *is_class_extension
)
26886 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26888 *super
= *categ
= NULL_TREE
;
26889 *is_class_extension
= false;
26890 if (next
->type
== CPP_COLON
)
26892 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26893 *super
= cp_parser_identifier (parser
);
26895 else if (next
->type
== CPP_OPEN_PAREN
)
26897 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26899 /* If there is no category name, and this is an @interface, we
26900 have a class extension. */
26901 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26903 *categ
= NULL_TREE
;
26904 *is_class_extension
= true;
26907 *categ
= cp_parser_identifier (parser
);
26909 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26913 /* Parse an Objective-C class interface. */
26916 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26918 tree name
, super
, categ
, protos
;
26919 bool is_class_extension
;
26921 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26922 name
= cp_parser_identifier (parser
);
26923 if (name
== error_mark_node
)
26925 /* It's hard to recover because even if valid @interface stuff
26926 is to follow, we can't compile it (or validate it) if we
26927 don't even know which class it refers to. Let's assume this
26928 was a stray '@interface' token in the stream and skip it.
26932 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26933 &is_class_extension
);
26934 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26936 /* We have either a class or a category on our hands. */
26937 if (categ
|| is_class_extension
)
26938 objc_start_category_interface (name
, categ
, protos
, attributes
);
26941 objc_start_class_interface (name
, super
, protos
, attributes
);
26942 /* Handle instance variable declarations, if any. */
26943 cp_parser_objc_class_ivars (parser
);
26944 objc_continue_interface ();
26947 cp_parser_objc_method_prototype_list (parser
);
26950 /* Parse an Objective-C class implementation. */
26953 cp_parser_objc_class_implementation (cp_parser
* parser
)
26955 tree name
, super
, categ
;
26956 bool is_class_extension
;
26958 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26959 name
= cp_parser_identifier (parser
);
26960 if (name
== error_mark_node
)
26962 /* It's hard to recover because even if valid @implementation
26963 stuff is to follow, we can't compile it (or validate it) if
26964 we don't even know which class it refers to. Let's assume
26965 this was a stray '@implementation' token in the stream and
26970 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26971 &is_class_extension
);
26973 /* We have either a class or a category on our hands. */
26975 objc_start_category_implementation (name
, categ
);
26978 objc_start_class_implementation (name
, super
);
26979 /* Handle instance variable declarations, if any. */
26980 cp_parser_objc_class_ivars (parser
);
26981 objc_continue_implementation ();
26984 cp_parser_objc_method_definition_list (parser
);
26987 /* Consume the @end token and finish off the implementation. */
26990 cp_parser_objc_end_implementation (cp_parser
* parser
)
26992 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26993 objc_finish_implementation ();
26996 /* Parse an Objective-C declaration. */
26999 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
27001 /* Try to figure out what kind of declaration is present. */
27002 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27005 switch (kwd
->keyword
)
27010 error_at (kwd
->location
, "attributes may not be specified before"
27011 " the %<@%D%> Objective-C++ keyword",
27015 case RID_AT_IMPLEMENTATION
:
27016 warning_at (kwd
->location
, OPT_Wattributes
,
27017 "prefix attributes are ignored before %<@%D%>",
27024 switch (kwd
->keyword
)
27027 cp_parser_objc_alias_declaration (parser
);
27030 cp_parser_objc_class_declaration (parser
);
27032 case RID_AT_PROTOCOL
:
27033 cp_parser_objc_protocol_declaration (parser
, attributes
);
27035 case RID_AT_INTERFACE
:
27036 cp_parser_objc_class_interface (parser
, attributes
);
27038 case RID_AT_IMPLEMENTATION
:
27039 cp_parser_objc_class_implementation (parser
);
27042 cp_parser_objc_end_implementation (parser
);
27045 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27047 cp_parser_skip_to_end_of_block_or_statement (parser
);
27051 /* Parse an Objective-C try-catch-finally statement.
27053 objc-try-catch-finally-stmt:
27054 @try compound-statement objc-catch-clause-seq [opt]
27055 objc-finally-clause [opt]
27057 objc-catch-clause-seq:
27058 objc-catch-clause objc-catch-clause-seq [opt]
27061 @catch ( objc-exception-declaration ) compound-statement
27063 objc-finally-clause:
27064 @finally compound-statement
27066 objc-exception-declaration:
27067 parameter-declaration
27070 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27074 PS: This function is identical to c_parser_objc_try_catch_finally_statement
27075 for C. Keep them in sync. */
27078 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
27080 location_t location
;
27083 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
27084 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27085 objc_maybe_warn_exceptions (location
);
27086 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27087 node, lest it get absorbed into the surrounding block. */
27088 stmt
= push_stmt_list ();
27089 cp_parser_compound_statement (parser
, NULL
, false, false);
27090 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
27092 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
27094 cp_parameter_declarator
*parm
;
27095 tree parameter_declaration
= error_mark_node
;
27096 bool seen_open_paren
= false;
27098 cp_lexer_consume_token (parser
->lexer
);
27099 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27100 seen_open_paren
= true;
27101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27103 /* We have "@catch (...)" (where the '...' are literally
27104 what is in the code). Skip the '...'.
27105 parameter_declaration is set to NULL_TREE, and
27106 objc_being_catch_clauses() knows that that means
27108 cp_lexer_consume_token (parser
->lexer
);
27109 parameter_declaration
= NULL_TREE
;
27113 /* We have "@catch (NSException *exception)" or something
27114 like that. Parse the parameter declaration. */
27115 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
27117 parameter_declaration
= error_mark_node
;
27119 parameter_declaration
= grokdeclarator (parm
->declarator
,
27120 &parm
->decl_specifiers
,
27121 PARM
, /*initialized=*/0,
27122 /*attrlist=*/NULL
);
27124 if (seen_open_paren
)
27125 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27128 /* If there was no open parenthesis, we are recovering from
27129 an error, and we are trying to figure out what mistake
27130 the user has made. */
27132 /* If there is an immediate closing parenthesis, the user
27133 probably forgot the opening one (ie, they typed "@catch
27134 NSException *e)". Parse the closing parenthesis and keep
27136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
27137 cp_lexer_consume_token (parser
->lexer
);
27139 /* If these is no immediate closing parenthesis, the user
27140 probably doesn't know that parenthesis are required at
27141 all (ie, they typed "@catch NSException *e"). So, just
27142 forget about the closing parenthesis and keep going. */
27144 objc_begin_catch_clause (parameter_declaration
);
27145 cp_parser_compound_statement (parser
, NULL
, false, false);
27146 objc_finish_catch_clause ();
27148 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
27150 cp_lexer_consume_token (parser
->lexer
);
27151 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27152 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27153 node, lest it get absorbed into the surrounding block. */
27154 stmt
= push_stmt_list ();
27155 cp_parser_compound_statement (parser
, NULL
, false, false);
27156 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
27159 return objc_finish_try_stmt ();
27162 /* Parse an Objective-C synchronized statement.
27164 objc-synchronized-stmt:
27165 @synchronized ( expression ) compound-statement
27167 Returns NULL_TREE. */
27170 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
27172 location_t location
;
27175 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
27177 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27178 objc_maybe_warn_exceptions (location
);
27179 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27180 lock
= cp_parser_expression (parser
);
27181 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27183 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27184 node, lest it get absorbed into the surrounding block. */
27185 stmt
= push_stmt_list ();
27186 cp_parser_compound_statement (parser
, NULL
, false, false);
27188 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
27191 /* Parse an Objective-C throw statement.
27194 @throw assignment-expression [opt] ;
27196 Returns a constructed '@throw' statement. */
27199 cp_parser_objc_throw_statement (cp_parser
*parser
)
27201 tree expr
= NULL_TREE
;
27202 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27204 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
27206 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27207 expr
= cp_parser_expression (parser
);
27209 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27211 return objc_build_throw_stmt (loc
, expr
);
27214 /* Parse an Objective-C statement. */
27217 cp_parser_objc_statement (cp_parser
* parser
)
27219 /* Try to figure out what kind of declaration is present. */
27220 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27222 switch (kwd
->keyword
)
27225 return cp_parser_objc_try_catch_finally_statement (parser
);
27226 case RID_AT_SYNCHRONIZED
:
27227 return cp_parser_objc_synchronized_statement (parser
);
27229 return cp_parser_objc_throw_statement (parser
);
27231 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27233 cp_parser_skip_to_end_of_block_or_statement (parser
);
27236 return error_mark_node
;
27239 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27240 look ahead to see if an objc keyword follows the attributes. This
27241 is to detect the use of prefix attributes on ObjC @interface and
27245 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27247 cp_lexer_save_tokens (parser
->lexer
);
27248 *attrib
= cp_parser_attributes_opt (parser
);
27249 gcc_assert (*attrib
);
27250 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27252 cp_lexer_commit_tokens (parser
->lexer
);
27255 cp_lexer_rollback_tokens (parser
->lexer
);
27259 /* This routine is a minimal replacement for
27260 c_parser_struct_declaration () used when parsing the list of
27261 types/names or ObjC++ properties. For example, when parsing the
27264 @property (readonly) int a, b, c;
27266 this function is responsible for parsing "int a, int b, int c" and
27267 returning the declarations as CHAIN of DECLs.
27269 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27270 similar parsing. */
27272 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27274 tree decls
= NULL_TREE
;
27275 cp_decl_specifier_seq declspecs
;
27276 int decl_class_or_enum_p
;
27277 tree prefix_attributes
;
27279 cp_parser_decl_specifier_seq (parser
,
27280 CP_PARSER_FLAGS_NONE
,
27282 &decl_class_or_enum_p
);
27284 if (declspecs
.type
== error_mark_node
)
27285 return error_mark_node
;
27287 /* auto, register, static, extern, mutable. */
27288 if (declspecs
.storage_class
!= sc_none
)
27290 cp_parser_error (parser
, "invalid type for property");
27291 declspecs
.storage_class
= sc_none
;
27294 /* thread_local. */
27295 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27297 cp_parser_error (parser
, "invalid type for property");
27298 declspecs
.locations
[ds_thread
] = 0;
27302 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27304 cp_parser_error (parser
, "invalid type for property");
27305 declspecs
.locations
[ds_typedef
] = 0;
27308 prefix_attributes
= declspecs
.attributes
;
27309 declspecs
.attributes
= NULL_TREE
;
27311 /* Keep going until we hit the `;' at the end of the declaration. */
27312 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27314 tree attributes
, first_attribute
, decl
;
27315 cp_declarator
*declarator
;
27318 /* Parse the declarator. */
27319 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27320 NULL
, NULL
, false, false);
27322 /* Look for attributes that apply to the ivar. */
27323 attributes
= cp_parser_attributes_opt (parser
);
27324 /* Remember which attributes are prefix attributes and
27326 first_attribute
= attributes
;
27327 /* Combine the attributes. */
27328 attributes
= chainon (prefix_attributes
, attributes
);
27330 decl
= grokfield (declarator
, &declspecs
,
27331 NULL_TREE
, /*init_const_expr_p=*/false,
27332 NULL_TREE
, attributes
);
27334 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27335 return error_mark_node
;
27337 /* Reset PREFIX_ATTRIBUTES. */
27338 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27339 attributes
= TREE_CHAIN (attributes
);
27341 TREE_CHAIN (attributes
) = NULL_TREE
;
27343 DECL_CHAIN (decl
) = decls
;
27346 token
= cp_lexer_peek_token (parser
->lexer
);
27347 if (token
->type
== CPP_COMMA
)
27349 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27358 /* Parse an Objective-C @property declaration. The syntax is:
27360 objc-property-declaration:
27361 '@property' objc-property-attributes[opt] struct-declaration ;
27363 objc-property-attributes:
27364 '(' objc-property-attribute-list ')'
27366 objc-property-attribute-list:
27367 objc-property-attribute
27368 objc-property-attribute-list, objc-property-attribute
27370 objc-property-attribute
27371 'getter' = identifier
27372 'setter' = identifier
27381 @property NSString *name;
27382 @property (readonly) id object;
27383 @property (retain, nonatomic, getter=getTheName) id name;
27384 @property int a, b, c;
27386 PS: This function is identical to
27387 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27389 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27391 /* The following variables hold the attributes of the properties as
27392 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27393 seen. When we see an attribute, we set them to 'true' (if they
27394 are boolean properties) or to the identifier (if they have an
27395 argument, ie, for getter and setter). Note that here we only
27396 parse the list of attributes, check the syntax and accumulate the
27397 attributes that we find. objc_add_property_declaration() will
27398 then process the information. */
27399 bool property_assign
= false;
27400 bool property_copy
= false;
27401 tree property_getter_ident
= NULL_TREE
;
27402 bool property_nonatomic
= false;
27403 bool property_readonly
= false;
27404 bool property_readwrite
= false;
27405 bool property_retain
= false;
27406 tree property_setter_ident
= NULL_TREE
;
27408 /* 'properties' is the list of properties that we read. Usually a
27409 single one, but maybe more (eg, in "@property int a, b, c;" there
27414 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27416 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27418 /* Parse the optional attribute list... */
27419 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27422 cp_lexer_consume_token (parser
->lexer
);
27426 bool syntax_error
= false;
27427 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27430 if (token
->type
!= CPP_NAME
)
27432 cp_parser_error (parser
, "expected identifier");
27435 keyword
= C_RID_CODE (token
->u
.value
);
27436 cp_lexer_consume_token (parser
->lexer
);
27439 case RID_ASSIGN
: property_assign
= true; break;
27440 case RID_COPY
: property_copy
= true; break;
27441 case RID_NONATOMIC
: property_nonatomic
= true; break;
27442 case RID_READONLY
: property_readonly
= true; break;
27443 case RID_READWRITE
: property_readwrite
= true; break;
27444 case RID_RETAIN
: property_retain
= true; break;
27448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27450 if (keyword
== RID_GETTER
)
27451 cp_parser_error (parser
,
27452 "missing %<=%> (after %<getter%> attribute)");
27454 cp_parser_error (parser
,
27455 "missing %<=%> (after %<setter%> attribute)");
27456 syntax_error
= true;
27459 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27460 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27462 cp_parser_error (parser
, "expected identifier");
27463 syntax_error
= true;
27466 if (keyword
== RID_SETTER
)
27468 if (property_setter_ident
!= NULL_TREE
)
27470 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27471 cp_lexer_consume_token (parser
->lexer
);
27474 property_setter_ident
= cp_parser_objc_selector (parser
);
27475 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27476 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27478 cp_lexer_consume_token (parser
->lexer
);
27482 if (property_getter_ident
!= NULL_TREE
)
27484 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27485 cp_lexer_consume_token (parser
->lexer
);
27488 property_getter_ident
= cp_parser_objc_selector (parser
);
27492 cp_parser_error (parser
, "unknown property attribute");
27493 syntax_error
= true;
27500 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27501 cp_lexer_consume_token (parser
->lexer
);
27506 /* FIXME: "@property (setter, assign);" will generate a spurious
27507 "error: expected ‘)’ before ‘,’ token". This is because
27508 cp_parser_require, unlike the C counterpart, will produce an
27509 error even if we are in error recovery. */
27510 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27512 cp_parser_skip_to_closing_parenthesis (parser
,
27513 /*recovering=*/true,
27514 /*or_comma=*/false,
27515 /*consume_paren=*/true);
27519 /* ... and the property declaration(s). */
27520 properties
= cp_parser_objc_struct_declaration (parser
);
27522 if (properties
== error_mark_node
)
27524 cp_parser_skip_to_end_of_statement (parser
);
27525 /* If the next token is now a `;', consume it. */
27526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27527 cp_lexer_consume_token (parser
->lexer
);
27531 if (properties
== NULL_TREE
)
27532 cp_parser_error (parser
, "expected identifier");
27535 /* Comma-separated properties are chained together in
27536 reverse order; add them one by one. */
27537 properties
= nreverse (properties
);
27539 for (; properties
; properties
= TREE_CHAIN (properties
))
27540 objc_add_property_declaration (loc
, copy_node (properties
),
27541 property_readonly
, property_readwrite
,
27542 property_assign
, property_retain
,
27543 property_copy
, property_nonatomic
,
27544 property_getter_ident
, property_setter_ident
);
27547 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27550 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27552 objc-synthesize-declaration:
27553 @synthesize objc-synthesize-identifier-list ;
27555 objc-synthesize-identifier-list:
27556 objc-synthesize-identifier
27557 objc-synthesize-identifier-list, objc-synthesize-identifier
27559 objc-synthesize-identifier
27561 identifier = identifier
27564 @synthesize MyProperty;
27565 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27567 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27568 for C. Keep them in sync.
27571 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27573 tree list
= NULL_TREE
;
27575 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27577 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27580 tree property
, ivar
;
27581 property
= cp_parser_identifier (parser
);
27582 if (property
== error_mark_node
)
27584 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27589 cp_lexer_consume_token (parser
->lexer
);
27590 ivar
= cp_parser_identifier (parser
);
27591 if (ivar
== error_mark_node
)
27593 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27599 list
= chainon (list
, build_tree_list (ivar
, property
));
27600 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27601 cp_lexer_consume_token (parser
->lexer
);
27605 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27606 objc_add_synthesize_declaration (loc
, list
);
27609 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27611 objc-dynamic-declaration:
27612 @dynamic identifier-list ;
27615 @dynamic MyProperty;
27616 @dynamic MyProperty, AnotherProperty;
27618 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27619 for C. Keep them in sync.
27622 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27624 tree list
= NULL_TREE
;
27626 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27628 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27632 property
= cp_parser_identifier (parser
);
27633 if (property
== error_mark_node
)
27635 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27638 list
= chainon (list
, build_tree_list (NULL
, property
));
27639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27640 cp_lexer_consume_token (parser
->lexer
);
27644 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27645 objc_add_dynamic_declaration (loc
, list
);
27649 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27651 /* Returns name of the next clause.
27652 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27653 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27654 returned and the token is consumed. */
27656 static pragma_omp_clause
27657 cp_parser_omp_clause_name (cp_parser
*parser
)
27659 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27661 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27662 result
= PRAGMA_OMP_CLAUSE_IF
;
27663 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27664 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27665 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
27666 result
= PRAGMA_OACC_CLAUSE_DELETE
;
27667 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27668 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27669 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27670 result
= PRAGMA_OMP_CLAUSE_FOR
;
27671 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27673 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27674 const char *p
= IDENTIFIER_POINTER (id
);
27679 if (!strcmp ("aligned", p
))
27680 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27681 else if (!strcmp ("async", p
))
27682 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
27685 if (!strcmp ("collapse", p
))
27686 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27687 else if (!strcmp ("copy", p
))
27688 result
= PRAGMA_OACC_CLAUSE_COPY
;
27689 else if (!strcmp ("copyin", p
))
27690 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27691 else if (!strcmp ("copyout", p
))
27692 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
27693 else if (!strcmp ("copyprivate", p
))
27694 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27695 else if (!strcmp ("create", p
))
27696 result
= PRAGMA_OACC_CLAUSE_CREATE
;
27699 if (!strcmp ("depend", p
))
27700 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27701 else if (!strcmp ("device", p
))
27702 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27703 else if (!strcmp ("deviceptr", p
))
27704 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
27705 else if (!strcmp ("dist_schedule", p
))
27706 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27709 if (!strcmp ("final", p
))
27710 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27711 else if (!strcmp ("firstprivate", p
))
27712 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27713 else if (!strcmp ("from", p
))
27714 result
= PRAGMA_OMP_CLAUSE_FROM
;
27717 if (!strcmp ("host", p
))
27718 result
= PRAGMA_OACC_CLAUSE_HOST
;
27721 if (!strcmp ("inbranch", p
))
27722 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27725 if (!strcmp ("lastprivate", p
))
27726 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27727 else if (!strcmp ("linear", p
))
27728 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27731 if (!strcmp ("map", p
))
27732 result
= PRAGMA_OMP_CLAUSE_MAP
;
27733 else if (!strcmp ("mergeable", p
))
27734 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27735 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27736 result
= PRAGMA_CILK_CLAUSE_MASK
;
27739 if (!strcmp ("notinbranch", p
))
27740 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27741 else if (!strcmp ("nowait", p
))
27742 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27743 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27744 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27745 else if (!strcmp ("num_gangs", p
))
27746 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
27747 else if (!strcmp ("num_teams", p
))
27748 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27749 else if (!strcmp ("num_threads", p
))
27750 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27751 else if (!strcmp ("num_workers", p
))
27752 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
27755 if (!strcmp ("ordered", p
))
27756 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27759 if (!strcmp ("parallel", p
))
27760 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27761 else if (!strcmp ("present", p
))
27762 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
27763 else if (!strcmp ("present_or_copy", p
)
27764 || !strcmp ("pcopy", p
))
27765 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
;
27766 else if (!strcmp ("present_or_copyin", p
)
27767 || !strcmp ("pcopyin", p
))
27768 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
;
27769 else if (!strcmp ("present_or_copyout", p
)
27770 || !strcmp ("pcopyout", p
))
27771 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
;
27772 else if (!strcmp ("present_or_create", p
)
27773 || !strcmp ("pcreate", p
))
27774 result
= PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
;
27775 else if (!strcmp ("proc_bind", p
))
27776 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27779 if (!strcmp ("reduction", p
))
27780 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27783 if (!strcmp ("safelen", p
))
27784 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27785 else if (!strcmp ("schedule", p
))
27786 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27787 else if (!strcmp ("sections", p
))
27788 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27789 else if (!strcmp ("self", p
))
27790 result
= PRAGMA_OACC_CLAUSE_SELF
;
27791 else if (!strcmp ("shared", p
))
27792 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27793 else if (!strcmp ("simdlen", p
))
27794 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27797 if (!strcmp ("taskgroup", p
))
27798 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27799 else if (!strcmp ("thread_limit", p
))
27800 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27801 else if (!strcmp ("to", p
))
27802 result
= PRAGMA_OMP_CLAUSE_TO
;
27805 if (!strcmp ("uniform", p
))
27806 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27807 else if (!strcmp ("untied", p
))
27808 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27811 if (!strcmp ("vector_length", p
))
27812 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
27813 else if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27814 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27817 if (!strcmp ("wait", p
))
27818 result
= PRAGMA_OACC_CLAUSE_WAIT
;
27823 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27824 cp_lexer_consume_token (parser
->lexer
);
27829 /* Validate that a clause of the given type does not already exist. */
27832 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27833 const char *name
, location_t location
)
27837 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27838 if (OMP_CLAUSE_CODE (c
) == code
)
27840 error_at (location
, "too many %qs clauses", name
);
27848 variable-list , identifier
27850 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27851 colon). An opening parenthesis will have been consumed by the caller.
27853 If KIND is nonzero, create the appropriate node and install the decl
27854 in OMP_CLAUSE_DECL and add the node to the head of the list.
27856 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27857 return the list created.
27859 COLON can be NULL if only closing parenthesis should end the list,
27860 or pointer to bool which will receive false if the list is terminated
27861 by closing parenthesis or true if the list is terminated by colon. */
27864 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27865 tree list
, bool *colon
)
27868 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27871 parser
->colon_corrects_to_scope_p
= false;
27878 token
= cp_lexer_peek_token (parser
->lexer
);
27879 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27880 /*check_dependency_p=*/true,
27881 /*template_p=*/NULL
,
27882 /*declarator_p=*/false,
27883 /*optional_p=*/false);
27884 if (name
== error_mark_node
)
27887 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27888 if (decl
== error_mark_node
)
27889 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27891 else if (kind
!= 0)
27895 case OMP_CLAUSE__CACHE_
:
27896 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
27898 error_at (token
->location
, "expected %<[%>");
27899 decl
= error_mark_node
;
27902 /* FALL THROUGH. */
27903 case OMP_CLAUSE_MAP
:
27904 case OMP_CLAUSE_FROM
:
27905 case OMP_CLAUSE_TO
:
27906 case OMP_CLAUSE_DEPEND
:
27907 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27909 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27911 parser
->colon_corrects_to_scope_p
= false;
27912 cp_lexer_consume_token (parser
->lexer
);
27913 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27914 low_bound
= cp_parser_expression (parser
);
27916 parser
->colon_corrects_to_scope_p
27917 = saved_colon_corrects_to_scope_p
;
27918 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27919 length
= integer_one_node
;
27922 /* Look for `:'. */
27923 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27925 if (!cp_lexer_next_token_is (parser
->lexer
,
27927 length
= cp_parser_expression (parser
);
27929 /* Look for the closing `]'. */
27930 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27934 if (kind
== OMP_CLAUSE__CACHE_
)
27936 if (TREE_CODE (low_bound
) != INTEGER_CST
27937 && !TREE_READONLY (low_bound
))
27939 error_at (token
->location
,
27940 "%qD is not a constant", low_bound
);
27941 decl
= error_mark_node
;
27944 if (TREE_CODE (length
) != INTEGER_CST
27945 && !TREE_READONLY (length
))
27947 error_at (token
->location
,
27948 "%qD is not a constant", length
);
27949 decl
= error_mark_node
;
27953 decl
= tree_cons (low_bound
, length
, decl
);
27960 tree u
= build_omp_clause (token
->location
, kind
);
27961 OMP_CLAUSE_DECL (u
) = decl
;
27962 OMP_CLAUSE_CHAIN (u
) = list
;
27966 list
= tree_cons (decl
, NULL_TREE
, list
);
27969 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27971 cp_lexer_consume_token (parser
->lexer
);
27975 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27977 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27980 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27984 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27988 /* Try to resync to an unnested comma. Copied from
27989 cp_parser_parenthesized_expression_list. */
27992 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27993 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27994 /*recovering=*/true,
27996 /*consume_paren=*/true);
28004 /* Similarly, but expect leading and trailing parenthesis. This is a very
28005 common case for omp clauses. */
28008 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
28010 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28011 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
28016 copy ( variable-list )
28017 copyin ( variable-list )
28018 copyout ( variable-list )
28019 create ( variable-list )
28020 delete ( variable-list )
28021 present ( variable-list )
28022 present_or_copy ( variable-list )
28023 pcopy ( variable-list )
28024 present_or_copyin ( variable-list )
28025 pcopyin ( variable-list )
28026 present_or_copyout ( variable-list )
28027 pcopyout ( variable-list )
28028 present_or_create ( variable-list )
28029 pcreate ( variable-list ) */
28032 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
28035 enum gomp_map_kind kind
;
28038 case PRAGMA_OACC_CLAUSE_COPY
:
28039 kind
= GOMP_MAP_FORCE_TOFROM
;
28041 case PRAGMA_OACC_CLAUSE_COPYIN
:
28042 kind
= GOMP_MAP_FORCE_TO
;
28044 case PRAGMA_OACC_CLAUSE_COPYOUT
:
28045 kind
= GOMP_MAP_FORCE_FROM
;
28047 case PRAGMA_OACC_CLAUSE_CREATE
:
28048 kind
= GOMP_MAP_FORCE_ALLOC
;
28050 case PRAGMA_OACC_CLAUSE_DELETE
:
28051 kind
= GOMP_MAP_FORCE_DEALLOC
;
28053 case PRAGMA_OACC_CLAUSE_DEVICE
:
28054 kind
= GOMP_MAP_FORCE_TO
;
28056 case PRAGMA_OACC_CLAUSE_HOST
:
28057 case PRAGMA_OACC_CLAUSE_SELF
:
28058 kind
= GOMP_MAP_FORCE_FROM
;
28060 case PRAGMA_OACC_CLAUSE_PRESENT
:
28061 kind
= GOMP_MAP_FORCE_PRESENT
;
28063 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
28064 kind
= GOMP_MAP_TOFROM
;
28066 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
28067 kind
= GOMP_MAP_TO
;
28069 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
28070 kind
= GOMP_MAP_FROM
;
28072 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
28073 kind
= GOMP_MAP_ALLOC
;
28076 gcc_unreachable ();
28079 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
28081 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28082 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
28088 deviceptr ( variable-list ) */
28091 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
28093 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28096 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28097 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28098 variable-list must only allow for pointer variables. */
28099 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
28100 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
28102 tree v
= TREE_PURPOSE (t
);
28104 /* FIXME diagnostics: Ideally we should keep individual
28105 locations for all the variables in the var list to make the
28106 following errors more precise. Perhaps
28107 c_parser_omp_var_list_parens should construct a list of
28108 locations to go along with the var list. */
28111 error_at (loc
, "%qD is not a variable", v
);
28112 else if (TREE_TYPE (v
) == error_mark_node
)
28114 else if (!POINTER_TYPE_P (TREE_TYPE (v
)))
28115 error_at (loc
, "%qD is not a pointer variable", v
);
28117 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
28118 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
28119 OMP_CLAUSE_DECL (u
) = v
;
28120 OMP_CLAUSE_CHAIN (u
) = list
;
28128 vector_length ( expression ) */
28131 cp_parser_oacc_clause_vector_length (cp_parser
*parser
, tree list
)
28134 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28135 bool error
= false;
28137 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28140 t
= cp_parser_condition (parser
);
28141 if (t
== error_mark_node
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28143 error_at (location
, "expected positive integer expression");
28147 if (error
|| !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28149 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28150 /*or_comma=*/false,
28151 /*consume_paren=*/true);
28155 check_no_duplicate_clause (list
, OMP_CLAUSE_VECTOR_LENGTH
, "vector_length",
28158 c
= build_omp_clause (location
, OMP_CLAUSE_VECTOR_LENGTH
);
28159 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c
) = t
;
28160 OMP_CLAUSE_CHAIN (c
) = list
;
28167 Parse wait clause or directive parameters. */
28170 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
28172 vec
<tree
, va_gc
> *args
;
28175 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
28177 /*allow_expansion_p=*/true,
28178 /*non_constant_p=*/NULL
);
28180 if (args
== NULL
|| args
->length () == 0)
28182 cp_parser_error (parser
, "expected integer expression before ')'");
28184 release_tree_vector (args
);
28188 args_tree
= build_tree_list_vec (args
);
28190 release_tree_vector (args
);
28192 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
28194 tree targ
= TREE_VALUE (t
);
28196 if (targ
!= error_mark_node
)
28198 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
28199 error ("%<wait%> expression must be integral");
28202 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
28204 mark_rvalue_use (targ
);
28205 OMP_CLAUSE_DECL (c
) = targ
;
28206 OMP_CLAUSE_CHAIN (c
) = list
;
28216 wait ( int-expr-list ) */
28219 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
28221 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28223 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_PAREN
)
28226 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
28232 collapse ( constant-expression ) */
28235 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
28241 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28242 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28245 num
= cp_parser_constant_expression (parser
);
28247 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28248 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28249 /*or_comma=*/false,
28250 /*consume_paren=*/true);
28252 if (num
== error_mark_node
)
28254 num
= fold_non_dependent_expr (num
);
28255 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
28256 || !tree_fits_shwi_p (num
)
28257 || (n
= tree_to_shwi (num
)) <= 0
28260 error_at (loc
, "collapse argument needs positive constant integer expression");
28264 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
28265 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
28266 OMP_CLAUSE_CHAIN (c
) = list
;
28267 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
28273 default ( shared | none ) */
28276 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
28278 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
28281 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28283 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28285 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28286 const char *p
= IDENTIFIER_POINTER (id
);
28291 if (strcmp ("none", p
) != 0)
28293 kind
= OMP_CLAUSE_DEFAULT_NONE
;
28297 if (strcmp ("shared", p
) != 0)
28299 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
28306 cp_lexer_consume_token (parser
->lexer
);
28311 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
28314 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28315 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28316 /*or_comma=*/false,
28317 /*consume_paren=*/true);
28319 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
28322 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
28323 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
28324 OMP_CLAUSE_CHAIN (c
) = list
;
28325 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
28331 final ( expression ) */
28334 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
28338 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28341 t
= cp_parser_condition (parser
);
28343 if (t
== error_mark_node
28344 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28345 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28346 /*or_comma=*/false,
28347 /*consume_paren=*/true);
28349 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
28351 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
28352 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
28353 OMP_CLAUSE_CHAIN (c
) = list
;
28359 if ( expression ) */
28362 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
28366 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28369 t
= cp_parser_condition (parser
);
28371 if (t
== error_mark_node
28372 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28373 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28374 /*or_comma=*/false,
28375 /*consume_paren=*/true);
28377 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
28379 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
28380 OMP_CLAUSE_IF_EXPR (c
) = t
;
28381 OMP_CLAUSE_CHAIN (c
) = list
;
28390 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
28391 tree list
, location_t location
)
28395 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
28398 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
28399 OMP_CLAUSE_CHAIN (c
) = list
;
28407 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
28408 tree list
, location_t location
)
28412 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
28414 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
28415 OMP_CLAUSE_CHAIN (c
) = list
;
28420 num_gangs ( expression ) */
28423 cp_parser_omp_clause_num_gangs (cp_parser
*parser
, tree list
)
28426 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28428 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28431 t
= cp_parser_condition (parser
);
28433 if (t
== error_mark_node
28434 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28435 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28436 /*or_comma=*/false,
28437 /*consume_paren=*/true);
28439 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28441 error_at (location
, "expected positive integer expression");
28445 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_GANGS
, "num_gangs", location
);
28447 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_GANGS
);
28448 OMP_CLAUSE_NUM_GANGS_EXPR (c
) = t
;
28449 OMP_CLAUSE_CHAIN (c
) = list
;
28456 num_threads ( expression ) */
28459 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
28460 location_t location
)
28464 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28467 t
= cp_parser_expression (parser
);
28469 if (t
== error_mark_node
28470 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28471 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28472 /*or_comma=*/false,
28473 /*consume_paren=*/true);
28475 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
28476 "num_threads", location
);
28478 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
28479 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
28480 OMP_CLAUSE_CHAIN (c
) = list
;
28486 num_workers ( expression ) */
28489 cp_parser_omp_clause_num_workers (cp_parser
*parser
, tree list
)
28492 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
28494 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28497 t
= cp_parser_condition (parser
);
28499 if (t
== error_mark_node
28500 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28501 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28502 /*or_comma=*/false,
28503 /*consume_paren=*/true);
28505 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
28507 error_at (location
, "expected positive integer expression");
28511 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_WORKERS
, "num_gangs",
28514 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_WORKERS
);
28515 OMP_CLAUSE_NUM_WORKERS_EXPR (c
) = t
;
28516 OMP_CLAUSE_CHAIN (c
) = list
;
28526 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
28527 tree list
, location_t location
)
28531 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
28532 "ordered", location
);
28534 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
28535 OMP_CLAUSE_CHAIN (c
) = list
;
28540 reduction ( reduction-operator : variable-list )
28542 reduction-operator:
28543 One of: + * - & ^ | && ||
28547 reduction-operator:
28548 One of: + * - & ^ | && || min max
28552 reduction-operator:
28553 One of: + * - & ^ | && ||
28557 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
28559 enum tree_code code
= ERROR_MARK
;
28560 tree nlist
, c
, id
= NULL_TREE
;
28562 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28565 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28567 case CPP_PLUS
: code
= PLUS_EXPR
; break;
28568 case CPP_MULT
: code
= MULT_EXPR
; break;
28569 case CPP_MINUS
: code
= MINUS_EXPR
; break;
28570 case CPP_AND
: code
= BIT_AND_EXPR
; break;
28571 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
28572 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
28573 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
28574 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
28578 if (code
!= ERROR_MARK
)
28579 cp_lexer_consume_token (parser
->lexer
);
28582 bool saved_colon_corrects_to_scope_p
;
28583 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
28584 parser
->colon_corrects_to_scope_p
= false;
28585 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
28586 /*check_dependency_p=*/true,
28587 /*template_p=*/NULL
,
28588 /*declarator_p=*/false,
28589 /*optional_p=*/false);
28590 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
28591 if (identifier_p (id
))
28593 const char *p
= IDENTIFIER_POINTER (id
);
28595 if (strcmp (p
, "min") == 0)
28597 else if (strcmp (p
, "max") == 0)
28599 else if (id
== ansi_opname (PLUS_EXPR
))
28601 else if (id
== ansi_opname (MULT_EXPR
))
28603 else if (id
== ansi_opname (MINUS_EXPR
))
28605 else if (id
== ansi_opname (BIT_AND_EXPR
))
28606 code
= BIT_AND_EXPR
;
28607 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28608 code
= BIT_IOR_EXPR
;
28609 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28610 code
= BIT_XOR_EXPR
;
28611 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28612 code
= TRUTH_ANDIF_EXPR
;
28613 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28614 code
= TRUTH_ORIF_EXPR
;
28615 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28616 tree scope
= parser
->scope
;
28618 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28619 parser
->scope
= NULL_TREE
;
28620 parser
->qualifying_scope
= NULL_TREE
;
28621 parser
->object_scope
= NULL_TREE
;
28625 error ("invalid reduction-identifier");
28627 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28628 /*or_comma=*/false,
28629 /*consume_paren=*/true);
28634 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28637 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28639 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28641 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28642 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28649 schedule ( schedule-kind )
28650 schedule ( schedule-kind , expression )
28653 static | dynamic | guided | runtime | auto */
28656 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28660 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28663 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28667 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28668 const char *p
= IDENTIFIER_POINTER (id
);
28673 if (strcmp ("dynamic", p
) != 0)
28675 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28679 if (strcmp ("guided", p
) != 0)
28681 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28685 if (strcmp ("runtime", p
) != 0)
28687 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28694 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28695 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28696 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28697 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28700 cp_lexer_consume_token (parser
->lexer
);
28702 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28705 cp_lexer_consume_token (parser
->lexer
);
28707 token
= cp_lexer_peek_token (parser
->lexer
);
28708 t
= cp_parser_assignment_expression (parser
);
28710 if (t
== error_mark_node
)
28712 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28713 error_at (token
->location
, "schedule %<runtime%> does not take "
28714 "a %<chunk_size%> parameter");
28715 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28716 error_at (token
->location
, "schedule %<auto%> does not take "
28717 "a %<chunk_size%> parameter");
28719 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28721 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28724 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28727 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28728 OMP_CLAUSE_CHAIN (c
) = list
;
28732 cp_parser_error (parser
, "invalid schedule kind");
28734 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28735 /*or_comma=*/false,
28736 /*consume_paren=*/true);
28744 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28745 tree list
, location_t location
)
28749 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28751 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28752 OMP_CLAUSE_CHAIN (c
) = list
;
28761 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28762 tree list
, location_t location
)
28764 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28765 tree c
= build_omp_clause (location
, code
);
28766 OMP_CLAUSE_CHAIN (c
) = list
;
28777 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28778 enum omp_clause_code code
,
28779 tree list
, location_t location
)
28781 tree c
= build_omp_clause (location
, code
);
28782 OMP_CLAUSE_CHAIN (c
) = list
;
28787 num_teams ( expression ) */
28790 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28791 location_t location
)
28795 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28798 t
= cp_parser_expression (parser
);
28800 if (t
== error_mark_node
28801 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28802 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28803 /*or_comma=*/false,
28804 /*consume_paren=*/true);
28806 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28807 "num_teams", location
);
28809 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28810 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28811 OMP_CLAUSE_CHAIN (c
) = list
;
28817 thread_limit ( expression ) */
28820 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28821 location_t location
)
28825 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28828 t
= cp_parser_expression (parser
);
28830 if (t
== error_mark_node
28831 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28832 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28833 /*or_comma=*/false,
28834 /*consume_paren=*/true);
28836 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28837 "thread_limit", location
);
28839 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28840 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28841 OMP_CLAUSE_CHAIN (c
) = list
;
28847 aligned ( variable-list )
28848 aligned ( variable-list : constant-expression ) */
28851 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28853 tree nlist
, c
, alignment
= NULL_TREE
;
28856 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28859 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28864 alignment
= cp_parser_constant_expression (parser
);
28866 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28867 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28868 /*or_comma=*/false,
28869 /*consume_paren=*/true);
28871 if (alignment
== error_mark_node
)
28872 alignment
= NULL_TREE
;
28875 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28876 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28882 linear ( variable-list )
28883 linear ( variable-list : expression ) */
28886 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28887 bool is_cilk_simd_fn
)
28889 tree nlist
, c
, step
= integer_one_node
;
28892 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28895 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28900 step
= cp_parser_expression (parser
);
28902 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28904 sorry ("using parameters for %<linear%> step is not supported yet");
28905 step
= integer_one_node
;
28907 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28908 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28909 /*or_comma=*/false,
28910 /*consume_paren=*/true);
28912 if (step
== error_mark_node
)
28916 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28917 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28923 safelen ( constant-expression ) */
28926 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28927 location_t location
)
28931 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28934 t
= cp_parser_constant_expression (parser
);
28936 if (t
== error_mark_node
28937 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28938 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28939 /*or_comma=*/false,
28940 /*consume_paren=*/true);
28942 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28944 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28945 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28946 OMP_CLAUSE_CHAIN (c
) = list
;
28952 simdlen ( constant-expression ) */
28955 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28956 location_t location
)
28960 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28963 t
= cp_parser_constant_expression (parser
);
28965 if (t
== error_mark_node
28966 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28967 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28968 /*or_comma=*/false,
28969 /*consume_paren=*/true);
28971 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28973 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28974 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28975 OMP_CLAUSE_CHAIN (c
) = list
;
28981 depend ( depend-kind : variable-list )
28984 in | out | inout */
28987 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28990 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28992 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28997 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28998 const char *p
= IDENTIFIER_POINTER (id
);
29000 if (strcmp ("in", p
) == 0)
29001 kind
= OMP_CLAUSE_DEPEND_IN
;
29002 else if (strcmp ("inout", p
) == 0)
29003 kind
= OMP_CLAUSE_DEPEND_INOUT
;
29004 else if (strcmp ("out", p
) == 0)
29005 kind
= OMP_CLAUSE_DEPEND_OUT
;
29012 cp_lexer_consume_token (parser
->lexer
);
29013 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
29016 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
29019 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
29020 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
29025 cp_parser_error (parser
, "invalid depend kind");
29027 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29028 /*or_comma=*/false,
29029 /*consume_paren=*/true);
29034 map ( map-kind : variable-list )
29035 map ( variable-list )
29038 alloc | to | from | tofrom */
29041 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
29044 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
29046 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
29050 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
29052 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29053 const char *p
= IDENTIFIER_POINTER (id
);
29055 if (strcmp ("alloc", p
) == 0)
29056 kind
= GOMP_MAP_ALLOC
;
29057 else if (strcmp ("to", p
) == 0)
29058 kind
= GOMP_MAP_TO
;
29059 else if (strcmp ("from", p
) == 0)
29060 kind
= GOMP_MAP_FROM
;
29061 else if (strcmp ("tofrom", p
) == 0)
29062 kind
= GOMP_MAP_TOFROM
;
29065 cp_parser_error (parser
, "invalid map kind");
29066 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29067 /*or_comma=*/false,
29068 /*consume_paren=*/true);
29071 cp_lexer_consume_token (parser
->lexer
);
29072 cp_lexer_consume_token (parser
->lexer
);
29075 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
29078 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
29079 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
29085 device ( expression ) */
29088 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
29089 location_t location
)
29093 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29096 t
= cp_parser_expression (parser
);
29098 if (t
== error_mark_node
29099 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29100 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29101 /*or_comma=*/false,
29102 /*consume_paren=*/true);
29104 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
29105 "device", location
);
29107 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
29108 OMP_CLAUSE_DEVICE_ID (c
) = t
;
29109 OMP_CLAUSE_CHAIN (c
) = list
;
29115 dist_schedule ( static )
29116 dist_schedule ( static , expression ) */
29119 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
29120 location_t location
)
29124 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29127 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
29129 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
29131 cp_lexer_consume_token (parser
->lexer
);
29133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29135 cp_lexer_consume_token (parser
->lexer
);
29137 t
= cp_parser_assignment_expression (parser
);
29139 if (t
== error_mark_node
)
29141 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
29143 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29146 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
29149 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
29151 OMP_CLAUSE_CHAIN (c
) = list
;
29155 cp_parser_error (parser
, "invalid dist_schedule kind");
29157 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29158 /*or_comma=*/false,
29159 /*consume_paren=*/true);
29164 proc_bind ( proc-bind-kind )
29167 master | close | spread */
29170 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
29171 location_t location
)
29174 enum omp_clause_proc_bind_kind kind
;
29176 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29179 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29181 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29182 const char *p
= IDENTIFIER_POINTER (id
);
29184 if (strcmp ("master", p
) == 0)
29185 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
29186 else if (strcmp ("close", p
) == 0)
29187 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
29188 else if (strcmp ("spread", p
) == 0)
29189 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
29196 cp_lexer_consume_token (parser
->lexer
);
29197 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
29200 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
29201 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
29203 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
29204 OMP_CLAUSE_CHAIN (c
) = list
;
29208 cp_parser_error (parser
, "invalid depend kind");
29210 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29211 /*or_comma=*/false,
29212 /*consume_paren=*/true);
29217 async [( int-expr )] */
29220 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
29223 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29225 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
29227 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
29229 cp_lexer_consume_token (parser
->lexer
);
29231 t
= cp_parser_expression (parser
);
29232 if (t
== error_mark_node
29233 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29234 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29235 /*or_comma=*/false,
29236 /*consume_paren=*/true);
29239 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
29241 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
29242 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
29243 OMP_CLAUSE_CHAIN (c
) = list
;
29249 /* Parse all OpenACC clauses. The set clauses allowed by the directive
29250 is a bitmask in MASK. Return the list of clauses found. */
29253 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29254 const char *where
, cp_token
*pragma_tok
,
29255 bool finish_p
= true)
29257 tree clauses
= NULL
;
29260 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29263 pragma_omp_clause c_kind
;
29264 const char *c_name
;
29265 tree prev
= clauses
;
29267 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29268 cp_lexer_consume_token (parser
->lexer
);
29270 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
29271 c_kind
= cp_parser_omp_clause_name (parser
);
29275 case PRAGMA_OACC_CLAUSE_ASYNC
:
29276 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
29279 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
29280 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
29281 c_name
= "collapse";
29283 case PRAGMA_OACC_CLAUSE_COPY
:
29284 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29287 case PRAGMA_OACC_CLAUSE_COPYIN
:
29288 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29291 case PRAGMA_OACC_CLAUSE_COPYOUT
:
29292 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29293 c_name
= "copyout";
29295 case PRAGMA_OACC_CLAUSE_CREATE
:
29296 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29299 case PRAGMA_OACC_CLAUSE_DELETE
:
29300 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29303 case PRAGMA_OACC_CLAUSE_DEVICE
:
29304 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29307 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
29308 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
29309 c_name
= "deviceptr";
29311 case PRAGMA_OACC_CLAUSE_HOST
:
29312 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29315 case PRAGMA_OACC_CLAUSE_IF
:
29316 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
);
29319 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
29320 clauses
= cp_parser_omp_clause_num_gangs (parser
, clauses
);
29321 c_name
= "num_gangs";
29323 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
29324 clauses
= cp_parser_omp_clause_num_workers (parser
, clauses
);
29325 c_name
= "num_workers";
29327 case PRAGMA_OACC_CLAUSE_PRESENT
:
29328 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29329 c_name
= "present";
29331 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY
:
29332 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29333 c_name
= "present_or_copy";
29335 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN
:
29336 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29337 c_name
= "present_or_copyin";
29339 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT
:
29340 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29341 c_name
= "present_or_copyout";
29343 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE
:
29344 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29345 c_name
= "present_or_create";
29347 case PRAGMA_OACC_CLAUSE_REDUCTION
:
29348 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29349 c_name
= "reduction";
29351 case PRAGMA_OACC_CLAUSE_SELF
:
29352 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
29355 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
29356 clauses
= cp_parser_oacc_clause_vector_length (parser
, clauses
);
29357 c_name
= "vector_length";
29359 case PRAGMA_OACC_CLAUSE_WAIT
:
29360 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
29364 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
29370 if (((mask
>> c_kind
) & 1) == 0)
29372 /* Remove the invalid clause(s) from the list to avoid
29373 confusing the rest of the compiler. */
29375 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
29380 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29383 return finish_omp_clauses (clauses
);
29388 /* Parse all OpenMP clauses. The set clauses allowed by the directive
29389 is a bitmask in MASK. Return the list of clauses found; the result
29390 of clause default goes in *pdefault. */
29393 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
29394 const char *where
, cp_token
*pragma_tok
,
29395 bool finish_p
= true)
29397 tree clauses
= NULL
;
29399 cp_token
*token
= NULL
;
29400 bool cilk_simd_fn
= false;
29402 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
29404 pragma_omp_clause c_kind
;
29405 const char *c_name
;
29406 tree prev
= clauses
;
29408 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
29409 cp_lexer_consume_token (parser
->lexer
);
29411 token
= cp_lexer_peek_token (parser
->lexer
);
29412 c_kind
= cp_parser_omp_clause_name (parser
);
29416 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
29417 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
29419 c_name
= "collapse";
29421 case PRAGMA_OMP_CLAUSE_COPYIN
:
29422 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
29425 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
29426 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
29428 c_name
= "copyprivate";
29430 case PRAGMA_OMP_CLAUSE_DEFAULT
:
29431 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
29433 c_name
= "default";
29435 case PRAGMA_OMP_CLAUSE_FINAL
:
29436 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
29439 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
29440 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
29442 c_name
= "firstprivate";
29444 case PRAGMA_OMP_CLAUSE_IF
:
29445 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
29448 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
29449 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
29451 c_name
= "lastprivate";
29453 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
29454 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
29456 c_name
= "mergeable";
29458 case PRAGMA_OMP_CLAUSE_NOWAIT
:
29459 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
29462 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
29463 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
29465 c_name
= "num_threads";
29467 case PRAGMA_OMP_CLAUSE_ORDERED
:
29468 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
29470 c_name
= "ordered";
29472 case PRAGMA_OMP_CLAUSE_PRIVATE
:
29473 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
29475 c_name
= "private";
29477 case PRAGMA_OMP_CLAUSE_REDUCTION
:
29478 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
29479 c_name
= "reduction";
29481 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
29482 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
29484 c_name
= "schedule";
29486 case PRAGMA_OMP_CLAUSE_SHARED
:
29487 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
29491 case PRAGMA_OMP_CLAUSE_UNTIED
:
29492 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
29496 case PRAGMA_OMP_CLAUSE_INBRANCH
:
29497 case PRAGMA_CILK_CLAUSE_MASK
:
29498 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
29499 clauses
, token
->location
);
29500 c_name
= "inbranch";
29502 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
29503 case PRAGMA_CILK_CLAUSE_NOMASK
:
29504 clauses
= cp_parser_omp_clause_branch (parser
,
29505 OMP_CLAUSE_NOTINBRANCH
,
29506 clauses
, token
->location
);
29507 c_name
= "notinbranch";
29509 case PRAGMA_OMP_CLAUSE_PARALLEL
:
29510 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
29511 clauses
, token
->location
);
29512 c_name
= "parallel";
29516 error_at (token
->location
, "%qs must be the first clause of %qs",
29521 case PRAGMA_OMP_CLAUSE_FOR
:
29522 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
29523 clauses
, token
->location
);
29526 goto clause_not_first
;
29528 case PRAGMA_OMP_CLAUSE_SECTIONS
:
29529 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
29530 clauses
, token
->location
);
29531 c_name
= "sections";
29533 goto clause_not_first
;
29535 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
29536 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
29537 clauses
, token
->location
);
29538 c_name
= "taskgroup";
29540 goto clause_not_first
;
29542 case PRAGMA_OMP_CLAUSE_TO
:
29543 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
29547 case PRAGMA_OMP_CLAUSE_FROM
:
29548 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
29552 case PRAGMA_OMP_CLAUSE_UNIFORM
:
29553 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
29555 c_name
= "uniform";
29557 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
29558 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
29560 c_name
= "num_teams";
29562 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
29563 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
29565 c_name
= "thread_limit";
29567 case PRAGMA_OMP_CLAUSE_ALIGNED
:
29568 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
29569 c_name
= "aligned";
29571 case PRAGMA_OMP_CLAUSE_LINEAR
:
29572 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
29573 cilk_simd_fn
= true;
29574 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
29577 case PRAGMA_OMP_CLAUSE_DEPEND
:
29578 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
29581 case PRAGMA_OMP_CLAUSE_MAP
:
29582 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
29585 case PRAGMA_OMP_CLAUSE_DEVICE
:
29586 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
29590 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
29591 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
29593 c_name
= "dist_schedule";
29595 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
29596 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
29598 c_name
= "proc_bind";
29600 case PRAGMA_OMP_CLAUSE_SAFELEN
:
29601 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
29603 c_name
= "safelen";
29605 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
29606 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
29608 c_name
= "simdlen";
29610 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
29611 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
29612 c_name
= "simdlen";
29615 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
29621 if (((mask
>> c_kind
) & 1) == 0)
29623 /* Remove the invalid clause(s) from the list to avoid
29624 confusing the rest of the compiler. */
29626 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
29630 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29631 no reason to skip to the end. */
29632 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
29633 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
29635 return finish_omp_clauses (clauses
);
29643 In practice, we're also interested in adding the statement to an
29644 outer node. So it is convenient if we work around the fact that
29645 cp_parser_statement calls add_stmt. */
29648 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
29650 unsigned save
= parser
->in_statement
;
29652 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29653 This preserves the "not within loop or switch" style error messages
29654 for nonsense cases like
29660 if (parser
->in_statement
)
29661 parser
->in_statement
= IN_OMP_BLOCK
;
29667 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
29669 parser
->in_statement
= save
;
29673 cp_parser_omp_structured_block (cp_parser
*parser
)
29675 tree stmt
= begin_omp_structured_block ();
29676 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
29678 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29680 cp_parser_end_omp_structured_block (parser
, save
);
29681 return finish_omp_structured_block (stmt
);
29685 # pragma omp atomic new-line
29689 x binop= expr | x++ | ++x | x-- | --x
29691 +, *, -, /, &, ^, |, <<, >>
29693 where x is an lvalue expression with scalar type.
29696 # pragma omp atomic new-line
29699 # pragma omp atomic read new-line
29702 # pragma omp atomic write new-line
29705 # pragma omp atomic update new-line
29708 # pragma omp atomic capture new-line
29711 # pragma omp atomic capture new-line
29719 expression-stmt | x = x binop expr
29721 v = expression-stmt
29723 { v = x; update-stmt; } | { update-stmt; v = x; }
29727 expression-stmt | x = x binop expr | x = expr binop x
29731 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29733 where x and v are lvalue expressions with scalar type. */
29736 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
29738 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
29739 tree rhs1
= NULL_TREE
, orig_lhs
;
29740 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
29741 bool structured_block
= false;
29742 bool seq_cst
= false;
29744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29746 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29747 const char *p
= IDENTIFIER_POINTER (id
);
29749 if (!strcmp (p
, "seq_cst"))
29752 cp_lexer_consume_token (parser
->lexer
);
29753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29754 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29755 cp_lexer_consume_token (parser
->lexer
);
29758 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29760 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29761 const char *p
= IDENTIFIER_POINTER (id
);
29763 if (!strcmp (p
, "read"))
29764 code
= OMP_ATOMIC_READ
;
29765 else if (!strcmp (p
, "write"))
29767 else if (!strcmp (p
, "update"))
29769 else if (!strcmp (p
, "capture"))
29770 code
= OMP_ATOMIC_CAPTURE_NEW
;
29774 cp_lexer_consume_token (parser
->lexer
);
29778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29779 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29780 cp_lexer_consume_token (parser
->lexer
);
29782 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29784 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29785 const char *p
= IDENTIFIER_POINTER (id
);
29787 if (!strcmp (p
, "seq_cst"))
29790 cp_lexer_consume_token (parser
->lexer
);
29794 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29798 case OMP_ATOMIC_READ
:
29799 case NOP_EXPR
: /* atomic write */
29800 v
= cp_parser_unary_expression (parser
);
29801 if (v
== error_mark_node
)
29803 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29805 if (code
== NOP_EXPR
)
29806 lhs
= cp_parser_expression (parser
);
29808 lhs
= cp_parser_unary_expression (parser
);
29809 if (lhs
== error_mark_node
)
29811 if (code
== NOP_EXPR
)
29813 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29821 case OMP_ATOMIC_CAPTURE_NEW
:
29822 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29824 cp_lexer_consume_token (parser
->lexer
);
29825 structured_block
= true;
29829 v
= cp_parser_unary_expression (parser
);
29830 if (v
== error_mark_node
)
29832 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29840 lhs
= cp_parser_unary_expression (parser
);
29842 switch (TREE_CODE (lhs
))
29847 case POSTINCREMENT_EXPR
:
29848 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29849 code
= OMP_ATOMIC_CAPTURE_OLD
;
29851 case PREINCREMENT_EXPR
:
29852 lhs
= TREE_OPERAND (lhs
, 0);
29853 opcode
= PLUS_EXPR
;
29854 rhs
= integer_one_node
;
29857 case POSTDECREMENT_EXPR
:
29858 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29859 code
= OMP_ATOMIC_CAPTURE_OLD
;
29861 case PREDECREMENT_EXPR
:
29862 lhs
= TREE_OPERAND (lhs
, 0);
29863 opcode
= MINUS_EXPR
;
29864 rhs
= integer_one_node
;
29867 case COMPOUND_EXPR
:
29868 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29869 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29870 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29871 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29872 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29873 (TREE_OPERAND (lhs
, 1), 0), 0)))
29875 /* Undo effects of boolean_increment for post {in,de}crement. */
29876 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29879 if (TREE_CODE (lhs
) == MODIFY_EXPR
29880 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29882 /* Undo effects of boolean_increment. */
29883 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29885 /* This is pre or post increment. */
29886 rhs
= TREE_OPERAND (lhs
, 1);
29887 lhs
= TREE_OPERAND (lhs
, 0);
29889 if (code
== OMP_ATOMIC_CAPTURE_NEW
29890 && !structured_block
29891 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29892 code
= OMP_ATOMIC_CAPTURE_OLD
;
29898 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29901 opcode
= MULT_EXPR
;
29904 opcode
= TRUNC_DIV_EXPR
;
29907 opcode
= PLUS_EXPR
;
29910 opcode
= MINUS_EXPR
;
29912 case CPP_LSHIFT_EQ
:
29913 opcode
= LSHIFT_EXPR
;
29915 case CPP_RSHIFT_EQ
:
29916 opcode
= RSHIFT_EXPR
;
29919 opcode
= BIT_AND_EXPR
;
29922 opcode
= BIT_IOR_EXPR
;
29925 opcode
= BIT_XOR_EXPR
;
29928 enum cp_parser_prec oprec
;
29930 cp_lexer_consume_token (parser
->lexer
);
29931 cp_parser_parse_tentatively (parser
);
29932 rhs1
= cp_parser_simple_cast_expression (parser
);
29933 if (rhs1
== error_mark_node
)
29935 cp_parser_abort_tentative_parse (parser
);
29936 cp_parser_simple_cast_expression (parser
);
29939 token
= cp_lexer_peek_token (parser
->lexer
);
29940 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29942 cp_parser_abort_tentative_parse (parser
);
29943 cp_parser_parse_tentatively (parser
);
29944 rhs
= cp_parser_binary_expression (parser
, false, true,
29945 PREC_NOT_OPERATOR
, NULL
);
29946 if (rhs
== error_mark_node
)
29948 cp_parser_abort_tentative_parse (parser
);
29949 cp_parser_binary_expression (parser
, false, true,
29950 PREC_NOT_OPERATOR
, NULL
);
29953 switch (TREE_CODE (rhs
))
29956 case TRUNC_DIV_EXPR
:
29965 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29967 if (cp_parser_parse_definitely (parser
))
29969 opcode
= TREE_CODE (rhs
);
29970 rhs1
= TREE_OPERAND (rhs
, 0);
29971 rhs
= TREE_OPERAND (rhs
, 1);
29981 cp_parser_abort_tentative_parse (parser
);
29982 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29984 rhs
= cp_parser_expression (parser
);
29985 if (rhs
== error_mark_node
)
29991 cp_parser_error (parser
,
29992 "invalid form of %<#pragma omp atomic%>");
29995 if (!cp_parser_parse_definitely (parser
))
29997 switch (token
->type
)
29999 case CPP_SEMICOLON
:
30000 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
30002 code
= OMP_ATOMIC_CAPTURE_OLD
;
30007 cp_lexer_consume_token (parser
->lexer
);
30010 else if (structured_block
)
30017 cp_parser_error (parser
,
30018 "invalid form of %<#pragma omp atomic%>");
30021 opcode
= MULT_EXPR
;
30024 opcode
= TRUNC_DIV_EXPR
;
30027 opcode
= PLUS_EXPR
;
30030 opcode
= MINUS_EXPR
;
30033 opcode
= LSHIFT_EXPR
;
30036 opcode
= RSHIFT_EXPR
;
30039 opcode
= BIT_AND_EXPR
;
30042 opcode
= BIT_IOR_EXPR
;
30045 opcode
= BIT_XOR_EXPR
;
30048 cp_parser_error (parser
,
30049 "invalid operator for %<#pragma omp atomic%>");
30052 oprec
= TOKEN_PRECEDENCE (token
);
30053 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
30054 if (commutative_tree_code (opcode
))
30055 oprec
= (enum cp_parser_prec
) (oprec
- 1);
30056 cp_lexer_consume_token (parser
->lexer
);
30057 rhs
= cp_parser_binary_expression (parser
, false, false,
30059 if (rhs
== error_mark_node
)
30064 cp_parser_error (parser
,
30065 "invalid operator for %<#pragma omp atomic%>");
30068 cp_lexer_consume_token (parser
->lexer
);
30070 rhs
= cp_parser_expression (parser
);
30071 if (rhs
== error_mark_node
)
30076 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
30078 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
30080 v
= cp_parser_unary_expression (parser
);
30081 if (v
== error_mark_node
)
30083 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
30085 lhs1
= cp_parser_unary_expression (parser
);
30086 if (lhs1
== error_mark_node
)
30089 if (structured_block
)
30091 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30092 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30095 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
30096 if (!structured_block
)
30097 cp_parser_consume_semicolon_at_end_of_statement (parser
);
30101 cp_parser_skip_to_end_of_block_or_statement (parser
);
30102 if (structured_block
)
30104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30105 cp_lexer_consume_token (parser
->lexer
);
30106 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
30108 cp_parser_skip_to_end_of_block_or_statement (parser
);
30109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30110 cp_lexer_consume_token (parser
->lexer
);
30117 # pragma omp barrier new-line */
30120 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
30122 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30123 finish_omp_barrier ();
30127 # pragma omp critical [(name)] new-line
30128 structured-block */
30131 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
30133 tree stmt
, name
= NULL
;
30135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30137 cp_lexer_consume_token (parser
->lexer
);
30139 name
= cp_parser_identifier (parser
);
30141 if (name
== error_mark_node
30142 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30143 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30144 /*or_comma=*/false,
30145 /*consume_paren=*/true);
30146 if (name
== error_mark_node
)
30149 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30151 stmt
= cp_parser_omp_structured_block (parser
);
30152 return c_finish_omp_critical (input_location
, stmt
, name
);
30156 # pragma omp flush flush-vars[opt] new-line
30159 ( variable-list ) */
30162 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
30164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30165 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30166 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30168 finish_omp_flush ();
30171 /* Helper function, to parse omp for increment expression. */
30174 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
30176 tree cond
= cp_parser_binary_expression (parser
, false, true,
30177 PREC_NOT_OPERATOR
, NULL
);
30178 if (cond
== error_mark_node
30179 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30181 cp_parser_skip_to_end_of_statement (parser
);
30182 return error_mark_node
;
30185 switch (TREE_CODE (cond
))
30193 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30195 /* Fall through: OpenMP disallows NE_EXPR. */
30197 return error_mark_node
;
30200 /* If decl is an iterator, preserve LHS and RHS of the relational
30201 expr until finish_omp_for. */
30203 && (type_dependent_expression_p (decl
)
30204 || CLASS_TYPE_P (TREE_TYPE (decl
))))
30207 return build_x_binary_op (input_location
, TREE_CODE (cond
),
30208 TREE_OPERAND (cond
, 0), ERROR_MARK
,
30209 TREE_OPERAND (cond
, 1), ERROR_MARK
,
30210 /*overload=*/NULL
, tf_warning_or_error
);
30213 /* Helper function, to parse omp for increment expression. */
30216 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
30218 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30224 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30226 op
= (token
->type
== CPP_PLUS_PLUS
30227 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
30228 cp_lexer_consume_token (parser
->lexer
);
30229 lhs
= cp_parser_simple_cast_expression (parser
);
30231 return error_mark_node
;
30232 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30235 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
30237 return error_mark_node
;
30239 token
= cp_lexer_peek_token (parser
->lexer
);
30240 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
30242 op
= (token
->type
== CPP_PLUS_PLUS
30243 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
30244 cp_lexer_consume_token (parser
->lexer
);
30245 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
30248 op
= cp_parser_assignment_operator_opt (parser
);
30249 if (op
== ERROR_MARK
)
30250 return error_mark_node
;
30252 if (op
!= NOP_EXPR
)
30254 rhs
= cp_parser_assignment_expression (parser
);
30255 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
30256 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30259 lhs
= cp_parser_binary_expression (parser
, false, false,
30260 PREC_ADDITIVE_EXPRESSION
, NULL
);
30261 token
= cp_lexer_peek_token (parser
->lexer
);
30262 decl_first
= lhs
== decl
;
30265 if (token
->type
!= CPP_PLUS
30266 && token
->type
!= CPP_MINUS
)
30267 return error_mark_node
;
30271 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
30272 cp_lexer_consume_token (parser
->lexer
);
30273 rhs
= cp_parser_binary_expression (parser
, false, false,
30274 PREC_ADDITIVE_EXPRESSION
, NULL
);
30275 token
= cp_lexer_peek_token (parser
->lexer
);
30276 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
30278 if (lhs
== NULL_TREE
)
30280 if (op
== PLUS_EXPR
)
30283 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
30284 tf_warning_or_error
);
30287 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
30288 ERROR_MARK
, NULL
, tf_warning_or_error
);
30291 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
30295 if (rhs
!= decl
|| op
== MINUS_EXPR
)
30296 return error_mark_node
;
30297 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
30300 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
30302 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
30305 /* Parse the initialization statement of either an OpenMP for loop or
30306 a Cilk Plus for loop.
30308 Return true if the resulting construct should have an
30309 OMP_CLAUSE_PRIVATE added to it. */
30312 cp_parser_omp_for_loop_init (cp_parser
*parser
,
30313 enum tree_code code
,
30314 tree
&this_pre_body
,
30315 vec
<tree
, va_gc
> *for_block
,
30320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30323 bool add_private_clause
= false;
30325 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30329 integer-type var = lb
30330 random-access-iterator-type var = lb
30331 pointer-type var = lb
30333 cp_decl_specifier_seq type_specifiers
;
30335 /* First, try to parse as an initialized declaration. See
30336 cp_parser_condition, from whence the bulk of this is copied. */
30338 cp_parser_parse_tentatively (parser
);
30339 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
30340 /*is_trailing_return=*/false,
30342 if (cp_parser_parse_definitely (parser
))
30344 /* If parsing a type specifier seq succeeded, then this
30345 MUST be a initialized declaration. */
30346 tree asm_specification
, attributes
;
30347 cp_declarator
*declarator
;
30349 declarator
= cp_parser_declarator (parser
,
30350 CP_PARSER_DECLARATOR_NAMED
,
30351 /*ctor_dtor_or_conv_p=*/NULL
,
30352 /*parenthesized_p=*/NULL
,
30353 /*member_p=*/false,
30354 /*friend_p=*/false);
30355 attributes
= cp_parser_attributes_opt (parser
);
30356 asm_specification
= cp_parser_asm_specification_opt (parser
);
30358 if (declarator
== cp_error_declarator
)
30359 cp_parser_skip_to_end_of_statement (parser
);
30363 tree pushed_scope
, auto_node
;
30365 decl
= start_decl (declarator
, &type_specifiers
,
30366 SD_INITIALIZED
, attributes
,
30367 /*prefix_attributes=*/NULL_TREE
,
30370 auto_node
= type_uses_auto (TREE_TYPE (decl
));
30371 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
30373 if (cp_lexer_next_token_is (parser
->lexer
,
30376 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
30377 error ("parenthesized initialization is not allowed in "
30378 "OpenMP %<for%> loop");
30380 error ("parenthesized initialization is "
30381 "not allowed in for-loop");
30384 /* Trigger an error. */
30385 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30387 init
= error_mark_node
;
30388 cp_parser_skip_to_end_of_statement (parser
);
30390 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
30391 || type_dependent_expression_p (decl
)
30394 bool is_direct_init
, is_non_constant_init
;
30396 init
= cp_parser_initializer (parser
,
30398 &is_non_constant_init
);
30403 = do_auto_deduction (TREE_TYPE (decl
), init
,
30406 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
30407 && !type_dependent_expression_p (decl
))
30411 cp_finish_decl (decl
, init
, !is_non_constant_init
,
30413 LOOKUP_ONLYCONVERTING
);
30414 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
30416 vec_safe_push (for_block
, this_pre_body
);
30420 init
= pop_stmt_list (this_pre_body
);
30421 this_pre_body
= NULL_TREE
;
30426 cp_lexer_consume_token (parser
->lexer
);
30427 init
= cp_parser_assignment_expression (parser
);
30430 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
30431 init
= error_mark_node
;
30433 cp_finish_decl (decl
, NULL_TREE
,
30434 /*init_const_expr_p=*/false,
30436 LOOKUP_ONLYCONVERTING
);
30440 pop_scope (pushed_scope
);
30446 /* If parsing a type specifier sequence failed, then
30447 this MUST be a simple expression. */
30448 if (code
== CILK_FOR
)
30449 error ("%<_Cilk_for%> allows expression instead of declaration only "
30450 "in C, not in C++");
30451 cp_parser_parse_tentatively (parser
);
30452 decl
= cp_parser_primary_expression (parser
, false, false,
30454 if (!cp_parser_error_occurred (parser
)
30457 && CLASS_TYPE_P (TREE_TYPE (decl
)))
30461 cp_parser_parse_definitely (parser
);
30462 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
30463 rhs
= cp_parser_assignment_expression (parser
);
30464 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
30467 tf_warning_or_error
));
30468 add_private_clause
= true;
30473 cp_parser_abort_tentative_parse (parser
);
30474 init
= cp_parser_expression (parser
);
30477 if (TREE_CODE (init
) == MODIFY_EXPR
30478 || TREE_CODE (init
) == MODOP_EXPR
)
30479 real_decl
= TREE_OPERAND (init
, 0);
30483 return add_private_clause
;
30486 /* Parse the restricted form of the for statement allowed by OpenMP. */
30489 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
30492 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
30493 tree real_decl
, initv
, condv
, incrv
, declv
;
30494 tree this_pre_body
, cl
;
30495 location_t loc_first
;
30496 bool collapse_err
= false;
30497 int i
, collapse
= 1, nbraces
= 0;
30498 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
30500 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
30501 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
30502 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
30504 gcc_assert (collapse
>= 1);
30506 declv
= make_tree_vec (collapse
);
30507 initv
= make_tree_vec (collapse
);
30508 condv
= make_tree_vec (collapse
);
30509 incrv
= make_tree_vec (collapse
);
30511 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
30513 for (i
= 0; i
< collapse
; i
++)
30515 int bracecount
= 0;
30516 bool add_private_clause
= false;
30519 if (code
!= CILK_FOR
30520 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30522 cp_parser_error (parser
, "for statement expected");
30525 if (code
== CILK_FOR
30526 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
30528 cp_parser_error (parser
, "_Cilk_for statement expected");
30531 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
30533 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
30536 init
= decl
= real_decl
= NULL
;
30537 this_pre_body
= push_stmt_list ();
30540 |= cp_parser_omp_for_loop_init (parser
, code
,
30541 this_pre_body
, for_block
,
30542 init
, decl
, real_decl
);
30544 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30547 this_pre_body
= pop_stmt_list (this_pre_body
);
30551 pre_body
= push_stmt_list ();
30553 add_stmt (this_pre_body
);
30554 pre_body
= pop_stmt_list (pre_body
);
30557 pre_body
= this_pre_body
;
30562 if (cclauses
!= NULL
30563 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
30564 && real_decl
!= NULL_TREE
)
30567 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
30568 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
30569 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30571 error_at (loc
, "iteration variable %qD"
30572 " should not be firstprivate", real_decl
);
30573 *c
= OMP_CLAUSE_CHAIN (*c
);
30575 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
30576 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30578 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
30580 *c
= OMP_CLAUSE_CHAIN (*c
);
30581 if (code
== OMP_SIMD
)
30583 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30584 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
30588 OMP_CLAUSE_CHAIN (l
) = clauses
;
30591 add_private_clause
= false;
30595 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
30596 && OMP_CLAUSE_DECL (*c
) == real_decl
)
30597 add_private_clause
= false;
30598 c
= &OMP_CLAUSE_CHAIN (*c
);
30602 if (add_private_clause
)
30605 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
30607 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
30608 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
30609 && OMP_CLAUSE_DECL (c
) == decl
)
30611 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
30612 && OMP_CLAUSE_DECL (c
) == decl
)
30613 error_at (loc
, "iteration variable %qD "
30614 "should not be firstprivate",
30616 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
30617 && OMP_CLAUSE_DECL (c
) == decl
)
30618 error_at (loc
, "iteration variable %qD should not be reduction",
30623 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
30624 OMP_CLAUSE_DECL (c
) = decl
;
30625 c
= finish_omp_clauses (c
);
30628 OMP_CLAUSE_CHAIN (c
) = clauses
;
30635 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
30636 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
30637 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
30640 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
30642 /* If decl is an iterator, preserve the operator on decl
30643 until finish_omp_for. */
30645 && ((processing_template_decl
30646 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
30647 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
30648 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
30650 incr
= cp_parser_expression (parser
);
30651 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
30652 SET_EXPR_LOCATION (incr
, input_location
);
30655 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30656 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30657 /*or_comma=*/false,
30658 /*consume_paren=*/true);
30660 TREE_VEC_ELT (declv
, i
) = decl
;
30661 TREE_VEC_ELT (initv
, i
) = init
;
30662 TREE_VEC_ELT (condv
, i
) = cond
;
30663 TREE_VEC_ELT (incrv
, i
) = incr
;
30665 if (i
== collapse
- 1)
30668 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30669 in between the collapsed for loops to be still considered perfectly
30670 nested. Hopefully the final version clarifies this.
30671 For now handle (multiple) {'s and empty statements. */
30672 cp_parser_parse_tentatively (parser
);
30675 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30677 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30679 cp_lexer_consume_token (parser
->lexer
);
30682 else if (bracecount
30683 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30684 cp_lexer_consume_token (parser
->lexer
);
30687 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30688 error_at (loc
, "not enough collapsed for loops");
30689 collapse_err
= true;
30690 cp_parser_abort_tentative_parse (parser
);
30699 cp_parser_parse_definitely (parser
);
30700 nbraces
+= bracecount
;
30704 /* Note that we saved the original contents of this flag when we entered
30705 the structured block, and so we don't need to re-save it here. */
30706 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30707 parser
->in_statement
= IN_CILK_SIMD_FOR
;
30709 parser
->in_statement
= IN_OMP_FOR
;
30711 /* Note that the grammar doesn't call for a structured block here,
30712 though the loop as a whole is a structured block. */
30713 body
= push_stmt_list ();
30714 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30715 body
= pop_stmt_list (body
);
30717 if (declv
== NULL_TREE
)
30720 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
30721 pre_body
, clauses
);
30725 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30727 cp_lexer_consume_token (parser
->lexer
);
30730 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30731 cp_lexer_consume_token (parser
->lexer
);
30736 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30737 "collapsed loops not perfectly nested");
30739 collapse_err
= true;
30740 cp_parser_statement_seq_opt (parser
, NULL
);
30741 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30746 while (!for_block
->is_empty ())
30747 add_stmt (pop_stmt_list (for_block
->pop ()));
30748 release_tree_vector (for_block
);
30753 /* Helper function for OpenMP parsing, split clauses and call
30754 finish_omp_clauses on each of the set of clauses afterwards. */
30757 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
30758 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
30761 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
30762 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30764 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30768 #pragma omp simd simd-clause[optseq] new-line
30771 #define OMP_SIMD_CLAUSE_MASK \
30772 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30781 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30782 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30784 tree clauses
, sb
, ret
;
30786 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30788 strcat (p_name
, " simd");
30789 mask
|= OMP_SIMD_CLAUSE_MASK
;
30790 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30792 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30796 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30797 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30800 sb
= begin_omp_structured_block ();
30801 save
= cp_parser_begin_omp_structured_block (parser
);
30803 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30805 cp_parser_end_omp_structured_block (parser
, save
);
30806 add_stmt (finish_omp_structured_block (sb
));
30812 #pragma omp for for-clause[optseq] new-line
30816 #pragma omp for simd for-simd-clause[optseq] new-line
30819 #define OMP_FOR_CLAUSE_MASK \
30820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30830 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30831 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30833 tree clauses
, sb
, ret
;
30835 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30837 strcat (p_name
, " for");
30838 mask
|= OMP_FOR_CLAUSE_MASK
;
30840 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30844 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30845 const char *p
= IDENTIFIER_POINTER (id
);
30847 if (strcmp (p
, "simd") == 0)
30849 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30850 if (cclauses
== NULL
)
30851 cclauses
= cclauses_buf
;
30853 cp_lexer_consume_token (parser
->lexer
);
30854 if (!flag_openmp
) /* flag_openmp_simd */
30855 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30857 sb
= begin_omp_structured_block ();
30858 save
= cp_parser_begin_omp_structured_block (parser
);
30859 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30861 cp_parser_end_omp_structured_block (parser
, save
);
30862 tree body
= finish_omp_structured_block (sb
);
30865 ret
= make_node (OMP_FOR
);
30866 TREE_TYPE (ret
) = void_type_node
;
30867 OMP_FOR_BODY (ret
) = body
;
30868 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30869 SET_EXPR_LOCATION (ret
, loc
);
30874 if (!flag_openmp
) /* flag_openmp_simd */
30876 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30880 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30884 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30885 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30888 sb
= begin_omp_structured_block ();
30889 save
= cp_parser_begin_omp_structured_block (parser
);
30891 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30893 cp_parser_end_omp_structured_block (parser
, save
);
30894 add_stmt (finish_omp_structured_block (sb
));
30900 # pragma omp master new-line
30901 structured-block */
30904 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30906 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30907 return c_finish_omp_master (input_location
,
30908 cp_parser_omp_structured_block (parser
));
30912 # pragma omp ordered new-line
30913 structured-block */
30916 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30918 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30919 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30920 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30926 { section-sequence }
30929 section-directive[opt] structured-block
30930 section-sequence section-directive structured-block */
30933 cp_parser_omp_sections_scope (cp_parser
*parser
)
30935 tree stmt
, substmt
;
30936 bool error_suppress
= false;
30939 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30942 stmt
= push_stmt_list ();
30944 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30946 substmt
= cp_parser_omp_structured_block (parser
);
30947 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30948 add_stmt (substmt
);
30953 tok
= cp_lexer_peek_token (parser
->lexer
);
30954 if (tok
->type
== CPP_CLOSE_BRACE
)
30956 if (tok
->type
== CPP_EOF
)
30959 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30961 cp_lexer_consume_token (parser
->lexer
);
30962 cp_parser_require_pragma_eol (parser
, tok
);
30963 error_suppress
= false;
30965 else if (!error_suppress
)
30967 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30968 error_suppress
= true;
30971 substmt
= cp_parser_omp_structured_block (parser
);
30972 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30973 add_stmt (substmt
);
30975 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30977 substmt
= pop_stmt_list (stmt
);
30979 stmt
= make_node (OMP_SECTIONS
);
30980 TREE_TYPE (stmt
) = void_type_node
;
30981 OMP_SECTIONS_BODY (stmt
) = substmt
;
30988 # pragma omp sections sections-clause[optseq] newline
30991 #define OMP_SECTIONS_CLAUSE_MASK \
30992 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30999 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
31000 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31003 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31005 strcat (p_name
, " sections");
31006 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
31008 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
31010 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31014 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
31015 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
31018 ret
= cp_parser_omp_sections_scope (parser
);
31020 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
31026 # pragma omp parallel parallel-clause[optseq] new-line
31028 # pragma omp parallel for parallel-for-clause[optseq] new-line
31030 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31034 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31035 structured-block */
31037 #define OMP_PARALLEL_CLAUSE_MASK \
31038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
31044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
31046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31049 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
31050 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31052 tree stmt
, clauses
, block
;
31054 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31056 strcat (p_name
, " parallel");
31057 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
31059 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
31061 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31062 if (cclauses
== NULL
)
31063 cclauses
= cclauses_buf
;
31065 cp_lexer_consume_token (parser
->lexer
);
31066 if (!flag_openmp
) /* flag_openmp_simd */
31067 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
31068 block
= begin_omp_parallel ();
31069 save
= cp_parser_begin_omp_structured_block (parser
);
31070 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
31071 cp_parser_end_omp_structured_block (parser
, save
);
31072 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
31074 if (ret
== NULL_TREE
)
31076 OMP_PARALLEL_COMBINED (stmt
) = 1;
31081 error_at (loc
, "expected %<for%> after %qs", p_name
);
31082 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31085 else if (!flag_openmp
) /* flag_openmp_simd */
31087 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31090 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31092 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31093 const char *p
= IDENTIFIER_POINTER (id
);
31094 if (strcmp (p
, "sections") == 0)
31096 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31097 cclauses
= cclauses_buf
;
31099 cp_lexer_consume_token (parser
->lexer
);
31100 block
= begin_omp_parallel ();
31101 save
= cp_parser_begin_omp_structured_block (parser
);
31102 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
31103 cp_parser_end_omp_structured_block (parser
, save
);
31104 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
31106 OMP_PARALLEL_COMBINED (stmt
) = 1;
31111 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
31113 block
= begin_omp_parallel ();
31114 save
= cp_parser_begin_omp_structured_block (parser
);
31115 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31116 cp_parser_end_omp_structured_block (parser
, save
);
31117 stmt
= finish_omp_parallel (clauses
, block
);
31122 # pragma omp single single-clause[optseq] new-line
31123 structured-block */
31125 #define OMP_SINGLE_CLAUSE_MASK \
31126 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
31129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31132 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
31134 tree stmt
= make_node (OMP_SINGLE
);
31135 TREE_TYPE (stmt
) = void_type_node
;
31137 OMP_SINGLE_CLAUSES (stmt
)
31138 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
31139 "#pragma omp single", pragma_tok
);
31140 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31142 return add_stmt (stmt
);
31146 # pragma omp task task-clause[optseq] new-line
31147 structured-block */
31149 #define OMP_TASK_CLAUSE_MASK \
31150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
31151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
31152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
31153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
31157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
31158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31161 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
31163 tree clauses
, block
;
31166 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
31167 "#pragma omp task", pragma_tok
);
31168 block
= begin_omp_task ();
31169 save
= cp_parser_begin_omp_structured_block (parser
);
31170 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31171 cp_parser_end_omp_structured_block (parser
, save
);
31172 return finish_omp_task (clauses
, block
);
31176 # pragma omp taskwait new-line */
31179 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
31181 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31182 finish_omp_taskwait ();
31186 # pragma omp taskyield new-line */
31189 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
31191 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31192 finish_omp_taskyield ();
31196 # pragma omp taskgroup new-line
31197 structured-block */
31200 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
31202 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31203 return c_finish_omp_taskgroup (input_location
,
31204 cp_parser_omp_structured_block (parser
));
31209 # pragma omp threadprivate (variable-list) */
31212 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
31216 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
31217 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31219 finish_omp_threadprivate (vars
);
31223 # pragma omp cancel cancel-clause[optseq] new-line */
31225 #define OMP_CANCEL_CLAUSE_MASK \
31226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
31230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31233 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
31235 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
31236 "#pragma omp cancel", pragma_tok
);
31237 finish_omp_cancel (clauses
);
31241 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
31243 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
31244 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
31245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
31246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
31247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31250 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
31253 bool point_seen
= false;
31255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31257 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31258 const char *p
= IDENTIFIER_POINTER (id
);
31260 if (strcmp (p
, "point") == 0)
31262 cp_lexer_consume_token (parser
->lexer
);
31268 cp_parser_error (parser
, "expected %<point%>");
31269 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31273 clauses
= cp_parser_omp_all_clauses (parser
,
31274 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
31275 "#pragma omp cancellation point",
31277 finish_omp_cancellation_point (clauses
);
31281 #pragma omp distribute distribute-clause[optseq] new-line
31284 #define OMP_DISTRIBUTE_CLAUSE_MASK \
31285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31291 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
31292 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31294 tree clauses
, sb
, ret
;
31296 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31298 strcat (p_name
, " distribute");
31299 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
31301 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31303 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31304 const char *p
= IDENTIFIER_POINTER (id
);
31306 bool parallel
= false;
31308 if (strcmp (p
, "simd") == 0)
31311 parallel
= strcmp (p
, "parallel") == 0;
31312 if (parallel
|| simd
)
31314 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31315 if (cclauses
== NULL
)
31316 cclauses
= cclauses_buf
;
31317 cp_lexer_consume_token (parser
->lexer
);
31318 if (!flag_openmp
) /* flag_openmp_simd */
31321 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31324 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31327 sb
= begin_omp_structured_block ();
31328 save
= cp_parser_begin_omp_structured_block (parser
);
31330 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
31333 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
31335 cp_parser_end_omp_structured_block (parser
, save
);
31336 tree body
= finish_omp_structured_block (sb
);
31339 ret
= make_node (OMP_DISTRIBUTE
);
31340 TREE_TYPE (ret
) = void_type_node
;
31341 OMP_FOR_BODY (ret
) = body
;
31342 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31343 SET_EXPR_LOCATION (ret
, loc
);
31348 if (!flag_openmp
) /* flag_openmp_simd */
31350 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31354 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31358 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
31359 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
31362 sb
= begin_omp_structured_block ();
31363 save
= cp_parser_begin_omp_structured_block (parser
);
31365 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
31367 cp_parser_end_omp_structured_block (parser
, save
);
31368 add_stmt (finish_omp_structured_block (sb
));
31374 # pragma omp teams teams-clause[optseq] new-line
31375 structured-block */
31377 #define OMP_TEAMS_CLAUSE_MASK \
31378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
31379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
31381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
31382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
31383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31387 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
31388 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
31390 tree clauses
, sb
, ret
;
31392 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31394 strcat (p_name
, " teams");
31395 mask
|= OMP_TEAMS_CLAUSE_MASK
;
31397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31399 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31400 const char *p
= IDENTIFIER_POINTER (id
);
31401 if (strcmp (p
, "distribute") == 0)
31403 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
31404 if (cclauses
== NULL
)
31405 cclauses
= cclauses_buf
;
31407 cp_lexer_consume_token (parser
->lexer
);
31408 if (!flag_openmp
) /* flag_openmp_simd */
31409 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31411 sb
= begin_omp_structured_block ();
31412 save
= cp_parser_begin_omp_structured_block (parser
);
31413 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
31415 cp_parser_end_omp_structured_block (parser
, save
);
31416 tree body
= finish_omp_structured_block (sb
);
31419 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31420 ret
= make_node (OMP_TEAMS
);
31421 TREE_TYPE (ret
) = void_type_node
;
31422 OMP_TEAMS_CLAUSES (ret
) = clauses
;
31423 OMP_TEAMS_BODY (ret
) = body
;
31424 OMP_TEAMS_COMBINED (ret
) = 1;
31425 return add_stmt (ret
);
31428 if (!flag_openmp
) /* flag_openmp_simd */
31430 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31434 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
31438 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
31439 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
31442 tree stmt
= make_node (OMP_TEAMS
);
31443 TREE_TYPE (stmt
) = void_type_node
;
31444 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
31445 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31447 return add_stmt (stmt
);
31451 # pragma omp target data target-data-clause[optseq] new-line
31452 structured-block */
31454 #define OMP_TARGET_DATA_CLAUSE_MASK \
31455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31460 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31462 tree stmt
= make_node (OMP_TARGET_DATA
);
31463 TREE_TYPE (stmt
) = void_type_node
;
31465 OMP_TARGET_DATA_CLAUSES (stmt
)
31466 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
31467 "#pragma omp target data", pragma_tok
);
31468 keep_next_level (true);
31469 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31471 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31472 return add_stmt (stmt
);
31476 # pragma omp target update target-update-clause[optseq] new-line */
31478 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
31479 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
31480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
31481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31485 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
31486 enum pragma_context context
)
31488 if (context
== pragma_stmt
)
31490 error_at (pragma_tok
->location
,
31491 "%<#pragma omp target update%> may only be "
31492 "used in compound statements");
31493 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31498 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
31499 "#pragma omp target update", pragma_tok
);
31500 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
31501 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
31503 error_at (pragma_tok
->location
,
31504 "%<#pragma omp target update%> must contain at least one "
31505 "%<from%> or %<to%> clauses");
31509 tree stmt
= make_node (OMP_TARGET_UPDATE
);
31510 TREE_TYPE (stmt
) = void_type_node
;
31511 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
31512 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31518 # pragma omp target target-clause[optseq] new-line
31519 structured-block */
31521 #define OMP_TARGET_CLAUSE_MASK \
31522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
31523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
31524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31527 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
31528 enum pragma_context context
)
31530 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
31532 cp_parser_error (parser
, "expected declaration specifiers");
31533 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31539 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31540 const char *p
= IDENTIFIER_POINTER (id
);
31542 if (strcmp (p
, "teams") == 0)
31544 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
31545 char p_name
[sizeof ("#pragma omp target teams distribute "
31546 "parallel for simd")];
31548 cp_lexer_consume_token (parser
->lexer
);
31549 strcpy (p_name
, "#pragma omp target");
31550 if (!flag_openmp
) /* flag_openmp_simd */
31552 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31553 OMP_TARGET_CLAUSE_MASK
,
31555 return stmt
!= NULL_TREE
;
31557 keep_next_level (true);
31558 tree sb
= begin_omp_structured_block ();
31559 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
31560 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
31561 OMP_TARGET_CLAUSE_MASK
, cclauses
);
31562 cp_parser_end_omp_structured_block (parser
, save
);
31563 tree body
= finish_omp_structured_block (sb
);
31564 if (ret
== NULL_TREE
)
31566 tree stmt
= make_node (OMP_TARGET
);
31567 TREE_TYPE (stmt
) = void_type_node
;
31568 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
31569 OMP_TARGET_BODY (stmt
) = body
;
31573 else if (!flag_openmp
) /* flag_openmp_simd */
31575 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31578 else if (strcmp (p
, "data") == 0)
31580 cp_lexer_consume_token (parser
->lexer
);
31581 cp_parser_omp_target_data (parser
, pragma_tok
);
31584 else if (strcmp (p
, "update") == 0)
31586 cp_lexer_consume_token (parser
->lexer
);
31587 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
31591 tree stmt
= make_node (OMP_TARGET
);
31592 TREE_TYPE (stmt
) = void_type_node
;
31594 OMP_TARGET_CLAUSES (stmt
)
31595 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
31596 "#pragma omp target", pragma_tok
);
31597 keep_next_level (true);
31598 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
31600 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31606 # pragma acc cache (variable-list) new-line
31610 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
31612 tree stmt
, clauses
;
31614 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
31615 clauses
= finish_omp_clauses (clauses
);
31617 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
31619 stmt
= make_node (OACC_CACHE
);
31620 TREE_TYPE (stmt
) = void_type_node
;
31621 OACC_CACHE_CLAUSES (stmt
) = clauses
;
31622 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31629 # pragma acc data oacc-data-clause[optseq] new-line
31630 structured-block */
31632 #define OACC_DATA_CLAUSE_MASK \
31633 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31646 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
)
31648 tree stmt
, clauses
, block
;
31651 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
31652 "#pragma acc data", pragma_tok
);
31654 block
= begin_omp_parallel ();
31655 save
= cp_parser_begin_omp_structured_block (parser
);
31656 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31657 cp_parser_end_omp_structured_block (parser
, save
);
31658 stmt
= finish_oacc_data (clauses
, block
);
31663 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31667 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31669 LOC is the location of the #pragma token.
31672 #define OACC_ENTER_DATA_CLAUSE_MASK \
31673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31681 #define OACC_EXIT_DATA_CLAUSE_MASK \
31682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
31686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31689 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
31692 tree stmt
, clauses
;
31694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
)
31695 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
31697 cp_parser_error (parser
, enter
31698 ? "expected %<data%> in %<#pragma acc enter data%>"
31699 : "expected %<data%> in %<#pragma acc exit data%>");
31700 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31705 IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
31706 if (strcmp (p
, "data") != 0)
31708 cp_parser_error (parser
, "invalid pragma");
31709 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31713 cp_lexer_consume_token (parser
->lexer
);
31716 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
31717 "#pragma acc enter data", pragma_tok
);
31719 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
31720 "#pragma acc exit data", pragma_tok
);
31722 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31724 error_at (pragma_tok
->location
,
31725 "%<#pragma acc enter data%> has no data movement clause");
31729 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
31730 TREE_TYPE (stmt
) = void_type_node
;
31731 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
31732 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31738 # pragma acc kernels oacc-kernels-clause[optseq] new-line
31739 structured-block */
31741 #define OACC_KERNELS_CLAUSE_MASK \
31742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31757 cp_parser_oacc_kernels (cp_parser
*parser
, cp_token
*pragma_tok
)
31759 tree stmt
, clauses
, block
;
31762 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_KERNELS_CLAUSE_MASK
,
31763 "#pragma acc kernels", pragma_tok
);
31765 block
= begin_omp_parallel ();
31766 save
= cp_parser_begin_omp_structured_block (parser
);
31767 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31768 cp_parser_end_omp_structured_block (parser
, save
);
31769 stmt
= finish_oacc_kernels (clauses
, block
);
31774 # pragma acc loop oacc-loop-clause[optseq] new-line
31775 structured-block */
31777 #define OACC_LOOP_CLAUSE_MASK \
31778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
31779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31782 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
)
31784 tree stmt
, clauses
, block
;
31787 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_LOOP_CLAUSE_MASK
,
31788 "#pragma acc loop", pragma_tok
);
31790 block
= begin_omp_structured_block ();
31791 save
= cp_parser_begin_omp_structured_block (parser
);
31792 stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
);
31793 cp_parser_end_omp_structured_block (parser
, save
);
31794 add_stmt (finish_omp_structured_block (block
));
31799 # pragma acc parallel oacc-parallel-clause[optseq] new-line
31800 structured-block */
31802 #define OACC_PARALLEL_CLAUSE_MASK \
31803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
31805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
31806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
31807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
31808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
31809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
31811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
31812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
31813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
31814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
31815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
31816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
31817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
31818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
31819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31822 cp_parser_oacc_parallel (cp_parser
*parser
, cp_token
*pragma_tok
)
31824 tree stmt
, clauses
, block
;
31827 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_PARALLEL_CLAUSE_MASK
,
31828 "#pragma acc parallel", pragma_tok
);
31830 block
= begin_omp_parallel ();
31831 save
= cp_parser_begin_omp_structured_block (parser
);
31832 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
31833 cp_parser_end_omp_structured_block (parser
, save
);
31834 stmt
= finish_oacc_parallel (clauses
, block
);
31839 # pragma acc update oacc-update-clause[optseq] new-line
31842 #define OACC_UPDATE_CLAUSE_MASK \
31843 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
31844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
31845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
31846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
31847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
31848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31851 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
31853 tree stmt
, clauses
;
31855 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
31856 "#pragma acc update", pragma_tok
);
31858 if (find_omp_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
31860 error_at (pragma_tok
->location
,
31861 "%<#pragma acc update%> must contain at least one "
31862 "%<device%> or %<host/self%> clause");
31866 stmt
= make_node (OACC_UPDATE
);
31867 TREE_TYPE (stmt
) = void_type_node
;
31868 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
31869 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31875 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31877 LOC is the location of the #pragma token.
31880 #define OACC_WAIT_CLAUSE_MASK \
31881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31884 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
31886 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
31887 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31889 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31890 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
31892 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
31893 "#pragma acc wait", pragma_tok
);
31895 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
31901 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
31903 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
31904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
31905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
31906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
31907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
31908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
31909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31912 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
31913 enum pragma_context context
)
31915 bool first_p
= parser
->omp_declare_simd
== NULL
;
31916 cp_omp_declare_simd_data data
;
31919 data
.error_seen
= false;
31920 data
.fndecl_seen
= false;
31921 data
.tokens
= vNULL
;
31922 parser
->omp_declare_simd
= &data
;
31924 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31925 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31926 cp_lexer_consume_token (parser
->lexer
);
31927 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31928 parser
->omp_declare_simd
->error_seen
= true;
31929 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31930 struct cp_token_cache
*cp
31931 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
31932 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
31935 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
31936 cp_parser_pragma (parser
, context
);
31939 case pragma_external
:
31940 cp_parser_declaration (parser
);
31942 case pragma_member
:
31943 cp_parser_member_declaration (parser
);
31945 case pragma_objc_icode
:
31946 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31949 cp_parser_declaration_statement (parser
);
31952 if (parser
->omp_declare_simd
31953 && !parser
->omp_declare_simd
->error_seen
31954 && !parser
->omp_declare_simd
->fndecl_seen
)
31955 error_at (pragma_tok
->location
,
31956 "%<#pragma omp declare simd%> not immediately followed by "
31957 "function declaration or definition");
31958 data
.tokens
.release ();
31959 parser
->omp_declare_simd
= NULL
;
31963 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31964 This function is modelled similar to the late parsing of omp declare
31968 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
31970 struct cp_token_cache
*ce
;
31971 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
31974 if (parser
->omp_declare_simd
!= NULL
)
31976 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31977 " marked as a Cilk Plus SIMD-enabled function");
31978 XDELETE (parser
->cilk_simd_fn_info
);
31979 parser
->cilk_simd_fn_info
= NULL
;
31982 if (!info
->error_seen
&& info
->fndecl_seen
)
31984 error ("vector attribute not immediately followed by a single function"
31985 " declaration or definition");
31986 info
->error_seen
= true;
31988 if (info
->error_seen
)
31991 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
31995 cp_parser_push_lexer_for_tokens (parser
, ce
);
31996 parser
->lexer
->in_pragma
= true;
31997 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
31998 "SIMD-enabled functions attribute",
32000 cp_parser_pop_lexer (parser
);
32002 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
32004 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
32005 TREE_CHAIN (c
) = attrs
;
32008 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
32009 TREE_CHAIN (c
) = attrs
;
32010 if (processing_template_decl
)
32011 ATTR_IS_DEPENDENT (c
) = 1;
32014 info
->fndecl_seen
= true;
32015 XDELETE (parser
->cilk_simd_fn_info
);
32016 parser
->cilk_simd_fn_info
= NULL
;
32020 /* Finalize #pragma omp declare simd clauses after direct declarator has
32021 been parsed, and put that into "omp declare simd" attribute. */
32024 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
32026 struct cp_token_cache
*ce
;
32027 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
32030 if (!data
->error_seen
&& data
->fndecl_seen
)
32032 error ("%<#pragma omp declare simd%> not immediately followed by "
32033 "a single function declaration or definition");
32034 data
->error_seen
= true;
32037 if (data
->error_seen
)
32040 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
32044 cp_parser_push_lexer_for_tokens (parser
, ce
);
32045 parser
->lexer
->in_pragma
= true;
32046 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
32047 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
32048 cp_lexer_consume_token (parser
->lexer
);
32049 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
32050 "#pragma omp declare simd", pragma_tok
);
32051 cp_parser_pop_lexer (parser
);
32053 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
32054 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
32055 TREE_CHAIN (c
) = attrs
;
32056 if (processing_template_decl
)
32057 ATTR_IS_DEPENDENT (c
) = 1;
32061 data
->fndecl_seen
= true;
32067 # pragma omp declare target new-line
32068 declarations and definitions
32069 # pragma omp end declare target new-line */
32072 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
32074 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32075 scope_chain
->omp_declare_target_attribute
++;
32079 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
32081 const char *p
= "";
32082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32084 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32085 p
= IDENTIFIER_POINTER (id
);
32087 if (strcmp (p
, "declare") == 0)
32089 cp_lexer_consume_token (parser
->lexer
);
32091 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32093 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32094 p
= IDENTIFIER_POINTER (id
);
32096 if (strcmp (p
, "target") == 0)
32097 cp_lexer_consume_token (parser
->lexer
);
32100 cp_parser_error (parser
, "expected %<target%>");
32101 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32107 cp_parser_error (parser
, "expected %<declare%>");
32108 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32111 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32112 if (!scope_chain
->omp_declare_target_attribute
)
32113 error_at (pragma_tok
->location
,
32114 "%<#pragma omp end declare target%> without corresponding "
32115 "%<#pragma omp declare target%>");
32117 scope_chain
->omp_declare_target_attribute
--;
32120 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
32121 expression and optional initializer clause of
32122 #pragma omp declare reduction. We store the expression(s) as
32123 either 3, 6 or 7 special statements inside of the artificial function's
32124 body. The first two statements are DECL_EXPRs for the artificial
32125 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32126 expression that uses those variables.
32127 If there was any INITIALIZER clause, this is followed by further statements,
32128 the fourth and fifth statements are DECL_EXPRs for the artificial
32129 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
32130 constructor variant (first token after open paren is not omp_priv),
32131 then the sixth statement is a statement with the function call expression
32132 that uses the OMP_PRIV and optionally OMP_ORIG variable.
32133 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32134 to initialize the OMP_PRIV artificial variable and there is seventh
32135 statement, a DECL_EXPR of the OMP_PRIV statement again. */
32138 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
32140 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
32141 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
32142 type
= TREE_TYPE (type
);
32143 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
32144 DECL_ARTIFICIAL (omp_out
) = 1;
32145 pushdecl (omp_out
);
32146 add_decl_expr (omp_out
);
32147 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
32148 DECL_ARTIFICIAL (omp_in
) = 1;
32150 add_decl_expr (omp_in
);
32152 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
32154 keep_next_level (true);
32155 tree block
= begin_omp_structured_block ();
32156 combiner
= cp_parser_expression (parser
);
32157 finish_expr_stmt (combiner
);
32158 block
= finish_omp_structured_block (block
);
32161 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32164 const char *p
= "";
32165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32167 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32168 p
= IDENTIFIER_POINTER (id
);
32171 if (strcmp (p
, "initializer") == 0)
32173 cp_lexer_consume_token (parser
->lexer
);
32174 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32180 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32181 p
= IDENTIFIER_POINTER (id
);
32184 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
32185 DECL_ARTIFICIAL (omp_priv
) = 1;
32186 pushdecl (omp_priv
);
32187 add_decl_expr (omp_priv
);
32188 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
32189 DECL_ARTIFICIAL (omp_orig
) = 1;
32190 pushdecl (omp_orig
);
32191 add_decl_expr (omp_orig
);
32193 keep_next_level (true);
32194 block
= begin_omp_structured_block ();
32197 if (strcmp (p
, "omp_priv") == 0)
32199 bool is_direct_init
, is_non_constant_init
;
32201 cp_lexer_consume_token (parser
->lexer
);
32202 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
32203 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
32204 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32205 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
32207 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
32208 == CPP_CLOSE_PAREN
))
32210 finish_omp_structured_block (block
);
32211 error ("invalid initializer clause");
32214 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
32215 &is_non_constant_init
);
32216 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
32217 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
32221 cp_parser_parse_tentatively (parser
);
32222 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
32223 /*check_dependency_p=*/true,
32224 /*template_p=*/NULL
,
32225 /*declarator_p=*/false,
32226 /*optional_p=*/false);
32227 vec
<tree
, va_gc
> *args
;
32228 if (fn_name
== error_mark_node
32229 || cp_parser_error_occurred (parser
)
32230 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
32231 || ((args
= cp_parser_parenthesized_expression_list
32232 (parser
, non_attr
, /*cast_p=*/false,
32233 /*allow_expansion_p=*/true,
32234 /*non_constant_p=*/NULL
)),
32235 cp_parser_error_occurred (parser
)))
32237 finish_omp_structured_block (block
);
32238 cp_parser_abort_tentative_parse (parser
);
32239 cp_parser_error (parser
, "expected id-expression (arguments)");
32244 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
32245 if (arg
== omp_priv
32246 || (TREE_CODE (arg
) == ADDR_EXPR
32247 && TREE_OPERAND (arg
, 0) == omp_priv
))
32249 cp_parser_abort_tentative_parse (parser
);
32250 if (arg
== NULL_TREE
)
32251 error ("one of the initializer call arguments should be %<omp_priv%>"
32252 " or %<&omp_priv%>");
32253 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
32255 finish_expr_stmt (initializer
);
32258 block
= finish_omp_structured_block (block
);
32259 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
32263 add_decl_expr (omp_orig
);
32265 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32269 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
32270 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
32276 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32277 initializer-clause[opt] new-line
32279 initializer-clause:
32280 initializer (omp_priv initializer)
32281 initializer (function-name (argument-list)) */
32284 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
32285 enum pragma_context
)
32287 auto_vec
<tree
> types
;
32288 enum tree_code reduc_code
= ERROR_MARK
;
32289 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
32291 cp_token
*first_token
;
32292 cp_token_cache
*cp
;
32296 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
32297 p
= obstack_alloc (&declarator_obstack
, 0);
32299 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32302 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
32305 reduc_code
= PLUS_EXPR
;
32308 reduc_code
= MULT_EXPR
;
32311 reduc_code
= MINUS_EXPR
;
32314 reduc_code
= BIT_AND_EXPR
;
32317 reduc_code
= BIT_XOR_EXPR
;
32320 reduc_code
= BIT_IOR_EXPR
;
32323 reduc_code
= TRUTH_ANDIF_EXPR
;
32326 reduc_code
= TRUTH_ORIF_EXPR
;
32329 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
32332 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32333 "%<|%>, %<&&%>, %<||%> or identifier");
32337 if (reduc_code
!= ERROR_MARK
)
32338 cp_lexer_consume_token (parser
->lexer
);
32340 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
32341 if (reduc_id
== error_mark_node
)
32344 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32347 /* Types may not be defined in declare reduction type list. */
32348 const char *saved_message
;
32349 saved_message
= parser
->type_definition_forbidden_message
;
32350 parser
->type_definition_forbidden_message
32351 = G_("types may not be defined in declare reduction type list");
32352 bool saved_colon_corrects_to_scope_p
;
32353 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32354 parser
->colon_corrects_to_scope_p
= false;
32355 bool saved_colon_doesnt_start_class_def_p
;
32356 saved_colon_doesnt_start_class_def_p
32357 = parser
->colon_doesnt_start_class_def_p
;
32358 parser
->colon_doesnt_start_class_def_p
= true;
32362 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32363 type
= cp_parser_type_id (parser
);
32364 if (type
== error_mark_node
)
32366 else if (ARITHMETIC_TYPE_P (type
)
32367 && (orig_reduc_id
== NULL_TREE
32368 || (TREE_CODE (type
) != COMPLEX_TYPE
32369 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32371 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
32373 error_at (loc
, "predeclared arithmetic type %qT in "
32374 "%<#pragma omp declare reduction%>", type
);
32375 else if (TREE_CODE (type
) == FUNCTION_TYPE
32376 || TREE_CODE (type
) == METHOD_TYPE
32377 || TREE_CODE (type
) == ARRAY_TYPE
)
32378 error_at (loc
, "function or array type %qT in "
32379 "%<#pragma omp declare reduction%>", type
);
32380 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
32381 error_at (loc
, "reference type %qT in "
32382 "%<#pragma omp declare reduction%>", type
);
32383 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
32384 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
32385 "%<#pragma omp declare reduction%>", type
);
32387 types
.safe_push (type
);
32389 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32390 cp_lexer_consume_token (parser
->lexer
);
32395 /* Restore the saved message. */
32396 parser
->type_definition_forbidden_message
= saved_message
;
32397 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32398 parser
->colon_doesnt_start_class_def_p
32399 = saved_colon_doesnt_start_class_def_p
;
32401 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
32402 || types
.is_empty ())
32405 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32409 first_token
= cp_lexer_peek_token (parser
->lexer
);
32412 FOR_EACH_VEC_ELT (types
, i
, type
)
32415 = build_function_type_list (void_type_node
,
32416 cp_build_reference_type (type
, false),
32418 tree this_reduc_id
= reduc_id
;
32419 if (!dependent_type_p (type
))
32420 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
32421 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
32422 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
32423 DECL_ARTIFICIAL (fndecl
) = 1;
32424 DECL_EXTERNAL (fndecl
) = 1;
32425 DECL_DECLARED_INLINE_P (fndecl
) = 1;
32426 DECL_IGNORED_P (fndecl
) = 1;
32427 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
32428 DECL_ATTRIBUTES (fndecl
)
32429 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
32430 DECL_ATTRIBUTES (fndecl
));
32431 if (processing_template_decl
)
32432 fndecl
= push_template_decl (fndecl
);
32433 bool block_scope
= false;
32434 tree block
= NULL_TREE
;
32435 if (current_function_decl
)
32437 block_scope
= true;
32438 DECL_CONTEXT (fndecl
) = global_namespace
;
32439 if (!processing_template_decl
)
32442 else if (current_class_type
)
32446 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32447 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
32448 cp_lexer_consume_token (parser
->lexer
);
32449 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
32451 cp
= cp_token_cache_new (first_token
,
32452 cp_lexer_peek_nth_token (parser
->lexer
,
32455 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
32456 finish_member_declaration (fndecl
);
32457 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
32458 DECL_PENDING_INLINE_P (fndecl
) = 1;
32459 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
32464 DECL_CONTEXT (fndecl
) = current_namespace
;
32468 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
32470 block
= begin_omp_structured_block ();
32473 cp_parser_push_lexer_for_tokens (parser
, cp
);
32474 parser
->lexer
->in_pragma
= true;
32476 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
32479 finish_function (0);
32481 DECL_CONTEXT (fndecl
) = current_function_decl
;
32483 cp_parser_pop_lexer (parser
);
32487 cp_parser_pop_lexer (parser
);
32489 finish_function (0);
32492 DECL_CONTEXT (fndecl
) = current_function_decl
;
32493 block
= finish_omp_structured_block (block
);
32494 if (TREE_CODE (block
) == BIND_EXPR
)
32495 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
32496 else if (TREE_CODE (block
) == STATEMENT_LIST
)
32497 DECL_SAVED_TREE (fndecl
) = block
;
32498 if (processing_template_decl
)
32499 add_decl_expr (fndecl
);
32501 cp_check_omp_declare_reduction (fndecl
);
32502 if (cp
== NULL
&& types
.length () > 1)
32503 cp
= cp_token_cache_new (first_token
,
32504 cp_lexer_peek_nth_token (parser
->lexer
, 2));
32505 if (errs
!= errorcount
)
32509 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32512 /* Free any declarators allocated. */
32513 obstack_free (&declarator_obstack
, p
);
32517 #pragma omp declare simd declare-simd-clauses[optseq] new-line
32518 #pragma omp declare reduction (reduction-id : typename-list : expression) \
32519 initializer-clause[opt] new-line
32520 #pragma omp declare target new-line */
32523 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
32524 enum pragma_context context
)
32526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
32528 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
32529 const char *p
= IDENTIFIER_POINTER (id
);
32531 if (strcmp (p
, "simd") == 0)
32533 cp_lexer_consume_token (parser
->lexer
);
32534 cp_parser_omp_declare_simd (parser
, pragma_tok
,
32538 cp_ensure_no_omp_declare_simd (parser
);
32539 if (strcmp (p
, "reduction") == 0)
32541 cp_lexer_consume_token (parser
->lexer
);
32542 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
32546 if (!flag_openmp
) /* flag_openmp_simd */
32548 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32551 if (strcmp (p
, "target") == 0)
32553 cp_lexer_consume_token (parser
->lexer
);
32554 cp_parser_omp_declare_target (parser
, pragma_tok
);
32558 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
32560 cp_parser_require_pragma_eol (parser
, pragma_tok
);
32563 /* Main entry point to OpenMP statement pragmas. */
32566 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
32569 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
32570 omp_clause_mask
mask (0);
32572 switch (pragma_tok
->pragma_kind
)
32574 case PRAGMA_OACC_CACHE
:
32575 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
32577 case PRAGMA_OACC_DATA
:
32578 stmt
= cp_parser_oacc_data (parser
, pragma_tok
);
32580 case PRAGMA_OACC_ENTER_DATA
:
32581 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
32583 case PRAGMA_OACC_EXIT_DATA
:
32584 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
32586 case PRAGMA_OACC_KERNELS
:
32587 stmt
= cp_parser_oacc_kernels (parser
, pragma_tok
);
32589 case PRAGMA_OACC_LOOP
:
32590 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
);
32592 case PRAGMA_OACC_PARALLEL
:
32593 stmt
= cp_parser_oacc_parallel (parser
, pragma_tok
);
32595 case PRAGMA_OACC_UPDATE
:
32596 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
32598 case PRAGMA_OACC_WAIT
:
32599 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
32601 case PRAGMA_OMP_ATOMIC
:
32602 cp_parser_omp_atomic (parser
, pragma_tok
);
32604 case PRAGMA_OMP_CRITICAL
:
32605 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
32607 case PRAGMA_OMP_DISTRIBUTE
:
32608 strcpy (p_name
, "#pragma omp");
32609 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
32611 case PRAGMA_OMP_FOR
:
32612 strcpy (p_name
, "#pragma omp");
32613 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
32615 case PRAGMA_OMP_MASTER
:
32616 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
32618 case PRAGMA_OMP_ORDERED
:
32619 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
32621 case PRAGMA_OMP_PARALLEL
:
32622 strcpy (p_name
, "#pragma omp");
32623 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
32625 case PRAGMA_OMP_SECTIONS
:
32626 strcpy (p_name
, "#pragma omp");
32627 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
32629 case PRAGMA_OMP_SIMD
:
32630 strcpy (p_name
, "#pragma omp");
32631 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
32633 case PRAGMA_OMP_SINGLE
:
32634 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
32636 case PRAGMA_OMP_TASK
:
32637 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
32639 case PRAGMA_OMP_TASKGROUP
:
32640 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
32642 case PRAGMA_OMP_TEAMS
:
32643 strcpy (p_name
, "#pragma omp");
32644 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
32647 gcc_unreachable ();
32651 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
32654 /* Transactional Memory parsing routines. */
32656 /* Parse a transaction attribute.
32662 ??? Simplify this when C++0x bracket attributes are
32663 implemented properly. */
32666 cp_parser_txn_attribute_opt (cp_parser
*parser
)
32669 tree attr_name
, attr
= NULL
;
32671 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32672 return cp_parser_attributes_opt (parser
);
32674 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
32676 cp_lexer_consume_token (parser
->lexer
);
32677 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
32680 token
= cp_lexer_peek_token (parser
->lexer
);
32681 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
32683 token
= cp_lexer_consume_token (parser
->lexer
);
32685 attr_name
= (token
->type
== CPP_KEYWORD
32686 /* For keywords, use the canonical spelling,
32687 not the parsed identifier. */
32688 ? ridpointers
[(int) token
->keyword
]
32690 attr
= build_tree_list (attr_name
, NULL_TREE
);
32693 cp_parser_error (parser
, "expected identifier");
32695 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32697 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32701 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32703 transaction-statement:
32704 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32706 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32710 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
32712 unsigned char old_in
= parser
->in_transaction
;
32713 unsigned char this_in
= 1, new_in
;
32715 tree stmt
, attrs
, noex
;
32717 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32718 || keyword
== RID_TRANSACTION_RELAXED
);
32719 token
= cp_parser_require_keyword (parser
, keyword
,
32720 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32721 : RT_TRANSACTION_RELAXED
));
32722 gcc_assert (token
!= NULL
);
32724 if (keyword
== RID_TRANSACTION_RELAXED
)
32725 this_in
|= TM_STMT_ATTR_RELAXED
;
32728 attrs
= cp_parser_txn_attribute_opt (parser
);
32730 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32733 /* Parse a noexcept specification. */
32734 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
32736 /* Keep track if we're in the lexical scope of an outer transaction. */
32737 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
32739 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
32741 parser
->in_transaction
= new_in
;
32742 cp_parser_compound_statement (parser
, NULL
, false, false);
32743 parser
->in_transaction
= old_in
;
32745 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
32750 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32752 transaction-expression:
32753 __transaction_atomic txn-noexcept-spec[opt] ( expression )
32754 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32758 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
32760 unsigned char old_in
= parser
->in_transaction
;
32761 unsigned char this_in
= 1;
32766 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32767 || keyword
== RID_TRANSACTION_RELAXED
);
32770 error (keyword
== RID_TRANSACTION_RELAXED
32771 ? G_("%<__transaction_relaxed%> without transactional memory "
32773 : G_("%<__transaction_atomic%> without transactional memory "
32774 "support enabled"));
32776 token
= cp_parser_require_keyword (parser
, keyword
,
32777 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32778 : RT_TRANSACTION_RELAXED
));
32779 gcc_assert (token
!= NULL
);
32781 if (keyword
== RID_TRANSACTION_RELAXED
)
32782 this_in
|= TM_STMT_ATTR_RELAXED
;
32784 /* Set this early. This might mean that we allow transaction_cancel in
32785 an expression that we find out later actually has to be a constexpr.
32786 However, we expect that cxx_constant_value will be able to deal with
32787 this; also, if the noexcept has no constexpr, then what we parse next
32788 really is a transaction's body. */
32789 parser
->in_transaction
= this_in
;
32791 /* Parse a noexcept specification. */
32792 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
32795 if (!noex
|| !noex_expr
32796 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
32798 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
32800 expr
= cp_parser_expression (parser
);
32801 expr
= finish_parenthesized_expr (expr
);
32803 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
32807 /* The only expression that is available got parsed for the noexcept
32808 already. noexcept is true then. */
32810 noex
= boolean_true_node
;
32813 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
32814 parser
->in_transaction
= old_in
;
32816 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
32817 return error_mark_node
;
32819 return (flag_tm
? expr
: error_mark_node
);
32822 /* Parse a function-transaction-block.
32824 function-transaction-block:
32825 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32827 __transaction_atomic txn-attribute[opt] function-try-block
32828 __transaction_relaxed ctor-initializer[opt] function-body
32829 __transaction_relaxed function-try-block
32833 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
32835 unsigned char old_in
= parser
->in_transaction
;
32836 unsigned char new_in
= 1;
32837 tree compound_stmt
, stmt
, attrs
;
32838 bool ctor_initializer_p
;
32841 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
32842 || keyword
== RID_TRANSACTION_RELAXED
);
32843 token
= cp_parser_require_keyword (parser
, keyword
,
32844 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
32845 : RT_TRANSACTION_RELAXED
));
32846 gcc_assert (token
!= NULL
);
32848 if (keyword
== RID_TRANSACTION_RELAXED
)
32849 new_in
|= TM_STMT_ATTR_RELAXED
;
32852 attrs
= cp_parser_txn_attribute_opt (parser
);
32854 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
32857 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
32859 parser
->in_transaction
= new_in
;
32861 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
32862 ctor_initializer_p
= cp_parser_function_try_block (parser
);
32864 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
32865 (parser
, /*in_function_try_block=*/false);
32867 parser
->in_transaction
= old_in
;
32869 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
32871 return ctor_initializer_p
;
32874 /* Parse a __transaction_cancel statement.
32877 __transaction_cancel txn-attribute[opt] ;
32878 __transaction_cancel txn-attribute[opt] throw-expression ;
32880 ??? Cancel and throw is not yet implemented. */
32883 cp_parser_transaction_cancel (cp_parser
*parser
)
32886 bool is_outer
= false;
32889 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
32890 RT_TRANSACTION_CANCEL
);
32891 gcc_assert (token
!= NULL
);
32893 attrs
= cp_parser_txn_attribute_opt (parser
);
32895 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
32897 /* ??? Parse cancel-and-throw here. */
32899 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
32903 error_at (token
->location
, "%<__transaction_cancel%> without "
32904 "transactional memory support enabled");
32905 return error_mark_node
;
32907 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
32909 error_at (token
->location
, "%<__transaction_cancel%> within a "
32910 "%<__transaction_relaxed%>");
32911 return error_mark_node
;
32915 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
32916 && !is_tm_may_cancel_outer (current_function_decl
))
32918 error_at (token
->location
, "outer %<__transaction_cancel%> not "
32919 "within outer %<__transaction_atomic%>");
32920 error_at (token
->location
,
32921 " or a %<transaction_may_cancel_outer%> function");
32922 return error_mark_node
;
32925 else if (parser
->in_transaction
== 0)
32927 error_at (token
->location
, "%<__transaction_cancel%> not within "
32928 "%<__transaction_atomic%>");
32929 return error_mark_node
;
32932 stmt
= build_tm_abort_call (token
->location
, is_outer
);
32940 static GTY (()) cp_parser
*the_parser
;
32943 /* Special handling for the first token or line in the file. The first
32944 thing in the file might be #pragma GCC pch_preprocess, which loads a
32945 PCH file, which is a GC collection point. So we need to handle this
32946 first pragma without benefit of an existing lexer structure.
32948 Always returns one token to the caller in *FIRST_TOKEN. This is
32949 either the true first token of the file, or the first token after
32950 the initial pragma. */
32953 cp_parser_initial_pragma (cp_token
*first_token
)
32957 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32958 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
32961 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32962 if (first_token
->type
== CPP_STRING
)
32964 name
= first_token
->u
.value
;
32966 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32967 if (first_token
->type
!= CPP_PRAGMA_EOL
)
32968 error_at (first_token
->location
,
32969 "junk at end of %<#pragma GCC pch_preprocess%>");
32972 error_at (first_token
->location
, "expected string literal");
32974 /* Skip to the end of the pragma. */
32975 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
32976 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32978 /* Now actually load the PCH file. */
32980 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
32982 /* Read one more token to return to our caller. We have to do this
32983 after reading the PCH file in, since its pointers have to be
32985 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32988 /* Parses the grainsize pragma for the _Cilk_for statement.
32990 #pragma cilk grainsize = <VALUE>. */
32993 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
32995 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32997 tree exp
= cp_parser_binary_expression (parser
, false, false,
32998 PREC_NOT_OPERATOR
, NULL
);
32999 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33000 if (!exp
|| exp
== error_mark_node
)
33002 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
33006 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
33007 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
33008 cp_parser_cilk_for (parser
, exp
);
33010 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
33011 "%<#pragma cilk grainsize%> is not followed by "
33015 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33018 /* Normal parsing of a pragma token. Here we can (and must) use the
33022 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
33024 cp_token
*pragma_tok
;
33027 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
33028 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
33029 parser
->lexer
->in_pragma
= true;
33031 id
= pragma_tok
->pragma_kind
;
33032 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
33033 cp_ensure_no_omp_declare_simd (parser
);
33036 case PRAGMA_GCC_PCH_PREPROCESS
:
33037 error_at (pragma_tok
->location
,
33038 "%<#pragma GCC pch_preprocess%> must be first");
33041 case PRAGMA_OMP_BARRIER
:
33044 case pragma_compound
:
33045 cp_parser_omp_barrier (parser
, pragma_tok
);
33048 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
33049 "used in compound statements");
33056 case PRAGMA_OMP_FLUSH
:
33059 case pragma_compound
:
33060 cp_parser_omp_flush (parser
, pragma_tok
);
33063 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
33064 "used in compound statements");
33071 case PRAGMA_OMP_TASKWAIT
:
33074 case pragma_compound
:
33075 cp_parser_omp_taskwait (parser
, pragma_tok
);
33078 error_at (pragma_tok
->location
,
33079 "%<#pragma omp taskwait%> may only be "
33080 "used in compound statements");
33087 case PRAGMA_OMP_TASKYIELD
:
33090 case pragma_compound
:
33091 cp_parser_omp_taskyield (parser
, pragma_tok
);
33094 error_at (pragma_tok
->location
,
33095 "%<#pragma omp taskyield%> may only be "
33096 "used in compound statements");
33103 case PRAGMA_OMP_CANCEL
:
33106 case pragma_compound
:
33107 cp_parser_omp_cancel (parser
, pragma_tok
);
33110 error_at (pragma_tok
->location
,
33111 "%<#pragma omp cancel%> may only be "
33112 "used in compound statements");
33119 case PRAGMA_OMP_CANCELLATION_POINT
:
33122 case pragma_compound
:
33123 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
33126 error_at (pragma_tok
->location
,
33127 "%<#pragma omp cancellation point%> may only be "
33128 "used in compound statements");
33135 case PRAGMA_OMP_THREADPRIVATE
:
33136 cp_parser_omp_threadprivate (parser
, pragma_tok
);
33139 case PRAGMA_OMP_DECLARE_REDUCTION
:
33140 cp_parser_omp_declare (parser
, pragma_tok
, context
);
33143 case PRAGMA_OACC_CACHE
:
33144 case PRAGMA_OACC_DATA
:
33145 case PRAGMA_OACC_ENTER_DATA
:
33146 case PRAGMA_OACC_EXIT_DATA
:
33147 case PRAGMA_OACC_KERNELS
:
33148 case PRAGMA_OACC_PARALLEL
:
33149 case PRAGMA_OACC_LOOP
:
33150 case PRAGMA_OACC_UPDATE
:
33151 case PRAGMA_OACC_WAIT
:
33152 case PRAGMA_OMP_ATOMIC
:
33153 case PRAGMA_OMP_CRITICAL
:
33154 case PRAGMA_OMP_DISTRIBUTE
:
33155 case PRAGMA_OMP_FOR
:
33156 case PRAGMA_OMP_MASTER
:
33157 case PRAGMA_OMP_ORDERED
:
33158 case PRAGMA_OMP_PARALLEL
:
33159 case PRAGMA_OMP_SECTIONS
:
33160 case PRAGMA_OMP_SIMD
:
33161 case PRAGMA_OMP_SINGLE
:
33162 case PRAGMA_OMP_TASK
:
33163 case PRAGMA_OMP_TASKGROUP
:
33164 case PRAGMA_OMP_TEAMS
:
33165 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
33167 cp_parser_omp_construct (parser
, pragma_tok
);
33170 case PRAGMA_OMP_TARGET
:
33171 return cp_parser_omp_target (parser
, pragma_tok
, context
);
33173 case PRAGMA_OMP_END_DECLARE_TARGET
:
33174 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
33177 case PRAGMA_OMP_SECTION
:
33178 error_at (pragma_tok
->location
,
33179 "%<#pragma omp section%> may only be used in "
33180 "%<#pragma omp sections%> construct");
33185 if (context
== pragma_external
)
33187 error_at (pragma_tok
->location
,
33188 "%<#pragma GCC ivdep%> must be inside a function");
33191 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33193 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33194 if (tok
->type
!= CPP_KEYWORD
33195 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
33196 && tok
->keyword
!= RID_DO
))
33198 cp_parser_error (parser
, "for, while or do statement expected");
33201 cp_parser_iteration_statement (parser
, true);
33205 case PRAGMA_CILK_SIMD
:
33206 if (context
== pragma_external
)
33208 error_at (pragma_tok
->location
,
33209 "%<#pragma simd%> must be inside a function");
33212 cp_parser_cilk_simd (parser
, pragma_tok
);
33215 case PRAGMA_CILK_GRAINSIZE
:
33216 if (context
== pragma_external
)
33218 error_at (pragma_tok
->location
,
33219 "%<#pragma cilk grainsize%> must be inside a function");
33223 /* Ignore the pragma if Cilk Plus is not enabled. */
33226 cp_parser_cilk_grainsize (parser
, pragma_tok
);
33231 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
33232 "%<#pragma cilk grainsize%>");
33237 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
33238 c_invoke_pragma_handler (id
);
33242 cp_parser_error (parser
, "expected declaration specifiers");
33246 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
33250 /* The interface the pragma parsers have to the lexer. */
33253 pragma_lex (tree
*value
)
33256 enum cpp_ttype ret
;
33258 tok
= cp_lexer_peek_token (the_parser
->lexer
);
33261 *value
= tok
->u
.value
;
33263 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
33265 else if (ret
== CPP_STRING
)
33266 *value
= cp_parser_string_literal (the_parser
, false, false);
33269 cp_lexer_consume_token (the_parser
->lexer
);
33270 if (ret
== CPP_KEYWORD
)
33278 /* External interface. */
33280 /* Parse one entire translation unit. */
33283 c_parse_file (void)
33285 static bool already_called
= false;
33287 if (already_called
)
33288 fatal_error (input_location
,
33289 "inter-module optimizations not implemented for C++");
33290 already_called
= true;
33292 the_parser
= cp_parser_new ();
33293 push_deferring_access_checks (flag_access_control
33294 ? dk_no_deferred
: dk_no_check
);
33295 cp_parser_translation_unit (the_parser
);
33299 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
33300 vectorlength clause:
33302 vectorlength ( constant-expression ) */
33305 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
33308 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33310 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33311 safelen clause. Thus, vectorlength is represented as OMP 4.0
33312 safelen. For SIMD-enabled function it is represented by OMP 4.0
33315 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
33318 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
33321 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33322 return error_mark_node
;
33324 expr
= cp_parser_constant_expression (parser
);
33325 expr
= maybe_constant_value (expr
);
33327 /* If expr == error_mark_node, then don't emit any errors nor
33328 create a clause. if any of the above functions returns
33329 error mark node then they would have emitted an error message. */
33330 if (expr
== error_mark_node
)
33332 else if (!TREE_TYPE (expr
)
33333 || !TREE_CONSTANT (expr
)
33334 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
33335 error_at (loc
, "vectorlength must be an integer constant");
33336 else if (TREE_CONSTANT (expr
)
33337 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
33338 error_at (loc
, "vectorlength must be a power of 2");
33344 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
33345 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
33346 OMP_CLAUSE_CHAIN (c
) = clauses
;
33351 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
33352 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
33353 OMP_CLAUSE_CHAIN (c
) = clauses
;
33358 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33359 return error_mark_node
;
33363 /* Handles the Cilk Plus #pragma simd linear clause.
33365 linear ( simd-linear-variable-list )
33367 simd-linear-variable-list:
33368 simd-linear-variable
33369 simd-linear-variable-list , simd-linear-variable
33371 simd-linear-variable:
33373 id-expression : simd-linear-step
33376 conditional-expression */
33379 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
33381 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33383 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33385 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33387 cp_parser_error (parser
, "expected identifier");
33388 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33389 return error_mark_node
;
33392 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33393 parser
->colon_corrects_to_scope_p
= false;
33396 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33397 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33399 cp_parser_error (parser
, "expected variable-name");
33400 clauses
= error_mark_node
;
33404 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
33406 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
33408 if (decl
== error_mark_node
)
33410 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
33412 clauses
= error_mark_node
;
33416 tree e
= NULL_TREE
;
33417 tree step_size
= integer_one_node
;
33419 /* If present, parse the linear step. Otherwise, assume the default
33421 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
33423 cp_lexer_consume_token (parser
->lexer
);
33425 e
= cp_parser_assignment_expression (parser
);
33426 e
= maybe_constant_value (e
);
33428 if (e
== error_mark_node
)
33430 /* If an error has occurred, then the whole pragma is
33431 considered ill-formed. Thus, no reason to keep
33433 clauses
= error_mark_node
;
33436 else if (type_dependent_expression_p (e
)
33437 || value_dependent_expression_p (e
)
33439 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
33440 && (TREE_CONSTANT (e
)
33444 cp_parser_error (parser
,
33445 "step size must be an integer constant "
33446 "expression or an integer variable");
33449 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
33450 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
33451 OMP_CLAUSE_DECL (l
) = decl
;
33452 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
33453 OMP_CLAUSE_CHAIN (l
) = clauses
;
33456 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33457 cp_lexer_consume_token (parser
->lexer
);
33458 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33462 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33463 "expected %<,%> or %<)%> after %qE", decl
);
33464 clauses
= error_mark_node
;
33468 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33469 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33473 /* Returns the name of the next clause. If the clause is not
33474 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33475 token is not consumed. Otherwise, the appropriate enum from the
33476 pragma_simd_clause is returned and the token is consumed. */
33478 static pragma_omp_clause
33479 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
33481 pragma_omp_clause clause_type
;
33482 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33484 if (token
->keyword
== RID_PRIVATE
)
33485 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
33486 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
33487 return PRAGMA_CILK_CLAUSE_NONE
;
33488 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
33489 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
33490 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
33491 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
33492 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
33493 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
33494 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
33495 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
33496 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
33497 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
33499 return PRAGMA_CILK_CLAUSE_NONE
;
33501 cp_lexer_consume_token (parser
->lexer
);
33502 return clause_type
;
33505 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
33508 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
33510 tree clauses
= NULL_TREE
;
33512 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
33513 && clauses
!= error_mark_node
)
33515 pragma_omp_clause c_kind
;
33516 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
33517 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
33518 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
33519 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
33520 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
33521 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
33522 /* Use the OpenMP 4.0 equivalent function. */
33523 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
33524 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
33525 /* Use the OpenMP 4.0 equivalent function. */
33526 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
33528 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
33529 /* Use the OMP 4.0 equivalent function. */
33530 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
33532 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
33533 /* Use the OMP 4.0 equivalent function. */
33534 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
33537 clauses
= error_mark_node
;
33538 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
33543 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
33545 if (clauses
== error_mark_node
)
33546 return error_mark_node
;
33548 return c_finish_cilk_clauses (clauses
);
33551 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
33554 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
33556 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
33558 if (clauses
== error_mark_node
)
33561 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
33563 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33564 "for statement expected");
33568 tree sb
= begin_omp_structured_block ();
33569 int save
= cp_parser_begin_omp_structured_block (parser
);
33570 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
33572 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
33573 cp_parser_end_omp_structured_block (parser
, save
);
33574 add_stmt (finish_omp_structured_block (sb
));
33577 /* Main entry-point for parsing Cilk Plus _Cilk_for
33578 loops. The return value is error_mark_node
33579 when errors happen and CILK_FOR tree on success. */
33582 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
33584 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
33585 gcc_unreachable ();
33587 tree sb
= begin_omp_structured_block ();
33588 int save
= cp_parser_begin_omp_structured_block (parser
);
33590 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
33591 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
33592 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
33593 clauses
= finish_omp_clauses (clauses
);
33595 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
33597 cpp_validate_cilk_plus_loop (ret
);
33599 ret
= error_mark_node
;
33601 cp_parser_end_omp_structured_block (parser
, save
);
33602 add_stmt (finish_omp_structured_block (sb
));
33606 /* Create an identifier for a generic parameter type (a synthesized
33607 template parameter implied by `auto' or a concept identifier). */
33609 static GTY(()) int generic_parm_count
;
33611 make_generic_type_name ()
33614 sprintf (buf
, "auto:%d", ++generic_parm_count
);
33615 return get_identifier (buf
);
33618 /* Predicate that behaves as is_auto_or_concept but matches the parent
33619 node of the generic type rather than the generic type itself. This
33620 allows for type transformation in add_implicit_template_parms. */
33623 tree_type_is_auto_or_concept (const_tree t
)
33625 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
33628 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33629 (creating a new template parameter list if necessary). Returns the newly
33630 created template type parm. */
33633 synthesize_implicit_template_parm (cp_parser
*parser
)
33635 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
33637 /* We are either continuing a function template that already contains implicit
33638 template parameters, creating a new fully-implicit function template, or
33639 extending an existing explicit function template with implicit template
33642 cp_binding_level
*const entry_scope
= current_binding_level
;
33644 bool become_template
= false;
33645 cp_binding_level
*parent_scope
= 0;
33647 if (parser
->implicit_template_scope
)
33649 gcc_assert (parser
->implicit_template_parms
);
33651 current_binding_level
= parser
->implicit_template_scope
;
33655 /* Roll back to the existing template parameter scope (in the case of
33656 extending an explicit function template) or introduce a new template
33657 parameter scope ahead of the function parameter scope (or class scope
33658 in the case of out-of-line member definitions). The function scope is
33659 added back after template parameter synthesis below. */
33661 cp_binding_level
*scope
= entry_scope
;
33663 while (scope
->kind
== sk_function_parms
)
33665 parent_scope
= scope
;
33666 scope
= scope
->level_chain
;
33668 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
33670 /* If not defining a class, then any class scope is a scope level in
33671 an out-of-line member definition. In this case simply wind back
33672 beyond the first such scope to inject the template parameter list.
33673 Otherwise wind back to the class being defined. The latter can
33674 occur in class member friend declarations such as:
33680 friend void A::foo (auto);
33683 The template parameter list synthesized for the friend declaration
33684 must be injected in the scope of 'B'. This can also occur in
33685 erroneous cases such as:
33691 void B::foo (auto) {}
33694 Here the attempted definition of 'B::foo' within 'A' is ill-formed
33695 but, nevertheless, the template parameter list synthesized for the
33696 declarator should be injected into the scope of 'A' as if the
33697 ill-formed template was specified explicitly. */
33699 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
33701 parent_scope
= scope
;
33702 scope
= scope
->level_chain
;
33706 current_binding_level
= scope
;
33708 if (scope
->kind
!= sk_template_parms
33709 || !function_being_declared_is_template_p (parser
))
33711 /* Introduce a new template parameter list for implicit template
33714 become_template
= true;
33716 parser
->implicit_template_scope
33717 = begin_scope (sk_template_parms
, NULL
);
33719 ++processing_template_decl
;
33721 parser
->fully_implicit_function_template_p
= true;
33722 ++parser
->num_template_parameter_lists
;
33726 /* Synthesize implicit template parameters at the end of the explicit
33727 template parameter list. */
33729 gcc_assert (current_template_parms
);
33731 parser
->implicit_template_scope
= scope
;
33733 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33734 parser
->implicit_template_parms
33735 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
33739 /* Synthesize a new template parameter and track the current template
33740 parameter chain with implicit_template_parms. */
33742 tree synth_id
= make_generic_type_name ();
33743 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
33746 = process_template_parm (parser
->implicit_template_parms
,
33748 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
33749 /*non_type=*/false,
33750 /*param_pack=*/false);
33753 if (parser
->implicit_template_parms
)
33754 parser
->implicit_template_parms
33755 = TREE_CHAIN (parser
->implicit_template_parms
);
33757 parser
->implicit_template_parms
= new_parm
;
33759 tree new_type
= TREE_TYPE (getdecls ());
33761 /* If creating a fully implicit function template, start the new implicit
33762 template parameter list with this synthesized type, otherwise grow the
33763 current template parameter list. */
33765 if (become_template
)
33767 parent_scope
->level_chain
= current_binding_level
;
33769 tree new_parms
= make_tree_vec (1);
33770 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
33771 current_template_parms
= tree_cons (size_int (processing_template_decl
),
33772 new_parms
, current_template_parms
);
33776 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
33777 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
33778 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
33779 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
33782 current_binding_level
= entry_scope
;
33787 /* Finish the declaration of a fully implicit function template. Such a
33788 template has no explicit template parameter list so has not been through the
33789 normal template head and tail processing. synthesize_implicit_template_parm
33790 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
33791 provided if the declaration is a class member such that its template
33792 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
33793 form is returned. Otherwise NULL_TREE is returned. */
33796 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
33798 gcc_assert (parser
->fully_implicit_function_template_p
);
33800 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
33801 && DECL_VIRTUAL_P (member_decl_opt
))
33803 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
33804 "implicit templates may not be %<virtual%>");
33805 DECL_VIRTUAL_P (member_decl_opt
) = false;
33808 if (member_decl_opt
)
33809 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
33810 end_template_decl ();
33812 parser
->fully_implicit_function_template_p
= false;
33813 --parser
->num_template_parameter_lists
;
33815 return member_decl_opt
;
33818 #include "gt-cp-parser.h"