2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
30 #include "double-int.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
40 #include "trans-mem.h"
43 #include "c-family/c-pragma.h"
46 #include "diagnostic-core.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
59 #include "tree-pretty-print.h"
61 #include "type-utils.h"
67 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
68 and c-lex.c) and the C++ parser. */
70 static cp_token eof_token
=
72 CPP_EOF
, RID_MAX
, 0, PRAGMA_NONE
, false, false, false, 0, { NULL
}
75 /* The various kinds of non integral constant we encounter. */
76 typedef enum non_integral_constant
{
78 /* floating-point literal */
82 /* %<__FUNCTION__%> */
84 /* %<__PRETTY_FUNCTION__%> */
92 /* %<typeid%> operator */
94 /* non-constant compound literals */
102 /* an array reference */
108 /* the address of a label */
122 /* calls to overloaded operators */
126 /* a comma operator */
128 /* a call to a constructor */
130 /* a transaction expression */
132 } non_integral_constant
;
134 /* The various kinds of errors about name-lookup failing. */
135 typedef enum name_lookup_error
{
140 /* is not a class or namespace */
142 /* is not a class, namespace, or enumeration */
146 /* The various kinds of required token */
147 typedef enum required_token
{
149 RT_SEMICOLON
, /* ';' */
150 RT_OPEN_PAREN
, /* '(' */
151 RT_CLOSE_BRACE
, /* '}' */
152 RT_OPEN_BRACE
, /* '{' */
153 RT_CLOSE_SQUARE
, /* ']' */
154 RT_OPEN_SQUARE
, /* '[' */
158 RT_GREATER
, /* '>' */
160 RT_ELLIPSIS
, /* '...' */
164 RT_COLON_SCOPE
, /* ':' or '::' */
165 RT_CLOSE_PAREN
, /* ')' */
166 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
167 RT_PRAGMA_EOL
, /* end of line */
168 RT_NAME
, /* identifier */
170 /* The type is CPP_KEYWORD */
172 RT_DELETE
, /* delete */
173 RT_RETURN
, /* return */
174 RT_WHILE
, /* while */
175 RT_EXTERN
, /* extern */
176 RT_STATIC_ASSERT
, /* static_assert */
177 RT_DECLTYPE
, /* decltype */
178 RT_OPERATOR
, /* operator */
179 RT_CLASS
, /* class */
180 RT_TEMPLATE
, /* template */
181 RT_NAMESPACE
, /* namespace */
182 RT_USING
, /* using */
185 RT_CATCH
, /* catch */
186 RT_THROW
, /* throw */
187 RT_LABEL
, /* __label__ */
188 RT_AT_TRY
, /* @try */
189 RT_AT_SYNCHRONIZED
, /* @synchronized */
190 RT_AT_THROW
, /* @throw */
192 RT_SELECT
, /* selection-statement */
193 RT_INTERATION
, /* iteration-statement */
194 RT_JUMP
, /* jump-statement */
195 RT_CLASS_KEY
, /* class-key */
196 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
197 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
198 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
199 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer
*, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
250 static void cp_parser_initial_pragma
253 static tree cp_literal_operator_id
256 static void cp_parser_cilk_simd
257 (cp_parser
*, cp_token
*);
258 static tree cp_parser_cilk_for
260 static bool cp_parser_omp_declare_reduction_exprs
262 static tree cp_parser_cilk_simd_vectorlength
263 (cp_parser
*, tree
, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream
;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand
;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
285 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
286 cp_token
*start_token
, unsigned num
,
287 cp_token
*curr_token
)
289 unsigned i
, nprinted
;
293 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
299 num
= buffer
->length ();
301 if (start_token
== NULL
)
302 start_token
= buffer
->address ();
304 if (start_token
> buffer
->address ())
306 cp_lexer_print_token (file
, &(*buffer
)[0]);
307 fprintf (file
, " ... ");
312 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
314 if (token
== start_token
)
321 if (token
== curr_token
)
322 fprintf (file
, "[[");
324 cp_lexer_print_token (file
, token
);
326 if (token
== curr_token
)
327 fprintf (file
, "]]");
333 case CPP_CLOSE_BRACE
:
343 if (i
== num
&& i
< buffer
->length ())
345 fprintf (file
, " ... ");
346 cp_lexer_print_token (file
, &buffer
->last ());
349 fprintf (file
, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
356 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
358 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> &ref
)
364 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
368 debug (vec
<cp_token
, va_gc
> *ptr
)
373 fprintf (stderr
, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
381 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
385 fprintf (file
, "%s: ", desc
);
386 print_node_brief (file
, "", t
, 0);
391 /* Dump parser context C to FILE. */
394 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
396 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
398 print_node_brief (file
, "", c
->object_type
, 0);
399 fprintf (file
, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
406 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
409 cp_parser_context
*c
;
411 fprintf (file
, "Parsing context stack:\n");
412 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
414 fprintf (file
, "\t#%u: ", i
);
415 cp_debug_print_context (file
, c
);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
423 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
426 fprintf (file
, "%s: true\n", desc
);
430 /* Print an unparsed function entry UF to FILE. */
433 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
436 cp_default_arg_entry
*default_arg_fn
;
439 fprintf (file
, "\tFunctions with default args:\n");
441 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
444 fprintf (file
, "\t\tClass type: ");
445 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
446 fprintf (file
, "\t\tDeclaration: ");
447 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
448 fprintf (file
, "\n");
451 fprintf (file
, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
455 print_node_brief (file
, "", fn
, 0);
458 fprintf (file
, "\n");
460 fprintf (file
, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
464 print_node_brief (file
, "", fn
, 0);
467 fprintf (file
, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
474 cp_debug_print_unparsed_queues (FILE *file
,
475 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
478 cp_unparsed_functions_entry
*uf
;
480 fprintf (file
, "Unparsed functions\n");
481 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
483 fprintf (file
, "#%u:\n", i
);
484 cp_debug_print_unparsed_function (file
, uf
);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
493 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
495 cp_token
*next_token
, *first_token
, *start_token
;
500 next_token
= parser
->lexer
->next_token
;
501 first_token
= parser
->lexer
->buffer
->address ();
502 start_token
= (next_token
> first_token
+ window_size
/ 2)
503 ? next_token
- window_size
/ 2
505 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
514 cp_debug_parser (FILE *file
, cp_parser
*parser
)
516 const size_t window_size
= 20;
518 expanded_location eloc
;
523 fprintf (file
, "Parser state\n\n");
524 fprintf (file
, "Number of tokens: %u\n",
525 vec_safe_length (parser
->lexer
->buffer
));
526 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
527 cp_debug_print_tree_if_set (file
, "Object scope",
528 parser
->object_scope
);
529 cp_debug_print_tree_if_set (file
, "Qualifying scope",
530 parser
->qualifying_scope
);
531 cp_debug_print_context_stack (file
, parser
->context
);
532 cp_debug_print_flag (file
, "Allow GNU extensions",
533 parser
->allow_gnu_extensions_p
);
534 cp_debug_print_flag (file
, "'>' token is greater-than",
535 parser
->greater_than_is_operator_p
);
536 cp_debug_print_flag (file
, "Default args allowed in current "
537 "parameter list", parser
->default_arg_ok_p
);
538 cp_debug_print_flag (file
, "Parsing integral constant-expression",
539 parser
->integral_constant_expression_p
);
540 cp_debug_print_flag (file
, "Allow non-constant expression in current "
541 "constant-expression",
542 parser
->allow_non_integral_constant_expression_p
);
543 cp_debug_print_flag (file
, "Seen non-constant expression",
544 parser
->non_integral_constant_expression_p
);
545 cp_debug_print_flag (file
, "Local names and 'this' forbidden in "
547 parser
->local_variables_forbidden_p
);
548 cp_debug_print_flag (file
, "In unbraced linkage specification",
549 parser
->in_unbraced_linkage_specification_p
);
550 cp_debug_print_flag (file
, "Parsing a declarator",
551 parser
->in_declarator_p
);
552 cp_debug_print_flag (file
, "In template argument list",
553 parser
->in_template_argument_list_p
);
554 cp_debug_print_flag (file
, "Parsing an iteration statement",
555 parser
->in_statement
& IN_ITERATION_STMT
);
556 cp_debug_print_flag (file
, "Parsing a switch statement",
557 parser
->in_statement
& IN_SWITCH_STMT
);
558 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
559 parser
->in_statement
& IN_OMP_BLOCK
);
560 cp_debug_print_flag (file
, "Parsing a Cilk Plus for loop",
561 parser
->in_statement
& IN_CILK_SIMD_FOR
);
562 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
563 parser
->in_statement
& IN_OMP_FOR
);
564 cp_debug_print_flag (file
, "Parsing an if statement",
565 parser
->in_statement
& IN_IF_STMT
);
566 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
567 "context", parser
->in_type_id_in_expr_p
);
568 cp_debug_print_flag (file
, "Declarations are implicitly extern \"C\"",
569 parser
->implicit_extern_c
);
570 cp_debug_print_flag (file
, "String expressions should be translated "
571 "to execution character set",
572 parser
->translate_strings_p
);
573 cp_debug_print_flag (file
, "Parsing function body outside of a "
574 "local class", parser
->in_function_body
);
575 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
576 parser
->colon_corrects_to_scope_p
);
577 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
578 parser
->colon_doesnt_start_class_def_p
);
579 if (parser
->type_definition_forbidden_message
)
580 fprintf (file
, "Error message for forbidden type definitions: %s\n",
581 parser
->type_definition_forbidden_message
);
582 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
583 fprintf (file
, "Number of class definitions in progress: %u\n",
584 parser
->num_classes_being_defined
);
585 fprintf (file
, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser
->num_template_parameter_lists
);
587 cp_debug_parser_tokens (file
, parser
, window_size
);
588 token
= parser
->lexer
->next_token
;
589 fprintf (file
, "Next token to parse:\n");
590 fprintf (file
, "\tToken: ");
591 cp_lexer_print_token (file
, token
);
592 eloc
= expand_location (token
->location
);
593 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
594 fprintf (file
, "\tLine: %d\n", eloc
.line
);
595 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
599 debug (cp_parser
&ref
)
601 cp_debug_parser (stderr
, &ref
);
605 debug (cp_parser
*ptr
)
610 fprintf (stderr
, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
616 cp_lexer_alloc (void)
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
625 /* Initially we are not debugging. */
626 lexer
->debugging_p
= false;
628 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
630 /* Create the buffer. */
631 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
641 cp_lexer_new_main (void)
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token
);
651 lexer
= cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer
->buffer
->quick_push (token
);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token
.type
!= CPP_EOF
)
659 cp_lexer_get_preprocessor_token (lexer
, &token
);
660 vec_safe_push (lexer
->buffer
, token
);
663 lexer
->last_token
= lexer
->buffer
->address ()
664 + lexer
->buffer
->length ()
666 lexer
->next_token
= lexer
->buffer
->length ()
667 ? lexer
->buffer
->address ()
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
674 gcc_assert (!lexer
->next_token
->purged_p
);
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
682 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
684 cp_token
*first
= cache
->first
;
685 cp_token
*last
= cache
->last
;
686 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
688 /* We do not own the buffer. */
689 lexer
->buffer
= NULL
;
690 lexer
->next_token
= first
== last
? &eof_token
: first
;
691 lexer
->last_token
= last
;
693 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
695 /* Initially we are not debugging. */
696 lexer
->debugging_p
= false;
698 gcc_assert (!lexer
->next_token
->purged_p
);
702 /* Frees all resources associated with LEXER. */
705 cp_lexer_destroy (cp_lexer
*lexer
)
707 vec_free (lexer
->buffer
);
708 lexer
->saved_tokens
.release ();
712 /* Returns nonzero if debugging information should be output. */
715 cp_lexer_debugging_p (cp_lexer
*lexer
)
717 return lexer
->debugging_p
;
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
724 gcc_assert (!previous_p
|| lexer
->next_token
!= &eof_token
);
726 return lexer
->next_token
- previous_p
;
729 static inline cp_token
*
730 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
736 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
738 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer
*lexer
)
744 if (lexer
->next_token
== &eof_token
)
745 return lexer
->last_token
- 1;
747 return cp_lexer_token_position (lexer
, true);
750 static inline cp_token
*
751 cp_lexer_previous_token (cp_lexer
*lexer
)
753 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
755 return cp_lexer_token_at (lexer
, tp
);
758 /* nonzero if we are presently saving tokens. */
761 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
763 return lexer
->saved_tokens
.length () != 0;
766 /* Store the next token from the preprocessor in *TOKEN. Return true
767 if we reach EOF. If LEXER is NULL, assume we are handling an
768 initial #pragma pch_preprocess, and thus want the lexer to return
769 processed strings. */
772 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
774 static int is_extern_c
= 0;
776 /* Get a new token from the preprocessor. */
778 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
779 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
780 token
->keyword
= RID_MAX
;
781 token
->pragma_kind
= PRAGMA_NONE
;
782 token
->purged_p
= false;
783 token
->error_reported
= false;
785 /* On some systems, some header files are surrounded by an
786 implicit extern "C" block. Set a flag in the token if it
787 comes from such a header. */
788 is_extern_c
+= pending_lang_change
;
789 pending_lang_change
= 0;
790 token
->implicit_extern_c
= is_extern_c
> 0;
792 /* Check to see if this token is a keyword. */
793 if (token
->type
== CPP_NAME
)
795 if (C_IS_RESERVED_WORD (token
->u
.value
))
797 /* Mark this token as a keyword. */
798 token
->type
= CPP_KEYWORD
;
799 /* Record which keyword. */
800 token
->keyword
= C_RID_CODE (token
->u
.value
);
804 if (warn_cxx0x_compat
805 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX0X
806 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX0X
)
808 /* Warn about the C++0x keyword (but still treat it as
810 warning (OPT_Wc__0x_compat
,
811 "identifier %qE is a keyword in C++11",
814 /* Clear out the C_RID_CODE so we don't warn about this
815 particular identifier-turned-keyword again. */
816 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
819 token
->keyword
= RID_MAX
;
822 else if (token
->type
== CPP_AT_NAME
)
824 /* This only happens in Objective-C++; it must be a keyword. */
825 token
->type
= CPP_KEYWORD
;
826 switch (C_RID_CODE (token
->u
.value
))
828 /* Replace 'class' with '@class', 'private' with '@private',
829 etc. This prevents confusion with the C++ keyword
830 'class', and makes the tokens consistent with other
831 Objective-C 'AT' keywords. For example '@class' is
832 reported as RID_AT_CLASS which is consistent with
833 '@synchronized', which is reported as
836 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
837 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
838 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
839 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
840 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
841 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
842 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
843 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
846 else if (token
->type
== CPP_PRAGMA
)
848 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
849 token
->pragma_kind
= ((enum pragma_kind
)
850 TREE_INT_CST_LOW (token
->u
.value
));
851 token
->u
.value
= NULL_TREE
;
855 /* Update the globals input_location and the input file stack from TOKEN. */
857 cp_lexer_set_source_position_from_token (cp_token
*token
)
859 if (token
->type
!= CPP_EOF
)
861 input_location
= token
->location
;
865 /* Update the globals input_location and the input file stack from LEXER. */
867 cp_lexer_set_source_position (cp_lexer
*lexer
)
869 cp_token
*token
= cp_lexer_peek_token (lexer
);
870 cp_lexer_set_source_position_from_token (token
);
873 /* Return a pointer to the next token in the token stream, but do not
876 static inline cp_token
*
877 cp_lexer_peek_token (cp_lexer
*lexer
)
879 if (cp_lexer_debugging_p (lexer
))
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
882 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
883 putc ('\n', cp_lexer_debug_stream
);
885 return lexer
->next_token
;
888 /* Return true if the next token has the indicated TYPE. */
891 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
893 return cp_lexer_peek_token (lexer
)->type
== type
;
896 /* Return true if the next token does not have the indicated TYPE. */
899 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
901 return !cp_lexer_next_token_is (lexer
, type
);
904 /* Return true if the next token is the indicated KEYWORD. */
907 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
909 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
913 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
915 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
919 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
921 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
924 /* Return true if the next token is not the indicated KEYWORD. */
927 cp_lexer_next_token_is_not_keyword (cp_lexer
* lexer
, enum rid keyword
)
929 return cp_lexer_peek_token (lexer
)->keyword
!= keyword
;
932 /* Return true if the next token is a keyword for a decl-specifier. */
935 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
939 token
= cp_lexer_peek_token (lexer
);
940 switch (token
->keyword
)
942 /* auto specifier: storage-class-specifier in C++,
943 simple-type-specifier in C++0x. */
945 /* Storage classes. */
951 /* Elaborated type specifiers. */
957 /* Simple type specifiers. */
971 /* GNU extensions. */
974 /* C++0x extensions. */
976 case RID_UNDERLYING_TYPE
:
980 if (token
->keyword
>= RID_FIRST_INT_N
981 && token
->keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
982 && int_n_enabled_p
[token
->keyword
- RID_FIRST_INT_N
])
988 /* Returns TRUE iff the token T begins a decltype type. */
991 token_is_decltype (cp_token
*t
)
993 return (t
->keyword
== RID_DECLTYPE
994 || t
->type
== CPP_DECLTYPE
);
997 /* Returns TRUE iff the next token begins a decltype type. */
1000 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1002 cp_token
*t
= cp_lexer_peek_token (lexer
);
1003 return token_is_decltype (t
);
1006 /* Return a pointer to the Nth token in the token stream. If N is 1,
1007 then this is precisely equivalent to cp_lexer_peek_token (except
1008 that it is not inline). One would like to disallow that case, but
1009 there is one case (cp_parser_nth_token_starts_template_id) where
1010 the caller passes a variable for N and it might be 1. */
1013 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1017 /* N is 1-based, not zero-based. */
1020 if (cp_lexer_debugging_p (lexer
))
1021 fprintf (cp_lexer_debug_stream
,
1022 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1025 token
= lexer
->next_token
;
1026 gcc_assert (!n
|| token
!= &eof_token
);
1030 if (token
== lexer
->last_token
)
1036 if (!token
->purged_p
)
1040 if (cp_lexer_debugging_p (lexer
))
1042 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1043 putc ('\n', cp_lexer_debug_stream
);
1049 /* Return the next token, and advance the lexer's next_token pointer
1050 to point to the next non-purged token. */
1053 cp_lexer_consume_token (cp_lexer
* lexer
)
1055 cp_token
*token
= lexer
->next_token
;
1057 gcc_assert (token
!= &eof_token
);
1058 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1062 lexer
->next_token
++;
1063 if (lexer
->next_token
== lexer
->last_token
)
1065 lexer
->next_token
= &eof_token
;
1070 while (lexer
->next_token
->purged_p
);
1072 cp_lexer_set_source_position_from_token (token
);
1074 /* Provide debugging output. */
1075 if (cp_lexer_debugging_p (lexer
))
1077 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1078 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1079 putc ('\n', cp_lexer_debug_stream
);
1085 /* Permanently remove the next token from the token stream, and
1086 advance the next_token pointer to refer to the next non-purged
1090 cp_lexer_purge_token (cp_lexer
*lexer
)
1092 cp_token
*tok
= lexer
->next_token
;
1094 gcc_assert (tok
!= &eof_token
);
1095 tok
->purged_p
= true;
1096 tok
->location
= UNKNOWN_LOCATION
;
1097 tok
->u
.value
= NULL_TREE
;
1098 tok
->keyword
= RID_MAX
;
1103 if (tok
== lexer
->last_token
)
1109 while (tok
->purged_p
);
1110 lexer
->next_token
= tok
;
1113 /* Permanently remove all tokens after TOK, up to, but not
1114 including, the token that will be returned next by
1115 cp_lexer_peek_token. */
1118 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1120 cp_token
*peek
= lexer
->next_token
;
1122 if (peek
== &eof_token
)
1123 peek
= lexer
->last_token
;
1125 gcc_assert (tok
< peek
);
1127 for ( tok
+= 1; tok
!= peek
; tok
+= 1)
1129 tok
->purged_p
= true;
1130 tok
->location
= UNKNOWN_LOCATION
;
1131 tok
->u
.value
= NULL_TREE
;
1132 tok
->keyword
= RID_MAX
;
1136 /* Begin saving tokens. All tokens consumed after this point will be
1140 cp_lexer_save_tokens (cp_lexer
* lexer
)
1142 /* Provide debugging output. */
1143 if (cp_lexer_debugging_p (lexer
))
1144 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1146 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1149 /* Commit to the portion of the token stream most recently saved. */
1152 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1154 /* Provide debugging output. */
1155 if (cp_lexer_debugging_p (lexer
))
1156 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1158 lexer
->saved_tokens
.pop ();
1161 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1162 to the token stream. Stop saving tokens. */
1165 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1167 /* Provide debugging output. */
1168 if (cp_lexer_debugging_p (lexer
))
1169 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1171 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1174 /* RAII wrapper around the above functions, with sanity checking. Creating
1175 a variable saves tokens, which are committed when the variable is
1176 destroyed unless they are explicitly rolled back by calling the rollback
1179 struct saved_token_sentinel
1184 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1186 len
= lexer
->saved_tokens
.length ();
1187 cp_lexer_save_tokens (lexer
);
1191 cp_lexer_rollback_tokens (lexer
);
1194 ~saved_token_sentinel()
1197 cp_lexer_commit_tokens (lexer
);
1198 gcc_assert (lexer
->saved_tokens
.length () == len
);
1202 /* Print a representation of the TOKEN on the STREAM. */
1205 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1207 /* We don't use cpp_type2name here because the parser defines
1208 a few tokens of its own. */
1209 static const char *const token_names
[] = {
1210 /* cpplib-defined token types */
1211 #define OP(e, s) #e,
1212 #define TK(e, s) #e,
1216 /* C++ parser token types - see "Manifest constants", above. */
1219 "NESTED_NAME_SPECIFIER",
1222 /* For some tokens, print the associated data. */
1223 switch (token
->type
)
1226 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1227 For example, `struct' is mapped to an INTEGER_CST. */
1228 if (!identifier_p (token
->u
.value
))
1230 /* else fall through */
1232 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1239 case CPP_UTF8STRING
:
1240 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1244 print_generic_expr (stream
, token
->u
.value
, 0);
1248 /* If we have a name for the token, print it out. Otherwise, we
1249 simply give the numeric code. */
1250 if (token
->type
< ARRAY_SIZE(token_names
))
1251 fputs (token_names
[token
->type
], stream
);
1253 fprintf (stream
, "[%d]", token
->type
);
1259 debug (cp_token
&ref
)
1261 cp_lexer_print_token (stderr
, &ref
);
1262 fprintf (stderr
, "\n");
1266 debug (cp_token
*ptr
)
1271 fprintf (stderr
, "<nil>\n");
1275 /* Start emitting debugging information. */
1278 cp_lexer_start_debugging (cp_lexer
* lexer
)
1280 lexer
->debugging_p
= true;
1281 cp_lexer_debug_stream
= stderr
;
1284 /* Stop emitting debugging information. */
1287 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1289 lexer
->debugging_p
= false;
1290 cp_lexer_debug_stream
= NULL
;
1293 /* Create a new cp_token_cache, representing a range of tokens. */
1295 static cp_token_cache
*
1296 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1298 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1299 cache
->first
= first
;
1304 /* Diagnose if #pragma omp declare simd isn't followed immediately
1305 by function declaration or definition. */
1308 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1310 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1312 error ("%<#pragma omp declare simd%> not immediately followed by "
1313 "function declaration or definition");
1314 parser
->omp_declare_simd
= NULL
;
1318 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1319 and put that into "omp declare simd" attribute. */
1322 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1324 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1326 if (fndecl
== error_mark_node
)
1328 parser
->omp_declare_simd
= NULL
;
1331 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1333 cp_ensure_no_omp_declare_simd (parser
);
1339 /* Decl-specifiers. */
1341 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1344 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1346 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1351 /* Nothing other than the parser should be creating declarators;
1352 declarators are a semi-syntactic representation of C++ entities.
1353 Other parts of the front end that need to create entities (like
1354 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1356 static cp_declarator
*make_call_declarator
1357 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
);
1358 static cp_declarator
*make_array_declarator
1359 (cp_declarator
*, tree
);
1360 static cp_declarator
*make_pointer_declarator
1361 (cp_cv_quals
, cp_declarator
*, tree
);
1362 static cp_declarator
*make_reference_declarator
1363 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1364 static cp_parameter_declarator
*make_parameter_declarator
1365 (cp_decl_specifier_seq
*, cp_declarator
*, tree
);
1366 static cp_declarator
*make_ptrmem_declarator
1367 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1369 /* An erroneous declarator. */
1370 static cp_declarator
*cp_error_declarator
;
1372 /* The obstack on which declarators and related data structures are
1374 static struct obstack declarator_obstack
;
1376 /* Alloc BYTES from the declarator memory pool. */
1378 static inline void *
1379 alloc_declarator (size_t bytes
)
1381 return obstack_alloc (&declarator_obstack
, bytes
);
1384 /* Allocate a declarator of the indicated KIND. Clear fields that are
1385 common to all declarators. */
1387 static cp_declarator
*
1388 make_declarator (cp_declarator_kind kind
)
1390 cp_declarator
*declarator
;
1392 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1393 declarator
->kind
= kind
;
1394 declarator
->attributes
= NULL_TREE
;
1395 declarator
->std_attributes
= NULL_TREE
;
1396 declarator
->declarator
= NULL
;
1397 declarator
->parameter_pack_p
= false;
1398 declarator
->id_loc
= UNKNOWN_LOCATION
;
1403 /* Make a declarator for a generalized identifier. If
1404 QUALIFYING_SCOPE is non-NULL, the identifier is
1405 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1406 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1409 static cp_declarator
*
1410 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1411 special_function_kind sfk
)
1413 cp_declarator
*declarator
;
1415 /* It is valid to write:
1417 class C { void f(); };
1421 The standard is not clear about whether `typedef const C D' is
1422 legal; as of 2002-09-15 the committee is considering that
1423 question. EDG 3.0 allows that syntax. Therefore, we do as
1425 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1426 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1428 gcc_assert (identifier_p (unqualified_name
)
1429 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1430 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1432 declarator
= make_declarator (cdk_id
);
1433 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1434 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1435 declarator
->u
.id
.sfk
= sfk
;
1440 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1441 of modifiers such as const or volatile to apply to the pointer
1442 type, represented as identifiers. ATTRIBUTES represent the attributes that
1443 appertain to the pointer or reference. */
1446 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1449 cp_declarator
*declarator
;
1451 declarator
= make_declarator (cdk_pointer
);
1452 declarator
->declarator
= target
;
1453 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1454 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1457 declarator
->id_loc
= target
->id_loc
;
1458 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1459 target
->parameter_pack_p
= false;
1462 declarator
->parameter_pack_p
= false;
1464 declarator
->std_attributes
= attributes
;
1469 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1470 represent the attributes that appertain to the pointer or
1474 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1475 bool rvalue_ref
, tree attributes
)
1477 cp_declarator
*declarator
;
1479 declarator
= make_declarator (cdk_reference
);
1480 declarator
->declarator
= target
;
1481 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1482 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1485 declarator
->id_loc
= target
->id_loc
;
1486 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1487 target
->parameter_pack_p
= false;
1490 declarator
->parameter_pack_p
= false;
1492 declarator
->std_attributes
= attributes
;
1497 /* Like make_pointer_declarator -- but for a pointer to a non-static
1498 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1499 appertain to the pointer or reference. */
1502 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1503 cp_declarator
*pointee
,
1506 cp_declarator
*declarator
;
1508 declarator
= make_declarator (cdk_ptrmem
);
1509 declarator
->declarator
= pointee
;
1510 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1511 declarator
->u
.pointer
.class_type
= class_type
;
1515 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1516 pointee
->parameter_pack_p
= false;
1519 declarator
->parameter_pack_p
= false;
1521 declarator
->std_attributes
= attributes
;
1526 /* Make a declarator for the function given by TARGET, with the
1527 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1528 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1529 indicates what exceptions can be thrown. */
1532 make_call_declarator (cp_declarator
*target
,
1534 cp_cv_quals cv_qualifiers
,
1535 cp_virt_specifiers virt_specifiers
,
1536 cp_ref_qualifier ref_qualifier
,
1537 tree exception_specification
,
1538 tree late_return_type
)
1540 cp_declarator
*declarator
;
1542 declarator
= make_declarator (cdk_function
);
1543 declarator
->declarator
= target
;
1544 declarator
->u
.function
.parameters
= parms
;
1545 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1546 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1547 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1548 declarator
->u
.function
.exception_specification
= exception_specification
;
1549 declarator
->u
.function
.late_return_type
= late_return_type
;
1552 declarator
->id_loc
= target
->id_loc
;
1553 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1554 target
->parameter_pack_p
= false;
1557 declarator
->parameter_pack_p
= false;
1562 /* Make a declarator for an array of BOUNDS elements, each of which is
1563 defined by ELEMENT. */
1566 make_array_declarator (cp_declarator
*element
, tree bounds
)
1568 cp_declarator
*declarator
;
1570 declarator
= make_declarator (cdk_array
);
1571 declarator
->declarator
= element
;
1572 declarator
->u
.array
.bounds
= bounds
;
1575 declarator
->id_loc
= element
->id_loc
;
1576 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1577 element
->parameter_pack_p
= false;
1580 declarator
->parameter_pack_p
= false;
1585 /* Determine whether the declarator we've seen so far can be a
1586 parameter pack, when followed by an ellipsis. */
1588 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1590 /* Search for a declarator name, or any other declarator that goes
1591 after the point where the ellipsis could appear in a parameter
1592 pack. If we find any of these, then this declarator can not be
1593 made into a parameter pack. */
1595 while (declarator
&& !found
)
1597 switch ((int)declarator
->kind
)
1608 declarator
= declarator
->declarator
;
1616 cp_parameter_declarator
*no_parameters
;
1618 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1619 DECLARATOR and DEFAULT_ARGUMENT. */
1621 cp_parameter_declarator
*
1622 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1623 cp_declarator
*declarator
,
1624 tree default_argument
)
1626 cp_parameter_declarator
*parameter
;
1628 parameter
= ((cp_parameter_declarator
*)
1629 alloc_declarator (sizeof (cp_parameter_declarator
)));
1630 parameter
->next
= NULL
;
1631 if (decl_specifiers
)
1632 parameter
->decl_specifiers
= *decl_specifiers
;
1634 clear_decl_specs (¶meter
->decl_specifiers
);
1635 parameter
->declarator
= declarator
;
1636 parameter
->default_argument
= default_argument
;
1637 parameter
->ellipsis_p
= false;
1642 /* Returns true iff DECLARATOR is a declaration for a function. */
1645 function_declarator_p (const cp_declarator
*declarator
)
1649 if (declarator
->kind
== cdk_function
1650 && declarator
->declarator
->kind
== cdk_id
)
1652 if (declarator
->kind
== cdk_id
1653 || declarator
->kind
== cdk_error
)
1655 declarator
= declarator
->declarator
;
1665 A cp_parser parses the token stream as specified by the C++
1666 grammar. Its job is purely parsing, not semantic analysis. For
1667 example, the parser breaks the token stream into declarators,
1668 expressions, statements, and other similar syntactic constructs.
1669 It does not check that the types of the expressions on either side
1670 of an assignment-statement are compatible, or that a function is
1671 not declared with a parameter of type `void'.
1673 The parser invokes routines elsewhere in the compiler to perform
1674 semantic analysis and to build up the abstract syntax tree for the
1677 The parser (and the template instantiation code, which is, in a
1678 way, a close relative of parsing) are the only parts of the
1679 compiler that should be calling push_scope and pop_scope, or
1680 related functions. The parser (and template instantiation code)
1681 keeps track of what scope is presently active; everything else
1682 should simply honor that. (The code that generates static
1683 initializers may also need to set the scope, in order to check
1684 access control correctly when emitting the initializers.)
1689 The parser is of the standard recursive-descent variety. Upcoming
1690 tokens in the token stream are examined in order to determine which
1691 production to use when parsing a non-terminal. Some C++ constructs
1692 require arbitrary look ahead to disambiguate. For example, it is
1693 impossible, in the general case, to tell whether a statement is an
1694 expression or declaration without scanning the entire statement.
1695 Therefore, the parser is capable of "parsing tentatively." When the
1696 parser is not sure what construct comes next, it enters this mode.
1697 Then, while we attempt to parse the construct, the parser queues up
1698 error messages, rather than issuing them immediately, and saves the
1699 tokens it consumes. If the construct is parsed successfully, the
1700 parser "commits", i.e., it issues any queued error messages and
1701 the tokens that were being preserved are permanently discarded.
1702 If, however, the construct is not parsed successfully, the parser
1703 rolls back its state completely so that it can resume parsing using
1704 a different alternative.
1709 The performance of the parser could probably be improved substantially.
1710 We could often eliminate the need to parse tentatively by looking ahead
1711 a little bit. In some places, this approach might not entirely eliminate
1712 the need to parse tentatively, but it might still speed up the average
1715 /* Flags that are passed to some parsing functions. These values can
1716 be bitwise-ored together. */
1721 CP_PARSER_FLAGS_NONE
= 0x0,
1722 /* The construct is optional. If it is not present, then no error
1723 should be issued. */
1724 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1725 /* When parsing a type-specifier, treat user-defined type-names
1726 as non-type identifiers. */
1727 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1728 /* When parsing a type-specifier, do not try to parse a class-specifier
1729 or enum-specifier. */
1730 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1731 /* When parsing a decl-specifier-seq, only allow type-specifier or
1733 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8
1736 /* This type is used for parameters and variables which hold
1737 combinations of the above flags. */
1738 typedef int cp_parser_flags
;
1740 /* The different kinds of declarators we want to parse. */
1742 typedef enum cp_parser_declarator_kind
1744 /* We want an abstract declarator. */
1745 CP_PARSER_DECLARATOR_ABSTRACT
,
1746 /* We want a named declarator. */
1747 CP_PARSER_DECLARATOR_NAMED
,
1748 /* We don't mind, but the name must be an unqualified-id. */
1749 CP_PARSER_DECLARATOR_EITHER
1750 } cp_parser_declarator_kind
;
1752 /* The precedence values used to parse binary expressions. The minimum value
1753 of PREC must be 1, because zero is reserved to quickly discriminate
1754 binary operators from other tokens. */
1759 PREC_LOGICAL_OR_EXPRESSION
,
1760 PREC_LOGICAL_AND_EXPRESSION
,
1761 PREC_INCLUSIVE_OR_EXPRESSION
,
1762 PREC_EXCLUSIVE_OR_EXPRESSION
,
1763 PREC_AND_EXPRESSION
,
1764 PREC_EQUALITY_EXPRESSION
,
1765 PREC_RELATIONAL_EXPRESSION
,
1766 PREC_SHIFT_EXPRESSION
,
1767 PREC_ADDITIVE_EXPRESSION
,
1768 PREC_MULTIPLICATIVE_EXPRESSION
,
1770 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1773 /* A mapping from a token type to a corresponding tree node type, with a
1774 precedence value. */
1776 typedef struct cp_parser_binary_operations_map_node
1778 /* The token type. */
1779 enum cpp_ttype token_type
;
1780 /* The corresponding tree code. */
1781 enum tree_code tree_type
;
1782 /* The precedence of this operator. */
1783 enum cp_parser_prec prec
;
1784 } cp_parser_binary_operations_map_node
;
1786 typedef struct cp_parser_expression_stack_entry
1788 /* Left hand side of the binary operation we are currently
1791 /* Original tree code for left hand side, if it was a binary
1792 expression itself (used for -Wparentheses). */
1793 enum tree_code lhs_type
;
1794 /* Tree code for the binary operation we are parsing. */
1795 enum tree_code tree_type
;
1796 /* Precedence of the binary operation we are parsing. */
1797 enum cp_parser_prec prec
;
1798 /* Location of the binary operation we are parsing. */
1800 } cp_parser_expression_stack_entry
;
1802 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1803 entries because precedence levels on the stack are monotonically
1805 typedef struct cp_parser_expression_stack_entry
1806 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1810 /* Constructors and destructors. */
1812 static cp_parser_context
*cp_parser_context_new
1813 (cp_parser_context
*);
1815 /* Class variables. */
1817 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1819 /* The operator-precedence table used by cp_parser_binary_expression.
1820 Transformed into an associative array (binops_by_token) by
1823 static const cp_parser_binary_operations_map_node binops
[] = {
1824 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1825 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1827 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1828 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1829 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1831 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1832 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1834 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1835 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1837 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1838 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1839 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1840 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1842 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1843 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1845 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1847 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1849 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1851 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1853 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1856 /* The same as binops, but initialized by cp_parser_new so that
1857 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1859 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1861 /* Constructors and destructors. */
1863 /* Construct a new context. The context below this one on the stack
1864 is given by NEXT. */
1866 static cp_parser_context
*
1867 cp_parser_context_new (cp_parser_context
* next
)
1869 cp_parser_context
*context
;
1871 /* Allocate the storage. */
1872 if (cp_parser_context_free_list
!= NULL
)
1874 /* Pull the first entry from the free list. */
1875 context
= cp_parser_context_free_list
;
1876 cp_parser_context_free_list
= context
->next
;
1877 memset (context
, 0, sizeof (*context
));
1880 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1882 /* No errors have occurred yet in this context. */
1883 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1884 /* If this is not the bottommost context, copy information that we
1885 need from the previous context. */
1888 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1889 expression, then we are parsing one in this context, too. */
1890 context
->object_type
= next
->object_type
;
1891 /* Thread the stack. */
1892 context
->next
= next
;
1898 /* Managing the unparsed function queues. */
1900 #define unparsed_funs_with_default_args \
1901 parser->unparsed_queues->last ().funs_with_default_args
1902 #define unparsed_funs_with_definitions \
1903 parser->unparsed_queues->last ().funs_with_definitions
1904 #define unparsed_nsdmis \
1905 parser->unparsed_queues->last ().nsdmis
1906 #define unparsed_classes \
1907 parser->unparsed_queues->last ().classes
1910 push_unparsed_function_queues (cp_parser
*parser
)
1912 cp_unparsed_functions_entry e
= {NULL
, make_tree_vector (), NULL
, NULL
};
1913 vec_safe_push (parser
->unparsed_queues
, e
);
1917 pop_unparsed_function_queues (cp_parser
*parser
)
1919 release_tree_vector (unparsed_funs_with_definitions
);
1920 parser
->unparsed_queues
->pop ();
1925 /* Constructors and destructors. */
1927 static cp_parser
*cp_parser_new
1930 /* Routines to parse various constructs.
1932 Those that return `tree' will return the error_mark_node (rather
1933 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1934 Sometimes, they will return an ordinary node if error-recovery was
1935 attempted, even though a parse error occurred. So, to check
1936 whether or not a parse error occurred, you should always use
1937 cp_parser_error_occurred. If the construct is optional (indicated
1938 either by an `_opt' in the name of the function that does the
1939 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1940 the construct is not present. */
1942 /* Lexical conventions [gram.lex] */
1944 static tree cp_parser_identifier
1946 static tree cp_parser_string_literal
1947 (cp_parser
*, bool, bool, bool);
1948 static tree cp_parser_userdef_char_literal
1950 static tree cp_parser_userdef_string_literal
1952 static tree cp_parser_userdef_numeric_literal
1955 /* Basic concepts [gram.basic] */
1957 static bool cp_parser_translation_unit
1960 /* Expressions [gram.expr] */
1962 static tree cp_parser_primary_expression
1963 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
1964 static tree cp_parser_id_expression
1965 (cp_parser
*, bool, bool, bool *, bool, bool);
1966 static tree cp_parser_unqualified_id
1967 (cp_parser
*, bool, bool, bool, bool);
1968 static tree cp_parser_nested_name_specifier_opt
1969 (cp_parser
*, bool, bool, bool, bool);
1970 static tree cp_parser_nested_name_specifier
1971 (cp_parser
*, bool, bool, bool, bool);
1972 static tree cp_parser_qualifying_entity
1973 (cp_parser
*, bool, bool, bool, bool, bool);
1974 static tree cp_parser_postfix_expression
1975 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
1976 static tree cp_parser_postfix_open_square_expression
1977 (cp_parser
*, tree
, bool, bool);
1978 static tree cp_parser_postfix_dot_deref_expression
1979 (cp_parser
*, enum cpp_ttype
, tree
, bool, cp_id_kind
*, location_t
);
1980 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
1981 (cp_parser
*, int, bool, bool, bool *, bool = false);
1982 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1983 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
1984 static void cp_parser_pseudo_destructor_name
1985 (cp_parser
*, tree
, tree
*, tree
*);
1986 static tree cp_parser_unary_expression
1987 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
1988 static enum tree_code cp_parser_unary_operator
1990 static tree cp_parser_new_expression
1992 static vec
<tree
, va_gc
> *cp_parser_new_placement
1994 static tree cp_parser_new_type_id
1995 (cp_parser
*, tree
*);
1996 static cp_declarator
*cp_parser_new_declarator_opt
1998 static cp_declarator
*cp_parser_direct_new_declarator
2000 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2002 static tree cp_parser_delete_expression
2004 static tree cp_parser_cast_expression
2005 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2006 static tree cp_parser_binary_expression
2007 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2008 static tree cp_parser_question_colon_clause
2009 (cp_parser
*, tree
);
2010 static tree cp_parser_assignment_expression
2011 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2012 static enum tree_code cp_parser_assignment_operator_opt
2014 static tree cp_parser_expression
2015 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2016 static tree cp_parser_constant_expression
2017 (cp_parser
*, bool = false, bool * = NULL
);
2018 static tree cp_parser_builtin_offsetof
2020 static tree cp_parser_lambda_expression
2022 static void cp_parser_lambda_introducer
2023 (cp_parser
*, tree
);
2024 static bool cp_parser_lambda_declarator_opt
2025 (cp_parser
*, tree
);
2026 static void cp_parser_lambda_body
2027 (cp_parser
*, tree
);
2029 /* Statements [gram.stmt.stmt] */
2031 static void cp_parser_statement
2032 (cp_parser
*, tree
, bool, bool *);
2033 static void cp_parser_label_for_labeled_statement
2034 (cp_parser
*, tree
);
2035 static tree cp_parser_expression_statement
2036 (cp_parser
*, tree
);
2037 static tree cp_parser_compound_statement
2038 (cp_parser
*, tree
, bool, bool);
2039 static void cp_parser_statement_seq_opt
2040 (cp_parser
*, tree
);
2041 static tree cp_parser_selection_statement
2042 (cp_parser
*, bool *);
2043 static tree cp_parser_condition
2045 static tree cp_parser_iteration_statement
2046 (cp_parser
*, bool);
2047 static bool cp_parser_for_init_statement
2048 (cp_parser
*, tree
*decl
);
2049 static tree cp_parser_for
2050 (cp_parser
*, bool);
2051 static tree cp_parser_c_for
2052 (cp_parser
*, tree
, tree
, bool);
2053 static tree cp_parser_range_for
2054 (cp_parser
*, tree
, tree
, tree
, bool);
2055 static void do_range_for_auto_deduction
2057 static tree cp_parser_perform_range_for_lookup
2058 (tree
, tree
*, tree
*);
2059 static tree cp_parser_range_for_member_function
2061 static tree cp_parser_jump_statement
2063 static void cp_parser_declaration_statement
2066 static tree cp_parser_implicitly_scoped_statement
2067 (cp_parser
*, bool *);
2068 static void cp_parser_already_scoped_statement
2071 /* Declarations [gram.dcl.dcl] */
2073 static void cp_parser_declaration_seq_opt
2075 static void cp_parser_declaration
2077 static void cp_parser_block_declaration
2078 (cp_parser
*, bool);
2079 static void cp_parser_simple_declaration
2080 (cp_parser
*, bool, tree
*);
2081 static void cp_parser_decl_specifier_seq
2082 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2083 static tree cp_parser_storage_class_specifier_opt
2085 static tree cp_parser_function_specifier_opt
2086 (cp_parser
*, cp_decl_specifier_seq
*);
2087 static tree cp_parser_type_specifier
2088 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2090 static tree cp_parser_simple_type_specifier
2091 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2092 static tree cp_parser_type_name
2094 static tree cp_parser_nonclass_name
2095 (cp_parser
* parser
);
2096 static tree cp_parser_elaborated_type_specifier
2097 (cp_parser
*, bool, bool);
2098 static tree cp_parser_enum_specifier
2100 static void cp_parser_enumerator_list
2101 (cp_parser
*, tree
);
2102 static void cp_parser_enumerator_definition
2103 (cp_parser
*, tree
);
2104 static tree cp_parser_namespace_name
2106 static void cp_parser_namespace_definition
2108 static void cp_parser_namespace_body
2110 static tree cp_parser_qualified_namespace_specifier
2112 static void cp_parser_namespace_alias_definition
2114 static bool cp_parser_using_declaration
2115 (cp_parser
*, bool);
2116 static void cp_parser_using_directive
2118 static tree cp_parser_alias_declaration
2120 static void cp_parser_asm_definition
2122 static void cp_parser_linkage_specification
2124 static void cp_parser_static_assert
2125 (cp_parser
*, bool);
2126 static tree cp_parser_decltype
2129 /* Declarators [gram.dcl.decl] */
2131 static tree cp_parser_init_declarator
2132 (cp_parser
*, cp_decl_specifier_seq
*, vec
<deferred_access_check
, va_gc
> *,
2133 bool, bool, int, bool *, tree
*, location_t
*);
2134 static cp_declarator
*cp_parser_declarator
2135 (cp_parser
*, cp_parser_declarator_kind
, int *, bool *, bool, bool);
2136 static cp_declarator
*cp_parser_direct_declarator
2137 (cp_parser
*, cp_parser_declarator_kind
, int *, bool, bool);
2138 static enum tree_code cp_parser_ptr_operator
2139 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2140 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146 static tree cp_parser_late_return_type_opt
2147 (cp_parser
*, cp_declarator
*, cp_cv_quals
);
2148 static tree cp_parser_declarator_id
2149 (cp_parser
*, bool);
2150 static tree cp_parser_type_id
2152 static tree cp_parser_template_type_arg
2154 static tree
cp_parser_trailing_type_id (cp_parser
*);
2155 static tree cp_parser_type_id_1
2156 (cp_parser
*, bool, bool);
2157 static void cp_parser_type_specifier_seq
2158 (cp_parser
*, bool, bool, cp_decl_specifier_seq
*);
2159 static tree cp_parser_parameter_declaration_clause
2161 static tree cp_parser_parameter_declaration_list
2162 (cp_parser
*, bool *);
2163 static cp_parameter_declarator
*cp_parser_parameter_declaration
2164 (cp_parser
*, bool, bool *);
2165 static tree cp_parser_default_argument
2166 (cp_parser
*, bool);
2167 static void cp_parser_function_body
2168 (cp_parser
*, bool);
2169 static tree cp_parser_initializer
2170 (cp_parser
*, bool *, bool *);
2171 static tree cp_parser_initializer_clause
2172 (cp_parser
*, bool *);
2173 static tree cp_parser_braced_list
2174 (cp_parser
*, bool*);
2175 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2176 (cp_parser
*, bool *);
2178 static bool cp_parser_ctor_initializer_opt_and_function_body
2179 (cp_parser
*, bool);
2181 static tree cp_parser_late_parsing_omp_declare_simd
2182 (cp_parser
*, tree
);
2184 static tree cp_parser_late_parsing_cilk_simd_fn_info
2185 (cp_parser
*, tree
);
2187 static tree synthesize_implicit_template_parm
2189 static tree finish_fully_implicit_template
2190 (cp_parser
*, tree
);
2192 /* Classes [gram.class] */
2194 static tree cp_parser_class_name
2195 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool);
2196 static tree cp_parser_class_specifier
2198 static tree cp_parser_class_head
2199 (cp_parser
*, bool *);
2200 static enum tag_types cp_parser_class_key
2202 static void cp_parser_type_parameter_key
2203 (cp_parser
* parser
);
2204 static void cp_parser_member_specification_opt
2206 static void cp_parser_member_declaration
2208 static tree cp_parser_pure_specifier
2210 static tree cp_parser_constant_initializer
2213 /* Derived classes [gram.class.derived] */
2215 static tree cp_parser_base_clause
2217 static tree cp_parser_base_specifier
2220 /* Special member functions [gram.special] */
2222 static tree cp_parser_conversion_function_id
2224 static tree cp_parser_conversion_type_id
2226 static cp_declarator
*cp_parser_conversion_declarator_opt
2228 static bool cp_parser_ctor_initializer_opt
2230 static void cp_parser_mem_initializer_list
2232 static tree cp_parser_mem_initializer
2234 static tree cp_parser_mem_initializer_id
2237 /* Overloading [gram.over] */
2239 static tree cp_parser_operator_function_id
2241 static tree cp_parser_operator
2244 /* Templates [gram.temp] */
2246 static void cp_parser_template_declaration
2247 (cp_parser
*, bool);
2248 static tree cp_parser_template_parameter_list
2250 static tree cp_parser_template_parameter
2251 (cp_parser
*, bool *, bool *);
2252 static tree cp_parser_type_parameter
2253 (cp_parser
*, bool *);
2254 static tree cp_parser_template_id
2255 (cp_parser
*, bool, bool, enum tag_types
, bool);
2256 static tree cp_parser_template_name
2257 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2258 static tree cp_parser_template_argument_list
2260 static tree cp_parser_template_argument
2262 static void cp_parser_explicit_instantiation
2264 static void cp_parser_explicit_specialization
2267 /* Exception handling [gram.exception] */
2269 static tree cp_parser_try_block
2271 static bool cp_parser_function_try_block
2273 static void cp_parser_handler_seq
2275 static void cp_parser_handler
2277 static tree cp_parser_exception_declaration
2279 static tree cp_parser_throw_expression
2281 static tree cp_parser_exception_specification_opt
2283 static tree cp_parser_type_id_list
2286 /* GNU Extensions */
2288 static tree cp_parser_asm_specification_opt
2290 static tree cp_parser_asm_operand_list
2292 static tree cp_parser_asm_clobber_list
2294 static tree cp_parser_asm_label_list
2296 static bool cp_next_tokens_can_be_attribute_p
2298 static bool cp_next_tokens_can_be_gnu_attribute_p
2300 static bool cp_next_tokens_can_be_std_attribute_p
2302 static bool cp_nth_tokens_can_be_std_attribute_p
2303 (cp_parser
*, size_t);
2304 static bool cp_nth_tokens_can_be_gnu_attribute_p
2305 (cp_parser
*, size_t);
2306 static bool cp_nth_tokens_can_be_attribute_p
2307 (cp_parser
*, size_t);
2308 static tree cp_parser_attributes_opt
2310 static tree cp_parser_gnu_attributes_opt
2312 static tree cp_parser_gnu_attribute_list
2314 static tree cp_parser_std_attribute
2316 static tree cp_parser_std_attribute_spec
2318 static tree cp_parser_std_attribute_spec_seq
2320 static bool cp_parser_extension_opt
2321 (cp_parser
*, int *);
2322 static void cp_parser_label_declaration
2325 /* Transactional Memory Extensions */
2327 static tree cp_parser_transaction
2328 (cp_parser
*, enum rid
);
2329 static tree cp_parser_transaction_expression
2330 (cp_parser
*, enum rid
);
2331 static bool cp_parser_function_transaction
2332 (cp_parser
*, enum rid
);
2333 static tree cp_parser_transaction_cancel
2336 enum pragma_context
{
2343 static bool cp_parser_pragma
2344 (cp_parser
*, enum pragma_context
);
2346 /* Objective-C++ Productions */
2348 static tree cp_parser_objc_message_receiver
2350 static tree cp_parser_objc_message_args
2352 static tree cp_parser_objc_message_expression
2354 static tree cp_parser_objc_encode_expression
2356 static tree cp_parser_objc_defs_expression
2358 static tree cp_parser_objc_protocol_expression
2360 static tree cp_parser_objc_selector_expression
2362 static tree cp_parser_objc_expression
2364 static bool cp_parser_objc_selector_p
2366 static tree cp_parser_objc_selector
2368 static tree cp_parser_objc_protocol_refs_opt
2370 static void cp_parser_objc_declaration
2371 (cp_parser
*, tree
);
2372 static tree cp_parser_objc_statement
2374 static bool cp_parser_objc_valid_prefix_attributes
2375 (cp_parser
*, tree
*);
2376 static void cp_parser_objc_at_property_declaration
2378 static void cp_parser_objc_at_synthesize_declaration
2380 static void cp_parser_objc_at_dynamic_declaration
2382 static tree cp_parser_objc_struct_declaration
2385 /* Utility Routines */
2387 static tree cp_parser_lookup_name
2388 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2389 static tree cp_parser_lookup_name_simple
2390 (cp_parser
*, tree
, location_t
);
2391 static tree cp_parser_maybe_treat_template_as_class
2393 static bool cp_parser_check_declarator_template_parameters
2394 (cp_parser
*, cp_declarator
*, location_t
);
2395 static bool cp_parser_check_template_parameters
2396 (cp_parser
*, unsigned, location_t
, cp_declarator
*);
2397 static tree cp_parser_simple_cast_expression
2399 static tree cp_parser_global_scope_opt
2400 (cp_parser
*, bool);
2401 static bool cp_parser_constructor_declarator_p
2402 (cp_parser
*, bool);
2403 static tree cp_parser_function_definition_from_specifiers_and_declarator
2404 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2405 static tree cp_parser_function_definition_after_declarator
2406 (cp_parser
*, bool);
2407 static void cp_parser_template_declaration_after_export
2408 (cp_parser
*, bool);
2409 static void cp_parser_perform_template_parameter_access_checks
2410 (vec
<deferred_access_check
, va_gc
> *);
2411 static tree cp_parser_single_declaration
2412 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2413 static tree cp_parser_functional_cast
2414 (cp_parser
*, tree
);
2415 static tree cp_parser_save_member_function_body
2416 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2417 static tree cp_parser_save_nsdmi
2419 static tree cp_parser_enclosed_template_argument_list
2421 static void cp_parser_save_default_args
2422 (cp_parser
*, tree
);
2423 static void cp_parser_late_parsing_for_member
2424 (cp_parser
*, tree
);
2425 static tree cp_parser_late_parse_one_default_arg
2426 (cp_parser
*, tree
, tree
, tree
);
2427 static void cp_parser_late_parsing_nsdmi
2428 (cp_parser
*, tree
);
2429 static void cp_parser_late_parsing_default_args
2430 (cp_parser
*, tree
);
2431 static tree cp_parser_sizeof_operand
2432 (cp_parser
*, enum rid
);
2433 static tree cp_parser_trait_expr
2434 (cp_parser
*, enum rid
);
2435 static bool cp_parser_declares_only_class_p
2437 static void cp_parser_set_storage_class
2438 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2439 static void cp_parser_set_decl_spec_type
2440 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2441 static void set_and_check_decl_spec_loc
2442 (cp_decl_specifier_seq
*decl_specs
,
2443 cp_decl_spec ds
, cp_token
*);
2444 static bool cp_parser_friend_p
2445 (const cp_decl_specifier_seq
*);
2446 static void cp_parser_required_error
2447 (cp_parser
*, required_token
, bool);
2448 static cp_token
*cp_parser_require
2449 (cp_parser
*, enum cpp_ttype
, required_token
);
2450 static cp_token
*cp_parser_require_keyword
2451 (cp_parser
*, enum rid
, required_token
);
2452 static bool cp_parser_token_starts_function_definition_p
2454 static bool cp_parser_next_token_starts_class_definition_p
2456 static bool cp_parser_next_token_ends_template_argument_p
2458 static bool cp_parser_nth_token_starts_template_argument_list_p
2459 (cp_parser
*, size_t);
2460 static enum tag_types cp_parser_token_is_class_key
2462 static enum tag_types cp_parser_token_is_type_parameter_key
2464 static void cp_parser_check_class_key
2465 (enum tag_types
, tree type
);
2466 static void cp_parser_check_access_in_redeclaration
2467 (tree type
, location_t location
);
2468 static bool cp_parser_optional_template_keyword
2470 static void cp_parser_pre_parsed_nested_name_specifier
2472 static bool cp_parser_cache_group
2473 (cp_parser
*, enum cpp_ttype
, unsigned);
2474 static tree cp_parser_cache_defarg
2475 (cp_parser
*parser
, bool nsdmi
);
2476 static void cp_parser_parse_tentatively
2478 static void cp_parser_commit_to_tentative_parse
2480 static void cp_parser_commit_to_topmost_tentative_parse
2482 static void cp_parser_abort_tentative_parse
2484 static bool cp_parser_parse_definitely
2486 static inline bool cp_parser_parsing_tentatively
2488 static bool cp_parser_uncommitted_to_tentative_parse_p
2490 static void cp_parser_error
2491 (cp_parser
*, const char *);
2492 static void cp_parser_name_lookup_error
2493 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2494 static bool cp_parser_simulate_error
2496 static bool cp_parser_check_type_definition
2498 static void cp_parser_check_for_definition_in_return_type
2499 (cp_declarator
*, tree
, location_t type_location
);
2500 static void cp_parser_check_for_invalid_template_id
2501 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2502 static bool cp_parser_non_integral_constant_expression
2503 (cp_parser
*, non_integral_constant
);
2504 static void cp_parser_diagnose_invalid_type_name
2505 (cp_parser
*, tree
, location_t
);
2506 static bool cp_parser_parse_and_diagnose_invalid_type_name
2508 static int cp_parser_skip_to_closing_parenthesis
2509 (cp_parser
*, bool, bool, bool);
2510 static void cp_parser_skip_to_end_of_statement
2512 static void cp_parser_consume_semicolon_at_end_of_statement
2514 static void cp_parser_skip_to_end_of_block_or_statement
2516 static bool cp_parser_skip_to_closing_brace
2518 static void cp_parser_skip_to_end_of_template_parameter_list
2520 static void cp_parser_skip_to_pragma_eol
2521 (cp_parser
*, cp_token
*);
2522 static bool cp_parser_error_occurred
2524 static bool cp_parser_allow_gnu_extensions_p
2526 static bool cp_parser_is_pure_string_literal
2528 static bool cp_parser_is_string_literal
2530 static bool cp_parser_is_keyword
2531 (cp_token
*, enum rid
);
2532 static tree cp_parser_make_typename_type
2533 (cp_parser
*, tree
, location_t location
);
2534 static cp_declarator
* cp_parser_make_indirect_declarator
2535 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2536 static bool cp_parser_compound_literal_p
2538 static bool cp_parser_array_designator_p
2540 static bool cp_parser_skip_to_closing_square_bracket
2543 /* Returns nonzero if we are parsing tentatively. */
2546 cp_parser_parsing_tentatively (cp_parser
* parser
)
2548 return parser
->context
->next
!= NULL
;
2551 /* Returns nonzero if TOKEN is a string literal. */
2554 cp_parser_is_pure_string_literal (cp_token
* token
)
2556 return (token
->type
== CPP_STRING
||
2557 token
->type
== CPP_STRING16
||
2558 token
->type
== CPP_STRING32
||
2559 token
->type
== CPP_WSTRING
||
2560 token
->type
== CPP_UTF8STRING
);
2563 /* Returns nonzero if TOKEN is a string literal
2564 of a user-defined string literal. */
2567 cp_parser_is_string_literal (cp_token
* token
)
2569 return (cp_parser_is_pure_string_literal (token
) ||
2570 token
->type
== CPP_STRING_USERDEF
||
2571 token
->type
== CPP_STRING16_USERDEF
||
2572 token
->type
== CPP_STRING32_USERDEF
||
2573 token
->type
== CPP_WSTRING_USERDEF
||
2574 token
->type
== CPP_UTF8STRING_USERDEF
);
2577 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2580 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2582 return token
->keyword
== keyword
;
2585 /* If not parsing tentatively, issue a diagnostic of the form
2586 FILE:LINE: MESSAGE before TOKEN
2587 where TOKEN is the next token in the input stream. MESSAGE
2588 (specified by the caller) is usually of the form "expected
2592 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2594 if (!cp_parser_simulate_error (parser
))
2596 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2597 /* This diagnostic makes more sense if it is tagged to the line
2598 of the token we just peeked at. */
2599 cp_lexer_set_source_position_from_token (token
);
2601 if (token
->type
== CPP_PRAGMA
)
2603 error_at (token
->location
,
2604 "%<#pragma%> is not allowed here");
2605 cp_parser_skip_to_pragma_eol (parser
, token
);
2609 c_parse_error (gmsgid
,
2610 /* Because c_parser_error does not understand
2611 CPP_KEYWORD, keywords are treated like
2613 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2614 token
->u
.value
, token
->flags
);
2618 /* Issue an error about name-lookup failing. NAME is the
2619 IDENTIFIER_NODE DECL is the result of
2620 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2621 the thing that we hoped to find. */
2624 cp_parser_name_lookup_error (cp_parser
* parser
,
2627 name_lookup_error desired
,
2628 location_t location
)
2630 /* If name lookup completely failed, tell the user that NAME was not
2632 if (decl
== error_mark_node
)
2634 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2635 error_at (location
, "%<%E::%E%> has not been declared",
2636 parser
->scope
, name
);
2637 else if (parser
->scope
== global_namespace
)
2638 error_at (location
, "%<::%E%> has not been declared", name
);
2639 else if (parser
->object_scope
2640 && !CLASS_TYPE_P (parser
->object_scope
))
2641 error_at (location
, "request for member %qE in non-class type %qT",
2642 name
, parser
->object_scope
);
2643 else if (parser
->object_scope
)
2644 error_at (location
, "%<%T::%E%> has not been declared",
2645 parser
->object_scope
, name
);
2647 error_at (location
, "%qE has not been declared", name
);
2649 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
2654 error_at (location
, "%<%E::%E%> is not a type",
2655 parser
->scope
, name
);
2658 error_at (location
, "%<%E::%E%> is not a class or namespace",
2659 parser
->scope
, name
);
2663 "%<%E::%E%> is not a class, namespace, or enumeration",
2664 parser
->scope
, name
);
2671 else if (parser
->scope
== global_namespace
)
2676 error_at (location
, "%<::%E%> is not a type", name
);
2679 error_at (location
, "%<::%E%> is not a class or namespace", name
);
2683 "%<::%E%> is not a class, namespace, or enumeration",
2695 error_at (location
, "%qE is not a type", name
);
2698 error_at (location
, "%qE is not a class or namespace", name
);
2702 "%qE is not a class, namespace, or enumeration", name
);
2710 /* If we are parsing tentatively, remember that an error has occurred
2711 during this tentative parse. Returns true if the error was
2712 simulated; false if a message should be issued by the caller. */
2715 cp_parser_simulate_error (cp_parser
* parser
)
2717 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2719 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
2725 /* This function is called when a type is defined. If type
2726 definitions are forbidden at this point, an error message is
2730 cp_parser_check_type_definition (cp_parser
* parser
)
2732 /* If types are forbidden here, issue a message. */
2733 if (parser
->type_definition_forbidden_message
)
2735 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2736 in the message need to be interpreted. */
2737 error (parser
->type_definition_forbidden_message
);
2743 /* This function is called when the DECLARATOR is processed. The TYPE
2744 was a type defined in the decl-specifiers. If it is invalid to
2745 define a type in the decl-specifiers for DECLARATOR, an error is
2746 issued. TYPE_LOCATION is the location of TYPE and is used
2747 for error reporting. */
2750 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
2751 tree type
, location_t type_location
)
2753 /* [dcl.fct] forbids type definitions in return types.
2754 Unfortunately, it's not easy to know whether or not we are
2755 processing a return type until after the fact. */
2757 && (declarator
->kind
== cdk_pointer
2758 || declarator
->kind
== cdk_reference
2759 || declarator
->kind
== cdk_ptrmem
))
2760 declarator
= declarator
->declarator
;
2762 && declarator
->kind
== cdk_function
)
2764 error_at (type_location
,
2765 "new types may not be defined in a return type");
2766 inform (type_location
,
2767 "(perhaps a semicolon is missing after the definition of %qT)",
2772 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2773 "<" in any valid C++ program. If the next token is indeed "<",
2774 issue a message warning the user about what appears to be an
2775 invalid attempt to form a template-id. LOCATION is the location
2776 of the type-specifier (TYPE) */
2779 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
2781 enum tag_types tag_type
,
2782 location_t location
)
2784 cp_token_position start
= 0;
2786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
2789 error_at (location
, "%qT is not a template", type
);
2790 else if (identifier_p (type
))
2792 if (tag_type
!= none_type
)
2793 error_at (location
, "%qE is not a class template", type
);
2795 error_at (location
, "%qE is not a template", type
);
2798 error_at (location
, "invalid template-id");
2799 /* Remember the location of the invalid "<". */
2800 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
2801 start
= cp_lexer_token_position (parser
->lexer
, true);
2802 /* Consume the "<". */
2803 cp_lexer_consume_token (parser
->lexer
);
2804 /* Parse the template arguments. */
2805 cp_parser_enclosed_template_argument_list (parser
);
2806 /* Permanently remove the invalid template arguments so that
2807 this error message is not issued again. */
2809 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
2813 /* If parsing an integral constant-expression, issue an error message
2814 about the fact that THING appeared and return true. Otherwise,
2815 return false. In either case, set
2816 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2819 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
2820 non_integral_constant thing
)
2822 parser
->non_integral_constant_expression_p
= true;
2823 if (parser
->integral_constant_expression_p
)
2825 if (!parser
->allow_non_integral_constant_expression_p
)
2827 const char *msg
= NULL
;
2831 error ("floating-point literal "
2832 "cannot appear in a constant-expression");
2835 error ("a cast to a type other than an integral or "
2836 "enumeration type cannot appear in a "
2837 "constant-expression");
2840 error ("%<typeid%> operator "
2841 "cannot appear in a constant-expression");
2844 error ("non-constant compound literals "
2845 "cannot appear in a constant-expression");
2848 error ("a function call "
2849 "cannot appear in a constant-expression");
2852 error ("an increment "
2853 "cannot appear in a constant-expression");
2856 error ("an decrement "
2857 "cannot appear in a constant-expression");
2860 error ("an array reference "
2861 "cannot appear in a constant-expression");
2863 case NIC_ADDR_LABEL
:
2864 error ("the address of a label "
2865 "cannot appear in a constant-expression");
2867 case NIC_OVERLOADED
:
2868 error ("calls to overloaded operators "
2869 "cannot appear in a constant-expression");
2871 case NIC_ASSIGNMENT
:
2872 error ("an assignment cannot appear in a constant-expression");
2875 error ("a comma operator "
2876 "cannot appear in a constant-expression");
2878 case NIC_CONSTRUCTOR
:
2879 error ("a call to a constructor "
2880 "cannot appear in a constant-expression");
2882 case NIC_TRANSACTION
:
2883 error ("a transaction expression "
2884 "cannot appear in a constant-expression");
2890 msg
= "__FUNCTION__";
2892 case NIC_PRETTY_FUNC
:
2893 msg
= "__PRETTY_FUNCTION__";
2913 case NIC_PREINCREMENT
:
2916 case NIC_PREDECREMENT
:
2929 error ("%qs cannot appear in a constant-expression", msg
);
2936 /* Emit a diagnostic for an invalid type name. This function commits
2937 to the current active tentative parse, if any. (Otherwise, the
2938 problematic construct might be encountered again later, resulting
2939 in duplicate error messages.) LOCATION is the location of ID. */
2942 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
2943 location_t location
)
2945 tree decl
, ambiguous_decls
;
2946 cp_parser_commit_to_tentative_parse (parser
);
2947 /* Try to lookup the identifier. */
2948 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
2949 /*is_template=*/false,
2950 /*is_namespace=*/false,
2951 /*check_dependency=*/true,
2952 &ambiguous_decls
, location
);
2953 if (ambiguous_decls
)
2954 /* If the lookup was ambiguous, an error will already have
2957 /* If the lookup found a template-name, it means that the user forgot
2958 to specify an argument list. Emit a useful error message. */
2959 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2961 "invalid use of template-name %qE without an argument list",
2963 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
2964 error_at (location
, "invalid use of destructor %qD as a type", id
);
2965 else if (TREE_CODE (decl
) == TYPE_DECL
)
2966 /* Something like 'unsigned A a;' */
2967 error_at (location
, "invalid combination of multiple type-specifiers");
2968 else if (!parser
->scope
)
2970 /* Issue an error message. */
2971 error_at (location
, "%qE does not name a type", id
);
2972 /* If we're in a template class, it's possible that the user was
2973 referring to a type from a base class. For example:
2975 template <typename T> struct A { typedef T X; };
2976 template <typename T> struct B : public A<T> { X x; };
2978 The user should have said "typename A<T>::X". */
2979 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
2980 inform (location
, "C++11 %<constexpr%> only available with "
2981 "-std=c++11 or -std=gnu++11");
2982 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
2983 inform (location
, "C++11 %<noexcept%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (cxx_dialect
< cxx11
2986 && TREE_CODE (id
) == IDENTIFIER_NODE
2987 && !strcmp (IDENTIFIER_POINTER (id
), "thread_local"))
2988 inform (location
, "C++11 %<thread_local%> only available with "
2989 "-std=c++11 or -std=gnu++11");
2990 else if (processing_template_decl
&& current_class_type
2991 && TYPE_BINFO (current_class_type
))
2995 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
2999 tree base_type
= BINFO_TYPE (b
);
3000 if (CLASS_TYPE_P (base_type
)
3001 && dependent_type_p (base_type
))
3004 /* Go from a particular instantiation of the
3005 template (which will have an empty TYPE_FIELDs),
3006 to the main version. */
3007 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3008 for (field
= TYPE_FIELDS (base_type
);
3010 field
= DECL_CHAIN (field
))
3011 if (TREE_CODE (field
) == TYPE_DECL
3012 && DECL_NAME (field
) == id
)
3015 "(perhaps %<typename %T::%E%> was intended)",
3016 BINFO_TYPE (b
), id
);
3025 /* Here we diagnose qualified-ids where the scope is actually correct,
3026 but the identifier does not resolve to a valid type name. */
3027 else if (parser
->scope
!= error_mark_node
)
3029 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3031 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3032 error_at (location_of (id
),
3033 "%qE in namespace %qE does not name a template type",
3036 error_at (location_of (id
),
3037 "%qE in namespace %qE does not name a type",
3040 else if (CLASS_TYPE_P (parser
->scope
)
3041 && constructor_name_p (id
, parser
->scope
))
3044 error_at (location
, "%<%T::%E%> names the constructor, not"
3045 " the type", parser
->scope
, id
);
3046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3047 error_at (location
, "and %qT has no template constructors",
3050 else if (TYPE_P (parser
->scope
)
3051 && dependent_scope_p (parser
->scope
))
3052 error_at (location
, "need %<typename%> before %<%T::%E%> because "
3053 "%qT is a dependent scope",
3054 parser
->scope
, id
, parser
->scope
);
3055 else if (TYPE_P (parser
->scope
))
3057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3058 error_at (location_of (id
),
3059 "%qE in %q#T does not name a template type",
3062 error_at (location_of (id
),
3063 "%qE in %q#T does not name a type",
3071 /* Check for a common situation where a type-name should be present,
3072 but is not, and issue a sensible error message. Returns true if an
3073 invalid type-name was detected.
3075 The situation handled by this function are variable declarations of the
3076 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3077 Usually, `ID' should name a type, but if we got here it means that it
3078 does not. We try to emit the best possible error message depending on
3079 how exactly the id-expression looks like. */
3082 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3085 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3087 /* Avoid duplicate error about ambiguous lookup. */
3088 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3090 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3091 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3095 cp_parser_parse_tentatively (parser
);
3096 id
= cp_parser_id_expression (parser
,
3097 /*template_keyword_p=*/false,
3098 /*check_dependency_p=*/true,
3099 /*template_p=*/NULL
,
3100 /*declarator_p=*/true,
3101 /*optional_p=*/false);
3102 /* If the next token is a (, this is a function with no explicit return
3103 type, i.e. constructor, destructor or conversion op. */
3104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3105 || TREE_CODE (id
) == TYPE_DECL
)
3107 cp_parser_abort_tentative_parse (parser
);
3110 if (!cp_parser_parse_definitely (parser
))
3113 /* Emit a diagnostic for the invalid type. */
3114 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3116 /* If we aren't in the middle of a declarator (i.e. in a
3117 parameter-declaration-clause), skip to the end of the declaration;
3118 there's no point in trying to process it. */
3119 if (!parser
->in_declarator_p
)
3120 cp_parser_skip_to_end_of_block_or_statement (parser
);
3124 /* Consume tokens up to, and including, the next non-nested closing `)'.
3125 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3126 are doing error recovery. Returns -1 if OR_COMMA is true and we
3127 found an unnested comma. */
3130 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3135 unsigned paren_depth
= 0;
3136 unsigned brace_depth
= 0;
3137 unsigned square_depth
= 0;
3139 if (recovering
&& !or_comma
3140 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3145 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3147 switch (token
->type
)
3150 case CPP_PRAGMA_EOL
:
3151 /* If we've run out of tokens, then there is no closing `)'. */
3154 /* This is good for lambda expression capture-lists. */
3155 case CPP_OPEN_SQUARE
:
3158 case CPP_CLOSE_SQUARE
:
3159 if (!square_depth
--)
3164 /* This matches the processing in skip_to_end_of_statement. */
3169 case CPP_OPEN_BRACE
:
3172 case CPP_CLOSE_BRACE
:
3178 if (recovering
&& or_comma
&& !brace_depth
&& !paren_depth
3183 case CPP_OPEN_PAREN
:
3188 case CPP_CLOSE_PAREN
:
3189 if (!brace_depth
&& !paren_depth
--)
3192 cp_lexer_consume_token (parser
->lexer
);
3201 /* Consume the token. */
3202 cp_lexer_consume_token (parser
->lexer
);
3206 /* Consume tokens until we reach the end of the current statement.
3207 Normally, that will be just before consuming a `;'. However, if a
3208 non-nested `}' comes first, then we stop before consuming that. */
3211 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3213 unsigned nesting_depth
= 0;
3215 /* Unwind generic function template scope if necessary. */
3216 if (parser
->fully_implicit_function_template_p
)
3217 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3221 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3223 switch (token
->type
)
3226 case CPP_PRAGMA_EOL
:
3227 /* If we've run out of tokens, stop. */
3231 /* If the next token is a `;', we have reached the end of the
3237 case CPP_CLOSE_BRACE
:
3238 /* If this is a non-nested '}', stop before consuming it.
3239 That way, when confronted with something like:
3243 we stop before consuming the closing '}', even though we
3244 have not yet reached a `;'. */
3245 if (nesting_depth
== 0)
3248 /* If it is the closing '}' for a block that we have
3249 scanned, stop -- but only after consuming the token.
3255 we will stop after the body of the erroneously declared
3256 function, but before consuming the following `typedef'
3258 if (--nesting_depth
== 0)
3260 cp_lexer_consume_token (parser
->lexer
);
3264 case CPP_OPEN_BRACE
:
3272 /* Consume the token. */
3273 cp_lexer_consume_token (parser
->lexer
);
3277 /* This function is called at the end of a statement or declaration.
3278 If the next token is a semicolon, it is consumed; otherwise, error
3279 recovery is attempted. */
3282 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3284 /* Look for the trailing `;'. */
3285 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3287 /* If there is additional (erroneous) input, skip to the end of
3289 cp_parser_skip_to_end_of_statement (parser
);
3290 /* If the next token is now a `;', consume it. */
3291 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3292 cp_lexer_consume_token (parser
->lexer
);
3296 /* Skip tokens until we have consumed an entire block, or until we
3297 have consumed a non-nested `;'. */
3300 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3302 int nesting_depth
= 0;
3304 /* Unwind generic function template scope if necessary. */
3305 if (parser
->fully_implicit_function_template_p
)
3306 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
3308 while (nesting_depth
>= 0)
3310 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3312 switch (token
->type
)
3315 case CPP_PRAGMA_EOL
:
3316 /* If we've run out of tokens, stop. */
3320 /* Stop if this is an unnested ';'. */
3325 case CPP_CLOSE_BRACE
:
3326 /* Stop if this is an unnested '}', or closes the outermost
3329 if (nesting_depth
< 0)
3335 case CPP_OPEN_BRACE
:
3344 /* Consume the token. */
3345 cp_lexer_consume_token (parser
->lexer
);
3349 /* Skip tokens until a non-nested closing curly brace is the next
3350 token, or there are no more tokens. Return true in the first case,
3354 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3356 unsigned nesting_depth
= 0;
3360 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3362 switch (token
->type
)
3365 case CPP_PRAGMA_EOL
:
3366 /* If we've run out of tokens, stop. */
3369 case CPP_CLOSE_BRACE
:
3370 /* If the next token is a non-nested `}', then we have reached
3371 the end of the current block. */
3372 if (nesting_depth
-- == 0)
3376 case CPP_OPEN_BRACE
:
3377 /* If it the next token is a `{', then we are entering a new
3378 block. Consume the entire block. */
3386 /* Consume the token. */
3387 cp_lexer_consume_token (parser
->lexer
);
3391 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3392 parameter is the PRAGMA token, allowing us to purge the entire pragma
3396 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3400 parser
->lexer
->in_pragma
= false;
3403 token
= cp_lexer_consume_token (parser
->lexer
);
3404 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3406 /* Ensure that the pragma is not parsed again. */
3407 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3410 /* Require pragma end of line, resyncing with it as necessary. The
3411 arguments are as for cp_parser_skip_to_pragma_eol. */
3414 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3416 parser
->lexer
->in_pragma
= false;
3417 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3418 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3421 /* This is a simple wrapper around make_typename_type. When the id is
3422 an unresolved identifier node, we can provide a superior diagnostic
3423 using cp_parser_diagnose_invalid_type_name. */
3426 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3427 location_t id_location
)
3430 if (identifier_p (id
))
3432 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3433 /*complain=*/tf_none
);
3434 if (result
== error_mark_node
)
3435 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3438 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3441 /* This is a wrapper around the
3442 make_{pointer,ptrmem,reference}_declarator functions that decides
3443 which one to call based on the CODE and CLASS_TYPE arguments. The
3444 CODE argument should be one of the values returned by
3445 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3446 appertain to the pointer or reference. */
3448 static cp_declarator
*
3449 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3450 cp_cv_quals cv_qualifiers
,
3451 cp_declarator
*target
,
3454 if (code
== ERROR_MARK
)
3455 return cp_error_declarator
;
3457 if (code
== INDIRECT_REF
)
3458 if (class_type
== NULL_TREE
)
3459 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3461 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3462 target
, attributes
);
3463 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3464 return make_reference_declarator (cv_qualifiers
, target
,
3466 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3467 return make_reference_declarator (cv_qualifiers
, target
,
3472 /* Create a new C++ parser. */
3475 cp_parser_new (void)
3481 /* cp_lexer_new_main is called before doing GC allocation because
3482 cp_lexer_new_main might load a PCH file. */
3483 lexer
= cp_lexer_new_main ();
3485 /* Initialize the binops_by_token so that we can get the tree
3486 directly from the token. */
3487 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3488 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3490 parser
= ggc_cleared_alloc
<cp_parser
> ();
3491 parser
->lexer
= lexer
;
3492 parser
->context
= cp_parser_context_new (NULL
);
3494 /* For now, we always accept GNU extensions. */
3495 parser
->allow_gnu_extensions_p
= 1;
3497 /* The `>' token is a greater-than operator, not the end of a
3499 parser
->greater_than_is_operator_p
= true;
3501 parser
->default_arg_ok_p
= true;
3503 /* We are not parsing a constant-expression. */
3504 parser
->integral_constant_expression_p
= false;
3505 parser
->allow_non_integral_constant_expression_p
= false;
3506 parser
->non_integral_constant_expression_p
= false;
3508 /* Local variable names are not forbidden. */
3509 parser
->local_variables_forbidden_p
= false;
3511 /* We are not processing an `extern "C"' declaration. */
3512 parser
->in_unbraced_linkage_specification_p
= false;
3514 /* We are not processing a declarator. */
3515 parser
->in_declarator_p
= false;
3517 /* We are not processing a template-argument-list. */
3518 parser
->in_template_argument_list_p
= false;
3520 /* We are not in an iteration statement. */
3521 parser
->in_statement
= 0;
3523 /* We are not in a switch statement. */
3524 parser
->in_switch_statement_p
= false;
3526 /* We are not parsing a type-id inside an expression. */
3527 parser
->in_type_id_in_expr_p
= false;
3529 /* Declarations aren't implicitly extern "C". */
3530 parser
->implicit_extern_c
= false;
3532 /* String literals should be translated to the execution character set. */
3533 parser
->translate_strings_p
= true;
3535 /* We are not parsing a function body. */
3536 parser
->in_function_body
= false;
3538 /* We can correct until told otherwise. */
3539 parser
->colon_corrects_to_scope_p
= true;
3541 /* The unparsed function queue is empty. */
3542 push_unparsed_function_queues (parser
);
3544 /* There are no classes being defined. */
3545 parser
->num_classes_being_defined
= 0;
3547 /* No template parameters apply. */
3548 parser
->num_template_parameter_lists
= 0;
3550 /* Not declaring an implicit function template. */
3551 parser
->auto_is_implicit_function_template_parm_p
= false;
3552 parser
->fully_implicit_function_template_p
= false;
3553 parser
->implicit_template_parms
= 0;
3554 parser
->implicit_template_scope
= 0;
3559 /* Create a cp_lexer structure which will emit the tokens in CACHE
3560 and push it onto the parser's lexer stack. This is used for delayed
3561 parsing of in-class method bodies and default arguments, and should
3562 not be confused with tentative parsing. */
3564 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
3566 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
3567 lexer
->next
= parser
->lexer
;
3568 parser
->lexer
= lexer
;
3570 /* Move the current source position to that of the first token in the
3572 cp_lexer_set_source_position_from_token (lexer
->next_token
);
3575 /* Pop the top lexer off the parser stack. This is never used for the
3576 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3578 cp_parser_pop_lexer (cp_parser
*parser
)
3580 cp_lexer
*lexer
= parser
->lexer
;
3581 parser
->lexer
= lexer
->next
;
3582 cp_lexer_destroy (lexer
);
3584 /* Put the current source position back where it was before this
3585 lexer was pushed. */
3586 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
3589 /* Lexical conventions [gram.lex] */
3591 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3595 cp_parser_identifier (cp_parser
* parser
)
3599 /* Look for the identifier. */
3600 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
3601 /* Return the value. */
3602 return token
? token
->u
.value
: error_mark_node
;
3605 /* Parse a sequence of adjacent string constants. Returns a
3606 TREE_STRING representing the combined, nul-terminated string
3607 constant. If TRANSLATE is true, translate the string to the
3608 execution character set. If WIDE_OK is true, a wide string is
3611 C++98 [lex.string] says that if a narrow string literal token is
3612 adjacent to a wide string literal token, the behavior is undefined.
3613 However, C99 6.4.5p4 says that this results in a wide string literal.
3614 We follow C99 here, for consistency with the C front end.
3616 This code is largely lifted from lex_string() in c-lex.c.
3618 FUTURE: ObjC++ will need to handle @-strings here. */
3620 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
3621 bool lookup_udlit
= true)
3625 struct obstack str_ob
;
3626 cpp_string str
, istr
, *strs
;
3628 enum cpp_ttype type
, curr_type
;
3629 int have_suffix_p
= 0;
3631 tree suffix_id
= NULL_TREE
;
3632 bool curr_tok_is_userdef_p
= false;
3634 tok
= cp_lexer_peek_token (parser
->lexer
);
3635 if (!cp_parser_is_string_literal (tok
))
3637 cp_parser_error (parser
, "expected string-literal");
3638 return error_mark_node
;
3641 if (cpp_userdef_string_p (tok
->type
))
3643 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3644 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3645 curr_tok_is_userdef_p
= true;
3649 string_tree
= tok
->u
.value
;
3650 curr_type
= tok
->type
;
3654 /* Try to avoid the overhead of creating and destroying an obstack
3655 for the common case of just one string. */
3656 if (!cp_parser_is_string_literal
3657 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
3659 cp_lexer_consume_token (parser
->lexer
);
3661 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3662 str
.len
= TREE_STRING_LENGTH (string_tree
);
3665 if (curr_tok_is_userdef_p
)
3667 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3669 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3672 curr_type
= tok
->type
;
3678 gcc_obstack_init (&str_ob
);
3683 cp_lexer_consume_token (parser
->lexer
);
3685 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
3686 str
.len
= TREE_STRING_LENGTH (string_tree
);
3688 if (curr_tok_is_userdef_p
)
3690 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
3691 if (have_suffix_p
== 0)
3693 suffix_id
= curr_suffix_id
;
3696 else if (have_suffix_p
== 1
3697 && curr_suffix_id
!= suffix_id
)
3699 error ("inconsistent user-defined literal suffixes"
3700 " %qD and %qD in string literal",
3701 suffix_id
, curr_suffix_id
);
3704 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3707 curr_type
= tok
->type
;
3709 if (type
!= curr_type
)
3711 if (type
== CPP_STRING
)
3713 else if (curr_type
!= CPP_STRING
)
3714 error_at (tok
->location
,
3715 "unsupported non-standard concatenation "
3716 "of string literals");
3719 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
3721 tok
= cp_lexer_peek_token (parser
->lexer
);
3722 if (cpp_userdef_string_p (tok
->type
))
3724 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
3725 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
3726 curr_tok_is_userdef_p
= true;
3730 string_tree
= tok
->u
.value
;
3731 curr_type
= tok
->type
;
3732 curr_tok_is_userdef_p
= false;
3735 while (cp_parser_is_string_literal (tok
));
3737 strs
= (cpp_string
*) obstack_finish (&str_ob
);
3740 if (type
!= CPP_STRING
&& !wide_ok
)
3742 cp_parser_error (parser
, "a wide string is invalid in this context");
3746 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
3747 (parse_in
, strs
, count
, &istr
, type
))
3749 value
= build_string (istr
.len
, (const char *)istr
.text
);
3750 free (CONST_CAST (unsigned char *, istr
.text
));
3756 case CPP_UTF8STRING
:
3757 TREE_TYPE (value
) = char_array_type_node
;
3760 TREE_TYPE (value
) = char16_array_type_node
;
3763 TREE_TYPE (value
) = char32_array_type_node
;
3766 TREE_TYPE (value
) = wchar_array_type_node
;
3770 value
= fix_string_type (value
);
3774 tree literal
= build_userdef_literal (suffix_id
, value
,
3775 OT_NONE
, NULL_TREE
);
3777 value
= cp_parser_userdef_string_literal (literal
);
3783 /* cpp_interpret_string has issued an error. */
3784 value
= error_mark_node
;
3787 obstack_free (&str_ob
, 0);
3792 /* Look up a literal operator with the name and the exact arguments. */
3795 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
3798 decl
= lookup_name (name
);
3799 if (!decl
|| !is_overloaded_fn (decl
))
3800 return error_mark_node
;
3802 for (fns
= decl
; fns
; fns
= OVL_NEXT (fns
))
3806 tree fn
= OVL_CURRENT (fns
);
3807 tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3808 if (parmtypes
!= NULL_TREE
)
3810 for (ix
= 0; ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
3811 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
3813 tree tparm
= TREE_VALUE (parmtypes
);
3814 tree targ
= TREE_TYPE ((*args
)[ix
]);
3815 bool ptr
= TYPE_PTR_P (tparm
);
3816 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
3817 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
3819 || !same_type_p (TREE_TYPE (tparm
),
3824 && ix
== vec_safe_length (args
)
3825 /* May be this should be sufficient_parms_p instead,
3826 depending on how exactly should user-defined literals
3827 work in presence of default arguments on the literal
3828 operator parameters. */
3829 && parmtypes
== void_list_node
)
3834 return error_mark_node
;
3837 /* Parse a user-defined char constant. Returns a call to a user-defined
3838 literal operator taking the character as an argument. */
3841 cp_parser_userdef_char_literal (cp_parser
*parser
)
3843 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3844 tree literal
= token
->u
.value
;
3845 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3846 tree value
= USERDEF_LITERAL_VALUE (literal
);
3847 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3850 /* Build up a call to the user-defined operator */
3851 /* Lookup the name we got back from the id-expression. */
3852 vec
<tree
, va_gc
> *args
= make_tree_vector ();
3853 vec_safe_push (args
, value
);
3854 decl
= lookup_literal_operator (name
, args
);
3855 if (!decl
|| decl
== error_mark_node
)
3857 error ("unable to find character literal operator %qD with %qT argument",
3858 name
, TREE_TYPE (value
));
3859 release_tree_vector (args
);
3860 return error_mark_node
;
3862 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
3863 release_tree_vector (args
);
3864 if (result
!= error_mark_node
)
3867 error ("unable to find character literal operator %qD with %qT argument",
3868 name
, TREE_TYPE (value
));
3869 return error_mark_node
;
3872 /* A subroutine of cp_parser_userdef_numeric_literal to
3873 create a char... template parameter pack from a string node. */
3876 make_char_string_pack (tree value
)
3879 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3880 const char *str
= TREE_STRING_POINTER (value
);
3881 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
3882 tree argvec
= make_tree_vec (1);
3884 /* Fill in CHARVEC with all of the parameters. */
3885 charvec
= make_tree_vec (len
);
3886 for (i
= 0; i
< len
; ++i
)
3887 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
, str
[i
]);
3889 /* Build the argument packs. */
3890 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3891 TREE_TYPE (argpack
) = char_type_node
;
3893 TREE_VEC_ELT (argvec
, 0) = argpack
;
3898 /* A subroutine of cp_parser_userdef_numeric_literal to
3899 create a char... template parameter pack from a string node. */
3902 make_string_pack (tree value
)
3905 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
3906 const unsigned char *str
3907 = (const unsigned char *) TREE_STRING_POINTER (value
);
3908 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
3909 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
3910 tree argvec
= make_tree_vec (2);
3912 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
3913 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
3915 /* First template parm is character type. */
3916 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
3918 /* Fill in CHARVEC with all of the parameters. */
3919 charvec
= make_tree_vec (len
);
3920 for (int i
= 0; i
< len
; ++i
)
3921 TREE_VEC_ELT (charvec
, i
)
3922 = double_int_to_tree (str_char_type_node
,
3923 double_int::from_buffer (str
+ i
* sz
, sz
));
3925 /* Build the argument packs. */
3926 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
3927 TREE_TYPE (argpack
) = str_char_type_node
;
3929 TREE_VEC_ELT (argvec
, 1) = argpack
;
3934 /* Parse a user-defined numeric constant. returns a call to a user-defined
3935 literal operator. */
3938 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
3940 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
3941 tree literal
= token
->u
.value
;
3942 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
3943 tree value
= USERDEF_LITERAL_VALUE (literal
);
3944 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
3945 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
3946 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
3948 vec
<tree
, va_gc
> *args
;
3950 /* Look for a literal operator taking the exact type of numeric argument
3951 as the literal value. */
3952 args
= make_tree_vector ();
3953 vec_safe_push (args
, value
);
3954 decl
= lookup_literal_operator (name
, args
);
3955 if (decl
&& decl
!= error_mark_node
)
3957 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
3958 if (result
!= error_mark_node
)
3960 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
3961 warning_at (token
->location
, OPT_Woverflow
,
3962 "integer literal exceeds range of %qT type",
3963 long_long_unsigned_type_node
);
3967 warning_at (token
->location
, OPT_Woverflow
,
3968 "floating literal exceeds range of %qT type",
3969 long_double_type_node
);
3970 else if (overflow
< 0)
3971 warning_at (token
->location
, OPT_Woverflow
,
3972 "floating literal truncated to zero");
3974 release_tree_vector (args
);
3978 release_tree_vector (args
);
3980 /* If the numeric argument didn't work, look for a raw literal
3981 operator taking a const char* argument consisting of the number
3982 in string format. */
3983 args
= make_tree_vector ();
3984 vec_safe_push (args
, num_string
);
3985 decl
= lookup_literal_operator (name
, args
);
3986 if (decl
&& decl
!= error_mark_node
)
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 /* If the raw literal didn't work, look for a non-type template
3998 function with parameter pack char.... Call the function with
3999 template parameter characters representing the number. */
4000 args
= make_tree_vector ();
4001 decl
= lookup_literal_operator (name
, args
);
4002 if (decl
&& decl
!= error_mark_node
)
4004 tree tmpl_args
= make_char_string_pack (num_string
);
4005 decl
= lookup_template_function (decl
, tmpl_args
);
4006 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4007 if (result
!= error_mark_node
)
4009 release_tree_vector (args
);
4013 release_tree_vector (args
);
4015 error ("unable to find numeric literal operator %qD", name
);
4016 if (!cpp_get_options (parse_in
)->ext_numeric_literals
)
4017 inform (token
->location
, "use -std=gnu++11 or -fext-numeric-literals "
4018 "to enable more built-in suffixes");
4019 return error_mark_node
;
4022 /* Parse a user-defined string constant. Returns a call to a user-defined
4023 literal operator taking a character pointer and the length of the string
4027 cp_parser_userdef_string_literal (tree literal
)
4029 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4030 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4031 tree value
= USERDEF_LITERAL_VALUE (literal
);
4032 int len
= TREE_STRING_LENGTH (value
)
4033 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4035 vec
<tree
, va_gc
> *args
;
4037 /* Look for a template function with typename parameter CharT
4038 and parameter pack CharT... Call the function with
4039 template parameter characters representing the string. */
4040 args
= make_tree_vector ();
4041 decl
= lookup_literal_operator (name
, args
);
4042 if (decl
&& decl
!= error_mark_node
)
4044 tree tmpl_args
= make_string_pack (value
);
4045 decl
= lookup_template_function (decl
, tmpl_args
);
4046 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4047 if (result
!= error_mark_node
)
4049 release_tree_vector (args
);
4053 release_tree_vector (args
);
4055 /* Build up a call to the user-defined operator */
4056 /* Lookup the name we got back from the id-expression. */
4057 args
= make_tree_vector ();
4058 vec_safe_push (args
, value
);
4059 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4060 decl
= lookup_name (name
);
4061 if (!decl
|| decl
== error_mark_node
)
4063 error ("unable to find string literal operator %qD", name
);
4064 release_tree_vector (args
);
4065 return error_mark_node
;
4067 result
= finish_call_expr (decl
, &args
, false, true, tf_none
);
4068 release_tree_vector (args
);
4069 if (result
!= error_mark_node
)
4072 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4073 name
, TREE_TYPE (value
), size_type_node
);
4074 return error_mark_node
;
4078 /* Basic concepts [gram.basic] */
4080 /* Parse a translation-unit.
4083 declaration-seq [opt]
4085 Returns TRUE if all went well. */
4088 cp_parser_translation_unit (cp_parser
* parser
)
4090 /* The address of the first non-permanent object on the declarator
4092 static void *declarator_obstack_base
;
4096 /* Create the declarator obstack, if necessary. */
4097 if (!cp_error_declarator
)
4099 gcc_obstack_init (&declarator_obstack
);
4100 /* Create the error declarator. */
4101 cp_error_declarator
= make_declarator (cdk_error
);
4102 /* Create the empty parameter list. */
4103 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
);
4104 /* Remember where the base of the declarator obstack lies. */
4105 declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4108 cp_parser_declaration_seq_opt (parser
);
4110 /* If there are no tokens left then all went well. */
4111 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
4113 /* Get rid of the token array; we don't need it any more. */
4114 cp_lexer_destroy (parser
->lexer
);
4115 parser
->lexer
= NULL
;
4117 /* This file might have been a context that's implicitly extern
4118 "C". If so, pop the lang context. (Only relevant for PCH.) */
4119 if (parser
->implicit_extern_c
)
4121 pop_lang_context ();
4122 parser
->implicit_extern_c
= false;
4126 finish_translation_unit ();
4132 cp_parser_error (parser
, "expected declaration");
4136 /* Make sure the declarator obstack was fully cleaned up. */
4137 gcc_assert (obstack_next_free (&declarator_obstack
)
4138 == declarator_obstack_base
);
4140 /* All went well. */
4144 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4145 decltype context. */
4147 static inline tsubst_flags_t
4148 complain_flags (bool decltype_p
)
4150 tsubst_flags_t complain
= tf_warning_or_error
;
4152 complain
|= tf_decltype
;
4156 /* We're about to parse a collection of statements. If we're currently
4157 parsing tentatively, set up a firewall so that any nested
4158 cp_parser_commit_to_tentative_parse won't affect the current context. */
4160 static cp_token_position
4161 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4163 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4166 cp_parser_parse_tentatively (parser
);
4167 cp_parser_commit_to_topmost_tentative_parse (parser
);
4168 return cp_lexer_token_position (parser
->lexer
, false);
4171 /* We've finished parsing the collection of statements. Wrap up the
4172 firewall and replace the relevant tokens with the parsed form. */
4175 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4181 /* Finish the firewall level. */
4182 cp_parser_parse_definitely (parser
);
4183 /* And remember the result of the parse for when we try again. */
4184 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4185 token
->type
= CPP_PREPARSED_EXPR
;
4186 token
->u
.value
= expr
;
4187 token
->keyword
= RID_MAX
;
4188 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4191 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4192 enclosing parentheses. */
4195 cp_parser_statement_expr (cp_parser
*parser
)
4197 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4199 /* Consume the '('. */
4200 cp_lexer_consume_token (parser
->lexer
);
4201 /* Start the statement-expression. */
4202 tree expr
= begin_stmt_expr ();
4203 /* Parse the compound-statement. */
4204 cp_parser_compound_statement (parser
, expr
, false, false);
4206 expr
= finish_stmt_expr (expr
, false);
4207 /* Consume the ')'. */
4208 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4209 cp_parser_skip_to_end_of_statement (parser
);
4211 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4215 /* Expressions [gram.expr] */
4217 /* Parse a primary-expression.
4224 lambda-expression (C++11)
4229 ( compound-statement )
4230 __builtin_va_arg ( assignment-expression , type-id )
4231 __builtin_offsetof ( type-id , offsetof-expression )
4234 __has_nothrow_assign ( type-id )
4235 __has_nothrow_constructor ( type-id )
4236 __has_nothrow_copy ( type-id )
4237 __has_trivial_assign ( type-id )
4238 __has_trivial_constructor ( type-id )
4239 __has_trivial_copy ( type-id )
4240 __has_trivial_destructor ( type-id )
4241 __has_virtual_destructor ( type-id )
4242 __is_abstract ( type-id )
4243 __is_base_of ( type-id , type-id )
4244 __is_class ( type-id )
4245 __is_empty ( type-id )
4246 __is_enum ( type-id )
4247 __is_final ( type-id )
4248 __is_literal_type ( type-id )
4249 __is_pod ( type-id )
4250 __is_polymorphic ( type-id )
4251 __is_std_layout ( type-id )
4252 __is_trivial ( type-id )
4253 __is_union ( type-id )
4255 Objective-C++ Extension:
4263 ADDRESS_P is true iff this expression was immediately preceded by
4264 "&" and therefore might denote a pointer-to-member. CAST_P is true
4265 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4266 true iff this expression is a template argument.
4268 Returns a representation of the expression. Upon return, *IDK
4269 indicates what kind of id-expression (if any) was present. */
4272 cp_parser_primary_expression (cp_parser
*parser
,
4275 bool template_arg_p
,
4279 cp_token
*token
= NULL
;
4281 /* Assume the primary expression is not an id-expression. */
4282 *idk
= CP_ID_KIND_NONE
;
4284 /* Peek at the next token. */
4285 token
= cp_lexer_peek_token (parser
->lexer
);
4286 switch ((int) token
->type
)
4295 user-defined-literal */
4301 case CPP_PREPARSED_EXPR
:
4302 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
4303 return cp_parser_userdef_numeric_literal (parser
);
4304 token
= cp_lexer_consume_token (parser
->lexer
);
4305 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
4307 error_at (token
->location
,
4308 "fixed-point types not supported in C++");
4309 return error_mark_node
;
4311 /* Floating-point literals are only allowed in an integral
4312 constant expression if they are cast to an integral or
4313 enumeration type. */
4314 if (TREE_CODE (token
->u
.value
) == REAL_CST
4315 && parser
->integral_constant_expression_p
4318 /* CAST_P will be set even in invalid code like "int(2.7 +
4319 ...)". Therefore, we have to check that the next token
4320 is sure to end the cast. */
4323 cp_token
*next_token
;
4325 next_token
= cp_lexer_peek_token (parser
->lexer
);
4326 if (/* The comma at the end of an
4327 enumerator-definition. */
4328 next_token
->type
!= CPP_COMMA
4329 /* The curly brace at the end of an enum-specifier. */
4330 && next_token
->type
!= CPP_CLOSE_BRACE
4331 /* The end of a statement. */
4332 && next_token
->type
!= CPP_SEMICOLON
4333 /* The end of the cast-expression. */
4334 && next_token
->type
!= CPP_CLOSE_PAREN
4335 /* The end of an array bound. */
4336 && next_token
->type
!= CPP_CLOSE_SQUARE
4337 /* The closing ">" in a template-argument-list. */
4338 && (next_token
->type
!= CPP_GREATER
4339 || parser
->greater_than_is_operator_p
)
4340 /* C++0x only: A ">>" treated like two ">" tokens,
4341 in a template-argument-list. */
4342 && (next_token
->type
!= CPP_RSHIFT
4343 || (cxx_dialect
== cxx98
)
4344 || parser
->greater_than_is_operator_p
))
4348 /* If we are within a cast, then the constraint that the
4349 cast is to an integral or enumeration type will be
4350 checked at that point. If we are not within a cast, then
4351 this code is invalid. */
4353 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
4355 return token
->u
.value
;
4357 case CPP_CHAR_USERDEF
:
4358 case CPP_CHAR16_USERDEF
:
4359 case CPP_CHAR32_USERDEF
:
4360 case CPP_WCHAR_USERDEF
:
4361 return cp_parser_userdef_char_literal (parser
);
4367 case CPP_UTF8STRING
:
4368 case CPP_STRING_USERDEF
:
4369 case CPP_STRING16_USERDEF
:
4370 case CPP_STRING32_USERDEF
:
4371 case CPP_WSTRING_USERDEF
:
4372 case CPP_UTF8STRING_USERDEF
:
4373 /* ??? Should wide strings be allowed when parser->translate_strings_p
4374 is false (i.e. in attributes)? If not, we can kill the third
4375 argument to cp_parser_string_literal. */
4376 return cp_parser_string_literal (parser
,
4377 parser
->translate_strings_p
,
4380 case CPP_OPEN_PAREN
:
4381 /* If we see `( { ' then we are looking at the beginning of
4382 a GNU statement-expression. */
4383 if (cp_parser_allow_gnu_extensions_p (parser
)
4384 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
4386 /* Statement-expressions are not allowed by the standard. */
4387 pedwarn (token
->location
, OPT_Wpedantic
,
4388 "ISO C++ forbids braced-groups within expressions");
4390 /* And they're not allowed outside of a function-body; you
4391 cannot, for example, write:
4393 int i = ({ int j = 3; j + 1; });
4395 at class or namespace scope. */
4396 if (!parser
->in_function_body
4397 || parser
->in_template_argument_list_p
)
4399 error_at (token
->location
,
4400 "statement-expressions are not allowed outside "
4401 "functions nor in template-argument lists");
4402 cp_parser_skip_to_end_of_block_or_statement (parser
);
4403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
4404 cp_lexer_consume_token (parser
->lexer
);
4405 return error_mark_node
;
4408 return cp_parser_statement_expr (parser
);
4410 /* Otherwise it's a normal parenthesized expression. */
4413 bool saved_greater_than_is_operator_p
;
4415 /* Consume the `('. */
4416 cp_lexer_consume_token (parser
->lexer
);
4417 /* Within a parenthesized expression, a `>' token is always
4418 the greater-than operator. */
4419 saved_greater_than_is_operator_p
4420 = parser
->greater_than_is_operator_p
;
4421 parser
->greater_than_is_operator_p
= true;
4423 /* Parse the parenthesized expression. */
4424 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
4425 /* Let the front end know that this expression was
4426 enclosed in parentheses. This matters in case, for
4427 example, the expression is of the form `A::B', since
4428 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4430 expr
= finish_parenthesized_expr (expr
);
4431 /* DR 705: Wrapping an unqualified name in parentheses
4432 suppresses arg-dependent lookup. We want to pass back
4433 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4434 (c++/37862), but none of the others. */
4435 if (*idk
!= CP_ID_KIND_QUALIFIED
)
4436 *idk
= CP_ID_KIND_NONE
;
4438 /* The `>' token might be the end of a template-id or
4439 template-parameter-list now. */
4440 parser
->greater_than_is_operator_p
4441 = saved_greater_than_is_operator_p
;
4442 /* Consume the `)'. */
4443 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
4444 cp_parser_skip_to_end_of_statement (parser
);
4449 case CPP_OPEN_SQUARE
:
4451 if (c_dialect_objc ())
4453 /* We might have an Objective-C++ message. */
4454 cp_parser_parse_tentatively (parser
);
4455 tree msg
= cp_parser_objc_message_expression (parser
);
4456 /* If that works out, we're done ... */
4457 if (cp_parser_parse_definitely (parser
))
4459 /* ... else, fall though to see if it's a lambda. */
4461 tree lam
= cp_parser_lambda_expression (parser
);
4462 /* Don't warn about a failed tentative parse. */
4463 if (cp_parser_error_occurred (parser
))
4464 return error_mark_node
;
4465 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
4469 case CPP_OBJC_STRING
:
4470 if (c_dialect_objc ())
4471 /* We have an Objective-C++ string literal. */
4472 return cp_parser_objc_expression (parser
);
4473 cp_parser_error (parser
, "expected primary-expression");
4474 return error_mark_node
;
4477 switch (token
->keyword
)
4479 /* These two are the boolean literals. */
4481 cp_lexer_consume_token (parser
->lexer
);
4482 return boolean_true_node
;
4484 cp_lexer_consume_token (parser
->lexer
);
4485 return boolean_false_node
;
4487 /* The `__null' literal. */
4489 cp_lexer_consume_token (parser
->lexer
);
4492 /* The `nullptr' literal. */
4494 cp_lexer_consume_token (parser
->lexer
);
4495 return nullptr_node
;
4497 /* Recognize the `this' keyword. */
4499 cp_lexer_consume_token (parser
->lexer
);
4500 if (parser
->local_variables_forbidden_p
)
4502 error_at (token
->location
,
4503 "%<this%> may not be used in this context");
4504 return error_mark_node
;
4506 /* Pointers cannot appear in constant-expressions. */
4507 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
4508 return error_mark_node
;
4509 return finish_this_expr ();
4511 /* The `operator' keyword can be the beginning of an
4516 case RID_FUNCTION_NAME
:
4517 case RID_PRETTY_FUNCTION_NAME
:
4518 case RID_C99_FUNCTION_NAME
:
4520 non_integral_constant name
;
4522 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4523 __func__ are the names of variables -- but they are
4524 treated specially. Therefore, they are handled here,
4525 rather than relying on the generic id-expression logic
4526 below. Grammatically, these names are id-expressions.
4528 Consume the token. */
4529 token
= cp_lexer_consume_token (parser
->lexer
);
4531 switch (token
->keyword
)
4533 case RID_FUNCTION_NAME
:
4534 name
= NIC_FUNC_NAME
;
4536 case RID_PRETTY_FUNCTION_NAME
:
4537 name
= NIC_PRETTY_FUNC
;
4539 case RID_C99_FUNCTION_NAME
:
4540 name
= NIC_C99_FUNC
;
4546 if (cp_parser_non_integral_constant_expression (parser
, name
))
4547 return error_mark_node
;
4549 /* Look up the name. */
4550 return finish_fname (token
->u
.value
);
4557 source_location type_location
;
4559 /* The `__builtin_va_arg' construct is used to handle
4560 `va_arg'. Consume the `__builtin_va_arg' token. */
4561 cp_lexer_consume_token (parser
->lexer
);
4562 /* Look for the opening `('. */
4563 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
4564 /* Now, parse the assignment-expression. */
4565 expression
= cp_parser_assignment_expression (parser
);
4566 /* Look for the `,'. */
4567 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
4568 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
4569 /* Parse the type-id. */
4570 type
= cp_parser_type_id (parser
);
4571 /* Look for the closing `)'. */
4572 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
4573 /* Using `va_arg' in a constant-expression is not
4575 if (cp_parser_non_integral_constant_expression (parser
,
4577 return error_mark_node
;
4578 return build_x_va_arg (type_location
, expression
, type
);
4582 return cp_parser_builtin_offsetof (parser
);
4584 case RID_HAS_NOTHROW_ASSIGN
:
4585 case RID_HAS_NOTHROW_CONSTRUCTOR
:
4586 case RID_HAS_NOTHROW_COPY
:
4587 case RID_HAS_TRIVIAL_ASSIGN
:
4588 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
4589 case RID_HAS_TRIVIAL_COPY
:
4590 case RID_HAS_TRIVIAL_DESTRUCTOR
:
4591 case RID_HAS_VIRTUAL_DESTRUCTOR
:
4592 case RID_IS_ABSTRACT
:
4593 case RID_IS_BASE_OF
:
4598 case RID_IS_LITERAL_TYPE
:
4600 case RID_IS_POLYMORPHIC
:
4601 case RID_IS_STD_LAYOUT
:
4602 case RID_IS_TRIVIAL
:
4603 case RID_IS_TRIVIALLY_ASSIGNABLE
:
4604 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
4605 case RID_IS_TRIVIALLY_COPYABLE
:
4607 return cp_parser_trait_expr (parser
, token
->keyword
);
4609 /* Objective-C++ expressions. */
4611 case RID_AT_PROTOCOL
:
4612 case RID_AT_SELECTOR
:
4613 return cp_parser_objc_expression (parser
);
4616 if (parser
->in_function_body
4617 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
4620 error_at (token
->location
,
4621 "a template declaration cannot appear at block scope");
4622 cp_parser_skip_to_end_of_block_or_statement (parser
);
4623 return error_mark_node
;
4626 cp_parser_error (parser
, "expected primary-expression");
4627 return error_mark_node
;
4630 /* An id-expression can start with either an identifier, a
4631 `::' as the beginning of a qualified-id, or the "operator"
4635 case CPP_TEMPLATE_ID
:
4636 case CPP_NESTED_NAME_SPECIFIER
:
4640 const char *error_msg
;
4643 cp_token
*id_expr_token
;
4646 /* Parse the id-expression. */
4648 = cp_parser_id_expression (parser
,
4649 /*template_keyword_p=*/false,
4650 /*check_dependency_p=*/true,
4652 /*declarator_p=*/false,
4653 /*optional_p=*/false);
4654 if (id_expression
== error_mark_node
)
4655 return error_mark_node
;
4656 id_expr_token
= token
;
4657 token
= cp_lexer_peek_token (parser
->lexer
);
4658 done
= (token
->type
!= CPP_OPEN_SQUARE
4659 && token
->type
!= CPP_OPEN_PAREN
4660 && token
->type
!= CPP_DOT
4661 && token
->type
!= CPP_DEREF
4662 && token
->type
!= CPP_PLUS_PLUS
4663 && token
->type
!= CPP_MINUS_MINUS
);
4664 /* If we have a template-id, then no further lookup is
4665 required. If the template-id was for a template-class, we
4666 will sometimes have a TYPE_DECL at this point. */
4667 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
4668 || TREE_CODE (id_expression
) == TYPE_DECL
)
4669 decl
= id_expression
;
4670 /* Look up the name. */
4673 tree ambiguous_decls
;
4675 /* If we already know that this lookup is ambiguous, then
4676 we've already issued an error message; there's no reason
4678 if (id_expr_token
->type
== CPP_NAME
4679 && id_expr_token
->error_reported
)
4681 cp_parser_simulate_error (parser
);
4682 return error_mark_node
;
4685 decl
= cp_parser_lookup_name (parser
, id_expression
,
4688 /*is_namespace=*/false,
4689 /*check_dependency=*/true,
4691 id_expr_token
->location
);
4692 /* If the lookup was ambiguous, an error will already have
4694 if (ambiguous_decls
)
4695 return error_mark_node
;
4697 /* In Objective-C++, we may have an Objective-C 2.0
4698 dot-syntax for classes here. */
4699 if (c_dialect_objc ()
4700 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
4701 && TREE_CODE (decl
) == TYPE_DECL
4702 && objc_is_class_name (decl
))
4705 cp_lexer_consume_token (parser
->lexer
);
4706 component
= cp_parser_identifier (parser
);
4707 if (component
== error_mark_node
)
4708 return error_mark_node
;
4710 return objc_build_class_component_ref (id_expression
, component
);
4713 /* In Objective-C++, an instance variable (ivar) may be preferred
4714 to whatever cp_parser_lookup_name() found. */
4715 decl
= objc_lookup_ivar (decl
, id_expression
);
4717 /* If name lookup gives us a SCOPE_REF, then the
4718 qualifying scope was dependent. */
4719 if (TREE_CODE (decl
) == SCOPE_REF
)
4721 /* At this point, we do not know if DECL is a valid
4722 integral constant expression. We assume that it is
4723 in fact such an expression, so that code like:
4725 template <int N> struct A {
4729 is accepted. At template-instantiation time, we
4730 will check that B<N>::i is actually a constant. */
4733 /* Check to see if DECL is a local variable in a context
4734 where that is forbidden. */
4735 if (parser
->local_variables_forbidden_p
4736 && local_variable_p (decl
))
4738 /* It might be that we only found DECL because we are
4739 trying to be generous with pre-ISO scoping rules.
4740 For example, consider:
4744 for (int i = 0; i < 10; ++i) {}
4745 extern void f(int j = i);
4748 Here, name look up will originally find the out
4749 of scope `i'. We need to issue a warning message,
4750 but then use the global `i'. */
4751 decl
= check_for_out_of_scope_variable (decl
);
4752 if (local_variable_p (decl
))
4754 error_at (id_expr_token
->location
,
4755 "local variable %qD may not appear in this context",
4757 return error_mark_node
;
4762 decl
= (finish_id_expression
4763 (id_expression
, decl
, parser
->scope
,
4765 parser
->integral_constant_expression_p
,
4766 parser
->allow_non_integral_constant_expression_p
,
4767 &parser
->non_integral_constant_expression_p
,
4768 template_p
, done
, address_p
,
4771 id_expr_token
->location
));
4773 cp_parser_error (parser
, error_msg
);
4777 /* Anything else is an error. */
4779 cp_parser_error (parser
, "expected primary-expression");
4780 return error_mark_node
;
4785 cp_parser_primary_expression (cp_parser
*parser
,
4788 bool template_arg_p
,
4791 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
4792 /*decltype*/false, idk
);
4795 /* Parse an id-expression.
4802 :: [opt] nested-name-specifier template [opt] unqualified-id
4804 :: operator-function-id
4807 Return a representation of the unqualified portion of the
4808 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4809 a `::' or nested-name-specifier.
4811 Often, if the id-expression was a qualified-id, the caller will
4812 want to make a SCOPE_REF to represent the qualified-id. This
4813 function does not do this in order to avoid wastefully creating
4814 SCOPE_REFs when they are not required.
4816 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4819 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4820 uninstantiated templates.
4822 If *TEMPLATE_P is non-NULL, it is set to true iff the
4823 `template' keyword is used to explicitly indicate that the entity
4824 named is a template.
4826 If DECLARATOR_P is true, the id-expression is appearing as part of
4827 a declarator, rather than as part of an expression. */
4830 cp_parser_id_expression (cp_parser
*parser
,
4831 bool template_keyword_p
,
4832 bool check_dependency_p
,
4837 bool global_scope_p
;
4838 bool nested_name_specifier_p
;
4840 /* Assume the `template' keyword was not used. */
4842 *template_p
= template_keyword_p
;
4844 /* Look for the optional `::' operator. */
4846 = (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false)
4848 /* Look for the optional nested-name-specifier. */
4849 nested_name_specifier_p
4850 = (cp_parser_nested_name_specifier_opt (parser
,
4851 /*typename_keyword_p=*/false,
4856 /* If there is a nested-name-specifier, then we are looking at
4857 the first qualified-id production. */
4858 if (nested_name_specifier_p
)
4861 tree saved_object_scope
;
4862 tree saved_qualifying_scope
;
4863 tree unqualified_id
;
4866 /* See if the next token is the `template' keyword. */
4868 template_p
= &is_template
;
4869 *template_p
= cp_parser_optional_template_keyword (parser
);
4870 /* Name lookup we do during the processing of the
4871 unqualified-id might obliterate SCOPE. */
4872 saved_scope
= parser
->scope
;
4873 saved_object_scope
= parser
->object_scope
;
4874 saved_qualifying_scope
= parser
->qualifying_scope
;
4875 /* Process the final unqualified-id. */
4876 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
4879 /*optional_p=*/false);
4880 /* Restore the SAVED_SCOPE for our caller. */
4881 parser
->scope
= saved_scope
;
4882 parser
->object_scope
= saved_object_scope
;
4883 parser
->qualifying_scope
= saved_qualifying_scope
;
4885 return unqualified_id
;
4887 /* Otherwise, if we are in global scope, then we are looking at one
4888 of the other qualified-id productions. */
4889 else if (global_scope_p
)
4894 /* Peek at the next token. */
4895 token
= cp_lexer_peek_token (parser
->lexer
);
4897 /* If it's an identifier, and the next token is not a "<", then
4898 we can avoid the template-id case. This is an optimization
4899 for this common case. */
4900 if (token
->type
== CPP_NAME
4901 && !cp_parser_nth_token_starts_template_argument_list_p
4903 return cp_parser_identifier (parser
);
4905 cp_parser_parse_tentatively (parser
);
4906 /* Try a template-id. */
4907 id
= cp_parser_template_id (parser
,
4908 /*template_keyword_p=*/false,
4909 /*check_dependency_p=*/true,
4912 /* If that worked, we're done. */
4913 if (cp_parser_parse_definitely (parser
))
4916 /* Peek at the next token. (Changes in the token buffer may
4917 have invalidated the pointer obtained above.) */
4918 token
= cp_lexer_peek_token (parser
->lexer
);
4920 switch (token
->type
)
4923 return cp_parser_identifier (parser
);
4926 if (token
->keyword
== RID_OPERATOR
)
4927 return cp_parser_operator_function_id (parser
);
4931 cp_parser_error (parser
, "expected id-expression");
4932 return error_mark_node
;
4936 return cp_parser_unqualified_id (parser
, template_keyword_p
,
4937 /*check_dependency_p=*/true,
4942 /* Parse an unqualified-id.
4946 operator-function-id
4947 conversion-function-id
4951 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4952 keyword, in a construct like `A::template ...'.
4954 Returns a representation of unqualified-id. For the `identifier'
4955 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4956 production a BIT_NOT_EXPR is returned; the operand of the
4957 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4958 other productions, see the documentation accompanying the
4959 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4960 names are looked up in uninstantiated templates. If DECLARATOR_P
4961 is true, the unqualified-id is appearing as part of a declarator,
4962 rather than as part of an expression. */
4965 cp_parser_unqualified_id (cp_parser
* parser
,
4966 bool template_keyword_p
,
4967 bool check_dependency_p
,
4973 /* Peek at the next token. */
4974 token
= cp_lexer_peek_token (parser
->lexer
);
4976 switch ((int) token
->type
)
4982 /* We don't know yet whether or not this will be a
4984 cp_parser_parse_tentatively (parser
);
4985 /* Try a template-id. */
4986 id
= cp_parser_template_id (parser
, template_keyword_p
,
4990 /* If it worked, we're done. */
4991 if (cp_parser_parse_definitely (parser
))
4993 /* Otherwise, it's an ordinary identifier. */
4994 return cp_parser_identifier (parser
);
4997 case CPP_TEMPLATE_ID
:
4998 return cp_parser_template_id (parser
, template_keyword_p
,
5006 tree qualifying_scope
;
5011 /* Consume the `~' token. */
5012 cp_lexer_consume_token (parser
->lexer
);
5013 /* Parse the class-name. The standard, as written, seems to
5016 template <typename T> struct S { ~S (); };
5017 template <typename T> S<T>::~S() {}
5019 is invalid, since `~' must be followed by a class-name, but
5020 `S<T>' is dependent, and so not known to be a class.
5021 That's not right; we need to look in uninstantiated
5022 templates. A further complication arises from:
5024 template <typename T> void f(T t) {
5028 Here, it is not possible to look up `T' in the scope of `T'
5029 itself. We must look in both the current scope, and the
5030 scope of the containing complete expression.
5032 Yet another issue is:
5041 The standard does not seem to say that the `S' in `~S'
5042 should refer to the type `S' and not the data member
5045 /* DR 244 says that we look up the name after the "~" in the
5046 same scope as we looked up the qualifying name. That idea
5047 isn't fully worked out; it's more complicated than that. */
5048 scope
= parser
->scope
;
5049 object_scope
= parser
->object_scope
;
5050 qualifying_scope
= parser
->qualifying_scope
;
5052 /* Check for invalid scopes. */
5053 if (scope
== error_mark_node
)
5055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5056 cp_lexer_consume_token (parser
->lexer
);
5057 return error_mark_node
;
5059 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
5061 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5062 error_at (token
->location
,
5063 "scope %qT before %<~%> is not a class-name",
5065 cp_parser_simulate_error (parser
);
5066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
5067 cp_lexer_consume_token (parser
->lexer
);
5068 return error_mark_node
;
5070 gcc_assert (!scope
|| TYPE_P (scope
));
5072 /* If the name is of the form "X::~X" it's OK even if X is a
5074 token
= cp_lexer_peek_token (parser
->lexer
);
5076 && token
->type
== CPP_NAME
5077 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5079 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
5080 || (CLASS_TYPE_P (scope
)
5081 && constructor_name_p (token
->u
.value
, scope
))))
5083 cp_lexer_consume_token (parser
->lexer
);
5084 return build_nt (BIT_NOT_EXPR
, scope
);
5087 /* ~auto means the destructor of whatever the object is. */
5088 if (cp_parser_is_keyword (token
, RID_AUTO
))
5090 if (cxx_dialect
< cxx14
)
5091 pedwarn (input_location
, 0,
5092 "%<~auto%> only available with "
5093 "-std=c++14 or -std=gnu++14");
5094 cp_lexer_consume_token (parser
->lexer
);
5095 return build_nt (BIT_NOT_EXPR
, make_auto ());
5098 /* If there was an explicit qualification (S::~T), first look
5099 in the scope given by the qualification (i.e., S).
5101 Note: in the calls to cp_parser_class_name below we pass
5102 typename_type so that lookup finds the injected-class-name
5103 rather than the constructor. */
5105 type_decl
= NULL_TREE
;
5108 cp_parser_parse_tentatively (parser
);
5109 type_decl
= cp_parser_class_name (parser
,
5110 /*typename_keyword_p=*/false,
5111 /*template_keyword_p=*/false,
5113 /*check_dependency=*/false,
5114 /*class_head_p=*/false,
5116 if (cp_parser_parse_definitely (parser
))
5119 /* In "N::S::~S", look in "N" as well. */
5120 if (!done
&& scope
&& qualifying_scope
)
5122 cp_parser_parse_tentatively (parser
);
5123 parser
->scope
= qualifying_scope
;
5124 parser
->object_scope
= NULL_TREE
;
5125 parser
->qualifying_scope
= NULL_TREE
;
5127 = cp_parser_class_name (parser
,
5128 /*typename_keyword_p=*/false,
5129 /*template_keyword_p=*/false,
5131 /*check_dependency=*/false,
5132 /*class_head_p=*/false,
5134 if (cp_parser_parse_definitely (parser
))
5137 /* In "p->S::~T", look in the scope given by "*p" as well. */
5138 else if (!done
&& object_scope
)
5140 cp_parser_parse_tentatively (parser
);
5141 parser
->scope
= object_scope
;
5142 parser
->object_scope
= NULL_TREE
;
5143 parser
->qualifying_scope
= NULL_TREE
;
5145 = cp_parser_class_name (parser
,
5146 /*typename_keyword_p=*/false,
5147 /*template_keyword_p=*/false,
5149 /*check_dependency=*/false,
5150 /*class_head_p=*/false,
5152 if (cp_parser_parse_definitely (parser
))
5155 /* Look in the surrounding context. */
5158 parser
->scope
= NULL_TREE
;
5159 parser
->object_scope
= NULL_TREE
;
5160 parser
->qualifying_scope
= NULL_TREE
;
5161 if (processing_template_decl
)
5162 cp_parser_parse_tentatively (parser
);
5164 = cp_parser_class_name (parser
,
5165 /*typename_keyword_p=*/false,
5166 /*template_keyword_p=*/false,
5168 /*check_dependency=*/false,
5169 /*class_head_p=*/false,
5171 if (processing_template_decl
5172 && ! cp_parser_parse_definitely (parser
))
5174 /* We couldn't find a type with this name, so just accept
5175 it and check for a match at instantiation time. */
5176 type_decl
= cp_parser_identifier (parser
);
5177 if (type_decl
!= error_mark_node
)
5178 type_decl
= build_nt (BIT_NOT_EXPR
, type_decl
);
5182 /* If an error occurred, assume that the name of the
5183 destructor is the same as the name of the qualifying
5184 class. That allows us to keep parsing after running
5185 into ill-formed destructor names. */
5186 if (type_decl
== error_mark_node
&& scope
)
5187 return build_nt (BIT_NOT_EXPR
, scope
);
5188 else if (type_decl
== error_mark_node
)
5189 return error_mark_node
;
5191 /* Check that destructor name and scope match. */
5192 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
5194 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
5195 error_at (token
->location
,
5196 "declaration of %<~%T%> as member of %qT",
5198 cp_parser_simulate_error (parser
);
5199 return error_mark_node
;
5204 A typedef-name that names a class shall not be used as the
5205 identifier in the declarator for a destructor declaration. */
5207 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
5208 && !DECL_SELF_REFERENCE_P (type_decl
)
5209 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5210 error_at (token
->location
,
5211 "typedef-name %qD used as destructor declarator",
5214 return build_nt (BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
5218 if (token
->keyword
== RID_OPERATOR
)
5222 /* This could be a template-id, so we try that first. */
5223 cp_parser_parse_tentatively (parser
);
5224 /* Try a template-id. */
5225 id
= cp_parser_template_id (parser
, template_keyword_p
,
5226 /*check_dependency_p=*/true,
5229 /* If that worked, we're done. */
5230 if (cp_parser_parse_definitely (parser
))
5232 /* We still don't know whether we're looking at an
5233 operator-function-id or a conversion-function-id. */
5234 cp_parser_parse_tentatively (parser
);
5235 /* Try an operator-function-id. */
5236 id
= cp_parser_operator_function_id (parser
);
5237 /* If that didn't work, try a conversion-function-id. */
5238 if (!cp_parser_parse_definitely (parser
))
5239 id
= cp_parser_conversion_function_id (parser
);
5240 else if (UDLIT_OPER_P (id
))
5243 const char *name
= UDLIT_OP_SUFFIX (id
);
5244 if (name
[0] != '_' && !in_system_header_at (input_location
)
5246 warning (0, "literal operator suffixes not preceded by %<_%>"
5247 " are reserved for future standardization");
5257 cp_parser_error (parser
, "expected unqualified-id");
5258 return error_mark_node
;
5262 /* Parse an (optional) nested-name-specifier.
5264 nested-name-specifier: [C++98]
5265 class-or-namespace-name :: nested-name-specifier [opt]
5266 class-or-namespace-name :: template nested-name-specifier [opt]
5268 nested-name-specifier: [C++0x]
5271 nested-name-specifier identifier ::
5272 nested-name-specifier template [opt] simple-template-id ::
5274 PARSER->SCOPE should be set appropriately before this function is
5275 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5276 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5279 Sets PARSER->SCOPE to the class (TYPE) or namespace
5280 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5281 it unchanged if there is no nested-name-specifier. Returns the new
5282 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5284 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5285 part of a declaration and/or decl-specifier. */
5288 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
5289 bool typename_keyword_p
,
5290 bool check_dependency_p
,
5292 bool is_declaration
)
5294 bool success
= false;
5295 cp_token_position start
= 0;
5298 /* Remember where the nested-name-specifier starts. */
5299 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5301 start
= cp_lexer_token_position (parser
->lexer
, false);
5302 push_deferring_access_checks (dk_deferred
);
5309 tree saved_qualifying_scope
;
5310 bool template_keyword_p
;
5312 /* Spot cases that cannot be the beginning of a
5313 nested-name-specifier. */
5314 token
= cp_lexer_peek_token (parser
->lexer
);
5316 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5317 the already parsed nested-name-specifier. */
5318 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
5320 /* Grab the nested-name-specifier and continue the loop. */
5321 cp_parser_pre_parsed_nested_name_specifier (parser
);
5322 /* If we originally encountered this nested-name-specifier
5323 with IS_DECLARATION set to false, we will not have
5324 resolved TYPENAME_TYPEs, so we must do so here. */
5326 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5328 new_scope
= resolve_typename_type (parser
->scope
,
5329 /*only_current_p=*/false);
5330 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
5331 parser
->scope
= new_scope
;
5337 /* Spot cases that cannot be the beginning of a
5338 nested-name-specifier. On the second and subsequent times
5339 through the loop, we look for the `template' keyword. */
5340 if (success
&& token
->keyword
== RID_TEMPLATE
)
5342 /* A template-id can start a nested-name-specifier. */
5343 else if (token
->type
== CPP_TEMPLATE_ID
)
5345 /* DR 743: decltype can be used in a nested-name-specifier. */
5346 else if (token_is_decltype (token
))
5350 /* If the next token is not an identifier, then it is
5351 definitely not a type-name or namespace-name. */
5352 if (token
->type
!= CPP_NAME
)
5354 /* If the following token is neither a `<' (to begin a
5355 template-id), nor a `::', then we are not looking at a
5356 nested-name-specifier. */
5357 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
5359 if (token
->type
== CPP_COLON
5360 && parser
->colon_corrects_to_scope_p
5361 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
5363 error_at (token
->location
,
5364 "found %<:%> in nested-name-specifier, expected %<::%>");
5365 token
->type
= CPP_SCOPE
;
5368 if (token
->type
!= CPP_SCOPE
5369 && !cp_parser_nth_token_starts_template_argument_list_p
5374 /* The nested-name-specifier is optional, so we parse
5376 cp_parser_parse_tentatively (parser
);
5378 /* Look for the optional `template' keyword, if this isn't the
5379 first time through the loop. */
5381 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
5383 template_keyword_p
= false;
5385 /* Save the old scope since the name lookup we are about to do
5386 might destroy it. */
5387 old_scope
= parser
->scope
;
5388 saved_qualifying_scope
= parser
->qualifying_scope
;
5389 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5390 look up names in "X<T>::I" in order to determine that "Y" is
5391 a template. So, if we have a typename at this point, we make
5392 an effort to look through it. */
5394 && !typename_keyword_p
5396 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
5397 parser
->scope
= resolve_typename_type (parser
->scope
,
5398 /*only_current_p=*/false);
5399 /* Parse the qualifying entity. */
5401 = cp_parser_qualifying_entity (parser
,
5407 /* Look for the `::' token. */
5408 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
5410 /* If we found what we wanted, we keep going; otherwise, we're
5412 if (!cp_parser_parse_definitely (parser
))
5414 bool error_p
= false;
5416 /* Restore the OLD_SCOPE since it was valid before the
5417 failed attempt at finding the last
5418 class-or-namespace-name. */
5419 parser
->scope
= old_scope
;
5420 parser
->qualifying_scope
= saved_qualifying_scope
;
5422 /* If the next token is a decltype, and the one after that is a
5423 `::', then the decltype has failed to resolve to a class or
5424 enumeration type. Give this error even when parsing
5425 tentatively since it can't possibly be valid--and we're going
5426 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5427 won't get another chance.*/
5428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
5429 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5432 token
= cp_lexer_consume_token (parser
->lexer
);
5433 error_at (token
->location
, "decltype evaluates to %qT, "
5434 "which is not a class or enumeration type",
5436 parser
->scope
= error_mark_node
;
5440 cp_lexer_consume_token (parser
->lexer
);
5443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
5444 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
5446 /* If we have a non-type template-id followed by ::, it can't
5447 possibly be valid. */
5448 token
= cp_lexer_peek_token (parser
->lexer
);
5449 tree tid
= token
->u
.tree_check_value
->value
;
5450 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
5451 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
5453 tree tmpl
= NULL_TREE
;
5454 if (is_overloaded_fn (tid
))
5456 tree fns
= get_fns (tid
);
5457 if (!OVL_CHAIN (fns
))
5458 tmpl
= OVL_CURRENT (fns
);
5459 error_at (token
->location
, "function template-id %qD "
5460 "in nested-name-specifier", tid
);
5464 /* Variable template. */
5465 tmpl
= TREE_OPERAND (tid
, 0);
5466 gcc_assert (variable_template_p (tmpl
));
5467 error_at (token
->location
, "variable template-id %qD "
5468 "in nested-name-specifier", tid
);
5471 inform (DECL_SOURCE_LOCATION (tmpl
),
5472 "%qD declared here", tmpl
);
5474 parser
->scope
= error_mark_node
;
5478 cp_lexer_consume_token (parser
->lexer
);
5479 cp_lexer_consume_token (parser
->lexer
);
5483 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
5485 /* If the next token is an identifier, and the one after
5486 that is a `::', then any valid interpretation would have
5487 found a class-or-namespace-name. */
5488 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
5489 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5491 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
5494 token
= cp_lexer_consume_token (parser
->lexer
);
5497 if (!token
->error_reported
)
5500 tree ambiguous_decls
;
5502 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
5504 /*is_template=*/false,
5505 /*is_namespace=*/false,
5506 /*check_dependency=*/true,
5509 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5510 error_at (token
->location
,
5511 "%qD used without template parameters",
5513 else if (ambiguous_decls
)
5515 // cp_parser_lookup_name has the same diagnostic,
5516 // thus make sure to emit it at most once.
5517 if (cp_parser_uncommitted_to_tentative_parse_p
5520 error_at (token
->location
,
5521 "reference to %qD is ambiguous",
5523 print_candidates (ambiguous_decls
);
5525 decl
= error_mark_node
;
5529 if (cxx_dialect
!= cxx98
)
5530 cp_parser_name_lookup_error
5531 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
5534 cp_parser_name_lookup_error
5535 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
5539 parser
->scope
= error_mark_node
;
5541 /* Treat this as a successful nested-name-specifier
5546 If the name found is not a class-name (clause
5547 _class_) or namespace-name (_namespace.def_), the
5548 program is ill-formed. */
5551 cp_lexer_consume_token (parser
->lexer
);
5555 /* We've found one valid nested-name-specifier. */
5557 /* Name lookup always gives us a DECL. */
5558 if (TREE_CODE (new_scope
) == TYPE_DECL
)
5559 new_scope
= TREE_TYPE (new_scope
);
5560 /* Uses of "template" must be followed by actual templates. */
5561 if (template_keyword_p
5562 && !(CLASS_TYPE_P (new_scope
)
5563 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
5564 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
5565 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
5566 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
5567 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
5568 == TEMPLATE_ID_EXPR
)))
5569 permerror (input_location
, TYPE_P (new_scope
)
5570 ? G_("%qT is not a template")
5571 : G_("%qD is not a template"),
5573 /* If it is a class scope, try to complete it; we are about to
5574 be looking up names inside the class. */
5575 if (TYPE_P (new_scope
)
5576 /* Since checking types for dependency can be expensive,
5577 avoid doing it if the type is already complete. */
5578 && !COMPLETE_TYPE_P (new_scope
)
5579 /* Do not try to complete dependent types. */
5580 && !dependent_type_p (new_scope
))
5582 new_scope
= complete_type (new_scope
);
5583 /* If it is a typedef to current class, use the current
5584 class instead, as the typedef won't have any names inside
5586 if (!COMPLETE_TYPE_P (new_scope
)
5587 && currently_open_class (new_scope
))
5588 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
5590 /* Make sure we look in the right scope the next time through
5592 parser
->scope
= new_scope
;
5595 /* If parsing tentatively, replace the sequence of tokens that makes
5596 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5597 token. That way, should we re-parse the token stream, we will
5598 not have to repeat the effort required to do the parse, nor will
5599 we issue duplicate error messages. */
5600 if (success
&& start
)
5604 token
= cp_lexer_token_at (parser
->lexer
, start
);
5605 /* Reset the contents of the START token. */
5606 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
5607 /* Retrieve any deferred checks. Do not pop this access checks yet
5608 so the memory will not be reclaimed during token replacing below. */
5609 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
5610 token
->u
.tree_check_value
->value
= parser
->scope
;
5611 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
5612 token
->u
.tree_check_value
->qualifying_scope
=
5613 parser
->qualifying_scope
;
5614 token
->keyword
= RID_MAX
;
5616 /* Purge all subsequent tokens. */
5617 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
5621 pop_to_parent_deferring_access_checks ();
5623 return success
? parser
->scope
: NULL_TREE
;
5626 /* Parse a nested-name-specifier. See
5627 cp_parser_nested_name_specifier_opt for details. This function
5628 behaves identically, except that it will an issue an error if no
5629 nested-name-specifier is present. */
5632 cp_parser_nested_name_specifier (cp_parser
*parser
,
5633 bool typename_keyword_p
,
5634 bool check_dependency_p
,
5636 bool is_declaration
)
5640 /* Look for the nested-name-specifier. */
5641 scope
= cp_parser_nested_name_specifier_opt (parser
,
5646 /* If it was not present, issue an error message. */
5649 cp_parser_error (parser
, "expected nested-name-specifier");
5650 parser
->scope
= NULL_TREE
;
5656 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5657 this is either a class-name or a namespace-name (which corresponds
5658 to the class-or-namespace-name production in the grammar). For
5659 C++0x, it can also be a type-name that refers to an enumeration
5660 type or a simple-template-id.
5662 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5663 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5664 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5665 TYPE_P is TRUE iff the next name should be taken as a class-name,
5666 even the same name is declared to be another entity in the same
5669 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5670 specified by the class-or-namespace-name. If neither is found the
5671 ERROR_MARK_NODE is returned. */
5674 cp_parser_qualifying_entity (cp_parser
*parser
,
5675 bool typename_keyword_p
,
5676 bool template_keyword_p
,
5677 bool check_dependency_p
,
5679 bool is_declaration
)
5682 tree saved_qualifying_scope
;
5683 tree saved_object_scope
;
5686 bool successful_parse_p
;
5688 /* DR 743: decltype can appear in a nested-name-specifier. */
5689 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
5691 scope
= cp_parser_decltype (parser
);
5692 if (TREE_CODE (scope
) != ENUMERAL_TYPE
5693 && !MAYBE_CLASS_TYPE_P (scope
))
5695 cp_parser_simulate_error (parser
);
5696 return error_mark_node
;
5698 if (TYPE_NAME (scope
))
5699 scope
= TYPE_NAME (scope
);
5703 /* Before we try to parse the class-name, we must save away the
5704 current PARSER->SCOPE since cp_parser_class_name will destroy
5706 saved_scope
= parser
->scope
;
5707 saved_qualifying_scope
= parser
->qualifying_scope
;
5708 saved_object_scope
= parser
->object_scope
;
5709 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5710 there is no need to look for a namespace-name. */
5711 only_class_p
= template_keyword_p
5712 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
5714 cp_parser_parse_tentatively (parser
);
5715 scope
= cp_parser_class_name (parser
,
5718 type_p
? class_type
: none_type
,
5720 /*class_head_p=*/false,
5722 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
5723 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5725 && cxx_dialect
!= cxx98
5726 && !successful_parse_p
)
5728 /* Restore the saved scope. */
5729 parser
->scope
= saved_scope
;
5730 parser
->qualifying_scope
= saved_qualifying_scope
;
5731 parser
->object_scope
= saved_object_scope
;
5733 /* Parse tentatively. */
5734 cp_parser_parse_tentatively (parser
);
5736 /* Parse a type-name */
5737 scope
= cp_parser_type_name (parser
);
5739 /* "If the name found does not designate a namespace or a class,
5740 enumeration, or dependent type, the program is ill-formed."
5742 We cover classes and dependent types above and namespaces below,
5743 so this code is only looking for enums. */
5744 if (!scope
|| TREE_CODE (scope
) != TYPE_DECL
5745 || TREE_CODE (TREE_TYPE (scope
)) != ENUMERAL_TYPE
)
5746 cp_parser_simulate_error (parser
);
5748 successful_parse_p
= cp_parser_parse_definitely (parser
);
5750 /* If that didn't work, try for a namespace-name. */
5751 if (!only_class_p
&& !successful_parse_p
)
5753 /* Restore the saved scope. */
5754 parser
->scope
= saved_scope
;
5755 parser
->qualifying_scope
= saved_qualifying_scope
;
5756 parser
->object_scope
= saved_object_scope
;
5757 /* If we are not looking at an identifier followed by the scope
5758 resolution operator, then this is not part of a
5759 nested-name-specifier. (Note that this function is only used
5760 to parse the components of a nested-name-specifier.) */
5761 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
5762 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
5763 return error_mark_node
;
5764 scope
= cp_parser_namespace_name (parser
);
5770 /* Return true if we are looking at a compound-literal, false otherwise. */
5773 cp_parser_compound_literal_p (cp_parser
*parser
)
5775 /* Consume the `('. */
5776 cp_lexer_consume_token (parser
->lexer
);
5778 cp_lexer_save_tokens (parser
->lexer
);
5780 /* Skip tokens until the next token is a closing parenthesis.
5781 If we find the closing `)', and the next token is a `{', then
5782 we are looking at a compound-literal. */
5783 bool compound_literal_p
5784 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
5785 /*consume_paren=*/true)
5786 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
5788 /* Roll back the tokens we skipped. */
5789 cp_lexer_rollback_tokens (parser
->lexer
);
5791 return compound_literal_p
;
5794 /* Parse a postfix-expression.
5798 postfix-expression [ expression ]
5799 postfix-expression ( expression-list [opt] )
5800 simple-type-specifier ( expression-list [opt] )
5801 typename :: [opt] nested-name-specifier identifier
5802 ( expression-list [opt] )
5803 typename :: [opt] nested-name-specifier template [opt] template-id
5804 ( expression-list [opt] )
5805 postfix-expression . template [opt] id-expression
5806 postfix-expression -> template [opt] id-expression
5807 postfix-expression . pseudo-destructor-name
5808 postfix-expression -> pseudo-destructor-name
5809 postfix-expression ++
5810 postfix-expression --
5811 dynamic_cast < type-id > ( expression )
5812 static_cast < type-id > ( expression )
5813 reinterpret_cast < type-id > ( expression )
5814 const_cast < type-id > ( expression )
5815 typeid ( expression )
5821 ( type-id ) { initializer-list , [opt] }
5823 This extension is a GNU version of the C99 compound-literal
5824 construct. (The C99 grammar uses `type-name' instead of `type-id',
5825 but they are essentially the same concept.)
5827 If ADDRESS_P is true, the postfix expression is the operand of the
5828 `&' operator. CAST_P is true if this expression is the target of a
5831 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5832 class member access expressions [expr.ref].
5834 Returns a representation of the expression. */
5837 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
5838 bool member_access_only_p
, bool decltype_p
,
5839 cp_id_kind
* pidk_return
)
5844 cp_id_kind idk
= CP_ID_KIND_NONE
;
5845 tree postfix_expression
= NULL_TREE
;
5846 bool is_member_access
= false;
5847 int saved_in_statement
= -1;
5849 /* Peek at the next token. */
5850 token
= cp_lexer_peek_token (parser
->lexer
);
5851 loc
= token
->location
;
5852 /* Some of the productions are determined by keywords. */
5853 keyword
= token
->keyword
;
5863 const char *saved_message
;
5864 bool saved_in_type_id_in_expr_p
;
5866 /* All of these can be handled in the same way from the point
5867 of view of parsing. Begin by consuming the token
5868 identifying the cast. */
5869 cp_lexer_consume_token (parser
->lexer
);
5871 /* New types cannot be defined in the cast. */
5872 saved_message
= parser
->type_definition_forbidden_message
;
5873 parser
->type_definition_forbidden_message
5874 = G_("types may not be defined in casts");
5876 /* Look for the opening `<'. */
5877 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
5878 /* Parse the type to which we are casting. */
5879 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5880 parser
->in_type_id_in_expr_p
= true;
5881 type
= cp_parser_type_id (parser
);
5882 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5883 /* Look for the closing `>'. */
5884 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
5885 /* Restore the old message. */
5886 parser
->type_definition_forbidden_message
= saved_message
;
5888 bool saved_greater_than_is_operator_p
5889 = parser
->greater_than_is_operator_p
;
5890 parser
->greater_than_is_operator_p
= true;
5892 /* And the expression which is being cast. */
5893 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5894 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
5895 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5897 parser
->greater_than_is_operator_p
5898 = saved_greater_than_is_operator_p
;
5900 /* Only type conversions to integral or enumeration types
5901 can be used in constant-expressions. */
5902 if (!cast_valid_in_integral_constant_expression_p (type
)
5903 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
5904 return error_mark_node
;
5910 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
5914 = build_static_cast (type
, expression
, tf_warning_or_error
);
5918 = build_reinterpret_cast (type
, expression
,
5919 tf_warning_or_error
);
5923 = build_const_cast (type
, expression
, tf_warning_or_error
);
5934 const char *saved_message
;
5935 bool saved_in_type_id_in_expr_p
;
5937 /* Consume the `typeid' token. */
5938 cp_lexer_consume_token (parser
->lexer
);
5939 /* Look for the `(' token. */
5940 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
5941 /* Types cannot be defined in a `typeid' expression. */
5942 saved_message
= parser
->type_definition_forbidden_message
;
5943 parser
->type_definition_forbidden_message
5944 = G_("types may not be defined in a %<typeid%> expression");
5945 /* We can't be sure yet whether we're looking at a type-id or an
5947 cp_parser_parse_tentatively (parser
);
5948 /* Try a type-id first. */
5949 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
5950 parser
->in_type_id_in_expr_p
= true;
5951 type
= cp_parser_type_id (parser
);
5952 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
5953 /* Look for the `)' token. Otherwise, we can't be sure that
5954 we're not looking at an expression: consider `typeid (int
5955 (3))', for example. */
5956 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5957 /* If all went well, simply lookup the type-id. */
5958 if (cp_parser_parse_definitely (parser
))
5959 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
5960 /* Otherwise, fall back to the expression variant. */
5965 /* Look for an expression. */
5966 expression
= cp_parser_expression (parser
, & idk
);
5967 /* Compute its typeid. */
5968 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
5969 /* Look for the `)' token. */
5970 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
5972 /* Restore the saved message. */
5973 parser
->type_definition_forbidden_message
= saved_message
;
5974 /* `typeid' may not appear in an integral constant expression. */
5975 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
5976 return error_mark_node
;
5983 /* The syntax permitted here is the same permitted for an
5984 elaborated-type-specifier. */
5985 type
= cp_parser_elaborated_type_specifier (parser
,
5986 /*is_friend=*/false,
5987 /*is_declaration=*/false);
5988 postfix_expression
= cp_parser_functional_cast (parser
, type
);
5992 case RID_CILK_SPAWN
:
5994 cp_lexer_consume_token (parser
->lexer
);
5995 token
= cp_lexer_peek_token (parser
->lexer
);
5996 if (token
->type
== CPP_SEMICOLON
)
5998 error_at (token
->location
, "%<_Cilk_spawn%> must be followed by "
6000 postfix_expression
= error_mark_node
;
6003 else if (!current_function_decl
)
6005 error_at (token
->location
, "%<_Cilk_spawn%> may only be used "
6006 "inside a function");
6007 postfix_expression
= error_mark_node
;
6012 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6013 saved_in_statement
= parser
->in_statement
;
6014 parser
->in_statement
|= IN_CILK_SPAWN
;
6016 cfun
->calls_cilk_spawn
= 1;
6017 postfix_expression
=
6018 cp_parser_postfix_expression (parser
, false, false,
6019 false, false, &idk
);
6022 error_at (token
->location
, "-fcilkplus must be enabled to use"
6023 " %<_Cilk_spawn%>");
6024 cfun
->calls_cilk_spawn
= 0;
6026 else if (saved_in_statement
& IN_CILK_SPAWN
)
6028 error_at (token
->location
, "consecutive %<_Cilk_spawn%> keywords "
6029 "are not permitted");
6030 postfix_expression
= error_mark_node
;
6031 cfun
->calls_cilk_spawn
= 0;
6035 postfix_expression
= build_cilk_spawn (token
->location
,
6036 postfix_expression
);
6037 if (postfix_expression
!= error_mark_node
)
6038 SET_EXPR_LOCATION (postfix_expression
, input_location
);
6039 parser
->in_statement
= parser
->in_statement
& ~IN_CILK_SPAWN
;
6044 case RID_BUILTIN_SHUFFLE
:
6046 vec
<tree
, va_gc
> *vec
;
6050 cp_lexer_consume_token (parser
->lexer
);
6051 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
6052 /*cast_p=*/false, /*allow_expansion_p=*/true,
6053 /*non_constant_p=*/NULL
);
6055 return error_mark_node
;
6057 FOR_EACH_VEC_ELT (*vec
, i
, p
)
6060 if (vec
->length () == 2)
6061 return build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
, (*vec
)[1],
6062 tf_warning_or_error
);
6063 else if (vec
->length () == 3)
6064 return build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1], (*vec
)[2],
6065 tf_warning_or_error
);
6068 error_at (loc
, "wrong number of arguments to "
6069 "%<__builtin_shuffle%>");
6070 return error_mark_node
;
6079 /* If the next thing is a simple-type-specifier, we may be
6080 looking at a functional cast. We could also be looking at
6081 an id-expression. So, we try the functional cast, and if
6082 that doesn't work we fall back to the primary-expression. */
6083 cp_parser_parse_tentatively (parser
);
6084 /* Look for the simple-type-specifier. */
6085 type
= cp_parser_simple_type_specifier (parser
,
6086 /*decl_specs=*/NULL
,
6087 CP_PARSER_FLAGS_NONE
);
6088 /* Parse the cast itself. */
6089 if (!cp_parser_error_occurred (parser
))
6091 = cp_parser_functional_cast (parser
, type
);
6092 /* If that worked, we're done. */
6093 if (cp_parser_parse_definitely (parser
))
6096 /* If the functional-cast didn't work out, try a
6097 compound-literal. */
6098 if (cp_parser_allow_gnu_extensions_p (parser
)
6099 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
6101 tree initializer
= NULL_TREE
;
6103 cp_parser_parse_tentatively (parser
);
6105 /* Avoid calling cp_parser_type_id pointlessly, see comment
6106 in cp_parser_cast_expression about c++/29234. */
6107 if (!cp_parser_compound_literal_p (parser
))
6108 cp_parser_simulate_error (parser
);
6111 /* Parse the type. */
6112 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6113 parser
->in_type_id_in_expr_p
= true;
6114 type
= cp_parser_type_id (parser
);
6115 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6116 /* Look for the `)'. */
6117 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
6120 /* If things aren't going well, there's no need to
6122 if (!cp_parser_error_occurred (parser
))
6124 bool non_constant_p
;
6125 /* Parse the brace-enclosed initializer list. */
6126 initializer
= cp_parser_braced_list (parser
,
6129 /* If that worked, we're definitely looking at a
6130 compound-literal expression. */
6131 if (cp_parser_parse_definitely (parser
))
6133 /* Warn the user that a compound literal is not
6134 allowed in standard C++. */
6135 pedwarn (input_location
, OPT_Wpedantic
,
6136 "ISO C++ forbids compound-literals");
6137 /* For simplicity, we disallow compound literals in
6138 constant-expressions. We could
6139 allow compound literals of integer type, whose
6140 initializer was a constant, in constant
6141 expressions. Permitting that usage, as a further
6142 extension, would not change the meaning of any
6143 currently accepted programs. (Of course, as
6144 compound literals are not part of ISO C++, the
6145 standard has nothing to say.) */
6146 if (cp_parser_non_integral_constant_expression (parser
,
6149 postfix_expression
= error_mark_node
;
6152 /* Form the representation of the compound-literal. */
6154 = finish_compound_literal (type
, initializer
,
6155 tf_warning_or_error
);
6160 /* It must be a primary-expression. */
6162 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
6163 /*template_arg_p=*/false,
6170 /* Note that we don't need to worry about calling build_cplus_new on a
6171 class-valued CALL_EXPR in decltype when it isn't the end of the
6172 postfix-expression; unary_complex_lvalue will take care of that for
6175 /* Keep looping until the postfix-expression is complete. */
6178 if (idk
== CP_ID_KIND_UNQUALIFIED
6179 && identifier_p (postfix_expression
)
6180 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
6181 /* It is not a Koenig lookup function call. */
6183 = unqualified_name_lookup_error (postfix_expression
);
6185 /* Peek at the next token. */
6186 token
= cp_lexer_peek_token (parser
->lexer
);
6188 switch (token
->type
)
6190 case CPP_OPEN_SQUARE
:
6191 if (cp_next_tokens_can_be_std_attribute_p (parser
))
6193 cp_parser_error (parser
,
6194 "two consecutive %<[%> shall "
6195 "only introduce an attribute");
6196 return error_mark_node
;
6199 = cp_parser_postfix_open_square_expression (parser
,
6203 idk
= CP_ID_KIND_NONE
;
6204 is_member_access
= false;
6207 case CPP_OPEN_PAREN
:
6208 /* postfix-expression ( expression-list [opt] ) */
6211 bool is_builtin_constant_p
;
6212 bool saved_integral_constant_expression_p
= false;
6213 bool saved_non_integral_constant_expression_p
= false;
6214 tsubst_flags_t complain
= complain_flags (decltype_p
);
6215 vec
<tree
, va_gc
> *args
;
6217 is_member_access
= false;
6219 is_builtin_constant_p
6220 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression
);
6221 if (is_builtin_constant_p
)
6223 /* The whole point of __builtin_constant_p is to allow
6224 non-constant expressions to appear as arguments. */
6225 saved_integral_constant_expression_p
6226 = parser
->integral_constant_expression_p
;
6227 saved_non_integral_constant_expression_p
6228 = parser
->non_integral_constant_expression_p
;
6229 parser
->integral_constant_expression_p
= false;
6231 args
= (cp_parser_parenthesized_expression_list
6233 /*cast_p=*/false, /*allow_expansion_p=*/true,
6234 /*non_constant_p=*/NULL
,
6235 /*want_literal_zero_p=*/warn_memset_transposed_args
));
6236 if (is_builtin_constant_p
)
6238 parser
->integral_constant_expression_p
6239 = saved_integral_constant_expression_p
;
6240 parser
->non_integral_constant_expression_p
6241 = saved_non_integral_constant_expression_p
;
6246 postfix_expression
= error_mark_node
;
6250 /* Function calls are not permitted in
6251 constant-expressions. */
6252 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
6253 && cp_parser_non_integral_constant_expression (parser
,
6256 postfix_expression
= error_mark_node
;
6257 release_tree_vector (args
);
6262 if (idk
== CP_ID_KIND_UNQUALIFIED
6263 || idk
== CP_ID_KIND_TEMPLATE_ID
)
6265 if (identifier_p (postfix_expression
))
6267 if (!args
->is_empty ())
6270 if (!any_type_dependent_arguments_p (args
))
6272 = perform_koenig_lookup (postfix_expression
, args
,
6277 = unqualified_fn_lookup_error (postfix_expression
);
6279 /* We do not perform argument-dependent lookup if
6280 normal lookup finds a non-function, in accordance
6281 with the expected resolution of DR 218. */
6282 else if (!args
->is_empty ()
6283 && is_overloaded_fn (postfix_expression
))
6285 tree fn
= get_first_fn (postfix_expression
);
6286 fn
= STRIP_TEMPLATE (fn
);
6288 /* Do not do argument dependent lookup if regular
6289 lookup finds a member function or a block-scope
6290 function declaration. [basic.lookup.argdep]/3 */
6291 if (!DECL_FUNCTION_MEMBER_P (fn
)
6292 && !DECL_LOCAL_FUNCTION_P (fn
))
6295 if (!any_type_dependent_arguments_p (args
))
6297 = perform_koenig_lookup (postfix_expression
, args
,
6303 if (warn_memset_transposed_args
)
6305 if (TREE_CODE (postfix_expression
) == FUNCTION_DECL
6306 && DECL_BUILT_IN_CLASS (postfix_expression
) == BUILT_IN_NORMAL
6307 && DECL_FUNCTION_CODE (postfix_expression
) == BUILT_IN_MEMSET
6308 && vec_safe_length (args
) == 3
6309 && integer_zerop ((*args
)[2])
6310 && LITERAL_ZERO_P ((*args
)[2])
6311 && !(integer_zerop ((*args
)[1])
6312 && LITERAL_ZERO_P ((*args
)[1])))
6313 warning (OPT_Wmemset_transposed_args
,
6314 "%<memset%> used with constant zero length "
6315 "parameter; this could be due to transposed "
6318 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6319 to avoid leaking those into folder and middle-end. */
6322 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
6323 if (TREE_CODE (arg
) == INTEGER_CST
&& LITERAL_ZERO_P (arg
))
6324 (*args
)[i
] = build_int_cst (TREE_TYPE (arg
), 0);
6327 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
6329 tree instance
= TREE_OPERAND (postfix_expression
, 0);
6330 tree fn
= TREE_OPERAND (postfix_expression
, 1);
6332 if (processing_template_decl
6333 && (type_dependent_expression_p (instance
)
6334 || (!BASELINK_P (fn
)
6335 && TREE_CODE (fn
) != FIELD_DECL
)
6336 || type_dependent_expression_p (fn
)
6337 || any_type_dependent_arguments_p (args
)))
6340 = build_nt_call_vec (postfix_expression
, args
);
6341 release_tree_vector (args
);
6345 if (BASELINK_P (fn
))
6348 = (build_new_method_call
6349 (instance
, fn
, &args
, NULL_TREE
,
6350 (idk
== CP_ID_KIND_QUALIFIED
6351 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
6358 = finish_call_expr (postfix_expression
, &args
,
6359 /*disallow_virtual=*/false,
6363 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
6364 || TREE_CODE (postfix_expression
) == MEMBER_REF
6365 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
6366 postfix_expression
= (build_offset_ref_call_from_tree
6367 (postfix_expression
, &args
,
6369 else if (idk
== CP_ID_KIND_QUALIFIED
)
6370 /* A call to a static class member, or a namespace-scope
6373 = finish_call_expr (postfix_expression
, &args
,
6374 /*disallow_virtual=*/true,
6378 /* All other function calls. */
6380 = finish_call_expr (postfix_expression
, &args
,
6381 /*disallow_virtual=*/false,
6385 protected_set_expr_location (postfix_expression
, token
->location
);
6387 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6388 idk
= CP_ID_KIND_NONE
;
6390 release_tree_vector (args
);
6396 /* postfix-expression . template [opt] id-expression
6397 postfix-expression . pseudo-destructor-name
6398 postfix-expression -> template [opt] id-expression
6399 postfix-expression -> pseudo-destructor-name */
6401 /* Consume the `.' or `->' operator. */
6402 cp_lexer_consume_token (parser
->lexer
);
6405 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
6409 is_member_access
= true;
6413 /* postfix-expression ++ */
6414 /* Consume the `++' token. */
6415 cp_lexer_consume_token (parser
->lexer
);
6416 /* Generate a representation for the complete expression. */
6418 = finish_increment_expr (postfix_expression
,
6419 POSTINCREMENT_EXPR
);
6420 /* Increments may not appear in constant-expressions. */
6421 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
6422 postfix_expression
= error_mark_node
;
6423 idk
= CP_ID_KIND_NONE
;
6424 is_member_access
= false;
6427 case CPP_MINUS_MINUS
:
6428 /* postfix-expression -- */
6429 /* Consume the `--' token. */
6430 cp_lexer_consume_token (parser
->lexer
);
6431 /* Generate a representation for the complete expression. */
6433 = finish_increment_expr (postfix_expression
,
6434 POSTDECREMENT_EXPR
);
6435 /* Decrements may not appear in constant-expressions. */
6436 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
6437 postfix_expression
= error_mark_node
;
6438 idk
= CP_ID_KIND_NONE
;
6439 is_member_access
= false;
6443 if (pidk_return
!= NULL
)
6444 * pidk_return
= idk
;
6445 if (member_access_only_p
)
6446 return is_member_access
? postfix_expression
: error_mark_node
;
6448 return postfix_expression
;
6452 /* We should never get here. */
6454 return error_mark_node
;
6457 /* This function parses Cilk Plus array notations. If a normal array expr. is
6458 parsed then the array index is passed back to the caller through *INIT_INDEX
6459 and the function returns a NULL_TREE. If array notation expr. is parsed,
6460 then *INIT_INDEX is ignored by the caller and the function returns
6461 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6465 cp_parser_array_notation (location_t loc
, cp_parser
*parser
, tree
*init_index
,
6468 cp_token
*token
= NULL
;
6469 tree length_index
, stride
= NULL_TREE
, value_tree
, array_type
;
6470 if (!array_value
|| array_value
== error_mark_node
)
6472 cp_parser_skip_to_end_of_statement (parser
);
6473 return error_mark_node
;
6476 array_type
= TREE_TYPE (array_value
);
6478 bool saved_colon_corrects
= parser
->colon_corrects_to_scope_p
;
6479 parser
->colon_corrects_to_scope_p
= false;
6480 token
= cp_lexer_peek_token (parser
->lexer
);
6484 cp_parser_error (parser
, "expected %<:%> or numeral");
6485 return error_mark_node
;
6487 else if (token
->type
== CPP_COLON
)
6489 /* Consume the ':'. */
6490 cp_lexer_consume_token (parser
->lexer
);
6492 /* If we are here, then we have a case like this A[:]. */
6493 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_CLOSE_SQUARE
)
6495 cp_parser_error (parser
, "expected %<]%>");
6496 cp_parser_skip_to_end_of_statement (parser
);
6497 return error_mark_node
;
6499 *init_index
= NULL_TREE
;
6501 length_index
= NULL_TREE
;
6505 /* If we are here, then there are three valid possibilities:
6507 2. ARRAY [ EXP : EXP ]
6508 3. ARRAY [ EXP : EXP : EXP ] */
6510 *init_index
= cp_parser_expression (parser
);
6511 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
6513 /* This indicates that we have a normal array expression. */
6514 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6518 /* Consume the ':'. */
6519 cp_lexer_consume_token (parser
->lexer
);
6520 length_index
= cp_parser_expression (parser
);
6521 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6523 cp_lexer_consume_token (parser
->lexer
);
6524 stride
= cp_parser_expression (parser
);
6527 parser
->colon_corrects_to_scope_p
= saved_colon_corrects
;
6529 if (*init_index
== error_mark_node
|| length_index
== error_mark_node
6530 || stride
== error_mark_node
|| array_type
== error_mark_node
)
6532 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_SQUARE
)
6533 cp_lexer_consume_token (parser
->lexer
);
6534 return error_mark_node
;
6536 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6538 value_tree
= build_array_notation_ref (loc
, array_value
, *init_index
,
6539 length_index
, stride
, array_type
);
6543 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6544 by cp_parser_builtin_offsetof. We're looking for
6546 postfix-expression [ expression ]
6547 postfix-expression [ braced-init-list ] (C++11)
6549 FOR_OFFSETOF is set if we're being called in that context, which
6550 changes how we deal with integer constant expressions. */
6553 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
6554 tree postfix_expression
,
6558 tree index
= NULL_TREE
;
6559 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
6560 bool saved_greater_than_is_operator_p
;
6562 /* Consume the `[' token. */
6563 cp_lexer_consume_token (parser
->lexer
);
6565 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
6566 parser
->greater_than_is_operator_p
= true;
6568 /* Parse the index expression. */
6569 /* ??? For offsetof, there is a question of what to allow here. If
6570 offsetof is not being used in an integral constant expression context,
6571 then we *could* get the right answer by computing the value at runtime.
6572 If we are in an integral constant expression context, then we might
6573 could accept any constant expression; hard to say without analysis.
6574 Rather than open the barn door too wide right away, allow only integer
6575 constant expressions here. */
6577 index
= cp_parser_constant_expression (parser
);
6580 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6582 bool expr_nonconst_p
;
6583 cp_lexer_set_source_position (parser
->lexer
);
6584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6585 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
6587 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
6589 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
6590 "braced list index is not allowed with array "
6592 cp_parser_skip_to_end_of_statement (parser
);
6593 return error_mark_node
;
6596 else if (flag_cilkplus
)
6598 /* Here are have these two options:
6599 ARRAY[EXP : EXP] - Array notation expr with default
6601 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6603 tree an_exp
= cp_parser_array_notation (loc
, parser
, &index
,
6604 postfix_expression
);
6609 index
= cp_parser_expression (parser
);
6612 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
6614 /* Look for the closing `]'. */
6615 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
6617 /* Build the ARRAY_REF. */
6618 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
6621 /* When not doing offsetof, array references are not permitted in
6622 constant-expressions. */
6624 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
6625 postfix_expression
= error_mark_node
;
6627 return postfix_expression
;
6630 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6631 by cp_parser_builtin_offsetof. We're looking for
6633 postfix-expression . template [opt] id-expression
6634 postfix-expression . pseudo-destructor-name
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression -> pseudo-destructor-name
6638 FOR_OFFSETOF is set if we're being called in that context. That sorta
6639 limits what of the above we'll actually accept, but nevermind.
6640 TOKEN_TYPE is the "." or "->" token, which will already have been
6641 removed from the stream. */
6644 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
6645 enum cpp_ttype token_type
,
6646 tree postfix_expression
,
6647 bool for_offsetof
, cp_id_kind
*idk
,
6648 location_t location
)
6652 bool pseudo_destructor_p
;
6653 tree scope
= NULL_TREE
;
6655 /* If this is a `->' operator, dereference the pointer. */
6656 if (token_type
== CPP_DEREF
)
6657 postfix_expression
= build_x_arrow (location
, postfix_expression
,
6658 tf_warning_or_error
);
6659 /* Check to see whether or not the expression is type-dependent. */
6660 dependent_p
= type_dependent_expression_p (postfix_expression
);
6661 /* The identifier following the `->' or `.' is not qualified. */
6662 parser
->scope
= NULL_TREE
;
6663 parser
->qualifying_scope
= NULL_TREE
;
6664 parser
->object_scope
= NULL_TREE
;
6665 *idk
= CP_ID_KIND_NONE
;
6667 /* Enter the scope corresponding to the type of the object
6668 given by the POSTFIX_EXPRESSION. */
6669 if (!dependent_p
&& TREE_TYPE (postfix_expression
) != NULL_TREE
)
6671 scope
= TREE_TYPE (postfix_expression
);
6672 /* According to the standard, no expression should ever have
6673 reference type. Unfortunately, we do not currently match
6674 the standard in this respect in that our internal representation
6675 of an expression may have reference type even when the standard
6676 says it does not. Therefore, we have to manually obtain the
6677 underlying type here. */
6678 scope
= non_reference (scope
);
6679 /* The type of the POSTFIX_EXPRESSION must be complete. */
6680 if (scope
== unknown_type_node
)
6682 error_at (location
, "%qE does not have class type",
6683 postfix_expression
);
6686 /* Unlike the object expression in other contexts, *this is not
6687 required to be of complete type for purposes of class member
6688 access (5.2.5) outside the member function body. */
6689 else if (postfix_expression
!= current_class_ref
6690 && !(processing_template_decl
&& scope
== current_class_type
))
6691 scope
= complete_type_or_else (scope
, NULL_TREE
);
6692 /* Let the name lookup machinery know that we are processing a
6693 class member access expression. */
6694 parser
->context
->object_type
= scope
;
6695 /* If something went wrong, we want to be able to discern that case,
6696 as opposed to the case where there was no SCOPE due to the type
6697 of expression being dependent. */
6699 scope
= error_mark_node
;
6700 /* If the SCOPE was erroneous, make the various semantic analysis
6701 functions exit quickly -- and without issuing additional error
6703 if (scope
== error_mark_node
)
6704 postfix_expression
= error_mark_node
;
6707 /* Assume this expression is not a pseudo-destructor access. */
6708 pseudo_destructor_p
= false;
6710 /* If the SCOPE is a scalar type, then, if this is a valid program,
6711 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6712 is type dependent, it can be pseudo-destructor-name or something else.
6713 Try to parse it as pseudo-destructor-name first. */
6714 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
6719 cp_parser_parse_tentatively (parser
);
6720 /* Parse the pseudo-destructor-name. */
6722 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
6725 && (cp_parser_error_occurred (parser
)
6726 || !SCALAR_TYPE_P (type
)))
6727 cp_parser_abort_tentative_parse (parser
);
6728 else if (cp_parser_parse_definitely (parser
))
6730 pseudo_destructor_p
= true;
6732 = finish_pseudo_destructor_expr (postfix_expression
,
6737 if (!pseudo_destructor_p
)
6739 /* If the SCOPE is not a scalar type, we are looking at an
6740 ordinary class member access expression, rather than a
6741 pseudo-destructor-name. */
6743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
6744 /* Parse the id-expression. */
6745 name
= (cp_parser_id_expression
6747 cp_parser_optional_template_keyword (parser
),
6748 /*check_dependency_p=*/true,
6750 /*declarator_p=*/false,
6751 /*optional_p=*/false));
6752 /* In general, build a SCOPE_REF if the member name is qualified.
6753 However, if the name was not dependent and has already been
6754 resolved; there is no need to build the SCOPE_REF. For example;
6756 struct X { void f(); };
6757 template <typename T> void f(T* t) { t->X::f(); }
6759 Even though "t" is dependent, "X::f" is not and has been resolved
6760 to a BASELINK; there is no need to include scope information. */
6762 /* But we do need to remember that there was an explicit scope for
6763 virtual function calls. */
6765 *idk
= CP_ID_KIND_QUALIFIED
;
6767 /* If the name is a template-id that names a type, we will get a
6768 TYPE_DECL here. That is invalid code. */
6769 if (TREE_CODE (name
) == TYPE_DECL
)
6771 error_at (token
->location
, "invalid use of %qD", name
);
6772 postfix_expression
= error_mark_node
;
6776 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
6778 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
6780 error_at (token
->location
, "%<%D::%D%> is not a class member",
6781 parser
->scope
, name
);
6782 postfix_expression
= error_mark_node
;
6785 name
= build_qualified_name (/*type=*/NULL_TREE
,
6789 parser
->scope
= NULL_TREE
;
6790 parser
->qualifying_scope
= NULL_TREE
;
6791 parser
->object_scope
= NULL_TREE
;
6793 if (parser
->scope
&& name
&& BASELINK_P (name
))
6794 adjust_result_of_qualified_name_lookup
6795 (name
, parser
->scope
, scope
);
6797 = finish_class_member_access_expr (postfix_expression
, name
,
6799 tf_warning_or_error
);
6803 /* We no longer need to look up names in the scope of the object on
6804 the left-hand side of the `.' or `->' operator. */
6805 parser
->context
->object_type
= NULL_TREE
;
6807 /* Outside of offsetof, these operators may not appear in
6808 constant-expressions. */
6810 && (cp_parser_non_integral_constant_expression
6811 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
6812 postfix_expression
= error_mark_node
;
6814 return postfix_expression
;
6817 /* Cache of LITERAL_ZERO_P constants. */
6819 static GTY(()) tree literal_zeros
[itk_none
];
6821 /* Parse a parenthesized expression-list.
6824 assignment-expression
6825 expression-list, assignment-expression
6830 identifier, expression-list
6832 CAST_P is true if this expression is the target of a cast.
6834 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6837 Returns a vector of trees. Each element is a representation of an
6838 assignment-expression. NULL is returned if the ( and or ) are
6839 missing. An empty, but allocated, vector is returned on no
6840 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6841 if we are parsing an attribute list for an attribute that wants a
6842 plain identifier argument, normal_attr for an attribute that wants
6843 an expression, or non_attr if we aren't parsing an attribute list. If
6844 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6845 not all of the expressions in the list were constant.
6846 WANT_LITERAL_ZERO_P is true if the caller is interested in
6847 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6848 immediately, this can be removed. */
6850 static vec
<tree
, va_gc
> *
6851 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
6852 int is_attribute_list
,
6854 bool allow_expansion_p
,
6855 bool *non_constant_p
,
6856 bool want_literal_zero_p
)
6858 vec
<tree
, va_gc
> *expression_list
;
6859 bool fold_expr_p
= is_attribute_list
!= non_attr
;
6860 tree identifier
= NULL_TREE
;
6861 bool saved_greater_than_is_operator_p
;
6863 /* Assume all the expressions will be constant. */
6865 *non_constant_p
= false;
6867 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
6870 expression_list
= make_tree_vector ();
6872 /* Within a parenthesized expression, a `>' token is always
6873 the greater-than operator. */
6874 saved_greater_than_is_operator_p
6875 = parser
->greater_than_is_operator_p
;
6876 parser
->greater_than_is_operator_p
= true;
6878 /* Consume expressions until there are no more. */
6879 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
6884 /* At the beginning of attribute lists, check to see if the
6885 next token is an identifier. */
6886 if (is_attribute_list
== id_attr
6887 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
6891 /* Consume the identifier. */
6892 token
= cp_lexer_consume_token (parser
->lexer
);
6893 /* Save the identifier. */
6894 identifier
= token
->u
.value
;
6898 bool expr_non_constant_p
;
6900 /* Parse the next assignment-expression. */
6901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
6903 /* A braced-init-list. */
6904 cp_lexer_set_source_position (parser
->lexer
);
6905 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
6906 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
6907 if (non_constant_p
&& expr_non_constant_p
)
6908 *non_constant_p
= true;
6910 else if (non_constant_p
)
6912 expr
= (cp_parser_constant_expression
6913 (parser
, /*allow_non_constant_p=*/true,
6914 &expr_non_constant_p
));
6915 if (expr_non_constant_p
)
6916 *non_constant_p
= true;
6921 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
6929 /* If a parameter is literal zero alone, remember it
6930 for -Wmemset-transposed-args warning. */
6931 if (integer_zerop (tok
->u
.value
)
6932 && !TREE_OVERFLOW (tok
->u
.value
)
6933 && want_literal_zero_p
6934 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6936 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6937 == CPP_CLOSE_PAREN
))
6940 for (i
= 0; i
< itk_none
; ++i
)
6941 if (TREE_TYPE (tok
->u
.value
) == integer_types
[i
])
6943 if (i
< itk_none
&& literal_zeros
[i
])
6944 expr
= literal_zeros
[i
];
6947 expr
= copy_node (tok
->u
.value
);
6948 LITERAL_ZERO_P (expr
) = 1;
6950 literal_zeros
[i
] = expr
;
6952 /* Consume the 0 token (or '\0', 0LL etc.). */
6953 cp_lexer_consume_token (parser
->lexer
);
6959 if (expr
== NULL_TREE
)
6960 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
6965 expr
= instantiate_non_dependent_expr (expr
);
6967 /* If we have an ellipsis, then this is an expression
6969 if (allow_expansion_p
6970 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
6972 /* Consume the `...'. */
6973 cp_lexer_consume_token (parser
->lexer
);
6975 /* Build the argument pack. */
6976 expr
= make_pack_expansion (expr
);
6979 /* Add it to the list. We add error_mark_node
6980 expressions to the list, so that we can still tell if
6981 the correct form for a parenthesized expression-list
6982 is found. That gives better errors. */
6983 vec_safe_push (expression_list
, expr
);
6985 if (expr
== error_mark_node
)
6989 /* After the first item, attribute lists look the same as
6990 expression lists. */
6991 is_attribute_list
= non_attr
;
6994 /* If the next token isn't a `,', then we are done. */
6995 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
6998 /* Otherwise, consume the `,' and keep going. */
6999 cp_lexer_consume_token (parser
->lexer
);
7002 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
7007 /* We try and resync to an unnested comma, as that will give the
7008 user better diagnostics. */
7009 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7010 /*recovering=*/true,
7012 /*consume_paren=*/true);
7017 parser
->greater_than_is_operator_p
7018 = saved_greater_than_is_operator_p
;
7023 parser
->greater_than_is_operator_p
7024 = saved_greater_than_is_operator_p
;
7027 vec_safe_insert (expression_list
, 0, identifier
);
7029 return expression_list
;
7032 /* Parse a pseudo-destructor-name.
7034 pseudo-destructor-name:
7035 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7036 :: [opt] nested-name-specifier template template-id :: ~ type-name
7037 :: [opt] nested-name-specifier [opt] ~ type-name
7039 If either of the first two productions is used, sets *SCOPE to the
7040 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7041 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7042 or ERROR_MARK_NODE if the parse fails. */
7045 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
7050 bool nested_name_specifier_p
;
7053 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
7054 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
7055 && !type_dependent_expression_p (object
))
7057 if (cxx_dialect
< cxx14
)
7058 pedwarn (input_location
, 0,
7059 "%<~auto%> only available with "
7060 "-std=c++14 or -std=gnu++14");
7061 cp_lexer_consume_token (parser
->lexer
);
7062 cp_lexer_consume_token (parser
->lexer
);
7064 *type
= TREE_TYPE (object
);
7068 /* Assume that things will not work out. */
7069 *type
= error_mark_node
;
7071 /* Look for the optional `::' operator. */
7072 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
7073 /* Look for the optional nested-name-specifier. */
7074 nested_name_specifier_p
7075 = (cp_parser_nested_name_specifier_opt (parser
,
7076 /*typename_keyword_p=*/false,
7077 /*check_dependency_p=*/true,
7079 /*is_declaration=*/false)
7081 /* Now, if we saw a nested-name-specifier, we might be doing the
7082 second production. */
7083 if (nested_name_specifier_p
7084 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
7086 /* Consume the `template' keyword. */
7087 cp_lexer_consume_token (parser
->lexer
);
7088 /* Parse the template-id. */
7089 cp_parser_template_id (parser
,
7090 /*template_keyword_p=*/true,
7091 /*check_dependency_p=*/false,
7093 /*is_declaration=*/true);
7094 /* Look for the `::' token. */
7095 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7097 /* If the next token is not a `~', then there might be some
7098 additional qualification. */
7099 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
7101 /* At this point, we're looking for "type-name :: ~". The type-name
7102 must not be a class-name, since this is a pseudo-destructor. So,
7103 it must be either an enum-name, or a typedef-name -- both of which
7104 are just identifiers. So, we peek ahead to check that the "::"
7105 and "~" tokens are present; if they are not, then we can avoid
7106 calling type_name. */
7107 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
7108 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
7109 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
7111 cp_parser_error (parser
, "non-scalar type");
7115 /* Look for the type-name. */
7116 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7117 if (*scope
== error_mark_node
)
7120 /* Look for the `::' token. */
7121 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
7126 /* Look for the `~'. */
7127 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
7129 /* Once we see the ~, this has to be a pseudo-destructor. */
7130 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
7131 cp_parser_commit_to_topmost_tentative_parse (parser
);
7133 /* Look for the type-name again. We are not responsible for
7134 checking that it matches the first type-name. */
7135 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
7138 /* Parse a unary-expression.
7144 unary-operator cast-expression
7145 sizeof unary-expression
7147 alignof ( type-id ) [C++0x]
7154 __extension__ cast-expression
7155 __alignof__ unary-expression
7156 __alignof__ ( type-id )
7157 alignof unary-expression [C++0x]
7158 __real__ cast-expression
7159 __imag__ cast-expression
7161 sizeof ( type-id ) { initializer-list , [opt] }
7162 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7163 __alignof__ ( type-id ) { initializer-list , [opt] }
7165 ADDRESS_P is true iff the unary-expression is appearing as the
7166 operand of the `&' operator. CAST_P is true if this expression is
7167 the target of a cast.
7169 Returns a representation of the expression. */
7172 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
7173 bool address_p
, bool cast_p
, bool decltype_p
)
7176 enum tree_code unary_operator
;
7178 /* Peek at the next token. */
7179 token
= cp_lexer_peek_token (parser
->lexer
);
7180 /* Some keywords give away the kind of expression. */
7181 if (token
->type
== CPP_KEYWORD
)
7183 enum rid keyword
= token
->keyword
;
7192 location_t first_loc
;
7194 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
7195 /* Consume the token. */
7196 cp_lexer_consume_token (parser
->lexer
);
7197 first_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7198 /* Parse the operand. */
7199 operand
= cp_parser_sizeof_operand (parser
, keyword
);
7201 if (TYPE_P (operand
))
7202 ret
= cxx_sizeof_or_alignof_type (operand
, op
, true);
7205 /* ISO C++ defines alignof only with types, not with
7206 expressions. So pedwarn if alignof is used with a non-
7207 type expression. However, __alignof__ is ok. */
7208 if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "alignof"))
7209 pedwarn (token
->location
, OPT_Wpedantic
,
7210 "ISO C++ does not allow %<alignof%> "
7213 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
7215 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7216 SIZEOF_EXPR with the original operand. */
7217 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
7219 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
7221 if (!processing_template_decl
&& TYPE_P (operand
))
7223 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
7224 build1 (NOP_EXPR
, operand
,
7226 SIZEOF_EXPR_TYPE_P (ret
) = 1;
7229 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
7230 TREE_SIDE_EFFECTS (ret
) = 0;
7231 TREE_READONLY (ret
) = 1;
7233 SET_EXPR_LOCATION (ret
, first_loc
);
7239 return cp_parser_new_expression (parser
);
7242 return cp_parser_delete_expression (parser
);
7246 /* The saved value of the PEDANTIC flag. */
7250 /* Save away the PEDANTIC flag. */
7251 cp_parser_extension_opt (parser
, &saved_pedantic
);
7252 /* Parse the cast-expression. */
7253 expr
= cp_parser_simple_cast_expression (parser
);
7254 /* Restore the PEDANTIC flag. */
7255 pedantic
= saved_pedantic
;
7265 /* Consume the `__real__' or `__imag__' token. */
7266 cp_lexer_consume_token (parser
->lexer
);
7267 /* Parse the cast-expression. */
7268 expression
= cp_parser_simple_cast_expression (parser
);
7269 /* Create the complete representation. */
7270 return build_x_unary_op (token
->location
,
7271 (keyword
== RID_REALPART
7272 ? REALPART_EXPR
: IMAGPART_EXPR
),
7274 tf_warning_or_error
);
7278 case RID_TRANSACTION_ATOMIC
:
7279 case RID_TRANSACTION_RELAXED
:
7280 return cp_parser_transaction_expression (parser
, keyword
);
7285 const char *saved_message
;
7286 bool saved_integral_constant_expression_p
;
7287 bool saved_non_integral_constant_expression_p
;
7288 bool saved_greater_than_is_operator_p
;
7290 cp_lexer_consume_token (parser
->lexer
);
7291 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
7293 saved_message
= parser
->type_definition_forbidden_message
;
7294 parser
->type_definition_forbidden_message
7295 = G_("types may not be defined in %<noexcept%> expressions");
7297 saved_integral_constant_expression_p
7298 = parser
->integral_constant_expression_p
;
7299 saved_non_integral_constant_expression_p
7300 = parser
->non_integral_constant_expression_p
;
7301 parser
->integral_constant_expression_p
= false;
7303 saved_greater_than_is_operator_p
7304 = parser
->greater_than_is_operator_p
;
7305 parser
->greater_than_is_operator_p
= true;
7307 ++cp_unevaluated_operand
;
7308 ++c_inhibit_evaluation_warnings
;
7309 ++cp_noexcept_operand
;
7310 expr
= cp_parser_expression (parser
);
7311 --cp_noexcept_operand
;
7312 --c_inhibit_evaluation_warnings
;
7313 --cp_unevaluated_operand
;
7315 parser
->greater_than_is_operator_p
7316 = saved_greater_than_is_operator_p
;
7318 parser
->integral_constant_expression_p
7319 = saved_integral_constant_expression_p
;
7320 parser
->non_integral_constant_expression_p
7321 = saved_non_integral_constant_expression_p
;
7323 parser
->type_definition_forbidden_message
= saved_message
;
7325 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7326 return finish_noexcept_expr (expr
, tf_warning_or_error
);
7334 /* Look for the `:: new' and `:: delete', which also signal the
7335 beginning of a new-expression, or delete-expression,
7336 respectively. If the next token is `::', then it might be one of
7338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
7342 /* See if the token after the `::' is one of the keywords in
7343 which we're interested. */
7344 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
7345 /* If it's `new', we have a new-expression. */
7346 if (keyword
== RID_NEW
)
7347 return cp_parser_new_expression (parser
);
7348 /* Similarly, for `delete'. */
7349 else if (keyword
== RID_DELETE
)
7350 return cp_parser_delete_expression (parser
);
7353 /* Look for a unary operator. */
7354 unary_operator
= cp_parser_unary_operator (token
);
7355 /* The `++' and `--' operators can be handled similarly, even though
7356 they are not technically unary-operators in the grammar. */
7357 if (unary_operator
== ERROR_MARK
)
7359 if (token
->type
== CPP_PLUS_PLUS
)
7360 unary_operator
= PREINCREMENT_EXPR
;
7361 else if (token
->type
== CPP_MINUS_MINUS
)
7362 unary_operator
= PREDECREMENT_EXPR
;
7363 /* Handle the GNU address-of-label extension. */
7364 else if (cp_parser_allow_gnu_extensions_p (parser
)
7365 && token
->type
== CPP_AND_AND
)
7369 location_t loc
= token
->location
;
7371 /* Consume the '&&' token. */
7372 cp_lexer_consume_token (parser
->lexer
);
7373 /* Look for the identifier. */
7374 identifier
= cp_parser_identifier (parser
);
7375 /* Create an expression representing the address. */
7376 expression
= finish_label_address_expr (identifier
, loc
);
7377 if (cp_parser_non_integral_constant_expression (parser
,
7379 expression
= error_mark_node
;
7383 if (unary_operator
!= ERROR_MARK
)
7385 tree cast_expression
;
7386 tree expression
= error_mark_node
;
7387 non_integral_constant non_constant_p
= NIC_NONE
;
7388 location_t loc
= token
->location
;
7389 tsubst_flags_t complain
= complain_flags (decltype_p
);
7391 /* Consume the operator token. */
7392 token
= cp_lexer_consume_token (parser
->lexer
);
7393 /* Parse the cast-expression. */
7395 = cp_parser_cast_expression (parser
,
7396 unary_operator
== ADDR_EXPR
,
7400 /* Now, build an appropriate representation. */
7401 switch (unary_operator
)
7404 non_constant_p
= NIC_STAR
;
7405 expression
= build_x_indirect_ref (loc
, cast_expression
,
7411 non_constant_p
= NIC_ADDR
;
7414 expression
= build_x_unary_op (loc
, unary_operator
,
7419 case PREINCREMENT_EXPR
:
7420 case PREDECREMENT_EXPR
:
7421 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
7422 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
7424 case UNARY_PLUS_EXPR
:
7426 case TRUTH_NOT_EXPR
:
7427 expression
= finish_unary_op_expr (loc
, unary_operator
,
7428 cast_expression
, complain
);
7435 if (non_constant_p
!= NIC_NONE
7436 && cp_parser_non_integral_constant_expression (parser
,
7438 expression
= error_mark_node
;
7443 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
7444 /*member_access_only_p=*/false,
7449 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7450 unary-operator, the corresponding tree code is returned. */
7452 static enum tree_code
7453 cp_parser_unary_operator (cp_token
* token
)
7455 switch (token
->type
)
7458 return INDIRECT_REF
;
7464 return UNARY_PLUS_EXPR
;
7470 return TRUTH_NOT_EXPR
;
7473 return BIT_NOT_EXPR
;
7480 /* Parse a new-expression.
7483 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7484 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7486 Returns a representation of the expression. */
7489 cp_parser_new_expression (cp_parser
* parser
)
7491 bool global_scope_p
;
7492 vec
<tree
, va_gc
> *placement
;
7494 vec
<tree
, va_gc
> *initializer
;
7495 tree nelts
= NULL_TREE
;
7498 /* Look for the optional `::' operator. */
7500 = (cp_parser_global_scope_opt (parser
,
7501 /*current_scope_valid_p=*/false)
7503 /* Look for the `new' operator. */
7504 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
7505 /* There's no easy way to tell a new-placement from the
7506 `( type-id )' construct. */
7507 cp_parser_parse_tentatively (parser
);
7508 /* Look for a new-placement. */
7509 placement
= cp_parser_new_placement (parser
);
7510 /* If that didn't work out, there's no new-placement. */
7511 if (!cp_parser_parse_definitely (parser
))
7513 if (placement
!= NULL
)
7514 release_tree_vector (placement
);
7518 /* If the next token is a `(', then we have a parenthesized
7520 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7523 const char *saved_message
= parser
->type_definition_forbidden_message
;
7525 /* Consume the `('. */
7526 cp_lexer_consume_token (parser
->lexer
);
7528 /* Parse the type-id. */
7529 parser
->type_definition_forbidden_message
7530 = G_("types may not be defined in a new-expression");
7531 type
= cp_parser_type_id (parser
);
7532 parser
->type_definition_forbidden_message
= saved_message
;
7534 /* Look for the closing `)'. */
7535 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
7536 token
= cp_lexer_peek_token (parser
->lexer
);
7537 /* There should not be a direct-new-declarator in this production,
7538 but GCC used to allowed this, so we check and emit a sensible error
7539 message for this case. */
7540 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7542 error_at (token
->location
,
7543 "array bound forbidden after parenthesized type-id");
7544 inform (token
->location
,
7545 "try removing the parentheses around the type-id");
7546 cp_parser_direct_new_declarator (parser
);
7549 /* Otherwise, there must be a new-type-id. */
7551 type
= cp_parser_new_type_id (parser
, &nelts
);
7553 /* If the next token is a `(' or '{', then we have a new-initializer. */
7554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
7555 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7556 initializer
= cp_parser_new_initializer (parser
);
7560 /* A new-expression may not appear in an integral constant
7562 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
7563 ret
= error_mark_node
;
7566 /* Create a representation of the new-expression. */
7567 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
7568 tf_warning_or_error
);
7571 if (placement
!= NULL
)
7572 release_tree_vector (placement
);
7573 if (initializer
!= NULL
)
7574 release_tree_vector (initializer
);
7579 /* Parse a new-placement.
7584 Returns the same representation as for an expression-list. */
7586 static vec
<tree
, va_gc
> *
7587 cp_parser_new_placement (cp_parser
* parser
)
7589 vec
<tree
, va_gc
> *expression_list
;
7591 /* Parse the expression-list. */
7592 expression_list
= (cp_parser_parenthesized_expression_list
7593 (parser
, non_attr
, /*cast_p=*/false,
7594 /*allow_expansion_p=*/true,
7595 /*non_constant_p=*/NULL
));
7597 return expression_list
;
7600 /* Parse a new-type-id.
7603 type-specifier-seq new-declarator [opt]
7605 Returns the TYPE allocated. If the new-type-id indicates an array
7606 type, *NELTS is set to the number of elements in the last array
7607 bound; the TYPE will not include the last array bound. */
7610 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
7612 cp_decl_specifier_seq type_specifier_seq
;
7613 cp_declarator
*new_declarator
;
7614 cp_declarator
*declarator
;
7615 cp_declarator
*outer_declarator
;
7616 const char *saved_message
;
7618 /* The type-specifier sequence must not contain type definitions.
7619 (It cannot contain declarations of new types either, but if they
7620 are not definitions we will catch that because they are not
7622 saved_message
= parser
->type_definition_forbidden_message
;
7623 parser
->type_definition_forbidden_message
7624 = G_("types may not be defined in a new-type-id");
7625 /* Parse the type-specifier-seq. */
7626 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
7627 /*is_trailing_return=*/false,
7628 &type_specifier_seq
);
7629 /* Restore the old message. */
7630 parser
->type_definition_forbidden_message
= saved_message
;
7632 if (type_specifier_seq
.type
== error_mark_node
)
7633 return error_mark_node
;
7635 /* Parse the new-declarator. */
7636 new_declarator
= cp_parser_new_declarator_opt (parser
);
7638 /* Determine the number of elements in the last array dimension, if
7641 /* Skip down to the last array dimension. */
7642 declarator
= new_declarator
;
7643 outer_declarator
= NULL
;
7644 while (declarator
&& (declarator
->kind
== cdk_pointer
7645 || declarator
->kind
== cdk_ptrmem
))
7647 outer_declarator
= declarator
;
7648 declarator
= declarator
->declarator
;
7651 && declarator
->kind
== cdk_array
7652 && declarator
->declarator
7653 && declarator
->declarator
->kind
== cdk_array
)
7655 outer_declarator
= declarator
;
7656 declarator
= declarator
->declarator
;
7659 if (declarator
&& declarator
->kind
== cdk_array
)
7661 *nelts
= declarator
->u
.array
.bounds
;
7662 if (*nelts
== error_mark_node
)
7663 *nelts
= integer_one_node
;
7665 if (outer_declarator
)
7666 outer_declarator
->declarator
= declarator
->declarator
;
7668 new_declarator
= NULL
;
7671 return groktypename (&type_specifier_seq
, new_declarator
, false);
7674 /* Parse an (optional) new-declarator.
7677 ptr-operator new-declarator [opt]
7678 direct-new-declarator
7680 Returns the declarator. */
7682 static cp_declarator
*
7683 cp_parser_new_declarator_opt (cp_parser
* parser
)
7685 enum tree_code code
;
7686 tree type
, std_attributes
= NULL_TREE
;
7687 cp_cv_quals cv_quals
;
7689 /* We don't know if there's a ptr-operator next, or not. */
7690 cp_parser_parse_tentatively (parser
);
7691 /* Look for a ptr-operator. */
7692 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
7693 /* If that worked, look for more new-declarators. */
7694 if (cp_parser_parse_definitely (parser
))
7696 cp_declarator
*declarator
;
7698 /* Parse another optional declarator. */
7699 declarator
= cp_parser_new_declarator_opt (parser
);
7701 declarator
= cp_parser_make_indirect_declarator
7702 (code
, type
, cv_quals
, declarator
, std_attributes
);
7707 /* If the next token is a `[', there is a direct-new-declarator. */
7708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7709 return cp_parser_direct_new_declarator (parser
);
7714 /* Parse a direct-new-declarator.
7716 direct-new-declarator:
7718 direct-new-declarator [constant-expression]
7722 static cp_declarator
*
7723 cp_parser_direct_new_declarator (cp_parser
* parser
)
7725 cp_declarator
*declarator
= NULL
;
7732 /* Look for the opening `['. */
7733 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
7735 token
= cp_lexer_peek_token (parser
->lexer
);
7736 expression
= cp_parser_expression (parser
);
7737 /* The standard requires that the expression have integral
7738 type. DR 74 adds enumeration types. We believe that the
7739 real intent is that these expressions be handled like the
7740 expression in a `switch' condition, which also allows
7741 classes with a single conversion to integral or
7742 enumeration type. */
7743 if (!processing_template_decl
)
7746 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
7751 error_at (token
->location
,
7752 "expression in new-declarator must have integral "
7753 "or enumeration type");
7754 expression
= error_mark_node
;
7758 /* Look for the closing `]'. */
7759 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7761 /* Add this bound to the declarator. */
7762 declarator
= make_array_declarator (declarator
, expression
);
7764 /* If the next token is not a `[', then there are no more
7766 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
7773 /* Parse a new-initializer.
7776 ( expression-list [opt] )
7779 Returns a representation of the expression-list. */
7781 static vec
<tree
, va_gc
> *
7782 cp_parser_new_initializer (cp_parser
* parser
)
7784 vec
<tree
, va_gc
> *expression_list
;
7786 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7789 bool expr_non_constant_p
;
7790 cp_lexer_set_source_position (parser
->lexer
);
7791 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7792 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7793 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
7794 expression_list
= make_tree_vector_single (t
);
7797 expression_list
= (cp_parser_parenthesized_expression_list
7798 (parser
, non_attr
, /*cast_p=*/false,
7799 /*allow_expansion_p=*/true,
7800 /*non_constant_p=*/NULL
));
7802 return expression_list
;
7805 /* Parse a delete-expression.
7808 :: [opt] delete cast-expression
7809 :: [opt] delete [ ] cast-expression
7811 Returns a representation of the expression. */
7814 cp_parser_delete_expression (cp_parser
* parser
)
7816 bool global_scope_p
;
7820 /* Look for the optional `::' operator. */
7822 = (cp_parser_global_scope_opt (parser
,
7823 /*current_scope_valid_p=*/false)
7825 /* Look for the `delete' keyword. */
7826 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
7827 /* See if the array syntax is in use. */
7828 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
7830 /* Consume the `[' token. */
7831 cp_lexer_consume_token (parser
->lexer
);
7832 /* Look for the `]' token. */
7833 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7834 /* Remember that this is the `[]' construct. */
7840 /* Parse the cast-expression. */
7841 expression
= cp_parser_simple_cast_expression (parser
);
7843 /* A delete-expression may not appear in an integral constant
7845 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
7846 return error_mark_node
;
7848 return delete_sanity (expression
, NULL_TREE
, array_p
, global_scope_p
,
7849 tf_warning_or_error
);
7852 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7853 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7857 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
7859 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7860 switch (token
->type
)
7866 case CPP_CLOSE_SQUARE
:
7867 case CPP_CLOSE_PAREN
:
7868 case CPP_CLOSE_BRACE
:
7869 case CPP_OPEN_BRACE
:
7873 case CPP_DEREF_STAR
:
7881 case CPP_GREATER_EQ
:
7902 case CPP_OPEN_PAREN
:
7903 /* In ((type ()) () the last () isn't a valid cast-expression,
7904 so the whole must be parsed as postfix-expression. */
7905 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
7908 case CPP_OPEN_SQUARE
:
7909 /* '[' may start a primary-expression in obj-c++ and in C++11,
7910 as a lambda-expression, eg, '(void)[]{}'. */
7911 if (cxx_dialect
>= cxx11
)
7913 return c_dialect_objc ();
7916 case CPP_MINUS_MINUS
:
7917 /* '++' and '--' may or may not start a cast-expression:
7919 struct T { void operator++(int); };
7920 void f() { (T())++; }
7933 /* Parse a cast-expression.
7937 ( type-id ) cast-expression
7939 ADDRESS_P is true iff the unary-expression is appearing as the
7940 operand of the `&' operator. CAST_P is true if this expression is
7941 the target of a cast.
7943 Returns a representation of the expression. */
7946 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7947 bool decltype_p
, cp_id_kind
* pidk
)
7949 /* If it's a `(', then we might be looking at a cast. */
7950 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7952 tree type
= NULL_TREE
;
7953 tree expr
= NULL_TREE
;
7954 int cast_expression
= 0;
7955 const char *saved_message
;
7957 /* There's no way to know yet whether or not this is a cast.
7958 For example, `(int (3))' is a unary-expression, while `(int)
7959 3' is a cast. So, we resort to parsing tentatively. */
7960 cp_parser_parse_tentatively (parser
);
7961 /* Types may not be defined in a cast. */
7962 saved_message
= parser
->type_definition_forbidden_message
;
7963 parser
->type_definition_forbidden_message
7964 = G_("types may not be defined in casts");
7965 /* Consume the `('. */
7966 cp_lexer_consume_token (parser
->lexer
);
7967 /* A very tricky bit is that `(struct S) { 3 }' is a
7968 compound-literal (which we permit in C++ as an extension).
7969 But, that construct is not a cast-expression -- it is a
7970 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7971 is legal; if the compound-literal were a cast-expression,
7972 you'd need an extra set of parentheses.) But, if we parse
7973 the type-id, and it happens to be a class-specifier, then we
7974 will commit to the parse at that point, because we cannot
7975 undo the action that is done when creating a new class. So,
7976 then we cannot back up and do a postfix-expression.
7978 Another tricky case is the following (c++/29234):
7980 struct S { void operator () (); };
7987 As a type-id we parse the parenthesized S()() as a function
7988 returning a function, groktypename complains and we cannot
7989 back up in this case either.
7991 Therefore, we scan ahead to the closing `)', and check to see
7992 if the tokens after the `)' can start a cast-expression. Otherwise
7993 we are dealing with an unary-expression, a postfix-expression
7996 Yet another tricky case, in C++11, is the following (c++/54891):
8000 The issue is that usually, besides the case of lambda-expressions,
8001 the parenthesized type-id cannot be followed by '[', and, eg, we
8002 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8003 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8004 we don't commit, we try a cast-expression, then an unary-expression.
8006 Save tokens so that we can put them back. */
8007 cp_lexer_save_tokens (parser
->lexer
);
8009 /* We may be looking at a cast-expression. */
8010 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8011 /*consume_paren=*/true))
8013 = cp_parser_tokens_start_cast_expression (parser
);
8015 /* Roll back the tokens we skipped. */
8016 cp_lexer_rollback_tokens (parser
->lexer
);
8017 /* If we aren't looking at a cast-expression, simulate an error so
8018 that the call to cp_parser_error_occurred below returns true. */
8019 if (!cast_expression
)
8020 cp_parser_simulate_error (parser
);
8023 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8024 parser
->in_type_id_in_expr_p
= true;
8025 /* Look for the type-id. */
8026 type
= cp_parser_type_id (parser
);
8027 /* Look for the closing `)'. */
8028 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8029 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8032 /* Restore the saved message. */
8033 parser
->type_definition_forbidden_message
= saved_message
;
8035 /* At this point this can only be either a cast or a
8036 parenthesized ctor such as `(T ())' that looks like a cast to
8037 function returning T. */
8038 if (!cp_parser_error_occurred (parser
))
8040 /* Only commit if the cast-expression doesn't start with
8041 '++', '--', or '[' in C++11. */
8042 if (cast_expression
> 0)
8043 cp_parser_commit_to_topmost_tentative_parse (parser
);
8045 expr
= cp_parser_cast_expression (parser
,
8046 /*address_p=*/false,
8048 /*decltype_p=*/false,
8051 if (cp_parser_parse_definitely (parser
))
8053 /* Warn about old-style casts, if so requested. */
8054 if (warn_old_style_cast
8055 && !in_system_header_at (input_location
)
8056 && !VOID_TYPE_P (type
)
8057 && current_lang_name
!= lang_name_c
)
8058 warning (OPT_Wold_style_cast
, "use of old-style cast");
8060 /* Only type conversions to integral or enumeration types
8061 can be used in constant-expressions. */
8062 if (!cast_valid_in_integral_constant_expression_p (type
)
8063 && cp_parser_non_integral_constant_expression (parser
,
8065 return error_mark_node
;
8067 /* Perform the cast. */
8068 expr
= build_c_cast (input_location
, type
, expr
);
8073 cp_parser_abort_tentative_parse (parser
);
8076 /* If we get here, then it's not a cast, so it must be a
8077 unary-expression. */
8078 return cp_parser_unary_expression (parser
, pidk
, address_p
,
8079 cast_p
, decltype_p
);
8082 /* Parse a binary expression of the general form:
8086 pm-expression .* cast-expression
8087 pm-expression ->* cast-expression
8089 multiplicative-expression:
8091 multiplicative-expression * pm-expression
8092 multiplicative-expression / pm-expression
8093 multiplicative-expression % pm-expression
8095 additive-expression:
8096 multiplicative-expression
8097 additive-expression + multiplicative-expression
8098 additive-expression - multiplicative-expression
8102 shift-expression << additive-expression
8103 shift-expression >> additive-expression
8105 relational-expression:
8107 relational-expression < shift-expression
8108 relational-expression > shift-expression
8109 relational-expression <= shift-expression
8110 relational-expression >= shift-expression
8114 relational-expression:
8115 relational-expression <? shift-expression
8116 relational-expression >? shift-expression
8118 equality-expression:
8119 relational-expression
8120 equality-expression == relational-expression
8121 equality-expression != relational-expression
8125 and-expression & equality-expression
8127 exclusive-or-expression:
8129 exclusive-or-expression ^ and-expression
8131 inclusive-or-expression:
8132 exclusive-or-expression
8133 inclusive-or-expression | exclusive-or-expression
8135 logical-and-expression:
8136 inclusive-or-expression
8137 logical-and-expression && inclusive-or-expression
8139 logical-or-expression:
8140 logical-and-expression
8141 logical-or-expression || logical-and-expression
8143 All these are implemented with a single function like:
8146 simple-cast-expression
8147 binary-expression <token> binary-expression
8149 CAST_P is true if this expression is the target of a cast.
8151 The binops_by_token map is used to get the tree codes for each <token> type.
8152 binary-expressions are associated according to a precedence table. */
8154 #define TOKEN_PRECEDENCE(token) \
8155 (((token->type == CPP_GREATER \
8156 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8157 && !parser->greater_than_is_operator_p) \
8158 ? PREC_NOT_OPERATOR \
8159 : binops_by_token[token->type].prec)
8162 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8163 bool no_toplevel_fold_p
,
8165 enum cp_parser_prec prec
,
8168 cp_parser_expression_stack stack
;
8169 cp_parser_expression_stack_entry
*sp
= &stack
[0];
8170 cp_parser_expression_stack_entry current
;
8173 enum tree_code rhs_type
;
8174 enum cp_parser_prec new_prec
, lookahead_prec
;
8177 /* Parse the first expression. */
8178 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8179 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8180 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
8181 cast_p
, decltype_p
, pidk
);
8182 current
.prec
= prec
;
8184 if (cp_parser_error_occurred (parser
))
8185 return error_mark_node
;
8189 /* Get an operator token. */
8190 token
= cp_lexer_peek_token (parser
->lexer
);
8192 if (warn_cxx0x_compat
8193 && token
->type
== CPP_RSHIFT
8194 && !parser
->greater_than_is_operator_p
)
8196 if (warning_at (token
->location
, OPT_Wc__0x_compat
,
8197 "%<>>%> operator is treated"
8198 " as two right angle brackets in C++11"))
8199 inform (token
->location
,
8200 "suggest parentheses around %<>>%> expression");
8203 new_prec
= TOKEN_PRECEDENCE (token
);
8205 /* Popping an entry off the stack means we completed a subexpression:
8206 - either we found a token which is not an operator (`>' where it is not
8207 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8208 will happen repeatedly;
8209 - or, we found an operator which has lower priority. This is the case
8210 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8212 if (new_prec
<= current
.prec
)
8221 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
8222 current
.loc
= token
->location
;
8224 /* We used the operator token. */
8225 cp_lexer_consume_token (parser
->lexer
);
8227 /* For "false && x" or "true || x", x will never be executed;
8228 disable warnings while evaluating it. */
8229 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8230 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_false_node
;
8231 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8232 c_inhibit_evaluation_warnings
+= current
.lhs
== truthvalue_true_node
;
8234 /* Extract another operand. It may be the RHS of this expression
8235 or the LHS of a new, higher priority expression. */
8236 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
8237 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
8238 rhs
= cp_parser_simple_cast_expression (parser
);
8240 /* Get another operator token. Look up its precedence to avoid
8241 building a useless (immediately popped) stack entry for common
8242 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8243 token
= cp_lexer_peek_token (parser
->lexer
);
8244 lookahead_prec
= TOKEN_PRECEDENCE (token
);
8245 if (lookahead_prec
> new_prec
)
8247 /* ... and prepare to parse the RHS of the new, higher priority
8248 expression. Since precedence levels on the stack are
8249 monotonically increasing, we do not have to care about
8254 current
.lhs_type
= rhs_type
;
8255 current
.prec
= new_prec
;
8256 new_prec
= lookahead_prec
;
8260 lookahead_prec
= new_prec
;
8261 /* If the stack is not empty, we have parsed into LHS the right side
8262 (`4' in the example above) of an expression we had suspended.
8263 We can use the information on the stack to recover the LHS (`3')
8264 from the stack together with the tree code (`MULT_EXPR'), and
8265 the precedence of the higher level subexpression
8266 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8267 which will be used to actually build the additive expression. */
8269 rhs_type
= current
.lhs_type
;
8274 /* Undo the disabling of warnings done above. */
8275 if (current
.tree_type
== TRUTH_ANDIF_EXPR
)
8276 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_false_node
;
8277 else if (current
.tree_type
== TRUTH_ORIF_EXPR
)
8278 c_inhibit_evaluation_warnings
-= current
.lhs
== truthvalue_true_node
;
8280 if (warn_logical_not_paren
8281 && current
.lhs_type
== TRUTH_NOT_EXPR
)
8282 warn_logical_not_parentheses (current
.loc
, current
.tree_type
, rhs
);
8285 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8286 ERROR_MARK for everything that is not a binary expression.
8287 This makes warn_about_parentheses miss some warnings that
8288 involve unary operators. For unary expressions we should
8289 pass the correct tree_code unless the unary expression was
8290 surrounded by parentheses.
8292 if (no_toplevel_fold_p
8293 && lookahead_prec
<= current
.prec
8295 current
.lhs
= build2 (current
.tree_type
,
8296 TREE_CODE_CLASS (current
.tree_type
)
8298 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
8301 current
.lhs
= build_x_binary_op (current
.loc
, current
.tree_type
,
8302 current
.lhs
, current
.lhs_type
,
8303 rhs
, rhs_type
, &overload
,
8304 complain_flags (decltype_p
));
8305 current
.lhs_type
= current
.tree_type
;
8306 if (EXPR_P (current
.lhs
))
8307 SET_EXPR_LOCATION (current
.lhs
, current
.loc
);
8309 /* If the binary operator required the use of an overloaded operator,
8310 then this expression cannot be an integral constant-expression.
8311 An overloaded operator can be used even if both operands are
8312 otherwise permissible in an integral constant-expression if at
8313 least one of the operands is of enumeration type. */
8316 && cp_parser_non_integral_constant_expression (parser
,
8318 return error_mark_node
;
8325 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
8326 bool no_toplevel_fold_p
,
8327 enum cp_parser_prec prec
,
8330 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
8331 /*decltype*/false, prec
, pidk
);
8334 /* Parse the `? expression : assignment-expression' part of a
8335 conditional-expression. The LOGICAL_OR_EXPR is the
8336 logical-or-expression that started the conditional-expression.
8337 Returns a representation of the entire conditional-expression.
8339 This routine is used by cp_parser_assignment_expression.
8341 ? expression : assignment-expression
8345 ? : assignment-expression */
8348 cp_parser_question_colon_clause (cp_parser
* parser
, tree logical_or_expr
)
8351 tree assignment_expr
;
8352 struct cp_token
*token
;
8353 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8355 /* Consume the `?' token. */
8356 cp_lexer_consume_token (parser
->lexer
);
8357 token
= cp_lexer_peek_token (parser
->lexer
);
8358 if (cp_parser_allow_gnu_extensions_p (parser
)
8359 && token
->type
== CPP_COLON
)
8361 pedwarn (token
->location
, OPT_Wpedantic
,
8362 "ISO C++ does not allow ?: with omitted middle operand");
8363 /* Implicit true clause. */
8365 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_true_node
;
8366 warn_for_omitted_condop (token
->location
, logical_or_expr
);
8370 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
8371 parser
->colon_corrects_to_scope_p
= false;
8372 /* Parse the expression. */
8373 c_inhibit_evaluation_warnings
+= logical_or_expr
== truthvalue_false_node
;
8374 expr
= cp_parser_expression (parser
);
8375 c_inhibit_evaluation_warnings
+=
8376 ((logical_or_expr
== truthvalue_true_node
)
8377 - (logical_or_expr
== truthvalue_false_node
));
8378 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
8381 /* The next token should be a `:'. */
8382 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
8383 /* Parse the assignment-expression. */
8384 assignment_expr
= cp_parser_assignment_expression (parser
);
8385 c_inhibit_evaluation_warnings
-= logical_or_expr
== truthvalue_true_node
;
8387 /* Build the conditional-expression. */
8388 return build_x_conditional_expr (loc
, logical_or_expr
,
8391 tf_warning_or_error
);
8394 /* Parse an assignment-expression.
8396 assignment-expression:
8397 conditional-expression
8398 logical-or-expression assignment-operator assignment_expression
8401 CAST_P is true if this expression is the target of a cast.
8402 DECLTYPE_P is true if this expression is the operand of decltype.
8404 Returns a representation for the expression. */
8407 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8408 bool cast_p
, bool decltype_p
)
8412 /* If the next token is the `throw' keyword, then we're looking at
8413 a throw-expression. */
8414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
8415 expr
= cp_parser_throw_expression (parser
);
8416 /* Otherwise, it must be that we are looking at a
8417 logical-or-expression. */
8420 /* Parse the binary expressions (logical-or-expression). */
8421 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
8423 PREC_NOT_OPERATOR
, pidk
);
8424 /* If the next token is a `?' then we're actually looking at a
8425 conditional-expression. */
8426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
8427 return cp_parser_question_colon_clause (parser
, expr
);
8430 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8432 /* If it's an assignment-operator, we're using the second
8434 enum tree_code assignment_operator
8435 = cp_parser_assignment_operator_opt (parser
);
8436 if (assignment_operator
!= ERROR_MARK
)
8438 bool non_constant_p
;
8439 location_t saved_input_location
;
8441 /* Parse the right-hand side of the assignment. */
8442 tree rhs
= cp_parser_initializer_clause (parser
, &non_constant_p
);
8444 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
8445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8447 /* An assignment may not appear in a
8448 constant-expression. */
8449 if (cp_parser_non_integral_constant_expression (parser
,
8451 return error_mark_node
;
8452 /* Build the assignment expression. Its default
8453 location is the location of the '=' token. */
8454 saved_input_location
= input_location
;
8455 input_location
= loc
;
8456 expr
= build_x_modify_expr (loc
, expr
,
8457 assignment_operator
,
8459 complain_flags (decltype_p
));
8460 input_location
= saved_input_location
;
8468 /* Parse an (optional) assignment-operator.
8470 assignment-operator: one of
8471 = *= /= %= += -= >>= <<= &= ^= |=
8475 assignment-operator: one of
8478 If the next token is an assignment operator, the corresponding tree
8479 code is returned, and the token is consumed. For example, for
8480 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8481 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8482 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8483 operator, ERROR_MARK is returned. */
8485 static enum tree_code
8486 cp_parser_assignment_operator_opt (cp_parser
* parser
)
8491 /* Peek at the next token. */
8492 token
= cp_lexer_peek_token (parser
->lexer
);
8494 switch (token
->type
)
8505 op
= TRUNC_DIV_EXPR
;
8509 op
= TRUNC_MOD_EXPR
;
8541 /* Nothing else is an assignment operator. */
8545 /* If it was an assignment operator, consume it. */
8546 if (op
!= ERROR_MARK
)
8547 cp_lexer_consume_token (parser
->lexer
);
8552 /* Parse an expression.
8555 assignment-expression
8556 expression , assignment-expression
8558 CAST_P is true if this expression is the target of a cast.
8559 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8560 except possibly parenthesized or on the RHS of a comma (N3276).
8562 Returns a representation of the expression. */
8565 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
8566 bool cast_p
, bool decltype_p
)
8568 tree expression
= NULL_TREE
;
8569 location_t loc
= UNKNOWN_LOCATION
;
8573 tree assignment_expression
;
8575 /* Parse the next assignment-expression. */
8576 assignment_expression
8577 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
8579 /* We don't create a temporary for a call that is the immediate operand
8580 of decltype or on the RHS of a comma. But when we see a comma, we
8581 need to create a temporary for a call on the LHS. */
8582 if (decltype_p
&& !processing_template_decl
8583 && TREE_CODE (assignment_expression
) == CALL_EXPR
8584 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
8585 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8586 assignment_expression
8587 = build_cplus_new (TREE_TYPE (assignment_expression
),
8588 assignment_expression
, tf_warning_or_error
);
8590 /* If this is the first assignment-expression, we can just
8593 expression
= assignment_expression
;
8595 expression
= build_x_compound_expr (loc
, expression
,
8596 assignment_expression
,
8597 complain_flags (decltype_p
));
8598 /* If the next token is not a comma, then we are done with the
8600 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8602 /* Consume the `,'. */
8603 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8604 cp_lexer_consume_token (parser
->lexer
);
8605 /* A comma operator cannot appear in a constant-expression. */
8606 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
8607 expression
= error_mark_node
;
8613 /* Parse a constant-expression.
8615 constant-expression:
8616 conditional-expression
8618 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8619 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8620 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8621 is false, NON_CONSTANT_P should be NULL. */
8624 cp_parser_constant_expression (cp_parser
* parser
,
8625 bool allow_non_constant_p
,
8626 bool *non_constant_p
)
8628 bool saved_integral_constant_expression_p
;
8629 bool saved_allow_non_integral_constant_expression_p
;
8630 bool saved_non_integral_constant_expression_p
;
8633 /* It might seem that we could simply parse the
8634 conditional-expression, and then check to see if it were
8635 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8636 one that the compiler can figure out is constant, possibly after
8637 doing some simplifications or optimizations. The standard has a
8638 precise definition of constant-expression, and we must honor
8639 that, even though it is somewhat more restrictive.
8645 is not a legal declaration, because `(2, 3)' is not a
8646 constant-expression. The `,' operator is forbidden in a
8647 constant-expression. However, GCC's constant-folding machinery
8648 will fold this operation to an INTEGER_CST for `3'. */
8650 /* Save the old settings. */
8651 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
8652 saved_allow_non_integral_constant_expression_p
8653 = parser
->allow_non_integral_constant_expression_p
;
8654 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
8655 /* We are now parsing a constant-expression. */
8656 parser
->integral_constant_expression_p
= true;
8657 parser
->allow_non_integral_constant_expression_p
8658 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
8659 parser
->non_integral_constant_expression_p
= false;
8660 /* Although the grammar says "conditional-expression", we parse an
8661 "assignment-expression", which also permits "throw-expression"
8662 and the use of assignment operators. In the case that
8663 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8664 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8665 actually essential that we look for an assignment-expression.
8666 For example, cp_parser_initializer_clauses uses this function to
8667 determine whether a particular assignment-expression is in fact
8669 expression
= cp_parser_assignment_expression (parser
);
8670 /* Restore the old settings. */
8671 parser
->integral_constant_expression_p
8672 = saved_integral_constant_expression_p
;
8673 parser
->allow_non_integral_constant_expression_p
8674 = saved_allow_non_integral_constant_expression_p
;
8675 if (cxx_dialect
>= cxx11
)
8677 /* Require an rvalue constant expression here; that's what our
8678 callers expect. Reference constant expressions are handled
8679 separately in e.g. cp_parser_template_argument. */
8680 bool is_const
= potential_rvalue_constant_expression (expression
);
8681 parser
->non_integral_constant_expression_p
= !is_const
;
8682 if (!is_const
&& !allow_non_constant_p
)
8683 require_potential_rvalue_constant_expression (expression
);
8685 if (allow_non_constant_p
)
8686 *non_constant_p
= parser
->non_integral_constant_expression_p
;
8687 parser
->non_integral_constant_expression_p
8688 = saved_non_integral_constant_expression_p
;
8693 /* Parse __builtin_offsetof.
8695 offsetof-expression:
8696 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8698 offsetof-member-designator:
8700 | offsetof-member-designator "." id-expression
8701 | offsetof-member-designator "[" expression "]"
8702 | offsetof-member-designator "->" id-expression */
8705 cp_parser_builtin_offsetof (cp_parser
*parser
)
8707 int save_ice_p
, save_non_ice_p
;
8712 /* We're about to accept non-integral-constant things, but will
8713 definitely yield an integral constant expression. Save and
8714 restore these values around our local parsing. */
8715 save_ice_p
= parser
->integral_constant_expression_p
;
8716 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
8718 /* Consume the "__builtin_offsetof" token. */
8719 cp_lexer_consume_token (parser
->lexer
);
8720 /* Consume the opening `('. */
8721 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8722 /* Parse the type-id. */
8723 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8724 type
= cp_parser_type_id (parser
);
8725 /* Look for the `,'. */
8726 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8727 token
= cp_lexer_peek_token (parser
->lexer
);
8729 /* Build the (type *)null that begins the traditional offsetof macro. */
8730 expr
= build_static_cast (build_pointer_type (type
), null_pointer_node
,
8731 tf_warning_or_error
);
8733 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8734 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, expr
,
8735 true, &dummy
, token
->location
);
8738 token
= cp_lexer_peek_token (parser
->lexer
);
8739 switch (token
->type
)
8741 case CPP_OPEN_SQUARE
:
8742 /* offsetof-member-designator "[" expression "]" */
8743 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
8748 /* offsetof-member-designator "->" identifier */
8749 expr
= grok_array_decl (token
->location
, expr
,
8750 integer_zero_node
, false);
8754 /* offsetof-member-designator "." identifier */
8755 cp_lexer_consume_token (parser
->lexer
);
8756 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
8761 case CPP_CLOSE_PAREN
:
8762 /* Consume the ")" token. */
8763 cp_lexer_consume_token (parser
->lexer
);
8767 /* Error. We know the following require will fail, but
8768 that gives the proper error message. */
8769 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8770 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8771 expr
= error_mark_node
;
8777 expr
= finish_offsetof (expr
, loc
);
8780 parser
->integral_constant_expression_p
= save_ice_p
;
8781 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
8786 /* Parse a trait expression.
8788 Returns a representation of the expression, the underlying type
8789 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8792 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
8795 tree type1
, type2
= NULL_TREE
;
8796 bool binary
= false;
8797 bool variadic
= false;
8801 case RID_HAS_NOTHROW_ASSIGN
:
8802 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
8804 case RID_HAS_NOTHROW_CONSTRUCTOR
:
8805 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
8807 case RID_HAS_NOTHROW_COPY
:
8808 kind
= CPTK_HAS_NOTHROW_COPY
;
8810 case RID_HAS_TRIVIAL_ASSIGN
:
8811 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
8813 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
8814 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
8816 case RID_HAS_TRIVIAL_COPY
:
8817 kind
= CPTK_HAS_TRIVIAL_COPY
;
8819 case RID_HAS_TRIVIAL_DESTRUCTOR
:
8820 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
8822 case RID_HAS_VIRTUAL_DESTRUCTOR
:
8823 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
8825 case RID_IS_ABSTRACT
:
8826 kind
= CPTK_IS_ABSTRACT
;
8828 case RID_IS_BASE_OF
:
8829 kind
= CPTK_IS_BASE_OF
;
8833 kind
= CPTK_IS_CLASS
;
8836 kind
= CPTK_IS_EMPTY
;
8839 kind
= CPTK_IS_ENUM
;
8842 kind
= CPTK_IS_FINAL
;
8844 case RID_IS_LITERAL_TYPE
:
8845 kind
= CPTK_IS_LITERAL_TYPE
;
8850 case RID_IS_POLYMORPHIC
:
8851 kind
= CPTK_IS_POLYMORPHIC
;
8853 case RID_IS_STD_LAYOUT
:
8854 kind
= CPTK_IS_STD_LAYOUT
;
8856 case RID_IS_TRIVIAL
:
8857 kind
= CPTK_IS_TRIVIAL
;
8859 case RID_IS_TRIVIALLY_ASSIGNABLE
:
8860 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
8863 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
8864 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
8867 case RID_IS_TRIVIALLY_COPYABLE
:
8868 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
8871 kind
= CPTK_IS_UNION
;
8873 case RID_UNDERLYING_TYPE
:
8874 kind
= CPTK_UNDERLYING_TYPE
;
8879 case RID_DIRECT_BASES
:
8880 kind
= CPTK_DIRECT_BASES
;
8886 /* Consume the token. */
8887 cp_lexer_consume_token (parser
->lexer
);
8889 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
8891 type1
= cp_parser_type_id (parser
);
8893 if (type1
== error_mark_node
)
8894 return error_mark_node
;
8898 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
8900 type2
= cp_parser_type_id (parser
);
8902 if (type2
== error_mark_node
)
8903 return error_mark_node
;
8907 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
8909 cp_lexer_consume_token (parser
->lexer
);
8910 tree elt
= cp_parser_type_id (parser
);
8911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8913 cp_lexer_consume_token (parser
->lexer
);
8914 elt
= make_pack_expansion (elt
);
8916 if (elt
== error_mark_node
)
8917 return error_mark_node
;
8918 type2
= tree_cons (NULL_TREE
, elt
, type2
);
8922 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
8924 /* Complete the trait expression, which may mean either processing
8925 the trait expr now or saving it for template instantiation. */
8928 case CPTK_UNDERLYING_TYPE
:
8929 return finish_underlying_type (type1
);
8931 return finish_bases (type1
, false);
8932 case CPTK_DIRECT_BASES
:
8933 return finish_bases (type1
, true);
8935 return finish_trait_expr (kind
, type1
, type2
);
8939 /* Lambdas that appear in variable initializer or default argument scope
8940 get that in their mangling, so we need to record it. We might as well
8941 use the count for function and namespace scopes as well. */
8942 static GTY(()) tree lambda_scope
;
8943 static GTY(()) int lambda_count
;
8944 typedef struct GTY(()) tree_int
8949 static GTY(()) vec
<tree_int
, va_gc
> *lambda_scope_stack
;
8952 start_lambda_scope (tree decl
)
8956 /* Once we're inside a function, we ignore other scopes and just push
8957 the function again so that popping works properly. */
8958 if (current_function_decl
&& TREE_CODE (decl
) != FUNCTION_DECL
)
8959 decl
= current_function_decl
;
8960 ti
.t
= lambda_scope
;
8961 ti
.i
= lambda_count
;
8962 vec_safe_push (lambda_scope_stack
, ti
);
8963 if (lambda_scope
!= decl
)
8965 /* Don't reset the count if we're still in the same function. */
8966 lambda_scope
= decl
;
8972 record_lambda_scope (tree lambda
)
8974 LAMBDA_EXPR_EXTRA_SCOPE (lambda
) = lambda_scope
;
8975 LAMBDA_EXPR_DISCRIMINATOR (lambda
) = lambda_count
++;
8979 finish_lambda_scope (void)
8981 tree_int
*p
= &lambda_scope_stack
->last ();
8982 if (lambda_scope
!= p
->t
)
8984 lambda_scope
= p
->t
;
8985 lambda_count
= p
->i
;
8987 lambda_scope_stack
->pop ();
8990 /* Parse a lambda expression.
8993 lambda-introducer lambda-declarator [opt] compound-statement
8995 Returns a representation of the expression. */
8998 cp_parser_lambda_expression (cp_parser
* parser
)
9000 tree lambda_expr
= build_lambda_expr ();
9003 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9004 cp_token_position start
= 0;
9006 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
9008 if (cp_unevaluated_operand
)
9010 if (!token
->error_reported
)
9012 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
9013 "lambda-expression in unevaluated context");
9014 token
->error_reported
= true;
9018 else if (parser
->in_template_argument_list_p
)
9020 if (!token
->error_reported
)
9022 error_at (token
->location
, "lambda-expression in template-argument");
9023 token
->error_reported
= true;
9028 /* We may be in the middle of deferred access check. Disable
9030 push_deferring_access_checks (dk_no_deferred
);
9032 cp_parser_lambda_introducer (parser
, lambda_expr
);
9034 type
= begin_lambda_type (lambda_expr
);
9035 if (type
== error_mark_node
)
9036 return error_mark_node
;
9038 record_lambda_scope (lambda_expr
);
9040 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9041 determine_visibility (TYPE_NAME (type
));
9043 /* Now that we've started the type, add the capture fields for any
9044 explicit captures. */
9045 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9048 /* Inside the class, surrounding template-parameter-lists do not apply. */
9049 unsigned int saved_num_template_parameter_lists
9050 = parser
->num_template_parameter_lists
;
9051 unsigned char in_statement
= parser
->in_statement
;
9052 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
9053 bool fully_implicit_function_template_p
9054 = parser
->fully_implicit_function_template_p
;
9055 tree implicit_template_parms
= parser
->implicit_template_parms
;
9056 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
9057 bool auto_is_implicit_function_template_parm_p
9058 = parser
->auto_is_implicit_function_template_parm_p
;
9060 parser
->num_template_parameter_lists
= 0;
9061 parser
->in_statement
= 0;
9062 parser
->in_switch_statement_p
= false;
9063 parser
->fully_implicit_function_template_p
= false;
9064 parser
->implicit_template_parms
= 0;
9065 parser
->implicit_template_scope
= 0;
9066 parser
->auto_is_implicit_function_template_parm_p
= false;
9068 /* By virtue of defining a local class, a lambda expression has access to
9069 the private variables of enclosing classes. */
9071 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
9075 if (!cp_parser_error_occurred (parser
)
9076 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
9077 && cp_parser_start_tentative_firewall (parser
))
9079 cp_parser_lambda_body (parser
, lambda_expr
);
9081 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9083 if (cp_parser_skip_to_closing_brace (parser
))
9084 cp_lexer_consume_token (parser
->lexer
);
9087 /* The capture list was built up in reverse order; fix that now. */
9088 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
9089 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
9092 maybe_add_lambda_conv_op (type
);
9094 type
= finish_struct (type
, /*attributes=*/NULL_TREE
);
9096 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
9097 parser
->in_statement
= in_statement
;
9098 parser
->in_switch_statement_p
= in_switch_statement_p
;
9099 parser
->fully_implicit_function_template_p
9100 = fully_implicit_function_template_p
;
9101 parser
->implicit_template_parms
= implicit_template_parms
;
9102 parser
->implicit_template_scope
= implicit_template_scope
;
9103 parser
->auto_is_implicit_function_template_parm_p
9104 = auto_is_implicit_function_template_parm_p
;
9107 pop_deferring_access_checks ();
9109 /* This field is only used during parsing of the lambda. */
9110 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
9112 /* This lambda shouldn't have any proxies left at this point. */
9113 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
9114 /* And now that we're done, push proxies for an enclosing lambda. */
9115 insert_pending_capture_proxies ();
9118 lambda_expr
= build_lambda_object (lambda_expr
);
9120 lambda_expr
= error_mark_node
;
9122 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
9127 /* Parse the beginning of a lambda expression.
9130 [ lambda-capture [opt] ]
9132 LAMBDA_EXPR is the current representation of the lambda expression. */
9135 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
9137 /* Need commas after the first capture. */
9140 /* Eat the leading `['. */
9141 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9143 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
9145 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_NAME
)
9146 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
9147 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
9148 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
9150 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
9152 cp_lexer_consume_token (parser
->lexer
);
9156 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
9158 cp_token
* capture_token
;
9160 tree capture_init_expr
;
9161 cp_id_kind idk
= CP_ID_KIND_NONE
;
9162 bool explicit_init_p
= false;
9164 enum capture_kind_type
9169 enum capture_kind_type capture_kind
= BY_COPY
;
9171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
9173 error ("expected end of capture-list");
9180 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
9182 /* Possibly capture `this'. */
9183 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
9185 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9186 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
9187 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
9188 "with by-copy capture default");
9189 cp_lexer_consume_token (parser
->lexer
);
9190 add_capture (lambda_expr
,
9191 /*id=*/this_identifier
,
9192 /*initializer=*/finish_this_expr(),
9193 /*by_reference_p=*/false,
9198 /* Remember whether we want to capture as a reference or not. */
9199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
9201 capture_kind
= BY_REFERENCE
;
9202 cp_lexer_consume_token (parser
->lexer
);
9205 /* Get the identifier. */
9206 capture_token
= cp_lexer_peek_token (parser
->lexer
);
9207 capture_id
= cp_parser_identifier (parser
);
9209 if (capture_id
== error_mark_node
)
9210 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9211 delimiters, but I modified this to stop on unnested ']' as well. It
9212 was already changed to stop on unnested '}', so the
9213 "closing_parenthesis" name is no more misleading with my change. */
9215 cp_parser_skip_to_closing_parenthesis (parser
,
9216 /*recovering=*/true,
9218 /*consume_paren=*/true);
9222 /* Find the initializer for this capture. */
9223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
9224 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
9225 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9227 bool direct
, non_constant
;
9228 /* An explicit initializer exists. */
9229 if (cxx_dialect
< cxx14
)
9230 pedwarn (input_location
, 0,
9231 "lambda capture initializers "
9232 "only available with -std=c++14 or -std=gnu++14");
9233 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
9235 explicit_init_p
= true;
9236 if (capture_init_expr
== NULL_TREE
)
9238 error ("empty initializer for lambda init-capture");
9239 capture_init_expr
= error_mark_node
;
9244 const char* error_msg
;
9246 /* Turn the identifier into an id-expression. */
9248 = cp_parser_lookup_name_simple (parser
, capture_id
,
9249 capture_token
->location
);
9251 if (capture_init_expr
== error_mark_node
)
9253 unqualified_name_lookup_error (capture_id
);
9256 else if (DECL_P (capture_init_expr
)
9257 && (!VAR_P (capture_init_expr
)
9258 && TREE_CODE (capture_init_expr
) != PARM_DECL
))
9260 error_at (capture_token
->location
,
9261 "capture of non-variable %qD ",
9263 inform (0, "%q+#D declared here", capture_init_expr
);
9266 if (VAR_P (capture_init_expr
)
9267 && decl_storage_duration (capture_init_expr
) != dk_auto
)
9269 if (pedwarn (capture_token
->location
, 0, "capture of variable "
9270 "%qD with non-automatic storage duration",
9272 inform (0, "%q+#D declared here", capture_init_expr
);
9277 = finish_id_expression
9282 /*integral_constant_expression_p=*/false,
9283 /*allow_non_integral_constant_expression_p=*/false,
9284 /*non_integral_constant_expression_p=*/NULL
,
9285 /*template_p=*/false,
9287 /*address_p=*/false,
9288 /*template_arg_p=*/false,
9290 capture_token
->location
);
9292 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
9294 cp_lexer_consume_token (parser
->lexer
);
9295 capture_init_expr
= make_pack_expansion (capture_init_expr
);
9298 check_for_bare_parameter_packs (capture_init_expr
);
9301 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
9302 && !explicit_init_p
)
9304 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
9305 && capture_kind
== BY_COPY
)
9306 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
9307 "of %qD redundant with by-copy capture default",
9309 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
9310 && capture_kind
== BY_REFERENCE
)
9311 pedwarn (capture_token
->location
, 0, "explicit by-reference "
9312 "capture of %qD redundant with by-reference capture "
9313 "default", capture_id
);
9316 add_capture (lambda_expr
,
9319 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
9323 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9326 /* Parse the (optional) middle of a lambda expression.
9329 < template-parameter-list [opt] >
9330 ( parameter-declaration-clause [opt] )
9331 attribute-specifier [opt]
9333 exception-specification [opt]
9334 lambda-return-type-clause [opt]
9336 LAMBDA_EXPR is the current representation of the lambda expression. */
9339 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
9341 /* 5.1.1.4 of the standard says:
9342 If a lambda-expression does not include a lambda-declarator, it is as if
9343 the lambda-declarator were ().
9344 This means an empty parameter list, no attributes, and no exception
9346 tree param_list
= void_list_node
;
9347 tree attributes
= NULL_TREE
;
9348 tree exception_spec
= NULL_TREE
;
9349 tree template_param_list
= NULL_TREE
;
9351 /* The template-parameter-list is optional, but must begin with
9352 an opening angle if present. */
9353 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
9355 if (cxx_dialect
< cxx14
)
9356 pedwarn (parser
->lexer
->next_token
->location
, 0,
9357 "lambda templates are only available with "
9358 "-std=c++14 or -std=gnu++14");
9360 cp_lexer_consume_token (parser
->lexer
);
9362 template_param_list
= cp_parser_template_parameter_list (parser
);
9364 cp_parser_skip_to_end_of_template_parameter_list (parser
);
9366 /* We just processed one more parameter list. */
9367 ++parser
->num_template_parameter_lists
;
9370 /* The parameter-declaration-clause is optional (unless
9371 template-parameter-list was given), but must begin with an
9372 opening parenthesis if present. */
9373 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9375 cp_lexer_consume_token (parser
->lexer
);
9377 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
9379 /* Parse parameters. */
9380 param_list
= cp_parser_parameter_declaration_clause (parser
);
9382 /* Default arguments shall not be specified in the
9383 parameter-declaration-clause of a lambda-declarator. */
9384 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
9385 if (TREE_PURPOSE (t
) && cxx_dialect
< cxx14
)
9386 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
9387 "default argument specified for lambda parameter");
9389 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
9391 attributes
= cp_parser_attributes_opt (parser
);
9393 /* Parse optional `mutable' keyword. */
9394 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_MUTABLE
))
9396 cp_lexer_consume_token (parser
->lexer
);
9397 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
9400 /* Parse optional exception specification. */
9401 exception_spec
= cp_parser_exception_specification_opt (parser
);
9403 /* Parse optional trailing return type. */
9404 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
9406 cp_lexer_consume_token (parser
->lexer
);
9407 LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9408 = cp_parser_trailing_type_id (parser
);
9411 /* The function parameters must be in scope all the way until after the
9412 trailing-return-type in case of decltype. */
9413 pop_bindings_and_leave_scope ();
9415 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
9416 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
9418 /* Create the function call operator.
9420 Messing with declarators like this is no uglier than building up the
9421 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9424 cp_decl_specifier_seq return_type_specs
;
9425 cp_declarator
* declarator
;
9430 clear_decl_specs (&return_type_specs
);
9431 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9432 return_type_specs
.type
= LAMBDA_EXPR_RETURN_TYPE (lambda_expr
);
9434 /* Maybe we will deduce the return type later. */
9435 return_type_specs
.type
= make_auto ();
9437 p
= obstack_alloc (&declarator_obstack
, 0);
9439 declarator
= make_id_declarator (NULL_TREE
, ansi_opname (CALL_EXPR
),
9442 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
9443 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
9444 declarator
= make_call_declarator (declarator
, param_list
, quals
,
9445 VIRT_SPEC_UNSPECIFIED
,
9448 /*late_return_type=*/NULL_TREE
);
9449 declarator
->id_loc
= LAMBDA_EXPR_LOCATION (lambda_expr
);
9451 fco
= grokmethod (&return_type_specs
,
9454 if (fco
!= error_mark_node
)
9456 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
9457 DECL_ARTIFICIAL (fco
) = 1;
9458 /* Give the object parameter a different name. */
9459 DECL_NAME (DECL_ARGUMENTS (fco
)) = get_identifier ("__closure");
9460 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr
))
9461 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco
)) = 1;
9463 if (template_param_list
)
9465 fco
= finish_member_template_decl (fco
);
9466 finish_template_decl (template_param_list
);
9467 --parser
->num_template_parameter_lists
;
9469 else if (parser
->fully_implicit_function_template_p
)
9470 fco
= finish_fully_implicit_template (parser
, fco
);
9472 finish_member_declaration (fco
);
9474 obstack_free (&declarator_obstack
, p
);
9476 return (fco
!= error_mark_node
);
9480 /* Parse the body of a lambda expression, which is simply
9484 but which requires special handling.
9485 LAMBDA_EXPR is the current representation of the lambda expression. */
9488 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
9490 bool nested
= (current_function_decl
!= NULL_TREE
);
9491 bool local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
9493 push_function_context ();
9495 /* Still increment function_depth so that we don't GC in the
9496 middle of an expression. */
9498 /* Clear this in case we're in the middle of a default argument. */
9499 parser
->local_variables_forbidden_p
= false;
9501 /* Finish the function call operator
9503 + late_parsing_for_member
9504 + function_definition_after_declarator
9505 + ctor_initializer_opt_and_function_body */
9507 tree fco
= lambda_function (lambda_expr
);
9513 /* Let the front end know that we are going to be defining this
9515 start_preparsed_function (fco
,
9517 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
9519 start_lambda_scope (fco
);
9520 body
= begin_function_body ();
9522 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
9525 /* Push the proxies for any explicit captures. */
9526 for (cap
= LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
); cap
;
9527 cap
= TREE_CHAIN (cap
))
9528 build_capture_proxy (TREE_PURPOSE (cap
));
9530 compound_stmt
= begin_compound_stmt (0);
9532 /* 5.1.1.4 of the standard says:
9533 If a lambda-expression does not include a trailing-return-type, it
9534 is as if the trailing-return-type denotes the following type:
9535 * if the compound-statement is of the form
9536 { return attribute-specifier [opt] expression ; }
9537 the type of the returned expression after lvalue-to-rvalue
9538 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9539 (_conv.array_ 4.2), and function-to-pointer conversion
9541 * otherwise, void. */
9543 /* In a lambda that has neither a lambda-return-type-clause
9544 nor a deducible form, errors should be reported for return statements
9545 in the body. Since we used void as the placeholder return type, parsing
9546 the body as usual will give such desired behavior. */
9547 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr
)
9548 && cp_lexer_peek_nth_token (parser
->lexer
, 1)->keyword
== RID_RETURN
9549 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SEMICOLON
)
9551 tree expr
= NULL_TREE
;
9552 cp_id_kind idk
= CP_ID_KIND_NONE
;
9554 /* Parse tentatively in case there's more after the initial return
9556 cp_parser_parse_tentatively (parser
);
9558 cp_parser_require_keyword (parser
, RID_RETURN
, RT_RETURN
);
9560 expr
= cp_parser_expression (parser
, &idk
);
9562 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9563 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9565 if (cp_parser_parse_definitely (parser
))
9567 if (!processing_template_decl
)
9568 apply_deduced_return_type (fco
, lambda_return_type (expr
));
9570 /* Will get error here if type not deduced yet. */
9571 finish_return_stmt (expr
);
9579 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
9580 cp_parser_label_declaration (parser
);
9581 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
9582 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
9585 finish_compound_stmt (compound_stmt
);
9588 finish_function_body (body
);
9589 finish_lambda_scope ();
9591 /* Finish the function and generate code for it if necessary. */
9592 tree fn
= finish_function (/*inline*/2);
9594 /* Only expand if the call op is not a template. */
9595 if (!DECL_TEMPLATE_INFO (fco
))
9596 expand_or_defer_fn (fn
);
9599 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
9601 pop_function_context();
9606 /* Statements [gram.stmt.stmt] */
9608 /* Parse a statement.
9612 expression-statement
9617 declaration-statement
9624 attribute-specifier-seq (opt) expression-statement
9625 attribute-specifier-seq (opt) compound-statement
9626 attribute-specifier-seq (opt) selection-statement
9627 attribute-specifier-seq (opt) iteration-statement
9628 attribute-specifier-seq (opt) jump-statement
9629 declaration-statement
9630 attribute-specifier-seq (opt) try-block
9637 IN_COMPOUND is true when the statement is nested inside a
9638 cp_parser_compound_statement; this matters for certain pragmas.
9640 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9641 is a (possibly labeled) if statement which is not enclosed in braces
9642 and has an else clause. This is used to implement -Wparentheses. */
9645 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
9646 bool in_compound
, bool *if_p
)
9648 tree statement
, std_attrs
= NULL_TREE
;
9650 location_t statement_location
, attrs_location
;
9655 /* There is no statement yet. */
9656 statement
= NULL_TREE
;
9658 saved_token_sentinel
saved_tokens (parser
->lexer
);
9659 attrs_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
9660 if (c_dialect_objc ())
9661 /* In obj-c++, seeing '[[' might be the either the beginning of
9662 c++11 attributes, or a nested objc-message-expression. So
9663 let's parse the c++11 attributes tentatively. */
9664 cp_parser_parse_tentatively (parser
);
9665 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
9666 if (c_dialect_objc ())
9668 if (!cp_parser_parse_definitely (parser
))
9669 std_attrs
= NULL_TREE
;
9672 /* Peek at the next token. */
9673 token
= cp_lexer_peek_token (parser
->lexer
);
9674 /* Remember the location of the first token in the statement. */
9675 statement_location
= token
->location
;
9676 /* If this is a keyword, then that will often determine what kind of
9677 statement we have. */
9678 if (token
->type
== CPP_KEYWORD
)
9680 enum rid keyword
= token
->keyword
;
9686 /* Looks like a labeled-statement with a case label.
9687 Parse the label, and then use tail recursion to parse
9689 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9694 statement
= cp_parser_selection_statement (parser
, if_p
);
9700 statement
= cp_parser_iteration_statement (parser
, false);
9706 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
9707 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9708 cp_lexer_consume_token (parser
->lexer
);
9709 statement
= error_mark_node
;
9712 statement
= cp_parser_cilk_for (parser
, integer_zero_node
);
9719 statement
= cp_parser_jump_statement (parser
);
9723 cp_lexer_consume_token (parser
->lexer
);
9726 tree sync_expr
= build_cilk_sync ();
9727 SET_EXPR_LOCATION (sync_expr
,
9729 statement
= finish_expr_stmt (sync_expr
);
9733 error_at (token
->location
, "-fcilkplus must be enabled to use"
9735 statement
= error_mark_node
;
9737 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
9740 /* Objective-C++ exception-handling constructs. */
9743 case RID_AT_FINALLY
:
9744 case RID_AT_SYNCHRONIZED
:
9746 statement
= cp_parser_objc_statement (parser
);
9750 statement
= cp_parser_try_block (parser
);
9754 /* This must be a namespace alias definition. */
9755 cp_parser_declaration_statement (parser
);
9758 case RID_TRANSACTION_ATOMIC
:
9759 case RID_TRANSACTION_RELAXED
:
9760 statement
= cp_parser_transaction (parser
, keyword
);
9762 case RID_TRANSACTION_CANCEL
:
9763 statement
= cp_parser_transaction_cancel (parser
);
9767 /* It might be a keyword like `int' that can start a
9768 declaration-statement. */
9772 else if (token
->type
== CPP_NAME
)
9774 /* If the next token is a `:', then we are looking at a
9775 labeled-statement. */
9776 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
9777 if (token
->type
== CPP_COLON
)
9779 /* Looks like a labeled-statement with an ordinary label.
9780 Parse the label, and then use tail recursion to parse
9783 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
9787 /* Anything that starts with a `{' must be a compound-statement. */
9788 else if (token
->type
== CPP_OPEN_BRACE
)
9789 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
9790 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9791 a statement all its own. */
9792 else if (token
->type
== CPP_PRAGMA
)
9794 /* Only certain OpenMP pragmas are attached to statements, and thus
9795 are considered statements themselves. All others are not. In
9796 the context of a compound, accept the pragma as a "statement" and
9797 return so that we can check for a close brace. Otherwise we
9798 require a real statement and must go back and read one. */
9800 cp_parser_pragma (parser
, pragma_compound
);
9801 else if (!cp_parser_pragma (parser
, pragma_stmt
))
9805 else if (token
->type
== CPP_EOF
)
9807 cp_parser_error (parser
, "expected statement");
9811 /* Everything else must be a declaration-statement or an
9812 expression-statement. Try for the declaration-statement
9813 first, unless we are looking at a `;', in which case we know that
9814 we have an expression-statement. */
9817 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9819 if (std_attrs
!= NULL_TREE
)
9821 /* Attributes should be parsed as part of the the
9822 declaration, so let's un-parse them. */
9823 saved_tokens
.rollback();
9824 std_attrs
= NULL_TREE
;
9827 cp_parser_parse_tentatively (parser
);
9828 /* Try to parse the declaration-statement. */
9829 cp_parser_declaration_statement (parser
);
9830 /* If that worked, we're done. */
9831 if (cp_parser_parse_definitely (parser
))
9834 /* Look for an expression-statement instead. */
9835 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
9838 /* Set the line number for the statement. */
9839 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
9840 SET_EXPR_LOCATION (statement
, statement_location
);
9842 /* Note that for now, we don't do anything with c++11 statements
9843 parsed at this level. */
9844 if (std_attrs
!= NULL_TREE
)
9845 warning_at (attrs_location
,
9847 "attributes at the beginning of statement are ignored");
9850 /* Parse the label for a labeled-statement, i.e.
9853 case constant-expression :
9857 case constant-expression ... constant-expression : statement
9859 When a label is parsed without errors, the label is added to the
9860 parse tree by the finish_* functions, so this function doesn't
9861 have to return the label. */
9864 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
9867 tree label
= NULL_TREE
;
9868 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9870 /* The next token should be an identifier. */
9871 token
= cp_lexer_peek_token (parser
->lexer
);
9872 if (token
->type
!= CPP_NAME
9873 && token
->type
!= CPP_KEYWORD
)
9875 cp_parser_error (parser
, "expected labeled-statement");
9879 parser
->colon_corrects_to_scope_p
= false;
9880 switch (token
->keyword
)
9887 /* Consume the `case' token. */
9888 cp_lexer_consume_token (parser
->lexer
);
9889 /* Parse the constant-expression. */
9890 expr
= cp_parser_constant_expression (parser
);
9891 if (check_for_bare_parameter_packs (expr
))
9892 expr
= error_mark_node
;
9894 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
9895 if (ellipsis
->type
== CPP_ELLIPSIS
)
9897 /* Consume the `...' token. */
9898 cp_lexer_consume_token (parser
->lexer
);
9899 expr_hi
= cp_parser_constant_expression (parser
);
9900 if (check_for_bare_parameter_packs (expr_hi
))
9901 expr_hi
= error_mark_node
;
9903 /* We don't need to emit warnings here, as the common code
9904 will do this for us. */
9907 expr_hi
= NULL_TREE
;
9909 if (parser
->in_switch_statement_p
)
9910 finish_case_label (token
->location
, expr
, expr_hi
);
9912 error_at (token
->location
,
9913 "case label %qE not within a switch statement",
9919 /* Consume the `default' token. */
9920 cp_lexer_consume_token (parser
->lexer
);
9922 if (parser
->in_switch_statement_p
)
9923 finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
9925 error_at (token
->location
, "case label not within a switch statement");
9929 /* Anything else must be an ordinary label. */
9930 label
= finish_label_stmt (cp_parser_identifier (parser
));
9934 /* Require the `:' token. */
9935 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9937 /* An ordinary label may optionally be followed by attributes.
9938 However, this is only permitted if the attributes are then
9939 followed by a semicolon. This is because, for backward
9940 compatibility, when parsing
9941 lab: __attribute__ ((unused)) int i;
9942 we want the attribute to attach to "i", not "lab". */
9943 if (label
!= NULL_TREE
9944 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
9947 cp_parser_parse_tentatively (parser
);
9948 attrs
= cp_parser_gnu_attributes_opt (parser
);
9949 if (attrs
== NULL_TREE
9950 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9951 cp_parser_abort_tentative_parse (parser
);
9952 else if (!cp_parser_parse_definitely (parser
))
9955 attributes
= chainon (attributes
, attrs
);
9958 if (attributes
!= NULL_TREE
)
9959 cplus_decl_attributes (&label
, attributes
, 0);
9961 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9964 /* Parse an expression-statement.
9966 expression-statement:
9969 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9970 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9971 indicates whether this expression-statement is part of an
9972 expression statement. */
9975 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
9977 tree statement
= NULL_TREE
;
9978 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9980 /* If the next token is a ';', then there is no expression
9982 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
9984 statement
= cp_parser_expression (parser
);
9985 if (statement
== error_mark_node
9986 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9988 cp_parser_skip_to_end_of_block_or_statement (parser
);
9989 return error_mark_node
;
9993 /* Give a helpful message for "A<T>::type t;" and the like. */
9994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
9995 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
9997 if (TREE_CODE (statement
) == SCOPE_REF
)
9998 error_at (token
->location
, "need %<typename%> before %qE because "
9999 "%qT is a dependent scope",
10000 statement
, TREE_OPERAND (statement
, 0));
10001 else if (is_overloaded_fn (statement
)
10002 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
10005 tree fn
= get_first_fn (statement
);
10006 error_at (token
->location
,
10007 "%<%T::%D%> names the constructor, not the type",
10008 DECL_CONTEXT (fn
), DECL_NAME (fn
));
10012 /* Consume the final `;'. */
10013 cp_parser_consume_semicolon_at_end_of_statement (parser
);
10015 if (in_statement_expr
10016 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
10017 /* This is the final expression statement of a statement
10019 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
10020 else if (statement
)
10021 statement
= finish_expr_stmt (statement
);
10026 /* Parse a compound-statement.
10028 compound-statement:
10029 { statement-seq [opt] }
10033 compound-statement:
10034 { label-declaration-seq [opt] statement-seq [opt] }
10036 label-declaration-seq:
10038 label-declaration-seq label-declaration
10040 Returns a tree representing the statement. */
10043 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
10044 bool in_try
, bool function_body
)
10046 tree compound_stmt
;
10048 /* Consume the `{'. */
10049 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10050 return error_mark_node
;
10051 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
10052 && !function_body
&& cxx_dialect
< cxx14
)
10053 pedwarn (input_location
, OPT_Wpedantic
,
10054 "compound-statement in constexpr function");
10055 /* Begin the compound-statement. */
10056 compound_stmt
= begin_compound_stmt (in_try
? BCS_TRY_BLOCK
: 0);
10057 /* If the next keyword is `__label__' we have a label declaration. */
10058 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
10059 cp_parser_label_declaration (parser
);
10060 /* Parse an (optional) statement-seq. */
10061 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
10062 /* Finish the compound-statement. */
10063 finish_compound_stmt (compound_stmt
);
10064 /* Consume the `}'. */
10065 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
10067 return compound_stmt
;
10070 /* Parse an (optional) statement-seq.
10074 statement-seq [opt] statement */
10077 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
10079 /* Scan statements until there aren't any more. */
10082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10084 /* If we are looking at a `}', then we have run out of
10085 statements; the same is true if we have reached the end
10086 of file, or have stumbled upon a stray '@end'. */
10087 if (token
->type
== CPP_CLOSE_BRACE
10088 || token
->type
== CPP_EOF
10089 || token
->type
== CPP_PRAGMA_EOL
10090 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
10093 /* If we are in a compound statement and find 'else' then
10094 something went wrong. */
10095 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
10097 if (parser
->in_statement
& IN_IF_STMT
)
10101 token
= cp_lexer_consume_token (parser
->lexer
);
10102 error_at (token
->location
, "%<else%> without a previous %<if%>");
10106 /* Parse the statement. */
10107 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
10111 /* Parse a selection-statement.
10113 selection-statement:
10114 if ( condition ) statement
10115 if ( condition ) statement else statement
10116 switch ( condition ) statement
10118 Returns the new IF_STMT or SWITCH_STMT.
10120 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10121 is a (possibly labeled) if statement which is not enclosed in
10122 braces and has an else clause. This is used to implement
10126 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
)
10134 /* Peek at the next token. */
10135 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
10137 /* See what kind of keyword it is. */
10138 keyword
= token
->keyword
;
10147 /* Look for the `('. */
10148 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
10150 cp_parser_skip_to_end_of_statement (parser
);
10151 return error_mark_node
;
10154 /* Begin the selection-statement. */
10155 if (keyword
== RID_IF
)
10156 statement
= begin_if_stmt ();
10158 statement
= begin_switch_stmt ();
10160 /* Parse the condition. */
10161 condition
= cp_parser_condition (parser
);
10162 /* Look for the `)'. */
10163 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
10164 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
10165 /*consume_paren=*/true);
10167 if (keyword
== RID_IF
)
10170 unsigned char in_statement
;
10172 /* Add the condition. */
10173 finish_if_stmt_cond (condition
, statement
);
10175 /* Parse the then-clause. */
10176 in_statement
= parser
->in_statement
;
10177 parser
->in_statement
|= IN_IF_STMT
;
10178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10180 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10181 add_stmt (build_empty_stmt (loc
));
10182 cp_lexer_consume_token (parser
->lexer
);
10183 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
10184 warning_at (loc
, OPT_Wempty_body
, "suggest braces around "
10185 "empty body in an %<if%> statement");
10189 cp_parser_implicitly_scoped_statement (parser
, &nested_if
);
10190 parser
->in_statement
= in_statement
;
10192 finish_then_clause (statement
);
10194 /* If the next token is `else', parse the else-clause. */
10195 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
10198 /* Consume the `else' keyword. */
10199 cp_lexer_consume_token (parser
->lexer
);
10200 begin_else_clause (statement
);
10201 /* Parse the else-clause. */
10202 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
10205 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10207 OPT_Wempty_body
, "suggest braces around "
10208 "empty body in an %<else%> statement");
10209 add_stmt (build_empty_stmt (loc
));
10210 cp_lexer_consume_token (parser
->lexer
);
10213 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10215 finish_else_clause (statement
);
10217 /* If we are currently parsing a then-clause, then
10218 IF_P will not be NULL. We set it to true to
10219 indicate that this if statement has an else clause.
10220 This may trigger the Wparentheses warning below
10221 when we get back up to the parent if statement. */
10227 /* This if statement does not have an else clause. If
10228 NESTED_IF is true, then the then-clause is an if
10229 statement which does have an else clause. We warn
10230 about the potential ambiguity. */
10232 warning_at (EXPR_LOCATION (statement
), OPT_Wparentheses
,
10233 "suggest explicit braces to avoid ambiguous"
10237 /* Now we're all done with the if-statement. */
10238 finish_if_stmt (statement
);
10242 bool in_switch_statement_p
;
10243 unsigned char in_statement
;
10245 /* Add the condition. */
10246 finish_switch_cond (condition
, statement
);
10248 /* Parse the body of the switch-statement. */
10249 in_switch_statement_p
= parser
->in_switch_statement_p
;
10250 in_statement
= parser
->in_statement
;
10251 parser
->in_switch_statement_p
= true;
10252 parser
->in_statement
|= IN_SWITCH_STMT
;
10253 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10254 parser
->in_switch_statement_p
= in_switch_statement_p
;
10255 parser
->in_statement
= in_statement
;
10257 /* Now we're all done with the switch-statement. */
10258 finish_switch_stmt (statement
);
10266 cp_parser_error (parser
, "expected selection-statement");
10267 return error_mark_node
;
10271 /* Parse a condition.
10275 type-specifier-seq declarator = initializer-clause
10276 type-specifier-seq declarator braced-init-list
10281 type-specifier-seq declarator asm-specification [opt]
10282 attributes [opt] = assignment-expression
10284 Returns the expression that should be tested. */
10287 cp_parser_condition (cp_parser
* parser
)
10289 cp_decl_specifier_seq type_specifiers
;
10290 const char *saved_message
;
10291 int declares_class_or_enum
;
10293 /* Try the declaration first. */
10294 cp_parser_parse_tentatively (parser
);
10295 /* New types are not allowed in the type-specifier-seq for a
10297 saved_message
= parser
->type_definition_forbidden_message
;
10298 parser
->type_definition_forbidden_message
10299 = G_("types may not be defined in conditions");
10300 /* Parse the type-specifier-seq. */
10301 cp_parser_decl_specifier_seq (parser
,
10302 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
10304 &declares_class_or_enum
);
10305 /* Restore the saved message. */
10306 parser
->type_definition_forbidden_message
= saved_message
;
10307 /* If all is well, we might be looking at a declaration. */
10308 if (!cp_parser_error_occurred (parser
))
10311 tree asm_specification
;
10313 cp_declarator
*declarator
;
10314 tree initializer
= NULL_TREE
;
10316 /* Parse the declarator. */
10317 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
10318 /*ctor_dtor_or_conv_p=*/NULL
,
10319 /*parenthesized_p=*/NULL
,
10320 /*member_p=*/false,
10321 /*friend_p=*/false);
10322 /* Parse the attributes. */
10323 attributes
= cp_parser_attributes_opt (parser
);
10324 /* Parse the asm-specification. */
10325 asm_specification
= cp_parser_asm_specification_opt (parser
);
10326 /* If the next token is not an `=' or '{', then we might still be
10327 looking at an expression. For example:
10331 looks like a decl-specifier-seq and a declarator -- but then
10332 there is no `=', so this is an expression. */
10333 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
10334 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
10335 cp_parser_simulate_error (parser
);
10337 /* If we did see an `=' or '{', then we are looking at a declaration
10339 if (cp_parser_parse_definitely (parser
))
10342 bool non_constant_p
;
10343 bool flags
= LOOKUP_ONLYCONVERTING
;
10345 /* Create the declaration. */
10346 decl
= start_decl (declarator
, &type_specifiers
,
10347 /*initialized_p=*/true,
10348 attributes
, /*prefix_attributes=*/NULL_TREE
,
10351 /* Parse the initializer. */
10352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10354 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
10355 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
10360 /* Consume the `='. */
10361 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
10362 initializer
= cp_parser_initializer_clause (parser
, &non_constant_p
);
10364 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
10365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10367 /* Process the initializer. */
10368 cp_finish_decl (decl
,
10369 initializer
, !non_constant_p
,
10374 pop_scope (pushed_scope
);
10376 return convert_from_reference (decl
);
10379 /* If we didn't even get past the declarator successfully, we are
10380 definitely not looking at a declaration. */
10382 cp_parser_abort_tentative_parse (parser
);
10384 /* Otherwise, we are looking at an expression. */
10385 return cp_parser_expression (parser
);
10388 /* Parses a for-statement or range-for-statement until the closing ')',
10392 cp_parser_for (cp_parser
*parser
, bool ivdep
)
10394 tree init
, scope
, decl
;
10397 /* Begin the for-statement. */
10398 scope
= begin_for_scope (&init
);
10400 /* Parse the initialization. */
10401 is_range_for
= cp_parser_for_init_statement (parser
, &decl
);
10404 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
);
10406 return cp_parser_c_for (parser
, scope
, init
, ivdep
);
10410 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
)
10412 /* Normal for loop */
10413 tree condition
= NULL_TREE
;
10414 tree expression
= NULL_TREE
;
10417 stmt
= begin_for_stmt (scope
, init
);
10418 /* The for-init-statement has already been parsed in
10419 cp_parser_for_init_statement, so no work is needed here. */
10420 finish_for_init_stmt (stmt
);
10422 /* If there's a condition, process it. */
10423 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10424 condition
= cp_parser_condition (parser
);
10427 cp_parser_error (parser
, "missing loop condition in loop with "
10428 "%<GCC ivdep%> pragma");
10429 condition
= error_mark_node
;
10431 finish_for_cond (condition
, stmt
, ivdep
);
10432 /* Look for the `;'. */
10433 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10435 /* If there's an expression, process it. */
10436 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
10437 expression
= cp_parser_expression (parser
);
10438 finish_for_expr (expression
, stmt
);
10443 /* Tries to parse a range-based for-statement:
10446 decl-specifier-seq declarator : expression
10448 The decl-specifier-seq declarator and the `:' are already parsed by
10449 cp_parser_for_init_statement. If processing_template_decl it returns a
10450 newly created RANGE_FOR_STMT; if not, it is converted to a
10451 regular FOR_STMT. */
10454 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
10457 tree stmt
, range_expr
;
10459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10461 bool expr_non_constant_p
;
10462 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
10465 range_expr
= cp_parser_expression (parser
);
10467 /* If in template, STMT is converted to a normal for-statement
10468 at instantiation. If not, it is done just ahead. */
10469 if (processing_template_decl
)
10471 if (check_for_bare_parameter_packs (range_expr
))
10472 range_expr
= error_mark_node
;
10473 stmt
= begin_range_for_stmt (scope
, init
);
10475 RANGE_FOR_IVDEP (stmt
) = 1;
10476 finish_range_for_decl (stmt
, range_decl
, range_expr
);
10477 if (!type_dependent_expression_p (range_expr
)
10478 /* do_auto_deduction doesn't mess with template init-lists. */
10479 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
10480 do_range_for_auto_deduction (range_decl
, range_expr
);
10484 stmt
= begin_for_stmt (scope
, init
);
10485 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
, ivdep
);
10490 /* Subroutine of cp_convert_range_for: given the initializer expression,
10491 builds up the range temporary. */
10494 build_range_temp (tree range_expr
)
10496 tree range_type
, range_temp
;
10498 /* Find out the type deduced by the declaration
10499 `auto &&__range = range_expr'. */
10500 range_type
= cp_build_reference_type (make_auto (), true);
10501 range_type
= do_auto_deduction (range_type
, range_expr
,
10502 type_uses_auto (range_type
));
10504 /* Create the __range variable. */
10505 range_temp
= build_decl (input_location
, VAR_DECL
,
10506 get_identifier ("__for_range"), range_type
);
10507 TREE_USED (range_temp
) = 1;
10508 DECL_ARTIFICIAL (range_temp
) = 1;
10513 /* Used by cp_parser_range_for in template context: we aren't going to
10514 do a full conversion yet, but we still need to resolve auto in the
10515 type of the for-range-declaration if present. This is basically
10516 a shortcut version of cp_convert_range_for. */
10519 do_range_for_auto_deduction (tree decl
, tree range_expr
)
10521 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
10524 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
10525 range_temp
= convert_from_reference (build_range_temp (range_expr
));
10526 iter_type
= (cp_parser_perform_range_for_lookup
10527 (range_temp
, &begin_dummy
, &end_dummy
));
10530 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
10532 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
, RO_NULL
,
10533 tf_warning_or_error
);
10534 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
10535 iter_decl
, auto_node
);
10540 /* Converts a range-based for-statement into a normal
10541 for-statement, as per the definition.
10543 for (RANGE_DECL : RANGE_EXPR)
10546 should be equivalent to:
10549 auto &&__range = RANGE_EXPR;
10550 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10554 RANGE_DECL = *__begin;
10559 If RANGE_EXPR is an array:
10560 BEGIN_EXPR = __range
10561 END_EXPR = __range + ARRAY_SIZE(__range)
10562 Else if RANGE_EXPR has a member 'begin' or 'end':
10563 BEGIN_EXPR = __range.begin()
10564 END_EXPR = __range.end()
10566 BEGIN_EXPR = begin(__range)
10567 END_EXPR = end(__range);
10569 If __range has a member 'begin' but not 'end', or vice versa, we must
10570 still use the second alternative (it will surely fail, however).
10571 When calling begin()/end() in the third alternative we must use
10572 argument dependent lookup, but always considering 'std' as an associated
10576 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
10580 tree iter_type
, begin_expr
, end_expr
;
10581 tree condition
, expression
;
10583 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
10584 /* If an error happened previously do nothing or else a lot of
10585 unhelpful errors would be issued. */
10586 begin_expr
= end_expr
= iter_type
= error_mark_node
;
10591 if (TREE_CODE (range_expr
) == VAR_DECL
10592 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
10593 /* Can't bind a reference to an array of runtime bound. */
10594 range_temp
= range_expr
;
10597 range_temp
= build_range_temp (range_expr
);
10598 pushdecl (range_temp
);
10599 cp_finish_decl (range_temp
, range_expr
,
10600 /*is_constant_init*/false, NULL_TREE
,
10601 LOOKUP_ONLYCONVERTING
);
10602 range_temp
= convert_from_reference (range_temp
);
10604 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
10605 &begin_expr
, &end_expr
);
10608 /* The new for initialization statement. */
10609 begin
= build_decl (input_location
, VAR_DECL
,
10610 get_identifier ("__for_begin"), iter_type
);
10611 TREE_USED (begin
) = 1;
10612 DECL_ARTIFICIAL (begin
) = 1;
10614 cp_finish_decl (begin
, begin_expr
,
10615 /*is_constant_init*/false, NULL_TREE
,
10616 LOOKUP_ONLYCONVERTING
);
10618 end
= build_decl (input_location
, VAR_DECL
,
10619 get_identifier ("__for_end"), iter_type
);
10620 TREE_USED (end
) = 1;
10621 DECL_ARTIFICIAL (end
) = 1;
10623 cp_finish_decl (end
, end_expr
,
10624 /*is_constant_init*/false, NULL_TREE
,
10625 LOOKUP_ONLYCONVERTING
);
10627 finish_for_init_stmt (statement
);
10629 /* The new for condition. */
10630 condition
= build_x_binary_op (input_location
, NE_EXPR
,
10633 NULL
, tf_warning_or_error
);
10634 finish_for_cond (condition
, statement
, ivdep
);
10636 /* The new increment expression. */
10637 expression
= finish_unary_op_expr (input_location
,
10638 PREINCREMENT_EXPR
, begin
,
10639 tf_warning_or_error
);
10640 finish_for_expr (expression
, statement
);
10642 /* The declaration is initialized with *__begin inside the loop body. */
10643 cp_finish_decl (range_decl
,
10644 build_x_indirect_ref (input_location
, begin
, RO_NULL
,
10645 tf_warning_or_error
),
10646 /*is_constant_init*/false, NULL_TREE
,
10647 LOOKUP_ONLYCONVERTING
);
10652 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10653 We need to solve both at the same time because the method used
10654 depends on the existence of members begin or end.
10655 Returns the type deduced for the iterator expression. */
10658 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
10660 if (error_operand_p (range
))
10662 *begin
= *end
= error_mark_node
;
10663 return error_mark_node
;
10666 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
10668 error ("range-based %<for%> expression of type %qT "
10669 "has incomplete type", TREE_TYPE (range
));
10670 *begin
= *end
= error_mark_node
;
10671 return error_mark_node
;
10673 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
10675 /* If RANGE is an array, we will use pointer arithmetic. */
10677 *end
= build_binary_op (input_location
, PLUS_EXPR
,
10679 array_type_nelts_top (TREE_TYPE (range
)),
10681 return build_pointer_type (TREE_TYPE (TREE_TYPE (range
)));
10685 /* If it is not an array, we must do a bit of magic. */
10686 tree id_begin
, id_end
;
10687 tree member_begin
, member_end
;
10689 *begin
= *end
= error_mark_node
;
10691 id_begin
= get_identifier ("begin");
10692 id_end
= get_identifier ("end");
10693 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
10694 /*protect=*/2, /*want_type=*/false,
10695 tf_warning_or_error
);
10696 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
10697 /*protect=*/2, /*want_type=*/false,
10698 tf_warning_or_error
);
10700 if (member_begin
!= NULL_TREE
|| member_end
!= NULL_TREE
)
10702 /* Use the member functions. */
10703 if (member_begin
!= NULL_TREE
)
10704 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
10706 error ("range-based %<for%> expression of type %qT has an "
10707 "%<end%> member but not a %<begin%>", TREE_TYPE (range
));
10709 if (member_end
!= NULL_TREE
)
10710 *end
= cp_parser_range_for_member_function (range
, id_end
);
10712 error ("range-based %<for%> expression of type %qT has a "
10713 "%<begin%> member but not an %<end%>", TREE_TYPE (range
));
10717 /* Use global functions with ADL. */
10718 vec
<tree
, va_gc
> *vec
;
10719 vec
= make_tree_vector ();
10721 vec_safe_push (vec
, range
);
10723 member_begin
= perform_koenig_lookup (id_begin
, vec
,
10724 tf_warning_or_error
);
10725 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
10726 tf_warning_or_error
);
10727 member_end
= perform_koenig_lookup (id_end
, vec
,
10728 tf_warning_or_error
);
10729 *end
= finish_call_expr (member_end
, &vec
, false, true,
10730 tf_warning_or_error
);
10732 release_tree_vector (vec
);
10735 /* Last common checks. */
10736 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
10738 /* If one of the expressions is an error do no more checks. */
10739 *begin
= *end
= error_mark_node
;
10740 return error_mark_node
;
10742 else if (type_dependent_expression_p (*begin
)
10743 || type_dependent_expression_p (*end
))
10744 /* Can happen, when, eg, in a template context, Koenig lookup
10745 can't resolve begin/end (c++/58503). */
10749 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
10750 /* The unqualified type of the __begin and __end temporaries should
10751 be the same, as required by the multiple auto declaration. */
10752 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
10753 error ("inconsistent begin/end types in range-based %<for%> "
10754 "statement: %qT and %qT",
10755 TREE_TYPE (*begin
), TREE_TYPE (*end
));
10761 /* Helper function for cp_parser_perform_range_for_lookup.
10762 Builds a tree for RANGE.IDENTIFIER(). */
10765 cp_parser_range_for_member_function (tree range
, tree identifier
)
10768 vec
<tree
, va_gc
> *vec
;
10770 member
= finish_class_member_access_expr (range
, identifier
,
10771 false, tf_warning_or_error
);
10772 if (member
== error_mark_node
)
10773 return error_mark_node
;
10775 vec
= make_tree_vector ();
10776 res
= finish_call_expr (member
, &vec
,
10777 /*disallow_virtual=*/false,
10778 /*koenig_p=*/false,
10779 tf_warning_or_error
);
10780 release_tree_vector (vec
);
10784 /* Parse an iteration-statement.
10786 iteration-statement:
10787 while ( condition ) statement
10788 do statement while ( expression ) ;
10789 for ( for-init-statement condition [opt] ; expression [opt] )
10792 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10795 cp_parser_iteration_statement (cp_parser
* parser
, bool ivdep
)
10800 unsigned char in_statement
;
10802 /* Peek at the next token. */
10803 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_INTERATION
);
10805 return error_mark_node
;
10807 /* Remember whether or not we are already within an iteration
10809 in_statement
= parser
->in_statement
;
10811 /* See what kind of keyword it is. */
10812 keyword
= token
->keyword
;
10819 /* Begin the while-statement. */
10820 statement
= begin_while_stmt ();
10821 /* Look for the `('. */
10822 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10823 /* Parse the condition. */
10824 condition
= cp_parser_condition (parser
);
10825 finish_while_stmt_cond (condition
, statement
, ivdep
);
10826 /* Look for the `)'. */
10827 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10828 /* Parse the dependent statement. */
10829 parser
->in_statement
= IN_ITERATION_STMT
;
10830 cp_parser_already_scoped_statement (parser
);
10831 parser
->in_statement
= in_statement
;
10832 /* We're done with the while-statement. */
10833 finish_while_stmt (statement
);
10841 /* Begin the do-statement. */
10842 statement
= begin_do_stmt ();
10843 /* Parse the body of the do-statement. */
10844 parser
->in_statement
= IN_ITERATION_STMT
;
10845 cp_parser_implicitly_scoped_statement (parser
, NULL
);
10846 parser
->in_statement
= in_statement
;
10847 finish_do_body (statement
);
10848 /* Look for the `while' keyword. */
10849 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
10850 /* Look for the `('. */
10851 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10852 /* Parse the expression. */
10853 expression
= cp_parser_expression (parser
);
10854 /* We're done with the do-statement. */
10855 finish_do_stmt (expression
, statement
, ivdep
);
10856 /* Look for the `)'. */
10857 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10858 /* Look for the `;'. */
10859 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10865 /* Look for the `('. */
10866 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
10868 statement
= cp_parser_for (parser
, ivdep
);
10870 /* Look for the `)'. */
10871 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
10873 /* Parse the body of the for-statement. */
10874 parser
->in_statement
= IN_ITERATION_STMT
;
10875 cp_parser_already_scoped_statement (parser
);
10876 parser
->in_statement
= in_statement
;
10878 /* We're done with the for-statement. */
10879 finish_for_stmt (statement
);
10884 cp_parser_error (parser
, "expected iteration-statement");
10885 statement
= error_mark_node
;
10892 /* Parse a for-init-statement or the declarator of a range-based-for.
10893 Returns true if a range-based-for declaration is seen.
10895 for-init-statement:
10896 expression-statement
10897 simple-declaration */
10900 cp_parser_for_init_statement (cp_parser
* parser
, tree
*decl
)
10902 /* If the next token is a `;', then we have an empty
10903 expression-statement. Grammatically, this is also a
10904 simple-declaration, but an invalid one, because it does not
10905 declare anything. Therefore, if we did not handle this case
10906 specially, we would issue an error message about an invalid
10908 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
10910 bool is_range_for
= false;
10911 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10913 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
10914 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
10916 /* N3994 -- for (id : init) ... */
10917 if (cxx_dialect
< cxx1z
)
10918 pedwarn (input_location
, 0, "range-based for loop without a "
10919 "type-specifier only available with "
10920 "-std=c++1z or -std=gnu++1z");
10921 tree name
= cp_parser_identifier (parser
);
10922 tree type
= cp_build_reference_type (make_auto (), /*rval*/true);
10923 *decl
= build_decl (input_location
, VAR_DECL
, name
, type
);
10925 cp_lexer_consume_token (parser
->lexer
);
10929 /* A colon is used in range-based for. */
10930 parser
->colon_corrects_to_scope_p
= false;
10932 /* We're going to speculatively look for a declaration, falling back
10933 to an expression, if necessary. */
10934 cp_parser_parse_tentatively (parser
);
10935 /* Parse the declaration. */
10936 cp_parser_simple_declaration (parser
,
10937 /*function_definition_allowed_p=*/false,
10939 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10940 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
10942 /* It is a range-for, consume the ':' */
10943 cp_lexer_consume_token (parser
->lexer
);
10944 is_range_for
= true;
10945 if (cxx_dialect
< cxx11
)
10947 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
10948 "range-based %<for%> loops only available with "
10949 "-std=c++11 or -std=gnu++11");
10950 *decl
= error_mark_node
;
10954 /* The ';' is not consumed yet because we told
10955 cp_parser_simple_declaration not to. */
10956 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
10958 if (cp_parser_parse_definitely (parser
))
10959 return is_range_for
;
10960 /* If the tentative parse failed, then we shall need to look for an
10961 expression-statement. */
10963 /* If we are here, it is an expression-statement. */
10964 cp_parser_expression_statement (parser
, NULL_TREE
);
10968 /* Parse a jump-statement.
10973 return expression [opt] ;
10974 return braced-init-list ;
10980 goto * expression ;
10982 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10985 cp_parser_jump_statement (cp_parser
* parser
)
10987 tree statement
= error_mark_node
;
10990 unsigned char in_statement
;
10992 /* Peek at the next token. */
10993 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
10995 return error_mark_node
;
10997 /* See what kind of keyword it is. */
10998 keyword
= token
->keyword
;
11002 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
11003 switch (in_statement
)
11006 error_at (token
->location
, "break statement not within loop or switch");
11009 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
11010 || in_statement
== IN_ITERATION_STMT
);
11011 statement
= finish_break_stmt ();
11012 if (in_statement
== IN_ITERATION_STMT
)
11013 break_maybe_infinite_loop ();
11016 error_at (token
->location
, "invalid exit from OpenMP structured block");
11019 error_at (token
->location
, "break statement used with OpenMP for loop");
11021 case IN_CILK_SIMD_FOR
:
11022 error_at (token
->location
, "break statement used with Cilk Plus for loop");
11025 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11029 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
11032 error_at (token
->location
, "continue statement not within a loop");
11034 case IN_CILK_SIMD_FOR
:
11035 error_at (token
->location
,
11036 "continue statement within %<#pragma simd%> loop body");
11037 /* Fall through. */
11038 case IN_ITERATION_STMT
:
11040 statement
= finish_continue_stmt ();
11043 error_at (token
->location
, "invalid exit from OpenMP structured block");
11046 gcc_unreachable ();
11048 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11054 bool expr_non_constant_p
;
11056 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11058 cp_lexer_set_source_position (parser
->lexer
);
11059 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
11060 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
11062 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11063 expr
= cp_parser_expression (parser
);
11065 /* If the next token is a `;', then there is no
11068 /* Build the return-statement. */
11069 statement
= finish_return_stmt (expr
);
11070 /* Look for the final `;'. */
11071 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11076 if (parser
->in_function_body
11077 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
11079 error ("%<goto%> in %<constexpr%> function");
11080 cp_function_chain
->invalid_constexpr
= true;
11083 /* Create the goto-statement. */
11084 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
11086 /* Issue a warning about this use of a GNU extension. */
11087 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
11088 /* Consume the '*' token. */
11089 cp_lexer_consume_token (parser
->lexer
);
11090 /* Parse the dependent expression. */
11091 finish_goto_stmt (cp_parser_expression (parser
));
11094 finish_goto_stmt (cp_parser_identifier (parser
));
11095 /* Look for the final `;'. */
11096 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11100 cp_parser_error (parser
, "expected jump-statement");
11107 /* Parse a declaration-statement.
11109 declaration-statement:
11110 block-declaration */
11113 cp_parser_declaration_statement (cp_parser
* parser
)
11117 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11118 p
= obstack_alloc (&declarator_obstack
, 0);
11120 /* Parse the block-declaration. */
11121 cp_parser_block_declaration (parser
, /*statement_p=*/true);
11123 /* Free any declarators allocated. */
11124 obstack_free (&declarator_obstack
, p
);
11127 /* Some dependent statements (like `if (cond) statement'), are
11128 implicitly in their own scope. In other words, if the statement is
11129 a single statement (as opposed to a compound-statement), it is
11130 none-the-less treated as if it were enclosed in braces. Any
11131 declarations appearing in the dependent statement are out of scope
11132 after control passes that point. This function parses a statement,
11133 but ensures that is in its own scope, even if it is not a
11134 compound-statement.
11136 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11137 is a (possibly labeled) if statement which is not enclosed in
11138 braces and has an else clause. This is used to implement
11141 Returns the new statement. */
11144 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
)
11151 /* Mark if () ; with a special NOP_EXPR. */
11152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11154 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11155 cp_lexer_consume_token (parser
->lexer
);
11156 statement
= add_stmt (build_empty_stmt (loc
));
11158 /* if a compound is opened, we simply parse the statement directly. */
11159 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11160 statement
= cp_parser_compound_statement (parser
, NULL
, false, false);
11161 /* If the token is not a `{', then we must take special action. */
11164 /* Create a compound-statement. */
11165 statement
= begin_compound_stmt (0);
11166 /* Parse the dependent-statement. */
11167 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
11168 /* Finish the dummy compound-statement. */
11169 finish_compound_stmt (statement
);
11172 /* Return the statement. */
11176 /* For some dependent statements (like `while (cond) statement'), we
11177 have already created a scope. Therefore, even if the dependent
11178 statement is a compound-statement, we do not want to create another
11182 cp_parser_already_scoped_statement (cp_parser
* parser
)
11184 /* If the token is a `{', then we must take special action. */
11185 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
11186 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
11189 /* Avoid calling cp_parser_compound_statement, so that we
11190 don't create a new scope. Do everything else by hand. */
11191 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
11192 /* If the next keyword is `__label__' we have a label declaration. */
11193 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11194 cp_parser_label_declaration (parser
);
11195 /* Parse an (optional) statement-seq. */
11196 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11197 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
11201 /* Declarations [gram.dcl.dcl] */
11203 /* Parse an optional declaration-sequence.
11207 declaration-seq declaration */
11210 cp_parser_declaration_seq_opt (cp_parser
* parser
)
11216 token
= cp_lexer_peek_token (parser
->lexer
);
11218 if (token
->type
== CPP_CLOSE_BRACE
11219 || token
->type
== CPP_EOF
11220 || token
->type
== CPP_PRAGMA_EOL
)
11223 if (token
->type
== CPP_SEMICOLON
)
11225 /* A declaration consisting of a single semicolon is
11226 invalid. Allow it unless we're being pedantic. */
11227 cp_lexer_consume_token (parser
->lexer
);
11228 if (!in_system_header_at (input_location
))
11229 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
11233 /* If we're entering or exiting a region that's implicitly
11234 extern "C", modify the lang context appropriately. */
11235 if (!parser
->implicit_extern_c
&& token
->implicit_extern_c
)
11237 push_lang_context (lang_name_c
);
11238 parser
->implicit_extern_c
= true;
11240 else if (parser
->implicit_extern_c
&& !token
->implicit_extern_c
)
11242 pop_lang_context ();
11243 parser
->implicit_extern_c
= false;
11246 if (token
->type
== CPP_PRAGMA
)
11248 /* A top-level declaration can consist solely of a #pragma.
11249 A nested declaration cannot, so this is done here and not
11250 in cp_parser_declaration. (A #pragma at block scope is
11251 handled in cp_parser_statement.) */
11252 cp_parser_pragma (parser
, pragma_external
);
11256 /* Parse the declaration itself. */
11257 cp_parser_declaration (parser
);
11261 /* Parse a declaration.
11265 function-definition
11266 template-declaration
11267 explicit-instantiation
11268 explicit-specialization
11269 linkage-specification
11270 namespace-definition
11275 __extension__ declaration */
11278 cp_parser_declaration (cp_parser
* parser
)
11282 int saved_pedantic
;
11284 tree attributes
= NULL_TREE
;
11286 /* Check for the `__extension__' keyword. */
11287 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11289 /* Parse the qualified declaration. */
11290 cp_parser_declaration (parser
);
11291 /* Restore the PEDANTIC flag. */
11292 pedantic
= saved_pedantic
;
11297 /* Try to figure out what kind of declaration is present. */
11298 token1
= *cp_lexer_peek_token (parser
->lexer
);
11300 if (token1
.type
!= CPP_EOF
)
11301 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
11304 token2
.type
= CPP_EOF
;
11305 token2
.keyword
= RID_MAX
;
11308 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11309 p
= obstack_alloc (&declarator_obstack
, 0);
11311 /* If the next token is `extern' and the following token is a string
11312 literal, then we have a linkage specification. */
11313 if (token1
.keyword
== RID_EXTERN
11314 && cp_parser_is_pure_string_literal (&token2
))
11315 cp_parser_linkage_specification (parser
);
11316 /* If the next token is `template', then we have either a template
11317 declaration, an explicit instantiation, or an explicit
11319 else if (token1
.keyword
== RID_TEMPLATE
)
11321 /* `template <>' indicates a template specialization. */
11322 if (token2
.type
== CPP_LESS
11323 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
11324 cp_parser_explicit_specialization (parser
);
11325 /* `template <' indicates a template declaration. */
11326 else if (token2
.type
== CPP_LESS
)
11327 cp_parser_template_declaration (parser
, /*member_p=*/false);
11328 /* Anything else must be an explicit instantiation. */
11330 cp_parser_explicit_instantiation (parser
);
11332 /* If the next token is `export', then we have a template
11334 else if (token1
.keyword
== RID_EXPORT
)
11335 cp_parser_template_declaration (parser
, /*member_p=*/false);
11336 /* If the next token is `extern', 'static' or 'inline' and the one
11337 after that is `template', we have a GNU extended explicit
11338 instantiation directive. */
11339 else if (cp_parser_allow_gnu_extensions_p (parser
)
11340 && (token1
.keyword
== RID_EXTERN
11341 || token1
.keyword
== RID_STATIC
11342 || token1
.keyword
== RID_INLINE
)
11343 && token2
.keyword
== RID_TEMPLATE
)
11344 cp_parser_explicit_instantiation (parser
);
11345 /* If the next token is `namespace', check for a named or unnamed
11346 namespace definition. */
11347 else if (token1
.keyword
== RID_NAMESPACE
11348 && (/* A named namespace definition. */
11349 (token2
.type
== CPP_NAME
11350 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
11352 /* An unnamed namespace definition. */
11353 || token2
.type
== CPP_OPEN_BRACE
11354 || token2
.keyword
== RID_ATTRIBUTE
))
11355 cp_parser_namespace_definition (parser
);
11356 /* An inline (associated) namespace definition. */
11357 else if (token1
.keyword
== RID_INLINE
11358 && token2
.keyword
== RID_NAMESPACE
)
11359 cp_parser_namespace_definition (parser
);
11360 /* Objective-C++ declaration/definition. */
11361 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
11362 cp_parser_objc_declaration (parser
, NULL_TREE
);
11363 else if (c_dialect_objc ()
11364 && token1
.keyword
== RID_ATTRIBUTE
11365 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
11366 cp_parser_objc_declaration (parser
, attributes
);
11367 /* We must have either a block declaration or a function
11370 /* Try to parse a block-declaration, or a function-definition. */
11371 cp_parser_block_declaration (parser
, /*statement_p=*/false);
11373 /* Free any declarators allocated. */
11374 obstack_free (&declarator_obstack
, p
);
11377 /* Parse a block-declaration.
11382 namespace-alias-definition
11389 __extension__ block-declaration
11394 static_assert-declaration
11396 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11397 part of a declaration-statement. */
11400 cp_parser_block_declaration (cp_parser
*parser
,
11404 int saved_pedantic
;
11406 /* Check for the `__extension__' keyword. */
11407 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
11409 /* Parse the qualified declaration. */
11410 cp_parser_block_declaration (parser
, statement_p
);
11411 /* Restore the PEDANTIC flag. */
11412 pedantic
= saved_pedantic
;
11417 /* Peek at the next token to figure out which kind of declaration is
11419 token1
= cp_lexer_peek_token (parser
->lexer
);
11421 /* If the next keyword is `asm', we have an asm-definition. */
11422 if (token1
->keyword
== RID_ASM
)
11425 cp_parser_commit_to_tentative_parse (parser
);
11426 cp_parser_asm_definition (parser
);
11428 /* If the next keyword is `namespace', we have a
11429 namespace-alias-definition. */
11430 else if (token1
->keyword
== RID_NAMESPACE
)
11431 cp_parser_namespace_alias_definition (parser
);
11432 /* If the next keyword is `using', we have a
11433 using-declaration, a using-directive, or an alias-declaration. */
11434 else if (token1
->keyword
== RID_USING
)
11439 cp_parser_commit_to_tentative_parse (parser
);
11440 /* If the token after `using' is `namespace', then we have a
11441 using-directive. */
11442 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11443 if (token2
->keyword
== RID_NAMESPACE
)
11444 cp_parser_using_directive (parser
);
11445 /* If the second token after 'using' is '=', then we have an
11446 alias-declaration. */
11447 else if (cxx_dialect
>= cxx11
11448 && token2
->type
== CPP_NAME
11449 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
11450 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
11451 cp_parser_alias_declaration (parser
);
11452 /* Otherwise, it's a using-declaration. */
11454 cp_parser_using_declaration (parser
,
11455 /*access_declaration_p=*/false);
11457 /* If the next keyword is `__label__' we have a misplaced label
11459 else if (token1
->keyword
== RID_LABEL
)
11461 cp_lexer_consume_token (parser
->lexer
);
11462 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
11463 cp_parser_skip_to_end_of_statement (parser
);
11464 /* If the next token is now a `;', consume it. */
11465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11466 cp_lexer_consume_token (parser
->lexer
);
11468 /* If the next token is `static_assert' we have a static assertion. */
11469 else if (token1
->keyword
== RID_STATIC_ASSERT
)
11470 cp_parser_static_assert (parser
, /*member_p=*/false);
11471 /* Anything else must be a simple-declaration. */
11473 cp_parser_simple_declaration (parser
, !statement_p
,
11474 /*maybe_range_for_decl*/NULL
);
11477 /* Parse a simple-declaration.
11479 simple-declaration:
11480 decl-specifier-seq [opt] init-declarator-list [opt] ;
11482 init-declarator-list:
11484 init-declarator-list , init-declarator
11486 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11487 function-definition as a simple-declaration.
11489 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11490 parsed declaration if it is an uninitialized single declarator not followed
11491 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11492 if present, will not be consumed. */
11495 cp_parser_simple_declaration (cp_parser
* parser
,
11496 bool function_definition_allowed_p
,
11497 tree
*maybe_range_for_decl
)
11499 cp_decl_specifier_seq decl_specifiers
;
11500 int declares_class_or_enum
;
11501 bool saw_declarator
;
11502 location_t comma_loc
= UNKNOWN_LOCATION
;
11503 location_t init_loc
= UNKNOWN_LOCATION
;
11505 if (maybe_range_for_decl
)
11506 *maybe_range_for_decl
= NULL_TREE
;
11508 /* Defer access checks until we know what is being declared; the
11509 checks for names appearing in the decl-specifier-seq should be
11510 done as if we were in the scope of the thing being declared. */
11511 push_deferring_access_checks (dk_deferred
);
11513 /* Parse the decl-specifier-seq. We have to keep track of whether
11514 or not the decl-specifier-seq declares a named class or
11515 enumeration type, since that is the only case in which the
11516 init-declarator-list is allowed to be empty.
11520 In a simple-declaration, the optional init-declarator-list can be
11521 omitted only when declaring a class or enumeration, that is when
11522 the decl-specifier-seq contains either a class-specifier, an
11523 elaborated-type-specifier, or an enum-specifier. */
11524 cp_parser_decl_specifier_seq (parser
,
11525 CP_PARSER_FLAGS_OPTIONAL
,
11527 &declares_class_or_enum
);
11528 /* We no longer need to defer access checks. */
11529 stop_deferring_access_checks ();
11531 /* In a block scope, a valid declaration must always have a
11532 decl-specifier-seq. By not trying to parse declarators, we can
11533 resolve the declaration/expression ambiguity more quickly. */
11534 if (!function_definition_allowed_p
11535 && !decl_specifiers
.any_specifiers_p
)
11537 cp_parser_error (parser
, "expected declaration");
11541 /* If the next two tokens are both identifiers, the code is
11542 erroneous. The usual cause of this situation is code like:
11546 where "T" should name a type -- but does not. */
11547 if (!decl_specifiers
.any_type_specifiers_p
11548 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
11550 /* If parsing tentatively, we should commit; we really are
11551 looking at a declaration. */
11552 cp_parser_commit_to_tentative_parse (parser
);
11557 /* If we have seen at least one decl-specifier, and the next token
11558 is not a parenthesis, then we must be looking at a declaration.
11559 (After "int (" we might be looking at a functional cast.) */
11560 if (decl_specifiers
.any_specifiers_p
11561 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
11562 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
11563 && !cp_parser_error_occurred (parser
))
11564 cp_parser_commit_to_tentative_parse (parser
);
11566 /* Keep going until we hit the `;' at the end of the simple
11568 saw_declarator
= false;
11569 while (cp_lexer_next_token_is_not (parser
->lexer
,
11573 bool function_definition_p
;
11576 if (saw_declarator
)
11578 /* If we are processing next declarator, comma is expected */
11579 token
= cp_lexer_peek_token (parser
->lexer
);
11580 gcc_assert (token
->type
== CPP_COMMA
);
11581 cp_lexer_consume_token (parser
->lexer
);
11582 if (maybe_range_for_decl
)
11584 *maybe_range_for_decl
= error_mark_node
;
11585 if (comma_loc
== UNKNOWN_LOCATION
)
11586 comma_loc
= token
->location
;
11590 saw_declarator
= true;
11592 /* Parse the init-declarator. */
11593 decl
= cp_parser_init_declarator (parser
, &decl_specifiers
,
11595 function_definition_allowed_p
,
11596 /*member_p=*/false,
11597 declares_class_or_enum
,
11598 &function_definition_p
,
11599 maybe_range_for_decl
,
11601 /* If an error occurred while parsing tentatively, exit quickly.
11602 (That usually happens when in the body of a function; each
11603 statement is treated as a declaration-statement until proven
11605 if (cp_parser_error_occurred (parser
))
11607 /* Handle function definitions specially. */
11608 if (function_definition_p
)
11610 /* If the next token is a `,', then we are probably
11611 processing something like:
11615 which is erroneous. */
11616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
11618 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11619 error_at (token
->location
,
11621 " declarations and function-definitions is forbidden");
11623 /* Otherwise, we're done with the list of declarators. */
11626 pop_deferring_access_checks ();
11630 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
11631 *maybe_range_for_decl
= decl
;
11632 /* The next token should be either a `,' or a `;'. */
11633 token
= cp_lexer_peek_token (parser
->lexer
);
11634 /* If it's a `,', there are more declarators to come. */
11635 if (token
->type
== CPP_COMMA
)
11636 /* will be consumed next time around */;
11637 /* If it's a `;', we are done. */
11638 else if (token
->type
== CPP_SEMICOLON
|| maybe_range_for_decl
)
11640 /* Anything else is an error. */
11643 /* If we have already issued an error message we don't need
11644 to issue another one. */
11645 if (decl
!= error_mark_node
11646 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
11647 cp_parser_error (parser
, "expected %<,%> or %<;%>");
11648 /* Skip tokens until we reach the end of the statement. */
11649 cp_parser_skip_to_end_of_statement (parser
);
11650 /* If the next token is now a `;', consume it. */
11651 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
11652 cp_lexer_consume_token (parser
->lexer
);
11655 /* After the first time around, a function-definition is not
11656 allowed -- even if it was OK at first. For example:
11661 function_definition_allowed_p
= false;
11664 /* Issue an error message if no declarators are present, and the
11665 decl-specifier-seq does not itself declare a class or
11666 enumeration: [dcl.dcl]/3. */
11667 if (!saw_declarator
)
11669 if (cp_parser_declares_only_class_p (parser
))
11671 if (!declares_class_or_enum
11672 && decl_specifiers
.type
11673 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
11674 /* Ensure an error is issued anyway when finish_decltype_type,
11675 called via cp_parser_decl_specifier_seq, returns a class or
11676 an enumeration (c++/51786). */
11677 decl_specifiers
.type
= NULL_TREE
;
11678 shadow_tag (&decl_specifiers
);
11680 /* Perform any deferred access checks. */
11681 perform_deferred_access_checks (tf_warning_or_error
);
11684 /* Consume the `;'. */
11685 if (!maybe_range_for_decl
)
11686 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
11687 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
11689 if (init_loc
!= UNKNOWN_LOCATION
)
11690 error_at (init_loc
, "initializer in range-based %<for%> loop");
11691 if (comma_loc
!= UNKNOWN_LOCATION
)
11692 error_at (comma_loc
,
11693 "multiple declarations in range-based %<for%> loop");
11697 pop_deferring_access_checks ();
11700 /* Parse a decl-specifier-seq.
11702 decl-specifier-seq:
11703 decl-specifier-seq [opt] decl-specifier
11704 decl-specifier attribute-specifier-seq [opt] (C++11)
11707 storage-class-specifier
11718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11720 The parser flags FLAGS is used to control type-specifier parsing.
11722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11725 1: one of the decl-specifiers is an elaborated-type-specifier
11726 (i.e., a type declaration)
11727 2: one of the decl-specifiers is an enum-specifier or a
11728 class-specifier (i.e., a type definition)
11733 cp_parser_decl_specifier_seq (cp_parser
* parser
,
11734 cp_parser_flags flags
,
11735 cp_decl_specifier_seq
*decl_specs
,
11736 int* declares_class_or_enum
)
11738 bool constructor_possible_p
= !parser
->in_declarator_p
;
11739 bool found_decl_spec
= false;
11740 cp_token
*start_token
= NULL
;
11743 /* Clear DECL_SPECS. */
11744 clear_decl_specs (decl_specs
);
11746 /* Assume no class or enumeration type is declared. */
11747 *declares_class_or_enum
= 0;
11749 /* Keep reading specifiers until there are no more to read. */
11752 bool constructor_p
;
11756 /* Peek at the next token. */
11757 token
= cp_lexer_peek_token (parser
->lexer
);
11759 /* Save the first token of the decl spec list for error
11762 start_token
= token
;
11763 /* Handle attributes. */
11764 if (cp_next_tokens_can_be_attribute_p (parser
))
11766 /* Parse the attributes. */
11767 tree attrs
= cp_parser_attributes_opt (parser
);
11769 /* In a sequence of declaration specifiers, c++11 attributes
11770 appertain to the type that precede them. In that case
11773 The attribute-specifier-seq affects the type only for
11774 the declaration it appears in, not other declarations
11775 involving the same type.
11777 But for now let's force the user to position the
11778 attribute either at the beginning of the declaration or
11779 after the declarator-id, which would clearly mean that it
11780 applies to the declarator. */
11781 if (cxx11_attribute_p (attrs
))
11783 if (!found_decl_spec
)
11784 /* The c++11 attribute is at the beginning of the
11785 declaration. It appertains to the entity being
11789 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
11791 /* This is an attribute following a
11792 class-specifier. */
11793 if (decl_specs
->type_definition_p
)
11794 warn_misplaced_attr_for_class_type (token
->location
,
11800 decl_specs
->std_attributes
11801 = chainon (decl_specs
->std_attributes
,
11803 if (decl_specs
->locations
[ds_std_attribute
] == 0)
11804 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
11810 decl_specs
->attributes
11811 = chainon (decl_specs
->attributes
,
11813 if (decl_specs
->locations
[ds_attribute
] == 0)
11814 decl_specs
->locations
[ds_attribute
] = token
->location
;
11817 /* Assume we will find a decl-specifier keyword. */
11818 found_decl_spec
= true;
11819 /* If the next token is an appropriate keyword, we can simply
11820 add it to the list. */
11821 switch (token
->keyword
)
11827 if (!at_class_scope_p ())
11829 error_at (token
->location
, "%<friend%> used outside of class");
11830 cp_lexer_purge_token (parser
->lexer
);
11835 /* Consume the token. */
11836 cp_lexer_consume_token (parser
->lexer
);
11840 case RID_CONSTEXPR
:
11842 cp_lexer_consume_token (parser
->lexer
);
11845 /* function-specifier:
11852 cp_parser_function_specifier_opt (parser
, decl_specs
);
11859 /* Consume the token. */
11860 cp_lexer_consume_token (parser
->lexer
);
11861 /* A constructor declarator cannot appear in a typedef. */
11862 constructor_possible_p
= false;
11863 /* The "typedef" keyword can only occur in a declaration; we
11864 may as well commit at this point. */
11865 cp_parser_commit_to_tentative_parse (parser
);
11867 if (decl_specs
->storage_class
!= sc_none
)
11868 decl_specs
->conflicting_specifiers_p
= true;
11871 /* storage-class-specifier:
11881 if (cxx_dialect
== cxx98
)
11883 /* Consume the token. */
11884 cp_lexer_consume_token (parser
->lexer
);
11886 /* Complain about `auto' as a storage specifier, if
11887 we're complaining about C++0x compatibility. */
11888 warning_at (token
->location
, OPT_Wc__0x_compat
, "%<auto%>"
11889 " changes meaning in C++11; please remove it");
11891 /* Set the storage class anyway. */
11892 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
11896 /* C++0x auto type-specifier. */
11897 found_decl_spec
= false;
11904 /* Consume the token. */
11905 cp_lexer_consume_token (parser
->lexer
);
11906 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
11910 /* Consume the token. */
11912 cp_lexer_consume_token (parser
->lexer
);
11916 /* We did not yet find a decl-specifier yet. */
11917 found_decl_spec
= false;
11921 if (found_decl_spec
11922 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
11923 && token
->keyword
!= RID_CONSTEXPR
)
11924 error ("decl-specifier invalid in condition");
11927 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
11929 /* Constructors are a special case. The `S' in `S()' is not a
11930 decl-specifier; it is the beginning of the declarator. */
11932 = (!found_decl_spec
11933 && constructor_possible_p
11934 && (cp_parser_constructor_declarator_p
11935 (parser
, decl_spec_seq_has_spec_p (decl_specs
, ds_friend
))));
11937 /* If we don't have a DECL_SPEC yet, then we must be looking at
11938 a type-specifier. */
11939 if (!found_decl_spec
&& !constructor_p
)
11941 int decl_spec_declares_class_or_enum
;
11942 bool is_cv_qualifier
;
11946 = cp_parser_type_specifier (parser
, flags
,
11948 /*is_declaration=*/true,
11949 &decl_spec_declares_class_or_enum
,
11951 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
11953 /* If this type-specifier referenced a user-defined type
11954 (a typedef, class-name, etc.), then we can't allow any
11955 more such type-specifiers henceforth.
11959 The longest sequence of decl-specifiers that could
11960 possibly be a type name is taken as the
11961 decl-specifier-seq of a declaration. The sequence shall
11962 be self-consistent as described below.
11966 As a general rule, at most one type-specifier is allowed
11967 in the complete decl-specifier-seq of a declaration. The
11968 only exceptions are the following:
11970 -- const or volatile can be combined with any other
11973 -- signed or unsigned can be combined with char, long,
11981 void g (const int Pc);
11983 Here, Pc is *not* part of the decl-specifier seq; it's
11984 the declarator. Therefore, once we see a type-specifier
11985 (other than a cv-qualifier), we forbid any additional
11986 user-defined types. We *do* still allow things like `int
11987 int' to be considered a decl-specifier-seq, and issue the
11988 error message later. */
11989 if (type_spec
&& !is_cv_qualifier
)
11990 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
11991 /* A constructor declarator cannot follow a type-specifier. */
11994 constructor_possible_p
= false;
11995 found_decl_spec
= true;
11996 if (!is_cv_qualifier
)
11997 decl_specs
->any_type_specifiers_p
= true;
12001 /* If we still do not have a DECL_SPEC, then there are no more
12002 decl-specifiers. */
12003 if (!found_decl_spec
)
12006 decl_specs
->any_specifiers_p
= true;
12007 /* After we see one decl-specifier, further decl-specifiers are
12008 always optional. */
12009 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
12012 /* Don't allow a friend specifier with a class definition. */
12013 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
12014 && (*declares_class_or_enum
& 2))
12015 error_at (decl_specs
->locations
[ds_friend
],
12016 "class definition may not be declared a friend");
12019 /* Parse an (optional) storage-class-specifier.
12021 storage-class-specifier:
12030 storage-class-specifier:
12033 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12036 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
12038 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
12041 if (cxx_dialect
!= cxx98
)
12043 /* Fall through for C++98. */
12050 /* Consume the token. */
12051 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12058 /* Parse an (optional) function-specifier.
12060 function-specifier:
12065 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12066 Updates DECL_SPECS, if it is non-NULL. */
12069 cp_parser_function_specifier_opt (cp_parser
* parser
,
12070 cp_decl_specifier_seq
*decl_specs
)
12072 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12073 switch (token
->keyword
)
12076 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
12080 /* 14.5.2.3 [temp.mem]
12082 A member function template shall not be virtual. */
12083 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12084 error_at (token
->location
, "templates may not be %<virtual%>");
12086 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
12090 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
12097 /* Consume the token. */
12098 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
12101 /* Parse a linkage-specification.
12103 linkage-specification:
12104 extern string-literal { declaration-seq [opt] }
12105 extern string-literal declaration */
12108 cp_parser_linkage_specification (cp_parser
* parser
)
12112 /* Look for the `extern' keyword. */
12113 cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
12115 /* Look for the string-literal. */
12116 linkage
= cp_parser_string_literal (parser
, false, false);
12118 /* Transform the literal into an identifier. If the literal is a
12119 wide-character string, or contains embedded NULs, then we can't
12120 handle it as the user wants. */
12121 if (strlen (TREE_STRING_POINTER (linkage
))
12122 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
12124 cp_parser_error (parser
, "invalid linkage-specification");
12125 /* Assume C++ linkage. */
12126 linkage
= lang_name_cplusplus
;
12129 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
12131 /* We're now using the new linkage. */
12132 push_lang_context (linkage
);
12134 /* If the next token is a `{', then we're using the first
12136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12138 cp_ensure_no_omp_declare_simd (parser
);
12140 /* Consume the `{' token. */
12141 cp_lexer_consume_token (parser
->lexer
);
12142 /* Parse the declarations. */
12143 cp_parser_declaration_seq_opt (parser
);
12144 /* Look for the closing `}'. */
12145 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
12147 /* Otherwise, there's just one declaration. */
12150 bool saved_in_unbraced_linkage_specification_p
;
12152 saved_in_unbraced_linkage_specification_p
12153 = parser
->in_unbraced_linkage_specification_p
;
12154 parser
->in_unbraced_linkage_specification_p
= true;
12155 cp_parser_declaration (parser
);
12156 parser
->in_unbraced_linkage_specification_p
12157 = saved_in_unbraced_linkage_specification_p
;
12160 /* We're done with the linkage-specification. */
12161 pop_lang_context ();
12164 /* Parse a static_assert-declaration.
12166 static_assert-declaration:
12167 static_assert ( constant-expression , string-literal ) ;
12169 If MEMBER_P, this static_assert is a class member. */
12172 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
12177 location_t saved_loc
;
12180 /* Peek at the `static_assert' token so we can keep track of exactly
12181 where the static assertion started. */
12182 token
= cp_lexer_peek_token (parser
->lexer
);
12183 saved_loc
= token
->location
;
12185 /* Look for the `static_assert' keyword. */
12186 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
12190 /* We know we are in a static assertion; commit to any tentative
12192 if (cp_parser_parsing_tentatively (parser
))
12193 cp_parser_commit_to_tentative_parse (parser
);
12195 /* Parse the `(' starting the static assertion condition. */
12196 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
12198 /* Parse the constant-expression. Allow a non-constant expression
12199 here in order to give better diagnostics in finish_static_assert. */
12201 cp_parser_constant_expression (parser
,
12202 /*allow_non_constant_p=*/true,
12203 /*non_constant_p=*/&dummy
);
12205 /* Parse the separating `,'. */
12206 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
12208 /* Parse the string-literal message. */
12209 message
= cp_parser_string_literal (parser
,
12210 /*translate=*/false,
12213 /* A `)' completes the static assertion. */
12214 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12215 cp_parser_skip_to_closing_parenthesis (parser
,
12216 /*recovering=*/true,
12217 /*or_comma=*/false,
12218 /*consume_paren=*/true);
12220 /* A semicolon terminates the declaration. */
12221 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12223 /* Complete the static assertion, which may mean either processing
12224 the static assert now or saving it for template instantiation. */
12225 finish_static_assert (condition
, message
, saved_loc
, member_p
);
12228 /* Parse the expression in decltype ( expression ). */
12231 cp_parser_decltype_expr (cp_parser
*parser
,
12232 bool &id_expression_or_member_access_p
)
12234 cp_token
*id_expr_start_token
;
12237 /* First, try parsing an id-expression. */
12238 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
12239 cp_parser_parse_tentatively (parser
);
12240 expr
= cp_parser_id_expression (parser
,
12241 /*template_keyword_p=*/false,
12242 /*check_dependency_p=*/true,
12243 /*template_p=*/NULL
,
12244 /*declarator_p=*/false,
12245 /*optional_p=*/false);
12247 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
12249 bool non_integral_constant_expression_p
= false;
12250 tree id_expression
= expr
;
12252 const char *error_msg
;
12254 if (identifier_p (expr
))
12255 /* Lookup the name we got back from the id-expression. */
12256 expr
= cp_parser_lookup_name_simple (parser
, expr
,
12257 id_expr_start_token
->location
);
12260 && expr
!= error_mark_node
12261 && TREE_CODE (expr
) != TYPE_DECL
12262 && (TREE_CODE (expr
) != BIT_NOT_EXPR
12263 || !TYPE_P (TREE_OPERAND (expr
, 0)))
12264 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12266 /* Complete lookup of the id-expression. */
12267 expr
= (finish_id_expression
12268 (id_expression
, expr
, parser
->scope
, &idk
,
12269 /*integral_constant_expression_p=*/false,
12270 /*allow_non_integral_constant_expression_p=*/true,
12271 &non_integral_constant_expression_p
,
12272 /*template_p=*/false,
12274 /*address_p=*/false,
12275 /*template_arg_p=*/false,
12277 id_expr_start_token
->location
));
12279 if (expr
== error_mark_node
)
12280 /* We found an id-expression, but it was something that we
12281 should not have found. This is an error, not something
12282 we can recover from, so note that we found an
12283 id-expression and we'll recover as gracefully as
12285 id_expression_or_member_access_p
= true;
12289 && expr
!= error_mark_node
12290 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12291 /* We have an id-expression. */
12292 id_expression_or_member_access_p
= true;
12295 if (!id_expression_or_member_access_p
)
12297 /* Abort the id-expression parse. */
12298 cp_parser_abort_tentative_parse (parser
);
12300 /* Parsing tentatively, again. */
12301 cp_parser_parse_tentatively (parser
);
12303 /* Parse a class member access. */
12304 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
12305 /*cast_p=*/false, /*decltype*/true,
12306 /*member_access_only_p=*/true, NULL
);
12309 && expr
!= error_mark_node
12310 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
12311 /* We have an id-expression. */
12312 id_expression_or_member_access_p
= true;
12315 if (id_expression_or_member_access_p
)
12316 /* We have parsed the complete id-expression or member access. */
12317 cp_parser_parse_definitely (parser
);
12320 /* Abort our attempt to parse an id-expression or member access
12322 cp_parser_abort_tentative_parse (parser
);
12324 /* Parse a full expression. */
12325 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
12326 /*decltype_p=*/true);
12332 /* Parse a `decltype' type. Returns the type.
12334 simple-type-specifier:
12335 decltype ( expression )
12337 decltype ( auto ) */
12340 cp_parser_decltype (cp_parser
*parser
)
12343 bool id_expression_or_member_access_p
= false;
12344 const char *saved_message
;
12345 bool saved_integral_constant_expression_p
;
12346 bool saved_non_integral_constant_expression_p
;
12347 bool saved_greater_than_is_operator_p
;
12348 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
12350 if (start_token
->type
== CPP_DECLTYPE
)
12352 /* Already parsed. */
12353 cp_lexer_consume_token (parser
->lexer
);
12354 return start_token
->u
.value
;
12357 /* Look for the `decltype' token. */
12358 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
12359 return error_mark_node
;
12361 /* Parse the opening `('. */
12362 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
12363 return error_mark_node
;
12365 /* decltype (auto) */
12366 if (cxx_dialect
>= cxx14
12367 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
12369 cp_lexer_consume_token (parser
->lexer
);
12370 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12371 return error_mark_node
;
12372 expr
= make_decltype_auto ();
12373 AUTO_IS_DECLTYPE (expr
) = true;
12377 /* Types cannot be defined in a `decltype' expression. Save away the
12379 saved_message
= parser
->type_definition_forbidden_message
;
12381 /* And create the new one. */
12382 parser
->type_definition_forbidden_message
12383 = G_("types may not be defined in %<decltype%> expressions");
12385 /* The restrictions on constant-expressions do not apply inside
12386 decltype expressions. */
12387 saved_integral_constant_expression_p
12388 = parser
->integral_constant_expression_p
;
12389 saved_non_integral_constant_expression_p
12390 = parser
->non_integral_constant_expression_p
;
12391 parser
->integral_constant_expression_p
= false;
12393 /* Within a parenthesized expression, a `>' token is always
12394 the greater-than operator. */
12395 saved_greater_than_is_operator_p
12396 = parser
->greater_than_is_operator_p
;
12397 parser
->greater_than_is_operator_p
= true;
12399 /* Do not actually evaluate the expression. */
12400 ++cp_unevaluated_operand
;
12402 /* Do not warn about problems with the expression. */
12403 ++c_inhibit_evaluation_warnings
;
12405 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
12407 /* Go back to evaluating expressions. */
12408 --cp_unevaluated_operand
;
12409 --c_inhibit_evaluation_warnings
;
12411 /* The `>' token might be the end of a template-id or
12412 template-parameter-list now. */
12413 parser
->greater_than_is_operator_p
12414 = saved_greater_than_is_operator_p
;
12416 /* Restore the old message and the integral constant expression
12418 parser
->type_definition_forbidden_message
= saved_message
;
12419 parser
->integral_constant_expression_p
12420 = saved_integral_constant_expression_p
;
12421 parser
->non_integral_constant_expression_p
12422 = saved_non_integral_constant_expression_p
;
12424 /* Parse to the closing `)'. */
12425 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
12427 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12428 /*consume_paren=*/true);
12429 return error_mark_node
;
12432 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
12433 tf_warning_or_error
);
12436 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12438 start_token
->type
= CPP_DECLTYPE
;
12439 start_token
->u
.value
= expr
;
12440 start_token
->keyword
= RID_MAX
;
12441 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
12446 /* Special member functions [gram.special] */
12448 /* Parse a conversion-function-id.
12450 conversion-function-id:
12451 operator conversion-type-id
12453 Returns an IDENTIFIER_NODE representing the operator. */
12456 cp_parser_conversion_function_id (cp_parser
* parser
)
12460 tree saved_qualifying_scope
;
12461 tree saved_object_scope
;
12462 tree pushed_scope
= NULL_TREE
;
12464 /* Look for the `operator' token. */
12465 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12466 return error_mark_node
;
12467 /* When we parse the conversion-type-id, the current scope will be
12468 reset. However, we need that information in able to look up the
12469 conversion function later, so we save it here. */
12470 saved_scope
= parser
->scope
;
12471 saved_qualifying_scope
= parser
->qualifying_scope
;
12472 saved_object_scope
= parser
->object_scope
;
12473 /* We must enter the scope of the class so that the names of
12474 entities declared within the class are available in the
12475 conversion-type-id. For example, consider:
12482 S::operator I() { ... }
12484 In order to see that `I' is a type-name in the definition, we
12485 must be in the scope of `S'. */
12487 pushed_scope
= push_scope (saved_scope
);
12488 /* Parse the conversion-type-id. */
12489 type
= cp_parser_conversion_type_id (parser
);
12490 /* Leave the scope of the class, if any. */
12492 pop_scope (pushed_scope
);
12493 /* Restore the saved scope. */
12494 parser
->scope
= saved_scope
;
12495 parser
->qualifying_scope
= saved_qualifying_scope
;
12496 parser
->object_scope
= saved_object_scope
;
12497 /* If the TYPE is invalid, indicate failure. */
12498 if (type
== error_mark_node
)
12499 return error_mark_node
;
12500 return mangle_conv_op_name_for_type (type
);
12503 /* Parse a conversion-type-id:
12505 conversion-type-id:
12506 type-specifier-seq conversion-declarator [opt]
12508 Returns the TYPE specified. */
12511 cp_parser_conversion_type_id (cp_parser
* parser
)
12514 cp_decl_specifier_seq type_specifiers
;
12515 cp_declarator
*declarator
;
12516 tree type_specified
;
12517 const char *saved_message
;
12519 /* Parse the attributes. */
12520 attributes
= cp_parser_attributes_opt (parser
);
12522 saved_message
= parser
->type_definition_forbidden_message
;
12523 parser
->type_definition_forbidden_message
12524 = G_("types may not be defined in a conversion-type-id");
12526 /* Parse the type-specifiers. */
12527 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
12528 /*is_trailing_return=*/false,
12531 parser
->type_definition_forbidden_message
= saved_message
;
12533 /* If that didn't work, stop. */
12534 if (type_specifiers
.type
== error_mark_node
)
12535 return error_mark_node
;
12536 /* Parse the conversion-declarator. */
12537 declarator
= cp_parser_conversion_declarator_opt (parser
);
12539 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
12540 /*initialized=*/0, &attributes
);
12542 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
12544 /* Don't give this error when parsing tentatively. This happens to
12545 work because we always parse this definitively once. */
12546 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
12547 && type_uses_auto (type_specified
))
12549 if (cxx_dialect
< cxx14
)
12551 error ("invalid use of %<auto%> in conversion operator");
12552 return error_mark_node
;
12554 else if (template_parm_scope_p ())
12555 warning (0, "use of %<auto%> in member template "
12556 "conversion operator can never be deduced");
12559 return type_specified
;
12562 /* Parse an (optional) conversion-declarator.
12564 conversion-declarator:
12565 ptr-operator conversion-declarator [opt]
12569 static cp_declarator
*
12570 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
12572 enum tree_code code
;
12573 tree class_type
, std_attributes
= NULL_TREE
;
12574 cp_cv_quals cv_quals
;
12576 /* We don't know if there's a ptr-operator next, or not. */
12577 cp_parser_parse_tentatively (parser
);
12578 /* Try the ptr-operator. */
12579 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
12581 /* If it worked, look for more conversion-declarators. */
12582 if (cp_parser_parse_definitely (parser
))
12584 cp_declarator
*declarator
;
12586 /* Parse another optional declarator. */
12587 declarator
= cp_parser_conversion_declarator_opt (parser
);
12589 declarator
= cp_parser_make_indirect_declarator
12590 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
12598 /* Parse an (optional) ctor-initializer.
12601 : mem-initializer-list
12603 Returns TRUE iff the ctor-initializer was actually present. */
12606 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
12608 /* If the next token is not a `:', then there is no
12609 ctor-initializer. */
12610 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
12612 /* Do default initialization of any bases and members. */
12613 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12614 finish_mem_initializers (NULL_TREE
);
12619 /* Consume the `:' token. */
12620 cp_lexer_consume_token (parser
->lexer
);
12621 /* And the mem-initializer-list. */
12622 cp_parser_mem_initializer_list (parser
);
12627 /* Parse a mem-initializer-list.
12629 mem-initializer-list:
12630 mem-initializer ... [opt]
12631 mem-initializer ... [opt] , mem-initializer-list */
12634 cp_parser_mem_initializer_list (cp_parser
* parser
)
12636 tree mem_initializer_list
= NULL_TREE
;
12637 tree target_ctor
= error_mark_node
;
12638 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12640 /* Let the semantic analysis code know that we are starting the
12641 mem-initializer-list. */
12642 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
12643 error_at (token
->location
,
12644 "only constructors take member initializers");
12646 /* Loop through the list. */
12649 tree mem_initializer
;
12651 token
= cp_lexer_peek_token (parser
->lexer
);
12652 /* Parse the mem-initializer. */
12653 mem_initializer
= cp_parser_mem_initializer (parser
);
12654 /* If the next token is a `...', we're expanding member initializers. */
12655 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
12657 /* Consume the `...'. */
12658 cp_lexer_consume_token (parser
->lexer
);
12660 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12661 can be expanded but members cannot. */
12662 if (mem_initializer
!= error_mark_node
12663 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
12665 error_at (token
->location
,
12666 "cannot expand initializer for member %<%D%>",
12667 TREE_PURPOSE (mem_initializer
));
12668 mem_initializer
= error_mark_node
;
12671 /* Construct the pack expansion type. */
12672 if (mem_initializer
!= error_mark_node
)
12673 mem_initializer
= make_pack_expansion (mem_initializer
);
12675 if (target_ctor
!= error_mark_node
12676 && mem_initializer
!= error_mark_node
)
12678 error ("mem-initializer for %qD follows constructor delegation",
12679 TREE_PURPOSE (mem_initializer
));
12680 mem_initializer
= error_mark_node
;
12682 /* Look for a target constructor. */
12683 if (mem_initializer
!= error_mark_node
12684 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
12685 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
12687 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
12688 if (mem_initializer_list
)
12690 error ("constructor delegation follows mem-initializer for %qD",
12691 TREE_PURPOSE (mem_initializer_list
));
12692 mem_initializer
= error_mark_node
;
12694 target_ctor
= mem_initializer
;
12696 /* Add it to the list, unless it was erroneous. */
12697 if (mem_initializer
!= error_mark_node
)
12699 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
12700 mem_initializer_list
= mem_initializer
;
12702 /* If the next token is not a `,', we're done. */
12703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
12705 /* Consume the `,' token. */
12706 cp_lexer_consume_token (parser
->lexer
);
12709 /* Perform semantic analysis. */
12710 if (DECL_CONSTRUCTOR_P (current_function_decl
))
12711 finish_mem_initializers (mem_initializer_list
);
12714 /* Parse a mem-initializer.
12717 mem-initializer-id ( expression-list [opt] )
12718 mem-initializer-id braced-init-list
12723 ( expression-list [opt] )
12725 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12726 class) or FIELD_DECL (for a non-static data member) to initialize;
12727 the TREE_VALUE is the expression-list. An empty initialization
12728 list is represented by void_list_node. */
12731 cp_parser_mem_initializer (cp_parser
* parser
)
12733 tree mem_initializer_id
;
12734 tree expression_list
;
12736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12738 /* Find out what is being initialized. */
12739 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
12741 permerror (token
->location
,
12742 "anachronistic old-style base class initializer");
12743 mem_initializer_id
= NULL_TREE
;
12747 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
12748 if (mem_initializer_id
== error_mark_node
)
12749 return mem_initializer_id
;
12751 member
= expand_member_init (mem_initializer_id
);
12752 if (member
&& !DECL_P (member
))
12753 in_base_initializer
= 1;
12755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12757 bool expr_non_constant_p
;
12758 cp_lexer_set_source_position (parser
->lexer
);
12759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12760 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12761 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
12762 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
12766 vec
<tree
, va_gc
> *vec
;
12767 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
12769 /*allow_expansion_p=*/true,
12770 /*non_constant_p=*/NULL
);
12772 return error_mark_node
;
12773 expression_list
= build_tree_list_vec (vec
);
12774 release_tree_vector (vec
);
12777 if (expression_list
== error_mark_node
)
12778 return error_mark_node
;
12779 if (!expression_list
)
12780 expression_list
= void_type_node
;
12782 in_base_initializer
= 0;
12784 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
12787 /* Parse a mem-initializer-id.
12789 mem-initializer-id:
12790 :: [opt] nested-name-specifier [opt] class-name
12793 Returns a TYPE indicating the class to be initializer for the first
12794 production. Returns an IDENTIFIER_NODE indicating the data member
12795 to be initialized for the second production. */
12798 cp_parser_mem_initializer_id (cp_parser
* parser
)
12800 bool global_scope_p
;
12801 bool nested_name_specifier_p
;
12802 bool template_p
= false;
12805 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12807 /* `typename' is not allowed in this context ([temp.res]). */
12808 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
12810 error_at (token
->location
,
12811 "keyword %<typename%> not allowed in this context (a qualified "
12812 "member initializer is implicitly a type)");
12813 cp_lexer_consume_token (parser
->lexer
);
12815 /* Look for the optional `::' operator. */
12817 = (cp_parser_global_scope_opt (parser
,
12818 /*current_scope_valid_p=*/false)
12820 /* Look for the optional nested-name-specifier. The simplest way to
12825 The keyword `typename' is not permitted in a base-specifier or
12826 mem-initializer; in these contexts a qualified name that
12827 depends on a template-parameter is implicitly assumed to be a
12830 is to assume that we have seen the `typename' keyword at this
12832 nested_name_specifier_p
12833 = (cp_parser_nested_name_specifier_opt (parser
,
12834 /*typename_keyword_p=*/true,
12835 /*check_dependency_p=*/true,
12837 /*is_declaration=*/true)
12839 if (nested_name_specifier_p
)
12840 template_p
= cp_parser_optional_template_keyword (parser
);
12841 /* If there is a `::' operator or a nested-name-specifier, then we
12842 are definitely looking for a class-name. */
12843 if (global_scope_p
|| nested_name_specifier_p
)
12844 return cp_parser_class_name (parser
,
12845 /*typename_keyword_p=*/true,
12846 /*template_keyword_p=*/template_p
,
12848 /*check_dependency_p=*/true,
12849 /*class_head_p=*/false,
12850 /*is_declaration=*/true);
12851 /* Otherwise, we could also be looking for an ordinary identifier. */
12852 cp_parser_parse_tentatively (parser
);
12853 /* Try a class-name. */
12854 id
= cp_parser_class_name (parser
,
12855 /*typename_keyword_p=*/true,
12856 /*template_keyword_p=*/false,
12858 /*check_dependency_p=*/true,
12859 /*class_head_p=*/false,
12860 /*is_declaration=*/true);
12861 /* If we found one, we're done. */
12862 if (cp_parser_parse_definitely (parser
))
12864 /* Otherwise, look for an ordinary identifier. */
12865 return cp_parser_identifier (parser
);
12868 /* Overloading [gram.over] */
12870 /* Parse an operator-function-id.
12872 operator-function-id:
12875 Returns an IDENTIFIER_NODE for the operator which is a
12876 human-readable spelling of the identifier, e.g., `operator +'. */
12879 cp_parser_operator_function_id (cp_parser
* parser
)
12881 /* Look for the `operator' keyword. */
12882 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
12883 return error_mark_node
;
12884 /* And then the name of the operator itself. */
12885 return cp_parser_operator (parser
);
12888 /* Return an identifier node for a user-defined literal operator.
12889 The suffix identifier is chained to the operator name identifier. */
12892 cp_literal_operator_id (const char* name
)
12895 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
12896 + strlen (name
) + 10);
12897 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
12898 identifier
= get_identifier (buffer
);
12903 /* Parse an operator.
12906 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12907 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12908 || ++ -- , ->* -> () []
12915 Returns an IDENTIFIER_NODE for the operator which is a
12916 human-readable spelling of the identifier, e.g., `operator +'. */
12919 cp_parser_operator (cp_parser
* parser
)
12921 tree id
= NULL_TREE
;
12925 /* Peek at the next token. */
12926 token
= cp_lexer_peek_token (parser
->lexer
);
12927 /* Figure out which operator we have. */
12928 switch (token
->type
)
12934 /* The keyword should be either `new' or `delete'. */
12935 if (token
->keyword
== RID_NEW
)
12937 else if (token
->keyword
== RID_DELETE
)
12942 /* Consume the `new' or `delete' token. */
12943 cp_lexer_consume_token (parser
->lexer
);
12945 /* Peek at the next token. */
12946 token
= cp_lexer_peek_token (parser
->lexer
);
12947 /* If it's a `[' token then this is the array variant of the
12949 if (token
->type
== CPP_OPEN_SQUARE
)
12951 /* Consume the `[' token. */
12952 cp_lexer_consume_token (parser
->lexer
);
12953 /* Look for the `]' token. */
12954 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
12955 id
= ansi_opname (op
== NEW_EXPR
12956 ? VEC_NEW_EXPR
: VEC_DELETE_EXPR
);
12958 /* Otherwise, we have the non-array variant. */
12960 id
= ansi_opname (op
);
12966 id
= ansi_opname (PLUS_EXPR
);
12970 id
= ansi_opname (MINUS_EXPR
);
12974 id
= ansi_opname (MULT_EXPR
);
12978 id
= ansi_opname (TRUNC_DIV_EXPR
);
12982 id
= ansi_opname (TRUNC_MOD_EXPR
);
12986 id
= ansi_opname (BIT_XOR_EXPR
);
12990 id
= ansi_opname (BIT_AND_EXPR
);
12994 id
= ansi_opname (BIT_IOR_EXPR
);
12998 id
= ansi_opname (BIT_NOT_EXPR
);
13002 id
= ansi_opname (TRUTH_NOT_EXPR
);
13006 id
= ansi_assopname (NOP_EXPR
);
13010 id
= ansi_opname (LT_EXPR
);
13014 id
= ansi_opname (GT_EXPR
);
13018 id
= ansi_assopname (PLUS_EXPR
);
13022 id
= ansi_assopname (MINUS_EXPR
);
13026 id
= ansi_assopname (MULT_EXPR
);
13030 id
= ansi_assopname (TRUNC_DIV_EXPR
);
13034 id
= ansi_assopname (TRUNC_MOD_EXPR
);
13038 id
= ansi_assopname (BIT_XOR_EXPR
);
13042 id
= ansi_assopname (BIT_AND_EXPR
);
13046 id
= ansi_assopname (BIT_IOR_EXPR
);
13050 id
= ansi_opname (LSHIFT_EXPR
);
13054 id
= ansi_opname (RSHIFT_EXPR
);
13057 case CPP_LSHIFT_EQ
:
13058 id
= ansi_assopname (LSHIFT_EXPR
);
13061 case CPP_RSHIFT_EQ
:
13062 id
= ansi_assopname (RSHIFT_EXPR
);
13066 id
= ansi_opname (EQ_EXPR
);
13070 id
= ansi_opname (NE_EXPR
);
13074 id
= ansi_opname (LE_EXPR
);
13077 case CPP_GREATER_EQ
:
13078 id
= ansi_opname (GE_EXPR
);
13082 id
= ansi_opname (TRUTH_ANDIF_EXPR
);
13086 id
= ansi_opname (TRUTH_ORIF_EXPR
);
13089 case CPP_PLUS_PLUS
:
13090 id
= ansi_opname (POSTINCREMENT_EXPR
);
13093 case CPP_MINUS_MINUS
:
13094 id
= ansi_opname (PREDECREMENT_EXPR
);
13098 id
= ansi_opname (COMPOUND_EXPR
);
13101 case CPP_DEREF_STAR
:
13102 id
= ansi_opname (MEMBER_REF
);
13106 id
= ansi_opname (COMPONENT_REF
);
13109 case CPP_OPEN_PAREN
:
13110 /* Consume the `('. */
13111 cp_lexer_consume_token (parser
->lexer
);
13112 /* Look for the matching `)'. */
13113 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
13114 return ansi_opname (CALL_EXPR
);
13116 case CPP_OPEN_SQUARE
:
13117 /* Consume the `['. */
13118 cp_lexer_consume_token (parser
->lexer
);
13119 /* Look for the matching `]'. */
13120 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
13121 return ansi_opname (ARRAY_REF
);
13123 case CPP_UTF8STRING
:
13124 case CPP_UTF8STRING_USERDEF
:
13130 case CPP_STRING_USERDEF
:
13131 case CPP_WSTRING_USERDEF
:
13132 case CPP_STRING16_USERDEF
:
13133 case CPP_STRING32_USERDEF
:
13135 tree str
, string_tree
;
13138 if (cxx_dialect
== cxx98
)
13139 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
13141 /* Consume the string. */
13142 str
= cp_parser_string_literal (parser
, /*translate=*/true,
13143 /*wide_ok=*/true, /*lookup_udlit=*/false);
13144 if (str
== error_mark_node
)
13145 return error_mark_node
;
13146 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
13148 string_tree
= USERDEF_LITERAL_VALUE (str
);
13149 id
= USERDEF_LITERAL_SUFFIX_ID (str
);
13154 /* Look for the suffix identifier. */
13155 token
= cp_lexer_peek_token (parser
->lexer
);
13156 if (token
->type
== CPP_NAME
)
13157 id
= cp_parser_identifier (parser
);
13158 else if (token
->type
== CPP_KEYWORD
)
13160 error ("unexpected keyword;"
13161 " remove space between quotes and suffix identifier");
13162 return error_mark_node
;
13166 error ("expected suffix identifier");
13167 return error_mark_node
;
13170 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13171 (TREE_TYPE (TREE_TYPE (string_tree
))));
13172 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
13175 error ("expected empty string after %<operator%> keyword");
13176 return error_mark_node
;
13178 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
13181 error ("invalid encoding prefix in literal operator");
13182 return error_mark_node
;
13184 if (id
!= error_mark_node
)
13186 const char *name
= IDENTIFIER_POINTER (id
);
13187 id
= cp_literal_operator_id (name
);
13193 /* Anything else is an error. */
13197 /* If we have selected an identifier, we need to consume the
13200 cp_lexer_consume_token (parser
->lexer
);
13201 /* Otherwise, no valid operator name was present. */
13204 cp_parser_error (parser
, "expected operator");
13205 id
= error_mark_node
;
13211 /* Parse a template-declaration.
13213 template-declaration:
13214 export [opt] template < template-parameter-list > declaration
13216 If MEMBER_P is TRUE, this template-declaration occurs within a
13219 The grammar rule given by the standard isn't correct. What
13220 is really meant is:
13222 template-declaration:
13223 export [opt] template-parameter-list-seq
13224 decl-specifier-seq [opt] init-declarator [opt] ;
13225 export [opt] template-parameter-list-seq
13226 function-definition
13228 template-parameter-list-seq:
13229 template-parameter-list-seq [opt]
13230 template < template-parameter-list > */
13233 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
13235 /* Check for `export'. */
13236 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
13238 /* Consume the `export' token. */
13239 cp_lexer_consume_token (parser
->lexer
);
13240 /* Warn that we do not support `export'. */
13241 warning (0, "keyword %<export%> not implemented, and will be ignored");
13244 cp_parser_template_declaration_after_export (parser
, member_p
);
13247 /* Parse a template-parameter-list.
13249 template-parameter-list:
13251 template-parameter-list , template-parameter
13253 Returns a TREE_LIST. Each node represents a template parameter.
13254 The nodes are connected via their TREE_CHAINs. */
13257 cp_parser_template_parameter_list (cp_parser
* parser
)
13259 tree parameter_list
= NULL_TREE
;
13261 begin_template_parm_list ();
13263 /* The loop below parses the template parms. We first need to know
13264 the total number of template parms to be able to compute proper
13265 canonical types of each dependent type. So after the loop, when
13266 we know the total number of template parms,
13267 end_template_parm_list computes the proper canonical types and
13268 fixes up the dependent types accordingly. */
13273 bool is_parameter_pack
;
13274 location_t parm_loc
;
13276 /* Parse the template-parameter. */
13277 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13278 parameter
= cp_parser_template_parameter (parser
,
13280 &is_parameter_pack
);
13281 /* Add it to the list. */
13282 if (parameter
!= error_mark_node
)
13283 parameter_list
= process_template_parm (parameter_list
,
13287 is_parameter_pack
);
13290 tree err_parm
= build_tree_list (parameter
, parameter
);
13291 parameter_list
= chainon (parameter_list
, err_parm
);
13294 /* If the next token is not a `,', we're done. */
13295 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13297 /* Otherwise, consume the `,' token. */
13298 cp_lexer_consume_token (parser
->lexer
);
13301 return end_template_parm_list (parameter_list
);
13304 /* Parse a template-parameter.
13306 template-parameter:
13308 parameter-declaration
13310 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13311 the parameter. The TREE_PURPOSE is the default value, if any.
13312 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13313 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13314 set to true iff this parameter is a parameter pack. */
13317 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
13318 bool *is_parameter_pack
)
13321 cp_parameter_declarator
*parameter_declarator
;
13322 cp_declarator
*id_declarator
;
13325 /* Assume it is a type parameter or a template parameter. */
13326 *is_non_type
= false;
13327 /* Assume it not a parameter pack. */
13328 *is_parameter_pack
= false;
13329 /* Peek at the next token. */
13330 token
= cp_lexer_peek_token (parser
->lexer
);
13331 /* If it is `class' or `template', we have a type-parameter. */
13332 if (token
->keyword
== RID_TEMPLATE
)
13333 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13334 /* If it is `class' or `typename' we do not know yet whether it is a
13335 type parameter or a non-type parameter. Consider:
13337 template <typename T, typename T::X X> ...
13341 template <class C, class D*> ...
13343 Here, the first parameter is a type parameter, and the second is
13344 a non-type parameter. We can tell by looking at the token after
13345 the identifier -- if it is a `,', `=', or `>' then we have a type
13347 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
13349 /* Peek at the token after `class' or `typename'. */
13350 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13351 /* If it's an ellipsis, we have a template type parameter
13353 if (token
->type
== CPP_ELLIPSIS
)
13354 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13355 /* If it's an identifier, skip it. */
13356 if (token
->type
== CPP_NAME
)
13357 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
13358 /* Now, see if the token looks like the end of a template
13360 if (token
->type
== CPP_COMMA
13361 || token
->type
== CPP_EQ
13362 || token
->type
== CPP_GREATER
)
13363 return cp_parser_type_parameter (parser
, is_parameter_pack
);
13366 /* Otherwise, it is a non-type parameter.
13370 When parsing a default template-argument for a non-type
13371 template-parameter, the first non-nested `>' is taken as the end
13372 of the template parameter-list rather than a greater-than
13374 *is_non_type
= true;
13375 parameter_declarator
13376 = cp_parser_parameter_declaration (parser
, /*template_parm_p=*/true,
13377 /*parenthesized_p=*/NULL
);
13379 if (!parameter_declarator
)
13380 return error_mark_node
;
13382 /* If the parameter declaration is marked as a parameter pack, set
13383 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13384 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13386 if (parameter_declarator
->declarator
13387 && parameter_declarator
->declarator
->parameter_pack_p
)
13389 *is_parameter_pack
= true;
13390 parameter_declarator
->declarator
->parameter_pack_p
= false;
13393 if (parameter_declarator
->default_argument
)
13395 /* Can happen in some cases of erroneous input (c++/34892). */
13396 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13397 /* Consume the `...' for better error recovery. */
13398 cp_lexer_consume_token (parser
->lexer
);
13400 /* If the next token is an ellipsis, and we don't already have it
13401 marked as a parameter pack, then we have a parameter pack (that
13402 has no declarator). */
13403 else if (!*is_parameter_pack
13404 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
13405 && (declarator_can_be_parameter_pack
13406 (parameter_declarator
->declarator
)))
13408 /* Consume the `...'. */
13409 cp_lexer_consume_token (parser
->lexer
);
13410 maybe_warn_variadic_templates ();
13412 *is_parameter_pack
= true;
13414 /* We might end up with a pack expansion as the type of the non-type
13415 template parameter, in which case this is a non-type template
13417 else if (parameter_declarator
->decl_specifiers
.type
13418 && PACK_EXPANSION_P (parameter_declarator
->decl_specifiers
.type
))
13420 *is_parameter_pack
= true;
13421 parameter_declarator
->decl_specifiers
.type
=
13422 PACK_EXPANSION_PATTERN (parameter_declarator
->decl_specifiers
.type
);
13425 if (*is_parameter_pack
&& cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13427 /* Parameter packs cannot have default arguments. However, a
13428 user may try to do so, so we'll parse them and give an
13429 appropriate diagnostic here. */
13431 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
13433 /* Find the name of the parameter pack. */
13434 id_declarator
= parameter_declarator
->declarator
;
13435 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
13436 id_declarator
= id_declarator
->declarator
;
13438 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
13439 error_at (start_token
->location
,
13440 "template parameter pack %qD cannot have a default argument",
13441 id_declarator
->u
.id
.unqualified_name
);
13443 error_at (start_token
->location
,
13444 "template parameter pack cannot have a default argument");
13446 /* Parse the default argument, but throw away the result. */
13447 cp_parser_default_argument (parser
, /*template_parm_p=*/true);
13450 parm
= grokdeclarator (parameter_declarator
->declarator
,
13451 ¶meter_declarator
->decl_specifiers
,
13452 TPARM
, /*initialized=*/0,
13453 /*attrlist=*/NULL
);
13454 if (parm
== error_mark_node
)
13455 return error_mark_node
;
13457 return build_tree_list (parameter_declarator
->default_argument
, parm
);
13460 /* Parse a type-parameter.
13463 class identifier [opt]
13464 class identifier [opt] = type-id
13465 typename identifier [opt]
13466 typename identifier [opt] = type-id
13467 template < template-parameter-list > class identifier [opt]
13468 template < template-parameter-list > class identifier [opt]
13471 GNU Extension (variadic templates):
13474 class ... identifier [opt]
13475 typename ... identifier [opt]
13477 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13478 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13479 the declaration of the parameter.
13481 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13484 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
13489 /* Look for a keyword to tell us what kind of parameter this is. */
13490 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
13492 return error_mark_node
;
13494 switch (token
->keyword
)
13500 tree default_argument
;
13502 /* If the next token is an ellipsis, we have a template
13504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13506 /* Consume the `...' token. */
13507 cp_lexer_consume_token (parser
->lexer
);
13508 maybe_warn_variadic_templates ();
13510 *is_parameter_pack
= true;
13513 /* If the next token is an identifier, then it names the
13515 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
13516 identifier
= cp_parser_identifier (parser
);
13518 identifier
= NULL_TREE
;
13520 /* Create the parameter. */
13521 parameter
= finish_template_type_parm (class_type_node
, identifier
);
13523 /* If the next token is an `=', we have a default argument. */
13524 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13526 /* Consume the `=' token. */
13527 cp_lexer_consume_token (parser
->lexer
);
13528 /* Parse the default-argument. */
13529 push_deferring_access_checks (dk_no_deferred
);
13530 default_argument
= cp_parser_type_id (parser
);
13532 /* Template parameter packs cannot have default
13534 if (*is_parameter_pack
)
13537 error_at (token
->location
,
13538 "template parameter pack %qD cannot have a "
13539 "default argument", identifier
);
13541 error_at (token
->location
,
13542 "template parameter packs cannot have "
13543 "default arguments");
13544 default_argument
= NULL_TREE
;
13546 else if (check_for_bare_parameter_packs (default_argument
))
13547 default_argument
= error_mark_node
;
13548 pop_deferring_access_checks ();
13551 default_argument
= NULL_TREE
;
13553 /* Create the combined representation of the parameter and the
13554 default argument. */
13555 parameter
= build_tree_list (default_argument
, parameter
);
13562 tree default_argument
;
13564 /* Look for the `<'. */
13565 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
13566 /* Parse the template-parameter-list. */
13567 cp_parser_template_parameter_list (parser
);
13568 /* Look for the `>'. */
13569 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
13570 /* Look for the `class' or 'typename' keywords. */
13571 cp_parser_type_parameter_key (parser
);
13572 /* If the next token is an ellipsis, we have a template
13574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
13576 /* Consume the `...' token. */
13577 cp_lexer_consume_token (parser
->lexer
);
13578 maybe_warn_variadic_templates ();
13580 *is_parameter_pack
= true;
13582 /* If the next token is an `=', then there is a
13583 default-argument. If the next token is a `>', we are at
13584 the end of the parameter-list. If the next token is a `,',
13585 then we are at the end of this parameter. */
13586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
13587 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
13588 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
13590 identifier
= cp_parser_identifier (parser
);
13591 /* Treat invalid names as if the parameter were nameless. */
13592 if (identifier
== error_mark_node
)
13593 identifier
= NULL_TREE
;
13596 identifier
= NULL_TREE
;
13598 /* Create the template parameter. */
13599 parameter
= finish_template_template_parm (class_type_node
,
13602 /* If the next token is an `=', then there is a
13603 default-argument. */
13604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
13608 /* Consume the `='. */
13609 cp_lexer_consume_token (parser
->lexer
);
13610 /* Parse the id-expression. */
13611 push_deferring_access_checks (dk_no_deferred
);
13612 /* save token before parsing the id-expression, for error
13614 token
= cp_lexer_peek_token (parser
->lexer
);
13616 = cp_parser_id_expression (parser
,
13617 /*template_keyword_p=*/false,
13618 /*check_dependency_p=*/true,
13619 /*template_p=*/&is_template
,
13620 /*declarator_p=*/false,
13621 /*optional_p=*/false);
13622 if (TREE_CODE (default_argument
) == TYPE_DECL
)
13623 /* If the id-expression was a template-id that refers to
13624 a template-class, we already have the declaration here,
13625 so no further lookup is needed. */
13628 /* Look up the name. */
13630 = cp_parser_lookup_name (parser
, default_argument
,
13632 /*is_template=*/is_template
,
13633 /*is_namespace=*/false,
13634 /*check_dependency=*/true,
13635 /*ambiguous_decls=*/NULL
,
13637 /* See if the default argument is valid. */
13639 = check_template_template_default_arg (default_argument
);
13641 /* Template parameter packs cannot have default
13643 if (*is_parameter_pack
)
13646 error_at (token
->location
,
13647 "template parameter pack %qD cannot "
13648 "have a default argument",
13651 error_at (token
->location
, "template parameter packs cannot "
13652 "have default arguments");
13653 default_argument
= NULL_TREE
;
13655 pop_deferring_access_checks ();
13658 default_argument
= NULL_TREE
;
13660 /* Create the combined representation of the parameter and the
13661 default argument. */
13662 parameter
= build_tree_list (default_argument
, parameter
);
13667 gcc_unreachable ();
13674 /* Parse a template-id.
13677 template-name < template-argument-list [opt] >
13679 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13680 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13681 returned. Otherwise, if the template-name names a function, or set
13682 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13683 names a class, returns a TYPE_DECL for the specialization.
13685 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13686 uninstantiated templates. */
13689 cp_parser_template_id (cp_parser
*parser
,
13690 bool template_keyword_p
,
13691 bool check_dependency_p
,
13692 enum tag_types tag_type
,
13693 bool is_declaration
)
13699 cp_token_position start_of_id
= 0;
13700 deferred_access_check
*chk
;
13701 vec
<deferred_access_check
, va_gc
> *access_check
;
13702 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
13703 bool is_identifier
;
13705 /* If the next token corresponds to a template-id, there is no need
13707 next_token
= cp_lexer_peek_token (parser
->lexer
);
13708 if (next_token
->type
== CPP_TEMPLATE_ID
)
13710 struct tree_check
*check_value
;
13712 /* Get the stored value. */
13713 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
13714 /* Perform any access checks that were deferred. */
13715 access_check
= check_value
->checks
;
13718 FOR_EACH_VEC_ELT (*access_check
, i
, chk
)
13719 perform_or_defer_access_check (chk
->binfo
,
13722 tf_warning_or_error
);
13724 /* Return the stored value. */
13725 return check_value
->value
;
13728 /* Avoid performing name lookup if there is no possibility of
13729 finding a template-id. */
13730 if ((next_token
->type
!= CPP_NAME
&& next_token
->keyword
!= RID_OPERATOR
)
13731 || (next_token
->type
== CPP_NAME
13732 && !cp_parser_nth_token_starts_template_argument_list_p
13735 cp_parser_error (parser
, "expected template-id");
13736 return error_mark_node
;
13739 /* Remember where the template-id starts. */
13740 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
13741 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
13743 push_deferring_access_checks (dk_deferred
);
13745 /* Parse the template-name. */
13746 is_identifier
= false;
13747 templ
= cp_parser_template_name (parser
, template_keyword_p
,
13748 check_dependency_p
,
13752 if (templ
== error_mark_node
|| is_identifier
)
13754 pop_deferring_access_checks ();
13758 /* If we find the sequence `[:' after a template-name, it's probably
13759 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13760 parse correctly the argument list. */
13761 next_token
= cp_lexer_peek_token (parser
->lexer
);
13762 next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13763 if (next_token
->type
== CPP_OPEN_SQUARE
13764 && next_token
->flags
& DIGRAPH
13765 && next_token_2
->type
== CPP_COLON
13766 && !(next_token_2
->flags
& PREV_WHITE
))
13768 cp_parser_parse_tentatively (parser
);
13769 /* Change `:' into `::'. */
13770 next_token_2
->type
= CPP_SCOPE
;
13771 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13773 cp_lexer_consume_token (parser
->lexer
);
13775 /* Parse the arguments. */
13776 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13777 if (!cp_parser_parse_definitely (parser
))
13779 /* If we couldn't parse an argument list, then we revert our changes
13780 and return simply an error. Maybe this is not a template-id
13782 next_token_2
->type
= CPP_COLON
;
13783 cp_parser_error (parser
, "expected %<<%>");
13784 pop_deferring_access_checks ();
13785 return error_mark_node
;
13787 /* Otherwise, emit an error about the invalid digraph, but continue
13788 parsing because we got our argument list. */
13789 if (permerror (next_token
->location
,
13790 "%<<::%> cannot begin a template-argument list"))
13792 static bool hint
= false;
13793 inform (next_token
->location
,
13794 "%<<:%> is an alternate spelling for %<[%>."
13795 " Insert whitespace between %<<%> and %<::%>");
13796 if (!hint
&& !flag_permissive
)
13798 inform (next_token
->location
, "(if you use %<-fpermissive%> "
13799 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13800 "accept your code)");
13807 /* Look for the `<' that starts the template-argument-list. */
13808 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
13810 pop_deferring_access_checks ();
13811 return error_mark_node
;
13813 /* Parse the arguments. */
13814 arguments
= cp_parser_enclosed_template_argument_list (parser
);
13817 /* Build a representation of the specialization. */
13818 if (identifier_p (templ
))
13819 template_id
= build_min_nt_loc (next_token
->location
,
13822 else if (DECL_TYPE_TEMPLATE_P (templ
)
13823 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
13825 bool entering_scope
;
13826 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13827 template (rather than some instantiation thereof) only if
13828 is not nested within some other construct. For example, in
13829 "template <typename T> void f(T) { A<T>::", A<T> is just an
13830 instantiation of A. */
13831 entering_scope
= (template_parm_scope_p ()
13832 && cp_lexer_next_token_is (parser
->lexer
,
13835 = finish_template_type (templ
, arguments
, entering_scope
);
13837 else if (variable_template_p (templ
))
13839 template_id
= lookup_template_variable (templ
, arguments
);
13843 /* If it's not a class-template or a template-template, it should be
13844 a function-template. */
13845 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ
)
13846 || TREE_CODE (templ
) == OVERLOAD
13847 || BASELINK_P (templ
)));
13849 template_id
= lookup_template_function (templ
, arguments
);
13852 /* If parsing tentatively, replace the sequence of tokens that makes
13853 up the template-id with a CPP_TEMPLATE_ID token. That way,
13854 should we re-parse the token stream, we will not have to repeat
13855 the effort required to do the parse, nor will we issue duplicate
13856 error messages about problems during instantiation of the
13859 /* Don't do this if we had a parse error in a declarator; re-parsing
13860 might succeed if a name changes meaning (60361). */
13861 && !(cp_parser_error_occurred (parser
)
13862 && cp_parser_parsing_tentatively (parser
)
13863 && parser
->in_declarator_p
))
13865 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start_of_id
);
13867 /* Reset the contents of the START_OF_ID token. */
13868 token
->type
= CPP_TEMPLATE_ID
;
13869 /* Retrieve any deferred checks. Do not pop this access checks yet
13870 so the memory will not be reclaimed during token replacing below. */
13871 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
13872 token
->u
.tree_check_value
->value
= template_id
;
13873 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
13874 token
->keyword
= RID_MAX
;
13876 /* Purge all subsequent tokens. */
13877 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
13879 /* ??? Can we actually assume that, if template_id ==
13880 error_mark_node, we will have issued a diagnostic to the
13881 user, as opposed to simply marking the tentative parse as
13883 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
13884 error_at (token
->location
, "parse error in template argument list");
13887 pop_to_parent_deferring_access_checks ();
13888 return template_id
;
13891 /* Parse a template-name.
13896 The standard should actually say:
13900 operator-function-id
13902 A defect report has been filed about this issue.
13904 A conversion-function-id cannot be a template name because they cannot
13905 be part of a template-id. In fact, looking at this code:
13907 a.operator K<int>()
13909 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13910 It is impossible to call a templated conversion-function-id with an
13911 explicit argument list, since the only allowed template parameter is
13912 the type to which it is converting.
13914 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13915 `template' keyword, in a construction like:
13919 In that case `f' is taken to be a template-name, even though there
13920 is no way of knowing for sure.
13922 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13923 name refers to a set of overloaded functions, at least one of which
13924 is a template, or an IDENTIFIER_NODE with the name of the template,
13925 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13926 names are looked up inside uninstantiated templates. */
13929 cp_parser_template_name (cp_parser
* parser
,
13930 bool template_keyword_p
,
13931 bool check_dependency_p
,
13932 bool is_declaration
,
13933 enum tag_types tag_type
,
13934 bool *is_identifier
)
13939 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13941 /* If the next token is `operator', then we have either an
13942 operator-function-id or a conversion-function-id. */
13943 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
13945 /* We don't know whether we're looking at an
13946 operator-function-id or a conversion-function-id. */
13947 cp_parser_parse_tentatively (parser
);
13948 /* Try an operator-function-id. */
13949 identifier
= cp_parser_operator_function_id (parser
);
13950 /* If that didn't work, try a conversion-function-id. */
13951 if (!cp_parser_parse_definitely (parser
))
13953 cp_parser_error (parser
, "expected template-name");
13954 return error_mark_node
;
13957 /* Look for the identifier. */
13959 identifier
= cp_parser_identifier (parser
);
13961 /* If we didn't find an identifier, we don't have a template-id. */
13962 if (identifier
== error_mark_node
)
13963 return error_mark_node
;
13965 /* If the name immediately followed the `template' keyword, then it
13966 is a template-name. However, if the next token is not `<', then
13967 we do not treat it as a template-name, since it is not being used
13968 as part of a template-id. This enables us to handle constructs
13971 template <typename T> struct S { S(); };
13972 template <typename T> S<T>::S();
13974 correctly. We would treat `S' as a template -- if it were `S<T>'
13975 -- but we do not if there is no `<'. */
13977 if (processing_template_decl
13978 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
13980 /* In a declaration, in a dependent context, we pretend that the
13981 "template" keyword was present in order to improve error
13982 recovery. For example, given:
13984 template <typename T> void f(T::X<int>);
13986 we want to treat "X<int>" as a template-id. */
13988 && !template_keyword_p
13989 && parser
->scope
&& TYPE_P (parser
->scope
)
13990 && check_dependency_p
13991 && dependent_scope_p (parser
->scope
)
13992 /* Do not do this for dtors (or ctors), since they never
13993 need the template keyword before their name. */
13994 && !constructor_name_p (identifier
, parser
->scope
))
13996 cp_token_position start
= 0;
13998 /* Explain what went wrong. */
13999 error_at (token
->location
, "non-template %qD used as template",
14001 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
14002 parser
->scope
, identifier
);
14003 /* If parsing tentatively, find the location of the "<" token. */
14004 if (cp_parser_simulate_error (parser
))
14005 start
= cp_lexer_token_position (parser
->lexer
, true);
14006 /* Parse the template arguments so that we can issue error
14007 messages about them. */
14008 cp_lexer_consume_token (parser
->lexer
);
14009 cp_parser_enclosed_template_argument_list (parser
);
14010 /* Skip tokens until we find a good place from which to
14011 continue parsing. */
14012 cp_parser_skip_to_closing_parenthesis (parser
,
14013 /*recovering=*/true,
14015 /*consume_paren=*/false);
14016 /* If parsing tentatively, permanently remove the
14017 template argument list. That will prevent duplicate
14018 error messages from being issued about the missing
14019 "template" keyword. */
14021 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
14023 *is_identifier
= true;
14027 /* If the "template" keyword is present, then there is generally
14028 no point in doing name-lookup, so we just return IDENTIFIER.
14029 But, if the qualifying scope is non-dependent then we can
14030 (and must) do name-lookup normally. */
14031 if (template_keyword_p
14033 || (TYPE_P (parser
->scope
)
14034 && dependent_type_p (parser
->scope
))))
14038 /* Look up the name. */
14039 decl
= cp_parser_lookup_name (parser
, identifier
,
14041 /*is_template=*/true,
14042 /*is_namespace=*/false,
14043 check_dependency_p
,
14044 /*ambiguous_decls=*/NULL
,
14047 /* If DECL is a template, then the name was a template-name. */
14048 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
14050 if (TREE_DEPRECATED (decl
)
14051 && deprecated_state
!= DEPRECATED_SUPPRESS
)
14052 warn_deprecated_use (decl
, NULL_TREE
);
14056 tree fn
= NULL_TREE
;
14058 /* The standard does not explicitly indicate whether a name that
14059 names a set of overloaded declarations, some of which are
14060 templates, is a template-name. However, such a name should
14061 be a template-name; otherwise, there is no way to form a
14062 template-id for the overloaded templates. */
14063 fns
= BASELINK_P (decl
) ? BASELINK_FUNCTIONS (decl
) : decl
;
14064 if (TREE_CODE (fns
) == OVERLOAD
)
14065 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
14066 if (TREE_CODE (OVL_CURRENT (fn
)) == TEMPLATE_DECL
)
14071 /* The name does not name a template. */
14072 cp_parser_error (parser
, "expected template-name");
14073 return error_mark_node
;
14077 /* If DECL is dependent, and refers to a function, then just return
14078 its name; we will look it up again during template instantiation. */
14079 if (DECL_FUNCTION_TEMPLATE_P (decl
) || !DECL_P (decl
))
14081 tree scope
= ovl_scope (decl
);
14082 if (TYPE_P (scope
) && dependent_type_p (scope
))
14089 /* Parse a template-argument-list.
14091 template-argument-list:
14092 template-argument ... [opt]
14093 template-argument-list , template-argument ... [opt]
14095 Returns a TREE_VEC containing the arguments. */
14098 cp_parser_template_argument_list (cp_parser
* parser
)
14100 tree fixed_args
[10];
14101 unsigned n_args
= 0;
14102 unsigned alloced
= 10;
14103 tree
*arg_ary
= fixed_args
;
14105 bool saved_in_template_argument_list_p
;
14107 bool saved_non_ice_p
;
14109 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
14110 parser
->in_template_argument_list_p
= true;
14111 /* Even if the template-id appears in an integral
14112 constant-expression, the contents of the argument list do
14114 saved_ice_p
= parser
->integral_constant_expression_p
;
14115 parser
->integral_constant_expression_p
= false;
14116 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
14117 parser
->non_integral_constant_expression_p
= false;
14119 /* Parse the arguments. */
14125 /* Consume the comma. */
14126 cp_lexer_consume_token (parser
->lexer
);
14128 /* Parse the template-argument. */
14129 argument
= cp_parser_template_argument (parser
);
14131 /* If the next token is an ellipsis, we're expanding a template
14133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
14135 if (argument
== error_mark_node
)
14137 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14138 error_at (token
->location
,
14139 "expected parameter pack before %<...%>");
14141 /* Consume the `...' token. */
14142 cp_lexer_consume_token (parser
->lexer
);
14144 /* Make the argument into a TYPE_PACK_EXPANSION or
14145 EXPR_PACK_EXPANSION. */
14146 argument
= make_pack_expansion (argument
);
14149 if (n_args
== alloced
)
14153 if (arg_ary
== fixed_args
)
14155 arg_ary
= XNEWVEC (tree
, alloced
);
14156 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
14159 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
14161 arg_ary
[n_args
++] = argument
;
14163 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
14165 vec
= make_tree_vec (n_args
);
14168 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
14170 if (arg_ary
!= fixed_args
)
14172 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
14173 parser
->integral_constant_expression_p
= saved_ice_p
;
14174 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
14175 #ifdef ENABLE_CHECKING
14176 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
14181 /* Parse a template-argument.
14184 assignment-expression
14188 The representation is that of an assignment-expression, type-id, or
14189 id-expression -- except that the qualified id-expression is
14190 evaluated, so that the value returned is either a DECL or an
14193 Although the standard says "assignment-expression", it forbids
14194 throw-expressions or assignments in the template argument.
14195 Therefore, we use "conditional-expression" instead. */
14198 cp_parser_template_argument (cp_parser
* parser
)
14203 bool maybe_type_id
= false;
14204 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
14205 location_t loc
= 0;
14208 /* There's really no way to know what we're looking at, so we just
14209 try each alternative in order.
14213 In a template-argument, an ambiguity between a type-id and an
14214 expression is resolved to a type-id, regardless of the form of
14215 the corresponding template-parameter.
14217 Therefore, we try a type-id first. */
14218 cp_parser_parse_tentatively (parser
);
14219 argument
= cp_parser_template_type_arg (parser
);
14220 /* If there was no error parsing the type-id but the next token is a
14221 '>>', our behavior depends on which dialect of C++ we're
14222 parsing. In C++98, we probably found a typo for '> >'. But there
14223 are type-id which are also valid expressions. For instance:
14225 struct X { int operator >> (int); };
14226 template <int V> struct Foo {};
14229 Here 'X()' is a valid type-id of a function type, but the user just
14230 wanted to write the expression "X() >> 5". Thus, we remember that we
14231 found a valid type-id, but we still try to parse the argument as an
14232 expression to see what happens.
14234 In C++0x, the '>>' will be considered two separate '>'
14236 if (!cp_parser_error_occurred (parser
)
14237 && cxx_dialect
== cxx98
14238 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
14240 maybe_type_id
= true;
14241 cp_parser_abort_tentative_parse (parser
);
14245 /* If the next token isn't a `,' or a `>', then this argument wasn't
14246 really finished. This means that the argument is not a valid
14248 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14249 cp_parser_error (parser
, "expected template-argument");
14250 /* If that worked, we're done. */
14251 if (cp_parser_parse_definitely (parser
))
14254 /* We're still not sure what the argument will be. */
14255 cp_parser_parse_tentatively (parser
);
14256 /* Try a template. */
14257 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
14258 argument
= cp_parser_id_expression (parser
,
14259 /*template_keyword_p=*/false,
14260 /*check_dependency_p=*/true,
14262 /*declarator_p=*/false,
14263 /*optional_p=*/false);
14264 /* If the next token isn't a `,' or a `>', then this argument wasn't
14265 really finished. */
14266 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14267 cp_parser_error (parser
, "expected template-argument");
14268 if (!cp_parser_error_occurred (parser
))
14270 /* Figure out what is being referred to. If the id-expression
14271 was for a class template specialization, then we will have a
14272 TYPE_DECL at this point. There is no need to do name lookup
14273 at this point in that case. */
14274 if (TREE_CODE (argument
) != TYPE_DECL
)
14275 argument
= cp_parser_lookup_name (parser
, argument
,
14277 /*is_template=*/template_p
,
14278 /*is_namespace=*/false,
14279 /*check_dependency=*/true,
14280 /*ambiguous_decls=*/NULL
,
14281 argument_start_token
->location
);
14282 if (TREE_CODE (argument
) != TEMPLATE_DECL
14283 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
14284 cp_parser_error (parser
, "expected template-name");
14286 if (cp_parser_parse_definitely (parser
))
14288 if (TREE_DEPRECATED (argument
))
14289 warn_deprecated_use (argument
, NULL_TREE
);
14292 /* It must be a non-type argument. There permitted cases are given
14293 in [temp.arg.nontype]:
14295 -- an integral constant-expression of integral or enumeration
14298 -- the name of a non-type template-parameter; or
14300 -- the name of an object or function with external linkage...
14302 -- the address of an object or function with external linkage...
14304 -- a pointer to member... */
14305 /* Look for a non-type template parameter. */
14306 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
14308 cp_parser_parse_tentatively (parser
);
14309 argument
= cp_parser_primary_expression (parser
,
14310 /*address_p=*/false,
14312 /*template_arg_p=*/true,
14314 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
14315 || !cp_parser_next_token_ends_template_argument_p (parser
))
14316 cp_parser_simulate_error (parser
);
14317 if (cp_parser_parse_definitely (parser
))
14321 /* If the next token is "&", the argument must be the address of an
14322 object or function with external linkage. */
14323 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
14326 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14327 cp_lexer_consume_token (parser
->lexer
);
14329 /* See if we might have an id-expression. */
14330 token
= cp_lexer_peek_token (parser
->lexer
);
14331 if (token
->type
== CPP_NAME
14332 || token
->keyword
== RID_OPERATOR
14333 || token
->type
== CPP_SCOPE
14334 || token
->type
== CPP_TEMPLATE_ID
14335 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
14337 cp_parser_parse_tentatively (parser
);
14338 argument
= cp_parser_primary_expression (parser
,
14341 /*template_arg_p=*/true,
14343 if (cp_parser_error_occurred (parser
)
14344 || !cp_parser_next_token_ends_template_argument_p (parser
))
14345 cp_parser_abort_tentative_parse (parser
);
14350 if (INDIRECT_REF_P (argument
))
14352 /* Strip the dereference temporarily. */
14353 gcc_assert (REFERENCE_REF_P (argument
));
14354 argument
= TREE_OPERAND (argument
, 0);
14357 /* If we're in a template, we represent a qualified-id referring
14358 to a static data member as a SCOPE_REF even if the scope isn't
14359 dependent so that we can check access control later. */
14361 if (TREE_CODE (probe
) == SCOPE_REF
)
14362 probe
= TREE_OPERAND (probe
, 1);
14365 /* A variable without external linkage might still be a
14366 valid constant-expression, so no error is issued here
14367 if the external-linkage check fails. */
14368 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
14369 cp_parser_simulate_error (parser
);
14371 else if (is_overloaded_fn (argument
))
14372 /* All overloaded functions are allowed; if the external
14373 linkage test does not pass, an error will be issued
14377 && (TREE_CODE (argument
) == OFFSET_REF
14378 || TREE_CODE (argument
) == SCOPE_REF
))
14379 /* A pointer-to-member. */
14381 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
14384 cp_parser_simulate_error (parser
);
14386 if (cp_parser_parse_definitely (parser
))
14389 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
14390 tf_warning_or_error
);
14392 argument
= convert_from_reference (argument
);
14397 /* If the argument started with "&", there are no other valid
14398 alternatives at this point. */
14401 cp_parser_error (parser
, "invalid non-type template argument");
14402 return error_mark_node
;
14405 /* If the argument wasn't successfully parsed as a type-id followed
14406 by '>>', the argument can only be a constant expression now.
14407 Otherwise, we try parsing the constant-expression tentatively,
14408 because the argument could really be a type-id. */
14410 cp_parser_parse_tentatively (parser
);
14411 argument
= cp_parser_constant_expression (parser
);
14413 if (!maybe_type_id
)
14415 if (!cp_parser_next_token_ends_template_argument_p (parser
))
14416 cp_parser_error (parser
, "expected template-argument");
14417 if (cp_parser_parse_definitely (parser
))
14419 /* We did our best to parse the argument as a non type-id, but that
14420 was the only alternative that matched (albeit with a '>' after
14421 it). We can assume it's just a typo from the user, and a
14422 diagnostic will then be issued. */
14423 return cp_parser_template_type_arg (parser
);
14426 /* Parse an explicit-instantiation.
14428 explicit-instantiation:
14429 template declaration
14431 Although the standard says `declaration', what it really means is:
14433 explicit-instantiation:
14434 template decl-specifier-seq [opt] declarator [opt] ;
14436 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14437 supposed to be allowed. A defect report has been filed about this
14442 explicit-instantiation:
14443 storage-class-specifier template
14444 decl-specifier-seq [opt] declarator [opt] ;
14445 function-specifier template
14446 decl-specifier-seq [opt] declarator [opt] ; */
14449 cp_parser_explicit_instantiation (cp_parser
* parser
)
14451 int declares_class_or_enum
;
14452 cp_decl_specifier_seq decl_specifiers
;
14453 tree extension_specifier
= NULL_TREE
;
14455 timevar_push (TV_TEMPLATE_INST
);
14457 /* Look for an (optional) storage-class-specifier or
14458 function-specifier. */
14459 if (cp_parser_allow_gnu_extensions_p (parser
))
14461 extension_specifier
14462 = cp_parser_storage_class_specifier_opt (parser
);
14463 if (!extension_specifier
)
14464 extension_specifier
14465 = cp_parser_function_specifier_opt (parser
,
14466 /*decl_specs=*/NULL
);
14469 /* Look for the `template' keyword. */
14470 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14471 /* Let the front end know that we are processing an explicit
14473 begin_explicit_instantiation ();
14474 /* [temp.explicit] says that we are supposed to ignore access
14475 control while processing explicit instantiation directives. */
14476 push_deferring_access_checks (dk_no_check
);
14477 /* Parse a decl-specifier-seq. */
14478 cp_parser_decl_specifier_seq (parser
,
14479 CP_PARSER_FLAGS_OPTIONAL
,
14481 &declares_class_or_enum
);
14482 /* If there was exactly one decl-specifier, and it declared a class,
14483 and there's no declarator, then we have an explicit type
14485 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
14489 type
= check_tag_decl (&decl_specifiers
,
14490 /*explicit_type_instantiation_p=*/true);
14491 /* Turn access control back on for names used during
14492 template instantiation. */
14493 pop_deferring_access_checks ();
14495 do_type_instantiation (type
, extension_specifier
,
14496 /*complain=*/tf_error
);
14500 cp_declarator
*declarator
;
14503 /* Parse the declarator. */
14505 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
14506 /*ctor_dtor_or_conv_p=*/NULL
,
14507 /*parenthesized_p=*/NULL
,
14508 /*member_p=*/false,
14509 /*friend_p=*/false);
14510 if (declares_class_or_enum
& 2)
14511 cp_parser_check_for_definition_in_return_type (declarator
,
14512 decl_specifiers
.type
,
14513 decl_specifiers
.locations
[ds_type_spec
]);
14514 if (declarator
!= cp_error_declarator
)
14516 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
14517 permerror (decl_specifiers
.locations
[ds_inline
],
14518 "explicit instantiation shall not use"
14519 " %<inline%> specifier");
14520 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
14521 permerror (decl_specifiers
.locations
[ds_constexpr
],
14522 "explicit instantiation shall not use"
14523 " %<constexpr%> specifier");
14525 decl
= grokdeclarator (declarator
, &decl_specifiers
,
14526 NORMAL
, 0, &decl_specifiers
.attributes
);
14527 /* Turn access control back on for names used during
14528 template instantiation. */
14529 pop_deferring_access_checks ();
14530 /* Do the explicit instantiation. */
14531 do_decl_instantiation (decl
, extension_specifier
);
14535 pop_deferring_access_checks ();
14536 /* Skip the body of the explicit instantiation. */
14537 cp_parser_skip_to_end_of_statement (parser
);
14540 /* We're done with the instantiation. */
14541 end_explicit_instantiation ();
14543 cp_parser_consume_semicolon_at_end_of_statement (parser
);
14545 timevar_pop (TV_TEMPLATE_INST
);
14548 /* Parse an explicit-specialization.
14550 explicit-specialization:
14551 template < > declaration
14553 Although the standard says `declaration', what it really means is:
14555 explicit-specialization:
14556 template <> decl-specifier [opt] init-declarator [opt] ;
14557 template <> function-definition
14558 template <> explicit-specialization
14559 template <> template-declaration */
14562 cp_parser_explicit_specialization (cp_parser
* parser
)
14564 bool need_lang_pop
;
14565 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14567 /* Look for the `template' keyword. */
14568 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
14569 /* Look for the `<'. */
14570 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
14571 /* Look for the `>'. */
14572 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
14573 /* We have processed another parameter list. */
14574 ++parser
->num_template_parameter_lists
;
14577 A template ... explicit specialization ... shall not have C
14579 if (current_lang_name
== lang_name_c
)
14581 error_at (token
->location
, "template specialization with C linkage");
14582 /* Give it C++ linkage to avoid confusing other parts of the
14584 push_lang_context (lang_name_cplusplus
);
14585 need_lang_pop
= true;
14588 need_lang_pop
= false;
14589 /* Let the front end know that we are beginning a specialization. */
14590 if (!begin_specialization ())
14592 end_specialization ();
14596 /* If the next keyword is `template', we need to figure out whether
14597 or not we're looking a template-declaration. */
14598 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
14600 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
14601 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
14602 cp_parser_template_declaration_after_export (parser
,
14603 /*member_p=*/false);
14605 cp_parser_explicit_specialization (parser
);
14608 /* Parse the dependent declaration. */
14609 cp_parser_single_declaration (parser
,
14611 /*member_p=*/false,
14612 /*explicit_specialization_p=*/true,
14613 /*friend_p=*/NULL
);
14614 /* We're done with the specialization. */
14615 end_specialization ();
14616 /* For the erroneous case of a template with C linkage, we pushed an
14617 implicit C++ linkage scope; exit that scope now. */
14619 pop_lang_context ();
14620 /* We're done with this parameter list. */
14621 --parser
->num_template_parameter_lists
;
14624 /* Parse a type-specifier.
14627 simple-type-specifier
14630 elaborated-type-specifier
14638 Returns a representation of the type-specifier. For a
14639 class-specifier, enum-specifier, or elaborated-type-specifier, a
14640 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14642 The parser flags FLAGS is used to control type-specifier parsing.
14644 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14645 in a decl-specifier-seq.
14647 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14648 class-specifier, enum-specifier, or elaborated-type-specifier, then
14649 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14650 if a type is declared; 2 if it is defined. Otherwise, it is set to
14653 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14654 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14655 is set to FALSE. */
14658 cp_parser_type_specifier (cp_parser
* parser
,
14659 cp_parser_flags flags
,
14660 cp_decl_specifier_seq
*decl_specs
,
14661 bool is_declaration
,
14662 int* declares_class_or_enum
,
14663 bool* is_cv_qualifier
)
14665 tree type_spec
= NULL_TREE
;
14668 cp_decl_spec ds
= ds_last
;
14670 /* Assume this type-specifier does not declare a new type. */
14671 if (declares_class_or_enum
)
14672 *declares_class_or_enum
= 0;
14673 /* And that it does not specify a cv-qualifier. */
14674 if (is_cv_qualifier
)
14675 *is_cv_qualifier
= false;
14676 /* Peek at the next token. */
14677 token
= cp_lexer_peek_token (parser
->lexer
);
14679 /* If we're looking at a keyword, we can use that to guide the
14680 production we choose. */
14681 keyword
= token
->keyword
;
14685 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14686 goto elaborated_type_specifier
;
14688 /* Look for the enum-specifier. */
14689 type_spec
= cp_parser_enum_specifier (parser
);
14690 /* If that worked, we're done. */
14693 if (declares_class_or_enum
)
14694 *declares_class_or_enum
= 2;
14696 cp_parser_set_decl_spec_type (decl_specs
,
14699 /*type_definition_p=*/true);
14703 goto elaborated_type_specifier
;
14705 /* Any of these indicate either a class-specifier, or an
14706 elaborated-type-specifier. */
14710 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
14711 goto elaborated_type_specifier
;
14713 /* Parse tentatively so that we can back up if we don't find a
14714 class-specifier. */
14715 cp_parser_parse_tentatively (parser
);
14716 /* Look for the class-specifier. */
14717 type_spec
= cp_parser_class_specifier (parser
);
14718 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
14719 /* If that worked, we're done. */
14720 if (cp_parser_parse_definitely (parser
))
14722 if (declares_class_or_enum
)
14723 *declares_class_or_enum
= 2;
14725 cp_parser_set_decl_spec_type (decl_specs
,
14728 /*type_definition_p=*/true);
14732 /* Fall through. */
14733 elaborated_type_specifier
:
14734 /* We're declaring (not defining) a class or enum. */
14735 if (declares_class_or_enum
)
14736 *declares_class_or_enum
= 1;
14738 /* Fall through. */
14740 /* Look for an elaborated-type-specifier. */
14742 = (cp_parser_elaborated_type_specifier
14744 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
14747 cp_parser_set_decl_spec_type (decl_specs
,
14750 /*type_definition_p=*/false);
14755 if (is_cv_qualifier
)
14756 *is_cv_qualifier
= true;
14761 if (is_cv_qualifier
)
14762 *is_cv_qualifier
= true;
14767 if (is_cv_qualifier
)
14768 *is_cv_qualifier
= true;
14772 /* The `__complex__' keyword is a GNU extension. */
14780 /* Handle simple keywords. */
14785 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14786 decl_specs
->any_specifiers_p
= true;
14788 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14791 /* If we do not already have a type-specifier, assume we are looking
14792 at a simple-type-specifier. */
14793 type_spec
= cp_parser_simple_type_specifier (parser
,
14797 /* If we didn't find a type-specifier, and a type-specifier was not
14798 optional in this context, issue an error message. */
14799 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
14801 cp_parser_error (parser
, "expected type specifier");
14802 return error_mark_node
;
14808 /* Parse a simple-type-specifier.
14810 simple-type-specifier:
14811 :: [opt] nested-name-specifier [opt] type-name
14812 :: [opt] nested-name-specifier template template-id
14827 simple-type-specifier:
14829 decltype ( expression )
14832 __underlying_type ( type-id )
14836 simple-type-specifier:
14838 __typeof__ unary-expression
14839 __typeof__ ( type-id )
14840 __typeof__ ( type-id ) { initializer-list , [opt] }
14842 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14843 appropriately updated. */
14846 cp_parser_simple_type_specifier (cp_parser
* parser
,
14847 cp_decl_specifier_seq
*decl_specs
,
14848 cp_parser_flags flags
)
14850 tree type
= NULL_TREE
;
14854 /* Peek at the next token. */
14855 token
= cp_lexer_peek_token (parser
->lexer
);
14857 /* If we're looking at a keyword, things are easy. */
14858 switch (token
->keyword
)
14862 decl_specs
->explicit_char_p
= true;
14863 type
= char_type_node
;
14866 type
= char16_type_node
;
14869 type
= char32_type_node
;
14872 type
= wchar_type_node
;
14875 type
= boolean_type_node
;
14878 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
14879 type
= short_integer_type_node
;
14883 decl_specs
->explicit_int_p
= true;
14884 type
= integer_type_node
;
14890 idx
= token
->keyword
- RID_INT_N_0
;
14891 if (! int_n_enabled_p
[idx
])
14895 decl_specs
->explicit_intN_p
= true;
14896 decl_specs
->int_n_idx
= idx
;
14898 type
= int_n_trees
[idx
].signed_type
;
14902 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
14903 type
= long_integer_type_node
;
14906 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
14907 type
= integer_type_node
;
14910 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
14911 type
= unsigned_type_node
;
14914 type
= float_type_node
;
14917 type
= double_type_node
;
14920 type
= void_type_node
;
14924 maybe_warn_cpp0x (CPP0X_AUTO
);
14925 if (parser
->auto_is_implicit_function_template_parm_p
)
14927 if (cxx_dialect
>= cxx14
)
14928 type
= synthesize_implicit_template_parm (parser
);
14930 type
= error_mark_node
;
14932 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
14934 if (cxx_dialect
< cxx14
)
14935 error_at (token
->location
,
14936 "use of %<auto%> in lambda parameter declaration "
14937 "only available with "
14938 "-std=c++14 or -std=gnu++14");
14940 else if (cxx_dialect
< cxx14
)
14941 error_at (token
->location
,
14942 "use of %<auto%> in parameter declaration "
14943 "only available with "
14944 "-std=c++14 or -std=gnu++14");
14946 pedwarn (token
->location
, OPT_Wpedantic
,
14947 "ISO C++ forbids use of %<auto%> in parameter "
14951 type
= make_auto ();
14955 /* Since DR 743, decltype can either be a simple-type-specifier by
14956 itself or begin a nested-name-specifier. Parsing it will replace
14957 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14958 handling below decide what to do. */
14959 cp_parser_decltype (parser
);
14960 cp_lexer_set_token_position (parser
->lexer
, token
);
14964 /* Consume the `typeof' token. */
14965 cp_lexer_consume_token (parser
->lexer
);
14966 /* Parse the operand to `typeof'. */
14967 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
14968 /* If it is not already a TYPE, take its type. */
14969 if (!TYPE_P (type
))
14970 type
= finish_typeof (type
);
14973 cp_parser_set_decl_spec_type (decl_specs
, type
,
14975 /*type_definition_p=*/false);
14979 case RID_UNDERLYING_TYPE
:
14980 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
14982 cp_parser_set_decl_spec_type (decl_specs
, type
,
14984 /*type_definition_p=*/false);
14989 case RID_DIRECT_BASES
:
14990 type
= cp_parser_trait_expr (parser
, token
->keyword
);
14992 cp_parser_set_decl_spec_type (decl_specs
, type
,
14994 /*type_definition_p=*/false);
15000 /* If token is an already-parsed decltype not followed by ::,
15001 it's a simple-type-specifier. */
15002 if (token
->type
== CPP_DECLTYPE
15003 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
15005 type
= token
->u
.value
;
15008 cp_parser_set_decl_spec_type (decl_specs
, type
,
15010 /*type_definition_p=*/false);
15011 /* Remember that we are handling a decltype in order to
15012 implement the resolution of DR 1510 when the argument
15013 isn't instantiation dependent. */
15014 decl_specs
->decltype_p
= true;
15016 cp_lexer_consume_token (parser
->lexer
);
15020 /* If the type-specifier was for a built-in type, we're done. */
15023 /* Record the type. */
15025 && (token
->keyword
!= RID_SIGNED
15026 && token
->keyword
!= RID_UNSIGNED
15027 && token
->keyword
!= RID_SHORT
15028 && token
->keyword
!= RID_LONG
))
15029 cp_parser_set_decl_spec_type (decl_specs
,
15032 /*type_definition_p=*/false);
15034 decl_specs
->any_specifiers_p
= true;
15036 /* Consume the token. */
15037 cp_lexer_consume_token (parser
->lexer
);
15039 if (type
== error_mark_node
)
15040 return error_mark_node
;
15042 /* There is no valid C++ program where a non-template type is
15043 followed by a "<". That usually indicates that the user thought
15044 that the type was a template. */
15045 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
15048 return TYPE_NAME (type
);
15051 /* The type-specifier must be a user-defined type. */
15052 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
15057 /* Don't gobble tokens or issue error messages if this is an
15058 optional type-specifier. */
15059 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
15060 cp_parser_parse_tentatively (parser
);
15062 /* Look for the optional `::' operator. */
15064 = (cp_parser_global_scope_opt (parser
,
15065 /*current_scope_valid_p=*/false)
15067 /* Look for the nested-name specifier. */
15069 = (cp_parser_nested_name_specifier_opt (parser
,
15070 /*typename_keyword_p=*/false,
15071 /*check_dependency_p=*/true,
15073 /*is_declaration=*/false)
15075 token
= cp_lexer_peek_token (parser
->lexer
);
15076 /* If we have seen a nested-name-specifier, and the next token
15077 is `template', then we are using the template-id production. */
15079 && cp_parser_optional_template_keyword (parser
))
15081 /* Look for the template-id. */
15082 type
= cp_parser_template_id (parser
,
15083 /*template_keyword_p=*/true,
15084 /*check_dependency_p=*/true,
15086 /*is_declaration=*/false);
15087 /* If the template-id did not name a type, we are out of
15089 if (TREE_CODE (type
) != TYPE_DECL
)
15091 cp_parser_error (parser
, "expected template-id for type");
15095 /* Otherwise, look for a type-name. */
15097 type
= cp_parser_type_name (parser
);
15098 /* Keep track of all name-lookups performed in class scopes. */
15102 && TREE_CODE (type
) == TYPE_DECL
15103 && identifier_p (DECL_NAME (type
)))
15104 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
15105 /* If it didn't work out, we don't have a TYPE. */
15106 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
15107 && !cp_parser_parse_definitely (parser
))
15109 if (type
&& decl_specs
)
15110 cp_parser_set_decl_spec_type (decl_specs
, type
,
15112 /*type_definition_p=*/false);
15115 /* If we didn't get a type-name, issue an error message. */
15116 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
15118 cp_parser_error (parser
, "expected type-name");
15119 return error_mark_node
;
15122 if (type
&& type
!= error_mark_node
)
15124 /* See if TYPE is an Objective-C type, and if so, parse and
15125 accept any protocol references following it. Do this before
15126 the cp_parser_check_for_invalid_template_id() call, because
15127 Objective-C types can be followed by '<...>' which would
15128 enclose protocol names rather than template arguments, and so
15129 everything is fine. */
15130 if (c_dialect_objc () && !parser
->scope
15131 && (objc_is_id (type
) || objc_is_class_name (type
)))
15133 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15134 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
15136 /* Clobber the "unqualified" type previously entered into
15137 DECL_SPECS with the new, improved protocol-qualified version. */
15139 decl_specs
->type
= qual_type
;
15144 /* There is no valid C++ program where a non-template type is
15145 followed by a "<". That usually indicates that the user
15146 thought that the type was a template. */
15147 cp_parser_check_for_invalid_template_id (parser
, TREE_TYPE (type
),
15155 /* Parse a type-name.
15161 simple-template-id [in c++0x]
15169 Returns a TYPE_DECL for the type. */
15172 cp_parser_type_name (cp_parser
* parser
)
15176 /* We can't know yet whether it is a class-name or not. */
15177 cp_parser_parse_tentatively (parser
);
15178 /* Try a class-name. */
15179 type_decl
= cp_parser_class_name (parser
,
15180 /*typename_keyword_p=*/false,
15181 /*template_keyword_p=*/false,
15183 /*check_dependency_p=*/true,
15184 /*class_head_p=*/false,
15185 /*is_declaration=*/false);
15186 /* If it's not a class-name, keep looking. */
15187 if (!cp_parser_parse_definitely (parser
))
15189 if (cxx_dialect
< cxx11
)
15190 /* It must be a typedef-name or an enum-name. */
15191 return cp_parser_nonclass_name (parser
);
15193 cp_parser_parse_tentatively (parser
);
15194 /* It is either a simple-template-id representing an
15195 instantiation of an alias template... */
15196 type_decl
= cp_parser_template_id (parser
,
15197 /*template_keyword_p=*/false,
15198 /*check_dependency_p=*/true,
15200 /*is_declaration=*/false);
15201 /* Note that this must be an instantiation of an alias template
15202 because [temp.names]/6 says:
15204 A template-id that names an alias template specialization
15207 Whereas [temp.names]/7 says:
15209 A simple-template-id that names a class template
15210 specialization is a class-name. */
15211 if (type_decl
!= NULL_TREE
15212 && TREE_CODE (type_decl
) == TYPE_DECL
15213 && TYPE_DECL_ALIAS_P (type_decl
))
15214 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
15216 cp_parser_simulate_error (parser
);
15218 if (!cp_parser_parse_definitely (parser
))
15219 /* ... Or a typedef-name or an enum-name. */
15220 return cp_parser_nonclass_name (parser
);
15226 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15234 Returns a TYPE_DECL for the type. */
15237 cp_parser_nonclass_name (cp_parser
* parser
)
15242 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15243 identifier
= cp_parser_identifier (parser
);
15244 if (identifier
== error_mark_node
)
15245 return error_mark_node
;
15247 /* Look up the type-name. */
15248 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
15250 type_decl
= strip_using_decl (type_decl
);
15252 if (TREE_CODE (type_decl
) != TYPE_DECL
15253 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
15255 /* See if this is an Objective-C type. */
15256 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
15257 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
15259 type_decl
= TYPE_NAME (type
);
15262 /* Issue an error if we did not find a type-name. */
15263 if (TREE_CODE (type_decl
) != TYPE_DECL
15264 /* In Objective-C, we have the complication that class names are
15265 normally type names and start declarations (eg, the
15266 "NSObject" in "NSObject *object;"), but can be used in an
15267 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15268 is an expression. So, a classname followed by a dot is not a
15269 valid type-name. */
15270 || (objc_is_class_name (TREE_TYPE (type_decl
))
15271 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
15273 if (!cp_parser_simulate_error (parser
))
15274 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
15275 NLE_TYPE
, token
->location
);
15276 return error_mark_node
;
15278 /* Remember that the name was used in the definition of the
15279 current class so that we can check later to see if the
15280 meaning would have been different after the class was
15281 entirely defined. */
15282 else if (type_decl
!= error_mark_node
15284 maybe_note_name_used_in_class (identifier
, type_decl
);
15289 /* Parse an elaborated-type-specifier. Note that the grammar given
15290 here incorporates the resolution to DR68.
15292 elaborated-type-specifier:
15293 class-key :: [opt] nested-name-specifier [opt] identifier
15294 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15295 enum-key :: [opt] nested-name-specifier [opt] identifier
15296 typename :: [opt] nested-name-specifier identifier
15297 typename :: [opt] nested-name-specifier template [opt]
15302 elaborated-type-specifier:
15303 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15304 class-key attributes :: [opt] nested-name-specifier [opt]
15305 template [opt] template-id
15306 enum attributes :: [opt] nested-name-specifier [opt] identifier
15308 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15309 declared `friend'. If IS_DECLARATION is TRUE, then this
15310 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15311 something is being declared.
15313 Returns the TYPE specified. */
15316 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
15318 bool is_declaration
)
15320 enum tag_types tag_type
;
15322 tree type
= NULL_TREE
;
15323 tree attributes
= NULL_TREE
;
15325 cp_token
*token
= NULL
;
15327 /* See if we're looking at the `enum' keyword. */
15328 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
15330 /* Consume the `enum' token. */
15331 cp_lexer_consume_token (parser
->lexer
);
15332 /* Remember that it's an enumeration type. */
15333 tag_type
= enum_type
;
15334 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15335 enums) is used here. */
15336 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15337 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15339 pedwarn (input_location
, 0, "elaborated-type-specifier "
15340 "for a scoped enum must not use the %<%D%> keyword",
15341 cp_lexer_peek_token (parser
->lexer
)->u
.value
);
15342 /* Consume the `struct' or `class' and parse it anyway. */
15343 cp_lexer_consume_token (parser
->lexer
);
15345 /* Parse the attributes. */
15346 attributes
= cp_parser_attributes_opt (parser
);
15348 /* Or, it might be `typename'. */
15349 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
15352 /* Consume the `typename' token. */
15353 cp_lexer_consume_token (parser
->lexer
);
15354 /* Remember that it's a `typename' type. */
15355 tag_type
= typename_type
;
15357 /* Otherwise it must be a class-key. */
15360 tag_type
= cp_parser_class_key (parser
);
15361 if (tag_type
== none_type
)
15362 return error_mark_node
;
15363 /* Parse the attributes. */
15364 attributes
= cp_parser_attributes_opt (parser
);
15367 /* Look for the `::' operator. */
15368 globalscope
= cp_parser_global_scope_opt (parser
,
15369 /*current_scope_valid_p=*/false);
15370 /* Look for the nested-name-specifier. */
15371 if (tag_type
== typename_type
&& !globalscope
)
15373 if (!cp_parser_nested_name_specifier (parser
,
15374 /*typename_keyword_p=*/true,
15375 /*check_dependency_p=*/true,
15378 return error_mark_node
;
15381 /* Even though `typename' is not present, the proposed resolution
15382 to Core Issue 180 says that in `class A<T>::B', `B' should be
15383 considered a type-name, even if `A<T>' is dependent. */
15384 cp_parser_nested_name_specifier_opt (parser
,
15385 /*typename_keyword_p=*/true,
15386 /*check_dependency_p=*/true,
15389 /* For everything but enumeration types, consider a template-id.
15390 For an enumeration type, consider only a plain identifier. */
15391 if (tag_type
!= enum_type
)
15393 bool template_p
= false;
15396 /* Allow the `template' keyword. */
15397 template_p
= cp_parser_optional_template_keyword (parser
);
15398 /* If we didn't see `template', we don't know if there's a
15399 template-id or not. */
15401 cp_parser_parse_tentatively (parser
);
15402 /* Parse the template-id. */
15403 token
= cp_lexer_peek_token (parser
->lexer
);
15404 decl
= cp_parser_template_id (parser
, template_p
,
15405 /*check_dependency_p=*/true,
15408 /* If we didn't find a template-id, look for an ordinary
15410 if (!template_p
&& !cp_parser_parse_definitely (parser
))
15412 /* We can get here when cp_parser_template_id, called by
15413 cp_parser_class_name with tag_type == none_type, succeeds
15414 and caches a BASELINK. Then, when called again here,
15415 instead of failing and returning an error_mark_node
15416 returns it (see template/typename17.C in C++11).
15417 ??? Could we diagnose this earlier? */
15418 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
15420 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
15421 type
= error_mark_node
;
15423 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15424 in effect, then we must assume that, upon instantiation, the
15425 template will correspond to a class. */
15426 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
15427 && tag_type
== typename_type
)
15428 type
= make_typename_type (parser
->scope
, decl
,
15430 /*complain=*/tf_error
);
15431 /* If the `typename' keyword is in effect and DECL is not a type
15432 decl, then type is non existent. */
15433 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
15435 else if (TREE_CODE (decl
) == TYPE_DECL
)
15436 type
= check_elaborated_type_specifier (tag_type
, decl
,
15437 /*allow_template_p=*/true);
15438 else if (decl
== error_mark_node
)
15439 type
= error_mark_node
;
15444 token
= cp_lexer_peek_token (parser
->lexer
);
15445 identifier
= cp_parser_identifier (parser
);
15447 if (identifier
== error_mark_node
)
15449 parser
->scope
= NULL_TREE
;
15450 return error_mark_node
;
15453 /* For a `typename', we needn't call xref_tag. */
15454 if (tag_type
== typename_type
15455 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
15456 return cp_parser_make_typename_type (parser
, identifier
,
15459 /* Template parameter lists apply only if we are not within a
15460 function parameter list. */
15461 bool template_parm_lists_apply
15462 = parser
->num_template_parameter_lists
;
15463 if (template_parm_lists_apply
)
15464 for (cp_binding_level
*s
= current_binding_level
;
15465 s
&& s
->kind
!= sk_template_parms
;
15466 s
= s
->level_chain
)
15467 if (s
->kind
== sk_function_parms
)
15468 template_parm_lists_apply
= false;
15470 /* Look up a qualified name in the usual way. */
15474 tree ambiguous_decls
;
15476 decl
= cp_parser_lookup_name (parser
, identifier
,
15478 /*is_template=*/false,
15479 /*is_namespace=*/false,
15480 /*check_dependency=*/true,
15484 /* If the lookup was ambiguous, an error will already have been
15486 if (ambiguous_decls
)
15487 return error_mark_node
;
15489 /* If we are parsing friend declaration, DECL may be a
15490 TEMPLATE_DECL tree node here. However, we need to check
15491 whether this TEMPLATE_DECL results in valid code. Consider
15492 the following example:
15495 template <class T> class C {};
15498 template <class T> friend class N::C; // #1, valid code
15500 template <class T> class Y {
15501 friend class N::C; // #2, invalid code
15504 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15505 name lookup of `N::C'. We see that friend declaration must
15506 be template for the code to be valid. Note that
15507 processing_template_decl does not work here since it is
15508 always 1 for the above two cases. */
15510 decl
= (cp_parser_maybe_treat_template_as_class
15511 (decl
, /*tag_name_p=*/is_friend
15512 && template_parm_lists_apply
));
15514 if (TREE_CODE (decl
) != TYPE_DECL
)
15516 cp_parser_diagnose_invalid_type_name (parser
,
15519 return error_mark_node
;
15522 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
15524 bool allow_template
= (template_parm_lists_apply
15525 || DECL_SELF_REFERENCE_P (decl
));
15526 type
= check_elaborated_type_specifier (tag_type
, decl
,
15529 if (type
== error_mark_node
)
15530 return error_mark_node
;
15533 /* Forward declarations of nested types, such as
15538 are invalid unless all components preceding the final '::'
15539 are complete. If all enclosing types are complete, these
15540 declarations become merely pointless.
15542 Invalid forward declarations of nested types are errors
15543 caught elsewhere in parsing. Those that are pointless arrive
15546 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
15547 && !is_friend
&& !processing_explicit_instantiation
)
15548 warning (0, "declaration %qD does not declare anything", decl
);
15550 type
= TREE_TYPE (decl
);
15554 /* An elaborated-type-specifier sometimes introduces a new type and
15555 sometimes names an existing type. Normally, the rule is that it
15556 introduces a new type only if there is not an existing type of
15557 the same name already in scope. For example, given:
15560 void f() { struct S s; }
15562 the `struct S' in the body of `f' is the same `struct S' as in
15563 the global scope; the existing definition is used. However, if
15564 there were no global declaration, this would introduce a new
15565 local class named `S'.
15567 An exception to this rule applies to the following code:
15569 namespace N { struct S; }
15571 Here, the elaborated-type-specifier names a new type
15572 unconditionally; even if there is already an `S' in the
15573 containing scope this declaration names a new type.
15574 This exception only applies if the elaborated-type-specifier
15575 forms the complete declaration:
15579 A declaration consisting solely of `class-key identifier ;' is
15580 either a redeclaration of the name in the current scope or a
15581 forward declaration of the identifier as a class name. It
15582 introduces the name into the current scope.
15584 We are in this situation precisely when the next token is a `;'.
15586 An exception to the exception is that a `friend' declaration does
15587 *not* name a new type; i.e., given:
15589 struct S { friend struct T; };
15591 `T' is not a new type in the scope of `S'.
15593 Also, `new struct S' or `sizeof (struct S)' never results in the
15594 definition of a new type; a new type can only be declared in a
15595 declaration context. */
15601 /* Friends have special name lookup rules. */
15602 ts
= ts_within_enclosing_non_class
;
15603 else if (is_declaration
15604 && cp_lexer_next_token_is (parser
->lexer
,
15606 /* This is a `class-key identifier ;' */
15612 (template_parm_lists_apply
15613 && (cp_parser_next_token_starts_class_definition_p (parser
)
15614 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
15615 /* An unqualified name was used to reference this type, so
15616 there were no qualifying templates. */
15617 if (template_parm_lists_apply
15618 && !cp_parser_check_template_parameters (parser
,
15619 /*num_templates=*/0,
15621 /*declarator=*/NULL
))
15622 return error_mark_node
;
15623 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
15627 if (type
== error_mark_node
)
15628 return error_mark_node
;
15630 /* Allow attributes on forward declarations of classes. */
15633 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15634 warning (OPT_Wattributes
,
15635 "attributes ignored on uninstantiated type");
15636 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
15637 && ! processing_explicit_instantiation
)
15638 warning (OPT_Wattributes
,
15639 "attributes ignored on template instantiation");
15640 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
15641 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
15643 warning (OPT_Wattributes
,
15644 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15647 if (tag_type
!= enum_type
)
15649 /* Indicate whether this class was declared as a `class' or as a
15651 if (TREE_CODE (type
) == RECORD_TYPE
)
15652 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
15653 cp_parser_check_class_key (tag_type
, type
);
15656 /* A "<" cannot follow an elaborated type specifier. If that
15657 happens, the user was probably trying to form a template-id. */
15658 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
15664 /* Parse an enum-specifier.
15667 enum-head { enumerator-list [opt] }
15668 enum-head { enumerator-list , } [C++0x]
15671 enum-key identifier [opt] enum-base [opt]
15672 enum-key nested-name-specifier identifier enum-base [opt]
15677 enum struct [C++0x]
15680 : type-specifier-seq
15682 opaque-enum-specifier:
15683 enum-key identifier enum-base [opt] ;
15686 enum-key attributes[opt] identifier [opt] enum-base [opt]
15687 { enumerator-list [opt] }attributes[opt]
15688 enum-key attributes[opt] identifier [opt] enum-base [opt]
15689 { enumerator-list, }attributes[opt] [C++0x]
15691 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15692 if the token stream isn't an enum-specifier after all. */
15695 cp_parser_enum_specifier (cp_parser
* parser
)
15698 tree type
= NULL_TREE
;
15700 tree nested_name_specifier
= NULL_TREE
;
15702 bool scoped_enum_p
= false;
15703 bool has_underlying_type
= false;
15704 bool nested_being_defined
= false;
15705 bool new_value_list
= false;
15706 bool is_new_type
= false;
15707 bool is_anonymous
= false;
15708 tree underlying_type
= NULL_TREE
;
15709 cp_token
*type_start_token
= NULL
;
15710 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
15712 parser
->colon_corrects_to_scope_p
= false;
15714 /* Parse tentatively so that we can back up if we don't find a
15716 cp_parser_parse_tentatively (parser
);
15718 /* Caller guarantees that the current token is 'enum', an identifier
15719 possibly follows, and the token after that is an opening brace.
15720 If we don't have an identifier, fabricate an anonymous name for
15721 the enumeration being defined. */
15722 cp_lexer_consume_token (parser
->lexer
);
15724 /* Parse the "class" or "struct", which indicates a scoped
15725 enumeration type in C++0x. */
15726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
15727 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
15729 if (cxx_dialect
< cxx11
)
15730 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15732 /* Consume the `struct' or `class' token. */
15733 cp_lexer_consume_token (parser
->lexer
);
15735 scoped_enum_p
= true;
15738 attributes
= cp_parser_attributes_opt (parser
);
15740 /* Clear the qualification. */
15741 parser
->scope
= NULL_TREE
;
15742 parser
->qualifying_scope
= NULL_TREE
;
15743 parser
->object_scope
= NULL_TREE
;
15745 /* Figure out in what scope the declaration is being placed. */
15746 prev_scope
= current_scope ();
15748 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
15750 push_deferring_access_checks (dk_no_check
);
15751 nested_name_specifier
15752 = cp_parser_nested_name_specifier_opt (parser
,
15753 /*typename_keyword_p=*/true,
15754 /*check_dependency_p=*/false,
15756 /*is_declaration=*/false);
15758 if (nested_name_specifier
)
15762 identifier
= cp_parser_identifier (parser
);
15763 name
= cp_parser_lookup_name (parser
, identifier
,
15765 /*is_template=*/false,
15766 /*is_namespace=*/false,
15767 /*check_dependency=*/true,
15768 /*ambiguous_decls=*/NULL
,
15770 if (name
&& name
!= error_mark_node
)
15772 type
= TREE_TYPE (name
);
15773 if (TREE_CODE (type
) == TYPENAME_TYPE
)
15775 /* Are template enums allowed in ISO? */
15776 if (template_parm_scope_p ())
15777 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15778 "%qD is an enumeration template", name
);
15779 /* ignore a typename reference, for it will be solved by name
15784 else if (nested_name_specifier
== error_mark_node
)
15785 /* We already issued an error. */;
15787 error_at (type_start_token
->location
,
15788 "%qD is not an enumerator-name", identifier
);
15792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
15793 identifier
= cp_parser_identifier (parser
);
15796 identifier
= make_anon_name ();
15797 is_anonymous
= true;
15799 error_at (type_start_token
->location
,
15800 "anonymous scoped enum is not allowed");
15803 pop_deferring_access_checks ();
15805 /* Check for the `:' that denotes a specified underlying type in C++0x.
15806 Note that a ':' could also indicate a bitfield width, however. */
15807 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
15809 cp_decl_specifier_seq type_specifiers
;
15811 /* Consume the `:'. */
15812 cp_lexer_consume_token (parser
->lexer
);
15814 /* Parse the type-specifier-seq. */
15815 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
15816 /*is_trailing_return=*/false,
15819 /* At this point this is surely not elaborated type specifier. */
15820 if (!cp_parser_parse_definitely (parser
))
15823 if (cxx_dialect
< cxx11
)
15824 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
15826 has_underlying_type
= true;
15828 /* If that didn't work, stop. */
15829 if (type_specifiers
.type
!= error_mark_node
)
15831 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
15832 /*initialized=*/0, NULL
);
15833 if (underlying_type
== error_mark_node
15834 || check_for_bare_parameter_packs (underlying_type
))
15835 underlying_type
= NULL_TREE
;
15839 /* Look for the `{' but don't consume it yet. */
15840 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15842 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
15844 cp_parser_error (parser
, "expected %<{%>");
15845 if (has_underlying_type
)
15851 /* An opaque-enum-specifier must have a ';' here. */
15852 if ((scoped_enum_p
|| underlying_type
)
15853 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
15855 cp_parser_error (parser
, "expected %<;%> or %<{%>");
15856 if (has_underlying_type
)
15864 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
15867 if (nested_name_specifier
)
15869 if (CLASS_TYPE_P (nested_name_specifier
))
15871 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
15872 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
15873 push_scope (nested_name_specifier
);
15875 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
15877 push_nested_namespace (nested_name_specifier
);
15881 /* Issue an error message if type-definitions are forbidden here. */
15882 if (!cp_parser_check_type_definition (parser
))
15883 type
= error_mark_node
;
15885 /* Create the new type. We do this before consuming the opening
15886 brace so the enum will be recorded as being on the line of its
15887 tag (or the 'enum' keyword, if there is no tag). */
15888 type
= start_enum (identifier
, type
, underlying_type
,
15889 scoped_enum_p
, &is_new_type
);
15891 /* If the next token is not '{' it is an opaque-enum-specifier or an
15892 elaborated-type-specifier. */
15893 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15895 timevar_push (TV_PARSE_ENUM
);
15896 if (nested_name_specifier
15897 && nested_name_specifier
!= error_mark_node
)
15899 /* The following catches invalid code such as:
15900 enum class S<int>::E { A, B, C }; */
15901 if (!processing_specialization
15902 && CLASS_TYPE_P (nested_name_specifier
)
15903 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
15904 error_at (type_start_token
->location
, "cannot add an enumerator "
15905 "list to a template instantiation");
15907 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
15909 error_at (type_start_token
->location
,
15910 "%<%T::%E%> has not been declared",
15911 TYPE_CONTEXT (nested_name_specifier
),
15912 nested_name_specifier
);
15913 type
= error_mark_node
;
15915 /* If that scope does not contain the scope in which the
15916 class was originally declared, the program is invalid. */
15917 else if (prev_scope
&& !is_ancestor (prev_scope
,
15918 nested_name_specifier
))
15920 if (at_namespace_scope_p ())
15921 error_at (type_start_token
->location
,
15922 "declaration of %qD in namespace %qD which does not "
15924 type
, prev_scope
, nested_name_specifier
);
15926 error_at (type_start_token
->location
,
15927 "declaration of %qD in %qD which does not "
15929 type
, prev_scope
, nested_name_specifier
);
15930 type
= error_mark_node
;
15935 begin_scope (sk_scoped_enum
, type
);
15937 /* Consume the opening brace. */
15938 cp_lexer_consume_token (parser
->lexer
);
15940 if (type
== error_mark_node
)
15941 ; /* Nothing to add */
15942 else if (OPAQUE_ENUM_P (type
)
15943 || (cxx_dialect
> cxx98
&& processing_specialization
))
15945 new_value_list
= true;
15946 SET_OPAQUE_ENUM_P (type
, false);
15947 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
15951 error_at (type_start_token
->location
,
15952 "multiple definition of %q#T", type
);
15953 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
15954 "previous definition here");
15955 type
= error_mark_node
;
15958 if (type
== error_mark_node
)
15959 cp_parser_skip_to_end_of_block_or_statement (parser
);
15960 /* If the next token is not '}', then there are some enumerators. */
15961 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
15963 if (is_anonymous
&& !scoped_enum_p
)
15964 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
15965 "ISO C++ forbids empty anonymous enum");
15968 cp_parser_enumerator_list (parser
, type
);
15970 /* Consume the final '}'. */
15971 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
15975 timevar_pop (TV_PARSE_ENUM
);
15979 /* If a ';' follows, then it is an opaque-enum-specifier
15980 and additional restrictions apply. */
15981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
15984 error_at (type_start_token
->location
,
15985 "opaque-enum-specifier without name");
15986 else if (nested_name_specifier
)
15987 error_at (type_start_token
->location
,
15988 "opaque-enum-specifier must use a simple identifier");
15992 /* Look for trailing attributes to apply to this enumeration, and
15993 apply them if appropriate. */
15994 if (cp_parser_allow_gnu_extensions_p (parser
))
15996 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
15997 trailing_attr
= chainon (trailing_attr
, attributes
);
15998 cplus_decl_attributes (&type
,
16000 (int) ATTR_FLAG_TYPE_IN_PLACE
);
16003 /* Finish up the enumeration. */
16004 if (type
!= error_mark_node
)
16006 if (new_value_list
)
16007 finish_enum_value_list (type
);
16009 finish_enum (type
);
16012 if (nested_name_specifier
)
16014 if (CLASS_TYPE_P (nested_name_specifier
))
16016 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
16017 pop_scope (nested_name_specifier
);
16019 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
16021 pop_nested_namespace (nested_name_specifier
);
16025 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
16029 /* Parse an enumerator-list. The enumerators all have the indicated
16033 enumerator-definition
16034 enumerator-list , enumerator-definition */
16037 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
16041 /* Parse an enumerator-definition. */
16042 cp_parser_enumerator_definition (parser
, type
);
16044 /* If the next token is not a ',', we've reached the end of
16046 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16048 /* Otherwise, consume the `,' and keep going. */
16049 cp_lexer_consume_token (parser
->lexer
);
16050 /* If the next token is a `}', there is a trailing comma. */
16051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
16053 if (cxx_dialect
< cxx11
&& !in_system_header_at (input_location
))
16054 pedwarn (input_location
, OPT_Wpedantic
,
16055 "comma at end of enumerator list");
16061 /* Parse an enumerator-definition. The enumerator has the indicated
16064 enumerator-definition:
16066 enumerator = constant-expression
16072 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
16078 /* Save the input location because we are interested in the location
16079 of the identifier and not the location of the explicit value. */
16080 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16082 /* Look for the identifier. */
16083 identifier
= cp_parser_identifier (parser
);
16084 if (identifier
== error_mark_node
)
16087 /* If the next token is an '=', then there is an explicit value. */
16088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16090 /* Consume the `=' token. */
16091 cp_lexer_consume_token (parser
->lexer
);
16092 /* Parse the value. */
16093 value
= cp_parser_constant_expression (parser
);
16098 /* If we are processing a template, make sure the initializer of the
16099 enumerator doesn't contain any bare template parameter pack. */
16100 if (check_for_bare_parameter_packs (value
))
16101 value
= error_mark_node
;
16103 /* Create the enumerator. */
16104 build_enumerator (identifier
, value
, type
, loc
);
16107 /* Parse a namespace-name.
16110 original-namespace-name
16113 Returns the NAMESPACE_DECL for the namespace. */
16116 cp_parser_namespace_name (cp_parser
* parser
)
16119 tree namespace_decl
;
16121 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16123 /* Get the name of the namespace. */
16124 identifier
= cp_parser_identifier (parser
);
16125 if (identifier
== error_mark_node
)
16126 return error_mark_node
;
16128 /* Look up the identifier in the currently active scope. Look only
16129 for namespaces, due to:
16131 [basic.lookup.udir]
16133 When looking up a namespace-name in a using-directive or alias
16134 definition, only namespace names are considered.
16138 [basic.lookup.qual]
16140 During the lookup of a name preceding the :: scope resolution
16141 operator, object, function, and enumerator names are ignored.
16143 (Note that cp_parser_qualifying_entity only calls this
16144 function if the token after the name is the scope resolution
16146 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
16148 /*is_template=*/false,
16149 /*is_namespace=*/true,
16150 /*check_dependency=*/true,
16151 /*ambiguous_decls=*/NULL
,
16153 /* If it's not a namespace, issue an error. */
16154 if (namespace_decl
== error_mark_node
16155 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
16157 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
16158 error_at (token
->location
, "%qD is not a namespace-name", identifier
);
16159 cp_parser_error (parser
, "expected namespace-name");
16160 namespace_decl
= error_mark_node
;
16163 return namespace_decl
;
16166 /* Parse a namespace-definition.
16168 namespace-definition:
16169 named-namespace-definition
16170 unnamed-namespace-definition
16172 named-namespace-definition:
16173 original-namespace-definition
16174 extension-namespace-definition
16176 original-namespace-definition:
16177 namespace identifier { namespace-body }
16179 extension-namespace-definition:
16180 namespace original-namespace-name { namespace-body }
16182 unnamed-namespace-definition:
16183 namespace { namespace-body } */
16186 cp_parser_namespace_definition (cp_parser
* parser
)
16188 tree identifier
, attribs
;
16189 bool has_visibility
;
16192 cp_ensure_no_omp_declare_simd (parser
);
16193 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
))
16195 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
16197 cp_lexer_consume_token (parser
->lexer
);
16202 /* Look for the `namespace' keyword. */
16203 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16205 /* Get the name of the namespace. We do not attempt to distinguish
16206 between an original-namespace-definition and an
16207 extension-namespace-definition at this point. The semantic
16208 analysis routines are responsible for that. */
16209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16210 identifier
= cp_parser_identifier (parser
);
16212 identifier
= NULL_TREE
;
16214 /* Parse any specified attributes. */
16215 attribs
= cp_parser_attributes_opt (parser
);
16217 /* Look for the `{' to start the namespace. */
16218 cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
);
16219 /* Start the namespace. */
16220 push_namespace (identifier
);
16222 /* "inline namespace" is equivalent to a stub namespace definition
16223 followed by a strong using directive. */
16226 tree name_space
= current_namespace
;
16227 /* Set up namespace association. */
16228 DECL_NAMESPACE_ASSOCIATIONS (name_space
)
16229 = tree_cons (CP_DECL_CONTEXT (name_space
), NULL_TREE
,
16230 DECL_NAMESPACE_ASSOCIATIONS (name_space
));
16231 /* Import the contents of the inline namespace. */
16233 do_using_directive (name_space
);
16234 push_namespace (identifier
);
16237 has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
16239 /* Parse the body of the namespace. */
16240 cp_parser_namespace_body (parser
);
16242 if (has_visibility
)
16243 pop_visibility (1);
16245 /* Finish the namespace. */
16247 /* Look for the final `}'. */
16248 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
16251 /* Parse a namespace-body.
16254 declaration-seq [opt] */
16257 cp_parser_namespace_body (cp_parser
* parser
)
16259 cp_parser_declaration_seq_opt (parser
);
16262 /* Parse a namespace-alias-definition.
16264 namespace-alias-definition:
16265 namespace identifier = qualified-namespace-specifier ; */
16268 cp_parser_namespace_alias_definition (cp_parser
* parser
)
16271 tree namespace_specifier
;
16273 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16275 /* Look for the `namespace' keyword. */
16276 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16277 /* Look for the identifier. */
16278 identifier
= cp_parser_identifier (parser
);
16279 if (identifier
== error_mark_node
)
16281 /* Look for the `=' token. */
16282 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
16283 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
16285 error_at (token
->location
, "%<namespace%> definition is not allowed here");
16286 /* Skip the definition. */
16287 cp_lexer_consume_token (parser
->lexer
);
16288 if (cp_parser_skip_to_closing_brace (parser
))
16289 cp_lexer_consume_token (parser
->lexer
);
16292 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16293 /* Look for the qualified-namespace-specifier. */
16294 namespace_specifier
16295 = cp_parser_qualified_namespace_specifier (parser
);
16296 /* Look for the `;' token. */
16297 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16299 /* Register the alias in the symbol table. */
16300 do_namespace_alias (identifier
, namespace_specifier
);
16303 /* Parse a qualified-namespace-specifier.
16305 qualified-namespace-specifier:
16306 :: [opt] nested-name-specifier [opt] namespace-name
16308 Returns a NAMESPACE_DECL corresponding to the specified
16312 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
16314 /* Look for the optional `::'. */
16315 cp_parser_global_scope_opt (parser
,
16316 /*current_scope_valid_p=*/false);
16318 /* Look for the optional nested-name-specifier. */
16319 cp_parser_nested_name_specifier_opt (parser
,
16320 /*typename_keyword_p=*/false,
16321 /*check_dependency_p=*/true,
16323 /*is_declaration=*/true);
16325 return cp_parser_namespace_name (parser
);
16328 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16329 access declaration.
16332 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16333 using :: unqualified-id ;
16335 access-declaration:
16341 cp_parser_using_declaration (cp_parser
* parser
,
16342 bool access_declaration_p
)
16345 bool typename_p
= false;
16346 bool global_scope_p
;
16350 int oldcount
= errorcount
;
16351 cp_token
*diag_token
= NULL
;
16353 if (access_declaration_p
)
16355 diag_token
= cp_lexer_peek_token (parser
->lexer
);
16356 cp_parser_parse_tentatively (parser
);
16360 /* Look for the `using' keyword. */
16361 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16363 /* Peek at the next token. */
16364 token
= cp_lexer_peek_token (parser
->lexer
);
16365 /* See if it's `typename'. */
16366 if (token
->keyword
== RID_TYPENAME
)
16368 /* Remember that we've seen it. */
16370 /* Consume the `typename' token. */
16371 cp_lexer_consume_token (parser
->lexer
);
16375 /* Look for the optional global scope qualification. */
16377 = (cp_parser_global_scope_opt (parser
,
16378 /*current_scope_valid_p=*/false)
16381 /* If we saw `typename', or didn't see `::', then there must be a
16382 nested-name-specifier present. */
16383 if (typename_p
|| !global_scope_p
)
16385 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
16386 /*check_dependency_p=*/true,
16388 /*is_declaration=*/true);
16389 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
16391 cp_parser_skip_to_end_of_block_or_statement (parser
);
16395 /* Otherwise, we could be in either of the two productions. In that
16396 case, treat the nested-name-specifier as optional. */
16398 qscope
= cp_parser_nested_name_specifier_opt (parser
,
16399 /*typename_keyword_p=*/false,
16400 /*check_dependency_p=*/true,
16402 /*is_declaration=*/true);
16404 qscope
= global_namespace
;
16405 else if (UNSCOPED_ENUM_P (qscope
))
16406 qscope
= CP_TYPE_CONTEXT (qscope
);
16408 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
16409 /* Something has already gone wrong; there's no need to parse
16410 further. Since an error has occurred, the return value of
16411 cp_parser_parse_definitely will be false, as required. */
16412 return cp_parser_parse_definitely (parser
);
16414 token
= cp_lexer_peek_token (parser
->lexer
);
16415 /* Parse the unqualified-id. */
16416 identifier
= cp_parser_unqualified_id (parser
,
16417 /*template_keyword_p=*/false,
16418 /*check_dependency_p=*/true,
16419 /*declarator_p=*/true,
16420 /*optional_p=*/false);
16422 if (access_declaration_p
)
16424 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
16425 cp_parser_simulate_error (parser
);
16426 if (!cp_parser_parse_definitely (parser
))
16430 /* The function we call to handle a using-declaration is different
16431 depending on what scope we are in. */
16432 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
16434 else if (!identifier_p (identifier
)
16435 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
16436 /* [namespace.udecl]
16438 A using declaration shall not name a template-id. */
16439 error_at (token
->location
,
16440 "a template-id may not appear in a using-declaration");
16443 if (at_class_scope_p ())
16445 /* Create the USING_DECL. */
16446 decl
= do_class_using_decl (parser
->scope
, identifier
);
16448 if (decl
&& typename_p
)
16449 USING_DECL_TYPENAME_P (decl
) = 1;
16451 if (check_for_bare_parameter_packs (decl
))
16453 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16457 /* Add it to the list of members in this class. */
16458 finish_member_declaration (decl
);
16462 decl
= cp_parser_lookup_name_simple (parser
,
16465 if (decl
== error_mark_node
)
16466 cp_parser_name_lookup_error (parser
, identifier
,
16469 else if (check_for_bare_parameter_packs (decl
))
16471 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16474 else if (!at_namespace_scope_p ())
16475 do_local_using_decl (decl
, qscope
, identifier
);
16477 do_toplevel_using_decl (decl
, qscope
, identifier
);
16481 /* Look for the final `;'. */
16482 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16484 if (access_declaration_p
&& errorcount
== oldcount
)
16485 warning_at (diag_token
->location
, OPT_Wdeprecated
,
16486 "access declarations are deprecated "
16487 "in favour of using-declarations; "
16488 "suggestion: add the %<using%> keyword");
16493 /* Parse an alias-declaration.
16496 using identifier attribute-specifier-seq [opt] = type-id */
16499 cp_parser_alias_declaration (cp_parser
* parser
)
16501 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
16502 location_t id_location
;
16503 cp_declarator
*declarator
;
16504 cp_decl_specifier_seq decl_specs
;
16506 const char *saved_message
= NULL
;
16508 /* Look for the `using' keyword. */
16509 cp_token
*using_token
16510 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16511 if (using_token
== NULL
)
16512 return error_mark_node
;
16514 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
16515 id
= cp_parser_identifier (parser
);
16516 if (id
== error_mark_node
)
16517 return error_mark_node
;
16519 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
16520 attributes
= cp_parser_attributes_opt (parser
);
16521 if (attributes
== error_mark_node
)
16522 return error_mark_node
;
16524 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
16526 if (cp_parser_error_occurred (parser
))
16527 return error_mark_node
;
16529 cp_parser_commit_to_tentative_parse (parser
);
16531 /* Now we are going to parse the type-id of the declaration. */
16536 "A type-specifier-seq shall not define a class or enumeration
16537 unless it appears in the type-id of an alias-declaration (7.1.3) that
16538 is not the declaration of a template-declaration."
16540 In other words, if we currently are in an alias template, the
16541 type-id should not define a type.
16543 So let's set parser->type_definition_forbidden_message in that
16544 case; cp_parser_check_type_definition (called by
16545 cp_parser_class_specifier) will then emit an error if a type is
16546 defined in the type-id. */
16547 if (parser
->num_template_parameter_lists
)
16549 saved_message
= parser
->type_definition_forbidden_message
;
16550 parser
->type_definition_forbidden_message
=
16551 G_("types may not be defined in alias template declarations");
16554 type
= cp_parser_type_id (parser
);
16556 /* Restore the error message if need be. */
16557 if (parser
->num_template_parameter_lists
)
16558 parser
->type_definition_forbidden_message
= saved_message
;
16560 if (type
== error_mark_node
16561 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
16563 cp_parser_skip_to_end_of_block_or_statement (parser
);
16564 return error_mark_node
;
16567 /* A typedef-name can also be introduced by an alias-declaration. The
16568 identifier following the using keyword becomes a typedef-name. It has
16569 the same semantics as if it were introduced by the typedef
16570 specifier. In particular, it does not define a new type and it shall
16571 not appear in the type-id. */
16573 clear_decl_specs (&decl_specs
);
16574 decl_specs
.type
= type
;
16575 if (attributes
!= NULL_TREE
)
16577 decl_specs
.attributes
= attributes
;
16578 set_and_check_decl_spec_loc (&decl_specs
,
16582 set_and_check_decl_spec_loc (&decl_specs
,
16585 set_and_check_decl_spec_loc (&decl_specs
,
16589 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
);
16590 declarator
->id_loc
= id_location
;
16592 member_p
= at_class_scope_p ();
16594 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
16595 NULL_TREE
, attributes
);
16597 decl
= start_decl (declarator
, &decl_specs
, 0,
16598 attributes
, NULL_TREE
, &pushed_scope
);
16599 if (decl
== error_mark_node
)
16602 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
16605 pop_scope (pushed_scope
);
16607 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16608 added into the symbol table; otherwise, return the TYPE_DECL. */
16609 if (DECL_LANG_SPECIFIC (decl
)
16610 && DECL_TEMPLATE_INFO (decl
)
16611 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
16613 decl
= DECL_TI_TEMPLATE (decl
);
16615 check_member_template (decl
);
16621 /* Parse a using-directive.
16624 using namespace :: [opt] nested-name-specifier [opt]
16625 namespace-name ; */
16628 cp_parser_using_directive (cp_parser
* parser
)
16630 tree namespace_decl
;
16633 /* Look for the `using' keyword. */
16634 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
16635 /* And the `namespace' keyword. */
16636 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
16637 /* Look for the optional `::' operator. */
16638 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
16639 /* And the optional nested-name-specifier. */
16640 cp_parser_nested_name_specifier_opt (parser
,
16641 /*typename_keyword_p=*/false,
16642 /*check_dependency_p=*/true,
16644 /*is_declaration=*/true);
16645 /* Get the namespace being used. */
16646 namespace_decl
= cp_parser_namespace_name (parser
);
16647 /* And any specified attributes. */
16648 attribs
= cp_parser_attributes_opt (parser
);
16649 /* Update the symbol table. */
16650 parse_using_directive (namespace_decl
, attribs
);
16651 /* Look for the final `;'. */
16652 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16655 /* Parse an asm-definition.
16658 asm ( string-literal ) ;
16663 asm volatile [opt] ( string-literal ) ;
16664 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16665 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16666 : asm-operand-list [opt] ) ;
16667 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16668 : asm-operand-list [opt]
16669 : asm-clobber-list [opt] ) ;
16670 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16671 : asm-clobber-list [opt]
16672 : asm-goto-list ) ; */
16675 cp_parser_asm_definition (cp_parser
* parser
)
16678 tree outputs
= NULL_TREE
;
16679 tree inputs
= NULL_TREE
;
16680 tree clobbers
= NULL_TREE
;
16681 tree labels
= NULL_TREE
;
16683 bool volatile_p
= false;
16684 bool extended_p
= false;
16685 bool invalid_inputs_p
= false;
16686 bool invalid_outputs_p
= false;
16687 bool goto_p
= false;
16688 required_token missing
= RT_NONE
;
16690 /* Look for the `asm' keyword. */
16691 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
16693 if (parser
->in_function_body
16694 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
16696 error ("%<asm%> in %<constexpr%> function");
16697 cp_function_chain
->invalid_constexpr
= true;
16700 /* See if the next token is `volatile'. */
16701 if (cp_parser_allow_gnu_extensions_p (parser
)
16702 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_VOLATILE
))
16704 /* Remember that we saw the `volatile' keyword. */
16706 /* Consume the token. */
16707 cp_lexer_consume_token (parser
->lexer
);
16709 if (cp_parser_allow_gnu_extensions_p (parser
)
16710 && parser
->in_function_body
16711 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_GOTO
))
16713 /* Remember that we saw the `goto' keyword. */
16715 /* Consume the token. */
16716 cp_lexer_consume_token (parser
->lexer
);
16718 /* Look for the opening `('. */
16719 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
16721 /* Look for the string. */
16722 string
= cp_parser_string_literal (parser
, false, false);
16723 if (string
== error_mark_node
)
16725 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16726 /*consume_paren=*/true);
16730 /* If we're allowing GNU extensions, check for the extended assembly
16731 syntax. Unfortunately, the `:' tokens need not be separated by
16732 a space in C, and so, for compatibility, we tolerate that here
16733 too. Doing that means that we have to treat the `::' operator as
16735 if (cp_parser_allow_gnu_extensions_p (parser
)
16736 && parser
->in_function_body
16737 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
16738 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
16740 bool inputs_p
= false;
16741 bool clobbers_p
= false;
16742 bool labels_p
= false;
16744 /* The extended syntax was used. */
16747 /* Look for outputs. */
16748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16750 /* Consume the `:'. */
16751 cp_lexer_consume_token (parser
->lexer
);
16752 /* Parse the output-operands. */
16753 if (cp_lexer_next_token_is_not (parser
->lexer
,
16755 && cp_lexer_next_token_is_not (parser
->lexer
,
16757 && cp_lexer_next_token_is_not (parser
->lexer
,
16760 outputs
= cp_parser_asm_operand_list (parser
);
16762 if (outputs
== error_mark_node
)
16763 invalid_outputs_p
= true;
16765 /* If the next token is `::', there are no outputs, and the
16766 next token is the beginning of the inputs. */
16767 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16768 /* The inputs are coming next. */
16771 /* Look for inputs. */
16773 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16775 /* Consume the `:' or `::'. */
16776 cp_lexer_consume_token (parser
->lexer
);
16777 /* Parse the output-operands. */
16778 if (cp_lexer_next_token_is_not (parser
->lexer
,
16780 && cp_lexer_next_token_is_not (parser
->lexer
,
16782 && cp_lexer_next_token_is_not (parser
->lexer
,
16784 inputs
= cp_parser_asm_operand_list (parser
);
16786 if (inputs
== error_mark_node
)
16787 invalid_inputs_p
= true;
16789 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16790 /* The clobbers are coming next. */
16793 /* Look for clobbers. */
16795 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
16798 /* Consume the `:' or `::'. */
16799 cp_lexer_consume_token (parser
->lexer
);
16800 /* Parse the clobbers. */
16801 if (cp_lexer_next_token_is_not (parser
->lexer
,
16803 && cp_lexer_next_token_is_not (parser
->lexer
,
16805 clobbers
= cp_parser_asm_clobber_list (parser
);
16808 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
16809 /* The labels are coming next. */
16812 /* Look for labels. */
16814 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
16817 /* Consume the `:' or `::'. */
16818 cp_lexer_consume_token (parser
->lexer
);
16819 /* Parse the labels. */
16820 labels
= cp_parser_asm_label_list (parser
);
16823 if (goto_p
&& !labels_p
)
16824 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
16827 missing
= RT_COLON_SCOPE
;
16829 /* Look for the closing `)'. */
16830 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
16831 missing
? missing
: RT_CLOSE_PAREN
))
16832 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
16833 /*consume_paren=*/true);
16834 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
16836 if (!invalid_inputs_p
&& !invalid_outputs_p
)
16838 /* Create the ASM_EXPR. */
16839 if (parser
->in_function_body
)
16841 asm_stmt
= finish_asm_stmt (volatile_p
, string
, outputs
,
16842 inputs
, clobbers
, labels
);
16843 /* If the extended syntax was not used, mark the ASM_EXPR. */
16846 tree temp
= asm_stmt
;
16847 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
16848 temp
= TREE_OPERAND (temp
, 0);
16850 ASM_INPUT_P (temp
) = 1;
16854 symtab
->finalize_toplevel_asm (string
);
16858 /* Declarators [gram.dcl.decl] */
16860 /* Parse an init-declarator.
16863 declarator initializer [opt]
16868 declarator asm-specification [opt] attributes [opt] initializer [opt]
16870 function-definition:
16871 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16873 decl-specifier-seq [opt] declarator function-try-block
16877 function-definition:
16878 __extension__ function-definition
16882 function-definition:
16883 decl-specifier-seq [opt] declarator function-transaction-block
16885 The DECL_SPECIFIERS apply to this declarator. Returns a
16886 representation of the entity declared. If MEMBER_P is TRUE, then
16887 this declarator appears in a class scope. The new DECL created by
16888 this declarator is returned.
16890 The CHECKS are access checks that should be performed once we know
16891 what entity is being declared (and, therefore, what classes have
16894 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16895 for a function-definition here as well. If the declarator is a
16896 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16897 be TRUE upon return. By that point, the function-definition will
16898 have been completely parsed.
16900 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16903 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16904 parsed declaration if it is an uninitialized single declarator not followed
16905 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16906 if present, will not be consumed. If returned, this declarator will be
16907 created with SD_INITIALIZED but will not call cp_finish_decl.
16909 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16910 and there is an initializer, the pointed location_t is set to the
16911 location of the '=' or `(', or '{' in C++11 token introducing the
16915 cp_parser_init_declarator (cp_parser
* parser
,
16916 cp_decl_specifier_seq
*decl_specifiers
,
16917 vec
<deferred_access_check
, va_gc
> *checks
,
16918 bool function_definition_allowed_p
,
16920 int declares_class_or_enum
,
16921 bool* function_definition_p
,
16922 tree
* maybe_range_for_decl
,
16923 location_t
* init_loc
)
16925 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
16926 *attributes_start_token
= NULL
;
16927 cp_declarator
*declarator
;
16928 tree prefix_attributes
;
16929 tree attributes
= NULL
;
16930 tree asm_specification
;
16932 tree decl
= NULL_TREE
;
16934 int is_initialized
;
16935 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16936 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16938 enum cpp_ttype initialization_kind
;
16939 bool is_direct_init
= false;
16940 bool is_non_constant_init
;
16941 int ctor_dtor_or_conv_p
;
16942 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
16943 tree pushed_scope
= NULL_TREE
;
16944 bool range_for_decl_p
= false;
16945 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
16946 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
16948 /* Gather the attributes that were provided with the
16949 decl-specifiers. */
16950 prefix_attributes
= decl_specifiers
->attributes
;
16952 /* Assume that this is not the declarator for a function
16954 if (function_definition_p
)
16955 *function_definition_p
= false;
16957 /* Default arguments are only permitted for function parameters. */
16958 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
16959 parser
->default_arg_ok_p
= false;
16961 /* Defer access checks while parsing the declarator; we cannot know
16962 what names are accessible until we know what is being
16964 resume_deferring_access_checks ();
16966 /* Parse the declarator. */
16967 token
= cp_lexer_peek_token (parser
->lexer
);
16969 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
16970 &ctor_dtor_or_conv_p
,
16971 /*parenthesized_p=*/NULL
,
16972 member_p
, friend_p
);
16973 /* Gather up the deferred checks. */
16974 stop_deferring_access_checks ();
16976 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
16978 /* If the DECLARATOR was erroneous, there's no need to go
16980 if (declarator
== cp_error_declarator
)
16981 return error_mark_node
;
16983 /* Check that the number of template-parameter-lists is OK. */
16984 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
16986 return error_mark_node
;
16988 if (declares_class_or_enum
& 2)
16989 cp_parser_check_for_definition_in_return_type (declarator
,
16990 decl_specifiers
->type
,
16991 decl_specifiers
->locations
[ds_type_spec
]);
16993 /* Figure out what scope the entity declared by the DECLARATOR is
16994 located in. `grokdeclarator' sometimes changes the scope, so
16995 we compute it now. */
16996 scope
= get_scope_of_declarator (declarator
);
16998 /* Perform any lookups in the declared type which were thought to be
16999 dependent, but are not in the scope of the declarator. */
17000 decl_specifiers
->type
17001 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
17003 /* If we're allowing GNU extensions, look for an
17004 asm-specification. */
17005 if (cp_parser_allow_gnu_extensions_p (parser
))
17007 /* Look for an asm-specification. */
17008 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
17009 asm_specification
= cp_parser_asm_specification_opt (parser
);
17012 asm_specification
= NULL_TREE
;
17014 /* Look for attributes. */
17015 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
17016 attributes
= cp_parser_attributes_opt (parser
);
17018 /* Peek at the next token. */
17019 token
= cp_lexer_peek_token (parser
->lexer
);
17021 bool bogus_implicit_tmpl
= false;
17023 if (function_declarator_p (declarator
))
17025 /* Check to see if the token indicates the start of a
17026 function-definition. */
17027 if (cp_parser_token_starts_function_definition_p (token
))
17029 if (!function_definition_allowed_p
)
17031 /* If a function-definition should not appear here, issue an
17033 cp_parser_error (parser
,
17034 "a function-definition is not allowed here");
17035 return error_mark_node
;
17038 location_t func_brace_location
17039 = cp_lexer_peek_token (parser
->lexer
)->location
;
17041 /* Neither attributes nor an asm-specification are allowed
17042 on a function-definition. */
17043 if (asm_specification
)
17044 error_at (asm_spec_start_token
->location
,
17045 "an asm-specification is not allowed "
17046 "on a function-definition");
17048 error_at (attributes_start_token
->location
,
17049 "attributes are not allowed "
17050 "on a function-definition");
17051 /* This is a function-definition. */
17052 *function_definition_p
= true;
17054 /* Parse the function definition. */
17056 decl
= cp_parser_save_member_function_body (parser
,
17059 prefix_attributes
);
17062 (cp_parser_function_definition_from_specifiers_and_declarator
17063 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
17065 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
17067 /* This is where the prologue starts... */
17068 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
17069 = func_brace_location
;
17075 else if (parser
->fully_implicit_function_template_p
)
17077 /* A non-template declaration involving a function parameter list
17078 containing an implicit template parameter will be made into a
17079 template. If the resulting declaration is not going to be an
17080 actual function then finish the template scope here to prevent it.
17081 An error message will be issued once we have a decl to talk about.
17083 FIXME probably we should do type deduction rather than create an
17084 implicit template, but the standard currently doesn't allow it. */
17085 bogus_implicit_tmpl
= true;
17086 finish_fully_implicit_template (parser
, NULL_TREE
);
17091 Only in function declarations for constructors, destructors, and
17092 type conversions can the decl-specifier-seq be omitted.
17094 We explicitly postpone this check past the point where we handle
17095 function-definitions because we tolerate function-definitions
17096 that are missing their return types in some modes. */
17097 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
17099 cp_parser_error (parser
,
17100 "expected constructor, destructor, or type conversion");
17101 return error_mark_node
;
17104 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17105 if (token
->type
== CPP_EQ
17106 || token
->type
== CPP_OPEN_PAREN
17107 || token
->type
== CPP_OPEN_BRACE
)
17109 is_initialized
= SD_INITIALIZED
;
17110 initialization_kind
= token
->type
;
17111 if (maybe_range_for_decl
)
17112 *maybe_range_for_decl
= error_mark_node
;
17113 tmp_init_loc
= token
->location
;
17114 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
17115 *init_loc
= tmp_init_loc
;
17117 if (token
->type
== CPP_EQ
17118 && function_declarator_p (declarator
))
17120 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
17121 if (t2
->keyword
== RID_DEFAULT
)
17122 is_initialized
= SD_DEFAULTED
;
17123 else if (t2
->keyword
== RID_DELETE
)
17124 is_initialized
= SD_DELETED
;
17129 /* If the init-declarator isn't initialized and isn't followed by a
17130 `,' or `;', it's not a valid init-declarator. */
17131 if (token
->type
!= CPP_COMMA
17132 && token
->type
!= CPP_SEMICOLON
)
17134 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
17135 range_for_decl_p
= true;
17138 if (!maybe_range_for_decl
)
17139 cp_parser_error (parser
, "expected initializer");
17140 return error_mark_node
;
17143 is_initialized
= SD_UNINITIALIZED
;
17144 initialization_kind
= CPP_EOF
;
17147 /* Because start_decl has side-effects, we should only call it if we
17148 know we're going ahead. By this point, we know that we cannot
17149 possibly be looking at any other construct. */
17150 cp_parser_commit_to_tentative_parse (parser
);
17152 /* Enter the newly declared entry in the symbol table. If we're
17153 processing a declaration in a class-specifier, we wait until
17154 after processing the initializer. */
17157 if (parser
->in_unbraced_linkage_specification_p
)
17158 decl_specifiers
->storage_class
= sc_extern
;
17159 decl
= start_decl (declarator
, decl_specifiers
,
17160 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
17161 attributes
, prefix_attributes
, &pushed_scope
);
17162 cp_finalize_omp_declare_simd (parser
, decl
);
17163 /* Adjust location of decl if declarator->id_loc is more appropriate:
17164 set, and decl wasn't merged with another decl, in which case its
17165 location would be different from input_location, and more accurate. */
17167 && declarator
->id_loc
!= UNKNOWN_LOCATION
17168 && DECL_SOURCE_LOCATION (decl
) == input_location
)
17169 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
17172 /* Enter the SCOPE. That way unqualified names appearing in the
17173 initializer will be looked up in SCOPE. */
17174 pushed_scope
= push_scope (scope
);
17176 /* Perform deferred access control checks, now that we know in which
17177 SCOPE the declared entity resides. */
17178 if (!member_p
&& decl
)
17180 tree saved_current_function_decl
= NULL_TREE
;
17182 /* If the entity being declared is a function, pretend that we
17183 are in its scope. If it is a `friend', it may have access to
17184 things that would not otherwise be accessible. */
17185 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17187 saved_current_function_decl
= current_function_decl
;
17188 current_function_decl
= decl
;
17191 /* Perform access checks for template parameters. */
17192 cp_parser_perform_template_parameter_access_checks (checks
);
17194 /* Perform the access control checks for the declarator and the
17195 decl-specifiers. */
17196 perform_deferred_access_checks (tf_warning_or_error
);
17198 /* Restore the saved value. */
17199 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17200 current_function_decl
= saved_current_function_decl
;
17203 /* Parse the initializer. */
17204 initializer
= NULL_TREE
;
17205 is_direct_init
= false;
17206 is_non_constant_init
= true;
17207 if (is_initialized
)
17209 if (function_declarator_p (declarator
))
17211 if (initialization_kind
== CPP_EQ
)
17212 initializer
= cp_parser_pure_specifier (parser
);
17215 /* If the declaration was erroneous, we don't really
17216 know what the user intended, so just silently
17217 consume the initializer. */
17218 if (decl
!= error_mark_node
)
17219 error_at (tmp_init_loc
, "initializer provided for function");
17220 cp_parser_skip_to_closing_parenthesis (parser
,
17221 /*recovering=*/true,
17222 /*or_comma=*/false,
17223 /*consume_paren=*/true);
17228 /* We want to record the extra mangling scope for in-class
17229 initializers of class members and initializers of static data
17230 member templates. The former involves deferring
17231 parsing of the initializer until end of class as with default
17232 arguments. So right here we only handle the latter. */
17233 if (!member_p
&& processing_template_decl
)
17234 start_lambda_scope (decl
);
17235 initializer
= cp_parser_initializer (parser
,
17237 &is_non_constant_init
);
17238 if (!member_p
&& processing_template_decl
)
17239 finish_lambda_scope ();
17240 if (initializer
== error_mark_node
)
17241 cp_parser_skip_to_end_of_statement (parser
);
17245 /* The old parser allows attributes to appear after a parenthesized
17246 initializer. Mark Mitchell proposed removing this functionality
17247 on the GCC mailing lists on 2002-08-13. This parser accepts the
17248 attributes -- but ignores them. */
17249 if (cp_parser_allow_gnu_extensions_p (parser
)
17250 && initialization_kind
== CPP_OPEN_PAREN
)
17251 if (cp_parser_attributes_opt (parser
))
17252 warning (OPT_Wattributes
,
17253 "attributes after parenthesized initializer ignored");
17255 /* And now complain about a non-function implicit template. */
17256 if (bogus_implicit_tmpl
)
17257 error_at (DECL_SOURCE_LOCATION (decl
),
17258 "non-function %qD declared as implicit template", decl
);
17260 /* For an in-class declaration, use `grokfield' to create the
17266 pop_scope (pushed_scope
);
17267 pushed_scope
= NULL_TREE
;
17269 decl
= grokfield (declarator
, decl_specifiers
,
17270 initializer
, !is_non_constant_init
,
17271 /*asmspec=*/NULL_TREE
,
17272 chainon (attributes
, prefix_attributes
));
17273 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17274 cp_parser_save_default_args (parser
, decl
);
17275 cp_finalize_omp_declare_simd (parser
, decl
);
17278 /* Finish processing the declaration. But, skip member
17280 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
17282 cp_finish_decl (decl
,
17283 initializer
, !is_non_constant_init
,
17285 /* If the initializer is in parentheses, then this is
17286 a direct-initialization, which means that an
17287 `explicit' constructor is OK. Otherwise, an
17288 `explicit' constructor cannot be used. */
17289 ((is_direct_init
|| !is_initialized
)
17290 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
17292 else if ((cxx_dialect
!= cxx98
) && friend_p
17293 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
17294 /* Core issue #226 (C++0x only): A default template-argument
17295 shall not be specified in a friend class template
17297 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
17298 /*is_partial=*/false, /*is_friend_decl=*/1);
17300 if (!friend_p
&& pushed_scope
)
17301 pop_scope (pushed_scope
);
17303 if (function_declarator_p (declarator
)
17304 && parser
->fully_implicit_function_template_p
)
17307 decl
= finish_fully_implicit_template (parser
, decl
);
17309 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
17315 /* Parse a declarator.
17319 ptr-operator declarator
17321 abstract-declarator:
17322 ptr-operator abstract-declarator [opt]
17323 direct-abstract-declarator
17328 attributes [opt] direct-declarator
17329 attributes [opt] ptr-operator declarator
17331 abstract-declarator:
17332 attributes [opt] ptr-operator abstract-declarator [opt]
17333 attributes [opt] direct-abstract-declarator
17335 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17336 detect constructor, destructor or conversion operators. It is set
17337 to -1 if the declarator is a name, and +1 if it is a
17338 function. Otherwise it is set to zero. Usually you just want to
17339 test for >0, but internally the negative value is used.
17341 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17342 a decl-specifier-seq unless it declares a constructor, destructor,
17343 or conversion. It might seem that we could check this condition in
17344 semantic analysis, rather than parsing, but that makes it difficult
17345 to handle something like `f()'. We want to notice that there are
17346 no decl-specifiers, and therefore realize that this is an
17347 expression, not a declaration.)
17349 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17350 the declarator is a direct-declarator of the form "(...)".
17352 MEMBER_P is true iff this declarator is a member-declarator.
17354 FRIEND_P is true iff this declarator is a friend. */
17356 static cp_declarator
*
17357 cp_parser_declarator (cp_parser
* parser
,
17358 cp_parser_declarator_kind dcl_kind
,
17359 int* ctor_dtor_or_conv_p
,
17360 bool* parenthesized_p
,
17361 bool member_p
, bool friend_p
)
17363 cp_declarator
*declarator
;
17364 enum tree_code code
;
17365 cp_cv_quals cv_quals
;
17367 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
17369 /* Assume this is not a constructor, destructor, or type-conversion
17371 if (ctor_dtor_or_conv_p
)
17372 *ctor_dtor_or_conv_p
= 0;
17374 if (cp_parser_allow_gnu_extensions_p (parser
))
17375 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
17377 /* Check for the ptr-operator production. */
17378 cp_parser_parse_tentatively (parser
);
17379 /* Parse the ptr-operator. */
17380 code
= cp_parser_ptr_operator (parser
,
17385 /* If that worked, then we have a ptr-operator. */
17386 if (cp_parser_parse_definitely (parser
))
17388 /* If a ptr-operator was found, then this declarator was not
17390 if (parenthesized_p
)
17391 *parenthesized_p
= true;
17392 /* The dependent declarator is optional if we are parsing an
17393 abstract-declarator. */
17394 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17395 cp_parser_parse_tentatively (parser
);
17397 /* Parse the dependent declarator. */
17398 declarator
= cp_parser_declarator (parser
, dcl_kind
,
17399 /*ctor_dtor_or_conv_p=*/NULL
,
17400 /*parenthesized_p=*/NULL
,
17401 /*member_p=*/false,
17404 /* If we are parsing an abstract-declarator, we must handle the
17405 case where the dependent declarator is absent. */
17406 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
17407 && !cp_parser_parse_definitely (parser
))
17410 declarator
= cp_parser_make_indirect_declarator
17411 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
17413 /* Everything else is a direct-declarator. */
17416 if (parenthesized_p
)
17417 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
17419 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
17420 ctor_dtor_or_conv_p
,
17421 member_p
, friend_p
);
17424 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
17425 declarator
->attributes
= gnu_attributes
;
17429 /* Parse a direct-declarator or direct-abstract-declarator.
17433 direct-declarator ( parameter-declaration-clause )
17434 cv-qualifier-seq [opt]
17435 ref-qualifier [opt]
17436 exception-specification [opt]
17437 direct-declarator [ constant-expression [opt] ]
17440 direct-abstract-declarator:
17441 direct-abstract-declarator [opt]
17442 ( parameter-declaration-clause )
17443 cv-qualifier-seq [opt]
17444 ref-qualifier [opt]
17445 exception-specification [opt]
17446 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17447 ( abstract-declarator )
17449 Returns a representation of the declarator. DCL_KIND is
17450 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17451 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17452 we are parsing a direct-declarator. It is
17453 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17454 of ambiguity we prefer an abstract declarator, as per
17455 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17456 as for cp_parser_declarator. */
17458 static cp_declarator
*
17459 cp_parser_direct_declarator (cp_parser
* parser
,
17460 cp_parser_declarator_kind dcl_kind
,
17461 int* ctor_dtor_or_conv_p
,
17462 bool member_p
, bool friend_p
)
17465 cp_declarator
*declarator
= NULL
;
17466 tree scope
= NULL_TREE
;
17467 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
17468 bool saved_in_declarator_p
= parser
->in_declarator_p
;
17470 tree pushed_scope
= NULL_TREE
;
17474 /* Peek at the next token. */
17475 token
= cp_lexer_peek_token (parser
->lexer
);
17476 if (token
->type
== CPP_OPEN_PAREN
)
17478 /* This is either a parameter-declaration-clause, or a
17479 parenthesized declarator. When we know we are parsing a
17480 named declarator, it must be a parenthesized declarator
17481 if FIRST is true. For instance, `(int)' is a
17482 parameter-declaration-clause, with an omitted
17483 direct-abstract-declarator. But `((*))', is a
17484 parenthesized abstract declarator. Finally, when T is a
17485 template parameter `(T)' is a
17486 parameter-declaration-clause, and not a parenthesized
17489 We first try and parse a parameter-declaration-clause,
17490 and then try a nested declarator (if FIRST is true).
17492 It is not an error for it not to be a
17493 parameter-declaration-clause, even when FIRST is
17499 The first is the declaration of a function while the
17500 second is the definition of a variable, including its
17503 Having seen only the parenthesis, we cannot know which of
17504 these two alternatives should be selected. Even more
17505 complex are examples like:
17510 The former is a function-declaration; the latter is a
17511 variable initialization.
17513 Thus again, we try a parameter-declaration-clause, and if
17514 that fails, we back out and return. */
17516 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17519 bool is_declarator
= false;
17521 /* In a member-declarator, the only valid interpretation
17522 of a parenthesis is the start of a
17523 parameter-declaration-clause. (It is invalid to
17524 initialize a static data member with a parenthesized
17525 initializer; only the "=" form of initialization is
17528 cp_parser_parse_tentatively (parser
);
17530 /* Consume the `('. */
17531 cp_lexer_consume_token (parser
->lexer
);
17534 /* If this is going to be an abstract declarator, we're
17535 in a declarator and we can't have default args. */
17536 parser
->default_arg_ok_p
= false;
17537 parser
->in_declarator_p
= true;
17540 begin_scope (sk_function_parms
, NULL_TREE
);
17542 /* Parse the parameter-declaration-clause. */
17543 params
= cp_parser_parameter_declaration_clause (parser
);
17545 /* Consume the `)'. */
17546 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
17548 /* If all went well, parse the cv-qualifier-seq,
17549 ref-qualifier and the exception-specification. */
17550 if (member_p
|| cp_parser_parse_definitely (parser
))
17552 cp_cv_quals cv_quals
;
17553 cp_virt_specifiers virt_specifiers
;
17554 cp_ref_qualifier ref_qual
;
17555 tree exception_specification
;
17558 bool memfn
= (member_p
|| (pushed_scope
17559 && CLASS_TYPE_P (pushed_scope
)));
17561 is_declarator
= true;
17563 if (ctor_dtor_or_conv_p
)
17564 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
17567 /* Parse the cv-qualifier-seq. */
17568 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
17569 /* Parse the ref-qualifier. */
17570 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
17571 /* And the exception-specification. */
17572 exception_specification
17573 = cp_parser_exception_specification_opt (parser
);
17575 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17577 /* In here, we handle cases where attribute is used after
17578 the function declaration. For example:
17579 void func (int x) __attribute__((vector(..))); */
17581 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
17583 cp_parser_parse_tentatively (parser
);
17584 tree attr
= cp_parser_gnu_attributes_opt (parser
);
17585 if (cp_lexer_next_token_is_not (parser
->lexer
,
17587 && cp_lexer_next_token_is_not (parser
->lexer
,
17589 cp_parser_abort_tentative_parse (parser
);
17590 else if (!cp_parser_parse_definitely (parser
))
17593 attrs
= chainon (attr
, attrs
);
17595 late_return
= (cp_parser_late_return_type_opt
17596 (parser
, declarator
,
17597 memfn
? cv_quals
: -1));
17600 /* Parse the virt-specifier-seq. */
17601 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
17603 /* Create the function-declarator. */
17604 declarator
= make_call_declarator (declarator
,
17609 exception_specification
,
17611 declarator
->std_attributes
= attrs
;
17612 /* Any subsequent parameter lists are to do with
17613 return type, so are not those of the declared
17615 parser
->default_arg_ok_p
= false;
17618 /* Remove the function parms from scope. */
17619 pop_bindings_and_leave_scope ();
17622 /* Repeat the main loop. */
17626 /* If this is the first, we can try a parenthesized
17630 bool saved_in_type_id_in_expr_p
;
17632 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17633 parser
->in_declarator_p
= saved_in_declarator_p
;
17635 /* Consume the `('. */
17636 cp_lexer_consume_token (parser
->lexer
);
17637 /* Parse the nested declarator. */
17638 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
17639 parser
->in_type_id_in_expr_p
= true;
17641 = cp_parser_declarator (parser
, dcl_kind
, ctor_dtor_or_conv_p
,
17642 /*parenthesized_p=*/NULL
,
17643 member_p
, friend_p
);
17644 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
17646 /* Expect a `)'. */
17647 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
17648 declarator
= cp_error_declarator
;
17649 if (declarator
== cp_error_declarator
)
17652 goto handle_declarator
;
17654 /* Otherwise, we must be done. */
17658 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
17659 && token
->type
== CPP_OPEN_SQUARE
17660 && !cp_next_tokens_can_be_attribute_p (parser
))
17662 /* Parse an array-declarator. */
17663 tree bounds
, attrs
;
17665 if (ctor_dtor_or_conv_p
)
17666 *ctor_dtor_or_conv_p
= 0;
17669 parser
->default_arg_ok_p
= false;
17670 parser
->in_declarator_p
= true;
17671 /* Consume the `['. */
17672 cp_lexer_consume_token (parser
->lexer
);
17673 /* Peek at the next token. */
17674 token
= cp_lexer_peek_token (parser
->lexer
);
17675 /* If the next token is `]', then there is no
17676 constant-expression. */
17677 if (token
->type
!= CPP_CLOSE_SQUARE
)
17679 bool non_constant_p
;
17681 = cp_parser_constant_expression (parser
,
17682 /*allow_non_constant=*/true,
17684 if (!non_constant_p
)
17686 else if (error_operand_p (bounds
))
17687 /* Already gave an error. */;
17688 else if (!parser
->in_function_body
17689 || current_binding_level
->kind
== sk_function_parms
)
17691 /* Normally, the array bound must be an integral constant
17692 expression. However, as an extension, we allow VLAs
17693 in function scopes as long as they aren't part of a
17694 parameter declaration. */
17695 cp_parser_error (parser
,
17696 "array bound is not an integer constant");
17697 bounds
= error_mark_node
;
17699 else if (processing_template_decl
17700 && !type_dependent_expression_p (bounds
))
17702 /* Remember this wasn't a constant-expression. */
17703 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
17704 TREE_SIDE_EFFECTS (bounds
) = 1;
17708 bounds
= NULL_TREE
;
17709 /* Look for the closing `]'. */
17710 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
17712 declarator
= cp_error_declarator
;
17716 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17717 declarator
= make_array_declarator (declarator
, bounds
);
17718 declarator
->std_attributes
= attrs
;
17720 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
17723 tree qualifying_scope
;
17724 tree unqualified_name
;
17726 special_function_kind sfk
;
17728 bool pack_expansion_p
= false;
17729 cp_token
*declarator_id_start_token
;
17731 /* Parse a declarator-id */
17732 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
17735 cp_parser_parse_tentatively (parser
);
17737 /* If we see an ellipsis, we should be looking at a
17739 if (token
->type
== CPP_ELLIPSIS
)
17741 /* Consume the `...' */
17742 cp_lexer_consume_token (parser
->lexer
);
17744 pack_expansion_p
= true;
17748 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
17750 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
17751 qualifying_scope
= parser
->scope
;
17756 if (!unqualified_name
&& pack_expansion_p
)
17758 /* Check whether an error occurred. */
17759 okay
= !cp_parser_error_occurred (parser
);
17761 /* We already consumed the ellipsis to mark a
17762 parameter pack, but we have no way to report it,
17763 so abort the tentative parse. We will be exiting
17764 immediately anyway. */
17765 cp_parser_abort_tentative_parse (parser
);
17768 okay
= cp_parser_parse_definitely (parser
);
17771 unqualified_name
= error_mark_node
;
17772 else if (unqualified_name
17773 && (qualifying_scope
17774 || (!identifier_p (unqualified_name
))))
17776 cp_parser_error (parser
, "expected unqualified-id");
17777 unqualified_name
= error_mark_node
;
17781 if (!unqualified_name
)
17783 if (unqualified_name
== error_mark_node
)
17785 declarator
= cp_error_declarator
;
17786 pack_expansion_p
= false;
17787 declarator
->parameter_pack_p
= false;
17791 attrs
= cp_parser_std_attribute_spec_seq (parser
);
17793 if (qualifying_scope
&& at_namespace_scope_p ()
17794 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
17796 /* In the declaration of a member of a template class
17797 outside of the class itself, the SCOPE will sometimes
17798 be a TYPENAME_TYPE. For example, given:
17800 template <typename T>
17801 int S<T>::R::i = 3;
17803 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17804 this context, we must resolve S<T>::R to an ordinary
17805 type, rather than a typename type.
17807 The reason we normally avoid resolving TYPENAME_TYPEs
17808 is that a specialization of `S' might render
17809 `S<T>::R' not a type. However, if `S' is
17810 specialized, then this `i' will not be used, so there
17811 is no harm in resolving the types here. */
17814 /* Resolve the TYPENAME_TYPE. */
17815 type
= resolve_typename_type (qualifying_scope
,
17816 /*only_current_p=*/false);
17817 /* If that failed, the declarator is invalid. */
17818 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17820 if (typedef_variant_p (type
))
17821 error_at (declarator_id_start_token
->location
,
17822 "cannot define member of dependent typedef "
17825 error_at (declarator_id_start_token
->location
,
17826 "%<%T::%E%> is not a type",
17827 TYPE_CONTEXT (qualifying_scope
),
17828 TYPE_IDENTIFIER (qualifying_scope
));
17830 qualifying_scope
= type
;
17835 if (unqualified_name
)
17839 if (qualifying_scope
17840 && CLASS_TYPE_P (qualifying_scope
))
17841 class_type
= qualifying_scope
;
17843 class_type
= current_class_type
;
17845 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
17847 tree name_type
= TREE_TYPE (unqualified_name
);
17848 if (class_type
&& same_type_p (name_type
, class_type
))
17850 if (qualifying_scope
17851 && CLASSTYPE_USE_TEMPLATE (name_type
))
17853 error_at (declarator_id_start_token
->location
,
17854 "invalid use of constructor as a template");
17855 inform (declarator_id_start_token
->location
,
17856 "use %<%T::%D%> instead of %<%T::%D%> to "
17857 "name the constructor in a qualified name",
17859 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
17860 class_type
, name_type
);
17861 declarator
= cp_error_declarator
;
17865 unqualified_name
= constructor_name (class_type
);
17869 /* We do not attempt to print the declarator
17870 here because we do not have enough
17871 information about its original syntactic
17873 cp_parser_error (parser
, "invalid declarator");
17874 declarator
= cp_error_declarator
;
17881 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
17882 sfk
= sfk_destructor
;
17883 else if (IDENTIFIER_TYPENAME_P (unqualified_name
))
17884 sfk
= sfk_conversion
;
17885 else if (/* There's no way to declare a constructor
17886 for an anonymous type, even if the type
17887 got a name for linkage purposes. */
17888 !TYPE_WAS_ANONYMOUS (class_type
)
17889 /* Handle correctly (c++/19200):
17903 friend void N::S();
17906 && class_type
!= qualifying_scope
)
17907 && constructor_name_p (unqualified_name
,
17910 unqualified_name
= constructor_name (class_type
);
17911 sfk
= sfk_constructor
;
17913 else if (is_overloaded_fn (unqualified_name
)
17914 && DECL_CONSTRUCTOR_P (get_first_fn
17915 (unqualified_name
)))
17916 sfk
= sfk_constructor
;
17918 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
17919 *ctor_dtor_or_conv_p
= -1;
17922 declarator
= make_id_declarator (qualifying_scope
,
17925 declarator
->std_attributes
= attrs
;
17926 declarator
->id_loc
= token
->location
;
17927 declarator
->parameter_pack_p
= pack_expansion_p
;
17929 if (pack_expansion_p
)
17930 maybe_warn_variadic_templates ();
17933 handle_declarator
:;
17934 scope
= get_scope_of_declarator (declarator
);
17937 /* Any names that appear after the declarator-id for a
17938 member are looked up in the containing scope. */
17939 if (at_function_scope_p ())
17941 /* But declarations with qualified-ids can't appear in a
17943 cp_parser_error (parser
, "qualified-id in declaration");
17944 declarator
= cp_error_declarator
;
17947 pushed_scope
= push_scope (scope
);
17949 parser
->in_declarator_p
= true;
17950 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
17951 || (declarator
&& declarator
->kind
== cdk_id
))
17952 /* Default args are only allowed on function
17954 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17956 parser
->default_arg_ok_p
= false;
17965 /* For an abstract declarator, we might wind up with nothing at this
17966 point. That's an error; the declarator is not optional. */
17968 cp_parser_error (parser
, "expected declarator");
17970 /* If we entered a scope, we must exit it now. */
17972 pop_scope (pushed_scope
);
17974 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
17975 parser
->in_declarator_p
= saved_in_declarator_p
;
17980 /* Parse a ptr-operator.
17983 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17984 * cv-qualifier-seq [opt]
17986 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17987 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17992 & cv-qualifier-seq [opt]
17994 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17995 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17996 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17997 filled in with the TYPE containing the member. *CV_QUALS is
17998 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17999 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18000 Note that the tree codes returned by this function have nothing
18001 to do with the types of trees that will be eventually be created
18002 to represent the pointer or reference type being parsed. They are
18003 just constants with suggestive names. */
18004 static enum tree_code
18005 cp_parser_ptr_operator (cp_parser
* parser
,
18007 cp_cv_quals
*cv_quals
,
18010 enum tree_code code
= ERROR_MARK
;
18012 tree attrs
= NULL_TREE
;
18014 /* Assume that it's not a pointer-to-member. */
18016 /* And that there are no cv-qualifiers. */
18017 *cv_quals
= TYPE_UNQUALIFIED
;
18019 /* Peek at the next token. */
18020 token
= cp_lexer_peek_token (parser
->lexer
);
18022 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18023 if (token
->type
== CPP_MULT
)
18024 code
= INDIRECT_REF
;
18025 else if (token
->type
== CPP_AND
)
18027 else if ((cxx_dialect
!= cxx98
) &&
18028 token
->type
== CPP_AND_AND
) /* C++0x only */
18029 code
= NON_LVALUE_EXPR
;
18031 if (code
!= ERROR_MARK
)
18033 /* Consume the `*', `&' or `&&'. */
18034 cp_lexer_consume_token (parser
->lexer
);
18036 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18037 `&', if we are allowing GNU extensions. (The only qualifier
18038 that can legally appear after `&' is `restrict', but that is
18039 enforced during semantic analysis. */
18040 if (code
== INDIRECT_REF
18041 || cp_parser_allow_gnu_extensions_p (parser
))
18042 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18044 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18045 if (attributes
!= NULL
)
18046 *attributes
= attrs
;
18050 /* Try the pointer-to-member case. */
18051 cp_parser_parse_tentatively (parser
);
18052 /* Look for the optional `::' operator. */
18053 cp_parser_global_scope_opt (parser
,
18054 /*current_scope_valid_p=*/false);
18055 /* Look for the nested-name specifier. */
18056 token
= cp_lexer_peek_token (parser
->lexer
);
18057 cp_parser_nested_name_specifier (parser
,
18058 /*typename_keyword_p=*/false,
18059 /*check_dependency_p=*/true,
18061 /*is_declaration=*/false);
18062 /* If we found it, and the next token is a `*', then we are
18063 indeed looking at a pointer-to-member operator. */
18064 if (!cp_parser_error_occurred (parser
)
18065 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
18067 /* Indicate that the `*' operator was used. */
18068 code
= INDIRECT_REF
;
18070 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
18071 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
18072 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
18073 error_at (token
->location
, "cannot form pointer to member of "
18074 "non-class %q#T", parser
->scope
);
18077 /* The type of which the member is a member is given by the
18079 *type
= parser
->scope
;
18080 /* The next name will not be qualified. */
18081 parser
->scope
= NULL_TREE
;
18082 parser
->qualifying_scope
= NULL_TREE
;
18083 parser
->object_scope
= NULL_TREE
;
18084 /* Look for optional c++11 attributes. */
18085 attrs
= cp_parser_std_attribute_spec_seq (parser
);
18086 if (attributes
!= NULL
)
18087 *attributes
= attrs
;
18088 /* Look for the optional cv-qualifier-seq. */
18089 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
18092 /* If that didn't work we don't have a ptr-operator. */
18093 if (!cp_parser_parse_definitely (parser
))
18094 cp_parser_error (parser
, "expected ptr-operator");
18100 /* Parse an (optional) cv-qualifier-seq.
18103 cv-qualifier cv-qualifier-seq [opt]
18114 Returns a bitmask representing the cv-qualifiers. */
18117 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
18119 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
18124 cp_cv_quals cv_qualifier
;
18126 /* Peek at the next token. */
18127 token
= cp_lexer_peek_token (parser
->lexer
);
18128 /* See if it's a cv-qualifier. */
18129 switch (token
->keyword
)
18132 cv_qualifier
= TYPE_QUAL_CONST
;
18136 cv_qualifier
= TYPE_QUAL_VOLATILE
;
18140 cv_qualifier
= TYPE_QUAL_RESTRICT
;
18144 cv_qualifier
= TYPE_UNQUALIFIED
;
18151 if (cv_quals
& cv_qualifier
)
18153 error_at (token
->location
, "duplicate cv-qualifier");
18154 cp_lexer_purge_token (parser
->lexer
);
18158 cp_lexer_consume_token (parser
->lexer
);
18159 cv_quals
|= cv_qualifier
;
18166 /* Parse an (optional) ref-qualifier
18172 Returns cp_ref_qualifier representing ref-qualifier. */
18174 static cp_ref_qualifier
18175 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
18177 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
18179 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18180 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
18185 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
18186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18188 switch (token
->type
)
18191 curr_ref_qual
= REF_QUAL_LVALUE
;
18195 curr_ref_qual
= REF_QUAL_RVALUE
;
18199 curr_ref_qual
= REF_QUAL_NONE
;
18203 if (!curr_ref_qual
)
18207 error_at (token
->location
, "multiple ref-qualifiers");
18208 cp_lexer_purge_token (parser
->lexer
);
18212 ref_qual
= curr_ref_qual
;
18213 cp_lexer_consume_token (parser
->lexer
);
18220 /* Parse an (optional) virt-specifier-seq.
18222 virt-specifier-seq:
18223 virt-specifier virt-specifier-seq [opt]
18229 Returns a bitmask representing the virt-specifiers. */
18231 static cp_virt_specifiers
18232 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
18234 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
18239 cp_virt_specifiers virt_specifier
;
18241 /* Peek at the next token. */
18242 token
= cp_lexer_peek_token (parser
->lexer
);
18243 /* See if it's a virt-specifier-qualifier. */
18244 if (token
->type
!= CPP_NAME
)
18246 if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "override"))
18248 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18249 virt_specifier
= VIRT_SPEC_OVERRIDE
;
18251 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "final"))
18253 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
18254 virt_specifier
= VIRT_SPEC_FINAL
;
18256 else if (!strcmp (IDENTIFIER_POINTER(token
->u
.value
), "__final"))
18258 virt_specifier
= VIRT_SPEC_FINAL
;
18263 if (virt_specifiers
& virt_specifier
)
18265 error_at (token
->location
, "duplicate virt-specifier");
18266 cp_lexer_purge_token (parser
->lexer
);
18270 cp_lexer_consume_token (parser
->lexer
);
18271 virt_specifiers
|= virt_specifier
;
18274 return virt_specifiers
;
18277 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18278 is in scope even though it isn't real. */
18281 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
18285 if (current_class_ptr
)
18287 /* We don't clear this between NSDMIs. Is it already what we want? */
18288 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
18289 if (same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
18290 && cp_type_quals (type
) == quals
)
18294 this_parm
= build_this_parm (ctype
, quals
);
18295 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18296 current_class_ptr
= NULL_TREE
;
18298 = cp_build_indirect_ref (this_parm
, RO_NULL
, tf_warning_or_error
);
18299 current_class_ptr
= this_parm
;
18302 /* Return true iff our current scope is a non-static data member
18306 parsing_nsdmi (void)
18308 /* We recognize NSDMI context by the context-less 'this' pointer set up
18309 by the function above. */
18310 if (current_class_ptr
&& DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
18315 /* Parse a late-specified return type, if any. This is not a separate
18316 non-terminal, but part of a function declarator, which looks like
18318 -> trailing-type-specifier-seq abstract-declarator(opt)
18320 Returns the type indicated by the type-id.
18322 In addition to this this parses any queued up omp declare simd
18323 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18325 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18329 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
18333 tree type
= NULL_TREE
;
18334 bool declare_simd_p
= (parser
->omp_declare_simd
18336 && declarator
->kind
== cdk_id
);
18338 bool cilk_simd_fn_vector_p
= (parser
->cilk_simd_fn_info
18339 && declarator
&& declarator
->kind
== cdk_id
);
18341 /* Peek at the next token. */
18342 token
= cp_lexer_peek_token (parser
->lexer
);
18343 /* A late-specified return type is indicated by an initial '->'. */
18344 if (token
->type
!= CPP_DEREF
&& !(declare_simd_p
|| cilk_simd_fn_vector_p
))
18347 tree save_ccp
= current_class_ptr
;
18348 tree save_ccr
= current_class_ref
;
18351 /* DR 1207: 'this' is in scope in the trailing return type. */
18352 inject_this_parameter (current_class_type
, quals
);
18355 if (token
->type
== CPP_DEREF
)
18357 /* Consume the ->. */
18358 cp_lexer_consume_token (parser
->lexer
);
18360 type
= cp_parser_trailing_type_id (parser
);
18363 if (cilk_simd_fn_vector_p
)
18364 declarator
->std_attributes
18365 = cp_parser_late_parsing_cilk_simd_fn_info (parser
,
18366 declarator
->std_attributes
);
18367 if (declare_simd_p
)
18368 declarator
->std_attributes
18369 = cp_parser_late_parsing_omp_declare_simd (parser
,
18370 declarator
->std_attributes
);
18374 current_class_ptr
= save_ccp
;
18375 current_class_ref
= save_ccr
;
18381 /* Parse a declarator-id.
18385 :: [opt] nested-name-specifier [opt] type-name
18387 In the `id-expression' case, the value returned is as for
18388 cp_parser_id_expression if the id-expression was an unqualified-id.
18389 If the id-expression was a qualified-id, then a SCOPE_REF is
18390 returned. The first operand is the scope (either a NAMESPACE_DECL
18391 or TREE_TYPE), but the second is still just a representation of an
18395 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
18398 /* The expression must be an id-expression. Assume that qualified
18399 names are the names of types so that:
18402 int S<T>::R::i = 3;
18404 will work; we must treat `S<T>::R' as the name of a type.
18405 Similarly, assume that qualified names are templates, where
18409 int S<T>::R<T>::i = 3;
18412 id
= cp_parser_id_expression (parser
,
18413 /*template_keyword_p=*/false,
18414 /*check_dependency_p=*/false,
18415 /*template_p=*/NULL
,
18416 /*declarator_p=*/true,
18418 if (id
&& BASELINK_P (id
))
18419 id
= BASELINK_FUNCTIONS (id
);
18423 /* Parse a type-id.
18426 type-specifier-seq abstract-declarator [opt]
18428 Returns the TYPE specified. */
18431 cp_parser_type_id_1 (cp_parser
* parser
, bool is_template_arg
,
18432 bool is_trailing_return
)
18434 cp_decl_specifier_seq type_specifier_seq
;
18435 cp_declarator
*abstract_declarator
;
18437 /* Parse the type-specifier-seq. */
18438 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/false,
18439 is_trailing_return
,
18440 &type_specifier_seq
);
18441 if (type_specifier_seq
.type
== error_mark_node
)
18442 return error_mark_node
;
18444 /* There might or might not be an abstract declarator. */
18445 cp_parser_parse_tentatively (parser
);
18446 /* Look for the declarator. */
18447 abstract_declarator
18448 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
, NULL
,
18449 /*parenthesized_p=*/NULL
,
18450 /*member_p=*/false,
18451 /*friend_p=*/false);
18452 /* Check to see if there really was a declarator. */
18453 if (!cp_parser_parse_definitely (parser
))
18454 abstract_declarator
= NULL
;
18456 if (type_specifier_seq
.type
18457 /* None of the valid uses of 'auto' in C++14 involve the type-id
18458 nonterminal, but it is valid in a trailing-return-type. */
18459 && !(cxx_dialect
>= cxx14
&& is_trailing_return
)
18460 && type_uses_auto (type_specifier_seq
.type
))
18462 /* A type-id with type 'auto' is only ok if the abstract declarator
18463 is a function declarator with a late-specified return type. */
18464 if (abstract_declarator
18465 && abstract_declarator
->kind
== cdk_function
18466 && abstract_declarator
->u
.function
.late_return_type
)
18470 error ("invalid use of %<auto%>");
18471 return error_mark_node
;
18475 return groktypename (&type_specifier_seq
, abstract_declarator
,
18479 static tree
cp_parser_type_id (cp_parser
*parser
)
18481 return cp_parser_type_id_1 (parser
, false, false);
18484 static tree
cp_parser_template_type_arg (cp_parser
*parser
)
18487 const char *saved_message
= parser
->type_definition_forbidden_message
;
18488 parser
->type_definition_forbidden_message
18489 = G_("types may not be defined in template arguments");
18490 r
= cp_parser_type_id_1 (parser
, true, false);
18491 parser
->type_definition_forbidden_message
= saved_message
;
18492 if (cxx_dialect
>= cxx14
&& type_uses_auto (r
))
18494 error ("invalid use of %<auto%> in template argument");
18495 r
= error_mark_node
;
18500 static tree
cp_parser_trailing_type_id (cp_parser
*parser
)
18502 return cp_parser_type_id_1 (parser
, false, true);
18505 /* Parse a type-specifier-seq.
18507 type-specifier-seq:
18508 type-specifier type-specifier-seq [opt]
18512 type-specifier-seq:
18513 attributes type-specifier-seq [opt]
18515 If IS_DECLARATION is true, we are at the start of a "condition" or
18516 exception-declaration, so we might be followed by a declarator-id.
18518 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18519 i.e. we've just seen "->".
18521 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18524 cp_parser_type_specifier_seq (cp_parser
* parser
,
18525 bool is_declaration
,
18526 bool is_trailing_return
,
18527 cp_decl_specifier_seq
*type_specifier_seq
)
18529 bool seen_type_specifier
= false;
18530 cp_parser_flags flags
= CP_PARSER_FLAGS_OPTIONAL
;
18531 cp_token
*start_token
= NULL
;
18533 /* Clear the TYPE_SPECIFIER_SEQ. */
18534 clear_decl_specs (type_specifier_seq
);
18536 /* In the context of a trailing return type, enum E { } is an
18537 elaborated-type-specifier followed by a function-body, not an
18539 if (is_trailing_return
)
18540 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
18542 /* Parse the type-specifiers and attributes. */
18545 tree type_specifier
;
18546 bool is_cv_qualifier
;
18548 /* Check for attributes first. */
18549 if (cp_next_tokens_can_be_attribute_p (parser
))
18551 type_specifier_seq
->attributes
=
18552 chainon (type_specifier_seq
->attributes
,
18553 cp_parser_attributes_opt (parser
));
18557 /* record the token of the beginning of the type specifier seq,
18558 for error reporting purposes*/
18560 start_token
= cp_lexer_peek_token (parser
->lexer
);
18562 /* Look for the type-specifier. */
18563 type_specifier
= cp_parser_type_specifier (parser
,
18565 type_specifier_seq
,
18566 /*is_declaration=*/false,
18569 if (!type_specifier
)
18571 /* If the first type-specifier could not be found, this is not a
18572 type-specifier-seq at all. */
18573 if (!seen_type_specifier
)
18575 /* Set in_declarator_p to avoid skipping to the semicolon. */
18576 int in_decl
= parser
->in_declarator_p
;
18577 parser
->in_declarator_p
= true;
18579 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
18580 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18581 cp_parser_error (parser
, "expected type-specifier");
18583 parser
->in_declarator_p
= in_decl
;
18585 type_specifier_seq
->type
= error_mark_node
;
18588 /* If subsequent type-specifiers could not be found, the
18589 type-specifier-seq is complete. */
18593 seen_type_specifier
= true;
18594 /* The standard says that a condition can be:
18596 type-specifier-seq declarator = assignment-expression
18603 we should treat the "S" as a declarator, not as a
18604 type-specifier. The standard doesn't say that explicitly for
18605 type-specifier-seq, but it does say that for
18606 decl-specifier-seq in an ordinary declaration. Perhaps it
18607 would be clearer just to allow a decl-specifier-seq here, and
18608 then add a semantic restriction that if any decl-specifiers
18609 that are not type-specifiers appear, the program is invalid. */
18610 if (is_declaration
&& !is_cv_qualifier
)
18611 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
18615 /* Return whether the function currently being declared has an associated
18616 template parameter list. */
18619 function_being_declared_is_template_p (cp_parser
* parser
)
18621 if (!current_template_parms
|| processing_template_parmlist
)
18624 if (parser
->implicit_template_scope
)
18627 if (at_class_scope_p ()
18628 && TYPE_BEING_DEFINED (current_class_type
))
18629 return parser
->num_template_parameter_lists
!= 0;
18631 return ((int) parser
->num_template_parameter_lists
> template_class_depth
18632 (current_class_type
));
18635 /* Parse a parameter-declaration-clause.
18637 parameter-declaration-clause:
18638 parameter-declaration-list [opt] ... [opt]
18639 parameter-declaration-list , ...
18641 Returns a representation for the parameter declarations. A return
18642 value of NULL indicates a parameter-declaration-clause consisting
18643 only of an ellipsis. */
18646 cp_parser_parameter_declaration_clause (cp_parser
* parser
)
18655 int auto_is_implicit_function_template_parm_p
;
18657 parser
->auto_is_implicit_function_template_parm_p
18658 = auto_is_implicit_function_template_parm_p
;
18660 } cleanup
= { parser
, parser
->auto_is_implicit_function_template_parm_p
};
18664 if (!processing_specialization
18665 && !processing_template_parmlist
18666 && !processing_explicit_instantiation
)
18667 if (!current_function_decl
18668 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
18669 parser
->auto_is_implicit_function_template_parm_p
= true;
18671 /* Peek at the next token. */
18672 token
= cp_lexer_peek_token (parser
->lexer
);
18673 /* Check for trivial parameter-declaration-clauses. */
18674 if (token
->type
== CPP_ELLIPSIS
)
18676 /* Consume the `...' token. */
18677 cp_lexer_consume_token (parser
->lexer
);
18680 else if (token
->type
== CPP_CLOSE_PAREN
)
18681 /* There are no parameters. */
18683 #ifndef NO_IMPLICIT_EXTERN_C
18684 if (in_system_header_at (input_location
)
18685 && current_class_type
== NULL
18686 && current_lang_name
== lang_name_c
)
18690 return void_list_node
;
18692 /* Check for `(void)', too, which is a special case. */
18693 else if (token
->keyword
== RID_VOID
18694 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
18695 == CPP_CLOSE_PAREN
))
18697 /* Consume the `void' token. */
18698 cp_lexer_consume_token (parser
->lexer
);
18699 /* There are no parameters. */
18700 return void_list_node
;
18703 /* Parse the parameter-declaration-list. */
18704 parameters
= cp_parser_parameter_declaration_list (parser
, &is_error
);
18705 /* If a parse error occurred while parsing the
18706 parameter-declaration-list, then the entire
18707 parameter-declaration-clause is erroneous. */
18711 /* Peek at the next token. */
18712 token
= cp_lexer_peek_token (parser
->lexer
);
18713 /* If it's a `,', the clause should terminate with an ellipsis. */
18714 if (token
->type
== CPP_COMMA
)
18716 /* Consume the `,'. */
18717 cp_lexer_consume_token (parser
->lexer
);
18718 /* Expect an ellipsis. */
18720 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
18722 /* It might also be `...' if the optional trailing `,' was
18724 else if (token
->type
== CPP_ELLIPSIS
)
18726 /* Consume the `...' token. */
18727 cp_lexer_consume_token (parser
->lexer
);
18728 /* And remember that we saw it. */
18732 ellipsis_p
= false;
18734 /* Finish the parameter list. */
18736 parameters
= chainon (parameters
, void_list_node
);
18741 /* Parse a parameter-declaration-list.
18743 parameter-declaration-list:
18744 parameter-declaration
18745 parameter-declaration-list , parameter-declaration
18747 Returns a representation of the parameter-declaration-list, as for
18748 cp_parser_parameter_declaration_clause. However, the
18749 `void_list_node' is never appended to the list. Upon return,
18750 *IS_ERROR will be true iff an error occurred. */
18753 cp_parser_parameter_declaration_list (cp_parser
* parser
, bool *is_error
)
18755 tree parameters
= NULL_TREE
;
18756 tree
*tail
= ¶meters
;
18757 bool saved_in_unbraced_linkage_specification_p
;
18760 /* Assume all will go well. */
18762 /* The special considerations that apply to a function within an
18763 unbraced linkage specifications do not apply to the parameters
18764 to the function. */
18765 saved_in_unbraced_linkage_specification_p
18766 = parser
->in_unbraced_linkage_specification_p
;
18767 parser
->in_unbraced_linkage_specification_p
= false;
18769 /* Look for more parameters. */
18772 cp_parameter_declarator
*parameter
;
18773 tree decl
= error_mark_node
;
18774 bool parenthesized_p
= false;
18775 int template_parm_idx
= (function_being_declared_is_template_p (parser
)?
18776 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18777 (current_template_parms
)) : 0);
18779 /* Parse the parameter. */
18781 = cp_parser_parameter_declaration (parser
,
18782 /*template_parm_p=*/false,
18785 /* We don't know yet if the enclosing context is deprecated, so wait
18786 and warn in grokparms if appropriate. */
18787 deprecated_state
= DEPRECATED_SUPPRESS
;
18791 /* If a function parameter pack was specified and an implicit template
18792 parameter was introduced during cp_parser_parameter_declaration,
18793 change any implicit parameters introduced into packs. */
18794 if (parser
->implicit_template_parms
18795 && parameter
->declarator
18796 && parameter
->declarator
->parameter_pack_p
)
18798 int latest_template_parm_idx
= TREE_VEC_LENGTH
18799 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
18801 if (latest_template_parm_idx
!= template_parm_idx
)
18802 parameter
->decl_specifiers
.type
= convert_generic_types_to_packs
18803 (parameter
->decl_specifiers
.type
,
18804 template_parm_idx
, latest_template_parm_idx
);
18807 decl
= grokdeclarator (parameter
->declarator
,
18808 ¶meter
->decl_specifiers
,
18810 parameter
->default_argument
!= NULL_TREE
,
18811 ¶meter
->decl_specifiers
.attributes
);
18814 deprecated_state
= DEPRECATED_NORMAL
;
18816 /* If a parse error occurred parsing the parameter declaration,
18817 then the entire parameter-declaration-list is erroneous. */
18818 if (decl
== error_mark_node
)
18821 parameters
= error_mark_node
;
18825 if (parameter
->decl_specifiers
.attributes
)
18826 cplus_decl_attributes (&decl
,
18827 parameter
->decl_specifiers
.attributes
,
18829 if (DECL_NAME (decl
))
18830 decl
= pushdecl (decl
);
18832 if (decl
!= error_mark_node
)
18834 retrofit_lang_decl (decl
);
18835 DECL_PARM_INDEX (decl
) = ++index
;
18836 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
18839 /* Add the new parameter to the list. */
18840 *tail
= build_tree_list (parameter
->default_argument
, decl
);
18841 tail
= &TREE_CHAIN (*tail
);
18843 /* Peek at the next token. */
18844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
18845 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
18846 /* These are for Objective-C++ */
18847 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18848 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18849 /* The parameter-declaration-list is complete. */
18851 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
18855 /* Peek at the next token. */
18856 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
18857 /* If it's an ellipsis, then the list is complete. */
18858 if (token
->type
== CPP_ELLIPSIS
)
18860 /* Otherwise, there must be more parameters. Consume the
18862 cp_lexer_consume_token (parser
->lexer
);
18863 /* When parsing something like:
18865 int i(float f, double d)
18867 we can tell after seeing the declaration for "f" that we
18868 are not looking at an initialization of a variable "i",
18869 but rather at the declaration of a function "i".
18871 Due to the fact that the parsing of template arguments
18872 (as specified to a template-id) requires backtracking we
18873 cannot use this technique when inside a template argument
18875 if (!parser
->in_template_argument_list_p
18876 && !parser
->in_type_id_in_expr_p
18877 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
18878 /* However, a parameter-declaration of the form
18879 "float(f)" (which is a valid declaration of a
18880 parameter "f") can also be interpreted as an
18881 expression (the conversion of "f" to "float"). */
18882 && !parenthesized_p
)
18883 cp_parser_commit_to_tentative_parse (parser
);
18887 cp_parser_error (parser
, "expected %<,%> or %<...%>");
18888 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
18889 cp_parser_skip_to_closing_parenthesis (parser
,
18890 /*recovering=*/true,
18891 /*or_comma=*/false,
18892 /*consume_paren=*/false);
18897 parser
->in_unbraced_linkage_specification_p
18898 = saved_in_unbraced_linkage_specification_p
;
18900 /* Reset implicit_template_scope if we are about to leave the function
18901 parameter list that introduced it. Note that for out-of-line member
18902 definitions, there will be one or more class scopes before we get to
18903 the template parameter scope. */
18905 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
18906 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
18908 while (maybe_its
->kind
== sk_class
)
18909 maybe_its
= maybe_its
->level_chain
;
18910 if (maybe_its
== its
)
18912 parser
->implicit_template_parms
= 0;
18913 parser
->implicit_template_scope
= 0;
18920 /* Parse a parameter declaration.
18922 parameter-declaration:
18923 decl-specifier-seq ... [opt] declarator
18924 decl-specifier-seq declarator = assignment-expression
18925 decl-specifier-seq ... [opt] abstract-declarator [opt]
18926 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18928 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18929 declares a template parameter. (In that case, a non-nested `>'
18930 token encountered during the parsing of the assignment-expression
18931 is not interpreted as a greater-than operator.)
18933 Returns a representation of the parameter, or NULL if an error
18934 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18935 true iff the declarator is of the form "(p)". */
18937 static cp_parameter_declarator
*
18938 cp_parser_parameter_declaration (cp_parser
*parser
,
18939 bool template_parm_p
,
18940 bool *parenthesized_p
)
18942 int declares_class_or_enum
;
18943 cp_decl_specifier_seq decl_specifiers
;
18944 cp_declarator
*declarator
;
18945 tree default_argument
;
18946 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
18947 const char *saved_message
;
18949 /* In a template parameter, `>' is not an operator.
18953 When parsing a default template-argument for a non-type
18954 template-parameter, the first non-nested `>' is taken as the end
18955 of the template parameter-list rather than a greater-than
18958 /* Type definitions may not appear in parameter types. */
18959 saved_message
= parser
->type_definition_forbidden_message
;
18960 parser
->type_definition_forbidden_message
18961 = G_("types may not be defined in parameter types");
18963 /* Parse the declaration-specifiers. */
18964 cp_parser_decl_specifier_seq (parser
,
18965 CP_PARSER_FLAGS_NONE
,
18967 &declares_class_or_enum
);
18969 /* Complain about missing 'typename' or other invalid type names. */
18970 if (!decl_specifiers
.any_type_specifiers_p
18971 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
18972 decl_specifiers
.type
= error_mark_node
;
18974 /* If an error occurred, there's no reason to attempt to parse the
18975 rest of the declaration. */
18976 if (cp_parser_error_occurred (parser
))
18978 parser
->type_definition_forbidden_message
= saved_message
;
18982 /* Peek at the next token. */
18983 token
= cp_lexer_peek_token (parser
->lexer
);
18985 /* If the next token is a `)', `,', `=', `>', or `...', then there
18986 is no declarator. However, when variadic templates are enabled,
18987 there may be a declarator following `...'. */
18988 if (token
->type
== CPP_CLOSE_PAREN
18989 || token
->type
== CPP_COMMA
18990 || token
->type
== CPP_EQ
18991 || token
->type
== CPP_GREATER
)
18994 if (parenthesized_p
)
18995 *parenthesized_p
= false;
18997 /* Otherwise, there should be a declarator. */
19000 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
19001 parser
->default_arg_ok_p
= false;
19003 /* After seeing a decl-specifier-seq, if the next token is not a
19004 "(", there is no possibility that the code is a valid
19005 expression. Therefore, if parsing tentatively, we commit at
19007 if (!parser
->in_template_argument_list_p
19008 /* In an expression context, having seen:
19012 we cannot be sure whether we are looking at a
19013 function-type (taking a "char" as a parameter) or a cast
19014 of some object of type "char" to "int". */
19015 && !parser
->in_type_id_in_expr_p
19016 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
19017 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
19018 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
19019 cp_parser_commit_to_tentative_parse (parser
);
19020 /* Parse the declarator. */
19021 declarator_token_start
= token
;
19022 declarator
= cp_parser_declarator (parser
,
19023 CP_PARSER_DECLARATOR_EITHER
,
19024 /*ctor_dtor_or_conv_p=*/NULL
,
19026 /*member_p=*/false,
19027 /*friend_p=*/false);
19028 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
19029 /* After the declarator, allow more attributes. */
19030 decl_specifiers
.attributes
19031 = chainon (decl_specifiers
.attributes
,
19032 cp_parser_attributes_opt (parser
));
19035 /* If the next token is an ellipsis, and we have not seen a
19036 declarator name, and the type of the declarator contains parameter
19037 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19038 a parameter pack expansion expression. Otherwise, leave the
19039 ellipsis for a C-style variadic function. */
19040 token
= cp_lexer_peek_token (parser
->lexer
);
19041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19043 tree type
= decl_specifiers
.type
;
19045 if (type
&& DECL_P (type
))
19046 type
= TREE_TYPE (type
);
19049 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
19050 && declarator_can_be_parameter_pack (declarator
)
19051 && (!declarator
|| !declarator
->parameter_pack_p
)
19052 && uses_parameter_packs (type
))
19054 /* Consume the `...'. */
19055 cp_lexer_consume_token (parser
->lexer
);
19056 maybe_warn_variadic_templates ();
19058 /* Build a pack expansion type */
19060 declarator
->parameter_pack_p
= true;
19062 decl_specifiers
.type
= make_pack_expansion (type
);
19066 /* The restriction on defining new types applies only to the type
19067 of the parameter, not to the default argument. */
19068 parser
->type_definition_forbidden_message
= saved_message
;
19070 /* If the next token is `=', then process a default argument. */
19071 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19073 token
= cp_lexer_peek_token (parser
->lexer
);
19074 /* If we are defining a class, then the tokens that make up the
19075 default argument must be saved and processed later. */
19076 if (!template_parm_p
&& at_class_scope_p ()
19077 && TYPE_BEING_DEFINED (current_class_type
)
19078 && !LAMBDA_TYPE_P (current_class_type
))
19079 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
19080 /* Outside of a class definition, we can just parse the
19081 assignment-expression. */
19084 = cp_parser_default_argument (parser
, template_parm_p
);
19086 if (!parser
->default_arg_ok_p
)
19088 if (flag_permissive
)
19089 warning (0, "deprecated use of default argument for parameter of non-function");
19092 error_at (token
->location
,
19093 "default arguments are only "
19094 "permitted for function parameters");
19095 default_argument
= NULL_TREE
;
19098 else if ((declarator
&& declarator
->parameter_pack_p
)
19099 || (decl_specifiers
.type
19100 && PACK_EXPANSION_P (decl_specifiers
.type
)))
19102 /* Find the name of the parameter pack. */
19103 cp_declarator
*id_declarator
= declarator
;
19104 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
19105 id_declarator
= id_declarator
->declarator
;
19107 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
19108 error_at (declarator_token_start
->location
,
19110 ? G_("template parameter pack %qD "
19111 "cannot have a default argument")
19112 : G_("parameter pack %qD cannot have "
19113 "a default argument"),
19114 id_declarator
->u
.id
.unqualified_name
);
19116 error_at (declarator_token_start
->location
,
19118 ? G_("template parameter pack cannot have "
19119 "a default argument")
19120 : G_("parameter pack cannot have a "
19121 "default argument"));
19123 default_argument
= NULL_TREE
;
19127 default_argument
= NULL_TREE
;
19129 return make_parameter_declarator (&decl_specifiers
,
19134 /* Parse a default argument and return it.
19136 TEMPLATE_PARM_P is true if this is a default argument for a
19137 non-type template parameter. */
19139 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
19141 tree default_argument
= NULL_TREE
;
19142 bool saved_greater_than_is_operator_p
;
19143 bool saved_local_variables_forbidden_p
;
19144 bool non_constant_p
, is_direct_init
;
19146 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19148 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
19149 parser
->greater_than_is_operator_p
= !template_parm_p
;
19150 /* Local variable names (and the `this' keyword) may not
19151 appear in a default argument. */
19152 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
19153 parser
->local_variables_forbidden_p
= true;
19154 /* Parse the assignment-expression. */
19155 if (template_parm_p
)
19156 push_deferring_access_checks (dk_no_deferred
);
19157 tree saved_class_ptr
= NULL_TREE
;
19158 tree saved_class_ref
= NULL_TREE
;
19159 /* The "this" pointer is not valid in a default argument. */
19162 saved_class_ptr
= current_class_ptr
;
19163 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
19164 saved_class_ref
= current_class_ref
;
19165 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
19168 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
19169 /* Restore the "this" pointer. */
19172 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
19173 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
19175 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
19176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19177 if (template_parm_p
)
19178 pop_deferring_access_checks ();
19179 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
19180 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
19182 return default_argument
;
19185 /* Parse a function-body.
19188 compound_statement */
19191 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
19193 cp_parser_compound_statement (parser
, NULL
, in_function_try_block
, true);
19196 /* Parse a ctor-initializer-opt followed by a function-body. Return
19197 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19198 is true we are parsing a function-try-block. */
19201 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
19202 bool in_function_try_block
)
19205 bool ctor_initializer_p
;
19206 const bool check_body_p
=
19207 DECL_CONSTRUCTOR_P (current_function_decl
)
19208 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
);
19211 /* Begin the function body. */
19212 body
= begin_function_body ();
19213 /* Parse the optional ctor-initializer. */
19214 ctor_initializer_p
= cp_parser_ctor_initializer_opt (parser
);
19216 /* If we're parsing a constexpr constructor definition, we need
19217 to check that the constructor body is indeed empty. However,
19218 before we get to cp_parser_function_body lot of junk has been
19219 generated, so we can't just check that we have an empty block.
19220 Rather we take a snapshot of the outermost block, and check whether
19221 cp_parser_function_body changed its state. */
19224 list
= cur_stmt_list
;
19225 if (STATEMENT_LIST_TAIL (list
))
19226 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
19228 /* Parse the function-body. */
19229 cp_parser_function_body (parser
, in_function_try_block
);
19231 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
19232 /* Finish the function body. */
19233 finish_function_body (body
);
19235 return ctor_initializer_p
;
19238 /* Parse an initializer.
19241 = initializer-clause
19242 ( expression-list )
19244 Returns an expression representing the initializer. If no
19245 initializer is present, NULL_TREE is returned.
19247 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19248 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19249 set to TRUE if there is no initializer present. If there is an
19250 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19251 is set to true; otherwise it is set to false. */
19254 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
19255 bool* non_constant_p
)
19260 /* Peek at the next token. */
19261 token
= cp_lexer_peek_token (parser
->lexer
);
19263 /* Let our caller know whether or not this initializer was
19265 *is_direct_init
= (token
->type
!= CPP_EQ
);
19266 /* Assume that the initializer is constant. */
19267 *non_constant_p
= false;
19269 if (token
->type
== CPP_EQ
)
19271 /* Consume the `='. */
19272 cp_lexer_consume_token (parser
->lexer
);
19273 /* Parse the initializer-clause. */
19274 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
19276 else if (token
->type
== CPP_OPEN_PAREN
)
19278 vec
<tree
, va_gc
> *vec
;
19279 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
19281 /*allow_expansion_p=*/true,
19284 return error_mark_node
;
19285 init
= build_tree_list_vec (vec
);
19286 release_tree_vector (vec
);
19288 else if (token
->type
== CPP_OPEN_BRACE
)
19290 cp_lexer_set_source_position (parser
->lexer
);
19291 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
19292 init
= cp_parser_braced_list (parser
, non_constant_p
);
19293 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
19297 /* Anything else is an error. */
19298 cp_parser_error (parser
, "expected initializer");
19299 init
= error_mark_node
;
19305 /* Parse an initializer-clause.
19307 initializer-clause:
19308 assignment-expression
19311 Returns an expression representing the initializer.
19313 If the `assignment-expression' production is used the value
19314 returned is simply a representation for the expression.
19316 Otherwise, calls cp_parser_braced_list. */
19319 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
19323 /* Assume the expression is constant. */
19324 *non_constant_p
= false;
19326 /* If it is not a `{', then we are looking at an
19327 assignment-expression. */
19328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
19331 = cp_parser_constant_expression (parser
,
19332 /*allow_non_constant_p=*/true,
19336 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
19338 return initializer
;
19341 /* Parse a brace-enclosed initializer list.
19344 { initializer-list , [opt] }
19347 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19348 the elements of the initializer-list (or NULL, if the last
19349 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19350 NULL_TREE. There is no way to detect whether or not the optional
19351 trailing `,' was provided. NON_CONSTANT_P is as for
19352 cp_parser_initializer. */
19355 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
19359 /* Consume the `{' token. */
19360 cp_lexer_consume_token (parser
->lexer
);
19361 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19362 initializer
= make_node (CONSTRUCTOR
);
19363 /* If it's not a `}', then there is a non-trivial initializer. */
19364 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
19366 /* Parse the initializer list. */
19367 CONSTRUCTOR_ELTS (initializer
)
19368 = cp_parser_initializer_list (parser
, non_constant_p
);
19369 /* A trailing `,' token is allowed. */
19370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19371 cp_lexer_consume_token (parser
->lexer
);
19374 *non_constant_p
= false;
19375 /* Now, there should be a trailing `}'. */
19376 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19377 TREE_TYPE (initializer
) = init_list_type_node
;
19378 return initializer
;
19381 /* Consume tokens up to, and including, the next non-nested closing `]'.
19382 Returns true iff we found a closing `]'. */
19385 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
19387 unsigned square_depth
= 0;
19391 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
19393 switch (token
->type
)
19396 case CPP_PRAGMA_EOL
:
19397 /* If we've run out of tokens, then there is no closing `]'. */
19400 case CPP_OPEN_SQUARE
:
19404 case CPP_CLOSE_SQUARE
:
19405 if (!square_depth
--)
19407 cp_lexer_consume_token (parser
->lexer
);
19416 /* Consume the token. */
19417 cp_lexer_consume_token (parser
->lexer
);
19421 /* Return true if we are looking at an array-designator, false otherwise. */
19424 cp_parser_array_designator_p (cp_parser
*parser
)
19426 /* Consume the `['. */
19427 cp_lexer_consume_token (parser
->lexer
);
19429 cp_lexer_save_tokens (parser
->lexer
);
19431 /* Skip tokens until the next token is a closing square bracket.
19432 If we find the closing `]', and the next token is a `=', then
19433 we are looking at an array designator. */
19434 bool array_designator_p
19435 = (cp_parser_skip_to_closing_square_bracket (parser
)
19436 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
19438 /* Roll back the tokens we skipped. */
19439 cp_lexer_rollback_tokens (parser
->lexer
);
19441 return array_designator_p
;
19444 /* Parse an initializer-list.
19447 initializer-clause ... [opt]
19448 initializer-list , initializer-clause ... [opt]
19453 designation initializer-clause ...[opt]
19454 initializer-list , designation initializer-clause ...[opt]
19459 [ constant-expression ] =
19461 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19462 for the initializer. If the INDEX of the elt is non-NULL, it is the
19463 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19464 as for cp_parser_initializer. */
19466 static vec
<constructor_elt
, va_gc
> *
19467 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
)
19469 vec
<constructor_elt
, va_gc
> *v
= NULL
;
19471 /* Assume all of the expressions are constant. */
19472 *non_constant_p
= false;
19474 /* Parse the rest of the list. */
19480 bool clause_non_constant_p
;
19482 /* If the next token is an identifier and the following one is a
19483 colon, we are looking at the GNU designated-initializer
19485 if (cp_parser_allow_gnu_extensions_p (parser
)
19486 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
19487 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
19489 /* Warn the user that they are using an extension. */
19490 pedwarn (input_location
, OPT_Wpedantic
,
19491 "ISO C++ does not allow designated initializers");
19492 /* Consume the identifier. */
19493 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19494 /* Consume the `:'. */
19495 cp_lexer_consume_token (parser
->lexer
);
19497 /* Also handle the C99 syntax, '. id ='. */
19498 else if (cp_parser_allow_gnu_extensions_p (parser
)
19499 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
19500 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
19501 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
19503 /* Warn the user that they are using an extension. */
19504 pedwarn (input_location
, OPT_Wpedantic
,
19505 "ISO C++ does not allow C99 designated initializers");
19506 /* Consume the `.'. */
19507 cp_lexer_consume_token (parser
->lexer
);
19508 /* Consume the identifier. */
19509 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
19510 /* Consume the `='. */
19511 cp_lexer_consume_token (parser
->lexer
);
19513 /* Also handle C99 array designators, '[ const ] ='. */
19514 else if (cp_parser_allow_gnu_extensions_p (parser
)
19515 && !c_dialect_objc ()
19516 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
19518 /* In C++11, [ could start a lambda-introducer. */
19519 bool non_const
= false;
19521 cp_parser_parse_tentatively (parser
);
19523 if (!cp_parser_array_designator_p (parser
))
19525 cp_parser_simulate_error (parser
);
19526 designator
= NULL_TREE
;
19530 designator
= cp_parser_constant_expression (parser
, true,
19532 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
19533 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19536 if (!cp_parser_parse_definitely (parser
))
19537 designator
= NULL_TREE
;
19538 else if (non_const
)
19539 require_potential_rvalue_constant_expression (designator
);
19542 designator
= NULL_TREE
;
19544 /* Parse the initializer. */
19545 initializer
= cp_parser_initializer_clause (parser
,
19546 &clause_non_constant_p
);
19547 /* If any clause is non-constant, so is the entire initializer. */
19548 if (clause_non_constant_p
)
19549 *non_constant_p
= true;
19551 /* If we have an ellipsis, this is an initializer pack
19553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19555 /* Consume the `...'. */
19556 cp_lexer_consume_token (parser
->lexer
);
19558 /* Turn the initializer into an initializer expansion. */
19559 initializer
= make_pack_expansion (initializer
);
19562 /* Add it to the vector. */
19563 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
19565 /* If the next token is not a comma, we have reached the end of
19567 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19570 /* Peek at the next token. */
19571 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19572 /* If the next token is a `}', then we're still done. An
19573 initializer-clause can have a trailing `,' after the
19574 initializer-list and before the closing `}'. */
19575 if (token
->type
== CPP_CLOSE_BRACE
)
19578 /* Consume the `,' token. */
19579 cp_lexer_consume_token (parser
->lexer
);
19585 /* Classes [gram.class] */
19587 /* Parse a class-name.
19593 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19594 to indicate that names looked up in dependent types should be
19595 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19596 keyword has been used to indicate that the name that appears next
19597 is a template. TAG_TYPE indicates the explicit tag given before
19598 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19599 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19600 is the class being defined in a class-head.
19602 Returns the TYPE_DECL representing the class. */
19605 cp_parser_class_name (cp_parser
*parser
,
19606 bool typename_keyword_p
,
19607 bool template_keyword_p
,
19608 enum tag_types tag_type
,
19609 bool check_dependency_p
,
19611 bool is_declaration
)
19617 tree identifier
= NULL_TREE
;
19619 /* All class-names start with an identifier. */
19620 token
= cp_lexer_peek_token (parser
->lexer
);
19621 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
19623 cp_parser_error (parser
, "expected class-name");
19624 return error_mark_node
;
19627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19628 to a template-id, so we save it here. */
19629 scope
= parser
->scope
;
19630 if (scope
== error_mark_node
)
19631 return error_mark_node
;
19633 /* Any name names a type if we're following the `typename' keyword
19634 in a qualified name where the enclosing scope is type-dependent. */
19635 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
19636 && dependent_type_p (scope
));
19637 /* Handle the common case (an identifier, but not a template-id)
19639 if (token
->type
== CPP_NAME
19640 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
19642 cp_token
*identifier_token
;
19645 /* Look for the identifier. */
19646 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
19647 ambiguous_p
= identifier_token
->error_reported
;
19648 identifier
= cp_parser_identifier (parser
);
19649 /* If the next token isn't an identifier, we are certainly not
19650 looking at a class-name. */
19651 if (identifier
== error_mark_node
)
19652 decl
= error_mark_node
;
19653 /* If we know this is a type-name, there's no need to look it
19655 else if (typename_p
)
19659 tree ambiguous_decls
;
19660 /* If we already know that this lookup is ambiguous, then
19661 we've already issued an error message; there's no reason
19665 cp_parser_simulate_error (parser
);
19666 return error_mark_node
;
19668 /* If the next token is a `::', then the name must be a type
19671 [basic.lookup.qual]
19673 During the lookup for a name preceding the :: scope
19674 resolution operator, object, function, and enumerator
19675 names are ignored. */
19676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19677 tag_type
= typename_type
;
19678 /* Look up the name. */
19679 decl
= cp_parser_lookup_name (parser
, identifier
,
19681 /*is_template=*/false,
19682 /*is_namespace=*/false,
19683 check_dependency_p
,
19685 identifier_token
->location
);
19686 if (ambiguous_decls
)
19688 if (cp_parser_parsing_tentatively (parser
))
19689 cp_parser_simulate_error (parser
);
19690 return error_mark_node
;
19696 /* Try a template-id. */
19697 decl
= cp_parser_template_id (parser
, template_keyword_p
,
19698 check_dependency_p
,
19701 if (decl
== error_mark_node
)
19702 return error_mark_node
;
19705 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
19707 /* If this is a typename, create a TYPENAME_TYPE. */
19708 if (typename_p
&& decl
!= error_mark_node
)
19710 decl
= make_typename_type (scope
, decl
, typename_type
,
19711 /*complain=*/tf_error
);
19712 if (decl
!= error_mark_node
)
19713 decl
= TYPE_NAME (decl
);
19716 decl
= strip_using_decl (decl
);
19718 /* Check to see that it is really the name of a class. */
19719 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19720 && identifier_p (TREE_OPERAND (decl
, 0))
19721 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
19722 /* Situations like this:
19724 template <typename T> struct A {
19725 typename T::template X<int>::I i;
19728 are problematic. Is `T::template X<int>' a class-name? The
19729 standard does not seem to be definitive, but there is no other
19730 valid interpretation of the following `::'. Therefore, those
19731 names are considered class-names. */
19733 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
19734 if (decl
!= error_mark_node
)
19735 decl
= TYPE_NAME (decl
);
19737 else if (TREE_CODE (decl
) != TYPE_DECL
19738 || TREE_TYPE (decl
) == error_mark_node
19739 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
19740 /* In Objective-C 2.0, a classname followed by '.' starts a
19741 dot-syntax expression, and it's not a type-name. */
19742 || (c_dialect_objc ()
19743 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
19744 && objc_is_class_name (decl
)))
19745 decl
= error_mark_node
;
19747 if (decl
== error_mark_node
)
19748 cp_parser_error (parser
, "expected class-name");
19749 else if (identifier
&& !parser
->scope
)
19750 maybe_note_name_used_in_class (identifier
, decl
);
19755 /* Parse a class-specifier.
19758 class-head { member-specification [opt] }
19760 Returns the TREE_TYPE representing the class. */
19763 cp_parser_class_specifier_1 (cp_parser
* parser
)
19766 tree attributes
= NULL_TREE
;
19767 bool nested_name_specifier_p
;
19768 unsigned saved_num_template_parameter_lists
;
19769 bool saved_in_function_body
;
19770 unsigned char in_statement
;
19771 bool in_switch_statement_p
;
19772 bool saved_in_unbraced_linkage_specification_p
;
19773 tree old_scope
= NULL_TREE
;
19774 tree scope
= NULL_TREE
;
19775 cp_token
*closing_brace
;
19777 push_deferring_access_checks (dk_no_deferred
);
19779 /* Parse the class-head. */
19780 type
= cp_parser_class_head (parser
,
19781 &nested_name_specifier_p
);
19782 /* If the class-head was a semantic disaster, skip the entire body
19786 cp_parser_skip_to_end_of_block_or_statement (parser
);
19787 pop_deferring_access_checks ();
19788 return error_mark_node
;
19791 /* Look for the `{'. */
19792 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
19794 pop_deferring_access_checks ();
19795 return error_mark_node
;
19798 cp_ensure_no_omp_declare_simd (parser
);
19800 /* Issue an error message if type-definitions are forbidden here. */
19801 cp_parser_check_type_definition (parser
);
19802 /* Remember that we are defining one more class. */
19803 ++parser
->num_classes_being_defined
;
19804 /* Inside the class, surrounding template-parameter-lists do not
19806 saved_num_template_parameter_lists
19807 = parser
->num_template_parameter_lists
;
19808 parser
->num_template_parameter_lists
= 0;
19809 /* We are not in a function body. */
19810 saved_in_function_body
= parser
->in_function_body
;
19811 parser
->in_function_body
= false;
19812 /* Or in a loop. */
19813 in_statement
= parser
->in_statement
;
19814 parser
->in_statement
= 0;
19815 /* Or in a switch. */
19816 in_switch_statement_p
= parser
->in_switch_statement_p
;
19817 parser
->in_switch_statement_p
= false;
19818 /* We are not immediately inside an extern "lang" block. */
19819 saved_in_unbraced_linkage_specification_p
19820 = parser
->in_unbraced_linkage_specification_p
;
19821 parser
->in_unbraced_linkage_specification_p
= false;
19823 /* Start the class. */
19824 if (nested_name_specifier_p
)
19826 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
19827 old_scope
= push_inner_scope (scope
);
19829 type
= begin_class_definition (type
);
19831 if (type
== error_mark_node
)
19832 /* If the type is erroneous, skip the entire body of the class. */
19833 cp_parser_skip_to_closing_brace (parser
);
19835 /* Parse the member-specification. */
19836 cp_parser_member_specification_opt (parser
);
19838 /* Look for the trailing `}'. */
19839 closing_brace
= cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
19840 /* Look for trailing attributes to apply to this class. */
19841 if (cp_parser_allow_gnu_extensions_p (parser
))
19842 attributes
= cp_parser_gnu_attributes_opt (parser
);
19843 if (type
!= error_mark_node
)
19844 type
= finish_struct (type
, attributes
);
19845 if (nested_name_specifier_p
)
19846 pop_inner_scope (old_scope
, scope
);
19848 /* We've finished a type definition. Check for the common syntax
19849 error of forgetting a semicolon after the definition. We need to
19850 be careful, as we can't just check for not-a-semicolon and be done
19851 with it; the user might have typed:
19853 class X { } c = ...;
19854 class X { } *p = ...;
19856 and so forth. Instead, enumerate all the possible tokens that
19857 might follow this production; if we don't see one of them, then
19858 complain and silently insert the semicolon. */
19860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19861 bool want_semicolon
= true;
19863 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19864 /* Don't try to parse c++11 attributes here. As per the
19865 grammar, that should be a task for
19866 cp_parser_decl_specifier_seq. */
19867 want_semicolon
= false;
19869 switch (token
->type
)
19872 case CPP_SEMICOLON
:
19875 case CPP_OPEN_PAREN
:
19876 case CPP_CLOSE_PAREN
:
19878 want_semicolon
= false;
19881 /* While it's legal for type qualifiers and storage class
19882 specifiers to follow type definitions in the grammar, only
19883 compiler testsuites contain code like that. Assume that if
19884 we see such code, then what we're really seeing is a case
19888 const <type> var = ...;
19893 static <type> func (...) ...
19895 i.e. the qualifier or specifier applies to the next
19896 declaration. To do so, however, we need to look ahead one
19897 more token to see if *that* token is a type specifier.
19899 This code could be improved to handle:
19902 static const <type> var = ...; */
19904 if (keyword_is_decl_specifier (token
->keyword
))
19906 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
19908 /* Handling user-defined types here would be nice, but very
19911 = (lookahead
->type
== CPP_KEYWORD
19912 && keyword_begins_type_specifier (lookahead
->keyword
));
19919 /* If we don't have a type, then something is very wrong and we
19920 shouldn't try to do anything clever. Likewise for not seeing the
19922 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
19924 cp_token_position prev
19925 = cp_lexer_previous_token_position (parser
->lexer
);
19926 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
19927 location_t loc
= prev_token
->location
;
19929 if (CLASSTYPE_DECLARED_CLASS (type
))
19930 error_at (loc
, "expected %<;%> after class definition");
19931 else if (TREE_CODE (type
) == RECORD_TYPE
)
19932 error_at (loc
, "expected %<;%> after struct definition");
19933 else if (TREE_CODE (type
) == UNION_TYPE
)
19934 error_at (loc
, "expected %<;%> after union definition");
19936 gcc_unreachable ();
19938 /* Unget one token and smash it to look as though we encountered
19939 a semicolon in the input stream. */
19940 cp_lexer_set_token_position (parser
->lexer
, prev
);
19941 token
= cp_lexer_peek_token (parser
->lexer
);
19942 token
->type
= CPP_SEMICOLON
;
19943 token
->keyword
= RID_MAX
;
19947 /* If this class is not itself within the scope of another class,
19948 then we need to parse the bodies of all of the queued function
19949 definitions. Note that the queued functions defined in a class
19950 are not always processed immediately following the
19951 class-specifier for that class. Consider:
19954 struct B { void f() { sizeof (A); } };
19957 If `f' were processed before the processing of `A' were
19958 completed, there would be no way to compute the size of `A'.
19959 Note that the nesting we are interested in here is lexical --
19960 not the semantic nesting given by TYPE_CONTEXT. In particular,
19963 struct A { struct B; };
19964 struct A::B { void f() { } };
19966 there is no need to delay the parsing of `A::B::f'. */
19967 if (--parser
->num_classes_being_defined
== 0)
19970 tree class_type
= NULL_TREE
;
19971 tree pushed_scope
= NULL_TREE
;
19973 cp_default_arg_entry
*e
;
19974 tree save_ccp
, save_ccr
;
19976 /* In a first pass, parse default arguments to the functions.
19977 Then, in a second pass, parse the bodies of the functions.
19978 This two-phased approach handles cases like:
19986 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
19989 /* If there are default arguments that have not yet been processed,
19990 take care of them now. */
19991 if (class_type
!= e
->class_type
)
19994 pop_scope (pushed_scope
);
19995 class_type
= e
->class_type
;
19996 pushed_scope
= push_scope (class_type
);
19998 /* Make sure that any template parameters are in scope. */
19999 maybe_begin_member_template_processing (decl
);
20000 /* Parse the default argument expressions. */
20001 cp_parser_late_parsing_default_args (parser
, decl
);
20002 /* Remove any template parameters from the symbol table. */
20003 maybe_end_member_template_processing ();
20005 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
20006 /* Now parse any NSDMIs. */
20007 save_ccp
= current_class_ptr
;
20008 save_ccr
= current_class_ref
;
20009 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
20011 if (class_type
!= DECL_CONTEXT (decl
))
20014 pop_scope (pushed_scope
);
20015 class_type
= DECL_CONTEXT (decl
);
20016 pushed_scope
= push_scope (class_type
);
20018 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
20019 cp_parser_late_parsing_nsdmi (parser
, decl
);
20021 vec_safe_truncate (unparsed_nsdmis
, 0);
20022 current_class_ptr
= save_ccp
;
20023 current_class_ref
= save_ccr
;
20025 pop_scope (pushed_scope
);
20027 /* Now do some post-NSDMI bookkeeping. */
20028 FOR_EACH_VEC_SAFE_ELT (unparsed_classes
, ix
, class_type
)
20029 after_nsdmi_defaulted_late_checks (class_type
);
20030 vec_safe_truncate (unparsed_classes
, 0);
20031 after_nsdmi_defaulted_late_checks (type
);
20033 /* Now parse the body of the functions. */
20036 /* OpenMP UDRs need to be parsed before all other functions. */
20037 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20038 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
20039 cp_parser_late_parsing_for_member (parser
, decl
);
20040 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20041 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
20042 cp_parser_late_parsing_for_member (parser
, decl
);
20045 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
20046 cp_parser_late_parsing_for_member (parser
, decl
);
20047 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
20050 vec_safe_push (unparsed_classes
, type
);
20052 /* Put back any saved access checks. */
20053 pop_deferring_access_checks ();
20055 /* Restore saved state. */
20056 parser
->in_switch_statement_p
= in_switch_statement_p
;
20057 parser
->in_statement
= in_statement
;
20058 parser
->in_function_body
= saved_in_function_body
;
20059 parser
->num_template_parameter_lists
20060 = saved_num_template_parameter_lists
;
20061 parser
->in_unbraced_linkage_specification_p
20062 = saved_in_unbraced_linkage_specification_p
;
20068 cp_parser_class_specifier (cp_parser
* parser
)
20071 timevar_push (TV_PARSE_STRUCT
);
20072 ret
= cp_parser_class_specifier_1 (parser
);
20073 timevar_pop (TV_PARSE_STRUCT
);
20077 /* Parse a class-head.
20080 class-key identifier [opt] base-clause [opt]
20081 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20082 class-key nested-name-specifier [opt] template-id
20085 class-virt-specifier:
20089 class-key attributes identifier [opt] base-clause [opt]
20090 class-key attributes nested-name-specifier identifier base-clause [opt]
20091 class-key attributes nested-name-specifier [opt] template-id
20094 Upon return BASES is initialized to the list of base classes (or
20095 NULL, if there are none) in the same form returned by
20096 cp_parser_base_clause.
20098 Returns the TYPE of the indicated class. Sets
20099 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20100 involving a nested-name-specifier was used, and FALSE otherwise.
20102 Returns error_mark_node if this is not a class-head.
20104 Returns NULL_TREE if the class-head is syntactically valid, but
20105 semantically invalid in a way that means we should skip the entire
20106 body of the class. */
20109 cp_parser_class_head (cp_parser
* parser
,
20110 bool* nested_name_specifier_p
)
20112 tree nested_name_specifier
;
20113 enum tag_types class_key
;
20114 tree id
= NULL_TREE
;
20115 tree type
= NULL_TREE
;
20118 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
20119 bool template_id_p
= false;
20120 bool qualified_p
= false;
20121 bool invalid_nested_name_p
= false;
20122 bool invalid_explicit_specialization_p
= false;
20123 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20124 tree pushed_scope
= NULL_TREE
;
20125 unsigned num_templates
;
20126 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
20127 /* Assume no nested-name-specifier will be present. */
20128 *nested_name_specifier_p
= false;
20129 /* Assume no template parameter lists will be used in defining the
20132 parser
->colon_corrects_to_scope_p
= false;
20134 /* Look for the class-key. */
20135 class_key
= cp_parser_class_key (parser
);
20136 if (class_key
== none_type
)
20137 return error_mark_node
;
20139 /* Parse the attributes. */
20140 attributes
= cp_parser_attributes_opt (parser
);
20142 /* If the next token is `::', that is invalid -- but sometimes
20143 people do try to write:
20147 Handle this gracefully by accepting the extra qualifier, and then
20148 issuing an error about it later if this really is a
20149 class-head. If it turns out just to be an elaborated type
20150 specifier, remain silent. */
20151 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
20152 qualified_p
= true;
20154 push_deferring_access_checks (dk_no_check
);
20156 /* Determine the name of the class. Begin by looking for an
20157 optional nested-name-specifier. */
20158 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
20159 nested_name_specifier
20160 = cp_parser_nested_name_specifier_opt (parser
,
20161 /*typename_keyword_p=*/false,
20162 /*check_dependency_p=*/false,
20164 /*is_declaration=*/false);
20165 /* If there was a nested-name-specifier, then there *must* be an
20167 if (nested_name_specifier
)
20169 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20170 /* Although the grammar says `identifier', it really means
20171 `class-name' or `template-name'. You are only allowed to
20172 define a class that has already been declared with this
20175 The proposed resolution for Core Issue 180 says that wherever
20176 you see `class T::X' you should treat `X' as a type-name.
20178 It is OK to define an inaccessible class; for example:
20180 class A { class B; };
20183 We do not know if we will see a class-name, or a
20184 template-name. We look for a class-name first, in case the
20185 class-name is a template-id; if we looked for the
20186 template-name first we would stop after the template-name. */
20187 cp_parser_parse_tentatively (parser
);
20188 type
= cp_parser_class_name (parser
,
20189 /*typename_keyword_p=*/false,
20190 /*template_keyword_p=*/false,
20192 /*check_dependency_p=*/false,
20193 /*class_head_p=*/true,
20194 /*is_declaration=*/false);
20195 /* If that didn't work, ignore the nested-name-specifier. */
20196 if (!cp_parser_parse_definitely (parser
))
20198 invalid_nested_name_p
= true;
20199 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20200 id
= cp_parser_identifier (parser
);
20201 if (id
== error_mark_node
)
20204 /* If we could not find a corresponding TYPE, treat this
20205 declaration like an unqualified declaration. */
20206 if (type
== error_mark_node
)
20207 nested_name_specifier
= NULL_TREE
;
20208 /* Otherwise, count the number of templates used in TYPE and its
20209 containing scopes. */
20214 for (scope
= TREE_TYPE (type
);
20215 scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
;
20216 scope
= get_containing_scope (scope
))
20218 && CLASS_TYPE_P (scope
)
20219 && CLASSTYPE_TEMPLATE_INFO (scope
)
20220 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope
))
20221 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope
)
20222 || uses_template_parms (CLASSTYPE_TI_ARGS (scope
))))
20226 /* Otherwise, the identifier is optional. */
20229 /* We don't know whether what comes next is a template-id,
20230 an identifier, or nothing at all. */
20231 cp_parser_parse_tentatively (parser
);
20232 /* Check for a template-id. */
20233 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20234 id
= cp_parser_template_id (parser
,
20235 /*template_keyword_p=*/false,
20236 /*check_dependency_p=*/true,
20238 /*is_declaration=*/true);
20239 /* If that didn't work, it could still be an identifier. */
20240 if (!cp_parser_parse_definitely (parser
))
20242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20244 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
20245 id
= cp_parser_identifier (parser
);
20252 template_id_p
= true;
20257 pop_deferring_access_checks ();
20261 cp_parser_check_for_invalid_template_id (parser
, id
,
20263 type_start_token
->location
);
20265 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
20267 /* If it's not a `:' or a `{' then we can't really be looking at a
20268 class-head, since a class-head only appears as part of a
20269 class-specifier. We have to detect this situation before calling
20270 xref_tag, since that has irreversible side-effects. */
20271 if (!cp_parser_next_token_starts_class_definition_p (parser
))
20273 cp_parser_error (parser
, "expected %<{%> or %<:%>");
20274 type
= error_mark_node
;
20278 /* At this point, we're going ahead with the class-specifier, even
20279 if some other problem occurs. */
20280 cp_parser_commit_to_tentative_parse (parser
);
20281 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
20283 cp_parser_error (parser
,
20284 "cannot specify %<override%> for a class");
20285 type
= error_mark_node
;
20288 /* Issue the error about the overly-qualified name now. */
20291 cp_parser_error (parser
,
20292 "global qualification of class name is invalid");
20293 type
= error_mark_node
;
20296 else if (invalid_nested_name_p
)
20298 cp_parser_error (parser
,
20299 "qualified name does not name a class");
20300 type
= error_mark_node
;
20303 else if (nested_name_specifier
)
20307 /* Reject typedef-names in class heads. */
20308 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
20310 error_at (type_start_token
->location
,
20311 "invalid class name in declaration of %qD",
20317 /* Figure out in what scope the declaration is being placed. */
20318 scope
= current_scope ();
20319 /* If that scope does not contain the scope in which the
20320 class was originally declared, the program is invalid. */
20321 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
20323 if (at_namespace_scope_p ())
20324 error_at (type_start_token
->location
,
20325 "declaration of %qD in namespace %qD which does not "
20327 type
, scope
, nested_name_specifier
);
20329 error_at (type_start_token
->location
,
20330 "declaration of %qD in %qD which does not enclose %qD",
20331 type
, scope
, nested_name_specifier
);
20337 A declarator-id shall not be qualified except for the
20338 definition of a ... nested class outside of its class
20339 ... [or] the definition or explicit instantiation of a
20340 class member of a namespace outside of its namespace. */
20341 if (scope
== nested_name_specifier
)
20343 permerror (nested_name_specifier_token_start
->location
,
20344 "extra qualification not allowed");
20345 nested_name_specifier
= NULL_TREE
;
20349 /* An explicit-specialization must be preceded by "template <>". If
20350 it is not, try to recover gracefully. */
20351 if (at_namespace_scope_p ()
20352 && parser
->num_template_parameter_lists
== 0
20355 error_at (type_start_token
->location
,
20356 "an explicit specialization must be preceded by %<template <>%>");
20357 invalid_explicit_specialization_p
= true;
20358 /* Take the same action that would have been taken by
20359 cp_parser_explicit_specialization. */
20360 ++parser
->num_template_parameter_lists
;
20361 begin_specialization ();
20363 /* There must be no "return" statements between this point and the
20364 end of this function; set "type "to the correct return value and
20365 use "goto done;" to return. */
20366 /* Make sure that the right number of template parameters were
20368 if (!cp_parser_check_template_parameters (parser
, num_templates
,
20369 type_start_token
->location
,
20370 /*declarator=*/NULL
))
20372 /* If something went wrong, there is no point in even trying to
20373 process the class-definition. */
20378 /* Look up the type. */
20381 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
20382 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
20383 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
20385 error_at (type_start_token
->location
,
20386 "function template %qD redeclared as a class template", id
);
20387 type
= error_mark_node
;
20391 type
= TREE_TYPE (id
);
20392 type
= maybe_process_partial_specialization (type
);
20394 if (nested_name_specifier
)
20395 pushed_scope
= push_scope (nested_name_specifier
);
20397 else if (nested_name_specifier
)
20403 template <typename T> struct S { struct T };
20404 template <typename T> struct S<T>::T { };
20406 we will get a TYPENAME_TYPE when processing the definition of
20407 `S::T'. We need to resolve it to the actual type before we
20408 try to define it. */
20409 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
20411 class_type
= resolve_typename_type (TREE_TYPE (type
),
20412 /*only_current_p=*/false);
20413 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
20414 type
= TYPE_NAME (class_type
);
20417 cp_parser_error (parser
, "could not resolve typename type");
20418 type
= error_mark_node
;
20422 if (maybe_process_partial_specialization (TREE_TYPE (type
))
20423 == error_mark_node
)
20429 class_type
= current_class_type
;
20430 /* Enter the scope indicated by the nested-name-specifier. */
20431 pushed_scope
= push_scope (nested_name_specifier
);
20432 /* Get the canonical version of this type. */
20433 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
20434 /* Call push_template_decl if it seems like we should be defining a
20435 template either from the template headers or the type we're
20436 defining, so that we diagnose both extra and missing headers. */
20437 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20438 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
20439 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
20441 type
= push_template_decl (type
);
20442 if (type
== error_mark_node
)
20449 type
= TREE_TYPE (type
);
20450 *nested_name_specifier_p
= true;
20452 else /* The name is not a nested name. */
20454 /* If the class was unnamed, create a dummy name. */
20456 id
= make_anon_name ();
20457 type
= xref_tag (class_key
, id
, /*tag_scope=*/ts_current
,
20458 parser
->num_template_parameter_lists
);
20461 /* Indicate whether this class was declared as a `class' or as a
20463 if (TREE_CODE (type
) == RECORD_TYPE
)
20464 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
20465 cp_parser_check_class_key (class_key
, type
);
20467 /* If this type was already complete, and we see another definition,
20468 that's an error. */
20469 if (type
!= error_mark_node
&& COMPLETE_TYPE_P (type
))
20471 error_at (type_start_token
->location
, "redefinition of %q#T",
20473 error_at (type_start_token
->location
, "previous definition of %q+#T",
20478 else if (type
== error_mark_node
)
20483 /* Apply attributes now, before any use of the class as a template
20484 argument in its base list. */
20485 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
20486 fixup_attribute_variants (type
);
20489 /* We will have entered the scope containing the class; the names of
20490 base classes should be looked up in that context. For example:
20492 struct A { struct B {}; struct C; };
20493 struct A::C : B {};
20497 /* Get the list of base-classes, if there is one. */
20498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20500 /* PR59482: enter the class scope so that base-specifiers are looked
20504 bases
= cp_parser_base_clause (parser
);
20505 /* PR59482: get out of the previously pushed class scope so that the
20506 subsequent pops pop the right thing. */
20513 /* If we're really defining a class, process the base classes.
20514 If they're invalid, fail. */
20515 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
20516 && !xref_basetypes (type
, bases
))
20520 /* Leave the scope given by the nested-name-specifier. We will
20521 enter the class scope itself while processing the members. */
20523 pop_scope (pushed_scope
);
20525 if (invalid_explicit_specialization_p
)
20527 end_specialization ();
20528 --parser
->num_template_parameter_lists
;
20532 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20533 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
20534 CLASSTYPE_FINAL (type
) = 1;
20536 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
20540 /* Parse a class-key.
20547 Returns the kind of class-key specified, or none_type to indicate
20550 static enum tag_types
20551 cp_parser_class_key (cp_parser
* parser
)
20554 enum tag_types tag_type
;
20556 /* Look for the class-key. */
20557 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
20561 /* Check to see if the TOKEN is a class-key. */
20562 tag_type
= cp_parser_token_is_class_key (token
);
20564 cp_parser_error (parser
, "expected class-key");
20568 /* Parse a type-parameter-key.
20570 type-parameter-key:
20576 cp_parser_type_parameter_key (cp_parser
* parser
)
20578 /* Look for the type-parameter-key. */
20579 enum tag_types tag_type
= none_type
;
20580 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20581 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
20583 cp_lexer_consume_token (parser
->lexer
);
20584 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx1z
)
20585 /* typename is not allowed in a template template parameter
20586 by the standard until C++1Z. */
20587 pedwarn (token
->location
, OPT_Wpedantic
,
20588 "ISO C++ forbids typename key in template template parameter;"
20589 " use -std=c++1z or -std=gnu++1z");
20592 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
20597 /* Parse an (optional) member-specification.
20599 member-specification:
20600 member-declaration member-specification [opt]
20601 access-specifier : member-specification [opt] */
20604 cp_parser_member_specification_opt (cp_parser
* parser
)
20611 /* Peek at the next token. */
20612 token
= cp_lexer_peek_token (parser
->lexer
);
20613 /* If it's a `}', or EOF then we've seen all the members. */
20614 if (token
->type
== CPP_CLOSE_BRACE
20615 || token
->type
== CPP_EOF
20616 || token
->type
== CPP_PRAGMA_EOL
)
20619 /* See if this token is a keyword. */
20620 keyword
= token
->keyword
;
20624 case RID_PROTECTED
:
20626 /* Consume the access-specifier. */
20627 cp_lexer_consume_token (parser
->lexer
);
20628 /* Remember which access-specifier is active. */
20629 current_access_specifier
= token
->u
.value
;
20630 /* Look for the `:'. */
20631 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
20635 /* Accept #pragmas at class scope. */
20636 if (token
->type
== CPP_PRAGMA
)
20638 cp_parser_pragma (parser
, pragma_member
);
20642 /* Otherwise, the next construction must be a
20643 member-declaration. */
20644 cp_parser_member_declaration (parser
);
20649 /* Parse a member-declaration.
20651 member-declaration:
20652 decl-specifier-seq [opt] member-declarator-list [opt] ;
20653 function-definition ; [opt]
20654 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20656 template-declaration
20659 member-declarator-list:
20661 member-declarator-list , member-declarator
20664 declarator pure-specifier [opt]
20665 declarator constant-initializer [opt]
20666 identifier [opt] : constant-expression
20670 member-declaration:
20671 __extension__ member-declaration
20674 declarator attributes [opt] pure-specifier [opt]
20675 declarator attributes [opt] constant-initializer [opt]
20676 identifier [opt] attributes [opt] : constant-expression
20680 member-declaration:
20681 static_assert-declaration */
20684 cp_parser_member_declaration (cp_parser
* parser
)
20686 cp_decl_specifier_seq decl_specifiers
;
20687 tree prefix_attributes
;
20689 int declares_class_or_enum
;
20691 cp_token
*token
= NULL
;
20692 cp_token
*decl_spec_token_start
= NULL
;
20693 cp_token
*initializer_token_start
= NULL
;
20694 int saved_pedantic
;
20695 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
20697 /* Check for the `__extension__' keyword. */
20698 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
20701 cp_parser_member_declaration (parser
);
20702 /* Restore the old value of the PEDANTIC flag. */
20703 pedantic
= saved_pedantic
;
20708 /* Check for a template-declaration. */
20709 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
20711 /* An explicit specialization here is an error condition, and we
20712 expect the specialization handler to detect and report this. */
20713 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
20714 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
20715 cp_parser_explicit_specialization (parser
);
20717 cp_parser_template_declaration (parser
, /*member_p=*/true);
20722 /* Check for a using-declaration. */
20723 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
20725 if (cxx_dialect
< cxx11
)
20727 /* Parse the using-declaration. */
20728 cp_parser_using_declaration (parser
,
20729 /*access_declaration_p=*/false);
20735 bool alias_decl_expected
;
20736 cp_parser_parse_tentatively (parser
);
20737 decl
= cp_parser_alias_declaration (parser
);
20738 /* Note that if we actually see the '=' token after the
20739 identifier, cp_parser_alias_declaration commits the
20740 tentative parse. In that case, we really expects an
20741 alias-declaration. Otherwise, we expect a using
20743 alias_decl_expected
=
20744 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
20745 cp_parser_parse_definitely (parser
);
20747 if (alias_decl_expected
)
20748 finish_member_declaration (decl
);
20750 cp_parser_using_declaration (parser
,
20751 /*access_declaration_p=*/false);
20756 /* Check for @defs. */
20757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
20760 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
20761 ivar
= ivar_chains
;
20765 ivar
= TREE_CHAIN (member
);
20766 TREE_CHAIN (member
) = NULL_TREE
;
20767 finish_member_declaration (member
);
20772 /* If the next token is `static_assert' we have a static assertion. */
20773 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
20775 cp_parser_static_assert (parser
, /*member_p=*/true);
20779 parser
->colon_corrects_to_scope_p
= false;
20781 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
20784 /* Parse the decl-specifier-seq. */
20785 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
20786 cp_parser_decl_specifier_seq (parser
,
20787 CP_PARSER_FLAGS_OPTIONAL
,
20789 &declares_class_or_enum
);
20790 /* Check for an invalid type-name. */
20791 if (!decl_specifiers
.any_type_specifiers_p
20792 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
20794 /* If there is no declarator, then the decl-specifier-seq should
20796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20798 /* If there was no decl-specifier-seq, and the next token is a
20799 `;', then we have something like:
20805 Each member-declaration shall declare at least one member
20806 name of the class. */
20807 if (!decl_specifiers
.any_specifiers_p
)
20809 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20810 if (!in_system_header_at (token
->location
))
20811 pedwarn (token
->location
, OPT_Wpedantic
, "extra %<;%>");
20817 /* See if this declaration is a friend. */
20818 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20819 /* If there were decl-specifiers, check to see if there was
20820 a class-declaration. */
20821 type
= check_tag_decl (&decl_specifiers
,
20822 /*explicit_type_instantiation_p=*/false);
20823 /* Nested classes have already been added to the class, but
20824 a `friend' needs to be explicitly registered. */
20827 /* If the `friend' keyword was present, the friend must
20828 be introduced with a class-key. */
20829 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
20830 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
20831 "in C++03 a class-key must be used "
20832 "when declaring a friend");
20835 template <typename T> struct A {
20836 friend struct A<T>::B;
20839 A<T>::B will be represented by a TYPENAME_TYPE, and
20840 therefore not recognized by check_tag_decl. */
20843 type
= decl_specifiers
.type
;
20844 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
20845 type
= TREE_TYPE (type
);
20847 if (!type
|| !TYPE_P (type
))
20848 error_at (decl_spec_token_start
->location
,
20849 "friend declaration does not name a class or "
20852 make_friend_class (current_class_type
, type
,
20853 /*complain=*/true);
20855 /* If there is no TYPE, an error message will already have
20857 else if (!type
|| type
== error_mark_node
)
20859 /* An anonymous aggregate has to be handled specially; such
20860 a declaration really declares a data member (with a
20861 particular type), as opposed to a nested class. */
20862 else if (ANON_AGGR_TYPE_P (type
))
20865 if (decl_specifiers
.storage_class
!= sc_none
)
20866 error_at (decl_spec_token_start
->location
,
20867 "a storage class on an anonymous aggregate "
20868 "in class scope is not allowed");
20870 /* Remove constructors and such from TYPE, now that we
20871 know it is an anonymous aggregate. */
20872 fixup_anonymous_aggr (type
);
20873 /* And make the corresponding data member. */
20874 decl
= build_decl (decl_spec_token_start
->location
,
20875 FIELD_DECL
, NULL_TREE
, type
);
20876 /* Add it to the class. */
20877 finish_member_declaration (decl
);
20880 cp_parser_check_access_in_redeclaration
20882 decl_spec_token_start
->location
);
20887 bool assume_semicolon
= false;
20889 /* Clear attributes from the decl_specifiers but keep them
20890 around as prefix attributes that apply them to the entity
20892 prefix_attributes
= decl_specifiers
.attributes
;
20893 decl_specifiers
.attributes
= NULL_TREE
;
20895 /* See if these declarations will be friends. */
20896 friend_p
= cp_parser_friend_p (&decl_specifiers
);
20898 /* Keep going until we hit the `;' at the end of the
20900 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20902 tree attributes
= NULL_TREE
;
20903 tree first_attribute
;
20905 /* Peek at the next token. */
20906 token
= cp_lexer_peek_token (parser
->lexer
);
20908 /* Check for a bitfield declaration. */
20909 if (token
->type
== CPP_COLON
20910 || (token
->type
== CPP_NAME
20911 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
20917 /* Get the name of the bitfield. Note that we cannot just
20918 check TOKEN here because it may have been invalidated by
20919 the call to cp_lexer_peek_nth_token above. */
20920 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_COLON
)
20921 identifier
= cp_parser_identifier (parser
);
20923 identifier
= NULL_TREE
;
20925 /* Consume the `:' token. */
20926 cp_lexer_consume_token (parser
->lexer
);
20927 /* Get the width of the bitfield. */
20929 = cp_parser_constant_expression (parser
);
20931 /* Look for attributes that apply to the bitfield. */
20932 attributes
= cp_parser_attributes_opt (parser
);
20933 /* Remember which attributes are prefix attributes and
20935 first_attribute
= attributes
;
20936 /* Combine the attributes. */
20937 attributes
= chainon (prefix_attributes
, attributes
);
20939 /* Create the bitfield declaration. */
20940 decl
= grokbitfield (identifier
20941 ? make_id_declarator (NULL_TREE
,
20951 cp_declarator
*declarator
;
20953 tree asm_specification
;
20954 int ctor_dtor_or_conv_p
;
20956 /* Parse the declarator. */
20958 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20959 &ctor_dtor_or_conv_p
,
20960 /*parenthesized_p=*/NULL
,
20964 /* If something went wrong parsing the declarator, make sure
20965 that we at least consume some tokens. */
20966 if (declarator
== cp_error_declarator
)
20968 /* Skip to the end of the statement. */
20969 cp_parser_skip_to_end_of_statement (parser
);
20970 /* If the next token is not a semicolon, that is
20971 probably because we just skipped over the body of
20972 a function. So, we consume a semicolon if
20973 present, but do not issue an error message if it
20975 if (cp_lexer_next_token_is (parser
->lexer
,
20977 cp_lexer_consume_token (parser
->lexer
);
20981 if (declares_class_or_enum
& 2)
20982 cp_parser_check_for_definition_in_return_type
20983 (declarator
, decl_specifiers
.type
,
20984 decl_specifiers
.locations
[ds_type_spec
]);
20986 /* Look for an asm-specification. */
20987 asm_specification
= cp_parser_asm_specification_opt (parser
);
20988 /* Look for attributes that apply to the declaration. */
20989 attributes
= cp_parser_attributes_opt (parser
);
20990 /* Remember which attributes are prefix attributes and
20992 first_attribute
= attributes
;
20993 /* Combine the attributes. */
20994 attributes
= chainon (prefix_attributes
, attributes
);
20996 /* If it's an `=', then we have a constant-initializer or a
20997 pure-specifier. It is not correct to parse the
20998 initializer before registering the member declaration
20999 since the member declaration should be in scope while
21000 its initializer is processed. However, the rest of the
21001 front end does not yet provide an interface that allows
21002 us to handle this correctly. */
21003 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
21007 A pure-specifier shall be used only in the declaration of
21008 a virtual function.
21010 A member-declarator can contain a constant-initializer
21011 only if it declares a static member of integral or
21014 Therefore, if the DECLARATOR is for a function, we look
21015 for a pure-specifier; otherwise, we look for a
21016 constant-initializer. When we call `grokfield', it will
21017 perform more stringent semantics checks. */
21018 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
21019 if (function_declarator_p (declarator
)
21020 || (decl_specifiers
.type
21021 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
21022 && declarator
->kind
== cdk_id
21023 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
21024 == FUNCTION_TYPE
)))
21025 initializer
= cp_parser_pure_specifier (parser
);
21026 else if (decl_specifiers
.storage_class
!= sc_static
)
21027 initializer
= cp_parser_save_nsdmi (parser
);
21028 else if (cxx_dialect
>= cxx11
)
21031 /* Don't require a constant rvalue in C++11, since we
21032 might want a reference constant. We'll enforce
21033 constancy later. */
21034 cp_lexer_consume_token (parser
->lexer
);
21035 /* Parse the initializer. */
21036 initializer
= cp_parser_initializer_clause (parser
,
21040 /* Parse the initializer. */
21041 initializer
= cp_parser_constant_initializer (parser
);
21043 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
21044 && !function_declarator_p (declarator
))
21047 if (decl_specifiers
.storage_class
!= sc_static
)
21048 initializer
= cp_parser_save_nsdmi (parser
);
21050 initializer
= cp_parser_initializer (parser
, &x
, &x
);
21052 /* Otherwise, there is no initializer. */
21054 initializer
= NULL_TREE
;
21056 /* See if we are probably looking at a function
21057 definition. We are certainly not looking at a
21058 member-declarator. Calling `grokfield' has
21059 side-effects, so we must not do it unless we are sure
21060 that we are looking at a member-declarator. */
21061 if (cp_parser_token_starts_function_definition_p
21062 (cp_lexer_peek_token (parser
->lexer
)))
21064 /* The grammar does not allow a pure-specifier to be
21065 used when a member function is defined. (It is
21066 possible that this fact is an oversight in the
21067 standard, since a pure function may be defined
21068 outside of the class-specifier. */
21069 if (initializer
&& initializer_token_start
)
21070 error_at (initializer_token_start
->location
,
21071 "pure-specifier on function-definition");
21072 decl
= cp_parser_save_member_function_body (parser
,
21076 if (parser
->fully_implicit_function_template_p
)
21077 decl
= finish_fully_implicit_template (parser
, decl
);
21078 /* If the member was not a friend, declare it here. */
21080 finish_member_declaration (decl
);
21081 /* Peek at the next token. */
21082 token
= cp_lexer_peek_token (parser
->lexer
);
21083 /* If the next token is a semicolon, consume it. */
21084 if (token
->type
== CPP_SEMICOLON
)
21085 cp_lexer_consume_token (parser
->lexer
);
21089 if (declarator
->kind
== cdk_function
)
21090 declarator
->id_loc
= token
->location
;
21091 /* Create the declaration. */
21092 decl
= grokfield (declarator
, &decl_specifiers
,
21093 initializer
, /*init_const_expr_p=*/true,
21094 asm_specification
, attributes
);
21095 if (parser
->fully_implicit_function_template_p
)
21098 finish_fully_implicit_template (parser
, 0);
21100 decl
= finish_fully_implicit_template (parser
, decl
);
21104 cp_finalize_omp_declare_simd (parser
, decl
);
21106 /* Reset PREFIX_ATTRIBUTES. */
21107 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
21108 attributes
= TREE_CHAIN (attributes
);
21110 TREE_CHAIN (attributes
) = NULL_TREE
;
21112 /* If there is any qualification still in effect, clear it
21113 now; we will be starting fresh with the next declarator. */
21114 parser
->scope
= NULL_TREE
;
21115 parser
->qualifying_scope
= NULL_TREE
;
21116 parser
->object_scope
= NULL_TREE
;
21117 /* If it's a `,', then there are more declarators. */
21118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
21120 cp_lexer_consume_token (parser
->lexer
);
21121 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
21123 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21124 error_at (token
->location
,
21125 "stray %<,%> at end of member declaration");
21128 /* If the next token isn't a `;', then we have a parse error. */
21129 else if (cp_lexer_next_token_is_not (parser
->lexer
,
21132 /* The next token might be a ways away from where the
21133 actual semicolon is missing. Find the previous token
21134 and use that for our error position. */
21135 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
21136 error_at (token
->location
,
21137 "expected %<;%> at end of member declaration");
21139 /* Assume that the user meant to provide a semicolon. If
21140 we were to cp_parser_skip_to_end_of_statement, we might
21141 skip to a semicolon inside a member function definition
21142 and issue nonsensical error messages. */
21143 assume_semicolon
= true;
21148 /* Add DECL to the list of members. */
21150 /* Explicitly include, eg, NSDMIs, for better error
21151 recovery (c++/58650). */
21152 || !DECL_DECLARES_FUNCTION_P (decl
))
21153 finish_member_declaration (decl
);
21155 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21156 cp_parser_save_default_args (parser
, decl
);
21157 else if (TREE_CODE (decl
) == FIELD_DECL
21158 && !DECL_C_BIT_FIELD (decl
)
21159 && DECL_INITIAL (decl
))
21160 /* Add DECL to the queue of NSDMI to be parsed later. */
21161 vec_safe_push (unparsed_nsdmis
, decl
);
21164 if (assume_semicolon
)
21169 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21171 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
21174 /* Parse a pure-specifier.
21179 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21180 Otherwise, ERROR_MARK_NODE is returned. */
21183 cp_parser_pure_specifier (cp_parser
* parser
)
21187 /* Look for the `=' token. */
21188 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21189 return error_mark_node
;
21190 /* Look for the `0' token. */
21191 token
= cp_lexer_peek_token (parser
->lexer
);
21193 if (token
->type
== CPP_EOF
21194 || token
->type
== CPP_PRAGMA_EOL
)
21195 return error_mark_node
;
21197 cp_lexer_consume_token (parser
->lexer
);
21199 /* Accept = default or = delete in c++0x mode. */
21200 if (token
->keyword
== RID_DEFAULT
21201 || token
->keyword
== RID_DELETE
)
21203 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
21204 return token
->u
.value
;
21207 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21208 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
21210 cp_parser_error (parser
,
21211 "invalid pure specifier (only %<= 0%> is allowed)");
21212 cp_parser_skip_to_end_of_statement (parser
);
21213 return error_mark_node
;
21215 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21217 error_at (token
->location
, "templates may not be %<virtual%>");
21218 return error_mark_node
;
21221 return integer_zero_node
;
21224 /* Parse a constant-initializer.
21226 constant-initializer:
21227 = constant-expression
21229 Returns a representation of the constant-expression. */
21232 cp_parser_constant_initializer (cp_parser
* parser
)
21234 /* Look for the `=' token. */
21235 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
21236 return error_mark_node
;
21238 /* It is invalid to write:
21240 struct S { static const int i = { 7 }; };
21243 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
21245 cp_parser_error (parser
,
21246 "a brace-enclosed initializer is not allowed here");
21247 /* Consume the opening brace. */
21248 cp_lexer_consume_token (parser
->lexer
);
21249 /* Skip the initializer. */
21250 cp_parser_skip_to_closing_brace (parser
);
21251 /* Look for the trailing `}'. */
21252 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
21254 return error_mark_node
;
21257 return cp_parser_constant_expression (parser
);
21260 /* Derived classes [gram.class.derived] */
21262 /* Parse a base-clause.
21265 : base-specifier-list
21267 base-specifier-list:
21268 base-specifier ... [opt]
21269 base-specifier-list , base-specifier ... [opt]
21271 Returns a TREE_LIST representing the base-classes, in the order in
21272 which they were declared. The representation of each node is as
21273 described by cp_parser_base_specifier.
21275 In the case that no bases are specified, this function will return
21276 NULL_TREE, not ERROR_MARK_NODE. */
21279 cp_parser_base_clause (cp_parser
* parser
)
21281 tree bases
= NULL_TREE
;
21283 /* Look for the `:' that begins the list. */
21284 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
21286 /* Scan the base-specifier-list. */
21291 bool pack_expansion_p
= false;
21293 /* Look for the base-specifier. */
21294 base
= cp_parser_base_specifier (parser
);
21295 /* Look for the (optional) ellipsis. */
21296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21298 /* Consume the `...'. */
21299 cp_lexer_consume_token (parser
->lexer
);
21301 pack_expansion_p
= true;
21304 /* Add BASE to the front of the list. */
21305 if (base
&& base
!= error_mark_node
)
21307 if (pack_expansion_p
)
21308 /* Make this a pack expansion type. */
21309 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
21311 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
21313 TREE_CHAIN (base
) = bases
;
21317 /* Peek at the next token. */
21318 token
= cp_lexer_peek_token (parser
->lexer
);
21319 /* If it's not a comma, then the list is complete. */
21320 if (token
->type
!= CPP_COMMA
)
21322 /* Consume the `,'. */
21323 cp_lexer_consume_token (parser
->lexer
);
21326 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21327 base class had a qualified name. However, the next name that
21328 appears is certainly not qualified. */
21329 parser
->scope
= NULL_TREE
;
21330 parser
->qualifying_scope
= NULL_TREE
;
21331 parser
->object_scope
= NULL_TREE
;
21333 return nreverse (bases
);
21336 /* Parse a base-specifier.
21339 :: [opt] nested-name-specifier [opt] class-name
21340 virtual access-specifier [opt] :: [opt] nested-name-specifier
21342 access-specifier virtual [opt] :: [opt] nested-name-specifier
21345 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21346 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21347 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21348 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21351 cp_parser_base_specifier (cp_parser
* parser
)
21355 bool virtual_p
= false;
21356 bool duplicate_virtual_error_issued_p
= false;
21357 bool duplicate_access_error_issued_p
= false;
21358 bool class_scope_p
, template_p
;
21359 tree access
= access_default_node
;
21362 /* Process the optional `virtual' and `access-specifier'. */
21365 /* Peek at the next token. */
21366 token
= cp_lexer_peek_token (parser
->lexer
);
21367 /* Process `virtual'. */
21368 switch (token
->keyword
)
21371 /* If `virtual' appears more than once, issue an error. */
21372 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
21374 cp_parser_error (parser
,
21375 "%<virtual%> specified more than once in base-specified");
21376 duplicate_virtual_error_issued_p
= true;
21381 /* Consume the `virtual' token. */
21382 cp_lexer_consume_token (parser
->lexer
);
21387 case RID_PROTECTED
:
21389 /* If more than one access specifier appears, issue an
21391 if (access
!= access_default_node
21392 && !duplicate_access_error_issued_p
)
21394 cp_parser_error (parser
,
21395 "more than one access specifier in base-specified");
21396 duplicate_access_error_issued_p
= true;
21399 access
= ridpointers
[(int) token
->keyword
];
21401 /* Consume the access-specifier. */
21402 cp_lexer_consume_token (parser
->lexer
);
21411 /* It is not uncommon to see programs mechanically, erroneously, use
21412 the 'typename' keyword to denote (dependent) qualified types
21413 as base classes. */
21414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
21416 token
= cp_lexer_peek_token (parser
->lexer
);
21417 if (!processing_template_decl
)
21418 error_at (token
->location
,
21419 "keyword %<typename%> not allowed outside of templates");
21421 error_at (token
->location
,
21422 "keyword %<typename%> not allowed in this context "
21423 "(the base class is implicitly a type)");
21424 cp_lexer_consume_token (parser
->lexer
);
21427 /* Look for the optional `::' operator. */
21428 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
21429 /* Look for the nested-name-specifier. The simplest way to
21434 The keyword `typename' is not permitted in a base-specifier or
21435 mem-initializer; in these contexts a qualified name that
21436 depends on a template-parameter is implicitly assumed to be a
21439 is to pretend that we have seen the `typename' keyword at this
21441 cp_parser_nested_name_specifier_opt (parser
,
21442 /*typename_keyword_p=*/true,
21443 /*check_dependency_p=*/true,
21445 /*is_declaration=*/true);
21446 /* If the base class is given by a qualified name, assume that names
21447 we see are type names or templates, as appropriate. */
21448 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
21449 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
21452 && cp_lexer_next_token_is_decltype (parser
->lexer
))
21453 /* DR 950 allows decltype as a base-specifier. */
21454 type
= cp_parser_decltype (parser
);
21457 /* Otherwise, look for the class-name. */
21458 type
= cp_parser_class_name (parser
,
21462 /*check_dependency_p=*/true,
21463 /*class_head_p=*/false,
21464 /*is_declaration=*/true);
21465 type
= TREE_TYPE (type
);
21468 if (type
== error_mark_node
)
21469 return error_mark_node
;
21471 return finish_base_specifier (type
, access
, virtual_p
);
21474 /* Exception handling [gram.exception] */
21476 /* Parse an (optional) noexcept-specification.
21478 noexcept-specification:
21479 noexcept ( constant-expression ) [opt]
21481 If no noexcept-specification is present, returns NULL_TREE.
21482 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21483 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21484 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21485 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21486 in which case a boolean condition is returned instead. */
21489 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
21490 bool require_constexpr
,
21491 bool* consumed_expr
,
21495 const char *saved_message
;
21497 /* Peek at the next token. */
21498 token
= cp_lexer_peek_token (parser
->lexer
);
21500 /* Is it a noexcept-specification? */
21501 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
21504 cp_lexer_consume_token (parser
->lexer
);
21506 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
21508 cp_lexer_consume_token (parser
->lexer
);
21510 if (require_constexpr
)
21512 /* Types may not be defined in an exception-specification. */
21513 saved_message
= parser
->type_definition_forbidden_message
;
21514 parser
->type_definition_forbidden_message
21515 = G_("types may not be defined in an exception-specification");
21517 expr
= cp_parser_constant_expression (parser
);
21519 /* Restore the saved message. */
21520 parser
->type_definition_forbidden_message
= saved_message
;
21524 expr
= cp_parser_expression (parser
);
21525 *consumed_expr
= true;
21528 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21532 expr
= boolean_true_node
;
21533 if (!require_constexpr
)
21534 *consumed_expr
= false;
21537 /* We cannot build a noexcept-spec right away because this will check
21538 that expr is a constexpr. */
21540 return build_noexcept_spec (expr
, tf_warning_or_error
);
21548 /* Parse an (optional) exception-specification.
21550 exception-specification:
21551 throw ( type-id-list [opt] )
21553 Returns a TREE_LIST representing the exception-specification. The
21554 TREE_VALUE of each node is a type. */
21557 cp_parser_exception_specification_opt (cp_parser
* parser
)
21561 const char *saved_message
;
21563 /* Peek at the next token. */
21564 token
= cp_lexer_peek_token (parser
->lexer
);
21566 /* Is it a noexcept-specification? */
21567 type_id_list
= cp_parser_noexcept_specification_opt(parser
, true, NULL
,
21569 if (type_id_list
!= NULL_TREE
)
21570 return type_id_list
;
21572 /* If it's not `throw', then there's no exception-specification. */
21573 if (!cp_parser_is_keyword (token
, RID_THROW
))
21577 /* Enable this once a lot of code has transitioned to noexcept? */
21578 if (cxx_dialect
>= cxx11
&& !in_system_header_at (input_location
))
21579 warning (OPT_Wdeprecated
, "dynamic exception specifications are "
21580 "deprecated in C++0x; use %<noexcept%> instead");
21583 /* Consume the `throw'. */
21584 cp_lexer_consume_token (parser
->lexer
);
21586 /* Look for the `('. */
21587 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21589 /* Peek at the next token. */
21590 token
= cp_lexer_peek_token (parser
->lexer
);
21591 /* If it's not a `)', then there is a type-id-list. */
21592 if (token
->type
!= CPP_CLOSE_PAREN
)
21594 /* Types may not be defined in an exception-specification. */
21595 saved_message
= parser
->type_definition_forbidden_message
;
21596 parser
->type_definition_forbidden_message
21597 = G_("types may not be defined in an exception-specification");
21598 /* Parse the type-id-list. */
21599 type_id_list
= cp_parser_type_id_list (parser
);
21600 /* Restore the saved message. */
21601 parser
->type_definition_forbidden_message
= saved_message
;
21604 type_id_list
= empty_except_spec
;
21606 /* Look for the `)'. */
21607 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21609 return type_id_list
;
21612 /* Parse an (optional) type-id-list.
21616 type-id-list , type-id ... [opt]
21618 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21619 in the order that the types were presented. */
21622 cp_parser_type_id_list (cp_parser
* parser
)
21624 tree types
= NULL_TREE
;
21631 /* Get the next type-id. */
21632 type
= cp_parser_type_id (parser
);
21633 /* Parse the optional ellipsis. */
21634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21636 /* Consume the `...'. */
21637 cp_lexer_consume_token (parser
->lexer
);
21639 /* Turn the type into a pack expansion expression. */
21640 type
= make_pack_expansion (type
);
21642 /* Add it to the list. */
21643 types
= add_exception_specifier (types
, type
, /*complain=*/1);
21644 /* Peek at the next token. */
21645 token
= cp_lexer_peek_token (parser
->lexer
);
21646 /* If it is not a `,', we are done. */
21647 if (token
->type
!= CPP_COMMA
)
21649 /* Consume the `,'. */
21650 cp_lexer_consume_token (parser
->lexer
);
21653 return nreverse (types
);
21656 /* Parse a try-block.
21659 try compound-statement handler-seq */
21662 cp_parser_try_block (cp_parser
* parser
)
21666 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
21667 if (parser
->in_function_body
21668 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
21669 error ("%<try%> in %<constexpr%> function");
21671 try_block
= begin_try_block ();
21672 cp_parser_compound_statement (parser
, NULL
, true, false);
21673 finish_try_block (try_block
);
21674 cp_parser_handler_seq (parser
);
21675 finish_handler_sequence (try_block
);
21680 /* Parse a function-try-block.
21682 function-try-block:
21683 try ctor-initializer [opt] function-body handler-seq */
21686 cp_parser_function_try_block (cp_parser
* parser
)
21688 tree compound_stmt
;
21690 bool ctor_initializer_p
;
21692 /* Look for the `try' keyword. */
21693 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
21695 /* Let the rest of the front end know where we are. */
21696 try_block
= begin_function_try_block (&compound_stmt
);
21697 /* Parse the function-body. */
21698 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
21699 (parser
, /*in_function_try_block=*/true);
21700 /* We're done with the `try' part. */
21701 finish_function_try_block (try_block
);
21702 /* Parse the handlers. */
21703 cp_parser_handler_seq (parser
);
21704 /* We're done with the handlers. */
21705 finish_function_handler_sequence (try_block
, compound_stmt
);
21707 return ctor_initializer_p
;
21710 /* Parse a handler-seq.
21713 handler handler-seq [opt] */
21716 cp_parser_handler_seq (cp_parser
* parser
)
21722 /* Parse the handler. */
21723 cp_parser_handler (parser
);
21724 /* Peek at the next token. */
21725 token
= cp_lexer_peek_token (parser
->lexer
);
21726 /* If it's not `catch' then there are no more handlers. */
21727 if (!cp_parser_is_keyword (token
, RID_CATCH
))
21732 /* Parse a handler.
21735 catch ( exception-declaration ) compound-statement */
21738 cp_parser_handler (cp_parser
* parser
)
21743 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
21744 handler
= begin_handler ();
21745 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21746 declaration
= cp_parser_exception_declaration (parser
);
21747 finish_handler_parms (declaration
, handler
);
21748 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21749 cp_parser_compound_statement (parser
, NULL
, false, false);
21750 finish_handler (handler
);
21753 /* Parse an exception-declaration.
21755 exception-declaration:
21756 type-specifier-seq declarator
21757 type-specifier-seq abstract-declarator
21761 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21762 ellipsis variant is used. */
21765 cp_parser_exception_declaration (cp_parser
* parser
)
21767 cp_decl_specifier_seq type_specifiers
;
21768 cp_declarator
*declarator
;
21769 const char *saved_message
;
21771 /* If it's an ellipsis, it's easy to handle. */
21772 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
21774 /* Consume the `...' token. */
21775 cp_lexer_consume_token (parser
->lexer
);
21779 /* Types may not be defined in exception-declarations. */
21780 saved_message
= parser
->type_definition_forbidden_message
;
21781 parser
->type_definition_forbidden_message
21782 = G_("types may not be defined in exception-declarations");
21784 /* Parse the type-specifier-seq. */
21785 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
21786 /*is_trailing_return=*/false,
21788 /* If it's a `)', then there is no declarator. */
21789 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
21792 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
21793 /*ctor_dtor_or_conv_p=*/NULL
,
21794 /*parenthesized_p=*/NULL
,
21795 /*member_p=*/false,
21796 /*friend_p=*/false);
21798 /* Restore the saved message. */
21799 parser
->type_definition_forbidden_message
= saved_message
;
21801 if (!type_specifiers
.any_specifiers_p
)
21802 return error_mark_node
;
21804 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
21807 /* Parse a throw-expression.
21810 throw assignment-expression [opt]
21812 Returns a THROW_EXPR representing the throw-expression. */
21815 cp_parser_throw_expression (cp_parser
* parser
)
21820 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
21821 token
= cp_lexer_peek_token (parser
->lexer
);
21822 /* Figure out whether or not there is an assignment-expression
21823 following the "throw" keyword. */
21824 if (token
->type
== CPP_COMMA
21825 || token
->type
== CPP_SEMICOLON
21826 || token
->type
== CPP_CLOSE_PAREN
21827 || token
->type
== CPP_CLOSE_SQUARE
21828 || token
->type
== CPP_CLOSE_BRACE
21829 || token
->type
== CPP_COLON
)
21830 expression
= NULL_TREE
;
21832 expression
= cp_parser_assignment_expression (parser
);
21834 return build_throw (expression
);
21837 /* GNU Extensions */
21839 /* Parse an (optional) asm-specification.
21842 asm ( string-literal )
21844 If the asm-specification is present, returns a STRING_CST
21845 corresponding to the string-literal. Otherwise, returns
21849 cp_parser_asm_specification_opt (cp_parser
* parser
)
21852 tree asm_specification
;
21854 /* Peek at the next token. */
21855 token
= cp_lexer_peek_token (parser
->lexer
);
21856 /* If the next token isn't the `asm' keyword, then there's no
21857 asm-specification. */
21858 if (!cp_parser_is_keyword (token
, RID_ASM
))
21861 /* Consume the `asm' token. */
21862 cp_lexer_consume_token (parser
->lexer
);
21863 /* Look for the `('. */
21864 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21866 /* Look for the string-literal. */
21867 asm_specification
= cp_parser_string_literal (parser
, false, false);
21869 /* Look for the `)'. */
21870 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21872 return asm_specification
;
21875 /* Parse an asm-operand-list.
21879 asm-operand-list , asm-operand
21882 string-literal ( expression )
21883 [ string-literal ] string-literal ( expression )
21885 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21886 each node is the expression. The TREE_PURPOSE is itself a
21887 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21888 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21889 is a STRING_CST for the string literal before the parenthesis. Returns
21890 ERROR_MARK_NODE if any of the operands are invalid. */
21893 cp_parser_asm_operand_list (cp_parser
* parser
)
21895 tree asm_operands
= NULL_TREE
;
21896 bool invalid_operands
= false;
21900 tree string_literal
;
21904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
21906 /* Consume the `[' token. */
21907 cp_lexer_consume_token (parser
->lexer
);
21908 /* Read the operand name. */
21909 name
= cp_parser_identifier (parser
);
21910 if (name
!= error_mark_node
)
21911 name
= build_string (IDENTIFIER_LENGTH (name
),
21912 IDENTIFIER_POINTER (name
));
21913 /* Look for the closing `]'. */
21914 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
21918 /* Look for the string-literal. */
21919 string_literal
= cp_parser_string_literal (parser
, false, false);
21921 /* Look for the `('. */
21922 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
21923 /* Parse the expression. */
21924 expression
= cp_parser_expression (parser
);
21925 /* Look for the `)'. */
21926 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
21928 if (name
== error_mark_node
21929 || string_literal
== error_mark_node
21930 || expression
== error_mark_node
)
21931 invalid_operands
= true;
21933 /* Add this operand to the list. */
21934 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
21937 /* If the next token is not a `,', there are no more
21939 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21941 /* Consume the `,'. */
21942 cp_lexer_consume_token (parser
->lexer
);
21945 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
21948 /* Parse an asm-clobber-list.
21952 asm-clobber-list , string-literal
21954 Returns a TREE_LIST, indicating the clobbers in the order that they
21955 appeared. The TREE_VALUE of each node is a STRING_CST. */
21958 cp_parser_asm_clobber_list (cp_parser
* parser
)
21960 tree clobbers
= NULL_TREE
;
21964 tree string_literal
;
21966 /* Look for the string literal. */
21967 string_literal
= cp_parser_string_literal (parser
, false, false);
21968 /* Add it to the list. */
21969 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
21970 /* If the next token is not a `,', then the list is
21972 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
21974 /* Consume the `,' token. */
21975 cp_lexer_consume_token (parser
->lexer
);
21981 /* Parse an asm-label-list.
21985 asm-label-list , identifier
21987 Returns a TREE_LIST, indicating the labels in the order that they
21988 appeared. The TREE_VALUE of each node is a label. */
21991 cp_parser_asm_label_list (cp_parser
* parser
)
21993 tree labels
= NULL_TREE
;
21997 tree identifier
, label
, name
;
21999 /* Look for the identifier. */
22000 identifier
= cp_parser_identifier (parser
);
22001 if (!error_operand_p (identifier
))
22003 label
= lookup_label (identifier
);
22004 if (TREE_CODE (label
) == LABEL_DECL
)
22006 TREE_USED (label
) = 1;
22007 check_goto (label
);
22008 name
= build_string (IDENTIFIER_LENGTH (identifier
),
22009 IDENTIFIER_POINTER (identifier
));
22010 labels
= tree_cons (name
, label
, labels
);
22013 /* If the next token is not a `,', then the list is
22015 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
22017 /* Consume the `,' token. */
22018 cp_lexer_consume_token (parser
->lexer
);
22021 return nreverse (labels
);
22024 /* Return TRUE iff the next tokens in the stream are possibly the
22025 beginning of a GNU extension attribute. */
22028 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
22030 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
22033 /* Return TRUE iff the next tokens in the stream are possibly the
22034 beginning of a standard C++-11 attribute specifier. */
22037 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
22039 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
22042 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22043 beginning of a standard C++-11 attribute specifier. */
22046 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
22048 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22050 return (cxx_dialect
>= cxx11
22051 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
22052 || (token
->type
== CPP_OPEN_SQUARE
22053 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
22054 && token
->type
== CPP_OPEN_SQUARE
)));
22057 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22058 beginning of a GNU extension attribute. */
22061 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
22063 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22065 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
22068 /* Return true iff the next tokens can be the beginning of either a
22069 GNU attribute list, or a standard C++11 attribute sequence. */
22072 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
22074 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
22075 || cp_next_tokens_can_be_std_attribute_p (parser
));
22078 /* Return true iff the next Nth tokens can be the beginning of either
22079 a GNU attribute list, or a standard C++11 attribute sequence. */
22082 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
22084 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
22085 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
22088 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22089 of GNU attributes, or return NULL. */
22092 cp_parser_attributes_opt (cp_parser
*parser
)
22094 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
22095 return cp_parser_gnu_attributes_opt (parser
);
22096 return cp_parser_std_attribute_spec_seq (parser
);
22099 #define CILK_SIMD_FN_CLAUSE_MASK \
22100 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22101 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22102 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22103 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22104 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22106 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22107 vector [(<clauses>)] */
22110 cp_parser_cilk_simd_fn_vector_attrs (cp_parser
*parser
, cp_token
*v_token
)
22112 bool first_p
= parser
->cilk_simd_fn_info
== NULL
;
22113 cp_token
*token
= v_token
;
22116 parser
->cilk_simd_fn_info
= XNEW (cp_omp_declare_simd_data
);
22117 parser
->cilk_simd_fn_info
->error_seen
= false;
22118 parser
->cilk_simd_fn_info
->fndecl_seen
= false;
22119 parser
->cilk_simd_fn_info
->tokens
= vNULL
;
22121 int paren_scope
= 0;
22122 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22124 cp_lexer_consume_token (parser
->lexer
);
22125 v_token
= cp_lexer_peek_token (parser
->lexer
);
22128 while (paren_scope
> 0)
22130 token
= cp_lexer_peek_token (parser
->lexer
);
22131 if (token
->type
== CPP_OPEN_PAREN
)
22133 else if (token
->type
== CPP_CLOSE_PAREN
)
22135 /* Do not push the last ')' */
22136 if (!(token
->type
== CPP_CLOSE_PAREN
&& paren_scope
== 0))
22137 cp_lexer_consume_token (parser
->lexer
);
22140 token
->type
= CPP_PRAGMA_EOL
;
22141 parser
->lexer
->next_token
= token
;
22142 cp_lexer_consume_token (parser
->lexer
);
22144 struct cp_token_cache
*cp
22145 = cp_token_cache_new (v_token
, cp_lexer_peek_token (parser
->lexer
));
22146 parser
->cilk_simd_fn_info
->tokens
.safe_push (cp
);
22149 /* Parse an (optional) series of attributes.
22152 attributes attribute
22155 __attribute__ (( attribute-list [opt] ))
22157 The return value is as for cp_parser_gnu_attribute_list. */
22160 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
22162 tree attributes
= NULL_TREE
;
22167 tree attribute_list
;
22170 /* Peek at the next token. */
22171 token
= cp_lexer_peek_token (parser
->lexer
);
22172 /* If it's not `__attribute__', then we're done. */
22173 if (token
->keyword
!= RID_ATTRIBUTE
)
22176 /* Consume the `__attribute__' keyword. */
22177 cp_lexer_consume_token (parser
->lexer
);
22178 /* Look for the two `(' tokens. */
22179 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22180 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
22182 /* Peek at the next token. */
22183 token
= cp_lexer_peek_token (parser
->lexer
);
22184 if (token
->type
!= CPP_CLOSE_PAREN
)
22185 /* Parse the attribute-list. */
22186 attribute_list
= cp_parser_gnu_attribute_list (parser
);
22188 /* If the next token is a `)', then there is no attribute
22190 attribute_list
= NULL
;
22192 /* Look for the two `)' tokens. */
22193 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22195 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
22198 cp_parser_skip_to_end_of_statement (parser
);
22200 /* Add these new attributes to the list. */
22201 attributes
= chainon (attributes
, attribute_list
);
22207 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22208 "__vector" or "__vector__." */
22211 is_cilkplus_vector_p (tree name
)
22213 if (flag_cilkplus
&& is_attribute_p ("vector", name
))
22218 /* Parse a GNU attribute-list.
22222 attribute-list , attribute
22226 identifier ( identifier )
22227 identifier ( identifier , expression-list )
22228 identifier ( expression-list )
22230 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22231 to an attribute. The TREE_PURPOSE of each node is the identifier
22232 indicating which attribute is in use. The TREE_VALUE represents
22233 the arguments, if any. */
22236 cp_parser_gnu_attribute_list (cp_parser
* parser
)
22238 tree attribute_list
= NULL_TREE
;
22239 bool save_translate_strings_p
= parser
->translate_strings_p
;
22241 parser
->translate_strings_p
= false;
22248 /* Look for the identifier. We also allow keywords here; for
22249 example `__attribute__ ((const))' is legal. */
22250 token
= cp_lexer_peek_token (parser
->lexer
);
22251 if (token
->type
== CPP_NAME
22252 || token
->type
== CPP_KEYWORD
)
22254 tree arguments
= NULL_TREE
;
22256 /* Consume the token, but save it since we need it for the
22257 SIMD enabled function parsing. */
22258 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
22260 /* Save away the identifier that indicates which attribute
22262 identifier
= (token
->type
== CPP_KEYWORD
)
22263 /* For keywords, use the canonical spelling, not the
22264 parsed identifier. */
22265 ? ridpointers
[(int) token
->keyword
]
22266 : id_token
->u
.value
;
22268 attribute
= build_tree_list (identifier
, NULL_TREE
);
22270 /* Peek at the next token. */
22271 token
= cp_lexer_peek_token (parser
->lexer
);
22272 /* If it's an `(', then parse the attribute arguments. */
22273 if (token
->type
== CPP_OPEN_PAREN
)
22275 vec
<tree
, va_gc
> *vec
;
22276 int attr_flag
= (attribute_takes_identifier_p (identifier
)
22277 ? id_attr
: normal_attr
);
22278 if (is_cilkplus_vector_p (identifier
))
22280 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22284 vec
= cp_parser_parenthesized_expression_list
22285 (parser
, attr_flag
, /*cast_p=*/false,
22286 /*allow_expansion_p=*/false,
22287 /*non_constant_p=*/NULL
);
22289 arguments
= error_mark_node
;
22292 arguments
= build_tree_list_vec (vec
);
22293 release_tree_vector (vec
);
22295 /* Save the arguments away. */
22296 TREE_VALUE (attribute
) = arguments
;
22298 else if (is_cilkplus_vector_p (identifier
))
22300 cp_parser_cilk_simd_fn_vector_attrs (parser
, id_token
);
22304 if (arguments
!= error_mark_node
)
22306 /* Add this attribute to the list. */
22307 TREE_CHAIN (attribute
) = attribute_list
;
22308 attribute_list
= attribute
;
22311 token
= cp_lexer_peek_token (parser
->lexer
);
22313 /* Now, look for more attributes. If the next token isn't a
22314 `,', we're done. */
22315 if (token
->type
!= CPP_COMMA
)
22318 /* Consume the comma and keep going. */
22319 cp_lexer_consume_token (parser
->lexer
);
22321 parser
->translate_strings_p
= save_translate_strings_p
;
22323 /* We built up the list in reverse order. */
22324 return nreverse (attribute_list
);
22327 /* Parse a standard C++11 attribute.
22329 The returned representation is a TREE_LIST which TREE_PURPOSE is
22330 the scoped name of the attribute, and the TREE_VALUE is its
22333 Note that the scoped name of the attribute is itself a TREE_LIST
22334 which TREE_PURPOSE is the namespace of the attribute, and
22335 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22336 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22337 and which TREE_PURPOSE is directly the attribute name.
22339 Clients of the attribute code should use get_attribute_namespace
22340 and get_attribute_name to get the actual namespace and name of
22341 attributes, regardless of their being GNU or C++11 attributes.
22344 attribute-token attribute-argument-clause [opt]
22348 attribute-scoped-token
22350 attribute-scoped-token:
22351 attribute-namespace :: identifier
22353 attribute-namespace:
22356 attribute-argument-clause:
22357 ( balanced-token-seq )
22359 balanced-token-seq:
22360 balanced-token [opt]
22361 balanced-token-seq balanced-token
22364 ( balanced-token-seq )
22365 [ balanced-token-seq ]
22366 { balanced-token-seq }. */
22369 cp_parser_std_attribute (cp_parser
*parser
)
22371 tree attribute
, attr_ns
= NULL_TREE
, attr_id
= NULL_TREE
, arguments
;
22374 /* First, parse name of the the attribute, a.k.a
22375 attribute-token. */
22377 token
= cp_lexer_peek_token (parser
->lexer
);
22378 if (token
->type
== CPP_NAME
)
22379 attr_id
= token
->u
.value
;
22380 else if (token
->type
== CPP_KEYWORD
)
22381 attr_id
= ridpointers
[(int) token
->keyword
];
22382 else if (token
->flags
& NAMED_OP
)
22383 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
22385 if (attr_id
== NULL_TREE
)
22388 cp_lexer_consume_token (parser
->lexer
);
22390 token
= cp_lexer_peek_token (parser
->lexer
);
22391 if (token
->type
== CPP_SCOPE
)
22393 /* We are seeing a scoped attribute token. */
22395 cp_lexer_consume_token (parser
->lexer
);
22398 token
= cp_lexer_consume_token (parser
->lexer
);
22399 if (token
->type
== CPP_NAME
)
22400 attr_id
= token
->u
.value
;
22401 else if (token
->type
== CPP_KEYWORD
)
22402 attr_id
= ridpointers
[(int) token
->keyword
];
22405 error_at (token
->location
,
22406 "expected an identifier for the attribute name");
22407 return error_mark_node
;
22409 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
22411 token
= cp_lexer_peek_token (parser
->lexer
);
22415 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
22417 /* C++11 noreturn attribute is equivalent to GNU's. */
22418 if (is_attribute_p ("noreturn", attr_id
))
22419 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22420 /* C++14 deprecated attribute is equivalent to GNU's. */
22421 else if (cxx_dialect
>= cxx11
&& is_attribute_p ("deprecated", attr_id
))
22423 if (cxx_dialect
== cxx11
)
22424 pedwarn (token
->location
, OPT_Wpedantic
,
22425 "%<deprecated%> is a C++14 feature;"
22426 " use %<gnu::deprecated%>");
22427 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = get_identifier ("gnu");
22431 /* Now parse the optional argument clause of the attribute. */
22433 if (token
->type
!= CPP_OPEN_PAREN
)
22437 vec
<tree
, va_gc
> *vec
;
22438 int attr_flag
= normal_attr
;
22440 if (attr_ns
== get_identifier ("gnu")
22441 && attribute_takes_identifier_p (attr_id
))
22442 /* A GNU attribute that takes an identifier in parameter. */
22443 attr_flag
= id_attr
;
22445 vec
= cp_parser_parenthesized_expression_list
22446 (parser
, attr_flag
, /*cast_p=*/false,
22447 /*allow_expansion_p=*/true,
22448 /*non_constant_p=*/NULL
);
22450 arguments
= error_mark_node
;
22453 arguments
= build_tree_list_vec (vec
);
22454 release_tree_vector (vec
);
22457 if (arguments
== error_mark_node
)
22458 attribute
= error_mark_node
;
22460 TREE_VALUE (attribute
) = arguments
;
22466 /* Parse a list of standard C++-11 attributes.
22470 attribute-list , attribute[opt]
22472 attribute-list , attribute ...
22476 cp_parser_std_attribute_list (cp_parser
*parser
)
22478 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
22479 cp_token
*token
= NULL
;
22483 attribute
= cp_parser_std_attribute (parser
);
22484 if (attribute
== error_mark_node
)
22486 if (attribute
!= NULL_TREE
)
22488 TREE_CHAIN (attribute
) = attributes
;
22489 attributes
= attribute
;
22491 token
= cp_lexer_peek_token (parser
->lexer
);
22492 if (token
->type
!= CPP_COMMA
)
22494 cp_lexer_consume_token (parser
->lexer
);
22496 attributes
= nreverse (attributes
);
22500 /* Parse a standard C++-11 attribute specifier.
22502 attribute-specifier:
22503 [ [ attribute-list ] ]
22504 alignment-specifier
22506 alignment-specifier:
22507 alignas ( type-id ... [opt] )
22508 alignas ( alignment-expression ... [opt] ). */
22511 cp_parser_std_attribute_spec (cp_parser
*parser
)
22513 tree attributes
= NULL_TREE
;
22514 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22516 if (token
->type
== CPP_OPEN_SQUARE
22517 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
22519 cp_lexer_consume_token (parser
->lexer
);
22520 cp_lexer_consume_token (parser
->lexer
);
22522 attributes
= cp_parser_std_attribute_list (parser
);
22524 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
22525 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22526 cp_parser_skip_to_end_of_statement (parser
);
22528 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22529 when we are sure that we have actually parsed them. */
22530 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22536 /* Look for an alignment-specifier. */
22538 token
= cp_lexer_peek_token (parser
->lexer
);
22540 if (token
->type
!= CPP_KEYWORD
22541 || token
->keyword
!= RID_ALIGNAS
)
22544 cp_lexer_consume_token (parser
->lexer
);
22545 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
22547 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
) == NULL
)
22549 cp_parser_error (parser
, "expected %<(%>");
22550 return error_mark_node
;
22553 cp_parser_parse_tentatively (parser
);
22554 alignas_expr
= cp_parser_type_id (parser
);
22556 if (!cp_parser_parse_definitely (parser
))
22558 gcc_assert (alignas_expr
== error_mark_node
22559 || alignas_expr
== NULL_TREE
);
22562 cp_parser_assignment_expression (parser
);
22563 if (alignas_expr
== error_mark_node
)
22564 cp_parser_skip_to_end_of_statement (parser
);
22565 if (alignas_expr
== NULL_TREE
22566 || alignas_expr
== error_mark_node
)
22567 return alignas_expr
;
22570 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) == NULL
)
22572 cp_parser_error (parser
, "expected %<)%>");
22573 return error_mark_node
;
22576 alignas_expr
= cxx_alignas_expr (alignas_expr
);
22578 /* Build the C++-11 representation of an 'aligned'
22581 build_tree_list (build_tree_list (get_identifier ("gnu"),
22582 get_identifier ("aligned")),
22583 build_tree_list (NULL_TREE
, alignas_expr
));
22589 /* Parse a standard C++-11 attribute-specifier-seq.
22591 attribute-specifier-seq:
22592 attribute-specifier-seq [opt] attribute-specifier
22596 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
22598 tree attr_specs
= NULL
;
22602 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
22603 if (attr_spec
== NULL_TREE
)
22605 if (attr_spec
== error_mark_node
)
22606 return error_mark_node
;
22608 TREE_CHAIN (attr_spec
) = attr_specs
;
22609 attr_specs
= attr_spec
;
22612 attr_specs
= nreverse (attr_specs
);
22616 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22617 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22618 current value of the PEDANTIC flag, regardless of whether or not
22619 the `__extension__' keyword is present. The caller is responsible
22620 for restoring the value of the PEDANTIC flag. */
22623 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
22625 /* Save the old value of the PEDANTIC flag. */
22626 *saved_pedantic
= pedantic
;
22628 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
22630 /* Consume the `__extension__' token. */
22631 cp_lexer_consume_token (parser
->lexer
);
22632 /* We're not being pedantic while the `__extension__' keyword is
22642 /* Parse a label declaration.
22645 __label__ label-declarator-seq ;
22647 label-declarator-seq:
22648 identifier , label-declarator-seq
22652 cp_parser_label_declaration (cp_parser
* parser
)
22654 /* Look for the `__label__' keyword. */
22655 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
22661 /* Look for an identifier. */
22662 identifier
= cp_parser_identifier (parser
);
22663 /* If we failed, stop. */
22664 if (identifier
== error_mark_node
)
22666 /* Declare it as a label. */
22667 finish_label_decl (identifier
);
22668 /* If the next token is a `;', stop. */
22669 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
22671 /* Look for the `,' separating the label declarations. */
22672 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
22675 /* Look for the final `;'. */
22676 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
22679 /* Support Functions */
22681 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22682 NAME should have one of the representations used for an
22683 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22684 is returned. If PARSER->SCOPE is a dependent type, then a
22685 SCOPE_REF is returned.
22687 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22688 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22689 was formed. Abstractly, such entities should not be passed to this
22690 function, because they do not need to be looked up, but it is
22691 simpler to check for this special case here, rather than at the
22694 In cases not explicitly covered above, this function returns a
22695 DECL, OVERLOAD, or baselink representing the result of the lookup.
22696 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22699 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22700 (e.g., "struct") that was used. In that case bindings that do not
22701 refer to types are ignored.
22703 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22706 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22709 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22712 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22713 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22714 NULL_TREE otherwise. */
22717 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
22718 enum tag_types tag_type
,
22721 bool check_dependency
,
22722 tree
*ambiguous_decls
,
22723 location_t name_location
)
22726 tree object_type
= parser
->context
->object_type
;
22728 /* Assume that the lookup will be unambiguous. */
22729 if (ambiguous_decls
)
22730 *ambiguous_decls
= NULL_TREE
;
22732 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22733 no longer valid. Note that if we are parsing tentatively, and
22734 the parse fails, OBJECT_TYPE will be automatically restored. */
22735 parser
->context
->object_type
= NULL_TREE
;
22737 if (name
== error_mark_node
)
22738 return error_mark_node
;
22740 /* A template-id has already been resolved; there is no lookup to
22742 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
22744 if (BASELINK_P (name
))
22746 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
22747 == TEMPLATE_ID_EXPR
);
22751 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22752 it should already have been checked to make sure that the name
22753 used matches the type being destroyed. */
22754 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
22758 /* Figure out to which type this destructor applies. */
22760 type
= parser
->scope
;
22761 else if (object_type
)
22762 type
= object_type
;
22764 type
= current_class_type
;
22765 /* If that's not a class type, there is no destructor. */
22766 if (!type
|| !CLASS_TYPE_P (type
))
22767 return error_mark_node
;
22768 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
22769 lazily_declare_fn (sfk_destructor
, type
);
22770 if (!CLASSTYPE_DESTRUCTORS (type
))
22771 return error_mark_node
;
22772 /* If it was a class type, return the destructor. */
22773 return CLASSTYPE_DESTRUCTORS (type
);
22776 /* By this point, the NAME should be an ordinary identifier. If
22777 the id-expression was a qualified name, the qualifying scope is
22778 stored in PARSER->SCOPE at this point. */
22779 gcc_assert (identifier_p (name
));
22781 /* Perform the lookup. */
22786 if (parser
->scope
== error_mark_node
)
22787 return error_mark_node
;
22789 /* If the SCOPE is dependent, the lookup must be deferred until
22790 the template is instantiated -- unless we are explicitly
22791 looking up names in uninstantiated templates. Even then, we
22792 cannot look up the name if the scope is not a class type; it
22793 might, for example, be a template type parameter. */
22794 dependent_p
= (TYPE_P (parser
->scope
)
22795 && dependent_scope_p (parser
->scope
));
22796 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
22798 /* Defer lookup. */
22799 decl
= error_mark_node
;
22802 tree pushed_scope
= NULL_TREE
;
22804 /* If PARSER->SCOPE is a dependent type, then it must be a
22805 class type, and we must not be checking dependencies;
22806 otherwise, we would have processed this lookup above. So
22807 that PARSER->SCOPE is not considered a dependent base by
22808 lookup_member, we must enter the scope here. */
22810 pushed_scope
= push_scope (parser
->scope
);
22812 /* If the PARSER->SCOPE is a template specialization, it
22813 may be instantiated during name lookup. In that case,
22814 errors may be issued. Even if we rollback the current
22815 tentative parse, those errors are valid. */
22816 decl
= lookup_qualified_name (parser
->scope
, name
,
22817 tag_type
!= none_type
,
22818 /*complain=*/true);
22820 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22821 lookup result and the nested-name-specifier nominates a class C:
22822 * if the name specified after the nested-name-specifier, when
22823 looked up in C, is the injected-class-name of C (Clause 9), or
22824 * if the name specified after the nested-name-specifier is the
22825 same as the identifier or the simple-template-id's template-
22826 name in the last component of the nested-name-specifier,
22827 the name is instead considered to name the constructor of
22828 class C. [ Note: for example, the constructor is not an
22829 acceptable lookup result in an elaborated-type-specifier so
22830 the constructor would not be used in place of the
22831 injected-class-name. --end note ] Such a constructor name
22832 shall be used only in the declarator-id of a declaration that
22833 names a constructor or in a using-declaration. */
22834 if (tag_type
== none_type
22835 && DECL_SELF_REFERENCE_P (decl
)
22836 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
22837 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
22838 tag_type
!= none_type
,
22839 /*complain=*/true);
22841 /* If we have a single function from a using decl, pull it out. */
22842 if (TREE_CODE (decl
) == OVERLOAD
22843 && !really_overloaded_fn (decl
))
22844 decl
= OVL_FUNCTION (decl
);
22847 pop_scope (pushed_scope
);
22850 /* If the scope is a dependent type and either we deferred lookup or
22851 we did lookup but didn't find the name, rememeber the name. */
22852 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
22853 && dependent_type_p (parser
->scope
))
22859 /* The resolution to Core Issue 180 says that `struct
22860 A::B' should be considered a type-name, even if `A'
22862 type
= make_typename_type (parser
->scope
, name
, tag_type
,
22863 /*complain=*/tf_error
);
22864 if (type
!= error_mark_node
)
22865 decl
= TYPE_NAME (type
);
22867 else if (is_template
22868 && (cp_parser_next_token_ends_template_argument_p (parser
)
22869 || cp_lexer_next_token_is (parser
->lexer
,
22871 decl
= make_unbound_class_template (parser
->scope
,
22873 /*complain=*/tf_error
);
22875 decl
= build_qualified_name (/*type=*/NULL_TREE
,
22876 parser
->scope
, name
,
22879 parser
->qualifying_scope
= parser
->scope
;
22880 parser
->object_scope
= NULL_TREE
;
22882 else if (object_type
)
22884 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22885 OBJECT_TYPE is not a class. */
22886 if (CLASS_TYPE_P (object_type
))
22887 /* If the OBJECT_TYPE is a template specialization, it may
22888 be instantiated during name lookup. In that case, errors
22889 may be issued. Even if we rollback the current tentative
22890 parse, those errors are valid. */
22891 decl
= lookup_member (object_type
,
22894 tag_type
!= none_type
,
22895 tf_warning_or_error
);
22900 /* Look it up in the enclosing context. */
22901 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22903 /*block_p=*/true, is_namespace
, 0);
22904 parser
->object_scope
= object_type
;
22905 parser
->qualifying_scope
= NULL_TREE
;
22909 decl
= lookup_name_real (name
, tag_type
!= none_type
,
22911 /*block_p=*/true, is_namespace
, 0);
22912 parser
->qualifying_scope
= NULL_TREE
;
22913 parser
->object_scope
= NULL_TREE
;
22916 /* If the lookup failed, let our caller know. */
22917 if (!decl
|| decl
== error_mark_node
)
22918 return error_mark_node
;
22920 /* Pull out the template from an injected-class-name (or multiple). */
22922 decl
= maybe_get_template_decl_from_type_decl (decl
);
22924 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22925 if (TREE_CODE (decl
) == TREE_LIST
)
22927 if (ambiguous_decls
)
22928 *ambiguous_decls
= decl
;
22929 /* The error message we have to print is too complicated for
22930 cp_parser_error, so we incorporate its actions directly. */
22931 if (!cp_parser_simulate_error (parser
))
22933 error_at (name_location
, "reference to %qD is ambiguous",
22935 print_candidates (decl
);
22937 return error_mark_node
;
22940 gcc_assert (DECL_P (decl
)
22941 || TREE_CODE (decl
) == OVERLOAD
22942 || TREE_CODE (decl
) == SCOPE_REF
22943 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
22944 || BASELINK_P (decl
));
22946 /* If we have resolved the name of a member declaration, check to
22947 see if the declaration is accessible. When the name resolves to
22948 set of overloaded functions, accessibility is checked when
22949 overload resolution is done.
22951 During an explicit instantiation, access is not checked at all,
22952 as per [temp.explicit]. */
22954 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
22956 maybe_record_typedef_use (decl
);
22961 /* Like cp_parser_lookup_name, but for use in the typical case where
22962 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22963 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22966 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
22968 return cp_parser_lookup_name (parser
, name
,
22970 /*is_template=*/false,
22971 /*is_namespace=*/false,
22972 /*check_dependency=*/true,
22973 /*ambiguous_decls=*/NULL
,
22977 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22978 the current context, return the TYPE_DECL. If TAG_NAME_P is
22979 true, the DECL indicates the class being defined in a class-head,
22980 or declared in an elaborated-type-specifier.
22982 Otherwise, return DECL. */
22985 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
22987 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22988 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22991 template <typename T> struct B;
22994 template <typename T> struct A::B {};
22996 Similarly, in an elaborated-type-specifier:
22998 namespace N { struct X{}; }
23001 template <typename T> friend struct N::X;
23004 However, if the DECL refers to a class type, and we are in
23005 the scope of the class, then the name lookup automatically
23006 finds the TYPE_DECL created by build_self_reference rather
23007 than a TEMPLATE_DECL. For example, in:
23009 template <class T> struct S {
23013 there is no need to handle such case. */
23015 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
23016 return DECL_TEMPLATE_RESULT (decl
);
23021 /* If too many, or too few, template-parameter lists apply to the
23022 declarator, issue an error message. Returns TRUE if all went well,
23023 and FALSE otherwise. */
23026 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
23027 cp_declarator
*declarator
,
23028 location_t declarator_location
)
23030 switch (declarator
->kind
)
23034 unsigned num_templates
= 0;
23035 tree scope
= declarator
->u
.id
.qualifying_scope
;
23038 num_templates
= num_template_headers_for_class (scope
);
23039 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
23040 == TEMPLATE_ID_EXPR
)
23041 /* If the DECLARATOR has the form `X<y>' then it uses one
23042 additional level of template parameters. */
23045 return cp_parser_check_template_parameters
23046 (parser
, num_templates
, declarator_location
, declarator
);
23052 case cdk_reference
:
23054 return (cp_parser_check_declarator_template_parameters
23055 (parser
, declarator
->declarator
, declarator_location
));
23061 gcc_unreachable ();
23066 /* NUM_TEMPLATES were used in the current declaration. If that is
23067 invalid, return FALSE and issue an error messages. Otherwise,
23068 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23069 declarator and we can print more accurate diagnostics. */
23072 cp_parser_check_template_parameters (cp_parser
* parser
,
23073 unsigned num_templates
,
23074 location_t location
,
23075 cp_declarator
*declarator
)
23077 /* If there are the same number of template classes and parameter
23078 lists, that's OK. */
23079 if (parser
->num_template_parameter_lists
== num_templates
)
23081 /* If there are more, but only one more, then we are referring to a
23082 member template. That's OK too. */
23083 if (parser
->num_template_parameter_lists
== num_templates
+ 1)
23085 /* If there are more template classes than parameter lists, we have
23088 template <class T> void S<T>::R<T>::f (); */
23089 if (parser
->num_template_parameter_lists
< num_templates
)
23091 if (declarator
&& !current_function_decl
)
23092 error_at (location
, "specializing member %<%T::%E%> "
23093 "requires %<template<>%> syntax",
23094 declarator
->u
.id
.qualifying_scope
,
23095 declarator
->u
.id
.unqualified_name
);
23096 else if (declarator
)
23097 error_at (location
, "invalid declaration of %<%T::%E%>",
23098 declarator
->u
.id
.qualifying_scope
,
23099 declarator
->u
.id
.unqualified_name
);
23101 error_at (location
, "too few template-parameter-lists");
23104 /* Otherwise, there are too many template parameter lists. We have
23107 template <class T> template <class U> void S::f(); */
23108 error_at (location
, "too many template-parameter-lists");
23112 /* Parse an optional `::' token indicating that the following name is
23113 from the global namespace. If so, PARSER->SCOPE is set to the
23114 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23115 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23116 Returns the new value of PARSER->SCOPE, if the `::' token is
23117 present, and NULL_TREE otherwise. */
23120 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
23124 /* Peek at the next token. */
23125 token
= cp_lexer_peek_token (parser
->lexer
);
23126 /* If we're looking at a `::' token then we're starting from the
23127 global namespace, not our current location. */
23128 if (token
->type
== CPP_SCOPE
)
23130 /* Consume the `::' token. */
23131 cp_lexer_consume_token (parser
->lexer
);
23132 /* Set the SCOPE so that we know where to start the lookup. */
23133 parser
->scope
= global_namespace
;
23134 parser
->qualifying_scope
= global_namespace
;
23135 parser
->object_scope
= NULL_TREE
;
23137 return parser
->scope
;
23139 else if (!current_scope_valid_p
)
23141 parser
->scope
= NULL_TREE
;
23142 parser
->qualifying_scope
= NULL_TREE
;
23143 parser
->object_scope
= NULL_TREE
;
23149 /* Returns TRUE if the upcoming token sequence is the start of a
23150 constructor declarator. If FRIEND_P is true, the declarator is
23151 preceded by the `friend' specifier. */
23154 cp_parser_constructor_declarator_p (cp_parser
*parser
, bool friend_p
)
23156 bool constructor_p
;
23157 bool outside_class_specifier_p
;
23158 tree nested_name_specifier
;
23159 cp_token
*next_token
;
23161 /* The common case is that this is not a constructor declarator, so
23162 try to avoid doing lots of work if at all possible. It's not
23163 valid declare a constructor at function scope. */
23164 if (parser
->in_function_body
)
23166 /* And only certain tokens can begin a constructor declarator. */
23167 next_token
= cp_lexer_peek_token (parser
->lexer
);
23168 if (next_token
->type
!= CPP_NAME
23169 && next_token
->type
!= CPP_SCOPE
23170 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
23171 && next_token
->type
!= CPP_TEMPLATE_ID
)
23174 /* Parse tentatively; we are going to roll back all of the tokens
23176 cp_parser_parse_tentatively (parser
);
23177 /* Assume that we are looking at a constructor declarator. */
23178 constructor_p
= true;
23180 /* Look for the optional `::' operator. */
23181 cp_parser_global_scope_opt (parser
,
23182 /*current_scope_valid_p=*/false);
23183 /* Look for the nested-name-specifier. */
23184 nested_name_specifier
23185 = (cp_parser_nested_name_specifier_opt (parser
,
23186 /*typename_keyword_p=*/false,
23187 /*check_dependency_p=*/false,
23189 /*is_declaration=*/false));
23191 outside_class_specifier_p
= (!at_class_scope_p ()
23192 || !TYPE_BEING_DEFINED (current_class_type
)
23195 /* Outside of a class-specifier, there must be a
23196 nested-name-specifier. */
23197 if (!nested_name_specifier
&& outside_class_specifier_p
)
23198 constructor_p
= false;
23199 else if (nested_name_specifier
== error_mark_node
)
23200 constructor_p
= false;
23202 /* If we have a class scope, this is easy; DR 147 says that S::S always
23203 names the constructor, and no other qualified name could. */
23204 if (constructor_p
&& nested_name_specifier
23205 && CLASS_TYPE_P (nested_name_specifier
))
23207 tree id
= cp_parser_unqualified_id (parser
,
23208 /*template_keyword_p=*/false,
23209 /*check_dependency_p=*/false,
23210 /*declarator_p=*/true,
23211 /*optional_p=*/false);
23212 if (is_overloaded_fn (id
))
23213 id
= DECL_NAME (get_first_fn (id
));
23214 if (!constructor_name_p (id
, nested_name_specifier
))
23215 constructor_p
= false;
23217 /* If we still think that this might be a constructor-declarator,
23218 look for a class-name. */
23219 else if (constructor_p
)
23223 template <typename T> struct S {
23227 we must recognize that the nested `S' names a class. */
23229 type_decl
= cp_parser_class_name (parser
,
23230 /*typename_keyword_p=*/false,
23231 /*template_keyword_p=*/false,
23233 /*check_dependency_p=*/false,
23234 /*class_head_p=*/false,
23235 /*is_declaration=*/false);
23236 /* If there was no class-name, then this is not a constructor.
23237 Otherwise, if we are in a class-specifier and we aren't
23238 handling a friend declaration, check that its type matches
23239 current_class_type (c++/38313). Note: error_mark_node
23240 is left alone for error recovery purposes. */
23241 constructor_p
= (!cp_parser_error_occurred (parser
)
23242 && (outside_class_specifier_p
23243 || type_decl
== error_mark_node
23244 || same_type_p (current_class_type
,
23245 TREE_TYPE (type_decl
))));
23247 /* If we're still considering a constructor, we have to see a `(',
23248 to begin the parameter-declaration-clause, followed by either a
23249 `)', an `...', or a decl-specifier. We need to check for a
23250 type-specifier to avoid being fooled into thinking that:
23254 is a constructor. (It is actually a function named `f' that
23255 takes one parameter (of type `int') and returns a value of type
23258 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
23259 constructor_p
= false;
23262 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
23263 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
23264 /* A parameter declaration begins with a decl-specifier,
23265 which is either the "attribute" keyword, a storage class
23266 specifier, or (usually) a type-specifier. */
23267 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
))
23270 tree pushed_scope
= NULL_TREE
;
23271 unsigned saved_num_template_parameter_lists
;
23273 /* Names appearing in the type-specifier should be looked up
23274 in the scope of the class. */
23275 if (current_class_type
)
23279 type
= TREE_TYPE (type_decl
);
23280 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23282 type
= resolve_typename_type (type
,
23283 /*only_current_p=*/false);
23284 if (TREE_CODE (type
) == TYPENAME_TYPE
)
23286 cp_parser_abort_tentative_parse (parser
);
23290 pushed_scope
= push_scope (type
);
23293 /* Inside the constructor parameter list, surrounding
23294 template-parameter-lists do not apply. */
23295 saved_num_template_parameter_lists
23296 = parser
->num_template_parameter_lists
;
23297 parser
->num_template_parameter_lists
= 0;
23299 /* Look for the type-specifier. */
23300 cp_parser_type_specifier (parser
,
23301 CP_PARSER_FLAGS_NONE
,
23302 /*decl_specs=*/NULL
,
23303 /*is_declarator=*/true,
23304 /*declares_class_or_enum=*/NULL
,
23305 /*is_cv_qualifier=*/NULL
);
23307 parser
->num_template_parameter_lists
23308 = saved_num_template_parameter_lists
;
23310 /* Leave the scope of the class. */
23312 pop_scope (pushed_scope
);
23314 constructor_p
= !cp_parser_error_occurred (parser
);
23318 /* We did not really want to consume any tokens. */
23319 cp_parser_abort_tentative_parse (parser
);
23321 return constructor_p
;
23324 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23325 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23326 they must be performed once we are in the scope of the function.
23328 Returns the function defined. */
23331 cp_parser_function_definition_from_specifiers_and_declarator
23332 (cp_parser
* parser
,
23333 cp_decl_specifier_seq
*decl_specifiers
,
23335 const cp_declarator
*declarator
)
23340 /* Begin the function-definition. */
23341 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
23343 /* The things we're about to see are not directly qualified by any
23344 template headers we've seen thus far. */
23345 reset_specialization ();
23347 /* If there were names looked up in the decl-specifier-seq that we
23348 did not check, check them now. We must wait until we are in the
23349 scope of the function to perform the checks, since the function
23350 might be a friend. */
23351 perform_deferred_access_checks (tf_warning_or_error
);
23355 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
23356 parser
->omp_declare_simd
= NULL
;
23361 /* Skip the entire function. */
23362 cp_parser_skip_to_end_of_block_or_statement (parser
);
23363 fn
= error_mark_node
;
23365 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
23367 /* Seen already, skip it. An error message has already been output. */
23368 cp_parser_skip_to_end_of_block_or_statement (parser
);
23369 fn
= current_function_decl
;
23370 current_function_decl
= NULL_TREE
;
23371 /* If this is a function from a class, pop the nested class. */
23372 if (current_class_name
)
23373 pop_nested_class ();
23378 if (DECL_DECLARED_INLINE_P (current_function_decl
))
23379 tv
= TV_PARSE_INLINE
;
23381 tv
= TV_PARSE_FUNC
;
23383 fn
= cp_parser_function_definition_after_declarator (parser
,
23384 /*inline_p=*/false);
23391 /* Parse the part of a function-definition that follows the
23392 declarator. INLINE_P is TRUE iff this function is an inline
23393 function defined within a class-specifier.
23395 Returns the function defined. */
23398 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
23402 bool ctor_initializer_p
= false;
23403 bool saved_in_unbraced_linkage_specification_p
;
23404 bool saved_in_function_body
;
23405 unsigned saved_num_template_parameter_lists
;
23407 bool fully_implicit_function_template_p
23408 = parser
->fully_implicit_function_template_p
;
23409 parser
->fully_implicit_function_template_p
= false;
23410 tree implicit_template_parms
23411 = parser
->implicit_template_parms
;
23412 parser
->implicit_template_parms
= 0;
23413 cp_binding_level
* implicit_template_scope
23414 = parser
->implicit_template_scope
;
23415 parser
->implicit_template_scope
= 0;
23417 saved_in_function_body
= parser
->in_function_body
;
23418 parser
->in_function_body
= true;
23419 /* If the next token is `return', then the code may be trying to
23420 make use of the "named return value" extension that G++ used to
23422 token
= cp_lexer_peek_token (parser
->lexer
);
23423 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
23425 /* Consume the `return' keyword. */
23426 cp_lexer_consume_token (parser
->lexer
);
23427 /* Look for the identifier that indicates what value is to be
23429 cp_parser_identifier (parser
);
23430 /* Issue an error message. */
23431 error_at (token
->location
,
23432 "named return values are no longer supported");
23433 /* Skip tokens until we reach the start of the function body. */
23436 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23437 if (token
->type
== CPP_OPEN_BRACE
23438 || token
->type
== CPP_EOF
23439 || token
->type
== CPP_PRAGMA_EOL
)
23441 cp_lexer_consume_token (parser
->lexer
);
23444 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23445 anything declared inside `f'. */
23446 saved_in_unbraced_linkage_specification_p
23447 = parser
->in_unbraced_linkage_specification_p
;
23448 parser
->in_unbraced_linkage_specification_p
= false;
23449 /* Inside the function, surrounding template-parameter-lists do not
23451 saved_num_template_parameter_lists
23452 = parser
->num_template_parameter_lists
;
23453 parser
->num_template_parameter_lists
= 0;
23455 start_lambda_scope (current_function_decl
);
23457 /* If the next token is `try', `__transaction_atomic', or
23458 `__transaction_relaxed`, then we are looking at either function-try-block
23459 or function-transaction-block. Note that all of these include the
23461 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
23462 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23463 RID_TRANSACTION_ATOMIC
);
23464 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23465 RID_TRANSACTION_RELAXED
))
23466 ctor_initializer_p
= cp_parser_function_transaction (parser
,
23467 RID_TRANSACTION_RELAXED
);
23468 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23469 ctor_initializer_p
= cp_parser_function_try_block (parser
);
23471 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
23472 (parser
, /*in_function_try_block=*/false);
23474 finish_lambda_scope ();
23476 /* Finish the function. */
23477 fn
= finish_function ((ctor_initializer_p
? 1 : 0) |
23478 (inline_p
? 2 : 0));
23479 /* Generate code for it, if necessary. */
23480 expand_or_defer_fn (fn
);
23481 /* Restore the saved values. */
23482 parser
->in_unbraced_linkage_specification_p
23483 = saved_in_unbraced_linkage_specification_p
;
23484 parser
->num_template_parameter_lists
23485 = saved_num_template_parameter_lists
;
23486 parser
->in_function_body
= saved_in_function_body
;
23488 parser
->fully_implicit_function_template_p
23489 = fully_implicit_function_template_p
;
23490 parser
->implicit_template_parms
23491 = implicit_template_parms
;
23492 parser
->implicit_template_scope
23493 = implicit_template_scope
;
23495 if (parser
->fully_implicit_function_template_p
)
23496 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
23501 /* Parse a template-declaration, assuming that the `export' (and
23502 `extern') keywords, if present, has already been scanned. MEMBER_P
23503 is as for cp_parser_template_declaration. */
23506 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
23508 tree decl
= NULL_TREE
;
23509 vec
<deferred_access_check
, va_gc
> *checks
;
23510 tree parameter_list
;
23511 bool friend_p
= false;
23512 bool need_lang_pop
;
23515 /* Look for the `template' keyword. */
23516 token
= cp_lexer_peek_token (parser
->lexer
);
23517 if (!cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
))
23521 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
23523 if (at_class_scope_p () && current_function_decl
)
23525 /* 14.5.2.2 [temp.mem]
23527 A local class shall not have member templates. */
23528 error_at (token
->location
,
23529 "invalid declaration of member template in local class");
23530 cp_parser_skip_to_end_of_block_or_statement (parser
);
23535 A template ... shall not have C linkage. */
23536 if (current_lang_name
== lang_name_c
)
23538 error_at (token
->location
, "template with C linkage");
23539 /* Give it C++ linkage to avoid confusing other parts of the
23541 push_lang_context (lang_name_cplusplus
);
23542 need_lang_pop
= true;
23545 need_lang_pop
= false;
23547 /* We cannot perform access checks on the template parameter
23548 declarations until we know what is being declared, just as we
23549 cannot check the decl-specifier list. */
23550 push_deferring_access_checks (dk_deferred
);
23552 /* If the next token is `>', then we have an invalid
23553 specialization. Rather than complain about an invalid template
23554 parameter, issue an error message here. */
23555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
23557 cp_parser_error (parser
, "invalid explicit specialization");
23558 begin_specialization ();
23559 parameter_list
= NULL_TREE
;
23563 /* Parse the template parameters. */
23564 parameter_list
= cp_parser_template_parameter_list (parser
);
23567 /* Get the deferred access checks from the parameter list. These
23568 will be checked once we know what is being declared, as for a
23569 member template the checks must be performed in the scope of the
23570 class containing the member. */
23571 checks
= get_deferred_access_checks ();
23573 /* Look for the `>'. */
23574 cp_parser_skip_to_end_of_template_parameter_list (parser
);
23575 /* We just processed one more parameter list. */
23576 ++parser
->num_template_parameter_lists
;
23577 /* If the next token is `template', there are more template
23579 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
23581 cp_parser_template_declaration_after_export (parser
, member_p
);
23582 else if (cxx_dialect
>= cxx11
23583 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
23584 decl
= cp_parser_alias_declaration (parser
);
23587 /* There are no access checks when parsing a template, as we do not
23588 know if a specialization will be a friend. */
23589 push_deferring_access_checks (dk_no_check
);
23590 token
= cp_lexer_peek_token (parser
->lexer
);
23591 decl
= cp_parser_single_declaration (parser
,
23594 /*explicit_specialization_p=*/false,
23596 pop_deferring_access_checks ();
23598 /* If this is a member template declaration, let the front
23600 if (member_p
&& !friend_p
&& decl
)
23602 if (TREE_CODE (decl
) == TYPE_DECL
)
23603 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
23605 decl
= finish_member_template_decl (decl
);
23607 else if (friend_p
&& decl
23608 && DECL_DECLARES_TYPE_P (decl
))
23609 make_friend_class (current_class_type
, TREE_TYPE (decl
),
23610 /*complain=*/true);
23612 /* We are done with the current parameter list. */
23613 --parser
->num_template_parameter_lists
;
23615 pop_deferring_access_checks ();
23618 finish_template_decl (parameter_list
);
23620 /* Check the template arguments for a literal operator template. */
23622 && DECL_DECLARES_FUNCTION_P (decl
)
23623 && UDLIT_OPER_P (DECL_NAME (decl
)))
23626 if (parameter_list
== NULL_TREE
)
23630 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
23631 if (num_parms
== 1)
23633 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
23634 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23635 if (TREE_TYPE (parm
) != char_type_node
23636 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23639 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
23641 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
23642 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
23643 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
23644 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
23645 if (TREE_TYPE (parm
) != TREE_TYPE (type
)
23646 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
23654 if (cxx_dialect
>= cxx14
)
23655 error ("literal operator template %qD has invalid parameter list."
23656 " Expected non-type template argument pack <char...>"
23657 " or <typename CharT, CharT...>",
23660 error ("literal operator template %qD has invalid parameter list."
23661 " Expected non-type template argument pack <char...>",
23665 /* Register member declarations. */
23666 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
23667 finish_member_declaration (decl
);
23668 /* For the erroneous case of a template with C linkage, we pushed an
23669 implicit C++ linkage scope; exit that scope now. */
23671 pop_lang_context ();
23672 /* If DECL is a function template, we must return to parse it later.
23673 (Even though there is no definition, there might be default
23674 arguments that need handling.) */
23675 if (member_p
&& decl
23676 && DECL_DECLARES_FUNCTION_P (decl
))
23677 vec_safe_push (unparsed_funs_with_definitions
, decl
);
23680 /* Perform the deferred access checks from a template-parameter-list.
23681 CHECKS is a TREE_LIST of access checks, as returned by
23682 get_deferred_access_checks. */
23685 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
23687 ++processing_template_parmlist
;
23688 perform_access_checks (checks
, tf_warning_or_error
);
23689 --processing_template_parmlist
;
23692 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23693 `function-definition' sequence that follows a template header.
23694 If MEMBER_P is true, this declaration appears in a class scope.
23696 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23697 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23700 cp_parser_single_declaration (cp_parser
* parser
,
23701 vec
<deferred_access_check
, va_gc
> *checks
,
23703 bool explicit_specialization_p
,
23706 int declares_class_or_enum
;
23707 tree decl
= NULL_TREE
;
23708 cp_decl_specifier_seq decl_specifiers
;
23709 bool function_definition_p
= false;
23710 cp_token
*decl_spec_token_start
;
23712 /* This function is only used when processing a template
23714 gcc_assert (innermost_scope_kind () == sk_template_parms
23715 || innermost_scope_kind () == sk_template_spec
);
23717 /* Defer access checks until we know what is being declared. */
23718 push_deferring_access_checks (dk_deferred
);
23720 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23722 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23723 cp_parser_decl_specifier_seq (parser
,
23724 CP_PARSER_FLAGS_OPTIONAL
,
23726 &declares_class_or_enum
);
23728 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
23730 /* There are no template typedefs. */
23731 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
23733 error_at (decl_spec_token_start
->location
,
23734 "template declaration of %<typedef%>");
23735 decl
= error_mark_node
;
23738 /* Gather up the access checks that occurred the
23739 decl-specifier-seq. */
23740 stop_deferring_access_checks ();
23742 /* Check for the declaration of a template class. */
23743 if (declares_class_or_enum
)
23745 if (cp_parser_declares_only_class_p (parser
))
23747 decl
= shadow_tag (&decl_specifiers
);
23752 friend template <typename T> struct A<T>::B;
23755 A<T>::B will be represented by a TYPENAME_TYPE, and
23756 therefore not recognized by shadow_tag. */
23757 if (friend_p
&& *friend_p
23759 && decl_specifiers
.type
23760 && TYPE_P (decl_specifiers
.type
))
23761 decl
= decl_specifiers
.type
;
23763 if (decl
&& decl
!= error_mark_node
)
23764 decl
= TYPE_NAME (decl
);
23766 decl
= error_mark_node
;
23768 /* Perform access checks for template parameters. */
23769 cp_parser_perform_template_parameter_access_checks (checks
);
23773 /* Complain about missing 'typename' or other invalid type names. */
23774 if (!decl_specifiers
.any_type_specifiers_p
23775 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23777 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23778 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23779 the rest of this declaration. */
23780 decl
= error_mark_node
;
23784 /* If it's not a template class, try for a template function. If
23785 the next token is a `;', then this declaration does not declare
23786 anything. But, if there were errors in the decl-specifiers, then
23787 the error might well have come from an attempted class-specifier.
23788 In that case, there's no need to warn about a missing declarator. */
23790 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
23791 || decl_specifiers
.type
!= error_mark_node
))
23793 decl
= cp_parser_init_declarator (parser
,
23796 /*function_definition_allowed_p=*/true,
23798 declares_class_or_enum
,
23799 &function_definition_p
,
23802 /* 7.1.1-1 [dcl.stc]
23804 A storage-class-specifier shall not be specified in an explicit
23805 specialization... */
23807 && explicit_specialization_p
23808 && decl_specifiers
.storage_class
!= sc_none
)
23810 error_at (decl_spec_token_start
->location
,
23811 "explicit template specialization cannot have a storage class");
23812 decl
= error_mark_node
;
23815 if (decl
&& VAR_P (decl
))
23816 check_template_variable (decl
);
23819 /* Look for a trailing `;' after the declaration. */
23820 if (!function_definition_p
23821 && (decl
== error_mark_node
23822 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
23823 cp_parser_skip_to_end_of_block_or_statement (parser
);
23826 pop_deferring_access_checks ();
23828 /* Clear any current qualification; whatever comes next is the start
23829 of something new. */
23830 parser
->scope
= NULL_TREE
;
23831 parser
->qualifying_scope
= NULL_TREE
;
23832 parser
->object_scope
= NULL_TREE
;
23837 /* Parse a cast-expression that is not the operand of a unary "&". */
23840 cp_parser_simple_cast_expression (cp_parser
*parser
)
23842 return cp_parser_cast_expression (parser
, /*address_p=*/false,
23843 /*cast_p=*/false, /*decltype*/false, NULL
);
23846 /* Parse a functional cast to TYPE. Returns an expression
23847 representing the cast. */
23850 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
23852 vec
<tree
, va_gc
> *vec
;
23853 tree expression_list
;
23858 type
= error_mark_node
;
23860 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23862 cp_lexer_set_source_position (parser
->lexer
);
23863 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23864 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
23865 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
23866 if (TREE_CODE (type
) == TYPE_DECL
)
23867 type
= TREE_TYPE (type
);
23868 return finish_compound_literal (type
, expression_list
,
23869 tf_warning_or_error
);
23873 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23875 /*allow_expansion_p=*/true,
23876 /*non_constant_p=*/NULL
);
23878 expression_list
= error_mark_node
;
23881 expression_list
= build_tree_list_vec (vec
);
23882 release_tree_vector (vec
);
23885 cast
= build_functional_cast (type
, expression_list
,
23886 tf_warning_or_error
);
23887 /* [expr.const]/1: In an integral constant expression "only type
23888 conversions to integral or enumeration type can be used". */
23889 if (TREE_CODE (type
) == TYPE_DECL
)
23890 type
= TREE_TYPE (type
);
23891 if (cast
!= error_mark_node
23892 && !cast_valid_in_integral_constant_expression_p (type
)
23893 && cp_parser_non_integral_constant_expression (parser
,
23895 return error_mark_node
;
23899 /* Save the tokens that make up the body of a member function defined
23900 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23901 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23902 specifiers applied to the declaration. Returns the FUNCTION_DECL
23903 for the member function. */
23906 cp_parser_save_member_function_body (cp_parser
* parser
,
23907 cp_decl_specifier_seq
*decl_specifiers
,
23908 cp_declarator
*declarator
,
23915 /* Create the FUNCTION_DECL. */
23916 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
23917 cp_finalize_omp_declare_simd (parser
, fn
);
23918 /* If something went badly wrong, bail out now. */
23919 if (fn
== error_mark_node
)
23921 /* If there's a function-body, skip it. */
23922 if (cp_parser_token_starts_function_definition_p
23923 (cp_lexer_peek_token (parser
->lexer
)))
23924 cp_parser_skip_to_end_of_block_or_statement (parser
);
23925 return error_mark_node
;
23928 /* Remember it, if there default args to post process. */
23929 cp_parser_save_default_args (parser
, fn
);
23931 /* Save away the tokens that make up the body of the
23933 first
= parser
->lexer
->next_token
;
23934 /* Handle function try blocks. */
23935 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
23936 cp_lexer_consume_token (parser
->lexer
);
23937 /* We can have braced-init-list mem-initializers before the fn body. */
23938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
23940 cp_lexer_consume_token (parser
->lexer
);
23941 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23943 /* cache_group will stop after an un-nested { } pair, too. */
23944 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
23947 /* variadic mem-inits have ... after the ')'. */
23948 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23949 cp_lexer_consume_token (parser
->lexer
);
23952 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23953 /* Handle function try blocks. */
23954 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
23955 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
23956 last
= parser
->lexer
->next_token
;
23958 /* Save away the inline definition; we will process it when the
23959 class is complete. */
23960 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
23961 DECL_PENDING_INLINE_P (fn
) = 1;
23963 /* We need to know that this was defined in the class, so that
23964 friend templates are handled correctly. */
23965 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
23967 /* Add FN to the queue of functions to be parsed later. */
23968 vec_safe_push (unparsed_funs_with_definitions
, fn
);
23973 /* Save the tokens that make up the in-class initializer for a non-static
23974 data member. Returns a DEFAULT_ARG. */
23977 cp_parser_save_nsdmi (cp_parser
* parser
)
23979 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
23982 /* Parse a template-argument-list, as well as the trailing ">" (but
23983 not the opening "<"). See cp_parser_template_argument_list for the
23987 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
23991 tree saved_qualifying_scope
;
23992 tree saved_object_scope
;
23993 bool saved_greater_than_is_operator_p
;
23994 int saved_unevaluated_operand
;
23995 int saved_inhibit_evaluation_warnings
;
23999 When parsing a template-id, the first non-nested `>' is taken as
24000 the end of the template-argument-list rather than a greater-than
24002 saved_greater_than_is_operator_p
24003 = parser
->greater_than_is_operator_p
;
24004 parser
->greater_than_is_operator_p
= false;
24005 /* Parsing the argument list may modify SCOPE, so we save it
24007 saved_scope
= parser
->scope
;
24008 saved_qualifying_scope
= parser
->qualifying_scope
;
24009 saved_object_scope
= parser
->object_scope
;
24010 /* We need to evaluate the template arguments, even though this
24011 template-id may be nested within a "sizeof". */
24012 saved_unevaluated_operand
= cp_unevaluated_operand
;
24013 cp_unevaluated_operand
= 0;
24014 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
24015 c_inhibit_evaluation_warnings
= 0;
24016 /* Parse the template-argument-list itself. */
24017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
24018 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24019 arguments
= NULL_TREE
;
24021 arguments
= cp_parser_template_argument_list (parser
);
24022 /* Look for the `>' that ends the template-argument-list. If we find
24023 a '>>' instead, it's probably just a typo. */
24024 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
24026 if (cxx_dialect
!= cxx98
)
24028 /* In C++0x, a `>>' in a template argument list or cast
24029 expression is considered to be two separate `>'
24030 tokens. So, change the current token to a `>', but don't
24031 consume it: it will be consumed later when the outer
24032 template argument list (or cast expression) is parsed.
24033 Note that this replacement of `>' for `>>' is necessary
24034 even if we are parsing tentatively: in the tentative
24035 case, after calling
24036 cp_parser_enclosed_template_argument_list we will always
24037 throw away all of the template arguments and the first
24038 closing `>', either because the template argument list
24039 was erroneous or because we are replacing those tokens
24040 with a CPP_TEMPLATE_ID token. The second `>' (which will
24041 not have been thrown away) is needed either to close an
24042 outer template argument list or to complete a new-style
24044 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24045 token
->type
= CPP_GREATER
;
24047 else if (!saved_greater_than_is_operator_p
)
24049 /* If we're in a nested template argument list, the '>>' has
24050 to be a typo for '> >'. We emit the error message, but we
24051 continue parsing and we push a '>' as next token, so that
24052 the argument list will be parsed correctly. Note that the
24053 global source location is still on the token before the
24054 '>>', so we need to say explicitly where we want it. */
24055 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24056 error_at (token
->location
, "%<>>%> should be %<> >%> "
24057 "within a nested template argument list");
24059 token
->type
= CPP_GREATER
;
24063 /* If this is not a nested template argument list, the '>>'
24064 is a typo for '>'. Emit an error message and continue.
24065 Same deal about the token location, but here we can get it
24066 right by consuming the '>>' before issuing the diagnostic. */
24067 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
24068 error_at (token
->location
,
24069 "spurious %<>>%>, use %<>%> to terminate "
24070 "a template argument list");
24074 cp_parser_skip_to_end_of_template_parameter_list (parser
);
24075 /* The `>' token might be a greater-than operator again now. */
24076 parser
->greater_than_is_operator_p
24077 = saved_greater_than_is_operator_p
;
24078 /* Restore the SAVED_SCOPE. */
24079 parser
->scope
= saved_scope
;
24080 parser
->qualifying_scope
= saved_qualifying_scope
;
24081 parser
->object_scope
= saved_object_scope
;
24082 cp_unevaluated_operand
= saved_unevaluated_operand
;
24083 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
24088 /* MEMBER_FUNCTION is a member function, or a friend. If default
24089 arguments, or the body of the function have not yet been parsed,
24093 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
24095 timevar_push (TV_PARSE_INMETH
);
24096 /* If this member is a template, get the underlying
24098 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
24099 member_function
= DECL_TEMPLATE_RESULT (member_function
);
24101 /* There should not be any class definitions in progress at this
24102 point; the bodies of members are only parsed outside of all class
24104 gcc_assert (parser
->num_classes_being_defined
== 0);
24105 /* While we're parsing the member functions we might encounter more
24106 classes. We want to handle them right away, but we don't want
24107 them getting mixed up with functions that are currently in the
24109 push_unparsed_function_queues (parser
);
24111 /* Make sure that any template parameters are in scope. */
24112 maybe_begin_member_template_processing (member_function
);
24114 /* If the body of the function has not yet been parsed, parse it
24116 if (DECL_PENDING_INLINE_P (member_function
))
24118 tree function_scope
;
24119 cp_token_cache
*tokens
;
24121 /* The function is no longer pending; we are processing it. */
24122 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
24123 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
24124 DECL_PENDING_INLINE_P (member_function
) = 0;
24126 /* If this is a local class, enter the scope of the containing
24128 function_scope
= current_function_decl
;
24129 if (function_scope
)
24130 push_function_context ();
24132 /* Push the body of the function onto the lexer stack. */
24133 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24135 /* Let the front end know that we going to be defining this
24137 start_preparsed_function (member_function
, NULL_TREE
,
24138 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
24140 /* Don't do access checking if it is a templated function. */
24141 if (processing_template_decl
)
24142 push_deferring_access_checks (dk_no_check
);
24144 /* #pragma omp declare reduction needs special parsing. */
24145 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
24147 parser
->lexer
->in_pragma
= true;
24148 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
24149 finish_function (/*inline*/2);
24150 cp_check_omp_declare_reduction (member_function
);
24153 /* Now, parse the body of the function. */
24154 cp_parser_function_definition_after_declarator (parser
,
24155 /*inline_p=*/true);
24157 if (processing_template_decl
)
24158 pop_deferring_access_checks ();
24160 /* Leave the scope of the containing function. */
24161 if (function_scope
)
24162 pop_function_context ();
24163 cp_parser_pop_lexer (parser
);
24166 /* Remove any template parameters from the symbol table. */
24167 maybe_end_member_template_processing ();
24169 /* Restore the queue. */
24170 pop_unparsed_function_queues (parser
);
24171 timevar_pop (TV_PARSE_INMETH
);
24174 /* If DECL contains any default args, remember it on the unparsed
24175 functions queue. */
24178 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
24182 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
24184 probe
= TREE_CHAIN (probe
))
24185 if (TREE_PURPOSE (probe
))
24187 cp_default_arg_entry entry
= {current_class_type
, decl
};
24188 vec_safe_push (unparsed_funs_with_default_args
, entry
);
24193 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24194 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24195 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24196 from the parameter-type-list. */
24199 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
24200 tree default_arg
, tree parmtype
)
24202 cp_token_cache
*tokens
;
24206 if (default_arg
== error_mark_node
)
24207 return error_mark_node
;
24209 /* Push the saved tokens for the default argument onto the parser's
24211 tokens
= DEFARG_TOKENS (default_arg
);
24212 cp_parser_push_lexer_for_tokens (parser
, tokens
);
24214 start_lambda_scope (decl
);
24216 /* Parse the default argument. */
24217 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
24218 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
24219 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24221 finish_lambda_scope ();
24223 if (parsed_arg
== error_mark_node
)
24224 cp_parser_skip_to_end_of_statement (parser
);
24226 if (!processing_template_decl
)
24228 /* In a non-template class, check conversions now. In a template,
24229 we'll wait and instantiate these as needed. */
24230 if (TREE_CODE (decl
) == PARM_DECL
)
24231 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
24232 tf_warning_or_error
);
24234 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
);
24237 /* If the token stream has not been completely used up, then
24238 there was extra junk after the end of the default
24240 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
24242 if (TREE_CODE (decl
) == PARM_DECL
)
24243 cp_parser_error (parser
, "expected %<,%>");
24245 cp_parser_error (parser
, "expected %<;%>");
24248 /* Revert to the main lexer. */
24249 cp_parser_pop_lexer (parser
);
24254 /* FIELD is a non-static data member with an initializer which we saved for
24255 later; parse it now. */
24258 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
24262 maybe_begin_member_template_processing (field
);
24264 push_unparsed_function_queues (parser
);
24265 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
24266 DECL_INITIAL (field
),
24268 pop_unparsed_function_queues (parser
);
24270 maybe_end_member_template_processing ();
24272 DECL_INITIAL (field
) = def
;
24275 /* FN is a FUNCTION_DECL which may contains a parameter with an
24276 unparsed DEFAULT_ARG. Parse the default args now. This function
24277 assumes that the current scope is the scope in which the default
24278 argument should be processed. */
24281 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
24283 bool saved_local_variables_forbidden_p
;
24284 tree parm
, parmdecl
;
24286 /* While we're parsing the default args, we might (due to the
24287 statement expression extension) encounter more classes. We want
24288 to handle them right away, but we don't want them getting mixed
24289 up with default args that are currently in the queue. */
24290 push_unparsed_function_queues (parser
);
24292 /* Local variable names (and the `this' keyword) may not appear
24293 in a default argument. */
24294 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
24295 parser
->local_variables_forbidden_p
= true;
24297 push_defarg_context (fn
);
24299 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
24300 parmdecl
= DECL_ARGUMENTS (fn
);
24301 parm
&& parm
!= void_list_node
;
24302 parm
= TREE_CHAIN (parm
),
24303 parmdecl
= DECL_CHAIN (parmdecl
))
24305 tree default_arg
= TREE_PURPOSE (parm
);
24307 vec
<tree
, va_gc
> *insts
;
24314 if (TREE_CODE (default_arg
) != DEFAULT_ARG
)
24315 /* This can happen for a friend declaration for a function
24316 already declared with default arguments. */
24320 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
24322 TREE_VALUE (parm
));
24323 if (parsed_arg
== error_mark_node
)
24328 TREE_PURPOSE (parm
) = parsed_arg
;
24330 /* Update any instantiations we've already created. */
24331 for (insts
= DEFARG_INSTANTIATIONS (default_arg
), ix
= 0;
24332 vec_safe_iterate (insts
, ix
, ©
); ix
++)
24333 TREE_PURPOSE (copy
) = parsed_arg
;
24336 pop_defarg_context ();
24338 /* Make sure no default arg is missing. */
24339 check_default_args (fn
);
24341 /* Restore the state of local_variables_forbidden_p. */
24342 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
24344 /* Restore the queue. */
24345 pop_unparsed_function_queues (parser
);
24348 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24350 sizeof ... ( identifier )
24352 where the 'sizeof' token has already been consumed. */
24355 cp_parser_sizeof_pack (cp_parser
*parser
)
24357 /* Consume the `...'. */
24358 cp_lexer_consume_token (parser
->lexer
);
24359 maybe_warn_variadic_templates ();
24361 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
24363 cp_lexer_consume_token (parser
->lexer
);
24365 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
24366 "%<sizeof...%> argument must be surrounded by parentheses");
24368 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24369 tree name
= cp_parser_identifier (parser
);
24370 if (name
== error_mark_node
)
24371 return error_mark_node
;
24372 /* The name is not qualified. */
24373 parser
->scope
= NULL_TREE
;
24374 parser
->qualifying_scope
= NULL_TREE
;
24375 parser
->object_scope
= NULL_TREE
;
24376 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
24377 if (expr
== error_mark_node
)
24378 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
24380 if (TREE_CODE (expr
) == TYPE_DECL
)
24381 expr
= TREE_TYPE (expr
);
24382 else if (TREE_CODE (expr
) == CONST_DECL
)
24383 expr
= DECL_INITIAL (expr
);
24384 expr
= make_pack_expansion (expr
);
24387 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24392 /* Parse the operand of `sizeof' (or a similar operator). Returns
24393 either a TYPE or an expression, depending on the form of the
24394 input. The KEYWORD indicates which kind of expression we have
24398 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
24400 tree expr
= NULL_TREE
;
24401 const char *saved_message
;
24403 bool saved_integral_constant_expression_p
;
24404 bool saved_non_integral_constant_expression_p
;
24406 /* If it's a `...', then we are computing the length of a parameter
24408 if (keyword
== RID_SIZEOF
24409 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24410 return cp_parser_sizeof_pack (parser
);
24412 /* Types cannot be defined in a `sizeof' expression. Save away the
24414 saved_message
= parser
->type_definition_forbidden_message
;
24415 /* And create the new one. */
24416 tmp
= concat ("types may not be defined in %<",
24417 IDENTIFIER_POINTER (ridpointers
[keyword
]),
24418 "%> expressions", NULL
);
24419 parser
->type_definition_forbidden_message
= tmp
;
24421 /* The restrictions on constant-expressions do not apply inside
24422 sizeof expressions. */
24423 saved_integral_constant_expression_p
24424 = parser
->integral_constant_expression_p
;
24425 saved_non_integral_constant_expression_p
24426 = parser
->non_integral_constant_expression_p
;
24427 parser
->integral_constant_expression_p
= false;
24429 /* Do not actually evaluate the expression. */
24430 ++cp_unevaluated_operand
;
24431 ++c_inhibit_evaluation_warnings
;
24432 /* If it's a `(', then we might be looking at the type-id
24434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
24436 tree type
= NULL_TREE
;
24438 /* We can't be sure yet whether we're looking at a type-id or an
24440 cp_parser_parse_tentatively (parser
);
24441 /* Note: as a GNU Extension, compound literals are considered
24442 postfix-expressions as they are in C99, so they are valid
24443 arguments to sizeof. See comment in cp_parser_cast_expression
24445 if (cp_parser_compound_literal_p (parser
))
24446 cp_parser_simulate_error (parser
);
24449 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
24450 parser
->in_type_id_in_expr_p
= true;
24451 /* Look for the type-id. */
24452 type
= cp_parser_type_id (parser
);
24453 /* Look for the closing `)'. */
24454 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
24455 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
24458 /* If all went well, then we're done. */
24459 if (cp_parser_parse_definitely (parser
))
24461 cp_decl_specifier_seq decl_specs
;
24463 /* Build a trivial decl-specifier-seq. */
24464 clear_decl_specs (&decl_specs
);
24465 decl_specs
.type
= type
;
24467 /* Call grokdeclarator to figure out what type this is. */
24468 expr
= grokdeclarator (NULL
,
24472 /*attrlist=*/NULL
);
24476 /* If the type-id production did not work out, then we must be
24477 looking at the unary-expression production. */
24479 expr
= cp_parser_unary_expression (parser
);
24481 /* Go back to evaluating expressions. */
24482 --cp_unevaluated_operand
;
24483 --c_inhibit_evaluation_warnings
;
24485 /* Free the message we created. */
24487 /* And restore the old one. */
24488 parser
->type_definition_forbidden_message
= saved_message
;
24489 parser
->integral_constant_expression_p
24490 = saved_integral_constant_expression_p
;
24491 parser
->non_integral_constant_expression_p
24492 = saved_non_integral_constant_expression_p
;
24497 /* If the current declaration has no declarator, return true. */
24500 cp_parser_declares_only_class_p (cp_parser
*parser
)
24502 /* If the next token is a `;' or a `,' then there is no
24504 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
24505 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
24508 /* Update the DECL_SPECS to reflect the storage class indicated by
24512 cp_parser_set_storage_class (cp_parser
*parser
,
24513 cp_decl_specifier_seq
*decl_specs
,
24517 cp_storage_class storage_class
;
24519 if (parser
->in_unbraced_linkage_specification_p
)
24521 error_at (token
->location
, "invalid use of %qD in linkage specification",
24522 ridpointers
[keyword
]);
24525 else if (decl_specs
->storage_class
!= sc_none
)
24527 decl_specs
->conflicting_specifiers_p
= true;
24531 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
24532 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
24533 && decl_specs
->gnu_thread_keyword_p
)
24535 pedwarn (decl_specs
->locations
[ds_thread
], 0,
24536 "%<__thread%> before %qD", ridpointers
[keyword
]);
24542 storage_class
= sc_auto
;
24545 storage_class
= sc_register
;
24548 storage_class
= sc_static
;
24551 storage_class
= sc_extern
;
24554 storage_class
= sc_mutable
;
24557 gcc_unreachable ();
24559 decl_specs
->storage_class
= storage_class
;
24560 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
24562 /* A storage class specifier cannot be applied alongside a typedef
24563 specifier. If there is a typedef specifier present then set
24564 conflicting_specifiers_p which will trigger an error later
24565 on in grokdeclarator. */
24566 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
24567 decl_specs
->conflicting_specifiers_p
= true;
24570 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24571 is true, the type is a class or enum definition. */
24574 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
24577 bool type_definition_p
)
24579 decl_specs
->any_specifiers_p
= true;
24581 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24582 (with, for example, in "typedef int wchar_t;") we remember that
24583 this is what happened. In system headers, we ignore these
24584 declarations so that G++ can work with system headers that are not
24586 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
24587 && !type_definition_p
24588 && (type_spec
== boolean_type_node
24589 || type_spec
== char16_type_node
24590 || type_spec
== char32_type_node
24591 || type_spec
== wchar_type_node
)
24592 && (decl_specs
->type
24593 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
24594 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
24595 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
24596 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
24598 decl_specs
->redefined_builtin_type
= type_spec
;
24599 set_and_check_decl_spec_loc (decl_specs
,
24600 ds_redefined_builtin_type_spec
,
24602 if (!decl_specs
->type
)
24604 decl_specs
->type
= type_spec
;
24605 decl_specs
->type_definition_p
= false;
24606 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
24609 else if (decl_specs
->type
)
24610 decl_specs
->multiple_types_p
= true;
24613 decl_specs
->type
= type_spec
;
24614 decl_specs
->type_definition_p
= type_definition_p
;
24615 decl_specs
->redefined_builtin_type
= NULL_TREE
;
24616 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
24620 /* True iff TOKEN is the GNU keyword __thread. */
24623 token_is__thread (cp_token
*token
)
24625 gcc_assert (token
->keyword
== RID_THREAD
);
24626 return !strcmp (IDENTIFIER_POINTER (token
->u
.value
), "__thread");
24629 /* Set the location for a declarator specifier and check if it is
24632 DECL_SPECS is the sequence of declarator specifiers onto which to
24635 DS is the single declarator specifier to set which location is to
24636 be set onto the existing sequence of declarators.
24638 LOCATION is the location for the declarator specifier to
24642 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
24643 cp_decl_spec ds
, cp_token
*token
)
24645 gcc_assert (ds
< ds_last
);
24647 if (decl_specs
== NULL
)
24650 source_location location
= token
->location
;
24652 if (decl_specs
->locations
[ds
] == 0)
24654 decl_specs
->locations
[ds
] = location
;
24655 if (ds
== ds_thread
)
24656 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
24662 if (decl_specs
->locations
[ds_long_long
] != 0)
24663 error_at (location
,
24664 "%<long long long%> is too long for GCC");
24667 decl_specs
->locations
[ds_long_long
] = location
;
24668 pedwarn_cxx98 (location
,
24670 "ISO C++ 1998 does not support %<long long%>");
24673 else if (ds
== ds_thread
)
24675 bool gnu
= token_is__thread (token
);
24676 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
24677 error_at (location
,
24678 "both %<__thread%> and %<thread_local%> specified");
24680 error_at (location
, "duplicate %qD", token
->u
.value
);
24684 static const char *const decl_spec_names
[] = {
24701 error_at (location
,
24702 "duplicate %qs", decl_spec_names
[ds
]);
24707 /* Return true iff the declarator specifier DS is present in the
24708 sequence of declarator specifiers DECL_SPECS. */
24711 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
24714 gcc_assert (ds
< ds_last
);
24716 if (decl_specs
== NULL
)
24719 return decl_specs
->locations
[ds
] != 0;
24722 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24723 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24726 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
24728 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
24731 /* Issue an error message indicating that TOKEN_DESC was expected.
24732 If KEYWORD is true, it indicated this function is called by
24733 cp_parser_require_keword and the required token can only be
24734 a indicated keyword. */
24737 cp_parser_required_error (cp_parser
*parser
,
24738 required_token token_desc
,
24741 switch (token_desc
)
24744 cp_parser_error (parser
, "expected %<new%>");
24747 cp_parser_error (parser
, "expected %<delete%>");
24750 cp_parser_error (parser
, "expected %<return%>");
24753 cp_parser_error (parser
, "expected %<while%>");
24756 cp_parser_error (parser
, "expected %<extern%>");
24758 case RT_STATIC_ASSERT
:
24759 cp_parser_error (parser
, "expected %<static_assert%>");
24762 cp_parser_error (parser
, "expected %<decltype%>");
24765 cp_parser_error (parser
, "expected %<operator%>");
24768 cp_parser_error (parser
, "expected %<class%>");
24771 cp_parser_error (parser
, "expected %<template%>");
24774 cp_parser_error (parser
, "expected %<namespace%>");
24777 cp_parser_error (parser
, "expected %<using%>");
24780 cp_parser_error (parser
, "expected %<asm%>");
24783 cp_parser_error (parser
, "expected %<try%>");
24786 cp_parser_error (parser
, "expected %<catch%>");
24789 cp_parser_error (parser
, "expected %<throw%>");
24792 cp_parser_error (parser
, "expected %<__label__%>");
24795 cp_parser_error (parser
, "expected %<@try%>");
24797 case RT_AT_SYNCHRONIZED
:
24798 cp_parser_error (parser
, "expected %<@synchronized%>");
24801 cp_parser_error (parser
, "expected %<@throw%>");
24803 case RT_TRANSACTION_ATOMIC
:
24804 cp_parser_error (parser
, "expected %<__transaction_atomic%>");
24806 case RT_TRANSACTION_RELAXED
:
24807 cp_parser_error (parser
, "expected %<__transaction_relaxed%>");
24814 switch (token_desc
)
24817 cp_parser_error (parser
, "expected %<;%>");
24819 case RT_OPEN_PAREN
:
24820 cp_parser_error (parser
, "expected %<(%>");
24822 case RT_CLOSE_BRACE
:
24823 cp_parser_error (parser
, "expected %<}%>");
24825 case RT_OPEN_BRACE
:
24826 cp_parser_error (parser
, "expected %<{%>");
24828 case RT_CLOSE_SQUARE
:
24829 cp_parser_error (parser
, "expected %<]%>");
24831 case RT_OPEN_SQUARE
:
24832 cp_parser_error (parser
, "expected %<[%>");
24835 cp_parser_error (parser
, "expected %<,%>");
24838 cp_parser_error (parser
, "expected %<::%>");
24841 cp_parser_error (parser
, "expected %<<%>");
24844 cp_parser_error (parser
, "expected %<>%>");
24847 cp_parser_error (parser
, "expected %<=%>");
24850 cp_parser_error (parser
, "expected %<...%>");
24853 cp_parser_error (parser
, "expected %<*%>");
24856 cp_parser_error (parser
, "expected %<~%>");
24859 cp_parser_error (parser
, "expected %<:%>");
24861 case RT_COLON_SCOPE
:
24862 cp_parser_error (parser
, "expected %<:%> or %<::%>");
24864 case RT_CLOSE_PAREN
:
24865 cp_parser_error (parser
, "expected %<)%>");
24867 case RT_COMMA_CLOSE_PAREN
:
24868 cp_parser_error (parser
, "expected %<,%> or %<)%>");
24870 case RT_PRAGMA_EOL
:
24871 cp_parser_error (parser
, "expected end of line");
24874 cp_parser_error (parser
, "expected identifier");
24877 cp_parser_error (parser
, "expected selection-statement");
24879 case RT_INTERATION
:
24880 cp_parser_error (parser
, "expected iteration-statement");
24883 cp_parser_error (parser
, "expected jump-statement");
24886 cp_parser_error (parser
, "expected class-key");
24888 case RT_CLASS_TYPENAME_TEMPLATE
:
24889 cp_parser_error (parser
,
24890 "expected %<class%>, %<typename%>, or %<template%>");
24893 gcc_unreachable ();
24897 gcc_unreachable ();
24902 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24903 issue an error message indicating that TOKEN_DESC was expected.
24905 Returns the token consumed, if the token had the appropriate type.
24906 Otherwise, returns NULL. */
24909 cp_parser_require (cp_parser
* parser
,
24910 enum cpp_ttype type
,
24911 required_token token_desc
)
24913 if (cp_lexer_next_token_is (parser
->lexer
, type
))
24914 return cp_lexer_consume_token (parser
->lexer
);
24917 /* Output the MESSAGE -- unless we're parsing tentatively. */
24918 if (!cp_parser_simulate_error (parser
))
24919 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false);
24924 /* An error message is produced if the next token is not '>'.
24925 All further tokens are skipped until the desired token is
24926 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24929 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
24931 /* Current level of '< ... >'. */
24932 unsigned level
= 0;
24933 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24934 unsigned nesting_depth
= 0;
24936 /* Are we ready, yet? If not, issue error message. */
24937 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
24940 /* Skip tokens until the desired token is found. */
24943 /* Peek at the next token. */
24944 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
24947 if (!nesting_depth
)
24952 if (cxx_dialect
== cxx98
)
24953 /* C++0x views the `>>' operator as two `>' tokens, but
24956 else if (!nesting_depth
&& level
-- == 0)
24958 /* We've hit a `>>' where the first `>' closes the
24959 template argument list, and the second `>' is
24960 spurious. Just consume the `>>' and stop; we've
24961 already produced at least one error. */
24962 cp_lexer_consume_token (parser
->lexer
);
24965 /* Fall through for C++0x, so we handle the second `>' in
24969 if (!nesting_depth
&& level
-- == 0)
24971 /* We've reached the token we want, consume it and stop. */
24972 cp_lexer_consume_token (parser
->lexer
);
24977 case CPP_OPEN_PAREN
:
24978 case CPP_OPEN_SQUARE
:
24982 case CPP_CLOSE_PAREN
:
24983 case CPP_CLOSE_SQUARE
:
24984 if (nesting_depth
-- == 0)
24989 case CPP_PRAGMA_EOL
:
24990 case CPP_SEMICOLON
:
24991 case CPP_OPEN_BRACE
:
24992 case CPP_CLOSE_BRACE
:
24993 /* The '>' was probably forgotten, don't look further. */
25000 /* Consume this token. */
25001 cp_lexer_consume_token (parser
->lexer
);
25005 /* If the next token is the indicated keyword, consume it. Otherwise,
25006 issue an error message indicating that TOKEN_DESC was expected.
25008 Returns the token consumed, if the token had the appropriate type.
25009 Otherwise, returns NULL. */
25012 cp_parser_require_keyword (cp_parser
* parser
,
25014 required_token token_desc
)
25016 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
25018 if (token
&& token
->keyword
!= keyword
)
25020 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true);
25027 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25028 function-definition. */
25031 cp_parser_token_starts_function_definition_p (cp_token
* token
)
25033 return (/* An ordinary function-body begins with an `{'. */
25034 token
->type
== CPP_OPEN_BRACE
25035 /* A ctor-initializer begins with a `:'. */
25036 || token
->type
== CPP_COLON
25037 /* A function-try-block begins with `try'. */
25038 || token
->keyword
== RID_TRY
25039 /* A function-transaction-block begins with `__transaction_atomic'
25040 or `__transaction_relaxed'. */
25041 || token
->keyword
== RID_TRANSACTION_ATOMIC
25042 || token
->keyword
== RID_TRANSACTION_RELAXED
25043 /* The named return value extension begins with `return'. */
25044 || token
->keyword
== RID_RETURN
);
25047 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25051 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
25055 token
= cp_lexer_peek_token (parser
->lexer
);
25056 return (token
->type
== CPP_OPEN_BRACE
25057 || (token
->type
== CPP_COLON
25058 && !parser
->colon_doesnt_start_class_def_p
));
25061 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25062 C++0x) ending a template-argument. */
25065 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
25069 token
= cp_lexer_peek_token (parser
->lexer
);
25070 return (token
->type
== CPP_COMMA
25071 || token
->type
== CPP_GREATER
25072 || token
->type
== CPP_ELLIPSIS
25073 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
25076 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25077 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25080 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
25085 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25086 if (token
->type
== CPP_LESS
)
25088 /* Check for the sequence `<::' in the original code. It would be lexed as
25089 `[:', where `[' is a digraph, and there is no whitespace before
25091 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
25094 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
25095 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
25101 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25102 or none_type otherwise. */
25104 static enum tag_types
25105 cp_parser_token_is_class_key (cp_token
* token
)
25107 switch (token
->keyword
)
25112 return record_type
;
25121 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25122 or none_type otherwise or if the token is null. */
25124 static enum tag_types
25125 cp_parser_token_is_type_parameter_key (cp_token
* token
)
25130 switch (token
->keyword
)
25135 return typename_type
;
25142 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25145 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
25147 if (type
== error_mark_node
)
25149 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
25151 if (permerror (input_location
, "%qs tag used in naming %q#T",
25152 class_key
== union_type
? "union"
25153 : class_key
== record_type
? "struct" : "class",
25155 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
25156 "%q#T was previously declared here", type
);
25160 /* Issue an error message if DECL is redeclared with different
25161 access than its original declaration [class.access.spec/3].
25162 This applies to nested classes and nested class templates.
25166 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
25168 if (!decl
|| !CLASS_TYPE_P (TREE_TYPE (decl
)))
25171 if ((TREE_PRIVATE (decl
)
25172 != (current_access_specifier
== access_private_node
))
25173 || (TREE_PROTECTED (decl
)
25174 != (current_access_specifier
== access_protected_node
)))
25175 error_at (location
, "%qD redeclared with different access", decl
);
25178 /* Look for the `template' keyword, as a syntactic disambiguator.
25179 Return TRUE iff it is present, in which case it will be
25183 cp_parser_optional_template_keyword (cp_parser
*parser
)
25185 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25187 /* In C++98 the `template' keyword can only be used within templates;
25188 outside templates the parser can always figure out what is a
25189 template and what is not. In C++11, per the resolution of DR 468,
25190 `template' is allowed in cases where it is not strictly necessary. */
25191 if (!processing_template_decl
25192 && pedantic
&& cxx_dialect
== cxx98
)
25194 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25195 pedwarn (token
->location
, OPT_Wpedantic
,
25196 "in C++98 %<template%> (as a disambiguator) is only "
25197 "allowed within templates");
25198 /* If this part of the token stream is rescanned, the same
25199 error message would be generated. So, we purge the token
25200 from the stream. */
25201 cp_lexer_purge_token (parser
->lexer
);
25206 /* Consume the `template' keyword. */
25207 cp_lexer_consume_token (parser
->lexer
);
25214 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25215 set PARSER->SCOPE, and perform other related actions. */
25218 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
25221 struct tree_check
*check_value
;
25222 deferred_access_check
*chk
;
25223 vec
<deferred_access_check
, va_gc
> *checks
;
25225 /* Get the stored value. */
25226 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
25227 /* Perform any access checks that were deferred. */
25228 checks
= check_value
->checks
;
25231 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
25232 perform_or_defer_access_check (chk
->binfo
,
25234 chk
->diag_decl
, tf_warning_or_error
);
25236 /* Set the scope from the stored value. */
25237 parser
->scope
= check_value
->value
;
25238 parser
->qualifying_scope
= check_value
->qualifying_scope
;
25239 parser
->object_scope
= NULL_TREE
;
25242 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25243 encounter the end of a block before what we were looking for. */
25246 cp_parser_cache_group (cp_parser
*parser
,
25247 enum cpp_ttype end
,
25252 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25254 /* Abort a parenthesized expression if we encounter a semicolon. */
25255 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
25256 && token
->type
== CPP_SEMICOLON
)
25258 /* If we've reached the end of the file, stop. */
25259 if (token
->type
== CPP_EOF
25260 || (end
!= CPP_PRAGMA_EOL
25261 && token
->type
== CPP_PRAGMA_EOL
))
25263 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
25264 /* We've hit the end of an enclosing block, so there's been some
25265 kind of syntax error. */
25268 /* Consume the token. */
25269 cp_lexer_consume_token (parser
->lexer
);
25270 /* See if it starts a new group. */
25271 if (token
->type
== CPP_OPEN_BRACE
)
25273 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
25274 /* In theory this should probably check end == '}', but
25275 cp_parser_save_member_function_body needs it to exit
25276 after either '}' or ')' when called with ')'. */
25280 else if (token
->type
== CPP_OPEN_PAREN
)
25282 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
25283 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
25286 else if (token
->type
== CPP_PRAGMA
)
25287 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
25288 else if (token
->type
== end
)
25293 /* Like above, for caching a default argument or NSDMI. Both of these are
25294 terminated by a non-nested comma, but it can be unclear whether or not a
25295 comma is nested in a template argument list unless we do more parsing.
25296 In order to handle this ambiguity, when we encounter a ',' after a '<'
25297 we try to parse what follows as a parameter-declaration-list (in the
25298 case of a default argument) or a member-declarator (in the case of an
25299 NSDMI). If that succeeds, then we stop caching. */
25302 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
25304 unsigned depth
= 0;
25305 int maybe_template_id
= 0;
25306 cp_token
*first_token
;
25308 tree default_argument
;
25310 /* Add tokens until we have processed the entire default
25311 argument. We add the range [first_token, token). */
25312 first_token
= cp_lexer_peek_token (parser
->lexer
);
25313 if (first_token
->type
== CPP_OPEN_BRACE
)
25315 /* For list-initialization, this is straightforward. */
25316 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
25317 token
= cp_lexer_peek_token (parser
->lexer
);
25323 /* Peek at the next token. */
25324 token
= cp_lexer_peek_token (parser
->lexer
);
25325 /* What we do depends on what token we have. */
25326 switch (token
->type
)
25328 /* In valid code, a default argument must be
25329 immediately followed by a `,' `)', or `...'. */
25331 if (depth
== 0 && maybe_template_id
)
25333 /* If we've seen a '<', we might be in a
25334 template-argument-list. Until Core issue 325 is
25335 resolved, we don't know how this situation ought
25336 to be handled, so try to DTRT. We check whether
25337 what comes after the comma is a valid parameter
25338 declaration list. If it is, then the comma ends
25339 the default argument; otherwise the default
25340 argument continues. */
25341 bool error
= false;
25343 /* Set ITALP so cp_parser_parameter_declaration_list
25344 doesn't decide to commit to this parse. */
25345 bool saved_italp
= parser
->in_template_argument_list_p
;
25346 parser
->in_template_argument_list_p
= true;
25348 cp_parser_parse_tentatively (parser
);
25349 cp_lexer_consume_token (parser
->lexer
);
25353 int ctor_dtor_or_conv_p
;
25354 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25355 &ctor_dtor_or_conv_p
,
25356 /*parenthesized_p=*/NULL
,
25358 /*friend_p=*/false);
25362 begin_scope (sk_function_parms
, NULL_TREE
);
25363 cp_parser_parameter_declaration_list (parser
, &error
);
25364 pop_bindings_and_leave_scope ();
25366 if (!cp_parser_error_occurred (parser
) && !error
)
25368 cp_parser_abort_tentative_parse (parser
);
25370 parser
->in_template_argument_list_p
= saved_italp
;
25373 case CPP_CLOSE_PAREN
:
25375 /* If we run into a non-nested `;', `}', or `]',
25376 then the code is invalid -- but the default
25377 argument is certainly over. */
25378 case CPP_SEMICOLON
:
25379 case CPP_CLOSE_BRACE
:
25380 case CPP_CLOSE_SQUARE
:
25382 /* Handle correctly int n = sizeof ... ( p ); */
25383 && token
->type
!= CPP_ELLIPSIS
)
25385 /* Update DEPTH, if necessary. */
25386 else if (token
->type
== CPP_CLOSE_PAREN
25387 || token
->type
== CPP_CLOSE_BRACE
25388 || token
->type
== CPP_CLOSE_SQUARE
)
25392 case CPP_OPEN_PAREN
:
25393 case CPP_OPEN_SQUARE
:
25394 case CPP_OPEN_BRACE
:
25400 /* This might be the comparison operator, or it might
25401 start a template argument list. */
25402 ++maybe_template_id
;
25406 if (cxx_dialect
== cxx98
)
25408 /* Fall through for C++0x, which treats the `>>'
25409 operator like two `>' tokens in certain
25415 /* This might be an operator, or it might close a
25416 template argument list. But if a previous '<'
25417 started a template argument list, this will have
25418 closed it, so we can't be in one anymore. */
25419 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
25420 if (maybe_template_id
< 0)
25421 maybe_template_id
= 0;
25425 /* If we run out of tokens, issue an error message. */
25427 case CPP_PRAGMA_EOL
:
25428 error_at (token
->location
, "file ends in default argument");
25434 /* In these cases, we should look for template-ids.
25435 For example, if the default argument is
25436 `X<int, double>()', we need to do name lookup to
25437 figure out whether or not `X' is a template; if
25438 so, the `,' does not end the default argument.
25440 That is not yet done. */
25447 /* If we've reached the end, stop. */
25451 /* Add the token to the token block. */
25452 token
= cp_lexer_consume_token (parser
->lexer
);
25455 /* Create a DEFAULT_ARG to represent the unparsed default
25457 default_argument
= make_node (DEFAULT_ARG
);
25458 DEFARG_TOKENS (default_argument
)
25459 = cp_token_cache_new (first_token
, token
);
25460 DEFARG_INSTANTIATIONS (default_argument
) = NULL
;
25462 return default_argument
;
25465 /* Begin parsing tentatively. We always save tokens while parsing
25466 tentatively so that if the tentative parsing fails we can restore the
25470 cp_parser_parse_tentatively (cp_parser
* parser
)
25472 /* Enter a new parsing context. */
25473 parser
->context
= cp_parser_context_new (parser
->context
);
25474 /* Begin saving tokens. */
25475 cp_lexer_save_tokens (parser
->lexer
);
25476 /* In order to avoid repetitive access control error messages,
25477 access checks are queued up until we are no longer parsing
25479 push_deferring_access_checks (dk_deferred
);
25482 /* Commit to the currently active tentative parse. */
25485 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
25487 cp_parser_context
*context
;
25490 /* Mark all of the levels as committed. */
25491 lexer
= parser
->lexer
;
25492 for (context
= parser
->context
; context
->next
; context
= context
->next
)
25494 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25496 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25497 while (!cp_lexer_saving_tokens (lexer
))
25498 lexer
= lexer
->next
;
25499 cp_lexer_commit_tokens (lexer
);
25503 /* Commit to the topmost currently active tentative parse.
25505 Note that this function shouldn't be called when there are
25506 irreversible side-effects while in a tentative state. For
25507 example, we shouldn't create a permanent entry in the symbol
25508 table, or issue an error message that might not apply if the
25509 tentative parse is aborted. */
25512 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
25514 cp_parser_context
*context
= parser
->context
;
25515 cp_lexer
*lexer
= parser
->lexer
;
25519 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
25521 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
25523 while (!cp_lexer_saving_tokens (lexer
))
25524 lexer
= lexer
->next
;
25525 cp_lexer_commit_tokens (lexer
);
25529 /* Abort the currently active tentative parse. All consumed tokens
25530 will be rolled back, and no diagnostics will be issued. */
25533 cp_parser_abort_tentative_parse (cp_parser
* parser
)
25535 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
25536 || errorcount
> 0);
25537 cp_parser_simulate_error (parser
);
25538 /* Now, pretend that we want to see if the construct was
25539 successfully parsed. */
25540 cp_parser_parse_definitely (parser
);
25543 /* Stop parsing tentatively. If a parse error has occurred, restore the
25544 token stream. Otherwise, commit to the tokens we have consumed.
25545 Returns true if no error occurred; false otherwise. */
25548 cp_parser_parse_definitely (cp_parser
* parser
)
25550 bool error_occurred
;
25551 cp_parser_context
*context
;
25553 /* Remember whether or not an error occurred, since we are about to
25554 destroy that information. */
25555 error_occurred
= cp_parser_error_occurred (parser
);
25556 /* Remove the topmost context from the stack. */
25557 context
= parser
->context
;
25558 parser
->context
= context
->next
;
25559 /* If no parse errors occurred, commit to the tentative parse. */
25560 if (!error_occurred
)
25562 /* Commit to the tokens read tentatively, unless that was
25564 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
25565 cp_lexer_commit_tokens (parser
->lexer
);
25567 pop_to_parent_deferring_access_checks ();
25569 /* Otherwise, if errors occurred, roll back our state so that things
25570 are just as they were before we began the tentative parse. */
25573 cp_lexer_rollback_tokens (parser
->lexer
);
25574 pop_deferring_access_checks ();
25576 /* Add the context to the front of the free list. */
25577 context
->next
= cp_parser_context_free_list
;
25578 cp_parser_context_free_list
= context
;
25580 return !error_occurred
;
25583 /* Returns true if we are parsing tentatively and are not committed to
25584 this tentative parse. */
25587 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
25589 return (cp_parser_parsing_tentatively (parser
)
25590 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
25593 /* Returns nonzero iff an error has occurred during the most recent
25594 tentative parse. */
25597 cp_parser_error_occurred (cp_parser
* parser
)
25599 return (cp_parser_parsing_tentatively (parser
)
25600 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
25603 /* Returns nonzero if GNU extensions are allowed. */
25606 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
25608 return parser
->allow_gnu_extensions_p
;
25611 /* Objective-C++ Productions */
25614 /* Parse an Objective-C expression, which feeds into a primary-expression
25618 objc-message-expression
25619 objc-string-literal
25620 objc-encode-expression
25621 objc-protocol-expression
25622 objc-selector-expression
25624 Returns a tree representation of the expression. */
25627 cp_parser_objc_expression (cp_parser
* parser
)
25629 /* Try to figure out what kind of declaration is present. */
25630 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
25634 case CPP_OPEN_SQUARE
:
25635 return cp_parser_objc_message_expression (parser
);
25637 case CPP_OBJC_STRING
:
25638 kwd
= cp_lexer_consume_token (parser
->lexer
);
25639 return objc_build_string_object (kwd
->u
.value
);
25642 switch (kwd
->keyword
)
25644 case RID_AT_ENCODE
:
25645 return cp_parser_objc_encode_expression (parser
);
25647 case RID_AT_PROTOCOL
:
25648 return cp_parser_objc_protocol_expression (parser
);
25650 case RID_AT_SELECTOR
:
25651 return cp_parser_objc_selector_expression (parser
);
25657 error_at (kwd
->location
,
25658 "misplaced %<@%D%> Objective-C++ construct",
25660 cp_parser_skip_to_end_of_block_or_statement (parser
);
25663 return error_mark_node
;
25666 /* Parse an Objective-C message expression.
25668 objc-message-expression:
25669 [ objc-message-receiver objc-message-args ]
25671 Returns a representation of an Objective-C message. */
25674 cp_parser_objc_message_expression (cp_parser
* parser
)
25676 tree receiver
, messageargs
;
25678 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
25679 receiver
= cp_parser_objc_message_receiver (parser
);
25680 messageargs
= cp_parser_objc_message_args (parser
);
25681 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
25683 return objc_build_message_expr (receiver
, messageargs
);
25686 /* Parse an objc-message-receiver.
25688 objc-message-receiver:
25690 simple-type-specifier
25692 Returns a representation of the type or expression. */
25695 cp_parser_objc_message_receiver (cp_parser
* parser
)
25699 /* An Objective-C message receiver may be either (1) a type
25700 or (2) an expression. */
25701 cp_parser_parse_tentatively (parser
);
25702 rcv
= cp_parser_expression (parser
);
25704 /* If that worked out, fine. */
25705 if (cp_parser_parse_definitely (parser
))
25708 cp_parser_parse_tentatively (parser
);
25709 rcv
= cp_parser_simple_type_specifier (parser
,
25710 /*decl_specs=*/NULL
,
25711 CP_PARSER_FLAGS_NONE
);
25713 if (cp_parser_parse_definitely (parser
))
25714 return objc_get_class_reference (rcv
);
25716 cp_parser_error (parser
, "objective-c++ message receiver expected");
25717 return error_mark_node
;
25720 /* Parse the arguments and selectors comprising an Objective-C message.
25725 objc-selector-args , objc-comma-args
25727 objc-selector-args:
25728 objc-selector [opt] : assignment-expression
25729 objc-selector-args objc-selector [opt] : assignment-expression
25732 assignment-expression
25733 objc-comma-args , assignment-expression
25735 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25736 selector arguments and TREE_VALUE containing a list of comma
25740 cp_parser_objc_message_args (cp_parser
* parser
)
25742 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
25743 bool maybe_unary_selector_p
= true;
25744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25746 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
25748 tree selector
= NULL_TREE
, arg
;
25750 if (token
->type
!= CPP_COLON
)
25751 selector
= cp_parser_objc_selector (parser
);
25753 /* Detect if we have a unary selector. */
25754 if (maybe_unary_selector_p
25755 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
25756 return build_tree_list (selector
, NULL_TREE
);
25758 maybe_unary_selector_p
= false;
25759 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25760 arg
= cp_parser_assignment_expression (parser
);
25763 = chainon (sel_args
,
25764 build_tree_list (selector
, arg
));
25766 token
= cp_lexer_peek_token (parser
->lexer
);
25769 /* Handle non-selector arguments, if any. */
25770 while (token
->type
== CPP_COMMA
)
25774 cp_lexer_consume_token (parser
->lexer
);
25775 arg
= cp_parser_assignment_expression (parser
);
25778 = chainon (addl_args
,
25779 build_tree_list (NULL_TREE
, arg
));
25781 token
= cp_lexer_peek_token (parser
->lexer
);
25784 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
25786 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
25787 return build_tree_list (error_mark_node
, error_mark_node
);
25790 return build_tree_list (sel_args
, addl_args
);
25793 /* Parse an Objective-C encode expression.
25795 objc-encode-expression:
25796 @encode objc-typename
25798 Returns an encoded representation of the type argument. */
25801 cp_parser_objc_encode_expression (cp_parser
* parser
)
25806 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
25807 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25808 token
= cp_lexer_peek_token (parser
->lexer
);
25809 type
= complete_type (cp_parser_type_id (parser
));
25810 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25814 error_at (token
->location
,
25815 "%<@encode%> must specify a type as an argument");
25816 return error_mark_node
;
25819 /* This happens if we find @encode(T) (where T is a template
25820 typename or something dependent on a template typename) when
25821 parsing a template. In that case, we can't compile it
25822 immediately, but we rather create an AT_ENCODE_EXPR which will
25823 need to be instantiated when the template is used.
25825 if (dependent_type_p (type
))
25827 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
25828 TREE_READONLY (value
) = 1;
25832 return objc_build_encode_expr (type
);
25835 /* Parse an Objective-C @defs expression. */
25838 cp_parser_objc_defs_expression (cp_parser
*parser
)
25842 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
25843 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25844 name
= cp_parser_identifier (parser
);
25845 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25847 return objc_get_class_ivars (name
);
25850 /* Parse an Objective-C protocol expression.
25852 objc-protocol-expression:
25853 @protocol ( identifier )
25855 Returns a representation of the protocol expression. */
25858 cp_parser_objc_protocol_expression (cp_parser
* parser
)
25862 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
25863 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25864 proto
= cp_parser_identifier (parser
);
25865 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25867 return objc_build_protocol_expr (proto
);
25870 /* Parse an Objective-C selector expression.
25872 objc-selector-expression:
25873 @selector ( objc-method-signature )
25875 objc-method-signature:
25881 objc-selector-seq objc-selector :
25883 Returns a representation of the method selector. */
25886 cp_parser_objc_selector_expression (cp_parser
* parser
)
25888 tree sel_seq
= NULL_TREE
;
25889 bool maybe_unary_selector_p
= true;
25891 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
25893 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
25894 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
25895 token
= cp_lexer_peek_token (parser
->lexer
);
25897 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
25898 || token
->type
== CPP_SCOPE
)
25900 tree selector
= NULL_TREE
;
25902 if (token
->type
!= CPP_COLON
25903 || token
->type
== CPP_SCOPE
)
25904 selector
= cp_parser_objc_selector (parser
);
25906 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
25907 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
25909 /* Detect if we have a unary selector. */
25910 if (maybe_unary_selector_p
)
25912 sel_seq
= selector
;
25913 goto finish_selector
;
25917 cp_parser_error (parser
, "expected %<:%>");
25920 maybe_unary_selector_p
= false;
25921 token
= cp_lexer_consume_token (parser
->lexer
);
25923 if (token
->type
== CPP_SCOPE
)
25926 = chainon (sel_seq
,
25927 build_tree_list (selector
, NULL_TREE
));
25929 = chainon (sel_seq
,
25930 build_tree_list (NULL_TREE
, NULL_TREE
));
25934 = chainon (sel_seq
,
25935 build_tree_list (selector
, NULL_TREE
));
25937 token
= cp_lexer_peek_token (parser
->lexer
);
25941 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
25943 return objc_build_selector_expr (loc
, sel_seq
);
25946 /* Parse a list of identifiers.
25948 objc-identifier-list:
25950 objc-identifier-list , identifier
25952 Returns a TREE_LIST of identifier nodes. */
25955 cp_parser_objc_identifier_list (cp_parser
* parser
)
25961 identifier
= cp_parser_identifier (parser
);
25962 if (identifier
== error_mark_node
)
25963 return error_mark_node
;
25965 list
= build_tree_list (NULL_TREE
, identifier
);
25966 sep
= cp_lexer_peek_token (parser
->lexer
);
25968 while (sep
->type
== CPP_COMMA
)
25970 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
25971 identifier
= cp_parser_identifier (parser
);
25972 if (identifier
== error_mark_node
)
25975 list
= chainon (list
, build_tree_list (NULL_TREE
,
25977 sep
= cp_lexer_peek_token (parser
->lexer
);
25983 /* Parse an Objective-C alias declaration.
25985 objc-alias-declaration:
25986 @compatibility_alias identifier identifier ;
25988 This function registers the alias mapping with the Objective-C front end.
25989 It returns nothing. */
25992 cp_parser_objc_alias_declaration (cp_parser
* parser
)
25996 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
25997 alias
= cp_parser_identifier (parser
);
25998 orig
= cp_parser_identifier (parser
);
25999 objc_declare_alias (alias
, orig
);
26000 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26003 /* Parse an Objective-C class forward-declaration.
26005 objc-class-declaration:
26006 @class objc-identifier-list ;
26008 The function registers the forward declarations with the Objective-C
26009 front end. It returns nothing. */
26012 cp_parser_objc_class_declaration (cp_parser
* parser
)
26014 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
26019 id
= cp_parser_identifier (parser
);
26020 if (id
== error_mark_node
)
26023 objc_declare_class (id
);
26025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26026 cp_lexer_consume_token (parser
->lexer
);
26030 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26033 /* Parse a list of Objective-C protocol references.
26035 objc-protocol-refs-opt:
26036 objc-protocol-refs [opt]
26038 objc-protocol-refs:
26039 < objc-identifier-list >
26041 Returns a TREE_LIST of identifiers, if any. */
26044 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
26046 tree protorefs
= NULL_TREE
;
26048 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
26050 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
26051 protorefs
= cp_parser_objc_identifier_list (parser
);
26052 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
26058 /* Parse a Objective-C visibility specification. */
26061 cp_parser_objc_visibility_spec (cp_parser
* parser
)
26063 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
26065 switch (vis
->keyword
)
26067 case RID_AT_PRIVATE
:
26068 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
26070 case RID_AT_PROTECTED
:
26071 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
26073 case RID_AT_PUBLIC
:
26074 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
26076 case RID_AT_PACKAGE
:
26077 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
26083 /* Eat '@private'/'@protected'/'@public'. */
26084 cp_lexer_consume_token (parser
->lexer
);
26087 /* Parse an Objective-C method type. Return 'true' if it is a class
26088 (+) method, and 'false' if it is an instance (-) method. */
26091 cp_parser_objc_method_type (cp_parser
* parser
)
26093 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
26099 /* Parse an Objective-C protocol qualifier. */
26102 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
26104 tree quals
= NULL_TREE
, node
;
26105 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26107 node
= token
->u
.value
;
26109 while (node
&& identifier_p (node
)
26110 && (node
== ridpointers
[(int) RID_IN
]
26111 || node
== ridpointers
[(int) RID_OUT
]
26112 || node
== ridpointers
[(int) RID_INOUT
]
26113 || node
== ridpointers
[(int) RID_BYCOPY
]
26114 || node
== ridpointers
[(int) RID_BYREF
]
26115 || node
== ridpointers
[(int) RID_ONEWAY
]))
26117 quals
= tree_cons (NULL_TREE
, node
, quals
);
26118 cp_lexer_consume_token (parser
->lexer
);
26119 token
= cp_lexer_peek_token (parser
->lexer
);
26120 node
= token
->u
.value
;
26126 /* Parse an Objective-C typename. */
26129 cp_parser_objc_typename (cp_parser
* parser
)
26131 tree type_name
= NULL_TREE
;
26133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
26135 tree proto_quals
, cp_type
= NULL_TREE
;
26137 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26138 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
26140 /* An ObjC type name may consist of just protocol qualifiers, in which
26141 case the type shall default to 'id'. */
26142 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26144 cp_type
= cp_parser_type_id (parser
);
26146 /* If the type could not be parsed, an error has already
26147 been produced. For error recovery, behave as if it had
26148 not been specified, which will use the default type
26150 if (cp_type
== error_mark_node
)
26152 cp_type
= NULL_TREE
;
26153 /* We need to skip to the closing parenthesis as
26154 cp_parser_type_id() does not seem to do it for
26156 cp_parser_skip_to_closing_parenthesis (parser
,
26157 /*recovering=*/true,
26158 /*or_comma=*/false,
26159 /*consume_paren=*/false);
26163 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26164 type_name
= build_tree_list (proto_quals
, cp_type
);
26170 /* Check to see if TYPE refers to an Objective-C selector name. */
26173 cp_parser_objc_selector_p (enum cpp_ttype type
)
26175 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
26176 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
26177 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
26178 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
26179 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
26182 /* Parse an Objective-C selector. */
26185 cp_parser_objc_selector (cp_parser
* parser
)
26187 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
26189 if (!cp_parser_objc_selector_p (token
->type
))
26191 error_at (token
->location
, "invalid Objective-C++ selector name");
26192 return error_mark_node
;
26195 /* C++ operator names are allowed to appear in ObjC selectors. */
26196 switch (token
->type
)
26198 case CPP_AND_AND
: return get_identifier ("and");
26199 case CPP_AND_EQ
: return get_identifier ("and_eq");
26200 case CPP_AND
: return get_identifier ("bitand");
26201 case CPP_OR
: return get_identifier ("bitor");
26202 case CPP_COMPL
: return get_identifier ("compl");
26203 case CPP_NOT
: return get_identifier ("not");
26204 case CPP_NOT_EQ
: return get_identifier ("not_eq");
26205 case CPP_OR_OR
: return get_identifier ("or");
26206 case CPP_OR_EQ
: return get_identifier ("or_eq");
26207 case CPP_XOR
: return get_identifier ("xor");
26208 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
26209 default: return token
->u
.value
;
26213 /* Parse an Objective-C params list. */
26216 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
26218 tree params
= NULL_TREE
;
26219 bool maybe_unary_selector_p
= true;
26220 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26222 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
26224 tree selector
= NULL_TREE
, type_name
, identifier
;
26225 tree parm_attr
= NULL_TREE
;
26227 if (token
->keyword
== RID_ATTRIBUTE
)
26230 if (token
->type
!= CPP_COLON
)
26231 selector
= cp_parser_objc_selector (parser
);
26233 /* Detect if we have a unary selector. */
26234 if (maybe_unary_selector_p
26235 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
26237 params
= selector
; /* Might be followed by attributes. */
26241 maybe_unary_selector_p
= false;
26242 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
26244 /* Something went quite wrong. There should be a colon
26245 here, but there is not. Stop parsing parameters. */
26248 type_name
= cp_parser_objc_typename (parser
);
26249 /* New ObjC allows attributes on parameters too. */
26250 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
26251 parm_attr
= cp_parser_attributes_opt (parser
);
26252 identifier
= cp_parser_identifier (parser
);
26256 objc_build_keyword_decl (selector
,
26261 token
= cp_lexer_peek_token (parser
->lexer
);
26264 if (params
== NULL_TREE
)
26266 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26267 return error_mark_node
;
26270 /* We allow tail attributes for the method. */
26271 if (token
->keyword
== RID_ATTRIBUTE
)
26273 *attributes
= cp_parser_attributes_opt (parser
);
26274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26275 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26277 cp_parser_error (parser
,
26278 "method attributes must be specified at the end");
26279 return error_mark_node
;
26282 if (params
== NULL_TREE
)
26284 cp_parser_error (parser
, "objective-c++ method declaration is expected");
26285 return error_mark_node
;
26290 /* Parse the non-keyword Objective-C params. */
26293 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
26296 tree params
= make_node (TREE_LIST
);
26297 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26298 *ellipsisp
= false; /* Initially, assume no ellipsis. */
26300 while (token
->type
== CPP_COMMA
)
26302 cp_parameter_declarator
*parmdecl
;
26305 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26306 token
= cp_lexer_peek_token (parser
->lexer
);
26308 if (token
->type
== CPP_ELLIPSIS
)
26310 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
26312 token
= cp_lexer_peek_token (parser
->lexer
);
26316 /* TODO: parse attributes for tail parameters. */
26317 parmdecl
= cp_parser_parameter_declaration (parser
, false, NULL
);
26318 parm
= grokdeclarator (parmdecl
->declarator
,
26319 &parmdecl
->decl_specifiers
,
26320 PARM
, /*initialized=*/0,
26321 /*attrlist=*/NULL
);
26323 chainon (params
, build_tree_list (NULL_TREE
, parm
));
26324 token
= cp_lexer_peek_token (parser
->lexer
);
26327 /* We allow tail attributes for the method. */
26328 if (token
->keyword
== RID_ATTRIBUTE
)
26330 if (*attributes
== NULL_TREE
)
26332 *attributes
= cp_parser_attributes_opt (parser
);
26333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
26334 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26338 /* We have an error, but parse the attributes, so that we can
26340 *attributes
= cp_parser_attributes_opt (parser
);
26342 cp_parser_error (parser
,
26343 "method attributes must be specified at the end");
26344 return error_mark_node
;
26350 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26353 cp_parser_objc_interstitial_code (cp_parser
* parser
)
26355 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26357 /* If the next token is `extern' and the following token is a string
26358 literal, then we have a linkage specification. */
26359 if (token
->keyword
== RID_EXTERN
26360 && cp_parser_is_pure_string_literal
26361 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
26362 cp_parser_linkage_specification (parser
);
26363 /* Handle #pragma, if any. */
26364 else if (token
->type
== CPP_PRAGMA
)
26365 cp_parser_pragma (parser
, pragma_objc_icode
);
26366 /* Allow stray semicolons. */
26367 else if (token
->type
== CPP_SEMICOLON
)
26368 cp_lexer_consume_token (parser
->lexer
);
26369 /* Mark methods as optional or required, when building protocols. */
26370 else if (token
->keyword
== RID_AT_OPTIONAL
)
26372 cp_lexer_consume_token (parser
->lexer
);
26373 objc_set_method_opt (true);
26375 else if (token
->keyword
== RID_AT_REQUIRED
)
26377 cp_lexer_consume_token (parser
->lexer
);
26378 objc_set_method_opt (false);
26380 else if (token
->keyword
== RID_NAMESPACE
)
26381 cp_parser_namespace_definition (parser
);
26382 /* Other stray characters must generate errors. */
26383 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
26385 cp_lexer_consume_token (parser
->lexer
);
26386 error ("stray %qs between Objective-C++ methods",
26387 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
26389 /* Finally, try to parse a block-declaration, or a function-definition. */
26391 cp_parser_block_declaration (parser
, /*statement_p=*/false);
26394 /* Parse a method signature. */
26397 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
26399 tree rettype
, kwdparms
, optparms
;
26400 bool ellipsis
= false;
26401 bool is_class_method
;
26403 is_class_method
= cp_parser_objc_method_type (parser
);
26404 rettype
= cp_parser_objc_typename (parser
);
26405 *attributes
= NULL_TREE
;
26406 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
26407 if (kwdparms
== error_mark_node
)
26408 return error_mark_node
;
26409 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
26410 if (optparms
== error_mark_node
)
26411 return error_mark_node
;
26413 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
26417 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
26420 cp_lexer_save_tokens (parser
->lexer
);
26421 tattr
= cp_parser_attributes_opt (parser
);
26422 gcc_assert (tattr
) ;
26424 /* If the attributes are followed by a method introducer, this is not allowed.
26425 Dump the attributes and flag the situation. */
26426 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
26427 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
26430 /* Otherwise, the attributes introduce some interstitial code, possibly so
26431 rewind to allow that check. */
26432 cp_lexer_rollback_tokens (parser
->lexer
);
26436 /* Parse an Objective-C method prototype list. */
26439 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
26441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26443 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26445 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26447 tree attributes
, sig
;
26448 bool is_class_method
;
26449 if (token
->type
== CPP_PLUS
)
26450 is_class_method
= true;
26452 is_class_method
= false;
26453 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
26454 if (sig
== error_mark_node
)
26456 cp_parser_skip_to_end_of_block_or_statement (parser
);
26457 token
= cp_lexer_peek_token (parser
->lexer
);
26460 objc_add_method_declaration (is_class_method
, sig
, attributes
);
26461 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26463 else if (token
->keyword
== RID_AT_PROPERTY
)
26464 cp_parser_objc_at_property_declaration (parser
);
26465 else if (token
->keyword
== RID_ATTRIBUTE
26466 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26467 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
26469 "prefix attributes are ignored for methods");
26471 /* Allow for interspersed non-ObjC++ code. */
26472 cp_parser_objc_interstitial_code (parser
);
26474 token
= cp_lexer_peek_token (parser
->lexer
);
26477 if (token
->type
!= CPP_EOF
)
26478 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26480 cp_parser_error (parser
, "expected %<@end%>");
26482 objc_finish_interface ();
26485 /* Parse an Objective-C method definition list. */
26488 cp_parser_objc_method_definition_list (cp_parser
* parser
)
26490 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26492 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26496 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
26499 tree sig
, attribute
;
26500 bool is_class_method
;
26501 if (token
->type
== CPP_PLUS
)
26502 is_class_method
= true;
26504 is_class_method
= false;
26505 push_deferring_access_checks (dk_deferred
);
26506 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
26507 if (sig
== error_mark_node
)
26509 cp_parser_skip_to_end_of_block_or_statement (parser
);
26510 token
= cp_lexer_peek_token (parser
->lexer
);
26513 objc_start_method_definition (is_class_method
, sig
, attribute
,
26516 /* For historical reasons, we accept an optional semicolon. */
26517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26518 cp_lexer_consume_token (parser
->lexer
);
26520 ptk
= cp_lexer_peek_token (parser
->lexer
);
26521 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
26522 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
26524 perform_deferred_access_checks (tf_warning_or_error
);
26525 stop_deferring_access_checks ();
26526 meth
= cp_parser_function_definition_after_declarator (parser
,
26528 pop_deferring_access_checks ();
26529 objc_finish_method_definition (meth
);
26532 /* The following case will be removed once @synthesize is
26533 completely implemented. */
26534 else if (token
->keyword
== RID_AT_PROPERTY
)
26535 cp_parser_objc_at_property_declaration (parser
);
26536 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
26537 cp_parser_objc_at_synthesize_declaration (parser
);
26538 else if (token
->keyword
== RID_AT_DYNAMIC
)
26539 cp_parser_objc_at_dynamic_declaration (parser
);
26540 else if (token
->keyword
== RID_ATTRIBUTE
26541 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
26542 warning_at (token
->location
, OPT_Wattributes
,
26543 "prefix attributes are ignored for methods");
26545 /* Allow for interspersed non-ObjC++ code. */
26546 cp_parser_objc_interstitial_code (parser
);
26548 token
= cp_lexer_peek_token (parser
->lexer
);
26551 if (token
->type
!= CPP_EOF
)
26552 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26554 cp_parser_error (parser
, "expected %<@end%>");
26556 objc_finish_implementation ();
26559 /* Parse Objective-C ivars. */
26562 cp_parser_objc_class_ivars (cp_parser
* parser
)
26564 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26566 if (token
->type
!= CPP_OPEN_BRACE
)
26567 return; /* No ivars specified. */
26569 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
26570 token
= cp_lexer_peek_token (parser
->lexer
);
26572 while (token
->type
!= CPP_CLOSE_BRACE
26573 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26575 cp_decl_specifier_seq declspecs
;
26576 int decl_class_or_enum_p
;
26577 tree prefix_attributes
;
26579 cp_parser_objc_visibility_spec (parser
);
26581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
26584 cp_parser_decl_specifier_seq (parser
,
26585 CP_PARSER_FLAGS_OPTIONAL
,
26587 &decl_class_or_enum_p
);
26589 /* auto, register, static, extern, mutable. */
26590 if (declspecs
.storage_class
!= sc_none
)
26592 cp_parser_error (parser
, "invalid type for instance variable");
26593 declspecs
.storage_class
= sc_none
;
26596 /* thread_local. */
26597 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
26599 cp_parser_error (parser
, "invalid type for instance variable");
26600 declspecs
.locations
[ds_thread
] = 0;
26604 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
26606 cp_parser_error (parser
, "invalid type for instance variable");
26607 declspecs
.locations
[ds_typedef
] = 0;
26610 prefix_attributes
= declspecs
.attributes
;
26611 declspecs
.attributes
= NULL_TREE
;
26613 /* Keep going until we hit the `;' at the end of the
26615 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
26617 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
26618 cp_declarator
*declarator
= NULL
;
26619 int ctor_dtor_or_conv_p
;
26621 /* Check for a (possibly unnamed) bitfield declaration. */
26622 token
= cp_lexer_peek_token (parser
->lexer
);
26623 if (token
->type
== CPP_COLON
)
26626 if (token
->type
== CPP_NAME
26627 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
26630 /* Get the name of the bitfield. */
26631 declarator
= make_id_declarator (NULL_TREE
,
26632 cp_parser_identifier (parser
),
26636 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26637 /* Get the width of the bitfield. */
26639 = cp_parser_constant_expression (parser
);
26643 /* Parse the declarator. */
26645 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26646 &ctor_dtor_or_conv_p
,
26647 /*parenthesized_p=*/NULL
,
26648 /*member_p=*/false,
26649 /*friend_p=*/false);
26652 /* Look for attributes that apply to the ivar. */
26653 attributes
= cp_parser_attributes_opt (parser
);
26654 /* Remember which attributes are prefix attributes and
26656 first_attribute
= attributes
;
26657 /* Combine the attributes. */
26658 attributes
= chainon (prefix_attributes
, attributes
);
26661 /* Create the bitfield declaration. */
26662 decl
= grokbitfield (declarator
, &declspecs
,
26666 decl
= grokfield (declarator
, &declspecs
,
26667 NULL_TREE
, /*init_const_expr_p=*/false,
26668 NULL_TREE
, attributes
);
26670 /* Add the instance variable. */
26671 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
26672 objc_add_instance_variable (decl
);
26674 /* Reset PREFIX_ATTRIBUTES. */
26675 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26676 attributes
= TREE_CHAIN (attributes
);
26678 TREE_CHAIN (attributes
) = NULL_TREE
;
26680 token
= cp_lexer_peek_token (parser
->lexer
);
26682 if (token
->type
== CPP_COMMA
)
26684 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
26690 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26691 token
= cp_lexer_peek_token (parser
->lexer
);
26694 if (token
->keyword
== RID_AT_END
)
26695 cp_parser_error (parser
, "expected %<}%>");
26697 /* Do not consume the RID_AT_END, so it will be read again as terminating
26698 the @interface of @implementation. */
26699 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
26700 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
26702 /* For historical reasons, we accept an optional semicolon. */
26703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26704 cp_lexer_consume_token (parser
->lexer
);
26707 /* Parse an Objective-C protocol declaration. */
26710 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
26712 tree proto
, protorefs
;
26715 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
26716 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
26718 tok
= cp_lexer_peek_token (parser
->lexer
);
26719 error_at (tok
->location
, "identifier expected after %<@protocol%>");
26720 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26724 /* See if we have a forward declaration or a definition. */
26725 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26727 /* Try a forward declaration first. */
26728 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
26734 id
= cp_parser_identifier (parser
);
26735 if (id
== error_mark_node
)
26738 objc_declare_protocol (id
, attributes
);
26740 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26741 cp_lexer_consume_token (parser
->lexer
);
26745 cp_parser_consume_semicolon_at_end_of_statement (parser
);
26748 /* Ok, we got a full-fledged definition (or at least should). */
26751 proto
= cp_parser_identifier (parser
);
26752 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
26753 objc_start_protocol (proto
, protorefs
, attributes
);
26754 cp_parser_objc_method_prototype_list (parser
);
26758 /* Parse an Objective-C superclass or category. */
26761 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
26764 tree
*categ
, bool *is_class_extension
)
26766 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
26768 *super
= *categ
= NULL_TREE
;
26769 *is_class_extension
= false;
26770 if (next
->type
== CPP_COLON
)
26772 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
26773 *super
= cp_parser_identifier (parser
);
26775 else if (next
->type
== CPP_OPEN_PAREN
)
26777 cp_lexer_consume_token (parser
->lexer
); /* Eat '('. */
26779 /* If there is no category name, and this is an @interface, we
26780 have a class extension. */
26781 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26783 *categ
= NULL_TREE
;
26784 *is_class_extension
= true;
26787 *categ
= cp_parser_identifier (parser
);
26789 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
26793 /* Parse an Objective-C class interface. */
26796 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
26798 tree name
, super
, categ
, protos
;
26799 bool is_class_extension
;
26801 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
26802 name
= cp_parser_identifier (parser
);
26803 if (name
== error_mark_node
)
26805 /* It's hard to recover because even if valid @interface stuff
26806 is to follow, we can't compile it (or validate it) if we
26807 don't even know which class it refers to. Let's assume this
26808 was a stray '@interface' token in the stream and skip it.
26812 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
26813 &is_class_extension
);
26814 protos
= cp_parser_objc_protocol_refs_opt (parser
);
26816 /* We have either a class or a category on our hands. */
26817 if (categ
|| is_class_extension
)
26818 objc_start_category_interface (name
, categ
, protos
, attributes
);
26821 objc_start_class_interface (name
, super
, protos
, attributes
);
26822 /* Handle instance variable declarations, if any. */
26823 cp_parser_objc_class_ivars (parser
);
26824 objc_continue_interface ();
26827 cp_parser_objc_method_prototype_list (parser
);
26830 /* Parse an Objective-C class implementation. */
26833 cp_parser_objc_class_implementation (cp_parser
* parser
)
26835 tree name
, super
, categ
;
26836 bool is_class_extension
;
26838 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
26839 name
= cp_parser_identifier (parser
);
26840 if (name
== error_mark_node
)
26842 /* It's hard to recover because even if valid @implementation
26843 stuff is to follow, we can't compile it (or validate it) if
26844 we don't even know which class it refers to. Let's assume
26845 this was a stray '@implementation' token in the stream and
26850 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
26851 &is_class_extension
);
26853 /* We have either a class or a category on our hands. */
26855 objc_start_category_implementation (name
, categ
);
26858 objc_start_class_implementation (name
, super
);
26859 /* Handle instance variable declarations, if any. */
26860 cp_parser_objc_class_ivars (parser
);
26861 objc_continue_implementation ();
26864 cp_parser_objc_method_definition_list (parser
);
26867 /* Consume the @end token and finish off the implementation. */
26870 cp_parser_objc_end_implementation (cp_parser
* parser
)
26872 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
26873 objc_finish_implementation ();
26876 /* Parse an Objective-C declaration. */
26879 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
26881 /* Try to figure out what kind of declaration is present. */
26882 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
26885 switch (kwd
->keyword
)
26890 error_at (kwd
->location
, "attributes may not be specified before"
26891 " the %<@%D%> Objective-C++ keyword",
26895 case RID_AT_IMPLEMENTATION
:
26896 warning_at (kwd
->location
, OPT_Wattributes
,
26897 "prefix attributes are ignored before %<@%D%>",
26904 switch (kwd
->keyword
)
26907 cp_parser_objc_alias_declaration (parser
);
26910 cp_parser_objc_class_declaration (parser
);
26912 case RID_AT_PROTOCOL
:
26913 cp_parser_objc_protocol_declaration (parser
, attributes
);
26915 case RID_AT_INTERFACE
:
26916 cp_parser_objc_class_interface (parser
, attributes
);
26918 case RID_AT_IMPLEMENTATION
:
26919 cp_parser_objc_class_implementation (parser
);
26922 cp_parser_objc_end_implementation (parser
);
26925 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
26927 cp_parser_skip_to_end_of_block_or_statement (parser
);
26931 /* Parse an Objective-C try-catch-finally statement.
26933 objc-try-catch-finally-stmt:
26934 @try compound-statement objc-catch-clause-seq [opt]
26935 objc-finally-clause [opt]
26937 objc-catch-clause-seq:
26938 objc-catch-clause objc-catch-clause-seq [opt]
26941 @catch ( objc-exception-declaration ) compound-statement
26943 objc-finally-clause:
26944 @finally compound-statement
26946 objc-exception-declaration:
26947 parameter-declaration
26950 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26954 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26955 for C. Keep them in sync. */
26958 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
26960 location_t location
;
26963 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
26964 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
26965 objc_maybe_warn_exceptions (location
);
26966 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26967 node, lest it get absorbed into the surrounding block. */
26968 stmt
= push_stmt_list ();
26969 cp_parser_compound_statement (parser
, NULL
, false, false);
26970 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
26972 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
26974 cp_parameter_declarator
*parm
;
26975 tree parameter_declaration
= error_mark_node
;
26976 bool seen_open_paren
= false;
26978 cp_lexer_consume_token (parser
->lexer
);
26979 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
26980 seen_open_paren
= true;
26981 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26983 /* We have "@catch (...)" (where the '...' are literally
26984 what is in the code). Skip the '...'.
26985 parameter_declaration is set to NULL_TREE, and
26986 objc_being_catch_clauses() knows that that means
26988 cp_lexer_consume_token (parser
->lexer
);
26989 parameter_declaration
= NULL_TREE
;
26993 /* We have "@catch (NSException *exception)" or something
26994 like that. Parse the parameter declaration. */
26995 parm
= cp_parser_parameter_declaration (parser
, false, NULL
);
26997 parameter_declaration
= error_mark_node
;
26999 parameter_declaration
= grokdeclarator (parm
->declarator
,
27000 &parm
->decl_specifiers
,
27001 PARM
, /*initialized=*/0,
27002 /*attrlist=*/NULL
);
27004 if (seen_open_paren
)
27005 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27008 /* If there was no open parenthesis, we are recovering from
27009 an error, and we are trying to figure out what mistake
27010 the user has made. */
27012 /* If there is an immediate closing parenthesis, the user
27013 probably forgot the opening one (ie, they typed "@catch
27014 NSException *e)". Parse the closing parenthesis and keep
27016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
27017 cp_lexer_consume_token (parser
->lexer
);
27019 /* If these is no immediate closing parenthesis, the user
27020 probably doesn't know that parenthesis are required at
27021 all (ie, they typed "@catch NSException *e"). So, just
27022 forget about the closing parenthesis and keep going. */
27024 objc_begin_catch_clause (parameter_declaration
);
27025 cp_parser_compound_statement (parser
, NULL
, false, false);
27026 objc_finish_catch_clause ();
27028 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
27030 cp_lexer_consume_token (parser
->lexer
);
27031 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27032 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27033 node, lest it get absorbed into the surrounding block. */
27034 stmt
= push_stmt_list ();
27035 cp_parser_compound_statement (parser
, NULL
, false, false);
27036 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
27039 return objc_finish_try_stmt ();
27042 /* Parse an Objective-C synchronized statement.
27044 objc-synchronized-stmt:
27045 @synchronized ( expression ) compound-statement
27047 Returns NULL_TREE. */
27050 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
27052 location_t location
;
27055 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
27057 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
27058 objc_maybe_warn_exceptions (location
);
27059 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
27060 lock
= cp_parser_expression (parser
);
27061 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
27063 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27064 node, lest it get absorbed into the surrounding block. */
27065 stmt
= push_stmt_list ();
27066 cp_parser_compound_statement (parser
, NULL
, false, false);
27068 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
27071 /* Parse an Objective-C throw statement.
27074 @throw assignment-expression [opt] ;
27076 Returns a constructed '@throw' statement. */
27079 cp_parser_objc_throw_statement (cp_parser
*parser
)
27081 tree expr
= NULL_TREE
;
27082 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27084 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
27086 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27087 expr
= cp_parser_expression (parser
);
27089 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27091 return objc_build_throw_stmt (loc
, expr
);
27094 /* Parse an Objective-C statement. */
27097 cp_parser_objc_statement (cp_parser
* parser
)
27099 /* Try to figure out what kind of declaration is present. */
27100 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
27102 switch (kwd
->keyword
)
27105 return cp_parser_objc_try_catch_finally_statement (parser
);
27106 case RID_AT_SYNCHRONIZED
:
27107 return cp_parser_objc_synchronized_statement (parser
);
27109 return cp_parser_objc_throw_statement (parser
);
27111 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
27113 cp_parser_skip_to_end_of_block_or_statement (parser
);
27116 return error_mark_node
;
27119 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27120 look ahead to see if an objc keyword follows the attributes. This
27121 is to detect the use of prefix attributes on ObjC @interface and
27125 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
27127 cp_lexer_save_tokens (parser
->lexer
);
27128 *attrib
= cp_parser_attributes_opt (parser
);
27129 gcc_assert (*attrib
);
27130 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
27132 cp_lexer_commit_tokens (parser
->lexer
);
27135 cp_lexer_rollback_tokens (parser
->lexer
);
27139 /* This routine is a minimal replacement for
27140 c_parser_struct_declaration () used when parsing the list of
27141 types/names or ObjC++ properties. For example, when parsing the
27144 @property (readonly) int a, b, c;
27146 this function is responsible for parsing "int a, int b, int c" and
27147 returning the declarations as CHAIN of DECLs.
27149 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27150 similar parsing. */
27152 cp_parser_objc_struct_declaration (cp_parser
*parser
)
27154 tree decls
= NULL_TREE
;
27155 cp_decl_specifier_seq declspecs
;
27156 int decl_class_or_enum_p
;
27157 tree prefix_attributes
;
27159 cp_parser_decl_specifier_seq (parser
,
27160 CP_PARSER_FLAGS_NONE
,
27162 &decl_class_or_enum_p
);
27164 if (declspecs
.type
== error_mark_node
)
27165 return error_mark_node
;
27167 /* auto, register, static, extern, mutable. */
27168 if (declspecs
.storage_class
!= sc_none
)
27170 cp_parser_error (parser
, "invalid type for property");
27171 declspecs
.storage_class
= sc_none
;
27174 /* thread_local. */
27175 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
27177 cp_parser_error (parser
, "invalid type for property");
27178 declspecs
.locations
[ds_thread
] = 0;
27182 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
27184 cp_parser_error (parser
, "invalid type for property");
27185 declspecs
.locations
[ds_typedef
] = 0;
27188 prefix_attributes
= declspecs
.attributes
;
27189 declspecs
.attributes
= NULL_TREE
;
27191 /* Keep going until we hit the `;' at the end of the declaration. */
27192 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
27194 tree attributes
, first_attribute
, decl
;
27195 cp_declarator
*declarator
;
27198 /* Parse the declarator. */
27199 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
27200 NULL
, NULL
, false, false);
27202 /* Look for attributes that apply to the ivar. */
27203 attributes
= cp_parser_attributes_opt (parser
);
27204 /* Remember which attributes are prefix attributes and
27206 first_attribute
= attributes
;
27207 /* Combine the attributes. */
27208 attributes
= chainon (prefix_attributes
, attributes
);
27210 decl
= grokfield (declarator
, &declspecs
,
27211 NULL_TREE
, /*init_const_expr_p=*/false,
27212 NULL_TREE
, attributes
);
27214 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
27215 return error_mark_node
;
27217 /* Reset PREFIX_ATTRIBUTES. */
27218 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
27219 attributes
= TREE_CHAIN (attributes
);
27221 TREE_CHAIN (attributes
) = NULL_TREE
;
27223 DECL_CHAIN (decl
) = decls
;
27226 token
= cp_lexer_peek_token (parser
->lexer
);
27227 if (token
->type
== CPP_COMMA
)
27229 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
27238 /* Parse an Objective-C @property declaration. The syntax is:
27240 objc-property-declaration:
27241 '@property' objc-property-attributes[opt] struct-declaration ;
27243 objc-property-attributes:
27244 '(' objc-property-attribute-list ')'
27246 objc-property-attribute-list:
27247 objc-property-attribute
27248 objc-property-attribute-list, objc-property-attribute
27250 objc-property-attribute
27251 'getter' = identifier
27252 'setter' = identifier
27261 @property NSString *name;
27262 @property (readonly) id object;
27263 @property (retain, nonatomic, getter=getTheName) id name;
27264 @property int a, b, c;
27266 PS: This function is identical to
27267 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27269 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
27271 /* The following variables hold the attributes of the properties as
27272 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27273 seen. When we see an attribute, we set them to 'true' (if they
27274 are boolean properties) or to the identifier (if they have an
27275 argument, ie, for getter and setter). Note that here we only
27276 parse the list of attributes, check the syntax and accumulate the
27277 attributes that we find. objc_add_property_declaration() will
27278 then process the information. */
27279 bool property_assign
= false;
27280 bool property_copy
= false;
27281 tree property_getter_ident
= NULL_TREE
;
27282 bool property_nonatomic
= false;
27283 bool property_readonly
= false;
27284 bool property_readwrite
= false;
27285 bool property_retain
= false;
27286 tree property_setter_ident
= NULL_TREE
;
27288 /* 'properties' is the list of properties that we read. Usually a
27289 single one, but maybe more (eg, in "@property int a, b, c;" there
27294 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27296 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
27298 /* Parse the optional attribute list... */
27299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27302 cp_lexer_consume_token (parser
->lexer
);
27306 bool syntax_error
= false;
27307 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27310 if (token
->type
!= CPP_NAME
)
27312 cp_parser_error (parser
, "expected identifier");
27315 keyword
= C_RID_CODE (token
->u
.value
);
27316 cp_lexer_consume_token (parser
->lexer
);
27319 case RID_ASSIGN
: property_assign
= true; break;
27320 case RID_COPY
: property_copy
= true; break;
27321 case RID_NONATOMIC
: property_nonatomic
= true; break;
27322 case RID_READONLY
: property_readonly
= true; break;
27323 case RID_READWRITE
: property_readwrite
= true; break;
27324 case RID_RETAIN
: property_retain
= true; break;
27328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
27330 if (keyword
== RID_GETTER
)
27331 cp_parser_error (parser
,
27332 "missing %<=%> (after %<getter%> attribute)");
27334 cp_parser_error (parser
,
27335 "missing %<=%> (after %<setter%> attribute)");
27336 syntax_error
= true;
27339 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
27340 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
27342 cp_parser_error (parser
, "expected identifier");
27343 syntax_error
= true;
27346 if (keyword
== RID_SETTER
)
27348 if (property_setter_ident
!= NULL_TREE
)
27350 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
27351 cp_lexer_consume_token (parser
->lexer
);
27354 property_setter_ident
= cp_parser_objc_selector (parser
);
27355 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
27356 cp_parser_error (parser
, "setter name must terminate with %<:%>");
27358 cp_lexer_consume_token (parser
->lexer
);
27362 if (property_getter_ident
!= NULL_TREE
)
27364 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
27365 cp_lexer_consume_token (parser
->lexer
);
27368 property_getter_ident
= cp_parser_objc_selector (parser
);
27372 cp_parser_error (parser
, "unknown property attribute");
27373 syntax_error
= true;
27380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27381 cp_lexer_consume_token (parser
->lexer
);
27386 /* FIXME: "@property (setter, assign);" will generate a spurious
27387 "error: expected ‘)’ before ‘,’ token". This is because
27388 cp_parser_require, unlike the C counterpart, will produce an
27389 error even if we are in error recovery. */
27390 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27392 cp_parser_skip_to_closing_parenthesis (parser
,
27393 /*recovering=*/true,
27394 /*or_comma=*/false,
27395 /*consume_paren=*/true);
27399 /* ... and the property declaration(s). */
27400 properties
= cp_parser_objc_struct_declaration (parser
);
27402 if (properties
== error_mark_node
)
27404 cp_parser_skip_to_end_of_statement (parser
);
27405 /* If the next token is now a `;', consume it. */
27406 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27407 cp_lexer_consume_token (parser
->lexer
);
27411 if (properties
== NULL_TREE
)
27412 cp_parser_error (parser
, "expected identifier");
27415 /* Comma-separated properties are chained together in
27416 reverse order; add them one by one. */
27417 properties
= nreverse (properties
);
27419 for (; properties
; properties
= TREE_CHAIN (properties
))
27420 objc_add_property_declaration (loc
, copy_node (properties
),
27421 property_readonly
, property_readwrite
,
27422 property_assign
, property_retain
,
27423 property_copy
, property_nonatomic
,
27424 property_getter_ident
, property_setter_ident
);
27427 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27430 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27432 objc-synthesize-declaration:
27433 @synthesize objc-synthesize-identifier-list ;
27435 objc-synthesize-identifier-list:
27436 objc-synthesize-identifier
27437 objc-synthesize-identifier-list, objc-synthesize-identifier
27439 objc-synthesize-identifier
27441 identifier = identifier
27444 @synthesize MyProperty;
27445 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27447 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27448 for C. Keep them in sync.
27451 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
27453 tree list
= NULL_TREE
;
27455 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27457 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
27460 tree property
, ivar
;
27461 property
= cp_parser_identifier (parser
);
27462 if (property
== error_mark_node
)
27464 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27467 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
27469 cp_lexer_consume_token (parser
->lexer
);
27470 ivar
= cp_parser_identifier (parser
);
27471 if (ivar
== error_mark_node
)
27473 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27479 list
= chainon (list
, build_tree_list (ivar
, property
));
27480 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27481 cp_lexer_consume_token (parser
->lexer
);
27485 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27486 objc_add_synthesize_declaration (loc
, list
);
27489 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27491 objc-dynamic-declaration:
27492 @dynamic identifier-list ;
27495 @dynamic MyProperty;
27496 @dynamic MyProperty, AnotherProperty;
27498 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27499 for C. Keep them in sync.
27502 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
27504 tree list
= NULL_TREE
;
27506 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27508 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
27512 property
= cp_parser_identifier (parser
);
27513 if (property
== error_mark_node
)
27515 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27518 list
= chainon (list
, build_tree_list (NULL
, property
));
27519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
27520 cp_lexer_consume_token (parser
->lexer
);
27524 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27525 objc_add_dynamic_declaration (loc
, list
);
27529 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27531 /* Returns name of the next clause.
27532 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27533 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27534 returned and the token is consumed. */
27536 static pragma_omp_clause
27537 cp_parser_omp_clause_name (cp_parser
*parser
)
27539 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
27541 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
27542 result
= PRAGMA_OMP_CLAUSE_IF
;
27543 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
27544 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
27545 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
27546 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
27547 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
27548 result
= PRAGMA_OMP_CLAUSE_FOR
;
27549 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27551 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27552 const char *p
= IDENTIFIER_POINTER (id
);
27557 if (!strcmp ("aligned", p
))
27558 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
27561 if (!strcmp ("collapse", p
))
27562 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
27563 else if (!strcmp ("copyin", p
))
27564 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
27565 else if (!strcmp ("copyprivate", p
))
27566 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
27569 if (!strcmp ("depend", p
))
27570 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
27571 else if (!strcmp ("device", p
))
27572 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
27573 else if (!strcmp ("dist_schedule", p
))
27574 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
27577 if (!strcmp ("final", p
))
27578 result
= PRAGMA_OMP_CLAUSE_FINAL
;
27579 else if (!strcmp ("firstprivate", p
))
27580 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
27581 else if (!strcmp ("from", p
))
27582 result
= PRAGMA_OMP_CLAUSE_FROM
;
27585 if (!strcmp ("inbranch", p
))
27586 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
27589 if (!strcmp ("lastprivate", p
))
27590 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
27591 else if (!strcmp ("linear", p
))
27592 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
27595 if (!strcmp ("map", p
))
27596 result
= PRAGMA_OMP_CLAUSE_MAP
;
27597 else if (!strcmp ("mergeable", p
))
27598 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
27599 else if (flag_cilkplus
&& !strcmp ("mask", p
))
27600 result
= PRAGMA_CILK_CLAUSE_MASK
;
27603 if (!strcmp ("notinbranch", p
))
27604 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
27605 else if (!strcmp ("nowait", p
))
27606 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
27607 else if (flag_cilkplus
&& !strcmp ("nomask", p
))
27608 result
= PRAGMA_CILK_CLAUSE_NOMASK
;
27609 else if (!strcmp ("num_teams", p
))
27610 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
27611 else if (!strcmp ("num_threads", p
))
27612 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
27615 if (!strcmp ("ordered", p
))
27616 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
27619 if (!strcmp ("parallel", p
))
27620 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
27621 else if (!strcmp ("proc_bind", p
))
27622 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
27625 if (!strcmp ("reduction", p
))
27626 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
27629 if (!strcmp ("safelen", p
))
27630 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
27631 else if (!strcmp ("schedule", p
))
27632 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
27633 else if (!strcmp ("sections", p
))
27634 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
27635 else if (!strcmp ("shared", p
))
27636 result
= PRAGMA_OMP_CLAUSE_SHARED
;
27637 else if (!strcmp ("simdlen", p
))
27638 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
27641 if (!strcmp ("taskgroup", p
))
27642 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
27643 else if (!strcmp ("thread_limit", p
))
27644 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
27645 else if (!strcmp ("to", p
))
27646 result
= PRAGMA_OMP_CLAUSE_TO
;
27649 if (!strcmp ("uniform", p
))
27650 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
27651 else if (!strcmp ("untied", p
))
27652 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
27655 if (flag_cilkplus
&& !strcmp ("vectorlength", p
))
27656 result
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
27661 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
27662 cp_lexer_consume_token (parser
->lexer
);
27667 /* Validate that a clause of the given type does not already exist. */
27670 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
27671 const char *name
, location_t location
)
27675 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
27676 if (OMP_CLAUSE_CODE (c
) == code
)
27678 error_at (location
, "too many %qs clauses", name
);
27686 variable-list , identifier
27688 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27689 colon). An opening parenthesis will have been consumed by the caller.
27691 If KIND is nonzero, create the appropriate node and install the decl
27692 in OMP_CLAUSE_DECL and add the node to the head of the list.
27694 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27695 return the list created.
27697 COLON can be NULL if only closing parenthesis should end the list,
27698 or pointer to bool which will receive false if the list is terminated
27699 by closing parenthesis or true if the list is terminated by colon. */
27702 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
27703 tree list
, bool *colon
)
27706 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
27709 parser
->colon_corrects_to_scope_p
= false;
27716 token
= cp_lexer_peek_token (parser
->lexer
);
27717 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
27718 /*check_dependency_p=*/true,
27719 /*template_p=*/NULL
,
27720 /*declarator_p=*/false,
27721 /*optional_p=*/false);
27722 if (name
== error_mark_node
)
27725 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
27726 if (decl
== error_mark_node
)
27727 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
27729 else if (kind
!= 0)
27733 case OMP_CLAUSE_MAP
:
27734 case OMP_CLAUSE_FROM
:
27735 case OMP_CLAUSE_TO
:
27736 case OMP_CLAUSE_DEPEND
:
27737 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27739 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
27741 parser
->colon_corrects_to_scope_p
= false;
27742 cp_lexer_consume_token (parser
->lexer
);
27743 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27744 low_bound
= cp_parser_expression (parser
);
27746 parser
->colon_corrects_to_scope_p
27747 = saved_colon_corrects_to_scope_p
;
27748 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
27749 length
= integer_one_node
;
27752 /* Look for `:'. */
27753 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
27755 if (!cp_lexer_next_token_is (parser
->lexer
,
27757 length
= cp_parser_expression (parser
);
27759 /* Look for the closing `]'. */
27760 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
27763 decl
= tree_cons (low_bound
, length
, decl
);
27770 tree u
= build_omp_clause (token
->location
, kind
);
27771 OMP_CLAUSE_DECL (u
) = decl
;
27772 OMP_CLAUSE_CHAIN (u
) = list
;
27776 list
= tree_cons (decl
, NULL_TREE
, list
);
27779 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27781 cp_lexer_consume_token (parser
->lexer
);
27785 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27787 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
27790 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
27794 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27798 /* Try to resync to an unnested comma. Copied from
27799 cp_parser_parenthesized_expression_list. */
27802 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
27803 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
27804 /*recovering=*/true,
27806 /*consume_paren=*/true);
27814 /* Similarly, but expect leading and trailing parenthesis. This is a very
27815 common case for omp clauses. */
27818 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
27820 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27821 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
27826 collapse ( constant-expression ) */
27829 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
27835 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27836 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27839 num
= cp_parser_constant_expression (parser
);
27841 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27842 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27843 /*or_comma=*/false,
27844 /*consume_paren=*/true);
27846 if (num
== error_mark_node
)
27848 num
= fold_non_dependent_expr (num
);
27849 if (!INTEGRAL_TYPE_P (TREE_TYPE (num
))
27850 || !tree_fits_shwi_p (num
)
27851 || (n
= tree_to_shwi (num
)) <= 0
27854 error_at (loc
, "collapse argument needs positive constant integer expression");
27858 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
27859 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
27860 OMP_CLAUSE_CHAIN (c
) = list
;
27861 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
27867 default ( shared | none ) */
27870 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
, location_t location
)
27872 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
27875 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
27879 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
27880 const char *p
= IDENTIFIER_POINTER (id
);
27885 if (strcmp ("none", p
) != 0)
27887 kind
= OMP_CLAUSE_DEFAULT_NONE
;
27891 if (strcmp ("shared", p
) != 0)
27893 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
27900 cp_lexer_consume_token (parser
->lexer
);
27905 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
27908 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27909 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27910 /*or_comma=*/false,
27911 /*consume_paren=*/true);
27913 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
27916 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
27917 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
27918 OMP_CLAUSE_CHAIN (c
) = list
;
27919 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
27925 final ( expression ) */
27928 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
27932 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27935 t
= cp_parser_condition (parser
);
27937 if (t
== error_mark_node
27938 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27939 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27940 /*or_comma=*/false,
27941 /*consume_paren=*/true);
27943 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
27945 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
27946 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
27947 OMP_CLAUSE_CHAIN (c
) = list
;
27953 if ( expression ) */
27956 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
)
27960 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
27963 t
= cp_parser_condition (parser
);
27965 if (t
== error_mark_node
27966 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
27967 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
27968 /*or_comma=*/false,
27969 /*consume_paren=*/true);
27971 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if", location
);
27973 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
27974 OMP_CLAUSE_IF_EXPR (c
) = t
;
27975 OMP_CLAUSE_CHAIN (c
) = list
;
27984 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
27985 tree list
, location_t location
)
27989 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
27992 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
27993 OMP_CLAUSE_CHAIN (c
) = list
;
28001 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
28002 tree list
, location_t location
)
28006 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
28008 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
28009 OMP_CLAUSE_CHAIN (c
) = list
;
28014 num_threads ( expression ) */
28017 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
28018 location_t location
)
28022 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28025 t
= cp_parser_expression (parser
);
28027 if (t
== error_mark_node
28028 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28029 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28030 /*or_comma=*/false,
28031 /*consume_paren=*/true);
28033 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
28034 "num_threads", location
);
28036 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
28037 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
28038 OMP_CLAUSE_CHAIN (c
) = list
;
28047 cp_parser_omp_clause_ordered (cp_parser
* /*parser*/,
28048 tree list
, location_t location
)
28052 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
28053 "ordered", location
);
28055 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
28056 OMP_CLAUSE_CHAIN (c
) = list
;
28061 reduction ( reduction-operator : variable-list )
28063 reduction-operator:
28064 One of: + * - & ^ | && ||
28068 reduction-operator:
28069 One of: + * - & ^ | && || min max
28073 reduction-operator:
28074 One of: + * - & ^ | && ||
28078 cp_parser_omp_clause_reduction (cp_parser
*parser
, tree list
)
28080 enum tree_code code
= ERROR_MARK
;
28081 tree nlist
, c
, id
= NULL_TREE
;
28083 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28086 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
28088 case CPP_PLUS
: code
= PLUS_EXPR
; break;
28089 case CPP_MULT
: code
= MULT_EXPR
; break;
28090 case CPP_MINUS
: code
= MINUS_EXPR
; break;
28091 case CPP_AND
: code
= BIT_AND_EXPR
; break;
28092 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
28093 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
28094 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
28095 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
28099 if (code
!= ERROR_MARK
)
28100 cp_lexer_consume_token (parser
->lexer
);
28103 bool saved_colon_corrects_to_scope_p
;
28104 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
28105 parser
->colon_corrects_to_scope_p
= false;
28106 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
28107 /*check_dependency_p=*/true,
28108 /*template_p=*/NULL
,
28109 /*declarator_p=*/false,
28110 /*optional_p=*/false);
28111 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
28112 if (identifier_p (id
))
28114 const char *p
= IDENTIFIER_POINTER (id
);
28116 if (strcmp (p
, "min") == 0)
28118 else if (strcmp (p
, "max") == 0)
28120 else if (id
== ansi_opname (PLUS_EXPR
))
28122 else if (id
== ansi_opname (MULT_EXPR
))
28124 else if (id
== ansi_opname (MINUS_EXPR
))
28126 else if (id
== ansi_opname (BIT_AND_EXPR
))
28127 code
= BIT_AND_EXPR
;
28128 else if (id
== ansi_opname (BIT_IOR_EXPR
))
28129 code
= BIT_IOR_EXPR
;
28130 else if (id
== ansi_opname (BIT_XOR_EXPR
))
28131 code
= BIT_XOR_EXPR
;
28132 else if (id
== ansi_opname (TRUTH_ANDIF_EXPR
))
28133 code
= TRUTH_ANDIF_EXPR
;
28134 else if (id
== ansi_opname (TRUTH_ORIF_EXPR
))
28135 code
= TRUTH_ORIF_EXPR
;
28136 id
= omp_reduction_id (code
, id
, NULL_TREE
);
28137 tree scope
= parser
->scope
;
28139 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
28140 parser
->scope
= NULL_TREE
;
28141 parser
->qualifying_scope
= NULL_TREE
;
28142 parser
->object_scope
= NULL_TREE
;
28146 error ("invalid reduction-identifier");
28148 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28149 /*or_comma=*/false,
28150 /*consume_paren=*/true);
28155 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28158 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_REDUCTION
, list
,
28160 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28162 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
28163 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
28170 schedule ( schedule-kind )
28171 schedule ( schedule-kind , expression )
28174 static | dynamic | guided | runtime | auto */
28177 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
28181 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28184 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
28186 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28188 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28189 const char *p
= IDENTIFIER_POINTER (id
);
28194 if (strcmp ("dynamic", p
) != 0)
28196 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
28200 if (strcmp ("guided", p
) != 0)
28202 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
28206 if (strcmp ("runtime", p
) != 0)
28208 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
28215 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28216 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
28217 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
28218 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
28221 cp_lexer_consume_token (parser
->lexer
);
28223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28226 cp_lexer_consume_token (parser
->lexer
);
28228 token
= cp_lexer_peek_token (parser
->lexer
);
28229 t
= cp_parser_assignment_expression (parser
);
28231 if (t
== error_mark_node
)
28233 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
28234 error_at (token
->location
, "schedule %<runtime%> does not take "
28235 "a %<chunk_size%> parameter");
28236 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
28237 error_at (token
->location
, "schedule %<auto%> does not take "
28238 "a %<chunk_size%> parameter");
28240 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
28242 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28245 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28248 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
28249 OMP_CLAUSE_CHAIN (c
) = list
;
28253 cp_parser_error (parser
, "invalid schedule kind");
28255 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28256 /*or_comma=*/false,
28257 /*consume_paren=*/true);
28265 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
28266 tree list
, location_t location
)
28270 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
28272 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
28273 OMP_CLAUSE_CHAIN (c
) = list
;
28282 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
28283 tree list
, location_t location
)
28285 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
28286 tree c
= build_omp_clause (location
, code
);
28287 OMP_CLAUSE_CHAIN (c
) = list
;
28298 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
28299 enum omp_clause_code code
,
28300 tree list
, location_t location
)
28302 tree c
= build_omp_clause (location
, code
);
28303 OMP_CLAUSE_CHAIN (c
) = list
;
28308 num_teams ( expression ) */
28311 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
28312 location_t location
)
28316 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28319 t
= cp_parser_expression (parser
);
28321 if (t
== error_mark_node
28322 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28323 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28324 /*or_comma=*/false,
28325 /*consume_paren=*/true);
28327 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
28328 "num_teams", location
);
28330 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
28331 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
28332 OMP_CLAUSE_CHAIN (c
) = list
;
28338 thread_limit ( expression ) */
28341 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
28342 location_t location
)
28346 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28349 t
= cp_parser_expression (parser
);
28351 if (t
== error_mark_node
28352 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28353 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28354 /*or_comma=*/false,
28355 /*consume_paren=*/true);
28357 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
28358 "thread_limit", location
);
28360 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
28361 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
28362 OMP_CLAUSE_CHAIN (c
) = list
;
28368 aligned ( variable-list )
28369 aligned ( variable-list : constant-expression ) */
28372 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
28374 tree nlist
, c
, alignment
= NULL_TREE
;
28377 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28380 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
28385 alignment
= cp_parser_constant_expression (parser
);
28387 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28388 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28389 /*or_comma=*/false,
28390 /*consume_paren=*/true);
28392 if (alignment
== error_mark_node
)
28393 alignment
= NULL_TREE
;
28396 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28397 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
28403 linear ( variable-list )
28404 linear ( variable-list : expression ) */
28407 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
28408 bool is_cilk_simd_fn
)
28410 tree nlist
, c
, step
= integer_one_node
;
28413 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28416 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
28421 step
= cp_parser_expression (parser
);
28423 if (is_cilk_simd_fn
&& TREE_CODE (step
) == PARM_DECL
)
28425 sorry ("using parameters for %<linear%> step is not supported yet");
28426 step
= integer_one_node
;
28428 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28429 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28430 /*or_comma=*/false,
28431 /*consume_paren=*/true);
28433 if (step
== error_mark_node
)
28437 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28438 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
28444 safelen ( constant-expression ) */
28447 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
28448 location_t location
)
28452 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28455 t
= cp_parser_constant_expression (parser
);
28457 if (t
== error_mark_node
28458 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28459 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28460 /*or_comma=*/false,
28461 /*consume_paren=*/true);
28463 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
28465 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
28466 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
28467 OMP_CLAUSE_CHAIN (c
) = list
;
28473 simdlen ( constant-expression ) */
28476 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
28477 location_t location
)
28481 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28484 t
= cp_parser_constant_expression (parser
);
28486 if (t
== error_mark_node
28487 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28488 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28489 /*or_comma=*/false,
28490 /*consume_paren=*/true);
28492 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
28494 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
28495 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
28496 OMP_CLAUSE_CHAIN (c
) = list
;
28502 depend ( depend-kind : variable-list )
28505 in | out | inout */
28508 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
)
28511 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_INOUT
;
28513 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28518 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28519 const char *p
= IDENTIFIER_POINTER (id
);
28521 if (strcmp ("in", p
) == 0)
28522 kind
= OMP_CLAUSE_DEPEND_IN
;
28523 else if (strcmp ("inout", p
) == 0)
28524 kind
= OMP_CLAUSE_DEPEND_INOUT
;
28525 else if (strcmp ("out", p
) == 0)
28526 kind
= OMP_CLAUSE_DEPEND_OUT
;
28533 cp_lexer_consume_token (parser
->lexer
);
28534 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
28537 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
, list
,
28540 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28541 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
28546 cp_parser_error (parser
, "invalid depend kind");
28548 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28549 /*or_comma=*/false,
28550 /*consume_paren=*/true);
28555 map ( map-kind : variable-list )
28556 map ( variable-list )
28559 alloc | to | from | tofrom */
28562 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
28565 enum omp_clause_map_kind kind
= OMP_CLAUSE_MAP_TOFROM
;
28567 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28570 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
28571 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
28573 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28574 const char *p
= IDENTIFIER_POINTER (id
);
28576 if (strcmp ("alloc", p
) == 0)
28577 kind
= OMP_CLAUSE_MAP_ALLOC
;
28578 else if (strcmp ("to", p
) == 0)
28579 kind
= OMP_CLAUSE_MAP_TO
;
28580 else if (strcmp ("from", p
) == 0)
28581 kind
= OMP_CLAUSE_MAP_FROM
;
28582 else if (strcmp ("tofrom", p
) == 0)
28583 kind
= OMP_CLAUSE_MAP_TOFROM
;
28586 cp_parser_error (parser
, "invalid map kind");
28587 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28588 /*or_comma=*/false,
28589 /*consume_paren=*/true);
28592 cp_lexer_consume_token (parser
->lexer
);
28593 cp_lexer_consume_token (parser
->lexer
);
28596 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
28599 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
28600 OMP_CLAUSE_MAP_KIND (c
) = kind
;
28606 device ( expression ) */
28609 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
28610 location_t location
)
28614 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28617 t
= cp_parser_expression (parser
);
28619 if (t
== error_mark_node
28620 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28621 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28622 /*or_comma=*/false,
28623 /*consume_paren=*/true);
28625 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
28626 "device", location
);
28628 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
28629 OMP_CLAUSE_DEVICE_ID (c
) = t
;
28630 OMP_CLAUSE_CHAIN (c
) = list
;
28636 dist_schedule ( static )
28637 dist_schedule ( static , expression ) */
28640 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
28641 location_t location
)
28645 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28648 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
28650 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
28652 cp_lexer_consume_token (parser
->lexer
);
28654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28656 cp_lexer_consume_token (parser
->lexer
);
28658 t
= cp_parser_assignment_expression (parser
);
28660 if (t
== error_mark_node
)
28662 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
28664 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
28667 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28670 check_no_duplicate_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
, "dist_schedule",
28672 OMP_CLAUSE_CHAIN (c
) = list
;
28676 cp_parser_error (parser
, "invalid dist_schedule kind");
28678 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28679 /*or_comma=*/false,
28680 /*consume_paren=*/true);
28685 proc_bind ( proc-bind-kind )
28688 master | close | spread */
28691 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
28692 location_t location
)
28695 enum omp_clause_proc_bind_kind kind
;
28697 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28700 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
28702 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
28703 const char *p
= IDENTIFIER_POINTER (id
);
28705 if (strcmp ("master", p
) == 0)
28706 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
28707 else if (strcmp ("close", p
) == 0)
28708 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
28709 else if (strcmp ("spread", p
) == 0)
28710 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
28717 cp_lexer_consume_token (parser
->lexer
);
28718 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
28721 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
28722 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
28724 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
28725 OMP_CLAUSE_CHAIN (c
) = list
;
28729 cp_parser_error (parser
, "invalid depend kind");
28731 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
28732 /*or_comma=*/false,
28733 /*consume_paren=*/true);
28737 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28738 is a bitmask in MASK. Return the list of clauses found; the result
28739 of clause default goes in *pdefault. */
28742 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
28743 const char *where
, cp_token
*pragma_tok
,
28744 bool finish_p
= true)
28746 tree clauses
= NULL
;
28748 cp_token
*token
= NULL
;
28749 bool cilk_simd_fn
= false;
28751 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
28753 pragma_omp_clause c_kind
;
28754 const char *c_name
;
28755 tree prev
= clauses
;
28757 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
28758 cp_lexer_consume_token (parser
->lexer
);
28760 token
= cp_lexer_peek_token (parser
->lexer
);
28761 c_kind
= cp_parser_omp_clause_name (parser
);
28765 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
28766 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
28768 c_name
= "collapse";
28770 case PRAGMA_OMP_CLAUSE_COPYIN
:
28771 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
28774 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
28775 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
28777 c_name
= "copyprivate";
28779 case PRAGMA_OMP_CLAUSE_DEFAULT
:
28780 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
28782 c_name
= "default";
28784 case PRAGMA_OMP_CLAUSE_FINAL
:
28785 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
28788 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
28789 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
28791 c_name
= "firstprivate";
28793 case PRAGMA_OMP_CLAUSE_IF
:
28794 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
);
28797 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
28798 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
28800 c_name
= "lastprivate";
28802 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
28803 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
28805 c_name
= "mergeable";
28807 case PRAGMA_OMP_CLAUSE_NOWAIT
:
28808 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
, token
->location
);
28811 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
28812 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
28814 c_name
= "num_threads";
28816 case PRAGMA_OMP_CLAUSE_ORDERED
:
28817 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
28819 c_name
= "ordered";
28821 case PRAGMA_OMP_CLAUSE_PRIVATE
:
28822 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
28824 c_name
= "private";
28826 case PRAGMA_OMP_CLAUSE_REDUCTION
:
28827 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
28828 c_name
= "reduction";
28830 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
28831 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
28833 c_name
= "schedule";
28835 case PRAGMA_OMP_CLAUSE_SHARED
:
28836 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
28840 case PRAGMA_OMP_CLAUSE_UNTIED
:
28841 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
28845 case PRAGMA_OMP_CLAUSE_INBRANCH
:
28846 case PRAGMA_CILK_CLAUSE_MASK
:
28847 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
28848 clauses
, token
->location
);
28849 c_name
= "inbranch";
28851 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
28852 case PRAGMA_CILK_CLAUSE_NOMASK
:
28853 clauses
= cp_parser_omp_clause_branch (parser
,
28854 OMP_CLAUSE_NOTINBRANCH
,
28855 clauses
, token
->location
);
28856 c_name
= "notinbranch";
28858 case PRAGMA_OMP_CLAUSE_PARALLEL
:
28859 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
28860 clauses
, token
->location
);
28861 c_name
= "parallel";
28865 error_at (token
->location
, "%qs must be the first clause of %qs",
28870 case PRAGMA_OMP_CLAUSE_FOR
:
28871 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
28872 clauses
, token
->location
);
28875 goto clause_not_first
;
28877 case PRAGMA_OMP_CLAUSE_SECTIONS
:
28878 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
28879 clauses
, token
->location
);
28880 c_name
= "sections";
28882 goto clause_not_first
;
28884 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
28885 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
28886 clauses
, token
->location
);
28887 c_name
= "taskgroup";
28889 goto clause_not_first
;
28891 case PRAGMA_OMP_CLAUSE_TO
:
28892 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
,
28896 case PRAGMA_OMP_CLAUSE_FROM
:
28897 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
,
28901 case PRAGMA_OMP_CLAUSE_UNIFORM
:
28902 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
28904 c_name
= "uniform";
28906 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
28907 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
28909 c_name
= "num_teams";
28911 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
28912 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
28914 c_name
= "thread_limit";
28916 case PRAGMA_OMP_CLAUSE_ALIGNED
:
28917 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
28918 c_name
= "aligned";
28920 case PRAGMA_OMP_CLAUSE_LINEAR
:
28921 if (((mask
>> PRAGMA_CILK_CLAUSE_VECTORLENGTH
) & 1) != 0)
28922 cilk_simd_fn
= true;
28923 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, cilk_simd_fn
);
28926 case PRAGMA_OMP_CLAUSE_DEPEND
:
28927 clauses
= cp_parser_omp_clause_depend (parser
, clauses
);
28930 case PRAGMA_OMP_CLAUSE_MAP
:
28931 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
28934 case PRAGMA_OMP_CLAUSE_DEVICE
:
28935 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
28939 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
28940 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
28942 c_name
= "dist_schedule";
28944 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
28945 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
28947 c_name
= "proc_bind";
28949 case PRAGMA_OMP_CLAUSE_SAFELEN
:
28950 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
28952 c_name
= "safelen";
28954 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
28955 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
28957 c_name
= "simdlen";
28959 case PRAGMA_CILK_CLAUSE_VECTORLENGTH
:
28960 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, true);
28961 c_name
= "simdlen";
28964 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
28970 if (((mask
>> c_kind
) & 1) == 0)
28972 /* Remove the invalid clause(s) from the list to avoid
28973 confusing the rest of the compiler. */
28975 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
28979 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28980 no reason to skip to the end. */
28981 if (!(flag_cilkplus
&& pragma_tok
== NULL
))
28982 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
28984 return finish_omp_clauses (clauses
);
28992 In practice, we're also interested in adding the statement to an
28993 outer node. So it is convenient if we work around the fact that
28994 cp_parser_statement calls add_stmt. */
28997 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
28999 unsigned save
= parser
->in_statement
;
29001 /* Only move the values to IN_OMP_BLOCK if they weren't false.
29002 This preserves the "not within loop or switch" style error messages
29003 for nonsense cases like
29009 if (parser
->in_statement
)
29010 parser
->in_statement
= IN_OMP_BLOCK
;
29016 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
29018 parser
->in_statement
= save
;
29022 cp_parser_omp_structured_block (cp_parser
*parser
)
29024 tree stmt
= begin_omp_structured_block ();
29025 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
29027 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
29029 cp_parser_end_omp_structured_block (parser
, save
);
29030 return finish_omp_structured_block (stmt
);
29034 # pragma omp atomic new-line
29038 x binop= expr | x++ | ++x | x-- | --x
29040 +, *, -, /, &, ^, |, <<, >>
29042 where x is an lvalue expression with scalar type.
29045 # pragma omp atomic new-line
29048 # pragma omp atomic read new-line
29051 # pragma omp atomic write new-line
29054 # pragma omp atomic update new-line
29057 # pragma omp atomic capture new-line
29060 # pragma omp atomic capture new-line
29068 expression-stmt | x = x binop expr
29070 v = expression-stmt
29072 { v = x; update-stmt; } | { update-stmt; v = x; }
29076 expression-stmt | x = x binop expr | x = expr binop x
29080 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29082 where x and v are lvalue expressions with scalar type. */
29085 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
29087 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
29088 tree rhs1
= NULL_TREE
, orig_lhs
;
29089 enum tree_code code
= OMP_ATOMIC
, opcode
= NOP_EXPR
;
29090 bool structured_block
= false;
29091 bool seq_cst
= false;
29093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29095 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29096 const char *p
= IDENTIFIER_POINTER (id
);
29098 if (!strcmp (p
, "seq_cst"))
29101 cp_lexer_consume_token (parser
->lexer
);
29102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29103 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29104 cp_lexer_consume_token (parser
->lexer
);
29107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29109 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29110 const char *p
= IDENTIFIER_POINTER (id
);
29112 if (!strcmp (p
, "read"))
29113 code
= OMP_ATOMIC_READ
;
29114 else if (!strcmp (p
, "write"))
29116 else if (!strcmp (p
, "update"))
29118 else if (!strcmp (p
, "capture"))
29119 code
= OMP_ATOMIC_CAPTURE_NEW
;
29123 cp_lexer_consume_token (parser
->lexer
);
29127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
29128 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
)
29129 cp_lexer_consume_token (parser
->lexer
);
29131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
29133 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
29134 const char *p
= IDENTIFIER_POINTER (id
);
29136 if (!strcmp (p
, "seq_cst"))
29139 cp_lexer_consume_token (parser
->lexer
);
29143 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29147 case OMP_ATOMIC_READ
:
29148 case NOP_EXPR
: /* atomic write */
29149 v
= cp_parser_unary_expression (parser
);
29150 if (v
== error_mark_node
)
29152 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29154 if (code
== NOP_EXPR
)
29155 lhs
= cp_parser_expression (parser
);
29157 lhs
= cp_parser_unary_expression (parser
);
29158 if (lhs
== error_mark_node
)
29160 if (code
== NOP_EXPR
)
29162 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29170 case OMP_ATOMIC_CAPTURE_NEW
:
29171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29173 cp_lexer_consume_token (parser
->lexer
);
29174 structured_block
= true;
29178 v
= cp_parser_unary_expression (parser
);
29179 if (v
== error_mark_node
)
29181 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29189 lhs
= cp_parser_unary_expression (parser
);
29191 switch (TREE_CODE (lhs
))
29196 case POSTINCREMENT_EXPR
:
29197 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29198 code
= OMP_ATOMIC_CAPTURE_OLD
;
29200 case PREINCREMENT_EXPR
:
29201 lhs
= TREE_OPERAND (lhs
, 0);
29202 opcode
= PLUS_EXPR
;
29203 rhs
= integer_one_node
;
29206 case POSTDECREMENT_EXPR
:
29207 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
29208 code
= OMP_ATOMIC_CAPTURE_OLD
;
29210 case PREDECREMENT_EXPR
:
29211 lhs
= TREE_OPERAND (lhs
, 0);
29212 opcode
= MINUS_EXPR
;
29213 rhs
= integer_one_node
;
29216 case COMPOUND_EXPR
:
29217 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
29218 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
29219 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
29220 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
29221 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29222 (TREE_OPERAND (lhs
, 1), 0), 0)))
29224 /* Undo effects of boolean_increment for post {in,de}crement. */
29225 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
29228 if (TREE_CODE (lhs
) == MODIFY_EXPR
29229 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
29231 /* Undo effects of boolean_increment. */
29232 if (integer_onep (TREE_OPERAND (lhs
, 1)))
29234 /* This is pre or post increment. */
29235 rhs
= TREE_OPERAND (lhs
, 1);
29236 lhs
= TREE_OPERAND (lhs
, 0);
29238 if (code
== OMP_ATOMIC_CAPTURE_NEW
29239 && !structured_block
29240 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
29241 code
= OMP_ATOMIC_CAPTURE_OLD
;
29247 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
29250 opcode
= MULT_EXPR
;
29253 opcode
= TRUNC_DIV_EXPR
;
29256 opcode
= PLUS_EXPR
;
29259 opcode
= MINUS_EXPR
;
29261 case CPP_LSHIFT_EQ
:
29262 opcode
= LSHIFT_EXPR
;
29264 case CPP_RSHIFT_EQ
:
29265 opcode
= RSHIFT_EXPR
;
29268 opcode
= BIT_AND_EXPR
;
29271 opcode
= BIT_IOR_EXPR
;
29274 opcode
= BIT_XOR_EXPR
;
29277 enum cp_parser_prec oprec
;
29279 cp_lexer_consume_token (parser
->lexer
);
29280 cp_parser_parse_tentatively (parser
);
29281 rhs1
= cp_parser_simple_cast_expression (parser
);
29282 if (rhs1
== error_mark_node
)
29284 cp_parser_abort_tentative_parse (parser
);
29285 cp_parser_simple_cast_expression (parser
);
29288 token
= cp_lexer_peek_token (parser
->lexer
);
29289 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
29291 cp_parser_abort_tentative_parse (parser
);
29292 cp_parser_parse_tentatively (parser
);
29293 rhs
= cp_parser_binary_expression (parser
, false, true,
29294 PREC_NOT_OPERATOR
, NULL
);
29295 if (rhs
== error_mark_node
)
29297 cp_parser_abort_tentative_parse (parser
);
29298 cp_parser_binary_expression (parser
, false, true,
29299 PREC_NOT_OPERATOR
, NULL
);
29302 switch (TREE_CODE (rhs
))
29305 case TRUNC_DIV_EXPR
:
29313 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
29315 if (cp_parser_parse_definitely (parser
))
29317 opcode
= TREE_CODE (rhs
);
29318 rhs1
= TREE_OPERAND (rhs
, 0);
29319 rhs
= TREE_OPERAND (rhs
, 1);
29329 cp_parser_abort_tentative_parse (parser
);
29330 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
29332 rhs
= cp_parser_expression (parser
);
29333 if (rhs
== error_mark_node
)
29339 cp_parser_error (parser
,
29340 "invalid form of %<#pragma omp atomic%>");
29343 if (!cp_parser_parse_definitely (parser
))
29345 switch (token
->type
)
29347 case CPP_SEMICOLON
:
29348 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29350 code
= OMP_ATOMIC_CAPTURE_OLD
;
29355 cp_lexer_consume_token (parser
->lexer
);
29358 else if (structured_block
)
29365 cp_parser_error (parser
,
29366 "invalid form of %<#pragma omp atomic%>");
29369 opcode
= MULT_EXPR
;
29372 opcode
= TRUNC_DIV_EXPR
;
29375 opcode
= PLUS_EXPR
;
29378 opcode
= MINUS_EXPR
;
29381 opcode
= LSHIFT_EXPR
;
29384 opcode
= RSHIFT_EXPR
;
29387 opcode
= BIT_AND_EXPR
;
29390 opcode
= BIT_IOR_EXPR
;
29393 opcode
= BIT_XOR_EXPR
;
29396 cp_parser_error (parser
,
29397 "invalid operator for %<#pragma omp atomic%>");
29400 oprec
= TOKEN_PRECEDENCE (token
);
29401 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
29402 if (commutative_tree_code (opcode
))
29403 oprec
= (enum cp_parser_prec
) (oprec
- 1);
29404 cp_lexer_consume_token (parser
->lexer
);
29405 rhs
= cp_parser_binary_expression (parser
, false, false,
29407 if (rhs
== error_mark_node
)
29412 cp_parser_error (parser
,
29413 "invalid operator for %<#pragma omp atomic%>");
29416 cp_lexer_consume_token (parser
->lexer
);
29418 rhs
= cp_parser_expression (parser
);
29419 if (rhs
== error_mark_node
)
29424 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
29426 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
29428 v
= cp_parser_unary_expression (parser
);
29429 if (v
== error_mark_node
)
29431 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
29433 lhs1
= cp_parser_unary_expression (parser
);
29434 if (lhs1
== error_mark_node
)
29437 if (structured_block
)
29439 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29440 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
29443 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
, seq_cst
);
29444 if (!structured_block
)
29445 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29449 cp_parser_skip_to_end_of_block_or_statement (parser
);
29450 if (structured_block
)
29452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29453 cp_lexer_consume_token (parser
->lexer
);
29454 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
29456 cp_parser_skip_to_end_of_block_or_statement (parser
);
29457 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
29458 cp_lexer_consume_token (parser
->lexer
);
29465 # pragma omp barrier new-line */
29468 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
29470 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29471 finish_omp_barrier ();
29475 # pragma omp critical [(name)] new-line
29476 structured-block */
29479 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
)
29481 tree stmt
, name
= NULL
;
29483 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29485 cp_lexer_consume_token (parser
->lexer
);
29487 name
= cp_parser_identifier (parser
);
29489 if (name
== error_mark_node
29490 || !cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
29491 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
29492 /*or_comma=*/false,
29493 /*consume_paren=*/true);
29494 if (name
== error_mark_node
)
29497 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29499 stmt
= cp_parser_omp_structured_block (parser
);
29500 return c_finish_omp_critical (input_location
, stmt
, name
);
29504 # pragma omp flush flush-vars[opt] new-line
29507 ( variable-list ) */
29510 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
29512 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29513 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
29514 cp_parser_require_pragma_eol (parser
, pragma_tok
);
29516 finish_omp_flush ();
29519 /* Helper function, to parse omp for increment expression. */
29522 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
29524 tree cond
= cp_parser_binary_expression (parser
, false, true,
29525 PREC_NOT_OPERATOR
, NULL
);
29526 if (cond
== error_mark_node
29527 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29529 cp_parser_skip_to_end_of_statement (parser
);
29530 return error_mark_node
;
29533 switch (TREE_CODE (cond
))
29541 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
29543 /* Fall through: OpenMP disallows NE_EXPR. */
29545 return error_mark_node
;
29548 /* If decl is an iterator, preserve LHS and RHS of the relational
29549 expr until finish_omp_for. */
29551 && (type_dependent_expression_p (decl
)
29552 || CLASS_TYPE_P (TREE_TYPE (decl
))))
29555 return build_x_binary_op (input_location
, TREE_CODE (cond
),
29556 TREE_OPERAND (cond
, 0), ERROR_MARK
,
29557 TREE_OPERAND (cond
, 1), ERROR_MARK
,
29558 /*overload=*/NULL
, tf_warning_or_error
);
29561 /* Helper function, to parse omp for increment expression. */
29564 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
29566 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29572 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29574 op
= (token
->type
== CPP_PLUS_PLUS
29575 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
29576 cp_lexer_consume_token (parser
->lexer
);
29577 lhs
= cp_parser_simple_cast_expression (parser
);
29579 return error_mark_node
;
29580 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29583 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
29585 return error_mark_node
;
29587 token
= cp_lexer_peek_token (parser
->lexer
);
29588 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
29590 op
= (token
->type
== CPP_PLUS_PLUS
29591 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
29592 cp_lexer_consume_token (parser
->lexer
);
29593 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
29596 op
= cp_parser_assignment_operator_opt (parser
);
29597 if (op
== ERROR_MARK
)
29598 return error_mark_node
;
29600 if (op
!= NOP_EXPR
)
29602 rhs
= cp_parser_assignment_expression (parser
);
29603 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
29604 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29607 lhs
= cp_parser_binary_expression (parser
, false, false,
29608 PREC_ADDITIVE_EXPRESSION
, NULL
);
29609 token
= cp_lexer_peek_token (parser
->lexer
);
29610 decl_first
= lhs
== decl
;
29613 if (token
->type
!= CPP_PLUS
29614 && token
->type
!= CPP_MINUS
)
29615 return error_mark_node
;
29619 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
29620 cp_lexer_consume_token (parser
->lexer
);
29621 rhs
= cp_parser_binary_expression (parser
, false, false,
29622 PREC_ADDITIVE_EXPRESSION
, NULL
);
29623 token
= cp_lexer_peek_token (parser
->lexer
);
29624 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
29626 if (lhs
== NULL_TREE
)
29628 if (op
== PLUS_EXPR
)
29631 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
29632 tf_warning_or_error
);
29635 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
29636 ERROR_MARK
, NULL
, tf_warning_or_error
);
29639 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
29643 if (rhs
!= decl
|| op
== MINUS_EXPR
)
29644 return error_mark_node
;
29645 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
29648 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
29650 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
29653 /* Parse the initialization statement of either an OpenMP for loop or
29654 a Cilk Plus for loop.
29656 Return true if the resulting construct should have an
29657 OMP_CLAUSE_PRIVATE added to it. */
29660 cp_parser_omp_for_loop_init (cp_parser
*parser
,
29661 enum tree_code code
,
29662 tree
&this_pre_body
,
29663 vec
<tree
, va_gc
> *for_block
,
29668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
29671 bool add_private_clause
= false;
29673 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29677 integer-type var = lb
29678 random-access-iterator-type var = lb
29679 pointer-type var = lb
29681 cp_decl_specifier_seq type_specifiers
;
29683 /* First, try to parse as an initialized declaration. See
29684 cp_parser_condition, from whence the bulk of this is copied. */
29686 cp_parser_parse_tentatively (parser
);
29687 cp_parser_type_specifier_seq (parser
, /*is_declaration=*/true,
29688 /*is_trailing_return=*/false,
29690 if (cp_parser_parse_definitely (parser
))
29692 /* If parsing a type specifier seq succeeded, then this
29693 MUST be a initialized declaration. */
29694 tree asm_specification
, attributes
;
29695 cp_declarator
*declarator
;
29697 declarator
= cp_parser_declarator (parser
,
29698 CP_PARSER_DECLARATOR_NAMED
,
29699 /*ctor_dtor_or_conv_p=*/NULL
,
29700 /*parenthesized_p=*/NULL
,
29701 /*member_p=*/false,
29702 /*friend_p=*/false);
29703 attributes
= cp_parser_attributes_opt (parser
);
29704 asm_specification
= cp_parser_asm_specification_opt (parser
);
29706 if (declarator
== cp_error_declarator
)
29707 cp_parser_skip_to_end_of_statement (parser
);
29711 tree pushed_scope
, auto_node
;
29713 decl
= start_decl (declarator
, &type_specifiers
,
29714 SD_INITIALIZED
, attributes
,
29715 /*prefix_attributes=*/NULL_TREE
,
29718 auto_node
= type_uses_auto (TREE_TYPE (decl
));
29719 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
29721 if (cp_lexer_next_token_is (parser
->lexer
,
29724 if (code
!= CILK_SIMD
&& code
!= CILK_FOR
)
29725 error ("parenthesized initialization is not allowed in "
29726 "OpenMP %<for%> loop");
29728 error ("parenthesized initialization is "
29729 "not allowed in for-loop");
29732 /* Trigger an error. */
29733 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29735 init
= error_mark_node
;
29736 cp_parser_skip_to_end_of_statement (parser
);
29738 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
29739 || type_dependent_expression_p (decl
)
29742 bool is_direct_init
, is_non_constant_init
;
29744 init
= cp_parser_initializer (parser
,
29746 &is_non_constant_init
);
29751 = do_auto_deduction (TREE_TYPE (decl
), init
,
29754 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
29755 && !type_dependent_expression_p (decl
))
29759 cp_finish_decl (decl
, init
, !is_non_constant_init
,
29761 LOOKUP_ONLYCONVERTING
);
29762 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
29764 vec_safe_push (for_block
, this_pre_body
);
29768 init
= pop_stmt_list (this_pre_body
);
29769 this_pre_body
= NULL_TREE
;
29774 cp_lexer_consume_token (parser
->lexer
);
29775 init
= cp_parser_assignment_expression (parser
);
29778 if (TREE_CODE (TREE_TYPE (decl
)) == REFERENCE_TYPE
)
29779 init
= error_mark_node
;
29781 cp_finish_decl (decl
, NULL_TREE
,
29782 /*init_const_expr_p=*/false,
29784 LOOKUP_ONLYCONVERTING
);
29788 pop_scope (pushed_scope
);
29794 /* If parsing a type specifier sequence failed, then
29795 this MUST be a simple expression. */
29796 if (code
== CILK_FOR
)
29797 error ("%<_Cilk_for%> allows expression instead of declaration only "
29798 "in C, not in C++");
29799 cp_parser_parse_tentatively (parser
);
29800 decl
= cp_parser_primary_expression (parser
, false, false,
29802 if (!cp_parser_error_occurred (parser
)
29805 && CLASS_TYPE_P (TREE_TYPE (decl
)))
29809 cp_parser_parse_definitely (parser
);
29810 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
29811 rhs
= cp_parser_assignment_expression (parser
);
29812 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
29815 tf_warning_or_error
));
29816 add_private_clause
= true;
29821 cp_parser_abort_tentative_parse (parser
);
29822 init
= cp_parser_expression (parser
);
29825 if (TREE_CODE (init
) == MODIFY_EXPR
29826 || TREE_CODE (init
) == MODOP_EXPR
)
29827 real_decl
= TREE_OPERAND (init
, 0);
29831 return add_private_clause
;
29834 /* Parse the restricted form of the for statement allowed by OpenMP. */
29837 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
29840 tree init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
29841 tree real_decl
, initv
, condv
, incrv
, declv
;
29842 tree this_pre_body
, cl
;
29843 location_t loc_first
;
29844 bool collapse_err
= false;
29845 int i
, collapse
= 1, nbraces
= 0;
29846 vec
<tree
, va_gc
> *for_block
= make_tree_vector ();
29848 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
29849 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
29850 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
29852 gcc_assert (collapse
>= 1);
29854 declv
= make_tree_vec (collapse
);
29855 initv
= make_tree_vec (collapse
);
29856 condv
= make_tree_vec (collapse
);
29857 incrv
= make_tree_vec (collapse
);
29859 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
29861 for (i
= 0; i
< collapse
; i
++)
29863 int bracecount
= 0;
29864 bool add_private_clause
= false;
29867 if (code
!= CILK_FOR
29868 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
29870 cp_parser_error (parser
, "for statement expected");
29873 if (code
== CILK_FOR
29874 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
29876 cp_parser_error (parser
, "_Cilk_for statement expected");
29879 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
29881 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29884 init
= decl
= real_decl
= NULL
;
29885 this_pre_body
= push_stmt_list ();
29888 |= cp_parser_omp_for_loop_init (parser
, code
,
29889 this_pre_body
, for_block
,
29890 init
, decl
, real_decl
);
29892 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29895 this_pre_body
= pop_stmt_list (this_pre_body
);
29899 pre_body
= push_stmt_list ();
29901 add_stmt (this_pre_body
);
29902 pre_body
= pop_stmt_list (pre_body
);
29905 pre_body
= this_pre_body
;
29910 if (cclauses
!= NULL
29911 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
29912 && real_decl
!= NULL_TREE
)
29915 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
29916 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
29917 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29919 error_at (loc
, "iteration variable %qD"
29920 " should not be firstprivate", real_decl
);
29921 *c
= OMP_CLAUSE_CHAIN (*c
);
29923 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
29924 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29926 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29927 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29928 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
29929 OMP_CLAUSE_DECL (l
) = real_decl
;
29930 CP_OMP_CLAUSE_INFO (l
) = CP_OMP_CLAUSE_INFO (*c
);
29931 if (code
== OMP_SIMD
)
29933 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
29934 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
29938 OMP_CLAUSE_CHAIN (l
) = clauses
;
29941 OMP_CLAUSE_SET_CODE (*c
, OMP_CLAUSE_SHARED
);
29942 CP_OMP_CLAUSE_INFO (*c
) = NULL
;
29943 add_private_clause
= false;
29947 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
29948 && OMP_CLAUSE_DECL (*c
) == real_decl
)
29949 add_private_clause
= false;
29950 c
= &OMP_CLAUSE_CHAIN (*c
);
29954 if (add_private_clause
)
29957 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
29959 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
29960 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
29961 && OMP_CLAUSE_DECL (c
) == decl
)
29963 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
29964 && OMP_CLAUSE_DECL (c
) == decl
)
29965 error_at (loc
, "iteration variable %qD "
29966 "should not be firstprivate",
29968 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
29969 && OMP_CLAUSE_DECL (c
) == decl
)
29970 error_at (loc
, "iteration variable %qD should not be reduction",
29975 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
29976 OMP_CLAUSE_DECL (c
) = decl
;
29977 c
= finish_omp_clauses (c
);
29980 OMP_CLAUSE_CHAIN (c
) = clauses
;
29987 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
29988 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
29989 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
29992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
29994 /* If decl is an iterator, preserve the operator on decl
29995 until finish_omp_for. */
29997 && ((processing_template_decl
29998 && !POINTER_TYPE_P (TREE_TYPE (real_decl
)))
29999 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
30000 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
30002 incr
= cp_parser_expression (parser
);
30003 if (CAN_HAVE_LOCATION_P (incr
) && !EXPR_HAS_LOCATION (incr
))
30004 SET_EXPR_LOCATION (incr
, input_location
);
30007 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
30008 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
30009 /*or_comma=*/false,
30010 /*consume_paren=*/true);
30012 TREE_VEC_ELT (declv
, i
) = decl
;
30013 TREE_VEC_ELT (initv
, i
) = init
;
30014 TREE_VEC_ELT (condv
, i
) = cond
;
30015 TREE_VEC_ELT (incrv
, i
) = incr
;
30017 if (i
== collapse
- 1)
30020 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30021 in between the collapsed for loops to be still considered perfectly
30022 nested. Hopefully the final version clarifies this.
30023 For now handle (multiple) {'s and empty statements. */
30024 cp_parser_parse_tentatively (parser
);
30027 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30029 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30031 cp_lexer_consume_token (parser
->lexer
);
30034 else if (bracecount
30035 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30036 cp_lexer_consume_token (parser
->lexer
);
30039 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30040 error_at (loc
, "not enough collapsed for loops");
30041 collapse_err
= true;
30042 cp_parser_abort_tentative_parse (parser
);
30051 cp_parser_parse_definitely (parser
);
30052 nbraces
+= bracecount
;
30056 /* Note that we saved the original contents of this flag when we entered
30057 the structured block, and so we don't need to re-save it here. */
30058 if (code
== CILK_SIMD
|| code
== CILK_FOR
)
30059 parser
->in_statement
= IN_CILK_SIMD_FOR
;
30061 parser
->in_statement
= IN_OMP_FOR
;
30063 /* Note that the grammar doesn't call for a structured block here,
30064 though the loop as a whole is a structured block. */
30065 body
= push_stmt_list ();
30066 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30067 body
= pop_stmt_list (body
);
30069 if (declv
== NULL_TREE
)
30072 ret
= finish_omp_for (loc_first
, code
, declv
, initv
, condv
, incrv
, body
,
30073 pre_body
, clauses
);
30077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
30079 cp_lexer_consume_token (parser
->lexer
);
30082 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
30083 cp_lexer_consume_token (parser
->lexer
);
30088 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30089 "collapsed loops not perfectly nested");
30091 collapse_err
= true;
30092 cp_parser_statement_seq_opt (parser
, NULL
);
30093 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30098 while (!for_block
->is_empty ())
30099 add_stmt (pop_stmt_list (for_block
->pop ()));
30100 release_tree_vector (for_block
);
30105 /* Helper function for OpenMP parsing, split clauses and call
30106 finish_omp_clauses on each of the set of clauses afterwards. */
30109 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
30110 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
30113 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
30114 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
30116 cclauses
[i
] = finish_omp_clauses (cclauses
[i
]);
30120 #pragma omp simd simd-clause[optseq] new-line
30123 #define OMP_SIMD_CLAUSE_MASK \
30124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30133 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30134 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30136 tree clauses
, sb
, ret
;
30138 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30140 strcat (p_name
, " simd");
30141 mask
|= OMP_SIMD_CLAUSE_MASK
;
30142 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
30144 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30148 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
30149 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
30152 sb
= begin_omp_structured_block ();
30153 save
= cp_parser_begin_omp_structured_block (parser
);
30155 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
);
30157 cp_parser_end_omp_structured_block (parser
, save
);
30158 add_stmt (finish_omp_structured_block (sb
));
30164 #pragma omp for for-clause[optseq] new-line
30168 #pragma omp for simd for-simd-clause[optseq] new-line
30171 #define OMP_FOR_CLAUSE_MASK \
30172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30182 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
30183 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30185 tree clauses
, sb
, ret
;
30187 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30189 strcat (p_name
, " for");
30190 mask
|= OMP_FOR_CLAUSE_MASK
;
30192 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30196 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30197 const char *p
= IDENTIFIER_POINTER (id
);
30199 if (strcmp (p
, "simd") == 0)
30201 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30202 if (cclauses
== NULL
)
30203 cclauses
= cclauses_buf
;
30205 cp_lexer_consume_token (parser
->lexer
);
30206 if (!flag_openmp
) /* flag_openmp_simd */
30207 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30209 sb
= begin_omp_structured_block ();
30210 save
= cp_parser_begin_omp_structured_block (parser
);
30211 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30213 cp_parser_end_omp_structured_block (parser
, save
);
30214 tree body
= finish_omp_structured_block (sb
);
30217 ret
= make_node (OMP_FOR
);
30218 TREE_TYPE (ret
) = void_type_node
;
30219 OMP_FOR_BODY (ret
) = body
;
30220 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30221 SET_EXPR_LOCATION (ret
, loc
);
30226 if (!flag_openmp
) /* flag_openmp_simd */
30228 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30232 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30236 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
30237 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
30240 sb
= begin_omp_structured_block ();
30241 save
= cp_parser_begin_omp_structured_block (parser
);
30243 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
);
30245 cp_parser_end_omp_structured_block (parser
, save
);
30246 add_stmt (finish_omp_structured_block (sb
));
30252 # pragma omp master new-line
30253 structured-block */
30256 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
)
30258 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30259 return c_finish_omp_master (input_location
,
30260 cp_parser_omp_structured_block (parser
));
30264 # pragma omp ordered new-line
30265 structured-block */
30268 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
)
30270 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30271 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30272 return c_finish_omp_ordered (loc
, cp_parser_omp_structured_block (parser
));
30278 { section-sequence }
30281 section-directive[opt] structured-block
30282 section-sequence section-directive structured-block */
30285 cp_parser_omp_sections_scope (cp_parser
*parser
)
30287 tree stmt
, substmt
;
30288 bool error_suppress
= false;
30291 if (!cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
30294 stmt
= push_stmt_list ();
30296 if (cp_lexer_peek_token (parser
->lexer
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
30298 substmt
= cp_parser_omp_structured_block (parser
);
30299 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30300 add_stmt (substmt
);
30305 tok
= cp_lexer_peek_token (parser
->lexer
);
30306 if (tok
->type
== CPP_CLOSE_BRACE
)
30308 if (tok
->type
== CPP_EOF
)
30311 if (tok
->pragma_kind
== PRAGMA_OMP_SECTION
)
30313 cp_lexer_consume_token (parser
->lexer
);
30314 cp_parser_require_pragma_eol (parser
, tok
);
30315 error_suppress
= false;
30317 else if (!error_suppress
)
30319 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
30320 error_suppress
= true;
30323 substmt
= cp_parser_omp_structured_block (parser
);
30324 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
30325 add_stmt (substmt
);
30327 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
30329 substmt
= pop_stmt_list (stmt
);
30331 stmt
= make_node (OMP_SECTIONS
);
30332 TREE_TYPE (stmt
) = void_type_node
;
30333 OMP_SECTIONS_BODY (stmt
) = substmt
;
30340 # pragma omp sections sections-clause[optseq] newline
30343 #define OMP_SECTIONS_CLAUSE_MASK \
30344 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30351 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
30352 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30355 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30357 strcat (p_name
, " sections");
30358 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
30360 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
30362 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30366 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
30367 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
30370 ret
= cp_parser_omp_sections_scope (parser
);
30372 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
30378 # pragma omp parallel parallel-clause[optseq] new-line
30380 # pragma omp parallel for parallel-for-clause[optseq] new-line
30382 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30386 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30387 structured-block */
30389 #define OMP_PARALLEL_CLAUSE_MASK \
30390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30401 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
30402 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30404 tree stmt
, clauses
, block
;
30406 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30408 strcat (p_name
, " parallel");
30409 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
30411 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
30413 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30414 if (cclauses
== NULL
)
30415 cclauses
= cclauses_buf
;
30417 cp_lexer_consume_token (parser
->lexer
);
30418 if (!flag_openmp
) /* flag_openmp_simd */
30419 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30420 block
= begin_omp_parallel ();
30421 save
= cp_parser_begin_omp_structured_block (parser
);
30422 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30423 cp_parser_end_omp_structured_block (parser
, save
);
30424 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30426 if (ret
== NULL_TREE
)
30428 OMP_PARALLEL_COMBINED (stmt
) = 1;
30433 error_at (loc
, "expected %<for%> after %qs", p_name
);
30434 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30437 else if (!flag_openmp
) /* flag_openmp_simd */
30439 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30442 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30444 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30445 const char *p
= IDENTIFIER_POINTER (id
);
30446 if (strcmp (p
, "sections") == 0)
30448 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30449 cclauses
= cclauses_buf
;
30451 cp_lexer_consume_token (parser
->lexer
);
30452 block
= begin_omp_parallel ();
30453 save
= cp_parser_begin_omp_structured_block (parser
);
30454 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
30455 cp_parser_end_omp_structured_block (parser
, save
);
30456 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
30458 OMP_PARALLEL_COMBINED (stmt
) = 1;
30463 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
);
30465 block
= begin_omp_parallel ();
30466 save
= cp_parser_begin_omp_structured_block (parser
);
30467 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30468 cp_parser_end_omp_structured_block (parser
, save
);
30469 stmt
= finish_omp_parallel (clauses
, block
);
30474 # pragma omp single single-clause[optseq] new-line
30475 structured-block */
30477 #define OMP_SINGLE_CLAUSE_MASK \
30478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30484 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
)
30486 tree stmt
= make_node (OMP_SINGLE
);
30487 TREE_TYPE (stmt
) = void_type_node
;
30489 OMP_SINGLE_CLAUSES (stmt
)
30490 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
30491 "#pragma omp single", pragma_tok
);
30492 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30494 return add_stmt (stmt
);
30498 # pragma omp task task-clause[optseq] new-line
30499 structured-block */
30501 #define OMP_TASK_CLAUSE_MASK \
30502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30513 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
)
30515 tree clauses
, block
;
30518 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
30519 "#pragma omp task", pragma_tok
);
30520 block
= begin_omp_task ();
30521 save
= cp_parser_begin_omp_structured_block (parser
);
30522 cp_parser_statement (parser
, NULL_TREE
, false, NULL
);
30523 cp_parser_end_omp_structured_block (parser
, save
);
30524 return finish_omp_task (clauses
, block
);
30528 # pragma omp taskwait new-line */
30531 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
30533 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30534 finish_omp_taskwait ();
30538 # pragma omp taskyield new-line */
30541 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
30543 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30544 finish_omp_taskyield ();
30548 # pragma omp taskgroup new-line
30549 structured-block */
30552 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
)
30554 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30555 return c_finish_omp_taskgroup (input_location
,
30556 cp_parser_omp_structured_block (parser
));
30561 # pragma omp threadprivate (variable-list) */
30564 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
30568 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
30569 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30571 finish_omp_threadprivate (vars
);
30575 # pragma omp cancel cancel-clause[optseq] new-line */
30577 #define OMP_CANCEL_CLAUSE_MASK \
30578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30585 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
30587 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
30588 "#pragma omp cancel", pragma_tok
);
30589 finish_omp_cancel (clauses
);
30593 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30595 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30602 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
)
30605 bool point_seen
= false;
30607 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30609 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30610 const char *p
= IDENTIFIER_POINTER (id
);
30612 if (strcmp (p
, "point") == 0)
30614 cp_lexer_consume_token (parser
->lexer
);
30620 cp_parser_error (parser
, "expected %<point%>");
30621 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30625 clauses
= cp_parser_omp_all_clauses (parser
,
30626 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
30627 "#pragma omp cancellation point",
30629 finish_omp_cancellation_point (clauses
);
30633 #pragma omp distribute distribute-clause[optseq] new-line
30636 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30643 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
30644 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30646 tree clauses
, sb
, ret
;
30648 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30650 strcat (p_name
, " distribute");
30651 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
30653 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30655 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30656 const char *p
= IDENTIFIER_POINTER (id
);
30658 bool parallel
= false;
30660 if (strcmp (p
, "simd") == 0)
30663 parallel
= strcmp (p
, "parallel") == 0;
30664 if (parallel
|| simd
)
30666 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30667 if (cclauses
== NULL
)
30668 cclauses
= cclauses_buf
;
30669 cp_lexer_consume_token (parser
->lexer
);
30670 if (!flag_openmp
) /* flag_openmp_simd */
30673 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30676 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30679 sb
= begin_omp_structured_block ();
30680 save
= cp_parser_begin_omp_structured_block (parser
);
30682 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
30685 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
30687 cp_parser_end_omp_structured_block (parser
, save
);
30688 tree body
= finish_omp_structured_block (sb
);
30691 ret
= make_node (OMP_DISTRIBUTE
);
30692 TREE_TYPE (ret
) = void_type_node
;
30693 OMP_FOR_BODY (ret
) = body
;
30694 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30695 SET_EXPR_LOCATION (ret
, loc
);
30700 if (!flag_openmp
) /* flag_openmp_simd */
30702 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30706 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30710 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
30711 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
30714 sb
= begin_omp_structured_block ();
30715 save
= cp_parser_begin_omp_structured_block (parser
);
30717 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
);
30719 cp_parser_end_omp_structured_block (parser
, save
);
30720 add_stmt (finish_omp_structured_block (sb
));
30726 # pragma omp teams teams-clause[optseq] new-line
30727 structured-block */
30729 #define OMP_TEAMS_CLAUSE_MASK \
30730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30739 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
30740 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
30742 tree clauses
, sb
, ret
;
30744 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
30746 strcat (p_name
, " teams");
30747 mask
|= OMP_TEAMS_CLAUSE_MASK
;
30749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30751 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30752 const char *p
= IDENTIFIER_POINTER (id
);
30753 if (strcmp (p
, "distribute") == 0)
30755 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
30756 if (cclauses
== NULL
)
30757 cclauses
= cclauses_buf
;
30759 cp_lexer_consume_token (parser
->lexer
);
30760 if (!flag_openmp
) /* flag_openmp_simd */
30761 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30763 sb
= begin_omp_structured_block ();
30764 save
= cp_parser_begin_omp_structured_block (parser
);
30765 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
30767 cp_parser_end_omp_structured_block (parser
, save
);
30768 tree body
= finish_omp_structured_block (sb
);
30771 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30772 ret
= make_node (OMP_TEAMS
);
30773 TREE_TYPE (ret
) = void_type_node
;
30774 OMP_TEAMS_CLAUSES (ret
) = clauses
;
30775 OMP_TEAMS_BODY (ret
) = body
;
30776 return add_stmt (ret
);
30779 if (!flag_openmp
) /* flag_openmp_simd */
30781 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30785 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
30789 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
30790 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
30793 tree stmt
= make_node (OMP_TEAMS
);
30794 TREE_TYPE (stmt
) = void_type_node
;
30795 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
30796 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30798 return add_stmt (stmt
);
30802 # pragma omp target data target-data-clause[optseq] new-line
30803 structured-block */
30805 #define OMP_TARGET_DATA_CLAUSE_MASK \
30806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30811 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
)
30813 tree stmt
= make_node (OMP_TARGET_DATA
);
30814 TREE_TYPE (stmt
) = void_type_node
;
30816 OMP_TARGET_DATA_CLAUSES (stmt
)
30817 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
30818 "#pragma omp target data", pragma_tok
);
30819 keep_next_level (true);
30820 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30822 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30823 return add_stmt (stmt
);
30827 # pragma omp target update target-update-clause[optseq] new-line */
30829 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30836 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
30837 enum pragma_context context
)
30839 if (context
== pragma_stmt
)
30841 error_at (pragma_tok
->location
,
30842 "%<#pragma omp target update%> may only be "
30843 "used in compound statements");
30844 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30849 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
30850 "#pragma omp target update", pragma_tok
);
30851 if (find_omp_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
30852 && find_omp_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
30854 error_at (pragma_tok
->location
,
30855 "%<#pragma omp target update must contain at least one "
30856 "%<from%> or %<to%> clauses");
30860 tree stmt
= make_node (OMP_TARGET_UPDATE
);
30861 TREE_TYPE (stmt
) = void_type_node
;
30862 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
30863 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30869 # pragma omp target target-clause[optseq] new-line
30870 structured-block */
30872 #define OMP_TARGET_CLAUSE_MASK \
30873 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30878 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
30879 enum pragma_context context
)
30881 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
30883 cp_parser_error (parser
, "expected declaration specifiers");
30884 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
30888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
30890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
30891 const char *p
= IDENTIFIER_POINTER (id
);
30893 if (strcmp (p
, "teams") == 0)
30895 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
30896 char p_name
[sizeof ("#pragma omp target teams distribute "
30897 "parallel for simd")];
30899 cp_lexer_consume_token (parser
->lexer
);
30900 strcpy (p_name
, "#pragma omp target");
30901 if (!flag_openmp
) /* flag_openmp_simd */
30903 tree stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30904 OMP_TARGET_CLAUSE_MASK
,
30906 return stmt
!= NULL_TREE
;
30908 keep_next_level (true);
30909 tree sb
= begin_omp_structured_block ();
30910 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
30911 tree ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
30912 OMP_TARGET_CLAUSE_MASK
, cclauses
);
30913 cp_parser_end_omp_structured_block (parser
, save
);
30914 tree body
= finish_omp_structured_block (sb
);
30915 if (ret
== NULL_TREE
)
30917 tree stmt
= make_node (OMP_TARGET
);
30918 TREE_TYPE (stmt
) = void_type_node
;
30919 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
30920 OMP_TARGET_BODY (stmt
) = body
;
30924 else if (!flag_openmp
) /* flag_openmp_simd */
30926 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30929 else if (strcmp (p
, "data") == 0)
30931 cp_lexer_consume_token (parser
->lexer
);
30932 cp_parser_omp_target_data (parser
, pragma_tok
);
30935 else if (strcmp (p
, "update") == 0)
30937 cp_lexer_consume_token (parser
->lexer
);
30938 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
30942 tree stmt
= make_node (OMP_TARGET
);
30943 TREE_TYPE (stmt
) = void_type_node
;
30945 OMP_TARGET_CLAUSES (stmt
)
30946 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
30947 "#pragma omp target", pragma_tok
);
30948 keep_next_level (true);
30949 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
);
30951 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
30957 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30959 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30968 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
30969 enum pragma_context context
)
30971 bool first_p
= parser
->omp_declare_simd
== NULL
;
30972 cp_omp_declare_simd_data data
;
30975 data
.error_seen
= false;
30976 data
.fndecl_seen
= false;
30977 data
.tokens
= vNULL
;
30978 parser
->omp_declare_simd
= &data
;
30980 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
30981 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
30982 cp_lexer_consume_token (parser
->lexer
);
30983 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
30984 parser
->omp_declare_simd
->error_seen
= true;
30985 cp_parser_require_pragma_eol (parser
, pragma_tok
);
30986 struct cp_token_cache
*cp
30987 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
30988 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
30991 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
30992 cp_parser_pragma (parser
, context
);
30995 case pragma_external
:
30996 cp_parser_declaration (parser
);
30998 case pragma_member
:
30999 cp_parser_member_declaration (parser
);
31001 case pragma_objc_icode
:
31002 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31005 cp_parser_declaration_statement (parser
);
31008 if (parser
->omp_declare_simd
31009 && !parser
->omp_declare_simd
->error_seen
31010 && !parser
->omp_declare_simd
->fndecl_seen
)
31011 error_at (pragma_tok
->location
,
31012 "%<#pragma omp declare simd%> not immediately followed by "
31013 "function declaration or definition");
31014 data
.tokens
.release ();
31015 parser
->omp_declare_simd
= NULL
;
31019 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
31020 This function is modelled similar to the late parsing of omp declare
31024 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser
*parser
, tree attrs
)
31026 struct cp_token_cache
*ce
;
31027 cp_omp_declare_simd_data
*info
= parser
->cilk_simd_fn_info
;
31030 if (parser
->omp_declare_simd
!= NULL
)
31032 error ("%<#pragma omp declare simd%> cannot be used in the same function"
31033 " marked as a Cilk Plus SIMD-enabled function");
31034 XDELETE (parser
->cilk_simd_fn_info
);
31035 parser
->cilk_simd_fn_info
= NULL
;
31038 if (!info
->error_seen
&& info
->fndecl_seen
)
31040 error ("vector attribute not immediately followed by a single function"
31041 " declaration or definition");
31042 info
->error_seen
= true;
31044 if (info
->error_seen
)
31047 FOR_EACH_VEC_ELT (info
->tokens
, ii
, ce
)
31051 cp_parser_push_lexer_for_tokens (parser
, ce
);
31052 parser
->lexer
->in_pragma
= true;
31053 cl
= cp_parser_omp_all_clauses (parser
, CILK_SIMD_FN_CLAUSE_MASK
,
31054 "SIMD-enabled functions attribute",
31056 cp_parser_pop_lexer (parser
);
31058 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31060 c
= build_tree_list (get_identifier ("cilk simd function"), NULL_TREE
);
31061 TREE_CHAIN (c
) = attrs
;
31064 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31065 TREE_CHAIN (c
) = attrs
;
31066 if (processing_template_decl
)
31067 ATTR_IS_DEPENDENT (c
) = 1;
31070 info
->fndecl_seen
= true;
31071 XDELETE (parser
->cilk_simd_fn_info
);
31072 parser
->cilk_simd_fn_info
= NULL
;
31076 /* Finalize #pragma omp declare simd clauses after direct declarator has
31077 been parsed, and put that into "omp declare simd" attribute. */
31080 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
31082 struct cp_token_cache
*ce
;
31083 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
31086 if (!data
->error_seen
&& data
->fndecl_seen
)
31088 error ("%<#pragma omp declare simd%> not immediately followed by "
31089 "a single function declaration or definition");
31090 data
->error_seen
= true;
31093 if (data
->error_seen
)
31096 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
31100 cp_parser_push_lexer_for_tokens (parser
, ce
);
31101 parser
->lexer
->in_pragma
= true;
31102 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
31103 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
31104 cp_lexer_consume_token (parser
->lexer
);
31105 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
31106 "#pragma omp declare simd", pragma_tok
);
31107 cp_parser_pop_lexer (parser
);
31109 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
31110 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
31111 TREE_CHAIN (c
) = attrs
;
31112 if (processing_template_decl
)
31113 ATTR_IS_DEPENDENT (c
) = 1;
31117 data
->fndecl_seen
= true;
31123 # pragma omp declare target new-line
31124 declarations and definitions
31125 # pragma omp end declare target new-line */
31128 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31130 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31131 scope_chain
->omp_declare_target_attribute
++;
31135 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
31137 const char *p
= "";
31138 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31140 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31141 p
= IDENTIFIER_POINTER (id
);
31143 if (strcmp (p
, "declare") == 0)
31145 cp_lexer_consume_token (parser
->lexer
);
31147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31150 p
= IDENTIFIER_POINTER (id
);
31152 if (strcmp (p
, "target") == 0)
31153 cp_lexer_consume_token (parser
->lexer
);
31156 cp_parser_error (parser
, "expected %<target%>");
31157 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31163 cp_parser_error (parser
, "expected %<declare%>");
31164 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31167 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31168 if (!scope_chain
->omp_declare_target_attribute
)
31169 error_at (pragma_tok
->location
,
31170 "%<#pragma omp end declare target%> without corresponding "
31171 "%<#pragma omp declare target%>");
31173 scope_chain
->omp_declare_target_attribute
--;
31176 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31177 expression and optional initializer clause of
31178 #pragma omp declare reduction. We store the expression(s) as
31179 either 3, 6 or 7 special statements inside of the artificial function's
31180 body. The first two statements are DECL_EXPRs for the artificial
31181 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31182 expression that uses those variables.
31183 If there was any INITIALIZER clause, this is followed by further statements,
31184 the fourth and fifth statements are DECL_EXPRs for the artificial
31185 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31186 constructor variant (first token after open paren is not omp_priv),
31187 then the sixth statement is a statement with the function call expression
31188 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31189 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31190 to initialize the OMP_PRIV artificial variable and there is seventh
31191 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31194 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
31196 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
31197 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
31198 type
= TREE_TYPE (type
);
31199 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
31200 DECL_ARTIFICIAL (omp_out
) = 1;
31201 pushdecl (omp_out
);
31202 add_decl_expr (omp_out
);
31203 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
31204 DECL_ARTIFICIAL (omp_in
) = 1;
31206 add_decl_expr (omp_in
);
31208 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
31210 keep_next_level (true);
31211 tree block
= begin_omp_structured_block ();
31212 combiner
= cp_parser_expression (parser
);
31213 finish_expr_stmt (combiner
);
31214 block
= finish_omp_structured_block (block
);
31217 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31220 const char *p
= "";
31221 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31223 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31224 p
= IDENTIFIER_POINTER (id
);
31227 if (strcmp (p
, "initializer") == 0)
31229 cp_lexer_consume_token (parser
->lexer
);
31230 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31234 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31236 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31237 p
= IDENTIFIER_POINTER (id
);
31240 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
31241 DECL_ARTIFICIAL (omp_priv
) = 1;
31242 pushdecl (omp_priv
);
31243 add_decl_expr (omp_priv
);
31244 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
31245 DECL_ARTIFICIAL (omp_orig
) = 1;
31246 pushdecl (omp_orig
);
31247 add_decl_expr (omp_orig
);
31249 keep_next_level (true);
31250 block
= begin_omp_structured_block ();
31253 if (strcmp (p
, "omp_priv") == 0)
31255 bool is_direct_init
, is_non_constant_init
;
31257 cp_lexer_consume_token (parser
->lexer
);
31258 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
31260 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31261 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
31263 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
31264 == CPP_CLOSE_PAREN
))
31266 finish_omp_structured_block (block
);
31267 error ("invalid initializer clause");
31270 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
31271 &is_non_constant_init
);
31272 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
31273 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
31277 cp_parser_parse_tentatively (parser
);
31278 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
31279 /*check_dependency_p=*/true,
31280 /*template_p=*/NULL
,
31281 /*declarator_p=*/false,
31282 /*optional_p=*/false);
31283 vec
<tree
, va_gc
> *args
;
31284 if (fn_name
== error_mark_node
31285 || cp_parser_error_occurred (parser
)
31286 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
31287 || ((args
= cp_parser_parenthesized_expression_list
31288 (parser
, non_attr
, /*cast_p=*/false,
31289 /*allow_expansion_p=*/true,
31290 /*non_constant_p=*/NULL
)),
31291 cp_parser_error_occurred (parser
)))
31293 finish_omp_structured_block (block
);
31294 cp_parser_abort_tentative_parse (parser
);
31295 cp_parser_error (parser
, "expected id-expression (arguments)");
31300 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
31301 if (arg
== omp_priv
31302 || (TREE_CODE (arg
) == ADDR_EXPR
31303 && TREE_OPERAND (arg
, 0) == omp_priv
))
31305 cp_parser_abort_tentative_parse (parser
);
31306 if (arg
== NULL_TREE
)
31307 error ("one of the initializer call arguments should be %<omp_priv%>"
31308 " or %<&omp_priv%>");
31309 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
31311 finish_expr_stmt (initializer
);
31314 block
= finish_omp_structured_block (block
);
31315 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
31319 add_decl_expr (omp_orig
);
31321 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
31325 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
31326 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false);
31332 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31333 initializer-clause[opt] new-line
31335 initializer-clause:
31336 initializer (omp_priv initializer)
31337 initializer (function-name (argument-list)) */
31340 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
31341 enum pragma_context
)
31343 auto_vec
<tree
> types
;
31344 enum tree_code reduc_code
= ERROR_MARK
;
31345 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
31347 cp_token
*first_token
;
31348 cp_token_cache
*cp
;
31352 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31353 p
= obstack_alloc (&declarator_obstack
, 0);
31355 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
31358 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31361 reduc_code
= PLUS_EXPR
;
31364 reduc_code
= MULT_EXPR
;
31367 reduc_code
= MINUS_EXPR
;
31370 reduc_code
= BIT_AND_EXPR
;
31373 reduc_code
= BIT_XOR_EXPR
;
31376 reduc_code
= BIT_IOR_EXPR
;
31379 reduc_code
= TRUTH_ANDIF_EXPR
;
31382 reduc_code
= TRUTH_ORIF_EXPR
;
31385 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
31388 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31389 "%<|%>, %<&&%>, %<||%> or identifier");
31393 if (reduc_code
!= ERROR_MARK
)
31394 cp_lexer_consume_token (parser
->lexer
);
31396 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
31397 if (reduc_id
== error_mark_node
)
31400 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31403 /* Types may not be defined in declare reduction type list. */
31404 const char *saved_message
;
31405 saved_message
= parser
->type_definition_forbidden_message
;
31406 parser
->type_definition_forbidden_message
31407 = G_("types may not be defined in declare reduction type list");
31408 bool saved_colon_corrects_to_scope_p
;
31409 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
31410 parser
->colon_corrects_to_scope_p
= false;
31411 bool saved_colon_doesnt_start_class_def_p
;
31412 saved_colon_doesnt_start_class_def_p
31413 = parser
->colon_doesnt_start_class_def_p
;
31414 parser
->colon_doesnt_start_class_def_p
= true;
31418 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31419 type
= cp_parser_type_id (parser
);
31420 if (type
== error_mark_node
)
31422 else if (ARITHMETIC_TYPE_P (type
)
31423 && (orig_reduc_id
== NULL_TREE
31424 || (TREE_CODE (type
) != COMPLEX_TYPE
31425 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
31427 || strcmp (IDENTIFIER_POINTER (orig_reduc_id
),
31429 error_at (loc
, "predeclared arithmetic type %qT in "
31430 "%<#pragma omp declare reduction%>", type
);
31431 else if (TREE_CODE (type
) == FUNCTION_TYPE
31432 || TREE_CODE (type
) == METHOD_TYPE
31433 || TREE_CODE (type
) == ARRAY_TYPE
)
31434 error_at (loc
, "function or array type %qT in "
31435 "%<#pragma omp declare reduction%>", type
);
31436 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
31437 error_at (loc
, "reference type %qT in "
31438 "%<#pragma omp declare reduction%>", type
);
31439 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
31440 error_at (loc
, "const, volatile or __restrict qualified type %qT in "
31441 "%<#pragma omp declare reduction%>", type
);
31443 types
.safe_push (type
);
31445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31446 cp_lexer_consume_token (parser
->lexer
);
31451 /* Restore the saved message. */
31452 parser
->type_definition_forbidden_message
= saved_message
;
31453 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
31454 parser
->colon_doesnt_start_class_def_p
31455 = saved_colon_doesnt_start_class_def_p
;
31457 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
31458 || types
.is_empty ())
31461 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
31465 first_token
= cp_lexer_peek_token (parser
->lexer
);
31468 FOR_EACH_VEC_ELT (types
, i
, type
)
31471 = build_function_type_list (void_type_node
,
31472 cp_build_reference_type (type
, false),
31474 tree this_reduc_id
= reduc_id
;
31475 if (!dependent_type_p (type
))
31476 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
31477 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
31478 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
31479 DECL_ARTIFICIAL (fndecl
) = 1;
31480 DECL_EXTERNAL (fndecl
) = 1;
31481 DECL_DECLARED_INLINE_P (fndecl
) = 1;
31482 DECL_IGNORED_P (fndecl
) = 1;
31483 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
31484 DECL_ATTRIBUTES (fndecl
)
31485 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
31486 DECL_ATTRIBUTES (fndecl
));
31487 if (processing_template_decl
)
31488 fndecl
= push_template_decl (fndecl
);
31489 bool block_scope
= false;
31490 tree block
= NULL_TREE
;
31491 if (current_function_decl
)
31493 block_scope
= true;
31494 DECL_CONTEXT (fndecl
) = global_namespace
;
31495 if (!processing_template_decl
)
31498 else if (current_class_type
)
31502 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
31503 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
31504 cp_lexer_consume_token (parser
->lexer
);
31505 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
31507 cp
= cp_token_cache_new (first_token
,
31508 cp_lexer_peek_nth_token (parser
->lexer
,
31511 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
31512 finish_member_declaration (fndecl
);
31513 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
31514 DECL_PENDING_INLINE_P (fndecl
) = 1;
31515 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
31520 DECL_CONTEXT (fndecl
) = current_namespace
;
31524 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
31526 block
= begin_omp_structured_block ();
31529 cp_parser_push_lexer_for_tokens (parser
, cp
);
31530 parser
->lexer
->in_pragma
= true;
31532 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
31535 finish_function (0);
31537 DECL_CONTEXT (fndecl
) = current_function_decl
;
31539 cp_parser_pop_lexer (parser
);
31543 cp_parser_pop_lexer (parser
);
31545 finish_function (0);
31548 DECL_CONTEXT (fndecl
) = current_function_decl
;
31549 block
= finish_omp_structured_block (block
);
31550 if (TREE_CODE (block
) == BIND_EXPR
)
31551 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
31552 else if (TREE_CODE (block
) == STATEMENT_LIST
)
31553 DECL_SAVED_TREE (fndecl
) = block
;
31554 if (processing_template_decl
)
31555 add_decl_expr (fndecl
);
31557 cp_check_omp_declare_reduction (fndecl
);
31558 if (cp
== NULL
&& types
.length () > 1)
31559 cp
= cp_token_cache_new (first_token
,
31560 cp_lexer_peek_nth_token (parser
->lexer
, 2));
31561 if (errs
!= errorcount
)
31565 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31568 /* Free any declarators allocated. */
31569 obstack_free (&declarator_obstack
, p
);
31573 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31574 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31575 initializer-clause[opt] new-line
31576 #pragma omp declare target new-line */
31579 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
31580 enum pragma_context context
)
31582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
31584 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
31585 const char *p
= IDENTIFIER_POINTER (id
);
31587 if (strcmp (p
, "simd") == 0)
31589 cp_lexer_consume_token (parser
->lexer
);
31590 cp_parser_omp_declare_simd (parser
, pragma_tok
,
31594 cp_ensure_no_omp_declare_simd (parser
);
31595 if (strcmp (p
, "reduction") == 0)
31597 cp_lexer_consume_token (parser
->lexer
);
31598 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
31602 if (!flag_openmp
) /* flag_openmp_simd */
31604 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31607 if (strcmp (p
, "target") == 0)
31609 cp_lexer_consume_token (parser
->lexer
);
31610 cp_parser_omp_declare_target (parser
, pragma_tok
);
31614 cp_parser_error (parser
, "expected %<simd%> or %<reduction%> "
31616 cp_parser_require_pragma_eol (parser
, pragma_tok
);
31619 /* Main entry point to OpenMP statement pragmas. */
31622 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
)
31625 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
31626 omp_clause_mask
mask (0);
31628 switch (pragma_tok
->pragma_kind
)
31630 case PRAGMA_OMP_ATOMIC
:
31631 cp_parser_omp_atomic (parser
, pragma_tok
);
31633 case PRAGMA_OMP_CRITICAL
:
31634 stmt
= cp_parser_omp_critical (parser
, pragma_tok
);
31636 case PRAGMA_OMP_DISTRIBUTE
:
31637 strcpy (p_name
, "#pragma omp");
31638 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
);
31640 case PRAGMA_OMP_FOR
:
31641 strcpy (p_name
, "#pragma omp");
31642 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
);
31644 case PRAGMA_OMP_MASTER
:
31645 stmt
= cp_parser_omp_master (parser
, pragma_tok
);
31647 case PRAGMA_OMP_ORDERED
:
31648 stmt
= cp_parser_omp_ordered (parser
, pragma_tok
);
31650 case PRAGMA_OMP_PARALLEL
:
31651 strcpy (p_name
, "#pragma omp");
31652 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
);
31654 case PRAGMA_OMP_SECTIONS
:
31655 strcpy (p_name
, "#pragma omp");
31656 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
31658 case PRAGMA_OMP_SIMD
:
31659 strcpy (p_name
, "#pragma omp");
31660 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
);
31662 case PRAGMA_OMP_SINGLE
:
31663 stmt
= cp_parser_omp_single (parser
, pragma_tok
);
31665 case PRAGMA_OMP_TASK
:
31666 stmt
= cp_parser_omp_task (parser
, pragma_tok
);
31668 case PRAGMA_OMP_TASKGROUP
:
31669 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
);
31671 case PRAGMA_OMP_TEAMS
:
31672 strcpy (p_name
, "#pragma omp");
31673 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
);
31676 gcc_unreachable ();
31680 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
31683 /* Transactional Memory parsing routines. */
31685 /* Parse a transaction attribute.
31691 ??? Simplify this when C++0x bracket attributes are
31692 implemented properly. */
31695 cp_parser_txn_attribute_opt (cp_parser
*parser
)
31698 tree attr_name
, attr
= NULL
;
31700 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
31701 return cp_parser_attributes_opt (parser
);
31703 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
31705 cp_lexer_consume_token (parser
->lexer
);
31706 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
31709 token
= cp_lexer_peek_token (parser
->lexer
);
31710 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
31712 token
= cp_lexer_consume_token (parser
->lexer
);
31714 attr_name
= (token
->type
== CPP_KEYWORD
31715 /* For keywords, use the canonical spelling,
31716 not the parsed identifier. */
31717 ? ridpointers
[(int) token
->keyword
]
31719 attr
= build_tree_list (attr_name
, NULL_TREE
);
31722 cp_parser_error (parser
, "expected identifier");
31724 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31726 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31730 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31732 transaction-statement:
31733 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31735 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31739 cp_parser_transaction (cp_parser
*parser
, enum rid keyword
)
31741 unsigned char old_in
= parser
->in_transaction
;
31742 unsigned char this_in
= 1, new_in
;
31744 tree stmt
, attrs
, noex
;
31746 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31747 || keyword
== RID_TRANSACTION_RELAXED
);
31748 token
= cp_parser_require_keyword (parser
, keyword
,
31749 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31750 : RT_TRANSACTION_RELAXED
));
31751 gcc_assert (token
!= NULL
);
31753 if (keyword
== RID_TRANSACTION_RELAXED
)
31754 this_in
|= TM_STMT_ATTR_RELAXED
;
31757 attrs
= cp_parser_txn_attribute_opt (parser
);
31759 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31762 /* Parse a noexcept specification. */
31763 noex
= cp_parser_noexcept_specification_opt (parser
, true, NULL
, true);
31765 /* Keep track if we're in the lexical scope of an outer transaction. */
31766 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
31768 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
31770 parser
->in_transaction
= new_in
;
31771 cp_parser_compound_statement (parser
, NULL
, false, false);
31772 parser
->in_transaction
= old_in
;
31774 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
31779 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31781 transaction-expression:
31782 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31783 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31787 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
31789 unsigned char old_in
= parser
->in_transaction
;
31790 unsigned char this_in
= 1;
31795 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31796 || keyword
== RID_TRANSACTION_RELAXED
);
31799 error (keyword
== RID_TRANSACTION_RELAXED
31800 ? G_("%<__transaction_relaxed%> without transactional memory "
31802 : G_("%<__transaction_atomic%> without transactional memory "
31803 "support enabled"));
31805 token
= cp_parser_require_keyword (parser
, keyword
,
31806 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31807 : RT_TRANSACTION_RELAXED
));
31808 gcc_assert (token
!= NULL
);
31810 if (keyword
== RID_TRANSACTION_RELAXED
)
31811 this_in
|= TM_STMT_ATTR_RELAXED
;
31813 /* Set this early. This might mean that we allow transaction_cancel in
31814 an expression that we find out later actually has to be a constexpr.
31815 However, we expect that cxx_constant_value will be able to deal with
31816 this; also, if the noexcept has no constexpr, then what we parse next
31817 really is a transaction's body. */
31818 parser
->in_transaction
= this_in
;
31820 /* Parse a noexcept specification. */
31821 noex
= cp_parser_noexcept_specification_opt (parser
, false, &noex_expr
,
31824 if (!noex
|| !noex_expr
31825 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
31827 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
31829 expr
= cp_parser_expression (parser
);
31830 expr
= finish_parenthesized_expr (expr
);
31832 cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
);
31836 /* The only expression that is available got parsed for the noexcept
31837 already. noexcept is true then. */
31839 noex
= boolean_true_node
;
31842 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
31843 parser
->in_transaction
= old_in
;
31845 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
31846 return error_mark_node
;
31848 return (flag_tm
? expr
: error_mark_node
);
31851 /* Parse a function-transaction-block.
31853 function-transaction-block:
31854 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31856 __transaction_atomic txn-attribute[opt] function-try-block
31857 __transaction_relaxed ctor-initializer[opt] function-body
31858 __transaction_relaxed function-try-block
31862 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
31864 unsigned char old_in
= parser
->in_transaction
;
31865 unsigned char new_in
= 1;
31866 tree compound_stmt
, stmt
, attrs
;
31867 bool ctor_initializer_p
;
31870 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
31871 || keyword
== RID_TRANSACTION_RELAXED
);
31872 token
= cp_parser_require_keyword (parser
, keyword
,
31873 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
31874 : RT_TRANSACTION_RELAXED
));
31875 gcc_assert (token
!= NULL
);
31877 if (keyword
== RID_TRANSACTION_RELAXED
)
31878 new_in
|= TM_STMT_ATTR_RELAXED
;
31881 attrs
= cp_parser_txn_attribute_opt (parser
);
31883 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
31886 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
31888 parser
->in_transaction
= new_in
;
31890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
31891 ctor_initializer_p
= cp_parser_function_try_block (parser
);
31893 ctor_initializer_p
= cp_parser_ctor_initializer_opt_and_function_body
31894 (parser
, /*in_function_try_block=*/false);
31896 parser
->in_transaction
= old_in
;
31898 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
31900 return ctor_initializer_p
;
31903 /* Parse a __transaction_cancel statement.
31906 __transaction_cancel txn-attribute[opt] ;
31907 __transaction_cancel txn-attribute[opt] throw-expression ;
31909 ??? Cancel and throw is not yet implemented. */
31912 cp_parser_transaction_cancel (cp_parser
*parser
)
31915 bool is_outer
= false;
31918 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
31919 RT_TRANSACTION_CANCEL
);
31920 gcc_assert (token
!= NULL
);
31922 attrs
= cp_parser_txn_attribute_opt (parser
);
31924 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
31926 /* ??? Parse cancel-and-throw here. */
31928 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
31932 error_at (token
->location
, "%<__transaction_cancel%> without "
31933 "transactional memory support enabled");
31934 return error_mark_node
;
31936 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
31938 error_at (token
->location
, "%<__transaction_cancel%> within a "
31939 "%<__transaction_relaxed%>");
31940 return error_mark_node
;
31944 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
31945 && !is_tm_may_cancel_outer (current_function_decl
))
31947 error_at (token
->location
, "outer %<__transaction_cancel%> not "
31948 "within outer %<__transaction_atomic%>");
31949 error_at (token
->location
,
31950 " or a %<transaction_may_cancel_outer%> function");
31951 return error_mark_node
;
31954 else if (parser
->in_transaction
== 0)
31956 error_at (token
->location
, "%<__transaction_cancel%> not within "
31957 "%<__transaction_atomic%>");
31958 return error_mark_node
;
31961 stmt
= build_tm_abort_call (token
->location
, is_outer
);
31969 static GTY (()) cp_parser
*the_parser
;
31972 /* Special handling for the first token or line in the file. The first
31973 thing in the file might be #pragma GCC pch_preprocess, which loads a
31974 PCH file, which is a GC collection point. So we need to handle this
31975 first pragma without benefit of an existing lexer structure.
31977 Always returns one token to the caller in *FIRST_TOKEN. This is
31978 either the true first token of the file, or the first token after
31979 the initial pragma. */
31982 cp_parser_initial_pragma (cp_token
*first_token
)
31986 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31987 if (first_token
->pragma_kind
!= PRAGMA_GCC_PCH_PREPROCESS
)
31990 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31991 if (first_token
->type
== CPP_STRING
)
31993 name
= first_token
->u
.value
;
31995 cp_lexer_get_preprocessor_token (NULL
, first_token
);
31996 if (first_token
->type
!= CPP_PRAGMA_EOL
)
31997 error_at (first_token
->location
,
31998 "junk at end of %<#pragma GCC pch_preprocess%>");
32001 error_at (first_token
->location
, "expected string literal");
32003 /* Skip to the end of the pragma. */
32004 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
32005 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32007 /* Now actually load the PCH file. */
32009 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
32011 /* Read one more token to return to our caller. We have to do this
32012 after reading the PCH file in, since its pointers have to be
32014 cp_lexer_get_preprocessor_token (NULL
, first_token
);
32017 /* Parses the grainsize pragma for the _Cilk_for statement.
32019 #pragma cilk grainsize = <VALUE>. */
32022 cp_parser_cilk_grainsize (cp_parser
*parser
, cp_token
*pragma_tok
)
32024 if (cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
32026 tree exp
= cp_parser_binary_expression (parser
, false, false,
32027 PREC_NOT_OPERATOR
, NULL
);
32028 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32029 if (!exp
|| exp
== error_mark_node
)
32031 error_at (pragma_tok
->location
, "invalid grainsize for _Cilk_for");
32035 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
32036 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CILK_FOR
))
32037 cp_parser_cilk_for (parser
, exp
);
32039 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
32040 "%<#pragma cilk grainsize%> is not followed by "
32044 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32047 /* Normal parsing of a pragma token. Here we can (and must) use the
32051 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
)
32053 cp_token
*pragma_tok
;
32056 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
32057 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
32058 parser
->lexer
->in_pragma
= true;
32060 id
= pragma_tok
->pragma_kind
;
32061 if (id
!= PRAGMA_OMP_DECLARE_REDUCTION
)
32062 cp_ensure_no_omp_declare_simd (parser
);
32065 case PRAGMA_GCC_PCH_PREPROCESS
:
32066 error_at (pragma_tok
->location
,
32067 "%<#pragma GCC pch_preprocess%> must be first");
32070 case PRAGMA_OMP_BARRIER
:
32073 case pragma_compound
:
32074 cp_parser_omp_barrier (parser
, pragma_tok
);
32077 error_at (pragma_tok
->location
, "%<#pragma omp barrier%> may only be "
32078 "used in compound statements");
32085 case PRAGMA_OMP_FLUSH
:
32088 case pragma_compound
:
32089 cp_parser_omp_flush (parser
, pragma_tok
);
32092 error_at (pragma_tok
->location
, "%<#pragma omp flush%> may only be "
32093 "used in compound statements");
32100 case PRAGMA_OMP_TASKWAIT
:
32103 case pragma_compound
:
32104 cp_parser_omp_taskwait (parser
, pragma_tok
);
32107 error_at (pragma_tok
->location
,
32108 "%<#pragma omp taskwait%> may only be "
32109 "used in compound statements");
32116 case PRAGMA_OMP_TASKYIELD
:
32119 case pragma_compound
:
32120 cp_parser_omp_taskyield (parser
, pragma_tok
);
32123 error_at (pragma_tok
->location
,
32124 "%<#pragma omp taskyield%> may only be "
32125 "used in compound statements");
32132 case PRAGMA_OMP_CANCEL
:
32135 case pragma_compound
:
32136 cp_parser_omp_cancel (parser
, pragma_tok
);
32139 error_at (pragma_tok
->location
,
32140 "%<#pragma omp cancel%> may only be "
32141 "used in compound statements");
32148 case PRAGMA_OMP_CANCELLATION_POINT
:
32151 case pragma_compound
:
32152 cp_parser_omp_cancellation_point (parser
, pragma_tok
);
32155 error_at (pragma_tok
->location
,
32156 "%<#pragma omp cancellation point%> may only be "
32157 "used in compound statements");
32164 case PRAGMA_OMP_THREADPRIVATE
:
32165 cp_parser_omp_threadprivate (parser
, pragma_tok
);
32168 case PRAGMA_OMP_DECLARE_REDUCTION
:
32169 cp_parser_omp_declare (parser
, pragma_tok
, context
);
32172 case PRAGMA_OMP_ATOMIC
:
32173 case PRAGMA_OMP_CRITICAL
:
32174 case PRAGMA_OMP_DISTRIBUTE
:
32175 case PRAGMA_OMP_FOR
:
32176 case PRAGMA_OMP_MASTER
:
32177 case PRAGMA_OMP_ORDERED
:
32178 case PRAGMA_OMP_PARALLEL
:
32179 case PRAGMA_OMP_SECTIONS
:
32180 case PRAGMA_OMP_SIMD
:
32181 case PRAGMA_OMP_SINGLE
:
32182 case PRAGMA_OMP_TASK
:
32183 case PRAGMA_OMP_TASKGROUP
:
32184 case PRAGMA_OMP_TEAMS
:
32185 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
32187 cp_parser_omp_construct (parser
, pragma_tok
);
32190 case PRAGMA_OMP_TARGET
:
32191 return cp_parser_omp_target (parser
, pragma_tok
, context
);
32193 case PRAGMA_OMP_END_DECLARE_TARGET
:
32194 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
32197 case PRAGMA_OMP_SECTION
:
32198 error_at (pragma_tok
->location
,
32199 "%<#pragma omp section%> may only be used in "
32200 "%<#pragma omp sections%> construct");
32205 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32207 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32208 if (tok
->type
!= CPP_KEYWORD
32209 || (tok
->keyword
!= RID_FOR
&& tok
->keyword
!= RID_WHILE
32210 && tok
->keyword
!= RID_DO
))
32212 cp_parser_error (parser
, "for, while or do statement expected");
32215 cp_parser_iteration_statement (parser
, true);
32219 case PRAGMA_CILK_SIMD
:
32220 if (context
== pragma_external
)
32222 error_at (pragma_tok
->location
,
32223 "%<#pragma simd%> must be inside a function");
32226 cp_parser_cilk_simd (parser
, pragma_tok
);
32229 case PRAGMA_CILK_GRAINSIZE
:
32230 if (context
== pragma_external
)
32232 error_at (pragma_tok
->location
,
32233 "%<#pragma cilk grainsize%> must be inside a function");
32237 /* Ignore the pragma if Cilk Plus is not enabled. */
32240 cp_parser_cilk_grainsize (parser
, pragma_tok
);
32245 error_at (pragma_tok
->location
, "-fcilkplus must be enabled to use "
32246 "%<#pragma cilk grainsize%>");
32251 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
32252 c_invoke_pragma_handler (id
);
32256 cp_parser_error (parser
, "expected declaration specifiers");
32260 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
32264 /* The interface the pragma parsers have to the lexer. */
32267 pragma_lex (tree
*value
)
32270 enum cpp_ttype ret
;
32272 tok
= cp_lexer_peek_token (the_parser
->lexer
);
32275 *value
= tok
->u
.value
;
32277 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
32279 else if (ret
== CPP_STRING
)
32280 *value
= cp_parser_string_literal (the_parser
, false, false);
32283 cp_lexer_consume_token (the_parser
->lexer
);
32284 if (ret
== CPP_KEYWORD
)
32292 /* External interface. */
32294 /* Parse one entire translation unit. */
32297 c_parse_file (void)
32299 static bool already_called
= false;
32301 if (already_called
)
32302 fatal_error ("inter-module optimizations not implemented for C++");
32303 already_called
= true;
32305 the_parser
= cp_parser_new ();
32306 push_deferring_access_checks (flag_access_control
32307 ? dk_no_deferred
: dk_no_check
);
32308 cp_parser_translation_unit (the_parser
);
32312 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32313 vectorlength clause:
32315 vectorlength ( constant-expression ) */
32318 cp_parser_cilk_simd_vectorlength (cp_parser
*parser
, tree clauses
,
32321 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32323 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32324 safelen clause. Thus, vectorlength is represented as OMP 4.0
32325 safelen. For SIMD-enabled function it is represented by OMP 4.0
32328 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SAFELEN
, "vectorlength",
32331 check_no_duplicate_clause (clauses
, OMP_CLAUSE_SIMDLEN
, "vectorlength",
32334 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32335 return error_mark_node
;
32337 expr
= cp_parser_constant_expression (parser
);
32338 expr
= maybe_constant_value (expr
);
32340 /* If expr == error_mark_node, then don't emit any errors nor
32341 create a clause. if any of the above functions returns
32342 error mark node then they would have emitted an error message. */
32343 if (expr
== error_mark_node
)
32345 else if (!TREE_TYPE (expr
)
32346 || !TREE_CONSTANT (expr
)
32347 || !INTEGRAL_TYPE_P (TREE_TYPE (expr
)))
32348 error_at (loc
, "vectorlength must be an integer constant");
32349 else if (TREE_CONSTANT (expr
)
32350 && exact_log2 (TREE_INT_CST_LOW (expr
)) == -1)
32351 error_at (loc
, "vectorlength must be a power of 2");
32357 c
= build_omp_clause (loc
, OMP_CLAUSE_SAFELEN
);
32358 OMP_CLAUSE_SAFELEN_EXPR (c
) = expr
;
32359 OMP_CLAUSE_CHAIN (c
) = clauses
;
32364 c
= build_omp_clause (loc
, OMP_CLAUSE_SIMDLEN
);
32365 OMP_CLAUSE_SIMDLEN_EXPR (c
) = expr
;
32366 OMP_CLAUSE_CHAIN (c
) = clauses
;
32371 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
32372 return error_mark_node
;
32376 /* Handles the Cilk Plus #pragma simd linear clause.
32378 linear ( simd-linear-variable-list )
32380 simd-linear-variable-list:
32381 simd-linear-variable
32382 simd-linear-variable-list , simd-linear-variable
32384 simd-linear-variable:
32386 id-expression : simd-linear-step
32389 conditional-expression */
32392 cp_parser_cilk_simd_linear (cp_parser
*parser
, tree clauses
)
32394 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32396 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
32398 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32400 cp_parser_error (parser
, "expected identifier");
32401 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32402 return error_mark_node
;
32405 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
32406 parser
->colon_corrects_to_scope_p
= false;
32409 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32410 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32412 cp_parser_error (parser
, "expected variable-name");
32413 clauses
= error_mark_node
;
32417 tree var_name
= cp_parser_id_expression (parser
, false, true, NULL
,
32419 tree decl
= cp_parser_lookup_name_simple (parser
, var_name
,
32421 if (decl
== error_mark_node
)
32423 cp_parser_name_lookup_error (parser
, var_name
, decl
, NLE_NULL
,
32425 clauses
= error_mark_node
;
32429 tree e
= NULL_TREE
;
32430 tree step_size
= integer_one_node
;
32432 /* If present, parse the linear step. Otherwise, assume the default
32434 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_COLON
)
32436 cp_lexer_consume_token (parser
->lexer
);
32438 e
= cp_parser_assignment_expression (parser
);
32439 e
= maybe_constant_value (e
);
32441 if (e
== error_mark_node
)
32443 /* If an error has occurred, then the whole pragma is
32444 considered ill-formed. Thus, no reason to keep
32446 clauses
= error_mark_node
;
32449 else if (type_dependent_expression_p (e
)
32450 || value_dependent_expression_p (e
)
32452 && INTEGRAL_TYPE_P (TREE_TYPE (e
))
32453 && (TREE_CONSTANT (e
)
32457 cp_parser_error (parser
,
32458 "step size must be an integer constant "
32459 "expression or an integer variable");
32462 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32463 tree l
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
32464 OMP_CLAUSE_DECL (l
) = decl
;
32465 OMP_CLAUSE_LINEAR_STEP (l
) = step_size
;
32466 OMP_CLAUSE_CHAIN (l
) = clauses
;
32469 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32470 cp_lexer_consume_token (parser
->lexer
);
32471 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32475 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32476 "expected %<,%> or %<)%> after %qE", decl
);
32477 clauses
= error_mark_node
;
32481 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
32482 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
32486 /* Returns the name of the next clause. If the clause is not
32487 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32488 token is not consumed. Otherwise, the appropriate enum from the
32489 pragma_simd_clause is returned and the token is consumed. */
32491 static pragma_omp_clause
32492 cp_parser_cilk_simd_clause_name (cp_parser
*parser
)
32494 pragma_omp_clause clause_type
;
32495 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32497 if (token
->keyword
== RID_PRIVATE
)
32498 clause_type
= PRAGMA_CILK_CLAUSE_PRIVATE
;
32499 else if (!token
->u
.value
|| token
->type
!= CPP_NAME
)
32500 return PRAGMA_CILK_CLAUSE_NONE
;
32501 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "vectorlength"))
32502 clause_type
= PRAGMA_CILK_CLAUSE_VECTORLENGTH
;
32503 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "linear"))
32504 clause_type
= PRAGMA_CILK_CLAUSE_LINEAR
;
32505 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "firstprivate"))
32506 clause_type
= PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
;
32507 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "lastprivate"))
32508 clause_type
= PRAGMA_CILK_CLAUSE_LASTPRIVATE
;
32509 else if (!strcmp (IDENTIFIER_POINTER (token
->u
.value
), "reduction"))
32510 clause_type
= PRAGMA_CILK_CLAUSE_REDUCTION
;
32512 return PRAGMA_CILK_CLAUSE_NONE
;
32514 cp_lexer_consume_token (parser
->lexer
);
32515 return clause_type
;
32518 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32521 cp_parser_cilk_simd_all_clauses (cp_parser
*parser
, cp_token
*pragma_token
)
32523 tree clauses
= NULL_TREE
;
32525 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
32526 && clauses
!= error_mark_node
)
32528 pragma_omp_clause c_kind
;
32529 c_kind
= cp_parser_cilk_simd_clause_name (parser
);
32530 if (c_kind
== PRAGMA_CILK_CLAUSE_VECTORLENGTH
)
32531 clauses
= cp_parser_cilk_simd_vectorlength (parser
, clauses
, false);
32532 else if (c_kind
== PRAGMA_CILK_CLAUSE_LINEAR
)
32533 clauses
= cp_parser_cilk_simd_linear (parser
, clauses
);
32534 else if (c_kind
== PRAGMA_CILK_CLAUSE_PRIVATE
)
32535 /* Use the OpenMP 4.0 equivalent function. */
32536 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
, clauses
);
32537 else if (c_kind
== PRAGMA_CILK_CLAUSE_FIRSTPRIVATE
)
32538 /* Use the OpenMP 4.0 equivalent function. */
32539 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
32541 else if (c_kind
== PRAGMA_CILK_CLAUSE_LASTPRIVATE
)
32542 /* Use the OMP 4.0 equivalent function. */
32543 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LASTPRIVATE
,
32545 else if (c_kind
== PRAGMA_CILK_CLAUSE_REDUCTION
)
32546 /* Use the OMP 4.0 equivalent function. */
32547 clauses
= cp_parser_omp_clause_reduction (parser
, clauses
);
32550 clauses
= error_mark_node
;
32551 cp_parser_error (parser
, "expected %<#pragma simd%> clause");
32556 cp_parser_skip_to_pragma_eol (parser
, pragma_token
);
32558 if (clauses
== error_mark_node
)
32559 return error_mark_node
;
32561 return c_finish_cilk_clauses (clauses
);
32564 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32567 cp_parser_cilk_simd (cp_parser
*parser
, cp_token
*pragma_token
)
32569 tree clauses
= cp_parser_cilk_simd_all_clauses (parser
, pragma_token
);
32571 if (clauses
== error_mark_node
)
32574 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_FOR
))
32576 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32577 "for statement expected");
32581 tree sb
= begin_omp_structured_block ();
32582 int save
= cp_parser_begin_omp_structured_block (parser
);
32583 tree ret
= cp_parser_omp_for_loop (parser
, CILK_SIMD
, clauses
, NULL
);
32585 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret
));
32586 cp_parser_end_omp_structured_block (parser
, save
);
32587 add_stmt (finish_omp_structured_block (sb
));
32590 /* Main entry-point for parsing Cilk Plus _Cilk_for
32591 loops. The return value is error_mark_node
32592 when errors happen and CILK_FOR tree on success. */
32595 cp_parser_cilk_for (cp_parser
*parser
, tree grain
)
32597 if (cp_lexer_next_token_is_not_keyword (parser
->lexer
, RID_CILK_FOR
))
32598 gcc_unreachable ();
32600 tree sb
= begin_omp_structured_block ();
32601 int save
= cp_parser_begin_omp_structured_block (parser
);
32603 tree clauses
= build_omp_clause (EXPR_LOCATION (grain
), OMP_CLAUSE_SCHEDULE
);
32604 OMP_CLAUSE_SCHEDULE_KIND (clauses
) = OMP_CLAUSE_SCHEDULE_CILKFOR
;
32605 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses
) = grain
;
32606 clauses
= finish_omp_clauses (clauses
);
32608 tree ret
= cp_parser_omp_for_loop (parser
, CILK_FOR
, clauses
, NULL
);
32610 cpp_validate_cilk_plus_loop (ret
);
32612 ret
= error_mark_node
;
32614 cp_parser_end_omp_structured_block (parser
, save
);
32615 add_stmt (finish_omp_structured_block (sb
));
32619 /* Create an identifier for a generic parameter type (a synthesized
32620 template parameter implied by `auto' or a concept identifier). */
32622 static GTY(()) int generic_parm_count
;
32624 make_generic_type_name ()
32627 sprintf (buf
, "auto:%d", ++generic_parm_count
);
32628 return get_identifier (buf
);
32631 /* Predicate that behaves as is_auto_or_concept but matches the parent
32632 node of the generic type rather than the generic type itself. This
32633 allows for type transformation in add_implicit_template_parms. */
32636 tree_type_is_auto_or_concept (const_tree t
)
32638 return TREE_TYPE (t
) && is_auto_or_concept (TREE_TYPE (t
));
32641 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32642 (creating a new template parameter list if necessary). Returns the newly
32643 created template type parm. */
32646 synthesize_implicit_template_parm (cp_parser
*parser
)
32648 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
32650 /* We are either continuing a function template that already contains implicit
32651 template parameters, creating a new fully-implicit function template, or
32652 extending an existing explicit function template with implicit template
32655 cp_binding_level
*const entry_scope
= current_binding_level
;
32657 bool become_template
= false;
32658 cp_binding_level
*parent_scope
= 0;
32660 if (parser
->implicit_template_scope
)
32662 gcc_assert (parser
->implicit_template_parms
);
32664 current_binding_level
= parser
->implicit_template_scope
;
32668 /* Roll back to the existing template parameter scope (in the case of
32669 extending an explicit function template) or introduce a new template
32670 parameter scope ahead of the function parameter scope (or class scope
32671 in the case of out-of-line member definitions). The function scope is
32672 added back after template parameter synthesis below. */
32674 cp_binding_level
*scope
= entry_scope
;
32676 while (scope
->kind
== sk_function_parms
)
32678 parent_scope
= scope
;
32679 scope
= scope
->level_chain
;
32681 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
32683 /* If not defining a class, then any class scope is a scope level in
32684 an out-of-line member definition. In this case simply wind back
32685 beyond the first such scope to inject the template parameter list.
32686 Otherwise wind back to the class being defined. The latter can
32687 occur in class member friend declarations such as:
32693 friend void A::foo (auto);
32696 The template parameter list synthesized for the friend declaration
32697 must be injected in the scope of 'B'. This can also occur in
32698 erroneous cases such as:
32704 void B::foo (auto) {}
32707 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32708 but, nevertheless, the template parameter list synthesized for the
32709 declarator should be injected into the scope of 'A' as if the
32710 ill-formed template was specified explicitly. */
32712 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
32714 parent_scope
= scope
;
32715 scope
= scope
->level_chain
;
32719 current_binding_level
= scope
;
32721 if (scope
->kind
!= sk_template_parms
32722 || !function_being_declared_is_template_p (parser
))
32724 /* Introduce a new template parameter list for implicit template
32727 become_template
= true;
32729 parser
->implicit_template_scope
32730 = begin_scope (sk_template_parms
, NULL
);
32732 ++processing_template_decl
;
32734 parser
->fully_implicit_function_template_p
= true;
32735 ++parser
->num_template_parameter_lists
;
32739 /* Synthesize implicit template parameters at the end of the explicit
32740 template parameter list. */
32742 gcc_assert (current_template_parms
);
32744 parser
->implicit_template_scope
= scope
;
32746 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32747 parser
->implicit_template_parms
32748 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
32752 /* Synthesize a new template parameter and track the current template
32753 parameter chain with implicit_template_parms. */
32755 tree synth_id
= make_generic_type_name ();
32756 tree synth_tmpl_parm
= finish_template_type_parm (class_type_node
,
32759 = process_template_parm (parser
->implicit_template_parms
,
32761 build_tree_list (NULL_TREE
, synth_tmpl_parm
),
32762 /*non_type=*/false,
32763 /*param_pack=*/false);
32766 if (parser
->implicit_template_parms
)
32767 parser
->implicit_template_parms
32768 = TREE_CHAIN (parser
->implicit_template_parms
);
32770 parser
->implicit_template_parms
= new_parm
;
32772 tree new_type
= TREE_TYPE (getdecls ());
32774 /* If creating a fully implicit function template, start the new implicit
32775 template parameter list with this synthesized type, otherwise grow the
32776 current template parameter list. */
32778 if (become_template
)
32780 parent_scope
->level_chain
= current_binding_level
;
32782 tree new_parms
= make_tree_vec (1);
32783 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
32784 current_template_parms
= tree_cons (size_int (processing_template_decl
),
32785 new_parms
, current_template_parms
);
32789 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
32790 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
32791 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
32792 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
32795 current_binding_level
= entry_scope
;
32800 /* Finish the declaration of a fully implicit function template. Such a
32801 template has no explicit template parameter list so has not been through the
32802 normal template head and tail processing. synthesize_implicit_template_parm
32803 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32804 provided if the declaration is a class member such that its template
32805 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32806 form is returned. Otherwise NULL_TREE is returned. */
32809 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
32811 gcc_assert (parser
->fully_implicit_function_template_p
);
32813 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
32814 && DECL_VIRTUAL_P (member_decl_opt
))
32816 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
32817 "implicit templates may not be %<virtual%>");
32818 DECL_VIRTUAL_P (member_decl_opt
) = false;
32821 if (member_decl_opt
)
32822 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
32823 end_template_decl ();
32825 parser
->fully_implicit_function_template_p
= false;
32826 --parser
->num_template_parameter_lists
;
32828 return member_decl_opt
;
32831 #include "gt-cp-parser.h"