2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
31 #include "trans-mem.h"
34 #include "c-family/c-pragma.h"
37 #include "diagnostic-core.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
43 #include "tree-pretty-print.h"
45 #include "type-utils.h"
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token
=
56 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant
{
62 /* floating-point literal */
66 /* %<__FUNCTION__%> */
68 /* %<__PRETTY_FUNCTION__%> */
76 /* %<typeid%> operator */
78 /* non-constant compound literals */
86 /* an array reference */
92 /* the address of a label */
106 /* calls to overloaded operators */
110 /* a comma operator */
112 /* a call to a constructor */
114 /* a transaction expression */
116 } non_integral_constant
;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error
{
124 /* is not a class or namespace */
126 /* is not a class, namespace, or enumeration */
130 /* The various kinds of required token */
131 typedef enum required_token
{
133 RT_SEMICOLON
, /* ';' */
134 RT_OPEN_PAREN
, /* '(' */
135 RT_CLOSE_BRACE
, /* '}' */
136 RT_OPEN_BRACE
, /* '{' */
137 RT_CLOSE_SQUARE
, /* ']' */
138 RT_OPEN_SQUARE
, /* '[' */
142 RT_GREATER
, /* '>' */
144 RT_ELLIPSIS
, /* '...' */
148 RT_COLON_SCOPE
, /* ':' or '::' */
149 RT_CLOSE_PAREN
, /* ')' */
150 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
151 RT_PRAGMA_EOL
, /* end of line */
152 RT_NAME
, /* identifier */
154 /* The type is CPP_KEYWORD */
156 RT_DELETE
, /* delete */
157 RT_RETURN
, /* return */
158 RT_WHILE
, /* while */
159 RT_EXTERN
, /* extern */
160 RT_STATIC_ASSERT
, /* static_assert */
161 RT_DECLTYPE
, /* decltype */
162 RT_OPERATOR
, /* operator */
163 RT_CLASS
, /* class */
164 RT_TEMPLATE
, /* template */
165 RT_NAMESPACE
, /* namespace */
166 RT_USING
, /* using */
169 RT_CATCH
, /* catch */
170 RT_THROW
, /* throw */
171 RT_LABEL
, /* __label__ */
172 RT_AT_TRY
, /* @try */
173 RT_AT_SYNCHRONIZED
, /* @synchronized */
174 RT_AT_THROW
, /* @throw */
176 RT_SELECT
, /* selection-statement */
177 RT_INTERATION
, /* iteration-statement */
178 RT_JUMP
, /* jump-statement */
179 RT_CLASS_KEY
, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
188 static cp_lexer
*cp_lexer_new_main
190 static cp_lexer
*cp_lexer_new_from_tokens
191 (cp_token_cache
*tokens
);
192 static void cp_lexer_destroy
194 static int cp_lexer_saving_tokens
196 static cp_token
*cp_lexer_token_at
197 (cp_lexer
*, cp_token_position
);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer
*, cp_token
*);
200 static inline cp_token
*cp_lexer_peek_token
202 static cp_token
*cp_lexer_peek_nth_token
203 (cp_lexer
*, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer
*, enum cpp_ttype
);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer
*, enum cpp_ttype
);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer
*, enum rid
);
210 static cp_token
*cp_lexer_consume_token
212 static void cp_lexer_purge_token
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer
*, cp_token_position
);
216 static void cp_lexer_save_tokens
218 static void cp_lexer_commit_tokens
220 static void cp_lexer_rollback_tokens
222 static void cp_lexer_print_token
223 (FILE *, cp_token
*);
224 static inline bool cp_lexer_debugging_p
226 static void cp_lexer_start_debugging
227 (cp_lexer
*) ATTRIBUTE_UNUSED
;
228 static void cp_lexer_stop_debugging
229 (cp_lexer
*) ATTRIBUTE_UNUSED
;
231 static cp_token_cache
*cp_token_cache_new
232 (cp_token
*, cp_token
*);
234 static void cp_parser_initial_pragma
237 static tree cp_literal_operator_id
240 static void cp_parser_cilk_simd
241 (cp_parser
*, cp_token
*);
242 static tree cp_parser_cilk_for
244 static bool cp_parser_omp_declare_reduction_exprs
246 static tree cp_parser_cilk_simd_vectorlength
247 (cp_parser
*, tree
, bool);
249 /* Manifest constants. */
250 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
251 #define CP_SAVED_TOKEN_STACK 5
255 /* The stream to which debugging output should be written. */
256 static FILE *cp_lexer_debug_stream
;
258 /* Nonzero if we are parsing an unevaluated operand: an operand to
259 sizeof, typeof, or alignof. */
260 int cp_unevaluated_operand
;
262 /* Dump up to NUM tokens in BUFFER to FILE starting with token
263 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
264 first token in BUFFER. If NUM is 0, dump all the tokens. If
265 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
266 highlighted by surrounding it in [[ ]]. */
269 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
270 cp_token
*start_token
, unsigned num
,
271 cp_token
*curr_token
)
273 unsigned i
, nprinted
;
277 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
283 num
= buffer
->length ();
285 if (start_token
== NULL
)
286 start_token
= buffer
->address ();
288 if (start_token
> buffer
->address ())
290 cp_lexer_print_token (file
, &(*buffer
)[0]);
291 fprintf (file
, " ... ");
296 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
298 if (token
== start_token
)
305 if (token
== curr_token
)
306 fprintf (file
, "[[");
308 cp_lexer_print_token (file
, token
);
310 if (token
== curr_token
)
311 fprintf (file
, "]]");
317 case CPP_CLOSE_BRACE
:
327 if (i
== num
&& i
< buffer
->length ())
329 fprintf (file
, " ... ");
330 cp_lexer_print_token (file
, &buffer
->last ());
333 fprintf (file
, "\n");
337 /* Dump all tokens in BUFFER to stderr. */
340 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
342 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
346 debug (vec
<cp_token
, va_gc
> &ref
)
348 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
352 debug (vec
<cp_token
, va_gc
> *ptr
)
357 fprintf (stderr
, "<nil>\n");
361 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
362 description for T. */
365 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
369 fprintf (file
, "%s: ", desc
);
370 print_node_brief (file
, "", t
, 0);
375 /* Dump parser context C to FILE. */
378 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
380 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
381 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
382 print_node_brief (file
, "", c
->object_type
, 0);
383 fprintf (file
, "}\n");
387 /* Print the stack of parsing contexts to FILE starting with FIRST. */
390 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
393 cp_parser_context
*c
;
395 fprintf (file
, "Parsing context stack:\n");
396 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
398 fprintf (file
, "\t#%u: ", i
);
399 cp_debug_print_context (file
, c
);
404 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
407 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
410 fprintf (file
, "%s: true\n", desc
);
414 /* Print an unparsed function entry UF to FILE. */
417 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
420 cp_default_arg_entry
*default_arg_fn
;
423 fprintf (file
, "\tFunctions with default args:\n");
425 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
428 fprintf (file
, "\t\tClass type: ");
429 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
430 fprintf (file
, "\t\tDeclaration: ");
431 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
432 fprintf (file
, "\n");
435 fprintf (file
, "\n\tFunctions with definitions that require "
436 "post-processing\n\t\t");
437 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
439 print_node_brief (file
, "", fn
, 0);
442 fprintf (file
, "\n");
444 fprintf (file
, "\n\tNon-static data members with initializers that require "
445 "post-processing\n\t\t");
446 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
448 print_node_brief (file
, "", fn
, 0);
451 fprintf (file
, "\n");
455 /* Print the stack of unparsed member functions S to FILE. */
458 cp_debug_print_unparsed_queues (FILE *file
,
459 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
462 cp_unparsed_functions_entry
*uf
;
464 fprintf (file
, "Unparsed functions\n");
465 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
467 fprintf (file
, "#%u:\n", i
);
468 cp_debug_print_unparsed_function (file
, uf
);
473 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
474 the given PARSER. If FILE is NULL, the output is printed on stderr. */
477 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
479 cp_token
*next_token
, *first_token
, *start_token
;
484 next_token
= parser
->lexer
->next_token
;
485 first_token
= parser
->lexer
->buffer
->address ();
486 start_token
= (next_token
> first_token
+ window_size
/ 2)
487 ? next_token
- window_size
/ 2
489 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
494 /* Dump debugging information for the given PARSER. If FILE is NULL,
495 the output is printed on stderr. */
498 cp_debug_parser (FILE *file
, cp_parser
*parser
)
500 const size_t window_size
= 20;
502 expanded_location eloc
;
507 fprintf (file
, "Parser state\n\n");
508 fprintf (file
, "Number of tokens: %u\n",
509 vec_safe_length (parser
->lexer
->buffer
));
510 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
511 cp_debug_print_tree_if_set (file
, "Object scope",
512 parser
->object_scope
);
513 cp_debug_print_tree_if_set (file
, "Qualifying scope",
514 parser
->qualifying_scope
);
515 cp_debug_print_context_stack (file
, parser
->context
);
516 cp_debug_print_flag (file
, "Allow GNU extensions",
517 parser
->allow_gnu_extensions_p
);
518 cp_debug_print_flag (file
, "'>' token is greater-than",
519 parser
->greater_than_is_operator_p
);
520 cp_debug_print_flag (file
, "Default args allowed in current "
521 "parameter list", parser
->default_arg_ok_p
);
522 cp_debug_print_flag (file
, "Parsing integral constant-expression",
523 parser
->integral_constant_expression_p
);
524 cp_debug_print_flag (file
, "Allow non-constant expression in current "
525 "constant-expression",
526 parser
->allow_non_integral_constant_expression_p
);
527 cp_debug_print_flag (file
, "Seen non-constant expression",
528 parser
->non_integral_constant_expression_p
);
529 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
531 parser
->local_variables_forbidden_p
);
532 cp_debug_print_flag (file
, "In unbraced linkage specification",
533 parser
->in_unbraced_linkage_specification_p
);
534 cp_debug_print_flag (file
, "Parsing a declarator",
535 parser
->in_declarator_p
);
536 cp_debug_print_flag (file
, "In template argument list",
537 parser
->in_template_argument_list_p
);
538 cp_debug_print_flag (file
, "Parsing an iteration statement",
539 parser
->in_statement
& IN_ITERATION_STMT
);
540 cp_debug_print_flag (file
, "Parsing a switch statement",
541 parser
->in_statement
& IN_SWITCH_STMT
);
542 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
543 parser
->in_statement
& IN_OMP_BLOCK
);
544 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
545 parser
->in_statement
& IN_CILK_SIMD_FOR
);
546 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
547 parser
->in_statement
& IN_OMP_FOR
);
548 cp_debug_print_flag (file
, "Parsing an if statement",
549 parser
->in_statement
& IN_IF_STMT
);
550 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
551 "context", parser
->in_type_id_in_expr_p
);
552 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
553 parser
->implicit_extern_c
);
554 cp_debug_print_flag (file
, "String expressions should be translated "
555 "to execution character set",
556 parser
->translate_strings_p
);
557 cp_debug_print_flag (file
, "Parsing function body outside of a "
558 "local class", parser
->in_function_body
);
559 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
560 parser
->colon_corrects_to_scope_p
);
561 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
562 parser
->colon_doesnt_start_class_def_p
);
563 if (parser
->type_definition_forbidden_message
)
564 fprintf (file
, "Error message for forbidden type definitions: %s\n",
565 parser
->type_definition_forbidden_message
);
566 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
567 fprintf (file
, "Number of class definitions in progress: %u\n",
568 parser
->num_classes_being_defined
);
569 fprintf (file
, "Number of template parameter lists for the current "
570 "declaration: %u\n", parser
->num_template_parameter_lists
);
571 cp_debug_parser_tokens (file
, parser
, window_size
);
572 token
= parser
->lexer
->next_token
;
573 fprintf (file
, "Next token to parse:\n");
574 fprintf (file
, "\tToken: ");
575 cp_lexer_print_token (file
, token
);
576 eloc
= expand_location (token
->location
);
577 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
578 fprintf (file
, "\tLine: %d\n", eloc
.line
);
579 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
583 debug (cp_parser
&ref
)
585 cp_debug_parser (stderr
, &ref
);
589 debug (cp_parser
*ptr
)
594 fprintf (stderr
, "<nil>\n");
597 /* Allocate memory for a new lexer object and return it. */
600 cp_lexer_alloc (void)
604 c_common_no_more_pch ();
606 /* Allocate the memory. */
607 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
609 /* Initially we are not debugging. */
610 lexer
->debugging_p
= false;
612 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
614 /* Create the buffer. */
615 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
621 /* Create a new main C++ lexer, the lexer that gets tokens from the
625 cp_lexer_new_main (void)
630 /* It's possible that parsing the first pragma will load a PCH file,
631 which is a GC collection point. So we have to do that before
632 allocating any memory. */
633 cp_parser_initial_pragma (&token
);
635 lexer
= cp_lexer_alloc ();
637 /* Put the first token in the buffer. */
638 lexer
->buffer
->quick_push (token
);
640 /* Get the remaining tokens from the preprocessor. */
641 while (token
.type
!= CPP_EOF
)
643 cp_lexer_get_preprocessor_token (lexer
, &token
);
644 vec_safe_push (lexer
->buffer
, token
);
647 lexer
->last_token
= lexer
->buffer
->address ()
648 + lexer
->buffer
->length ()
650 lexer
->next_token
= lexer
->buffer
->length ()
651 ? lexer
->buffer
->address ()
654 /* Subsequent preprocessor diagnostics should use compiler
655 diagnostic functions to get the compiler source location. */
658 gcc_assert (!lexer
->next_token
->purged_p
);
662 /* Create a new lexer whose token stream is primed with the tokens in
663 CACHE. When these tokens are exhausted, no new tokens will be read. */
666 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
668 cp_token
*first
= cache
->first
;
669 cp_token
*last
= cache
->last
;
670 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
672 /* We do not own the buffer. */
673 lexer
->buffer
= NULL
;
674 lexer
->next_token
= first
== last
? &eof_token
: first
;
675 lexer
->last_token
= last
;
677 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
679 /* Initially we are not debugging. */
680 lexer
->debugging_p
= false;
682 gcc_assert (!lexer
->next_token
->purged_p
);
686 /* Frees all resources associated with LEXER. */
689 cp_lexer_destroy (cp_lexer
*lexer
)
691 vec_free (lexer
->buffer
);
692 lexer
->saved_tokens
.release ();
696 /* Returns nonzero if debugging information should be output. */
699 cp_lexer_debugging_p (cp_lexer
*lexer
)
701 return lexer
->debugging_p
;
705 static inline cp_token_position
706 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
708 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
710 return lexer
->next_token
- previous_p
;
713 static inline cp_token
*
714 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
720 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
722 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
725 static inline cp_token_position
726 cp_lexer_previous_token_position (cp_lexer
*lexer
)
728 if (lexer
->next_token
== &eof_token
)
729 return lexer
->last_token
- 1;
731 return cp_lexer_token_position (lexer
, true);
734 static inline cp_token
*
735 cp_lexer_previous_token (cp_lexer
*lexer
)
737 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
739 return cp_lexer_token_at (lexer
, tp
);
742 /* nonzero if we are presently saving tokens. */
745 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
747 return lexer
->saved_tokens
.length () != 0;
750 /* Store the next token from the preprocessor in *TOKEN. Return true
751 if we reach EOF. If LEXER is NULL, assume we are handling an
752 initial #pragma pch_preprocess, and thus want the lexer to return
753 processed strings. */
756 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
758 static int is_extern_c
= 0;
760 /* Get a new token from the preprocessor. */
762 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
763 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
764 token
->keyword
= RID_MAX
;
765 token
->pragma_kind
= PRAGMA_NONE
;
766 token
->purged_p
= false;
767 token
->error_reported
= false;
769 /* On some systems, some header files are surrounded by an
770 implicit extern "C" block. Set a flag in the token if it
771 comes from such a header. */
772 is_extern_c
+= pending_lang_change
;
773 pending_lang_change
= 0;
774 token
->implicit_extern_c
= is_extern_c
> 0;
776 /* Check to see if this token is a keyword. */
777 if (token
->type
== CPP_NAME
)
779 if (C_IS_RESERVED_WORD (token
->u
.value
))
781 /* Mark this token as a keyword. */
782 token
->type
= CPP_KEYWORD
;
783 /* Record which keyword. */
784 token
->keyword
= C_RID_CODE (token
->u
.value
);
788 if (warn_cxx0x_compat
789 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
790 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
792 /* Warn about the C++0x keyword (but still treat it as
794 warning (OPT_Wc__0x_compat
,
795 "identifier %qE is a keyword in C++11",
798 /* Clear out the C_RID_CODE so we don't warn about this
799 particular identifier-turned-keyword again. */
800 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
803 token
->keyword
= RID_MAX
;
806 else if (token
->type
== CPP_AT_NAME
)
808 /* This only happens in Objective-C++; it must be a keyword. */
809 token
->type
= CPP_KEYWORD
;
810 switch (C_RID_CODE (token
->u
.value
))
812 /* Replace 'class' with '@class', 'private' with '@private',
813 etc. This prevents confusion with the C++ keyword
814 'class', and makes the tokens consistent with other
815 Objective-C 'AT' keywords. For example '@class' is
816 reported as RID_AT_CLASS which is consistent with
817 '@synchronized', which is reported as
820 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
821 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
822 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
823 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
824 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
825 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
826 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
827 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
830 else if (token
->type
== CPP_PRAGMA
)
832 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
833 token
->pragma_kind
= ((enum pragma_kind
)
834 TREE_INT_CST_LOW (token
->u
.value
));
835 token
->u
.value
= NULL_TREE
;
839 /* Update the globals input_location and the input file stack from TOKEN. */
841 cp_lexer_set_source_position_from_token (cp_token
*token
)
843 if (token
->type
!= CPP_EOF
)
845 input_location
= token
->location
;
849 /* Update the globals input_location and the input file stack from LEXER. */
851 cp_lexer_set_source_position (cp_lexer
*lexer
)
853 cp_token
*token
= cp_lexer_peek_token (lexer
);
854 cp_lexer_set_source_position_from_token (token
);
857 /* Return a pointer to the next token in the token stream, but do not
860 static inline cp_token
*
861 cp_lexer_peek_token (cp_lexer
*lexer
)
863 if (cp_lexer_debugging_p (lexer
))
865 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
866 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
867 putc ('\n', cp_lexer_debug_stream
);
869 return lexer
->next_token
;
872 /* Return true if the next token has the indicated TYPE. */
875 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
877 return cp_lexer_peek_token (lexer
)->type
== type
;
880 /* Return true if the next token does not have the indicated TYPE. */
883 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
885 return !cp_lexer_next_token_is (lexer
, type
);
888 /* Return true if the next token is the indicated KEYWORD. */
891 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
893 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
897 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
899 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
903 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
905 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
908 /* Return true if the next token is not the indicated KEYWORD. */
911 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
913 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
916 /* Return true if the next token is a keyword for a decl-specifier. */
919 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
923 token
= cp_lexer_peek_token (lexer
);
924 switch (token
->keyword
)
926 /* auto specifier: storage-class-specifier in C++,
927 simple-type-specifier in C++0x. */
929 /* Storage classes. */
935 /* Elaborated type specifiers. */
941 /* Simple type specifiers. */
955 /* GNU extensions. */
958 /* C++0x extensions. */
960 case RID_UNDERLYING_TYPE
:
964 if (token
->keyword
>= RID_FIRST_INT_N
965 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
966 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
972 /* Returns TRUE iff the token T begins a decltype type. */
975 token_is_decltype (cp_token
*t
)
977 return (t
->keyword
== RID_DECLTYPE
978 || t
->type
== CPP_DECLTYPE
);
981 /* Returns TRUE iff the next token begins a decltype type. */
984 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
986 cp_token
*t
= cp_lexer_peek_token (lexer
);
987 return token_is_decltype (t
);
990 /* Return a pointer to the Nth token in the token stream. If N is 1,
991 then this is precisely equivalent to cp_lexer_peek_token (except
992 that it is not inline). One would like to disallow that case, but
993 there is one case (cp_parser_nth_token_starts_template_id) where
994 the caller passes a variable for N and it might be 1. */
997 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1001 /* N is 1-based, not zero-based. */
1004 if (cp_lexer_debugging_p (lexer
))
1005 fprintf (cp_lexer_debug_stream
,
1006 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1009 token
= lexer
->next_token
;
1010 gcc_assert (!n
|| token
!= &eof_token
);
1014 if (token
== lexer
->last_token
)
1020 if (!token
->purged_p
)
1024 if (cp_lexer_debugging_p (lexer
))
1026 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1027 putc ('\n', cp_lexer_debug_stream
);
1033 /* Return the next token, and advance the lexer's next_token pointer
1034 to point to the next non-purged token. */
1037 cp_lexer_consume_token (cp_lexer
* lexer
)
1039 cp_token
*token
= lexer
->next_token
;
1041 gcc_assert (token
!= &eof_token
);
1042 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1046 lexer
->next_token
++;
1047 if (lexer
->next_token
== lexer
->last_token
)
1049 lexer
->next_token
= &eof_token
;
1054 while (lexer
->next_token
->purged_p
);
1056 cp_lexer_set_source_position_from_token (token
);
1058 /* Provide debugging output. */
1059 if (cp_lexer_debugging_p (lexer
))
1061 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1062 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1063 putc ('\n', cp_lexer_debug_stream
);
1069 /* Permanently remove the next token from the token stream, and
1070 advance the next_token pointer to refer to the next non-purged
1074 cp_lexer_purge_token (cp_lexer
*lexer
)
1076 cp_token
*tok
= lexer
->next_token
;
1078 gcc_assert (tok
!= &eof_token
);
1079 tok
->purged_p
= true;
1080 tok
->location
= UNKNOWN_LOCATION
;
1081 tok
->u
.value
= NULL_TREE
;
1082 tok
->keyword
= RID_MAX
;
1087 if (tok
== lexer
->last_token
)
1093 while (tok
->purged_p
);
1094 lexer
->next_token
= tok
;
1097 /* Permanently remove all tokens after TOK, up to, but not
1098 including, the token that will be returned next by
1099 cp_lexer_peek_token. */
1102 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1104 cp_token
*peek
= lexer
->next_token
;
1106 if (peek
== &eof_token
)
1107 peek
= lexer
->last_token
;
1109 gcc_assert (tok
< peek
);
1111 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1113 tok
->purged_p
= true;
1114 tok
->location
= UNKNOWN_LOCATION
;
1115 tok
->u
.value
= NULL_TREE
;
1116 tok
->keyword
= RID_MAX
;
1120 /* Begin saving tokens. All tokens consumed after this point will be
1124 cp_lexer_save_tokens (cp_lexer
* lexer
)
1126 /* Provide debugging output. */
1127 if (cp_lexer_debugging_p (lexer
))
1128 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1130 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1133 /* Commit to the portion of the token stream most recently saved. */
1136 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1138 /* Provide debugging output. */
1139 if (cp_lexer_debugging_p (lexer
))
1140 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1142 lexer
->saved_tokens
.pop ();
1145 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1146 to the token stream. Stop saving tokens. */
1149 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1151 /* Provide debugging output. */
1152 if (cp_lexer_debugging_p (lexer
))
1153 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1155 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1158 /* RAII wrapper around the above functions, with sanity checking. Creating
1159 a variable saves tokens, which are committed when the variable is
1160 destroyed unless they are explicitly rolled back by calling the rollback
1163 struct saved_token_sentinel
1168 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1170 len
= lexer
->saved_tokens
.length ();
1171 cp_lexer_save_tokens (lexer
);
1175 cp_lexer_rollback_tokens (lexer
);
1178 ~saved_token_sentinel()
1181 cp_lexer_commit_tokens (lexer
);
1182 gcc_assert (lexer
->saved_tokens
.length () == len
);
1186 /* Print a representation of the TOKEN on the STREAM. */
1189 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1191 /* We don't use cpp_type2name here because the parser defines
1192 a few tokens of its own. */
1193 static const char *const token_names
[] = {
1194 /* cpplib-defined token types */
1195 #define OP(e, s) #e,
1196 #define TK(e, s) #e,
1200 /* C++ parser token types - see "Manifest constants", above. */
1203 "NESTED_NAME_SPECIFIER",
1206 /* For some tokens, print the associated data. */
1207 switch (token
->type
)
1210 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1211 For example, `struct' is mapped to an INTEGER_CST. */
1212 if (!identifier_p (token
->u
.value
))
1214 /* else fall through */
1216 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1223 case CPP_UTF8STRING
:
1224 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1228 print_generic_expr (stream
, token
->u
.value
, 0);
1232 /* If we have a name for the token, print it out. Otherwise, we
1233 simply give the numeric code. */
1234 if (token
->type
< ARRAY_SIZE(token_names
))
1235 fputs (token_names
[token
->type
], stream
);
1237 fprintf (stream
, "[%d]", token
->type
);
1243 debug (cp_token
&ref
)
1245 cp_lexer_print_token (stderr
, &ref
);
1246 fprintf (stderr
, "\n");
1250 debug (cp_token
*ptr
)
1255 fprintf (stderr
, "<nil>\n");
1259 /* Start emitting debugging information. */
1262 cp_lexer_start_debugging (cp_lexer
* lexer
)
1264 lexer
->debugging_p
= true;
1265 cp_lexer_debug_stream
= stderr
;
1268 /* Stop emitting debugging information. */
1271 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1273 lexer
->debugging_p
= false;
1274 cp_lexer_debug_stream
= NULL
;
1277 /* Create a new cp_token_cache, representing a range of tokens. */
1279 static cp_token_cache
*
1280 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1282 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1283 cache
->first
= first
;
1288 /* Diagnose if #pragma omp declare simd isn't followed immediately
1289 by function declaration or definition. */
1292 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1294 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1296 error ("%<#pragma omp declare simd%> not immediately followed by "
1297 "function declaration or definition");
1298 parser
->omp_declare_simd
= NULL
;
1302 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1303 and put that into "omp declare simd" attribute. */
1306 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1308 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1310 if (fndecl
== error_mark_node
)
1312 parser
->omp_declare_simd
= NULL
;
1315 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1317 cp_ensure_no_omp_declare_simd (parser
);
1323 /* Decl-specifiers. */
1325 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1328 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1330 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1335 /* Nothing other than the parser should be creating declarators;
1336 declarators are a semi-syntactic representation of C++ entities.
1337 Other parts of the front end that need to create entities (like
1338 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1340 static cp_declarator
*make_call_declarator
1341 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1342 static cp_declarator
*make_array_declarator
1343 (cp_declarator
*, tree
);
1344 static cp_declarator
*make_pointer_declarator
1345 (cp_cv_quals
, cp_declarator
*, tree
);
1346 static cp_declarator
*make_reference_declarator
1347 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1348 static cp_parameter_declarator
*make_parameter_declarator
1349 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1350 static cp_declarator
*make_ptrmem_declarator
1351 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1353 /* An erroneous declarator. */
1354 static cp_declarator
*cp_error_declarator
;
1356 /* The obstack on which declarators and related data structures are
1358 static struct obstack declarator_obstack
;
1360 /* Alloc BYTES from the declarator memory pool. */
1362 static inline void *
1363 alloc_declarator (size_t bytes
)
1365 return obstack_alloc (&declarator_obstack
, bytes
);
1368 /* Allocate a declarator of the indicated KIND. Clear fields that are
1369 common to all declarators. */
1371 static cp_declarator
*
1372 make_declarator (cp_declarator_kind kind
)
1374 cp_declarator
*declarator
;
1376 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1377 declarator
->kind
= kind
;
1378 declarator
->attributes
= NULL_TREE
;
1379 declarator
->std_attributes
= NULL_TREE
;
1380 declarator
->declarator
= NULL
;
1381 declarator
->parameter_pack_p
= false;
1382 declarator
->id_loc
= UNKNOWN_LOCATION
;
1387 /* Make a declarator for a generalized identifier. If
1388 QUALIFYING_SCOPE is non-NULL, the identifier is
1389 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1390 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1393 static cp_declarator
*
1394 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1395 special_function_kind sfk
)
1397 cp_declarator
*declarator
;
1399 /* It is valid to write:
1401 class C { void f(); };
1405 The standard is not clear about whether `typedef const C D' is
1406 legal; as of 2002-09-15 the committee is considering that
1407 question. EDG 3.0 allows that syntax. Therefore, we do as
1409 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1410 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1412 gcc_assert (identifier_p (unqualified_name
)
1413 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1414 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1416 declarator
= make_declarator (cdk_id
);
1417 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1418 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1419 declarator
->u
.id
.sfk
= sfk
;
1424 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1425 of modifiers such as const or volatile to apply to the pointer
1426 type, represented as identifiers. ATTRIBUTES represent the attributes that
1427 appertain to the pointer or reference. */
1430 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1433 cp_declarator
*declarator
;
1435 declarator
= make_declarator (cdk_pointer
);
1436 declarator
->declarator
= target
;
1437 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1438 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1441 declarator
->id_loc
= target
->id_loc
;
1442 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1443 target
->parameter_pack_p
= false;
1446 declarator
->parameter_pack_p
= false;
1448 declarator
->std_attributes
= attributes
;
1453 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1454 represent the attributes that appertain to the pointer or
1458 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1459 bool rvalue_ref
, tree attributes
)
1461 cp_declarator
*declarator
;
1463 declarator
= make_declarator (cdk_reference
);
1464 declarator
->declarator
= target
;
1465 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1466 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1469 declarator
->id_loc
= target
->id_loc
;
1470 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1471 target
->parameter_pack_p
= false;
1474 declarator
->parameter_pack_p
= false;
1476 declarator
->std_attributes
= attributes
;
1481 /* Like make_pointer_declarator -- but for a pointer to a non-static
1482 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1483 appertain to the pointer or reference. */
1486 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1487 cp_declarator
*pointee
,
1490 cp_declarator
*declarator
;
1492 declarator
= make_declarator (cdk_ptrmem
);
1493 declarator
->declarator
= pointee
;
1494 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1495 declarator
->u
.pointer
.class_type
= class_type
;
1499 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1500 pointee
->parameter_pack_p
= false;
1503 declarator
->parameter_pack_p
= false;
1505 declarator
->std_attributes
= attributes
;
1510 /* Make a declarator for the function given by TARGET, with the
1511 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1512 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1513 indicates what exceptions can be thrown. */
1516 make_call_declarator (cp_declarator
*target
,
1518 cp_cv_quals cv_qualifiers
,
1519 cp_virt_specifiers virt_specifiers
,
1520 cp_ref_qualifier ref_qualifier
,
1521 tree exception_specification
,
1522 tree late_return_type
)
1524 cp_declarator
*declarator
;
1526 declarator
= make_declarator (cdk_function
);
1527 declarator
->declarator
= target
;
1528 declarator
->u
.function
.parameters
= parms
;
1529 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1530 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1531 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1532 declarator
->u
.function
.exception_specification
= exception_specification
;
1533 declarator
->u
.function
.late_return_type
= late_return_type
;
1536 declarator
->id_loc
= target
->id_loc
;
1537 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1538 target
->parameter_pack_p
= false;
1541 declarator
->parameter_pack_p
= false;
1546 /* Make a declarator for an array of BOUNDS elements, each of which is
1547 defined by ELEMENT. */
1550 make_array_declarator (cp_declarator
*element
, tree bounds
)
1552 cp_declarator
*declarator
;
1554 declarator
= make_declarator (cdk_array
);
1555 declarator
->declarator
= element
;
1556 declarator
->u
.array
.bounds
= bounds
;
1559 declarator
->id_loc
= element
->id_loc
;
1560 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1561 element
->parameter_pack_p
= false;
1564 declarator
->parameter_pack_p
= false;
1569 /* Determine whether the declarator we've seen so far can be a
1570 parameter pack, when followed by an ellipsis. */
1572 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1574 /* Search for a declarator name, or any other declarator that goes
1575 after the point where the ellipsis could appear in a parameter
1576 pack. If we find any of these, then this declarator can not be
1577 made into a parameter pack. */
1579 while (declarator
&& !found
)
1581 switch ((int)declarator
->kind
)
1592 declarator
= declarator
->declarator
;
1600 cp_parameter_declarator
*no_parameters
;
1602 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1603 DECLARATOR and DEFAULT_ARGUMENT. */
1605 cp_parameter_declarator
*
1606 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1607 cp_declarator
*declarator
,
1608 tree default_argument
)
1610 cp_parameter_declarator
*parameter
;
1612 parameter
= ((cp_parameter_declarator
*)
1613 alloc_declarator (sizeof (cp_parameter_declarator
)));
1614 parameter
->next
= NULL
;
1615 if (decl_specifiers
)
1616 parameter
->decl_specifiers
= *decl_specifiers
;
1618 clear_decl_specs (¶meter
->decl_specifiers
);
1619 parameter
->declarator
= declarator
;
1620 parameter
->default_argument
= default_argument
;
1621 parameter
->ellipsis_p
= false;
1626 /* Returns true iff DECLARATOR is a declaration for a function. */
1629 function_declarator_p (const cp_declarator
*declarator
)
1633 if (declarator
->kind
== cdk_function
1634 && declarator
->declarator
->kind
== cdk_id
)
1636 if (declarator
->kind
== cdk_id
1637 || declarator
->kind
== cdk_error
)
1639 declarator
= declarator
->declarator
;
1649 A cp_parser parses the token stream as specified by the C++
1650 grammar. Its job is purely parsing, not semantic analysis. For
1651 example, the parser breaks the token stream into declarators,
1652 expressions, statements, and other similar syntactic constructs.
1653 It does not check that the types of the expressions on either side
1654 of an assignment-statement are compatible, or that a function is
1655 not declared with a parameter of type `void'.
1657 The parser invokes routines elsewhere in the compiler to perform
1658 semantic analysis and to build up the abstract syntax tree for the
1661 The parser (and the template instantiation code, which is, in a
1662 way, a close relative of parsing) are the only parts of the
1663 compiler that should be calling push_scope and pop_scope, or
1664 related functions. The parser (and template instantiation code)
1665 keeps track of what scope is presently active; everything else
1666 should simply honor that. (The code that generates static
1667 initializers may also need to set the scope, in order to check
1668 access control correctly when emitting the initializers.)
1673 The parser is of the standard recursive-descent variety. Upcoming
1674 tokens in the token stream are examined in order to determine which
1675 production to use when parsing a non-terminal. Some C++ constructs
1676 require arbitrary look ahead to disambiguate. For example, it is
1677 impossible, in the general case, to tell whether a statement is an
1678 expression or declaration without scanning the entire statement.
1679 Therefore, the parser is capable of "parsing tentatively." When the
1680 parser is not sure what construct comes next, it enters this mode.
1681 Then, while we attempt to parse the construct, the parser queues up
1682 error messages, rather than issuing them immediately, and saves the
1683 tokens it consumes. If the construct is parsed successfully, the
1684 parser "commits", i.e., it issues any queued error messages and
1685 the tokens that were being preserved are permanently discarded.
1686 If, however, the construct is not parsed successfully, the parser
1687 rolls back its state completely so that it can resume parsing using
1688 a different alternative.
1693 The performance of the parser could probably be improved substantially.
1694 We could often eliminate the need to parse tentatively by looking ahead
1695 a little bit. In some places, this approach might not entirely eliminate
1696 the need to parse tentatively, but it might still speed up the average
1699 /* Flags that are passed to some parsing functions. These values can
1700 be bitwise-ored together. */
1705 CP_PARSER_FLAGS_NONE
= 0x0,
1706 /* The construct is optional. If it is not present, then no error
1707 should be issued. */
1708 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1709 /* When parsing a type-specifier, treat user-defined type-names
1710 as non-type identifiers. */
1711 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1712 /* When parsing a type-specifier, do not try to parse a class-specifier
1713 or enum-specifier. */
1714 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1715 /* When parsing a decl-specifier-seq, only allow type-specifier or
1717 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1720 /* This type is used for parameters and variables which hold
1721 combinations of the above flags. */
1722 typedef int cp_parser_flags
;
1724 /* The different kinds of declarators we want to parse. */
1726 typedef enum cp_parser_declarator_kind
1728 /* We want an abstract declarator. */
1729 CP_PARSER_DECLARATOR_ABSTRACT
,
1730 /* We want a named declarator. */
1731 CP_PARSER_DECLARATOR_NAMED
,
1732 /* We don't mind, but the name must be an unqualified-id. */
1733 CP_PARSER_DECLARATOR_EITHER
1734 } cp_parser_declarator_kind
;
1736 /* The precedence values used to parse binary expressions. The minimum value
1737 of PREC must be 1, because zero is reserved to quickly discriminate
1738 binary operators from other tokens. */
1743 PREC_LOGICAL_OR_EXPRESSION
,
1744 PREC_LOGICAL_AND_EXPRESSION
,
1745 PREC_INCLUSIVE_OR_EXPRESSION
,
1746 PREC_EXCLUSIVE_OR_EXPRESSION
,
1747 PREC_AND_EXPRESSION
,
1748 PREC_EQUALITY_EXPRESSION
,
1749 PREC_RELATIONAL_EXPRESSION
,
1750 PREC_SHIFT_EXPRESSION
,
1751 PREC_ADDITIVE_EXPRESSION
,
1752 PREC_MULTIPLICATIVE_EXPRESSION
,
1754 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1757 /* A mapping from a token type to a corresponding tree node type, with a
1758 precedence value. */
1760 typedef struct cp_parser_binary_operations_map_node
1762 /* The token type. */
1763 enum cpp_ttype token_type
;
1764 /* The corresponding tree code. */
1765 enum tree_code tree_type
;
1766 /* The precedence of this operator. */
1767 enum cp_parser_prec prec
;
1768 } cp_parser_binary_operations_map_node
;
1770 typedef struct cp_parser_expression_stack_entry
1772 /* Left hand side of the binary operation we are currently
1775 /* Original tree code for left hand side, if it was a binary
1776 expression itself (used for -Wparentheses). */
1777 enum tree_code lhs_type
;
1778 /* Tree code for the binary operation we are parsing. */
1779 enum tree_code tree_type
;
1780 /* Precedence of the binary operation we are parsing. */
1781 enum cp_parser_prec prec
;
1782 /* Location of the binary operation we are parsing. */
1784 } cp_parser_expression_stack_entry
;
1786 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1787 entries because precedence levels on the stack are monotonically
1789 typedef struct cp_parser_expression_stack_entry
1790 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1794 /* Constructors and destructors. */
1796 static cp_parser_context
*cp_parser_context_new
1797 (cp_parser_context
*);
1799 /* Class variables. */
1801 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1803 /* The operator-precedence table used by cp_parser_binary_expression.
1804 Transformed into an associative array (binops_by_token) by
1807 static const cp_parser_binary_operations_map_node binops
[] = {
1808 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1809 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1811 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1812 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1813 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1815 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1816 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1818 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1819 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1821 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1822 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1823 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1824 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1826 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1827 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1829 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1831 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1833 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1835 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1837 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1840 /* The same as binops, but initialized by cp_parser_new so that
1841 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1843 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1845 /* Constructors and destructors. */
1847 /* Construct a new context. The context below this one on the stack
1848 is given by NEXT. */
1850 static cp_parser_context
*
1851 cp_parser_context_new (cp_parser_context
* next
)
1853 cp_parser_context
*context
;
1855 /* Allocate the storage. */
1856 if (cp_parser_context_free_list
!= NULL
)
1858 /* Pull the first entry from the free list. */
1859 context
= cp_parser_context_free_list
;
1860 cp_parser_context_free_list
= context
->next
;
1861 memset (context
, 0, sizeof (*context
));
1864 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1866 /* No errors have occurred yet in this context. */
1867 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1868 /* If this is not the bottommost context, copy information that we
1869 need from the previous context. */
1872 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1873 expression, then we are parsing one in this context, too. */
1874 context
->object_type
= next
->object_type
;
1875 /* Thread the stack. */
1876 context
->next
= next
;
1882 /* Managing the unparsed function queues. */
1884 #define unparsed_funs_with_default_args \
1885 parser->unparsed_queues->last ().funs_with_default_args
1886 #define unparsed_funs_with_definitions \
1887 parser->unparsed_queues->last ().funs_with_definitions
1888 #define unparsed_nsdmis \
1889 parser->unparsed_queues->last ().nsdmis
1890 #define unparsed_classes \
1891 parser->unparsed_queues->last ().classes
1894 push_unparsed_function_queues (cp_parser
*parser
)
1896 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1897 vec_safe_push (parser
->unparsed_queues
, e
);
1901 pop_unparsed_function_queues (cp_parser
*parser
)
1903 release_tree_vector (unparsed_funs_with_definitions
);
1904 parser
->unparsed_queues
->pop ();
1909 /* Constructors and destructors. */
1911 static cp_parser
*cp_parser_new
1914 /* Routines to parse various constructs.
1916 Those that return `tree' will return the error_mark_node (rather
1917 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1918 Sometimes, they will return an ordinary node if error-recovery was
1919 attempted, even though a parse error occurred. So, to check
1920 whether or not a parse error occurred, you should always use
1921 cp_parser_error_occurred. If the construct is optional (indicated
1922 either by an `_opt' in the name of the function that does the
1923 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1924 the construct is not present. */
1926 /* Lexical conventions [gram.lex] */
1928 static tree cp_parser_identifier
1930 static tree cp_parser_string_literal
1931 (cp_parser
*, bool, bool, bool);
1932 static tree cp_parser_userdef_char_literal
1934 static tree cp_parser_userdef_string_literal
1936 static tree cp_parser_userdef_numeric_literal
1939 /* Basic concepts [gram.basic] */
1941 static bool cp_parser_translation_unit
1944 /* Expressions [gram.expr] */
1946 static tree cp_parser_primary_expression
1947 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1948 static tree cp_parser_id_expression
1949 (cp_parser
*, bool, bool, bool *, bool, bool);
1950 static tree cp_parser_unqualified_id
1951 (cp_parser
*, bool, bool, bool, bool);
1952 static tree cp_parser_nested_name_specifier_opt
1953 (cp_parser
*, bool, bool, bool, bool);
1954 static tree cp_parser_nested_name_specifier
1955 (cp_parser
*, bool, bool, bool, bool);
1956 static tree cp_parser_qualifying_entity
1957 (cp_parser
*, bool, bool, bool, bool, bool);
1958 static tree cp_parser_postfix_expression
1959 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1960 static tree cp_parser_postfix_open_square_expression
1961 (cp_parser
*, tree
, bool, bool);
1962 static tree cp_parser_postfix_dot_deref_expression
1963 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1964 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1965 (cp_parser
*, int, bool, bool, bool *, bool = false);
1966 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1967 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1968 static void cp_parser_pseudo_destructor_name
1969 (cp_parser
*, tree
, tree
*, tree
*);
1970 static tree cp_parser_unary_expression
1971 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1972 static enum tree_code cp_parser_unary_operator
1974 static tree cp_parser_new_expression
1976 static vec
<tree
, va_gc
> *cp_parser_new_placement
1978 static tree cp_parser_new_type_id
1979 (cp_parser
*, tree
*);
1980 static cp_declarator
*cp_parser_new_declarator_opt
1982 static cp_declarator
*cp_parser_direct_new_declarator
1984 static vec
<tree
, va_gc
> *cp_parser_new_initializer
1986 static tree cp_parser_delete_expression
1988 static tree cp_parser_cast_expression
1989 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1990 static tree cp_parser_binary_expression
1991 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
1992 static tree cp_parser_question_colon_clause
1993 (cp_parser
*, tree
);
1994 static tree cp_parser_assignment_expression
1995 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
1996 static enum tree_code cp_parser_assignment_operator_opt
1998 static tree cp_parser_expression
1999 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2000 static tree cp_parser_constant_expression
2001 (cp_parser
*, bool = false, bool * = NULL
);
2002 static tree cp_parser_builtin_offsetof
2004 static tree cp_parser_lambda_expression
2006 static void cp_parser_lambda_introducer
2007 (cp_parser
*, tree
);
2008 static bool cp_parser_lambda_declarator_opt
2009 (cp_parser
*, tree
);
2010 static void cp_parser_lambda_body
2011 (cp_parser
*, tree
);
2013 /* Statements [gram.stmt.stmt] */
2015 static void cp_parser_statement
2016 (cp_parser
*, tree
, bool, bool *);
2017 static void cp_parser_label_for_labeled_statement
2018 (cp_parser
*, tree
);
2019 static tree cp_parser_expression_statement
2020 (cp_parser
*, tree
);
2021 static tree cp_parser_compound_statement
2022 (cp_parser
*, tree
, bool, bool);
2023 static void cp_parser_statement_seq_opt
2024 (cp_parser
*, tree
);
2025 static tree cp_parser_selection_statement
2026 (cp_parser
*, bool *);
2027 static tree cp_parser_condition
2029 static tree cp_parser_iteration_statement
2030 (cp_parser
*, bool);
2031 static bool cp_parser_for_init_statement
2032 (cp_parser
*, tree
*decl
);
2033 static tree cp_parser_for
2034 (cp_parser
*, bool);
2035 static tree cp_parser_c_for
2036 (cp_parser
*, tree
, tree
, bool);
2037 static tree cp_parser_range_for
2038 (cp_parser
*, tree
, tree
, tree
, bool);
2039 static void do_range_for_auto_deduction
2041 static tree cp_parser_perform_range_for_lookup
2042 (tree
, tree
*, tree
*);
2043 static tree cp_parser_range_for_member_function
2045 static tree cp_parser_jump_statement
2047 static void cp_parser_declaration_statement
2050 static tree cp_parser_implicitly_scoped_statement
2051 (cp_parser
*, bool *);
2052 static void cp_parser_already_scoped_statement
2055 /* Declarations [gram.dcl.dcl] */
2057 static void cp_parser_declaration_seq_opt
2059 static void cp_parser_declaration
2061 static void cp_parser_block_declaration
2062 (cp_parser
*, bool);
2063 static void cp_parser_simple_declaration
2064 (cp_parser
*, bool, tree
*);
2065 static void cp_parser_decl_specifier_seq
2066 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2067 static tree cp_parser_storage_class_specifier_opt
2069 static tree cp_parser_function_specifier_opt
2070 (cp_parser
*, cp_decl_specifier_seq
*);
2071 static tree cp_parser_type_specifier
2072 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2074 static tree cp_parser_simple_type_specifier
2075 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2076 static tree cp_parser_type_name
2078 static tree cp_parser_nonclass_name
2079 (cp_parser
* parser
);
2080 static tree cp_parser_elaborated_type_specifier
2081 (cp_parser
*, bool, bool);
2082 static tree cp_parser_enum_specifier
2084 static void cp_parser_enumerator_list
2085 (cp_parser
*, tree
);
2086 static void cp_parser_enumerator_definition
2087 (cp_parser
*, tree
);
2088 static tree cp_parser_namespace_name
2090 static void cp_parser_namespace_definition
2092 static void cp_parser_namespace_body
2094 static tree cp_parser_qualified_namespace_specifier
2096 static void cp_parser_namespace_alias_definition
2098 static bool cp_parser_using_declaration
2099 (cp_parser
*, bool);
2100 static void cp_parser_using_directive
2102 static tree cp_parser_alias_declaration
2104 static void cp_parser_asm_definition
2106 static void cp_parser_linkage_specification
2108 static void cp_parser_static_assert
2109 (cp_parser
*, bool);
2110 static tree cp_parser_decltype
2113 /* Declarators [gram.dcl.decl] */
2115 static tree cp_parser_init_declarator
2116 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*);
2117 static cp_declarator
*cp_parser_declarator
2118 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2119 static cp_declarator
*cp_parser_direct_declarator
2120 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2121 static enum tree_code cp_parser_ptr_operator
2122 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2123 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2125 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2127 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2129 static tree cp_parser_late_return_type_opt
2130 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2131 static tree cp_parser_declarator_id
2132 (cp_parser
*, bool);
2133 static tree cp_parser_type_id
2135 static tree cp_parser_template_type_arg
2137 static tree
cp_parser_trailing_type_id (cp_parser
*);
2138 static tree cp_parser_type_id_1
2139 (cp_parser
*, bool, bool);
2140 static void cp_parser_type_specifier_seq
2141 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2142 static tree cp_parser_parameter_declaration_clause
2144 static tree cp_parser_parameter_declaration_list
2145 (cp_parser
*, bool *);
2146 static cp_parameter_declarator
*cp_parser_parameter_declaration
2147 (cp_parser
*, bool, bool *);
2148 static tree cp_parser_default_argument
2149 (cp_parser
*, bool);
2150 static void cp_parser_function_body
2151 (cp_parser
*, bool);
2152 static tree cp_parser_initializer
2153 (cp_parser
*, bool *, bool *);
2154 static tree cp_parser_initializer_clause
2155 (cp_parser
*, bool *);
2156 static tree cp_parser_braced_list
2157 (cp_parser
*, bool*);
2158 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2159 (cp_parser
*, bool *);
2161 static bool cp_parser_ctor_initializer_opt_and_function_body
2162 (cp_parser
*, bool);
2164 static tree cp_parser_late_parsing_omp_declare_simd
2165 (cp_parser
*, tree
);
2167 static tree cp_parser_late_parsing_cilk_simd_fn_info
2168 (cp_parser
*, tree
);
2170 static tree synthesize_implicit_template_parm
2172 static tree finish_fully_implicit_template
2173 (cp_parser
*, tree
);
2175 /* Classes [gram.class] */
2177 static tree cp_parser_class_name
2178 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2179 static tree cp_parser_class_specifier
2181 static tree cp_parser_class_head
2182 (cp_parser
*, bool *);
2183 static enum tag_types cp_parser_class_key
2185 static void cp_parser_type_parameter_key
2186 (cp_parser
* parser
);
2187 static void cp_parser_member_specification_opt
2189 static void cp_parser_member_declaration
2191 static tree cp_parser_pure_specifier
2193 static tree cp_parser_constant_initializer
2196 /* Derived classes [gram.class.derived] */
2198 static tree cp_parser_base_clause
2200 static tree cp_parser_base_specifier
2203 /* Special member functions [gram.special] */
2205 static tree cp_parser_conversion_function_id
2207 static tree cp_parser_conversion_type_id
2209 static cp_declarator
*cp_parser_conversion_declarator_opt
2211 static bool cp_parser_ctor_initializer_opt
2213 static void cp_parser_mem_initializer_list
2215 static tree cp_parser_mem_initializer
2217 static tree cp_parser_mem_initializer_id
2220 /* Overloading [gram.over] */
2222 static tree cp_parser_operator_function_id
2224 static tree cp_parser_operator
2227 /* Templates [gram.temp] */
2229 static void cp_parser_template_declaration
2230 (cp_parser
*, bool);
2231 static tree cp_parser_template_parameter_list
2233 static tree cp_parser_template_parameter
2234 (cp_parser
*, bool *, bool *);
2235 static tree cp_parser_type_parameter
2236 (cp_parser
*, bool *);
2237 static tree cp_parser_template_id
2238 (cp_parser
*, bool, bool, enum tag_types
, bool);
2239 static tree cp_parser_template_name
2240 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2241 static tree cp_parser_template_argument_list
2243 static tree cp_parser_template_argument
2245 static void cp_parser_explicit_instantiation
2247 static void cp_parser_explicit_specialization
2250 /* Exception handling [gram.exception] */
2252 static tree cp_parser_try_block
2254 static bool cp_parser_function_try_block
2256 static void cp_parser_handler_seq
2258 static void cp_parser_handler
2260 static tree cp_parser_exception_declaration
2262 static tree cp_parser_throw_expression
2264 static tree cp_parser_exception_specification_opt
2266 static tree cp_parser_type_id_list
2269 /* GNU Extensions */
2271 static tree cp_parser_asm_specification_opt
2273 static tree cp_parser_asm_operand_list
2275 static tree cp_parser_asm_clobber_list
2277 static tree cp_parser_asm_label_list
2279 static bool cp_next_tokens_can_be_attribute_p
2281 static bool cp_next_tokens_can_be_gnu_attribute_p
2283 static bool cp_next_tokens_can_be_std_attribute_p
2285 static bool cp_nth_tokens_can_be_std_attribute_p
2286 (cp_parser
*, size_t);
2287 static bool cp_nth_tokens_can_be_gnu_attribute_p
2288 (cp_parser
*, size_t);
2289 static bool cp_nth_tokens_can_be_attribute_p
2290 (cp_parser
*, size_t);
2291 static tree cp_parser_attributes_opt
2293 static tree cp_parser_gnu_attributes_opt
2295 static tree cp_parser_gnu_attribute_list
2297 static tree cp_parser_std_attribute
2299 static tree cp_parser_std_attribute_spec
2301 static tree cp_parser_std_attribute_spec_seq
2303 static bool cp_parser_extension_opt
2304 (cp_parser
*, int *);
2305 static void cp_parser_label_declaration
2308 /* Transactional Memory Extensions */
2310 static tree cp_parser_transaction
2311 (cp_parser
*, enum rid
);
2312 static tree cp_parser_transaction_expression
2313 (cp_parser
*, enum rid
);
2314 static bool cp_parser_function_transaction
2315 (cp_parser
*, enum rid
);
2316 static tree cp_parser_transaction_cancel
2319 enum pragma_context
{
2326 static bool cp_parser_pragma
2327 (cp_parser
*, enum pragma_context
);
2329 /* Objective-C++ Productions */
2331 static tree cp_parser_objc_message_receiver
2333 static tree cp_parser_objc_message_args
2335 static tree cp_parser_objc_message_expression
2337 static tree cp_parser_objc_encode_expression
2339 static tree cp_parser_objc_defs_expression
2341 static tree cp_parser_objc_protocol_expression
2343 static tree cp_parser_objc_selector_expression
2345 static tree cp_parser_objc_expression
2347 static bool cp_parser_objc_selector_p
2349 static tree cp_parser_objc_selector
2351 static tree cp_parser_objc_protocol_refs_opt
2353 static void cp_parser_objc_declaration
2354 (cp_parser
*, tree
);
2355 static tree cp_parser_objc_statement
2357 static bool cp_parser_objc_valid_prefix_attributes
2358 (cp_parser
*, tree
*);
2359 static void cp_parser_objc_at_property_declaration
2361 static void cp_parser_objc_at_synthesize_declaration
2363 static void cp_parser_objc_at_dynamic_declaration
2365 static tree cp_parser_objc_struct_declaration
2368 /* Utility Routines */
2370 static tree cp_parser_lookup_name
2371 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2372 static tree cp_parser_lookup_name_simple
2373 (cp_parser
*, tree
, location_t
);
2374 static tree cp_parser_maybe_treat_template_as_class
2376 static bool cp_parser_check_declarator_template_parameters
2377 (cp_parser
*, cp_declarator
*, location_t
);
2378 static bool cp_parser_check_template_parameters
2379 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2380 static tree cp_parser_simple_cast_expression
2382 static tree cp_parser_global_scope_opt
2383 (cp_parser
*, bool);
2384 static bool cp_parser_constructor_declarator_p
2385 (cp_parser
*, bool);
2386 static tree cp_parser_function_definition_from_specifiers_and_declarator
2387 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2388 static tree cp_parser_function_definition_after_declarator
2389 (cp_parser
*, bool);
2390 static void cp_parser_template_declaration_after_export
2391 (cp_parser
*, bool);
2392 static void cp_parser_perform_template_parameter_access_checks
2393 (vec
<deferred_access_check
, va_gc
> *);
2394 static tree cp_parser_single_declaration
2395 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2396 static tree cp_parser_functional_cast
2397 (cp_parser
*, tree
);
2398 static tree cp_parser_save_member_function_body
2399 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2400 static tree cp_parser_save_nsdmi
2402 static tree cp_parser_enclosed_template_argument_list
2404 static void cp_parser_save_default_args
2405 (cp_parser
*, tree
);
2406 static void cp_parser_late_parsing_for_member
2407 (cp_parser
*, tree
);
2408 static tree cp_parser_late_parse_one_default_arg
2409 (cp_parser
*, tree
, tree
, tree
);
2410 static void cp_parser_late_parsing_nsdmi
2411 (cp_parser
*, tree
);
2412 static void cp_parser_late_parsing_default_args
2413 (cp_parser
*, tree
);
2414 static tree cp_parser_sizeof_operand
2415 (cp_parser
*, enum rid
);
2416 static tree cp_parser_trait_expr
2417 (cp_parser
*, enum rid
);
2418 static bool cp_parser_declares_only_class_p
2420 static void cp_parser_set_storage_class
2421 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2422 static void cp_parser_set_decl_spec_type
2423 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2424 static void set_and_check_decl_spec_loc
2425 (cp_decl_specifier_seq
*decl_specs
,
2426 cp_decl_spec ds
, cp_token
*);
2427 static bool cp_parser_friend_p
2428 (const cp_decl_specifier_seq
*);
2429 static void cp_parser_required_error
2430 (cp_parser
*, required_token
, bool);
2431 static cp_token
*cp_parser_require
2432 (cp_parser
*, enum cpp_ttype
, required_token
);
2433 static cp_token
*cp_parser_require_keyword
2434 (cp_parser
*, enum rid
, required_token
);
2435 static bool cp_parser_token_starts_function_definition_p
2437 static bool cp_parser_next_token_starts_class_definition_p
2439 static bool cp_parser_next_token_ends_template_argument_p
2441 static bool cp_parser_nth_token_starts_template_argument_list_p
2442 (cp_parser
*, size_t);
2443 static enum tag_types cp_parser_token_is_class_key
2445 static enum tag_types cp_parser_token_is_type_parameter_key
2447 static void cp_parser_check_class_key
2448 (enum tag_types
, tree type
);
2449 static void cp_parser_check_access_in_redeclaration
2450 (tree type
, location_t location
);
2451 static bool cp_parser_optional_template_keyword
2453 static void cp_parser_pre_parsed_nested_name_specifier
2455 static bool cp_parser_cache_group
2456 (cp_parser
*, enum cpp_ttype
, unsigned);
2457 static tree cp_parser_cache_defarg
2458 (cp_parser
*parser
, bool nsdmi
);
2459 static void cp_parser_parse_tentatively
2461 static void cp_parser_commit_to_tentative_parse
2463 static void cp_parser_commit_to_topmost_tentative_parse
2465 static void cp_parser_abort_tentative_parse
2467 static bool cp_parser_parse_definitely
2469 static inline bool cp_parser_parsing_tentatively
2471 static bool cp_parser_uncommitted_to_tentative_parse_p
2473 static void cp_parser_error
2474 (cp_parser
*, const char *);
2475 static void cp_parser_name_lookup_error
2476 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2477 static bool cp_parser_simulate_error
2479 static bool cp_parser_check_type_definition
2481 static void cp_parser_check_for_definition_in_return_type
2482 (cp_declarator
*, tree
, location_t type_location
);
2483 static void cp_parser_check_for_invalid_template_id
2484 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2485 static bool cp_parser_non_integral_constant_expression
2486 (cp_parser
*, non_integral_constant
);
2487 static void cp_parser_diagnose_invalid_type_name
2488 (cp_parser
*, tree
, location_t
);
2489 static bool cp_parser_parse_and_diagnose_invalid_type_name
2491 static int cp_parser_skip_to_closing_parenthesis
2492 (cp_parser
*, bool, bool, bool);
2493 static void cp_parser_skip_to_end_of_statement
2495 static void cp_parser_consume_semicolon_at_end_of_statement
2497 static void cp_parser_skip_to_end_of_block_or_statement
2499 static bool cp_parser_skip_to_closing_brace
2501 static void cp_parser_skip_to_end_of_template_parameter_list
2503 static void cp_parser_skip_to_pragma_eol
2504 (cp_parser
*, cp_token
*);
2505 static bool cp_parser_error_occurred
2507 static bool cp_parser_allow_gnu_extensions_p
2509 static bool cp_parser_is_pure_string_literal
2511 static bool cp_parser_is_string_literal
2513 static bool cp_parser_is_keyword
2514 (cp_token
*, enum rid
);
2515 static tree cp_parser_make_typename_type
2516 (cp_parser
*, tree
, location_t location
);
2517 static cp_declarator
* cp_parser_make_indirect_declarator
2518 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2519 static bool cp_parser_compound_literal_p
2521 static bool cp_parser_array_designator_p
2523 static bool cp_parser_skip_to_closing_square_bracket
2526 /* Returns nonzero if we are parsing tentatively. */
2529 cp_parser_parsing_tentatively (cp_parser
* parser
)
2531 return parser
->context
->next
!= NULL
;
2534 /* Returns nonzero if TOKEN is a string literal. */
2537 cp_parser_is_pure_string_literal (cp_token
* token
)
2539 return (token
->type
== CPP_STRING
||
2540 token
->type
== CPP_STRING16
||
2541 token
->type
== CPP_STRING32
||
2542 token
->type
== CPP_WSTRING
||
2543 token
->type
== CPP_UTF8STRING
);
2546 /* Returns nonzero if TOKEN is a string literal
2547 of a user-defined string literal. */
2550 cp_parser_is_string_literal (cp_token
* token
)
2552 return (cp_parser_is_pure_string_literal (token
) ||
2553 token
->type
== CPP_STRING_USERDEF
||
2554 token
->type
== CPP_STRING16_USERDEF
||
2555 token
->type
== CPP_STRING32_USERDEF
||
2556 token
->type
== CPP_WSTRING_USERDEF
||
2557 token
->type
== CPP_UTF8STRING_USERDEF
);
2560 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2563 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2565 return token
->keyword
== keyword
;
2568 /* If not parsing tentatively, issue a diagnostic of the form
2569 FILE:LINE: MESSAGE before TOKEN
2570 where TOKEN is the next token in the input stream. MESSAGE
2571 (specified by the caller) is usually of the form "expected
2575 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2577 if (!cp_parser_simulate_error (parser
))
2579 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2580 /* This diagnostic makes more sense if it is tagged to the line
2581 of the token we just peeked at. */
2582 cp_lexer_set_source_position_from_token (token
);
2584 if (token
->type
== CPP_PRAGMA
)
2586 error_at (token
->location
,
2587 "%<#pragma%> is not allowed here");
2588 cp_parser_skip_to_pragma_eol (parser
, token
);
2592 c_parse_error (gmsgid
,
2593 /* Because c_parser_error does not understand
2594 CPP_KEYWORD, keywords are treated like
2596 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2597 token
->u
.value
, token
->flags
);
2601 /* Issue an error about name-lookup failing. NAME is the
2602 IDENTIFIER_NODE DECL is the result of
2603 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2604 the thing that we hoped to find. */
2607 cp_parser_name_lookup_error (cp_parser
* parser
,
2610 name_lookup_error desired
,
2611 location_t location
)
2613 /* If name lookup completely failed, tell the user that NAME was not
2615 if (decl
== error_mark_node
)
2617 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2618 error_at (location
, "%<%E::%E%> has not been declared",
2619 parser
->scope
, name
);
2620 else if (parser
->scope
== global_namespace
)
2621 error_at (location
, "%<::%E%> has not been declared", name
);
2622 else if (parser
->object_scope
2623 && !CLASS_TYPE_P (parser
->object_scope
))
2624 error_at (location
, "request for member %qE in non-class type %qT",
2625 name
, parser
->object_scope
);
2626 else if (parser
->object_scope
)
2627 error_at (location
, "%<%T::%E%> has not been declared",
2628 parser
->object_scope
, name
);
2630 error_at (location
, "%qE has not been declared", name
);
2632 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2637 error_at (location
, "%<%E::%E%> is not a type",
2638 parser
->scope
, name
);
2641 error_at (location
, "%<%E::%E%> is not a class or namespace",
2642 parser
->scope
, name
);
2646 "%<%E::%E%> is not a class, namespace, or enumeration",
2647 parser
->scope
, name
);
2654 else if (parser
->scope
== global_namespace
)
2659 error_at (location
, "%<::%E%> is not a type", name
);
2662 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2666 "%<::%E%> is not a class, namespace, or enumeration",
2678 error_at (location
, "%qE is not a type", name
);
2681 error_at (location
, "%qE is not a class or namespace", name
);
2685 "%qE is not a class, namespace, or enumeration", name
);
2693 /* If we are parsing tentatively, remember that an error has occurred
2694 during this tentative parse. Returns true if the error was
2695 simulated; false if a message should be issued by the caller. */
2698 cp_parser_simulate_error (cp_parser
* parser
)
2700 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2702 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2708 /* This function is called when a type is defined. If type
2709 definitions are forbidden at this point, an error message is
2713 cp_parser_check_type_definition (cp_parser
* parser
)
2715 /* If types are forbidden here, issue a message. */
2716 if (parser
->type_definition_forbidden_message
)
2718 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2719 in the message need to be interpreted. */
2720 error (parser
->type_definition_forbidden_message
);
2726 /* This function is called when the DECLARATOR is processed. The TYPE
2727 was a type defined in the decl-specifiers. If it is invalid to
2728 define a type in the decl-specifiers for DECLARATOR, an error is
2729 issued. TYPE_LOCATION is the location of TYPE and is used
2730 for error reporting. */
2733 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2734 tree type
, location_t type_location
)
2736 /* [dcl.fct] forbids type definitions in return types.
2737 Unfortunately, it's not easy to know whether or not we are
2738 processing a return type until after the fact. */
2740 && (declarator
->kind
== cdk_pointer
2741 || declarator
->kind
== cdk_reference
2742 || declarator
->kind
== cdk_ptrmem
))
2743 declarator
= declarator
->declarator
;
2745 && declarator
->kind
== cdk_function
)
2747 error_at (type_location
,
2748 "new types may not be defined in a return type");
2749 inform (type_location
,
2750 "(perhaps a semicolon is missing after the definition of %qT)",
2755 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2756 "<" in any valid C++ program. If the next token is indeed "<",
2757 issue a message warning the user about what appears to be an
2758 invalid attempt to form a template-id. LOCATION is the location
2759 of the type-specifier (TYPE) */
2762 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2764 enum tag_types tag_type
,
2765 location_t location
)
2767 cp_token_position start
= 0;
2769 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2772 error_at (location
, "%qT is not a template", type
);
2773 else if (identifier_p (type
))
2775 if (tag_type
!= none_type
)
2776 error_at (location
, "%qE is not a class template", type
);
2778 error_at (location
, "%qE is not a template", type
);
2781 error_at (location
, "invalid template-id");
2782 /* Remember the location of the invalid "<". */
2783 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2784 start
= cp_lexer_token_position (parser
->lexer
, true);
2785 /* Consume the "<". */
2786 cp_lexer_consume_token (parser
->lexer
);
2787 /* Parse the template arguments. */
2788 cp_parser_enclosed_template_argument_list (parser
);
2789 /* Permanently remove the invalid template arguments so that
2790 this error message is not issued again. */
2792 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2796 /* If parsing an integral constant-expression, issue an error message
2797 about the fact that THING appeared and return true. Otherwise,
2798 return false. In either case, set
2799 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2802 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2803 non_integral_constant thing
)
2805 parser
->non_integral_constant_expression_p
= true;
2806 if (parser
->integral_constant_expression_p
)
2808 if (!parser
->allow_non_integral_constant_expression_p
)
2810 const char *msg
= NULL
;
2814 error ("floating-point literal "
2815 "cannot appear in a constant-expression");
2818 error ("a cast to a type other than an integral or "
2819 "enumeration type cannot appear in a "
2820 "constant-expression");
2823 error ("%<typeid%> operator "
2824 "cannot appear in a constant-expression");
2827 error ("non-constant compound literals "
2828 "cannot appear in a constant-expression");
2831 error ("a function call "
2832 "cannot appear in a constant-expression");
2835 error ("an increment "
2836 "cannot appear in a constant-expression");
2839 error ("an decrement "
2840 "cannot appear in a constant-expression");
2843 error ("an array reference "
2844 "cannot appear in a constant-expression");
2846 case NIC_ADDR_LABEL
:
2847 error ("the address of a label "
2848 "cannot appear in a constant-expression");
2850 case NIC_OVERLOADED
:
2851 error ("calls to overloaded operators "
2852 "cannot appear in a constant-expression");
2854 case NIC_ASSIGNMENT
:
2855 error ("an assignment cannot appear in a constant-expression");
2858 error ("a comma operator "
2859 "cannot appear in a constant-expression");
2861 case NIC_CONSTRUCTOR
:
2862 error ("a call to a constructor "
2863 "cannot appear in a constant-expression");
2865 case NIC_TRANSACTION
:
2866 error ("a transaction expression "
2867 "cannot appear in a constant-expression");
2873 msg
= "__FUNCTION__";
2875 case NIC_PRETTY_FUNC
:
2876 msg
= "__PRETTY_FUNCTION__";
2896 case NIC_PREINCREMENT
:
2899 case NIC_PREDECREMENT
:
2912 error ("%qs cannot appear in a constant-expression", msg
);
2919 /* Emit a diagnostic for an invalid type name. This function commits
2920 to the current active tentative parse, if any. (Otherwise, the
2921 problematic construct might be encountered again later, resulting
2922 in duplicate error messages.) LOCATION is the location of ID. */
2925 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2926 location_t location
)
2928 tree decl
, ambiguous_decls
;
2929 cp_parser_commit_to_tentative_parse (parser
);
2930 /* Try to lookup the identifier. */
2931 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2932 /*is_template=*/false,
2933 /*is_namespace=*/false,
2934 /*check_dependency=*/true,
2935 &ambiguous_decls
, location
);
2936 if (ambiguous_decls
)
2937 /* If the lookup was ambiguous, an error will already have
2940 /* If the lookup found a template-name, it means that the user forgot
2941 to specify an argument list. Emit a useful error message. */
2942 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2944 "invalid use of template-name %qE without an argument list",
2946 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2947 error_at (location
, "invalid use of destructor %qD as a type", id
);
2948 else if (TREE_CODE (decl
) == TYPE_DECL
)
2949 /* Something like 'unsigned A a;' */
2950 error_at (location
, "invalid combination of multiple type-specifiers");
2951 else if (!parser
->scope
)
2953 /* Issue an error message. */
2954 error_at (location
, "%qE does not name a type", id
);
2955 /* If we're in a template class, it's possible that the user was
2956 referring to a type from a base class. For example:
2958 template <typename T> struct A { typedef T X; };
2959 template <typename T> struct B : public A<T> { X x; };
2961 The user should have said "typename A<T>::X". */
2962 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2963 inform (location
, "C++11 %<constexpr%> only available with "
2964 "-std=c++11 or -std=gnu++11");
2965 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2966 inform (location
, "C++11 %<noexcept%> only available with "
2967 "-std=c++11 or -std=gnu++11");
2968 else if (cxx_dialect
< cxx11
2969 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2970 inform (location
, "C++11 %<thread_local%> only available with "
2971 "-std=c++11 or -std=gnu++11");
2972 else if (processing_template_decl
&& current_class_type
2973 && TYPE_BINFO (current_class_type
))
2977 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2981 tree base_type
= BINFO_TYPE (b
);
2982 if (CLASS_TYPE_P (base_type
)
2983 && dependent_type_p (base_type
))
2986 /* Go from a particular instantiation of the
2987 template (which will have an empty TYPE_FIELDs),
2988 to the main version. */
2989 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
2990 for (field
= TYPE_FIELDS (base_type
);
2992 field
= DECL_CHAIN (field
))
2993 if (TREE_CODE (field
) == TYPE_DECL
2994 && DECL_NAME (field
) == id
)
2997 "(perhaps %<typename %T::%E%> was intended)",
2998 BINFO_TYPE (b
), id
);
3007 /* Here we diagnose qualified-ids where the scope is actually correct,
3008 but the identifier does not resolve to a valid type name. */
3009 else if (parser
->scope
!= error_mark_node
)
3011 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3014 error_at (location_of (id
),
3015 "%qE in namespace %qE does not name a template type",
3018 error_at (location_of (id
),
3019 "%qE in namespace %qE does not name a type",
3022 else if (CLASS_TYPE_P (parser
->scope
)
3023 && constructor_name_p (id
, parser
->scope
))
3026 error_at (location
, "%<%T::%E%> names the constructor, not"
3027 " the type", parser
->scope
, id
);
3028 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3029 error_at (location
, "and %qT has no template constructors",
3032 else if (TYPE_P (parser
->scope
)
3033 && dependent_scope_p (parser
->scope
))
3034 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3035 "%qT is a dependent scope",
3036 parser
->scope
, id
, parser
->scope
);
3037 else if (TYPE_P (parser
->scope
))
3039 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3040 error_at (location_of (id
),
3041 "%qE in %q#T does not name a template type",
3044 error_at (location_of (id
),
3045 "%qE in %q#T does not name a type",
3053 /* Check for a common situation where a type-name should be present,
3054 but is not, and issue a sensible error message. Returns true if an
3055 invalid type-name was detected.
3057 The situation handled by this function are variable declarations of the
3058 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3059 Usually, `ID' should name a type, but if we got here it means that it
3060 does not. We try to emit the best possible error message depending on
3061 how exactly the id-expression looks like. */
3064 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3067 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3069 /* Avoid duplicate error about ambiguous lookup. */
3070 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3072 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3073 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3077 cp_parser_parse_tentatively (parser
);
3078 id
= cp_parser_id_expression (parser
,
3079 /*template_keyword_p=*/false,
3080 /*check_dependency_p=*/true,
3081 /*template_p=*/NULL
,
3082 /*declarator_p=*/true,
3083 /*optional_p=*/false);
3084 /* If the next token is a (, this is a function with no explicit return
3085 type, i.e. constructor, destructor or conversion op. */
3086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3087 || TREE_CODE (id
) == TYPE_DECL
)
3089 cp_parser_abort_tentative_parse (parser
);
3092 if (!cp_parser_parse_definitely (parser
))
3095 /* Emit a diagnostic for the invalid type. */
3096 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3098 /* If we aren't in the middle of a declarator (i.e. in a
3099 parameter-declaration-clause), skip to the end of the declaration;
3100 there's no point in trying to process it. */
3101 if (!parser
->in_declarator_p
)
3102 cp_parser_skip_to_end_of_block_or_statement (parser
);
3106 /* Consume tokens up to, and including, the next non-nested closing `)'.
3107 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3108 are doing error recovery. Returns -1 if OR_COMMA is true and we
3109 found an unnested comma. */
3112 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3117 unsigned paren_depth
= 0;
3118 unsigned brace_depth
= 0;
3119 unsigned square_depth
= 0;
3121 if (recovering
&& !or_comma
3122 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3127 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3129 switch (token
->type
)
3132 case CPP_PRAGMA_EOL
:
3133 /* If we've run out of tokens, then there is no closing `)'. */
3136 /* This is good for lambda expression capture-lists. */
3137 case CPP_OPEN_SQUARE
:
3140 case CPP_CLOSE_SQUARE
:
3141 if (!square_depth
--)
3146 /* This matches the processing in skip_to_end_of_statement. */
3151 case CPP_OPEN_BRACE
:
3154 case CPP_CLOSE_BRACE
:
3160 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3165 case CPP_OPEN_PAREN
:
3170 case CPP_CLOSE_PAREN
:
3171 if (!brace_depth
&& !paren_depth
--)
3174 cp_lexer_consume_token (parser
->lexer
);
3183 /* Consume the token. */
3184 cp_lexer_consume_token (parser
->lexer
);
3188 /* Consume tokens until we reach the end of the current statement.
3189 Normally, that will be just before consuming a `;'. However, if a
3190 non-nested `}' comes first, then we stop before consuming that. */
3193 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3195 unsigned nesting_depth
= 0;
3197 /* Unwind generic function template scope if necessary. */
3198 if (parser
->fully_implicit_function_template_p
)
3199 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3203 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3205 switch (token
->type
)
3208 case CPP_PRAGMA_EOL
:
3209 /* If we've run out of tokens, stop. */
3213 /* If the next token is a `;', we have reached the end of the
3219 case CPP_CLOSE_BRACE
:
3220 /* If this is a non-nested '}', stop before consuming it.
3221 That way, when confronted with something like:
3225 we stop before consuming the closing '}', even though we
3226 have not yet reached a `;'. */
3227 if (nesting_depth
== 0)
3230 /* If it is the closing '}' for a block that we have
3231 scanned, stop -- but only after consuming the token.
3237 we will stop after the body of the erroneously declared
3238 function, but before consuming the following `typedef'
3240 if (--nesting_depth
== 0)
3242 cp_lexer_consume_token (parser
->lexer
);
3246 case CPP_OPEN_BRACE
:
3254 /* Consume the token. */
3255 cp_lexer_consume_token (parser
->lexer
);
3259 /* This function is called at the end of a statement or declaration.
3260 If the next token is a semicolon, it is consumed; otherwise, error
3261 recovery is attempted. */
3264 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3266 /* Look for the trailing `;'. */
3267 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3269 /* If there is additional (erroneous) input, skip to the end of
3271 cp_parser_skip_to_end_of_statement (parser
);
3272 /* If the next token is now a `;', consume it. */
3273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3274 cp_lexer_consume_token (parser
->lexer
);
3278 /* Skip tokens until we have consumed an entire block, or until we
3279 have consumed a non-nested `;'. */
3282 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3284 int nesting_depth
= 0;
3286 /* Unwind generic function template scope if necessary. */
3287 if (parser
->fully_implicit_function_template_p
)
3288 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3290 while (nesting_depth
>= 0)
3292 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3294 switch (token
->type
)
3297 case CPP_PRAGMA_EOL
:
3298 /* If we've run out of tokens, stop. */
3302 /* Stop if this is an unnested ';'. */
3307 case CPP_CLOSE_BRACE
:
3308 /* Stop if this is an unnested '}', or closes the outermost
3311 if (nesting_depth
< 0)
3317 case CPP_OPEN_BRACE
:
3326 /* Consume the token. */
3327 cp_lexer_consume_token (parser
->lexer
);
3331 /* Skip tokens until a non-nested closing curly brace is the next
3332 token, or there are no more tokens. Return true in the first case,
3336 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3338 unsigned nesting_depth
= 0;
3342 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3344 switch (token
->type
)
3347 case CPP_PRAGMA_EOL
:
3348 /* If we've run out of tokens, stop. */
3351 case CPP_CLOSE_BRACE
:
3352 /* If the next token is a non-nested `}', then we have reached
3353 the end of the current block. */
3354 if (nesting_depth
-- == 0)
3358 case CPP_OPEN_BRACE
:
3359 /* If it the next token is a `{', then we are entering a new
3360 block. Consume the entire block. */
3368 /* Consume the token. */
3369 cp_lexer_consume_token (parser
->lexer
);
3373 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3374 parameter is the PRAGMA token, allowing us to purge the entire pragma
3378 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3382 parser
->lexer
->in_pragma
= false;
3385 token
= cp_lexer_consume_token (parser
->lexer
);
3386 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3388 /* Ensure that the pragma is not parsed again. */
3389 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3392 /* Require pragma end of line, resyncing with it as necessary. The
3393 arguments are as for cp_parser_skip_to_pragma_eol. */
3396 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3398 parser
->lexer
->in_pragma
= false;
3399 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3400 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3403 /* This is a simple wrapper around make_typename_type. When the id is
3404 an unresolved identifier node, we can provide a superior diagnostic
3405 using cp_parser_diagnose_invalid_type_name. */
3408 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3409 location_t id_location
)
3412 if (identifier_p (id
))
3414 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3415 /*complain=*/tf_none
);
3416 if (result
== error_mark_node
)
3417 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3420 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3423 /* This is a wrapper around the
3424 make_{pointer,ptrmem,reference}_declarator functions that decides
3425 which one to call based on the CODE and CLASS_TYPE arguments. The
3426 CODE argument should be one of the values returned by
3427 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3428 appertain to the pointer or reference. */
3430 static cp_declarator
*
3431 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3432 cp_cv_quals cv_qualifiers
,
3433 cp_declarator
*target
,
3436 if (code
== ERROR_MARK
)
3437 return cp_error_declarator
;
3439 if (code
== INDIRECT_REF
)
3440 if (class_type
== NULL_TREE
)
3441 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3443 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3444 target
, attributes
);
3445 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3446 return make_reference_declarator (cv_qualifiers
, target
,
3448 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3449 return make_reference_declarator (cv_qualifiers
, target
,
3454 /* Create a new C++ parser. */
3457 cp_parser_new (void)
3463 /* cp_lexer_new_main is called before doing GC allocation because
3464 cp_lexer_new_main might load a PCH file. */
3465 lexer
= cp_lexer_new_main ();
3467 /* Initialize the binops_by_token so that we can get the tree
3468 directly from the token. */
3469 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3470 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3472 parser
= ggc_cleared_alloc
<cp_parser
> ();
3473 parser
->lexer
= lexer
;
3474 parser
->context
= cp_parser_context_new (NULL
);
3476 /* For now, we always accept GNU extensions. */
3477 parser
->allow_gnu_extensions_p
= 1;
3479 /* The `>' token is a greater-than operator, not the end of a
3481 parser
->greater_than_is_operator_p
= true;
3483 parser
->default_arg_ok_p
= true;
3485 /* We are not parsing a constant-expression. */
3486 parser
->integral_constant_expression_p
= false;
3487 parser
->allow_non_integral_constant_expression_p
= false;
3488 parser
->non_integral_constant_expression_p
= false;
3490 /* Local variable names are not forbidden. */
3491 parser
->local_variables_forbidden_p
= false;
3493 /* We are not processing an `extern "C"' declaration. */
3494 parser
->in_unbraced_linkage_specification_p
= false;
3496 /* We are not processing a declarator. */
3497 parser
->in_declarator_p
= false;
3499 /* We are not processing a template-argument-list. */
3500 parser
->in_template_argument_list_p
= false;
3502 /* We are not in an iteration statement. */
3503 parser
->in_statement
= 0;
3505 /* We are not in a switch statement. */
3506 parser
->in_switch_statement_p
= false;
3508 /* We are not parsing a type-id inside an expression. */
3509 parser
->in_type_id_in_expr_p
= false;
3511 /* Declarations aren't implicitly extern "C". */
3512 parser
->implicit_extern_c
= false;
3514 /* String literals should be translated to the execution character set. */
3515 parser
->translate_strings_p
= true;
3517 /* We are not parsing a function body. */
3518 parser
->in_function_body
= false;
3520 /* We can correct until told otherwise. */
3521 parser
->colon_corrects_to_scope_p
= true;
3523 /* The unparsed function queue is empty. */
3524 push_unparsed_function_queues (parser
);
3526 /* There are no classes being defined. */
3527 parser
->num_classes_being_defined
= 0;
3529 /* No template parameters apply. */
3530 parser
->num_template_parameter_lists
= 0;
3532 /* Not declaring an implicit function template. */
3533 parser
->auto_is_implicit_function_template_parm_p
= false;
3534 parser
->fully_implicit_function_template_p
= false;
3535 parser
->implicit_template_parms
= 0;
3536 parser
->implicit_template_scope
= 0;
3541 /* Create a cp_lexer structure which will emit the tokens in CACHE
3542 and push it onto the parser's lexer stack. This is used for delayed
3543 parsing of in-class method bodies and default arguments, and should
3544 not be confused with tentative parsing. */
3546 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3548 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3549 lexer
->next
= parser
->lexer
;
3550 parser
->lexer
= lexer
;
3552 /* Move the current source position to that of the first token in the
3554 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3557 /* Pop the top lexer off the parser stack. This is never used for the
3558 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3560 cp_parser_pop_lexer (cp_parser
*parser
)
3562 cp_lexer
*lexer
= parser
->lexer
;
3563 parser
->lexer
= lexer
->next
;
3564 cp_lexer_destroy (lexer
);
3566 /* Put the current source position back where it was before this
3567 lexer was pushed. */
3568 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3571 /* Lexical conventions [gram.lex] */
3573 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3577 cp_parser_identifier (cp_parser
* parser
)
3581 /* Look for the identifier. */
3582 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3583 /* Return the value. */
3584 return token
? token
->u
.value
: error_mark_node
;
3587 /* Parse a sequence of adjacent string constants. Returns a
3588 TREE_STRING representing the combined, nul-terminated string
3589 constant. If TRANSLATE is true, translate the string to the
3590 execution character set. If WIDE_OK is true, a wide string is
3593 C++98 [lex.string] says that if a narrow string literal token is
3594 adjacent to a wide string literal token, the behavior is undefined.
3595 However, C99 6.4.5p4 says that this results in a wide string literal.
3596 We follow C99 here, for consistency with the C front end.
3598 This code is largely lifted from lex_string() in c-lex.c.
3600 FUTURE: ObjC++ will need to handle @-strings here. */
3602 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3603 bool lookup_udlit
= true)
3607 struct obstack str_ob
;
3608 cpp_string str
, istr
, *strs
;
3610 enum cpp_ttype type
, curr_type
;
3611 int have_suffix_p
= 0;
3613 tree suffix_id
= NULL_TREE
;
3614 bool curr_tok_is_userdef_p
= false;
3616 tok
= cp_lexer_peek_token (parser
->lexer
);
3617 if (!cp_parser_is_string_literal (tok
))
3619 cp_parser_error (parser
, "expected string-literal");
3620 return error_mark_node
;
3623 if (cpp_userdef_string_p (tok
->type
))
3625 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3626 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3627 curr_tok_is_userdef_p
= true;
3631 string_tree
= tok
->u
.value
;
3632 curr_type
= tok
->type
;
3636 /* Try to avoid the overhead of creating and destroying an obstack
3637 for the common case of just one string. */
3638 if (!cp_parser_is_string_literal
3639 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3641 cp_lexer_consume_token (parser
->lexer
);
3643 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3644 str
.len
= TREE_STRING_LENGTH (string_tree
);
3647 if (curr_tok_is_userdef_p
)
3649 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3651 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3654 curr_type
= tok
->type
;
3660 gcc_obstack_init (&str_ob
);
3665 cp_lexer_consume_token (parser
->lexer
);
3667 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3668 str
.len
= TREE_STRING_LENGTH (string_tree
);
3670 if (curr_tok_is_userdef_p
)
3672 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3673 if (have_suffix_p
== 0)
3675 suffix_id
= curr_suffix_id
;
3678 else if (have_suffix_p
== 1
3679 && curr_suffix_id
!= suffix_id
)
3681 error ("inconsistent user-defined literal suffixes"
3682 " %qD and %qD in string literal",
3683 suffix_id
, curr_suffix_id
);
3686 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3689 curr_type
= tok
->type
;
3691 if (type
!= curr_type
)
3693 if (type
== CPP_STRING
)
3695 else if (curr_type
!= CPP_STRING
)
3696 error_at (tok
->location
,
3697 "unsupported non-standard concatenation "
3698 "of string literals");
3701 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3703 tok
= cp_lexer_peek_token (parser
->lexer
);
3704 if (cpp_userdef_string_p (tok
->type
))
3706 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3707 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3708 curr_tok_is_userdef_p
= true;
3712 string_tree
= tok
->u
.value
;
3713 curr_type
= tok
->type
;
3714 curr_tok_is_userdef_p
= false;
3717 while (cp_parser_is_string_literal (tok
));
3719 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3722 if (type
!= CPP_STRING
&& !wide_ok
)
3724 cp_parser_error (parser
, "a wide string is invalid in this context");
3728 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3729 (parse_in
, strs
, count
, &istr
, type
))
3731 value
= build_string (istr
.len
, (const char *)istr
.text
);
3732 free (CONST_CAST (unsigned char *, istr
.text
));
3738 case CPP_UTF8STRING
:
3739 TREE_TYPE (value
) = char_array_type_node
;
3742 TREE_TYPE (value
) = char16_array_type_node
;
3745 TREE_TYPE (value
) = char32_array_type_node
;
3748 TREE_TYPE (value
) = wchar_array_type_node
;
3752 value
= fix_string_type (value
);
3756 tree literal
= build_userdef_literal (suffix_id
, value
,
3757 OT_NONE
, NULL_TREE
);
3759 value
= cp_parser_userdef_string_literal (literal
);
3765 /* cpp_interpret_string has issued an error. */
3766 value
= error_mark_node
;
3769 obstack_free (&str_ob
, 0);
3774 /* Look up a literal operator with the name and the exact arguments. */
3777 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3780 decl
= lookup_name (name
);
3781 if (!decl
|| !is_overloaded_fn (decl
))
3782 return error_mark_node
;
3784 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3788 tree fn
= OVL_CURRENT (fns
);
3789 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3790 if (parmtypes
!= NULL_TREE
)
3792 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3793 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3795 tree tparm
= TREE_VALUE (parmtypes
);
3796 tree targ
= TREE_TYPE ((*args
)[ix
]);
3797 bool ptr
= TYPE_PTR_P (tparm
);
3798 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3799 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3801 || !same_type_p (TREE_TYPE (tparm
),
3806 && ix
== vec_safe_length (args
)
3807 /* May be this should be sufficient_parms_p instead,
3808 depending on how exactly should user-defined literals
3809 work in presence of default arguments on the literal
3810 operator parameters. */
3811 && parmtypes
== void_list_node
)
3816 return error_mark_node
;
3819 /* Parse a user-defined char constant. Returns a call to a user-defined
3820 literal operator taking the character as an argument. */
3823 cp_parser_userdef_char_literal (cp_parser
*parser
)
3825 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3826 tree literal
= token
->u
.value
;
3827 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3828 tree value
= USERDEF_LITERAL_VALUE (literal
);
3829 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3832 /* Build up a call to the user-defined operator */
3833 /* Lookup the name we got back from the id-expression. */
3834 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3835 vec_safe_push (args
, value
);
3836 decl
= lookup_literal_operator (name
, args
);
3837 if (!decl
|| decl
== error_mark_node
)
3839 error ("unable to find character literal operator %qD with %qT argument",
3840 name
, TREE_TYPE (value
));
3841 release_tree_vector (args
);
3842 return error_mark_node
;
3844 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3845 release_tree_vector (args
);
3846 if (result
!= error_mark_node
)
3849 error ("unable to find character literal operator %qD with %qT argument",
3850 name
, TREE_TYPE (value
));
3851 return error_mark_node
;
3854 /* A subroutine of cp_parser_userdef_numeric_literal to
3855 create a char... template parameter pack from a string node. */
3858 make_char_string_pack (tree value
)
3861 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3862 const char *str
= TREE_STRING_POINTER (value
);
3863 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3864 tree argvec
= make_tree_vec (1);
3866 /* Fill in CHARVEC with all of the parameters. */
3867 charvec
= make_tree_vec (len
);
3868 for (i
= 0; i
< len
; ++i
)
3869 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3871 /* Build the argument packs. */
3872 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3873 TREE_TYPE (argpack
) = char_type_node
;
3875 TREE_VEC_ELT (argvec
, 0) = argpack
;
3880 /* A subroutine of cp_parser_userdef_numeric_literal to
3881 create a char... template parameter pack from a string node. */
3884 make_string_pack (tree value
)
3887 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3888 const unsigned char *str
3889 = (const unsigned char *) TREE_STRING_POINTER (value
);
3890 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3891 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3892 tree argvec
= make_tree_vec (2);
3894 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3895 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3897 /* First template parm is character type. */
3898 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3900 /* Fill in CHARVEC with all of the parameters. */
3901 charvec
= make_tree_vec (len
);
3902 for (int i
= 0; i
< len
; ++i
)
3903 TREE_VEC_ELT (charvec
, i
)
3904 = double_int_to_tree (str_char_type_node
,
3905 double_int::from_buffer (str
+ i
* sz
, sz
));
3907 /* Build the argument packs. */
3908 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3909 TREE_TYPE (argpack
) = str_char_type_node
;
3911 TREE_VEC_ELT (argvec
, 1) = argpack
;
3916 /* Parse a user-defined numeric constant. returns a call to a user-defined
3917 literal operator. */
3920 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3922 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3923 tree literal
= token
->u
.value
;
3924 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3925 tree value
= USERDEF_LITERAL_VALUE (literal
);
3926 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3927 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3928 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3930 vec
<tree
, va_gc
> *args
;
3932 /* Look for a literal operator taking the exact type of numeric argument
3933 as the literal value. */
3934 args
= make_tree_vector ();
3935 vec_safe_push (args
, value
);
3936 decl
= lookup_literal_operator (name
, args
);
3937 if (decl
&& decl
!= error_mark_node
)
3939 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3940 if (result
!= error_mark_node
)
3942 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3943 warning_at (token
->location
, OPT_Woverflow
,
3944 "integer literal exceeds range of %qT type",
3945 long_long_unsigned_type_node
);
3949 warning_at (token
->location
, OPT_Woverflow
,
3950 "floating literal exceeds range of %qT type",
3951 long_double_type_node
);
3952 else if (overflow
< 0)
3953 warning_at (token
->location
, OPT_Woverflow
,
3954 "floating literal truncated to zero");
3956 release_tree_vector (args
);
3960 release_tree_vector (args
);
3962 /* If the numeric argument didn't work, look for a raw literal
3963 operator taking a const char* argument consisting of the number
3964 in string format. */
3965 args
= make_tree_vector ();
3966 vec_safe_push (args
, num_string
);
3967 decl
= lookup_literal_operator (name
, args
);
3968 if (decl
&& decl
!= error_mark_node
)
3970 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3971 if (result
!= error_mark_node
)
3973 release_tree_vector (args
);
3977 release_tree_vector (args
);
3979 /* If the raw literal didn't work, look for a non-type template
3980 function with parameter pack char.... Call the function with
3981 template parameter characters representing the number. */
3982 args
= make_tree_vector ();
3983 decl
= lookup_literal_operator (name
, args
);
3984 if (decl
&& decl
!= error_mark_node
)
3986 tree tmpl_args
= make_char_string_pack (num_string
);
3987 decl
= lookup_template_function (decl
, tmpl_args
);
3988 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3989 if (result
!= error_mark_node
)
3991 release_tree_vector (args
);
3995 release_tree_vector (args
);
3997 error ("unable to find numeric literal operator %qD", name
);
3998 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
3999 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4000 "to enable more built-in suffixes");
4001 return error_mark_node
;
4004 /* Parse a user-defined string constant. Returns a call to a user-defined
4005 literal operator taking a character pointer and the length of the string
4009 cp_parser_userdef_string_literal (tree literal
)
4011 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4012 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4013 tree value
= USERDEF_LITERAL_VALUE (literal
);
4014 int len
= TREE_STRING_LENGTH (value
)
4015 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4017 vec
<tree
, va_gc
> *args
;
4019 /* Look for a template function with typename parameter CharT
4020 and parameter pack CharT... Call the function with
4021 template parameter characters representing the string. */
4022 args
= make_tree_vector ();
4023 decl
= lookup_literal_operator (name
, args
);
4024 if (decl
&& decl
!= error_mark_node
)
4026 tree tmpl_args
= make_string_pack (value
);
4027 decl
= lookup_template_function (decl
, tmpl_args
);
4028 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4029 if (result
!= error_mark_node
)
4031 release_tree_vector (args
);
4035 release_tree_vector (args
);
4037 /* Build up a call to the user-defined operator */
4038 /* Lookup the name we got back from the id-expression. */
4039 args
= make_tree_vector ();
4040 vec_safe_push (args
, value
);
4041 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4042 decl
= lookup_name (name
);
4043 if (!decl
|| decl
== error_mark_node
)
4045 error ("unable to find string literal operator %qD", name
);
4046 release_tree_vector (args
);
4047 return error_mark_node
;
4049 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4050 release_tree_vector (args
);
4051 if (result
!= error_mark_node
)
4054 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4055 name
, TREE_TYPE (value
), size_type_node
);
4056 return error_mark_node
;
4060 /* Basic concepts [gram.basic] */
4062 /* Parse a translation-unit.
4065 declaration-seq [opt]
4067 Returns TRUE if all went well. */
4070 cp_parser_translation_unit (cp_parser
* parser
)
4072 /* The address of the first non-permanent object on the declarator
4074 static void *declarator_obstack_base
;
4078 /* Create the declarator obstack, if necessary. */
4079 if (!cp_error_declarator
)
4081 gcc_obstack_init (&declarator_obstack
);
4082 /* Create the error declarator. */
4083 cp_error_declarator
= make_declarator (cdk_error
);
4084 /* Create the empty parameter list. */
4085 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4086 /* Remember where the base of the declarator obstack lies. */
4087 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4090 cp_parser_declaration_seq_opt (parser
);
4092 /* If there are no tokens left then all went well. */
4093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4095 /* Get rid of the token array; we don't need it any more. */
4096 cp_lexer_destroy (parser
->lexer
);
4097 parser
->lexer
= NULL
;
4099 /* This file might have been a context that's implicitly extern
4100 "C". If so, pop the lang context. (Only relevant for PCH.) */
4101 if (parser
->implicit_extern_c
)
4103 pop_lang_context ();
4104 parser
->implicit_extern_c
= false;
4108 finish_translation_unit ();
4114 cp_parser_error (parser
, "expected declaration");
4118 /* Make sure the declarator obstack was fully cleaned up. */
4119 gcc_assert (obstack_next_free (&declarator_obstack
)
4120 == declarator_obstack_base
);
4122 /* All went well. */
4126 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4127 decltype context. */
4129 static inline tsubst_flags_t
4130 complain_flags (bool decltype_p
)
4132 tsubst_flags_t complain
= tf_warning_or_error
;
4134 complain
|= tf_decltype
;
4138 /* We're about to parse a collection of statements. If we're currently
4139 parsing tentatively, set up a firewall so that any nested
4140 cp_parser_commit_to_tentative_parse won't affect the current context. */
4142 static cp_token_position
4143 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4145 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4148 cp_parser_parse_tentatively (parser
);
4149 cp_parser_commit_to_topmost_tentative_parse (parser
);
4150 return cp_lexer_token_position (parser
->lexer
, false);
4153 /* We've finished parsing the collection of statements. Wrap up the
4154 firewall and replace the relevant tokens with the parsed form. */
4157 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4163 /* Finish the firewall level. */
4164 cp_parser_parse_definitely (parser
);
4165 /* And remember the result of the parse for when we try again. */
4166 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4167 token
->type
= CPP_PREPARSED_EXPR
;
4168 token
->u
.value
= expr
;
4169 token
->keyword
= RID_MAX
;
4170 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4173 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4174 enclosing parentheses. */
4177 cp_parser_statement_expr (cp_parser
*parser
)
4179 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4181 /* Consume the '('. */
4182 cp_lexer_consume_token (parser
->lexer
);
4183 /* Start the statement-expression. */
4184 tree expr
= begin_stmt_expr ();
4185 /* Parse the compound-statement. */
4186 cp_parser_compound_statement (parser
, expr
, false, false);
4188 expr
= finish_stmt_expr (expr
, false);
4189 /* Consume the ')'. */
4190 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4191 cp_parser_skip_to_end_of_statement (parser
);
4193 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4197 /* Expressions [gram.expr] */
4199 /* Parse a primary-expression.
4206 lambda-expression (C++11)
4211 ( compound-statement )
4212 __builtin_va_arg ( assignment-expression , type-id )
4213 __builtin_offsetof ( type-id , offsetof-expression )
4216 __has_nothrow_assign ( type-id )
4217 __has_nothrow_constructor ( type-id )
4218 __has_nothrow_copy ( type-id )
4219 __has_trivial_assign ( type-id )
4220 __has_trivial_constructor ( type-id )
4221 __has_trivial_copy ( type-id )
4222 __has_trivial_destructor ( type-id )
4223 __has_virtual_destructor ( type-id )
4224 __is_abstract ( type-id )
4225 __is_base_of ( type-id , type-id )
4226 __is_class ( type-id )
4227 __is_empty ( type-id )
4228 __is_enum ( type-id )
4229 __is_final ( type-id )
4230 __is_literal_type ( type-id )
4231 __is_pod ( type-id )
4232 __is_polymorphic ( type-id )
4233 __is_std_layout ( type-id )
4234 __is_trivial ( type-id )
4235 __is_union ( type-id )
4237 Objective-C++ Extension:
4245 ADDRESS_P is true iff this expression was immediately preceded by
4246 "&" and therefore might denote a pointer-to-member. CAST_P is true
4247 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4248 true iff this expression is a template argument.
4250 Returns a representation of the expression. Upon return, *IDK
4251 indicates what kind of id-expression (if any) was present. */
4254 cp_parser_primary_expression (cp_parser
*parser
,
4257 bool template_arg_p
,
4261 cp_token
*token
= NULL
;
4263 /* Assume the primary expression is not an id-expression. */
4264 *idk
= CP_ID_KIND_NONE
;
4266 /* Peek at the next token. */
4267 token
= cp_lexer_peek_token (parser
->lexer
);
4268 switch ((int) token
->type
)
4277 user-defined-literal */
4283 case CPP_PREPARSED_EXPR
:
4284 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4285 return cp_parser_userdef_numeric_literal (parser
);
4286 token
= cp_lexer_consume_token (parser
->lexer
);
4287 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4289 error_at (token
->location
,
4290 "fixed-point types not supported in C++");
4291 return error_mark_node
;
4293 /* Floating-point literals are only allowed in an integral
4294 constant expression if they are cast to an integral or
4295 enumeration type. */
4296 if (TREE_CODE (token
->u
.value
) == REAL_CST
4297 && parser
->integral_constant_expression_p
4300 /* CAST_P will be set even in invalid code like "int(2.7 +
4301 ...)". Therefore, we have to check that the next token
4302 is sure to end the cast. */
4305 cp_token
*next_token
;
4307 next_token
= cp_lexer_peek_token (parser
->lexer
);
4308 if (/* The comma at the end of an
4309 enumerator-definition. */
4310 next_token
->type
!= CPP_COMMA
4311 /* The curly brace at the end of an enum-specifier. */
4312 && next_token
->type
!= CPP_CLOSE_BRACE
4313 /* The end of a statement. */
4314 && next_token
->type
!= CPP_SEMICOLON
4315 /* The end of the cast-expression. */
4316 && next_token
->type
!= CPP_CLOSE_PAREN
4317 /* The end of an array bound. */
4318 && next_token
->type
!= CPP_CLOSE_SQUARE
4319 /* The closing ">" in a template-argument-list. */
4320 && (next_token
->type
!= CPP_GREATER
4321 || parser
->greater_than_is_operator_p
)
4322 /* C++0x only: A ">>" treated like two ">" tokens,
4323 in a template-argument-list. */
4324 && (next_token
->type
!= CPP_RSHIFT
4325 || (cxx_dialect
== cxx98
)
4326 || parser
->greater_than_is_operator_p
))
4330 /* If we are within a cast, then the constraint that the
4331 cast is to an integral or enumeration type will be
4332 checked at that point. If we are not within a cast, then
4333 this code is invalid. */
4335 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4337 return token
->u
.value
;
4339 case CPP_CHAR_USERDEF
:
4340 case CPP_CHAR16_USERDEF
:
4341 case CPP_CHAR32_USERDEF
:
4342 case CPP_WCHAR_USERDEF
:
4343 return cp_parser_userdef_char_literal (parser
);
4349 case CPP_UTF8STRING
:
4350 case CPP_STRING_USERDEF
:
4351 case CPP_STRING16_USERDEF
:
4352 case CPP_STRING32_USERDEF
:
4353 case CPP_WSTRING_USERDEF
:
4354 case CPP_UTF8STRING_USERDEF
:
4355 /* ??? Should wide strings be allowed when parser->translate_strings_p
4356 is false (i.e. in attributes)? If not, we can kill the third
4357 argument to cp_parser_string_literal. */
4358 return cp_parser_string_literal (parser
,
4359 parser
->translate_strings_p
,
4362 case CPP_OPEN_PAREN
:
4363 /* If we see `( { ' then we are looking at the beginning of
4364 a GNU statement-expression. */
4365 if (cp_parser_allow_gnu_extensions_p (parser
)
4366 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4368 /* Statement-expressions are not allowed by the standard. */
4369 pedwarn (token
->location
, OPT_Wpedantic
,
4370 "ISO C++ forbids braced-groups within expressions");
4372 /* And they're not allowed outside of a function-body; you
4373 cannot, for example, write:
4375 int i = ({ int j = 3; j + 1; });
4377 at class or namespace scope. */
4378 if (!parser
->in_function_body
4379 || parser
->in_template_argument_list_p
)
4381 error_at (token
->location
,
4382 "statement-expressions are not allowed outside "
4383 "functions nor in template-argument lists");
4384 cp_parser_skip_to_end_of_block_or_statement (parser
);
4385 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4386 cp_lexer_consume_token (parser
->lexer
);
4387 return error_mark_node
;
4390 return cp_parser_statement_expr (parser
);
4392 /* Otherwise it's a normal parenthesized expression. */
4395 bool saved_greater_than_is_operator_p
;
4397 /* Consume the `('. */
4398 cp_lexer_consume_token (parser
->lexer
);
4399 /* Within a parenthesized expression, a `>' token is always
4400 the greater-than operator. */
4401 saved_greater_than_is_operator_p
4402 = parser
->greater_than_is_operator_p
;
4403 parser
->greater_than_is_operator_p
= true;
4405 /* Parse the parenthesized expression. */
4406 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4407 /* Let the front end know that this expression was
4408 enclosed in parentheses. This matters in case, for
4409 example, the expression is of the form `A::B', since
4410 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4412 expr
= finish_parenthesized_expr (expr
);
4413 /* DR 705: Wrapping an unqualified name in parentheses
4414 suppresses arg-dependent lookup. We want to pass back
4415 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4416 (c++/37862), but none of the others. */
4417 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4418 *idk
= CP_ID_KIND_NONE
;
4420 /* The `>' token might be the end of a template-id or
4421 template-parameter-list now. */
4422 parser
->greater_than_is_operator_p
4423 = saved_greater_than_is_operator_p
;
4424 /* Consume the `)'. */
4425 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4426 cp_parser_skip_to_end_of_statement (parser
);
4431 case CPP_OPEN_SQUARE
:
4432 if (c_dialect_objc ())
4433 /* We have an Objective-C++ message. */
4434 return cp_parser_objc_expression (parser
);
4436 tree lam
= cp_parser_lambda_expression (parser
);
4437 /* Don't warn about a failed tentative parse. */
4438 if (cp_parser_error_occurred (parser
))
4439 return error_mark_node
;
4440 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4444 case CPP_OBJC_STRING
:
4445 if (c_dialect_objc ())
4446 /* We have an Objective-C++ string literal. */
4447 return cp_parser_objc_expression (parser
);
4448 cp_parser_error (parser
, "expected primary-expression");
4449 return error_mark_node
;
4452 switch (token
->keyword
)
4454 /* These two are the boolean literals. */
4456 cp_lexer_consume_token (parser
->lexer
);
4457 return boolean_true_node
;
4459 cp_lexer_consume_token (parser
->lexer
);
4460 return boolean_false_node
;
4462 /* The `__null' literal. */
4464 cp_lexer_consume_token (parser
->lexer
);
4467 /* The `nullptr' literal. */
4469 cp_lexer_consume_token (parser
->lexer
);
4470 return nullptr_node
;
4472 /* Recognize the `this' keyword. */
4474 cp_lexer_consume_token (parser
->lexer
);
4475 if (parser
->local_variables_forbidden_p
)
4477 error_at (token
->location
,
4478 "%<this%> may not be used in this context");
4479 return error_mark_node
;
4481 /* Pointers cannot appear in constant-expressions. */
4482 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4483 return error_mark_node
;
4484 return finish_this_expr ();
4486 /* The `operator' keyword can be the beginning of an
4491 case RID_FUNCTION_NAME
:
4492 case RID_PRETTY_FUNCTION_NAME
:
4493 case RID_C99_FUNCTION_NAME
:
4495 non_integral_constant name
;
4497 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4498 __func__ are the names of variables -- but they are
4499 treated specially. Therefore, they are handled here,
4500 rather than relying on the generic id-expression logic
4501 below. Grammatically, these names are id-expressions.
4503 Consume the token. */
4504 token
= cp_lexer_consume_token (parser
->lexer
);
4506 switch (token
->keyword
)
4508 case RID_FUNCTION_NAME
:
4509 name
= NIC_FUNC_NAME
;
4511 case RID_PRETTY_FUNCTION_NAME
:
4512 name
= NIC_PRETTY_FUNC
;
4514 case RID_C99_FUNCTION_NAME
:
4515 name
= NIC_C99_FUNC
;
4521 if (cp_parser_non_integral_constant_expression (parser
, name
))
4522 return error_mark_node
;
4524 /* Look up the name. */
4525 return finish_fname (token
->u
.value
);
4532 source_location type_location
;
4534 /* The `__builtin_va_arg' construct is used to handle
4535 `va_arg'. Consume the `__builtin_va_arg' token. */
4536 cp_lexer_consume_token (parser
->lexer
);
4537 /* Look for the opening `('. */
4538 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4539 /* Now, parse the assignment-expression. */
4540 expression
= cp_parser_assignment_expression (parser
);
4541 /* Look for the `,'. */
4542 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4543 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4544 /* Parse the type-id. */
4545 type
= cp_parser_type_id (parser
);
4546 /* Look for the closing `)'. */
4547 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4548 /* Using `va_arg' in a constant-expression is not
4550 if (cp_parser_non_integral_constant_expression (parser
,
4552 return error_mark_node
;
4553 return build_x_va_arg (type_location
, expression
, type
);
4557 return cp_parser_builtin_offsetof (parser
);
4559 case RID_HAS_NOTHROW_ASSIGN
:
4560 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4561 case RID_HAS_NOTHROW_COPY
:
4562 case RID_HAS_TRIVIAL_ASSIGN
:
4563 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4564 case RID_HAS_TRIVIAL_COPY
:
4565 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4566 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4567 case RID_IS_ABSTRACT
:
4568 case RID_IS_BASE_OF
:
4573 case RID_IS_LITERAL_TYPE
:
4575 case RID_IS_POLYMORPHIC
:
4576 case RID_IS_STD_LAYOUT
:
4577 case RID_IS_TRIVIAL
:
4578 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4579 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4580 case RID_IS_TRIVIALLY_COPYABLE
:
4582 return cp_parser_trait_expr (parser
, token
->keyword
);
4584 /* Objective-C++ expressions. */
4586 case RID_AT_PROTOCOL
:
4587 case RID_AT_SELECTOR
:
4588 return cp_parser_objc_expression (parser
);
4591 if (parser
->in_function_body
4592 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4595 error_at (token
->location
,
4596 "a template declaration cannot appear at block scope");
4597 cp_parser_skip_to_end_of_block_or_statement (parser
);
4598 return error_mark_node
;
4601 cp_parser_error (parser
, "expected primary-expression");
4602 return error_mark_node
;
4605 /* An id-expression can start with either an identifier, a
4606 `::' as the beginning of a qualified-id, or the "operator"
4610 case CPP_TEMPLATE_ID
:
4611 case CPP_NESTED_NAME_SPECIFIER
:
4615 const char *error_msg
;
4618 cp_token
*id_expr_token
;
4621 /* Parse the id-expression. */
4623 = cp_parser_id_expression (parser
,
4624 /*template_keyword_p=*/false,
4625 /*check_dependency_p=*/true,
4627 /*declarator_p=*/false,
4628 /*optional_p=*/false);
4629 if (id_expression
== error_mark_node
)
4630 return error_mark_node
;
4631 id_expr_token
= token
;
4632 token
= cp_lexer_peek_token (parser
->lexer
);
4633 done
= (token
->type
!= CPP_OPEN_SQUARE
4634 && token
->type
!= CPP_OPEN_PAREN
4635 && token
->type
!= CPP_DOT
4636 && token
->type
!= CPP_DEREF
4637 && token
->type
!= CPP_PLUS_PLUS
4638 && token
->type
!= CPP_MINUS_MINUS
);
4639 /* If we have a template-id, then no further lookup is
4640 required. If the template-id was for a template-class, we
4641 will sometimes have a TYPE_DECL at this point. */
4642 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4643 || TREE_CODE (id_expression
) == TYPE_DECL
)
4644 decl
= id_expression
;
4645 /* Look up the name. */
4648 tree ambiguous_decls
;
4650 /* If we already know that this lookup is ambiguous, then
4651 we've already issued an error message; there's no reason
4653 if (id_expr_token
->type
== CPP_NAME
4654 && id_expr_token
->error_reported
)
4656 cp_parser_simulate_error (parser
);
4657 return error_mark_node
;
4660 decl
= cp_parser_lookup_name (parser
, id_expression
,
4663 /*is_namespace=*/false,
4664 /*check_dependency=*/true,
4666 id_expr_token
->location
);
4667 /* If the lookup was ambiguous, an error will already have
4669 if (ambiguous_decls
)
4670 return error_mark_node
;
4672 /* In Objective-C++, we may have an Objective-C 2.0
4673 dot-syntax for classes here. */
4674 if (c_dialect_objc ()
4675 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4676 && TREE_CODE (decl
) == TYPE_DECL
4677 && objc_is_class_name (decl
))
4680 cp_lexer_consume_token (parser
->lexer
);
4681 component
= cp_parser_identifier (parser
);
4682 if (component
== error_mark_node
)
4683 return error_mark_node
;
4685 return objc_build_class_component_ref (id_expression
, component
);
4688 /* In Objective-C++, an instance variable (ivar) may be preferred
4689 to whatever cp_parser_lookup_name() found. */
4690 decl
= objc_lookup_ivar (decl
, id_expression
);
4692 /* If name lookup gives us a SCOPE_REF, then the
4693 qualifying scope was dependent. */
4694 if (TREE_CODE (decl
) == SCOPE_REF
)
4696 /* At this point, we do not know if DECL is a valid
4697 integral constant expression. We assume that it is
4698 in fact such an expression, so that code like:
4700 template <int N> struct A {
4704 is accepted. At template-instantiation time, we
4705 will check that B<N>::i is actually a constant. */
4708 /* Check to see if DECL is a local variable in a context
4709 where that is forbidden. */
4710 if (parser
->local_variables_forbidden_p
4711 && local_variable_p (decl
))
4713 /* It might be that we only found DECL because we are
4714 trying to be generous with pre-ISO scoping rules.
4715 For example, consider:
4719 for (int i = 0; i < 10; ++i) {}
4720 extern void f(int j = i);
4723 Here, name look up will originally find the out
4724 of scope `i'. We need to issue a warning message,
4725 but then use the global `i'. */
4726 decl
= check_for_out_of_scope_variable (decl
);
4727 if (local_variable_p (decl
))
4729 error_at (id_expr_token
->location
,
4730 "local variable %qD may not appear in this context",
4732 return error_mark_node
;
4737 decl
= (finish_id_expression
4738 (id_expression
, decl
, parser
->scope
,
4740 parser
->integral_constant_expression_p
,
4741 parser
->allow_non_integral_constant_expression_p
,
4742 &parser
->non_integral_constant_expression_p
,
4743 template_p
, done
, address_p
,
4746 id_expr_token
->location
));
4748 cp_parser_error (parser
, error_msg
);
4752 /* Anything else is an error. */
4754 cp_parser_error (parser
, "expected primary-expression");
4755 return error_mark_node
;
4760 cp_parser_primary_expression (cp_parser
*parser
,
4763 bool template_arg_p
,
4766 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4767 /*decltype*/false, idk
);
4770 /* Parse an id-expression.
4777 :: [opt] nested-name-specifier template [opt] unqualified-id
4779 :: operator-function-id
4782 Return a representation of the unqualified portion of the
4783 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4784 a `::' or nested-name-specifier.
4786 Often, if the id-expression was a qualified-id, the caller will
4787 want to make a SCOPE_REF to represent the qualified-id. This
4788 function does not do this in order to avoid wastefully creating
4789 SCOPE_REFs when they are not required.
4791 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4794 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4795 uninstantiated templates.
4797 If *TEMPLATE_P is non-NULL, it is set to true iff the
4798 `template' keyword is used to explicitly indicate that the entity
4799 named is a template.
4801 If DECLARATOR_P is true, the id-expression is appearing as part of
4802 a declarator, rather than as part of an expression. */
4805 cp_parser_id_expression (cp_parser
*parser
,
4806 bool template_keyword_p
,
4807 bool check_dependency_p
,
4812 bool global_scope_p
;
4813 bool nested_name_specifier_p
;
4815 /* Assume the `template' keyword was not used. */
4817 *template_p
= template_keyword_p
;
4819 /* Look for the optional `::' operator. */
4821 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4823 /* Look for the optional nested-name-specifier. */
4824 nested_name_specifier_p
4825 = (cp_parser_nested_name_specifier_opt (parser
,
4826 /*typename_keyword_p=*/false,
4831 /* If there is a nested-name-specifier, then we are looking at
4832 the first qualified-id production. */
4833 if (nested_name_specifier_p
)
4836 tree saved_object_scope
;
4837 tree saved_qualifying_scope
;
4838 tree unqualified_id
;
4841 /* See if the next token is the `template' keyword. */
4843 template_p
= &is_template
;
4844 *template_p
= cp_parser_optional_template_keyword (parser
);
4845 /* Name lookup we do during the processing of the
4846 unqualified-id might obliterate SCOPE. */
4847 saved_scope
= parser
->scope
;
4848 saved_object_scope
= parser
->object_scope
;
4849 saved_qualifying_scope
= parser
->qualifying_scope
;
4850 /* Process the final unqualified-id. */
4851 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4854 /*optional_p=*/false);
4855 /* Restore the SAVED_SCOPE for our caller. */
4856 parser
->scope
= saved_scope
;
4857 parser
->object_scope
= saved_object_scope
;
4858 parser
->qualifying_scope
= saved_qualifying_scope
;
4860 return unqualified_id
;
4862 /* Otherwise, if we are in global scope, then we are looking at one
4863 of the other qualified-id productions. */
4864 else if (global_scope_p
)
4869 /* Peek at the next token. */
4870 token
= cp_lexer_peek_token (parser
->lexer
);
4872 /* If it's an identifier, and the next token is not a "<", then
4873 we can avoid the template-id case. This is an optimization
4874 for this common case. */
4875 if (token
->type
== CPP_NAME
4876 && !cp_parser_nth_token_starts_template_argument_list_p
4878 return cp_parser_identifier (parser
);
4880 cp_parser_parse_tentatively (parser
);
4881 /* Try a template-id. */
4882 id
= cp_parser_template_id (parser
,
4883 /*template_keyword_p=*/false,
4884 /*check_dependency_p=*/true,
4887 /* If that worked, we're done. */
4888 if (cp_parser_parse_definitely (parser
))
4891 /* Peek at the next token. (Changes in the token buffer may
4892 have invalidated the pointer obtained above.) */
4893 token
= cp_lexer_peek_token (parser
->lexer
);
4895 switch (token
->type
)
4898 return cp_parser_identifier (parser
);
4901 if (token
->keyword
== RID_OPERATOR
)
4902 return cp_parser_operator_function_id (parser
);
4906 cp_parser_error (parser
, "expected id-expression");
4907 return error_mark_node
;
4911 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4912 /*check_dependency_p=*/true,
4917 /* Parse an unqualified-id.
4921 operator-function-id
4922 conversion-function-id
4926 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4927 keyword, in a construct like `A::template ...'.
4929 Returns a representation of unqualified-id. For the `identifier'
4930 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4931 production a BIT_NOT_EXPR is returned; the operand of the
4932 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4933 other productions, see the documentation accompanying the
4934 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4935 names are looked up in uninstantiated templates. If DECLARATOR_P
4936 is true, the unqualified-id is appearing as part of a declarator,
4937 rather than as part of an expression. */
4940 cp_parser_unqualified_id (cp_parser
* parser
,
4941 bool template_keyword_p
,
4942 bool check_dependency_p
,
4948 /* Peek at the next token. */
4949 token
= cp_lexer_peek_token (parser
->lexer
);
4951 switch ((int) token
->type
)
4957 /* We don't know yet whether or not this will be a
4959 cp_parser_parse_tentatively (parser
);
4960 /* Try a template-id. */
4961 id
= cp_parser_template_id (parser
, template_keyword_p
,
4965 /* If it worked, we're done. */
4966 if (cp_parser_parse_definitely (parser
))
4968 /* Otherwise, it's an ordinary identifier. */
4969 return cp_parser_identifier (parser
);
4972 case CPP_TEMPLATE_ID
:
4973 return cp_parser_template_id (parser
, template_keyword_p
,
4981 tree qualifying_scope
;
4986 /* Consume the `~' token. */
4987 cp_lexer_consume_token (parser
->lexer
);
4988 /* Parse the class-name. The standard, as written, seems to
4991 template <typename T> struct S { ~S (); };
4992 template <typename T> S<T>::~S() {}
4994 is invalid, since `~' must be followed by a class-name, but
4995 `S<T>' is dependent, and so not known to be a class.
4996 That's not right; we need to look in uninstantiated
4997 templates. A further complication arises from:
4999 template <typename T> void f(T t) {
5003 Here, it is not possible to look up `T' in the scope of `T'
5004 itself. We must look in both the current scope, and the
5005 scope of the containing complete expression.
5007 Yet another issue is:
5016 The standard does not seem to say that the `S' in `~S'
5017 should refer to the type `S' and not the data member
5020 /* DR 244 says that we look up the name after the "~" in the
5021 same scope as we looked up the qualifying name. That idea
5022 isn't fully worked out; it's more complicated than that. */
5023 scope
= parser
->scope
;
5024 object_scope
= parser
->object_scope
;
5025 qualifying_scope
= parser
->qualifying_scope
;
5027 /* Check for invalid scopes. */
5028 if (scope
== error_mark_node
)
5030 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5031 cp_lexer_consume_token (parser
->lexer
);
5032 return error_mark_node
;
5034 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5036 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5037 error_at (token
->location
,
5038 "scope %qT before %<~%> is not a class-name",
5040 cp_parser_simulate_error (parser
);
5041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5042 cp_lexer_consume_token (parser
->lexer
);
5043 return error_mark_node
;
5045 gcc_assert (!scope
|| TYPE_P (scope
));
5047 /* If the name is of the form "X::~X" it's OK even if X is a
5049 token
= cp_lexer_peek_token (parser
->lexer
);
5051 && token
->type
== CPP_NAME
5052 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5054 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5055 || (CLASS_TYPE_P (scope
)
5056 && constructor_name_p (token
->u
.value
, scope
))))
5058 cp_lexer_consume_token (parser
->lexer
);
5059 return build_nt (BIT_NOT_EXPR
, scope
);
5062 /* ~auto means the destructor of whatever the object is. */
5063 if (cp_parser_is_keyword (token
, RID_AUTO
))
5065 if (cxx_dialect
< cxx14
)
5066 pedwarn (input_location
, 0,
5067 "%<~auto%> only available with "
5068 "-std=c++14 or -std=gnu++14");
5069 cp_lexer_consume_token (parser
->lexer
);
5070 return build_nt (BIT_NOT_EXPR
, make_auto ());
5073 /* If there was an explicit qualification (S::~T), first look
5074 in the scope given by the qualification (i.e., S).
5076 Note: in the calls to cp_parser_class_name below we pass
5077 typename_type so that lookup finds the injected-class-name
5078 rather than the constructor. */
5080 type_decl
= NULL_TREE
;
5083 cp_parser_parse_tentatively (parser
);
5084 type_decl
= cp_parser_class_name (parser
,
5085 /*typename_keyword_p=*/false,
5086 /*template_keyword_p=*/false,
5088 /*check_dependency=*/false,
5089 /*class_head_p=*/false,
5091 if (cp_parser_parse_definitely (parser
))
5094 /* In "N::S::~S", look in "N" as well. */
5095 if (!done
&& scope
&& qualifying_scope
)
5097 cp_parser_parse_tentatively (parser
);
5098 parser
->scope
= qualifying_scope
;
5099 parser
->object_scope
= NULL_TREE
;
5100 parser
->qualifying_scope
= NULL_TREE
;
5102 = cp_parser_class_name (parser
,
5103 /*typename_keyword_p=*/false,
5104 /*template_keyword_p=*/false,
5106 /*check_dependency=*/false,
5107 /*class_head_p=*/false,
5109 if (cp_parser_parse_definitely (parser
))
5112 /* In "p->S::~T", look in the scope given by "*p" as well. */
5113 else if (!done
&& object_scope
)
5115 cp_parser_parse_tentatively (parser
);
5116 parser
->scope
= object_scope
;
5117 parser
->object_scope
= NULL_TREE
;
5118 parser
->qualifying_scope
= NULL_TREE
;
5120 = cp_parser_class_name (parser
,
5121 /*typename_keyword_p=*/false,
5122 /*template_keyword_p=*/false,
5124 /*check_dependency=*/false,
5125 /*class_head_p=*/false,
5127 if (cp_parser_parse_definitely (parser
))
5130 /* Look in the surrounding context. */
5133 parser
->scope
= NULL_TREE
;
5134 parser
->object_scope
= NULL_TREE
;
5135 parser
->qualifying_scope
= NULL_TREE
;
5136 if (processing_template_decl
)
5137 cp_parser_parse_tentatively (parser
);
5139 = cp_parser_class_name (parser
,
5140 /*typename_keyword_p=*/false,
5141 /*template_keyword_p=*/false,
5143 /*check_dependency=*/false,
5144 /*class_head_p=*/false,
5146 if (processing_template_decl
5147 && ! cp_parser_parse_definitely (parser
))
5149 /* We couldn't find a type with this name, so just accept
5150 it and check for a match at instantiation time. */
5151 type_decl
= cp_parser_identifier (parser
);
5152 if (type_decl
!= error_mark_node
)
5153 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5157 /* If an error occurred, assume that the name of the
5158 destructor is the same as the name of the qualifying
5159 class. That allows us to keep parsing after running
5160 into ill-formed destructor names. */
5161 if (type_decl
== error_mark_node
&& scope
)
5162 return build_nt (BIT_NOT_EXPR
, scope
);
5163 else if (type_decl
== error_mark_node
)
5164 return error_mark_node
;
5166 /* Check that destructor name and scope match. */
5167 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5169 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5170 error_at (token
->location
,
5171 "declaration of %<~%T%> as member of %qT",
5173 cp_parser_simulate_error (parser
);
5174 return error_mark_node
;
5179 A typedef-name that names a class shall not be used as the
5180 identifier in the declarator for a destructor declaration. */
5182 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5183 && !DECL_SELF_REFERENCE_P (type_decl
)
5184 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5185 error_at (token
->location
,
5186 "typedef-name %qD used as destructor declarator",
5189 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5193 if (token
->keyword
== RID_OPERATOR
)
5197 /* This could be a template-id, so we try that first. */
5198 cp_parser_parse_tentatively (parser
);
5199 /* Try a template-id. */
5200 id
= cp_parser_template_id (parser
, template_keyword_p
,
5201 /*check_dependency_p=*/true,
5204 /* If that worked, we're done. */
5205 if (cp_parser_parse_definitely (parser
))
5207 /* We still don't know whether we're looking at an
5208 operator-function-id or a conversion-function-id. */
5209 cp_parser_parse_tentatively (parser
);
5210 /* Try an operator-function-id. */
5211 id
= cp_parser_operator_function_id (parser
);
5212 /* If that didn't work, try a conversion-function-id. */
5213 if (!cp_parser_parse_definitely (parser
))
5214 id
= cp_parser_conversion_function_id (parser
);
5215 else if (UDLIT_OPER_P (id
))
5218 const char *name
= UDLIT_OP_SUFFIX (id
);
5219 if (name
[0] != '_' && !in_system_header_at (input_location
)
5221 warning (0, "literal operator suffixes not preceded by %<_%>"
5222 " are reserved for future standardization");
5232 cp_parser_error (parser
, "expected unqualified-id");
5233 return error_mark_node
;
5237 /* Parse an (optional) nested-name-specifier.
5239 nested-name-specifier: [C++98]
5240 class-or-namespace-name :: nested-name-specifier [opt]
5241 class-or-namespace-name :: template nested-name-specifier [opt]
5243 nested-name-specifier: [C++0x]
5246 nested-name-specifier identifier ::
5247 nested-name-specifier template [opt] simple-template-id ::
5249 PARSER->SCOPE should be set appropriately before this function is
5250 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5251 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5254 Sets PARSER->SCOPE to the class (TYPE) or namespace
5255 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5256 it unchanged if there is no nested-name-specifier. Returns the new
5257 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5259 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5260 part of a declaration and/or decl-specifier. */
5263 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5264 bool typename_keyword_p
,
5265 bool check_dependency_p
,
5267 bool is_declaration
)
5269 bool success
= false;
5270 cp_token_position start
= 0;
5273 /* Remember where the nested-name-specifier starts. */
5274 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5276 start
= cp_lexer_token_position (parser
->lexer
, false);
5277 push_deferring_access_checks (dk_deferred
);
5284 tree saved_qualifying_scope
;
5285 bool template_keyword_p
;
5287 /* Spot cases that cannot be the beginning of a
5288 nested-name-specifier. */
5289 token
= cp_lexer_peek_token (parser
->lexer
);
5291 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5292 the already parsed nested-name-specifier. */
5293 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5295 /* Grab the nested-name-specifier and continue the loop. */
5296 cp_parser_pre_parsed_nested_name_specifier (parser
);
5297 /* If we originally encountered this nested-name-specifier
5298 with IS_DECLARATION set to false, we will not have
5299 resolved TYPENAME_TYPEs, so we must do so here. */
5301 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5303 new_scope
= resolve_typename_type (parser
->scope
,
5304 /*only_current_p=*/false);
5305 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5306 parser
->scope
= new_scope
;
5312 /* Spot cases that cannot be the beginning of a
5313 nested-name-specifier. On the second and subsequent times
5314 through the loop, we look for the `template' keyword. */
5315 if (success
&& token
->keyword
== RID_TEMPLATE
)
5317 /* A template-id can start a nested-name-specifier. */
5318 else if (token
->type
== CPP_TEMPLATE_ID
)
5320 /* DR 743: decltype can be used in a nested-name-specifier. */
5321 else if (token_is_decltype (token
))
5325 /* If the next token is not an identifier, then it is
5326 definitely not a type-name or namespace-name. */
5327 if (token
->type
!= CPP_NAME
)
5329 /* If the following token is neither a `<' (to begin a
5330 template-id), nor a `::', then we are not looking at a
5331 nested-name-specifier. */
5332 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5334 if (token
->type
== CPP_COLON
5335 && parser
->colon_corrects_to_scope_p
5336 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5338 error_at (token
->location
,
5339 "found %<:%> in nested-name-specifier, expected %<::%>");
5340 token
->type
= CPP_SCOPE
;
5343 if (token
->type
!= CPP_SCOPE
5344 && !cp_parser_nth_token_starts_template_argument_list_p
5349 /* The nested-name-specifier is optional, so we parse
5351 cp_parser_parse_tentatively (parser
);
5353 /* Look for the optional `template' keyword, if this isn't the
5354 first time through the loop. */
5356 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5358 template_keyword_p
= false;
5360 /* Save the old scope since the name lookup we are about to do
5361 might destroy it. */
5362 old_scope
= parser
->scope
;
5363 saved_qualifying_scope
= parser
->qualifying_scope
;
5364 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5365 look up names in "X<T>::I" in order to determine that "Y" is
5366 a template. So, if we have a typename at this point, we make
5367 an effort to look through it. */
5369 && !typename_keyword_p
5371 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5372 parser
->scope
= resolve_typename_type (parser
->scope
,
5373 /*only_current_p=*/false);
5374 /* Parse the qualifying entity. */
5376 = cp_parser_qualifying_entity (parser
,
5382 /* Look for the `::' token. */
5383 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5385 /* If we found what we wanted, we keep going; otherwise, we're
5387 if (!cp_parser_parse_definitely (parser
))
5389 bool error_p
= false;
5391 /* Restore the OLD_SCOPE since it was valid before the
5392 failed attempt at finding the last
5393 class-or-namespace-name. */
5394 parser
->scope
= old_scope
;
5395 parser
->qualifying_scope
= saved_qualifying_scope
;
5397 /* If the next token is a decltype, and the one after that is a
5398 `::', then the decltype has failed to resolve to a class or
5399 enumeration type. Give this error even when parsing
5400 tentatively since it can't possibly be valid--and we're going
5401 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5402 won't get another chance.*/
5403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5404 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5407 token
= cp_lexer_consume_token (parser
->lexer
);
5408 error_at (token
->location
, "decltype evaluates to %qT, "
5409 "which is not a class or enumeration type",
5411 parser
->scope
= error_mark_node
;
5415 cp_lexer_consume_token (parser
->lexer
);
5418 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5420 /* If the next token is an identifier, and the one after
5421 that is a `::', then any valid interpretation would have
5422 found a class-or-namespace-name. */
5423 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5424 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5426 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5429 token
= cp_lexer_consume_token (parser
->lexer
);
5432 if (!token
->error_reported
)
5435 tree ambiguous_decls
;
5437 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5439 /*is_template=*/false,
5440 /*is_namespace=*/false,
5441 /*check_dependency=*/true,
5444 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5445 error_at (token
->location
,
5446 "%qD used without template parameters",
5448 else if (ambiguous_decls
)
5450 // cp_parser_lookup_name has the same diagnostic,
5451 // thus make sure to emit it at most once.
5452 if (cp_parser_uncommitted_to_tentative_parse_p
5455 error_at (token
->location
,
5456 "reference to %qD is ambiguous",
5458 print_candidates (ambiguous_decls
);
5460 decl
= error_mark_node
;
5464 if (cxx_dialect
!= cxx98
)
5465 cp_parser_name_lookup_error
5466 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5469 cp_parser_name_lookup_error
5470 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5474 parser
->scope
= error_mark_node
;
5476 /* Treat this as a successful nested-name-specifier
5481 If the name found is not a class-name (clause
5482 _class_) or namespace-name (_namespace.def_), the
5483 program is ill-formed. */
5486 cp_lexer_consume_token (parser
->lexer
);
5490 /* We've found one valid nested-name-specifier. */
5492 /* Name lookup always gives us a DECL. */
5493 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5494 new_scope
= TREE_TYPE (new_scope
);
5495 /* Uses of "template" must be followed by actual templates. */
5496 if (template_keyword_p
5497 && !(CLASS_TYPE_P (new_scope
)
5498 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5499 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5500 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5501 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5502 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5503 == TEMPLATE_ID_EXPR
)))
5504 permerror (input_location
, TYPE_P (new_scope
)
5505 ? G_("%qT is not a template")
5506 : G_("%qD is not a template"),
5508 /* If it is a class scope, try to complete it; we are about to
5509 be looking up names inside the class. */
5510 if (TYPE_P (new_scope
)
5511 /* Since checking types for dependency can be expensive,
5512 avoid doing it if the type is already complete. */
5513 && !COMPLETE_TYPE_P (new_scope
)
5514 /* Do not try to complete dependent types. */
5515 && !dependent_type_p (new_scope
))
5517 new_scope
= complete_type (new_scope
);
5518 /* If it is a typedef to current class, use the current
5519 class instead, as the typedef won't have any names inside
5521 if (!COMPLETE_TYPE_P (new_scope
)
5522 && currently_open_class (new_scope
))
5523 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5525 /* Make sure we look in the right scope the next time through
5527 parser
->scope
= new_scope
;
5530 /* If parsing tentatively, replace the sequence of tokens that makes
5531 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5532 token. That way, should we re-parse the token stream, we will
5533 not have to repeat the effort required to do the parse, nor will
5534 we issue duplicate error messages. */
5535 if (success
&& start
)
5539 token
= cp_lexer_token_at (parser
->lexer
, start
);
5540 /* Reset the contents of the START token. */
5541 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5542 /* Retrieve any deferred checks. Do not pop this access checks yet
5543 so the memory will not be reclaimed during token replacing below. */
5544 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5545 token
->u
.tree_check_value
->value
= parser
->scope
;
5546 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5547 token
->u
.tree_check_value
->qualifying_scope
=
5548 parser
->qualifying_scope
;
5549 token
->keyword
= RID_MAX
;
5551 /* Purge all subsequent tokens. */
5552 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5556 pop_to_parent_deferring_access_checks ();
5558 return success
? parser
->scope
: NULL_TREE
;
5561 /* Parse a nested-name-specifier. See
5562 cp_parser_nested_name_specifier_opt for details. This function
5563 behaves identically, except that it will an issue an error if no
5564 nested-name-specifier is present. */
5567 cp_parser_nested_name_specifier (cp_parser
*parser
,
5568 bool typename_keyword_p
,
5569 bool check_dependency_p
,
5571 bool is_declaration
)
5575 /* Look for the nested-name-specifier. */
5576 scope
= cp_parser_nested_name_specifier_opt (parser
,
5581 /* If it was not present, issue an error message. */
5584 cp_parser_error (parser
, "expected nested-name-specifier");
5585 parser
->scope
= NULL_TREE
;
5591 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5592 this is either a class-name or a namespace-name (which corresponds
5593 to the class-or-namespace-name production in the grammar). For
5594 C++0x, it can also be a type-name that refers to an enumeration
5595 type or a simple-template-id.
5597 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5598 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5599 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5600 TYPE_P is TRUE iff the next name should be taken as a class-name,
5601 even the same name is declared to be another entity in the same
5604 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5605 specified by the class-or-namespace-name. If neither is found the
5606 ERROR_MARK_NODE is returned. */
5609 cp_parser_qualifying_entity (cp_parser
*parser
,
5610 bool typename_keyword_p
,
5611 bool template_keyword_p
,
5612 bool check_dependency_p
,
5614 bool is_declaration
)
5617 tree saved_qualifying_scope
;
5618 tree saved_object_scope
;
5621 bool successful_parse_p
;
5623 /* DR 743: decltype can appear in a nested-name-specifier. */
5624 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5626 scope
= cp_parser_decltype (parser
);
5627 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5628 && !MAYBE_CLASS_TYPE_P (scope
))
5630 cp_parser_simulate_error (parser
);
5631 return error_mark_node
;
5633 if (TYPE_NAME (scope
))
5634 scope
= TYPE_NAME (scope
);
5638 /* Before we try to parse the class-name, we must save away the
5639 current PARSER->SCOPE since cp_parser_class_name will destroy
5641 saved_scope
= parser
->scope
;
5642 saved_qualifying_scope
= parser
->qualifying_scope
;
5643 saved_object_scope
= parser
->object_scope
;
5644 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5645 there is no need to look for a namespace-name. */
5646 only_class_p
= template_keyword_p
5647 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5649 cp_parser_parse_tentatively (parser
);
5650 scope
= cp_parser_class_name (parser
,
5653 type_p
? class_type
: none_type
,
5655 /*class_head_p=*/false,
5657 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5658 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5660 && cxx_dialect
!= cxx98
5661 && !successful_parse_p
)
5663 /* Restore the saved scope. */
5664 parser
->scope
= saved_scope
;
5665 parser
->qualifying_scope
= saved_qualifying_scope
;
5666 parser
->object_scope
= saved_object_scope
;
5668 /* Parse tentatively. */
5669 cp_parser_parse_tentatively (parser
);
5671 /* Parse a type-name */
5672 scope
= cp_parser_type_name (parser
);
5674 /* "If the name found does not designate a namespace or a class,
5675 enumeration, or dependent type, the program is ill-formed."
5677 We cover classes and dependent types above and namespaces below,
5678 so this code is only looking for enums. */
5679 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5680 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5681 cp_parser_simulate_error (parser
);
5683 successful_parse_p
= cp_parser_parse_definitely (parser
);
5685 /* If that didn't work, try for a namespace-name. */
5686 if (!only_class_p
&& !successful_parse_p
)
5688 /* Restore the saved scope. */
5689 parser
->scope
= saved_scope
;
5690 parser
->qualifying_scope
= saved_qualifying_scope
;
5691 parser
->object_scope
= saved_object_scope
;
5692 /* If we are not looking at an identifier followed by the scope
5693 resolution operator, then this is not part of a
5694 nested-name-specifier. (Note that this function is only used
5695 to parse the components of a nested-name-specifier.) */
5696 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5697 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5698 return error_mark_node
;
5699 scope
= cp_parser_namespace_name (parser
);
5705 /* Return true if we are looking at a compound-literal, false otherwise. */
5708 cp_parser_compound_literal_p (cp_parser
*parser
)
5710 /* Consume the `('. */
5711 cp_lexer_consume_token (parser
->lexer
);
5713 cp_lexer_save_tokens (parser
->lexer
);
5715 /* Skip tokens until the next token is a closing parenthesis.
5716 If we find the closing `)', and the next token is a `{', then
5717 we are looking at a compound-literal. */
5718 bool compound_literal_p
5719 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5720 /*consume_paren=*/true)
5721 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5723 /* Roll back the tokens we skipped. */
5724 cp_lexer_rollback_tokens (parser
->lexer
);
5726 return compound_literal_p
;
5729 /* Parse a postfix-expression.
5733 postfix-expression [ expression ]
5734 postfix-expression ( expression-list [opt] )
5735 simple-type-specifier ( expression-list [opt] )
5736 typename :: [opt] nested-name-specifier identifier
5737 ( expression-list [opt] )
5738 typename :: [opt] nested-name-specifier template [opt] template-id
5739 ( expression-list [opt] )
5740 postfix-expression . template [opt] id-expression
5741 postfix-expression -> template [opt] id-expression
5742 postfix-expression . pseudo-destructor-name
5743 postfix-expression -> pseudo-destructor-name
5744 postfix-expression ++
5745 postfix-expression --
5746 dynamic_cast < type-id > ( expression )
5747 static_cast < type-id > ( expression )
5748 reinterpret_cast < type-id > ( expression )
5749 const_cast < type-id > ( expression )
5750 typeid ( expression )
5756 ( type-id ) { initializer-list , [opt] }
5758 This extension is a GNU version of the C99 compound-literal
5759 construct. (The C99 grammar uses `type-name' instead of `type-id',
5760 but they are essentially the same concept.)
5762 If ADDRESS_P is true, the postfix expression is the operand of the
5763 `&' operator. CAST_P is true if this expression is the target of a
5766 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5767 class member access expressions [expr.ref].
5769 Returns a representation of the expression. */
5772 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5773 bool member_access_only_p
, bool decltype_p
,
5774 cp_id_kind
* pidk_return
)
5779 cp_id_kind idk
= CP_ID_KIND_NONE
;
5780 tree postfix_expression
= NULL_TREE
;
5781 bool is_member_access
= false;
5782 int saved_in_statement
= -1;
5784 /* Peek at the next token. */
5785 token
= cp_lexer_peek_token (parser
->lexer
);
5786 loc
= token
->location
;
5787 /* Some of the productions are determined by keywords. */
5788 keyword
= token
->keyword
;
5798 const char *saved_message
;
5799 bool saved_in_type_id_in_expr_p
;
5801 /* All of these can be handled in the same way from the point
5802 of view of parsing. Begin by consuming the token
5803 identifying the cast. */
5804 cp_lexer_consume_token (parser
->lexer
);
5806 /* New types cannot be defined in the cast. */
5807 saved_message
= parser
->type_definition_forbidden_message
;
5808 parser
->type_definition_forbidden_message
5809 = G_("types may not be defined in casts");
5811 /* Look for the opening `<'. */
5812 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5813 /* Parse the type to which we are casting. */
5814 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5815 parser
->in_type_id_in_expr_p
= true;
5816 type
= cp_parser_type_id (parser
);
5817 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5818 /* Look for the closing `>'. */
5819 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5820 /* Restore the old message. */
5821 parser
->type_definition_forbidden_message
= saved_message
;
5823 bool saved_greater_than_is_operator_p
5824 = parser
->greater_than_is_operator_p
;
5825 parser
->greater_than_is_operator_p
= true;
5827 /* And the expression which is being cast. */
5828 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5829 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5830 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5832 parser
->greater_than_is_operator_p
5833 = saved_greater_than_is_operator_p
;
5835 /* Only type conversions to integral or enumeration types
5836 can be used in constant-expressions. */
5837 if (!cast_valid_in_integral_constant_expression_p (type
)
5838 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5839 return error_mark_node
;
5845 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5849 = build_static_cast (type
, expression
, tf_warning_or_error
);
5853 = build_reinterpret_cast (type
, expression
,
5854 tf_warning_or_error
);
5858 = build_const_cast (type
, expression
, tf_warning_or_error
);
5869 const char *saved_message
;
5870 bool saved_in_type_id_in_expr_p
;
5872 /* Consume the `typeid' token. */
5873 cp_lexer_consume_token (parser
->lexer
);
5874 /* Look for the `(' token. */
5875 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5876 /* Types cannot be defined in a `typeid' expression. */
5877 saved_message
= parser
->type_definition_forbidden_message
;
5878 parser
->type_definition_forbidden_message
5879 = G_("types may not be defined in a %<typeid%> expression");
5880 /* We can't be sure yet whether we're looking at a type-id or an
5882 cp_parser_parse_tentatively (parser
);
5883 /* Try a type-id first. */
5884 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5885 parser
->in_type_id_in_expr_p
= true;
5886 type
= cp_parser_type_id (parser
);
5887 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5888 /* Look for the `)' token. Otherwise, we can't be sure that
5889 we're not looking at an expression: consider `typeid (int
5890 (3))', for example. */
5891 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5892 /* If all went well, simply lookup the type-id. */
5893 if (cp_parser_parse_definitely (parser
))
5894 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5895 /* Otherwise, fall back to the expression variant. */
5900 /* Look for an expression. */
5901 expression
= cp_parser_expression (parser
, & idk
);
5902 /* Compute its typeid. */
5903 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5904 /* Look for the `)' token. */
5905 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5907 /* Restore the saved message. */
5908 parser
->type_definition_forbidden_message
= saved_message
;
5909 /* `typeid' may not appear in an integral constant expression. */
5910 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5911 return error_mark_node
;
5918 /* The syntax permitted here is the same permitted for an
5919 elaborated-type-specifier. */
5920 type
= cp_parser_elaborated_type_specifier (parser
,
5921 /*is_friend=*/false,
5922 /*is_declaration=*/false);
5923 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5927 case RID_CILK_SPAWN
:
5929 cp_lexer_consume_token (parser
->lexer
);
5930 token
= cp_lexer_peek_token (parser
->lexer
);
5931 if (token
->type
== CPP_SEMICOLON
)
5933 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
5935 postfix_expression
= error_mark_node
;
5938 else if (!current_function_decl
)
5940 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
5941 "inside a function");
5942 postfix_expression
= error_mark_node
;
5947 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5948 saved_in_statement
= parser
->in_statement
;
5949 parser
->in_statement
|= IN_CILK_SPAWN
;
5951 cfun
->calls_cilk_spawn
= 1;
5952 postfix_expression
=
5953 cp_parser_postfix_expression (parser
, false, false,
5954 false, false, &idk
);
5957 error_at (token
->location
, "-fcilkplus must be enabled to use"
5958 " %<_Cilk_spawn%>");
5959 cfun
->calls_cilk_spawn
= 0;
5961 else if (saved_in_statement
& IN_CILK_SPAWN
)
5963 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
5964 "are not permitted");
5965 postfix_expression
= error_mark_node
;
5966 cfun
->calls_cilk_spawn
= 0;
5970 postfix_expression
= build_cilk_spawn (token
->location
,
5971 postfix_expression
);
5972 if (postfix_expression
!= error_mark_node
)
5973 SET_EXPR_LOCATION (postfix_expression
, input_location
);
5974 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
5979 case RID_BUILTIN_SHUFFLE
:
5981 vec
<tree
, va_gc
> *vec
;
5985 cp_lexer_consume_token (parser
->lexer
);
5986 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
5987 /*cast_p=*/false, /*allow_expansion_p=*/true,
5988 /*non_constant_p=*/NULL
);
5990 return error_mark_node
;
5992 FOR_EACH_VEC_ELT (*vec
, i
, p
)
5995 if (vec
->length () == 2)
5996 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
5997 tf_warning_or_error
);
5998 else if (vec
->length () == 3)
5999 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6000 tf_warning_or_error
);
6003 error_at (loc
, "wrong number of arguments to "
6004 "%<__builtin_shuffle%>");
6005 return error_mark_node
;
6014 /* If the next thing is a simple-type-specifier, we may be
6015 looking at a functional cast. We could also be looking at
6016 an id-expression. So, we try the functional cast, and if
6017 that doesn't work we fall back to the primary-expression. */
6018 cp_parser_parse_tentatively (parser
);
6019 /* Look for the simple-type-specifier. */
6020 type
= cp_parser_simple_type_specifier (parser
,
6021 /*decl_specs=*/NULL
,
6022 CP_PARSER_FLAGS_NONE
);
6023 /* Parse the cast itself. */
6024 if (!cp_parser_error_occurred (parser
))
6026 = cp_parser_functional_cast (parser
, type
);
6027 /* If that worked, we're done. */
6028 if (cp_parser_parse_definitely (parser
))
6031 /* If the functional-cast didn't work out, try a
6032 compound-literal. */
6033 if (cp_parser_allow_gnu_extensions_p (parser
)
6034 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6036 tree initializer
= NULL_TREE
;
6038 cp_parser_parse_tentatively (parser
);
6040 /* Avoid calling cp_parser_type_id pointlessly, see comment
6041 in cp_parser_cast_expression about c++/29234. */
6042 if (!cp_parser_compound_literal_p (parser
))
6043 cp_parser_simulate_error (parser
);
6046 /* Parse the type. */
6047 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6048 parser
->in_type_id_in_expr_p
= true;
6049 type
= cp_parser_type_id (parser
);
6050 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6051 /* Look for the `)'. */
6052 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6055 /* If things aren't going well, there's no need to
6057 if (!cp_parser_error_occurred (parser
))
6059 bool non_constant_p
;
6060 /* Parse the brace-enclosed initializer list. */
6061 initializer
= cp_parser_braced_list (parser
,
6064 /* If that worked, we're definitely looking at a
6065 compound-literal expression. */
6066 if (cp_parser_parse_definitely (parser
))
6068 /* Warn the user that a compound literal is not
6069 allowed in standard C++. */
6070 pedwarn (input_location
, OPT_Wpedantic
,
6071 "ISO C++ forbids compound-literals");
6072 /* For simplicity, we disallow compound literals in
6073 constant-expressions. We could
6074 allow compound literals of integer type, whose
6075 initializer was a constant, in constant
6076 expressions. Permitting that usage, as a further
6077 extension, would not change the meaning of any
6078 currently accepted programs. (Of course, as
6079 compound literals are not part of ISO C++, the
6080 standard has nothing to say.) */
6081 if (cp_parser_non_integral_constant_expression (parser
,
6084 postfix_expression
= error_mark_node
;
6087 /* Form the representation of the compound-literal. */
6089 = finish_compound_literal (type
, initializer
,
6090 tf_warning_or_error
);
6095 /* It must be a primary-expression. */
6097 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6098 /*template_arg_p=*/false,
6105 /* Note that we don't need to worry about calling build_cplus_new on a
6106 class-valued CALL_EXPR in decltype when it isn't the end of the
6107 postfix-expression; unary_complex_lvalue will take care of that for
6110 /* Keep looping until the postfix-expression is complete. */
6113 if (idk
== CP_ID_KIND_UNQUALIFIED
6114 && identifier_p (postfix_expression
)
6115 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6116 /* It is not a Koenig lookup function call. */
6118 = unqualified_name_lookup_error (postfix_expression
);
6120 /* Peek at the next token. */
6121 token
= cp_lexer_peek_token (parser
->lexer
);
6123 switch (token
->type
)
6125 case CPP_OPEN_SQUARE
:
6126 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6128 cp_parser_error (parser
,
6129 "two consecutive %<[%> shall "
6130 "only introduce an attribute");
6131 return error_mark_node
;
6134 = cp_parser_postfix_open_square_expression (parser
,
6138 idk
= CP_ID_KIND_NONE
;
6139 is_member_access
= false;
6142 case CPP_OPEN_PAREN
:
6143 /* postfix-expression ( expression-list [opt] ) */
6146 bool is_builtin_constant_p
;
6147 bool saved_integral_constant_expression_p
= false;
6148 bool saved_non_integral_constant_expression_p
= false;
6149 tsubst_flags_t complain
= complain_flags (decltype_p
);
6150 vec
<tree
, va_gc
> *args
;
6152 is_member_access
= false;
6154 is_builtin_constant_p
6155 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6156 if (is_builtin_constant_p
)
6158 /* The whole point of __builtin_constant_p is to allow
6159 non-constant expressions to appear as arguments. */
6160 saved_integral_constant_expression_p
6161 = parser
->integral_constant_expression_p
;
6162 saved_non_integral_constant_expression_p
6163 = parser
->non_integral_constant_expression_p
;
6164 parser
->integral_constant_expression_p
= false;
6166 args
= (cp_parser_parenthesized_expression_list
6168 /*cast_p=*/false, /*allow_expansion_p=*/true,
6169 /*non_constant_p=*/NULL
,
6170 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6171 if (is_builtin_constant_p
)
6173 parser
->integral_constant_expression_p
6174 = saved_integral_constant_expression_p
;
6175 parser
->non_integral_constant_expression_p
6176 = saved_non_integral_constant_expression_p
;
6181 postfix_expression
= error_mark_node
;
6185 /* Function calls are not permitted in
6186 constant-expressions. */
6187 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6188 && cp_parser_non_integral_constant_expression (parser
,
6191 postfix_expression
= error_mark_node
;
6192 release_tree_vector (args
);
6197 if (idk
== CP_ID_KIND_UNQUALIFIED
6198 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6200 if (identifier_p (postfix_expression
))
6202 if (!args
->is_empty ())
6205 if (!any_type_dependent_arguments_p (args
))
6207 = perform_koenig_lookup (postfix_expression
, args
,
6212 = unqualified_fn_lookup_error (postfix_expression
);
6214 /* We do not perform argument-dependent lookup if
6215 normal lookup finds a non-function, in accordance
6216 with the expected resolution of DR 218. */
6217 else if (!args
->is_empty ()
6218 && is_overloaded_fn (postfix_expression
))
6220 tree fn
= get_first_fn (postfix_expression
);
6221 fn
= STRIP_TEMPLATE (fn
);
6223 /* Do not do argument dependent lookup if regular
6224 lookup finds a member function or a block-scope
6225 function declaration. [basic.lookup.argdep]/3 */
6226 if (!DECL_FUNCTION_MEMBER_P (fn
)
6227 && !DECL_LOCAL_FUNCTION_P (fn
))
6230 if (!any_type_dependent_arguments_p (args
))
6232 = perform_koenig_lookup (postfix_expression
, args
,
6238 if (warn_memset_transposed_args
)
6240 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6241 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6242 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6243 && vec_safe_length (args
) == 3
6244 && integer_zerop ((*args
)[2])
6245 && LITERAL_ZERO_P ((*args
)[2])
6246 && !(integer_zerop ((*args
)[1])
6247 && LITERAL_ZERO_P ((*args
)[1])))
6248 warning (OPT_Wmemset_transposed_args
,
6249 "%<memset%> used with constant zero length "
6250 "parameter; this could be due to transposed "
6253 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6254 to avoid leaking those into folder and middle-end. */
6257 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6258 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6259 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6262 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6264 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6265 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6267 if (processing_template_decl
6268 && (type_dependent_expression_p (instance
)
6269 || (!BASELINK_P (fn
)
6270 && TREE_CODE (fn
) != FIELD_DECL
)
6271 || type_dependent_expression_p (fn
)
6272 || any_type_dependent_arguments_p (args
)))
6275 = build_nt_call_vec (postfix_expression
, args
);
6276 release_tree_vector (args
);
6280 if (BASELINK_P (fn
))
6283 = (build_new_method_call
6284 (instance
, fn
, &args
, NULL_TREE
,
6285 (idk
== CP_ID_KIND_QUALIFIED
6286 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6293 = finish_call_expr (postfix_expression
, &args
,
6294 /*disallow_virtual=*/false,
6298 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6299 || TREE_CODE (postfix_expression
) == MEMBER_REF
6300 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6301 postfix_expression
= (build_offset_ref_call_from_tree
6302 (postfix_expression
, &args
,
6304 else if (idk
== CP_ID_KIND_QUALIFIED
)
6305 /* A call to a static class member, or a namespace-scope
6308 = finish_call_expr (postfix_expression
, &args
,
6309 /*disallow_virtual=*/true,
6313 /* All other function calls. */
6315 = finish_call_expr (postfix_expression
, &args
,
6316 /*disallow_virtual=*/false,
6320 protected_set_expr_location (postfix_expression
, token
->location
);
6322 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6323 idk
= CP_ID_KIND_NONE
;
6325 release_tree_vector (args
);
6331 /* postfix-expression . template [opt] id-expression
6332 postfix-expression . pseudo-destructor-name
6333 postfix-expression -> template [opt] id-expression
6334 postfix-expression -> pseudo-destructor-name */
6336 /* Consume the `.' or `->' operator. */
6337 cp_lexer_consume_token (parser
->lexer
);
6340 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6344 is_member_access
= true;
6348 /* postfix-expression ++ */
6349 /* Consume the `++' token. */
6350 cp_lexer_consume_token (parser
->lexer
);
6351 /* Generate a representation for the complete expression. */
6353 = finish_increment_expr (postfix_expression
,
6354 POSTINCREMENT_EXPR
);
6355 /* Increments may not appear in constant-expressions. */
6356 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6357 postfix_expression
= error_mark_node
;
6358 idk
= CP_ID_KIND_NONE
;
6359 is_member_access
= false;
6362 case CPP_MINUS_MINUS
:
6363 /* postfix-expression -- */
6364 /* Consume the `--' token. */
6365 cp_lexer_consume_token (parser
->lexer
);
6366 /* Generate a representation for the complete expression. */
6368 = finish_increment_expr (postfix_expression
,
6369 POSTDECREMENT_EXPR
);
6370 /* Decrements may not appear in constant-expressions. */
6371 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6372 postfix_expression
= error_mark_node
;
6373 idk
= CP_ID_KIND_NONE
;
6374 is_member_access
= false;
6378 if (pidk_return
!= NULL
)
6379 * pidk_return
= idk
;
6380 if (member_access_only_p
)
6381 return is_member_access
? postfix_expression
: error_mark_node
;
6383 return postfix_expression
;
6387 /* We should never get here. */
6389 return error_mark_node
;
6392 /* This function parses Cilk Plus array notations. If a normal array expr. is
6393 parsed then the array index is passed back to the caller through *INIT_INDEX
6394 and the function returns a NULL_TREE. If array notation expr. is parsed,
6395 then *INIT_INDEX is ignored by the caller and the function returns
6396 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6400 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6403 cp_token
*token
= NULL
;
6404 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6405 if (!array_value
|| array_value
== error_mark_node
)
6407 cp_parser_skip_to_end_of_statement (parser
);
6408 return error_mark_node
;
6411 array_type
= TREE_TYPE (array_value
);
6413 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6414 parser
->colon_corrects_to_scope_p
= false;
6415 token
= cp_lexer_peek_token (parser
->lexer
);
6419 cp_parser_error (parser
, "expected %<:%> or numeral");
6420 return error_mark_node
;
6422 else if (token
->type
== CPP_COLON
)
6424 /* Consume the ':'. */
6425 cp_lexer_consume_token (parser
->lexer
);
6427 /* If we are here, then we have a case like this A[:]. */
6428 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6430 cp_parser_error (parser
, "expected %<]%>");
6431 cp_parser_skip_to_end_of_statement (parser
);
6432 return error_mark_node
;
6434 *init_index
= NULL_TREE
;
6436 length_index
= NULL_TREE
;
6440 /* If we are here, then there are three valid possibilities:
6442 2. ARRAY [ EXP : EXP ]
6443 3. ARRAY [ EXP : EXP : EXP ] */
6445 *init_index
= cp_parser_expression (parser
);
6446 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6448 /* This indicates that we have a normal array expression. */
6449 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6453 /* Consume the ':'. */
6454 cp_lexer_consume_token (parser
->lexer
);
6455 length_index
= cp_parser_expression (parser
);
6456 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6458 cp_lexer_consume_token (parser
->lexer
);
6459 stride
= cp_parser_expression (parser
);
6462 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6464 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6465 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6467 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6468 cp_lexer_consume_token (parser
->lexer
);
6469 return error_mark_node
;
6471 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6473 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6474 length_index
, stride
, array_type
);
6478 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6479 by cp_parser_builtin_offsetof. We're looking for
6481 postfix-expression [ expression ]
6482 postfix-expression [ braced-init-list ] (C++11)
6484 FOR_OFFSETOF is set if we're being called in that context, which
6485 changes how we deal with integer constant expressions. */
6488 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6489 tree postfix_expression
,
6493 tree index
= NULL_TREE
;
6494 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6495 bool saved_greater_than_is_operator_p
;
6497 /* Consume the `[' token. */
6498 cp_lexer_consume_token (parser
->lexer
);
6500 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6501 parser
->greater_than_is_operator_p
= true;
6503 /* Parse the index expression. */
6504 /* ??? For offsetof, there is a question of what to allow here. If
6505 offsetof is not being used in an integral constant expression context,
6506 then we *could* get the right answer by computing the value at runtime.
6507 If we are in an integral constant expression context, then we might
6508 could accept any constant expression; hard to say without analysis.
6509 Rather than open the barn door too wide right away, allow only integer
6510 constant expressions here. */
6512 index
= cp_parser_constant_expression (parser
);
6515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6517 bool expr_nonconst_p
;
6518 cp_lexer_set_source_position (parser
->lexer
);
6519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6520 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6522 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6524 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6525 "braced list index is not allowed with array "
6527 cp_parser_skip_to_end_of_statement (parser
);
6528 return error_mark_node
;
6531 else if (flag_cilkplus
)
6533 /* Here are have these two options:
6534 ARRAY[EXP : EXP] - Array notation expr with default
6536 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6538 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6539 postfix_expression
);
6544 index
= cp_parser_expression (parser
);
6547 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6549 /* Look for the closing `]'. */
6550 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6552 /* Build the ARRAY_REF. */
6553 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6556 /* When not doing offsetof, array references are not permitted in
6557 constant-expressions. */
6559 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6560 postfix_expression
= error_mark_node
;
6562 return postfix_expression
;
6565 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6566 by cp_parser_builtin_offsetof. We're looking for
6568 postfix-expression . template [opt] id-expression
6569 postfix-expression . pseudo-destructor-name
6570 postfix-expression -> template [opt] id-expression
6571 postfix-expression -> pseudo-destructor-name
6573 FOR_OFFSETOF is set if we're being called in that context. That sorta
6574 limits what of the above we'll actually accept, but nevermind.
6575 TOKEN_TYPE is the "." or "->" token, which will already have been
6576 removed from the stream. */
6579 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6580 enum cpp_ttype token_type
,
6581 tree postfix_expression
,
6582 bool for_offsetof
, cp_id_kind
*idk
,
6583 location_t location
)
6587 bool pseudo_destructor_p
;
6588 tree scope
= NULL_TREE
;
6590 /* If this is a `->' operator, dereference the pointer. */
6591 if (token_type
== CPP_DEREF
)
6592 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6593 tf_warning_or_error
);
6594 /* Check to see whether or not the expression is type-dependent. */
6595 dependent_p
= type_dependent_expression_p (postfix_expression
);
6596 /* The identifier following the `->' or `.' is not qualified. */
6597 parser
->scope
= NULL_TREE
;
6598 parser
->qualifying_scope
= NULL_TREE
;
6599 parser
->object_scope
= NULL_TREE
;
6600 *idk
= CP_ID_KIND_NONE
;
6602 /* Enter the scope corresponding to the type of the object
6603 given by the POSTFIX_EXPRESSION. */
6604 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6606 scope
= TREE_TYPE (postfix_expression
);
6607 /* According to the standard, no expression should ever have
6608 reference type. Unfortunately, we do not currently match
6609 the standard in this respect in that our internal representation
6610 of an expression may have reference type even when the standard
6611 says it does not. Therefore, we have to manually obtain the
6612 underlying type here. */
6613 scope
= non_reference (scope
);
6614 /* The type of the POSTFIX_EXPRESSION must be complete. */
6615 if (scope
== unknown_type_node
)
6617 error_at (location
, "%qE does not have class type",
6618 postfix_expression
);
6621 /* Unlike the object expression in other contexts, *this is not
6622 required to be of complete type for purposes of class member
6623 access (5.2.5) outside the member function body. */
6624 else if (postfix_expression
!= current_class_ref
6625 && !(processing_template_decl
&& scope
== current_class_type
))
6626 scope
= complete_type_or_else (scope
, NULL_TREE
);
6627 /* Let the name lookup machinery know that we are processing a
6628 class member access expression. */
6629 parser
->context
->object_type
= scope
;
6630 /* If something went wrong, we want to be able to discern that case,
6631 as opposed to the case where there was no SCOPE due to the type
6632 of expression being dependent. */
6634 scope
= error_mark_node
;
6635 /* If the SCOPE was erroneous, make the various semantic analysis
6636 functions exit quickly -- and without issuing additional error
6638 if (scope
== error_mark_node
)
6639 postfix_expression
= error_mark_node
;
6642 /* Assume this expression is not a pseudo-destructor access. */
6643 pseudo_destructor_p
= false;
6645 /* If the SCOPE is a scalar type, then, if this is a valid program,
6646 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6647 is type dependent, it can be pseudo-destructor-name or something else.
6648 Try to parse it as pseudo-destructor-name first. */
6649 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6654 cp_parser_parse_tentatively (parser
);
6655 /* Parse the pseudo-destructor-name. */
6657 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6660 && (cp_parser_error_occurred (parser
)
6661 || !SCALAR_TYPE_P (type
)))
6662 cp_parser_abort_tentative_parse (parser
);
6663 else if (cp_parser_parse_definitely (parser
))
6665 pseudo_destructor_p
= true;
6667 = finish_pseudo_destructor_expr (postfix_expression
,
6672 if (!pseudo_destructor_p
)
6674 /* If the SCOPE is not a scalar type, we are looking at an
6675 ordinary class member access expression, rather than a
6676 pseudo-destructor-name. */
6678 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6679 /* Parse the id-expression. */
6680 name
= (cp_parser_id_expression
6682 cp_parser_optional_template_keyword (parser
),
6683 /*check_dependency_p=*/true,
6685 /*declarator_p=*/false,
6686 /*optional_p=*/false));
6687 /* In general, build a SCOPE_REF if the member name is qualified.
6688 However, if the name was not dependent and has already been
6689 resolved; there is no need to build the SCOPE_REF. For example;
6691 struct X { void f(); };
6692 template <typename T> void f(T* t) { t->X::f(); }
6694 Even though "t" is dependent, "X::f" is not and has been resolved
6695 to a BASELINK; there is no need to include scope information. */
6697 /* But we do need to remember that there was an explicit scope for
6698 virtual function calls. */
6700 *idk
= CP_ID_KIND_QUALIFIED
;
6702 /* If the name is a template-id that names a type, we will get a
6703 TYPE_DECL here. That is invalid code. */
6704 if (TREE_CODE (name
) == TYPE_DECL
)
6706 error_at (token
->location
, "invalid use of %qD", name
);
6707 postfix_expression
= error_mark_node
;
6711 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6713 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6715 error_at (token
->location
, "%<%D::%D%> is not a class member",
6716 parser
->scope
, name
);
6717 postfix_expression
= error_mark_node
;
6720 name
= build_qualified_name (/*type=*/NULL_TREE
,
6724 parser
->scope
= NULL_TREE
;
6725 parser
->qualifying_scope
= NULL_TREE
;
6726 parser
->object_scope
= NULL_TREE
;
6728 if (parser
->scope
&& name
&& BASELINK_P (name
))
6729 adjust_result_of_qualified_name_lookup
6730 (name
, parser
->scope
, scope
);
6732 = finish_class_member_access_expr (postfix_expression
, name
,
6734 tf_warning_or_error
);
6738 /* We no longer need to look up names in the scope of the object on
6739 the left-hand side of the `.' or `->' operator. */
6740 parser
->context
->object_type
= NULL_TREE
;
6742 /* Outside of offsetof, these operators may not appear in
6743 constant-expressions. */
6745 && (cp_parser_non_integral_constant_expression
6746 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6747 postfix_expression
= error_mark_node
;
6749 return postfix_expression
;
6752 /* Cache of LITERAL_ZERO_P constants. */
6754 static GTY(()) tree literal_zeros
[itk_none
];
6756 /* Parse a parenthesized expression-list.
6759 assignment-expression
6760 expression-list, assignment-expression
6765 identifier, expression-list
6767 CAST_P is true if this expression is the target of a cast.
6769 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6772 Returns a vector of trees. Each element is a representation of an
6773 assignment-expression. NULL is returned if the ( and or ) are
6774 missing. An empty, but allocated, vector is returned on no
6775 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6776 if we are parsing an attribute list for an attribute that wants a
6777 plain identifier argument, normal_attr for an attribute that wants
6778 an expression, or non_attr if we aren't parsing an attribute list. If
6779 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6780 not all of the expressions in the list were constant.
6781 WANT_LITERAL_ZERO_P is true if the caller is interested in
6782 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6783 immediately, this can be removed. */
6785 static vec
<tree
, va_gc
> *
6786 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6787 int is_attribute_list
,
6789 bool allow_expansion_p
,
6790 bool *non_constant_p
,
6791 bool want_literal_zero_p
)
6793 vec
<tree
, va_gc
> *expression_list
;
6794 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6795 tree identifier
= NULL_TREE
;
6796 bool saved_greater_than_is_operator_p
;
6798 /* Assume all the expressions will be constant. */
6800 *non_constant_p
= false;
6802 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6805 expression_list
= make_tree_vector ();
6807 /* Within a parenthesized expression, a `>' token is always
6808 the greater-than operator. */
6809 saved_greater_than_is_operator_p
6810 = parser
->greater_than_is_operator_p
;
6811 parser
->greater_than_is_operator_p
= true;
6813 /* Consume expressions until there are no more. */
6814 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6819 /* At the beginning of attribute lists, check to see if the
6820 next token is an identifier. */
6821 if (is_attribute_list
== id_attr
6822 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6826 /* Consume the identifier. */
6827 token
= cp_lexer_consume_token (parser
->lexer
);
6828 /* Save the identifier. */
6829 identifier
= token
->u
.value
;
6833 bool expr_non_constant_p
;
6835 /* Parse the next assignment-expression. */
6836 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6838 /* A braced-init-list. */
6839 cp_lexer_set_source_position (parser
->lexer
);
6840 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6841 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6842 if (non_constant_p
&& expr_non_constant_p
)
6843 *non_constant_p
= true;
6845 else if (non_constant_p
)
6847 expr
= (cp_parser_constant_expression
6848 (parser
, /*allow_non_constant_p=*/true,
6849 &expr_non_constant_p
));
6850 if (expr_non_constant_p
)
6851 *non_constant_p
= true;
6856 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6864 /* If a parameter is literal zero alone, remember it
6865 for -Wmemset-transposed-args warning. */
6866 if (integer_zerop (tok
->u
.value
)
6867 && !TREE_OVERFLOW (tok
->u
.value
)
6868 && want_literal_zero_p
6869 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6871 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6872 == CPP_CLOSE_PAREN
))
6875 for (i
= 0; i
< itk_none
; ++i
)
6876 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6878 if (i
< itk_none
&& literal_zeros
[i
])
6879 expr
= literal_zeros
[i
];
6882 expr
= copy_node (tok
->u
.value
);
6883 LITERAL_ZERO_P (expr
) = 1;
6885 literal_zeros
[i
] = expr
;
6887 /* Consume the 0 token (or '\0', 0LL etc.). */
6888 cp_lexer_consume_token (parser
->lexer
);
6894 if (expr
== NULL_TREE
)
6895 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6900 expr
= fold_non_dependent_expr (expr
);
6902 /* If we have an ellipsis, then this is an expression
6904 if (allow_expansion_p
6905 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6907 /* Consume the `...'. */
6908 cp_lexer_consume_token (parser
->lexer
);
6910 /* Build the argument pack. */
6911 expr
= make_pack_expansion (expr
);
6914 /* Add it to the list. We add error_mark_node
6915 expressions to the list, so that we can still tell if
6916 the correct form for a parenthesized expression-list
6917 is found. That gives better errors. */
6918 vec_safe_push (expression_list
, expr
);
6920 if (expr
== error_mark_node
)
6924 /* After the first item, attribute lists look the same as
6925 expression lists. */
6926 is_attribute_list
= non_attr
;
6929 /* If the next token isn't a `,', then we are done. */
6930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6933 /* Otherwise, consume the `,' and keep going. */
6934 cp_lexer_consume_token (parser
->lexer
);
6937 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
6942 /* We try and resync to an unnested comma, as that will give the
6943 user better diagnostics. */
6944 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
6945 /*recovering=*/true,
6947 /*consume_paren=*/true);
6952 parser
->greater_than_is_operator_p
6953 = saved_greater_than_is_operator_p
;
6958 parser
->greater_than_is_operator_p
6959 = saved_greater_than_is_operator_p
;
6962 vec_safe_insert (expression_list
, 0, identifier
);
6964 return expression_list
;
6967 /* Parse a pseudo-destructor-name.
6969 pseudo-destructor-name:
6970 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6971 :: [opt] nested-name-specifier template template-id :: ~ type-name
6972 :: [opt] nested-name-specifier [opt] ~ type-name
6974 If either of the first two productions is used, sets *SCOPE to the
6975 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6976 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6977 or ERROR_MARK_NODE if the parse fails. */
6980 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
6985 bool nested_name_specifier_p
;
6988 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
6989 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
6990 && !type_dependent_expression_p (object
))
6992 if (cxx_dialect
< cxx14
)
6993 pedwarn (input_location
, 0,
6994 "%<~auto%> only available with "
6995 "-std=c++14 or -std=gnu++14");
6996 cp_lexer_consume_token (parser
->lexer
);
6997 cp_lexer_consume_token (parser
->lexer
);
6999 *type
= TREE_TYPE (object
);
7003 /* Assume that things will not work out. */
7004 *type
= error_mark_node
;
7006 /* Look for the optional `::' operator. */
7007 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7008 /* Look for the optional nested-name-specifier. */
7009 nested_name_specifier_p
7010 = (cp_parser_nested_name_specifier_opt (parser
,
7011 /*typename_keyword_p=*/false,
7012 /*check_dependency_p=*/true,
7014 /*is_declaration=*/false)
7016 /* Now, if we saw a nested-name-specifier, we might be doing the
7017 second production. */
7018 if (nested_name_specifier_p
7019 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7021 /* Consume the `template' keyword. */
7022 cp_lexer_consume_token (parser
->lexer
);
7023 /* Parse the template-id. */
7024 cp_parser_template_id (parser
,
7025 /*template_keyword_p=*/true,
7026 /*check_dependency_p=*/false,
7028 /*is_declaration=*/true);
7029 /* Look for the `::' token. */
7030 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7032 /* If the next token is not a `~', then there might be some
7033 additional qualification. */
7034 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7036 /* At this point, we're looking for "type-name :: ~". The type-name
7037 must not be a class-name, since this is a pseudo-destructor. So,
7038 it must be either an enum-name, or a typedef-name -- both of which
7039 are just identifiers. So, we peek ahead to check that the "::"
7040 and "~" tokens are present; if they are not, then we can avoid
7041 calling type_name. */
7042 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7043 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7044 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7046 cp_parser_error (parser
, "non-scalar type");
7050 /* Look for the type-name. */
7051 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7052 if (*scope
== error_mark_node
)
7055 /* Look for the `::' token. */
7056 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7061 /* Look for the `~'. */
7062 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7064 /* Once we see the ~, this has to be a pseudo-destructor. */
7065 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7066 cp_parser_commit_to_topmost_tentative_parse (parser
);
7068 /* Look for the type-name again. We are not responsible for
7069 checking that it matches the first type-name. */
7070 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7073 /* Parse a unary-expression.
7079 unary-operator cast-expression
7080 sizeof unary-expression
7082 alignof ( type-id ) [C++0x]
7089 __extension__ cast-expression
7090 __alignof__ unary-expression
7091 __alignof__ ( type-id )
7092 alignof unary-expression [C++0x]
7093 __real__ cast-expression
7094 __imag__ cast-expression
7096 sizeof ( type-id ) { initializer-list , [opt] }
7097 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7098 __alignof__ ( type-id ) { initializer-list , [opt] }
7100 ADDRESS_P is true iff the unary-expression is appearing as the
7101 operand of the `&' operator. CAST_P is true if this expression is
7102 the target of a cast.
7104 Returns a representation of the expression. */
7107 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7108 bool address_p
, bool cast_p
, bool decltype_p
)
7111 enum tree_code unary_operator
;
7113 /* Peek at the next token. */
7114 token
= cp_lexer_peek_token (parser
->lexer
);
7115 /* Some keywords give away the kind of expression. */
7116 if (token
->type
== CPP_KEYWORD
)
7118 enum rid keyword
= token
->keyword
;
7127 location_t first_loc
;
7129 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7130 /* Consume the token. */
7131 cp_lexer_consume_token (parser
->lexer
);
7132 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7133 /* Parse the operand. */
7134 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7136 if (TYPE_P (operand
))
7137 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7140 /* ISO C++ defines alignof only with types, not with
7141 expressions. So pedwarn if alignof is used with a non-
7142 type expression. However, __alignof__ is ok. */
7143 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7144 pedwarn (token
->location
, OPT_Wpedantic
,
7145 "ISO C++ does not allow %<alignof%> "
7148 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7150 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7151 SIZEOF_EXPR with the original operand. */
7152 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7154 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7156 if (!processing_template_decl
&& TYPE_P (operand
))
7158 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7159 build1 (NOP_EXPR
, operand
,
7161 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7164 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7165 TREE_SIDE_EFFECTS (ret
) = 0;
7166 TREE_READONLY (ret
) = 1;
7168 SET_EXPR_LOCATION (ret
, first_loc
);
7174 return cp_parser_new_expression (parser
);
7177 return cp_parser_delete_expression (parser
);
7181 /* The saved value of the PEDANTIC flag. */
7185 /* Save away the PEDANTIC flag. */
7186 cp_parser_extension_opt (parser
, &saved_pedantic
);
7187 /* Parse the cast-expression. */
7188 expr
= cp_parser_simple_cast_expression (parser
);
7189 /* Restore the PEDANTIC flag. */
7190 pedantic
= saved_pedantic
;
7200 /* Consume the `__real__' or `__imag__' token. */
7201 cp_lexer_consume_token (parser
->lexer
);
7202 /* Parse the cast-expression. */
7203 expression
= cp_parser_simple_cast_expression (parser
);
7204 /* Create the complete representation. */
7205 return build_x_unary_op (token
->location
,
7206 (keyword
== RID_REALPART
7207 ? REALPART_EXPR
: IMAGPART_EXPR
),
7209 tf_warning_or_error
);
7213 case RID_TRANSACTION_ATOMIC
:
7214 case RID_TRANSACTION_RELAXED
:
7215 return cp_parser_transaction_expression (parser
, keyword
);
7220 const char *saved_message
;
7221 bool saved_integral_constant_expression_p
;
7222 bool saved_non_integral_constant_expression_p
;
7223 bool saved_greater_than_is_operator_p
;
7225 cp_lexer_consume_token (parser
->lexer
);
7226 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7228 saved_message
= parser
->type_definition_forbidden_message
;
7229 parser
->type_definition_forbidden_message
7230 = G_("types may not be defined in %<noexcept%> expressions");
7232 saved_integral_constant_expression_p
7233 = parser
->integral_constant_expression_p
;
7234 saved_non_integral_constant_expression_p
7235 = parser
->non_integral_constant_expression_p
;
7236 parser
->integral_constant_expression_p
= false;
7238 saved_greater_than_is_operator_p
7239 = parser
->greater_than_is_operator_p
;
7240 parser
->greater_than_is_operator_p
= true;
7242 ++cp_unevaluated_operand
;
7243 ++c_inhibit_evaluation_warnings
;
7244 ++cp_noexcept_operand
;
7245 expr
= cp_parser_expression (parser
);
7246 --cp_noexcept_operand
;
7247 --c_inhibit_evaluation_warnings
;
7248 --cp_unevaluated_operand
;
7250 parser
->greater_than_is_operator_p
7251 = saved_greater_than_is_operator_p
;
7253 parser
->integral_constant_expression_p
7254 = saved_integral_constant_expression_p
;
7255 parser
->non_integral_constant_expression_p
7256 = saved_non_integral_constant_expression_p
;
7258 parser
->type_definition_forbidden_message
= saved_message
;
7260 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7261 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7269 /* Look for the `:: new' and `:: delete', which also signal the
7270 beginning of a new-expression, or delete-expression,
7271 respectively. If the next token is `::', then it might be one of
7273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7277 /* See if the token after the `::' is one of the keywords in
7278 which we're interested. */
7279 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7280 /* If it's `new', we have a new-expression. */
7281 if (keyword
== RID_NEW
)
7282 return cp_parser_new_expression (parser
);
7283 /* Similarly, for `delete'. */
7284 else if (keyword
== RID_DELETE
)
7285 return cp_parser_delete_expression (parser
);
7288 /* Look for a unary operator. */
7289 unary_operator
= cp_parser_unary_operator (token
);
7290 /* The `++' and `--' operators can be handled similarly, even though
7291 they are not technically unary-operators in the grammar. */
7292 if (unary_operator
== ERROR_MARK
)
7294 if (token
->type
== CPP_PLUS_PLUS
)
7295 unary_operator
= PREINCREMENT_EXPR
;
7296 else if (token
->type
== CPP_MINUS_MINUS
)
7297 unary_operator
= PREDECREMENT_EXPR
;
7298 /* Handle the GNU address-of-label extension. */
7299 else if (cp_parser_allow_gnu_extensions_p (parser
)
7300 && token
->type
== CPP_AND_AND
)
7304 location_t loc
= token
->location
;
7306 /* Consume the '&&' token. */
7307 cp_lexer_consume_token (parser
->lexer
);
7308 /* Look for the identifier. */
7309 identifier
= cp_parser_identifier (parser
);
7310 /* Create an expression representing the address. */
7311 expression
= finish_label_address_expr (identifier
, loc
);
7312 if (cp_parser_non_integral_constant_expression (parser
,
7314 expression
= error_mark_node
;
7318 if (unary_operator
!= ERROR_MARK
)
7320 tree cast_expression
;
7321 tree expression
= error_mark_node
;
7322 non_integral_constant non_constant_p
= NIC_NONE
;
7323 location_t loc
= token
->location
;
7324 tsubst_flags_t complain
= complain_flags (decltype_p
);
7326 /* Consume the operator token. */
7327 token
= cp_lexer_consume_token (parser
->lexer
);
7328 /* Parse the cast-expression. */
7330 = cp_parser_cast_expression (parser
,
7331 unary_operator
== ADDR_EXPR
,
7335 /* Now, build an appropriate representation. */
7336 switch (unary_operator
)
7339 non_constant_p
= NIC_STAR
;
7340 expression
= build_x_indirect_ref (loc
, cast_expression
,
7346 non_constant_p
= NIC_ADDR
;
7349 expression
= build_x_unary_op (loc
, unary_operator
,
7354 case PREINCREMENT_EXPR
:
7355 case PREDECREMENT_EXPR
:
7356 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7357 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7359 case UNARY_PLUS_EXPR
:
7361 case TRUTH_NOT_EXPR
:
7362 expression
= finish_unary_op_expr (loc
, unary_operator
,
7363 cast_expression
, complain
);
7370 if (non_constant_p
!= NIC_NONE
7371 && cp_parser_non_integral_constant_expression (parser
,
7373 expression
= error_mark_node
;
7378 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7379 /*member_access_only_p=*/false,
7384 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7385 unary-operator, the corresponding tree code is returned. */
7387 static enum tree_code
7388 cp_parser_unary_operator (cp_token
* token
)
7390 switch (token
->type
)
7393 return INDIRECT_REF
;
7399 return UNARY_PLUS_EXPR
;
7405 return TRUTH_NOT_EXPR
;
7408 return BIT_NOT_EXPR
;
7415 /* Parse a new-expression.
7418 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7419 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7421 Returns a representation of the expression. */
7424 cp_parser_new_expression (cp_parser
* parser
)
7426 bool global_scope_p
;
7427 vec
<tree
, va_gc
> *placement
;
7429 vec
<tree
, va_gc
> *initializer
;
7430 tree nelts
= NULL_TREE
;
7433 /* Look for the optional `::' operator. */
7435 = (cp_parser_global_scope_opt (parser
,
7436 /*current_scope_valid_p=*/false)
7438 /* Look for the `new' operator. */
7439 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7440 /* There's no easy way to tell a new-placement from the
7441 `( type-id )' construct. */
7442 cp_parser_parse_tentatively (parser
);
7443 /* Look for a new-placement. */
7444 placement
= cp_parser_new_placement (parser
);
7445 /* If that didn't work out, there's no new-placement. */
7446 if (!cp_parser_parse_definitely (parser
))
7448 if (placement
!= NULL
)
7449 release_tree_vector (placement
);
7453 /* If the next token is a `(', then we have a parenthesized
7455 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7458 const char *saved_message
= parser
->type_definition_forbidden_message
;
7460 /* Consume the `('. */
7461 cp_lexer_consume_token (parser
->lexer
);
7463 /* Parse the type-id. */
7464 parser
->type_definition_forbidden_message
7465 = G_("types may not be defined in a new-expression");
7466 type
= cp_parser_type_id (parser
);
7467 parser
->type_definition_forbidden_message
= saved_message
;
7469 /* Look for the closing `)'. */
7470 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7471 token
= cp_lexer_peek_token (parser
->lexer
);
7472 /* There should not be a direct-new-declarator in this production,
7473 but GCC used to allowed this, so we check and emit a sensible error
7474 message for this case. */
7475 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7477 error_at (token
->location
,
7478 "array bound forbidden after parenthesized type-id");
7479 inform (token
->location
,
7480 "try removing the parentheses around the type-id");
7481 cp_parser_direct_new_declarator (parser
);
7484 /* Otherwise, there must be a new-type-id. */
7486 type
= cp_parser_new_type_id (parser
, &nelts
);
7488 /* If the next token is a `(' or '{', then we have a new-initializer. */
7489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7490 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7491 initializer
= cp_parser_new_initializer (parser
);
7495 /* A new-expression may not appear in an integral constant
7497 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7498 ret
= error_mark_node
;
7501 /* Create a representation of the new-expression. */
7502 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7503 tf_warning_or_error
);
7506 if (placement
!= NULL
)
7507 release_tree_vector (placement
);
7508 if (initializer
!= NULL
)
7509 release_tree_vector (initializer
);
7514 /* Parse a new-placement.
7519 Returns the same representation as for an expression-list. */
7521 static vec
<tree
, va_gc
> *
7522 cp_parser_new_placement (cp_parser
* parser
)
7524 vec
<tree
, va_gc
> *expression_list
;
7526 /* Parse the expression-list. */
7527 expression_list
= (cp_parser_parenthesized_expression_list
7528 (parser
, non_attr
, /*cast_p=*/false,
7529 /*allow_expansion_p=*/true,
7530 /*non_constant_p=*/NULL
));
7532 return expression_list
;
7535 /* Parse a new-type-id.
7538 type-specifier-seq new-declarator [opt]
7540 Returns the TYPE allocated. If the new-type-id indicates an array
7541 type, *NELTS is set to the number of elements in the last array
7542 bound; the TYPE will not include the last array bound. */
7545 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7547 cp_decl_specifier_seq type_specifier_seq
;
7548 cp_declarator
*new_declarator
;
7549 cp_declarator
*declarator
;
7550 cp_declarator
*outer_declarator
;
7551 const char *saved_message
;
7553 /* The type-specifier sequence must not contain type definitions.
7554 (It cannot contain declarations of new types either, but if they
7555 are not definitions we will catch that because they are not
7557 saved_message
= parser
->type_definition_forbidden_message
;
7558 parser
->type_definition_forbidden_message
7559 = G_("types may not be defined in a new-type-id");
7560 /* Parse the type-specifier-seq. */
7561 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7562 /*is_trailing_return=*/false,
7563 &type_specifier_seq
);
7564 /* Restore the old message. */
7565 parser
->type_definition_forbidden_message
= saved_message
;
7567 if (type_specifier_seq
.type
== error_mark_node
)
7568 return error_mark_node
;
7570 /* Parse the new-declarator. */
7571 new_declarator
= cp_parser_new_declarator_opt (parser
);
7573 /* Determine the number of elements in the last array dimension, if
7576 /* Skip down to the last array dimension. */
7577 declarator
= new_declarator
;
7578 outer_declarator
= NULL
;
7579 while (declarator
&& (declarator
->kind
== cdk_pointer
7580 || declarator
->kind
== cdk_ptrmem
))
7582 outer_declarator
= declarator
;
7583 declarator
= declarator
->declarator
;
7586 && declarator
->kind
== cdk_array
7587 && declarator
->declarator
7588 && declarator
->declarator
->kind
== cdk_array
)
7590 outer_declarator
= declarator
;
7591 declarator
= declarator
->declarator
;
7594 if (declarator
&& declarator
->kind
== cdk_array
)
7596 *nelts
= declarator
->u
.array
.bounds
;
7597 if (*nelts
== error_mark_node
)
7598 *nelts
= integer_one_node
;
7600 if (outer_declarator
)
7601 outer_declarator
->declarator
= declarator
->declarator
;
7603 new_declarator
= NULL
;
7606 return groktypename (&type_specifier_seq
, new_declarator
, false);
7609 /* Parse an (optional) new-declarator.
7612 ptr-operator new-declarator [opt]
7613 direct-new-declarator
7615 Returns the declarator. */
7617 static cp_declarator
*
7618 cp_parser_new_declarator_opt (cp_parser
* parser
)
7620 enum tree_code code
;
7621 tree type
, std_attributes
= NULL_TREE
;
7622 cp_cv_quals cv_quals
;
7624 /* We don't know if there's a ptr-operator next, or not. */
7625 cp_parser_parse_tentatively (parser
);
7626 /* Look for a ptr-operator. */
7627 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7628 /* If that worked, look for more new-declarators. */
7629 if (cp_parser_parse_definitely (parser
))
7631 cp_declarator
*declarator
;
7633 /* Parse another optional declarator. */
7634 declarator
= cp_parser_new_declarator_opt (parser
);
7636 declarator
= cp_parser_make_indirect_declarator
7637 (code
, type
, cv_quals
, declarator
, std_attributes
);
7642 /* If the next token is a `[', there is a direct-new-declarator. */
7643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7644 return cp_parser_direct_new_declarator (parser
);
7649 /* Parse a direct-new-declarator.
7651 direct-new-declarator:
7653 direct-new-declarator [constant-expression]
7657 static cp_declarator
*
7658 cp_parser_direct_new_declarator (cp_parser
* parser
)
7660 cp_declarator
*declarator
= NULL
;
7667 /* Look for the opening `['. */
7668 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7670 token
= cp_lexer_peek_token (parser
->lexer
);
7671 expression
= cp_parser_expression (parser
);
7672 /* The standard requires that the expression have integral
7673 type. DR 74 adds enumeration types. We believe that the
7674 real intent is that these expressions be handled like the
7675 expression in a `switch' condition, which also allows
7676 classes with a single conversion to integral or
7677 enumeration type. */
7678 if (!processing_template_decl
)
7681 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7686 error_at (token
->location
,
7687 "expression in new-declarator must have integral "
7688 "or enumeration type");
7689 expression
= error_mark_node
;
7693 /* Look for the closing `]'. */
7694 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7696 /* Add this bound to the declarator. */
7697 declarator
= make_array_declarator (declarator
, expression
);
7699 /* If the next token is not a `[', then there are no more
7701 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7708 /* Parse a new-initializer.
7711 ( expression-list [opt] )
7714 Returns a representation of the expression-list. */
7716 static vec
<tree
, va_gc
> *
7717 cp_parser_new_initializer (cp_parser
* parser
)
7719 vec
<tree
, va_gc
> *expression_list
;
7721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7724 bool expr_non_constant_p
;
7725 cp_lexer_set_source_position (parser
->lexer
);
7726 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7727 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7728 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7729 expression_list
= make_tree_vector_single (t
);
7732 expression_list
= (cp_parser_parenthesized_expression_list
7733 (parser
, non_attr
, /*cast_p=*/false,
7734 /*allow_expansion_p=*/true,
7735 /*non_constant_p=*/NULL
));
7737 return expression_list
;
7740 /* Parse a delete-expression.
7743 :: [opt] delete cast-expression
7744 :: [opt] delete [ ] cast-expression
7746 Returns a representation of the expression. */
7749 cp_parser_delete_expression (cp_parser
* parser
)
7751 bool global_scope_p
;
7755 /* Look for the optional `::' operator. */
7757 = (cp_parser_global_scope_opt (parser
,
7758 /*current_scope_valid_p=*/false)
7760 /* Look for the `delete' keyword. */
7761 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7762 /* See if the array syntax is in use. */
7763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7765 /* Consume the `[' token. */
7766 cp_lexer_consume_token (parser
->lexer
);
7767 /* Look for the `]' token. */
7768 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7769 /* Remember that this is the `[]' construct. */
7775 /* Parse the cast-expression. */
7776 expression
= cp_parser_simple_cast_expression (parser
);
7778 /* A delete-expression may not appear in an integral constant
7780 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7781 return error_mark_node
;
7783 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7784 tf_warning_or_error
);
7787 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7788 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7792 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7794 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7795 switch (token
->type
)
7801 case CPP_CLOSE_SQUARE
:
7802 case CPP_CLOSE_PAREN
:
7803 case CPP_CLOSE_BRACE
:
7804 case CPP_OPEN_BRACE
:
7808 case CPP_DEREF_STAR
:
7816 case CPP_GREATER_EQ
:
7837 case CPP_OPEN_PAREN
:
7838 /* In ((type ()) () the last () isn't a valid cast-expression,
7839 so the whole must be parsed as postfix-expression. */
7840 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7843 case CPP_OPEN_SQUARE
:
7844 /* '[' may start a primary-expression in obj-c++ and in C++11,
7845 as a lambda-expression, eg, '(void)[]{}'. */
7846 if (cxx_dialect
>= cxx11
)
7848 return c_dialect_objc ();
7851 case CPP_MINUS_MINUS
:
7852 /* '++' and '--' may or may not start a cast-expression:
7854 struct T { void operator++(int); };
7855 void f() { (T())++; }
7868 /* Parse a cast-expression.
7872 ( type-id ) cast-expression
7874 ADDRESS_P is true iff the unary-expression is appearing as the
7875 operand of the `&' operator. CAST_P is true if this expression is
7876 the target of a cast.
7878 Returns a representation of the expression. */
7881 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7882 bool decltype_p
, cp_id_kind
* pidk
)
7884 /* If it's a `(', then we might be looking at a cast. */
7885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7887 tree type
= NULL_TREE
;
7888 tree expr
= NULL_TREE
;
7889 int cast_expression
= 0;
7890 const char *saved_message
;
7892 /* There's no way to know yet whether or not this is a cast.
7893 For example, `(int (3))' is a unary-expression, while `(int)
7894 3' is a cast. So, we resort to parsing tentatively. */
7895 cp_parser_parse_tentatively (parser
);
7896 /* Types may not be defined in a cast. */
7897 saved_message
= parser
->type_definition_forbidden_message
;
7898 parser
->type_definition_forbidden_message
7899 = G_("types may not be defined in casts");
7900 /* Consume the `('. */
7901 cp_lexer_consume_token (parser
->lexer
);
7902 /* A very tricky bit is that `(struct S) { 3 }' is a
7903 compound-literal (which we permit in C++ as an extension).
7904 But, that construct is not a cast-expression -- it is a
7905 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7906 is legal; if the compound-literal were a cast-expression,
7907 you'd need an extra set of parentheses.) But, if we parse
7908 the type-id, and it happens to be a class-specifier, then we
7909 will commit to the parse at that point, because we cannot
7910 undo the action that is done when creating a new class. So,
7911 then we cannot back up and do a postfix-expression.
7913 Another tricky case is the following (c++/29234):
7915 struct S { void operator () (); };
7922 As a type-id we parse the parenthesized S()() as a function
7923 returning a function, groktypename complains and we cannot
7924 back up in this case either.
7926 Therefore, we scan ahead to the closing `)', and check to see
7927 if the tokens after the `)' can start a cast-expression. Otherwise
7928 we are dealing with an unary-expression, a postfix-expression
7931 Yet another tricky case, in C++11, is the following (c++/54891):
7935 The issue is that usually, besides the case of lambda-expressions,
7936 the parenthesized type-id cannot be followed by '[', and, eg, we
7937 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7938 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7939 we don't commit, we try a cast-expression, then an unary-expression.
7941 Save tokens so that we can put them back. */
7942 cp_lexer_save_tokens (parser
->lexer
);
7944 /* We may be looking at a cast-expression. */
7945 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7946 /*consume_paren=*/true))
7948 = cp_parser_tokens_start_cast_expression (parser
);
7950 /* Roll back the tokens we skipped. */
7951 cp_lexer_rollback_tokens (parser
->lexer
);
7952 /* If we aren't looking at a cast-expression, simulate an error so
7953 that the call to cp_parser_error_occurred below returns true. */
7954 if (!cast_expression
)
7955 cp_parser_simulate_error (parser
);
7958 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7959 parser
->in_type_id_in_expr_p
= true;
7960 /* Look for the type-id. */
7961 type
= cp_parser_type_id (parser
);
7962 /* Look for the closing `)'. */
7963 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7964 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7967 /* Restore the saved message. */
7968 parser
->type_definition_forbidden_message
= saved_message
;
7970 /* At this point this can only be either a cast or a
7971 parenthesized ctor such as `(T ())' that looks like a cast to
7972 function returning T. */
7973 if (!cp_parser_error_occurred (parser
))
7975 /* Only commit if the cast-expression doesn't start with
7976 '++', '--', or '[' in C++11. */
7977 if (cast_expression
> 0)
7978 cp_parser_commit_to_topmost_tentative_parse (parser
);
7980 expr
= cp_parser_cast_expression (parser
,
7981 /*address_p=*/false,
7983 /*decltype_p=*/false,
7986 if (cp_parser_parse_definitely (parser
))
7988 /* Warn about old-style casts, if so requested. */
7989 if (warn_old_style_cast
7990 && !in_system_header_at (input_location
)
7991 && !VOID_TYPE_P (type
)
7992 && current_lang_name
!= lang_name_c
)
7993 warning (OPT_Wold_style_cast
, "use of old-style cast");
7995 /* Only type conversions to integral or enumeration types
7996 can be used in constant-expressions. */
7997 if (!cast_valid_in_integral_constant_expression_p (type
)
7998 && cp_parser_non_integral_constant_expression (parser
,
8000 return error_mark_node
;
8002 /* Perform the cast. */
8003 expr
= build_c_cast (input_location
, type
, expr
);
8008 cp_parser_abort_tentative_parse (parser
);
8011 /* If we get here, then it's not a cast, so it must be a
8012 unary-expression. */
8013 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8014 cast_p
, decltype_p
);
8017 /* Parse a binary expression of the general form:
8021 pm-expression .* cast-expression
8022 pm-expression ->* cast-expression
8024 multiplicative-expression:
8026 multiplicative-expression * pm-expression
8027 multiplicative-expression / pm-expression
8028 multiplicative-expression % pm-expression
8030 additive-expression:
8031 multiplicative-expression
8032 additive-expression + multiplicative-expression
8033 additive-expression - multiplicative-expression
8037 shift-expression << additive-expression
8038 shift-expression >> additive-expression
8040 relational-expression:
8042 relational-expression < shift-expression
8043 relational-expression > shift-expression
8044 relational-expression <= shift-expression
8045 relational-expression >= shift-expression
8049 relational-expression:
8050 relational-expression <? shift-expression
8051 relational-expression >? shift-expression
8053 equality-expression:
8054 relational-expression
8055 equality-expression == relational-expression
8056 equality-expression != relational-expression
8060 and-expression & equality-expression
8062 exclusive-or-expression:
8064 exclusive-or-expression ^ and-expression
8066 inclusive-or-expression:
8067 exclusive-or-expression
8068 inclusive-or-expression | exclusive-or-expression
8070 logical-and-expression:
8071 inclusive-or-expression
8072 logical-and-expression && inclusive-or-expression
8074 logical-or-expression:
8075 logical-and-expression
8076 logical-or-expression || logical-and-expression
8078 All these are implemented with a single function like:
8081 simple-cast-expression
8082 binary-expression <token> binary-expression
8084 CAST_P is true if this expression is the target of a cast.
8086 The binops_by_token map is used to get the tree codes for each <token> type.
8087 binary-expressions are associated according to a precedence table. */
8089 #define TOKEN_PRECEDENCE(token) \
8090 (((token->type == CPP_GREATER \
8091 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8092 && !parser->greater_than_is_operator_p) \
8093 ? PREC_NOT_OPERATOR \
8094 : binops_by_token[token->type].prec)
8097 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8098 bool no_toplevel_fold_p
,
8100 enum cp_parser_prec prec
,
8103 cp_parser_expression_stack stack
;
8104 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8105 cp_parser_expression_stack_entry current
;
8108 enum tree_code rhs_type
;
8109 enum cp_parser_prec new_prec
, lookahead_prec
;
8112 /* Parse the first expression. */
8113 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8114 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8115 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8116 cast_p
, decltype_p
, pidk
);
8117 current
.prec
= prec
;
8119 if (cp_parser_error_occurred (parser
))
8120 return error_mark_node
;
8124 /* Get an operator token. */
8125 token
= cp_lexer_peek_token (parser
->lexer
);
8127 if (warn_cxx0x_compat
8128 && token
->type
== CPP_RSHIFT
8129 && !parser
->greater_than_is_operator_p
)
8131 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
8132 "%<>>%> operator is treated"
8133 " as two right angle brackets in C++11"))
8134 inform (token
->location
,
8135 "suggest parentheses around %<>>%> expression");
8138 new_prec
= TOKEN_PRECEDENCE (token
);
8140 /* Popping an entry off the stack means we completed a subexpression:
8141 - either we found a token which is not an operator (`>' where it is not
8142 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8143 will happen repeatedly;
8144 - or, we found an operator which has lower priority. This is the case
8145 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8147 if (new_prec
<= current
.prec
)
8156 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8157 current
.loc
= token
->location
;
8159 /* We used the operator token. */
8160 cp_lexer_consume_token (parser
->lexer
);
8162 /* For "false && x" or "true || x", x will never be executed;
8163 disable warnings while evaluating it. */
8164 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8165 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8166 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8167 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8169 /* Extract another operand. It may be the RHS of this expression
8170 or the LHS of a new, higher priority expression. */
8171 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8172 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8173 rhs
= cp_parser_simple_cast_expression (parser
);
8175 /* Get another operator token. Look up its precedence to avoid
8176 building a useless (immediately popped) stack entry for common
8177 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8178 token
= cp_lexer_peek_token (parser
->lexer
);
8179 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8180 if (lookahead_prec
> new_prec
)
8182 /* ... and prepare to parse the RHS of the new, higher priority
8183 expression. Since precedence levels on the stack are
8184 monotonically increasing, we do not have to care about
8189 current
.lhs_type
= rhs_type
;
8190 current
.prec
= new_prec
;
8191 new_prec
= lookahead_prec
;
8195 lookahead_prec
= new_prec
;
8196 /* If the stack is not empty, we have parsed into LHS the right side
8197 (`4' in the example above) of an expression we had suspended.
8198 We can use the information on the stack to recover the LHS (`3')
8199 from the stack together with the tree code (`MULT_EXPR'), and
8200 the precedence of the higher level subexpression
8201 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8202 which will be used to actually build the additive expression. */
8204 rhs_type
= current
.lhs_type
;
8209 /* Undo the disabling of warnings done above. */
8210 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8211 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8212 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8213 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8215 if (warn_logical_not_paren
8216 && current
.lhs_type
== TRUTH_NOT_EXPR
)
8217 warn_logical_not_parentheses (current
.loc
, current
.tree_type
, rhs
);
8220 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8221 ERROR_MARK for everything that is not a binary expression.
8222 This makes warn_about_parentheses miss some warnings that
8223 involve unary operators. For unary expressions we should
8224 pass the correct tree_code unless the unary expression was
8225 surrounded by parentheses.
8227 if (no_toplevel_fold_p
8228 && lookahead_prec
<= current
.prec
8230 current
.lhs
= build2 (current
.tree_type
,
8231 TREE_CODE_CLASS (current
.tree_type
)
8233 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8236 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8237 current
.lhs
, current
.lhs_type
,
8238 rhs
, rhs_type
, &overload
,
8239 complain_flags (decltype_p
));
8240 current
.lhs_type
= current
.tree_type
;
8241 if (EXPR_P (current
.lhs
))
8242 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8244 /* If the binary operator required the use of an overloaded operator,
8245 then this expression cannot be an integral constant-expression.
8246 An overloaded operator can be used even if both operands are
8247 otherwise permissible in an integral constant-expression if at
8248 least one of the operands is of enumeration type. */
8251 && cp_parser_non_integral_constant_expression (parser
,
8253 return error_mark_node
;
8260 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8261 bool no_toplevel_fold_p
,
8262 enum cp_parser_prec prec
,
8265 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8266 /*decltype*/false, prec
, pidk
);
8269 /* Parse the `? expression : assignment-expression' part of a
8270 conditional-expression. The LOGICAL_OR_EXPR is the
8271 logical-or-expression that started the conditional-expression.
8272 Returns a representation of the entire conditional-expression.
8274 This routine is used by cp_parser_assignment_expression.
8276 ? expression : assignment-expression
8280 ? : assignment-expression */
8283 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8286 tree assignment_expr
;
8287 struct cp_token
*token
;
8288 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8290 /* Consume the `?' token. */
8291 cp_lexer_consume_token (parser
->lexer
);
8292 token
= cp_lexer_peek_token (parser
->lexer
);
8293 if (cp_parser_allow_gnu_extensions_p (parser
)
8294 && token
->type
== CPP_COLON
)
8296 pedwarn (token
->location
, OPT_Wpedantic
,
8297 "ISO C++ does not allow ?: with omitted middle operand");
8298 /* Implicit true clause. */
8300 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8301 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8305 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8306 parser
->colon_corrects_to_scope_p
= false;
8307 /* Parse the expression. */
8308 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8309 expr
= cp_parser_expression (parser
);
8310 c_inhibit_evaluation_warnings
+=
8311 ((logical_or_expr
== truthvalue_true_node
)
8312 - (logical_or_expr
== truthvalue_false_node
));
8313 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8316 /* The next token should be a `:'. */
8317 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8318 /* Parse the assignment-expression. */
8319 assignment_expr
= cp_parser_assignment_expression (parser
);
8320 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8322 /* Build the conditional-expression. */
8323 return build_x_conditional_expr (loc
, logical_or_expr
,
8326 tf_warning_or_error
);
8329 /* Parse an assignment-expression.
8331 assignment-expression:
8332 conditional-expression
8333 logical-or-expression assignment-operator assignment_expression
8336 CAST_P is true if this expression is the target of a cast.
8337 DECLTYPE_P is true if this expression is the operand of decltype.
8339 Returns a representation for the expression. */
8342 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8343 bool cast_p
, bool decltype_p
)
8347 /* If the next token is the `throw' keyword, then we're looking at
8348 a throw-expression. */
8349 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8350 expr
= cp_parser_throw_expression (parser
);
8351 /* Otherwise, it must be that we are looking at a
8352 logical-or-expression. */
8355 /* Parse the binary expressions (logical-or-expression). */
8356 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8358 PREC_NOT_OPERATOR
, pidk
);
8359 /* If the next token is a `?' then we're actually looking at a
8360 conditional-expression. */
8361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8362 return cp_parser_question_colon_clause (parser
, expr
);
8365 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8367 /* If it's an assignment-operator, we're using the second
8369 enum tree_code assignment_operator
8370 = cp_parser_assignment_operator_opt (parser
);
8371 if (assignment_operator
!= ERROR_MARK
)
8373 bool non_constant_p
;
8374 location_t saved_input_location
;
8376 /* Parse the right-hand side of the assignment. */
8377 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8379 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8380 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8382 /* An assignment may not appear in a
8383 constant-expression. */
8384 if (cp_parser_non_integral_constant_expression (parser
,
8386 return error_mark_node
;
8387 /* Build the assignment expression. Its default
8388 location is the location of the '=' token. */
8389 saved_input_location
= input_location
;
8390 input_location
= loc
;
8391 expr
= build_x_modify_expr (loc
, expr
,
8392 assignment_operator
,
8394 complain_flags (decltype_p
));
8395 input_location
= saved_input_location
;
8403 /* Parse an (optional) assignment-operator.
8405 assignment-operator: one of
8406 = *= /= %= += -= >>= <<= &= ^= |=
8410 assignment-operator: one of
8413 If the next token is an assignment operator, the corresponding tree
8414 code is returned, and the token is consumed. For example, for
8415 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8416 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8417 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8418 operator, ERROR_MARK is returned. */
8420 static enum tree_code
8421 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8426 /* Peek at the next token. */
8427 token
= cp_lexer_peek_token (parser
->lexer
);
8429 switch (token
->type
)
8440 op
= TRUNC_DIV_EXPR
;
8444 op
= TRUNC_MOD_EXPR
;
8476 /* Nothing else is an assignment operator. */
8480 /* If it was an assignment operator, consume it. */
8481 if (op
!= ERROR_MARK
)
8482 cp_lexer_consume_token (parser
->lexer
);
8487 /* Parse an expression.
8490 assignment-expression
8491 expression , assignment-expression
8493 CAST_P is true if this expression is the target of a cast.
8494 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8495 except possibly parenthesized or on the RHS of a comma (N3276).
8497 Returns a representation of the expression. */
8500 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8501 bool cast_p
, bool decltype_p
)
8503 tree expression
= NULL_TREE
;
8504 location_t loc
= UNKNOWN_LOCATION
;
8508 tree assignment_expression
;
8510 /* Parse the next assignment-expression. */
8511 assignment_expression
8512 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8514 /* We don't create a temporary for a call that is the immediate operand
8515 of decltype or on the RHS of a comma. But when we see a comma, we
8516 need to create a temporary for a call on the LHS. */
8517 if (decltype_p
&& !processing_template_decl
8518 && TREE_CODE (assignment_expression
) == CALL_EXPR
8519 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8520 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8521 assignment_expression
8522 = build_cplus_new (TREE_TYPE (assignment_expression
),
8523 assignment_expression
, tf_warning_or_error
);
8525 /* If this is the first assignment-expression, we can just
8528 expression
= assignment_expression
;
8530 expression
= build_x_compound_expr (loc
, expression
,
8531 assignment_expression
,
8532 complain_flags (decltype_p
));
8533 /* If the next token is not a comma, then we are done with the
8535 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8537 /* Consume the `,'. */
8538 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8539 cp_lexer_consume_token (parser
->lexer
);
8540 /* A comma operator cannot appear in a constant-expression. */
8541 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8542 expression
= error_mark_node
;
8548 /* Parse a constant-expression.
8550 constant-expression:
8551 conditional-expression
8553 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8554 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8555 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8556 is false, NON_CONSTANT_P should be NULL. */
8559 cp_parser_constant_expression (cp_parser
* parser
,
8560 bool allow_non_constant_p
,
8561 bool *non_constant_p
)
8563 bool saved_integral_constant_expression_p
;
8564 bool saved_allow_non_integral_constant_expression_p
;
8565 bool saved_non_integral_constant_expression_p
;
8568 /* It might seem that we could simply parse the
8569 conditional-expression, and then check to see if it were
8570 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8571 one that the compiler can figure out is constant, possibly after
8572 doing some simplifications or optimizations. The standard has a
8573 precise definition of constant-expression, and we must honor
8574 that, even though it is somewhat more restrictive.
8580 is not a legal declaration, because `(2, 3)' is not a
8581 constant-expression. The `,' operator is forbidden in a
8582 constant-expression. However, GCC's constant-folding machinery
8583 will fold this operation to an INTEGER_CST for `3'. */
8585 /* Save the old settings. */
8586 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8587 saved_allow_non_integral_constant_expression_p
8588 = parser
->allow_non_integral_constant_expression_p
;
8589 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8590 /* We are now parsing a constant-expression. */
8591 parser
->integral_constant_expression_p
= true;
8592 parser
->allow_non_integral_constant_expression_p
8593 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8594 parser
->non_integral_constant_expression_p
= false;
8595 /* Although the grammar says "conditional-expression", we parse an
8596 "assignment-expression", which also permits "throw-expression"
8597 and the use of assignment operators. In the case that
8598 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8599 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8600 actually essential that we look for an assignment-expression.
8601 For example, cp_parser_initializer_clauses uses this function to
8602 determine whether a particular assignment-expression is in fact
8604 expression
= cp_parser_assignment_expression (parser
);
8605 /* Restore the old settings. */
8606 parser
->integral_constant_expression_p
8607 = saved_integral_constant_expression_p
;
8608 parser
->allow_non_integral_constant_expression_p
8609 = saved_allow_non_integral_constant_expression_p
;
8610 if (cxx_dialect
>= cxx11
)
8612 /* Require an rvalue constant expression here; that's what our
8613 callers expect. Reference constant expressions are handled
8614 separately in e.g. cp_parser_template_argument. */
8615 bool is_const
= potential_rvalue_constant_expression (expression
);
8616 parser
->non_integral_constant_expression_p
= !is_const
;
8617 if (!is_const
&& !allow_non_constant_p
)
8618 require_potential_rvalue_constant_expression (expression
);
8620 if (allow_non_constant_p
)
8621 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8622 parser
->non_integral_constant_expression_p
8623 = saved_non_integral_constant_expression_p
;
8628 /* Parse __builtin_offsetof.
8630 offsetof-expression:
8631 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8633 offsetof-member-designator:
8635 | offsetof-member-designator "." id-expression
8636 | offsetof-member-designator "[" expression "]"
8637 | offsetof-member-designator "->" id-expression */
8640 cp_parser_builtin_offsetof (cp_parser
*parser
)
8642 int save_ice_p
, save_non_ice_p
;
8647 /* We're about to accept non-integral-constant things, but will
8648 definitely yield an integral constant expression. Save and
8649 restore these values around our local parsing. */
8650 save_ice_p
= parser
->integral_constant_expression_p
;
8651 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8653 /* Consume the "__builtin_offsetof" token. */
8654 cp_lexer_consume_token (parser
->lexer
);
8655 /* Consume the opening `('. */
8656 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8657 /* Parse the type-id. */
8658 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8659 type
= cp_parser_type_id (parser
);
8660 /* Look for the `,'. */
8661 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8662 token
= cp_lexer_peek_token (parser
->lexer
);
8664 /* Build the (type *)null that begins the traditional offsetof macro. */
8665 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8666 tf_warning_or_error
);
8668 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8669 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8670 true, &dummy
, token
->location
);
8673 token
= cp_lexer_peek_token (parser
->lexer
);
8674 switch (token
->type
)
8676 case CPP_OPEN_SQUARE
:
8677 /* offsetof-member-designator "[" expression "]" */
8678 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8683 /* offsetof-member-designator "->" identifier */
8684 expr
= grok_array_decl (token
->location
, expr
,
8685 integer_zero_node
, false);
8689 /* offsetof-member-designator "." identifier */
8690 cp_lexer_consume_token (parser
->lexer
);
8691 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8696 case CPP_CLOSE_PAREN
:
8697 /* Consume the ")" token. */
8698 cp_lexer_consume_token (parser
->lexer
);
8702 /* Error. We know the following require will fail, but
8703 that gives the proper error message. */
8704 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8705 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8706 expr
= error_mark_node
;
8712 /* If we're processing a template, we can't finish the semantics yet.
8713 Otherwise we can fold the entire expression now. */
8714 if (processing_template_decl
)
8716 expr
= build1 (OFFSETOF_EXPR
, size_type_node
, expr
);
8717 SET_EXPR_LOCATION (expr
, loc
);
8720 expr
= finish_offsetof (expr
, loc
);
8723 parser
->integral_constant_expression_p
= save_ice_p
;
8724 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8729 /* Parse a trait expression.
8731 Returns a representation of the expression, the underlying type
8732 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8735 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8738 tree type1
, type2
= NULL_TREE
;
8739 bool binary
= false;
8740 bool variadic
= false;
8744 case RID_HAS_NOTHROW_ASSIGN
:
8745 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8747 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8748 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8750 case RID_HAS_NOTHROW_COPY
:
8751 kind
= CPTK_HAS_NOTHROW_COPY
;
8753 case RID_HAS_TRIVIAL_ASSIGN
:
8754 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8756 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8757 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8759 case RID_HAS_TRIVIAL_COPY
:
8760 kind
= CPTK_HAS_TRIVIAL_COPY
;
8762 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8763 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8765 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8766 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8768 case RID_IS_ABSTRACT
:
8769 kind
= CPTK_IS_ABSTRACT
;
8771 case RID_IS_BASE_OF
:
8772 kind
= CPTK_IS_BASE_OF
;
8776 kind
= CPTK_IS_CLASS
;
8779 kind
= CPTK_IS_EMPTY
;
8782 kind
= CPTK_IS_ENUM
;
8785 kind
= CPTK_IS_FINAL
;
8787 case RID_IS_LITERAL_TYPE
:
8788 kind
= CPTK_IS_LITERAL_TYPE
;
8793 case RID_IS_POLYMORPHIC
:
8794 kind
= CPTK_IS_POLYMORPHIC
;
8796 case RID_IS_STD_LAYOUT
:
8797 kind
= CPTK_IS_STD_LAYOUT
;
8799 case RID_IS_TRIVIAL
:
8800 kind
= CPTK_IS_TRIVIAL
;
8802 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8803 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8806 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8807 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8810 case RID_IS_TRIVIALLY_COPYABLE
:
8811 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8814 kind
= CPTK_IS_UNION
;
8816 case RID_UNDERLYING_TYPE
:
8817 kind
= CPTK_UNDERLYING_TYPE
;
8822 case RID_DIRECT_BASES
:
8823 kind
= CPTK_DIRECT_BASES
;
8829 /* Consume the token. */
8830 cp_lexer_consume_token (parser
->lexer
);
8832 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8834 type1
= cp_parser_type_id (parser
);
8836 if (type1
== error_mark_node
)
8837 return error_mark_node
;
8841 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8843 type2
= cp_parser_type_id (parser
);
8845 if (type2
== error_mark_node
)
8846 return error_mark_node
;
8850 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8852 cp_lexer_consume_token (parser
->lexer
);
8853 tree elt
= cp_parser_type_id (parser
);
8854 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8856 cp_lexer_consume_token (parser
->lexer
);
8857 elt
= make_pack_expansion (elt
);
8859 if (elt
== error_mark_node
)
8860 return error_mark_node
;
8861 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8865 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8867 /* Complete the trait expression, which may mean either processing
8868 the trait expr now or saving it for template instantiation. */
8871 case CPTK_UNDERLYING_TYPE
:
8872 return finish_underlying_type (type1
);
8874 return finish_bases (type1
, false);
8875 case CPTK_DIRECT_BASES
:
8876 return finish_bases (type1
, true);
8878 return finish_trait_expr (kind
, type1
, type2
);
8882 /* Lambdas that appear in variable initializer or default argument scope
8883 get that in their mangling, so we need to record it. We might as well
8884 use the count for function and namespace scopes as well. */
8885 static GTY(()) tree lambda_scope
;
8886 static GTY(()) int lambda_count
;
8887 typedef struct GTY(()) tree_int
8892 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8895 start_lambda_scope (tree decl
)
8899 /* Once we're inside a function, we ignore other scopes and just push
8900 the function again so that popping works properly. */
8901 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8902 decl
= current_function_decl
;
8903 ti
.t
= lambda_scope
;
8904 ti
.i
= lambda_count
;
8905 vec_safe_push (lambda_scope_stack
, ti
);
8906 if (lambda_scope
!= decl
)
8908 /* Don't reset the count if we're still in the same function. */
8909 lambda_scope
= decl
;
8915 record_lambda_scope (tree lambda
)
8917 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8918 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8922 finish_lambda_scope (void)
8924 tree_int
*p
= &lambda_scope_stack
->last ();
8925 if (lambda_scope
!= p
->t
)
8927 lambda_scope
= p
->t
;
8928 lambda_count
= p
->i
;
8930 lambda_scope_stack
->pop ();
8933 /* Parse a lambda expression.
8936 lambda-introducer lambda-declarator [opt] compound-statement
8938 Returns a representation of the expression. */
8941 cp_parser_lambda_expression (cp_parser
* parser
)
8943 tree lambda_expr
= build_lambda_expr ();
8946 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8947 cp_token_position start
= 0;
8949 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
8951 if (cp_unevaluated_operand
)
8953 if (!token
->error_reported
)
8955 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
8956 "lambda-expression in unevaluated context");
8957 token
->error_reported
= true;
8961 else if (parser
->in_template_argument_list_p
)
8963 if (!token
->error_reported
)
8965 error_at (token
->location
, "lambda-expression in template-argument");
8966 token
->error_reported
= true;
8971 /* We may be in the middle of deferred access check. Disable
8973 push_deferring_access_checks (dk_no_deferred
);
8975 cp_parser_lambda_introducer (parser
, lambda_expr
);
8977 type
= begin_lambda_type (lambda_expr
);
8978 if (type
== error_mark_node
)
8979 return error_mark_node
;
8981 record_lambda_scope (lambda_expr
);
8983 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8984 determine_visibility (TYPE_NAME (type
));
8986 /* Now that we've started the type, add the capture fields for any
8987 explicit captures. */
8988 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
8991 /* Inside the class, surrounding template-parameter-lists do not apply. */
8992 unsigned int saved_num_template_parameter_lists
8993 = parser
->num_template_parameter_lists
;
8994 unsigned char in_statement
= parser
->in_statement
;
8995 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
8996 bool fully_implicit_function_template_p
8997 = parser
->fully_implicit_function_template_p
;
8998 tree implicit_template_parms
= parser
->implicit_template_parms
;
8999 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9000 bool auto_is_implicit_function_template_parm_p
9001 = parser
->auto_is_implicit_function_template_parm_p
;
9003 parser
->num_template_parameter_lists
= 0;
9004 parser
->in_statement
= 0;
9005 parser
->in_switch_statement_p
= false;
9006 parser
->fully_implicit_function_template_p
= false;
9007 parser
->implicit_template_parms
= 0;
9008 parser
->implicit_template_scope
= 0;
9009 parser
->auto_is_implicit_function_template_parm_p
= false;
9011 /* By virtue of defining a local class, a lambda expression has access to
9012 the private variables of enclosing classes. */
9014 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9018 if (!cp_parser_error_occurred (parser
)
9019 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9020 && cp_parser_start_tentative_firewall (parser
))
9022 cp_parser_lambda_body (parser
, lambda_expr
);
9024 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9026 if (cp_parser_skip_to_closing_brace (parser
))
9027 cp_lexer_consume_token (parser
->lexer
);
9030 /* The capture list was built up in reverse order; fix that now. */
9031 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9032 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9035 maybe_add_lambda_conv_op (type
);
9037 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9039 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9040 parser
->in_statement
= in_statement
;
9041 parser
->in_switch_statement_p
= in_switch_statement_p
;
9042 parser
->fully_implicit_function_template_p
9043 = fully_implicit_function_template_p
;
9044 parser
->implicit_template_parms
= implicit_template_parms
;
9045 parser
->implicit_template_scope
= implicit_template_scope
;
9046 parser
->auto_is_implicit_function_template_parm_p
9047 = auto_is_implicit_function_template_parm_p
;
9050 pop_deferring_access_checks ();
9052 /* This field is only used during parsing of the lambda. */
9053 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9055 /* This lambda shouldn't have any proxies left at this point. */
9056 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9057 /* And now that we're done, push proxies for an enclosing lambda. */
9058 insert_pending_capture_proxies ();
9061 lambda_expr
= build_lambda_object (lambda_expr
);
9063 lambda_expr
= error_mark_node
;
9065 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9070 /* Parse the beginning of a lambda expression.
9073 [ lambda-capture [opt] ]
9075 LAMBDA_EXPR is the current representation of the lambda expression. */
9078 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9080 /* Need commas after the first capture. */
9083 /* Eat the leading `['. */
9084 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9086 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9087 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9088 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9089 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9090 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9091 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9093 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9095 cp_lexer_consume_token (parser
->lexer
);
9099 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9101 cp_token
* capture_token
;
9103 tree capture_init_expr
;
9104 cp_id_kind idk
= CP_ID_KIND_NONE
;
9105 bool explicit_init_p
= false;
9107 enum capture_kind_type
9112 enum capture_kind_type capture_kind
= BY_COPY
;
9114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9116 error ("expected end of capture-list");
9123 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9125 /* Possibly capture `this'. */
9126 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9128 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9129 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9130 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9131 "with by-copy capture default");
9132 cp_lexer_consume_token (parser
->lexer
);
9133 add_capture (lambda_expr
,
9134 /*id=*/this_identifier
,
9135 /*initializer=*/finish_this_expr(),
9136 /*by_reference_p=*/false,
9141 /* Remember whether we want to capture as a reference or not. */
9142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9144 capture_kind
= BY_REFERENCE
;
9145 cp_lexer_consume_token (parser
->lexer
);
9148 /* Get the identifier. */
9149 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9150 capture_id
= cp_parser_identifier (parser
);
9152 if (capture_id
== error_mark_node
)
9153 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9154 delimiters, but I modified this to stop on unnested ']' as well. It
9155 was already changed to stop on unnested '}', so the
9156 "closing_parenthesis" name is no more misleading with my change. */
9158 cp_parser_skip_to_closing_parenthesis (parser
,
9159 /*recovering=*/true,
9161 /*consume_paren=*/true);
9165 /* Find the initializer for this capture. */
9166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9167 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9168 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9170 bool direct
, non_constant
;
9171 /* An explicit initializer exists. */
9172 if (cxx_dialect
< cxx14
)
9173 pedwarn (input_location
, 0,
9174 "lambda capture initializers "
9175 "only available with -std=c++14 or -std=gnu++14");
9176 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9178 explicit_init_p
= true;
9179 if (capture_init_expr
== NULL_TREE
)
9181 error ("empty initializer for lambda init-capture");
9182 capture_init_expr
= error_mark_node
;
9187 const char* error_msg
;
9189 /* Turn the identifier into an id-expression. */
9191 = cp_parser_lookup_name_simple (parser
, capture_id
,
9192 capture_token
->location
);
9194 if (capture_init_expr
== error_mark_node
)
9196 unqualified_name_lookup_error (capture_id
);
9199 else if (DECL_P (capture_init_expr
)
9200 && (!VAR_P (capture_init_expr
)
9201 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9203 error_at (capture_token
->location
,
9204 "capture of non-variable %qD ",
9206 inform (0, "%q+#D declared here", capture_init_expr
);
9209 if (VAR_P (capture_init_expr
)
9210 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9212 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9213 "%qD with non-automatic storage duration",
9215 inform (0, "%q+#D declared here", capture_init_expr
);
9220 = finish_id_expression
9225 /*integral_constant_expression_p=*/false,
9226 /*allow_non_integral_constant_expression_p=*/false,
9227 /*non_integral_constant_expression_p=*/NULL
,
9228 /*template_p=*/false,
9230 /*address_p=*/false,
9231 /*template_arg_p=*/false,
9233 capture_token
->location
);
9235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9237 cp_lexer_consume_token (parser
->lexer
);
9238 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9241 check_for_bare_parameter_packs (capture_init_expr
);
9244 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9245 && !explicit_init_p
)
9247 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9248 && capture_kind
== BY_COPY
)
9249 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9250 "of %qD redundant with by-copy capture default",
9252 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9253 && capture_kind
== BY_REFERENCE
)
9254 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9255 "capture of %qD redundant with by-reference capture "
9256 "default", capture_id
);
9259 add_capture (lambda_expr
,
9262 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9266 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9269 /* Parse the (optional) middle of a lambda expression.
9272 < template-parameter-list [opt] >
9273 ( parameter-declaration-clause [opt] )
9274 attribute-specifier [opt]
9276 exception-specification [opt]
9277 lambda-return-type-clause [opt]
9279 LAMBDA_EXPR is the current representation of the lambda expression. */
9282 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9284 /* 5.1.1.4 of the standard says:
9285 If a lambda-expression does not include a lambda-declarator, it is as if
9286 the lambda-declarator were ().
9287 This means an empty parameter list, no attributes, and no exception
9289 tree param_list
= void_list_node
;
9290 tree attributes
= NULL_TREE
;
9291 tree exception_spec
= NULL_TREE
;
9292 tree template_param_list
= NULL_TREE
;
9294 /* The template-parameter-list is optional, but must begin with
9295 an opening angle if present. */
9296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9298 if (cxx_dialect
< cxx14
)
9299 pedwarn (parser
->lexer
->next_token
->location
, 0,
9300 "lambda templates are only available with "
9301 "-std=c++14 or -std=gnu++14");
9303 cp_lexer_consume_token (parser
->lexer
);
9305 template_param_list
= cp_parser_template_parameter_list (parser
);
9307 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9309 /* We just processed one more parameter list. */
9310 ++parser
->num_template_parameter_lists
;
9313 /* The parameter-declaration-clause is optional (unless
9314 template-parameter-list was given), but must begin with an
9315 opening parenthesis if present. */
9316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9318 cp_lexer_consume_token (parser
->lexer
);
9320 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9322 /* Parse parameters. */
9323 param_list
= cp_parser_parameter_declaration_clause (parser
);
9325 /* Default arguments shall not be specified in the
9326 parameter-declaration-clause of a lambda-declarator. */
9327 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9328 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9329 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9330 "default argument specified for lambda parameter");
9332 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9334 attributes
= cp_parser_attributes_opt (parser
);
9336 /* Parse optional `mutable' keyword. */
9337 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9339 cp_lexer_consume_token (parser
->lexer
);
9340 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9343 /* Parse optional exception specification. */
9344 exception_spec
= cp_parser_exception_specification_opt (parser
);
9346 /* Parse optional trailing return type. */
9347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9349 cp_lexer_consume_token (parser
->lexer
);
9350 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9351 = cp_parser_trailing_type_id (parser
);
9354 /* The function parameters must be in scope all the way until after the
9355 trailing-return-type in case of decltype. */
9356 pop_bindings_and_leave_scope ();
9358 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9359 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9361 /* Create the function call operator.
9363 Messing with declarators like this is no uglier than building up the
9364 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9367 cp_decl_specifier_seq return_type_specs
;
9368 cp_declarator
* declarator
;
9373 clear_decl_specs (&return_type_specs
);
9374 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9375 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9377 /* Maybe we will deduce the return type later. */
9378 return_type_specs
.type
= make_auto ();
9380 p
= obstack_alloc (&declarator_obstack
, 0);
9382 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9385 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9386 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9387 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9388 VIRT_SPEC_UNSPECIFIED
,
9391 /*late_return_type=*/NULL_TREE
);
9392 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9394 fco
= grokmethod (&return_type_specs
,
9397 if (fco
!= error_mark_node
)
9399 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9400 DECL_ARTIFICIAL (fco
) = 1;
9401 /* Give the object parameter a different name. */
9402 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9403 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9404 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9406 if (template_param_list
)
9408 fco
= finish_member_template_decl (fco
);
9409 finish_template_decl (template_param_list
);
9410 --parser
->num_template_parameter_lists
;
9412 else if (parser
->fully_implicit_function_template_p
)
9413 fco
= finish_fully_implicit_template (parser
, fco
);
9415 finish_member_declaration (fco
);
9417 obstack_free (&declarator_obstack
, p
);
9419 return (fco
!= error_mark_node
);
9423 /* Parse the body of a lambda expression, which is simply
9427 but which requires special handling.
9428 LAMBDA_EXPR is the current representation of the lambda expression. */
9431 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9433 bool nested
= (current_function_decl
!= NULL_TREE
);
9434 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9436 push_function_context ();
9438 /* Still increment function_depth so that we don't GC in the
9439 middle of an expression. */
9441 /* Clear this in case we're in the middle of a default argument. */
9442 parser
->local_variables_forbidden_p
= false;
9444 /* Finish the function call operator
9446 + late_parsing_for_member
9447 + function_definition_after_declarator
9448 + ctor_initializer_opt_and_function_body */
9450 tree fco
= lambda_function (lambda_expr
);
9456 /* Let the front end know that we are going to be defining this
9458 start_preparsed_function (fco
,
9460 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9462 start_lambda_scope (fco
);
9463 body
= begin_function_body ();
9465 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9468 /* Push the proxies for any explicit captures. */
9469 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9470 cap
= TREE_CHAIN (cap
))
9471 build_capture_proxy (TREE_PURPOSE (cap
));
9473 compound_stmt
= begin_compound_stmt (0);
9475 /* 5.1.1.4 of the standard says:
9476 If a lambda-expression does not include a trailing-return-type, it
9477 is as if the trailing-return-type denotes the following type:
9478 * if the compound-statement is of the form
9479 { return attribute-specifier [opt] expression ; }
9480 the type of the returned expression after lvalue-to-rvalue
9481 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9482 (_conv.array_ 4.2), and function-to-pointer conversion
9484 * otherwise, void. */
9486 /* In a lambda that has neither a lambda-return-type-clause
9487 nor a deducible form, errors should be reported for return statements
9488 in the body. Since we used void as the placeholder return type, parsing
9489 the body as usual will give such desired behavior. */
9490 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9491 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9492 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9494 tree expr
= NULL_TREE
;
9495 cp_id_kind idk
= CP_ID_KIND_NONE
;
9497 /* Parse tentatively in case there's more after the initial return
9499 cp_parser_parse_tentatively (parser
);
9501 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9503 expr
= cp_parser_expression (parser
, &idk
);
9505 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9506 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9508 if (cp_parser_parse_definitely (parser
))
9510 if (!processing_template_decl
)
9511 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9513 /* Will get error here if type not deduced yet. */
9514 finish_return_stmt (expr
);
9522 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9523 cp_parser_label_declaration (parser
);
9524 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9525 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9528 finish_compound_stmt (compound_stmt
);
9531 finish_function_body (body
);
9532 finish_lambda_scope ();
9534 /* Finish the function and generate code for it if necessary. */
9535 tree fn
= finish_function (/*inline*/2);
9537 /* Only expand if the call op is not a template. */
9538 if (!DECL_TEMPLATE_INFO (fco
))
9539 expand_or_defer_fn (fn
);
9542 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9544 pop_function_context();
9549 /* Statements [gram.stmt.stmt] */
9551 /* Parse a statement.
9555 expression-statement
9560 declaration-statement
9567 attribute-specifier-seq (opt) expression-statement
9568 attribute-specifier-seq (opt) compound-statement
9569 attribute-specifier-seq (opt) selection-statement
9570 attribute-specifier-seq (opt) iteration-statement
9571 attribute-specifier-seq (opt) jump-statement
9572 declaration-statement
9573 attribute-specifier-seq (opt) try-block
9580 IN_COMPOUND is true when the statement is nested inside a
9581 cp_parser_compound_statement; this matters for certain pragmas.
9583 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9584 is a (possibly labeled) if statement which is not enclosed in braces
9585 and has an else clause. This is used to implement -Wparentheses. */
9588 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9589 bool in_compound
, bool *if_p
)
9591 tree statement
, std_attrs
= NULL_TREE
;
9593 location_t statement_location
, attrs_location
;
9598 /* There is no statement yet. */
9599 statement
= NULL_TREE
;
9601 saved_token_sentinel
saved_tokens (parser
->lexer
);
9602 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9603 if (c_dialect_objc ())
9604 /* In obj-c++, seeing '[[' might be the either the beginning of
9605 c++11 attributes, or a nested objc-message-expression. So
9606 let's parse the c++11 attributes tentatively. */
9607 cp_parser_parse_tentatively (parser
);
9608 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9609 if (c_dialect_objc ())
9611 if (!cp_parser_parse_definitely (parser
))
9612 std_attrs
= NULL_TREE
;
9615 /* Peek at the next token. */
9616 token
= cp_lexer_peek_token (parser
->lexer
);
9617 /* Remember the location of the first token in the statement. */
9618 statement_location
= token
->location
;
9619 /* If this is a keyword, then that will often determine what kind of
9620 statement we have. */
9621 if (token
->type
== CPP_KEYWORD
)
9623 enum rid keyword
= token
->keyword
;
9629 /* Looks like a labeled-statement with a case label.
9630 Parse the label, and then use tail recursion to parse
9632 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9637 statement
= cp_parser_selection_statement (parser
, if_p
);
9643 statement
= cp_parser_iteration_statement (parser
, false);
9649 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9650 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9651 cp_lexer_consume_token (parser
->lexer
);
9652 statement
= error_mark_node
;
9655 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9662 statement
= cp_parser_jump_statement (parser
);
9666 cp_lexer_consume_token (parser
->lexer
);
9669 tree sync_expr
= build_cilk_sync ();
9670 SET_EXPR_LOCATION (sync_expr
,
9672 statement
= finish_expr_stmt (sync_expr
);
9676 error_at (token
->location
, "-fcilkplus must be enabled to use"
9678 statement
= error_mark_node
;
9680 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9683 /* Objective-C++ exception-handling constructs. */
9686 case RID_AT_FINALLY
:
9687 case RID_AT_SYNCHRONIZED
:
9689 statement
= cp_parser_objc_statement (parser
);
9693 statement
= cp_parser_try_block (parser
);
9697 /* This must be a namespace alias definition. */
9698 cp_parser_declaration_statement (parser
);
9701 case RID_TRANSACTION_ATOMIC
:
9702 case RID_TRANSACTION_RELAXED
:
9703 statement
= cp_parser_transaction (parser
, keyword
);
9705 case RID_TRANSACTION_CANCEL
:
9706 statement
= cp_parser_transaction_cancel (parser
);
9710 /* It might be a keyword like `int' that can start a
9711 declaration-statement. */
9715 else if (token
->type
== CPP_NAME
)
9717 /* If the next token is a `:', then we are looking at a
9718 labeled-statement. */
9719 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9720 if (token
->type
== CPP_COLON
)
9722 /* Looks like a labeled-statement with an ordinary label.
9723 Parse the label, and then use tail recursion to parse
9726 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9730 /* Anything that starts with a `{' must be a compound-statement. */
9731 else if (token
->type
== CPP_OPEN_BRACE
)
9732 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9733 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9734 a statement all its own. */
9735 else if (token
->type
== CPP_PRAGMA
)
9737 /* Only certain OpenMP pragmas are attached to statements, and thus
9738 are considered statements themselves. All others are not. In
9739 the context of a compound, accept the pragma as a "statement" and
9740 return so that we can check for a close brace. Otherwise we
9741 require a real statement and must go back and read one. */
9743 cp_parser_pragma (parser
, pragma_compound
);
9744 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9748 else if (token
->type
== CPP_EOF
)
9750 cp_parser_error (parser
, "expected statement");
9754 /* Everything else must be a declaration-statement or an
9755 expression-statement. Try for the declaration-statement
9756 first, unless we are looking at a `;', in which case we know that
9757 we have an expression-statement. */
9760 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9762 if (std_attrs
!= NULL_TREE
)
9764 /* Attributes should be parsed as part of the the
9765 declaration, so let's un-parse them. */
9766 saved_tokens
.rollback();
9767 std_attrs
= NULL_TREE
;
9770 cp_parser_parse_tentatively (parser
);
9771 /* Try to parse the declaration-statement. */
9772 cp_parser_declaration_statement (parser
);
9773 /* If that worked, we're done. */
9774 if (cp_parser_parse_definitely (parser
))
9777 /* Look for an expression-statement instead. */
9778 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9781 /* Set the line number for the statement. */
9782 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9783 SET_EXPR_LOCATION (statement
, statement_location
);
9785 /* Note that for now, we don't do anything with c++11 statements
9786 parsed at this level. */
9787 if (std_attrs
!= NULL_TREE
)
9788 warning_at (attrs_location
,
9790 "attributes at the beginning of statement are ignored");
9793 /* Parse the label for a labeled-statement, i.e.
9796 case constant-expression :
9800 case constant-expression ... constant-expression : statement
9802 When a label is parsed without errors, the label is added to the
9803 parse tree by the finish_* functions, so this function doesn't
9804 have to return the label. */
9807 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9810 tree label
= NULL_TREE
;
9811 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9813 /* The next token should be an identifier. */
9814 token
= cp_lexer_peek_token (parser
->lexer
);
9815 if (token
->type
!= CPP_NAME
9816 && token
->type
!= CPP_KEYWORD
)
9818 cp_parser_error (parser
, "expected labeled-statement");
9822 parser
->colon_corrects_to_scope_p
= false;
9823 switch (token
->keyword
)
9830 /* Consume the `case' token. */
9831 cp_lexer_consume_token (parser
->lexer
);
9832 /* Parse the constant-expression. */
9833 expr
= cp_parser_constant_expression (parser
);
9835 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9836 if (ellipsis
->type
== CPP_ELLIPSIS
)
9838 /* Consume the `...' token. */
9839 cp_lexer_consume_token (parser
->lexer
);
9841 cp_parser_constant_expression (parser
);
9843 /* We don't need to emit warnings here, as the common code
9844 will do this for us. */
9847 expr_hi
= NULL_TREE
;
9849 if (parser
->in_switch_statement_p
)
9850 finish_case_label (token
->location
, expr
, expr_hi
);
9852 error_at (token
->location
,
9853 "case label %qE not within a switch statement",
9859 /* Consume the `default' token. */
9860 cp_lexer_consume_token (parser
->lexer
);
9862 if (parser
->in_switch_statement_p
)
9863 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9865 error_at (token
->location
, "case label not within a switch statement");
9869 /* Anything else must be an ordinary label. */
9870 label
= finish_label_stmt (cp_parser_identifier (parser
));
9874 /* Require the `:' token. */
9875 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9877 /* An ordinary label may optionally be followed by attributes.
9878 However, this is only permitted if the attributes are then
9879 followed by a semicolon. This is because, for backward
9880 compatibility, when parsing
9881 lab: __attribute__ ((unused)) int i;
9882 we want the attribute to attach to "i", not "lab". */
9883 if (label
!= NULL_TREE
9884 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9887 cp_parser_parse_tentatively (parser
);
9888 attrs
= cp_parser_gnu_attributes_opt (parser
);
9889 if (attrs
== NULL_TREE
9890 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9891 cp_parser_abort_tentative_parse (parser
);
9892 else if (!cp_parser_parse_definitely (parser
))
9895 attributes
= chainon (attributes
, attrs
);
9898 if (attributes
!= NULL_TREE
)
9899 cplus_decl_attributes (&label
, attributes
, 0);
9901 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9904 /* Parse an expression-statement.
9906 expression-statement:
9909 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9910 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9911 indicates whether this expression-statement is part of an
9912 expression statement. */
9915 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9917 tree statement
= NULL_TREE
;
9918 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9920 /* If the next token is a ';', then there is no expression
9922 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9924 statement
= cp_parser_expression (parser
);
9925 if (statement
== error_mark_node
9926 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9928 cp_parser_skip_to_end_of_block_or_statement (parser
);
9929 return error_mark_node
;
9933 /* Give a helpful message for "A<T>::type t;" and the like. */
9934 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9935 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9937 if (TREE_CODE (statement
) == SCOPE_REF
)
9938 error_at (token
->location
, "need %<typename%> before %qE because "
9939 "%qT is a dependent scope",
9940 statement
, TREE_OPERAND (statement
, 0));
9941 else if (is_overloaded_fn (statement
)
9942 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
9945 tree fn
= get_first_fn (statement
);
9946 error_at (token
->location
,
9947 "%<%T::%D%> names the constructor, not the type",
9948 DECL_CONTEXT (fn
), DECL_NAME (fn
));
9952 /* Consume the final `;'. */
9953 cp_parser_consume_semicolon_at_end_of_statement (parser
);
9955 if (in_statement_expr
9956 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
9957 /* This is the final expression statement of a statement
9959 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
9961 statement
= finish_expr_stmt (statement
);
9966 /* Parse a compound-statement.
9969 { statement-seq [opt] }
9974 { label-declaration-seq [opt] statement-seq [opt] }
9976 label-declaration-seq:
9978 label-declaration-seq label-declaration
9980 Returns a tree representing the statement. */
9983 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
9984 bool in_try
, bool function_body
)
9988 /* Consume the `{'. */
9989 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9990 return error_mark_node
;
9991 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
9992 && !function_body
&& cxx_dialect
< cxx14
)
9993 pedwarn (input_location
, OPT_Wpedantic
,
9994 "compound-statement in constexpr function");
9995 /* Begin the compound-statement. */
9996 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
9997 /* If the next keyword is `__label__' we have a label declaration. */
9998 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9999 cp_parser_label_declaration (parser
);
10000 /* Parse an (optional) statement-seq. */
10001 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10002 /* Finish the compound-statement. */
10003 finish_compound_stmt (compound_stmt
);
10004 /* Consume the `}'. */
10005 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10007 return compound_stmt
;
10010 /* Parse an (optional) statement-seq.
10014 statement-seq [opt] statement */
10017 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10019 /* Scan statements until there aren't any more. */
10022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10024 /* If we are looking at a `}', then we have run out of
10025 statements; the same is true if we have reached the end
10026 of file, or have stumbled upon a stray '@end'. */
10027 if (token
->type
== CPP_CLOSE_BRACE
10028 || token
->type
== CPP_EOF
10029 || token
->type
== CPP_PRAGMA_EOL
10030 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10033 /* If we are in a compound statement and find 'else' then
10034 something went wrong. */
10035 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10037 if (parser
->in_statement
& IN_IF_STMT
)
10041 token
= cp_lexer_consume_token (parser
->lexer
);
10042 error_at (token
->location
, "%<else%> without a previous %<if%>");
10046 /* Parse the statement. */
10047 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10051 /* Parse a selection-statement.
10053 selection-statement:
10054 if ( condition ) statement
10055 if ( condition ) statement else statement
10056 switch ( condition ) statement
10058 Returns the new IF_STMT or SWITCH_STMT.
10060 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10061 is a (possibly labeled) if statement which is not enclosed in
10062 braces and has an else clause. This is used to implement
10066 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10074 /* Peek at the next token. */
10075 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10077 /* See what kind of keyword it is. */
10078 keyword
= token
->keyword
;
10087 /* Look for the `('. */
10088 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10090 cp_parser_skip_to_end_of_statement (parser
);
10091 return error_mark_node
;
10094 /* Begin the selection-statement. */
10095 if (keyword
== RID_IF
)
10096 statement
= begin_if_stmt ();
10098 statement
= begin_switch_stmt ();
10100 /* Parse the condition. */
10101 condition
= cp_parser_condition (parser
);
10102 /* Look for the `)'. */
10103 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10104 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10105 /*consume_paren=*/true);
10107 if (keyword
== RID_IF
)
10110 unsigned char in_statement
;
10112 /* Add the condition. */
10113 finish_if_stmt_cond (condition
, statement
);
10115 /* Parse the then-clause. */
10116 in_statement
= parser
->in_statement
;
10117 parser
->in_statement
|= IN_IF_STMT
;
10118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10120 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10121 add_stmt (build_empty_stmt (loc
));
10122 cp_lexer_consume_token (parser
->lexer
);
10123 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10124 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10125 "empty body in an %<if%> statement");
10129 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
10130 parser
->in_statement
= in_statement
;
10132 finish_then_clause (statement
);
10134 /* If the next token is `else', parse the else-clause. */
10135 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10138 /* Consume the `else' keyword. */
10139 cp_lexer_consume_token (parser
->lexer
);
10140 begin_else_clause (statement
);
10141 /* Parse the else-clause. */
10142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10145 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10147 OPT_Wempty_body
, "suggest braces around "
10148 "empty body in an %<else%> statement");
10149 add_stmt (build_empty_stmt (loc
));
10150 cp_lexer_consume_token (parser
->lexer
);
10153 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10155 finish_else_clause (statement
);
10157 /* If we are currently parsing a then-clause, then
10158 IF_P will not be NULL. We set it to true to
10159 indicate that this if statement has an else clause.
10160 This may trigger the Wparentheses warning below
10161 when we get back up to the parent if statement. */
10167 /* This if statement does not have an else clause. If
10168 NESTED_IF is true, then the then-clause is an if
10169 statement which does have an else clause. We warn
10170 about the potential ambiguity. */
10172 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10173 "suggest explicit braces to avoid ambiguous"
10177 /* Now we're all done with the if-statement. */
10178 finish_if_stmt (statement
);
10182 bool in_switch_statement_p
;
10183 unsigned char in_statement
;
10185 /* Add the condition. */
10186 finish_switch_cond (condition
, statement
);
10188 /* Parse the body of the switch-statement. */
10189 in_switch_statement_p
= parser
->in_switch_statement_p
;
10190 in_statement
= parser
->in_statement
;
10191 parser
->in_switch_statement_p
= true;
10192 parser
->in_statement
|= IN_SWITCH_STMT
;
10193 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10194 parser
->in_switch_statement_p
= in_switch_statement_p
;
10195 parser
->in_statement
= in_statement
;
10197 /* Now we're all done with the switch-statement. */
10198 finish_switch_stmt (statement
);
10206 cp_parser_error (parser
, "expected selection-statement");
10207 return error_mark_node
;
10211 /* Parse a condition.
10215 type-specifier-seq declarator = initializer-clause
10216 type-specifier-seq declarator braced-init-list
10221 type-specifier-seq declarator asm-specification [opt]
10222 attributes [opt] = assignment-expression
10224 Returns the expression that should be tested. */
10227 cp_parser_condition (cp_parser
* parser
)
10229 cp_decl_specifier_seq type_specifiers
;
10230 const char *saved_message
;
10231 int declares_class_or_enum
;
10233 /* Try the declaration first. */
10234 cp_parser_parse_tentatively (parser
);
10235 /* New types are not allowed in the type-specifier-seq for a
10237 saved_message
= parser
->type_definition_forbidden_message
;
10238 parser
->type_definition_forbidden_message
10239 = G_("types may not be defined in conditions");
10240 /* Parse the type-specifier-seq. */
10241 cp_parser_decl_specifier_seq (parser
,
10242 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10244 &declares_class_or_enum
);
10245 /* Restore the saved message. */
10246 parser
->type_definition_forbidden_message
= saved_message
;
10247 /* If all is well, we might be looking at a declaration. */
10248 if (!cp_parser_error_occurred (parser
))
10251 tree asm_specification
;
10253 cp_declarator
*declarator
;
10254 tree initializer
= NULL_TREE
;
10256 /* Parse the declarator. */
10257 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10258 /*ctor_dtor_or_conv_p=*/NULL
,
10259 /*parenthesized_p=*/NULL
,
10260 /*member_p=*/false,
10261 /*friend_p=*/false);
10262 /* Parse the attributes. */
10263 attributes
= cp_parser_attributes_opt (parser
);
10264 /* Parse the asm-specification. */
10265 asm_specification
= cp_parser_asm_specification_opt (parser
);
10266 /* If the next token is not an `=' or '{', then we might still be
10267 looking at an expression. For example:
10271 looks like a decl-specifier-seq and a declarator -- but then
10272 there is no `=', so this is an expression. */
10273 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10274 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10275 cp_parser_simulate_error (parser
);
10277 /* If we did see an `=' or '{', then we are looking at a declaration
10279 if (cp_parser_parse_definitely (parser
))
10282 bool non_constant_p
;
10283 bool flags
= LOOKUP_ONLYCONVERTING
;
10285 /* Create the declaration. */
10286 decl
= start_decl (declarator
, &type_specifiers
,
10287 /*initialized_p=*/true,
10288 attributes
, /*prefix_attributes=*/NULL_TREE
,
10291 /* Parse the initializer. */
10292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10294 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10295 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10300 /* Consume the `='. */
10301 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10302 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10304 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10307 /* Process the initializer. */
10308 cp_finish_decl (decl
,
10309 initializer
, !non_constant_p
,
10314 pop_scope (pushed_scope
);
10316 return convert_from_reference (decl
);
10319 /* If we didn't even get past the declarator successfully, we are
10320 definitely not looking at a declaration. */
10322 cp_parser_abort_tentative_parse (parser
);
10324 /* Otherwise, we are looking at an expression. */
10325 return cp_parser_expression (parser
);
10328 /* Parses a for-statement or range-for-statement until the closing ')',
10332 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10334 tree init
, scope
, decl
;
10337 /* Begin the for-statement. */
10338 scope
= begin_for_scope (&init
);
10340 /* Parse the initialization. */
10341 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10344 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10346 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10350 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10352 /* Normal for loop */
10353 tree condition
= NULL_TREE
;
10354 tree expression
= NULL_TREE
;
10357 stmt
= begin_for_stmt (scope
, init
);
10358 /* The for-init-statement has already been parsed in
10359 cp_parser_for_init_statement, so no work is needed here. */
10360 finish_for_init_stmt (stmt
);
10362 /* If there's a condition, process it. */
10363 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10364 condition
= cp_parser_condition (parser
);
10367 cp_parser_error (parser
, "missing loop condition in loop with "
10368 "%<GCC ivdep%> pragma");
10369 condition
= error_mark_node
;
10371 finish_for_cond (condition
, stmt
, ivdep
);
10372 /* Look for the `;'. */
10373 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10375 /* If there's an expression, process it. */
10376 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10377 expression
= cp_parser_expression (parser
);
10378 finish_for_expr (expression
, stmt
);
10383 /* Tries to parse a range-based for-statement:
10386 decl-specifier-seq declarator : expression
10388 The decl-specifier-seq declarator and the `:' are already parsed by
10389 cp_parser_for_init_statement. If processing_template_decl it returns a
10390 newly created RANGE_FOR_STMT; if not, it is converted to a
10391 regular FOR_STMT. */
10394 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10397 tree stmt
, range_expr
;
10399 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10401 bool expr_non_constant_p
;
10402 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10405 range_expr
= cp_parser_expression (parser
);
10407 /* If in template, STMT is converted to a normal for-statement
10408 at instantiation. If not, it is done just ahead. */
10409 if (processing_template_decl
)
10411 if (check_for_bare_parameter_packs (range_expr
))
10412 range_expr
= error_mark_node
;
10413 stmt
= begin_range_for_stmt (scope
, init
);
10415 RANGE_FOR_IVDEP (stmt
) = 1;
10416 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10417 if (!type_dependent_expression_p (range_expr
)
10418 /* do_auto_deduction doesn't mess with template init-lists. */
10419 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10420 do_range_for_auto_deduction (range_decl
, range_expr
);
10424 stmt
= begin_for_stmt (scope
, init
);
10425 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10430 /* Subroutine of cp_convert_range_for: given the initializer expression,
10431 builds up the range temporary. */
10434 build_range_temp (tree range_expr
)
10436 tree range_type
, range_temp
;
10438 /* Find out the type deduced by the declaration
10439 `auto &&__range = range_expr'. */
10440 range_type
= cp_build_reference_type (make_auto (), true);
10441 range_type
= do_auto_deduction (range_type
, range_expr
,
10442 type_uses_auto (range_type
));
10444 /* Create the __range variable. */
10445 range_temp
= build_decl (input_location
, VAR_DECL
,
10446 get_identifier ("__for_range"), range_type
);
10447 TREE_USED (range_temp
) = 1;
10448 DECL_ARTIFICIAL (range_temp
) = 1;
10453 /* Used by cp_parser_range_for in template context: we aren't going to
10454 do a full conversion yet, but we still need to resolve auto in the
10455 type of the for-range-declaration if present. This is basically
10456 a shortcut version of cp_convert_range_for. */
10459 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10461 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10464 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10465 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10466 iter_type
= (cp_parser_perform_range_for_lookup
10467 (range_temp
, &begin_dummy
, &end_dummy
));
10470 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10472 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10473 tf_warning_or_error
);
10474 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10475 iter_decl
, auto_node
);
10480 /* Converts a range-based for-statement into a normal
10481 for-statement, as per the definition.
10483 for (RANGE_DECL : RANGE_EXPR)
10486 should be equivalent to:
10489 auto &&__range = RANGE_EXPR;
10490 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10494 RANGE_DECL = *__begin;
10499 If RANGE_EXPR is an array:
10500 BEGIN_EXPR = __range
10501 END_EXPR = __range + ARRAY_SIZE(__range)
10502 Else if RANGE_EXPR has a member 'begin' or 'end':
10503 BEGIN_EXPR = __range.begin()
10504 END_EXPR = __range.end()
10506 BEGIN_EXPR = begin(__range)
10507 END_EXPR = end(__range);
10509 If __range has a member 'begin' but not 'end', or vice versa, we must
10510 still use the second alternative (it will surely fail, however).
10511 When calling begin()/end() in the third alternative we must use
10512 argument dependent lookup, but always considering 'std' as an associated
10516 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10520 tree iter_type
, begin_expr
, end_expr
;
10521 tree condition
, expression
;
10523 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10524 /* If an error happened previously do nothing or else a lot of
10525 unhelpful errors would be issued. */
10526 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10531 if (TREE_CODE (range_expr
) == VAR_DECL
10532 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10533 /* Can't bind a reference to an array of runtime bound. */
10534 range_temp
= range_expr
;
10537 range_temp
= build_range_temp (range_expr
);
10538 pushdecl (range_temp
);
10539 cp_finish_decl (range_temp
, range_expr
,
10540 /*is_constant_init*/false, NULL_TREE
,
10541 LOOKUP_ONLYCONVERTING
);
10542 range_temp
= convert_from_reference (range_temp
);
10544 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10545 &begin_expr
, &end_expr
);
10548 /* The new for initialization statement. */
10549 begin
= build_decl (input_location
, VAR_DECL
,
10550 get_identifier ("__for_begin"), iter_type
);
10551 TREE_USED (begin
) = 1;
10552 DECL_ARTIFICIAL (begin
) = 1;
10554 cp_finish_decl (begin
, begin_expr
,
10555 /*is_constant_init*/false, NULL_TREE
,
10556 LOOKUP_ONLYCONVERTING
);
10558 end
= build_decl (input_location
, VAR_DECL
,
10559 get_identifier ("__for_end"), iter_type
);
10560 TREE_USED (end
) = 1;
10561 DECL_ARTIFICIAL (end
) = 1;
10563 cp_finish_decl (end
, end_expr
,
10564 /*is_constant_init*/false, NULL_TREE
,
10565 LOOKUP_ONLYCONVERTING
);
10567 finish_for_init_stmt (statement
);
10569 /* The new for condition. */
10570 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10573 NULL
, tf_warning_or_error
);
10574 finish_for_cond (condition
, statement
, ivdep
);
10576 /* The new increment expression. */
10577 expression
= finish_unary_op_expr (input_location
,
10578 PREINCREMENT_EXPR
, begin
,
10579 tf_warning_or_error
);
10580 finish_for_expr (expression
, statement
);
10582 /* The declaration is initialized with *__begin inside the loop body. */
10583 cp_finish_decl (range_decl
,
10584 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10585 tf_warning_or_error
),
10586 /*is_constant_init*/false, NULL_TREE
,
10587 LOOKUP_ONLYCONVERTING
);
10592 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10593 We need to solve both at the same time because the method used
10594 depends on the existence of members begin or end.
10595 Returns the type deduced for the iterator expression. */
10598 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10600 if (error_operand_p (range
))
10602 *begin
= *end
= error_mark_node
;
10603 return error_mark_node
;
10606 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10608 error ("range-based %<for%> expression of type %qT "
10609 "has incomplete type", TREE_TYPE (range
));
10610 *begin
= *end
= error_mark_node
;
10611 return error_mark_node
;
10613 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10615 /* If RANGE is an array, we will use pointer arithmetic. */
10617 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10619 array_type_nelts_top (TREE_TYPE (range
)),
10621 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10625 /* If it is not an array, we must do a bit of magic. */
10626 tree id_begin
, id_end
;
10627 tree member_begin
, member_end
;
10629 *begin
= *end
= error_mark_node
;
10631 id_begin
= get_identifier ("begin");
10632 id_end
= get_identifier ("end");
10633 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10634 /*protect=*/2, /*want_type=*/false,
10635 tf_warning_or_error
);
10636 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10637 /*protect=*/2, /*want_type=*/false,
10638 tf_warning_or_error
);
10640 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10642 /* Use the member functions. */
10643 if (member_begin
!= NULL_TREE
)
10644 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10646 error ("range-based %<for%> expression of type %qT has an "
10647 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10649 if (member_end
!= NULL_TREE
)
10650 *end
= cp_parser_range_for_member_function (range
, id_end
);
10652 error ("range-based %<for%> expression of type %qT has a "
10653 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10657 /* Use global functions with ADL. */
10658 vec
<tree
, va_gc
> *vec
;
10659 vec
= make_tree_vector ();
10661 vec_safe_push (vec
, range
);
10663 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10664 tf_warning_or_error
);
10665 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10666 tf_warning_or_error
);
10667 member_end
= perform_koenig_lookup (id_end
, vec
,
10668 tf_warning_or_error
);
10669 *end
= finish_call_expr (member_end
, &vec
, false, true,
10670 tf_warning_or_error
);
10672 release_tree_vector (vec
);
10675 /* Last common checks. */
10676 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10678 /* If one of the expressions is an error do no more checks. */
10679 *begin
= *end
= error_mark_node
;
10680 return error_mark_node
;
10682 else if (type_dependent_expression_p (*begin
)
10683 || type_dependent_expression_p (*end
))
10684 /* Can happen, when, eg, in a template context, Koenig lookup
10685 can't resolve begin/end (c++/58503). */
10689 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10690 /* The unqualified type of the __begin and __end temporaries should
10691 be the same, as required by the multiple auto declaration. */
10692 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10693 error ("inconsistent begin/end types in range-based %<for%> "
10694 "statement: %qT and %qT",
10695 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10701 /* Helper function for cp_parser_perform_range_for_lookup.
10702 Builds a tree for RANGE.IDENTIFIER(). */
10705 cp_parser_range_for_member_function (tree range
, tree identifier
)
10708 vec
<tree
, va_gc
> *vec
;
10710 member
= finish_class_member_access_expr (range
, identifier
,
10711 false, tf_warning_or_error
);
10712 if (member
== error_mark_node
)
10713 return error_mark_node
;
10715 vec
= make_tree_vector ();
10716 res
= finish_call_expr (member
, &vec
,
10717 /*disallow_virtual=*/false,
10718 /*koenig_p=*/false,
10719 tf_warning_or_error
);
10720 release_tree_vector (vec
);
10724 /* Parse an iteration-statement.
10726 iteration-statement:
10727 while ( condition ) statement
10728 do statement while ( expression ) ;
10729 for ( for-init-statement condition [opt] ; expression [opt] )
10732 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10735 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10740 unsigned char in_statement
;
10742 /* Peek at the next token. */
10743 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10745 return error_mark_node
;
10747 /* Remember whether or not we are already within an iteration
10749 in_statement
= parser
->in_statement
;
10751 /* See what kind of keyword it is. */
10752 keyword
= token
->keyword
;
10759 /* Begin the while-statement. */
10760 statement
= begin_while_stmt ();
10761 /* Look for the `('. */
10762 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10763 /* Parse the condition. */
10764 condition
= cp_parser_condition (parser
);
10765 finish_while_stmt_cond (condition
, statement
, ivdep
);
10766 /* Look for the `)'. */
10767 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10768 /* Parse the dependent statement. */
10769 parser
->in_statement
= IN_ITERATION_STMT
;
10770 cp_parser_already_scoped_statement (parser
);
10771 parser
->in_statement
= in_statement
;
10772 /* We're done with the while-statement. */
10773 finish_while_stmt (statement
);
10781 /* Begin the do-statement. */
10782 statement
= begin_do_stmt ();
10783 /* Parse the body of the do-statement. */
10784 parser
->in_statement
= IN_ITERATION_STMT
;
10785 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10786 parser
->in_statement
= in_statement
;
10787 finish_do_body (statement
);
10788 /* Look for the `while' keyword. */
10789 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10790 /* Look for the `('. */
10791 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10792 /* Parse the expression. */
10793 expression
= cp_parser_expression (parser
);
10794 /* We're done with the do-statement. */
10795 finish_do_stmt (expression
, statement
, ivdep
);
10796 /* Look for the `)'. */
10797 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10798 /* Look for the `;'. */
10799 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10805 /* Look for the `('. */
10806 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10808 statement
= cp_parser_for (parser
, ivdep
);
10810 /* Look for the `)'. */
10811 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10813 /* Parse the body of the for-statement. */
10814 parser
->in_statement
= IN_ITERATION_STMT
;
10815 cp_parser_already_scoped_statement (parser
);
10816 parser
->in_statement
= in_statement
;
10818 /* We're done with the for-statement. */
10819 finish_for_stmt (statement
);
10824 cp_parser_error (parser
, "expected iteration-statement");
10825 statement
= error_mark_node
;
10832 /* Parse a for-init-statement or the declarator of a range-based-for.
10833 Returns true if a range-based-for declaration is seen.
10835 for-init-statement:
10836 expression-statement
10837 simple-declaration */
10840 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10842 /* If the next token is a `;', then we have an empty
10843 expression-statement. Grammatically, this is also a
10844 simple-declaration, but an invalid one, because it does not
10845 declare anything. Therefore, if we did not handle this case
10846 specially, we would issue an error message about an invalid
10848 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10850 bool is_range_for
= false;
10851 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10854 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10856 /* N3994 -- for (id : init) ... */
10857 if (cxx_dialect
< cxx1z
)
10858 pedwarn (input_location
, 0, "range-based for loop without a "
10859 "type-specifier only available with "
10860 "-std=c++1z or -std=gnu++1z");
10861 tree name
= cp_parser_identifier (parser
);
10862 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10863 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10865 cp_lexer_consume_token (parser
->lexer
);
10869 /* A colon is used in range-based for. */
10870 parser
->colon_corrects_to_scope_p
= false;
10872 /* We're going to speculatively look for a declaration, falling back
10873 to an expression, if necessary. */
10874 cp_parser_parse_tentatively (parser
);
10875 /* Parse the declaration. */
10876 cp_parser_simple_declaration (parser
,
10877 /*function_definition_allowed_p=*/false,
10879 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10882 /* It is a range-for, consume the ':' */
10883 cp_lexer_consume_token (parser
->lexer
);
10884 is_range_for
= true;
10885 if (cxx_dialect
< cxx11
)
10887 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10888 "range-based %<for%> loops only available with "
10889 "-std=c++11 or -std=gnu++11");
10890 *decl
= error_mark_node
;
10894 /* The ';' is not consumed yet because we told
10895 cp_parser_simple_declaration not to. */
10896 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10898 if (cp_parser_parse_definitely (parser
))
10899 return is_range_for
;
10900 /* If the tentative parse failed, then we shall need to look for an
10901 expression-statement. */
10903 /* If we are here, it is an expression-statement. */
10904 cp_parser_expression_statement (parser
, NULL_TREE
);
10908 /* Parse a jump-statement.
10913 return expression [opt] ;
10914 return braced-init-list ;
10920 goto * expression ;
10922 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10925 cp_parser_jump_statement (cp_parser
* parser
)
10927 tree statement
= error_mark_node
;
10930 unsigned char in_statement
;
10932 /* Peek at the next token. */
10933 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10935 return error_mark_node
;
10937 /* See what kind of keyword it is. */
10938 keyword
= token
->keyword
;
10942 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
10943 switch (in_statement
)
10946 error_at (token
->location
, "break statement not within loop or switch");
10949 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
10950 || in_statement
== IN_ITERATION_STMT
);
10951 statement
= finish_break_stmt ();
10952 if (in_statement
== IN_ITERATION_STMT
)
10953 break_maybe_infinite_loop ();
10956 error_at (token
->location
, "invalid exit from OpenMP structured block");
10959 error_at (token
->location
, "break statement used with OpenMP for loop");
10961 case IN_CILK_SIMD_FOR
:
10962 error_at (token
->location
, "break statement used with Cilk Plus for loop");
10965 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10969 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
10972 error_at (token
->location
, "continue statement not within a loop");
10974 case IN_CILK_SIMD_FOR
:
10975 error_at (token
->location
,
10976 "continue statement within %<#pragma simd%> loop body");
10977 /* Fall through. */
10978 case IN_ITERATION_STMT
:
10980 statement
= finish_continue_stmt ();
10983 error_at (token
->location
, "invalid exit from OpenMP structured block");
10986 gcc_unreachable ();
10988 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10994 bool expr_non_constant_p
;
10996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10998 cp_lexer_set_source_position (parser
->lexer
);
10999 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11000 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11002 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11003 expr
= cp_parser_expression (parser
);
11005 /* If the next token is a `;', then there is no
11008 /* Build the return-statement. */
11009 statement
= finish_return_stmt (expr
);
11010 /* Look for the final `;'. */
11011 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11016 if (parser
->in_function_body
11017 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11018 error ("%<goto%> in %<constexpr%> function");
11020 /* Create the goto-statement. */
11021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11023 /* Issue a warning about this use of a GNU extension. */
11024 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11025 /* Consume the '*' token. */
11026 cp_lexer_consume_token (parser
->lexer
);
11027 /* Parse the dependent expression. */
11028 finish_goto_stmt (cp_parser_expression (parser
));
11031 finish_goto_stmt (cp_parser_identifier (parser
));
11032 /* Look for the final `;'. */
11033 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11037 cp_parser_error (parser
, "expected jump-statement");
11044 /* Parse a declaration-statement.
11046 declaration-statement:
11047 block-declaration */
11050 cp_parser_declaration_statement (cp_parser
* parser
)
11054 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11055 p
= obstack_alloc (&declarator_obstack
, 0);
11057 /* Parse the block-declaration. */
11058 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11060 /* Free any declarators allocated. */
11061 obstack_free (&declarator_obstack
, p
);
11064 /* Some dependent statements (like `if (cond) statement'), are
11065 implicitly in their own scope. In other words, if the statement is
11066 a single statement (as opposed to a compound-statement), it is
11067 none-the-less treated as if it were enclosed in braces. Any
11068 declarations appearing in the dependent statement are out of scope
11069 after control passes that point. This function parses a statement,
11070 but ensures that is in its own scope, even if it is not a
11071 compound-statement.
11073 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11074 is a (possibly labeled) if statement which is not enclosed in
11075 braces and has an else clause. This is used to implement
11078 Returns the new statement. */
11081 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
11088 /* Mark if () ; with a special NOP_EXPR. */
11089 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11091 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11092 cp_lexer_consume_token (parser
->lexer
);
11093 statement
= add_stmt (build_empty_stmt (loc
));
11095 /* if a compound is opened, we simply parse the statement directly. */
11096 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11097 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11098 /* If the token is not a `{', then we must take special action. */
11101 /* Create a compound-statement. */
11102 statement
= begin_compound_stmt (0);
11103 /* Parse the dependent-statement. */
11104 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11105 /* Finish the dummy compound-statement. */
11106 finish_compound_stmt (statement
);
11109 /* Return the statement. */
11113 /* For some dependent statements (like `while (cond) statement'), we
11114 have already created a scope. Therefore, even if the dependent
11115 statement is a compound-statement, we do not want to create another
11119 cp_parser_already_scoped_statement (cp_parser
* parser
)
11121 /* If the token is a `{', then we must take special action. */
11122 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11123 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11126 /* Avoid calling cp_parser_compound_statement, so that we
11127 don't create a new scope. Do everything else by hand. */
11128 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11129 /* If the next keyword is `__label__' we have a label declaration. */
11130 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11131 cp_parser_label_declaration (parser
);
11132 /* Parse an (optional) statement-seq. */
11133 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11134 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11138 /* Declarations [gram.dcl.dcl] */
11140 /* Parse an optional declaration-sequence.
11144 declaration-seq declaration */
11147 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11153 token
= cp_lexer_peek_token (parser
->lexer
);
11155 if (token
->type
== CPP_CLOSE_BRACE
11156 || token
->type
== CPP_EOF
11157 || token
->type
== CPP_PRAGMA_EOL
)
11160 if (token
->type
== CPP_SEMICOLON
)
11162 /* A declaration consisting of a single semicolon is
11163 invalid. Allow it unless we're being pedantic. */
11164 cp_lexer_consume_token (parser
->lexer
);
11165 if (!in_system_header_at (input_location
))
11166 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11170 /* If we're entering or exiting a region that's implicitly
11171 extern "C", modify the lang context appropriately. */
11172 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11174 push_lang_context (lang_name_c
);
11175 parser
->implicit_extern_c
= true;
11177 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11179 pop_lang_context ();
11180 parser
->implicit_extern_c
= false;
11183 if (token
->type
== CPP_PRAGMA
)
11185 /* A top-level declaration can consist solely of a #pragma.
11186 A nested declaration cannot, so this is done here and not
11187 in cp_parser_declaration. (A #pragma at block scope is
11188 handled in cp_parser_statement.) */
11189 cp_parser_pragma (parser
, pragma_external
);
11193 /* Parse the declaration itself. */
11194 cp_parser_declaration (parser
);
11198 /* Parse a declaration.
11202 function-definition
11203 template-declaration
11204 explicit-instantiation
11205 explicit-specialization
11206 linkage-specification
11207 namespace-definition
11212 __extension__ declaration */
11215 cp_parser_declaration (cp_parser
* parser
)
11219 int saved_pedantic
;
11221 tree attributes
= NULL_TREE
;
11223 /* Check for the `__extension__' keyword. */
11224 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11226 /* Parse the qualified declaration. */
11227 cp_parser_declaration (parser
);
11228 /* Restore the PEDANTIC flag. */
11229 pedantic
= saved_pedantic
;
11234 /* Try to figure out what kind of declaration is present. */
11235 token1
= *cp_lexer_peek_token (parser
->lexer
);
11237 if (token1
.type
!= CPP_EOF
)
11238 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11241 token2
.type
= CPP_EOF
;
11242 token2
.keyword
= RID_MAX
;
11245 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11246 p
= obstack_alloc (&declarator_obstack
, 0);
11248 /* If the next token is `extern' and the following token is a string
11249 literal, then we have a linkage specification. */
11250 if (token1
.keyword
== RID_EXTERN
11251 && cp_parser_is_pure_string_literal (&token2
))
11252 cp_parser_linkage_specification (parser
);
11253 /* If the next token is `template', then we have either a template
11254 declaration, an explicit instantiation, or an explicit
11256 else if (token1
.keyword
== RID_TEMPLATE
)
11258 /* `template <>' indicates a template specialization. */
11259 if (token2
.type
== CPP_LESS
11260 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11261 cp_parser_explicit_specialization (parser
);
11262 /* `template <' indicates a template declaration. */
11263 else if (token2
.type
== CPP_LESS
)
11264 cp_parser_template_declaration (parser
, /*member_p=*/false);
11265 /* Anything else must be an explicit instantiation. */
11267 cp_parser_explicit_instantiation (parser
);
11269 /* If the next token is `export', then we have a template
11271 else if (token1
.keyword
== RID_EXPORT
)
11272 cp_parser_template_declaration (parser
, /*member_p=*/false);
11273 /* If the next token is `extern', 'static' or 'inline' and the one
11274 after that is `template', we have a GNU extended explicit
11275 instantiation directive. */
11276 else if (cp_parser_allow_gnu_extensions_p (parser
)
11277 && (token1
.keyword
== RID_EXTERN
11278 || token1
.keyword
== RID_STATIC
11279 || token1
.keyword
== RID_INLINE
)
11280 && token2
.keyword
== RID_TEMPLATE
)
11281 cp_parser_explicit_instantiation (parser
);
11282 /* If the next token is `namespace', check for a named or unnamed
11283 namespace definition. */
11284 else if (token1
.keyword
== RID_NAMESPACE
11285 && (/* A named namespace definition. */
11286 (token2
.type
== CPP_NAME
11287 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11289 /* An unnamed namespace definition. */
11290 || token2
.type
== CPP_OPEN_BRACE
11291 || token2
.keyword
== RID_ATTRIBUTE
))
11292 cp_parser_namespace_definition (parser
);
11293 /* An inline (associated) namespace definition. */
11294 else if (token1
.keyword
== RID_INLINE
11295 && token2
.keyword
== RID_NAMESPACE
)
11296 cp_parser_namespace_definition (parser
);
11297 /* Objective-C++ declaration/definition. */
11298 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11299 cp_parser_objc_declaration (parser
, NULL_TREE
);
11300 else if (c_dialect_objc ()
11301 && token1
.keyword
== RID_ATTRIBUTE
11302 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11303 cp_parser_objc_declaration (parser
, attributes
);
11304 /* We must have either a block declaration or a function
11307 /* Try to parse a block-declaration, or a function-definition. */
11308 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11310 /* Free any declarators allocated. */
11311 obstack_free (&declarator_obstack
, p
);
11314 /* Parse a block-declaration.
11319 namespace-alias-definition
11326 __extension__ block-declaration
11331 static_assert-declaration
11333 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11334 part of a declaration-statement. */
11337 cp_parser_block_declaration (cp_parser
*parser
,
11341 int saved_pedantic
;
11343 /* Check for the `__extension__' keyword. */
11344 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11346 /* Parse the qualified declaration. */
11347 cp_parser_block_declaration (parser
, statement_p
);
11348 /* Restore the PEDANTIC flag. */
11349 pedantic
= saved_pedantic
;
11354 /* Peek at the next token to figure out which kind of declaration is
11356 token1
= cp_lexer_peek_token (parser
->lexer
);
11358 /* If the next keyword is `asm', we have an asm-definition. */
11359 if (token1
->keyword
== RID_ASM
)
11362 cp_parser_commit_to_tentative_parse (parser
);
11363 cp_parser_asm_definition (parser
);
11365 /* If the next keyword is `namespace', we have a
11366 namespace-alias-definition. */
11367 else if (token1
->keyword
== RID_NAMESPACE
)
11368 cp_parser_namespace_alias_definition (parser
);
11369 /* If the next keyword is `using', we have a
11370 using-declaration, a using-directive, or an alias-declaration. */
11371 else if (token1
->keyword
== RID_USING
)
11376 cp_parser_commit_to_tentative_parse (parser
);
11377 /* If the token after `using' is `namespace', then we have a
11378 using-directive. */
11379 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11380 if (token2
->keyword
== RID_NAMESPACE
)
11381 cp_parser_using_directive (parser
);
11382 /* If the second token after 'using' is '=', then we have an
11383 alias-declaration. */
11384 else if (cxx_dialect
>= cxx11
11385 && token2
->type
== CPP_NAME
11386 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11387 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11388 cp_parser_alias_declaration (parser
);
11389 /* Otherwise, it's a using-declaration. */
11391 cp_parser_using_declaration (parser
,
11392 /*access_declaration_p=*/false);
11394 /* If the next keyword is `__label__' we have a misplaced label
11396 else if (token1
->keyword
== RID_LABEL
)
11398 cp_lexer_consume_token (parser
->lexer
);
11399 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11400 cp_parser_skip_to_end_of_statement (parser
);
11401 /* If the next token is now a `;', consume it. */
11402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11403 cp_lexer_consume_token (parser
->lexer
);
11405 /* If the next token is `static_assert' we have a static assertion. */
11406 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11407 cp_parser_static_assert (parser
, /*member_p=*/false);
11408 /* Anything else must be a simple-declaration. */
11410 cp_parser_simple_declaration (parser
, !statement_p
,
11411 /*maybe_range_for_decl*/NULL
);
11414 /* Parse a simple-declaration.
11416 simple-declaration:
11417 decl-specifier-seq [opt] init-declarator-list [opt] ;
11419 init-declarator-list:
11421 init-declarator-list , init-declarator
11423 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11424 function-definition as a simple-declaration.
11426 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11427 parsed declaration if it is an uninitialized single declarator not followed
11428 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11429 if present, will not be consumed. */
11432 cp_parser_simple_declaration (cp_parser
* parser
,
11433 bool function_definition_allowed_p
,
11434 tree
*maybe_range_for_decl
)
11436 cp_decl_specifier_seq decl_specifiers
;
11437 int declares_class_or_enum
;
11438 bool saw_declarator
;
11440 if (maybe_range_for_decl
)
11441 *maybe_range_for_decl
= NULL_TREE
;
11443 /* Defer access checks until we know what is being declared; the
11444 checks for names appearing in the decl-specifier-seq should be
11445 done as if we were in the scope of the thing being declared. */
11446 push_deferring_access_checks (dk_deferred
);
11448 /* Parse the decl-specifier-seq. We have to keep track of whether
11449 or not the decl-specifier-seq declares a named class or
11450 enumeration type, since that is the only case in which the
11451 init-declarator-list is allowed to be empty.
11455 In a simple-declaration, the optional init-declarator-list can be
11456 omitted only when declaring a class or enumeration, that is when
11457 the decl-specifier-seq contains either a class-specifier, an
11458 elaborated-type-specifier, or an enum-specifier. */
11459 cp_parser_decl_specifier_seq (parser
,
11460 CP_PARSER_FLAGS_OPTIONAL
,
11462 &declares_class_or_enum
);
11463 /* We no longer need to defer access checks. */
11464 stop_deferring_access_checks ();
11466 /* In a block scope, a valid declaration must always have a
11467 decl-specifier-seq. By not trying to parse declarators, we can
11468 resolve the declaration/expression ambiguity more quickly. */
11469 if (!function_definition_allowed_p
11470 && !decl_specifiers
.any_specifiers_p
)
11472 cp_parser_error (parser
, "expected declaration");
11476 /* If the next two tokens are both identifiers, the code is
11477 erroneous. The usual cause of this situation is code like:
11481 where "T" should name a type -- but does not. */
11482 if (!decl_specifiers
.any_type_specifiers_p
11483 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11485 /* If parsing tentatively, we should commit; we really are
11486 looking at a declaration. */
11487 cp_parser_commit_to_tentative_parse (parser
);
11492 /* If we have seen at least one decl-specifier, and the next token
11493 is not a parenthesis, then we must be looking at a declaration.
11494 (After "int (" we might be looking at a functional cast.) */
11495 if (decl_specifiers
.any_specifiers_p
11496 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11497 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11498 && !cp_parser_error_occurred (parser
))
11499 cp_parser_commit_to_tentative_parse (parser
);
11501 /* Keep going until we hit the `;' at the end of the simple
11503 saw_declarator
= false;
11504 while (cp_lexer_next_token_is_not (parser
->lexer
,
11508 bool function_definition_p
;
11511 if (saw_declarator
)
11513 /* If we are processing next declarator, coma is expected */
11514 token
= cp_lexer_peek_token (parser
->lexer
);
11515 gcc_assert (token
->type
== CPP_COMMA
);
11516 cp_lexer_consume_token (parser
->lexer
);
11517 if (maybe_range_for_decl
)
11518 *maybe_range_for_decl
= error_mark_node
;
11521 saw_declarator
= true;
11523 /* Parse the init-declarator. */
11524 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11526 function_definition_allowed_p
,
11527 /*member_p=*/false,
11528 declares_class_or_enum
,
11529 &function_definition_p
,
11530 maybe_range_for_decl
);
11531 /* If an error occurred while parsing tentatively, exit quickly.
11532 (That usually happens when in the body of a function; each
11533 statement is treated as a declaration-statement until proven
11535 if (cp_parser_error_occurred (parser
))
11537 /* Handle function definitions specially. */
11538 if (function_definition_p
)
11540 /* If the next token is a `,', then we are probably
11541 processing something like:
11545 which is erroneous. */
11546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11549 error_at (token
->location
,
11551 " declarations and function-definitions is forbidden");
11553 /* Otherwise, we're done with the list of declarators. */
11556 pop_deferring_access_checks ();
11560 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11561 *maybe_range_for_decl
= decl
;
11562 /* The next token should be either a `,' or a `;'. */
11563 token
= cp_lexer_peek_token (parser
->lexer
);
11564 /* If it's a `,', there are more declarators to come. */
11565 if (token
->type
== CPP_COMMA
)
11566 /* will be consumed next time around */;
11567 /* If it's a `;', we are done. */
11568 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11570 /* Anything else is an error. */
11573 /* If we have already issued an error message we don't need
11574 to issue another one. */
11575 if (decl
!= error_mark_node
11576 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11577 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11578 /* Skip tokens until we reach the end of the statement. */
11579 cp_parser_skip_to_end_of_statement (parser
);
11580 /* If the next token is now a `;', consume it. */
11581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11582 cp_lexer_consume_token (parser
->lexer
);
11585 /* After the first time around, a function-definition is not
11586 allowed -- even if it was OK at first. For example:
11591 function_definition_allowed_p
= false;
11594 /* Issue an error message if no declarators are present, and the
11595 decl-specifier-seq does not itself declare a class or
11596 enumeration: [dcl.dcl]/3. */
11597 if (!saw_declarator
)
11599 if (cp_parser_declares_only_class_p (parser
))
11601 if (!declares_class_or_enum
11602 && decl_specifiers
.type
11603 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11604 /* Ensure an error is issued anyway when finish_decltype_type,
11605 called via cp_parser_decl_specifier_seq, returns a class or
11606 an enumeration (c++/51786). */
11607 decl_specifiers
.type
= NULL_TREE
;
11608 shadow_tag (&decl_specifiers
);
11610 /* Perform any deferred access checks. */
11611 perform_deferred_access_checks (tf_warning_or_error
);
11614 /* Consume the `;'. */
11615 if (!maybe_range_for_decl
)
11616 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11619 pop_deferring_access_checks ();
11622 /* Parse a decl-specifier-seq.
11624 decl-specifier-seq:
11625 decl-specifier-seq [opt] decl-specifier
11626 decl-specifier attribute-specifier-seq [opt] (C++11)
11629 storage-class-specifier
11640 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11642 The parser flags FLAGS is used to control type-specifier parsing.
11644 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11647 1: one of the decl-specifiers is an elaborated-type-specifier
11648 (i.e., a type declaration)
11649 2: one of the decl-specifiers is an enum-specifier or a
11650 class-specifier (i.e., a type definition)
11655 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11656 cp_parser_flags flags
,
11657 cp_decl_specifier_seq
*decl_specs
,
11658 int* declares_class_or_enum
)
11660 bool constructor_possible_p
= !parser
->in_declarator_p
;
11661 bool found_decl_spec
= false;
11662 cp_token
*start_token
= NULL
;
11665 /* Clear DECL_SPECS. */
11666 clear_decl_specs (decl_specs
);
11668 /* Assume no class or enumeration type is declared. */
11669 *declares_class_or_enum
= 0;
11671 /* Keep reading specifiers until there are no more to read. */
11674 bool constructor_p
;
11678 /* Peek at the next token. */
11679 token
= cp_lexer_peek_token (parser
->lexer
);
11681 /* Save the first token of the decl spec list for error
11684 start_token
= token
;
11685 /* Handle attributes. */
11686 if (cp_next_tokens_can_be_attribute_p (parser
))
11688 /* Parse the attributes. */
11689 tree attrs
= cp_parser_attributes_opt (parser
);
11691 /* In a sequence of declaration specifiers, c++11 attributes
11692 appertain to the type that precede them. In that case
11695 The attribute-specifier-seq affects the type only for
11696 the declaration it appears in, not other declarations
11697 involving the same type.
11699 But for now let's force the user to position the
11700 attribute either at the beginning of the declaration or
11701 after the declarator-id, which would clearly mean that it
11702 applies to the declarator. */
11703 if (cxx11_attribute_p (attrs
))
11705 if (!found_decl_spec
)
11706 /* The c++11 attribute is at the beginning of the
11707 declaration. It appertains to the entity being
11711 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11713 /* This is an attribute following a
11714 class-specifier. */
11715 if (decl_specs
->type_definition_p
)
11716 warn_misplaced_attr_for_class_type (token
->location
,
11722 decl_specs
->std_attributes
11723 = chainon (decl_specs
->std_attributes
,
11725 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11726 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11732 decl_specs
->attributes
11733 = chainon (decl_specs
->attributes
,
11735 if (decl_specs
->locations
[ds_attribute
] == 0)
11736 decl_specs
->locations
[ds_attribute
] = token
->location
;
11739 /* Assume we will find a decl-specifier keyword. */
11740 found_decl_spec
= true;
11741 /* If the next token is an appropriate keyword, we can simply
11742 add it to the list. */
11743 switch (token
->keyword
)
11749 if (!at_class_scope_p ())
11751 error_at (token
->location
, "%<friend%> used outside of class");
11752 cp_lexer_purge_token (parser
->lexer
);
11757 /* Consume the token. */
11758 cp_lexer_consume_token (parser
->lexer
);
11762 case RID_CONSTEXPR
:
11764 cp_lexer_consume_token (parser
->lexer
);
11767 /* function-specifier:
11774 cp_parser_function_specifier_opt (parser
, decl_specs
);
11781 /* Consume the token. */
11782 cp_lexer_consume_token (parser
->lexer
);
11783 /* A constructor declarator cannot appear in a typedef. */
11784 constructor_possible_p
= false;
11785 /* The "typedef" keyword can only occur in a declaration; we
11786 may as well commit at this point. */
11787 cp_parser_commit_to_tentative_parse (parser
);
11789 if (decl_specs
->storage_class
!= sc_none
)
11790 decl_specs
->conflicting_specifiers_p
= true;
11793 /* storage-class-specifier:
11803 if (cxx_dialect
== cxx98
)
11805 /* Consume the token. */
11806 cp_lexer_consume_token (parser
->lexer
);
11808 /* Complain about `auto' as a storage specifier, if
11809 we're complaining about C++0x compatibility. */
11810 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11811 " changes meaning in C++11; please remove it");
11813 /* Set the storage class anyway. */
11814 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11818 /* C++0x auto type-specifier. */
11819 found_decl_spec
= false;
11826 /* Consume the token. */
11827 cp_lexer_consume_token (parser
->lexer
);
11828 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11832 /* Consume the token. */
11834 cp_lexer_consume_token (parser
->lexer
);
11838 /* We did not yet find a decl-specifier yet. */
11839 found_decl_spec
= false;
11843 if (found_decl_spec
11844 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11845 && token
->keyword
!= RID_CONSTEXPR
)
11846 error ("decl-specifier invalid in condition");
11849 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11851 /* Constructors are a special case. The `S' in `S()' is not a
11852 decl-specifier; it is the beginning of the declarator. */
11854 = (!found_decl_spec
11855 && constructor_possible_p
11856 && (cp_parser_constructor_declarator_p
11857 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11859 /* If we don't have a DECL_SPEC yet, then we must be looking at
11860 a type-specifier. */
11861 if (!found_decl_spec
&& !constructor_p
)
11863 int decl_spec_declares_class_or_enum
;
11864 bool is_cv_qualifier
;
11868 = cp_parser_type_specifier (parser
, flags
,
11870 /*is_declaration=*/true,
11871 &decl_spec_declares_class_or_enum
,
11873 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11875 /* If this type-specifier referenced a user-defined type
11876 (a typedef, class-name, etc.), then we can't allow any
11877 more such type-specifiers henceforth.
11881 The longest sequence of decl-specifiers that could
11882 possibly be a type name is taken as the
11883 decl-specifier-seq of a declaration. The sequence shall
11884 be self-consistent as described below.
11888 As a general rule, at most one type-specifier is allowed
11889 in the complete decl-specifier-seq of a declaration. The
11890 only exceptions are the following:
11892 -- const or volatile can be combined with any other
11895 -- signed or unsigned can be combined with char, long,
11903 void g (const int Pc);
11905 Here, Pc is *not* part of the decl-specifier seq; it's
11906 the declarator. Therefore, once we see a type-specifier
11907 (other than a cv-qualifier), we forbid any additional
11908 user-defined types. We *do* still allow things like `int
11909 int' to be considered a decl-specifier-seq, and issue the
11910 error message later. */
11911 if (type_spec
&& !is_cv_qualifier
)
11912 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11913 /* A constructor declarator cannot follow a type-specifier. */
11916 constructor_possible_p
= false;
11917 found_decl_spec
= true;
11918 if (!is_cv_qualifier
)
11919 decl_specs
->any_type_specifiers_p
= true;
11923 /* If we still do not have a DECL_SPEC, then there are no more
11924 decl-specifiers. */
11925 if (!found_decl_spec
)
11928 decl_specs
->any_specifiers_p
= true;
11929 /* After we see one decl-specifier, further decl-specifiers are
11930 always optional. */
11931 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
11934 /* Don't allow a friend specifier with a class definition. */
11935 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
11936 && (*declares_class_or_enum
& 2))
11937 error_at (decl_specs
->locations
[ds_friend
],
11938 "class definition may not be declared a friend");
11941 /* Parse an (optional) storage-class-specifier.
11943 storage-class-specifier:
11952 storage-class-specifier:
11955 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11958 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
11960 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
11963 if (cxx_dialect
!= cxx98
)
11965 /* Fall through for C++98. */
11972 /* Consume the token. */
11973 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
11980 /* Parse an (optional) function-specifier.
11982 function-specifier:
11987 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11988 Updates DECL_SPECS, if it is non-NULL. */
11991 cp_parser_function_specifier_opt (cp_parser
* parser
,
11992 cp_decl_specifier_seq
*decl_specs
)
11994 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11995 switch (token
->keyword
)
11998 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12002 /* 14.5.2.3 [temp.mem]
12004 A member function template shall not be virtual. */
12005 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12006 error_at (token
->location
, "templates may not be %<virtual%>");
12008 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12012 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12019 /* Consume the token. */
12020 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12023 /* Parse a linkage-specification.
12025 linkage-specification:
12026 extern string-literal { declaration-seq [opt] }
12027 extern string-literal declaration */
12030 cp_parser_linkage_specification (cp_parser
* parser
)
12034 /* Look for the `extern' keyword. */
12035 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12037 /* Look for the string-literal. */
12038 linkage
= cp_parser_string_literal (parser
, false, false);
12040 /* Transform the literal into an identifier. If the literal is a
12041 wide-character string, or contains embedded NULs, then we can't
12042 handle it as the user wants. */
12043 if (strlen (TREE_STRING_POINTER (linkage
))
12044 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12046 cp_parser_error (parser
, "invalid linkage-specification");
12047 /* Assume C++ linkage. */
12048 linkage
= lang_name_cplusplus
;
12051 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12053 /* We're now using the new linkage. */
12054 push_lang_context (linkage
);
12056 /* If the next token is a `{', then we're using the first
12058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12060 cp_ensure_no_omp_declare_simd (parser
);
12062 /* Consume the `{' token. */
12063 cp_lexer_consume_token (parser
->lexer
);
12064 /* Parse the declarations. */
12065 cp_parser_declaration_seq_opt (parser
);
12066 /* Look for the closing `}'. */
12067 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12069 /* Otherwise, there's just one declaration. */
12072 bool saved_in_unbraced_linkage_specification_p
;
12074 saved_in_unbraced_linkage_specification_p
12075 = parser
->in_unbraced_linkage_specification_p
;
12076 parser
->in_unbraced_linkage_specification_p
= true;
12077 cp_parser_declaration (parser
);
12078 parser
->in_unbraced_linkage_specification_p
12079 = saved_in_unbraced_linkage_specification_p
;
12082 /* We're done with the linkage-specification. */
12083 pop_lang_context ();
12086 /* Parse a static_assert-declaration.
12088 static_assert-declaration:
12089 static_assert ( constant-expression , string-literal ) ;
12091 If MEMBER_P, this static_assert is a class member. */
12094 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12099 location_t saved_loc
;
12102 /* Peek at the `static_assert' token so we can keep track of exactly
12103 where the static assertion started. */
12104 token
= cp_lexer_peek_token (parser
->lexer
);
12105 saved_loc
= token
->location
;
12107 /* Look for the `static_assert' keyword. */
12108 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12112 /* We know we are in a static assertion; commit to any tentative
12114 if (cp_parser_parsing_tentatively (parser
))
12115 cp_parser_commit_to_tentative_parse (parser
);
12117 /* Parse the `(' starting the static assertion condition. */
12118 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12120 /* Parse the constant-expression. Allow a non-constant expression
12121 here in order to give better diagnostics in finish_static_assert. */
12123 cp_parser_constant_expression (parser
,
12124 /*allow_non_constant_p=*/true,
12125 /*non_constant_p=*/&dummy
);
12127 /* Parse the separating `,'. */
12128 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12130 /* Parse the string-literal message. */
12131 message
= cp_parser_string_literal (parser
,
12132 /*translate=*/false,
12135 /* A `)' completes the static assertion. */
12136 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12137 cp_parser_skip_to_closing_parenthesis (parser
,
12138 /*recovering=*/true,
12139 /*or_comma=*/false,
12140 /*consume_paren=*/true);
12142 /* A semicolon terminates the declaration. */
12143 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12145 /* Complete the static assertion, which may mean either processing
12146 the static assert now or saving it for template instantiation. */
12147 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12150 /* Parse the expression in decltype ( expression ). */
12153 cp_parser_decltype_expr (cp_parser
*parser
,
12154 bool &id_expression_or_member_access_p
)
12156 cp_token
*id_expr_start_token
;
12159 /* First, try parsing an id-expression. */
12160 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12161 cp_parser_parse_tentatively (parser
);
12162 expr
= cp_parser_id_expression (parser
,
12163 /*template_keyword_p=*/false,
12164 /*check_dependency_p=*/true,
12165 /*template_p=*/NULL
,
12166 /*declarator_p=*/false,
12167 /*optional_p=*/false);
12169 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12171 bool non_integral_constant_expression_p
= false;
12172 tree id_expression
= expr
;
12174 const char *error_msg
;
12176 if (identifier_p (expr
))
12177 /* Lookup the name we got back from the id-expression. */
12178 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12179 id_expr_start_token
->location
);
12182 && expr
!= error_mark_node
12183 && TREE_CODE (expr
) != TEMPLATE_ID_EXPR
12184 && TREE_CODE (expr
) != TYPE_DECL
12185 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12186 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12187 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12189 /* Complete lookup of the id-expression. */
12190 expr
= (finish_id_expression
12191 (id_expression
, expr
, parser
->scope
, &idk
,
12192 /*integral_constant_expression_p=*/false,
12193 /*allow_non_integral_constant_expression_p=*/true,
12194 &non_integral_constant_expression_p
,
12195 /*template_p=*/false,
12197 /*address_p=*/false,
12198 /*template_arg_p=*/false,
12200 id_expr_start_token
->location
));
12202 if (expr
== error_mark_node
)
12203 /* We found an id-expression, but it was something that we
12204 should not have found. This is an error, not something
12205 we can recover from, so note that we found an
12206 id-expression and we'll recover as gracefully as
12208 id_expression_or_member_access_p
= true;
12212 && expr
!= error_mark_node
12213 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12214 /* We have an id-expression. */
12215 id_expression_or_member_access_p
= true;
12218 if (!id_expression_or_member_access_p
)
12220 /* Abort the id-expression parse. */
12221 cp_parser_abort_tentative_parse (parser
);
12223 /* Parsing tentatively, again. */
12224 cp_parser_parse_tentatively (parser
);
12226 /* Parse a class member access. */
12227 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12228 /*cast_p=*/false, /*decltype*/true,
12229 /*member_access_only_p=*/true, NULL
);
12232 && expr
!= error_mark_node
12233 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12234 /* We have an id-expression. */
12235 id_expression_or_member_access_p
= true;
12238 if (id_expression_or_member_access_p
)
12239 /* We have parsed the complete id-expression or member access. */
12240 cp_parser_parse_definitely (parser
);
12243 /* Abort our attempt to parse an id-expression or member access
12245 cp_parser_abort_tentative_parse (parser
);
12247 /* Parse a full expression. */
12248 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12249 /*decltype_p=*/true);
12255 /* Parse a `decltype' type. Returns the type.
12257 simple-type-specifier:
12258 decltype ( expression )
12260 decltype ( auto ) */
12263 cp_parser_decltype (cp_parser
*parser
)
12266 bool id_expression_or_member_access_p
= false;
12267 const char *saved_message
;
12268 bool saved_integral_constant_expression_p
;
12269 bool saved_non_integral_constant_expression_p
;
12270 bool saved_greater_than_is_operator_p
;
12271 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12273 if (start_token
->type
== CPP_DECLTYPE
)
12275 /* Already parsed. */
12276 cp_lexer_consume_token (parser
->lexer
);
12277 return start_token
->u
.value
;
12280 /* Look for the `decltype' token. */
12281 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12282 return error_mark_node
;
12284 /* Parse the opening `('. */
12285 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12286 return error_mark_node
;
12288 /* decltype (auto) */
12289 if (cxx_dialect
>= cxx14
12290 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12292 cp_lexer_consume_token (parser
->lexer
);
12293 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12294 return error_mark_node
;
12295 expr
= make_decltype_auto ();
12296 AUTO_IS_DECLTYPE (expr
) = true;
12300 /* Types cannot be defined in a `decltype' expression. Save away the
12302 saved_message
= parser
->type_definition_forbidden_message
;
12304 /* And create the new one. */
12305 parser
->type_definition_forbidden_message
12306 = G_("types may not be defined in %<decltype%> expressions");
12308 /* The restrictions on constant-expressions do not apply inside
12309 decltype expressions. */
12310 saved_integral_constant_expression_p
12311 = parser
->integral_constant_expression_p
;
12312 saved_non_integral_constant_expression_p
12313 = parser
->non_integral_constant_expression_p
;
12314 parser
->integral_constant_expression_p
= false;
12316 /* Within a parenthesized expression, a `>' token is always
12317 the greater-than operator. */
12318 saved_greater_than_is_operator_p
12319 = parser
->greater_than_is_operator_p
;
12320 parser
->greater_than_is_operator_p
= true;
12322 /* Do not actually evaluate the expression. */
12323 ++cp_unevaluated_operand
;
12325 /* Do not warn about problems with the expression. */
12326 ++c_inhibit_evaluation_warnings
;
12328 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12330 /* Go back to evaluating expressions. */
12331 --cp_unevaluated_operand
;
12332 --c_inhibit_evaluation_warnings
;
12334 /* The `>' token might be the end of a template-id or
12335 template-parameter-list now. */
12336 parser
->greater_than_is_operator_p
12337 = saved_greater_than_is_operator_p
;
12339 /* Restore the old message and the integral constant expression
12341 parser
->type_definition_forbidden_message
= saved_message
;
12342 parser
->integral_constant_expression_p
12343 = saved_integral_constant_expression_p
;
12344 parser
->non_integral_constant_expression_p
12345 = saved_non_integral_constant_expression_p
;
12347 /* Parse to the closing `)'. */
12348 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12350 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12351 /*consume_paren=*/true);
12352 return error_mark_node
;
12355 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12356 tf_warning_or_error
);
12359 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12361 start_token
->type
= CPP_DECLTYPE
;
12362 start_token
->u
.value
= expr
;
12363 start_token
->keyword
= RID_MAX
;
12364 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12369 /* Special member functions [gram.special] */
12371 /* Parse a conversion-function-id.
12373 conversion-function-id:
12374 operator conversion-type-id
12376 Returns an IDENTIFIER_NODE representing the operator. */
12379 cp_parser_conversion_function_id (cp_parser
* parser
)
12383 tree saved_qualifying_scope
;
12384 tree saved_object_scope
;
12385 tree pushed_scope
= NULL_TREE
;
12387 /* Look for the `operator' token. */
12388 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12389 return error_mark_node
;
12390 /* When we parse the conversion-type-id, the current scope will be
12391 reset. However, we need that information in able to look up the
12392 conversion function later, so we save it here. */
12393 saved_scope
= parser
->scope
;
12394 saved_qualifying_scope
= parser
->qualifying_scope
;
12395 saved_object_scope
= parser
->object_scope
;
12396 /* We must enter the scope of the class so that the names of
12397 entities declared within the class are available in the
12398 conversion-type-id. For example, consider:
12405 S::operator I() { ... }
12407 In order to see that `I' is a type-name in the definition, we
12408 must be in the scope of `S'. */
12410 pushed_scope
= push_scope (saved_scope
);
12411 /* Parse the conversion-type-id. */
12412 type
= cp_parser_conversion_type_id (parser
);
12413 /* Leave the scope of the class, if any. */
12415 pop_scope (pushed_scope
);
12416 /* Restore the saved scope. */
12417 parser
->scope
= saved_scope
;
12418 parser
->qualifying_scope
= saved_qualifying_scope
;
12419 parser
->object_scope
= saved_object_scope
;
12420 /* If the TYPE is invalid, indicate failure. */
12421 if (type
== error_mark_node
)
12422 return error_mark_node
;
12423 return mangle_conv_op_name_for_type (type
);
12426 /* Parse a conversion-type-id:
12428 conversion-type-id:
12429 type-specifier-seq conversion-declarator [opt]
12431 Returns the TYPE specified. */
12434 cp_parser_conversion_type_id (cp_parser
* parser
)
12437 cp_decl_specifier_seq type_specifiers
;
12438 cp_declarator
*declarator
;
12439 tree type_specified
;
12440 const char *saved_message
;
12442 /* Parse the attributes. */
12443 attributes
= cp_parser_attributes_opt (parser
);
12445 saved_message
= parser
->type_definition_forbidden_message
;
12446 parser
->type_definition_forbidden_message
12447 = G_("types may not be defined in a conversion-type-id");
12449 /* Parse the type-specifiers. */
12450 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12451 /*is_trailing_return=*/false,
12454 parser
->type_definition_forbidden_message
= saved_message
;
12456 /* If that didn't work, stop. */
12457 if (type_specifiers
.type
== error_mark_node
)
12458 return error_mark_node
;
12459 /* Parse the conversion-declarator. */
12460 declarator
= cp_parser_conversion_declarator_opt (parser
);
12462 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12463 /*initialized=*/0, &attributes
);
12465 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12467 /* Don't give this error when parsing tentatively. This happens to
12468 work because we always parse this definitively once. */
12469 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12470 && type_uses_auto (type_specified
))
12472 if (cxx_dialect
< cxx14
)
12474 error ("invalid use of %<auto%> in conversion operator");
12475 return error_mark_node
;
12477 else if (template_parm_scope_p ())
12478 warning (0, "use of %<auto%> in member template "
12479 "conversion operator can never be deduced");
12482 return type_specified
;
12485 /* Parse an (optional) conversion-declarator.
12487 conversion-declarator:
12488 ptr-operator conversion-declarator [opt]
12492 static cp_declarator
*
12493 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12495 enum tree_code code
;
12496 tree class_type
, std_attributes
= NULL_TREE
;
12497 cp_cv_quals cv_quals
;
12499 /* We don't know if there's a ptr-operator next, or not. */
12500 cp_parser_parse_tentatively (parser
);
12501 /* Try the ptr-operator. */
12502 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12504 /* If it worked, look for more conversion-declarators. */
12505 if (cp_parser_parse_definitely (parser
))
12507 cp_declarator
*declarator
;
12509 /* Parse another optional declarator. */
12510 declarator
= cp_parser_conversion_declarator_opt (parser
);
12512 declarator
= cp_parser_make_indirect_declarator
12513 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12521 /* Parse an (optional) ctor-initializer.
12524 : mem-initializer-list
12526 Returns TRUE iff the ctor-initializer was actually present. */
12529 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12531 /* If the next token is not a `:', then there is no
12532 ctor-initializer. */
12533 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12535 /* Do default initialization of any bases and members. */
12536 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12537 finish_mem_initializers (NULL_TREE
);
12542 /* Consume the `:' token. */
12543 cp_lexer_consume_token (parser
->lexer
);
12544 /* And the mem-initializer-list. */
12545 cp_parser_mem_initializer_list (parser
);
12550 /* Parse a mem-initializer-list.
12552 mem-initializer-list:
12553 mem-initializer ... [opt]
12554 mem-initializer ... [opt] , mem-initializer-list */
12557 cp_parser_mem_initializer_list (cp_parser
* parser
)
12559 tree mem_initializer_list
= NULL_TREE
;
12560 tree target_ctor
= error_mark_node
;
12561 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12563 /* Let the semantic analysis code know that we are starting the
12564 mem-initializer-list. */
12565 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12566 error_at (token
->location
,
12567 "only constructors take member initializers");
12569 /* Loop through the list. */
12572 tree mem_initializer
;
12574 token
= cp_lexer_peek_token (parser
->lexer
);
12575 /* Parse the mem-initializer. */
12576 mem_initializer
= cp_parser_mem_initializer (parser
);
12577 /* If the next token is a `...', we're expanding member initializers. */
12578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12580 /* Consume the `...'. */
12581 cp_lexer_consume_token (parser
->lexer
);
12583 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12584 can be expanded but members cannot. */
12585 if (mem_initializer
!= error_mark_node
12586 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12588 error_at (token
->location
,
12589 "cannot expand initializer for member %<%D%>",
12590 TREE_PURPOSE (mem_initializer
));
12591 mem_initializer
= error_mark_node
;
12594 /* Construct the pack expansion type. */
12595 if (mem_initializer
!= error_mark_node
)
12596 mem_initializer
= make_pack_expansion (mem_initializer
);
12598 if (target_ctor
!= error_mark_node
12599 && mem_initializer
!= error_mark_node
)
12601 error ("mem-initializer for %qD follows constructor delegation",
12602 TREE_PURPOSE (mem_initializer
));
12603 mem_initializer
= error_mark_node
;
12605 /* Look for a target constructor. */
12606 if (mem_initializer
!= error_mark_node
12607 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12608 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12610 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12611 if (mem_initializer_list
)
12613 error ("constructor delegation follows mem-initializer for %qD",
12614 TREE_PURPOSE (mem_initializer_list
));
12615 mem_initializer
= error_mark_node
;
12617 target_ctor
= mem_initializer
;
12619 /* Add it to the list, unless it was erroneous. */
12620 if (mem_initializer
!= error_mark_node
)
12622 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12623 mem_initializer_list
= mem_initializer
;
12625 /* If the next token is not a `,', we're done. */
12626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12628 /* Consume the `,' token. */
12629 cp_lexer_consume_token (parser
->lexer
);
12632 /* Perform semantic analysis. */
12633 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12634 finish_mem_initializers (mem_initializer_list
);
12637 /* Parse a mem-initializer.
12640 mem-initializer-id ( expression-list [opt] )
12641 mem-initializer-id braced-init-list
12646 ( expression-list [opt] )
12648 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12649 class) or FIELD_DECL (for a non-static data member) to initialize;
12650 the TREE_VALUE is the expression-list. An empty initialization
12651 list is represented by void_list_node. */
12654 cp_parser_mem_initializer (cp_parser
* parser
)
12656 tree mem_initializer_id
;
12657 tree expression_list
;
12659 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12661 /* Find out what is being initialized. */
12662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12664 permerror (token
->location
,
12665 "anachronistic old-style base class initializer");
12666 mem_initializer_id
= NULL_TREE
;
12670 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12671 if (mem_initializer_id
== error_mark_node
)
12672 return mem_initializer_id
;
12674 member
= expand_member_init (mem_initializer_id
);
12675 if (member
&& !DECL_P (member
))
12676 in_base_initializer
= 1;
12678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12680 bool expr_non_constant_p
;
12681 cp_lexer_set_source_position (parser
->lexer
);
12682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12683 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12684 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12685 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12689 vec
<tree
, va_gc
> *vec
;
12690 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12692 /*allow_expansion_p=*/true,
12693 /*non_constant_p=*/NULL
);
12695 return error_mark_node
;
12696 expression_list
= build_tree_list_vec (vec
);
12697 release_tree_vector (vec
);
12700 if (expression_list
== error_mark_node
)
12701 return error_mark_node
;
12702 if (!expression_list
)
12703 expression_list
= void_type_node
;
12705 in_base_initializer
= 0;
12707 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12710 /* Parse a mem-initializer-id.
12712 mem-initializer-id:
12713 :: [opt] nested-name-specifier [opt] class-name
12716 Returns a TYPE indicating the class to be initializer for the first
12717 production. Returns an IDENTIFIER_NODE indicating the data member
12718 to be initialized for the second production. */
12721 cp_parser_mem_initializer_id (cp_parser
* parser
)
12723 bool global_scope_p
;
12724 bool nested_name_specifier_p
;
12725 bool template_p
= false;
12728 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12730 /* `typename' is not allowed in this context ([temp.res]). */
12731 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12733 error_at (token
->location
,
12734 "keyword %<typename%> not allowed in this context (a qualified "
12735 "member initializer is implicitly a type)");
12736 cp_lexer_consume_token (parser
->lexer
);
12738 /* Look for the optional `::' operator. */
12740 = (cp_parser_global_scope_opt (parser
,
12741 /*current_scope_valid_p=*/false)
12743 /* Look for the optional nested-name-specifier. The simplest way to
12748 The keyword `typename' is not permitted in a base-specifier or
12749 mem-initializer; in these contexts a qualified name that
12750 depends on a template-parameter is implicitly assumed to be a
12753 is to assume that we have seen the `typename' keyword at this
12755 nested_name_specifier_p
12756 = (cp_parser_nested_name_specifier_opt (parser
,
12757 /*typename_keyword_p=*/true,
12758 /*check_dependency_p=*/true,
12760 /*is_declaration=*/true)
12762 if (nested_name_specifier_p
)
12763 template_p
= cp_parser_optional_template_keyword (parser
);
12764 /* If there is a `::' operator or a nested-name-specifier, then we
12765 are definitely looking for a class-name. */
12766 if (global_scope_p
|| nested_name_specifier_p
)
12767 return cp_parser_class_name (parser
,
12768 /*typename_keyword_p=*/true,
12769 /*template_keyword_p=*/template_p
,
12771 /*check_dependency_p=*/true,
12772 /*class_head_p=*/false,
12773 /*is_declaration=*/true);
12774 /* Otherwise, we could also be looking for an ordinary identifier. */
12775 cp_parser_parse_tentatively (parser
);
12776 /* Try a class-name. */
12777 id
= cp_parser_class_name (parser
,
12778 /*typename_keyword_p=*/true,
12779 /*template_keyword_p=*/false,
12781 /*check_dependency_p=*/true,
12782 /*class_head_p=*/false,
12783 /*is_declaration=*/true);
12784 /* If we found one, we're done. */
12785 if (cp_parser_parse_definitely (parser
))
12787 /* Otherwise, look for an ordinary identifier. */
12788 return cp_parser_identifier (parser
);
12791 /* Overloading [gram.over] */
12793 /* Parse an operator-function-id.
12795 operator-function-id:
12798 Returns an IDENTIFIER_NODE for the operator which is a
12799 human-readable spelling of the identifier, e.g., `operator +'. */
12802 cp_parser_operator_function_id (cp_parser
* parser
)
12804 /* Look for the `operator' keyword. */
12805 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12806 return error_mark_node
;
12807 /* And then the name of the operator itself. */
12808 return cp_parser_operator (parser
);
12811 /* Return an identifier node for a user-defined literal operator.
12812 The suffix identifier is chained to the operator name identifier. */
12815 cp_literal_operator_id (const char* name
)
12818 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12819 + strlen (name
) + 10);
12820 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12821 identifier
= get_identifier (buffer
);
12826 /* Parse an operator.
12829 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12830 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12831 || ++ -- , ->* -> () []
12838 Returns an IDENTIFIER_NODE for the operator which is a
12839 human-readable spelling of the identifier, e.g., `operator +'. */
12842 cp_parser_operator (cp_parser
* parser
)
12844 tree id
= NULL_TREE
;
12848 /* Peek at the next token. */
12849 token
= cp_lexer_peek_token (parser
->lexer
);
12850 /* Figure out which operator we have. */
12851 switch (token
->type
)
12857 /* The keyword should be either `new' or `delete'. */
12858 if (token
->keyword
== RID_NEW
)
12860 else if (token
->keyword
== RID_DELETE
)
12865 /* Consume the `new' or `delete' token. */
12866 cp_lexer_consume_token (parser
->lexer
);
12868 /* Peek at the next token. */
12869 token
= cp_lexer_peek_token (parser
->lexer
);
12870 /* If it's a `[' token then this is the array variant of the
12872 if (token
->type
== CPP_OPEN_SQUARE
)
12874 /* Consume the `[' token. */
12875 cp_lexer_consume_token (parser
->lexer
);
12876 /* Look for the `]' token. */
12877 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12878 id
= ansi_opname (op
== NEW_EXPR
12879 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12881 /* Otherwise, we have the non-array variant. */
12883 id
= ansi_opname (op
);
12889 id
= ansi_opname (PLUS_EXPR
);
12893 id
= ansi_opname (MINUS_EXPR
);
12897 id
= ansi_opname (MULT_EXPR
);
12901 id
= ansi_opname (TRUNC_DIV_EXPR
);
12905 id
= ansi_opname (TRUNC_MOD_EXPR
);
12909 id
= ansi_opname (BIT_XOR_EXPR
);
12913 id
= ansi_opname (BIT_AND_EXPR
);
12917 id
= ansi_opname (BIT_IOR_EXPR
);
12921 id
= ansi_opname (BIT_NOT_EXPR
);
12925 id
= ansi_opname (TRUTH_NOT_EXPR
);
12929 id
= ansi_assopname (NOP_EXPR
);
12933 id
= ansi_opname (LT_EXPR
);
12937 id
= ansi_opname (GT_EXPR
);
12941 id
= ansi_assopname (PLUS_EXPR
);
12945 id
= ansi_assopname (MINUS_EXPR
);
12949 id
= ansi_assopname (MULT_EXPR
);
12953 id
= ansi_assopname (TRUNC_DIV_EXPR
);
12957 id
= ansi_assopname (TRUNC_MOD_EXPR
);
12961 id
= ansi_assopname (BIT_XOR_EXPR
);
12965 id
= ansi_assopname (BIT_AND_EXPR
);
12969 id
= ansi_assopname (BIT_IOR_EXPR
);
12973 id
= ansi_opname (LSHIFT_EXPR
);
12977 id
= ansi_opname (RSHIFT_EXPR
);
12980 case CPP_LSHIFT_EQ
:
12981 id
= ansi_assopname (LSHIFT_EXPR
);
12984 case CPP_RSHIFT_EQ
:
12985 id
= ansi_assopname (RSHIFT_EXPR
);
12989 id
= ansi_opname (EQ_EXPR
);
12993 id
= ansi_opname (NE_EXPR
);
12997 id
= ansi_opname (LE_EXPR
);
13000 case CPP_GREATER_EQ
:
13001 id
= ansi_opname (GE_EXPR
);
13005 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13009 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13012 case CPP_PLUS_PLUS
:
13013 id
= ansi_opname (POSTINCREMENT_EXPR
);
13016 case CPP_MINUS_MINUS
:
13017 id
= ansi_opname (PREDECREMENT_EXPR
);
13021 id
= ansi_opname (COMPOUND_EXPR
);
13024 case CPP_DEREF_STAR
:
13025 id
= ansi_opname (MEMBER_REF
);
13029 id
= ansi_opname (COMPONENT_REF
);
13032 case CPP_OPEN_PAREN
:
13033 /* Consume the `('. */
13034 cp_lexer_consume_token (parser
->lexer
);
13035 /* Look for the matching `)'. */
13036 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13037 return ansi_opname (CALL_EXPR
);
13039 case CPP_OPEN_SQUARE
:
13040 /* Consume the `['. */
13041 cp_lexer_consume_token (parser
->lexer
);
13042 /* Look for the matching `]'. */
13043 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13044 return ansi_opname (ARRAY_REF
);
13046 case CPP_UTF8STRING
:
13047 case CPP_UTF8STRING_USERDEF
:
13053 case CPP_STRING_USERDEF
:
13054 case CPP_WSTRING_USERDEF
:
13055 case CPP_STRING16_USERDEF
:
13056 case CPP_STRING32_USERDEF
:
13058 tree str
, string_tree
;
13061 if (cxx_dialect
== cxx98
)
13062 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13064 /* Consume the string. */
13065 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13066 /*wide_ok=*/true, /*lookup_udlit=*/false);
13067 if (str
== error_mark_node
)
13068 return error_mark_node
;
13069 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13071 string_tree
= USERDEF_LITERAL_VALUE (str
);
13072 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13077 /* Look for the suffix identifier. */
13078 token
= cp_lexer_peek_token (parser
->lexer
);
13079 if (token
->type
== CPP_NAME
)
13080 id
= cp_parser_identifier (parser
);
13081 else if (token
->type
== CPP_KEYWORD
)
13083 error ("unexpected keyword;"
13084 " remove space between quotes and suffix identifier");
13085 return error_mark_node
;
13089 error ("expected suffix identifier");
13090 return error_mark_node
;
13093 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13094 (TREE_TYPE (TREE_TYPE (string_tree
))));
13095 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13098 error ("expected empty string after %<operator%> keyword");
13099 return error_mark_node
;
13101 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13104 error ("invalid encoding prefix in literal operator");
13105 return error_mark_node
;
13107 if (id
!= error_mark_node
)
13109 const char *name
= IDENTIFIER_POINTER (id
);
13110 id
= cp_literal_operator_id (name
);
13116 /* Anything else is an error. */
13120 /* If we have selected an identifier, we need to consume the
13123 cp_lexer_consume_token (parser
->lexer
);
13124 /* Otherwise, no valid operator name was present. */
13127 cp_parser_error (parser
, "expected operator");
13128 id
= error_mark_node
;
13134 /* Parse a template-declaration.
13136 template-declaration:
13137 export [opt] template < template-parameter-list > declaration
13139 If MEMBER_P is TRUE, this template-declaration occurs within a
13142 The grammar rule given by the standard isn't correct. What
13143 is really meant is:
13145 template-declaration:
13146 export [opt] template-parameter-list-seq
13147 decl-specifier-seq [opt] init-declarator [opt] ;
13148 export [opt] template-parameter-list-seq
13149 function-definition
13151 template-parameter-list-seq:
13152 template-parameter-list-seq [opt]
13153 template < template-parameter-list > */
13156 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13158 /* Check for `export'. */
13159 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13161 /* Consume the `export' token. */
13162 cp_lexer_consume_token (parser
->lexer
);
13163 /* Warn that we do not support `export'. */
13164 warning (0, "keyword %<export%> not implemented, and will be ignored");
13167 cp_parser_template_declaration_after_export (parser
, member_p
);
13170 /* Parse a template-parameter-list.
13172 template-parameter-list:
13174 template-parameter-list , template-parameter
13176 Returns a TREE_LIST. Each node represents a template parameter.
13177 The nodes are connected via their TREE_CHAINs. */
13180 cp_parser_template_parameter_list (cp_parser
* parser
)
13182 tree parameter_list
= NULL_TREE
;
13184 begin_template_parm_list ();
13186 /* The loop below parses the template parms. We first need to know
13187 the total number of template parms to be able to compute proper
13188 canonical types of each dependent type. So after the loop, when
13189 we know the total number of template parms,
13190 end_template_parm_list computes the proper canonical types and
13191 fixes up the dependent types accordingly. */
13196 bool is_parameter_pack
;
13197 location_t parm_loc
;
13199 /* Parse the template-parameter. */
13200 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13201 parameter
= cp_parser_template_parameter (parser
,
13203 &is_parameter_pack
);
13204 /* Add it to the list. */
13205 if (parameter
!= error_mark_node
)
13206 parameter_list
= process_template_parm (parameter_list
,
13210 is_parameter_pack
);
13213 tree err_parm
= build_tree_list (parameter
, parameter
);
13214 parameter_list
= chainon (parameter_list
, err_parm
);
13217 /* If the next token is not a `,', we're done. */
13218 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13220 /* Otherwise, consume the `,' token. */
13221 cp_lexer_consume_token (parser
->lexer
);
13224 return end_template_parm_list (parameter_list
);
13227 /* Parse a template-parameter.
13229 template-parameter:
13231 parameter-declaration
13233 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13234 the parameter. The TREE_PURPOSE is the default value, if any.
13235 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13236 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13237 set to true iff this parameter is a parameter pack. */
13240 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13241 bool *is_parameter_pack
)
13244 cp_parameter_declarator
*parameter_declarator
;
13245 cp_declarator
*id_declarator
;
13248 /* Assume it is a type parameter or a template parameter. */
13249 *is_non_type
= false;
13250 /* Assume it not a parameter pack. */
13251 *is_parameter_pack
= false;
13252 /* Peek at the next token. */
13253 token
= cp_lexer_peek_token (parser
->lexer
);
13254 /* If it is `class' or `template', we have a type-parameter. */
13255 if (token
->keyword
== RID_TEMPLATE
)
13256 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13257 /* If it is `class' or `typename' we do not know yet whether it is a
13258 type parameter or a non-type parameter. Consider:
13260 template <typename T, typename T::X X> ...
13264 template <class C, class D*> ...
13266 Here, the first parameter is a type parameter, and the second is
13267 a non-type parameter. We can tell by looking at the token after
13268 the identifier -- if it is a `,', `=', or `>' then we have a type
13270 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13272 /* Peek at the token after `class' or `typename'. */
13273 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13274 /* If it's an ellipsis, we have a template type parameter
13276 if (token
->type
== CPP_ELLIPSIS
)
13277 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13278 /* If it's an identifier, skip it. */
13279 if (token
->type
== CPP_NAME
)
13280 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13281 /* Now, see if the token looks like the end of a template
13283 if (token
->type
== CPP_COMMA
13284 || token
->type
== CPP_EQ
13285 || token
->type
== CPP_GREATER
)
13286 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13289 /* Otherwise, it is a non-type parameter.
13293 When parsing a default template-argument for a non-type
13294 template-parameter, the first non-nested `>' is taken as the end
13295 of the template parameter-list rather than a greater-than
13297 *is_non_type
= true;
13298 parameter_declarator
13299 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13300 /*parenthesized_p=*/NULL
);
13302 if (!parameter_declarator
)
13303 return error_mark_node
;
13305 /* If the parameter declaration is marked as a parameter pack, set
13306 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13307 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13309 if (parameter_declarator
->declarator
13310 && parameter_declarator
->declarator
->parameter_pack_p
)
13312 *is_parameter_pack
= true;
13313 parameter_declarator
->declarator
->parameter_pack_p
= false;
13316 if (parameter_declarator
->default_argument
)
13318 /* Can happen in some cases of erroneous input (c++/34892). */
13319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13320 /* Consume the `...' for better error recovery. */
13321 cp_lexer_consume_token (parser
->lexer
);
13323 /* If the next token is an ellipsis, and we don't already have it
13324 marked as a parameter pack, then we have a parameter pack (that
13325 has no declarator). */
13326 else if (!*is_parameter_pack
13327 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13328 && (declarator_can_be_parameter_pack
13329 (parameter_declarator
->declarator
)))
13331 /* Consume the `...'. */
13332 cp_lexer_consume_token (parser
->lexer
);
13333 maybe_warn_variadic_templates ();
13335 *is_parameter_pack
= true;
13337 /* We might end up with a pack expansion as the type of the non-type
13338 template parameter, in which case this is a non-type template
13340 else if (parameter_declarator
->decl_specifiers
.type
13341 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13343 *is_parameter_pack
= true;
13344 parameter_declarator
->decl_specifiers
.type
=
13345 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13348 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13350 /* Parameter packs cannot have default arguments. However, a
13351 user may try to do so, so we'll parse them and give an
13352 appropriate diagnostic here. */
13354 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13356 /* Find the name of the parameter pack. */
13357 id_declarator
= parameter_declarator
->declarator
;
13358 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13359 id_declarator
= id_declarator
->declarator
;
13361 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13362 error_at (start_token
->location
,
13363 "template parameter pack %qD cannot have a default argument",
13364 id_declarator
->u
.id
.unqualified_name
);
13366 error_at (start_token
->location
,
13367 "template parameter pack cannot have a default argument");
13369 /* Parse the default argument, but throw away the result. */
13370 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13373 parm
= grokdeclarator (parameter_declarator
->declarator
,
13374 ¶meter_declarator
->decl_specifiers
,
13375 TPARM
, /*initialized=*/0,
13376 /*attrlist=*/NULL
);
13377 if (parm
== error_mark_node
)
13378 return error_mark_node
;
13380 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13383 /* Parse a type-parameter.
13386 class identifier [opt]
13387 class identifier [opt] = type-id
13388 typename identifier [opt]
13389 typename identifier [opt] = type-id
13390 template < template-parameter-list > class identifier [opt]
13391 template < template-parameter-list > class identifier [opt]
13394 GNU Extension (variadic templates):
13397 class ... identifier [opt]
13398 typename ... identifier [opt]
13400 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13401 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13402 the declaration of the parameter.
13404 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13407 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13412 /* Look for a keyword to tell us what kind of parameter this is. */
13413 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13415 return error_mark_node
;
13417 switch (token
->keyword
)
13423 tree default_argument
;
13425 /* If the next token is an ellipsis, we have a template
13427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13429 /* Consume the `...' token. */
13430 cp_lexer_consume_token (parser
->lexer
);
13431 maybe_warn_variadic_templates ();
13433 *is_parameter_pack
= true;
13436 /* If the next token is an identifier, then it names the
13438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13439 identifier
= cp_parser_identifier (parser
);
13441 identifier
= NULL_TREE
;
13443 /* Create the parameter. */
13444 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13446 /* If the next token is an `=', we have a default argument. */
13447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13449 /* Consume the `=' token. */
13450 cp_lexer_consume_token (parser
->lexer
);
13451 /* Parse the default-argument. */
13452 push_deferring_access_checks (dk_no_deferred
);
13453 default_argument
= cp_parser_type_id (parser
);
13455 /* Template parameter packs cannot have default
13457 if (*is_parameter_pack
)
13460 error_at (token
->location
,
13461 "template parameter pack %qD cannot have a "
13462 "default argument", identifier
);
13464 error_at (token
->location
,
13465 "template parameter packs cannot have "
13466 "default arguments");
13467 default_argument
= NULL_TREE
;
13469 pop_deferring_access_checks ();
13472 default_argument
= NULL_TREE
;
13474 /* Create the combined representation of the parameter and the
13475 default argument. */
13476 parameter
= build_tree_list (default_argument
, parameter
);
13483 tree default_argument
;
13485 /* Look for the `<'. */
13486 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13487 /* Parse the template-parameter-list. */
13488 cp_parser_template_parameter_list (parser
);
13489 /* Look for the `>'. */
13490 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13491 /* Look for the `class' or 'typename' keywords. */
13492 cp_parser_type_parameter_key (parser
);
13493 /* If the next token is an ellipsis, we have a template
13495 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13497 /* Consume the `...' token. */
13498 cp_lexer_consume_token (parser
->lexer
);
13499 maybe_warn_variadic_templates ();
13501 *is_parameter_pack
= true;
13503 /* If the next token is an `=', then there is a
13504 default-argument. If the next token is a `>', we are at
13505 the end of the parameter-list. If the next token is a `,',
13506 then we are at the end of this parameter. */
13507 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13508 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13509 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13511 identifier
= cp_parser_identifier (parser
);
13512 /* Treat invalid names as if the parameter were nameless. */
13513 if (identifier
== error_mark_node
)
13514 identifier
= NULL_TREE
;
13517 identifier
= NULL_TREE
;
13519 /* Create the template parameter. */
13520 parameter
= finish_template_template_parm (class_type_node
,
13523 /* If the next token is an `=', then there is a
13524 default-argument. */
13525 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13529 /* Consume the `='. */
13530 cp_lexer_consume_token (parser
->lexer
);
13531 /* Parse the id-expression. */
13532 push_deferring_access_checks (dk_no_deferred
);
13533 /* save token before parsing the id-expression, for error
13535 token
= cp_lexer_peek_token (parser
->lexer
);
13537 = cp_parser_id_expression (parser
,
13538 /*template_keyword_p=*/false,
13539 /*check_dependency_p=*/true,
13540 /*template_p=*/&is_template
,
13541 /*declarator_p=*/false,
13542 /*optional_p=*/false);
13543 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13544 /* If the id-expression was a template-id that refers to
13545 a template-class, we already have the declaration here,
13546 so no further lookup is needed. */
13549 /* Look up the name. */
13551 = cp_parser_lookup_name (parser
, default_argument
,
13553 /*is_template=*/is_template
,
13554 /*is_namespace=*/false,
13555 /*check_dependency=*/true,
13556 /*ambiguous_decls=*/NULL
,
13558 /* See if the default argument is valid. */
13560 = check_template_template_default_arg (default_argument
);
13562 /* Template parameter packs cannot have default
13564 if (*is_parameter_pack
)
13567 error_at (token
->location
,
13568 "template parameter pack %qD cannot "
13569 "have a default argument",
13572 error_at (token
->location
, "template parameter packs cannot "
13573 "have default arguments");
13574 default_argument
= NULL_TREE
;
13576 pop_deferring_access_checks ();
13579 default_argument
= NULL_TREE
;
13581 /* Create the combined representation of the parameter and the
13582 default argument. */
13583 parameter
= build_tree_list (default_argument
, parameter
);
13588 gcc_unreachable ();
13595 /* Parse a template-id.
13598 template-name < template-argument-list [opt] >
13600 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13601 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13602 returned. Otherwise, if the template-name names a function, or set
13603 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13604 names a class, returns a TYPE_DECL for the specialization.
13606 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13607 uninstantiated templates. */
13610 cp_parser_template_id (cp_parser
*parser
,
13611 bool template_keyword_p
,
13612 bool check_dependency_p
,
13613 enum tag_types tag_type
,
13614 bool is_declaration
)
13620 cp_token_position start_of_id
= 0;
13621 deferred_access_check
*chk
;
13622 vec
<deferred_access_check
, va_gc
> *access_check
;
13623 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13624 bool is_identifier
;
13626 /* If the next token corresponds to a template-id, there is no need
13628 next_token
= cp_lexer_peek_token (parser
->lexer
);
13629 if (next_token
->type
== CPP_TEMPLATE_ID
)
13631 struct tree_check
*check_value
;
13633 /* Get the stored value. */
13634 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13635 /* Perform any access checks that were deferred. */
13636 access_check
= check_value
->checks
;
13639 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13640 perform_or_defer_access_check (chk
->binfo
,
13643 tf_warning_or_error
);
13645 /* Return the stored value. */
13646 return check_value
->value
;
13649 /* Avoid performing name lookup if there is no possibility of
13650 finding a template-id. */
13651 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13652 || (next_token
->type
== CPP_NAME
13653 && !cp_parser_nth_token_starts_template_argument_list_p
13656 cp_parser_error (parser
, "expected template-id");
13657 return error_mark_node
;
13660 /* Remember where the template-id starts. */
13661 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13662 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13664 push_deferring_access_checks (dk_deferred
);
13666 /* Parse the template-name. */
13667 is_identifier
= false;
13668 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13669 check_dependency_p
,
13673 if (templ
== error_mark_node
|| is_identifier
)
13675 pop_deferring_access_checks ();
13679 /* If we find the sequence `[:' after a template-name, it's probably
13680 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13681 parse correctly the argument list. */
13682 next_token
= cp_lexer_peek_token (parser
->lexer
);
13683 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13684 if (next_token
->type
== CPP_OPEN_SQUARE
13685 && next_token
->flags
& DIGRAPH
13686 && next_token_2
->type
== CPP_COLON
13687 && !(next_token_2
->flags
& PREV_WHITE
))
13689 cp_parser_parse_tentatively (parser
);
13690 /* Change `:' into `::'. */
13691 next_token_2
->type
= CPP_SCOPE
;
13692 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13694 cp_lexer_consume_token (parser
->lexer
);
13696 /* Parse the arguments. */
13697 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13698 if (!cp_parser_parse_definitely (parser
))
13700 /* If we couldn't parse an argument list, then we revert our changes
13701 and return simply an error. Maybe this is not a template-id
13703 next_token_2
->type
= CPP_COLON
;
13704 cp_parser_error (parser
, "expected %<<%>");
13705 pop_deferring_access_checks ();
13706 return error_mark_node
;
13708 /* Otherwise, emit an error about the invalid digraph, but continue
13709 parsing because we got our argument list. */
13710 if (permerror (next_token
->location
,
13711 "%<<::%> cannot begin a template-argument list"))
13713 static bool hint
= false;
13714 inform (next_token
->location
,
13715 "%<<:%> is an alternate spelling for %<[%>."
13716 " Insert whitespace between %<<%> and %<::%>");
13717 if (!hint
&& !flag_permissive
)
13719 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13720 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13721 "accept your code)");
13728 /* Look for the `<' that starts the template-argument-list. */
13729 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13731 pop_deferring_access_checks ();
13732 return error_mark_node
;
13734 /* Parse the arguments. */
13735 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13738 /* Build a representation of the specialization. */
13739 if (identifier_p (templ
))
13740 template_id
= build_min_nt_loc (next_token
->location
,
13743 else if (DECL_TYPE_TEMPLATE_P (templ
)
13744 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13746 bool entering_scope
;
13747 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13748 template (rather than some instantiation thereof) only if
13749 is not nested within some other construct. For example, in
13750 "template <typename T> void f(T) { A<T>::", A<T> is just an
13751 instantiation of A. */
13752 entering_scope
= (template_parm_scope_p ()
13753 && cp_lexer_next_token_is (parser
->lexer
,
13756 = finish_template_type (templ
, arguments
, entering_scope
);
13758 else if (variable_template_p (templ
))
13760 template_id
= lookup_template_variable (templ
, arguments
);
13764 /* If it's not a class-template or a template-template, it should be
13765 a function-template. */
13766 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13767 || TREE_CODE (templ
) == OVERLOAD
13768 || BASELINK_P (templ
)));
13770 template_id
= lookup_template_function (templ
, arguments
);
13773 /* If parsing tentatively, replace the sequence of tokens that makes
13774 up the template-id with a CPP_TEMPLATE_ID token. That way,
13775 should we re-parse the token stream, we will not have to repeat
13776 the effort required to do the parse, nor will we issue duplicate
13777 error messages about problems during instantiation of the
13780 /* Don't do this if we had a parse error in a declarator; re-parsing
13781 might succeed if a name changes meaning (60361). */
13782 && !(cp_parser_error_occurred (parser
)
13783 && cp_parser_parsing_tentatively (parser
)
13784 && parser
->in_declarator_p
))
13786 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13788 /* Reset the contents of the START_OF_ID token. */
13789 token
->type
= CPP_TEMPLATE_ID
;
13790 /* Retrieve any deferred checks. Do not pop this access checks yet
13791 so the memory will not be reclaimed during token replacing below. */
13792 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13793 token
->u
.tree_check_value
->value
= template_id
;
13794 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13795 token
->keyword
= RID_MAX
;
13797 /* Purge all subsequent tokens. */
13798 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13800 /* ??? Can we actually assume that, if template_id ==
13801 error_mark_node, we will have issued a diagnostic to the
13802 user, as opposed to simply marking the tentative parse as
13804 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13805 error_at (token
->location
, "parse error in template argument list");
13808 pop_to_parent_deferring_access_checks ();
13809 return template_id
;
13812 /* Parse a template-name.
13817 The standard should actually say:
13821 operator-function-id
13823 A defect report has been filed about this issue.
13825 A conversion-function-id cannot be a template name because they cannot
13826 be part of a template-id. In fact, looking at this code:
13828 a.operator K<int>()
13830 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13831 It is impossible to call a templated conversion-function-id with an
13832 explicit argument list, since the only allowed template parameter is
13833 the type to which it is converting.
13835 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13836 `template' keyword, in a construction like:
13840 In that case `f' is taken to be a template-name, even though there
13841 is no way of knowing for sure.
13843 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13844 name refers to a set of overloaded functions, at least one of which
13845 is a template, or an IDENTIFIER_NODE with the name of the template,
13846 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13847 names are looked up inside uninstantiated templates. */
13850 cp_parser_template_name (cp_parser
* parser
,
13851 bool template_keyword_p
,
13852 bool check_dependency_p
,
13853 bool is_declaration
,
13854 enum tag_types tag_type
,
13855 bool *is_identifier
)
13860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13862 /* If the next token is `operator', then we have either an
13863 operator-function-id or a conversion-function-id. */
13864 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13866 /* We don't know whether we're looking at an
13867 operator-function-id or a conversion-function-id. */
13868 cp_parser_parse_tentatively (parser
);
13869 /* Try an operator-function-id. */
13870 identifier
= cp_parser_operator_function_id (parser
);
13871 /* If that didn't work, try a conversion-function-id. */
13872 if (!cp_parser_parse_definitely (parser
))
13874 cp_parser_error (parser
, "expected template-name");
13875 return error_mark_node
;
13878 /* Look for the identifier. */
13880 identifier
= cp_parser_identifier (parser
);
13882 /* If we didn't find an identifier, we don't have a template-id. */
13883 if (identifier
== error_mark_node
)
13884 return error_mark_node
;
13886 /* If the name immediately followed the `template' keyword, then it
13887 is a template-name. However, if the next token is not `<', then
13888 we do not treat it as a template-name, since it is not being used
13889 as part of a template-id. This enables us to handle constructs
13892 template <typename T> struct S { S(); };
13893 template <typename T> S<T>::S();
13895 correctly. We would treat `S' as a template -- if it were `S<T>'
13896 -- but we do not if there is no `<'. */
13898 if (processing_template_decl
13899 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13901 /* In a declaration, in a dependent context, we pretend that the
13902 "template" keyword was present in order to improve error
13903 recovery. For example, given:
13905 template <typename T> void f(T::X<int>);
13907 we want to treat "X<int>" as a template-id. */
13909 && !template_keyword_p
13910 && parser
->scope
&& TYPE_P (parser
->scope
)
13911 && check_dependency_p
13912 && dependent_scope_p (parser
->scope
)
13913 /* Do not do this for dtors (or ctors), since they never
13914 need the template keyword before their name. */
13915 && !constructor_name_p (identifier
, parser
->scope
))
13917 cp_token_position start
= 0;
13919 /* Explain what went wrong. */
13920 error_at (token
->location
, "non-template %qD used as template",
13922 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
13923 parser
->scope
, identifier
);
13924 /* If parsing tentatively, find the location of the "<" token. */
13925 if (cp_parser_simulate_error (parser
))
13926 start
= cp_lexer_token_position (parser
->lexer
, true);
13927 /* Parse the template arguments so that we can issue error
13928 messages about them. */
13929 cp_lexer_consume_token (parser
->lexer
);
13930 cp_parser_enclosed_template_argument_list (parser
);
13931 /* Skip tokens until we find a good place from which to
13932 continue parsing. */
13933 cp_parser_skip_to_closing_parenthesis (parser
,
13934 /*recovering=*/true,
13936 /*consume_paren=*/false);
13937 /* If parsing tentatively, permanently remove the
13938 template argument list. That will prevent duplicate
13939 error messages from being issued about the missing
13940 "template" keyword. */
13942 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
13944 *is_identifier
= true;
13948 /* If the "template" keyword is present, then there is generally
13949 no point in doing name-lookup, so we just return IDENTIFIER.
13950 But, if the qualifying scope is non-dependent then we can
13951 (and must) do name-lookup normally. */
13952 if (template_keyword_p
13954 || (TYPE_P (parser
->scope
)
13955 && dependent_type_p (parser
->scope
))))
13959 /* Look up the name. */
13960 decl
= cp_parser_lookup_name (parser
, identifier
,
13962 /*is_template=*/true,
13963 /*is_namespace=*/false,
13964 check_dependency_p
,
13965 /*ambiguous_decls=*/NULL
,
13968 /* If DECL is a template, then the name was a template-name. */
13969 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
13973 tree fn
= NULL_TREE
;
13975 /* The standard does not explicitly indicate whether a name that
13976 names a set of overloaded declarations, some of which are
13977 templates, is a template-name. However, such a name should
13978 be a template-name; otherwise, there is no way to form a
13979 template-id for the overloaded templates. */
13980 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
13981 if (TREE_CODE (fns
) == OVERLOAD
)
13982 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
13983 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
13988 /* The name does not name a template. */
13989 cp_parser_error (parser
, "expected template-name");
13990 return error_mark_node
;
13994 /* If DECL is dependent, and refers to a function, then just return
13995 its name; we will look it up again during template instantiation. */
13996 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
13998 tree scope
= ovl_scope (decl
);
13999 if (TYPE_P (scope
) && dependent_type_p (scope
))
14006 /* Parse a template-argument-list.
14008 template-argument-list:
14009 template-argument ... [opt]
14010 template-argument-list , template-argument ... [opt]
14012 Returns a TREE_VEC containing the arguments. */
14015 cp_parser_template_argument_list (cp_parser
* parser
)
14017 tree fixed_args
[10];
14018 unsigned n_args
= 0;
14019 unsigned alloced
= 10;
14020 tree
*arg_ary
= fixed_args
;
14022 bool saved_in_template_argument_list_p
;
14024 bool saved_non_ice_p
;
14026 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14027 parser
->in_template_argument_list_p
= true;
14028 /* Even if the template-id appears in an integral
14029 constant-expression, the contents of the argument list do
14031 saved_ice_p
= parser
->integral_constant_expression_p
;
14032 parser
->integral_constant_expression_p
= false;
14033 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14034 parser
->non_integral_constant_expression_p
= false;
14036 /* Parse the arguments. */
14042 /* Consume the comma. */
14043 cp_lexer_consume_token (parser
->lexer
);
14045 /* Parse the template-argument. */
14046 argument
= cp_parser_template_argument (parser
);
14048 /* If the next token is an ellipsis, we're expanding a template
14050 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14052 if (argument
== error_mark_node
)
14054 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14055 error_at (token
->location
,
14056 "expected parameter pack before %<...%>");
14058 /* Consume the `...' token. */
14059 cp_lexer_consume_token (parser
->lexer
);
14061 /* Make the argument into a TYPE_PACK_EXPANSION or
14062 EXPR_PACK_EXPANSION. */
14063 argument
= make_pack_expansion (argument
);
14066 if (n_args
== alloced
)
14070 if (arg_ary
== fixed_args
)
14072 arg_ary
= XNEWVEC (tree
, alloced
);
14073 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14076 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14078 arg_ary
[n_args
++] = argument
;
14080 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14082 vec
= make_tree_vec (n_args
);
14085 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14087 if (arg_ary
!= fixed_args
)
14089 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14090 parser
->integral_constant_expression_p
= saved_ice_p
;
14091 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14092 #ifdef ENABLE_CHECKING
14093 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14098 /* Parse a template-argument.
14101 assignment-expression
14105 The representation is that of an assignment-expression, type-id, or
14106 id-expression -- except that the qualified id-expression is
14107 evaluated, so that the value returned is either a DECL or an
14110 Although the standard says "assignment-expression", it forbids
14111 throw-expressions or assignments in the template argument.
14112 Therefore, we use "conditional-expression" instead. */
14115 cp_parser_template_argument (cp_parser
* parser
)
14120 bool maybe_type_id
= false;
14121 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14122 location_t loc
= 0;
14125 /* There's really no way to know what we're looking at, so we just
14126 try each alternative in order.
14130 In a template-argument, an ambiguity between a type-id and an
14131 expression is resolved to a type-id, regardless of the form of
14132 the corresponding template-parameter.
14134 Therefore, we try a type-id first. */
14135 cp_parser_parse_tentatively (parser
);
14136 argument
= cp_parser_template_type_arg (parser
);
14137 /* If there was no error parsing the type-id but the next token is a
14138 '>>', our behavior depends on which dialect of C++ we're
14139 parsing. In C++98, we probably found a typo for '> >'. But there
14140 are type-id which are also valid expressions. For instance:
14142 struct X { int operator >> (int); };
14143 template <int V> struct Foo {};
14146 Here 'X()' is a valid type-id of a function type, but the user just
14147 wanted to write the expression "X() >> 5". Thus, we remember that we
14148 found a valid type-id, but we still try to parse the argument as an
14149 expression to see what happens.
14151 In C++0x, the '>>' will be considered two separate '>'
14153 if (!cp_parser_error_occurred (parser
)
14154 && cxx_dialect
== cxx98
14155 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14157 maybe_type_id
= true;
14158 cp_parser_abort_tentative_parse (parser
);
14162 /* If the next token isn't a `,' or a `>', then this argument wasn't
14163 really finished. This means that the argument is not a valid
14165 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14166 cp_parser_error (parser
, "expected template-argument");
14167 /* If that worked, we're done. */
14168 if (cp_parser_parse_definitely (parser
))
14171 /* We're still not sure what the argument will be. */
14172 cp_parser_parse_tentatively (parser
);
14173 /* Try a template. */
14174 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14175 argument
= cp_parser_id_expression (parser
,
14176 /*template_keyword_p=*/false,
14177 /*check_dependency_p=*/true,
14179 /*declarator_p=*/false,
14180 /*optional_p=*/false);
14181 /* If the next token isn't a `,' or a `>', then this argument wasn't
14182 really finished. */
14183 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14184 cp_parser_error (parser
, "expected template-argument");
14185 if (!cp_parser_error_occurred (parser
))
14187 /* Figure out what is being referred to. If the id-expression
14188 was for a class template specialization, then we will have a
14189 TYPE_DECL at this point. There is no need to do name lookup
14190 at this point in that case. */
14191 if (TREE_CODE (argument
) != TYPE_DECL
)
14192 argument
= cp_parser_lookup_name (parser
, argument
,
14194 /*is_template=*/template_p
,
14195 /*is_namespace=*/false,
14196 /*check_dependency=*/true,
14197 /*ambiguous_decls=*/NULL
,
14198 argument_start_token
->location
);
14199 if (TREE_CODE (argument
) != TEMPLATE_DECL
14200 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14201 cp_parser_error (parser
, "expected template-name");
14203 if (cp_parser_parse_definitely (parser
))
14205 /* It must be a non-type argument. There permitted cases are given
14206 in [temp.arg.nontype]:
14208 -- an integral constant-expression of integral or enumeration
14211 -- the name of a non-type template-parameter; or
14213 -- the name of an object or function with external linkage...
14215 -- the address of an object or function with external linkage...
14217 -- a pointer to member... */
14218 /* Look for a non-type template parameter. */
14219 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14221 cp_parser_parse_tentatively (parser
);
14222 argument
= cp_parser_primary_expression (parser
,
14223 /*address_p=*/false,
14225 /*template_arg_p=*/true,
14227 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14228 || !cp_parser_next_token_ends_template_argument_p (parser
))
14229 cp_parser_simulate_error (parser
);
14230 if (cp_parser_parse_definitely (parser
))
14234 /* If the next token is "&", the argument must be the address of an
14235 object or function with external linkage. */
14236 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14239 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14240 cp_lexer_consume_token (parser
->lexer
);
14242 /* See if we might have an id-expression. */
14243 token
= cp_lexer_peek_token (parser
->lexer
);
14244 if (token
->type
== CPP_NAME
14245 || token
->keyword
== RID_OPERATOR
14246 || token
->type
== CPP_SCOPE
14247 || token
->type
== CPP_TEMPLATE_ID
14248 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14250 cp_parser_parse_tentatively (parser
);
14251 argument
= cp_parser_primary_expression (parser
,
14254 /*template_arg_p=*/true,
14256 if (cp_parser_error_occurred (parser
)
14257 || !cp_parser_next_token_ends_template_argument_p (parser
))
14258 cp_parser_abort_tentative_parse (parser
);
14263 if (INDIRECT_REF_P (argument
))
14265 /* Strip the dereference temporarily. */
14266 gcc_assert (REFERENCE_REF_P (argument
));
14267 argument
= TREE_OPERAND (argument
, 0);
14270 /* If we're in a template, we represent a qualified-id referring
14271 to a static data member as a SCOPE_REF even if the scope isn't
14272 dependent so that we can check access control later. */
14274 if (TREE_CODE (probe
) == SCOPE_REF
)
14275 probe
= TREE_OPERAND (probe
, 1);
14278 /* A variable without external linkage might still be a
14279 valid constant-expression, so no error is issued here
14280 if the external-linkage check fails. */
14281 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14282 cp_parser_simulate_error (parser
);
14284 else if (is_overloaded_fn (argument
))
14285 /* All overloaded functions are allowed; if the external
14286 linkage test does not pass, an error will be issued
14290 && (TREE_CODE (argument
) == OFFSET_REF
14291 || TREE_CODE (argument
) == SCOPE_REF
))
14292 /* A pointer-to-member. */
14294 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14297 cp_parser_simulate_error (parser
);
14299 if (cp_parser_parse_definitely (parser
))
14302 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14303 tf_warning_or_error
);
14305 argument
= convert_from_reference (argument
);
14310 /* If the argument started with "&", there are no other valid
14311 alternatives at this point. */
14314 cp_parser_error (parser
, "invalid non-type template argument");
14315 return error_mark_node
;
14318 /* If the argument wasn't successfully parsed as a type-id followed
14319 by '>>', the argument can only be a constant expression now.
14320 Otherwise, we try parsing the constant-expression tentatively,
14321 because the argument could really be a type-id. */
14323 cp_parser_parse_tentatively (parser
);
14324 argument
= cp_parser_constant_expression (parser
);
14326 if (!maybe_type_id
)
14328 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14329 cp_parser_error (parser
, "expected template-argument");
14330 if (cp_parser_parse_definitely (parser
))
14332 /* We did our best to parse the argument as a non type-id, but that
14333 was the only alternative that matched (albeit with a '>' after
14334 it). We can assume it's just a typo from the user, and a
14335 diagnostic will then be issued. */
14336 return cp_parser_template_type_arg (parser
);
14339 /* Parse an explicit-instantiation.
14341 explicit-instantiation:
14342 template declaration
14344 Although the standard says `declaration', what it really means is:
14346 explicit-instantiation:
14347 template decl-specifier-seq [opt] declarator [opt] ;
14349 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14350 supposed to be allowed. A defect report has been filed about this
14355 explicit-instantiation:
14356 storage-class-specifier template
14357 decl-specifier-seq [opt] declarator [opt] ;
14358 function-specifier template
14359 decl-specifier-seq [opt] declarator [opt] ; */
14362 cp_parser_explicit_instantiation (cp_parser
* parser
)
14364 int declares_class_or_enum
;
14365 cp_decl_specifier_seq decl_specifiers
;
14366 tree extension_specifier
= NULL_TREE
;
14368 timevar_push (TV_TEMPLATE_INST
);
14370 /* Look for an (optional) storage-class-specifier or
14371 function-specifier. */
14372 if (cp_parser_allow_gnu_extensions_p (parser
))
14374 extension_specifier
14375 = cp_parser_storage_class_specifier_opt (parser
);
14376 if (!extension_specifier
)
14377 extension_specifier
14378 = cp_parser_function_specifier_opt (parser
,
14379 /*decl_specs=*/NULL
);
14382 /* Look for the `template' keyword. */
14383 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14384 /* Let the front end know that we are processing an explicit
14386 begin_explicit_instantiation ();
14387 /* [temp.explicit] says that we are supposed to ignore access
14388 control while processing explicit instantiation directives. */
14389 push_deferring_access_checks (dk_no_check
);
14390 /* Parse a decl-specifier-seq. */
14391 cp_parser_decl_specifier_seq (parser
,
14392 CP_PARSER_FLAGS_OPTIONAL
,
14394 &declares_class_or_enum
);
14395 /* If there was exactly one decl-specifier, and it declared a class,
14396 and there's no declarator, then we have an explicit type
14398 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14402 type
= check_tag_decl (&decl_specifiers
,
14403 /*explicit_type_instantiation_p=*/true);
14404 /* Turn access control back on for names used during
14405 template instantiation. */
14406 pop_deferring_access_checks ();
14408 do_type_instantiation (type
, extension_specifier
,
14409 /*complain=*/tf_error
);
14413 cp_declarator
*declarator
;
14416 /* Parse the declarator. */
14418 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14419 /*ctor_dtor_or_conv_p=*/NULL
,
14420 /*parenthesized_p=*/NULL
,
14421 /*member_p=*/false,
14422 /*friend_p=*/false);
14423 if (declares_class_or_enum
& 2)
14424 cp_parser_check_for_definition_in_return_type (declarator
,
14425 decl_specifiers
.type
,
14426 decl_specifiers
.locations
[ds_type_spec
]);
14427 if (declarator
!= cp_error_declarator
)
14429 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14430 permerror (decl_specifiers
.locations
[ds_inline
],
14431 "explicit instantiation shall not use"
14432 " %<inline%> specifier");
14433 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14434 permerror (decl_specifiers
.locations
[ds_constexpr
],
14435 "explicit instantiation shall not use"
14436 " %<constexpr%> specifier");
14438 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14439 NORMAL
, 0, &decl_specifiers
.attributes
);
14440 /* Turn access control back on for names used during
14441 template instantiation. */
14442 pop_deferring_access_checks ();
14443 /* Do the explicit instantiation. */
14444 do_decl_instantiation (decl
, extension_specifier
);
14448 pop_deferring_access_checks ();
14449 /* Skip the body of the explicit instantiation. */
14450 cp_parser_skip_to_end_of_statement (parser
);
14453 /* We're done with the instantiation. */
14454 end_explicit_instantiation ();
14456 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14458 timevar_pop (TV_TEMPLATE_INST
);
14461 /* Parse an explicit-specialization.
14463 explicit-specialization:
14464 template < > declaration
14466 Although the standard says `declaration', what it really means is:
14468 explicit-specialization:
14469 template <> decl-specifier [opt] init-declarator [opt] ;
14470 template <> function-definition
14471 template <> explicit-specialization
14472 template <> template-declaration */
14475 cp_parser_explicit_specialization (cp_parser
* parser
)
14477 bool need_lang_pop
;
14478 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14480 /* Look for the `template' keyword. */
14481 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14482 /* Look for the `<'. */
14483 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14484 /* Look for the `>'. */
14485 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14486 /* We have processed another parameter list. */
14487 ++parser
->num_template_parameter_lists
;
14490 A template ... explicit specialization ... shall not have C
14492 if (current_lang_name
== lang_name_c
)
14494 error_at (token
->location
, "template specialization with C linkage");
14495 /* Give it C++ linkage to avoid confusing other parts of the
14497 push_lang_context (lang_name_cplusplus
);
14498 need_lang_pop
= true;
14501 need_lang_pop
= false;
14502 /* Let the front end know that we are beginning a specialization. */
14503 if (!begin_specialization ())
14505 end_specialization ();
14509 /* If the next keyword is `template', we need to figure out whether
14510 or not we're looking a template-declaration. */
14511 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14513 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14514 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14515 cp_parser_template_declaration_after_export (parser
,
14516 /*member_p=*/false);
14518 cp_parser_explicit_specialization (parser
);
14521 /* Parse the dependent declaration. */
14522 cp_parser_single_declaration (parser
,
14524 /*member_p=*/false,
14525 /*explicit_specialization_p=*/true,
14526 /*friend_p=*/NULL
);
14527 /* We're done with the specialization. */
14528 end_specialization ();
14529 /* For the erroneous case of a template with C linkage, we pushed an
14530 implicit C++ linkage scope; exit that scope now. */
14532 pop_lang_context ();
14533 /* We're done with this parameter list. */
14534 --parser
->num_template_parameter_lists
;
14537 /* Parse a type-specifier.
14540 simple-type-specifier
14543 elaborated-type-specifier
14551 Returns a representation of the type-specifier. For a
14552 class-specifier, enum-specifier, or elaborated-type-specifier, a
14553 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14555 The parser flags FLAGS is used to control type-specifier parsing.
14557 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14558 in a decl-specifier-seq.
14560 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14561 class-specifier, enum-specifier, or elaborated-type-specifier, then
14562 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14563 if a type is declared; 2 if it is defined. Otherwise, it is set to
14566 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14567 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14568 is set to FALSE. */
14571 cp_parser_type_specifier (cp_parser
* parser
,
14572 cp_parser_flags flags
,
14573 cp_decl_specifier_seq
*decl_specs
,
14574 bool is_declaration
,
14575 int* declares_class_or_enum
,
14576 bool* is_cv_qualifier
)
14578 tree type_spec
= NULL_TREE
;
14581 cp_decl_spec ds
= ds_last
;
14583 /* Assume this type-specifier does not declare a new type. */
14584 if (declares_class_or_enum
)
14585 *declares_class_or_enum
= 0;
14586 /* And that it does not specify a cv-qualifier. */
14587 if (is_cv_qualifier
)
14588 *is_cv_qualifier
= false;
14589 /* Peek at the next token. */
14590 token
= cp_lexer_peek_token (parser
->lexer
);
14592 /* If we're looking at a keyword, we can use that to guide the
14593 production we choose. */
14594 keyword
= token
->keyword
;
14598 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14599 goto elaborated_type_specifier
;
14601 /* Look for the enum-specifier. */
14602 type_spec
= cp_parser_enum_specifier (parser
);
14603 /* If that worked, we're done. */
14606 if (declares_class_or_enum
)
14607 *declares_class_or_enum
= 2;
14609 cp_parser_set_decl_spec_type (decl_specs
,
14612 /*type_definition_p=*/true);
14616 goto elaborated_type_specifier
;
14618 /* Any of these indicate either a class-specifier, or an
14619 elaborated-type-specifier. */
14623 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14624 goto elaborated_type_specifier
;
14626 /* Parse tentatively so that we can back up if we don't find a
14627 class-specifier. */
14628 cp_parser_parse_tentatively (parser
);
14629 /* Look for the class-specifier. */
14630 type_spec
= cp_parser_class_specifier (parser
);
14631 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14632 /* If that worked, we're done. */
14633 if (cp_parser_parse_definitely (parser
))
14635 if (declares_class_or_enum
)
14636 *declares_class_or_enum
= 2;
14638 cp_parser_set_decl_spec_type (decl_specs
,
14641 /*type_definition_p=*/true);
14645 /* Fall through. */
14646 elaborated_type_specifier
:
14647 /* We're declaring (not defining) a class or enum. */
14648 if (declares_class_or_enum
)
14649 *declares_class_or_enum
= 1;
14651 /* Fall through. */
14653 /* Look for an elaborated-type-specifier. */
14655 = (cp_parser_elaborated_type_specifier
14657 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14660 cp_parser_set_decl_spec_type (decl_specs
,
14663 /*type_definition_p=*/false);
14668 if (is_cv_qualifier
)
14669 *is_cv_qualifier
= true;
14674 if (is_cv_qualifier
)
14675 *is_cv_qualifier
= true;
14680 if (is_cv_qualifier
)
14681 *is_cv_qualifier
= true;
14685 /* The `__complex__' keyword is a GNU extension. */
14693 /* Handle simple keywords. */
14698 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14699 decl_specs
->any_specifiers_p
= true;
14701 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14704 /* If we do not already have a type-specifier, assume we are looking
14705 at a simple-type-specifier. */
14706 type_spec
= cp_parser_simple_type_specifier (parser
,
14710 /* If we didn't find a type-specifier, and a type-specifier was not
14711 optional in this context, issue an error message. */
14712 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14714 cp_parser_error (parser
, "expected type specifier");
14715 return error_mark_node
;
14721 /* Parse a simple-type-specifier.
14723 simple-type-specifier:
14724 :: [opt] nested-name-specifier [opt] type-name
14725 :: [opt] nested-name-specifier template template-id
14740 simple-type-specifier:
14742 decltype ( expression )
14745 __underlying_type ( type-id )
14749 simple-type-specifier:
14751 __typeof__ unary-expression
14752 __typeof__ ( type-id )
14753 __typeof__ ( type-id ) { initializer-list , [opt] }
14755 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14756 appropriately updated. */
14759 cp_parser_simple_type_specifier (cp_parser
* parser
,
14760 cp_decl_specifier_seq
*decl_specs
,
14761 cp_parser_flags flags
)
14763 tree type
= NULL_TREE
;
14767 /* Peek at the next token. */
14768 token
= cp_lexer_peek_token (parser
->lexer
);
14770 /* If we're looking at a keyword, things are easy. */
14771 switch (token
->keyword
)
14775 decl_specs
->explicit_char_p
= true;
14776 type
= char_type_node
;
14779 type
= char16_type_node
;
14782 type
= char32_type_node
;
14785 type
= wchar_type_node
;
14788 type
= boolean_type_node
;
14791 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14792 type
= short_integer_type_node
;
14796 decl_specs
->explicit_int_p
= true;
14797 type
= integer_type_node
;
14803 idx
= token
->keyword
- RID_INT_N_0
;
14804 if (! int_n_enabled_p
[idx
])
14808 decl_specs
->explicit_intN_p
= true;
14809 decl_specs
->int_n_idx
= idx
;
14811 type
= int_n_trees
[idx
].signed_type
;
14815 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14816 type
= long_integer_type_node
;
14819 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14820 type
= integer_type_node
;
14823 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14824 type
= unsigned_type_node
;
14827 type
= float_type_node
;
14830 type
= double_type_node
;
14833 type
= void_type_node
;
14837 maybe_warn_cpp0x (CPP0X_AUTO
);
14838 if (parser
->auto_is_implicit_function_template_parm_p
)
14840 type
= synthesize_implicit_template_parm (parser
);
14842 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14844 if (cxx_dialect
< cxx14
)
14845 pedwarn (location_of (type
), 0,
14846 "use of %<auto%> in lambda parameter declaration "
14847 "only available with "
14848 "-std=c++14 or -std=gnu++14");
14850 else if (cxx_dialect
< cxx14
)
14851 pedwarn (location_of (type
), 0,
14852 "use of %<auto%> in parameter declaration "
14853 "only available with "
14854 "-std=c++14 or -std=gnu++14");
14856 pedwarn (location_of (type
), OPT_Wpedantic
,
14857 "ISO C++ forbids use of %<auto%> in parameter "
14861 type
= make_auto ();
14865 /* Since DR 743, decltype can either be a simple-type-specifier by
14866 itself or begin a nested-name-specifier. Parsing it will replace
14867 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14868 handling below decide what to do. */
14869 cp_parser_decltype (parser
);
14870 cp_lexer_set_token_position (parser
->lexer
, token
);
14874 /* Consume the `typeof' token. */
14875 cp_lexer_consume_token (parser
->lexer
);
14876 /* Parse the operand to `typeof'. */
14877 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14878 /* If it is not already a TYPE, take its type. */
14879 if (!TYPE_P (type
))
14880 type
= finish_typeof (type
);
14883 cp_parser_set_decl_spec_type (decl_specs
, type
,
14885 /*type_definition_p=*/false);
14889 case RID_UNDERLYING_TYPE
:
14890 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14892 cp_parser_set_decl_spec_type (decl_specs
, type
,
14894 /*type_definition_p=*/false);
14899 case RID_DIRECT_BASES
:
14900 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14902 cp_parser_set_decl_spec_type (decl_specs
, type
,
14904 /*type_definition_p=*/false);
14910 /* If token is an already-parsed decltype not followed by ::,
14911 it's a simple-type-specifier. */
14912 if (token
->type
== CPP_DECLTYPE
14913 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
14915 type
= token
->u
.value
;
14917 cp_parser_set_decl_spec_type (decl_specs
, type
,
14919 /*type_definition_p=*/false);
14920 cp_lexer_consume_token (parser
->lexer
);
14924 /* If the type-specifier was for a built-in type, we're done. */
14927 /* Record the type. */
14929 && (token
->keyword
!= RID_SIGNED
14930 && token
->keyword
!= RID_UNSIGNED
14931 && token
->keyword
!= RID_SHORT
14932 && token
->keyword
!= RID_LONG
))
14933 cp_parser_set_decl_spec_type (decl_specs
,
14936 /*type_definition_p=*/false);
14938 decl_specs
->any_specifiers_p
= true;
14940 /* Consume the token. */
14941 cp_lexer_consume_token (parser
->lexer
);
14943 /* There is no valid C++ program where a non-template type is
14944 followed by a "<". That usually indicates that the user thought
14945 that the type was a template. */
14946 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
14949 return TYPE_NAME (type
);
14952 /* The type-specifier must be a user-defined type. */
14953 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
14958 /* Don't gobble tokens or issue error messages if this is an
14959 optional type-specifier. */
14960 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
14961 cp_parser_parse_tentatively (parser
);
14963 /* Look for the optional `::' operator. */
14965 = (cp_parser_global_scope_opt (parser
,
14966 /*current_scope_valid_p=*/false)
14968 /* Look for the nested-name specifier. */
14970 = (cp_parser_nested_name_specifier_opt (parser
,
14971 /*typename_keyword_p=*/false,
14972 /*check_dependency_p=*/true,
14974 /*is_declaration=*/false)
14976 token
= cp_lexer_peek_token (parser
->lexer
);
14977 /* If we have seen a nested-name-specifier, and the next token
14978 is `template', then we are using the template-id production. */
14980 && cp_parser_optional_template_keyword (parser
))
14982 /* Look for the template-id. */
14983 type
= cp_parser_template_id (parser
,
14984 /*template_keyword_p=*/true,
14985 /*check_dependency_p=*/true,
14987 /*is_declaration=*/false);
14988 /* If the template-id did not name a type, we are out of
14990 if (TREE_CODE (type
) != TYPE_DECL
)
14992 cp_parser_error (parser
, "expected template-id for type");
14996 /* Otherwise, look for a type-name. */
14998 type
= cp_parser_type_name (parser
);
14999 /* Keep track of all name-lookups performed in class scopes. */
15003 && TREE_CODE (type
) == TYPE_DECL
15004 && identifier_p (DECL_NAME (type
)))
15005 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15006 /* If it didn't work out, we don't have a TYPE. */
15007 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15008 && !cp_parser_parse_definitely (parser
))
15010 if (type
&& decl_specs
)
15011 cp_parser_set_decl_spec_type (decl_specs
, type
,
15013 /*type_definition_p=*/false);
15016 /* If we didn't get a type-name, issue an error message. */
15017 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15019 cp_parser_error (parser
, "expected type-name");
15020 return error_mark_node
;
15023 if (type
&& type
!= error_mark_node
)
15025 /* See if TYPE is an Objective-C type, and if so, parse and
15026 accept any protocol references following it. Do this before
15027 the cp_parser_check_for_invalid_template_id() call, because
15028 Objective-C types can be followed by '<...>' which would
15029 enclose protocol names rather than template arguments, and so
15030 everything is fine. */
15031 if (c_dialect_objc () && !parser
->scope
15032 && (objc_is_id (type
) || objc_is_class_name (type
)))
15034 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15035 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15037 /* Clobber the "unqualified" type previously entered into
15038 DECL_SPECS with the new, improved protocol-qualified version. */
15040 decl_specs
->type
= qual_type
;
15045 /* There is no valid C++ program where a non-template type is
15046 followed by a "<". That usually indicates that the user
15047 thought that the type was a template. */
15048 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15056 /* Parse a type-name.
15062 simple-template-id [in c++0x]
15070 Returns a TYPE_DECL for the type. */
15073 cp_parser_type_name (cp_parser
* parser
)
15077 /* We can't know yet whether it is a class-name or not. */
15078 cp_parser_parse_tentatively (parser
);
15079 /* Try a class-name. */
15080 type_decl
= cp_parser_class_name (parser
,
15081 /*typename_keyword_p=*/false,
15082 /*template_keyword_p=*/false,
15084 /*check_dependency_p=*/true,
15085 /*class_head_p=*/false,
15086 /*is_declaration=*/false);
15087 /* If it's not a class-name, keep looking. */
15088 if (!cp_parser_parse_definitely (parser
))
15090 if (cxx_dialect
< cxx11
)
15091 /* It must be a typedef-name or an enum-name. */
15092 return cp_parser_nonclass_name (parser
);
15094 cp_parser_parse_tentatively (parser
);
15095 /* It is either a simple-template-id representing an
15096 instantiation of an alias template... */
15097 type_decl
= cp_parser_template_id (parser
,
15098 /*template_keyword_p=*/false,
15099 /*check_dependency_p=*/true,
15101 /*is_declaration=*/false);
15102 /* Note that this must be an instantiation of an alias template
15103 because [temp.names]/6 says:
15105 A template-id that names an alias template specialization
15108 Whereas [temp.names]/7 says:
15110 A simple-template-id that names a class template
15111 specialization is a class-name. */
15112 if (type_decl
!= NULL_TREE
15113 && TREE_CODE (type_decl
) == TYPE_DECL
15114 && TYPE_DECL_ALIAS_P (type_decl
))
15115 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15117 cp_parser_simulate_error (parser
);
15119 if (!cp_parser_parse_definitely (parser
))
15120 /* ... Or a typedef-name or an enum-name. */
15121 return cp_parser_nonclass_name (parser
);
15127 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15135 Returns a TYPE_DECL for the type. */
15138 cp_parser_nonclass_name (cp_parser
* parser
)
15143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15144 identifier
= cp_parser_identifier (parser
);
15145 if (identifier
== error_mark_node
)
15146 return error_mark_node
;
15148 /* Look up the type-name. */
15149 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15151 type_decl
= strip_using_decl (type_decl
);
15153 if (TREE_CODE (type_decl
) != TYPE_DECL
15154 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15156 /* See if this is an Objective-C type. */
15157 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15158 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15160 type_decl
= TYPE_NAME (type
);
15163 /* Issue an error if we did not find a type-name. */
15164 if (TREE_CODE (type_decl
) != TYPE_DECL
15165 /* In Objective-C, we have the complication that class names are
15166 normally type names and start declarations (eg, the
15167 "NSObject" in "NSObject *object;"), but can be used in an
15168 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15169 is an expression. So, a classname followed by a dot is not a
15170 valid type-name. */
15171 || (objc_is_class_name (TREE_TYPE (type_decl
))
15172 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15174 if (!cp_parser_simulate_error (parser
))
15175 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15176 NLE_TYPE
, token
->location
);
15177 return error_mark_node
;
15179 /* Remember that the name was used in the definition of the
15180 current class so that we can check later to see if the
15181 meaning would have been different after the class was
15182 entirely defined. */
15183 else if (type_decl
!= error_mark_node
15185 maybe_note_name_used_in_class (identifier
, type_decl
);
15190 /* Parse an elaborated-type-specifier. Note that the grammar given
15191 here incorporates the resolution to DR68.
15193 elaborated-type-specifier:
15194 class-key :: [opt] nested-name-specifier [opt] identifier
15195 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15196 enum-key :: [opt] nested-name-specifier [opt] identifier
15197 typename :: [opt] nested-name-specifier identifier
15198 typename :: [opt] nested-name-specifier template [opt]
15203 elaborated-type-specifier:
15204 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15205 class-key attributes :: [opt] nested-name-specifier [opt]
15206 template [opt] template-id
15207 enum attributes :: [opt] nested-name-specifier [opt] identifier
15209 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15210 declared `friend'. If IS_DECLARATION is TRUE, then this
15211 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15212 something is being declared.
15214 Returns the TYPE specified. */
15217 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15219 bool is_declaration
)
15221 enum tag_types tag_type
;
15223 tree type
= NULL_TREE
;
15224 tree attributes
= NULL_TREE
;
15226 cp_token
*token
= NULL
;
15228 /* See if we're looking at the `enum' keyword. */
15229 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15231 /* Consume the `enum' token. */
15232 cp_lexer_consume_token (parser
->lexer
);
15233 /* Remember that it's an enumeration type. */
15234 tag_type
= enum_type
;
15235 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15236 enums) is used here. */
15237 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15238 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15240 pedwarn (input_location
, 0, "elaborated-type-specifier "
15241 "for a scoped enum must not use the %<%D%> keyword",
15242 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15243 /* Consume the `struct' or `class' and parse it anyway. */
15244 cp_lexer_consume_token (parser
->lexer
);
15246 /* Parse the attributes. */
15247 attributes
= cp_parser_attributes_opt (parser
);
15249 /* Or, it might be `typename'. */
15250 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15253 /* Consume the `typename' token. */
15254 cp_lexer_consume_token (parser
->lexer
);
15255 /* Remember that it's a `typename' type. */
15256 tag_type
= typename_type
;
15258 /* Otherwise it must be a class-key. */
15261 tag_type
= cp_parser_class_key (parser
);
15262 if (tag_type
== none_type
)
15263 return error_mark_node
;
15264 /* Parse the attributes. */
15265 attributes
= cp_parser_attributes_opt (parser
);
15268 /* Look for the `::' operator. */
15269 globalscope
= cp_parser_global_scope_opt (parser
,
15270 /*current_scope_valid_p=*/false);
15271 /* Look for the nested-name-specifier. */
15272 if (tag_type
== typename_type
&& !globalscope
)
15274 if (!cp_parser_nested_name_specifier (parser
,
15275 /*typename_keyword_p=*/true,
15276 /*check_dependency_p=*/true,
15279 return error_mark_node
;
15282 /* Even though `typename' is not present, the proposed resolution
15283 to Core Issue 180 says that in `class A<T>::B', `B' should be
15284 considered a type-name, even if `A<T>' is dependent. */
15285 cp_parser_nested_name_specifier_opt (parser
,
15286 /*typename_keyword_p=*/true,
15287 /*check_dependency_p=*/true,
15290 /* For everything but enumeration types, consider a template-id.
15291 For an enumeration type, consider only a plain identifier. */
15292 if (tag_type
!= enum_type
)
15294 bool template_p
= false;
15297 /* Allow the `template' keyword. */
15298 template_p
= cp_parser_optional_template_keyword (parser
);
15299 /* If we didn't see `template', we don't know if there's a
15300 template-id or not. */
15302 cp_parser_parse_tentatively (parser
);
15303 /* Parse the template-id. */
15304 token
= cp_lexer_peek_token (parser
->lexer
);
15305 decl
= cp_parser_template_id (parser
, template_p
,
15306 /*check_dependency_p=*/true,
15309 /* If we didn't find a template-id, look for an ordinary
15311 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15313 /* We can get here when cp_parser_template_id, called by
15314 cp_parser_class_name with tag_type == none_type, succeeds
15315 and caches a BASELINK. Then, when called again here,
15316 instead of failing and returning an error_mark_node
15317 returns it (see template/typename17.C in C++11).
15318 ??? Could we diagnose this earlier? */
15319 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15321 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15322 type
= error_mark_node
;
15324 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15325 in effect, then we must assume that, upon instantiation, the
15326 template will correspond to a class. */
15327 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15328 && tag_type
== typename_type
)
15329 type
= make_typename_type (parser
->scope
, decl
,
15331 /*complain=*/tf_error
);
15332 /* If the `typename' keyword is in effect and DECL is not a type
15333 decl, then type is non existent. */
15334 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15336 else if (TREE_CODE (decl
) == TYPE_DECL
)
15337 type
= check_elaborated_type_specifier (tag_type
, decl
,
15338 /*allow_template_p=*/true);
15339 else if (decl
== error_mark_node
)
15340 type
= error_mark_node
;
15345 token
= cp_lexer_peek_token (parser
->lexer
);
15346 identifier
= cp_parser_identifier (parser
);
15348 if (identifier
== error_mark_node
)
15350 parser
->scope
= NULL_TREE
;
15351 return error_mark_node
;
15354 /* For a `typename', we needn't call xref_tag. */
15355 if (tag_type
== typename_type
15356 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15357 return cp_parser_make_typename_type (parser
, identifier
,
15360 /* Template parameter lists apply only if we are not within a
15361 function parameter list. */
15362 bool template_parm_lists_apply
15363 = parser
->num_template_parameter_lists
;
15364 if (template_parm_lists_apply
)
15365 for (cp_binding_level
*s
= current_binding_level
;
15366 s
&& s
->kind
!= sk_template_parms
;
15367 s
= s
->level_chain
)
15368 if (s
->kind
== sk_function_parms
)
15369 template_parm_lists_apply
= false;
15371 /* Look up a qualified name in the usual way. */
15375 tree ambiguous_decls
;
15377 decl
= cp_parser_lookup_name (parser
, identifier
,
15379 /*is_template=*/false,
15380 /*is_namespace=*/false,
15381 /*check_dependency=*/true,
15385 /* If the lookup was ambiguous, an error will already have been
15387 if (ambiguous_decls
)
15388 return error_mark_node
;
15390 /* If we are parsing friend declaration, DECL may be a
15391 TEMPLATE_DECL tree node here. However, we need to check
15392 whether this TEMPLATE_DECL results in valid code. Consider
15393 the following example:
15396 template <class T> class C {};
15399 template <class T> friend class N::C; // #1, valid code
15401 template <class T> class Y {
15402 friend class N::C; // #2, invalid code
15405 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15406 name lookup of `N::C'. We see that friend declaration must
15407 be template for the code to be valid. Note that
15408 processing_template_decl does not work here since it is
15409 always 1 for the above two cases. */
15411 decl
= (cp_parser_maybe_treat_template_as_class
15412 (decl
, /*tag_name_p=*/is_friend
15413 && template_parm_lists_apply
));
15415 if (TREE_CODE (decl
) != TYPE_DECL
)
15417 cp_parser_diagnose_invalid_type_name (parser
,
15420 return error_mark_node
;
15423 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15425 bool allow_template
= (template_parm_lists_apply
15426 || DECL_SELF_REFERENCE_P (decl
));
15427 type
= check_elaborated_type_specifier (tag_type
, decl
,
15430 if (type
== error_mark_node
)
15431 return error_mark_node
;
15434 /* Forward declarations of nested types, such as
15439 are invalid unless all components preceding the final '::'
15440 are complete. If all enclosing types are complete, these
15441 declarations become merely pointless.
15443 Invalid forward declarations of nested types are errors
15444 caught elsewhere in parsing. Those that are pointless arrive
15447 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15448 && !is_friend
&& !processing_explicit_instantiation
)
15449 warning (0, "declaration %qD does not declare anything", decl
);
15451 type
= TREE_TYPE (decl
);
15455 /* An elaborated-type-specifier sometimes introduces a new type and
15456 sometimes names an existing type. Normally, the rule is that it
15457 introduces a new type only if there is not an existing type of
15458 the same name already in scope. For example, given:
15461 void f() { struct S s; }
15463 the `struct S' in the body of `f' is the same `struct S' as in
15464 the global scope; the existing definition is used. However, if
15465 there were no global declaration, this would introduce a new
15466 local class named `S'.
15468 An exception to this rule applies to the following code:
15470 namespace N { struct S; }
15472 Here, the elaborated-type-specifier names a new type
15473 unconditionally; even if there is already an `S' in the
15474 containing scope this declaration names a new type.
15475 This exception only applies if the elaborated-type-specifier
15476 forms the complete declaration:
15480 A declaration consisting solely of `class-key identifier ;' is
15481 either a redeclaration of the name in the current scope or a
15482 forward declaration of the identifier as a class name. It
15483 introduces the name into the current scope.
15485 We are in this situation precisely when the next token is a `;'.
15487 An exception to the exception is that a `friend' declaration does
15488 *not* name a new type; i.e., given:
15490 struct S { friend struct T; };
15492 `T' is not a new type in the scope of `S'.
15494 Also, `new struct S' or `sizeof (struct S)' never results in the
15495 definition of a new type; a new type can only be declared in a
15496 declaration context. */
15502 /* Friends have special name lookup rules. */
15503 ts
= ts_within_enclosing_non_class
;
15504 else if (is_declaration
15505 && cp_lexer_next_token_is (parser
->lexer
,
15507 /* This is a `class-key identifier ;' */
15513 (template_parm_lists_apply
15514 && (cp_parser_next_token_starts_class_definition_p (parser
)
15515 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15516 /* An unqualified name was used to reference this type, so
15517 there were no qualifying templates. */
15518 if (template_parm_lists_apply
15519 && !cp_parser_check_template_parameters (parser
,
15520 /*num_templates=*/0,
15522 /*declarator=*/NULL
))
15523 return error_mark_node
;
15524 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15528 if (type
== error_mark_node
)
15529 return error_mark_node
;
15531 /* Allow attributes on forward declarations of classes. */
15534 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15535 warning (OPT_Wattributes
,
15536 "attributes ignored on uninstantiated type");
15537 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15538 && ! processing_explicit_instantiation
)
15539 warning (OPT_Wattributes
,
15540 "attributes ignored on template instantiation");
15541 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15542 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15544 warning (OPT_Wattributes
,
15545 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15548 if (tag_type
!= enum_type
)
15550 /* Indicate whether this class was declared as a `class' or as a
15552 if (TREE_CODE (type
) == RECORD_TYPE
)
15553 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15554 cp_parser_check_class_key (tag_type
, type
);
15557 /* A "<" cannot follow an elaborated type specifier. If that
15558 happens, the user was probably trying to form a template-id. */
15559 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15565 /* Parse an enum-specifier.
15568 enum-head { enumerator-list [opt] }
15569 enum-head { enumerator-list , } [C++0x]
15572 enum-key identifier [opt] enum-base [opt]
15573 enum-key nested-name-specifier identifier enum-base [opt]
15578 enum struct [C++0x]
15581 : type-specifier-seq
15583 opaque-enum-specifier:
15584 enum-key identifier enum-base [opt] ;
15587 enum-key attributes[opt] identifier [opt] enum-base [opt]
15588 { enumerator-list [opt] }attributes[opt]
15589 enum-key attributes[opt] identifier [opt] enum-base [opt]
15590 { enumerator-list, }attributes[opt] [C++0x]
15592 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15593 if the token stream isn't an enum-specifier after all. */
15596 cp_parser_enum_specifier (cp_parser
* parser
)
15599 tree type
= NULL_TREE
;
15601 tree nested_name_specifier
= NULL_TREE
;
15603 bool scoped_enum_p
= false;
15604 bool has_underlying_type
= false;
15605 bool nested_being_defined
= false;
15606 bool new_value_list
= false;
15607 bool is_new_type
= false;
15608 bool is_anonymous
= false;
15609 tree underlying_type
= NULL_TREE
;
15610 cp_token
*type_start_token
= NULL
;
15611 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15613 parser
->colon_corrects_to_scope_p
= false;
15615 /* Parse tentatively so that we can back up if we don't find a
15617 cp_parser_parse_tentatively (parser
);
15619 /* Caller guarantees that the current token is 'enum', an identifier
15620 possibly follows, and the token after that is an opening brace.
15621 If we don't have an identifier, fabricate an anonymous name for
15622 the enumeration being defined. */
15623 cp_lexer_consume_token (parser
->lexer
);
15625 /* Parse the "class" or "struct", which indicates a scoped
15626 enumeration type in C++0x. */
15627 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15628 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15630 if (cxx_dialect
< cxx11
)
15631 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15633 /* Consume the `struct' or `class' token. */
15634 cp_lexer_consume_token (parser
->lexer
);
15636 scoped_enum_p
= true;
15639 attributes
= cp_parser_attributes_opt (parser
);
15641 /* Clear the qualification. */
15642 parser
->scope
= NULL_TREE
;
15643 parser
->qualifying_scope
= NULL_TREE
;
15644 parser
->object_scope
= NULL_TREE
;
15646 /* Figure out in what scope the declaration is being placed. */
15647 prev_scope
= current_scope ();
15649 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15651 push_deferring_access_checks (dk_no_check
);
15652 nested_name_specifier
15653 = cp_parser_nested_name_specifier_opt (parser
,
15654 /*typename_keyword_p=*/true,
15655 /*check_dependency_p=*/false,
15657 /*is_declaration=*/false);
15659 if (nested_name_specifier
)
15663 identifier
= cp_parser_identifier (parser
);
15664 name
= cp_parser_lookup_name (parser
, identifier
,
15666 /*is_template=*/false,
15667 /*is_namespace=*/false,
15668 /*check_dependency=*/true,
15669 /*ambiguous_decls=*/NULL
,
15671 if (name
&& name
!= error_mark_node
)
15673 type
= TREE_TYPE (name
);
15674 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15676 /* Are template enums allowed in ISO? */
15677 if (template_parm_scope_p ())
15678 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15679 "%qD is an enumeration template", name
);
15680 /* ignore a typename reference, for it will be solved by name
15685 else if (nested_name_specifier
== error_mark_node
)
15686 /* We already issued an error. */;
15688 error_at (type_start_token
->location
,
15689 "%qD is not an enumerator-name", identifier
);
15693 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15694 identifier
= cp_parser_identifier (parser
);
15697 identifier
= make_anon_name ();
15698 is_anonymous
= true;
15700 error_at (type_start_token
->location
,
15701 "anonymous scoped enum is not allowed");
15704 pop_deferring_access_checks ();
15706 /* Check for the `:' that denotes a specified underlying type in C++0x.
15707 Note that a ':' could also indicate a bitfield width, however. */
15708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15710 cp_decl_specifier_seq type_specifiers
;
15712 /* Consume the `:'. */
15713 cp_lexer_consume_token (parser
->lexer
);
15715 /* Parse the type-specifier-seq. */
15716 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15717 /*is_trailing_return=*/false,
15720 /* At this point this is surely not elaborated type specifier. */
15721 if (!cp_parser_parse_definitely (parser
))
15724 if (cxx_dialect
< cxx11
)
15725 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15727 has_underlying_type
= true;
15729 /* If that didn't work, stop. */
15730 if (type_specifiers
.type
!= error_mark_node
)
15732 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15733 /*initialized=*/0, NULL
);
15734 if (underlying_type
== error_mark_node
15735 || check_for_bare_parameter_packs (underlying_type
))
15736 underlying_type
= NULL_TREE
;
15740 /* Look for the `{' but don't consume it yet. */
15741 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15743 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15745 cp_parser_error (parser
, "expected %<{%>");
15746 if (has_underlying_type
)
15752 /* An opaque-enum-specifier must have a ';' here. */
15753 if ((scoped_enum_p
|| underlying_type
)
15754 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15756 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15757 if (has_underlying_type
)
15765 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15768 if (nested_name_specifier
)
15770 if (CLASS_TYPE_P (nested_name_specifier
))
15772 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15773 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15774 push_scope (nested_name_specifier
);
15776 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15778 push_nested_namespace (nested_name_specifier
);
15782 /* Issue an error message if type-definitions are forbidden here. */
15783 if (!cp_parser_check_type_definition (parser
))
15784 type
= error_mark_node
;
15786 /* Create the new type. We do this before consuming the opening
15787 brace so the enum will be recorded as being on the line of its
15788 tag (or the 'enum' keyword, if there is no tag). */
15789 type
= start_enum (identifier
, type
, underlying_type
,
15790 scoped_enum_p
, &is_new_type
);
15792 /* If the next token is not '{' it is an opaque-enum-specifier or an
15793 elaborated-type-specifier. */
15794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15796 timevar_push (TV_PARSE_ENUM
);
15797 if (nested_name_specifier
15798 && nested_name_specifier
!= error_mark_node
)
15800 /* The following catches invalid code such as:
15801 enum class S<int>::E { A, B, C }; */
15802 if (!processing_specialization
15803 && CLASS_TYPE_P (nested_name_specifier
)
15804 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15805 error_at (type_start_token
->location
, "cannot add an enumerator "
15806 "list to a template instantiation");
15808 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15810 error_at (type_start_token
->location
,
15811 "%<%T::%E%> has not been declared",
15812 TYPE_CONTEXT (nested_name_specifier
),
15813 nested_name_specifier
);
15814 type
= error_mark_node
;
15816 /* If that scope does not contain the scope in which the
15817 class was originally declared, the program is invalid. */
15818 else if (prev_scope
&& !is_ancestor (prev_scope
,
15819 nested_name_specifier
))
15821 if (at_namespace_scope_p ())
15822 error_at (type_start_token
->location
,
15823 "declaration of %qD in namespace %qD which does not "
15825 type
, prev_scope
, nested_name_specifier
);
15827 error_at (type_start_token
->location
,
15828 "declaration of %qD in %qD which does not "
15830 type
, prev_scope
, nested_name_specifier
);
15831 type
= error_mark_node
;
15836 begin_scope (sk_scoped_enum
, type
);
15838 /* Consume the opening brace. */
15839 cp_lexer_consume_token (parser
->lexer
);
15841 if (type
== error_mark_node
)
15842 ; /* Nothing to add */
15843 else if (OPAQUE_ENUM_P (type
)
15844 || (cxx_dialect
> cxx98
&& processing_specialization
))
15846 new_value_list
= true;
15847 SET_OPAQUE_ENUM_P (type
, false);
15848 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15852 error_at (type_start_token
->location
,
15853 "multiple definition of %q#T", type
);
15854 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15855 "previous definition here");
15856 type
= error_mark_node
;
15859 if (type
== error_mark_node
)
15860 cp_parser_skip_to_end_of_block_or_statement (parser
);
15861 /* If the next token is not '}', then there are some enumerators. */
15862 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15864 if (is_anonymous
&& !scoped_enum_p
)
15865 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15866 "ISO C++ forbids empty anonymous enum");
15869 cp_parser_enumerator_list (parser
, type
);
15871 /* Consume the final '}'. */
15872 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15876 timevar_pop (TV_PARSE_ENUM
);
15880 /* If a ';' follows, then it is an opaque-enum-specifier
15881 and additional restrictions apply. */
15882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15885 error_at (type_start_token
->location
,
15886 "opaque-enum-specifier without name");
15887 else if (nested_name_specifier
)
15888 error_at (type_start_token
->location
,
15889 "opaque-enum-specifier must use a simple identifier");
15893 /* Look for trailing attributes to apply to this enumeration, and
15894 apply them if appropriate. */
15895 if (cp_parser_allow_gnu_extensions_p (parser
))
15897 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15898 trailing_attr
= chainon (trailing_attr
, attributes
);
15899 cplus_decl_attributes (&type
,
15901 (int) ATTR_FLAG_TYPE_IN_PLACE
);
15904 /* Finish up the enumeration. */
15905 if (type
!= error_mark_node
)
15907 if (new_value_list
)
15908 finish_enum_value_list (type
);
15910 finish_enum (type
);
15913 if (nested_name_specifier
)
15915 if (CLASS_TYPE_P (nested_name_specifier
))
15917 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
15918 pop_scope (nested_name_specifier
);
15920 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15922 pop_nested_namespace (nested_name_specifier
);
15926 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
15930 /* Parse an enumerator-list. The enumerators all have the indicated
15934 enumerator-definition
15935 enumerator-list , enumerator-definition */
15938 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
15942 /* Parse an enumerator-definition. */
15943 cp_parser_enumerator_definition (parser
, type
);
15945 /* If the next token is not a ',', we've reached the end of
15947 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15949 /* Otherwise, consume the `,' and keep going. */
15950 cp_lexer_consume_token (parser
->lexer
);
15951 /* If the next token is a `}', there is a trailing comma. */
15952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15954 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
15955 pedwarn (input_location
, OPT_Wpedantic
,
15956 "comma at end of enumerator list");
15962 /* Parse an enumerator-definition. The enumerator has the indicated
15965 enumerator-definition:
15967 enumerator = constant-expression
15973 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
15979 /* Save the input location because we are interested in the location
15980 of the identifier and not the location of the explicit value. */
15981 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15983 /* Look for the identifier. */
15984 identifier
= cp_parser_identifier (parser
);
15985 if (identifier
== error_mark_node
)
15988 /* If the next token is an '=', then there is an explicit value. */
15989 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
15991 /* Consume the `=' token. */
15992 cp_lexer_consume_token (parser
->lexer
);
15993 /* Parse the value. */
15994 value
= cp_parser_constant_expression (parser
);
15999 /* If we are processing a template, make sure the initializer of the
16000 enumerator doesn't contain any bare template parameter pack. */
16001 if (check_for_bare_parameter_packs (value
))
16002 value
= error_mark_node
;
16004 /* integral_constant_value will pull out this expression, so make sure
16005 it's folded as appropriate. */
16006 value
= fold_non_dependent_expr (value
);
16008 /* Create the enumerator. */
16009 build_enumerator (identifier
, value
, type
, loc
);
16012 /* Parse a namespace-name.
16015 original-namespace-name
16018 Returns the NAMESPACE_DECL for the namespace. */
16021 cp_parser_namespace_name (cp_parser
* parser
)
16024 tree namespace_decl
;
16026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16028 /* Get the name of the namespace. */
16029 identifier
= cp_parser_identifier (parser
);
16030 if (identifier
== error_mark_node
)
16031 return error_mark_node
;
16033 /* Look up the identifier in the currently active scope. Look only
16034 for namespaces, due to:
16036 [basic.lookup.udir]
16038 When looking up a namespace-name in a using-directive or alias
16039 definition, only namespace names are considered.
16043 [basic.lookup.qual]
16045 During the lookup of a name preceding the :: scope resolution
16046 operator, object, function, and enumerator names are ignored.
16048 (Note that cp_parser_qualifying_entity only calls this
16049 function if the token after the name is the scope resolution
16051 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16053 /*is_template=*/false,
16054 /*is_namespace=*/true,
16055 /*check_dependency=*/true,
16056 /*ambiguous_decls=*/NULL
,
16058 /* If it's not a namespace, issue an error. */
16059 if (namespace_decl
== error_mark_node
16060 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16062 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16063 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16064 cp_parser_error (parser
, "expected namespace-name");
16065 namespace_decl
= error_mark_node
;
16068 return namespace_decl
;
16071 /* Parse a namespace-definition.
16073 namespace-definition:
16074 named-namespace-definition
16075 unnamed-namespace-definition
16077 named-namespace-definition:
16078 original-namespace-definition
16079 extension-namespace-definition
16081 original-namespace-definition:
16082 namespace identifier { namespace-body }
16084 extension-namespace-definition:
16085 namespace original-namespace-name { namespace-body }
16087 unnamed-namespace-definition:
16088 namespace { namespace-body } */
16091 cp_parser_namespace_definition (cp_parser
* parser
)
16093 tree identifier
, attribs
;
16094 bool has_visibility
;
16097 cp_ensure_no_omp_declare_simd (parser
);
16098 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16100 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16102 cp_lexer_consume_token (parser
->lexer
);
16107 /* Look for the `namespace' keyword. */
16108 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16110 /* Get the name of the namespace. We do not attempt to distinguish
16111 between an original-namespace-definition and an
16112 extension-namespace-definition at this point. The semantic
16113 analysis routines are responsible for that. */
16114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16115 identifier
= cp_parser_identifier (parser
);
16117 identifier
= NULL_TREE
;
16119 /* Parse any specified attributes. */
16120 attribs
= cp_parser_attributes_opt (parser
);
16122 /* Look for the `{' to start the namespace. */
16123 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16124 /* Start the namespace. */
16125 push_namespace (identifier
);
16127 /* "inline namespace" is equivalent to a stub namespace definition
16128 followed by a strong using directive. */
16131 tree name_space
= current_namespace
;
16132 /* Set up namespace association. */
16133 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16134 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16135 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16136 /* Import the contents of the inline namespace. */
16138 do_using_directive (name_space
);
16139 push_namespace (identifier
);
16142 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16144 /* Parse the body of the namespace. */
16145 cp_parser_namespace_body (parser
);
16147 if (has_visibility
)
16148 pop_visibility (1);
16150 /* Finish the namespace. */
16152 /* Look for the final `}'. */
16153 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16156 /* Parse a namespace-body.
16159 declaration-seq [opt] */
16162 cp_parser_namespace_body (cp_parser
* parser
)
16164 cp_parser_declaration_seq_opt (parser
);
16167 /* Parse a namespace-alias-definition.
16169 namespace-alias-definition:
16170 namespace identifier = qualified-namespace-specifier ; */
16173 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16176 tree namespace_specifier
;
16178 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16180 /* Look for the `namespace' keyword. */
16181 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16182 /* Look for the identifier. */
16183 identifier
= cp_parser_identifier (parser
);
16184 if (identifier
== error_mark_node
)
16186 /* Look for the `=' token. */
16187 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16188 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16190 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16191 /* Skip the definition. */
16192 cp_lexer_consume_token (parser
->lexer
);
16193 if (cp_parser_skip_to_closing_brace (parser
))
16194 cp_lexer_consume_token (parser
->lexer
);
16197 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16198 /* Look for the qualified-namespace-specifier. */
16199 namespace_specifier
16200 = cp_parser_qualified_namespace_specifier (parser
);
16201 /* Look for the `;' token. */
16202 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16204 /* Register the alias in the symbol table. */
16205 do_namespace_alias (identifier
, namespace_specifier
);
16208 /* Parse a qualified-namespace-specifier.
16210 qualified-namespace-specifier:
16211 :: [opt] nested-name-specifier [opt] namespace-name
16213 Returns a NAMESPACE_DECL corresponding to the specified
16217 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16219 /* Look for the optional `::'. */
16220 cp_parser_global_scope_opt (parser
,
16221 /*current_scope_valid_p=*/false);
16223 /* Look for the optional nested-name-specifier. */
16224 cp_parser_nested_name_specifier_opt (parser
,
16225 /*typename_keyword_p=*/false,
16226 /*check_dependency_p=*/true,
16228 /*is_declaration=*/true);
16230 return cp_parser_namespace_name (parser
);
16233 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16234 access declaration.
16237 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16238 using :: unqualified-id ;
16240 access-declaration:
16246 cp_parser_using_declaration (cp_parser
* parser
,
16247 bool access_declaration_p
)
16250 bool typename_p
= false;
16251 bool global_scope_p
;
16255 int oldcount
= errorcount
;
16256 cp_token
*diag_token
= NULL
;
16258 if (access_declaration_p
)
16260 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16261 cp_parser_parse_tentatively (parser
);
16265 /* Look for the `using' keyword. */
16266 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16268 /* Peek at the next token. */
16269 token
= cp_lexer_peek_token (parser
->lexer
);
16270 /* See if it's `typename'. */
16271 if (token
->keyword
== RID_TYPENAME
)
16273 /* Remember that we've seen it. */
16275 /* Consume the `typename' token. */
16276 cp_lexer_consume_token (parser
->lexer
);
16280 /* Look for the optional global scope qualification. */
16282 = (cp_parser_global_scope_opt (parser
,
16283 /*current_scope_valid_p=*/false)
16286 /* If we saw `typename', or didn't see `::', then there must be a
16287 nested-name-specifier present. */
16288 if (typename_p
|| !global_scope_p
)
16290 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16291 /*check_dependency_p=*/true,
16293 /*is_declaration=*/true);
16294 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16296 cp_parser_skip_to_end_of_block_or_statement (parser
);
16300 /* Otherwise, we could be in either of the two productions. In that
16301 case, treat the nested-name-specifier as optional. */
16303 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16304 /*typename_keyword_p=*/false,
16305 /*check_dependency_p=*/true,
16307 /*is_declaration=*/true);
16309 qscope
= global_namespace
;
16310 else if (UNSCOPED_ENUM_P (qscope
))
16311 qscope
= CP_TYPE_CONTEXT (qscope
);
16313 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16314 /* Something has already gone wrong; there's no need to parse
16315 further. Since an error has occurred, the return value of
16316 cp_parser_parse_definitely will be false, as required. */
16317 return cp_parser_parse_definitely (parser
);
16319 token
= cp_lexer_peek_token (parser
->lexer
);
16320 /* Parse the unqualified-id. */
16321 identifier
= cp_parser_unqualified_id (parser
,
16322 /*template_keyword_p=*/false,
16323 /*check_dependency_p=*/true,
16324 /*declarator_p=*/true,
16325 /*optional_p=*/false);
16327 if (access_declaration_p
)
16329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16330 cp_parser_simulate_error (parser
);
16331 if (!cp_parser_parse_definitely (parser
))
16335 /* The function we call to handle a using-declaration is different
16336 depending on what scope we are in. */
16337 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16339 else if (!identifier_p (identifier
)
16340 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16341 /* [namespace.udecl]
16343 A using declaration shall not name a template-id. */
16344 error_at (token
->location
,
16345 "a template-id may not appear in a using-declaration");
16348 if (at_class_scope_p ())
16350 /* Create the USING_DECL. */
16351 decl
= do_class_using_decl (parser
->scope
, identifier
);
16353 if (decl
&& typename_p
)
16354 USING_DECL_TYPENAME_P (decl
) = 1;
16356 if (check_for_bare_parameter_packs (decl
))
16358 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16362 /* Add it to the list of members in this class. */
16363 finish_member_declaration (decl
);
16367 decl
= cp_parser_lookup_name_simple (parser
,
16370 if (decl
== error_mark_node
)
16371 cp_parser_name_lookup_error (parser
, identifier
,
16374 else if (check_for_bare_parameter_packs (decl
))
16376 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16379 else if (!at_namespace_scope_p ())
16380 do_local_using_decl (decl
, qscope
, identifier
);
16382 do_toplevel_using_decl (decl
, qscope
, identifier
);
16386 /* Look for the final `;'. */
16387 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16389 if (access_declaration_p
&& errorcount
== oldcount
)
16390 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16391 "access declarations are deprecated "
16392 "in favour of using-declarations; "
16393 "suggestion: add the %<using%> keyword");
16398 /* Parse an alias-declaration.
16401 using identifier attribute-specifier-seq [opt] = type-id */
16404 cp_parser_alias_declaration (cp_parser
* parser
)
16406 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16407 location_t id_location
;
16408 cp_declarator
*declarator
;
16409 cp_decl_specifier_seq decl_specs
;
16411 const char *saved_message
= NULL
;
16413 /* Look for the `using' keyword. */
16414 cp_token
*using_token
16415 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16416 if (using_token
== NULL
)
16417 return error_mark_node
;
16419 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16420 id
= cp_parser_identifier (parser
);
16421 if (id
== error_mark_node
)
16422 return error_mark_node
;
16424 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16425 attributes
= cp_parser_attributes_opt (parser
);
16426 if (attributes
== error_mark_node
)
16427 return error_mark_node
;
16429 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16431 if (cp_parser_error_occurred (parser
))
16432 return error_mark_node
;
16434 cp_parser_commit_to_tentative_parse (parser
);
16436 /* Now we are going to parse the type-id of the declaration. */
16441 "A type-specifier-seq shall not define a class or enumeration
16442 unless it appears in the type-id of an alias-declaration (7.1.3) that
16443 is not the declaration of a template-declaration."
16445 In other words, if we currently are in an alias template, the
16446 type-id should not define a type.
16448 So let's set parser->type_definition_forbidden_message in that
16449 case; cp_parser_check_type_definition (called by
16450 cp_parser_class_specifier) will then emit an error if a type is
16451 defined in the type-id. */
16452 if (parser
->num_template_parameter_lists
)
16454 saved_message
= parser
->type_definition_forbidden_message
;
16455 parser
->type_definition_forbidden_message
=
16456 G_("types may not be defined in alias template declarations");
16459 type
= cp_parser_type_id (parser
);
16461 /* Restore the error message if need be. */
16462 if (parser
->num_template_parameter_lists
)
16463 parser
->type_definition_forbidden_message
= saved_message
;
16465 if (type
== error_mark_node
16466 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16468 cp_parser_skip_to_end_of_block_or_statement (parser
);
16469 return error_mark_node
;
16472 /* A typedef-name can also be introduced by an alias-declaration. The
16473 identifier following the using keyword becomes a typedef-name. It has
16474 the same semantics as if it were introduced by the typedef
16475 specifier. In particular, it does not define a new type and it shall
16476 not appear in the type-id. */
16478 clear_decl_specs (&decl_specs
);
16479 decl_specs
.type
= type
;
16480 if (attributes
!= NULL_TREE
)
16482 decl_specs
.attributes
= attributes
;
16483 set_and_check_decl_spec_loc (&decl_specs
,
16487 set_and_check_decl_spec_loc (&decl_specs
,
16490 set_and_check_decl_spec_loc (&decl_specs
,
16494 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16495 declarator
->id_loc
= id_location
;
16497 member_p
= at_class_scope_p ();
16499 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16500 NULL_TREE
, attributes
);
16502 decl
= start_decl (declarator
, &decl_specs
, 0,
16503 attributes
, NULL_TREE
, &pushed_scope
);
16504 if (decl
== error_mark_node
)
16507 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16510 pop_scope (pushed_scope
);
16512 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16513 added into the symbol table; otherwise, return the TYPE_DECL. */
16514 if (DECL_LANG_SPECIFIC (decl
)
16515 && DECL_TEMPLATE_INFO (decl
)
16516 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16518 decl
= DECL_TI_TEMPLATE (decl
);
16520 check_member_template (decl
);
16526 /* Parse a using-directive.
16529 using namespace :: [opt] nested-name-specifier [opt]
16530 namespace-name ; */
16533 cp_parser_using_directive (cp_parser
* parser
)
16535 tree namespace_decl
;
16538 /* Look for the `using' keyword. */
16539 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16540 /* And the `namespace' keyword. */
16541 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16542 /* Look for the optional `::' operator. */
16543 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16544 /* And the optional nested-name-specifier. */
16545 cp_parser_nested_name_specifier_opt (parser
,
16546 /*typename_keyword_p=*/false,
16547 /*check_dependency_p=*/true,
16549 /*is_declaration=*/true);
16550 /* Get the namespace being used. */
16551 namespace_decl
= cp_parser_namespace_name (parser
);
16552 /* And any specified attributes. */
16553 attribs
= cp_parser_attributes_opt (parser
);
16554 /* Update the symbol table. */
16555 parse_using_directive (namespace_decl
, attribs
);
16556 /* Look for the final `;'. */
16557 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16560 /* Parse an asm-definition.
16563 asm ( string-literal ) ;
16568 asm volatile [opt] ( string-literal ) ;
16569 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16570 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16571 : asm-operand-list [opt] ) ;
16572 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16573 : asm-operand-list [opt]
16574 : asm-clobber-list [opt] ) ;
16575 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16576 : asm-clobber-list [opt]
16577 : asm-goto-list ) ; */
16580 cp_parser_asm_definition (cp_parser
* parser
)
16583 tree outputs
= NULL_TREE
;
16584 tree inputs
= NULL_TREE
;
16585 tree clobbers
= NULL_TREE
;
16586 tree labels
= NULL_TREE
;
16588 bool volatile_p
= false;
16589 bool extended_p
= false;
16590 bool invalid_inputs_p
= false;
16591 bool invalid_outputs_p
= false;
16592 bool goto_p
= false;
16593 required_token missing
= RT_NONE
;
16595 /* Look for the `asm' keyword. */
16596 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16598 if (parser
->in_function_body
16599 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16600 error ("%<asm%> in %<constexpr%> function");
16602 /* See if the next token is `volatile'. */
16603 if (cp_parser_allow_gnu_extensions_p (parser
)
16604 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16606 /* Remember that we saw the `volatile' keyword. */
16608 /* Consume the token. */
16609 cp_lexer_consume_token (parser
->lexer
);
16611 if (cp_parser_allow_gnu_extensions_p (parser
)
16612 && parser
->in_function_body
16613 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16615 /* Remember that we saw the `goto' keyword. */
16617 /* Consume the token. */
16618 cp_lexer_consume_token (parser
->lexer
);
16620 /* Look for the opening `('. */
16621 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16623 /* Look for the string. */
16624 string
= cp_parser_string_literal (parser
, false, false);
16625 if (string
== error_mark_node
)
16627 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16628 /*consume_paren=*/true);
16632 /* If we're allowing GNU extensions, check for the extended assembly
16633 syntax. Unfortunately, the `:' tokens need not be separated by
16634 a space in C, and so, for compatibility, we tolerate that here
16635 too. Doing that means that we have to treat the `::' operator as
16637 if (cp_parser_allow_gnu_extensions_p (parser
)
16638 && parser
->in_function_body
16639 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16640 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16642 bool inputs_p
= false;
16643 bool clobbers_p
= false;
16644 bool labels_p
= false;
16646 /* The extended syntax was used. */
16649 /* Look for outputs. */
16650 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16652 /* Consume the `:'. */
16653 cp_lexer_consume_token (parser
->lexer
);
16654 /* Parse the output-operands. */
16655 if (cp_lexer_next_token_is_not (parser
->lexer
,
16657 && cp_lexer_next_token_is_not (parser
->lexer
,
16659 && cp_lexer_next_token_is_not (parser
->lexer
,
16662 outputs
= cp_parser_asm_operand_list (parser
);
16664 if (outputs
== error_mark_node
)
16665 invalid_outputs_p
= true;
16667 /* If the next token is `::', there are no outputs, and the
16668 next token is the beginning of the inputs. */
16669 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16670 /* The inputs are coming next. */
16673 /* Look for inputs. */
16675 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16677 /* Consume the `:' or `::'. */
16678 cp_lexer_consume_token (parser
->lexer
);
16679 /* Parse the output-operands. */
16680 if (cp_lexer_next_token_is_not (parser
->lexer
,
16682 && cp_lexer_next_token_is_not (parser
->lexer
,
16684 && cp_lexer_next_token_is_not (parser
->lexer
,
16686 inputs
= cp_parser_asm_operand_list (parser
);
16688 if (inputs
== error_mark_node
)
16689 invalid_inputs_p
= true;
16691 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16692 /* The clobbers are coming next. */
16695 /* Look for clobbers. */
16697 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16700 /* Consume the `:' or `::'. */
16701 cp_lexer_consume_token (parser
->lexer
);
16702 /* Parse the clobbers. */
16703 if (cp_lexer_next_token_is_not (parser
->lexer
,
16705 && cp_lexer_next_token_is_not (parser
->lexer
,
16707 clobbers
= cp_parser_asm_clobber_list (parser
);
16710 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16711 /* The labels are coming next. */
16714 /* Look for labels. */
16716 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16719 /* Consume the `:' or `::'. */
16720 cp_lexer_consume_token (parser
->lexer
);
16721 /* Parse the labels. */
16722 labels
= cp_parser_asm_label_list (parser
);
16725 if (goto_p
&& !labels_p
)
16726 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16729 missing
= RT_COLON_SCOPE
;
16731 /* Look for the closing `)'. */
16732 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16733 missing
? missing
: RT_CLOSE_PAREN
))
16734 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16735 /*consume_paren=*/true);
16736 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16738 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16740 /* Create the ASM_EXPR. */
16741 if (parser
->in_function_body
)
16743 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16744 inputs
, clobbers
, labels
);
16745 /* If the extended syntax was not used, mark the ASM_EXPR. */
16748 tree temp
= asm_stmt
;
16749 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16750 temp
= TREE_OPERAND (temp
, 0);
16752 ASM_INPUT_P (temp
) = 1;
16756 symtab
->finalize_toplevel_asm (string
);
16760 /* Declarators [gram.dcl.decl] */
16762 /* Parse an init-declarator.
16765 declarator initializer [opt]
16770 declarator asm-specification [opt] attributes [opt] initializer [opt]
16772 function-definition:
16773 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16775 decl-specifier-seq [opt] declarator function-try-block
16779 function-definition:
16780 __extension__ function-definition
16784 function-definition:
16785 decl-specifier-seq [opt] declarator function-transaction-block
16787 The DECL_SPECIFIERS apply to this declarator. Returns a
16788 representation of the entity declared. If MEMBER_P is TRUE, then
16789 this declarator appears in a class scope. The new DECL created by
16790 this declarator is returned.
16792 The CHECKS are access checks that should be performed once we know
16793 what entity is being declared (and, therefore, what classes have
16796 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16797 for a function-definition here as well. If the declarator is a
16798 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16799 be TRUE upon return. By that point, the function-definition will
16800 have been completely parsed.
16802 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16805 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16806 parsed declaration if it is an uninitialized single declarator not followed
16807 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16808 if present, will not be consumed. If returned, this declarator will be
16809 created with SD_INITIALIZED but will not call cp_finish_decl. */
16812 cp_parser_init_declarator (cp_parser
* parser
,
16813 cp_decl_specifier_seq
*decl_specifiers
,
16814 vec
<deferred_access_check
, va_gc
> *checks
,
16815 bool function_definition_allowed_p
,
16817 int declares_class_or_enum
,
16818 bool* function_definition_p
,
16819 tree
* maybe_range_for_decl
)
16821 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16822 *attributes_start_token
= NULL
;
16823 cp_declarator
*declarator
;
16824 tree prefix_attributes
;
16825 tree attributes
= NULL
;
16826 tree asm_specification
;
16828 tree decl
= NULL_TREE
;
16830 int is_initialized
;
16831 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16832 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16834 enum cpp_ttype initialization_kind
;
16835 bool is_direct_init
= false;
16836 bool is_non_constant_init
;
16837 int ctor_dtor_or_conv_p
;
16838 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16839 tree pushed_scope
= NULL_TREE
;
16840 bool range_for_decl_p
= false;
16841 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16843 /* Gather the attributes that were provided with the
16844 decl-specifiers. */
16845 prefix_attributes
= decl_specifiers
->attributes
;
16847 /* Assume that this is not the declarator for a function
16849 if (function_definition_p
)
16850 *function_definition_p
= false;
16852 /* Default arguments are only permitted for function parameters. */
16853 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16854 parser
->default_arg_ok_p
= false;
16856 /* Defer access checks while parsing the declarator; we cannot know
16857 what names are accessible until we know what is being
16859 resume_deferring_access_checks ();
16861 /* Parse the declarator. */
16862 token
= cp_lexer_peek_token (parser
->lexer
);
16864 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16865 &ctor_dtor_or_conv_p
,
16866 /*parenthesized_p=*/NULL
,
16867 member_p
, friend_p
);
16868 /* Gather up the deferred checks. */
16869 stop_deferring_access_checks ();
16871 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16873 /* If the DECLARATOR was erroneous, there's no need to go
16875 if (declarator
== cp_error_declarator
)
16876 return error_mark_node
;
16878 /* Check that the number of template-parameter-lists is OK. */
16879 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16881 return error_mark_node
;
16883 if (declares_class_or_enum
& 2)
16884 cp_parser_check_for_definition_in_return_type (declarator
,
16885 decl_specifiers
->type
,
16886 decl_specifiers
->locations
[ds_type_spec
]);
16888 /* Figure out what scope the entity declared by the DECLARATOR is
16889 located in. `grokdeclarator' sometimes changes the scope, so
16890 we compute it now. */
16891 scope
= get_scope_of_declarator (declarator
);
16893 /* Perform any lookups in the declared type which were thought to be
16894 dependent, but are not in the scope of the declarator. */
16895 decl_specifiers
->type
16896 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
16898 /* If we're allowing GNU extensions, look for an
16899 asm-specification. */
16900 if (cp_parser_allow_gnu_extensions_p (parser
))
16902 /* Look for an asm-specification. */
16903 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
16904 asm_specification
= cp_parser_asm_specification_opt (parser
);
16907 asm_specification
= NULL_TREE
;
16909 /* Look for attributes. */
16910 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
16911 attributes
= cp_parser_attributes_opt (parser
);
16913 /* Peek at the next token. */
16914 token
= cp_lexer_peek_token (parser
->lexer
);
16916 bool bogus_implicit_tmpl
= false;
16918 if (function_declarator_p (declarator
))
16920 /* Check to see if the token indicates the start of a
16921 function-definition. */
16922 if (cp_parser_token_starts_function_definition_p (token
))
16924 if (!function_definition_allowed_p
)
16926 /* If a function-definition should not appear here, issue an
16928 cp_parser_error (parser
,
16929 "a function-definition is not allowed here");
16930 return error_mark_node
;
16933 location_t func_brace_location
16934 = cp_lexer_peek_token (parser
->lexer
)->location
;
16936 /* Neither attributes nor an asm-specification are allowed
16937 on a function-definition. */
16938 if (asm_specification
)
16939 error_at (asm_spec_start_token
->location
,
16940 "an asm-specification is not allowed "
16941 "on a function-definition");
16943 error_at (attributes_start_token
->location
,
16944 "attributes are not allowed "
16945 "on a function-definition");
16946 /* This is a function-definition. */
16947 *function_definition_p
= true;
16949 /* Parse the function definition. */
16951 decl
= cp_parser_save_member_function_body (parser
,
16954 prefix_attributes
);
16957 (cp_parser_function_definition_from_specifiers_and_declarator
16958 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
16960 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
16962 /* This is where the prologue starts... */
16963 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
16964 = func_brace_location
;
16970 else if (parser
->fully_implicit_function_template_p
)
16972 /* A non-template declaration involving a function parameter list
16973 containing an implicit template parameter will be made into a
16974 template. If the resulting declaration is not going to be an
16975 actual function then finish the template scope here to prevent it.
16976 An error message will be issued once we have a decl to talk about.
16978 FIXME probably we should do type deduction rather than create an
16979 implicit template, but the standard currently doesn't allow it. */
16980 bogus_implicit_tmpl
= true;
16981 finish_fully_implicit_template (parser
, NULL_TREE
);
16986 Only in function declarations for constructors, destructors, and
16987 type conversions can the decl-specifier-seq be omitted.
16989 We explicitly postpone this check past the point where we handle
16990 function-definitions because we tolerate function-definitions
16991 that are missing their return types in some modes. */
16992 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
16994 cp_parser_error (parser
,
16995 "expected constructor, destructor, or type conversion");
16996 return error_mark_node
;
16999 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17000 if (token
->type
== CPP_EQ
17001 || token
->type
== CPP_OPEN_PAREN
17002 || token
->type
== CPP_OPEN_BRACE
)
17004 is_initialized
= SD_INITIALIZED
;
17005 initialization_kind
= token
->type
;
17006 if (maybe_range_for_decl
)
17007 *maybe_range_for_decl
= error_mark_node
;
17009 if (token
->type
== CPP_EQ
17010 && function_declarator_p (declarator
))
17012 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17013 if (t2
->keyword
== RID_DEFAULT
)
17014 is_initialized
= SD_DEFAULTED
;
17015 else if (t2
->keyword
== RID_DELETE
)
17016 is_initialized
= SD_DELETED
;
17021 /* If the init-declarator isn't initialized and isn't followed by a
17022 `,' or `;', it's not a valid init-declarator. */
17023 if (token
->type
!= CPP_COMMA
17024 && token
->type
!= CPP_SEMICOLON
)
17026 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17027 range_for_decl_p
= true;
17030 cp_parser_error (parser
, "expected initializer");
17031 return error_mark_node
;
17034 is_initialized
= SD_UNINITIALIZED
;
17035 initialization_kind
= CPP_EOF
;
17038 /* Because start_decl has side-effects, we should only call it if we
17039 know we're going ahead. By this point, we know that we cannot
17040 possibly be looking at any other construct. */
17041 cp_parser_commit_to_tentative_parse (parser
);
17043 /* Enter the newly declared entry in the symbol table. If we're
17044 processing a declaration in a class-specifier, we wait until
17045 after processing the initializer. */
17048 if (parser
->in_unbraced_linkage_specification_p
)
17049 decl_specifiers
->storage_class
= sc_extern
;
17050 decl
= start_decl (declarator
, decl_specifiers
,
17051 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17052 attributes
, prefix_attributes
, &pushed_scope
);
17053 cp_finalize_omp_declare_simd (parser
, decl
);
17054 /* Adjust location of decl if declarator->id_loc is more appropriate:
17055 set, and decl wasn't merged with another decl, in which case its
17056 location would be different from input_location, and more accurate. */
17058 && declarator
->id_loc
!= UNKNOWN_LOCATION
17059 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17060 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17063 /* Enter the SCOPE. That way unqualified names appearing in the
17064 initializer will be looked up in SCOPE. */
17065 pushed_scope
= push_scope (scope
);
17067 /* Perform deferred access control checks, now that we know in which
17068 SCOPE the declared entity resides. */
17069 if (!member_p
&& decl
)
17071 tree saved_current_function_decl
= NULL_TREE
;
17073 /* If the entity being declared is a function, pretend that we
17074 are in its scope. If it is a `friend', it may have access to
17075 things that would not otherwise be accessible. */
17076 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17078 saved_current_function_decl
= current_function_decl
;
17079 current_function_decl
= decl
;
17082 /* Perform access checks for template parameters. */
17083 cp_parser_perform_template_parameter_access_checks (checks
);
17085 /* Perform the access control checks for the declarator and the
17086 decl-specifiers. */
17087 perform_deferred_access_checks (tf_warning_or_error
);
17089 /* Restore the saved value. */
17090 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17091 current_function_decl
= saved_current_function_decl
;
17094 /* Parse the initializer. */
17095 initializer
= NULL_TREE
;
17096 is_direct_init
= false;
17097 is_non_constant_init
= true;
17098 if (is_initialized
)
17100 if (function_declarator_p (declarator
))
17102 cp_token
*initializer_start_token
= cp_lexer_peek_token (parser
->lexer
);
17103 if (initialization_kind
== CPP_EQ
)
17104 initializer
= cp_parser_pure_specifier (parser
);
17107 /* If the declaration was erroneous, we don't really
17108 know what the user intended, so just silently
17109 consume the initializer. */
17110 if (decl
!= error_mark_node
)
17111 error_at (initializer_start_token
->location
,
17112 "initializer provided for function");
17113 cp_parser_skip_to_closing_parenthesis (parser
,
17114 /*recovering=*/true,
17115 /*or_comma=*/false,
17116 /*consume_paren=*/true);
17121 /* We want to record the extra mangling scope for in-class
17122 initializers of class members and initializers of static data
17123 member templates. The former involves deferring
17124 parsing of the initializer until end of class as with default
17125 arguments. So right here we only handle the latter. */
17126 if (!member_p
&& processing_template_decl
)
17127 start_lambda_scope (decl
);
17128 initializer
= cp_parser_initializer (parser
,
17130 &is_non_constant_init
);
17131 if (!member_p
&& processing_template_decl
)
17132 finish_lambda_scope ();
17133 if (initializer
== error_mark_node
)
17134 cp_parser_skip_to_end_of_statement (parser
);
17138 /* The old parser allows attributes to appear after a parenthesized
17139 initializer. Mark Mitchell proposed removing this functionality
17140 on the GCC mailing lists on 2002-08-13. This parser accepts the
17141 attributes -- but ignores them. */
17142 if (cp_parser_allow_gnu_extensions_p (parser
)
17143 && initialization_kind
== CPP_OPEN_PAREN
)
17144 if (cp_parser_attributes_opt (parser
))
17145 warning (OPT_Wattributes
,
17146 "attributes after parenthesized initializer ignored");
17148 /* And now complain about a non-function implicit template. */
17149 if (bogus_implicit_tmpl
)
17150 error_at (DECL_SOURCE_LOCATION (decl
),
17151 "non-function %qD declared as implicit template", decl
);
17153 /* For an in-class declaration, use `grokfield' to create the
17159 pop_scope (pushed_scope
);
17160 pushed_scope
= NULL_TREE
;
17162 decl
= grokfield (declarator
, decl_specifiers
,
17163 initializer
, !is_non_constant_init
,
17164 /*asmspec=*/NULL_TREE
,
17165 chainon (attributes
, prefix_attributes
));
17166 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17167 cp_parser_save_default_args (parser
, decl
);
17168 cp_finalize_omp_declare_simd (parser
, decl
);
17171 /* Finish processing the declaration. But, skip member
17173 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17175 cp_finish_decl (decl
,
17176 initializer
, !is_non_constant_init
,
17178 /* If the initializer is in parentheses, then this is
17179 a direct-initialization, which means that an
17180 `explicit' constructor is OK. Otherwise, an
17181 `explicit' constructor cannot be used. */
17182 ((is_direct_init
|| !is_initialized
)
17183 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17185 else if ((cxx_dialect
!= cxx98
) && friend_p
17186 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17187 /* Core issue #226 (C++0x only): A default template-argument
17188 shall not be specified in a friend class template
17190 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17191 /*is_partial=*/false, /*is_friend_decl=*/1);
17193 if (!friend_p
&& pushed_scope
)
17194 pop_scope (pushed_scope
);
17196 if (function_declarator_p (declarator
)
17197 && parser
->fully_implicit_function_template_p
)
17200 decl
= finish_fully_implicit_template (parser
, decl
);
17202 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17208 /* Parse a declarator.
17212 ptr-operator declarator
17214 abstract-declarator:
17215 ptr-operator abstract-declarator [opt]
17216 direct-abstract-declarator
17221 attributes [opt] direct-declarator
17222 attributes [opt] ptr-operator declarator
17224 abstract-declarator:
17225 attributes [opt] ptr-operator abstract-declarator [opt]
17226 attributes [opt] direct-abstract-declarator
17228 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17229 detect constructor, destructor or conversion operators. It is set
17230 to -1 if the declarator is a name, and +1 if it is a
17231 function. Otherwise it is set to zero. Usually you just want to
17232 test for >0, but internally the negative value is used.
17234 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17235 a decl-specifier-seq unless it declares a constructor, destructor,
17236 or conversion. It might seem that we could check this condition in
17237 semantic analysis, rather than parsing, but that makes it difficult
17238 to handle something like `f()'. We want to notice that there are
17239 no decl-specifiers, and therefore realize that this is an
17240 expression, not a declaration.)
17242 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17243 the declarator is a direct-declarator of the form "(...)".
17245 MEMBER_P is true iff this declarator is a member-declarator.
17247 FRIEND_P is true iff this declarator is a friend. */
17249 static cp_declarator
*
17250 cp_parser_declarator (cp_parser
* parser
,
17251 cp_parser_declarator_kind dcl_kind
,
17252 int* ctor_dtor_or_conv_p
,
17253 bool* parenthesized_p
,
17254 bool member_p
, bool friend_p
)
17256 cp_declarator
*declarator
;
17257 enum tree_code code
;
17258 cp_cv_quals cv_quals
;
17260 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17262 /* Assume this is not a constructor, destructor, or type-conversion
17264 if (ctor_dtor_or_conv_p
)
17265 *ctor_dtor_or_conv_p
= 0;
17267 if (cp_parser_allow_gnu_extensions_p (parser
))
17268 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17270 /* Check for the ptr-operator production. */
17271 cp_parser_parse_tentatively (parser
);
17272 /* Parse the ptr-operator. */
17273 code
= cp_parser_ptr_operator (parser
,
17278 /* If that worked, then we have a ptr-operator. */
17279 if (cp_parser_parse_definitely (parser
))
17281 /* If a ptr-operator was found, then this declarator was not
17283 if (parenthesized_p
)
17284 *parenthesized_p
= true;
17285 /* The dependent declarator is optional if we are parsing an
17286 abstract-declarator. */
17287 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17288 cp_parser_parse_tentatively (parser
);
17290 /* Parse the dependent declarator. */
17291 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17292 /*ctor_dtor_or_conv_p=*/NULL
,
17293 /*parenthesized_p=*/NULL
,
17294 /*member_p=*/false,
17297 /* If we are parsing an abstract-declarator, we must handle the
17298 case where the dependent declarator is absent. */
17299 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17300 && !cp_parser_parse_definitely (parser
))
17303 declarator
= cp_parser_make_indirect_declarator
17304 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17306 /* Everything else is a direct-declarator. */
17309 if (parenthesized_p
)
17310 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17312 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17313 ctor_dtor_or_conv_p
,
17314 member_p
, friend_p
);
17317 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17318 declarator
->attributes
= gnu_attributes
;
17322 /* Parse a direct-declarator or direct-abstract-declarator.
17326 direct-declarator ( parameter-declaration-clause )
17327 cv-qualifier-seq [opt]
17328 ref-qualifier [opt]
17329 exception-specification [opt]
17330 direct-declarator [ constant-expression [opt] ]
17333 direct-abstract-declarator:
17334 direct-abstract-declarator [opt]
17335 ( parameter-declaration-clause )
17336 cv-qualifier-seq [opt]
17337 ref-qualifier [opt]
17338 exception-specification [opt]
17339 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17340 ( abstract-declarator )
17342 Returns a representation of the declarator. DCL_KIND is
17343 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17344 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17345 we are parsing a direct-declarator. It is
17346 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17347 of ambiguity we prefer an abstract declarator, as per
17348 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17349 as for cp_parser_declarator. */
17351 static cp_declarator
*
17352 cp_parser_direct_declarator (cp_parser
* parser
,
17353 cp_parser_declarator_kind dcl_kind
,
17354 int* ctor_dtor_or_conv_p
,
17355 bool member_p
, bool friend_p
)
17358 cp_declarator
*declarator
= NULL
;
17359 tree scope
= NULL_TREE
;
17360 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17361 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17363 tree pushed_scope
= NULL_TREE
;
17367 /* Peek at the next token. */
17368 token
= cp_lexer_peek_token (parser
->lexer
);
17369 if (token
->type
== CPP_OPEN_PAREN
)
17371 /* This is either a parameter-declaration-clause, or a
17372 parenthesized declarator. When we know we are parsing a
17373 named declarator, it must be a parenthesized declarator
17374 if FIRST is true. For instance, `(int)' is a
17375 parameter-declaration-clause, with an omitted
17376 direct-abstract-declarator. But `((*))', is a
17377 parenthesized abstract declarator. Finally, when T is a
17378 template parameter `(T)' is a
17379 parameter-declaration-clause, and not a parenthesized
17382 We first try and parse a parameter-declaration-clause,
17383 and then try a nested declarator (if FIRST is true).
17385 It is not an error for it not to be a
17386 parameter-declaration-clause, even when FIRST is
17392 The first is the declaration of a function while the
17393 second is the definition of a variable, including its
17396 Having seen only the parenthesis, we cannot know which of
17397 these two alternatives should be selected. Even more
17398 complex are examples like:
17403 The former is a function-declaration; the latter is a
17404 variable initialization.
17406 Thus again, we try a parameter-declaration-clause, and if
17407 that fails, we back out and return. */
17409 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17412 bool is_declarator
= false;
17414 /* In a member-declarator, the only valid interpretation
17415 of a parenthesis is the start of a
17416 parameter-declaration-clause. (It is invalid to
17417 initialize a static data member with a parenthesized
17418 initializer; only the "=" form of initialization is
17421 cp_parser_parse_tentatively (parser
);
17423 /* Consume the `('. */
17424 cp_lexer_consume_token (parser
->lexer
);
17427 /* If this is going to be an abstract declarator, we're
17428 in a declarator and we can't have default args. */
17429 parser
->default_arg_ok_p
= false;
17430 parser
->in_declarator_p
= true;
17433 begin_scope (sk_function_parms
, NULL_TREE
);
17435 /* Parse the parameter-declaration-clause. */
17436 params
= cp_parser_parameter_declaration_clause (parser
);
17438 /* Consume the `)'. */
17439 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17441 /* If all went well, parse the cv-qualifier-seq,
17442 ref-qualifier and the exception-specification. */
17443 if (member_p
|| cp_parser_parse_definitely (parser
))
17445 cp_cv_quals cv_quals
;
17446 cp_virt_specifiers virt_specifiers
;
17447 cp_ref_qualifier ref_qual
;
17448 tree exception_specification
;
17451 bool memfn
= (member_p
|| (pushed_scope
17452 && CLASS_TYPE_P (pushed_scope
)));
17454 is_declarator
= true;
17456 if (ctor_dtor_or_conv_p
)
17457 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17460 /* Parse the cv-qualifier-seq. */
17461 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17462 /* Parse the ref-qualifier. */
17463 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17464 /* And the exception-specification. */
17465 exception_specification
17466 = cp_parser_exception_specification_opt (parser
);
17468 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17470 /* In here, we handle cases where attribute is used after
17471 the function declaration. For example:
17472 void func (int x) __attribute__((vector(..))); */
17474 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17476 cp_parser_parse_tentatively (parser
);
17477 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17478 if (cp_lexer_next_token_is_not (parser
->lexer
,
17480 && cp_lexer_next_token_is_not (parser
->lexer
,
17482 cp_parser_abort_tentative_parse (parser
);
17483 else if (!cp_parser_parse_definitely (parser
))
17486 attrs
= chainon (attr
, attrs
);
17488 late_return
= (cp_parser_late_return_type_opt
17489 (parser
, declarator
,
17490 memfn
? cv_quals
: -1));
17493 /* Parse the virt-specifier-seq. */
17494 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17496 /* Create the function-declarator. */
17497 declarator
= make_call_declarator (declarator
,
17502 exception_specification
,
17504 declarator
->std_attributes
= attrs
;
17505 /* Any subsequent parameter lists are to do with
17506 return type, so are not those of the declared
17508 parser
->default_arg_ok_p
= false;
17511 /* Remove the function parms from scope. */
17512 pop_bindings_and_leave_scope ();
17515 /* Repeat the main loop. */
17519 /* If this is the first, we can try a parenthesized
17523 bool saved_in_type_id_in_expr_p
;
17525 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17526 parser
->in_declarator_p
= saved_in_declarator_p
;
17528 /* Consume the `('. */
17529 cp_lexer_consume_token (parser
->lexer
);
17530 /* Parse the nested declarator. */
17531 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17532 parser
->in_type_id_in_expr_p
= true;
17534 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17535 /*parenthesized_p=*/NULL
,
17536 member_p
, friend_p
);
17537 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17539 /* Expect a `)'. */
17540 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17541 declarator
= cp_error_declarator
;
17542 if (declarator
== cp_error_declarator
)
17545 goto handle_declarator
;
17547 /* Otherwise, we must be done. */
17551 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17552 && token
->type
== CPP_OPEN_SQUARE
17553 && !cp_next_tokens_can_be_attribute_p (parser
))
17555 /* Parse an array-declarator. */
17556 tree bounds
, attrs
;
17558 if (ctor_dtor_or_conv_p
)
17559 *ctor_dtor_or_conv_p
= 0;
17562 parser
->default_arg_ok_p
= false;
17563 parser
->in_declarator_p
= true;
17564 /* Consume the `['. */
17565 cp_lexer_consume_token (parser
->lexer
);
17566 /* Peek at the next token. */
17567 token
= cp_lexer_peek_token (parser
->lexer
);
17568 /* If the next token is `]', then there is no
17569 constant-expression. */
17570 if (token
->type
!= CPP_CLOSE_SQUARE
)
17572 bool non_constant_p
;
17574 = cp_parser_constant_expression (parser
,
17575 /*allow_non_constant=*/true,
17577 if (!non_constant_p
)
17579 else if (error_operand_p (bounds
))
17580 /* Already gave an error. */;
17581 else if (!parser
->in_function_body
17582 || current_binding_level
->kind
== sk_function_parms
)
17584 /* Normally, the array bound must be an integral constant
17585 expression. However, as an extension, we allow VLAs
17586 in function scopes as long as they aren't part of a
17587 parameter declaration. */
17588 cp_parser_error (parser
,
17589 "array bound is not an integer constant");
17590 bounds
= error_mark_node
;
17592 else if (processing_template_decl
17593 && !type_dependent_expression_p (bounds
))
17595 /* Remember this wasn't a constant-expression. */
17596 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17597 TREE_SIDE_EFFECTS (bounds
) = 1;
17601 bounds
= NULL_TREE
;
17602 /* Look for the closing `]'. */
17603 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17605 declarator
= cp_error_declarator
;
17609 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17610 declarator
= make_array_declarator (declarator
, bounds
);
17611 declarator
->std_attributes
= attrs
;
17613 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17616 tree qualifying_scope
;
17617 tree unqualified_name
;
17619 special_function_kind sfk
;
17621 bool pack_expansion_p
= false;
17622 cp_token
*declarator_id_start_token
;
17624 /* Parse a declarator-id */
17625 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17628 cp_parser_parse_tentatively (parser
);
17630 /* If we see an ellipsis, we should be looking at a
17632 if (token
->type
== CPP_ELLIPSIS
)
17634 /* Consume the `...' */
17635 cp_lexer_consume_token (parser
->lexer
);
17637 pack_expansion_p
= true;
17641 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17643 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17644 qualifying_scope
= parser
->scope
;
17649 if (!unqualified_name
&& pack_expansion_p
)
17651 /* Check whether an error occurred. */
17652 okay
= !cp_parser_error_occurred (parser
);
17654 /* We already consumed the ellipsis to mark a
17655 parameter pack, but we have no way to report it,
17656 so abort the tentative parse. We will be exiting
17657 immediately anyway. */
17658 cp_parser_abort_tentative_parse (parser
);
17661 okay
= cp_parser_parse_definitely (parser
);
17664 unqualified_name
= error_mark_node
;
17665 else if (unqualified_name
17666 && (qualifying_scope
17667 || (!identifier_p (unqualified_name
))))
17669 cp_parser_error (parser
, "expected unqualified-id");
17670 unqualified_name
= error_mark_node
;
17674 if (!unqualified_name
)
17676 if (unqualified_name
== error_mark_node
)
17678 declarator
= cp_error_declarator
;
17679 pack_expansion_p
= false;
17680 declarator
->parameter_pack_p
= false;
17684 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17686 if (qualifying_scope
&& at_namespace_scope_p ()
17687 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17689 /* In the declaration of a member of a template class
17690 outside of the class itself, the SCOPE will sometimes
17691 be a TYPENAME_TYPE. For example, given:
17693 template <typename T>
17694 int S<T>::R::i = 3;
17696 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17697 this context, we must resolve S<T>::R to an ordinary
17698 type, rather than a typename type.
17700 The reason we normally avoid resolving TYPENAME_TYPEs
17701 is that a specialization of `S' might render
17702 `S<T>::R' not a type. However, if `S' is
17703 specialized, then this `i' will not be used, so there
17704 is no harm in resolving the types here. */
17707 /* Resolve the TYPENAME_TYPE. */
17708 type
= resolve_typename_type (qualifying_scope
,
17709 /*only_current_p=*/false);
17710 /* If that failed, the declarator is invalid. */
17711 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17713 if (typedef_variant_p (type
))
17714 error_at (declarator_id_start_token
->location
,
17715 "cannot define member of dependent typedef "
17718 error_at (declarator_id_start_token
->location
,
17719 "%<%T::%E%> is not a type",
17720 TYPE_CONTEXT (qualifying_scope
),
17721 TYPE_IDENTIFIER (qualifying_scope
));
17723 qualifying_scope
= type
;
17728 if (unqualified_name
)
17732 if (qualifying_scope
17733 && CLASS_TYPE_P (qualifying_scope
))
17734 class_type
= qualifying_scope
;
17736 class_type
= current_class_type
;
17738 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17740 tree name_type
= TREE_TYPE (unqualified_name
);
17741 if (class_type
&& same_type_p (name_type
, class_type
))
17743 if (qualifying_scope
17744 && CLASSTYPE_USE_TEMPLATE (name_type
))
17746 error_at (declarator_id_start_token
->location
,
17747 "invalid use of constructor as a template");
17748 inform (declarator_id_start_token
->location
,
17749 "use %<%T::%D%> instead of %<%T::%D%> to "
17750 "name the constructor in a qualified name",
17752 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17753 class_type
, name_type
);
17754 declarator
= cp_error_declarator
;
17758 unqualified_name
= constructor_name (class_type
);
17762 /* We do not attempt to print the declarator
17763 here because we do not have enough
17764 information about its original syntactic
17766 cp_parser_error (parser
, "invalid declarator");
17767 declarator
= cp_error_declarator
;
17774 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17775 sfk
= sfk_destructor
;
17776 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17777 sfk
= sfk_conversion
;
17778 else if (/* There's no way to declare a constructor
17779 for an anonymous type, even if the type
17780 got a name for linkage purposes. */
17781 !TYPE_WAS_ANONYMOUS (class_type
)
17782 /* Handle correctly (c++/19200):
17796 friend void N::S();
17799 && class_type
!= qualifying_scope
)
17800 && constructor_name_p (unqualified_name
,
17803 unqualified_name
= constructor_name (class_type
);
17804 sfk
= sfk_constructor
;
17806 else if (is_overloaded_fn (unqualified_name
)
17807 && DECL_CONSTRUCTOR_P (get_first_fn
17808 (unqualified_name
)))
17809 sfk
= sfk_constructor
;
17811 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17812 *ctor_dtor_or_conv_p
= -1;
17815 declarator
= make_id_declarator (qualifying_scope
,
17818 declarator
->std_attributes
= attrs
;
17819 declarator
->id_loc
= token
->location
;
17820 declarator
->parameter_pack_p
= pack_expansion_p
;
17822 if (pack_expansion_p
)
17823 maybe_warn_variadic_templates ();
17826 handle_declarator
:;
17827 scope
= get_scope_of_declarator (declarator
);
17830 /* Any names that appear after the declarator-id for a
17831 member are looked up in the containing scope. */
17832 if (at_function_scope_p ())
17834 /* But declarations with qualified-ids can't appear in a
17836 cp_parser_error (parser
, "qualified-id in declaration");
17837 declarator
= cp_error_declarator
;
17840 pushed_scope
= push_scope (scope
);
17842 parser
->in_declarator_p
= true;
17843 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17844 || (declarator
&& declarator
->kind
== cdk_id
))
17845 /* Default args are only allowed on function
17847 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17849 parser
->default_arg_ok_p
= false;
17858 /* For an abstract declarator, we might wind up with nothing at this
17859 point. That's an error; the declarator is not optional. */
17861 cp_parser_error (parser
, "expected declarator");
17863 /* If we entered a scope, we must exit it now. */
17865 pop_scope (pushed_scope
);
17867 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17868 parser
->in_declarator_p
= saved_in_declarator_p
;
17873 /* Parse a ptr-operator.
17876 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17877 * cv-qualifier-seq [opt]
17879 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17880 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17885 & cv-qualifier-seq [opt]
17887 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17888 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17889 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17890 filled in with the TYPE containing the member. *CV_QUALS is
17891 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17892 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17893 Note that the tree codes returned by this function have nothing
17894 to do with the types of trees that will be eventually be created
17895 to represent the pointer or reference type being parsed. They are
17896 just constants with suggestive names. */
17897 static enum tree_code
17898 cp_parser_ptr_operator (cp_parser
* parser
,
17900 cp_cv_quals
*cv_quals
,
17903 enum tree_code code
= ERROR_MARK
;
17905 tree attrs
= NULL_TREE
;
17907 /* Assume that it's not a pointer-to-member. */
17909 /* And that there are no cv-qualifiers. */
17910 *cv_quals
= TYPE_UNQUALIFIED
;
17912 /* Peek at the next token. */
17913 token
= cp_lexer_peek_token (parser
->lexer
);
17915 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17916 if (token
->type
== CPP_MULT
)
17917 code
= INDIRECT_REF
;
17918 else if (token
->type
== CPP_AND
)
17920 else if ((cxx_dialect
!= cxx98
) &&
17921 token
->type
== CPP_AND_AND
) /* C++0x only */
17922 code
= NON_LVALUE_EXPR
;
17924 if (code
!= ERROR_MARK
)
17926 /* Consume the `*', `&' or `&&'. */
17927 cp_lexer_consume_token (parser
->lexer
);
17929 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17930 `&', if we are allowing GNU extensions. (The only qualifier
17931 that can legally appear after `&' is `restrict', but that is
17932 enforced during semantic analysis. */
17933 if (code
== INDIRECT_REF
17934 || cp_parser_allow_gnu_extensions_p (parser
))
17935 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17937 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17938 if (attributes
!= NULL
)
17939 *attributes
= attrs
;
17943 /* Try the pointer-to-member case. */
17944 cp_parser_parse_tentatively (parser
);
17945 /* Look for the optional `::' operator. */
17946 cp_parser_global_scope_opt (parser
,
17947 /*current_scope_valid_p=*/false);
17948 /* Look for the nested-name specifier. */
17949 token
= cp_lexer_peek_token (parser
->lexer
);
17950 cp_parser_nested_name_specifier (parser
,
17951 /*typename_keyword_p=*/false,
17952 /*check_dependency_p=*/true,
17954 /*is_declaration=*/false);
17955 /* If we found it, and the next token is a `*', then we are
17956 indeed looking at a pointer-to-member operator. */
17957 if (!cp_parser_error_occurred (parser
)
17958 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
17960 /* Indicate that the `*' operator was used. */
17961 code
= INDIRECT_REF
;
17963 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
17964 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
17965 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
17966 error_at (token
->location
, "cannot form pointer to member of "
17967 "non-class %q#T", parser
->scope
);
17970 /* The type of which the member is a member is given by the
17972 *type
= parser
->scope
;
17973 /* The next name will not be qualified. */
17974 parser
->scope
= NULL_TREE
;
17975 parser
->qualifying_scope
= NULL_TREE
;
17976 parser
->object_scope
= NULL_TREE
;
17977 /* Look for optional c++11 attributes. */
17978 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17979 if (attributes
!= NULL
)
17980 *attributes
= attrs
;
17981 /* Look for the optional cv-qualifier-seq. */
17982 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17985 /* If that didn't work we don't have a ptr-operator. */
17986 if (!cp_parser_parse_definitely (parser
))
17987 cp_parser_error (parser
, "expected ptr-operator");
17993 /* Parse an (optional) cv-qualifier-seq.
17996 cv-qualifier cv-qualifier-seq [opt]
18007 Returns a bitmask representing the cv-qualifiers. */
18010 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18012 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18017 cp_cv_quals cv_qualifier
;
18019 /* Peek at the next token. */
18020 token
= cp_lexer_peek_token (parser
->lexer
);
18021 /* See if it's a cv-qualifier. */
18022 switch (token
->keyword
)
18025 cv_qualifier
= TYPE_QUAL_CONST
;
18029 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18033 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18037 cv_qualifier
= TYPE_UNQUALIFIED
;
18044 if (cv_quals
& cv_qualifier
)
18046 error_at (token
->location
, "duplicate cv-qualifier");
18047 cp_lexer_purge_token (parser
->lexer
);
18051 cp_lexer_consume_token (parser
->lexer
);
18052 cv_quals
|= cv_qualifier
;
18059 /* Parse an (optional) ref-qualifier
18065 Returns cp_ref_qualifier representing ref-qualifier. */
18067 static cp_ref_qualifier
18068 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18070 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18072 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18073 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18078 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18079 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18081 switch (token
->type
)
18084 curr_ref_qual
= REF_QUAL_LVALUE
;
18088 curr_ref_qual
= REF_QUAL_RVALUE
;
18092 curr_ref_qual
= REF_QUAL_NONE
;
18096 if (!curr_ref_qual
)
18100 error_at (token
->location
, "multiple ref-qualifiers");
18101 cp_lexer_purge_token (parser
->lexer
);
18105 ref_qual
= curr_ref_qual
;
18106 cp_lexer_consume_token (parser
->lexer
);
18113 /* Parse an (optional) virt-specifier-seq.
18115 virt-specifier-seq:
18116 virt-specifier virt-specifier-seq [opt]
18122 Returns a bitmask representing the virt-specifiers. */
18124 static cp_virt_specifiers
18125 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18127 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18132 cp_virt_specifiers virt_specifier
;
18134 /* Peek at the next token. */
18135 token
= cp_lexer_peek_token (parser
->lexer
);
18136 /* See if it's a virt-specifier-qualifier. */
18137 if (token
->type
!= CPP_NAME
)
18139 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18141 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18142 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18144 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18146 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18147 virt_specifier
= VIRT_SPEC_FINAL
;
18149 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18151 virt_specifier
= VIRT_SPEC_FINAL
;
18156 if (virt_specifiers
& virt_specifier
)
18158 error_at (token
->location
, "duplicate virt-specifier");
18159 cp_lexer_purge_token (parser
->lexer
);
18163 cp_lexer_consume_token (parser
->lexer
);
18164 virt_specifiers
|= virt_specifier
;
18167 return virt_specifiers
;
18170 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18171 is in scope even though it isn't real. */
18174 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18178 if (current_class_ptr
)
18180 /* We don't clear this between NSDMIs. Is it already what we want? */
18181 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18182 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18183 && cp_type_quals (type
) == quals
)
18187 this_parm
= build_this_parm (ctype
, quals
);
18188 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18189 current_class_ptr
= NULL_TREE
;
18191 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18192 current_class_ptr
= this_parm
;
18195 /* Return true iff our current scope is a non-static data member
18199 parsing_nsdmi (void)
18201 /* We recognize NSDMI context by the context-less 'this' pointer set up
18202 by the function above. */
18203 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18208 /* Parse a late-specified return type, if any. This is not a separate
18209 non-terminal, but part of a function declarator, which looks like
18211 -> trailing-type-specifier-seq abstract-declarator(opt)
18213 Returns the type indicated by the type-id.
18215 In addition to this this parses any queued up omp declare simd
18216 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18218 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18222 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18226 tree type
= NULL_TREE
;
18227 bool declare_simd_p
= (parser
->omp_declare_simd
18229 && declarator
->kind
== cdk_id
);
18231 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18232 && declarator
&& declarator
->kind
== cdk_id
);
18234 /* Peek at the next token. */
18235 token
= cp_lexer_peek_token (parser
->lexer
);
18236 /* A late-specified return type is indicated by an initial '->'. */
18237 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18240 tree save_ccp
= current_class_ptr
;
18241 tree save_ccr
= current_class_ref
;
18244 /* DR 1207: 'this' is in scope in the trailing return type. */
18245 inject_this_parameter (current_class_type
, quals
);
18248 if (token
->type
== CPP_DEREF
)
18250 /* Consume the ->. */
18251 cp_lexer_consume_token (parser
->lexer
);
18253 type
= cp_parser_trailing_type_id (parser
);
18256 if (cilk_simd_fn_vector_p
)
18257 declarator
->std_attributes
18258 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18259 declarator
->std_attributes
);
18260 if (declare_simd_p
)
18261 declarator
->std_attributes
18262 = cp_parser_late_parsing_omp_declare_simd (parser
,
18263 declarator
->std_attributes
);
18267 current_class_ptr
= save_ccp
;
18268 current_class_ref
= save_ccr
;
18274 /* Parse a declarator-id.
18278 :: [opt] nested-name-specifier [opt] type-name
18280 In the `id-expression' case, the value returned is as for
18281 cp_parser_id_expression if the id-expression was an unqualified-id.
18282 If the id-expression was a qualified-id, then a SCOPE_REF is
18283 returned. The first operand is the scope (either a NAMESPACE_DECL
18284 or TREE_TYPE), but the second is still just a representation of an
18288 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18291 /* The expression must be an id-expression. Assume that qualified
18292 names are the names of types so that:
18295 int S<T>::R::i = 3;
18297 will work; we must treat `S<T>::R' as the name of a type.
18298 Similarly, assume that qualified names are templates, where
18302 int S<T>::R<T>::i = 3;
18305 id
= cp_parser_id_expression (parser
,
18306 /*template_keyword_p=*/false,
18307 /*check_dependency_p=*/false,
18308 /*template_p=*/NULL
,
18309 /*declarator_p=*/true,
18311 if (id
&& BASELINK_P (id
))
18312 id
= BASELINK_FUNCTIONS (id
);
18316 /* Parse a type-id.
18319 type-specifier-seq abstract-declarator [opt]
18321 Returns the TYPE specified. */
18324 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18325 bool is_trailing_return
)
18327 cp_decl_specifier_seq type_specifier_seq
;
18328 cp_declarator
*abstract_declarator
;
18330 /* Parse the type-specifier-seq. */
18331 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18332 is_trailing_return
,
18333 &type_specifier_seq
);
18334 if (type_specifier_seq
.type
== error_mark_node
)
18335 return error_mark_node
;
18337 /* There might or might not be an abstract declarator. */
18338 cp_parser_parse_tentatively (parser
);
18339 /* Look for the declarator. */
18340 abstract_declarator
18341 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18342 /*parenthesized_p=*/NULL
,
18343 /*member_p=*/false,
18344 /*friend_p=*/false);
18345 /* Check to see if there really was a declarator. */
18346 if (!cp_parser_parse_definitely (parser
))
18347 abstract_declarator
= NULL
;
18349 if (type_specifier_seq
.type
18350 /* None of the valid uses of 'auto' in C++14 involve the type-id
18351 nonterminal, but it is valid in a trailing-return-type. */
18352 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18353 && type_uses_auto (type_specifier_seq
.type
))
18355 /* A type-id with type 'auto' is only ok if the abstract declarator
18356 is a function declarator with a late-specified return type. */
18357 if (abstract_declarator
18358 && abstract_declarator
->kind
== cdk_function
18359 && abstract_declarator
->u
.function
.late_return_type
)
18363 error ("invalid use of %<auto%>");
18364 return error_mark_node
;
18368 return groktypename (&type_specifier_seq
, abstract_declarator
,
18372 static tree
cp_parser_type_id (cp_parser
*parser
)
18374 return cp_parser_type_id_1 (parser
, false, false);
18377 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18380 const char *saved_message
= parser
->type_definition_forbidden_message
;
18381 parser
->type_definition_forbidden_message
18382 = G_("types may not be defined in template arguments");
18383 r
= cp_parser_type_id_1 (parser
, true, false);
18384 parser
->type_definition_forbidden_message
= saved_message
;
18385 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18387 error ("invalid use of %<auto%> in template argument");
18388 r
= error_mark_node
;
18393 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18395 return cp_parser_type_id_1 (parser
, false, true);
18398 /* Parse a type-specifier-seq.
18400 type-specifier-seq:
18401 type-specifier type-specifier-seq [opt]
18405 type-specifier-seq:
18406 attributes type-specifier-seq [opt]
18408 If IS_DECLARATION is true, we are at the start of a "condition" or
18409 exception-declaration, so we might be followed by a declarator-id.
18411 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18412 i.e. we've just seen "->".
18414 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18417 cp_parser_type_specifier_seq (cp_parser
* parser
,
18418 bool is_declaration
,
18419 bool is_trailing_return
,
18420 cp_decl_specifier_seq
*type_specifier_seq
)
18422 bool seen_type_specifier
= false;
18423 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18424 cp_token
*start_token
= NULL
;
18426 /* Clear the TYPE_SPECIFIER_SEQ. */
18427 clear_decl_specs (type_specifier_seq
);
18429 /* In the context of a trailing return type, enum E { } is an
18430 elaborated-type-specifier followed by a function-body, not an
18432 if (is_trailing_return
)
18433 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18435 /* Parse the type-specifiers and attributes. */
18438 tree type_specifier
;
18439 bool is_cv_qualifier
;
18441 /* Check for attributes first. */
18442 if (cp_next_tokens_can_be_attribute_p (parser
))
18444 type_specifier_seq
->attributes
=
18445 chainon (type_specifier_seq
->attributes
,
18446 cp_parser_attributes_opt (parser
));
18450 /* record the token of the beginning of the type specifier seq,
18451 for error reporting purposes*/
18453 start_token
= cp_lexer_peek_token (parser
->lexer
);
18455 /* Look for the type-specifier. */
18456 type_specifier
= cp_parser_type_specifier (parser
,
18458 type_specifier_seq
,
18459 /*is_declaration=*/false,
18462 if (!type_specifier
)
18464 /* If the first type-specifier could not be found, this is not a
18465 type-specifier-seq at all. */
18466 if (!seen_type_specifier
)
18468 /* Set in_declarator_p to avoid skipping to the semicolon. */
18469 int in_decl
= parser
->in_declarator_p
;
18470 parser
->in_declarator_p
= true;
18472 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18473 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18474 cp_parser_error (parser
, "expected type-specifier");
18476 parser
->in_declarator_p
= in_decl
;
18478 type_specifier_seq
->type
= error_mark_node
;
18481 /* If subsequent type-specifiers could not be found, the
18482 type-specifier-seq is complete. */
18486 seen_type_specifier
= true;
18487 /* The standard says that a condition can be:
18489 type-specifier-seq declarator = assignment-expression
18496 we should treat the "S" as a declarator, not as a
18497 type-specifier. The standard doesn't say that explicitly for
18498 type-specifier-seq, but it does say that for
18499 decl-specifier-seq in an ordinary declaration. Perhaps it
18500 would be clearer just to allow a decl-specifier-seq here, and
18501 then add a semantic restriction that if any decl-specifiers
18502 that are not type-specifiers appear, the program is invalid. */
18503 if (is_declaration
&& !is_cv_qualifier
)
18504 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18508 /* Return whether the function currently being declared has an associated
18509 template parameter list. */
18512 function_being_declared_is_template_p (cp_parser
* parser
)
18514 if (!current_template_parms
|| processing_template_parmlist
)
18517 if (parser
->implicit_template_scope
)
18520 if (at_class_scope_p ()
18521 && TYPE_BEING_DEFINED (current_class_type
))
18522 return parser
->num_template_parameter_lists
!= 0;
18524 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18525 (current_class_type
));
18528 /* Parse a parameter-declaration-clause.
18530 parameter-declaration-clause:
18531 parameter-declaration-list [opt] ... [opt]
18532 parameter-declaration-list , ...
18534 Returns a representation for the parameter declarations. A return
18535 value of NULL indicates a parameter-declaration-clause consisting
18536 only of an ellipsis. */
18539 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18548 int auto_is_implicit_function_template_parm_p
;
18550 parser
->auto_is_implicit_function_template_parm_p
18551 = auto_is_implicit_function_template_parm_p
;
18553 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18557 if (!processing_specialization
18558 && !processing_template_parmlist
18559 && !processing_explicit_instantiation
)
18560 if (!current_function_decl
18561 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18562 parser
->auto_is_implicit_function_template_parm_p
= true;
18564 /* Peek at the next token. */
18565 token
= cp_lexer_peek_token (parser
->lexer
);
18566 /* Check for trivial parameter-declaration-clauses. */
18567 if (token
->type
== CPP_ELLIPSIS
)
18569 /* Consume the `...' token. */
18570 cp_lexer_consume_token (parser
->lexer
);
18573 else if (token
->type
== CPP_CLOSE_PAREN
)
18574 /* There are no parameters. */
18576 #ifndef NO_IMPLICIT_EXTERN_C
18577 if (in_system_header_at (input_location
)
18578 && current_class_type
== NULL
18579 && current_lang_name
== lang_name_c
)
18583 return void_list_node
;
18585 /* Check for `(void)', too, which is a special case. */
18586 else if (token
->keyword
== RID_VOID
18587 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18588 == CPP_CLOSE_PAREN
))
18590 /* Consume the `void' token. */
18591 cp_lexer_consume_token (parser
->lexer
);
18592 /* There are no parameters. */
18593 return void_list_node
;
18596 /* Parse the parameter-declaration-list. */
18597 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18598 /* If a parse error occurred while parsing the
18599 parameter-declaration-list, then the entire
18600 parameter-declaration-clause is erroneous. */
18604 /* Peek at the next token. */
18605 token
= cp_lexer_peek_token (parser
->lexer
);
18606 /* If it's a `,', the clause should terminate with an ellipsis. */
18607 if (token
->type
== CPP_COMMA
)
18609 /* Consume the `,'. */
18610 cp_lexer_consume_token (parser
->lexer
);
18611 /* Expect an ellipsis. */
18613 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18615 /* It might also be `...' if the optional trailing `,' was
18617 else if (token
->type
== CPP_ELLIPSIS
)
18619 /* Consume the `...' token. */
18620 cp_lexer_consume_token (parser
->lexer
);
18621 /* And remember that we saw it. */
18625 ellipsis_p
= false;
18627 /* Finish the parameter list. */
18629 parameters
= chainon (parameters
, void_list_node
);
18634 /* Parse a parameter-declaration-list.
18636 parameter-declaration-list:
18637 parameter-declaration
18638 parameter-declaration-list , parameter-declaration
18640 Returns a representation of the parameter-declaration-list, as for
18641 cp_parser_parameter_declaration_clause. However, the
18642 `void_list_node' is never appended to the list. Upon return,
18643 *IS_ERROR will be true iff an error occurred. */
18646 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18648 tree parameters
= NULL_TREE
;
18649 tree
*tail
= ¶meters
;
18650 bool saved_in_unbraced_linkage_specification_p
;
18653 /* Assume all will go well. */
18655 /* The special considerations that apply to a function within an
18656 unbraced linkage specifications do not apply to the parameters
18657 to the function. */
18658 saved_in_unbraced_linkage_specification_p
18659 = parser
->in_unbraced_linkage_specification_p
;
18660 parser
->in_unbraced_linkage_specification_p
= false;
18662 /* Look for more parameters. */
18665 cp_parameter_declarator
*parameter
;
18666 tree decl
= error_mark_node
;
18667 bool parenthesized_p
= false;
18668 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18669 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18670 (current_template_parms
)) : 0);
18672 /* Parse the parameter. */
18674 = cp_parser_parameter_declaration (parser
,
18675 /*template_parm_p=*/false,
18678 /* We don't know yet if the enclosing context is deprecated, so wait
18679 and warn in grokparms if appropriate. */
18680 deprecated_state
= DEPRECATED_SUPPRESS
;
18684 /* If a function parameter pack was specified and an implicit template
18685 parameter was introduced during cp_parser_parameter_declaration,
18686 change any implicit parameters introduced into packs. */
18687 if (parser
->implicit_template_parms
18688 && parameter
->declarator
18689 && parameter
->declarator
->parameter_pack_p
)
18691 int latest_template_parm_idx
= TREE_VEC_LENGTH
18692 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18694 if (latest_template_parm_idx
!= template_parm_idx
)
18695 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18696 (parameter
->decl_specifiers
.type
,
18697 template_parm_idx
, latest_template_parm_idx
);
18700 decl
= grokdeclarator (parameter
->declarator
,
18701 ¶meter
->decl_specifiers
,
18703 parameter
->default_argument
!= NULL_TREE
,
18704 ¶meter
->decl_specifiers
.attributes
);
18707 deprecated_state
= DEPRECATED_NORMAL
;
18709 /* If a parse error occurred parsing the parameter declaration,
18710 then the entire parameter-declaration-list is erroneous. */
18711 if (decl
== error_mark_node
)
18714 parameters
= error_mark_node
;
18718 if (parameter
->decl_specifiers
.attributes
)
18719 cplus_decl_attributes (&decl
,
18720 parameter
->decl_specifiers
.attributes
,
18722 if (DECL_NAME (decl
))
18723 decl
= pushdecl (decl
);
18725 if (decl
!= error_mark_node
)
18727 retrofit_lang_decl (decl
);
18728 DECL_PARM_INDEX (decl
) = ++index
;
18729 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18732 /* Add the new parameter to the list. */
18733 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18734 tail
= &TREE_CHAIN (*tail
);
18736 /* Peek at the next token. */
18737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18738 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18739 /* These are for Objective-C++ */
18740 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18741 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18742 /* The parameter-declaration-list is complete. */
18744 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18748 /* Peek at the next token. */
18749 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18750 /* If it's an ellipsis, then the list is complete. */
18751 if (token
->type
== CPP_ELLIPSIS
)
18753 /* Otherwise, there must be more parameters. Consume the
18755 cp_lexer_consume_token (parser
->lexer
);
18756 /* When parsing something like:
18758 int i(float f, double d)
18760 we can tell after seeing the declaration for "f" that we
18761 are not looking at an initialization of a variable "i",
18762 but rather at the declaration of a function "i".
18764 Due to the fact that the parsing of template arguments
18765 (as specified to a template-id) requires backtracking we
18766 cannot use this technique when inside a template argument
18768 if (!parser
->in_template_argument_list_p
18769 && !parser
->in_type_id_in_expr_p
18770 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18771 /* However, a parameter-declaration of the form
18772 "float(f)" (which is a valid declaration of a
18773 parameter "f") can also be interpreted as an
18774 expression (the conversion of "f" to "float"). */
18775 && !parenthesized_p
)
18776 cp_parser_commit_to_tentative_parse (parser
);
18780 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18781 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18782 cp_parser_skip_to_closing_parenthesis (parser
,
18783 /*recovering=*/true,
18784 /*or_comma=*/false,
18785 /*consume_paren=*/false);
18790 parser
->in_unbraced_linkage_specification_p
18791 = saved_in_unbraced_linkage_specification_p
;
18793 /* Reset implicit_template_scope if we are about to leave the function
18794 parameter list that introduced it. Note that for out-of-line member
18795 definitions, there will be one or more class scopes before we get to
18796 the template parameter scope. */
18798 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18799 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18801 while (maybe_its
->kind
== sk_class
)
18802 maybe_its
= maybe_its
->level_chain
;
18803 if (maybe_its
== its
)
18805 parser
->implicit_template_parms
= 0;
18806 parser
->implicit_template_scope
= 0;
18813 /* Parse a parameter declaration.
18815 parameter-declaration:
18816 decl-specifier-seq ... [opt] declarator
18817 decl-specifier-seq declarator = assignment-expression
18818 decl-specifier-seq ... [opt] abstract-declarator [opt]
18819 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18821 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18822 declares a template parameter. (In that case, a non-nested `>'
18823 token encountered during the parsing of the assignment-expression
18824 is not interpreted as a greater-than operator.)
18826 Returns a representation of the parameter, or NULL if an error
18827 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18828 true iff the declarator is of the form "(p)". */
18830 static cp_parameter_declarator
*
18831 cp_parser_parameter_declaration (cp_parser
*parser
,
18832 bool template_parm_p
,
18833 bool *parenthesized_p
)
18835 int declares_class_or_enum
;
18836 cp_decl_specifier_seq decl_specifiers
;
18837 cp_declarator
*declarator
;
18838 tree default_argument
;
18839 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18840 const char *saved_message
;
18842 /* In a template parameter, `>' is not an operator.
18846 When parsing a default template-argument for a non-type
18847 template-parameter, the first non-nested `>' is taken as the end
18848 of the template parameter-list rather than a greater-than
18851 /* Type definitions may not appear in parameter types. */
18852 saved_message
= parser
->type_definition_forbidden_message
;
18853 parser
->type_definition_forbidden_message
18854 = G_("types may not be defined in parameter types");
18856 /* Parse the declaration-specifiers. */
18857 cp_parser_decl_specifier_seq (parser
,
18858 CP_PARSER_FLAGS_NONE
,
18860 &declares_class_or_enum
);
18862 /* Complain about missing 'typename' or other invalid type names. */
18863 if (!decl_specifiers
.any_type_specifiers_p
18864 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18865 decl_specifiers
.type
= error_mark_node
;
18867 /* If an error occurred, there's no reason to attempt to parse the
18868 rest of the declaration. */
18869 if (cp_parser_error_occurred (parser
))
18871 parser
->type_definition_forbidden_message
= saved_message
;
18875 /* Peek at the next token. */
18876 token
= cp_lexer_peek_token (parser
->lexer
);
18878 /* If the next token is a `)', `,', `=', `>', or `...', then there
18879 is no declarator. However, when variadic templates are enabled,
18880 there may be a declarator following `...'. */
18881 if (token
->type
== CPP_CLOSE_PAREN
18882 || token
->type
== CPP_COMMA
18883 || token
->type
== CPP_EQ
18884 || token
->type
== CPP_GREATER
)
18887 if (parenthesized_p
)
18888 *parenthesized_p
= false;
18890 /* Otherwise, there should be a declarator. */
18893 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
18894 parser
->default_arg_ok_p
= false;
18896 /* After seeing a decl-specifier-seq, if the next token is not a
18897 "(", there is no possibility that the code is a valid
18898 expression. Therefore, if parsing tentatively, we commit at
18900 if (!parser
->in_template_argument_list_p
18901 /* In an expression context, having seen:
18905 we cannot be sure whether we are looking at a
18906 function-type (taking a "char" as a parameter) or a cast
18907 of some object of type "char" to "int". */
18908 && !parser
->in_type_id_in_expr_p
18909 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18910 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
18911 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
18912 cp_parser_commit_to_tentative_parse (parser
);
18913 /* Parse the declarator. */
18914 declarator_token_start
= token
;
18915 declarator
= cp_parser_declarator (parser
,
18916 CP_PARSER_DECLARATOR_EITHER
,
18917 /*ctor_dtor_or_conv_p=*/NULL
,
18919 /*member_p=*/false,
18920 /*friend_p=*/false);
18921 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
18922 /* After the declarator, allow more attributes. */
18923 decl_specifiers
.attributes
18924 = chainon (decl_specifiers
.attributes
,
18925 cp_parser_attributes_opt (parser
));
18928 /* If the next token is an ellipsis, and we have not seen a
18929 declarator name, and the type of the declarator contains parameter
18930 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18931 a parameter pack expansion expression. Otherwise, leave the
18932 ellipsis for a C-style variadic function. */
18933 token
= cp_lexer_peek_token (parser
->lexer
);
18934 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
18936 tree type
= decl_specifiers
.type
;
18938 if (type
&& DECL_P (type
))
18939 type
= TREE_TYPE (type
);
18942 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
18943 && declarator_can_be_parameter_pack (declarator
)
18944 && (!declarator
|| !declarator
->parameter_pack_p
)
18945 && uses_parameter_packs (type
))
18947 /* Consume the `...'. */
18948 cp_lexer_consume_token (parser
->lexer
);
18949 maybe_warn_variadic_templates ();
18951 /* Build a pack expansion type */
18953 declarator
->parameter_pack_p
= true;
18955 decl_specifiers
.type
= make_pack_expansion (type
);
18959 /* The restriction on defining new types applies only to the type
18960 of the parameter, not to the default argument. */
18961 parser
->type_definition_forbidden_message
= saved_message
;
18963 /* If the next token is `=', then process a default argument. */
18964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
18966 token
= cp_lexer_peek_token (parser
->lexer
);
18967 /* If we are defining a class, then the tokens that make up the
18968 default argument must be saved and processed later. */
18969 if (!template_parm_p
&& at_class_scope_p ()
18970 && TYPE_BEING_DEFINED (current_class_type
)
18971 && !LAMBDA_TYPE_P (current_class_type
))
18972 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
18973 /* Outside of a class definition, we can just parse the
18974 assignment-expression. */
18977 = cp_parser_default_argument (parser
, template_parm_p
);
18979 if (!parser
->default_arg_ok_p
)
18981 if (flag_permissive
)
18982 warning (0, "deprecated use of default argument for parameter of non-function");
18985 error_at (token
->location
,
18986 "default arguments are only "
18987 "permitted for function parameters");
18988 default_argument
= NULL_TREE
;
18991 else if ((declarator
&& declarator
->parameter_pack_p
)
18992 || (decl_specifiers
.type
18993 && PACK_EXPANSION_P (decl_specifiers
.type
)))
18995 /* Find the name of the parameter pack. */
18996 cp_declarator
*id_declarator
= declarator
;
18997 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
18998 id_declarator
= id_declarator
->declarator
;
19000 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19001 error_at (declarator_token_start
->location
,
19003 ? G_("template parameter pack %qD "
19004 "cannot have a default argument")
19005 : G_("parameter pack %qD cannot have "
19006 "a default argument"),
19007 id_declarator
->u
.id
.unqualified_name
);
19009 error_at (declarator_token_start
->location
,
19011 ? G_("template parameter pack cannot have "
19012 "a default argument")
19013 : G_("parameter pack cannot have a "
19014 "default argument"));
19016 default_argument
= NULL_TREE
;
19020 default_argument
= NULL_TREE
;
19022 return make_parameter_declarator (&decl_specifiers
,
19027 /* Parse a default argument and return it.
19029 TEMPLATE_PARM_P is true if this is a default argument for a
19030 non-type template parameter. */
19032 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19034 tree default_argument
= NULL_TREE
;
19035 bool saved_greater_than_is_operator_p
;
19036 bool saved_local_variables_forbidden_p
;
19037 bool non_constant_p
, is_direct_init
;
19039 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19041 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19042 parser
->greater_than_is_operator_p
= !template_parm_p
;
19043 /* Local variable names (and the `this' keyword) may not
19044 appear in a default argument. */
19045 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19046 parser
->local_variables_forbidden_p
= true;
19047 /* Parse the assignment-expression. */
19048 if (template_parm_p
)
19049 push_deferring_access_checks (dk_no_deferred
);
19050 tree saved_class_ptr
= NULL_TREE
;
19051 tree saved_class_ref
= NULL_TREE
;
19052 /* The "this" pointer is not valid in a default argument. */
19055 saved_class_ptr
= current_class_ptr
;
19056 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19057 saved_class_ref
= current_class_ref
;
19058 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19061 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19062 /* Restore the "this" pointer. */
19065 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19066 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19068 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19069 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19070 if (template_parm_p
)
19071 pop_deferring_access_checks ();
19072 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19073 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19075 return default_argument
;
19078 /* Parse a function-body.
19081 compound_statement */
19084 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19086 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19089 /* Parse a ctor-initializer-opt followed by a function-body. Return
19090 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19091 is true we are parsing a function-try-block. */
19094 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19095 bool in_function_try_block
)
19098 bool ctor_initializer_p
;
19099 const bool check_body_p
=
19100 DECL_CONSTRUCTOR_P (current_function_decl
)
19101 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19104 /* Begin the function body. */
19105 body
= begin_function_body ();
19106 /* Parse the optional ctor-initializer. */
19107 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19109 /* If we're parsing a constexpr constructor definition, we need
19110 to check that the constructor body is indeed empty. However,
19111 before we get to cp_parser_function_body lot of junk has been
19112 generated, so we can't just check that we have an empty block.
19113 Rather we take a snapshot of the outermost block, and check whether
19114 cp_parser_function_body changed its state. */
19117 list
= cur_stmt_list
;
19118 if (STATEMENT_LIST_TAIL (list
))
19119 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19121 /* Parse the function-body. */
19122 cp_parser_function_body (parser
, in_function_try_block
);
19124 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19125 /* Finish the function body. */
19126 finish_function_body (body
);
19128 return ctor_initializer_p
;
19131 /* Parse an initializer.
19134 = initializer-clause
19135 ( expression-list )
19137 Returns an expression representing the initializer. If no
19138 initializer is present, NULL_TREE is returned.
19140 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19141 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19142 set to TRUE if there is no initializer present. If there is an
19143 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19144 is set to true; otherwise it is set to false. */
19147 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19148 bool* non_constant_p
)
19153 /* Peek at the next token. */
19154 token
= cp_lexer_peek_token (parser
->lexer
);
19156 /* Let our caller know whether or not this initializer was
19158 *is_direct_init
= (token
->type
!= CPP_EQ
);
19159 /* Assume that the initializer is constant. */
19160 *non_constant_p
= false;
19162 if (token
->type
== CPP_EQ
)
19164 /* Consume the `='. */
19165 cp_lexer_consume_token (parser
->lexer
);
19166 /* Parse the initializer-clause. */
19167 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19169 else if (token
->type
== CPP_OPEN_PAREN
)
19171 vec
<tree
, va_gc
> *vec
;
19172 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19174 /*allow_expansion_p=*/true,
19177 return error_mark_node
;
19178 init
= build_tree_list_vec (vec
);
19179 release_tree_vector (vec
);
19181 else if (token
->type
== CPP_OPEN_BRACE
)
19183 cp_lexer_set_source_position (parser
->lexer
);
19184 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19185 init
= cp_parser_braced_list (parser
, non_constant_p
);
19186 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19190 /* Anything else is an error. */
19191 cp_parser_error (parser
, "expected initializer");
19192 init
= error_mark_node
;
19198 /* Parse an initializer-clause.
19200 initializer-clause:
19201 assignment-expression
19204 Returns an expression representing the initializer.
19206 If the `assignment-expression' production is used the value
19207 returned is simply a representation for the expression.
19209 Otherwise, calls cp_parser_braced_list. */
19212 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19216 /* Assume the expression is constant. */
19217 *non_constant_p
= false;
19219 /* If it is not a `{', then we are looking at an
19220 assignment-expression. */
19221 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19224 = cp_parser_constant_expression (parser
,
19225 /*allow_non_constant_p=*/true,
19229 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19231 return initializer
;
19234 /* Parse a brace-enclosed initializer list.
19237 { initializer-list , [opt] }
19240 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19241 the elements of the initializer-list (or NULL, if the last
19242 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19243 NULL_TREE. There is no way to detect whether or not the optional
19244 trailing `,' was provided. NON_CONSTANT_P is as for
19245 cp_parser_initializer. */
19248 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19252 /* Consume the `{' token. */
19253 cp_lexer_consume_token (parser
->lexer
);
19254 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19255 initializer
= make_node (CONSTRUCTOR
);
19256 /* If it's not a `}', then there is a non-trivial initializer. */
19257 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19259 /* Parse the initializer list. */
19260 CONSTRUCTOR_ELTS (initializer
)
19261 = cp_parser_initializer_list (parser
, non_constant_p
);
19262 /* A trailing `,' token is allowed. */
19263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19264 cp_lexer_consume_token (parser
->lexer
);
19267 *non_constant_p
= false;
19268 /* Now, there should be a trailing `}'. */
19269 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19270 TREE_TYPE (initializer
) = init_list_type_node
;
19271 return initializer
;
19274 /* Consume tokens up to, and including, the next non-nested closing `]'.
19275 Returns true iff we found a closing `]'. */
19278 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19280 unsigned square_depth
= 0;
19284 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19286 switch (token
->type
)
19289 case CPP_PRAGMA_EOL
:
19290 /* If we've run out of tokens, then there is no closing `]'. */
19293 case CPP_OPEN_SQUARE
:
19297 case CPP_CLOSE_SQUARE
:
19298 if (!square_depth
--)
19300 cp_lexer_consume_token (parser
->lexer
);
19309 /* Consume the token. */
19310 cp_lexer_consume_token (parser
->lexer
);
19314 /* Return true if we are looking at an array-designator, false otherwise. */
19317 cp_parser_array_designator_p (cp_parser
*parser
)
19319 /* Consume the `['. */
19320 cp_lexer_consume_token (parser
->lexer
);
19322 cp_lexer_save_tokens (parser
->lexer
);
19324 /* Skip tokens until the next token is a closing square bracket.
19325 If we find the closing `]', and the next token is a `=', then
19326 we are looking at an array designator. */
19327 bool array_designator_p
19328 = (cp_parser_skip_to_closing_square_bracket (parser
)
19329 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19331 /* Roll back the tokens we skipped. */
19332 cp_lexer_rollback_tokens (parser
->lexer
);
19334 return array_designator_p
;
19337 /* Parse an initializer-list.
19340 initializer-clause ... [opt]
19341 initializer-list , initializer-clause ... [opt]
19346 designation initializer-clause ...[opt]
19347 initializer-list , designation initializer-clause ...[opt]
19352 [ constant-expression ] =
19354 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19355 for the initializer. If the INDEX of the elt is non-NULL, it is the
19356 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19357 as for cp_parser_initializer. */
19359 static vec
<constructor_elt
, va_gc
> *
19360 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19362 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19364 /* Assume all of the expressions are constant. */
19365 *non_constant_p
= false;
19367 /* Parse the rest of the list. */
19373 bool clause_non_constant_p
;
19375 /* If the next token is an identifier and the following one is a
19376 colon, we are looking at the GNU designated-initializer
19378 if (cp_parser_allow_gnu_extensions_p (parser
)
19379 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19380 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19382 /* Warn the user that they are using an extension. */
19383 pedwarn (input_location
, OPT_Wpedantic
,
19384 "ISO C++ does not allow designated initializers");
19385 /* Consume the identifier. */
19386 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19387 /* Consume the `:'. */
19388 cp_lexer_consume_token (parser
->lexer
);
19390 /* Also handle the C99 syntax, '. id ='. */
19391 else if (cp_parser_allow_gnu_extensions_p (parser
)
19392 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19393 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19394 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19396 /* Warn the user that they are using an extension. */
19397 pedwarn (input_location
, OPT_Wpedantic
,
19398 "ISO C++ does not allow C99 designated initializers");
19399 /* Consume the `.'. */
19400 cp_lexer_consume_token (parser
->lexer
);
19401 /* Consume the identifier. */
19402 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19403 /* Consume the `='. */
19404 cp_lexer_consume_token (parser
->lexer
);
19406 /* Also handle C99 array designators, '[ const ] ='. */
19407 else if (cp_parser_allow_gnu_extensions_p (parser
)
19408 && !c_dialect_objc ()
19409 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19411 /* In C++11, [ could start a lambda-introducer. */
19412 bool non_const
= false;
19414 cp_parser_parse_tentatively (parser
);
19416 if (!cp_parser_array_designator_p (parser
))
19418 cp_parser_simulate_error (parser
);
19419 designator
= NULL_TREE
;
19423 designator
= cp_parser_constant_expression (parser
, true,
19425 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19426 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19429 if (!cp_parser_parse_definitely (parser
))
19430 designator
= NULL_TREE
;
19431 else if (non_const
)
19432 require_potential_rvalue_constant_expression (designator
);
19435 designator
= NULL_TREE
;
19437 /* Parse the initializer. */
19438 initializer
= cp_parser_initializer_clause (parser
,
19439 &clause_non_constant_p
);
19440 /* If any clause is non-constant, so is the entire initializer. */
19441 if (clause_non_constant_p
)
19442 *non_constant_p
= true;
19444 /* If we have an ellipsis, this is an initializer pack
19446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19448 /* Consume the `...'. */
19449 cp_lexer_consume_token (parser
->lexer
);
19451 /* Turn the initializer into an initializer expansion. */
19452 initializer
= make_pack_expansion (initializer
);
19455 /* Add it to the vector. */
19456 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19458 /* If the next token is not a comma, we have reached the end of
19460 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19463 /* Peek at the next token. */
19464 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19465 /* If the next token is a `}', then we're still done. An
19466 initializer-clause can have a trailing `,' after the
19467 initializer-list and before the closing `}'. */
19468 if (token
->type
== CPP_CLOSE_BRACE
)
19471 /* Consume the `,' token. */
19472 cp_lexer_consume_token (parser
->lexer
);
19478 /* Classes [gram.class] */
19480 /* Parse a class-name.
19486 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19487 to indicate that names looked up in dependent types should be
19488 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19489 keyword has been used to indicate that the name that appears next
19490 is a template. TAG_TYPE indicates the explicit tag given before
19491 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19492 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19493 is the class being defined in a class-head.
19495 Returns the TYPE_DECL representing the class. */
19498 cp_parser_class_name (cp_parser
*parser
,
19499 bool typename_keyword_p
,
19500 bool template_keyword_p
,
19501 enum tag_types tag_type
,
19502 bool check_dependency_p
,
19504 bool is_declaration
)
19510 tree identifier
= NULL_TREE
;
19512 /* All class-names start with an identifier. */
19513 token
= cp_lexer_peek_token (parser
->lexer
);
19514 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19516 cp_parser_error (parser
, "expected class-name");
19517 return error_mark_node
;
19520 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19521 to a template-id, so we save it here. */
19522 scope
= parser
->scope
;
19523 if (scope
== error_mark_node
)
19524 return error_mark_node
;
19526 /* Any name names a type if we're following the `typename' keyword
19527 in a qualified name where the enclosing scope is type-dependent. */
19528 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19529 && dependent_type_p (scope
));
19530 /* Handle the common case (an identifier, but not a template-id)
19532 if (token
->type
== CPP_NAME
19533 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19535 cp_token
*identifier_token
;
19538 /* Look for the identifier. */
19539 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19540 ambiguous_p
= identifier_token
->error_reported
;
19541 identifier
= cp_parser_identifier (parser
);
19542 /* If the next token isn't an identifier, we are certainly not
19543 looking at a class-name. */
19544 if (identifier
== error_mark_node
)
19545 decl
= error_mark_node
;
19546 /* If we know this is a type-name, there's no need to look it
19548 else if (typename_p
)
19552 tree ambiguous_decls
;
19553 /* If we already know that this lookup is ambiguous, then
19554 we've already issued an error message; there's no reason
19558 cp_parser_simulate_error (parser
);
19559 return error_mark_node
;
19561 /* If the next token is a `::', then the name must be a type
19564 [basic.lookup.qual]
19566 During the lookup for a name preceding the :: scope
19567 resolution operator, object, function, and enumerator
19568 names are ignored. */
19569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19570 tag_type
= typename_type
;
19571 /* Look up the name. */
19572 decl
= cp_parser_lookup_name (parser
, identifier
,
19574 /*is_template=*/false,
19575 /*is_namespace=*/false,
19576 check_dependency_p
,
19578 identifier_token
->location
);
19579 if (ambiguous_decls
)
19581 if (cp_parser_parsing_tentatively (parser
))
19582 cp_parser_simulate_error (parser
);
19583 return error_mark_node
;
19589 /* Try a template-id. */
19590 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19591 check_dependency_p
,
19594 if (decl
== error_mark_node
)
19595 return error_mark_node
;
19598 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19600 /* If this is a typename, create a TYPENAME_TYPE. */
19601 if (typename_p
&& decl
!= error_mark_node
)
19603 decl
= make_typename_type (scope
, decl
, typename_type
,
19604 /*complain=*/tf_error
);
19605 if (decl
!= error_mark_node
)
19606 decl
= TYPE_NAME (decl
);
19609 decl
= strip_using_decl (decl
);
19611 /* Check to see that it is really the name of a class. */
19612 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19613 && identifier_p (TREE_OPERAND (decl
, 0))
19614 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19615 /* Situations like this:
19617 template <typename T> struct A {
19618 typename T::template X<int>::I i;
19621 are problematic. Is `T::template X<int>' a class-name? The
19622 standard does not seem to be definitive, but there is no other
19623 valid interpretation of the following `::'. Therefore, those
19624 names are considered class-names. */
19626 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19627 if (decl
!= error_mark_node
)
19628 decl
= TYPE_NAME (decl
);
19630 else if (TREE_CODE (decl
) != TYPE_DECL
19631 || TREE_TYPE (decl
) == error_mark_node
19632 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19633 /* In Objective-C 2.0, a classname followed by '.' starts a
19634 dot-syntax expression, and it's not a type-name. */
19635 || (c_dialect_objc ()
19636 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19637 && objc_is_class_name (decl
)))
19638 decl
= error_mark_node
;
19640 if (decl
== error_mark_node
)
19641 cp_parser_error (parser
, "expected class-name");
19642 else if (identifier
&& !parser
->scope
)
19643 maybe_note_name_used_in_class (identifier
, decl
);
19648 /* Parse a class-specifier.
19651 class-head { member-specification [opt] }
19653 Returns the TREE_TYPE representing the class. */
19656 cp_parser_class_specifier_1 (cp_parser
* parser
)
19659 tree attributes
= NULL_TREE
;
19660 bool nested_name_specifier_p
;
19661 unsigned saved_num_template_parameter_lists
;
19662 bool saved_in_function_body
;
19663 unsigned char in_statement
;
19664 bool in_switch_statement_p
;
19665 bool saved_in_unbraced_linkage_specification_p
;
19666 tree old_scope
= NULL_TREE
;
19667 tree scope
= NULL_TREE
;
19668 cp_token
*closing_brace
;
19670 push_deferring_access_checks (dk_no_deferred
);
19672 /* Parse the class-head. */
19673 type
= cp_parser_class_head (parser
,
19674 &nested_name_specifier_p
);
19675 /* If the class-head was a semantic disaster, skip the entire body
19679 cp_parser_skip_to_end_of_block_or_statement (parser
);
19680 pop_deferring_access_checks ();
19681 return error_mark_node
;
19684 /* Look for the `{'. */
19685 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19687 pop_deferring_access_checks ();
19688 return error_mark_node
;
19691 cp_ensure_no_omp_declare_simd (parser
);
19693 /* Issue an error message if type-definitions are forbidden here. */
19694 cp_parser_check_type_definition (parser
);
19695 /* Remember that we are defining one more class. */
19696 ++parser
->num_classes_being_defined
;
19697 /* Inside the class, surrounding template-parameter-lists do not
19699 saved_num_template_parameter_lists
19700 = parser
->num_template_parameter_lists
;
19701 parser
->num_template_parameter_lists
= 0;
19702 /* We are not in a function body. */
19703 saved_in_function_body
= parser
->in_function_body
;
19704 parser
->in_function_body
= false;
19705 /* Or in a loop. */
19706 in_statement
= parser
->in_statement
;
19707 parser
->in_statement
= 0;
19708 /* Or in a switch. */
19709 in_switch_statement_p
= parser
->in_switch_statement_p
;
19710 parser
->in_switch_statement_p
= false;
19711 /* We are not immediately inside an extern "lang" block. */
19712 saved_in_unbraced_linkage_specification_p
19713 = parser
->in_unbraced_linkage_specification_p
;
19714 parser
->in_unbraced_linkage_specification_p
= false;
19716 /* Start the class. */
19717 if (nested_name_specifier_p
)
19719 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19720 old_scope
= push_inner_scope (scope
);
19722 type
= begin_class_definition (type
);
19724 if (type
== error_mark_node
)
19725 /* If the type is erroneous, skip the entire body of the class. */
19726 cp_parser_skip_to_closing_brace (parser
);
19728 /* Parse the member-specification. */
19729 cp_parser_member_specification_opt (parser
);
19731 /* Look for the trailing `}'. */
19732 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19733 /* Look for trailing attributes to apply to this class. */
19734 if (cp_parser_allow_gnu_extensions_p (parser
))
19735 attributes
= cp_parser_gnu_attributes_opt (parser
);
19736 if (type
!= error_mark_node
)
19737 type
= finish_struct (type
, attributes
);
19738 if (nested_name_specifier_p
)
19739 pop_inner_scope (old_scope
, scope
);
19741 /* We've finished a type definition. Check for the common syntax
19742 error of forgetting a semicolon after the definition. We need to
19743 be careful, as we can't just check for not-a-semicolon and be done
19744 with it; the user might have typed:
19746 class X { } c = ...;
19747 class X { } *p = ...;
19749 and so forth. Instead, enumerate all the possible tokens that
19750 might follow this production; if we don't see one of them, then
19751 complain and silently insert the semicolon. */
19753 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19754 bool want_semicolon
= true;
19756 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19757 /* Don't try to parse c++11 attributes here. As per the
19758 grammar, that should be a task for
19759 cp_parser_decl_specifier_seq. */
19760 want_semicolon
= false;
19762 switch (token
->type
)
19765 case CPP_SEMICOLON
:
19768 case CPP_OPEN_PAREN
:
19769 case CPP_CLOSE_PAREN
:
19771 want_semicolon
= false;
19774 /* While it's legal for type qualifiers and storage class
19775 specifiers to follow type definitions in the grammar, only
19776 compiler testsuites contain code like that. Assume that if
19777 we see such code, then what we're really seeing is a case
19781 const <type> var = ...;
19786 static <type> func (...) ...
19788 i.e. the qualifier or specifier applies to the next
19789 declaration. To do so, however, we need to look ahead one
19790 more token to see if *that* token is a type specifier.
19792 This code could be improved to handle:
19795 static const <type> var = ...; */
19797 if (keyword_is_decl_specifier (token
->keyword
))
19799 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19801 /* Handling user-defined types here would be nice, but very
19804 = (lookahead
->type
== CPP_KEYWORD
19805 && keyword_begins_type_specifier (lookahead
->keyword
));
19812 /* If we don't have a type, then something is very wrong and we
19813 shouldn't try to do anything clever. Likewise for not seeing the
19815 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19817 cp_token_position prev
19818 = cp_lexer_previous_token_position (parser
->lexer
);
19819 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19820 location_t loc
= prev_token
->location
;
19822 if (CLASSTYPE_DECLARED_CLASS (type
))
19823 error_at (loc
, "expected %<;%> after class definition");
19824 else if (TREE_CODE (type
) == RECORD_TYPE
)
19825 error_at (loc
, "expected %<;%> after struct definition");
19826 else if (TREE_CODE (type
) == UNION_TYPE
)
19827 error_at (loc
, "expected %<;%> after union definition");
19829 gcc_unreachable ();
19831 /* Unget one token and smash it to look as though we encountered
19832 a semicolon in the input stream. */
19833 cp_lexer_set_token_position (parser
->lexer
, prev
);
19834 token
= cp_lexer_peek_token (parser
->lexer
);
19835 token
->type
= CPP_SEMICOLON
;
19836 token
->keyword
= RID_MAX
;
19840 /* If this class is not itself within the scope of another class,
19841 then we need to parse the bodies of all of the queued function
19842 definitions. Note that the queued functions defined in a class
19843 are not always processed immediately following the
19844 class-specifier for that class. Consider:
19847 struct B { void f() { sizeof (A); } };
19850 If `f' were processed before the processing of `A' were
19851 completed, there would be no way to compute the size of `A'.
19852 Note that the nesting we are interested in here is lexical --
19853 not the semantic nesting given by TYPE_CONTEXT. In particular,
19856 struct A { struct B; };
19857 struct A::B { void f() { } };
19859 there is no need to delay the parsing of `A::B::f'. */
19860 if (--parser
->num_classes_being_defined
== 0)
19863 tree class_type
= NULL_TREE
;
19864 tree pushed_scope
= NULL_TREE
;
19866 cp_default_arg_entry
*e
;
19867 tree save_ccp
, save_ccr
;
19869 /* In a first pass, parse default arguments to the functions.
19870 Then, in a second pass, parse the bodies of the functions.
19871 This two-phased approach handles cases like:
19879 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19882 /* If there are default arguments that have not yet been processed,
19883 take care of them now. */
19884 if (class_type
!= e
->class_type
)
19887 pop_scope (pushed_scope
);
19888 class_type
= e
->class_type
;
19889 pushed_scope
= push_scope (class_type
);
19891 /* Make sure that any template parameters are in scope. */
19892 maybe_begin_member_template_processing (decl
);
19893 /* Parse the default argument expressions. */
19894 cp_parser_late_parsing_default_args (parser
, decl
);
19895 /* Remove any template parameters from the symbol table. */
19896 maybe_end_member_template_processing ();
19898 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
19899 /* Now parse any NSDMIs. */
19900 save_ccp
= current_class_ptr
;
19901 save_ccr
= current_class_ref
;
19902 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
19904 if (class_type
!= DECL_CONTEXT (decl
))
19907 pop_scope (pushed_scope
);
19908 class_type
= DECL_CONTEXT (decl
);
19909 pushed_scope
= push_scope (class_type
);
19911 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
19912 cp_parser_late_parsing_nsdmi (parser
, decl
);
19914 vec_safe_truncate (unparsed_nsdmis
, 0);
19915 current_class_ptr
= save_ccp
;
19916 current_class_ref
= save_ccr
;
19918 pop_scope (pushed_scope
);
19920 /* Now do some post-NSDMI bookkeeping. */
19921 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
19922 after_nsdmi_defaulted_late_checks (class_type
);
19923 vec_safe_truncate (unparsed_classes
, 0);
19924 after_nsdmi_defaulted_late_checks (type
);
19926 /* Now parse the body of the functions. */
19929 /* OpenMP UDRs need to be parsed before all other functions. */
19930 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19931 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
19932 cp_parser_late_parsing_for_member (parser
, decl
);
19933 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19934 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
19935 cp_parser_late_parsing_for_member (parser
, decl
);
19938 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
19939 cp_parser_late_parsing_for_member (parser
, decl
);
19940 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
19943 vec_safe_push (unparsed_classes
, type
);
19945 /* Put back any saved access checks. */
19946 pop_deferring_access_checks ();
19948 /* Restore saved state. */
19949 parser
->in_switch_statement_p
= in_switch_statement_p
;
19950 parser
->in_statement
= in_statement
;
19951 parser
->in_function_body
= saved_in_function_body
;
19952 parser
->num_template_parameter_lists
19953 = saved_num_template_parameter_lists
;
19954 parser
->in_unbraced_linkage_specification_p
19955 = saved_in_unbraced_linkage_specification_p
;
19961 cp_parser_class_specifier (cp_parser
* parser
)
19964 timevar_push (TV_PARSE_STRUCT
);
19965 ret
= cp_parser_class_specifier_1 (parser
);
19966 timevar_pop (TV_PARSE_STRUCT
);
19970 /* Parse a class-head.
19973 class-key identifier [opt] base-clause [opt]
19974 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19975 class-key nested-name-specifier [opt] template-id
19978 class-virt-specifier:
19982 class-key attributes identifier [opt] base-clause [opt]
19983 class-key attributes nested-name-specifier identifier base-clause [opt]
19984 class-key attributes nested-name-specifier [opt] template-id
19987 Upon return BASES is initialized to the list of base classes (or
19988 NULL, if there are none) in the same form returned by
19989 cp_parser_base_clause.
19991 Returns the TYPE of the indicated class. Sets
19992 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19993 involving a nested-name-specifier was used, and FALSE otherwise.
19995 Returns error_mark_node if this is not a class-head.
19997 Returns NULL_TREE if the class-head is syntactically valid, but
19998 semantically invalid in a way that means we should skip the entire
19999 body of the class. */
20002 cp_parser_class_head (cp_parser
* parser
,
20003 bool* nested_name_specifier_p
)
20005 tree nested_name_specifier
;
20006 enum tag_types class_key
;
20007 tree id
= NULL_TREE
;
20008 tree type
= NULL_TREE
;
20011 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20012 bool template_id_p
= false;
20013 bool qualified_p
= false;
20014 bool invalid_nested_name_p
= false;
20015 bool invalid_explicit_specialization_p
= false;
20016 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20017 tree pushed_scope
= NULL_TREE
;
20018 unsigned num_templates
;
20019 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20020 /* Assume no nested-name-specifier will be present. */
20021 *nested_name_specifier_p
= false;
20022 /* Assume no template parameter lists will be used in defining the
20025 parser
->colon_corrects_to_scope_p
= false;
20027 /* Look for the class-key. */
20028 class_key
= cp_parser_class_key (parser
);
20029 if (class_key
== none_type
)
20030 return error_mark_node
;
20032 /* Parse the attributes. */
20033 attributes
= cp_parser_attributes_opt (parser
);
20035 /* If the next token is `::', that is invalid -- but sometimes
20036 people do try to write:
20040 Handle this gracefully by accepting the extra qualifier, and then
20041 issuing an error about it later if this really is a
20042 class-head. If it turns out just to be an elaborated type
20043 specifier, remain silent. */
20044 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20045 qualified_p
= true;
20047 push_deferring_access_checks (dk_no_check
);
20049 /* Determine the name of the class. Begin by looking for an
20050 optional nested-name-specifier. */
20051 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20052 nested_name_specifier
20053 = cp_parser_nested_name_specifier_opt (parser
,
20054 /*typename_keyword_p=*/false,
20055 /*check_dependency_p=*/false,
20057 /*is_declaration=*/false);
20058 /* If there was a nested-name-specifier, then there *must* be an
20060 if (nested_name_specifier
)
20062 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20063 /* Although the grammar says `identifier', it really means
20064 `class-name' or `template-name'. You are only allowed to
20065 define a class that has already been declared with this
20068 The proposed resolution for Core Issue 180 says that wherever
20069 you see `class T::X' you should treat `X' as a type-name.
20071 It is OK to define an inaccessible class; for example:
20073 class A { class B; };
20076 We do not know if we will see a class-name, or a
20077 template-name. We look for a class-name first, in case the
20078 class-name is a template-id; if we looked for the
20079 template-name first we would stop after the template-name. */
20080 cp_parser_parse_tentatively (parser
);
20081 type
= cp_parser_class_name (parser
,
20082 /*typename_keyword_p=*/false,
20083 /*template_keyword_p=*/false,
20085 /*check_dependency_p=*/false,
20086 /*class_head_p=*/true,
20087 /*is_declaration=*/false);
20088 /* If that didn't work, ignore the nested-name-specifier. */
20089 if (!cp_parser_parse_definitely (parser
))
20091 invalid_nested_name_p
= true;
20092 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20093 id
= cp_parser_identifier (parser
);
20094 if (id
== error_mark_node
)
20097 /* If we could not find a corresponding TYPE, treat this
20098 declaration like an unqualified declaration. */
20099 if (type
== error_mark_node
)
20100 nested_name_specifier
= NULL_TREE
;
20101 /* Otherwise, count the number of templates used in TYPE and its
20102 containing scopes. */
20107 for (scope
= TREE_TYPE (type
);
20108 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20109 scope
= get_containing_scope (scope
))
20111 && CLASS_TYPE_P (scope
)
20112 && CLASSTYPE_TEMPLATE_INFO (scope
)
20113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20114 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20115 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20119 /* Otherwise, the identifier is optional. */
20122 /* We don't know whether what comes next is a template-id,
20123 an identifier, or nothing at all. */
20124 cp_parser_parse_tentatively (parser
);
20125 /* Check for a template-id. */
20126 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20127 id
= cp_parser_template_id (parser
,
20128 /*template_keyword_p=*/false,
20129 /*check_dependency_p=*/true,
20131 /*is_declaration=*/true);
20132 /* If that didn't work, it could still be an identifier. */
20133 if (!cp_parser_parse_definitely (parser
))
20135 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20137 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20138 id
= cp_parser_identifier (parser
);
20145 template_id_p
= true;
20150 pop_deferring_access_checks ();
20154 cp_parser_check_for_invalid_template_id (parser
, id
,
20156 type_start_token
->location
);
20158 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20160 /* If it's not a `:' or a `{' then we can't really be looking at a
20161 class-head, since a class-head only appears as part of a
20162 class-specifier. We have to detect this situation before calling
20163 xref_tag, since that has irreversible side-effects. */
20164 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20166 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20167 type
= error_mark_node
;
20171 /* At this point, we're going ahead with the class-specifier, even
20172 if some other problem occurs. */
20173 cp_parser_commit_to_tentative_parse (parser
);
20174 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20176 cp_parser_error (parser
,
20177 "cannot specify %<override%> for a class");
20178 type
= error_mark_node
;
20181 /* Issue the error about the overly-qualified name now. */
20184 cp_parser_error (parser
,
20185 "global qualification of class name is invalid");
20186 type
= error_mark_node
;
20189 else if (invalid_nested_name_p
)
20191 cp_parser_error (parser
,
20192 "qualified name does not name a class");
20193 type
= error_mark_node
;
20196 else if (nested_name_specifier
)
20200 /* Reject typedef-names in class heads. */
20201 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20203 error_at (type_start_token
->location
,
20204 "invalid class name in declaration of %qD",
20210 /* Figure out in what scope the declaration is being placed. */
20211 scope
= current_scope ();
20212 /* If that scope does not contain the scope in which the
20213 class was originally declared, the program is invalid. */
20214 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20216 if (at_namespace_scope_p ())
20217 error_at (type_start_token
->location
,
20218 "declaration of %qD in namespace %qD which does not "
20220 type
, scope
, nested_name_specifier
);
20222 error_at (type_start_token
->location
,
20223 "declaration of %qD in %qD which does not enclose %qD",
20224 type
, scope
, nested_name_specifier
);
20230 A declarator-id shall not be qualified except for the
20231 definition of a ... nested class outside of its class
20232 ... [or] the definition or explicit instantiation of a
20233 class member of a namespace outside of its namespace. */
20234 if (scope
== nested_name_specifier
)
20236 permerror (nested_name_specifier_token_start
->location
,
20237 "extra qualification not allowed");
20238 nested_name_specifier
= NULL_TREE
;
20242 /* An explicit-specialization must be preceded by "template <>". If
20243 it is not, try to recover gracefully. */
20244 if (at_namespace_scope_p ()
20245 && parser
->num_template_parameter_lists
== 0
20248 error_at (type_start_token
->location
,
20249 "an explicit specialization must be preceded by %<template <>%>");
20250 invalid_explicit_specialization_p
= true;
20251 /* Take the same action that would have been taken by
20252 cp_parser_explicit_specialization. */
20253 ++parser
->num_template_parameter_lists
;
20254 begin_specialization ();
20256 /* There must be no "return" statements between this point and the
20257 end of this function; set "type "to the correct return value and
20258 use "goto done;" to return. */
20259 /* Make sure that the right number of template parameters were
20261 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20262 type_start_token
->location
,
20263 /*declarator=*/NULL
))
20265 /* If something went wrong, there is no point in even trying to
20266 process the class-definition. */
20271 /* Look up the type. */
20274 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20275 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20276 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20278 error_at (type_start_token
->location
,
20279 "function template %qD redeclared as a class template", id
);
20280 type
= error_mark_node
;
20284 type
= TREE_TYPE (id
);
20285 type
= maybe_process_partial_specialization (type
);
20287 if (nested_name_specifier
)
20288 pushed_scope
= push_scope (nested_name_specifier
);
20290 else if (nested_name_specifier
)
20296 template <typename T> struct S { struct T };
20297 template <typename T> struct S<T>::T { };
20299 we will get a TYPENAME_TYPE when processing the definition of
20300 `S::T'. We need to resolve it to the actual type before we
20301 try to define it. */
20302 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20304 class_type
= resolve_typename_type (TREE_TYPE (type
),
20305 /*only_current_p=*/false);
20306 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20307 type
= TYPE_NAME (class_type
);
20310 cp_parser_error (parser
, "could not resolve typename type");
20311 type
= error_mark_node
;
20315 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20316 == error_mark_node
)
20322 class_type
= current_class_type
;
20323 /* Enter the scope indicated by the nested-name-specifier. */
20324 pushed_scope
= push_scope (nested_name_specifier
);
20325 /* Get the canonical version of this type. */
20326 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20327 /* Call push_template_decl if it seems like we should be defining a
20328 template either from the template headers or the type we're
20329 defining, so that we diagnose both extra and missing headers. */
20330 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20331 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20332 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20334 type
= push_template_decl (type
);
20335 if (type
== error_mark_node
)
20342 type
= TREE_TYPE (type
);
20343 *nested_name_specifier_p
= true;
20345 else /* The name is not a nested name. */
20347 /* If the class was unnamed, create a dummy name. */
20349 id
= make_anon_name ();
20350 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20351 parser
->num_template_parameter_lists
);
20354 /* Indicate whether this class was declared as a `class' or as a
20356 if (TREE_CODE (type
) == RECORD_TYPE
)
20357 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20358 cp_parser_check_class_key (class_key
, type
);
20360 /* If this type was already complete, and we see another definition,
20361 that's an error. */
20362 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20364 error_at (type_start_token
->location
, "redefinition of %q#T",
20366 error_at (type_start_token
->location
, "previous definition of %q+#T",
20371 else if (type
== error_mark_node
)
20376 /* Apply attributes now, before any use of the class as a template
20377 argument in its base list. */
20378 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20379 fixup_attribute_variants (type
);
20382 /* We will have entered the scope containing the class; the names of
20383 base classes should be looked up in that context. For example:
20385 struct A { struct B {}; struct C; };
20386 struct A::C : B {};
20390 /* Get the list of base-classes, if there is one. */
20391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20393 /* PR59482: enter the class scope so that base-specifiers are looked
20397 bases
= cp_parser_base_clause (parser
);
20398 /* PR59482: get out of the previously pushed class scope so that the
20399 subsequent pops pop the right thing. */
20406 /* If we're really defining a class, process the base classes.
20407 If they're invalid, fail. */
20408 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20409 && !xref_basetypes (type
, bases
))
20413 /* Leave the scope given by the nested-name-specifier. We will
20414 enter the class scope itself while processing the members. */
20416 pop_scope (pushed_scope
);
20418 if (invalid_explicit_specialization_p
)
20420 end_specialization ();
20421 --parser
->num_template_parameter_lists
;
20425 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20426 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20427 CLASSTYPE_FINAL (type
) = 1;
20429 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20433 /* Parse a class-key.
20440 Returns the kind of class-key specified, or none_type to indicate
20443 static enum tag_types
20444 cp_parser_class_key (cp_parser
* parser
)
20447 enum tag_types tag_type
;
20449 /* Look for the class-key. */
20450 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20454 /* Check to see if the TOKEN is a class-key. */
20455 tag_type
= cp_parser_token_is_class_key (token
);
20457 cp_parser_error (parser
, "expected class-key");
20461 /* Parse a type-parameter-key.
20463 type-parameter-key:
20469 cp_parser_type_parameter_key (cp_parser
* parser
)
20471 /* Look for the type-parameter-key. */
20472 enum tag_types tag_type
= none_type
;
20473 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20474 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20476 cp_lexer_consume_token (parser
->lexer
);
20477 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20478 /* typename is not allowed in a template template parameter
20479 by the standard until C++1Z. */
20480 pedwarn (token
->location
, OPT_Wpedantic
,
20481 "ISO C++ forbids typename key in template template parameter;"
20482 " use -std=c++1z or -std=gnu++1z");
20485 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20490 /* Parse an (optional) member-specification.
20492 member-specification:
20493 member-declaration member-specification [opt]
20494 access-specifier : member-specification [opt] */
20497 cp_parser_member_specification_opt (cp_parser
* parser
)
20504 /* Peek at the next token. */
20505 token
= cp_lexer_peek_token (parser
->lexer
);
20506 /* If it's a `}', or EOF then we've seen all the members. */
20507 if (token
->type
== CPP_CLOSE_BRACE
20508 || token
->type
== CPP_EOF
20509 || token
->type
== CPP_PRAGMA_EOL
)
20512 /* See if this token is a keyword. */
20513 keyword
= token
->keyword
;
20517 case RID_PROTECTED
:
20519 /* Consume the access-specifier. */
20520 cp_lexer_consume_token (parser
->lexer
);
20521 /* Remember which access-specifier is active. */
20522 current_access_specifier
= token
->u
.value
;
20523 /* Look for the `:'. */
20524 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20528 /* Accept #pragmas at class scope. */
20529 if (token
->type
== CPP_PRAGMA
)
20531 cp_parser_pragma (parser
, pragma_member
);
20535 /* Otherwise, the next construction must be a
20536 member-declaration. */
20537 cp_parser_member_declaration (parser
);
20542 /* Parse a member-declaration.
20544 member-declaration:
20545 decl-specifier-seq [opt] member-declarator-list [opt] ;
20546 function-definition ; [opt]
20547 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20549 template-declaration
20552 member-declarator-list:
20554 member-declarator-list , member-declarator
20557 declarator pure-specifier [opt]
20558 declarator constant-initializer [opt]
20559 identifier [opt] : constant-expression
20563 member-declaration:
20564 __extension__ member-declaration
20567 declarator attributes [opt] pure-specifier [opt]
20568 declarator attributes [opt] constant-initializer [opt]
20569 identifier [opt] attributes [opt] : constant-expression
20573 member-declaration:
20574 static_assert-declaration */
20577 cp_parser_member_declaration (cp_parser
* parser
)
20579 cp_decl_specifier_seq decl_specifiers
;
20580 tree prefix_attributes
;
20582 int declares_class_or_enum
;
20584 cp_token
*token
= NULL
;
20585 cp_token
*decl_spec_token_start
= NULL
;
20586 cp_token
*initializer_token_start
= NULL
;
20587 int saved_pedantic
;
20588 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20590 /* Check for the `__extension__' keyword. */
20591 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20594 cp_parser_member_declaration (parser
);
20595 /* Restore the old value of the PEDANTIC flag. */
20596 pedantic
= saved_pedantic
;
20601 /* Check for a template-declaration. */
20602 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20604 /* An explicit specialization here is an error condition, and we
20605 expect the specialization handler to detect and report this. */
20606 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20607 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20608 cp_parser_explicit_specialization (parser
);
20610 cp_parser_template_declaration (parser
, /*member_p=*/true);
20615 /* Check for a using-declaration. */
20616 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20618 if (cxx_dialect
< cxx11
)
20620 /* Parse the using-declaration. */
20621 cp_parser_using_declaration (parser
,
20622 /*access_declaration_p=*/false);
20628 bool alias_decl_expected
;
20629 cp_parser_parse_tentatively (parser
);
20630 decl
= cp_parser_alias_declaration (parser
);
20631 /* Note that if we actually see the '=' token after the
20632 identifier, cp_parser_alias_declaration commits the
20633 tentative parse. In that case, we really expects an
20634 alias-declaration. Otherwise, we expect a using
20636 alias_decl_expected
=
20637 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20638 cp_parser_parse_definitely (parser
);
20640 if (alias_decl_expected
)
20641 finish_member_declaration (decl
);
20643 cp_parser_using_declaration (parser
,
20644 /*access_declaration_p=*/false);
20649 /* Check for @defs. */
20650 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20653 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20654 ivar
= ivar_chains
;
20658 ivar
= TREE_CHAIN (member
);
20659 TREE_CHAIN (member
) = NULL_TREE
;
20660 finish_member_declaration (member
);
20665 /* If the next token is `static_assert' we have a static assertion. */
20666 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20668 cp_parser_static_assert (parser
, /*member_p=*/true);
20672 parser
->colon_corrects_to_scope_p
= false;
20674 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20677 /* Parse the decl-specifier-seq. */
20678 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20679 cp_parser_decl_specifier_seq (parser
,
20680 CP_PARSER_FLAGS_OPTIONAL
,
20682 &declares_class_or_enum
);
20683 /* Check for an invalid type-name. */
20684 if (!decl_specifiers
.any_type_specifiers_p
20685 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20687 /* If there is no declarator, then the decl-specifier-seq should
20689 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20691 /* If there was no decl-specifier-seq, and the next token is a
20692 `;', then we have something like:
20698 Each member-declaration shall declare at least one member
20699 name of the class. */
20700 if (!decl_specifiers
.any_specifiers_p
)
20702 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20703 if (!in_system_header_at (token
->location
))
20704 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20710 /* See if this declaration is a friend. */
20711 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20712 /* If there were decl-specifiers, check to see if there was
20713 a class-declaration. */
20714 type
= check_tag_decl (&decl_specifiers
,
20715 /*explicit_type_instantiation_p=*/false);
20716 /* Nested classes have already been added to the class, but
20717 a `friend' needs to be explicitly registered. */
20720 /* If the `friend' keyword was present, the friend must
20721 be introduced with a class-key. */
20722 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20723 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20724 "in C++03 a class-key must be used "
20725 "when declaring a friend");
20728 template <typename T> struct A {
20729 friend struct A<T>::B;
20732 A<T>::B will be represented by a TYPENAME_TYPE, and
20733 therefore not recognized by check_tag_decl. */
20736 type
= decl_specifiers
.type
;
20737 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20738 type
= TREE_TYPE (type
);
20740 if (!type
|| !TYPE_P (type
))
20741 error_at (decl_spec_token_start
->location
,
20742 "friend declaration does not name a class or "
20745 make_friend_class (current_class_type
, type
,
20746 /*complain=*/true);
20748 /* If there is no TYPE, an error message will already have
20750 else if (!type
|| type
== error_mark_node
)
20752 /* An anonymous aggregate has to be handled specially; such
20753 a declaration really declares a data member (with a
20754 particular type), as opposed to a nested class. */
20755 else if (ANON_AGGR_TYPE_P (type
))
20758 if (decl_specifiers
.storage_class
!= sc_none
)
20759 error_at (decl_spec_token_start
->location
,
20760 "a storage class on an anonymous aggregate "
20761 "in class scope is not allowed");
20763 /* Remove constructors and such from TYPE, now that we
20764 know it is an anonymous aggregate. */
20765 fixup_anonymous_aggr (type
);
20766 /* And make the corresponding data member. */
20767 decl
= build_decl (decl_spec_token_start
->location
,
20768 FIELD_DECL
, NULL_TREE
, type
);
20769 /* Add it to the class. */
20770 finish_member_declaration (decl
);
20773 cp_parser_check_access_in_redeclaration
20775 decl_spec_token_start
->location
);
20780 bool assume_semicolon
= false;
20782 /* Clear attributes from the decl_specifiers but keep them
20783 around as prefix attributes that apply them to the entity
20785 prefix_attributes
= decl_specifiers
.attributes
;
20786 decl_specifiers
.attributes
= NULL_TREE
;
20788 /* See if these declarations will be friends. */
20789 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20791 /* Keep going until we hit the `;' at the end of the
20793 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20795 tree attributes
= NULL_TREE
;
20796 tree first_attribute
;
20798 /* Peek at the next token. */
20799 token
= cp_lexer_peek_token (parser
->lexer
);
20801 /* Check for a bitfield declaration. */
20802 if (token
->type
== CPP_COLON
20803 || (token
->type
== CPP_NAME
20804 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20810 /* Get the name of the bitfield. Note that we cannot just
20811 check TOKEN here because it may have been invalidated by
20812 the call to cp_lexer_peek_nth_token above. */
20813 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20814 identifier
= cp_parser_identifier (parser
);
20816 identifier
= NULL_TREE
;
20818 /* Consume the `:' token. */
20819 cp_lexer_consume_token (parser
->lexer
);
20820 /* Get the width of the bitfield. */
20822 = cp_parser_constant_expression (parser
);
20824 /* Look for attributes that apply to the bitfield. */
20825 attributes
= cp_parser_attributes_opt (parser
);
20826 /* Remember which attributes are prefix attributes and
20828 first_attribute
= attributes
;
20829 /* Combine the attributes. */
20830 attributes
= chainon (prefix_attributes
, attributes
);
20832 /* Create the bitfield declaration. */
20833 decl
= grokbitfield (identifier
20834 ? make_id_declarator (NULL_TREE
,
20844 cp_declarator
*declarator
;
20846 tree asm_specification
;
20847 int ctor_dtor_or_conv_p
;
20849 /* Parse the declarator. */
20851 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20852 &ctor_dtor_or_conv_p
,
20853 /*parenthesized_p=*/NULL
,
20857 /* If something went wrong parsing the declarator, make sure
20858 that we at least consume some tokens. */
20859 if (declarator
== cp_error_declarator
)
20861 /* Skip to the end of the statement. */
20862 cp_parser_skip_to_end_of_statement (parser
);
20863 /* If the next token is not a semicolon, that is
20864 probably because we just skipped over the body of
20865 a function. So, we consume a semicolon if
20866 present, but do not issue an error message if it
20868 if (cp_lexer_next_token_is (parser
->lexer
,
20870 cp_lexer_consume_token (parser
->lexer
);
20874 if (declares_class_or_enum
& 2)
20875 cp_parser_check_for_definition_in_return_type
20876 (declarator
, decl_specifiers
.type
,
20877 decl_specifiers
.locations
[ds_type_spec
]);
20879 /* Look for an asm-specification. */
20880 asm_specification
= cp_parser_asm_specification_opt (parser
);
20881 /* Look for attributes that apply to the declaration. */
20882 attributes
= cp_parser_attributes_opt (parser
);
20883 /* Remember which attributes are prefix attributes and
20885 first_attribute
= attributes
;
20886 /* Combine the attributes. */
20887 attributes
= chainon (prefix_attributes
, attributes
);
20889 /* If it's an `=', then we have a constant-initializer or a
20890 pure-specifier. It is not correct to parse the
20891 initializer before registering the member declaration
20892 since the member declaration should be in scope while
20893 its initializer is processed. However, the rest of the
20894 front end does not yet provide an interface that allows
20895 us to handle this correctly. */
20896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20900 A pure-specifier shall be used only in the declaration of
20901 a virtual function.
20903 A member-declarator can contain a constant-initializer
20904 only if it declares a static member of integral or
20907 Therefore, if the DECLARATOR is for a function, we look
20908 for a pure-specifier; otherwise, we look for a
20909 constant-initializer. When we call `grokfield', it will
20910 perform more stringent semantics checks. */
20911 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
20912 if (function_declarator_p (declarator
)
20913 || (decl_specifiers
.type
20914 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
20915 && declarator
->kind
== cdk_id
20916 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
20917 == FUNCTION_TYPE
)))
20918 initializer
= cp_parser_pure_specifier (parser
);
20919 else if (decl_specifiers
.storage_class
!= sc_static
)
20920 initializer
= cp_parser_save_nsdmi (parser
);
20921 else if (cxx_dialect
>= cxx11
)
20924 /* Don't require a constant rvalue in C++11, since we
20925 might want a reference constant. We'll enforce
20926 constancy later. */
20927 cp_lexer_consume_token (parser
->lexer
);
20928 /* Parse the initializer. */
20929 initializer
= cp_parser_initializer_clause (parser
,
20933 /* Parse the initializer. */
20934 initializer
= cp_parser_constant_initializer (parser
);
20936 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20937 && !function_declarator_p (declarator
))
20940 if (decl_specifiers
.storage_class
!= sc_static
)
20941 initializer
= cp_parser_save_nsdmi (parser
);
20943 initializer
= cp_parser_initializer (parser
, &x
, &x
);
20945 /* Otherwise, there is no initializer. */
20947 initializer
= NULL_TREE
;
20949 /* See if we are probably looking at a function
20950 definition. We are certainly not looking at a
20951 member-declarator. Calling `grokfield' has
20952 side-effects, so we must not do it unless we are sure
20953 that we are looking at a member-declarator. */
20954 if (cp_parser_token_starts_function_definition_p
20955 (cp_lexer_peek_token (parser
->lexer
)))
20957 /* The grammar does not allow a pure-specifier to be
20958 used when a member function is defined. (It is
20959 possible that this fact is an oversight in the
20960 standard, since a pure function may be defined
20961 outside of the class-specifier. */
20962 if (initializer
&& initializer_token_start
)
20963 error_at (initializer_token_start
->location
,
20964 "pure-specifier on function-definition");
20965 decl
= cp_parser_save_member_function_body (parser
,
20969 if (parser
->fully_implicit_function_template_p
)
20970 decl
= finish_fully_implicit_template (parser
, decl
);
20971 /* If the member was not a friend, declare it here. */
20973 finish_member_declaration (decl
);
20974 /* Peek at the next token. */
20975 token
= cp_lexer_peek_token (parser
->lexer
);
20976 /* If the next token is a semicolon, consume it. */
20977 if (token
->type
== CPP_SEMICOLON
)
20978 cp_lexer_consume_token (parser
->lexer
);
20982 if (declarator
->kind
== cdk_function
)
20983 declarator
->id_loc
= token
->location
;
20984 /* Create the declaration. */
20985 decl
= grokfield (declarator
, &decl_specifiers
,
20986 initializer
, /*init_const_expr_p=*/true,
20987 asm_specification
, attributes
);
20988 if (parser
->fully_implicit_function_template_p
)
20991 finish_fully_implicit_template (parser
, 0);
20993 decl
= finish_fully_implicit_template (parser
, decl
);
20997 cp_finalize_omp_declare_simd (parser
, decl
);
20999 /* Reset PREFIX_ATTRIBUTES. */
21000 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21001 attributes
= TREE_CHAIN (attributes
);
21003 TREE_CHAIN (attributes
) = NULL_TREE
;
21005 /* If there is any qualification still in effect, clear it
21006 now; we will be starting fresh with the next declarator. */
21007 parser
->scope
= NULL_TREE
;
21008 parser
->qualifying_scope
= NULL_TREE
;
21009 parser
->object_scope
= NULL_TREE
;
21010 /* If it's a `,', then there are more declarators. */
21011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21013 cp_lexer_consume_token (parser
->lexer
);
21014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21016 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21017 error_at (token
->location
,
21018 "stray %<,%> at end of member declaration");
21021 /* If the next token isn't a `;', then we have a parse error. */
21022 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21025 /* The next token might be a ways away from where the
21026 actual semicolon is missing. Find the previous token
21027 and use that for our error position. */
21028 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21029 error_at (token
->location
,
21030 "expected %<;%> at end of member declaration");
21032 /* Assume that the user meant to provide a semicolon. If
21033 we were to cp_parser_skip_to_end_of_statement, we might
21034 skip to a semicolon inside a member function definition
21035 and issue nonsensical error messages. */
21036 assume_semicolon
= true;
21041 /* Add DECL to the list of members. */
21043 finish_member_declaration (decl
);
21045 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21046 cp_parser_save_default_args (parser
, decl
);
21047 else if (TREE_CODE (decl
) == FIELD_DECL
21048 && !DECL_C_BIT_FIELD (decl
)
21049 && DECL_INITIAL (decl
))
21050 /* Add DECL to the queue of NSDMI to be parsed later. */
21051 vec_safe_push (unparsed_nsdmis
, decl
);
21054 if (assume_semicolon
)
21059 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21061 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21064 /* Parse a pure-specifier.
21069 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21070 Otherwise, ERROR_MARK_NODE is returned. */
21073 cp_parser_pure_specifier (cp_parser
* parser
)
21077 /* Look for the `=' token. */
21078 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21079 return error_mark_node
;
21080 /* Look for the `0' token. */
21081 token
= cp_lexer_peek_token (parser
->lexer
);
21083 if (token
->type
== CPP_EOF
21084 || token
->type
== CPP_PRAGMA_EOL
)
21085 return error_mark_node
;
21087 cp_lexer_consume_token (parser
->lexer
);
21089 /* Accept = default or = delete in c++0x mode. */
21090 if (token
->keyword
== RID_DEFAULT
21091 || token
->keyword
== RID_DELETE
)
21093 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21094 return token
->u
.value
;
21097 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21098 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21100 cp_parser_error (parser
,
21101 "invalid pure specifier (only %<= 0%> is allowed)");
21102 cp_parser_skip_to_end_of_statement (parser
);
21103 return error_mark_node
;
21105 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21107 error_at (token
->location
, "templates may not be %<virtual%>");
21108 return error_mark_node
;
21111 return integer_zero_node
;
21114 /* Parse a constant-initializer.
21116 constant-initializer:
21117 = constant-expression
21119 Returns a representation of the constant-expression. */
21122 cp_parser_constant_initializer (cp_parser
* parser
)
21124 /* Look for the `=' token. */
21125 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21126 return error_mark_node
;
21128 /* It is invalid to write:
21130 struct S { static const int i = { 7 }; };
21133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21135 cp_parser_error (parser
,
21136 "a brace-enclosed initializer is not allowed here");
21137 /* Consume the opening brace. */
21138 cp_lexer_consume_token (parser
->lexer
);
21139 /* Skip the initializer. */
21140 cp_parser_skip_to_closing_brace (parser
);
21141 /* Look for the trailing `}'. */
21142 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21144 return error_mark_node
;
21147 return cp_parser_constant_expression (parser
);
21150 /* Derived classes [gram.class.derived] */
21152 /* Parse a base-clause.
21155 : base-specifier-list
21157 base-specifier-list:
21158 base-specifier ... [opt]
21159 base-specifier-list , base-specifier ... [opt]
21161 Returns a TREE_LIST representing the base-classes, in the order in
21162 which they were declared. The representation of each node is as
21163 described by cp_parser_base_specifier.
21165 In the case that no bases are specified, this function will return
21166 NULL_TREE, not ERROR_MARK_NODE. */
21169 cp_parser_base_clause (cp_parser
* parser
)
21171 tree bases
= NULL_TREE
;
21173 /* Look for the `:' that begins the list. */
21174 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21176 /* Scan the base-specifier-list. */
21181 bool pack_expansion_p
= false;
21183 /* Look for the base-specifier. */
21184 base
= cp_parser_base_specifier (parser
);
21185 /* Look for the (optional) ellipsis. */
21186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21188 /* Consume the `...'. */
21189 cp_lexer_consume_token (parser
->lexer
);
21191 pack_expansion_p
= true;
21194 /* Add BASE to the front of the list. */
21195 if (base
&& base
!= error_mark_node
)
21197 if (pack_expansion_p
)
21198 /* Make this a pack expansion type. */
21199 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21201 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21203 TREE_CHAIN (base
) = bases
;
21207 /* Peek at the next token. */
21208 token
= cp_lexer_peek_token (parser
->lexer
);
21209 /* If it's not a comma, then the list is complete. */
21210 if (token
->type
!= CPP_COMMA
)
21212 /* Consume the `,'. */
21213 cp_lexer_consume_token (parser
->lexer
);
21216 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21217 base class had a qualified name. However, the next name that
21218 appears is certainly not qualified. */
21219 parser
->scope
= NULL_TREE
;
21220 parser
->qualifying_scope
= NULL_TREE
;
21221 parser
->object_scope
= NULL_TREE
;
21223 return nreverse (bases
);
21226 /* Parse a base-specifier.
21229 :: [opt] nested-name-specifier [opt] class-name
21230 virtual access-specifier [opt] :: [opt] nested-name-specifier
21232 access-specifier virtual [opt] :: [opt] nested-name-specifier
21235 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21236 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21237 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21238 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21241 cp_parser_base_specifier (cp_parser
* parser
)
21245 bool virtual_p
= false;
21246 bool duplicate_virtual_error_issued_p
= false;
21247 bool duplicate_access_error_issued_p
= false;
21248 bool class_scope_p
, template_p
;
21249 tree access
= access_default_node
;
21252 /* Process the optional `virtual' and `access-specifier'. */
21255 /* Peek at the next token. */
21256 token
= cp_lexer_peek_token (parser
->lexer
);
21257 /* Process `virtual'. */
21258 switch (token
->keyword
)
21261 /* If `virtual' appears more than once, issue an error. */
21262 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21264 cp_parser_error (parser
,
21265 "%<virtual%> specified more than once in base-specified");
21266 duplicate_virtual_error_issued_p
= true;
21271 /* Consume the `virtual' token. */
21272 cp_lexer_consume_token (parser
->lexer
);
21277 case RID_PROTECTED
:
21279 /* If more than one access specifier appears, issue an
21281 if (access
!= access_default_node
21282 && !duplicate_access_error_issued_p
)
21284 cp_parser_error (parser
,
21285 "more than one access specifier in base-specified");
21286 duplicate_access_error_issued_p
= true;
21289 access
= ridpointers
[(int) token
->keyword
];
21291 /* Consume the access-specifier. */
21292 cp_lexer_consume_token (parser
->lexer
);
21301 /* It is not uncommon to see programs mechanically, erroneously, use
21302 the 'typename' keyword to denote (dependent) qualified types
21303 as base classes. */
21304 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21306 token
= cp_lexer_peek_token (parser
->lexer
);
21307 if (!processing_template_decl
)
21308 error_at (token
->location
,
21309 "keyword %<typename%> not allowed outside of templates");
21311 error_at (token
->location
,
21312 "keyword %<typename%> not allowed in this context "
21313 "(the base class is implicitly a type)");
21314 cp_lexer_consume_token (parser
->lexer
);
21317 /* Look for the optional `::' operator. */
21318 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21319 /* Look for the nested-name-specifier. The simplest way to
21324 The keyword `typename' is not permitted in a base-specifier or
21325 mem-initializer; in these contexts a qualified name that
21326 depends on a template-parameter is implicitly assumed to be a
21329 is to pretend that we have seen the `typename' keyword at this
21331 cp_parser_nested_name_specifier_opt (parser
,
21332 /*typename_keyword_p=*/true,
21333 /*check_dependency_p=*/true,
21335 /*is_declaration=*/true);
21336 /* If the base class is given by a qualified name, assume that names
21337 we see are type names or templates, as appropriate. */
21338 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21339 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21342 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21343 /* DR 950 allows decltype as a base-specifier. */
21344 type
= cp_parser_decltype (parser
);
21347 /* Otherwise, look for the class-name. */
21348 type
= cp_parser_class_name (parser
,
21352 /*check_dependency_p=*/true,
21353 /*class_head_p=*/false,
21354 /*is_declaration=*/true);
21355 type
= TREE_TYPE (type
);
21358 if (type
== error_mark_node
)
21359 return error_mark_node
;
21361 return finish_base_specifier (type
, access
, virtual_p
);
21364 /* Exception handling [gram.exception] */
21366 /* Parse an (optional) noexcept-specification.
21368 noexcept-specification:
21369 noexcept ( constant-expression ) [opt]
21371 If no noexcept-specification is present, returns NULL_TREE.
21372 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21373 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21374 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21375 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21376 in which case a boolean condition is returned instead. */
21379 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21380 bool require_constexpr
,
21381 bool* consumed_expr
,
21385 const char *saved_message
;
21387 /* Peek at the next token. */
21388 token
= cp_lexer_peek_token (parser
->lexer
);
21390 /* Is it a noexcept-specification? */
21391 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21394 cp_lexer_consume_token (parser
->lexer
);
21396 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21398 cp_lexer_consume_token (parser
->lexer
);
21400 if (require_constexpr
)
21402 /* Types may not be defined in an exception-specification. */
21403 saved_message
= parser
->type_definition_forbidden_message
;
21404 parser
->type_definition_forbidden_message
21405 = G_("types may not be defined in an exception-specification");
21407 expr
= cp_parser_constant_expression (parser
);
21409 /* Restore the saved message. */
21410 parser
->type_definition_forbidden_message
= saved_message
;
21414 expr
= cp_parser_expression (parser
);
21415 *consumed_expr
= true;
21418 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21422 expr
= boolean_true_node
;
21423 if (!require_constexpr
)
21424 *consumed_expr
= false;
21427 /* We cannot build a noexcept-spec right away because this will check
21428 that expr is a constexpr. */
21430 return build_noexcept_spec (expr
, tf_warning_or_error
);
21438 /* Parse an (optional) exception-specification.
21440 exception-specification:
21441 throw ( type-id-list [opt] )
21443 Returns a TREE_LIST representing the exception-specification. The
21444 TREE_VALUE of each node is a type. */
21447 cp_parser_exception_specification_opt (cp_parser
* parser
)
21451 const char *saved_message
;
21453 /* Peek at the next token. */
21454 token
= cp_lexer_peek_token (parser
->lexer
);
21456 /* Is it a noexcept-specification? */
21457 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21459 if (type_id_list
!= NULL_TREE
)
21460 return type_id_list
;
21462 /* If it's not `throw', then there's no exception-specification. */
21463 if (!cp_parser_is_keyword (token
, RID_THROW
))
21467 /* Enable this once a lot of code has transitioned to noexcept? */
21468 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21469 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21470 "deprecated in C++0x; use %<noexcept%> instead");
21473 /* Consume the `throw'. */
21474 cp_lexer_consume_token (parser
->lexer
);
21476 /* Look for the `('. */
21477 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21479 /* Peek at the next token. */
21480 token
= cp_lexer_peek_token (parser
->lexer
);
21481 /* If it's not a `)', then there is a type-id-list. */
21482 if (token
->type
!= CPP_CLOSE_PAREN
)
21484 /* Types may not be defined in an exception-specification. */
21485 saved_message
= parser
->type_definition_forbidden_message
;
21486 parser
->type_definition_forbidden_message
21487 = G_("types may not be defined in an exception-specification");
21488 /* Parse the type-id-list. */
21489 type_id_list
= cp_parser_type_id_list (parser
);
21490 /* Restore the saved message. */
21491 parser
->type_definition_forbidden_message
= saved_message
;
21494 type_id_list
= empty_except_spec
;
21496 /* Look for the `)'. */
21497 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21499 return type_id_list
;
21502 /* Parse an (optional) type-id-list.
21506 type-id-list , type-id ... [opt]
21508 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21509 in the order that the types were presented. */
21512 cp_parser_type_id_list (cp_parser
* parser
)
21514 tree types
= NULL_TREE
;
21521 /* Get the next type-id. */
21522 type
= cp_parser_type_id (parser
);
21523 /* Parse the optional ellipsis. */
21524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21526 /* Consume the `...'. */
21527 cp_lexer_consume_token (parser
->lexer
);
21529 /* Turn the type into a pack expansion expression. */
21530 type
= make_pack_expansion (type
);
21532 /* Add it to the list. */
21533 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21534 /* Peek at the next token. */
21535 token
= cp_lexer_peek_token (parser
->lexer
);
21536 /* If it is not a `,', we are done. */
21537 if (token
->type
!= CPP_COMMA
)
21539 /* Consume the `,'. */
21540 cp_lexer_consume_token (parser
->lexer
);
21543 return nreverse (types
);
21546 /* Parse a try-block.
21549 try compound-statement handler-seq */
21552 cp_parser_try_block (cp_parser
* parser
)
21556 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21557 if (parser
->in_function_body
21558 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21559 error ("%<try%> in %<constexpr%> function");
21561 try_block
= begin_try_block ();
21562 cp_parser_compound_statement (parser
, NULL
, true, false);
21563 finish_try_block (try_block
);
21564 cp_parser_handler_seq (parser
);
21565 finish_handler_sequence (try_block
);
21570 /* Parse a function-try-block.
21572 function-try-block:
21573 try ctor-initializer [opt] function-body handler-seq */
21576 cp_parser_function_try_block (cp_parser
* parser
)
21578 tree compound_stmt
;
21580 bool ctor_initializer_p
;
21582 /* Look for the `try' keyword. */
21583 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21585 /* Let the rest of the front end know where we are. */
21586 try_block
= begin_function_try_block (&compound_stmt
);
21587 /* Parse the function-body. */
21588 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21589 (parser
, /*in_function_try_block=*/true);
21590 /* We're done with the `try' part. */
21591 finish_function_try_block (try_block
);
21592 /* Parse the handlers. */
21593 cp_parser_handler_seq (parser
);
21594 /* We're done with the handlers. */
21595 finish_function_handler_sequence (try_block
, compound_stmt
);
21597 return ctor_initializer_p
;
21600 /* Parse a handler-seq.
21603 handler handler-seq [opt] */
21606 cp_parser_handler_seq (cp_parser
* parser
)
21612 /* Parse the handler. */
21613 cp_parser_handler (parser
);
21614 /* Peek at the next token. */
21615 token
= cp_lexer_peek_token (parser
->lexer
);
21616 /* If it's not `catch' then there are no more handlers. */
21617 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21622 /* Parse a handler.
21625 catch ( exception-declaration ) compound-statement */
21628 cp_parser_handler (cp_parser
* parser
)
21633 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21634 handler
= begin_handler ();
21635 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21636 declaration
= cp_parser_exception_declaration (parser
);
21637 finish_handler_parms (declaration
, handler
);
21638 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21639 cp_parser_compound_statement (parser
, NULL
, false, false);
21640 finish_handler (handler
);
21643 /* Parse an exception-declaration.
21645 exception-declaration:
21646 type-specifier-seq declarator
21647 type-specifier-seq abstract-declarator
21651 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21652 ellipsis variant is used. */
21655 cp_parser_exception_declaration (cp_parser
* parser
)
21657 cp_decl_specifier_seq type_specifiers
;
21658 cp_declarator
*declarator
;
21659 const char *saved_message
;
21661 /* If it's an ellipsis, it's easy to handle. */
21662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21664 /* Consume the `...' token. */
21665 cp_lexer_consume_token (parser
->lexer
);
21669 /* Types may not be defined in exception-declarations. */
21670 saved_message
= parser
->type_definition_forbidden_message
;
21671 parser
->type_definition_forbidden_message
21672 = G_("types may not be defined in exception-declarations");
21674 /* Parse the type-specifier-seq. */
21675 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21676 /*is_trailing_return=*/false,
21678 /* If it's a `)', then there is no declarator. */
21679 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21682 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21683 /*ctor_dtor_or_conv_p=*/NULL
,
21684 /*parenthesized_p=*/NULL
,
21685 /*member_p=*/false,
21686 /*friend_p=*/false);
21688 /* Restore the saved message. */
21689 parser
->type_definition_forbidden_message
= saved_message
;
21691 if (!type_specifiers
.any_specifiers_p
)
21692 return error_mark_node
;
21694 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21697 /* Parse a throw-expression.
21700 throw assignment-expression [opt]
21702 Returns a THROW_EXPR representing the throw-expression. */
21705 cp_parser_throw_expression (cp_parser
* parser
)
21710 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21711 token
= cp_lexer_peek_token (parser
->lexer
);
21712 /* Figure out whether or not there is an assignment-expression
21713 following the "throw" keyword. */
21714 if (token
->type
== CPP_COMMA
21715 || token
->type
== CPP_SEMICOLON
21716 || token
->type
== CPP_CLOSE_PAREN
21717 || token
->type
== CPP_CLOSE_SQUARE
21718 || token
->type
== CPP_CLOSE_BRACE
21719 || token
->type
== CPP_COLON
)
21720 expression
= NULL_TREE
;
21722 expression
= cp_parser_assignment_expression (parser
);
21724 return build_throw (expression
);
21727 /* GNU Extensions */
21729 /* Parse an (optional) asm-specification.
21732 asm ( string-literal )
21734 If the asm-specification is present, returns a STRING_CST
21735 corresponding to the string-literal. Otherwise, returns
21739 cp_parser_asm_specification_opt (cp_parser
* parser
)
21742 tree asm_specification
;
21744 /* Peek at the next token. */
21745 token
= cp_lexer_peek_token (parser
->lexer
);
21746 /* If the next token isn't the `asm' keyword, then there's no
21747 asm-specification. */
21748 if (!cp_parser_is_keyword (token
, RID_ASM
))
21751 /* Consume the `asm' token. */
21752 cp_lexer_consume_token (parser
->lexer
);
21753 /* Look for the `('. */
21754 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21756 /* Look for the string-literal. */
21757 asm_specification
= cp_parser_string_literal (parser
, false, false);
21759 /* Look for the `)'. */
21760 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21762 return asm_specification
;
21765 /* Parse an asm-operand-list.
21769 asm-operand-list , asm-operand
21772 string-literal ( expression )
21773 [ string-literal ] string-literal ( expression )
21775 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21776 each node is the expression. The TREE_PURPOSE is itself a
21777 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21778 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21779 is a STRING_CST for the string literal before the parenthesis. Returns
21780 ERROR_MARK_NODE if any of the operands are invalid. */
21783 cp_parser_asm_operand_list (cp_parser
* parser
)
21785 tree asm_operands
= NULL_TREE
;
21786 bool invalid_operands
= false;
21790 tree string_literal
;
21794 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21796 /* Consume the `[' token. */
21797 cp_lexer_consume_token (parser
->lexer
);
21798 /* Read the operand name. */
21799 name
= cp_parser_identifier (parser
);
21800 if (name
!= error_mark_node
)
21801 name
= build_string (IDENTIFIER_LENGTH (name
),
21802 IDENTIFIER_POINTER (name
));
21803 /* Look for the closing `]'. */
21804 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21808 /* Look for the string-literal. */
21809 string_literal
= cp_parser_string_literal (parser
, false, false);
21811 /* Look for the `('. */
21812 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21813 /* Parse the expression. */
21814 expression
= cp_parser_expression (parser
);
21815 /* Look for the `)'. */
21816 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21818 if (name
== error_mark_node
21819 || string_literal
== error_mark_node
21820 || expression
== error_mark_node
)
21821 invalid_operands
= true;
21823 /* Add this operand to the list. */
21824 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21827 /* If the next token is not a `,', there are no more
21829 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21831 /* Consume the `,'. */
21832 cp_lexer_consume_token (parser
->lexer
);
21835 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21838 /* Parse an asm-clobber-list.
21842 asm-clobber-list , string-literal
21844 Returns a TREE_LIST, indicating the clobbers in the order that they
21845 appeared. The TREE_VALUE of each node is a STRING_CST. */
21848 cp_parser_asm_clobber_list (cp_parser
* parser
)
21850 tree clobbers
= NULL_TREE
;
21854 tree string_literal
;
21856 /* Look for the string literal. */
21857 string_literal
= cp_parser_string_literal (parser
, false, false);
21858 /* Add it to the list. */
21859 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21860 /* If the next token is not a `,', then the list is
21862 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21864 /* Consume the `,' token. */
21865 cp_lexer_consume_token (parser
->lexer
);
21871 /* Parse an asm-label-list.
21875 asm-label-list , identifier
21877 Returns a TREE_LIST, indicating the labels in the order that they
21878 appeared. The TREE_VALUE of each node is a label. */
21881 cp_parser_asm_label_list (cp_parser
* parser
)
21883 tree labels
= NULL_TREE
;
21887 tree identifier
, label
, name
;
21889 /* Look for the identifier. */
21890 identifier
= cp_parser_identifier (parser
);
21891 if (!error_operand_p (identifier
))
21893 label
= lookup_label (identifier
);
21894 if (TREE_CODE (label
) == LABEL_DECL
)
21896 TREE_USED (label
) = 1;
21897 check_goto (label
);
21898 name
= build_string (IDENTIFIER_LENGTH (identifier
),
21899 IDENTIFIER_POINTER (identifier
));
21900 labels
= tree_cons (name
, label
, labels
);
21903 /* If the next token is not a `,', then the list is
21905 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21907 /* Consume the `,' token. */
21908 cp_lexer_consume_token (parser
->lexer
);
21911 return nreverse (labels
);
21914 /* Return TRUE iff the next tokens in the stream are possibly the
21915 beginning of a GNU extension attribute. */
21918 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
21920 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
21923 /* Return TRUE iff the next tokens in the stream are possibly the
21924 beginning of a standard C++-11 attribute specifier. */
21927 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
21929 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
21932 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21933 beginning of a standard C++-11 attribute specifier. */
21936 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
21938 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21940 return (cxx_dialect
>= cxx11
21941 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
21942 || (token
->type
== CPP_OPEN_SQUARE
21943 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
21944 && token
->type
== CPP_OPEN_SQUARE
)));
21947 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21948 beginning of a GNU extension attribute. */
21951 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
21953 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
21955 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
21958 /* Return true iff the next tokens can be the beginning of either a
21959 GNU attribute list, or a standard C++11 attribute sequence. */
21962 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
21964 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
21965 || cp_next_tokens_can_be_std_attribute_p (parser
));
21968 /* Return true iff the next Nth tokens can be the beginning of either
21969 a GNU attribute list, or a standard C++11 attribute sequence. */
21972 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
21974 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
21975 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
21978 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21979 of GNU attributes, or return NULL. */
21982 cp_parser_attributes_opt (cp_parser
*parser
)
21984 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
21985 return cp_parser_gnu_attributes_opt (parser
);
21986 return cp_parser_std_attribute_spec_seq (parser
);
21989 #define CILK_SIMD_FN_CLAUSE_MASK \
21990 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21991 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21992 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21993 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21994 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21996 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21997 vector [(<clauses>)] */
22000 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22002 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22003 cp_token
*token
= v_token
;
22006 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22007 parser
->cilk_simd_fn_info
->error_seen
= false;
22008 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22009 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22011 int paren_scope
= 0;
22012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22014 cp_lexer_consume_token (parser
->lexer
);
22015 v_token
= cp_lexer_peek_token (parser
->lexer
);
22018 while (paren_scope
> 0)
22020 token
= cp_lexer_peek_token (parser
->lexer
);
22021 if (token
->type
== CPP_OPEN_PAREN
)
22023 else if (token
->type
== CPP_CLOSE_PAREN
)
22025 /* Do not push the last ')' */
22026 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22027 cp_lexer_consume_token (parser
->lexer
);
22030 token
->type
= CPP_PRAGMA_EOL
;
22031 parser
->lexer
->next_token
= token
;
22032 cp_lexer_consume_token (parser
->lexer
);
22034 struct cp_token_cache
*cp
22035 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22036 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22039 /* Parse an (optional) series of attributes.
22042 attributes attribute
22045 __attribute__ (( attribute-list [opt] ))
22047 The return value is as for cp_parser_gnu_attribute_list. */
22050 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22052 tree attributes
= NULL_TREE
;
22057 tree attribute_list
;
22060 /* Peek at the next token. */
22061 token
= cp_lexer_peek_token (parser
->lexer
);
22062 /* If it's not `__attribute__', then we're done. */
22063 if (token
->keyword
!= RID_ATTRIBUTE
)
22066 /* Consume the `__attribute__' keyword. */
22067 cp_lexer_consume_token (parser
->lexer
);
22068 /* Look for the two `(' tokens. */
22069 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22070 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22072 /* Peek at the next token. */
22073 token
= cp_lexer_peek_token (parser
->lexer
);
22074 if (token
->type
!= CPP_CLOSE_PAREN
)
22075 /* Parse the attribute-list. */
22076 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22078 /* If the next token is a `)', then there is no attribute
22080 attribute_list
= NULL
;
22082 /* Look for the two `)' tokens. */
22083 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22085 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22088 cp_parser_skip_to_end_of_statement (parser
);
22090 /* Add these new attributes to the list. */
22091 attributes
= chainon (attributes
, attribute_list
);
22097 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22098 "__vector" or "__vector__." */
22101 is_cilkplus_vector_p (tree name
)
22103 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22108 /* Parse a GNU attribute-list.
22112 attribute-list , attribute
22116 identifier ( identifier )
22117 identifier ( identifier , expression-list )
22118 identifier ( expression-list )
22120 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22121 to an attribute. The TREE_PURPOSE of each node is the identifier
22122 indicating which attribute is in use. The TREE_VALUE represents
22123 the arguments, if any. */
22126 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22128 tree attribute_list
= NULL_TREE
;
22129 bool save_translate_strings_p
= parser
->translate_strings_p
;
22131 parser
->translate_strings_p
= false;
22138 /* Look for the identifier. We also allow keywords here; for
22139 example `__attribute__ ((const))' is legal. */
22140 token
= cp_lexer_peek_token (parser
->lexer
);
22141 if (token
->type
== CPP_NAME
22142 || token
->type
== CPP_KEYWORD
)
22144 tree arguments
= NULL_TREE
;
22146 /* Consume the token, but save it since we need it for the
22147 SIMD enabled function parsing. */
22148 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22150 /* Save away the identifier that indicates which attribute
22152 identifier
= (token
->type
== CPP_KEYWORD
)
22153 /* For keywords, use the canonical spelling, not the
22154 parsed identifier. */
22155 ? ridpointers
[(int) token
->keyword
]
22156 : id_token
->u
.value
;
22158 attribute
= build_tree_list (identifier
, NULL_TREE
);
22160 /* Peek at the next token. */
22161 token
= cp_lexer_peek_token (parser
->lexer
);
22162 /* If it's an `(', then parse the attribute arguments. */
22163 if (token
->type
== CPP_OPEN_PAREN
)
22165 vec
<tree
, va_gc
> *vec
;
22166 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22167 ? id_attr
: normal_attr
);
22168 if (is_cilkplus_vector_p (identifier
))
22170 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22174 vec
= cp_parser_parenthesized_expression_list
22175 (parser
, attr_flag
, /*cast_p=*/false,
22176 /*allow_expansion_p=*/false,
22177 /*non_constant_p=*/NULL
);
22179 arguments
= error_mark_node
;
22182 arguments
= build_tree_list_vec (vec
);
22183 release_tree_vector (vec
);
22185 /* Save the arguments away. */
22186 TREE_VALUE (attribute
) = arguments
;
22188 else if (is_cilkplus_vector_p (identifier
))
22190 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22194 if (arguments
!= error_mark_node
)
22196 /* Add this attribute to the list. */
22197 TREE_CHAIN (attribute
) = attribute_list
;
22198 attribute_list
= attribute
;
22201 token
= cp_lexer_peek_token (parser
->lexer
);
22203 /* Now, look for more attributes. If the next token isn't a
22204 `,', we're done. */
22205 if (token
->type
!= CPP_COMMA
)
22208 /* Consume the comma and keep going. */
22209 cp_lexer_consume_token (parser
->lexer
);
22211 parser
->translate_strings_p
= save_translate_strings_p
;
22213 /* We built up the list in reverse order. */
22214 return nreverse (attribute_list
);
22217 /* Parse a standard C++11 attribute.
22219 The returned representation is a TREE_LIST which TREE_PURPOSE is
22220 the scoped name of the attribute, and the TREE_VALUE is its
22223 Note that the scoped name of the attribute is itself a TREE_LIST
22224 which TREE_PURPOSE is the namespace of the attribute, and
22225 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22226 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22227 and which TREE_PURPOSE is directly the attribute name.
22229 Clients of the attribute code should use get_attribute_namespace
22230 and get_attribute_name to get the actual namespace and name of
22231 attributes, regardless of their being GNU or C++11 attributes.
22234 attribute-token attribute-argument-clause [opt]
22238 attribute-scoped-token
22240 attribute-scoped-token:
22241 attribute-namespace :: identifier
22243 attribute-namespace:
22246 attribute-argument-clause:
22247 ( balanced-token-seq )
22249 balanced-token-seq:
22250 balanced-token [opt]
22251 balanced-token-seq balanced-token
22254 ( balanced-token-seq )
22255 [ balanced-token-seq ]
22256 { balanced-token-seq }. */
22259 cp_parser_std_attribute (cp_parser
*parser
)
22261 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22264 /* First, parse name of the the attribute, a.k.a
22265 attribute-token. */
22267 token
= cp_lexer_peek_token (parser
->lexer
);
22268 if (token
->type
== CPP_NAME
)
22269 attr_id
= token
->u
.value
;
22270 else if (token
->type
== CPP_KEYWORD
)
22271 attr_id
= ridpointers
[(int) token
->keyword
];
22272 else if (token
->flags
& NAMED_OP
)
22273 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22275 if (attr_id
== NULL_TREE
)
22278 cp_lexer_consume_token (parser
->lexer
);
22280 token
= cp_lexer_peek_token (parser
->lexer
);
22281 if (token
->type
== CPP_SCOPE
)
22283 /* We are seeing a scoped attribute token. */
22285 cp_lexer_consume_token (parser
->lexer
);
22288 token
= cp_lexer_consume_token (parser
->lexer
);
22289 if (token
->type
== CPP_NAME
)
22290 attr_id
= token
->u
.value
;
22291 else if (token
->type
== CPP_KEYWORD
)
22292 attr_id
= ridpointers
[(int) token
->keyword
];
22295 error_at (token
->location
,
22296 "expected an identifier for the attribute name");
22297 return error_mark_node
;
22299 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22301 token
= cp_lexer_peek_token (parser
->lexer
);
22305 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22307 /* C++11 noreturn attribute is equivalent to GNU's. */
22308 if (is_attribute_p ("noreturn", attr_id
))
22309 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22310 /* C++14 deprecated attribute is equivalent to GNU's. */
22311 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22313 if (cxx_dialect
== cxx11
)
22314 pedwarn (token
->location
, OPT_Wpedantic
,
22315 "%<deprecated%> is a C++14 feature;"
22316 " use %<gnu::deprecated%>");
22317 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22321 /* Now parse the optional argument clause of the attribute. */
22323 if (token
->type
!= CPP_OPEN_PAREN
)
22327 vec
<tree
, va_gc
> *vec
;
22328 int attr_flag
= normal_attr
;
22330 if (attr_ns
== get_identifier ("gnu")
22331 && attribute_takes_identifier_p (attr_id
))
22332 /* A GNU attribute that takes an identifier in parameter. */
22333 attr_flag
= id_attr
;
22335 vec
= cp_parser_parenthesized_expression_list
22336 (parser
, attr_flag
, /*cast_p=*/false,
22337 /*allow_expansion_p=*/true,
22338 /*non_constant_p=*/NULL
);
22340 arguments
= error_mark_node
;
22343 arguments
= build_tree_list_vec (vec
);
22344 release_tree_vector (vec
);
22347 if (arguments
== error_mark_node
)
22348 attribute
= error_mark_node
;
22350 TREE_VALUE (attribute
) = arguments
;
22356 /* Parse a list of standard C++-11 attributes.
22360 attribute-list , attribute[opt]
22362 attribute-list , attribute ...
22366 cp_parser_std_attribute_list (cp_parser
*parser
)
22368 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22369 cp_token
*token
= NULL
;
22373 attribute
= cp_parser_std_attribute (parser
);
22374 if (attribute
== error_mark_node
)
22376 if (attribute
!= NULL_TREE
)
22378 TREE_CHAIN (attribute
) = attributes
;
22379 attributes
= attribute
;
22381 token
= cp_lexer_peek_token (parser
->lexer
);
22382 if (token
->type
!= CPP_COMMA
)
22384 cp_lexer_consume_token (parser
->lexer
);
22386 attributes
= nreverse (attributes
);
22390 /* Parse a standard C++-11 attribute specifier.
22392 attribute-specifier:
22393 [ [ attribute-list ] ]
22394 alignment-specifier
22396 alignment-specifier:
22397 alignas ( type-id ... [opt] )
22398 alignas ( alignment-expression ... [opt] ). */
22401 cp_parser_std_attribute_spec (cp_parser
*parser
)
22403 tree attributes
= NULL_TREE
;
22404 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22406 if (token
->type
== CPP_OPEN_SQUARE
22407 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22409 cp_lexer_consume_token (parser
->lexer
);
22410 cp_lexer_consume_token (parser
->lexer
);
22412 attributes
= cp_parser_std_attribute_list (parser
);
22414 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22415 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22416 cp_parser_skip_to_end_of_statement (parser
);
22418 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22419 when we are sure that we have actually parsed them. */
22420 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22426 /* Look for an alignment-specifier. */
22428 token
= cp_lexer_peek_token (parser
->lexer
);
22430 if (token
->type
!= CPP_KEYWORD
22431 || token
->keyword
!= RID_ALIGNAS
)
22434 cp_lexer_consume_token (parser
->lexer
);
22435 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22437 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22439 cp_parser_error (parser
, "expected %<(%>");
22440 return error_mark_node
;
22443 cp_parser_parse_tentatively (parser
);
22444 alignas_expr
= cp_parser_type_id (parser
);
22446 if (!cp_parser_parse_definitely (parser
))
22448 gcc_assert (alignas_expr
== error_mark_node
22449 || alignas_expr
== NULL_TREE
);
22452 cp_parser_assignment_expression (parser
);
22453 if (alignas_expr
== error_mark_node
)
22454 cp_parser_skip_to_end_of_statement (parser
);
22455 if (alignas_expr
== NULL_TREE
22456 || alignas_expr
== error_mark_node
)
22457 return alignas_expr
;
22460 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22462 cp_parser_error (parser
, "expected %<)%>");
22463 return error_mark_node
;
22466 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22468 /* Build the C++-11 representation of an 'aligned'
22471 build_tree_list (build_tree_list (get_identifier ("gnu"),
22472 get_identifier ("aligned")),
22473 build_tree_list (NULL_TREE
, alignas_expr
));
22479 /* Parse a standard C++-11 attribute-specifier-seq.
22481 attribute-specifier-seq:
22482 attribute-specifier-seq [opt] attribute-specifier
22486 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22488 tree attr_specs
= NULL
;
22492 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22493 if (attr_spec
== NULL_TREE
)
22495 if (attr_spec
== error_mark_node
)
22496 return error_mark_node
;
22498 TREE_CHAIN (attr_spec
) = attr_specs
;
22499 attr_specs
= attr_spec
;
22502 attr_specs
= nreverse (attr_specs
);
22506 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22507 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22508 current value of the PEDANTIC flag, regardless of whether or not
22509 the `__extension__' keyword is present. The caller is responsible
22510 for restoring the value of the PEDANTIC flag. */
22513 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22515 /* Save the old value of the PEDANTIC flag. */
22516 *saved_pedantic
= pedantic
;
22518 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22520 /* Consume the `__extension__' token. */
22521 cp_lexer_consume_token (parser
->lexer
);
22522 /* We're not being pedantic while the `__extension__' keyword is
22532 /* Parse a label declaration.
22535 __label__ label-declarator-seq ;
22537 label-declarator-seq:
22538 identifier , label-declarator-seq
22542 cp_parser_label_declaration (cp_parser
* parser
)
22544 /* Look for the `__label__' keyword. */
22545 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22551 /* Look for an identifier. */
22552 identifier
= cp_parser_identifier (parser
);
22553 /* If we failed, stop. */
22554 if (identifier
== error_mark_node
)
22556 /* Declare it as a label. */
22557 finish_label_decl (identifier
);
22558 /* If the next token is a `;', stop. */
22559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22561 /* Look for the `,' separating the label declarations. */
22562 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22565 /* Look for the final `;'. */
22566 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22569 /* Support Functions */
22571 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22572 NAME should have one of the representations used for an
22573 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22574 is returned. If PARSER->SCOPE is a dependent type, then a
22575 SCOPE_REF is returned.
22577 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22578 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22579 was formed. Abstractly, such entities should not be passed to this
22580 function, because they do not need to be looked up, but it is
22581 simpler to check for this special case here, rather than at the
22584 In cases not explicitly covered above, this function returns a
22585 DECL, OVERLOAD, or baselink representing the result of the lookup.
22586 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22589 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22590 (e.g., "struct") that was used. In that case bindings that do not
22591 refer to types are ignored.
22593 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22596 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22599 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22602 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22603 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22604 NULL_TREE otherwise. */
22607 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22608 enum tag_types tag_type
,
22611 bool check_dependency
,
22612 tree
*ambiguous_decls
,
22613 location_t name_location
)
22616 tree object_type
= parser
->context
->object_type
;
22618 /* Assume that the lookup will be unambiguous. */
22619 if (ambiguous_decls
)
22620 *ambiguous_decls
= NULL_TREE
;
22622 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22623 no longer valid. Note that if we are parsing tentatively, and
22624 the parse fails, OBJECT_TYPE will be automatically restored. */
22625 parser
->context
->object_type
= NULL_TREE
;
22627 if (name
== error_mark_node
)
22628 return error_mark_node
;
22630 /* A template-id has already been resolved; there is no lookup to
22632 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22634 if (BASELINK_P (name
))
22636 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22637 == TEMPLATE_ID_EXPR
);
22641 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22642 it should already have been checked to make sure that the name
22643 used matches the type being destroyed. */
22644 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22648 /* Figure out to which type this destructor applies. */
22650 type
= parser
->scope
;
22651 else if (object_type
)
22652 type
= object_type
;
22654 type
= current_class_type
;
22655 /* If that's not a class type, there is no destructor. */
22656 if (!type
|| !CLASS_TYPE_P (type
))
22657 return error_mark_node
;
22658 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22659 lazily_declare_fn (sfk_destructor
, type
);
22660 if (!CLASSTYPE_DESTRUCTORS (type
))
22661 return error_mark_node
;
22662 /* If it was a class type, return the destructor. */
22663 return CLASSTYPE_DESTRUCTORS (type
);
22666 /* By this point, the NAME should be an ordinary identifier. If
22667 the id-expression was a qualified name, the qualifying scope is
22668 stored in PARSER->SCOPE at this point. */
22669 gcc_assert (identifier_p (name
));
22671 /* Perform the lookup. */
22676 if (parser
->scope
== error_mark_node
)
22677 return error_mark_node
;
22679 /* If the SCOPE is dependent, the lookup must be deferred until
22680 the template is instantiated -- unless we are explicitly
22681 looking up names in uninstantiated templates. Even then, we
22682 cannot look up the name if the scope is not a class type; it
22683 might, for example, be a template type parameter. */
22684 dependent_p
= (TYPE_P (parser
->scope
)
22685 && dependent_scope_p (parser
->scope
));
22686 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22688 /* Defer lookup. */
22689 decl
= error_mark_node
;
22692 tree pushed_scope
= NULL_TREE
;
22694 /* If PARSER->SCOPE is a dependent type, then it must be a
22695 class type, and we must not be checking dependencies;
22696 otherwise, we would have processed this lookup above. So
22697 that PARSER->SCOPE is not considered a dependent base by
22698 lookup_member, we must enter the scope here. */
22700 pushed_scope
= push_scope (parser
->scope
);
22702 /* If the PARSER->SCOPE is a template specialization, it
22703 may be instantiated during name lookup. In that case,
22704 errors may be issued. Even if we rollback the current
22705 tentative parse, those errors are valid. */
22706 decl
= lookup_qualified_name (parser
->scope
, name
,
22707 tag_type
!= none_type
,
22708 /*complain=*/true);
22710 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22711 lookup result and the nested-name-specifier nominates a class C:
22712 * if the name specified after the nested-name-specifier, when
22713 looked up in C, is the injected-class-name of C (Clause 9), or
22714 * if the name specified after the nested-name-specifier is the
22715 same as the identifier or the simple-template-id's template-
22716 name in the last component of the nested-name-specifier,
22717 the name is instead considered to name the constructor of
22718 class C. [ Note: for example, the constructor is not an
22719 acceptable lookup result in an elaborated-type-specifier so
22720 the constructor would not be used in place of the
22721 injected-class-name. --end note ] Such a constructor name
22722 shall be used only in the declarator-id of a declaration that
22723 names a constructor or in a using-declaration. */
22724 if (tag_type
== none_type
22725 && DECL_SELF_REFERENCE_P (decl
)
22726 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22727 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22728 tag_type
!= none_type
,
22729 /*complain=*/true);
22731 /* If we have a single function from a using decl, pull it out. */
22732 if (TREE_CODE (decl
) == OVERLOAD
22733 && !really_overloaded_fn (decl
))
22734 decl
= OVL_FUNCTION (decl
);
22737 pop_scope (pushed_scope
);
22740 /* If the scope is a dependent type and either we deferred lookup or
22741 we did lookup but didn't find the name, rememeber the name. */
22742 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22743 && dependent_type_p (parser
->scope
))
22749 /* The resolution to Core Issue 180 says that `struct
22750 A::B' should be considered a type-name, even if `A'
22752 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22753 /*complain=*/tf_error
);
22754 if (type
!= error_mark_node
)
22755 decl
= TYPE_NAME (type
);
22757 else if (is_template
22758 && (cp_parser_next_token_ends_template_argument_p (parser
)
22759 || cp_lexer_next_token_is (parser
->lexer
,
22761 decl
= make_unbound_class_template (parser
->scope
,
22763 /*complain=*/tf_error
);
22765 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22766 parser
->scope
, name
,
22769 parser
->qualifying_scope
= parser
->scope
;
22770 parser
->object_scope
= NULL_TREE
;
22772 else if (object_type
)
22774 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22775 OBJECT_TYPE is not a class. */
22776 if (CLASS_TYPE_P (object_type
))
22777 /* If the OBJECT_TYPE is a template specialization, it may
22778 be instantiated during name lookup. In that case, errors
22779 may be issued. Even if we rollback the current tentative
22780 parse, those errors are valid. */
22781 decl
= lookup_member (object_type
,
22784 tag_type
!= none_type
,
22785 tf_warning_or_error
);
22790 /* Look it up in the enclosing context. */
22791 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22793 /*block_p=*/true, is_namespace
, 0);
22794 parser
->object_scope
= object_type
;
22795 parser
->qualifying_scope
= NULL_TREE
;
22799 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22801 /*block_p=*/true, is_namespace
, 0);
22802 parser
->qualifying_scope
= NULL_TREE
;
22803 parser
->object_scope
= NULL_TREE
;
22806 /* If the lookup failed, let our caller know. */
22807 if (!decl
|| decl
== error_mark_node
)
22808 return error_mark_node
;
22810 /* Pull out the template from an injected-class-name (or multiple). */
22812 decl
= maybe_get_template_decl_from_type_decl (decl
);
22814 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22815 if (TREE_CODE (decl
) == TREE_LIST
)
22817 if (ambiguous_decls
)
22818 *ambiguous_decls
= decl
;
22819 /* The error message we have to print is too complicated for
22820 cp_parser_error, so we incorporate its actions directly. */
22821 if (!cp_parser_simulate_error (parser
))
22823 error_at (name_location
, "reference to %qD is ambiguous",
22825 print_candidates (decl
);
22827 return error_mark_node
;
22830 gcc_assert (DECL_P (decl
)
22831 || TREE_CODE (decl
) == OVERLOAD
22832 || TREE_CODE (decl
) == SCOPE_REF
22833 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22834 || BASELINK_P (decl
));
22836 /* If we have resolved the name of a member declaration, check to
22837 see if the declaration is accessible. When the name resolves to
22838 set of overloaded functions, accessibility is checked when
22839 overload resolution is done.
22841 During an explicit instantiation, access is not checked at all,
22842 as per [temp.explicit]. */
22844 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22846 maybe_record_typedef_use (decl
);
22851 /* Like cp_parser_lookup_name, but for use in the typical case where
22852 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22853 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22856 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22858 return cp_parser_lookup_name (parser
, name
,
22860 /*is_template=*/false,
22861 /*is_namespace=*/false,
22862 /*check_dependency=*/true,
22863 /*ambiguous_decls=*/NULL
,
22867 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22868 the current context, return the TYPE_DECL. If TAG_NAME_P is
22869 true, the DECL indicates the class being defined in a class-head,
22870 or declared in an elaborated-type-specifier.
22872 Otherwise, return DECL. */
22875 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22877 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22878 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22881 template <typename T> struct B;
22884 template <typename T> struct A::B {};
22886 Similarly, in an elaborated-type-specifier:
22888 namespace N { struct X{}; }
22891 template <typename T> friend struct N::X;
22894 However, if the DECL refers to a class type, and we are in
22895 the scope of the class, then the name lookup automatically
22896 finds the TYPE_DECL created by build_self_reference rather
22897 than a TEMPLATE_DECL. For example, in:
22899 template <class T> struct S {
22903 there is no need to handle such case. */
22905 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
22906 return DECL_TEMPLATE_RESULT (decl
);
22911 /* If too many, or too few, template-parameter lists apply to the
22912 declarator, issue an error message. Returns TRUE if all went well,
22913 and FALSE otherwise. */
22916 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
22917 cp_declarator
*declarator
,
22918 location_t declarator_location
)
22920 switch (declarator
->kind
)
22924 unsigned num_templates
= 0;
22925 tree scope
= declarator
->u
.id
.qualifying_scope
;
22928 num_templates
= num_template_headers_for_class (scope
);
22929 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
22930 == TEMPLATE_ID_EXPR
)
22931 /* If the DECLARATOR has the form `X<y>' then it uses one
22932 additional level of template parameters. */
22935 return cp_parser_check_template_parameters
22936 (parser
, num_templates
, declarator_location
, declarator
);
22942 case cdk_reference
:
22944 return (cp_parser_check_declarator_template_parameters
22945 (parser
, declarator
->declarator
, declarator_location
));
22951 gcc_unreachable ();
22956 /* NUM_TEMPLATES were used in the current declaration. If that is
22957 invalid, return FALSE and issue an error messages. Otherwise,
22958 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22959 declarator and we can print more accurate diagnostics. */
22962 cp_parser_check_template_parameters (cp_parser
* parser
,
22963 unsigned num_templates
,
22964 location_t location
,
22965 cp_declarator
*declarator
)
22967 /* If there are the same number of template classes and parameter
22968 lists, that's OK. */
22969 if (parser
->num_template_parameter_lists
== num_templates
)
22971 /* If there are more, but only one more, then we are referring to a
22972 member template. That's OK too. */
22973 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
22975 /* If there are more template classes than parameter lists, we have
22978 template <class T> void S<T>::R<T>::f (); */
22979 if (parser
->num_template_parameter_lists
< num_templates
)
22981 if (declarator
&& !current_function_decl
)
22982 error_at (location
, "specializing member %<%T::%E%> "
22983 "requires %<template<>%> syntax",
22984 declarator
->u
.id
.qualifying_scope
,
22985 declarator
->u
.id
.unqualified_name
);
22986 else if (declarator
)
22987 error_at (location
, "invalid declaration of %<%T::%E%>",
22988 declarator
->u
.id
.qualifying_scope
,
22989 declarator
->u
.id
.unqualified_name
);
22991 error_at (location
, "too few template-parameter-lists");
22994 /* Otherwise, there are too many template parameter lists. We have
22997 template <class T> template <class U> void S::f(); */
22998 error_at (location
, "too many template-parameter-lists");
23002 /* Parse an optional `::' token indicating that the following name is
23003 from the global namespace. If so, PARSER->SCOPE is set to the
23004 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23005 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23006 Returns the new value of PARSER->SCOPE, if the `::' token is
23007 present, and NULL_TREE otherwise. */
23010 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23014 /* Peek at the next token. */
23015 token
= cp_lexer_peek_token (parser
->lexer
);
23016 /* If we're looking at a `::' token then we're starting from the
23017 global namespace, not our current location. */
23018 if (token
->type
== CPP_SCOPE
)
23020 /* Consume the `::' token. */
23021 cp_lexer_consume_token (parser
->lexer
);
23022 /* Set the SCOPE so that we know where to start the lookup. */
23023 parser
->scope
= global_namespace
;
23024 parser
->qualifying_scope
= global_namespace
;
23025 parser
->object_scope
= NULL_TREE
;
23027 return parser
->scope
;
23029 else if (!current_scope_valid_p
)
23031 parser
->scope
= NULL_TREE
;
23032 parser
->qualifying_scope
= NULL_TREE
;
23033 parser
->object_scope
= NULL_TREE
;
23039 /* Returns TRUE if the upcoming token sequence is the start of a
23040 constructor declarator. If FRIEND_P is true, the declarator is
23041 preceded by the `friend' specifier. */
23044 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23046 bool constructor_p
;
23047 bool outside_class_specifier_p
;
23048 tree nested_name_specifier
;
23049 cp_token
*next_token
;
23051 /* The common case is that this is not a constructor declarator, so
23052 try to avoid doing lots of work if at all possible. It's not
23053 valid declare a constructor at function scope. */
23054 if (parser
->in_function_body
)
23056 /* And only certain tokens can begin a constructor declarator. */
23057 next_token
= cp_lexer_peek_token (parser
->lexer
);
23058 if (next_token
->type
!= CPP_NAME
23059 && next_token
->type
!= CPP_SCOPE
23060 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23061 && next_token
->type
!= CPP_TEMPLATE_ID
)
23064 /* Parse tentatively; we are going to roll back all of the tokens
23066 cp_parser_parse_tentatively (parser
);
23067 /* Assume that we are looking at a constructor declarator. */
23068 constructor_p
= true;
23070 /* Look for the optional `::' operator. */
23071 cp_parser_global_scope_opt (parser
,
23072 /*current_scope_valid_p=*/false);
23073 /* Look for the nested-name-specifier. */
23074 nested_name_specifier
23075 = (cp_parser_nested_name_specifier_opt (parser
,
23076 /*typename_keyword_p=*/false,
23077 /*check_dependency_p=*/false,
23079 /*is_declaration=*/false));
23081 outside_class_specifier_p
= (!at_class_scope_p ()
23082 || !TYPE_BEING_DEFINED (current_class_type
)
23085 /* Outside of a class-specifier, there must be a
23086 nested-name-specifier. */
23087 if (!nested_name_specifier
&& outside_class_specifier_p
)
23088 constructor_p
= false;
23089 else if (nested_name_specifier
== error_mark_node
)
23090 constructor_p
= false;
23092 /* If we have a class scope, this is easy; DR 147 says that S::S always
23093 names the constructor, and no other qualified name could. */
23094 if (constructor_p
&& nested_name_specifier
23095 && CLASS_TYPE_P (nested_name_specifier
))
23097 tree id
= cp_parser_unqualified_id (parser
,
23098 /*template_keyword_p=*/false,
23099 /*check_dependency_p=*/false,
23100 /*declarator_p=*/true,
23101 /*optional_p=*/false);
23102 if (is_overloaded_fn (id
))
23103 id
= DECL_NAME (get_first_fn (id
));
23104 if (!constructor_name_p (id
, nested_name_specifier
))
23105 constructor_p
= false;
23107 /* If we still think that this might be a constructor-declarator,
23108 look for a class-name. */
23109 else if (constructor_p
)
23113 template <typename T> struct S {
23117 we must recognize that the nested `S' names a class. */
23119 type_decl
= cp_parser_class_name (parser
,
23120 /*typename_keyword_p=*/false,
23121 /*template_keyword_p=*/false,
23123 /*check_dependency_p=*/false,
23124 /*class_head_p=*/false,
23125 /*is_declaration=*/false);
23126 /* If there was no class-name, then this is not a constructor.
23127 Otherwise, if we are in a class-specifier and we aren't
23128 handling a friend declaration, check that its type matches
23129 current_class_type (c++/38313). Note: error_mark_node
23130 is left alone for error recovery purposes. */
23131 constructor_p
= (!cp_parser_error_occurred (parser
)
23132 && (outside_class_specifier_p
23133 || type_decl
== error_mark_node
23134 || same_type_p (current_class_type
,
23135 TREE_TYPE (type_decl
))));
23137 /* If we're still considering a constructor, we have to see a `(',
23138 to begin the parameter-declaration-clause, followed by either a
23139 `)', an `...', or a decl-specifier. We need to check for a
23140 type-specifier to avoid being fooled into thinking that:
23144 is a constructor. (It is actually a function named `f' that
23145 takes one parameter (of type `int') and returns a value of type
23148 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23149 constructor_p
= false;
23152 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23153 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23154 /* A parameter declaration begins with a decl-specifier,
23155 which is either the "attribute" keyword, a storage class
23156 specifier, or (usually) a type-specifier. */
23157 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23160 tree pushed_scope
= NULL_TREE
;
23161 unsigned saved_num_template_parameter_lists
;
23163 /* Names appearing in the type-specifier should be looked up
23164 in the scope of the class. */
23165 if (current_class_type
)
23169 type
= TREE_TYPE (type_decl
);
23170 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23172 type
= resolve_typename_type (type
,
23173 /*only_current_p=*/false);
23174 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23176 cp_parser_abort_tentative_parse (parser
);
23180 pushed_scope
= push_scope (type
);
23183 /* Inside the constructor parameter list, surrounding
23184 template-parameter-lists do not apply. */
23185 saved_num_template_parameter_lists
23186 = parser
->num_template_parameter_lists
;
23187 parser
->num_template_parameter_lists
= 0;
23189 /* Look for the type-specifier. */
23190 cp_parser_type_specifier (parser
,
23191 CP_PARSER_FLAGS_NONE
,
23192 /*decl_specs=*/NULL
,
23193 /*is_declarator=*/true,
23194 /*declares_class_or_enum=*/NULL
,
23195 /*is_cv_qualifier=*/NULL
);
23197 parser
->num_template_parameter_lists
23198 = saved_num_template_parameter_lists
;
23200 /* Leave the scope of the class. */
23202 pop_scope (pushed_scope
);
23204 constructor_p
= !cp_parser_error_occurred (parser
);
23208 /* We did not really want to consume any tokens. */
23209 cp_parser_abort_tentative_parse (parser
);
23211 return constructor_p
;
23214 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23215 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23216 they must be performed once we are in the scope of the function.
23218 Returns the function defined. */
23221 cp_parser_function_definition_from_specifiers_and_declarator
23222 (cp_parser
* parser
,
23223 cp_decl_specifier_seq
*decl_specifiers
,
23225 const cp_declarator
*declarator
)
23230 /* Begin the function-definition. */
23231 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23233 /* The things we're about to see are not directly qualified by any
23234 template headers we've seen thus far. */
23235 reset_specialization ();
23237 /* If there were names looked up in the decl-specifier-seq that we
23238 did not check, check them now. We must wait until we are in the
23239 scope of the function to perform the checks, since the function
23240 might be a friend. */
23241 perform_deferred_access_checks (tf_warning_or_error
);
23245 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23246 parser
->omp_declare_simd
= NULL
;
23251 /* Skip the entire function. */
23252 cp_parser_skip_to_end_of_block_or_statement (parser
);
23253 fn
= error_mark_node
;
23255 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23257 /* Seen already, skip it. An error message has already been output. */
23258 cp_parser_skip_to_end_of_block_or_statement (parser
);
23259 fn
= current_function_decl
;
23260 current_function_decl
= NULL_TREE
;
23261 /* If this is a function from a class, pop the nested class. */
23262 if (current_class_name
)
23263 pop_nested_class ();
23268 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23269 tv
= TV_PARSE_INLINE
;
23271 tv
= TV_PARSE_FUNC
;
23273 fn
= cp_parser_function_definition_after_declarator (parser
,
23274 /*inline_p=*/false);
23281 /* Parse the part of a function-definition that follows the
23282 declarator. INLINE_P is TRUE iff this function is an inline
23283 function defined within a class-specifier.
23285 Returns the function defined. */
23288 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23292 bool ctor_initializer_p
= false;
23293 bool saved_in_unbraced_linkage_specification_p
;
23294 bool saved_in_function_body
;
23295 unsigned saved_num_template_parameter_lists
;
23297 bool fully_implicit_function_template_p
23298 = parser
->fully_implicit_function_template_p
;
23299 parser
->fully_implicit_function_template_p
= false;
23300 tree implicit_template_parms
23301 = parser
->implicit_template_parms
;
23302 parser
->implicit_template_parms
= 0;
23303 cp_binding_level
* implicit_template_scope
23304 = parser
->implicit_template_scope
;
23305 parser
->implicit_template_scope
= 0;
23307 saved_in_function_body
= parser
->in_function_body
;
23308 parser
->in_function_body
= true;
23309 /* If the next token is `return', then the code may be trying to
23310 make use of the "named return value" extension that G++ used to
23312 token
= cp_lexer_peek_token (parser
->lexer
);
23313 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23315 /* Consume the `return' keyword. */
23316 cp_lexer_consume_token (parser
->lexer
);
23317 /* Look for the identifier that indicates what value is to be
23319 cp_parser_identifier (parser
);
23320 /* Issue an error message. */
23321 error_at (token
->location
,
23322 "named return values are no longer supported");
23323 /* Skip tokens until we reach the start of the function body. */
23326 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23327 if (token
->type
== CPP_OPEN_BRACE
23328 || token
->type
== CPP_EOF
23329 || token
->type
== CPP_PRAGMA_EOL
)
23331 cp_lexer_consume_token (parser
->lexer
);
23334 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23335 anything declared inside `f'. */
23336 saved_in_unbraced_linkage_specification_p
23337 = parser
->in_unbraced_linkage_specification_p
;
23338 parser
->in_unbraced_linkage_specification_p
= false;
23339 /* Inside the function, surrounding template-parameter-lists do not
23341 saved_num_template_parameter_lists
23342 = parser
->num_template_parameter_lists
;
23343 parser
->num_template_parameter_lists
= 0;
23345 start_lambda_scope (current_function_decl
);
23347 /* If the next token is `try', `__transaction_atomic', or
23348 `__transaction_relaxed`, then we are looking at either function-try-block
23349 or function-transaction-block. Note that all of these include the
23351 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23352 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23353 RID_TRANSACTION_ATOMIC
);
23354 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23355 RID_TRANSACTION_RELAXED
))
23356 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23357 RID_TRANSACTION_RELAXED
);
23358 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23359 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23361 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23362 (parser
, /*in_function_try_block=*/false);
23364 finish_lambda_scope ();
23366 /* Finish the function. */
23367 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23368 (inline_p
? 2 : 0));
23369 /* Generate code for it, if necessary. */
23370 expand_or_defer_fn (fn
);
23371 /* Restore the saved values. */
23372 parser
->in_unbraced_linkage_specification_p
23373 = saved_in_unbraced_linkage_specification_p
;
23374 parser
->num_template_parameter_lists
23375 = saved_num_template_parameter_lists
;
23376 parser
->in_function_body
= saved_in_function_body
;
23378 parser
->fully_implicit_function_template_p
23379 = fully_implicit_function_template_p
;
23380 parser
->implicit_template_parms
23381 = implicit_template_parms
;
23382 parser
->implicit_template_scope
23383 = implicit_template_scope
;
23385 if (parser
->fully_implicit_function_template_p
)
23386 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23391 /* Parse a template-declaration, assuming that the `export' (and
23392 `extern') keywords, if present, has already been scanned. MEMBER_P
23393 is as for cp_parser_template_declaration. */
23396 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23398 tree decl
= NULL_TREE
;
23399 vec
<deferred_access_check
, va_gc
> *checks
;
23400 tree parameter_list
;
23401 bool friend_p
= false;
23402 bool need_lang_pop
;
23405 /* Look for the `template' keyword. */
23406 token
= cp_lexer_peek_token (parser
->lexer
);
23407 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23411 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23413 if (at_class_scope_p () && current_function_decl
)
23415 /* 14.5.2.2 [temp.mem]
23417 A local class shall not have member templates. */
23418 error_at (token
->location
,
23419 "invalid declaration of member template in local class");
23420 cp_parser_skip_to_end_of_block_or_statement (parser
);
23425 A template ... shall not have C linkage. */
23426 if (current_lang_name
== lang_name_c
)
23428 error_at (token
->location
, "template with C linkage");
23429 /* Give it C++ linkage to avoid confusing other parts of the
23431 push_lang_context (lang_name_cplusplus
);
23432 need_lang_pop
= true;
23435 need_lang_pop
= false;
23437 /* We cannot perform access checks on the template parameter
23438 declarations until we know what is being declared, just as we
23439 cannot check the decl-specifier list. */
23440 push_deferring_access_checks (dk_deferred
);
23442 /* If the next token is `>', then we have an invalid
23443 specialization. Rather than complain about an invalid template
23444 parameter, issue an error message here. */
23445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23447 cp_parser_error (parser
, "invalid explicit specialization");
23448 begin_specialization ();
23449 parameter_list
= NULL_TREE
;
23453 /* Parse the template parameters. */
23454 parameter_list
= cp_parser_template_parameter_list (parser
);
23457 /* Get the deferred access checks from the parameter list. These
23458 will be checked once we know what is being declared, as for a
23459 member template the checks must be performed in the scope of the
23460 class containing the member. */
23461 checks
= get_deferred_access_checks ();
23463 /* Look for the `>'. */
23464 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23465 /* We just processed one more parameter list. */
23466 ++parser
->num_template_parameter_lists
;
23467 /* If the next token is `template', there are more template
23469 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23471 cp_parser_template_declaration_after_export (parser
, member_p
);
23472 else if (cxx_dialect
>= cxx11
23473 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23474 decl
= cp_parser_alias_declaration (parser
);
23477 /* There are no access checks when parsing a template, as we do not
23478 know if a specialization will be a friend. */
23479 push_deferring_access_checks (dk_no_check
);
23480 token
= cp_lexer_peek_token (parser
->lexer
);
23481 decl
= cp_parser_single_declaration (parser
,
23484 /*explicit_specialization_p=*/false,
23486 pop_deferring_access_checks ();
23488 /* If this is a member template declaration, let the front
23490 if (member_p
&& !friend_p
&& decl
)
23492 if (TREE_CODE (decl
) == TYPE_DECL
)
23493 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23495 decl
= finish_member_template_decl (decl
);
23497 else if (friend_p
&& decl
23498 && DECL_DECLARES_TYPE_P (decl
))
23499 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23500 /*complain=*/true);
23502 /* We are done with the current parameter list. */
23503 --parser
->num_template_parameter_lists
;
23505 pop_deferring_access_checks ();
23508 finish_template_decl (parameter_list
);
23510 /* Check the template arguments for a literal operator template. */
23512 && DECL_DECLARES_FUNCTION_P (decl
)
23513 && UDLIT_OPER_P (DECL_NAME (decl
)))
23516 if (parameter_list
== NULL_TREE
)
23520 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23521 if (num_parms
== 1)
23523 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23524 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23525 if (TREE_TYPE (parm
) != char_type_node
23526 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23529 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23531 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23532 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23533 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23534 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23535 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23536 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23544 if (cxx_dialect
>= cxx14
)
23545 error ("literal operator template %qD has invalid parameter list."
23546 " Expected non-type template argument pack <char...>"
23547 " or <typename CharT, CharT...>",
23550 error ("literal operator template %qD has invalid parameter list."
23551 " Expected non-type template argument pack <char...>",
23555 /* Register member declarations. */
23556 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23557 finish_member_declaration (decl
);
23558 /* For the erroneous case of a template with C linkage, we pushed an
23559 implicit C++ linkage scope; exit that scope now. */
23561 pop_lang_context ();
23562 /* If DECL is a function template, we must return to parse it later.
23563 (Even though there is no definition, there might be default
23564 arguments that need handling.) */
23565 if (member_p
&& decl
23566 && DECL_DECLARES_FUNCTION_P (decl
))
23567 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23570 /* Perform the deferred access checks from a template-parameter-list.
23571 CHECKS is a TREE_LIST of access checks, as returned by
23572 get_deferred_access_checks. */
23575 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23577 ++processing_template_parmlist
;
23578 perform_access_checks (checks
, tf_warning_or_error
);
23579 --processing_template_parmlist
;
23582 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23583 `function-definition' sequence that follows a template header.
23584 If MEMBER_P is true, this declaration appears in a class scope.
23586 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23587 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23590 cp_parser_single_declaration (cp_parser
* parser
,
23591 vec
<deferred_access_check
, va_gc
> *checks
,
23593 bool explicit_specialization_p
,
23596 int declares_class_or_enum
;
23597 tree decl
= NULL_TREE
;
23598 cp_decl_specifier_seq decl_specifiers
;
23599 bool function_definition_p
= false;
23600 cp_token
*decl_spec_token_start
;
23602 /* This function is only used when processing a template
23604 gcc_assert (innermost_scope_kind () == sk_template_parms
23605 || innermost_scope_kind () == sk_template_spec
);
23607 /* Defer access checks until we know what is being declared. */
23608 push_deferring_access_checks (dk_deferred
);
23610 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23612 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23613 cp_parser_decl_specifier_seq (parser
,
23614 CP_PARSER_FLAGS_OPTIONAL
,
23616 &declares_class_or_enum
);
23618 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23620 /* There are no template typedefs. */
23621 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23623 error_at (decl_spec_token_start
->location
,
23624 "template declaration of %<typedef%>");
23625 decl
= error_mark_node
;
23628 /* Gather up the access checks that occurred the
23629 decl-specifier-seq. */
23630 stop_deferring_access_checks ();
23632 /* Check for the declaration of a template class. */
23633 if (declares_class_or_enum
)
23635 if (cp_parser_declares_only_class_p (parser
))
23637 decl
= shadow_tag (&decl_specifiers
);
23642 friend template <typename T> struct A<T>::B;
23645 A<T>::B will be represented by a TYPENAME_TYPE, and
23646 therefore not recognized by shadow_tag. */
23647 if (friend_p
&& *friend_p
23649 && decl_specifiers
.type
23650 && TYPE_P (decl_specifiers
.type
))
23651 decl
= decl_specifiers
.type
;
23653 if (decl
&& decl
!= error_mark_node
)
23654 decl
= TYPE_NAME (decl
);
23656 decl
= error_mark_node
;
23658 /* Perform access checks for template parameters. */
23659 cp_parser_perform_template_parameter_access_checks (checks
);
23663 /* Complain about missing 'typename' or other invalid type names. */
23664 if (!decl_specifiers
.any_type_specifiers_p
23665 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23667 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23668 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23669 the rest of this declaration. */
23670 decl
= error_mark_node
;
23674 /* If it's not a template class, try for a template function. If
23675 the next token is a `;', then this declaration does not declare
23676 anything. But, if there were errors in the decl-specifiers, then
23677 the error might well have come from an attempted class-specifier.
23678 In that case, there's no need to warn about a missing declarator. */
23680 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23681 || decl_specifiers
.type
!= error_mark_node
))
23683 decl
= cp_parser_init_declarator (parser
,
23686 /*function_definition_allowed_p=*/true,
23688 declares_class_or_enum
,
23689 &function_definition_p
,
23692 /* 7.1.1-1 [dcl.stc]
23694 A storage-class-specifier shall not be specified in an explicit
23695 specialization... */
23697 && explicit_specialization_p
23698 && decl_specifiers
.storage_class
!= sc_none
)
23700 error_at (decl_spec_token_start
->location
,
23701 "explicit template specialization cannot have a storage class");
23702 decl
= error_mark_node
;
23705 if (decl
&& VAR_P (decl
))
23706 check_template_variable (decl
);
23709 /* Look for a trailing `;' after the declaration. */
23710 if (!function_definition_p
23711 && (decl
== error_mark_node
23712 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23713 cp_parser_skip_to_end_of_block_or_statement (parser
);
23716 pop_deferring_access_checks ();
23718 /* Clear any current qualification; whatever comes next is the start
23719 of something new. */
23720 parser
->scope
= NULL_TREE
;
23721 parser
->qualifying_scope
= NULL_TREE
;
23722 parser
->object_scope
= NULL_TREE
;
23727 /* Parse a cast-expression that is not the operand of a unary "&". */
23730 cp_parser_simple_cast_expression (cp_parser
*parser
)
23732 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23733 /*cast_p=*/false, /*decltype*/false, NULL
);
23736 /* Parse a functional cast to TYPE. Returns an expression
23737 representing the cast. */
23740 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23742 vec
<tree
, va_gc
> *vec
;
23743 tree expression_list
;
23748 type
= error_mark_node
;
23750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23752 cp_lexer_set_source_position (parser
->lexer
);
23753 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23754 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23755 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23756 if (TREE_CODE (type
) == TYPE_DECL
)
23757 type
= TREE_TYPE (type
);
23758 return finish_compound_literal (type
, expression_list
,
23759 tf_warning_or_error
);
23763 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23765 /*allow_expansion_p=*/true,
23766 /*non_constant_p=*/NULL
);
23768 expression_list
= error_mark_node
;
23771 expression_list
= build_tree_list_vec (vec
);
23772 release_tree_vector (vec
);
23775 cast
= build_functional_cast (type
, expression_list
,
23776 tf_warning_or_error
);
23777 /* [expr.const]/1: In an integral constant expression "only type
23778 conversions to integral or enumeration type can be used". */
23779 if (TREE_CODE (type
) == TYPE_DECL
)
23780 type
= TREE_TYPE (type
);
23781 if (cast
!= error_mark_node
23782 && !cast_valid_in_integral_constant_expression_p (type
)
23783 && cp_parser_non_integral_constant_expression (parser
,
23785 return error_mark_node
;
23789 /* Save the tokens that make up the body of a member function defined
23790 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23791 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23792 specifiers applied to the declaration. Returns the FUNCTION_DECL
23793 for the member function. */
23796 cp_parser_save_member_function_body (cp_parser
* parser
,
23797 cp_decl_specifier_seq
*decl_specifiers
,
23798 cp_declarator
*declarator
,
23805 /* Create the FUNCTION_DECL. */
23806 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23807 cp_finalize_omp_declare_simd (parser
, fn
);
23808 /* If something went badly wrong, bail out now. */
23809 if (fn
== error_mark_node
)
23811 /* If there's a function-body, skip it. */
23812 if (cp_parser_token_starts_function_definition_p
23813 (cp_lexer_peek_token (parser
->lexer
)))
23814 cp_parser_skip_to_end_of_block_or_statement (parser
);
23815 return error_mark_node
;
23818 /* Remember it, if there default args to post process. */
23819 cp_parser_save_default_args (parser
, fn
);
23821 /* Save away the tokens that make up the body of the
23823 first
= parser
->lexer
->next_token
;
23824 /* Handle function try blocks. */
23825 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23826 cp_lexer_consume_token (parser
->lexer
);
23827 /* We can have braced-init-list mem-initializers before the fn body. */
23828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23830 cp_lexer_consume_token (parser
->lexer
);
23831 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23833 /* cache_group will stop after an un-nested { } pair, too. */
23834 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23837 /* variadic mem-inits have ... after the ')'. */
23838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23839 cp_lexer_consume_token (parser
->lexer
);
23842 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23843 /* Handle function try blocks. */
23844 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23845 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23846 last
= parser
->lexer
->next_token
;
23848 /* Save away the inline definition; we will process it when the
23849 class is complete. */
23850 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23851 DECL_PENDING_INLINE_P (fn
) = 1;
23853 /* We need to know that this was defined in the class, so that
23854 friend templates are handled correctly. */
23855 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23857 /* Add FN to the queue of functions to be parsed later. */
23858 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23863 /* Save the tokens that make up the in-class initializer for a non-static
23864 data member. Returns a DEFAULT_ARG. */
23867 cp_parser_save_nsdmi (cp_parser
* parser
)
23869 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23872 /* Parse a template-argument-list, as well as the trailing ">" (but
23873 not the opening "<"). See cp_parser_template_argument_list for the
23877 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23881 tree saved_qualifying_scope
;
23882 tree saved_object_scope
;
23883 bool saved_greater_than_is_operator_p
;
23884 int saved_unevaluated_operand
;
23885 int saved_inhibit_evaluation_warnings
;
23889 When parsing a template-id, the first non-nested `>' is taken as
23890 the end of the template-argument-list rather than a greater-than
23892 saved_greater_than_is_operator_p
23893 = parser
->greater_than_is_operator_p
;
23894 parser
->greater_than_is_operator_p
= false;
23895 /* Parsing the argument list may modify SCOPE, so we save it
23897 saved_scope
= parser
->scope
;
23898 saved_qualifying_scope
= parser
->qualifying_scope
;
23899 saved_object_scope
= parser
->object_scope
;
23900 /* We need to evaluate the template arguments, even though this
23901 template-id may be nested within a "sizeof". */
23902 saved_unevaluated_operand
= cp_unevaluated_operand
;
23903 cp_unevaluated_operand
= 0;
23904 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
23905 c_inhibit_evaluation_warnings
= 0;
23906 /* Parse the template-argument-list itself. */
23907 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
23908 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23909 arguments
= NULL_TREE
;
23911 arguments
= cp_parser_template_argument_list (parser
);
23912 /* Look for the `>' that ends the template-argument-list. If we find
23913 a '>>' instead, it's probably just a typo. */
23914 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
23916 if (cxx_dialect
!= cxx98
)
23918 /* In C++0x, a `>>' in a template argument list or cast
23919 expression is considered to be two separate `>'
23920 tokens. So, change the current token to a `>', but don't
23921 consume it: it will be consumed later when the outer
23922 template argument list (or cast expression) is parsed.
23923 Note that this replacement of `>' for `>>' is necessary
23924 even if we are parsing tentatively: in the tentative
23925 case, after calling
23926 cp_parser_enclosed_template_argument_list we will always
23927 throw away all of the template arguments and the first
23928 closing `>', either because the template argument list
23929 was erroneous or because we are replacing those tokens
23930 with a CPP_TEMPLATE_ID token. The second `>' (which will
23931 not have been thrown away) is needed either to close an
23932 outer template argument list or to complete a new-style
23934 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23935 token
->type
= CPP_GREATER
;
23937 else if (!saved_greater_than_is_operator_p
)
23939 /* If we're in a nested template argument list, the '>>' has
23940 to be a typo for '> >'. We emit the error message, but we
23941 continue parsing and we push a '>' as next token, so that
23942 the argument list will be parsed correctly. Note that the
23943 global source location is still on the token before the
23944 '>>', so we need to say explicitly where we want it. */
23945 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23946 error_at (token
->location
, "%<>>%> should be %<> >%> "
23947 "within a nested template argument list");
23949 token
->type
= CPP_GREATER
;
23953 /* If this is not a nested template argument list, the '>>'
23954 is a typo for '>'. Emit an error message and continue.
23955 Same deal about the token location, but here we can get it
23956 right by consuming the '>>' before issuing the diagnostic. */
23957 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
23958 error_at (token
->location
,
23959 "spurious %<>>%>, use %<>%> to terminate "
23960 "a template argument list");
23964 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23965 /* The `>' token might be a greater-than operator again now. */
23966 parser
->greater_than_is_operator_p
23967 = saved_greater_than_is_operator_p
;
23968 /* Restore the SAVED_SCOPE. */
23969 parser
->scope
= saved_scope
;
23970 parser
->qualifying_scope
= saved_qualifying_scope
;
23971 parser
->object_scope
= saved_object_scope
;
23972 cp_unevaluated_operand
= saved_unevaluated_operand
;
23973 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
23978 /* MEMBER_FUNCTION is a member function, or a friend. If default
23979 arguments, or the body of the function have not yet been parsed,
23983 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
23985 timevar_push (TV_PARSE_INMETH
);
23986 /* If this member is a template, get the underlying
23988 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
23989 member_function
= DECL_TEMPLATE_RESULT (member_function
);
23991 /* There should not be any class definitions in progress at this
23992 point; the bodies of members are only parsed outside of all class
23994 gcc_assert (parser
->num_classes_being_defined
== 0);
23995 /* While we're parsing the member functions we might encounter more
23996 classes. We want to handle them right away, but we don't want
23997 them getting mixed up with functions that are currently in the
23999 push_unparsed_function_queues (parser
);
24001 /* Make sure that any template parameters are in scope. */
24002 maybe_begin_member_template_processing (member_function
);
24004 /* If the body of the function has not yet been parsed, parse it
24006 if (DECL_PENDING_INLINE_P (member_function
))
24008 tree function_scope
;
24009 cp_token_cache
*tokens
;
24011 /* The function is no longer pending; we are processing it. */
24012 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24013 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24014 DECL_PENDING_INLINE_P (member_function
) = 0;
24016 /* If this is a local class, enter the scope of the containing
24018 function_scope
= current_function_decl
;
24019 if (function_scope
)
24020 push_function_context ();
24022 /* Push the body of the function onto the lexer stack. */
24023 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24025 /* Let the front end know that we going to be defining this
24027 start_preparsed_function (member_function
, NULL_TREE
,
24028 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24030 /* Don't do access checking if it is a templated function. */
24031 if (processing_template_decl
)
24032 push_deferring_access_checks (dk_no_check
);
24034 /* #pragma omp declare reduction needs special parsing. */
24035 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24037 parser
->lexer
->in_pragma
= true;
24038 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24039 finish_function (/*inline*/2);
24040 cp_check_omp_declare_reduction (member_function
);
24043 /* Now, parse the body of the function. */
24044 cp_parser_function_definition_after_declarator (parser
,
24045 /*inline_p=*/true);
24047 if (processing_template_decl
)
24048 pop_deferring_access_checks ();
24050 /* Leave the scope of the containing function. */
24051 if (function_scope
)
24052 pop_function_context ();
24053 cp_parser_pop_lexer (parser
);
24056 /* Remove any template parameters from the symbol table. */
24057 maybe_end_member_template_processing ();
24059 /* Restore the queue. */
24060 pop_unparsed_function_queues (parser
);
24061 timevar_pop (TV_PARSE_INMETH
);
24064 /* If DECL contains any default args, remember it on the unparsed
24065 functions queue. */
24068 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24072 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24074 probe
= TREE_CHAIN (probe
))
24075 if (TREE_PURPOSE (probe
))
24077 cp_default_arg_entry entry
= {current_class_type
, decl
};
24078 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24083 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24084 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24085 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24086 from the parameter-type-list. */
24089 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24090 tree default_arg
, tree parmtype
)
24092 cp_token_cache
*tokens
;
24096 if (default_arg
== error_mark_node
)
24097 return error_mark_node
;
24099 /* Push the saved tokens for the default argument onto the parser's
24101 tokens
= DEFARG_TOKENS (default_arg
);
24102 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24104 start_lambda_scope (decl
);
24106 /* Parse the default argument. */
24107 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24108 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24111 finish_lambda_scope ();
24113 if (parsed_arg
== error_mark_node
)
24114 cp_parser_skip_to_end_of_statement (parser
);
24116 if (!processing_template_decl
)
24118 /* In a non-template class, check conversions now. In a template,
24119 we'll wait and instantiate these as needed. */
24120 if (TREE_CODE (decl
) == PARM_DECL
)
24121 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24122 tf_warning_or_error
);
24124 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24127 /* If the token stream has not been completely used up, then
24128 there was extra junk after the end of the default
24130 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24132 if (TREE_CODE (decl
) == PARM_DECL
)
24133 cp_parser_error (parser
, "expected %<,%>");
24135 cp_parser_error (parser
, "expected %<;%>");
24138 /* Revert to the main lexer. */
24139 cp_parser_pop_lexer (parser
);
24144 /* FIELD is a non-static data member with an initializer which we saved for
24145 later; parse it now. */
24148 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24152 maybe_begin_member_template_processing (field
);
24154 push_unparsed_function_queues (parser
);
24155 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24156 DECL_INITIAL (field
),
24158 pop_unparsed_function_queues (parser
);
24160 maybe_end_member_template_processing ();
24162 DECL_INITIAL (field
) = def
;
24165 /* FN is a FUNCTION_DECL which may contains a parameter with an
24166 unparsed DEFAULT_ARG. Parse the default args now. This function
24167 assumes that the current scope is the scope in which the default
24168 argument should be processed. */
24171 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24173 bool saved_local_variables_forbidden_p
;
24174 tree parm
, parmdecl
;
24176 /* While we're parsing the default args, we might (due to the
24177 statement expression extension) encounter more classes. We want
24178 to handle them right away, but we don't want them getting mixed
24179 up with default args that are currently in the queue. */
24180 push_unparsed_function_queues (parser
);
24182 /* Local variable names (and the `this' keyword) may not appear
24183 in a default argument. */
24184 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24185 parser
->local_variables_forbidden_p
= true;
24187 push_defarg_context (fn
);
24189 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24190 parmdecl
= DECL_ARGUMENTS (fn
);
24191 parm
&& parm
!= void_list_node
;
24192 parm
= TREE_CHAIN (parm
),
24193 parmdecl
= DECL_CHAIN (parmdecl
))
24195 tree default_arg
= TREE_PURPOSE (parm
);
24197 vec
<tree
, va_gc
> *insts
;
24204 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24205 /* This can happen for a friend declaration for a function
24206 already declared with default arguments. */
24210 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24212 TREE_VALUE (parm
));
24213 if (parsed_arg
== error_mark_node
)
24218 TREE_PURPOSE (parm
) = parsed_arg
;
24220 /* Update any instantiations we've already created. */
24221 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24222 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24223 TREE_PURPOSE (copy
) = parsed_arg
;
24226 pop_defarg_context ();
24228 /* Make sure no default arg is missing. */
24229 check_default_args (fn
);
24231 /* Restore the state of local_variables_forbidden_p. */
24232 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24234 /* Restore the queue. */
24235 pop_unparsed_function_queues (parser
);
24238 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24240 sizeof ... ( identifier )
24242 where the 'sizeof' token has already been consumed. */
24245 cp_parser_sizeof_pack (cp_parser
*parser
)
24247 /* Consume the `...'. */
24248 cp_lexer_consume_token (parser
->lexer
);
24249 maybe_warn_variadic_templates ();
24251 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24253 cp_lexer_consume_token (parser
->lexer
);
24255 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24256 "%<sizeof...%> argument must be surrounded by parentheses");
24258 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24259 tree name
= cp_parser_identifier (parser
);
24260 if (name
== error_mark_node
)
24261 return error_mark_node
;
24262 /* The name is not qualified. */
24263 parser
->scope
= NULL_TREE
;
24264 parser
->qualifying_scope
= NULL_TREE
;
24265 parser
->object_scope
= NULL_TREE
;
24266 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24267 if (expr
== error_mark_node
)
24268 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24270 if (TREE_CODE (expr
) == TYPE_DECL
)
24271 expr
= TREE_TYPE (expr
);
24272 else if (TREE_CODE (expr
) == CONST_DECL
)
24273 expr
= DECL_INITIAL (expr
);
24274 expr
= make_pack_expansion (expr
);
24277 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24282 /* Parse the operand of `sizeof' (or a similar operator). Returns
24283 either a TYPE or an expression, depending on the form of the
24284 input. The KEYWORD indicates which kind of expression we have
24288 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24290 tree expr
= NULL_TREE
;
24291 const char *saved_message
;
24293 bool saved_integral_constant_expression_p
;
24294 bool saved_non_integral_constant_expression_p
;
24296 /* If it's a `...', then we are computing the length of a parameter
24298 if (keyword
== RID_SIZEOF
24299 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24300 return cp_parser_sizeof_pack (parser
);
24302 /* Types cannot be defined in a `sizeof' expression. Save away the
24304 saved_message
= parser
->type_definition_forbidden_message
;
24305 /* And create the new one. */
24306 tmp
= concat ("types may not be defined in %<",
24307 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24308 "%> expressions", NULL
);
24309 parser
->type_definition_forbidden_message
= tmp
;
24311 /* The restrictions on constant-expressions do not apply inside
24312 sizeof expressions. */
24313 saved_integral_constant_expression_p
24314 = parser
->integral_constant_expression_p
;
24315 saved_non_integral_constant_expression_p
24316 = parser
->non_integral_constant_expression_p
;
24317 parser
->integral_constant_expression_p
= false;
24319 /* Do not actually evaluate the expression. */
24320 ++cp_unevaluated_operand
;
24321 ++c_inhibit_evaluation_warnings
;
24322 /* If it's a `(', then we might be looking at the type-id
24324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24326 tree type
= NULL_TREE
;
24328 /* We can't be sure yet whether we're looking at a type-id or an
24330 cp_parser_parse_tentatively (parser
);
24331 /* Note: as a GNU Extension, compound literals are considered
24332 postfix-expressions as they are in C99, so they are valid
24333 arguments to sizeof. See comment in cp_parser_cast_expression
24335 if (cp_parser_compound_literal_p (parser
))
24336 cp_parser_simulate_error (parser
);
24339 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24340 parser
->in_type_id_in_expr_p
= true;
24341 /* Look for the type-id. */
24342 type
= cp_parser_type_id (parser
);
24343 /* Look for the closing `)'. */
24344 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24345 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24348 /* If all went well, then we're done. */
24349 if (cp_parser_parse_definitely (parser
))
24351 cp_decl_specifier_seq decl_specs
;
24353 /* Build a trivial decl-specifier-seq. */
24354 clear_decl_specs (&decl_specs
);
24355 decl_specs
.type
= type
;
24357 /* Call grokdeclarator to figure out what type this is. */
24358 expr
= grokdeclarator (NULL
,
24362 /*attrlist=*/NULL
);
24366 /* If the type-id production did not work out, then we must be
24367 looking at the unary-expression production. */
24369 expr
= cp_parser_unary_expression (parser
);
24371 /* Go back to evaluating expressions. */
24372 --cp_unevaluated_operand
;
24373 --c_inhibit_evaluation_warnings
;
24375 /* Free the message we created. */
24377 /* And restore the old one. */
24378 parser
->type_definition_forbidden_message
= saved_message
;
24379 parser
->integral_constant_expression_p
24380 = saved_integral_constant_expression_p
;
24381 parser
->non_integral_constant_expression_p
24382 = saved_non_integral_constant_expression_p
;
24387 /* If the current declaration has no declarator, return true. */
24390 cp_parser_declares_only_class_p (cp_parser
*parser
)
24392 /* If the next token is a `;' or a `,' then there is no
24394 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24395 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24398 /* Update the DECL_SPECS to reflect the storage class indicated by
24402 cp_parser_set_storage_class (cp_parser
*parser
,
24403 cp_decl_specifier_seq
*decl_specs
,
24407 cp_storage_class storage_class
;
24409 if (parser
->in_unbraced_linkage_specification_p
)
24411 error_at (token
->location
, "invalid use of %qD in linkage specification",
24412 ridpointers
[keyword
]);
24415 else if (decl_specs
->storage_class
!= sc_none
)
24417 decl_specs
->conflicting_specifiers_p
= true;
24421 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24422 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24423 && decl_specs
->gnu_thread_keyword_p
)
24425 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24426 "%<__thread%> before %qD", ridpointers
[keyword
]);
24432 storage_class
= sc_auto
;
24435 storage_class
= sc_register
;
24438 storage_class
= sc_static
;
24441 storage_class
= sc_extern
;
24444 storage_class
= sc_mutable
;
24447 gcc_unreachable ();
24449 decl_specs
->storage_class
= storage_class
;
24450 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24452 /* A storage class specifier cannot be applied alongside a typedef
24453 specifier. If there is a typedef specifier present then set
24454 conflicting_specifiers_p which will trigger an error later
24455 on in grokdeclarator. */
24456 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24457 decl_specs
->conflicting_specifiers_p
= true;
24460 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24461 is true, the type is a class or enum definition. */
24464 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24467 bool type_definition_p
)
24469 decl_specs
->any_specifiers_p
= true;
24471 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24472 (with, for example, in "typedef int wchar_t;") we remember that
24473 this is what happened. In system headers, we ignore these
24474 declarations so that G++ can work with system headers that are not
24476 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24477 && !type_definition_p
24478 && (type_spec
== boolean_type_node
24479 || type_spec
== char16_type_node
24480 || type_spec
== char32_type_node
24481 || type_spec
== wchar_type_node
)
24482 && (decl_specs
->type
24483 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24484 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24485 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24486 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24488 decl_specs
->redefined_builtin_type
= type_spec
;
24489 set_and_check_decl_spec_loc (decl_specs
,
24490 ds_redefined_builtin_type_spec
,
24492 if (!decl_specs
->type
)
24494 decl_specs
->type
= type_spec
;
24495 decl_specs
->type_definition_p
= false;
24496 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24499 else if (decl_specs
->type
)
24500 decl_specs
->multiple_types_p
= true;
24503 decl_specs
->type
= type_spec
;
24504 decl_specs
->type_definition_p
= type_definition_p
;
24505 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24506 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24510 /* True iff TOKEN is the GNU keyword __thread. */
24513 token_is__thread (cp_token
*token
)
24515 gcc_assert (token
->keyword
== RID_THREAD
);
24516 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24519 /* Set the location for a declarator specifier and check if it is
24522 DECL_SPECS is the sequence of declarator specifiers onto which to
24525 DS is the single declarator specifier to set which location is to
24526 be set onto the existing sequence of declarators.
24528 LOCATION is the location for the declarator specifier to
24532 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24533 cp_decl_spec ds
, cp_token
*token
)
24535 gcc_assert (ds
< ds_last
);
24537 if (decl_specs
== NULL
)
24540 source_location location
= token
->location
;
24542 if (decl_specs
->locations
[ds
] == 0)
24544 decl_specs
->locations
[ds
] = location
;
24545 if (ds
== ds_thread
)
24546 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24552 if (decl_specs
->locations
[ds_long_long
] != 0)
24553 error_at (location
,
24554 "%<long long long%> is too long for GCC");
24557 decl_specs
->locations
[ds_long_long
] = location
;
24558 pedwarn_cxx98 (location
,
24560 "ISO C++ 1998 does not support %<long long%>");
24563 else if (ds
== ds_thread
)
24565 bool gnu
= token_is__thread (token
);
24566 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24567 error_at (location
,
24568 "both %<__thread%> and %<thread_local%> specified");
24570 error_at (location
, "duplicate %qD", token
->u
.value
);
24574 static const char *const decl_spec_names
[] = {
24591 error_at (location
,
24592 "duplicate %qs", decl_spec_names
[ds
]);
24597 /* Return true iff the declarator specifier DS is present in the
24598 sequence of declarator specifiers DECL_SPECS. */
24601 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24604 gcc_assert (ds
< ds_last
);
24606 if (decl_specs
== NULL
)
24609 return decl_specs
->locations
[ds
] != 0;
24612 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24613 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24616 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24618 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24621 /* Issue an error message indicating that TOKEN_DESC was expected.
24622 If KEYWORD is true, it indicated this function is called by
24623 cp_parser_require_keword and the required token can only be
24624 a indicated keyword. */
24627 cp_parser_required_error (cp_parser
*parser
,
24628 required_token token_desc
,
24631 switch (token_desc
)
24634 cp_parser_error (parser
, "expected %<new%>");
24637 cp_parser_error (parser
, "expected %<delete%>");
24640 cp_parser_error (parser
, "expected %<return%>");
24643 cp_parser_error (parser
, "expected %<while%>");
24646 cp_parser_error (parser
, "expected %<extern%>");
24648 case RT_STATIC_ASSERT
:
24649 cp_parser_error (parser
, "expected %<static_assert%>");
24652 cp_parser_error (parser
, "expected %<decltype%>");
24655 cp_parser_error (parser
, "expected %<operator%>");
24658 cp_parser_error (parser
, "expected %<class%>");
24661 cp_parser_error (parser
, "expected %<template%>");
24664 cp_parser_error (parser
, "expected %<namespace%>");
24667 cp_parser_error (parser
, "expected %<using%>");
24670 cp_parser_error (parser
, "expected %<asm%>");
24673 cp_parser_error (parser
, "expected %<try%>");
24676 cp_parser_error (parser
, "expected %<catch%>");
24679 cp_parser_error (parser
, "expected %<throw%>");
24682 cp_parser_error (parser
, "expected %<__label__%>");
24685 cp_parser_error (parser
, "expected %<@try%>");
24687 case RT_AT_SYNCHRONIZED
:
24688 cp_parser_error (parser
, "expected %<@synchronized%>");
24691 cp_parser_error (parser
, "expected %<@throw%>");
24693 case RT_TRANSACTION_ATOMIC
:
24694 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24696 case RT_TRANSACTION_RELAXED
:
24697 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24704 switch (token_desc
)
24707 cp_parser_error (parser
, "expected %<;%>");
24709 case RT_OPEN_PAREN
:
24710 cp_parser_error (parser
, "expected %<(%>");
24712 case RT_CLOSE_BRACE
:
24713 cp_parser_error (parser
, "expected %<}%>");
24715 case RT_OPEN_BRACE
:
24716 cp_parser_error (parser
, "expected %<{%>");
24718 case RT_CLOSE_SQUARE
:
24719 cp_parser_error (parser
, "expected %<]%>");
24721 case RT_OPEN_SQUARE
:
24722 cp_parser_error (parser
, "expected %<[%>");
24725 cp_parser_error (parser
, "expected %<,%>");
24728 cp_parser_error (parser
, "expected %<::%>");
24731 cp_parser_error (parser
, "expected %<<%>");
24734 cp_parser_error (parser
, "expected %<>%>");
24737 cp_parser_error (parser
, "expected %<=%>");
24740 cp_parser_error (parser
, "expected %<...%>");
24743 cp_parser_error (parser
, "expected %<*%>");
24746 cp_parser_error (parser
, "expected %<~%>");
24749 cp_parser_error (parser
, "expected %<:%>");
24751 case RT_COLON_SCOPE
:
24752 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24754 case RT_CLOSE_PAREN
:
24755 cp_parser_error (parser
, "expected %<)%>");
24757 case RT_COMMA_CLOSE_PAREN
:
24758 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24760 case RT_PRAGMA_EOL
:
24761 cp_parser_error (parser
, "expected end of line");
24764 cp_parser_error (parser
, "expected identifier");
24767 cp_parser_error (parser
, "expected selection-statement");
24769 case RT_INTERATION
:
24770 cp_parser_error (parser
, "expected iteration-statement");
24773 cp_parser_error (parser
, "expected jump-statement");
24776 cp_parser_error (parser
, "expected class-key");
24778 case RT_CLASS_TYPENAME_TEMPLATE
:
24779 cp_parser_error (parser
,
24780 "expected %<class%>, %<typename%>, or %<template%>");
24783 gcc_unreachable ();
24787 gcc_unreachable ();
24792 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24793 issue an error message indicating that TOKEN_DESC was expected.
24795 Returns the token consumed, if the token had the appropriate type.
24796 Otherwise, returns NULL. */
24799 cp_parser_require (cp_parser
* parser
,
24800 enum cpp_ttype type
,
24801 required_token token_desc
)
24803 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24804 return cp_lexer_consume_token (parser
->lexer
);
24807 /* Output the MESSAGE -- unless we're parsing tentatively. */
24808 if (!cp_parser_simulate_error (parser
))
24809 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24814 /* An error message is produced if the next token is not '>'.
24815 All further tokens are skipped until the desired token is
24816 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24819 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24821 /* Current level of '< ... >'. */
24822 unsigned level
= 0;
24823 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24824 unsigned nesting_depth
= 0;
24826 /* Are we ready, yet? If not, issue error message. */
24827 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24830 /* Skip tokens until the desired token is found. */
24833 /* Peek at the next token. */
24834 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24837 if (!nesting_depth
)
24842 if (cxx_dialect
== cxx98
)
24843 /* C++0x views the `>>' operator as two `>' tokens, but
24846 else if (!nesting_depth
&& level
-- == 0)
24848 /* We've hit a `>>' where the first `>' closes the
24849 template argument list, and the second `>' is
24850 spurious. Just consume the `>>' and stop; we've
24851 already produced at least one error. */
24852 cp_lexer_consume_token (parser
->lexer
);
24855 /* Fall through for C++0x, so we handle the second `>' in
24859 if (!nesting_depth
&& level
-- == 0)
24861 /* We've reached the token we want, consume it and stop. */
24862 cp_lexer_consume_token (parser
->lexer
);
24867 case CPP_OPEN_PAREN
:
24868 case CPP_OPEN_SQUARE
:
24872 case CPP_CLOSE_PAREN
:
24873 case CPP_CLOSE_SQUARE
:
24874 if (nesting_depth
-- == 0)
24879 case CPP_PRAGMA_EOL
:
24880 case CPP_SEMICOLON
:
24881 case CPP_OPEN_BRACE
:
24882 case CPP_CLOSE_BRACE
:
24883 /* The '>' was probably forgotten, don't look further. */
24890 /* Consume this token. */
24891 cp_lexer_consume_token (parser
->lexer
);
24895 /* If the next token is the indicated keyword, consume it. Otherwise,
24896 issue an error message indicating that TOKEN_DESC was expected.
24898 Returns the token consumed, if the token had the appropriate type.
24899 Otherwise, returns NULL. */
24902 cp_parser_require_keyword (cp_parser
* parser
,
24904 required_token token_desc
)
24906 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
24908 if (token
&& token
->keyword
!= keyword
)
24910 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
24917 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24918 function-definition. */
24921 cp_parser_token_starts_function_definition_p (cp_token
* token
)
24923 return (/* An ordinary function-body begins with an `{'. */
24924 token
->type
== CPP_OPEN_BRACE
24925 /* A ctor-initializer begins with a `:'. */
24926 || token
->type
== CPP_COLON
24927 /* A function-try-block begins with `try'. */
24928 || token
->keyword
== RID_TRY
24929 /* A function-transaction-block begins with `__transaction_atomic'
24930 or `__transaction_relaxed'. */
24931 || token
->keyword
== RID_TRANSACTION_ATOMIC
24932 || token
->keyword
== RID_TRANSACTION_RELAXED
24933 /* The named return value extension begins with `return'. */
24934 || token
->keyword
== RID_RETURN
);
24937 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24941 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
24945 token
= cp_lexer_peek_token (parser
->lexer
);
24946 return (token
->type
== CPP_OPEN_BRACE
24947 || (token
->type
== CPP_COLON
24948 && !parser
->colon_doesnt_start_class_def_p
));
24951 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24952 C++0x) ending a template-argument. */
24955 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
24959 token
= cp_lexer_peek_token (parser
->lexer
);
24960 return (token
->type
== CPP_COMMA
24961 || token
->type
== CPP_GREATER
24962 || token
->type
== CPP_ELLIPSIS
24963 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
24966 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24967 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24970 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
24975 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24976 if (token
->type
== CPP_LESS
)
24978 /* Check for the sequence `<::' in the original code. It would be lexed as
24979 `[:', where `[' is a digraph, and there is no whitespace before
24981 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
24984 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
24985 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
24991 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24992 or none_type otherwise. */
24994 static enum tag_types
24995 cp_parser_token_is_class_key (cp_token
* token
)
24997 switch (token
->keyword
)
25002 return record_type
;
25011 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25012 or none_type otherwise or if the token is null. */
25014 static enum tag_types
25015 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25020 switch (token
->keyword
)
25025 return typename_type
;
25032 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25035 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25037 if (type
== error_mark_node
)
25039 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25041 if (permerror (input_location
, "%qs tag used in naming %q#T",
25042 class_key
== union_type
? "union"
25043 : class_key
== record_type
? "struct" : "class",
25045 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25046 "%q#T was previously declared here", type
);
25050 /* Issue an error message if DECL is redeclared with different
25051 access than its original declaration [class.access.spec/3].
25052 This applies to nested classes and nested class templates.
25056 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25058 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25061 if ((TREE_PRIVATE (decl
)
25062 != (current_access_specifier
== access_private_node
))
25063 || (TREE_PROTECTED (decl
)
25064 != (current_access_specifier
== access_protected_node
)))
25065 error_at (location
, "%qD redeclared with different access", decl
);
25068 /* Look for the `template' keyword, as a syntactic disambiguator.
25069 Return TRUE iff it is present, in which case it will be
25073 cp_parser_optional_template_keyword (cp_parser
*parser
)
25075 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25077 /* In C++98 the `template' keyword can only be used within templates;
25078 outside templates the parser can always figure out what is a
25079 template and what is not. In C++11, per the resolution of DR 468,
25080 `template' is allowed in cases where it is not strictly necessary. */
25081 if (!processing_template_decl
25082 && pedantic
&& cxx_dialect
== cxx98
)
25084 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25085 pedwarn (token
->location
, OPT_Wpedantic
,
25086 "in C++98 %<template%> (as a disambiguator) is only "
25087 "allowed within templates");
25088 /* If this part of the token stream is rescanned, the same
25089 error message would be generated. So, we purge the token
25090 from the stream. */
25091 cp_lexer_purge_token (parser
->lexer
);
25096 /* Consume the `template' keyword. */
25097 cp_lexer_consume_token (parser
->lexer
);
25104 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25105 set PARSER->SCOPE, and perform other related actions. */
25108 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25111 struct tree_check
*check_value
;
25112 deferred_access_check
*chk
;
25113 vec
<deferred_access_check
, va_gc
> *checks
;
25115 /* Get the stored value. */
25116 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25117 /* Perform any access checks that were deferred. */
25118 checks
= check_value
->checks
;
25121 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25122 perform_or_defer_access_check (chk
->binfo
,
25124 chk
->diag_decl
, tf_warning_or_error
);
25126 /* Set the scope from the stored value. */
25127 parser
->scope
= check_value
->value
;
25128 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25129 parser
->object_scope
= NULL_TREE
;
25132 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25133 encounter the end of a block before what we were looking for. */
25136 cp_parser_cache_group (cp_parser
*parser
,
25137 enum cpp_ttype end
,
25142 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25144 /* Abort a parenthesized expression if we encounter a semicolon. */
25145 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25146 && token
->type
== CPP_SEMICOLON
)
25148 /* If we've reached the end of the file, stop. */
25149 if (token
->type
== CPP_EOF
25150 || (end
!= CPP_PRAGMA_EOL
25151 && token
->type
== CPP_PRAGMA_EOL
))
25153 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25154 /* We've hit the end of an enclosing block, so there's been some
25155 kind of syntax error. */
25158 /* Consume the token. */
25159 cp_lexer_consume_token (parser
->lexer
);
25160 /* See if it starts a new group. */
25161 if (token
->type
== CPP_OPEN_BRACE
)
25163 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25164 /* In theory this should probably check end == '}', but
25165 cp_parser_save_member_function_body needs it to exit
25166 after either '}' or ')' when called with ')'. */
25170 else if (token
->type
== CPP_OPEN_PAREN
)
25172 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25173 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25176 else if (token
->type
== CPP_PRAGMA
)
25177 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25178 else if (token
->type
== end
)
25183 /* Like above, for caching a default argument or NSDMI. Both of these are
25184 terminated by a non-nested comma, but it can be unclear whether or not a
25185 comma is nested in a template argument list unless we do more parsing.
25186 In order to handle this ambiguity, when we encounter a ',' after a '<'
25187 we try to parse what follows as a parameter-declaration-list (in the
25188 case of a default argument) or a member-declarator (in the case of an
25189 NSDMI). If that succeeds, then we stop caching. */
25192 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25194 unsigned depth
= 0;
25195 int maybe_template_id
= 0;
25196 cp_token
*first_token
;
25198 tree default_argument
;
25200 /* Add tokens until we have processed the entire default
25201 argument. We add the range [first_token, token). */
25202 first_token
= cp_lexer_peek_token (parser
->lexer
);
25203 if (first_token
->type
== CPP_OPEN_BRACE
)
25205 /* For list-initialization, this is straightforward. */
25206 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25207 token
= cp_lexer_peek_token (parser
->lexer
);
25213 /* Peek at the next token. */
25214 token
= cp_lexer_peek_token (parser
->lexer
);
25215 /* What we do depends on what token we have. */
25216 switch (token
->type
)
25218 /* In valid code, a default argument must be
25219 immediately followed by a `,' `)', or `...'. */
25221 if (depth
== 0 && maybe_template_id
)
25223 /* If we've seen a '<', we might be in a
25224 template-argument-list. Until Core issue 325 is
25225 resolved, we don't know how this situation ought
25226 to be handled, so try to DTRT. We check whether
25227 what comes after the comma is a valid parameter
25228 declaration list. If it is, then the comma ends
25229 the default argument; otherwise the default
25230 argument continues. */
25231 bool error
= false;
25233 /* Set ITALP so cp_parser_parameter_declaration_list
25234 doesn't decide to commit to this parse. */
25235 bool saved_italp
= parser
->in_template_argument_list_p
;
25236 parser
->in_template_argument_list_p
= true;
25238 cp_parser_parse_tentatively (parser
);
25239 cp_lexer_consume_token (parser
->lexer
);
25243 int ctor_dtor_or_conv_p
;
25244 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25245 &ctor_dtor_or_conv_p
,
25246 /*parenthesized_p=*/NULL
,
25248 /*friend_p=*/false);
25252 begin_scope (sk_function_parms
, NULL_TREE
);
25253 cp_parser_parameter_declaration_list (parser
, &error
);
25254 pop_bindings_and_leave_scope ();
25256 if (!cp_parser_error_occurred (parser
) && !error
)
25258 cp_parser_abort_tentative_parse (parser
);
25260 parser
->in_template_argument_list_p
= saved_italp
;
25263 case CPP_CLOSE_PAREN
:
25265 /* If we run into a non-nested `;', `}', or `]',
25266 then the code is invalid -- but the default
25267 argument is certainly over. */
25268 case CPP_SEMICOLON
:
25269 case CPP_CLOSE_BRACE
:
25270 case CPP_CLOSE_SQUARE
:
25272 /* Handle correctly int n = sizeof ... ( p ); */
25273 && token
->type
!= CPP_ELLIPSIS
)
25275 /* Update DEPTH, if necessary. */
25276 else if (token
->type
== CPP_CLOSE_PAREN
25277 || token
->type
== CPP_CLOSE_BRACE
25278 || token
->type
== CPP_CLOSE_SQUARE
)
25282 case CPP_OPEN_PAREN
:
25283 case CPP_OPEN_SQUARE
:
25284 case CPP_OPEN_BRACE
:
25290 /* This might be the comparison operator, or it might
25291 start a template argument list. */
25292 ++maybe_template_id
;
25296 if (cxx_dialect
== cxx98
)
25298 /* Fall through for C++0x, which treats the `>>'
25299 operator like two `>' tokens in certain
25305 /* This might be an operator, or it might close a
25306 template argument list. But if a previous '<'
25307 started a template argument list, this will have
25308 closed it, so we can't be in one anymore. */
25309 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25310 if (maybe_template_id
< 0)
25311 maybe_template_id
= 0;
25315 /* If we run out of tokens, issue an error message. */
25317 case CPP_PRAGMA_EOL
:
25318 error_at (token
->location
, "file ends in default argument");
25324 /* In these cases, we should look for template-ids.
25325 For example, if the default argument is
25326 `X<int, double>()', we need to do name lookup to
25327 figure out whether or not `X' is a template; if
25328 so, the `,' does not end the default argument.
25330 That is not yet done. */
25337 /* If we've reached the end, stop. */
25341 /* Add the token to the token block. */
25342 token
= cp_lexer_consume_token (parser
->lexer
);
25345 /* Create a DEFAULT_ARG to represent the unparsed default
25347 default_argument
= make_node (DEFAULT_ARG
);
25348 DEFARG_TOKENS (default_argument
)
25349 = cp_token_cache_new (first_token
, token
);
25350 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25352 return default_argument
;
25355 /* Begin parsing tentatively. We always save tokens while parsing
25356 tentatively so that if the tentative parsing fails we can restore the
25360 cp_parser_parse_tentatively (cp_parser
* parser
)
25362 /* Enter a new parsing context. */
25363 parser
->context
= cp_parser_context_new (parser
->context
);
25364 /* Begin saving tokens. */
25365 cp_lexer_save_tokens (parser
->lexer
);
25366 /* In order to avoid repetitive access control error messages,
25367 access checks are queued up until we are no longer parsing
25369 push_deferring_access_checks (dk_deferred
);
25372 /* Commit to the currently active tentative parse. */
25375 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25377 cp_parser_context
*context
;
25380 /* Mark all of the levels as committed. */
25381 lexer
= parser
->lexer
;
25382 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25384 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25386 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25387 while (!cp_lexer_saving_tokens (lexer
))
25388 lexer
= lexer
->next
;
25389 cp_lexer_commit_tokens (lexer
);
25393 /* Commit to the topmost currently active tentative parse.
25395 Note that this function shouldn't be called when there are
25396 irreversible side-effects while in a tentative state. For
25397 example, we shouldn't create a permanent entry in the symbol
25398 table, or issue an error message that might not apply if the
25399 tentative parse is aborted. */
25402 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25404 cp_parser_context
*context
= parser
->context
;
25405 cp_lexer
*lexer
= parser
->lexer
;
25409 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25411 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25413 while (!cp_lexer_saving_tokens (lexer
))
25414 lexer
= lexer
->next
;
25415 cp_lexer_commit_tokens (lexer
);
25419 /* Abort the currently active tentative parse. All consumed tokens
25420 will be rolled back, and no diagnostics will be issued. */
25423 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25425 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25426 || errorcount
> 0);
25427 cp_parser_simulate_error (parser
);
25428 /* Now, pretend that we want to see if the construct was
25429 successfully parsed. */
25430 cp_parser_parse_definitely (parser
);
25433 /* Stop parsing tentatively. If a parse error has occurred, restore the
25434 token stream. Otherwise, commit to the tokens we have consumed.
25435 Returns true if no error occurred; false otherwise. */
25438 cp_parser_parse_definitely (cp_parser
* parser
)
25440 bool error_occurred
;
25441 cp_parser_context
*context
;
25443 /* Remember whether or not an error occurred, since we are about to
25444 destroy that information. */
25445 error_occurred
= cp_parser_error_occurred (parser
);
25446 /* Remove the topmost context from the stack. */
25447 context
= parser
->context
;
25448 parser
->context
= context
->next
;
25449 /* If no parse errors occurred, commit to the tentative parse. */
25450 if (!error_occurred
)
25452 /* Commit to the tokens read tentatively, unless that was
25454 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25455 cp_lexer_commit_tokens (parser
->lexer
);
25457 pop_to_parent_deferring_access_checks ();
25459 /* Otherwise, if errors occurred, roll back our state so that things
25460 are just as they were before we began the tentative parse. */
25463 cp_lexer_rollback_tokens (parser
->lexer
);
25464 pop_deferring_access_checks ();
25466 /* Add the context to the front of the free list. */
25467 context
->next
= cp_parser_context_free_list
;
25468 cp_parser_context_free_list
= context
;
25470 return !error_occurred
;
25473 /* Returns true if we are parsing tentatively and are not committed to
25474 this tentative parse. */
25477 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25479 return (cp_parser_parsing_tentatively (parser
)
25480 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25483 /* Returns nonzero iff an error has occurred during the most recent
25484 tentative parse. */
25487 cp_parser_error_occurred (cp_parser
* parser
)
25489 return (cp_parser_parsing_tentatively (parser
)
25490 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25493 /* Returns nonzero if GNU extensions are allowed. */
25496 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25498 return parser
->allow_gnu_extensions_p
;
25501 /* Objective-C++ Productions */
25504 /* Parse an Objective-C expression, which feeds into a primary-expression
25508 objc-message-expression
25509 objc-string-literal
25510 objc-encode-expression
25511 objc-protocol-expression
25512 objc-selector-expression
25514 Returns a tree representation of the expression. */
25517 cp_parser_objc_expression (cp_parser
* parser
)
25519 /* Try to figure out what kind of declaration is present. */
25520 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25524 case CPP_OPEN_SQUARE
:
25525 return cp_parser_objc_message_expression (parser
);
25527 case CPP_OBJC_STRING
:
25528 kwd
= cp_lexer_consume_token (parser
->lexer
);
25529 return objc_build_string_object (kwd
->u
.value
);
25532 switch (kwd
->keyword
)
25534 case RID_AT_ENCODE
:
25535 return cp_parser_objc_encode_expression (parser
);
25537 case RID_AT_PROTOCOL
:
25538 return cp_parser_objc_protocol_expression (parser
);
25540 case RID_AT_SELECTOR
:
25541 return cp_parser_objc_selector_expression (parser
);
25547 error_at (kwd
->location
,
25548 "misplaced %<@%D%> Objective-C++ construct",
25550 cp_parser_skip_to_end_of_block_or_statement (parser
);
25553 return error_mark_node
;
25556 /* Parse an Objective-C message expression.
25558 objc-message-expression:
25559 [ objc-message-receiver objc-message-args ]
25561 Returns a representation of an Objective-C message. */
25564 cp_parser_objc_message_expression (cp_parser
* parser
)
25566 tree receiver
, messageargs
;
25568 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25569 receiver
= cp_parser_objc_message_receiver (parser
);
25570 messageargs
= cp_parser_objc_message_args (parser
);
25571 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25573 return objc_build_message_expr (receiver
, messageargs
);
25576 /* Parse an objc-message-receiver.
25578 objc-message-receiver:
25580 simple-type-specifier
25582 Returns a representation of the type or expression. */
25585 cp_parser_objc_message_receiver (cp_parser
* parser
)
25589 /* An Objective-C message receiver may be either (1) a type
25590 or (2) an expression. */
25591 cp_parser_parse_tentatively (parser
);
25592 rcv
= cp_parser_expression (parser
);
25594 if (cp_parser_parse_definitely (parser
))
25597 rcv
= cp_parser_simple_type_specifier (parser
,
25598 /*decl_specs=*/NULL
,
25599 CP_PARSER_FLAGS_NONE
);
25601 return objc_get_class_reference (rcv
);
25604 /* Parse the arguments and selectors comprising an Objective-C message.
25609 objc-selector-args , objc-comma-args
25611 objc-selector-args:
25612 objc-selector [opt] : assignment-expression
25613 objc-selector-args objc-selector [opt] : assignment-expression
25616 assignment-expression
25617 objc-comma-args , assignment-expression
25619 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25620 selector arguments and TREE_VALUE containing a list of comma
25624 cp_parser_objc_message_args (cp_parser
* parser
)
25626 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25627 bool maybe_unary_selector_p
= true;
25628 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25630 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25632 tree selector
= NULL_TREE
, arg
;
25634 if (token
->type
!= CPP_COLON
)
25635 selector
= cp_parser_objc_selector (parser
);
25637 /* Detect if we have a unary selector. */
25638 if (maybe_unary_selector_p
25639 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25640 return build_tree_list (selector
, NULL_TREE
);
25642 maybe_unary_selector_p
= false;
25643 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25644 arg
= cp_parser_assignment_expression (parser
);
25647 = chainon (sel_args
,
25648 build_tree_list (selector
, arg
));
25650 token
= cp_lexer_peek_token (parser
->lexer
);
25653 /* Handle non-selector arguments, if any. */
25654 while (token
->type
== CPP_COMMA
)
25658 cp_lexer_consume_token (parser
->lexer
);
25659 arg
= cp_parser_assignment_expression (parser
);
25662 = chainon (addl_args
,
25663 build_tree_list (NULL_TREE
, arg
));
25665 token
= cp_lexer_peek_token (parser
->lexer
);
25668 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25670 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25671 return build_tree_list (error_mark_node
, error_mark_node
);
25674 return build_tree_list (sel_args
, addl_args
);
25677 /* Parse an Objective-C encode expression.
25679 objc-encode-expression:
25680 @encode objc-typename
25682 Returns an encoded representation of the type argument. */
25685 cp_parser_objc_encode_expression (cp_parser
* parser
)
25690 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25691 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25692 token
= cp_lexer_peek_token (parser
->lexer
);
25693 type
= complete_type (cp_parser_type_id (parser
));
25694 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25698 error_at (token
->location
,
25699 "%<@encode%> must specify a type as an argument");
25700 return error_mark_node
;
25703 /* This happens if we find @encode(T) (where T is a template
25704 typename or something dependent on a template typename) when
25705 parsing a template. In that case, we can't compile it
25706 immediately, but we rather create an AT_ENCODE_EXPR which will
25707 need to be instantiated when the template is used.
25709 if (dependent_type_p (type
))
25711 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25712 TREE_READONLY (value
) = 1;
25716 return objc_build_encode_expr (type
);
25719 /* Parse an Objective-C @defs expression. */
25722 cp_parser_objc_defs_expression (cp_parser
*parser
)
25726 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25727 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25728 name
= cp_parser_identifier (parser
);
25729 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25731 return objc_get_class_ivars (name
);
25734 /* Parse an Objective-C protocol expression.
25736 objc-protocol-expression:
25737 @protocol ( identifier )
25739 Returns a representation of the protocol expression. */
25742 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25746 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25747 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25748 proto
= cp_parser_identifier (parser
);
25749 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25751 return objc_build_protocol_expr (proto
);
25754 /* Parse an Objective-C selector expression.
25756 objc-selector-expression:
25757 @selector ( objc-method-signature )
25759 objc-method-signature:
25765 objc-selector-seq objc-selector :
25767 Returns a representation of the method selector. */
25770 cp_parser_objc_selector_expression (cp_parser
* parser
)
25772 tree sel_seq
= NULL_TREE
;
25773 bool maybe_unary_selector_p
= true;
25775 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25777 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25778 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25779 token
= cp_lexer_peek_token (parser
->lexer
);
25781 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25782 || token
->type
== CPP_SCOPE
)
25784 tree selector
= NULL_TREE
;
25786 if (token
->type
!= CPP_COLON
25787 || token
->type
== CPP_SCOPE
)
25788 selector
= cp_parser_objc_selector (parser
);
25790 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25791 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25793 /* Detect if we have a unary selector. */
25794 if (maybe_unary_selector_p
)
25796 sel_seq
= selector
;
25797 goto finish_selector
;
25801 cp_parser_error (parser
, "expected %<:%>");
25804 maybe_unary_selector_p
= false;
25805 token
= cp_lexer_consume_token (parser
->lexer
);
25807 if (token
->type
== CPP_SCOPE
)
25810 = chainon (sel_seq
,
25811 build_tree_list (selector
, NULL_TREE
));
25813 = chainon (sel_seq
,
25814 build_tree_list (NULL_TREE
, NULL_TREE
));
25818 = chainon (sel_seq
,
25819 build_tree_list (selector
, NULL_TREE
));
25821 token
= cp_lexer_peek_token (parser
->lexer
);
25825 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25827 return objc_build_selector_expr (loc
, sel_seq
);
25830 /* Parse a list of identifiers.
25832 objc-identifier-list:
25834 objc-identifier-list , identifier
25836 Returns a TREE_LIST of identifier nodes. */
25839 cp_parser_objc_identifier_list (cp_parser
* parser
)
25845 identifier
= cp_parser_identifier (parser
);
25846 if (identifier
== error_mark_node
)
25847 return error_mark_node
;
25849 list
= build_tree_list (NULL_TREE
, identifier
);
25850 sep
= cp_lexer_peek_token (parser
->lexer
);
25852 while (sep
->type
== CPP_COMMA
)
25854 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25855 identifier
= cp_parser_identifier (parser
);
25856 if (identifier
== error_mark_node
)
25859 list
= chainon (list
, build_tree_list (NULL_TREE
,
25861 sep
= cp_lexer_peek_token (parser
->lexer
);
25867 /* Parse an Objective-C alias declaration.
25869 objc-alias-declaration:
25870 @compatibility_alias identifier identifier ;
25872 This function registers the alias mapping with the Objective-C front end.
25873 It returns nothing. */
25876 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25880 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25881 alias
= cp_parser_identifier (parser
);
25882 orig
= cp_parser_identifier (parser
);
25883 objc_declare_alias (alias
, orig
);
25884 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25887 /* Parse an Objective-C class forward-declaration.
25889 objc-class-declaration:
25890 @class objc-identifier-list ;
25892 The function registers the forward declarations with the Objective-C
25893 front end. It returns nothing. */
25896 cp_parser_objc_class_declaration (cp_parser
* parser
)
25898 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
25903 id
= cp_parser_identifier (parser
);
25904 if (id
== error_mark_node
)
25907 objc_declare_class (id
);
25909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25910 cp_lexer_consume_token (parser
->lexer
);
25914 cp_parser_consume_semicolon_at_end_of_statement (parser
);
25917 /* Parse a list of Objective-C protocol references.
25919 objc-protocol-refs-opt:
25920 objc-protocol-refs [opt]
25922 objc-protocol-refs:
25923 < objc-identifier-list >
25925 Returns a TREE_LIST of identifiers, if any. */
25928 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
25930 tree protorefs
= NULL_TREE
;
25932 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
25934 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
25935 protorefs
= cp_parser_objc_identifier_list (parser
);
25936 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
25942 /* Parse a Objective-C visibility specification. */
25945 cp_parser_objc_visibility_spec (cp_parser
* parser
)
25947 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
25949 switch (vis
->keyword
)
25951 case RID_AT_PRIVATE
:
25952 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
25954 case RID_AT_PROTECTED
:
25955 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
25957 case RID_AT_PUBLIC
:
25958 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
25960 case RID_AT_PACKAGE
:
25961 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
25967 /* Eat '@private'/'@protected'/'@public'. */
25968 cp_lexer_consume_token (parser
->lexer
);
25971 /* Parse an Objective-C method type. Return 'true' if it is a class
25972 (+) method, and 'false' if it is an instance (-) method. */
25975 cp_parser_objc_method_type (cp_parser
* parser
)
25977 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
25983 /* Parse an Objective-C protocol qualifier. */
25986 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
25988 tree quals
= NULL_TREE
, node
;
25989 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25991 node
= token
->u
.value
;
25993 while (node
&& identifier_p (node
)
25994 && (node
== ridpointers
[(int) RID_IN
]
25995 || node
== ridpointers
[(int) RID_OUT
]
25996 || node
== ridpointers
[(int) RID_INOUT
]
25997 || node
== ridpointers
[(int) RID_BYCOPY
]
25998 || node
== ridpointers
[(int) RID_BYREF
]
25999 || node
== ridpointers
[(int) RID_ONEWAY
]))
26001 quals
= tree_cons (NULL_TREE
, node
, quals
);
26002 cp_lexer_consume_token (parser
->lexer
);
26003 token
= cp_lexer_peek_token (parser
->lexer
);
26004 node
= token
->u
.value
;
26010 /* Parse an Objective-C typename. */
26013 cp_parser_objc_typename (cp_parser
* parser
)
26015 tree type_name
= NULL_TREE
;
26017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26019 tree proto_quals
, cp_type
= NULL_TREE
;
26021 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26022 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26024 /* An ObjC type name may consist of just protocol qualifiers, in which
26025 case the type shall default to 'id'. */
26026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26028 cp_type
= cp_parser_type_id (parser
);
26030 /* If the type could not be parsed, an error has already
26031 been produced. For error recovery, behave as if it had
26032 not been specified, which will use the default type
26034 if (cp_type
== error_mark_node
)
26036 cp_type
= NULL_TREE
;
26037 /* We need to skip to the closing parenthesis as
26038 cp_parser_type_id() does not seem to do it for
26040 cp_parser_skip_to_closing_parenthesis (parser
,
26041 /*recovering=*/true,
26042 /*or_comma=*/false,
26043 /*consume_paren=*/false);
26047 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26048 type_name
= build_tree_list (proto_quals
, cp_type
);
26054 /* Check to see if TYPE refers to an Objective-C selector name. */
26057 cp_parser_objc_selector_p (enum cpp_ttype type
)
26059 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26060 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26061 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26062 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26063 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26066 /* Parse an Objective-C selector. */
26069 cp_parser_objc_selector (cp_parser
* parser
)
26071 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26073 if (!cp_parser_objc_selector_p (token
->type
))
26075 error_at (token
->location
, "invalid Objective-C++ selector name");
26076 return error_mark_node
;
26079 /* C++ operator names are allowed to appear in ObjC selectors. */
26080 switch (token
->type
)
26082 case CPP_AND_AND
: return get_identifier ("and");
26083 case CPP_AND_EQ
: return get_identifier ("and_eq");
26084 case CPP_AND
: return get_identifier ("bitand");
26085 case CPP_OR
: return get_identifier ("bitor");
26086 case CPP_COMPL
: return get_identifier ("compl");
26087 case CPP_NOT
: return get_identifier ("not");
26088 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26089 case CPP_OR_OR
: return get_identifier ("or");
26090 case CPP_OR_EQ
: return get_identifier ("or_eq");
26091 case CPP_XOR
: return get_identifier ("xor");
26092 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26093 default: return token
->u
.value
;
26097 /* Parse an Objective-C params list. */
26100 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26102 tree params
= NULL_TREE
;
26103 bool maybe_unary_selector_p
= true;
26104 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26106 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26108 tree selector
= NULL_TREE
, type_name
, identifier
;
26109 tree parm_attr
= NULL_TREE
;
26111 if (token
->keyword
== RID_ATTRIBUTE
)
26114 if (token
->type
!= CPP_COLON
)
26115 selector
= cp_parser_objc_selector (parser
);
26117 /* Detect if we have a unary selector. */
26118 if (maybe_unary_selector_p
26119 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26121 params
= selector
; /* Might be followed by attributes. */
26125 maybe_unary_selector_p
= false;
26126 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26128 /* Something went quite wrong. There should be a colon
26129 here, but there is not. Stop parsing parameters. */
26132 type_name
= cp_parser_objc_typename (parser
);
26133 /* New ObjC allows attributes on parameters too. */
26134 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26135 parm_attr
= cp_parser_attributes_opt (parser
);
26136 identifier
= cp_parser_identifier (parser
);
26140 objc_build_keyword_decl (selector
,
26145 token
= cp_lexer_peek_token (parser
->lexer
);
26148 if (params
== NULL_TREE
)
26150 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26151 return error_mark_node
;
26154 /* We allow tail attributes for the method. */
26155 if (token
->keyword
== RID_ATTRIBUTE
)
26157 *attributes
= cp_parser_attributes_opt (parser
);
26158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26159 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26161 cp_parser_error (parser
,
26162 "method attributes must be specified at the end");
26163 return error_mark_node
;
26166 if (params
== NULL_TREE
)
26168 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26169 return error_mark_node
;
26174 /* Parse the non-keyword Objective-C params. */
26177 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26180 tree params
= make_node (TREE_LIST
);
26181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26182 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26184 while (token
->type
== CPP_COMMA
)
26186 cp_parameter_declarator
*parmdecl
;
26189 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26190 token
= cp_lexer_peek_token (parser
->lexer
);
26192 if (token
->type
== CPP_ELLIPSIS
)
26194 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26196 token
= cp_lexer_peek_token (parser
->lexer
);
26200 /* TODO: parse attributes for tail parameters. */
26201 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26202 parm
= grokdeclarator (parmdecl
->declarator
,
26203 &parmdecl
->decl_specifiers
,
26204 PARM
, /*initialized=*/0,
26205 /*attrlist=*/NULL
);
26207 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26208 token
= cp_lexer_peek_token (parser
->lexer
);
26211 /* We allow tail attributes for the method. */
26212 if (token
->keyword
== RID_ATTRIBUTE
)
26214 if (*attributes
== NULL_TREE
)
26216 *attributes
= cp_parser_attributes_opt (parser
);
26217 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26218 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26222 /* We have an error, but parse the attributes, so that we can
26224 *attributes
= cp_parser_attributes_opt (parser
);
26226 cp_parser_error (parser
,
26227 "method attributes must be specified at the end");
26228 return error_mark_node
;
26234 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26237 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26239 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26241 /* If the next token is `extern' and the following token is a string
26242 literal, then we have a linkage specification. */
26243 if (token
->keyword
== RID_EXTERN
26244 && cp_parser_is_pure_string_literal
26245 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26246 cp_parser_linkage_specification (parser
);
26247 /* Handle #pragma, if any. */
26248 else if (token
->type
== CPP_PRAGMA
)
26249 cp_parser_pragma (parser
, pragma_objc_icode
);
26250 /* Allow stray semicolons. */
26251 else if (token
->type
== CPP_SEMICOLON
)
26252 cp_lexer_consume_token (parser
->lexer
);
26253 /* Mark methods as optional or required, when building protocols. */
26254 else if (token
->keyword
== RID_AT_OPTIONAL
)
26256 cp_lexer_consume_token (parser
->lexer
);
26257 objc_set_method_opt (true);
26259 else if (token
->keyword
== RID_AT_REQUIRED
)
26261 cp_lexer_consume_token (parser
->lexer
);
26262 objc_set_method_opt (false);
26264 else if (token
->keyword
== RID_NAMESPACE
)
26265 cp_parser_namespace_definition (parser
);
26266 /* Other stray characters must generate errors. */
26267 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26269 cp_lexer_consume_token (parser
->lexer
);
26270 error ("stray %qs between Objective-C++ methods",
26271 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26273 /* Finally, try to parse a block-declaration, or a function-definition. */
26275 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26278 /* Parse a method signature. */
26281 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26283 tree rettype
, kwdparms
, optparms
;
26284 bool ellipsis
= false;
26285 bool is_class_method
;
26287 is_class_method
= cp_parser_objc_method_type (parser
);
26288 rettype
= cp_parser_objc_typename (parser
);
26289 *attributes
= NULL_TREE
;
26290 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26291 if (kwdparms
== error_mark_node
)
26292 return error_mark_node
;
26293 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26294 if (optparms
== error_mark_node
)
26295 return error_mark_node
;
26297 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26301 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26304 cp_lexer_save_tokens (parser
->lexer
);
26305 tattr
= cp_parser_attributes_opt (parser
);
26306 gcc_assert (tattr
) ;
26308 /* If the attributes are followed by a method introducer, this is not allowed.
26309 Dump the attributes and flag the situation. */
26310 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26311 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26314 /* Otherwise, the attributes introduce some interstitial code, possibly so
26315 rewind to allow that check. */
26316 cp_lexer_rollback_tokens (parser
->lexer
);
26320 /* Parse an Objective-C method prototype list. */
26323 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26325 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26327 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26329 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26331 tree attributes
, sig
;
26332 bool is_class_method
;
26333 if (token
->type
== CPP_PLUS
)
26334 is_class_method
= true;
26336 is_class_method
= false;
26337 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26338 if (sig
== error_mark_node
)
26340 cp_parser_skip_to_end_of_block_or_statement (parser
);
26341 token
= cp_lexer_peek_token (parser
->lexer
);
26344 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26345 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26347 else if (token
->keyword
== RID_AT_PROPERTY
)
26348 cp_parser_objc_at_property_declaration (parser
);
26349 else if (token
->keyword
== RID_ATTRIBUTE
26350 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26351 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26353 "prefix attributes are ignored for methods");
26355 /* Allow for interspersed non-ObjC++ code. */
26356 cp_parser_objc_interstitial_code (parser
);
26358 token
= cp_lexer_peek_token (parser
->lexer
);
26361 if (token
->type
!= CPP_EOF
)
26362 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26364 cp_parser_error (parser
, "expected %<@end%>");
26366 objc_finish_interface ();
26369 /* Parse an Objective-C method definition list. */
26372 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26374 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26376 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26380 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26383 tree sig
, attribute
;
26384 bool is_class_method
;
26385 if (token
->type
== CPP_PLUS
)
26386 is_class_method
= true;
26388 is_class_method
= false;
26389 push_deferring_access_checks (dk_deferred
);
26390 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26391 if (sig
== error_mark_node
)
26393 cp_parser_skip_to_end_of_block_or_statement (parser
);
26394 token
= cp_lexer_peek_token (parser
->lexer
);
26397 objc_start_method_definition (is_class_method
, sig
, attribute
,
26400 /* For historical reasons, we accept an optional semicolon. */
26401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26402 cp_lexer_consume_token (parser
->lexer
);
26404 ptk
= cp_lexer_peek_token (parser
->lexer
);
26405 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26406 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26408 perform_deferred_access_checks (tf_warning_or_error
);
26409 stop_deferring_access_checks ();
26410 meth
= cp_parser_function_definition_after_declarator (parser
,
26412 pop_deferring_access_checks ();
26413 objc_finish_method_definition (meth
);
26416 /* The following case will be removed once @synthesize is
26417 completely implemented. */
26418 else if (token
->keyword
== RID_AT_PROPERTY
)
26419 cp_parser_objc_at_property_declaration (parser
);
26420 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26421 cp_parser_objc_at_synthesize_declaration (parser
);
26422 else if (token
->keyword
== RID_AT_DYNAMIC
)
26423 cp_parser_objc_at_dynamic_declaration (parser
);
26424 else if (token
->keyword
== RID_ATTRIBUTE
26425 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26426 warning_at (token
->location
, OPT_Wattributes
,
26427 "prefix attributes are ignored for methods");
26429 /* Allow for interspersed non-ObjC++ code. */
26430 cp_parser_objc_interstitial_code (parser
);
26432 token
= cp_lexer_peek_token (parser
->lexer
);
26435 if (token
->type
!= CPP_EOF
)
26436 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26438 cp_parser_error (parser
, "expected %<@end%>");
26440 objc_finish_implementation ();
26443 /* Parse Objective-C ivars. */
26446 cp_parser_objc_class_ivars (cp_parser
* parser
)
26448 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26450 if (token
->type
!= CPP_OPEN_BRACE
)
26451 return; /* No ivars specified. */
26453 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26454 token
= cp_lexer_peek_token (parser
->lexer
);
26456 while (token
->type
!= CPP_CLOSE_BRACE
26457 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26459 cp_decl_specifier_seq declspecs
;
26460 int decl_class_or_enum_p
;
26461 tree prefix_attributes
;
26463 cp_parser_objc_visibility_spec (parser
);
26465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26468 cp_parser_decl_specifier_seq (parser
,
26469 CP_PARSER_FLAGS_OPTIONAL
,
26471 &decl_class_or_enum_p
);
26473 /* auto, register, static, extern, mutable. */
26474 if (declspecs
.storage_class
!= sc_none
)
26476 cp_parser_error (parser
, "invalid type for instance variable");
26477 declspecs
.storage_class
= sc_none
;
26480 /* thread_local. */
26481 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26483 cp_parser_error (parser
, "invalid type for instance variable");
26484 declspecs
.locations
[ds_thread
] = 0;
26488 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26490 cp_parser_error (parser
, "invalid type for instance variable");
26491 declspecs
.locations
[ds_typedef
] = 0;
26494 prefix_attributes
= declspecs
.attributes
;
26495 declspecs
.attributes
= NULL_TREE
;
26497 /* Keep going until we hit the `;' at the end of the
26499 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26501 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26502 cp_declarator
*declarator
= NULL
;
26503 int ctor_dtor_or_conv_p
;
26505 /* Check for a (possibly unnamed) bitfield declaration. */
26506 token
= cp_lexer_peek_token (parser
->lexer
);
26507 if (token
->type
== CPP_COLON
)
26510 if (token
->type
== CPP_NAME
26511 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26514 /* Get the name of the bitfield. */
26515 declarator
= make_id_declarator (NULL_TREE
,
26516 cp_parser_identifier (parser
),
26520 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26521 /* Get the width of the bitfield. */
26523 = cp_parser_constant_expression (parser
);
26527 /* Parse the declarator. */
26529 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26530 &ctor_dtor_or_conv_p
,
26531 /*parenthesized_p=*/NULL
,
26532 /*member_p=*/false,
26533 /*friend_p=*/false);
26536 /* Look for attributes that apply to the ivar. */
26537 attributes
= cp_parser_attributes_opt (parser
);
26538 /* Remember which attributes are prefix attributes and
26540 first_attribute
= attributes
;
26541 /* Combine the attributes. */
26542 attributes
= chainon (prefix_attributes
, attributes
);
26545 /* Create the bitfield declaration. */
26546 decl
= grokbitfield (declarator
, &declspecs
,
26550 decl
= grokfield (declarator
, &declspecs
,
26551 NULL_TREE
, /*init_const_expr_p=*/false,
26552 NULL_TREE
, attributes
);
26554 /* Add the instance variable. */
26555 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26556 objc_add_instance_variable (decl
);
26558 /* Reset PREFIX_ATTRIBUTES. */
26559 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26560 attributes
= TREE_CHAIN (attributes
);
26562 TREE_CHAIN (attributes
) = NULL_TREE
;
26564 token
= cp_lexer_peek_token (parser
->lexer
);
26566 if (token
->type
== CPP_COMMA
)
26568 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26574 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26575 token
= cp_lexer_peek_token (parser
->lexer
);
26578 if (token
->keyword
== RID_AT_END
)
26579 cp_parser_error (parser
, "expected %<}%>");
26581 /* Do not consume the RID_AT_END, so it will be read again as terminating
26582 the @interface of @implementation. */
26583 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26584 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26586 /* For historical reasons, we accept an optional semicolon. */
26587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26588 cp_lexer_consume_token (parser
->lexer
);
26591 /* Parse an Objective-C protocol declaration. */
26594 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26596 tree proto
, protorefs
;
26599 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26602 tok
= cp_lexer_peek_token (parser
->lexer
);
26603 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26604 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26608 /* See if we have a forward declaration or a definition. */
26609 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26611 /* Try a forward declaration first. */
26612 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26618 id
= cp_parser_identifier (parser
);
26619 if (id
== error_mark_node
)
26622 objc_declare_protocol (id
, attributes
);
26624 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26625 cp_lexer_consume_token (parser
->lexer
);
26629 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26632 /* Ok, we got a full-fledged definition (or at least should). */
26635 proto
= cp_parser_identifier (parser
);
26636 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26637 objc_start_protocol (proto
, protorefs
, attributes
);
26638 cp_parser_objc_method_prototype_list (parser
);
26642 /* Parse an Objective-C superclass or category. */
26645 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26648 tree
*categ
, bool *is_class_extension
)
26650 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26652 *super
= *categ
= NULL_TREE
;
26653 *is_class_extension
= false;
26654 if (next
->type
== CPP_COLON
)
26656 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26657 *super
= cp_parser_identifier (parser
);
26659 else if (next
->type
== CPP_OPEN_PAREN
)
26661 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26663 /* If there is no category name, and this is an @interface, we
26664 have a class extension. */
26665 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26667 *categ
= NULL_TREE
;
26668 *is_class_extension
= true;
26671 *categ
= cp_parser_identifier (parser
);
26673 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26677 /* Parse an Objective-C class interface. */
26680 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26682 tree name
, super
, categ
, protos
;
26683 bool is_class_extension
;
26685 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26686 name
= cp_parser_identifier (parser
);
26687 if (name
== error_mark_node
)
26689 /* It's hard to recover because even if valid @interface stuff
26690 is to follow, we can't compile it (or validate it) if we
26691 don't even know which class it refers to. Let's assume this
26692 was a stray '@interface' token in the stream and skip it.
26696 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26697 &is_class_extension
);
26698 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26700 /* We have either a class or a category on our hands. */
26701 if (categ
|| is_class_extension
)
26702 objc_start_category_interface (name
, categ
, protos
, attributes
);
26705 objc_start_class_interface (name
, super
, protos
, attributes
);
26706 /* Handle instance variable declarations, if any. */
26707 cp_parser_objc_class_ivars (parser
);
26708 objc_continue_interface ();
26711 cp_parser_objc_method_prototype_list (parser
);
26714 /* Parse an Objective-C class implementation. */
26717 cp_parser_objc_class_implementation (cp_parser
* parser
)
26719 tree name
, super
, categ
;
26720 bool is_class_extension
;
26722 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26723 name
= cp_parser_identifier (parser
);
26724 if (name
== error_mark_node
)
26726 /* It's hard to recover because even if valid @implementation
26727 stuff is to follow, we can't compile it (or validate it) if
26728 we don't even know which class it refers to. Let's assume
26729 this was a stray '@implementation' token in the stream and
26734 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26735 &is_class_extension
);
26737 /* We have either a class or a category on our hands. */
26739 objc_start_category_implementation (name
, categ
);
26742 objc_start_class_implementation (name
, super
);
26743 /* Handle instance variable declarations, if any. */
26744 cp_parser_objc_class_ivars (parser
);
26745 objc_continue_implementation ();
26748 cp_parser_objc_method_definition_list (parser
);
26751 /* Consume the @end token and finish off the implementation. */
26754 cp_parser_objc_end_implementation (cp_parser
* parser
)
26756 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26757 objc_finish_implementation ();
26760 /* Parse an Objective-C declaration. */
26763 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26765 /* Try to figure out what kind of declaration is present. */
26766 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26769 switch (kwd
->keyword
)
26774 error_at (kwd
->location
, "attributes may not be specified before"
26775 " the %<@%D%> Objective-C++ keyword",
26779 case RID_AT_IMPLEMENTATION
:
26780 warning_at (kwd
->location
, OPT_Wattributes
,
26781 "prefix attributes are ignored before %<@%D%>",
26788 switch (kwd
->keyword
)
26791 cp_parser_objc_alias_declaration (parser
);
26794 cp_parser_objc_class_declaration (parser
);
26796 case RID_AT_PROTOCOL
:
26797 cp_parser_objc_protocol_declaration (parser
, attributes
);
26799 case RID_AT_INTERFACE
:
26800 cp_parser_objc_class_interface (parser
, attributes
);
26802 case RID_AT_IMPLEMENTATION
:
26803 cp_parser_objc_class_implementation (parser
);
26806 cp_parser_objc_end_implementation (parser
);
26809 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26811 cp_parser_skip_to_end_of_block_or_statement (parser
);
26815 /* Parse an Objective-C try-catch-finally statement.
26817 objc-try-catch-finally-stmt:
26818 @try compound-statement objc-catch-clause-seq [opt]
26819 objc-finally-clause [opt]
26821 objc-catch-clause-seq:
26822 objc-catch-clause objc-catch-clause-seq [opt]
26825 @catch ( objc-exception-declaration ) compound-statement
26827 objc-finally-clause:
26828 @finally compound-statement
26830 objc-exception-declaration:
26831 parameter-declaration
26834 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26838 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26839 for C. Keep them in sync. */
26842 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26844 location_t location
;
26847 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26848 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26849 objc_maybe_warn_exceptions (location
);
26850 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26851 node, lest it get absorbed into the surrounding block. */
26852 stmt
= push_stmt_list ();
26853 cp_parser_compound_statement (parser
, NULL
, false, false);
26854 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26856 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26858 cp_parameter_declarator
*parm
;
26859 tree parameter_declaration
= error_mark_node
;
26860 bool seen_open_paren
= false;
26862 cp_lexer_consume_token (parser
->lexer
);
26863 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26864 seen_open_paren
= true;
26865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26867 /* We have "@catch (...)" (where the '...' are literally
26868 what is in the code). Skip the '...'.
26869 parameter_declaration is set to NULL_TREE, and
26870 objc_being_catch_clauses() knows that that means
26872 cp_lexer_consume_token (parser
->lexer
);
26873 parameter_declaration
= NULL_TREE
;
26877 /* We have "@catch (NSException *exception)" or something
26878 like that. Parse the parameter declaration. */
26879 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26881 parameter_declaration
= error_mark_node
;
26883 parameter_declaration
= grokdeclarator (parm
->declarator
,
26884 &parm
->decl_specifiers
,
26885 PARM
, /*initialized=*/0,
26886 /*attrlist=*/NULL
);
26888 if (seen_open_paren
)
26889 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26892 /* If there was no open parenthesis, we are recovering from
26893 an error, and we are trying to figure out what mistake
26894 the user has made. */
26896 /* If there is an immediate closing parenthesis, the user
26897 probably forgot the opening one (ie, they typed "@catch
26898 NSException *e)". Parse the closing parenthesis and keep
26900 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26901 cp_lexer_consume_token (parser
->lexer
);
26903 /* If these is no immediate closing parenthesis, the user
26904 probably doesn't know that parenthesis are required at
26905 all (ie, they typed "@catch NSException *e"). So, just
26906 forget about the closing parenthesis and keep going. */
26908 objc_begin_catch_clause (parameter_declaration
);
26909 cp_parser_compound_statement (parser
, NULL
, false, false);
26910 objc_finish_catch_clause ();
26912 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
26914 cp_lexer_consume_token (parser
->lexer
);
26915 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26916 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26917 node, lest it get absorbed into the surrounding block. */
26918 stmt
= push_stmt_list ();
26919 cp_parser_compound_statement (parser
, NULL
, false, false);
26920 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
26923 return objc_finish_try_stmt ();
26926 /* Parse an Objective-C synchronized statement.
26928 objc-synchronized-stmt:
26929 @synchronized ( expression ) compound-statement
26931 Returns NULL_TREE. */
26934 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
26936 location_t location
;
26939 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
26941 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26942 objc_maybe_warn_exceptions (location
);
26943 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
26944 lock
= cp_parser_expression (parser
);
26945 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26947 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26948 node, lest it get absorbed into the surrounding block. */
26949 stmt
= push_stmt_list ();
26950 cp_parser_compound_statement (parser
, NULL
, false, false);
26952 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
26955 /* Parse an Objective-C throw statement.
26958 @throw assignment-expression [opt] ;
26960 Returns a constructed '@throw' statement. */
26963 cp_parser_objc_throw_statement (cp_parser
*parser
)
26965 tree expr
= NULL_TREE
;
26966 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26968 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
26970 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26971 expr
= cp_parser_expression (parser
);
26973 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26975 return objc_build_throw_stmt (loc
, expr
);
26978 /* Parse an Objective-C statement. */
26981 cp_parser_objc_statement (cp_parser
* parser
)
26983 /* Try to figure out what kind of declaration is present. */
26984 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26986 switch (kwd
->keyword
)
26989 return cp_parser_objc_try_catch_finally_statement (parser
);
26990 case RID_AT_SYNCHRONIZED
:
26991 return cp_parser_objc_synchronized_statement (parser
);
26993 return cp_parser_objc_throw_statement (parser
);
26995 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26997 cp_parser_skip_to_end_of_block_or_statement (parser
);
27000 return error_mark_node
;
27003 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27004 look ahead to see if an objc keyword follows the attributes. This
27005 is to detect the use of prefix attributes on ObjC @interface and
27009 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27011 cp_lexer_save_tokens (parser
->lexer
);
27012 *attrib
= cp_parser_attributes_opt (parser
);
27013 gcc_assert (*attrib
);
27014 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27016 cp_lexer_commit_tokens (parser
->lexer
);
27019 cp_lexer_rollback_tokens (parser
->lexer
);
27023 /* This routine is a minimal replacement for
27024 c_parser_struct_declaration () used when parsing the list of
27025 types/names or ObjC++ properties. For example, when parsing the
27028 @property (readonly) int a, b, c;
27030 this function is responsible for parsing "int a, int b, int c" and
27031 returning the declarations as CHAIN of DECLs.
27033 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27034 similar parsing. */
27036 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27038 tree decls
= NULL_TREE
;
27039 cp_decl_specifier_seq declspecs
;
27040 int decl_class_or_enum_p
;
27041 tree prefix_attributes
;
27043 cp_parser_decl_specifier_seq (parser
,
27044 CP_PARSER_FLAGS_NONE
,
27046 &decl_class_or_enum_p
);
27048 if (declspecs
.type
== error_mark_node
)
27049 return error_mark_node
;
27051 /* auto, register, static, extern, mutable. */
27052 if (declspecs
.storage_class
!= sc_none
)
27054 cp_parser_error (parser
, "invalid type for property");
27055 declspecs
.storage_class
= sc_none
;
27058 /* thread_local. */
27059 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27061 cp_parser_error (parser
, "invalid type for property");
27062 declspecs
.locations
[ds_thread
] = 0;
27066 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27068 cp_parser_error (parser
, "invalid type for property");
27069 declspecs
.locations
[ds_typedef
] = 0;
27072 prefix_attributes
= declspecs
.attributes
;
27073 declspecs
.attributes
= NULL_TREE
;
27075 /* Keep going until we hit the `;' at the end of the declaration. */
27076 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27078 tree attributes
, first_attribute
, decl
;
27079 cp_declarator
*declarator
;
27082 /* Parse the declarator. */
27083 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27084 NULL
, NULL
, false, false);
27086 /* Look for attributes that apply to the ivar. */
27087 attributes
= cp_parser_attributes_opt (parser
);
27088 /* Remember which attributes are prefix attributes and
27090 first_attribute
= attributes
;
27091 /* Combine the attributes. */
27092 attributes
= chainon (prefix_attributes
, attributes
);
27094 decl
= grokfield (declarator
, &declspecs
,
27095 NULL_TREE
, /*init_const_expr_p=*/false,
27096 NULL_TREE
, attributes
);
27098 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27099 return error_mark_node
;
27101 /* Reset PREFIX_ATTRIBUTES. */
27102 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27103 attributes
= TREE_CHAIN (attributes
);
27105 TREE_CHAIN (attributes
) = NULL_TREE
;
27107 DECL_CHAIN (decl
) = decls
;
27110 token
= cp_lexer_peek_token (parser
->lexer
);
27111 if (token
->type
== CPP_COMMA
)
27113 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27122 /* Parse an Objective-C @property declaration. The syntax is:
27124 objc-property-declaration:
27125 '@property' objc-property-attributes[opt] struct-declaration ;
27127 objc-property-attributes:
27128 '(' objc-property-attribute-list ')'
27130 objc-property-attribute-list:
27131 objc-property-attribute
27132 objc-property-attribute-list, objc-property-attribute
27134 objc-property-attribute
27135 'getter' = identifier
27136 'setter' = identifier
27145 @property NSString *name;
27146 @property (readonly) id object;
27147 @property (retain, nonatomic, getter=getTheName) id name;
27148 @property int a, b, c;
27150 PS: This function is identical to
27151 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27153 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27155 /* The following variables hold the attributes of the properties as
27156 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27157 seen. When we see an attribute, we set them to 'true' (if they
27158 are boolean properties) or to the identifier (if they have an
27159 argument, ie, for getter and setter). Note that here we only
27160 parse the list of attributes, check the syntax and accumulate the
27161 attributes that we find. objc_add_property_declaration() will
27162 then process the information. */
27163 bool property_assign
= false;
27164 bool property_copy
= false;
27165 tree property_getter_ident
= NULL_TREE
;
27166 bool property_nonatomic
= false;
27167 bool property_readonly
= false;
27168 bool property_readwrite
= false;
27169 bool property_retain
= false;
27170 tree property_setter_ident
= NULL_TREE
;
27172 /* 'properties' is the list of properties that we read. Usually a
27173 single one, but maybe more (eg, in "@property int a, b, c;" there
27178 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27180 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27182 /* Parse the optional attribute list... */
27183 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27186 cp_lexer_consume_token (parser
->lexer
);
27190 bool syntax_error
= false;
27191 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27194 if (token
->type
!= CPP_NAME
)
27196 cp_parser_error (parser
, "expected identifier");
27199 keyword
= C_RID_CODE (token
->u
.value
);
27200 cp_lexer_consume_token (parser
->lexer
);
27203 case RID_ASSIGN
: property_assign
= true; break;
27204 case RID_COPY
: property_copy
= true; break;
27205 case RID_NONATOMIC
: property_nonatomic
= true; break;
27206 case RID_READONLY
: property_readonly
= true; break;
27207 case RID_READWRITE
: property_readwrite
= true; break;
27208 case RID_RETAIN
: property_retain
= true; break;
27212 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27214 if (keyword
== RID_GETTER
)
27215 cp_parser_error (parser
,
27216 "missing %<=%> (after %<getter%> attribute)");
27218 cp_parser_error (parser
,
27219 "missing %<=%> (after %<setter%> attribute)");
27220 syntax_error
= true;
27223 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27224 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27226 cp_parser_error (parser
, "expected identifier");
27227 syntax_error
= true;
27230 if (keyword
== RID_SETTER
)
27232 if (property_setter_ident
!= NULL_TREE
)
27234 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27235 cp_lexer_consume_token (parser
->lexer
);
27238 property_setter_ident
= cp_parser_objc_selector (parser
);
27239 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27240 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27242 cp_lexer_consume_token (parser
->lexer
);
27246 if (property_getter_ident
!= NULL_TREE
)
27248 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27249 cp_lexer_consume_token (parser
->lexer
);
27252 property_getter_ident
= cp_parser_objc_selector (parser
);
27256 cp_parser_error (parser
, "unknown property attribute");
27257 syntax_error
= true;
27264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27265 cp_lexer_consume_token (parser
->lexer
);
27270 /* FIXME: "@property (setter, assign);" will generate a spurious
27271 "error: expected ‘)’ before ‘,’ token". This is because
27272 cp_parser_require, unlike the C counterpart, will produce an
27273 error even if we are in error recovery. */
27274 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27276 cp_parser_skip_to_closing_parenthesis (parser
,
27277 /*recovering=*/true,
27278 /*or_comma=*/false,
27279 /*consume_paren=*/true);
27283 /* ... and the property declaration(s). */
27284 properties
= cp_parser_objc_struct_declaration (parser
);
27286 if (properties
== error_mark_node
)
27288 cp_parser_skip_to_end_of_statement (parser
);
27289 /* If the next token is now a `;', consume it. */
27290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27291 cp_lexer_consume_token (parser
->lexer
);
27295 if (properties
== NULL_TREE
)
27296 cp_parser_error (parser
, "expected identifier");
27299 /* Comma-separated properties are chained together in
27300 reverse order; add them one by one. */
27301 properties
= nreverse (properties
);
27303 for (; properties
; properties
= TREE_CHAIN (properties
))
27304 objc_add_property_declaration (loc
, copy_node (properties
),
27305 property_readonly
, property_readwrite
,
27306 property_assign
, property_retain
,
27307 property_copy
, property_nonatomic
,
27308 property_getter_ident
, property_setter_ident
);
27311 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27314 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27316 objc-synthesize-declaration:
27317 @synthesize objc-synthesize-identifier-list ;
27319 objc-synthesize-identifier-list:
27320 objc-synthesize-identifier
27321 objc-synthesize-identifier-list, objc-synthesize-identifier
27323 objc-synthesize-identifier
27325 identifier = identifier
27328 @synthesize MyProperty;
27329 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27331 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27332 for C. Keep them in sync.
27335 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27337 tree list
= NULL_TREE
;
27339 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27341 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27344 tree property
, ivar
;
27345 property
= cp_parser_identifier (parser
);
27346 if (property
== error_mark_node
)
27348 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27351 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27353 cp_lexer_consume_token (parser
->lexer
);
27354 ivar
= cp_parser_identifier (parser
);
27355 if (ivar
== error_mark_node
)
27357 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27363 list
= chainon (list
, build_tree_list (ivar
, property
));
27364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27365 cp_lexer_consume_token (parser
->lexer
);
27369 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27370 objc_add_synthesize_declaration (loc
, list
);
27373 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27375 objc-dynamic-declaration:
27376 @dynamic identifier-list ;
27379 @dynamic MyProperty;
27380 @dynamic MyProperty, AnotherProperty;
27382 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27383 for C. Keep them in sync.
27386 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27388 tree list
= NULL_TREE
;
27390 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27392 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27396 property
= cp_parser_identifier (parser
);
27397 if (property
== error_mark_node
)
27399 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27402 list
= chainon (list
, build_tree_list (NULL
, property
));
27403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27404 cp_lexer_consume_token (parser
->lexer
);
27408 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27409 objc_add_dynamic_declaration (loc
, list
);
27413 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27415 /* Returns name of the next clause.
27416 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27417 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27418 returned and the token is consumed. */
27420 static pragma_omp_clause
27421 cp_parser_omp_clause_name (cp_parser
*parser
)
27423 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27425 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27426 result
= PRAGMA_OMP_CLAUSE_IF
;
27427 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27428 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27429 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27430 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27431 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27432 result
= PRAGMA_OMP_CLAUSE_FOR
;
27433 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27435 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27436 const char *p
= IDENTIFIER_POINTER (id
);
27441 if (!strcmp ("aligned", p
))
27442 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27445 if (!strcmp ("collapse", p
))
27446 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27447 else if (!strcmp ("copyin", p
))
27448 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27449 else if (!strcmp ("copyprivate", p
))
27450 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27453 if (!strcmp ("depend", p
))
27454 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27455 else if (!strcmp ("device", p
))
27456 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27457 else if (!strcmp ("dist_schedule", p
))
27458 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27461 if (!strcmp ("final", p
))
27462 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27463 else if (!strcmp ("firstprivate", p
))
27464 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27465 else if (!strcmp ("from", p
))
27466 result
= PRAGMA_OMP_CLAUSE_FROM
;
27469 if (!strcmp ("inbranch", p
))
27470 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27473 if (!strcmp ("lastprivate", p
))
27474 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27475 else if (!strcmp ("linear", p
))
27476 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27479 if (!strcmp ("map", p
))
27480 result
= PRAGMA_OMP_CLAUSE_MAP
;
27481 else if (!strcmp ("mergeable", p
))
27482 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27483 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27484 result
= PRAGMA_CILK_CLAUSE_MASK
;
27487 if (!strcmp ("notinbranch", p
))
27488 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27489 else if (!strcmp ("nowait", p
))
27490 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27491 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27492 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27493 else if (!strcmp ("num_teams", p
))
27494 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27495 else if (!strcmp ("num_threads", p
))
27496 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27499 if (!strcmp ("ordered", p
))
27500 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27503 if (!strcmp ("parallel", p
))
27504 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27505 else if (!strcmp ("proc_bind", p
))
27506 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27509 if (!strcmp ("reduction", p
))
27510 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27513 if (!strcmp ("safelen", p
))
27514 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27515 else if (!strcmp ("schedule", p
))
27516 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27517 else if (!strcmp ("sections", p
))
27518 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27519 else if (!strcmp ("shared", p
))
27520 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27521 else if (!strcmp ("simdlen", p
))
27522 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27525 if (!strcmp ("taskgroup", p
))
27526 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27527 else if (!strcmp ("thread_limit", p
))
27528 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27529 else if (!strcmp ("to", p
))
27530 result
= PRAGMA_OMP_CLAUSE_TO
;
27533 if (!strcmp ("uniform", p
))
27534 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27535 else if (!strcmp ("untied", p
))
27536 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27539 if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27540 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27545 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27546 cp_lexer_consume_token (parser
->lexer
);
27551 /* Validate that a clause of the given type does not already exist. */
27554 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27555 const char *name
, location_t location
)
27559 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27560 if (OMP_CLAUSE_CODE (c
) == code
)
27562 error_at (location
, "too many %qs clauses", name
);
27570 variable-list , identifier
27572 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27573 colon). An opening parenthesis will have been consumed by the caller.
27575 If KIND is nonzero, create the appropriate node and install the decl
27576 in OMP_CLAUSE_DECL and add the node to the head of the list.
27578 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27579 return the list created.
27581 COLON can be NULL if only closing parenthesis should end the list,
27582 or pointer to bool which will receive false if the list is terminated
27583 by closing parenthesis or true if the list is terminated by colon. */
27586 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27587 tree list
, bool *colon
)
27590 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27593 parser
->colon_corrects_to_scope_p
= false;
27600 token
= cp_lexer_peek_token (parser
->lexer
);
27601 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27602 /*check_dependency_p=*/true,
27603 /*template_p=*/NULL
,
27604 /*declarator_p=*/false,
27605 /*optional_p=*/false);
27606 if (name
== error_mark_node
)
27609 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27610 if (decl
== error_mark_node
)
27611 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27613 else if (kind
!= 0)
27617 case OMP_CLAUSE_MAP
:
27618 case OMP_CLAUSE_FROM
:
27619 case OMP_CLAUSE_TO
:
27620 case OMP_CLAUSE_DEPEND
:
27621 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27623 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27625 parser
->colon_corrects_to_scope_p
= false;
27626 cp_lexer_consume_token (parser
->lexer
);
27627 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27628 low_bound
= cp_parser_expression (parser
);
27630 parser
->colon_corrects_to_scope_p
27631 = saved_colon_corrects_to_scope_p
;
27632 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27633 length
= integer_one_node
;
27636 /* Look for `:'. */
27637 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27639 if (!cp_lexer_next_token_is (parser
->lexer
,
27641 length
= cp_parser_expression (parser
);
27643 /* Look for the closing `]'. */
27644 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27647 decl
= tree_cons (low_bound
, length
, decl
);
27654 tree u
= build_omp_clause (token
->location
, kind
);
27655 OMP_CLAUSE_DECL (u
) = decl
;
27656 OMP_CLAUSE_CHAIN (u
) = list
;
27660 list
= tree_cons (decl
, NULL_TREE
, list
);
27663 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27665 cp_lexer_consume_token (parser
->lexer
);
27669 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27671 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27674 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27678 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27682 /* Try to resync to an unnested comma. Copied from
27683 cp_parser_parenthesized_expression_list. */
27686 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27687 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27688 /*recovering=*/true,
27690 /*consume_paren=*/true);
27698 /* Similarly, but expect leading and trailing parenthesis. This is a very
27699 common case for omp clauses. */
27702 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27704 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27705 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27710 collapse ( constant-expression ) */
27713 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
27719 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27720 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27723 num
= cp_parser_constant_expression (parser
);
27725 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27726 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27727 /*or_comma=*/false,
27728 /*consume_paren=*/true);
27730 if (num
== error_mark_node
)
27732 num
= fold_non_dependent_expr (num
);
27733 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
27734 || !tree_fits_shwi_p (num
)
27735 || (n
= tree_to_shwi (num
)) <= 0
27738 error_at (loc
, "collapse argument needs positive constant integer expression");
27742 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
27743 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
27744 OMP_CLAUSE_CHAIN (c
) = list
;
27745 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
27751 default ( shared | none ) */
27754 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
27756 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
27759 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27763 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27764 const char *p
= IDENTIFIER_POINTER (id
);
27769 if (strcmp ("none", p
) != 0)
27771 kind
= OMP_CLAUSE_DEFAULT_NONE
;
27775 if (strcmp ("shared", p
) != 0)
27777 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
27784 cp_lexer_consume_token (parser
->lexer
);
27789 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
27792 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27793 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27794 /*or_comma=*/false,
27795 /*consume_paren=*/true);
27797 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
27800 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
27801 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
27802 OMP_CLAUSE_CHAIN (c
) = list
;
27803 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
27809 final ( expression ) */
27812 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
27816 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27819 t
= cp_parser_condition (parser
);
27821 if (t
== error_mark_node
27822 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27823 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27824 /*or_comma=*/false,
27825 /*consume_paren=*/true);
27827 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
27829 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
27830 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
27831 OMP_CLAUSE_CHAIN (c
) = list
;
27837 if ( expression ) */
27840 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
27844 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27847 t
= cp_parser_condition (parser
);
27849 if (t
== error_mark_node
27850 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27851 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27852 /*or_comma=*/false,
27853 /*consume_paren=*/true);
27855 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
27857 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
27858 OMP_CLAUSE_IF_EXPR (c
) = t
;
27859 OMP_CLAUSE_CHAIN (c
) = list
;
27868 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
27869 tree list
, location_t location
)
27873 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
27876 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
27877 OMP_CLAUSE_CHAIN (c
) = list
;
27885 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
27886 tree list
, location_t location
)
27890 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
27892 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
27893 OMP_CLAUSE_CHAIN (c
) = list
;
27898 num_threads ( expression ) */
27901 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
27902 location_t location
)
27906 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27909 t
= cp_parser_expression (parser
);
27911 if (t
== error_mark_node
27912 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27913 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27914 /*or_comma=*/false,
27915 /*consume_paren=*/true);
27917 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
27918 "num_threads", location
);
27920 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
27921 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
27922 OMP_CLAUSE_CHAIN (c
) = list
;
27931 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
27932 tree list
, location_t location
)
27936 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
27937 "ordered", location
);
27939 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
27940 OMP_CLAUSE_CHAIN (c
) = list
;
27945 reduction ( reduction-operator : variable-list )
27947 reduction-operator:
27948 One of: + * - & ^ | && ||
27952 reduction-operator:
27953 One of: + * - & ^ | && || min max
27957 reduction-operator:
27958 One of: + * - & ^ | && ||
27962 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
27964 enum tree_code code
= ERROR_MARK
;
27965 tree nlist
, c
, id
= NULL_TREE
;
27967 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27970 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
27972 case CPP_PLUS
: code
= PLUS_EXPR
; break;
27973 case CPP_MULT
: code
= MULT_EXPR
; break;
27974 case CPP_MINUS
: code
= MINUS_EXPR
; break;
27975 case CPP_AND
: code
= BIT_AND_EXPR
; break;
27976 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
27977 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
27978 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
27979 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
27983 if (code
!= ERROR_MARK
)
27984 cp_lexer_consume_token (parser
->lexer
);
27987 bool saved_colon_corrects_to_scope_p
;
27988 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27989 parser
->colon_corrects_to_scope_p
= false;
27990 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
27991 /*check_dependency_p=*/true,
27992 /*template_p=*/NULL
,
27993 /*declarator_p=*/false,
27994 /*optional_p=*/false);
27995 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27996 if (identifier_p (id
))
27998 const char *p
= IDENTIFIER_POINTER (id
);
28000 if (strcmp (p
, "min") == 0)
28002 else if (strcmp (p
, "max") == 0)
28004 else if (id
== ansi_opname (PLUS_EXPR
))
28006 else if (id
== ansi_opname (MULT_EXPR
))
28008 else if (id
== ansi_opname (MINUS_EXPR
))
28010 else if (id
== ansi_opname (BIT_AND_EXPR
))
28011 code
= BIT_AND_EXPR
;
28012 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28013 code
= BIT_IOR_EXPR
;
28014 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28015 code
= BIT_XOR_EXPR
;
28016 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28017 code
= TRUTH_ANDIF_EXPR
;
28018 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28019 code
= TRUTH_ORIF_EXPR
;
28020 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28021 tree scope
= parser
->scope
;
28023 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28024 parser
->scope
= NULL_TREE
;
28025 parser
->qualifying_scope
= NULL_TREE
;
28026 parser
->object_scope
= NULL_TREE
;
28030 error ("invalid reduction-identifier");
28032 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28033 /*or_comma=*/false,
28034 /*consume_paren=*/true);
28039 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28042 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28044 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28046 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28047 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28054 schedule ( schedule-kind )
28055 schedule ( schedule-kind , expression )
28058 static | dynamic | guided | runtime | auto */
28061 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28065 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28068 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28072 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28073 const char *p
= IDENTIFIER_POINTER (id
);
28078 if (strcmp ("dynamic", p
) != 0)
28080 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28084 if (strcmp ("guided", p
) != 0)
28086 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28090 if (strcmp ("runtime", p
) != 0)
28092 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28099 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28100 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28101 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28102 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28105 cp_lexer_consume_token (parser
->lexer
);
28107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28110 cp_lexer_consume_token (parser
->lexer
);
28112 token
= cp_lexer_peek_token (parser
->lexer
);
28113 t
= cp_parser_assignment_expression (parser
);
28115 if (t
== error_mark_node
)
28117 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28118 error_at (token
->location
, "schedule %<runtime%> does not take "
28119 "a %<chunk_size%> parameter");
28120 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28121 error_at (token
->location
, "schedule %<auto%> does not take "
28122 "a %<chunk_size%> parameter");
28124 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28126 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28129 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28132 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28133 OMP_CLAUSE_CHAIN (c
) = list
;
28137 cp_parser_error (parser
, "invalid schedule kind");
28139 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28140 /*or_comma=*/false,
28141 /*consume_paren=*/true);
28149 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28150 tree list
, location_t location
)
28154 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28156 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28157 OMP_CLAUSE_CHAIN (c
) = list
;
28166 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28167 tree list
, location_t location
)
28169 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28170 tree c
= build_omp_clause (location
, code
);
28171 OMP_CLAUSE_CHAIN (c
) = list
;
28182 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28183 enum omp_clause_code code
,
28184 tree list
, location_t location
)
28186 tree c
= build_omp_clause (location
, code
);
28187 OMP_CLAUSE_CHAIN (c
) = list
;
28192 num_teams ( expression ) */
28195 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28196 location_t location
)
28200 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28203 t
= cp_parser_expression (parser
);
28205 if (t
== error_mark_node
28206 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28207 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28208 /*or_comma=*/false,
28209 /*consume_paren=*/true);
28211 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28212 "num_teams", location
);
28214 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28215 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28216 OMP_CLAUSE_CHAIN (c
) = list
;
28222 thread_limit ( expression ) */
28225 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28226 location_t location
)
28230 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28233 t
= cp_parser_expression (parser
);
28235 if (t
== error_mark_node
28236 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28237 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28238 /*or_comma=*/false,
28239 /*consume_paren=*/true);
28241 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28242 "thread_limit", location
);
28244 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28245 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28246 OMP_CLAUSE_CHAIN (c
) = list
;
28252 aligned ( variable-list )
28253 aligned ( variable-list : constant-expression ) */
28256 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28258 tree nlist
, c
, alignment
= NULL_TREE
;
28261 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28264 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28269 alignment
= cp_parser_constant_expression (parser
);
28271 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28272 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28273 /*or_comma=*/false,
28274 /*consume_paren=*/true);
28276 if (alignment
== error_mark_node
)
28277 alignment
= NULL_TREE
;
28280 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28281 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28287 linear ( variable-list )
28288 linear ( variable-list : expression ) */
28291 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28292 bool is_cilk_simd_fn
)
28294 tree nlist
, c
, step
= integer_one_node
;
28297 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28300 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28305 step
= cp_parser_expression (parser
);
28307 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28309 sorry ("using parameters for %<linear%> step is not supported yet");
28310 step
= integer_one_node
;
28312 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28313 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28314 /*or_comma=*/false,
28315 /*consume_paren=*/true);
28317 if (step
== error_mark_node
)
28321 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28322 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28328 safelen ( constant-expression ) */
28331 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28332 location_t location
)
28336 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28339 t
= cp_parser_constant_expression (parser
);
28341 if (t
== error_mark_node
28342 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28343 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28344 /*or_comma=*/false,
28345 /*consume_paren=*/true);
28347 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28349 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28350 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28351 OMP_CLAUSE_CHAIN (c
) = list
;
28357 simdlen ( constant-expression ) */
28360 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28361 location_t location
)
28365 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28368 t
= cp_parser_constant_expression (parser
);
28370 if (t
== error_mark_node
28371 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28372 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28373 /*or_comma=*/false,
28374 /*consume_paren=*/true);
28376 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28378 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28379 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28380 OMP_CLAUSE_CHAIN (c
) = list
;
28386 depend ( depend-kind : variable-list )
28389 in | out | inout */
28392 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28395 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28397 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28402 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28403 const char *p
= IDENTIFIER_POINTER (id
);
28405 if (strcmp ("in", p
) == 0)
28406 kind
= OMP_CLAUSE_DEPEND_IN
;
28407 else if (strcmp ("inout", p
) == 0)
28408 kind
= OMP_CLAUSE_DEPEND_INOUT
;
28409 else if (strcmp ("out", p
) == 0)
28410 kind
= OMP_CLAUSE_DEPEND_OUT
;
28417 cp_lexer_consume_token (parser
->lexer
);
28418 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28421 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
28424 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28425 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
28430 cp_parser_error (parser
, "invalid depend kind");
28432 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28433 /*or_comma=*/false,
28434 /*consume_paren=*/true);
28439 map ( map-kind : variable-list )
28440 map ( variable-list )
28443 alloc | to | from | tofrom */
28446 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28449 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28451 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28455 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28457 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28458 const char *p
= IDENTIFIER_POINTER (id
);
28460 if (strcmp ("alloc", p
) == 0)
28461 kind
= OMP_CLAUSE_MAP_ALLOC
;
28462 else if (strcmp ("to", p
) == 0)
28463 kind
= OMP_CLAUSE_MAP_TO
;
28464 else if (strcmp ("from", p
) == 0)
28465 kind
= OMP_CLAUSE_MAP_FROM
;
28466 else if (strcmp ("tofrom", p
) == 0)
28467 kind
= OMP_CLAUSE_MAP_TOFROM
;
28470 cp_parser_error (parser
, "invalid map kind");
28471 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28472 /*or_comma=*/false,
28473 /*consume_paren=*/true);
28476 cp_lexer_consume_token (parser
->lexer
);
28477 cp_lexer_consume_token (parser
->lexer
);
28480 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28483 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28484 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28490 device ( expression ) */
28493 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28494 location_t location
)
28498 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28501 t
= cp_parser_expression (parser
);
28503 if (t
== error_mark_node
28504 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28505 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28506 /*or_comma=*/false,
28507 /*consume_paren=*/true);
28509 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28510 "device", location
);
28512 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28513 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28514 OMP_CLAUSE_CHAIN (c
) = list
;
28520 dist_schedule ( static )
28521 dist_schedule ( static , expression ) */
28524 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28525 location_t location
)
28529 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28532 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28534 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28536 cp_lexer_consume_token (parser
->lexer
);
28538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28540 cp_lexer_consume_token (parser
->lexer
);
28542 t
= cp_parser_assignment_expression (parser
);
28544 if (t
== error_mark_node
)
28546 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28548 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28551 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28554 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28556 OMP_CLAUSE_CHAIN (c
) = list
;
28560 cp_parser_error (parser
, "invalid dist_schedule kind");
28562 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28563 /*or_comma=*/false,
28564 /*consume_paren=*/true);
28569 proc_bind ( proc-bind-kind )
28572 master | close | spread */
28575 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28576 location_t location
)
28579 enum omp_clause_proc_bind_kind kind
;
28581 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28584 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28586 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28587 const char *p
= IDENTIFIER_POINTER (id
);
28589 if (strcmp ("master", p
) == 0)
28590 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28591 else if (strcmp ("close", p
) == 0)
28592 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28593 else if (strcmp ("spread", p
) == 0)
28594 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
28601 cp_lexer_consume_token (parser
->lexer
);
28602 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28605 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
28606 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
28608 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
28609 OMP_CLAUSE_CHAIN (c
) = list
;
28613 cp_parser_error (parser
, "invalid depend kind");
28615 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28616 /*or_comma=*/false,
28617 /*consume_paren=*/true);
28621 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28622 is a bitmask in MASK. Return the list of clauses found; the result
28623 of clause default goes in *pdefault. */
28626 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
28627 const char *where
, cp_token
*pragma_tok
,
28628 bool finish_p
= true)
28630 tree clauses
= NULL
;
28632 cp_token
*token
= NULL
;
28633 bool cilk_simd_fn
= false;
28635 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
28637 pragma_omp_clause c_kind
;
28638 const char *c_name
;
28639 tree prev
= clauses
;
28641 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28642 cp_lexer_consume_token (parser
->lexer
);
28644 token
= cp_lexer_peek_token (parser
->lexer
);
28645 c_kind
= cp_parser_omp_clause_name (parser
);
28649 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
28650 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
28652 c_name
= "collapse";
28654 case PRAGMA_OMP_CLAUSE_COPYIN
:
28655 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
28658 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
28659 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
28661 c_name
= "copyprivate";
28663 case PRAGMA_OMP_CLAUSE_DEFAULT
:
28664 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
28666 c_name
= "default";
28668 case PRAGMA_OMP_CLAUSE_FINAL
:
28669 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
28672 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
28673 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
28675 c_name
= "firstprivate";
28677 case PRAGMA_OMP_CLAUSE_IF
:
28678 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
28681 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
28682 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
28684 c_name
= "lastprivate";
28686 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
28687 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
28689 c_name
= "mergeable";
28691 case PRAGMA_OMP_CLAUSE_NOWAIT
:
28692 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
28695 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
28696 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
28698 c_name
= "num_threads";
28700 case PRAGMA_OMP_CLAUSE_ORDERED
:
28701 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
28703 c_name
= "ordered";
28705 case PRAGMA_OMP_CLAUSE_PRIVATE
:
28706 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
28708 c_name
= "private";
28710 case PRAGMA_OMP_CLAUSE_REDUCTION
:
28711 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
28712 c_name
= "reduction";
28714 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
28715 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
28717 c_name
= "schedule";
28719 case PRAGMA_OMP_CLAUSE_SHARED
:
28720 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
28724 case PRAGMA_OMP_CLAUSE_UNTIED
:
28725 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
28729 case PRAGMA_OMP_CLAUSE_INBRANCH
:
28730 case PRAGMA_CILK_CLAUSE_MASK
:
28731 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
28732 clauses
, token
->location
);
28733 c_name
= "inbranch";
28735 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
28736 case PRAGMA_CILK_CLAUSE_NOMASK
:
28737 clauses
= cp_parser_omp_clause_branch (parser
,
28738 OMP_CLAUSE_NOTINBRANCH
,
28739 clauses
, token
->location
);
28740 c_name
= "notinbranch";
28742 case PRAGMA_OMP_CLAUSE_PARALLEL
:
28743 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
28744 clauses
, token
->location
);
28745 c_name
= "parallel";
28749 error_at (token
->location
, "%qs must be the first clause of %qs",
28754 case PRAGMA_OMP_CLAUSE_FOR
:
28755 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
28756 clauses
, token
->location
);
28759 goto clause_not_first
;
28761 case PRAGMA_OMP_CLAUSE_SECTIONS
:
28762 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
28763 clauses
, token
->location
);
28764 c_name
= "sections";
28766 goto clause_not_first
;
28768 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
28769 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
28770 clauses
, token
->location
);
28771 c_name
= "taskgroup";
28773 goto clause_not_first
;
28775 case PRAGMA_OMP_CLAUSE_TO
:
28776 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
28780 case PRAGMA_OMP_CLAUSE_FROM
:
28781 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
28785 case PRAGMA_OMP_CLAUSE_UNIFORM
:
28786 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
28788 c_name
= "uniform";
28790 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
28791 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
28793 c_name
= "num_teams";
28795 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
28796 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
28798 c_name
= "thread_limit";
28800 case PRAGMA_OMP_CLAUSE_ALIGNED
:
28801 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
28802 c_name
= "aligned";
28804 case PRAGMA_OMP_CLAUSE_LINEAR
:
28805 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
28806 cilk_simd_fn
= true;
28807 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
28810 case PRAGMA_OMP_CLAUSE_DEPEND
:
28811 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
28814 case PRAGMA_OMP_CLAUSE_MAP
:
28815 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
28818 case PRAGMA_OMP_CLAUSE_DEVICE
:
28819 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
28823 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
28824 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
28826 c_name
= "dist_schedule";
28828 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
28829 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
28831 c_name
= "proc_bind";
28833 case PRAGMA_OMP_CLAUSE_SAFELEN
:
28834 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
28836 c_name
= "safelen";
28838 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
28839 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
28841 c_name
= "simdlen";
28843 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
28844 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
28845 c_name
= "simdlen";
28848 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
28854 if (((mask
>> c_kind
) & 1) == 0)
28856 /* Remove the invalid clause(s) from the list to avoid
28857 confusing the rest of the compiler. */
28859 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
28863 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28864 no reason to skip to the end. */
28865 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
28866 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28868 return finish_omp_clauses (clauses
);
28876 In practice, we're also interested in adding the statement to an
28877 outer node. So it is convenient if we work around the fact that
28878 cp_parser_statement calls add_stmt. */
28881 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
28883 unsigned save
= parser
->in_statement
;
28885 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28886 This preserves the "not within loop or switch" style error messages
28887 for nonsense cases like
28893 if (parser
->in_statement
)
28894 parser
->in_statement
= IN_OMP_BLOCK
;
28900 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
28902 parser
->in_statement
= save
;
28906 cp_parser_omp_structured_block (cp_parser
*parser
)
28908 tree stmt
= begin_omp_structured_block ();
28909 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
28911 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
28913 cp_parser_end_omp_structured_block (parser
, save
);
28914 return finish_omp_structured_block (stmt
);
28918 # pragma omp atomic new-line
28922 x binop= expr | x++ | ++x | x-- | --x
28924 +, *, -, /, &, ^, |, <<, >>
28926 where x is an lvalue expression with scalar type.
28929 # pragma omp atomic new-line
28932 # pragma omp atomic read new-line
28935 # pragma omp atomic write new-line
28938 # pragma omp atomic update new-line
28941 # pragma omp atomic capture new-line
28944 # pragma omp atomic capture new-line
28952 expression-stmt | x = x binop expr
28954 v = expression-stmt
28956 { v = x; update-stmt; } | { update-stmt; v = x; }
28960 expression-stmt | x = x binop expr | x = expr binop x
28964 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28966 where x and v are lvalue expressions with scalar type. */
28969 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
28971 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
28972 tree rhs1
= NULL_TREE
, orig_lhs
;
28973 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
28974 bool structured_block
= false;
28975 bool seq_cst
= false;
28977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28979 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28980 const char *p
= IDENTIFIER_POINTER (id
);
28982 if (!strcmp (p
, "seq_cst"))
28985 cp_lexer_consume_token (parser
->lexer
);
28986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
28987 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
28988 cp_lexer_consume_token (parser
->lexer
);
28991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28993 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28994 const char *p
= IDENTIFIER_POINTER (id
);
28996 if (!strcmp (p
, "read"))
28997 code
= OMP_ATOMIC_READ
;
28998 else if (!strcmp (p
, "write"))
29000 else if (!strcmp (p
, "update"))
29002 else if (!strcmp (p
, "capture"))
29003 code
= OMP_ATOMIC_CAPTURE_NEW
;
29007 cp_lexer_consume_token (parser
->lexer
);
29011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29012 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29013 cp_lexer_consume_token (parser
->lexer
);
29015 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29017 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29018 const char *p
= IDENTIFIER_POINTER (id
);
29020 if (!strcmp (p
, "seq_cst"))
29023 cp_lexer_consume_token (parser
->lexer
);
29027 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29031 case OMP_ATOMIC_READ
:
29032 case NOP_EXPR
: /* atomic write */
29033 v
= cp_parser_unary_expression (parser
);
29034 if (v
== error_mark_node
)
29036 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29038 if (code
== NOP_EXPR
)
29039 lhs
= cp_parser_expression (parser
);
29041 lhs
= cp_parser_unary_expression (parser
);
29042 if (lhs
== error_mark_node
)
29044 if (code
== NOP_EXPR
)
29046 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29054 case OMP_ATOMIC_CAPTURE_NEW
:
29055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29057 cp_lexer_consume_token (parser
->lexer
);
29058 structured_block
= true;
29062 v
= cp_parser_unary_expression (parser
);
29063 if (v
== error_mark_node
)
29065 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29073 lhs
= cp_parser_unary_expression (parser
);
29075 switch (TREE_CODE (lhs
))
29080 case POSTINCREMENT_EXPR
:
29081 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29082 code
= OMP_ATOMIC_CAPTURE_OLD
;
29084 case PREINCREMENT_EXPR
:
29085 lhs
= TREE_OPERAND (lhs
, 0);
29086 opcode
= PLUS_EXPR
;
29087 rhs
= integer_one_node
;
29090 case POSTDECREMENT_EXPR
:
29091 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29092 code
= OMP_ATOMIC_CAPTURE_OLD
;
29094 case PREDECREMENT_EXPR
:
29095 lhs
= TREE_OPERAND (lhs
, 0);
29096 opcode
= MINUS_EXPR
;
29097 rhs
= integer_one_node
;
29100 case COMPOUND_EXPR
:
29101 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29102 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29103 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29104 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29105 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29106 (TREE_OPERAND (lhs
, 1), 0), 0)))
29108 /* Undo effects of boolean_increment for post {in,de}crement. */
29109 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29112 if (TREE_CODE (lhs
) == MODIFY_EXPR
29113 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29115 /* Undo effects of boolean_increment. */
29116 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29118 /* This is pre or post increment. */
29119 rhs
= TREE_OPERAND (lhs
, 1);
29120 lhs
= TREE_OPERAND (lhs
, 0);
29122 if (code
== OMP_ATOMIC_CAPTURE_NEW
29123 && !structured_block
29124 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29125 code
= OMP_ATOMIC_CAPTURE_OLD
;
29131 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29134 opcode
= MULT_EXPR
;
29137 opcode
= TRUNC_DIV_EXPR
;
29140 opcode
= PLUS_EXPR
;
29143 opcode
= MINUS_EXPR
;
29145 case CPP_LSHIFT_EQ
:
29146 opcode
= LSHIFT_EXPR
;
29148 case CPP_RSHIFT_EQ
:
29149 opcode
= RSHIFT_EXPR
;
29152 opcode
= BIT_AND_EXPR
;
29155 opcode
= BIT_IOR_EXPR
;
29158 opcode
= BIT_XOR_EXPR
;
29161 enum cp_parser_prec oprec
;
29163 cp_lexer_consume_token (parser
->lexer
);
29164 cp_parser_parse_tentatively (parser
);
29165 rhs1
= cp_parser_simple_cast_expression (parser
);
29166 if (rhs1
== error_mark_node
)
29168 cp_parser_abort_tentative_parse (parser
);
29169 cp_parser_simple_cast_expression (parser
);
29172 token
= cp_lexer_peek_token (parser
->lexer
);
29173 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29175 cp_parser_abort_tentative_parse (parser
);
29176 cp_parser_parse_tentatively (parser
);
29177 rhs
= cp_parser_binary_expression (parser
, false, true,
29178 PREC_NOT_OPERATOR
, NULL
);
29179 if (rhs
== error_mark_node
)
29181 cp_parser_abort_tentative_parse (parser
);
29182 cp_parser_binary_expression (parser
, false, true,
29183 PREC_NOT_OPERATOR
, NULL
);
29186 switch (TREE_CODE (rhs
))
29189 case TRUNC_DIV_EXPR
:
29197 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29199 if (cp_parser_parse_definitely (parser
))
29201 opcode
= TREE_CODE (rhs
);
29202 rhs1
= TREE_OPERAND (rhs
, 0);
29203 rhs
= TREE_OPERAND (rhs
, 1);
29213 cp_parser_abort_tentative_parse (parser
);
29214 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29216 rhs
= cp_parser_expression (parser
);
29217 if (rhs
== error_mark_node
)
29223 cp_parser_error (parser
,
29224 "invalid form of %<#pragma omp atomic%>");
29227 if (!cp_parser_parse_definitely (parser
))
29229 switch (token
->type
)
29231 case CPP_SEMICOLON
:
29232 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29234 code
= OMP_ATOMIC_CAPTURE_OLD
;
29239 cp_lexer_consume_token (parser
->lexer
);
29242 else if (structured_block
)
29249 cp_parser_error (parser
,
29250 "invalid form of %<#pragma omp atomic%>");
29253 opcode
= MULT_EXPR
;
29256 opcode
= TRUNC_DIV_EXPR
;
29259 opcode
= PLUS_EXPR
;
29262 opcode
= MINUS_EXPR
;
29265 opcode
= LSHIFT_EXPR
;
29268 opcode
= RSHIFT_EXPR
;
29271 opcode
= BIT_AND_EXPR
;
29274 opcode
= BIT_IOR_EXPR
;
29277 opcode
= BIT_XOR_EXPR
;
29280 cp_parser_error (parser
,
29281 "invalid operator for %<#pragma omp atomic%>");
29284 oprec
= TOKEN_PRECEDENCE (token
);
29285 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
29286 if (commutative_tree_code (opcode
))
29287 oprec
= (enum cp_parser_prec
) (oprec
- 1);
29288 cp_lexer_consume_token (parser
->lexer
);
29289 rhs
= cp_parser_binary_expression (parser
, false, false,
29291 if (rhs
== error_mark_node
)
29296 cp_parser_error (parser
,
29297 "invalid operator for %<#pragma omp atomic%>");
29300 cp_lexer_consume_token (parser
->lexer
);
29302 rhs
= cp_parser_expression (parser
);
29303 if (rhs
== error_mark_node
)
29308 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29310 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
29312 v
= cp_parser_unary_expression (parser
);
29313 if (v
== error_mark_node
)
29315 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29317 lhs1
= cp_parser_unary_expression (parser
);
29318 if (lhs1
== error_mark_node
)
29321 if (structured_block
)
29323 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29324 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29327 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
29328 if (!structured_block
)
29329 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29333 cp_parser_skip_to_end_of_block_or_statement (parser
);
29334 if (structured_block
)
29336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29337 cp_lexer_consume_token (parser
->lexer
);
29338 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
29340 cp_parser_skip_to_end_of_block_or_statement (parser
);
29341 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29342 cp_lexer_consume_token (parser
->lexer
);
29349 # pragma omp barrier new-line */
29352 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
29354 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29355 finish_omp_barrier ();
29359 # pragma omp critical [(name)] new-line
29360 structured-block */
29363 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
29365 tree stmt
, name
= NULL
;
29367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29369 cp_lexer_consume_token (parser
->lexer
);
29371 name
= cp_parser_identifier (parser
);
29373 if (name
== error_mark_node
29374 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29375 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29376 /*or_comma=*/false,
29377 /*consume_paren=*/true);
29378 if (name
== error_mark_node
)
29381 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29383 stmt
= cp_parser_omp_structured_block (parser
);
29384 return c_finish_omp_critical (input_location
, stmt
, name
);
29388 # pragma omp flush flush-vars[opt] new-line
29391 ( variable-list ) */
29394 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
29396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29397 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29398 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29400 finish_omp_flush ();
29403 /* Helper function, to parse omp for increment expression. */
29406 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
29408 tree cond
= cp_parser_binary_expression (parser
, false, true,
29409 PREC_NOT_OPERATOR
, NULL
);
29410 if (cond
== error_mark_node
29411 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29413 cp_parser_skip_to_end_of_statement (parser
);
29414 return error_mark_node
;
29417 switch (TREE_CODE (cond
))
29425 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
29427 /* Fall through: OpenMP disallows NE_EXPR. */
29429 return error_mark_node
;
29432 /* If decl is an iterator, preserve LHS and RHS of the relational
29433 expr until finish_omp_for. */
29435 && (type_dependent_expression_p (decl
)
29436 || CLASS_TYPE_P (TREE_TYPE (decl
))))
29439 return build_x_binary_op (input_location
, TREE_CODE (cond
),
29440 TREE_OPERAND (cond
, 0), ERROR_MARK
,
29441 TREE_OPERAND (cond
, 1), ERROR_MARK
,
29442 /*overload=*/NULL
, tf_warning_or_error
);
29445 /* Helper function, to parse omp for increment expression. */
29448 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
29450 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29456 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29458 op
= (token
->type
== CPP_PLUS_PLUS
29459 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
29460 cp_lexer_consume_token (parser
->lexer
);
29461 lhs
= cp_parser_simple_cast_expression (parser
);
29463 return error_mark_node
;
29464 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29467 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
29469 return error_mark_node
;
29471 token
= cp_lexer_peek_token (parser
->lexer
);
29472 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29474 op
= (token
->type
== CPP_PLUS_PLUS
29475 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
29476 cp_lexer_consume_token (parser
->lexer
);
29477 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29480 op
= cp_parser_assignment_operator_opt (parser
);
29481 if (op
== ERROR_MARK
)
29482 return error_mark_node
;
29484 if (op
!= NOP_EXPR
)
29486 rhs
= cp_parser_assignment_expression (parser
);
29487 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
29488 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29491 lhs
= cp_parser_binary_expression (parser
, false, false,
29492 PREC_ADDITIVE_EXPRESSION
, NULL
);
29493 token
= cp_lexer_peek_token (parser
->lexer
);
29494 decl_first
= lhs
== decl
;
29497 if (token
->type
!= CPP_PLUS
29498 && token
->type
!= CPP_MINUS
)
29499 return error_mark_node
;
29503 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
29504 cp_lexer_consume_token (parser
->lexer
);
29505 rhs
= cp_parser_binary_expression (parser
, false, false,
29506 PREC_ADDITIVE_EXPRESSION
, NULL
);
29507 token
= cp_lexer_peek_token (parser
->lexer
);
29508 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
29510 if (lhs
== NULL_TREE
)
29512 if (op
== PLUS_EXPR
)
29515 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
29516 tf_warning_or_error
);
29519 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
29520 ERROR_MARK
, NULL
, tf_warning_or_error
);
29523 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
29527 if (rhs
!= decl
|| op
== MINUS_EXPR
)
29528 return error_mark_node
;
29529 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
29532 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
29534 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29537 /* Parse the initialization statement of either an OpenMP for loop or
29538 a Cilk Plus for loop.
29540 Return true if the resulting construct should have an
29541 OMP_CLAUSE_PRIVATE added to it. */
29544 cp_parser_omp_for_loop_init (cp_parser
*parser
,
29545 enum tree_code code
,
29546 tree
&this_pre_body
,
29547 vec
<tree
, va_gc
> *for_block
,
29552 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29555 bool add_private_clause
= false;
29557 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29561 integer-type var = lb
29562 random-access-iterator-type var = lb
29563 pointer-type var = lb
29565 cp_decl_specifier_seq type_specifiers
;
29567 /* First, try to parse as an initialized declaration. See
29568 cp_parser_condition, from whence the bulk of this is copied. */
29570 cp_parser_parse_tentatively (parser
);
29571 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
29572 /*is_trailing_return=*/false,
29574 if (cp_parser_parse_definitely (parser
))
29576 /* If parsing a type specifier seq succeeded, then this
29577 MUST be a initialized declaration. */
29578 tree asm_specification
, attributes
;
29579 cp_declarator
*declarator
;
29581 declarator
= cp_parser_declarator (parser
,
29582 CP_PARSER_DECLARATOR_NAMED
,
29583 /*ctor_dtor_or_conv_p=*/NULL
,
29584 /*parenthesized_p=*/NULL
,
29585 /*member_p=*/false,
29586 /*friend_p=*/false);
29587 attributes
= cp_parser_attributes_opt (parser
);
29588 asm_specification
= cp_parser_asm_specification_opt (parser
);
29590 if (declarator
== cp_error_declarator
)
29591 cp_parser_skip_to_end_of_statement (parser
);
29595 tree pushed_scope
, auto_node
;
29597 decl
= start_decl (declarator
, &type_specifiers
,
29598 SD_INITIALIZED
, attributes
,
29599 /*prefix_attributes=*/NULL_TREE
,
29602 auto_node
= type_uses_auto (TREE_TYPE (decl
));
29603 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
29605 if (cp_lexer_next_token_is (parser
->lexer
,
29608 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
29609 error ("parenthesized initialization is not allowed in "
29610 "OpenMP %<for%> loop");
29612 error ("parenthesized initialization is "
29613 "not allowed in for-loop");
29616 /* Trigger an error. */
29617 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29619 init
= error_mark_node
;
29620 cp_parser_skip_to_end_of_statement (parser
);
29622 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
29623 || type_dependent_expression_p (decl
)
29626 bool is_direct_init
, is_non_constant_init
;
29628 init
= cp_parser_initializer (parser
,
29630 &is_non_constant_init
);
29635 = do_auto_deduction (TREE_TYPE (decl
), init
,
29638 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
29639 && !type_dependent_expression_p (decl
))
29643 cp_finish_decl (decl
, init
, !is_non_constant_init
,
29645 LOOKUP_ONLYCONVERTING
);
29646 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
29648 vec_safe_push (for_block
, this_pre_body
);
29652 init
= pop_stmt_list (this_pre_body
);
29653 this_pre_body
= NULL_TREE
;
29658 cp_lexer_consume_token (parser
->lexer
);
29659 init
= cp_parser_assignment_expression (parser
);
29662 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
29663 init
= error_mark_node
;
29665 cp_finish_decl (decl
, NULL_TREE
,
29666 /*init_const_expr_p=*/false,
29668 LOOKUP_ONLYCONVERTING
);
29672 pop_scope (pushed_scope
);
29678 /* If parsing a type specifier sequence failed, then
29679 this MUST be a simple expression. */
29680 if (code
== CILK_FOR
)
29681 error ("%<_Cilk_for%> allows expression instead of declaration only "
29682 "in C, not in C++");
29683 cp_parser_parse_tentatively (parser
);
29684 decl
= cp_parser_primary_expression (parser
, false, false,
29686 if (!cp_parser_error_occurred (parser
)
29689 && CLASS_TYPE_P (TREE_TYPE (decl
)))
29693 cp_parser_parse_definitely (parser
);
29694 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29695 rhs
= cp_parser_assignment_expression (parser
);
29696 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
29699 tf_warning_or_error
));
29700 add_private_clause
= true;
29705 cp_parser_abort_tentative_parse (parser
);
29706 init
= cp_parser_expression (parser
);
29709 if (TREE_CODE (init
) == MODIFY_EXPR
29710 || TREE_CODE (init
) == MODOP_EXPR
)
29711 real_decl
= TREE_OPERAND (init
, 0);
29715 return add_private_clause
;
29718 /* Parse the restricted form of the for statement allowed by OpenMP. */
29721 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
29724 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
29725 tree real_decl
, initv
, condv
, incrv
, declv
;
29726 tree this_pre_body
, cl
;
29727 location_t loc_first
;
29728 bool collapse_err
= false;
29729 int i
, collapse
= 1, nbraces
= 0;
29730 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
29732 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
29733 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
29734 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
29736 gcc_assert (collapse
>= 1);
29738 declv
= make_tree_vec (collapse
);
29739 initv
= make_tree_vec (collapse
);
29740 condv
= make_tree_vec (collapse
);
29741 incrv
= make_tree_vec (collapse
);
29743 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
29745 for (i
= 0; i
< collapse
; i
++)
29747 int bracecount
= 0;
29748 bool add_private_clause
= false;
29751 if (code
!= CILK_FOR
29752 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29754 cp_parser_error (parser
, "for statement expected");
29757 if (code
== CILK_FOR
29758 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
29760 cp_parser_error (parser
, "_Cilk_for statement expected");
29763 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
29765 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29768 init
= decl
= real_decl
= NULL
;
29769 this_pre_body
= push_stmt_list ();
29772 |= cp_parser_omp_for_loop_init (parser
, code
,
29773 this_pre_body
, for_block
,
29774 init
, decl
, real_decl
);
29776 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29779 this_pre_body
= pop_stmt_list (this_pre_body
);
29783 pre_body
= push_stmt_list ();
29785 add_stmt (this_pre_body
);
29786 pre_body
= pop_stmt_list (pre_body
);
29789 pre_body
= this_pre_body
;
29794 if (cclauses
!= NULL
29795 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
29796 && real_decl
!= NULL_TREE
)
29799 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
29800 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
29801 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29803 error_at (loc
, "iteration variable %qD"
29804 " should not be firstprivate", real_decl
);
29805 *c
= OMP_CLAUSE_CHAIN (*c
);
29807 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
29808 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29810 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29811 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29812 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
29813 OMP_CLAUSE_DECL (l
) = real_decl
;
29814 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
29815 if (code
== OMP_SIMD
)
29817 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29818 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
29822 OMP_CLAUSE_CHAIN (l
) = clauses
;
29825 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
29826 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
29827 add_private_clause
= false;
29831 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
29832 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29833 add_private_clause
= false;
29834 c
= &OMP_CLAUSE_CHAIN (*c
);
29838 if (add_private_clause
)
29841 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29843 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
29844 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
29845 && OMP_CLAUSE_DECL (c
) == decl
)
29847 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
29848 && OMP_CLAUSE_DECL (c
) == decl
)
29849 error_at (loc
, "iteration variable %qD "
29850 "should not be firstprivate",
29852 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
29853 && OMP_CLAUSE_DECL (c
) == decl
)
29854 error_at (loc
, "iteration variable %qD should not be reduction",
29859 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
29860 OMP_CLAUSE_DECL (c
) = decl
;
29861 c
= finish_omp_clauses (c
);
29864 OMP_CLAUSE_CHAIN (c
) = clauses
;
29871 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29872 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
29873 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29876 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29878 /* If decl is an iterator, preserve the operator on decl
29879 until finish_omp_for. */
29881 && ((processing_template_decl
29882 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
29883 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
29884 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
29886 incr
= cp_parser_expression (parser
);
29887 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
29888 SET_EXPR_LOCATION (incr
, input_location
);
29891 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29892 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29893 /*or_comma=*/false,
29894 /*consume_paren=*/true);
29896 TREE_VEC_ELT (declv
, i
) = decl
;
29897 TREE_VEC_ELT (initv
, i
) = init
;
29898 TREE_VEC_ELT (condv
, i
) = cond
;
29899 TREE_VEC_ELT (incrv
, i
) = incr
;
29901 if (i
== collapse
- 1)
29904 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29905 in between the collapsed for loops to be still considered perfectly
29906 nested. Hopefully the final version clarifies this.
29907 For now handle (multiple) {'s and empty statements. */
29908 cp_parser_parse_tentatively (parser
);
29911 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29913 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29915 cp_lexer_consume_token (parser
->lexer
);
29918 else if (bracecount
29919 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29920 cp_lexer_consume_token (parser
->lexer
);
29923 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29924 error_at (loc
, "not enough collapsed for loops");
29925 collapse_err
= true;
29926 cp_parser_abort_tentative_parse (parser
);
29935 cp_parser_parse_definitely (parser
);
29936 nbraces
+= bracecount
;
29940 /* Note that we saved the original contents of this flag when we entered
29941 the structured block, and so we don't need to re-save it here. */
29942 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
29943 parser
->in_statement
= IN_CILK_SIMD_FOR
;
29945 parser
->in_statement
= IN_OMP_FOR
;
29947 /* Note that the grammar doesn't call for a structured block here,
29948 though the loop as a whole is a structured block. */
29949 body
= push_stmt_list ();
29950 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29951 body
= pop_stmt_list (body
);
29953 if (declv
== NULL_TREE
)
29956 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
29957 pre_body
, clauses
);
29961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29963 cp_lexer_consume_token (parser
->lexer
);
29966 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29967 cp_lexer_consume_token (parser
->lexer
);
29972 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29973 "collapsed loops not perfectly nested");
29975 collapse_err
= true;
29976 cp_parser_statement_seq_opt (parser
, NULL
);
29977 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
29982 while (!for_block
->is_empty ())
29983 add_stmt (pop_stmt_list (for_block
->pop ()));
29984 release_tree_vector (for_block
);
29989 /* Helper function for OpenMP parsing, split clauses and call
29990 finish_omp_clauses on each of the set of clauses afterwards. */
29993 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
29994 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
29997 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
29998 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30000 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30004 #pragma omp simd simd-clause[optseq] new-line
30007 #define OMP_SIMD_CLAUSE_MASK \
30008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30017 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30018 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30020 tree clauses
, sb
, ret
;
30022 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30024 strcat (p_name
, " simd");
30025 mask
|= OMP_SIMD_CLAUSE_MASK
;
30026 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30028 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30032 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30033 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30036 sb
= begin_omp_structured_block ();
30037 save
= cp_parser_begin_omp_structured_block (parser
);
30039 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30041 cp_parser_end_omp_structured_block (parser
, save
);
30042 add_stmt (finish_omp_structured_block (sb
));
30048 #pragma omp for for-clause[optseq] new-line
30052 #pragma omp for simd for-simd-clause[optseq] new-line
30055 #define OMP_FOR_CLAUSE_MASK \
30056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30066 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30067 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30069 tree clauses
, sb
, ret
;
30071 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30073 strcat (p_name
, " for");
30074 mask
|= OMP_FOR_CLAUSE_MASK
;
30076 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30078 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30080 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30081 const char *p
= IDENTIFIER_POINTER (id
);
30083 if (strcmp (p
, "simd") == 0)
30085 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30086 if (cclauses
== NULL
)
30087 cclauses
= cclauses_buf
;
30089 cp_lexer_consume_token (parser
->lexer
);
30090 if (!flag_openmp
) /* flag_openmp_simd */
30091 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30093 sb
= begin_omp_structured_block ();
30094 save
= cp_parser_begin_omp_structured_block (parser
);
30095 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30097 cp_parser_end_omp_structured_block (parser
, save
);
30098 tree body
= finish_omp_structured_block (sb
);
30101 ret
= make_node (OMP_FOR
);
30102 TREE_TYPE (ret
) = void_type_node
;
30103 OMP_FOR_BODY (ret
) = body
;
30104 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30105 SET_EXPR_LOCATION (ret
, loc
);
30110 if (!flag_openmp
) /* flag_openmp_simd */
30112 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30116 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30120 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30121 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30124 sb
= begin_omp_structured_block ();
30125 save
= cp_parser_begin_omp_structured_block (parser
);
30127 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30129 cp_parser_end_omp_structured_block (parser
, save
);
30130 add_stmt (finish_omp_structured_block (sb
));
30136 # pragma omp master new-line
30137 structured-block */
30140 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30142 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30143 return c_finish_omp_master (input_location
,
30144 cp_parser_omp_structured_block (parser
));
30148 # pragma omp ordered new-line
30149 structured-block */
30152 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30154 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30155 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30156 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30162 { section-sequence }
30165 section-directive[opt] structured-block
30166 section-sequence section-directive structured-block */
30169 cp_parser_omp_sections_scope (cp_parser
*parser
)
30171 tree stmt
, substmt
;
30172 bool error_suppress
= false;
30175 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30178 stmt
= push_stmt_list ();
30180 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30182 substmt
= cp_parser_omp_structured_block (parser
);
30183 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30184 add_stmt (substmt
);
30189 tok
= cp_lexer_peek_token (parser
->lexer
);
30190 if (tok
->type
== CPP_CLOSE_BRACE
)
30192 if (tok
->type
== CPP_EOF
)
30195 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30197 cp_lexer_consume_token (parser
->lexer
);
30198 cp_parser_require_pragma_eol (parser
, tok
);
30199 error_suppress
= false;
30201 else if (!error_suppress
)
30203 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30204 error_suppress
= true;
30207 substmt
= cp_parser_omp_structured_block (parser
);
30208 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30209 add_stmt (substmt
);
30211 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30213 substmt
= pop_stmt_list (stmt
);
30215 stmt
= make_node (OMP_SECTIONS
);
30216 TREE_TYPE (stmt
) = void_type_node
;
30217 OMP_SECTIONS_BODY (stmt
) = substmt
;
30224 # pragma omp sections sections-clause[optseq] newline
30227 #define OMP_SECTIONS_CLAUSE_MASK \
30228 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30235 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
30236 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30239 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30241 strcat (p_name
, " sections");
30242 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
30244 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30246 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30250 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
30251 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
30254 ret
= cp_parser_omp_sections_scope (parser
);
30256 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
30262 # pragma omp parallel parallel-clause[optseq] new-line
30264 # pragma omp parallel for parallel-for-clause[optseq] new-line
30266 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30270 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30271 structured-block */
30273 #define OMP_PARALLEL_CLAUSE_MASK \
30274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30285 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
30286 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30288 tree stmt
, clauses
, block
;
30290 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30292 strcat (p_name
, " parallel");
30293 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
30295 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30297 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30298 if (cclauses
== NULL
)
30299 cclauses
= cclauses_buf
;
30301 cp_lexer_consume_token (parser
->lexer
);
30302 if (!flag_openmp
) /* flag_openmp_simd */
30303 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30304 block
= begin_omp_parallel ();
30305 save
= cp_parser_begin_omp_structured_block (parser
);
30306 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30307 cp_parser_end_omp_structured_block (parser
, save
);
30308 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30310 if (ret
== NULL_TREE
)
30312 OMP_PARALLEL_COMBINED (stmt
) = 1;
30317 error_at (loc
, "expected %<for%> after %qs", p_name
);
30318 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30321 else if (!flag_openmp
) /* flag_openmp_simd */
30323 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30326 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30328 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30329 const char *p
= IDENTIFIER_POINTER (id
);
30330 if (strcmp (p
, "sections") == 0)
30332 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30333 cclauses
= cclauses_buf
;
30335 cp_lexer_consume_token (parser
->lexer
);
30336 block
= begin_omp_parallel ();
30337 save
= cp_parser_begin_omp_structured_block (parser
);
30338 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30339 cp_parser_end_omp_structured_block (parser
, save
);
30340 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30342 OMP_PARALLEL_COMBINED (stmt
) = 1;
30347 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
30349 block
= begin_omp_parallel ();
30350 save
= cp_parser_begin_omp_structured_block (parser
);
30351 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30352 cp_parser_end_omp_structured_block (parser
, save
);
30353 stmt
= finish_omp_parallel (clauses
, block
);
30358 # pragma omp single single-clause[optseq] new-line
30359 structured-block */
30361 #define OMP_SINGLE_CLAUSE_MASK \
30362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30368 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
30370 tree stmt
= make_node (OMP_SINGLE
);
30371 TREE_TYPE (stmt
) = void_type_node
;
30373 OMP_SINGLE_CLAUSES (stmt
)
30374 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
30375 "#pragma omp single", pragma_tok
);
30376 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30378 return add_stmt (stmt
);
30382 # pragma omp task task-clause[optseq] new-line
30383 structured-block */
30385 #define OMP_TASK_CLAUSE_MASK \
30386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30397 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
30399 tree clauses
, block
;
30402 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
30403 "#pragma omp task", pragma_tok
);
30404 block
= begin_omp_task ();
30405 save
= cp_parser_begin_omp_structured_block (parser
);
30406 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30407 cp_parser_end_omp_structured_block (parser
, save
);
30408 return finish_omp_task (clauses
, block
);
30412 # pragma omp taskwait new-line */
30415 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
30417 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30418 finish_omp_taskwait ();
30422 # pragma omp taskyield new-line */
30425 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
30427 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30428 finish_omp_taskyield ();
30432 # pragma omp taskgroup new-line
30433 structured-block */
30436 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
30438 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30439 return c_finish_omp_taskgroup (input_location
,
30440 cp_parser_omp_structured_block (parser
));
30445 # pragma omp threadprivate (variable-list) */
30448 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
30452 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30453 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30455 finish_omp_threadprivate (vars
);
30459 # pragma omp cancel cancel-clause[optseq] new-line */
30461 #define OMP_CANCEL_CLAUSE_MASK \
30462 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30469 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
30471 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
30472 "#pragma omp cancel", pragma_tok
);
30473 finish_omp_cancel (clauses
);
30477 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30479 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30486 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
30489 bool point_seen
= false;
30491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30493 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30494 const char *p
= IDENTIFIER_POINTER (id
);
30496 if (strcmp (p
, "point") == 0)
30498 cp_lexer_consume_token (parser
->lexer
);
30504 cp_parser_error (parser
, "expected %<point%>");
30505 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30509 clauses
= cp_parser_omp_all_clauses (parser
,
30510 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
30511 "#pragma omp cancellation point",
30513 finish_omp_cancellation_point (clauses
);
30517 #pragma omp distribute distribute-clause[optseq] new-line
30520 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30521 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30527 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
30528 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30530 tree clauses
, sb
, ret
;
30532 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30534 strcat (p_name
, " distribute");
30535 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
30537 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30539 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30540 const char *p
= IDENTIFIER_POINTER (id
);
30542 bool parallel
= false;
30544 if (strcmp (p
, "simd") == 0)
30547 parallel
= strcmp (p
, "parallel") == 0;
30548 if (parallel
|| simd
)
30550 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30551 if (cclauses
== NULL
)
30552 cclauses
= cclauses_buf
;
30553 cp_lexer_consume_token (parser
->lexer
);
30554 if (!flag_openmp
) /* flag_openmp_simd */
30557 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30560 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30563 sb
= begin_omp_structured_block ();
30564 save
= cp_parser_begin_omp_structured_block (parser
);
30566 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30569 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30571 cp_parser_end_omp_structured_block (parser
, save
);
30572 tree body
= finish_omp_structured_block (sb
);
30575 ret
= make_node (OMP_DISTRIBUTE
);
30576 TREE_TYPE (ret
) = void_type_node
;
30577 OMP_FOR_BODY (ret
) = body
;
30578 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30579 SET_EXPR_LOCATION (ret
, loc
);
30584 if (!flag_openmp
) /* flag_openmp_simd */
30586 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30590 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30594 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
30595 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30598 sb
= begin_omp_structured_block ();
30599 save
= cp_parser_begin_omp_structured_block (parser
);
30601 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
30603 cp_parser_end_omp_structured_block (parser
, save
);
30604 add_stmt (finish_omp_structured_block (sb
));
30610 # pragma omp teams teams-clause[optseq] new-line
30611 structured-block */
30613 #define OMP_TEAMS_CLAUSE_MASK \
30614 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30623 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
30624 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30626 tree clauses
, sb
, ret
;
30628 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30630 strcat (p_name
, " teams");
30631 mask
|= OMP_TEAMS_CLAUSE_MASK
;
30633 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30635 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30636 const char *p
= IDENTIFIER_POINTER (id
);
30637 if (strcmp (p
, "distribute") == 0)
30639 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30640 if (cclauses
== NULL
)
30641 cclauses
= cclauses_buf
;
30643 cp_lexer_consume_token (parser
->lexer
);
30644 if (!flag_openmp
) /* flag_openmp_simd */
30645 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30647 sb
= begin_omp_structured_block ();
30648 save
= cp_parser_begin_omp_structured_block (parser
);
30649 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30651 cp_parser_end_omp_structured_block (parser
, save
);
30652 tree body
= finish_omp_structured_block (sb
);
30655 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30656 ret
= make_node (OMP_TEAMS
);
30657 TREE_TYPE (ret
) = void_type_node
;
30658 OMP_TEAMS_CLAUSES (ret
) = clauses
;
30659 OMP_TEAMS_BODY (ret
) = body
;
30660 return add_stmt (ret
);
30663 if (!flag_openmp
) /* flag_openmp_simd */
30665 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30669 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30673 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
30674 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30677 tree stmt
= make_node (OMP_TEAMS
);
30678 TREE_TYPE (stmt
) = void_type_node
;
30679 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
30680 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30682 return add_stmt (stmt
);
30686 # pragma omp target data target-data-clause[optseq] new-line
30687 structured-block */
30689 #define OMP_TARGET_DATA_CLAUSE_MASK \
30690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30695 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
30697 tree stmt
= make_node (OMP_TARGET_DATA
);
30698 TREE_TYPE (stmt
) = void_type_node
;
30700 OMP_TARGET_DATA_CLAUSES (stmt
)
30701 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
30702 "#pragma omp target data", pragma_tok
);
30703 keep_next_level (true);
30704 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30706 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30707 return add_stmt (stmt
);
30711 # pragma omp target update target-update-clause[optseq] new-line */
30713 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30720 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
30721 enum pragma_context context
)
30723 if (context
== pragma_stmt
)
30725 error_at (pragma_tok
->location
,
30726 "%<#pragma omp target update%> may only be "
30727 "used in compound statements");
30728 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30733 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
30734 "#pragma omp target update", pragma_tok
);
30735 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
30736 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
30738 error_at (pragma_tok
->location
,
30739 "%<#pragma omp target update must contain at least one "
30740 "%<from%> or %<to%> clauses");
30744 tree stmt
= make_node (OMP_TARGET_UPDATE
);
30745 TREE_TYPE (stmt
) = void_type_node
;
30746 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
30747 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30753 # pragma omp target target-clause[optseq] new-line
30754 structured-block */
30756 #define OMP_TARGET_CLAUSE_MASK \
30757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30762 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
30763 enum pragma_context context
)
30765 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
30767 cp_parser_error (parser
, "expected declaration specifiers");
30768 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30774 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30775 const char *p
= IDENTIFIER_POINTER (id
);
30777 if (strcmp (p
, "teams") == 0)
30779 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
30780 char p_name
[sizeof ("#pragma omp target teams distribute "
30781 "parallel for simd")];
30783 cp_lexer_consume_token (parser
->lexer
);
30784 strcpy (p_name
, "#pragma omp target");
30785 if (!flag_openmp
) /* flag_openmp_simd */
30787 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30788 OMP_TARGET_CLAUSE_MASK
,
30790 return stmt
!= NULL_TREE
;
30792 keep_next_level (true);
30793 tree sb
= begin_omp_structured_block ();
30794 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
30795 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30796 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30797 cp_parser_end_omp_structured_block (parser
, save
);
30798 tree body
= finish_omp_structured_block (sb
);
30799 if (ret
== NULL_TREE
)
30801 tree stmt
= make_node (OMP_TARGET
);
30802 TREE_TYPE (stmt
) = void_type_node
;
30803 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
30804 OMP_TARGET_BODY (stmt
) = body
;
30808 else if (!flag_openmp
) /* flag_openmp_simd */
30810 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30813 else if (strcmp (p
, "data") == 0)
30815 cp_lexer_consume_token (parser
->lexer
);
30816 cp_parser_omp_target_data (parser
, pragma_tok
);
30819 else if (strcmp (p
, "update") == 0)
30821 cp_lexer_consume_token (parser
->lexer
);
30822 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
30826 tree stmt
= make_node (OMP_TARGET
);
30827 TREE_TYPE (stmt
) = void_type_node
;
30829 OMP_TARGET_CLAUSES (stmt
)
30830 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
30831 "#pragma omp target", pragma_tok
);
30832 keep_next_level (true);
30833 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30835 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30841 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30843 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30852 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30853 enum pragma_context context
)
30855 bool first_p
= parser
->omp_declare_simd
== NULL
;
30856 cp_omp_declare_simd_data data
;
30859 data
.error_seen
= false;
30860 data
.fndecl_seen
= false;
30861 data
.tokens
= vNULL
;
30862 parser
->omp_declare_simd
= &data
;
30864 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30865 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30866 cp_lexer_consume_token (parser
->lexer
);
30867 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30868 parser
->omp_declare_simd
->error_seen
= true;
30869 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30870 struct cp_token_cache
*cp
30871 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
30872 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
30875 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
30876 cp_parser_pragma (parser
, context
);
30879 case pragma_external
:
30880 cp_parser_declaration (parser
);
30882 case pragma_member
:
30883 cp_parser_member_declaration (parser
);
30885 case pragma_objc_icode
:
30886 cp_parser_block_declaration (parser
, /*statement_p=*/false);
30889 cp_parser_declaration_statement (parser
);
30892 if (parser
->omp_declare_simd
30893 && !parser
->omp_declare_simd
->error_seen
30894 && !parser
->omp_declare_simd
->fndecl_seen
)
30895 error_at (pragma_tok
->location
,
30896 "%<#pragma omp declare simd%> not immediately followed by "
30897 "function declaration or definition");
30898 data
.tokens
.release ();
30899 parser
->omp_declare_simd
= NULL
;
30903 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30904 This function is modelled similar to the late parsing of omp declare
30908 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
30910 struct cp_token_cache
*ce
;
30911 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
30914 if (parser
->omp_declare_simd
!= NULL
)
30916 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30917 " marked as a Cilk Plus SIMD-enabled function");
30918 XDELETE (parser
->cilk_simd_fn_info
);
30919 parser
->cilk_simd_fn_info
= NULL
;
30922 if (!info
->error_seen
&& info
->fndecl_seen
)
30924 error ("vector attribute not immediately followed by a single function"
30925 " declaration or definition");
30926 info
->error_seen
= true;
30928 if (info
->error_seen
)
30931 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
30935 cp_parser_push_lexer_for_tokens (parser
, ce
);
30936 parser
->lexer
->in_pragma
= true;
30937 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
30938 "SIMD-enabled functions attribute",
30940 cp_parser_pop_lexer (parser
);
30942 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30944 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
30945 TREE_CHAIN (c
) = attrs
;
30948 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30949 TREE_CHAIN (c
) = attrs
;
30950 if (processing_template_decl
)
30951 ATTR_IS_DEPENDENT (c
) = 1;
30954 info
->fndecl_seen
= true;
30955 XDELETE (parser
->cilk_simd_fn_info
);
30956 parser
->cilk_simd_fn_info
= NULL
;
30960 /* Finalize #pragma omp declare simd clauses after direct declarator has
30961 been parsed, and put that into "omp declare simd" attribute. */
30964 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
30966 struct cp_token_cache
*ce
;
30967 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
30970 if (!data
->error_seen
&& data
->fndecl_seen
)
30972 error ("%<#pragma omp declare simd%> not immediately followed by "
30973 "a single function declaration or definition");
30974 data
->error_seen
= true;
30977 if (data
->error_seen
)
30980 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
30984 cp_parser_push_lexer_for_tokens (parser
, ce
);
30985 parser
->lexer
->in_pragma
= true;
30986 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
30987 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
30988 cp_lexer_consume_token (parser
->lexer
);
30989 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
30990 "#pragma omp declare simd", pragma_tok
);
30991 cp_parser_pop_lexer (parser
);
30993 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
30994 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
30995 TREE_CHAIN (c
) = attrs
;
30996 if (processing_template_decl
)
30997 ATTR_IS_DEPENDENT (c
) = 1;
31001 data
->fndecl_seen
= true;
31007 # pragma omp declare target new-line
31008 declarations and definitions
31009 # pragma omp end declare target new-line */
31012 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31014 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31015 scope_chain
->omp_declare_target_attribute
++;
31019 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31021 const char *p
= "";
31022 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31024 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31025 p
= IDENTIFIER_POINTER (id
);
31027 if (strcmp (p
, "declare") == 0)
31029 cp_lexer_consume_token (parser
->lexer
);
31031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31033 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31034 p
= IDENTIFIER_POINTER (id
);
31036 if (strcmp (p
, "target") == 0)
31037 cp_lexer_consume_token (parser
->lexer
);
31040 cp_parser_error (parser
, "expected %<target%>");
31041 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31047 cp_parser_error (parser
, "expected %<declare%>");
31048 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31051 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31052 if (!scope_chain
->omp_declare_target_attribute
)
31053 error_at (pragma_tok
->location
,
31054 "%<#pragma omp end declare target%> without corresponding "
31055 "%<#pragma omp declare target%>");
31057 scope_chain
->omp_declare_target_attribute
--;
31060 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31061 expression and optional initializer clause of
31062 #pragma omp declare reduction. We store the expression(s) as
31063 either 3, 6 or 7 special statements inside of the artificial function's
31064 body. The first two statements are DECL_EXPRs for the artificial
31065 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31066 expression that uses those variables.
31067 If there was any INITIALIZER clause, this is followed by further statements,
31068 the fourth and fifth statements are DECL_EXPRs for the artificial
31069 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31070 constructor variant (first token after open paren is not omp_priv),
31071 then the sixth statement is a statement with the function call expression
31072 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31073 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31074 to initialize the OMP_PRIV artificial variable and there is seventh
31075 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31078 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
31080 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
31081 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
31082 type
= TREE_TYPE (type
);
31083 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
31084 DECL_ARTIFICIAL (omp_out
) = 1;
31085 pushdecl (omp_out
);
31086 add_decl_expr (omp_out
);
31087 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
31088 DECL_ARTIFICIAL (omp_in
) = 1;
31090 add_decl_expr (omp_in
);
31092 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
31094 keep_next_level (true);
31095 tree block
= begin_omp_structured_block ();
31096 combiner
= cp_parser_expression (parser
);
31097 finish_expr_stmt (combiner
);
31098 block
= finish_omp_structured_block (block
);
31101 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31104 const char *p
= "";
31105 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31107 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31108 p
= IDENTIFIER_POINTER (id
);
31111 if (strcmp (p
, "initializer") == 0)
31113 cp_lexer_consume_token (parser
->lexer
);
31114 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31120 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31121 p
= IDENTIFIER_POINTER (id
);
31124 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
31125 DECL_ARTIFICIAL (omp_priv
) = 1;
31126 pushdecl (omp_priv
);
31127 add_decl_expr (omp_priv
);
31128 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
31129 DECL_ARTIFICIAL (omp_orig
) = 1;
31130 pushdecl (omp_orig
);
31131 add_decl_expr (omp_orig
);
31133 keep_next_level (true);
31134 block
= begin_omp_structured_block ();
31137 if (strcmp (p
, "omp_priv") == 0)
31139 bool is_direct_init
, is_non_constant_init
;
31141 cp_lexer_consume_token (parser
->lexer
);
31142 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31143 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
31144 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31145 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31147 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
31148 == CPP_CLOSE_PAREN
))
31150 finish_omp_structured_block (block
);
31151 error ("invalid initializer clause");
31154 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
31155 &is_non_constant_init
);
31156 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
31157 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
31161 cp_parser_parse_tentatively (parser
);
31162 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31163 /*check_dependency_p=*/true,
31164 /*template_p=*/NULL
,
31165 /*declarator_p=*/false,
31166 /*optional_p=*/false);
31167 vec
<tree
, va_gc
> *args
;
31168 if (fn_name
== error_mark_node
31169 || cp_parser_error_occurred (parser
)
31170 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31171 || ((args
= cp_parser_parenthesized_expression_list
31172 (parser
, non_attr
, /*cast_p=*/false,
31173 /*allow_expansion_p=*/true,
31174 /*non_constant_p=*/NULL
)),
31175 cp_parser_error_occurred (parser
)))
31177 finish_omp_structured_block (block
);
31178 cp_parser_abort_tentative_parse (parser
);
31179 cp_parser_error (parser
, "expected id-expression (arguments)");
31184 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
31185 if (arg
== omp_priv
31186 || (TREE_CODE (arg
) == ADDR_EXPR
31187 && TREE_OPERAND (arg
, 0) == omp_priv
))
31189 cp_parser_abort_tentative_parse (parser
);
31190 if (arg
== NULL_TREE
)
31191 error ("one of the initializer call arguments should be %<omp_priv%>"
31192 " or %<&omp_priv%>");
31193 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
31195 finish_expr_stmt (initializer
);
31198 block
= finish_omp_structured_block (block
);
31199 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
31200 finish_expr_stmt (block
);
31203 add_decl_expr (omp_orig
);
31205 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31209 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
31210 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
31216 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31217 initializer-clause[opt] new-line
31219 initializer-clause:
31220 initializer (omp_priv initializer)
31221 initializer (function-name (argument-list)) */
31224 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
31225 enum pragma_context
)
31227 auto_vec
<tree
> types
;
31228 enum tree_code reduc_code
= ERROR_MARK
;
31229 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
31231 cp_token
*first_token
;
31232 cp_token_cache
*cp
;
31236 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31237 p
= obstack_alloc (&declarator_obstack
, 0);
31239 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31242 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31245 reduc_code
= PLUS_EXPR
;
31248 reduc_code
= MULT_EXPR
;
31251 reduc_code
= MINUS_EXPR
;
31254 reduc_code
= BIT_AND_EXPR
;
31257 reduc_code
= BIT_XOR_EXPR
;
31260 reduc_code
= BIT_IOR_EXPR
;
31263 reduc_code
= TRUTH_ANDIF_EXPR
;
31266 reduc_code
= TRUTH_ORIF_EXPR
;
31269 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
31272 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31273 "%<|%>, %<&&%>, %<||%> or identifier");
31277 if (reduc_code
!= ERROR_MARK
)
31278 cp_lexer_consume_token (parser
->lexer
);
31280 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
31281 if (reduc_id
== error_mark_node
)
31284 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31287 /* Types may not be defined in declare reduction type list. */
31288 const char *saved_message
;
31289 saved_message
= parser
->type_definition_forbidden_message
;
31290 parser
->type_definition_forbidden_message
31291 = G_("types may not be defined in declare reduction type list");
31292 bool saved_colon_corrects_to_scope_p
;
31293 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31294 parser
->colon_corrects_to_scope_p
= false;
31295 bool saved_colon_doesnt_start_class_def_p
;
31296 saved_colon_doesnt_start_class_def_p
31297 = parser
->colon_doesnt_start_class_def_p
;
31298 parser
->colon_doesnt_start_class_def_p
= true;
31302 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31303 type
= cp_parser_type_id (parser
);
31304 if (type
== error_mark_node
)
31306 else if (ARITHMETIC_TYPE_P (type
)
31307 && (orig_reduc_id
== NULL_TREE
31308 || (TREE_CODE (type
) != COMPLEX_TYPE
31309 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
31311 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
31313 error_at (loc
, "predeclared arithmetic type %qT in "
31314 "%<#pragma omp declare reduction%>", type
);
31315 else if (TREE_CODE (type
) == FUNCTION_TYPE
31316 || TREE_CODE (type
) == METHOD_TYPE
31317 || TREE_CODE (type
) == ARRAY_TYPE
)
31318 error_at (loc
, "function or array type %qT in "
31319 "%<#pragma omp declare reduction%>", type
);
31320 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
31321 error_at (loc
, "reference type %qT in "
31322 "%<#pragma omp declare reduction%>", type
);
31323 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
31324 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
31325 "%<#pragma omp declare reduction%>", type
);
31327 types
.safe_push (type
);
31329 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31330 cp_lexer_consume_token (parser
->lexer
);
31335 /* Restore the saved message. */
31336 parser
->type_definition_forbidden_message
= saved_message
;
31337 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31338 parser
->colon_doesnt_start_class_def_p
31339 = saved_colon_doesnt_start_class_def_p
;
31341 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
31342 || types
.is_empty ())
31345 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31349 first_token
= cp_lexer_peek_token (parser
->lexer
);
31352 FOR_EACH_VEC_ELT (types
, i
, type
)
31355 = build_function_type_list (void_type_node
,
31356 cp_build_reference_type (type
, false),
31358 tree this_reduc_id
= reduc_id
;
31359 if (!dependent_type_p (type
))
31360 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
31361 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
31362 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
31363 DECL_ARTIFICIAL (fndecl
) = 1;
31364 DECL_EXTERNAL (fndecl
) = 1;
31365 DECL_DECLARED_INLINE_P (fndecl
) = 1;
31366 DECL_IGNORED_P (fndecl
) = 1;
31367 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
31368 DECL_ATTRIBUTES (fndecl
)
31369 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
31370 DECL_ATTRIBUTES (fndecl
));
31371 if (processing_template_decl
)
31372 fndecl
= push_template_decl (fndecl
);
31373 bool block_scope
= false;
31374 tree block
= NULL_TREE
;
31375 if (current_function_decl
)
31377 block_scope
= true;
31378 DECL_CONTEXT (fndecl
) = global_namespace
;
31379 if (!processing_template_decl
)
31382 else if (current_class_type
)
31386 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31387 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31388 cp_lexer_consume_token (parser
->lexer
);
31389 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31391 cp
= cp_token_cache_new (first_token
,
31392 cp_lexer_peek_nth_token (parser
->lexer
,
31395 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
31396 finish_member_declaration (fndecl
);
31397 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
31398 DECL_PENDING_INLINE_P (fndecl
) = 1;
31399 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
31404 DECL_CONTEXT (fndecl
) = current_namespace
;
31408 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
31410 block
= begin_omp_structured_block ();
31413 cp_parser_push_lexer_for_tokens (parser
, cp
);
31414 parser
->lexer
->in_pragma
= true;
31416 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
31419 finish_function (0);
31421 DECL_CONTEXT (fndecl
) = current_function_decl
;
31423 cp_parser_pop_lexer (parser
);
31427 cp_parser_pop_lexer (parser
);
31429 finish_function (0);
31432 DECL_CONTEXT (fndecl
) = current_function_decl
;
31433 block
= finish_omp_structured_block (block
);
31434 if (TREE_CODE (block
) == BIND_EXPR
)
31435 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
31436 else if (TREE_CODE (block
) == STATEMENT_LIST
)
31437 DECL_SAVED_TREE (fndecl
) = block
;
31438 if (processing_template_decl
)
31439 add_decl_expr (fndecl
);
31441 cp_check_omp_declare_reduction (fndecl
);
31442 if (cp
== NULL
&& types
.length () > 1)
31443 cp
= cp_token_cache_new (first_token
,
31444 cp_lexer_peek_nth_token (parser
->lexer
, 2));
31445 if (errs
!= errorcount
)
31449 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31452 /* Free any declarators allocated. */
31453 obstack_free (&declarator_obstack
, p
);
31457 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31458 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31459 initializer-clause[opt] new-line
31460 #pragma omp declare target new-line */
31463 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
31464 enum pragma_context context
)
31466 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31468 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31469 const char *p
= IDENTIFIER_POINTER (id
);
31471 if (strcmp (p
, "simd") == 0)
31473 cp_lexer_consume_token (parser
->lexer
);
31474 cp_parser_omp_declare_simd (parser
, pragma_tok
,
31478 cp_ensure_no_omp_declare_simd (parser
);
31479 if (strcmp (p
, "reduction") == 0)
31481 cp_lexer_consume_token (parser
->lexer
);
31482 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
31486 if (!flag_openmp
) /* flag_openmp_simd */
31488 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31491 if (strcmp (p
, "target") == 0)
31493 cp_lexer_consume_token (parser
->lexer
);
31494 cp_parser_omp_declare_target (parser
, pragma_tok
);
31498 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
31500 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31503 /* Main entry point to OpenMP statement pragmas. */
31506 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
31509 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
31510 omp_clause_mask
mask (0);
31512 switch (pragma_tok
->pragma_kind
)
31514 case PRAGMA_OMP_ATOMIC
:
31515 cp_parser_omp_atomic (parser
, pragma_tok
);
31517 case PRAGMA_OMP_CRITICAL
:
31518 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
31520 case PRAGMA_OMP_DISTRIBUTE
:
31521 strcpy (p_name
, "#pragma omp");
31522 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
31524 case PRAGMA_OMP_FOR
:
31525 strcpy (p_name
, "#pragma omp");
31526 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
31528 case PRAGMA_OMP_MASTER
:
31529 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
31531 case PRAGMA_OMP_ORDERED
:
31532 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
31534 case PRAGMA_OMP_PARALLEL
:
31535 strcpy (p_name
, "#pragma omp");
31536 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
31538 case PRAGMA_OMP_SECTIONS
:
31539 strcpy (p_name
, "#pragma omp");
31540 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
31542 case PRAGMA_OMP_SIMD
:
31543 strcpy (p_name
, "#pragma omp");
31544 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
31546 case PRAGMA_OMP_SINGLE
:
31547 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
31549 case PRAGMA_OMP_TASK
:
31550 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
31552 case PRAGMA_OMP_TASKGROUP
:
31553 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
31555 case PRAGMA_OMP_TEAMS
:
31556 strcpy (p_name
, "#pragma omp");
31557 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
31560 gcc_unreachable ();
31564 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31567 /* Transactional Memory parsing routines. */
31569 /* Parse a transaction attribute.
31575 ??? Simplify this when C++0x bracket attributes are
31576 implemented properly. */
31579 cp_parser_txn_attribute_opt (cp_parser
*parser
)
31582 tree attr_name
, attr
= NULL
;
31584 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
31585 return cp_parser_attributes_opt (parser
);
31587 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
31589 cp_lexer_consume_token (parser
->lexer
);
31590 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
31593 token
= cp_lexer_peek_token (parser
->lexer
);
31594 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
31596 token
= cp_lexer_consume_token (parser
->lexer
);
31598 attr_name
= (token
->type
== CPP_KEYWORD
31599 /* For keywords, use the canonical spelling,
31600 not the parsed identifier. */
31601 ? ridpointers
[(int) token
->keyword
]
31603 attr
= build_tree_list (attr_name
, NULL_TREE
);
31606 cp_parser_error (parser
, "expected identifier");
31608 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31610 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31614 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31616 transaction-statement:
31617 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31619 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31623 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
31625 unsigned char old_in
= parser
->in_transaction
;
31626 unsigned char this_in
= 1, new_in
;
31628 tree stmt
, attrs
, noex
;
31630 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31631 || keyword
== RID_TRANSACTION_RELAXED
);
31632 token
= cp_parser_require_keyword (parser
, keyword
,
31633 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31634 : RT_TRANSACTION_RELAXED
));
31635 gcc_assert (token
!= NULL
);
31637 if (keyword
== RID_TRANSACTION_RELAXED
)
31638 this_in
|= TM_STMT_ATTR_RELAXED
;
31641 attrs
= cp_parser_txn_attribute_opt (parser
);
31643 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31646 /* Parse a noexcept specification. */
31647 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
31649 /* Keep track if we're in the lexical scope of an outer transaction. */
31650 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
31652 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
31654 parser
->in_transaction
= new_in
;
31655 cp_parser_compound_statement (parser
, NULL
, false, false);
31656 parser
->in_transaction
= old_in
;
31658 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
31663 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31665 transaction-expression:
31666 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31667 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31671 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
31673 unsigned char old_in
= parser
->in_transaction
;
31674 unsigned char this_in
= 1;
31679 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31680 || keyword
== RID_TRANSACTION_RELAXED
);
31683 error (keyword
== RID_TRANSACTION_RELAXED
31684 ? G_("%<__transaction_relaxed%> without transactional memory "
31686 : G_("%<__transaction_atomic%> without transactional memory "
31687 "support enabled"));
31689 token
= cp_parser_require_keyword (parser
, keyword
,
31690 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31691 : RT_TRANSACTION_RELAXED
));
31692 gcc_assert (token
!= NULL
);
31694 if (keyword
== RID_TRANSACTION_RELAXED
)
31695 this_in
|= TM_STMT_ATTR_RELAXED
;
31697 /* Set this early. This might mean that we allow transaction_cancel in
31698 an expression that we find out later actually has to be a constexpr.
31699 However, we expect that cxx_constant_value will be able to deal with
31700 this; also, if the noexcept has no constexpr, then what we parse next
31701 really is a transaction's body. */
31702 parser
->in_transaction
= this_in
;
31704 /* Parse a noexcept specification. */
31705 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
31708 if (!noex
|| !noex_expr
31709 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31711 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
31713 expr
= cp_parser_expression (parser
);
31714 expr
= finish_parenthesized_expr (expr
);
31716 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
31720 /* The only expression that is available got parsed for the noexcept
31721 already. noexcept is true then. */
31723 noex
= boolean_true_node
;
31726 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
31727 parser
->in_transaction
= old_in
;
31729 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
31730 return error_mark_node
;
31732 return (flag_tm
? expr
: error_mark_node
);
31735 /* Parse a function-transaction-block.
31737 function-transaction-block:
31738 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31740 __transaction_atomic txn-attribute[opt] function-try-block
31741 __transaction_relaxed ctor-initializer[opt] function-body
31742 __transaction_relaxed function-try-block
31746 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
31748 unsigned char old_in
= parser
->in_transaction
;
31749 unsigned char new_in
= 1;
31750 tree compound_stmt
, stmt
, attrs
;
31751 bool ctor_initializer_p
;
31754 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31755 || keyword
== RID_TRANSACTION_RELAXED
);
31756 token
= cp_parser_require_keyword (parser
, keyword
,
31757 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31758 : RT_TRANSACTION_RELAXED
));
31759 gcc_assert (token
!= NULL
);
31761 if (keyword
== RID_TRANSACTION_RELAXED
)
31762 new_in
|= TM_STMT_ATTR_RELAXED
;
31765 attrs
= cp_parser_txn_attribute_opt (parser
);
31767 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31770 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
31772 parser
->in_transaction
= new_in
;
31774 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31775 ctor_initializer_p
= cp_parser_function_try_block (parser
);
31777 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
31778 (parser
, /*in_function_try_block=*/false);
31780 parser
->in_transaction
= old_in
;
31782 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
31784 return ctor_initializer_p
;
31787 /* Parse a __transaction_cancel statement.
31790 __transaction_cancel txn-attribute[opt] ;
31791 __transaction_cancel txn-attribute[opt] throw-expression ;
31793 ??? Cancel and throw is not yet implemented. */
31796 cp_parser_transaction_cancel (cp_parser
*parser
)
31799 bool is_outer
= false;
31802 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
31803 RT_TRANSACTION_CANCEL
);
31804 gcc_assert (token
!= NULL
);
31806 attrs
= cp_parser_txn_attribute_opt (parser
);
31808 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
31810 /* ??? Parse cancel-and-throw here. */
31812 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
31816 error_at (token
->location
, "%<__transaction_cancel%> without "
31817 "transactional memory support enabled");
31818 return error_mark_node
;
31820 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
31822 error_at (token
->location
, "%<__transaction_cancel%> within a "
31823 "%<__transaction_relaxed%>");
31824 return error_mark_node
;
31828 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
31829 && !is_tm_may_cancel_outer (current_function_decl
))
31831 error_at (token
->location
, "outer %<__transaction_cancel%> not "
31832 "within outer %<__transaction_atomic%>");
31833 error_at (token
->location
,
31834 " or a %<transaction_may_cancel_outer%> function");
31835 return error_mark_node
;
31838 else if (parser
->in_transaction
== 0)
31840 error_at (token
->location
, "%<__transaction_cancel%> not within "
31841 "%<__transaction_atomic%>");
31842 return error_mark_node
;
31845 stmt
= build_tm_abort_call (token
->location
, is_outer
);
31853 static GTY (()) cp_parser
*the_parser
;
31856 /* Special handling for the first token or line in the file. The first
31857 thing in the file might be #pragma GCC pch_preprocess, which loads a
31858 PCH file, which is a GC collection point. So we need to handle this
31859 first pragma without benefit of an existing lexer structure.
31861 Always returns one token to the caller in *FIRST_TOKEN. This is
31862 either the true first token of the file, or the first token after
31863 the initial pragma. */
31866 cp_parser_initial_pragma (cp_token
*first_token
)
31870 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31871 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
31874 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31875 if (first_token
->type
== CPP_STRING
)
31877 name
= first_token
->u
.value
;
31879 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31880 if (first_token
->type
!= CPP_PRAGMA_EOL
)
31881 error_at (first_token
->location
,
31882 "junk at end of %<#pragma GCC pch_preprocess%>");
31885 error_at (first_token
->location
, "expected string literal");
31887 /* Skip to the end of the pragma. */
31888 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
31889 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31891 /* Now actually load the PCH file. */
31893 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
31895 /* Read one more token to return to our caller. We have to do this
31896 after reading the PCH file in, since its pointers have to be
31898 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31901 /* Parses the grainsize pragma for the _Cilk_for statement.
31903 #pragma cilk grainsize = <VALUE>. */
31906 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
31908 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
31910 tree exp
= cp_parser_binary_expression (parser
, false, false,
31911 PREC_NOT_OPERATOR
, NULL
);
31912 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31913 if (!exp
|| exp
== error_mark_node
)
31915 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
31919 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31920 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
31921 cp_parser_cilk_for (parser
, exp
);
31923 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
31924 "%<#pragma cilk grainsize%> is not followed by "
31928 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31931 /* Normal parsing of a pragma token. Here we can (and must) use the
31935 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
31937 cp_token
*pragma_tok
;
31940 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31941 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
31942 parser
->lexer
->in_pragma
= true;
31944 id
= pragma_tok
->pragma_kind
;
31945 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
31946 cp_ensure_no_omp_declare_simd (parser
);
31949 case PRAGMA_GCC_PCH_PREPROCESS
:
31950 error_at (pragma_tok
->location
,
31951 "%<#pragma GCC pch_preprocess%> must be first");
31954 case PRAGMA_OMP_BARRIER
:
31957 case pragma_compound
:
31958 cp_parser_omp_barrier (parser
, pragma_tok
);
31961 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
31962 "used in compound statements");
31969 case PRAGMA_OMP_FLUSH
:
31972 case pragma_compound
:
31973 cp_parser_omp_flush (parser
, pragma_tok
);
31976 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
31977 "used in compound statements");
31984 case PRAGMA_OMP_TASKWAIT
:
31987 case pragma_compound
:
31988 cp_parser_omp_taskwait (parser
, pragma_tok
);
31991 error_at (pragma_tok
->location
,
31992 "%<#pragma omp taskwait%> may only be "
31993 "used in compound statements");
32000 case PRAGMA_OMP_TASKYIELD
:
32003 case pragma_compound
:
32004 cp_parser_omp_taskyield (parser
, pragma_tok
);
32007 error_at (pragma_tok
->location
,
32008 "%<#pragma omp taskyield%> may only be "
32009 "used in compound statements");
32016 case PRAGMA_OMP_CANCEL
:
32019 case pragma_compound
:
32020 cp_parser_omp_cancel (parser
, pragma_tok
);
32023 error_at (pragma_tok
->location
,
32024 "%<#pragma omp cancel%> may only be "
32025 "used in compound statements");
32032 case PRAGMA_OMP_CANCELLATION_POINT
:
32035 case pragma_compound
:
32036 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
32039 error_at (pragma_tok
->location
,
32040 "%<#pragma omp cancellation point%> may only be "
32041 "used in compound statements");
32048 case PRAGMA_OMP_THREADPRIVATE
:
32049 cp_parser_omp_threadprivate (parser
, pragma_tok
);
32052 case PRAGMA_OMP_DECLARE_REDUCTION
:
32053 cp_parser_omp_declare (parser
, pragma_tok
, context
);
32056 case PRAGMA_OMP_ATOMIC
:
32057 case PRAGMA_OMP_CRITICAL
:
32058 case PRAGMA_OMP_DISTRIBUTE
:
32059 case PRAGMA_OMP_FOR
:
32060 case PRAGMA_OMP_MASTER
:
32061 case PRAGMA_OMP_ORDERED
:
32062 case PRAGMA_OMP_PARALLEL
:
32063 case PRAGMA_OMP_SECTIONS
:
32064 case PRAGMA_OMP_SIMD
:
32065 case PRAGMA_OMP_SINGLE
:
32066 case PRAGMA_OMP_TASK
:
32067 case PRAGMA_OMP_TASKGROUP
:
32068 case PRAGMA_OMP_TEAMS
:
32069 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
32071 cp_parser_omp_construct (parser
, pragma_tok
);
32074 case PRAGMA_OMP_TARGET
:
32075 return cp_parser_omp_target (parser
, pragma_tok
, context
);
32077 case PRAGMA_OMP_END_DECLARE_TARGET
:
32078 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
32081 case PRAGMA_OMP_SECTION
:
32082 error_at (pragma_tok
->location
,
32083 "%<#pragma omp section%> may only be used in "
32084 "%<#pragma omp sections%> construct");
32089 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32091 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32092 if (tok
->type
!= CPP_KEYWORD
32093 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
32094 && tok
->keyword
!= RID_DO
))
32096 cp_parser_error (parser
, "for, while or do statement expected");
32099 cp_parser_iteration_statement (parser
, true);
32103 case PRAGMA_CILK_SIMD
:
32104 if (context
== pragma_external
)
32106 error_at (pragma_tok
->location
,
32107 "%<#pragma simd%> must be inside a function");
32110 cp_parser_cilk_simd (parser
, pragma_tok
);
32113 case PRAGMA_CILK_GRAINSIZE
:
32114 if (context
== pragma_external
)
32116 error_at (pragma_tok
->location
,
32117 "%<#pragma cilk grainsize%> must be inside a function");
32121 /* Ignore the pragma if Cilk Plus is not enabled. */
32124 cp_parser_cilk_grainsize (parser
, pragma_tok
);
32129 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
32130 "%<#pragma cilk grainsize%>");
32135 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
32136 c_invoke_pragma_handler (id
);
32140 cp_parser_error (parser
, "expected declaration specifiers");
32144 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32148 /* The interface the pragma parsers have to the lexer. */
32151 pragma_lex (tree
*value
)
32154 enum cpp_ttype ret
;
32156 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32159 *value
= tok
->u
.value
;
32161 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
32163 else if (ret
== CPP_STRING
)
32164 *value
= cp_parser_string_literal (the_parser
, false, false);
32167 cp_lexer_consume_token (the_parser
->lexer
);
32168 if (ret
== CPP_KEYWORD
)
32176 /* External interface. */
32178 /* Parse one entire translation unit. */
32181 c_parse_file (void)
32183 static bool already_called
= false;
32185 if (already_called
)
32186 fatal_error ("inter-module optimizations not implemented for C++");
32187 already_called
= true;
32189 the_parser
= cp_parser_new ();
32190 push_deferring_access_checks (flag_access_control
32191 ? dk_no_deferred
: dk_no_check
);
32192 cp_parser_translation_unit (the_parser
);
32196 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32197 vectorlength clause:
32199 vectorlength ( constant-expression ) */
32202 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
32205 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32207 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32208 safelen clause. Thus, vectorlength is represented as OMP 4.0
32209 safelen. For SIMD-enabled function it is represented by OMP 4.0
32212 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
32215 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
32218 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32219 return error_mark_node
;
32221 expr
= cp_parser_constant_expression (parser
);
32222 expr
= maybe_constant_value (expr
);
32224 /* If expr == error_mark_node, then don't emit any errors nor
32225 create a clause. if any of the above functions returns
32226 error mark node then they would have emitted an error message. */
32227 if (expr
== error_mark_node
)
32229 else if (!TREE_TYPE (expr
)
32230 || !TREE_CONSTANT (expr
)
32231 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
32232 error_at (loc
, "vectorlength must be an integer constant");
32233 else if (TREE_CONSTANT (expr
)
32234 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
32235 error_at (loc
, "vectorlength must be a power of 2");
32241 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
32242 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
32243 OMP_CLAUSE_CHAIN (c
) = clauses
;
32248 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
32249 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
32250 OMP_CLAUSE_CHAIN (c
) = clauses
;
32255 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32256 return error_mark_node
;
32260 /* Handles the Cilk Plus #pragma simd linear clause.
32262 linear ( simd-linear-variable-list )
32264 simd-linear-variable-list:
32265 simd-linear-variable
32266 simd-linear-variable-list , simd-linear-variable
32268 simd-linear-variable:
32270 id-expression : simd-linear-step
32273 conditional-expression */
32276 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
32278 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32280 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32282 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32284 cp_parser_error (parser
, "expected identifier");
32285 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32286 return error_mark_node
;
32289 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32290 parser
->colon_corrects_to_scope_p
= false;
32293 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32294 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32296 cp_parser_error (parser
, "expected variable-name");
32297 clauses
= error_mark_node
;
32301 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
32303 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
32305 if (decl
== error_mark_node
)
32307 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
32309 clauses
= error_mark_node
;
32313 tree e
= NULL_TREE
;
32314 tree step_size
= integer_one_node
;
32316 /* If present, parse the linear step. Otherwise, assume the default
32318 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
32320 cp_lexer_consume_token (parser
->lexer
);
32322 e
= cp_parser_assignment_expression (parser
);
32323 e
= maybe_constant_value (e
);
32325 if (e
== error_mark_node
)
32327 /* If an error has occurred, then the whole pragma is
32328 considered ill-formed. Thus, no reason to keep
32330 clauses
= error_mark_node
;
32333 else if (type_dependent_expression_p (e
)
32334 || value_dependent_expression_p (e
)
32336 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
32337 && (TREE_CONSTANT (e
)
32341 cp_parser_error (parser
,
32342 "step size must be an integer constant "
32343 "expression or an integer variable");
32346 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32347 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
32348 OMP_CLAUSE_DECL (l
) = decl
;
32349 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
32350 OMP_CLAUSE_CHAIN (l
) = clauses
;
32353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32354 cp_lexer_consume_token (parser
->lexer
);
32355 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32359 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32360 "expected %<,%> or %<)%> after %qE", decl
);
32361 clauses
= error_mark_node
;
32365 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32366 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32370 /* Returns the name of the next clause. If the clause is not
32371 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32372 token is not consumed. Otherwise, the appropriate enum from the
32373 pragma_simd_clause is returned and the token is consumed. */
32375 static pragma_omp_clause
32376 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
32378 pragma_omp_clause clause_type
;
32379 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32381 if (token
->keyword
== RID_PRIVATE
)
32382 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
32383 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
32384 return PRAGMA_CILK_CLAUSE_NONE
;
32385 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
32386 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
32387 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
32388 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
32389 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
32390 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
32391 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
32392 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
32393 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
32394 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
32396 return PRAGMA_CILK_CLAUSE_NONE
;
32398 cp_lexer_consume_token (parser
->lexer
);
32399 return clause_type
;
32402 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32405 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
32407 tree clauses
= NULL_TREE
;
32409 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32410 && clauses
!= error_mark_node
)
32412 pragma_omp_clause c_kind
;
32413 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
32414 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
32415 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
32416 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
32417 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
32418 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
32419 /* Use the OpenMP 4.0 equivalent function. */
32420 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
32421 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
32422 /* Use the OpenMP 4.0 equivalent function. */
32423 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
32425 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
32426 /* Use the OMP 4.0 equivalent function. */
32427 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
32429 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
32430 /* Use the OMP 4.0 equivalent function. */
32431 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
32434 clauses
= error_mark_node
;
32435 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
32440 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
32442 if (clauses
== error_mark_node
)
32443 return error_mark_node
;
32445 return c_finish_cilk_clauses (clauses
);
32448 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32451 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
32453 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
32455 if (clauses
== error_mark_node
)
32458 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
32460 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32461 "for statement expected");
32465 tree sb
= begin_omp_structured_block ();
32466 int save
= cp_parser_begin_omp_structured_block (parser
);
32467 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
32469 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
32470 cp_parser_end_omp_structured_block (parser
, save
);
32471 add_stmt (finish_omp_structured_block (sb
));
32474 /* Main entry-point for parsing Cilk Plus _Cilk_for
32475 loops. The return value is error_mark_node
32476 when errors happen and CILK_FOR tree on success. */
32479 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
32481 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
32482 gcc_unreachable ();
32484 tree sb
= begin_omp_structured_block ();
32485 int save
= cp_parser_begin_omp_structured_block (parser
);
32487 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
32488 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
32489 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
32490 clauses
= finish_omp_clauses (clauses
);
32492 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
32494 cpp_validate_cilk_plus_loop (ret
);
32496 ret
= error_mark_node
;
32498 cp_parser_end_omp_structured_block (parser
, save
);
32499 add_stmt (finish_omp_structured_block (sb
));
32503 /* Create an identifier for a generic parameter type (a synthesized
32504 template parameter implied by `auto' or a concept identifier). */
32506 static GTY(()) int generic_parm_count
;
32508 make_generic_type_name ()
32511 sprintf (buf
, "auto:%d", ++generic_parm_count
);
32512 return get_identifier (buf
);
32515 /* Predicate that behaves as is_auto_or_concept but matches the parent
32516 node of the generic type rather than the generic type itself. This
32517 allows for type transformation in add_implicit_template_parms. */
32520 tree_type_is_auto_or_concept (const_tree t
)
32522 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
32525 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32526 (creating a new template parameter list if necessary). Returns the newly
32527 created template type parm. */
32530 synthesize_implicit_template_parm (cp_parser
*parser
)
32532 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
32534 /* We are either continuing a function template that already contains implicit
32535 template parameters, creating a new fully-implicit function template, or
32536 extending an existing explicit function template with implicit template
32539 cp_binding_level
*const entry_scope
= current_binding_level
;
32541 bool become_template
= false;
32542 cp_binding_level
*parent_scope
= 0;
32544 if (parser
->implicit_template_scope
)
32546 gcc_assert (parser
->implicit_template_parms
);
32548 current_binding_level
= parser
->implicit_template_scope
;
32552 /* Roll back to the existing template parameter scope (in the case of
32553 extending an explicit function template) or introduce a new template
32554 parameter scope ahead of the function parameter scope (or class scope
32555 in the case of out-of-line member definitions). The function scope is
32556 added back after template parameter synthesis below. */
32558 cp_binding_level
*scope
= entry_scope
;
32560 while (scope
->kind
== sk_function_parms
)
32562 parent_scope
= scope
;
32563 scope
= scope
->level_chain
;
32565 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
32567 /* If not defining a class, then any class scope is a scope level in
32568 an out-of-line member definition. In this case simply wind back
32569 beyond the first such scope to inject the template parameter list.
32570 Otherwise wind back to the class being defined. The latter can
32571 occur in class member friend declarations such as:
32577 friend void A::foo (auto);
32580 The template parameter list synthesized for the friend declaration
32581 must be injected in the scope of 'B'. This can also occur in
32582 erroneous cases such as:
32588 void B::foo (auto) {}
32591 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32592 but, nevertheless, the template parameter list synthesized for the
32593 declarator should be injected into the scope of 'A' as if the
32594 ill-formed template was specified explicitly. */
32596 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
32598 parent_scope
= scope
;
32599 scope
= scope
->level_chain
;
32603 current_binding_level
= scope
;
32605 if (scope
->kind
!= sk_template_parms
32606 || !function_being_declared_is_template_p (parser
))
32608 /* Introduce a new template parameter list for implicit template
32611 become_template
= true;
32613 parser
->implicit_template_scope
32614 = begin_scope (sk_template_parms
, NULL
);
32616 ++processing_template_decl
;
32618 parser
->fully_implicit_function_template_p
= true;
32619 ++parser
->num_template_parameter_lists
;
32623 /* Synthesize implicit template parameters at the end of the explicit
32624 template parameter list. */
32626 gcc_assert (current_template_parms
);
32628 parser
->implicit_template_scope
= scope
;
32630 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32631 parser
->implicit_template_parms
32632 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
32636 /* Synthesize a new template parameter and track the current template
32637 parameter chain with implicit_template_parms. */
32639 tree synth_id
= make_generic_type_name ();
32640 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
32643 = process_template_parm (parser
->implicit_template_parms
,
32645 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
32646 /*non_type=*/false,
32647 /*param_pack=*/false);
32650 if (parser
->implicit_template_parms
)
32651 parser
->implicit_template_parms
32652 = TREE_CHAIN (parser
->implicit_template_parms
);
32654 parser
->implicit_template_parms
= new_parm
;
32656 tree new_type
= TREE_TYPE (getdecls ());
32658 /* If creating a fully implicit function template, start the new implicit
32659 template parameter list with this synthesized type, otherwise grow the
32660 current template parameter list. */
32662 if (become_template
)
32664 parent_scope
->level_chain
= current_binding_level
;
32666 tree new_parms
= make_tree_vec (1);
32667 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
32668 current_template_parms
= tree_cons (size_int (processing_template_decl
),
32669 new_parms
, current_template_parms
);
32673 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32674 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
32675 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
32676 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
32679 current_binding_level
= entry_scope
;
32684 /* Finish the declaration of a fully implicit function template. Such a
32685 template has no explicit template parameter list so has not been through the
32686 normal template head and tail processing. synthesize_implicit_template_parm
32687 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32688 provided if the declaration is a class member such that its template
32689 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32690 form is returned. Otherwise NULL_TREE is returned. */
32693 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
32695 gcc_assert (parser
->fully_implicit_function_template_p
);
32697 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
32698 && DECL_VIRTUAL_P (member_decl_opt
))
32700 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
32701 "implicit templates may not be %<virtual%>");
32702 DECL_VIRTUAL_P (member_decl_opt
) = false;
32705 if (member_decl_opt
)
32706 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
32707 end_template_decl ();
32709 parser
->fully_implicit_function_template_p
= false;
32710 --parser
->num_template_parameter_lists
;
32712 return member_decl_opt
;
32715 #include "gt-cp-parser.h"